@goodandready/dsh-model-sync 0.2.1 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.js +26 -14
- package/lib/synchronizer.js +15 -2
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -23,25 +23,37 @@ export const Config = Schema.object({
|
|
|
23
23
|
export function apply(ctx, config) {
|
|
24
24
|
let liveConfig = Config(structuredClone(config || {}))
|
|
25
25
|
let reconfigure = () => {}
|
|
26
|
-
let
|
|
27
|
-
|
|
26
|
+
let saveConfigImpl
|
|
27
|
+
// ctx.inject starts a child fiber; keep a stable function reference for the
|
|
28
|
+
// synchronizer while that fiber connects to the Settings service.
|
|
29
|
+
const saveConfig = async (patch) => {
|
|
30
|
+
if (!saveConfigImpl) {
|
|
31
|
+
throw Object.assign(new Error('dsh-model-sync settings are not writable'), { code: 'CONFIG_UNAVAILABLE' })
|
|
32
|
+
}
|
|
33
|
+
return saveConfigImpl(patch)
|
|
28
34
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
35
|
+
|
|
36
|
+
// The settings capability is injected as sctx.settings; ctx.get('settings')
|
|
37
|
+
// returns the provider service and does not expose the register API.
|
|
38
|
+
ctx.inject(['settings'], (sctx) => {
|
|
39
|
+
const scope = sctx.settings.register(NS, Config, { base: config })
|
|
32
40
|
liveConfig = Config(scope.get() || config)
|
|
33
|
-
const
|
|
34
|
-
ctx.effect(() => watch?.call(scope, (next) => {
|
|
41
|
+
const stop = scope.watch((next) => {
|
|
35
42
|
liveConfig = Config(next || config)
|
|
36
43
|
reconfigure()
|
|
37
|
-
})
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
44
|
+
})
|
|
45
|
+
saveConfigImpl = async (patch) => {
|
|
46
|
+
const result = await scope.update(patch)
|
|
47
|
+
// Settings watchers are serialized asynchronously; refresh the snapshot
|
|
48
|
+
// synchronously as well so a following request sees the new allowlist.
|
|
49
|
+
liveConfig = Config(scope.get() || config)
|
|
50
|
+
return result
|
|
43
51
|
}
|
|
44
|
-
|
|
52
|
+
sctx.effect(() => () => {
|
|
53
|
+
saveConfigImpl = undefined
|
|
54
|
+
stop()
|
|
55
|
+
}, 'dsh-model-sync: settings')
|
|
56
|
+
})
|
|
45
57
|
|
|
46
58
|
const service = createProviderService(ctx)
|
|
47
59
|
const synchronizer = createModelSynchronizer(ctx, { getConfig: () => liveConfig, saveConfig })
|
package/lib/synchronizer.js
CHANGED
|
@@ -149,9 +149,22 @@ export function createModelSynchronizer(ctx, { getConfig = () => ({}), saveConfi
|
|
|
149
149
|
? selectedModels.map((id) => availableById.get(id))
|
|
150
150
|
: (available.length > 0 ? available : current)
|
|
151
151
|
const next = catalogPatch(section, provider, nextModels)
|
|
152
|
-
|
|
153
|
-
const modelSelections = { ...
|
|
152
|
+
const previousModelSelections = structuredClone(config.modelSelections ?? {})
|
|
153
|
+
const modelSelections = { ...previousModelSelections, [provider]: selectedModels }
|
|
154
|
+
|
|
155
|
+
// Persist the allowlist first. If the LLM catalog write fails, roll the
|
|
156
|
+
// allowlist back so the two namespaces cannot be left disagreeing.
|
|
154
157
|
await saveConfig({ modelSelections })
|
|
158
|
+
try {
|
|
159
|
+
await settings.replace(PIAI_NS, next, descriptor.revision)
|
|
160
|
+
} catch (error) {
|
|
161
|
+
try {
|
|
162
|
+
await saveConfig({ modelSelections: previousModelSelections })
|
|
163
|
+
} catch {
|
|
164
|
+
// Preserve the original, more actionable catalog-write error.
|
|
165
|
+
}
|
|
166
|
+
throw error
|
|
167
|
+
}
|
|
155
168
|
return { provider, selectedModels, models: nextModels, availableModels: available, applied: true }
|
|
156
169
|
}
|
|
157
170
|
|