Skip to content

Commit

Permalink
refactor(bridge/nodejs): add key to setting.isAlreadySet
Browse files Browse the repository at this point in the history
  • Loading branch information
louistiti committed May 27, 2023
1 parent 2e6abef commit 1f9137b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 21 deletions.
16 changes: 13 additions & 3 deletions bridges/nodejs/src/sdk/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@ export class Settings<T extends Record<string, unknown>> {

/**
* Check if the settings are already set
* @returns isAlreadySet() // true
* @param key The key to verify whether its value is set
* @returns isAlreadySet('apiKey') // true
*/
public async isAlreadySet(): Promise<boolean> {
public async isAlreadySet(key: string): Promise<boolean> {
const settingsSample = await this.getSettingsSample()
const settings = await this.get()
return JSON.stringify(settings) !== JSON.stringify(settingsSample)

return (
!!settings[key] &&
JSON.stringify(settings[key]) !== JSON.stringify(settingsSample[key])
)
}

/**
Expand All @@ -32,6 +37,7 @@ export class Settings<T extends Record<string, unknown>> {
*/
public async clear(): Promise<void> {
const settingsSample = await this.getSettingsSample()

await this.set(settingsSample)
}

Expand All @@ -45,6 +51,7 @@ export class Settings<T extends Record<string, unknown>> {
`Error while reading settings sample at "${this.settingsSamplePath}":`,
e
)

throw e
}
}
Expand All @@ -66,9 +73,11 @@ export class Settings<T extends Record<string, unknown>> {
const settings = JSON.parse(
await fs.promises.readFile(this.settingsPath, 'utf8')
)

if (key != null) {
return settings[key]
}

return settings
} catch (e) {
console.error(
Expand Down Expand Up @@ -109,6 +118,7 @@ export class Settings<T extends Record<string, unknown>> {
`Error while writing settings at "${this.settingsPath}":`,
e
)

throw e
}
}
Expand Down
34 changes: 17 additions & 17 deletions skills/leon/age/src/actions/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,26 +92,26 @@ export const run: ActionFunction = async function () {
}
})

const settings = new Settings<{
someSampleConfig: string
}>()
await leon.answer({
key: 'answer',
data: {
answer: `Skill settings already set: ${await settings.isAlreadySet()}`
}
})
const settings = new Settings()

if (!(await settings.isAlreadySet('apiKey'))) {
await leon.answer({
key: 'answer',
data: {
answer: "The API key isn't set..."
}
})
}

const currentSettings = await settings.get()

await settings.set({
someSampleConfig: 'Hello world'
...currentSettings,
apiKey: 'newAPIKey'
})
await settings.set('someSampleConfig', 'Hello world 2')
const options = await settings.get()
const someSampleConfig = await settings.get('someSampleConfig')

await leon.answer({
key: 'answer',
data: {
answer: options.someSampleConfig + someSampleConfig
}
key: `Is API set now? ${await settings.isAlreadySet('apiKey')}`
})

const network = new Network({
Expand Down
3 changes: 2 additions & 1 deletion skills/leon/age/src/settings.sample.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"someSampleConfig": "someSampleValue"
"someSampleConfig": "someSampleValue",
"apiKey": "YOUR_API_KEY"
}

0 comments on commit 1f9137b

Please sign in to comment.