@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 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 saveConfig = async () => {
27
- throw Object.assign(new Error('dsh-model-sync settings are not writable'), { code: 'CONFIG_UNAVAILABLE' })
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
- const settings = ctx.get('settings')
30
- if (settings?.settings?.register) {
31
- const scope = settings.settings.register(NS, Config, { base: config })
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 watch = scope.watch ?? scope.onChange
34
- ctx.effect(() => watch?.call(scope, (next) => {
41
+ const stop = scope.watch((next) => {
35
42
  liveConfig = Config(next || config)
36
43
  reconfigure()
37
- }), 'dsh-model-sync: settings')
38
- saveConfig = async (patch) => {
39
- if (typeof scope.update === 'function') return scope.update(patch)
40
- const next = { ...scope.get(), ...patch }
41
- if (typeof scope.replace === 'function') return scope.replace(next)
42
- throw Object.assign(new Error('dsh-model-sync settings are not writable'), { code: 'CONFIG_UNAVAILABLE' })
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 })
@@ -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
- await settings.replace(PIAI_NS, next, descriptor.revision)
153
- const modelSelections = { ...(config.modelSelections ?? {}), [provider]: selectedModels }
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
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@goodandready/dsh-model-sync",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
4
4
  "description": "Automatic model catalog synchronization for API-key DeepSeek Harness providers.",
5
5
  "keywords": [
6
6
  "deepseek-harness",