@aiwayds/dsh-model-sync 0.9.0 → 0.10.0

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.
Files changed (66) hide show
  1. package/README.md +8 -3
  2. package/README.zh-CN.md +8 -3
  3. package/lib/builtin-catalog-snapshot.js +6 -3
  4. package/lib/builtin-catalog-snapshot.js.map +1 -1
  5. package/lib/index.d.ts +366 -0
  6. package/lib/index.js +729 -13
  7. package/lib/index.js.map +1 -1
  8. package/lib/opencode-go-diagnose.d.ts +74 -0
  9. package/lib/opencode-go-diagnose.js +146 -0
  10. package/lib/opencode-go-diagnose.js.map +1 -0
  11. package/lib/opencode-go-grouping.d.ts +29 -0
  12. package/lib/opencode-go-grouping.js +77 -0
  13. package/lib/opencode-go-grouping.js.map +1 -0
  14. package/lib/opencode-go-journal.d.ts +110 -0
  15. package/lib/opencode-go-journal.js +406 -0
  16. package/lib/opencode-go-journal.js.map +1 -0
  17. package/lib/opencode-go-migrate.d.ts +343 -0
  18. package/lib/opencode-go-migrate.js +1300 -0
  19. package/lib/opencode-go-migrate.js.map +1 -0
  20. package/lib/opencode-go-migration.d.ts +227 -0
  21. package/lib/opencode-go-migration.js +384 -0
  22. package/lib/opencode-go-migration.js.map +1 -0
  23. package/lib/opencode-go-planner.d.ts +153 -0
  24. package/lib/opencode-go-planner.js +339 -0
  25. package/lib/opencode-go-planner.js.map +1 -0
  26. package/lib/opencode-go-profile.d.ts +57 -0
  27. package/lib/opencode-go-profile.js +103 -0
  28. package/lib/opencode-go-profile.js.map +1 -0
  29. package/lib/opencode-go-scheduler.d.ts +128 -0
  30. package/lib/opencode-go-scheduler.js +241 -0
  31. package/lib/opencode-go-scheduler.js.map +1 -0
  32. package/lib/opencode-go-source.d.ts +117 -0
  33. package/lib/opencode-go-source.js +331 -0
  34. package/lib/opencode-go-source.js.map +1 -0
  35. package/lib/opencode-go-store.d.ts +18 -0
  36. package/lib/opencode-go-store.js +68 -0
  37. package/lib/opencode-go-store.js.map +1 -0
  38. package/lib/opencode-go-sync.d.ts +94 -0
  39. package/lib/opencode-go-sync.js +364 -0
  40. package/lib/opencode-go-sync.js.map +1 -0
  41. package/lib/opencode-go-writer.d.ts +132 -0
  42. package/lib/opencode-go-writer.js +966 -0
  43. package/lib/opencode-go-writer.js.map +1 -0
  44. package/lib/provider-native.d.ts +3 -1
  45. package/lib/provider-native.js +4 -2
  46. package/lib/provider-native.js.map +1 -1
  47. package/lib/remote-catalog.d.ts +37 -0
  48. package/lib/remote-catalog.js +261 -3
  49. package/lib/remote-catalog.js.map +1 -1
  50. package/package.json +15 -15
  51. package/src/builtin-catalog-snapshot.ts +6 -3
  52. package/src/index.ts +873 -12
  53. package/src/opencode-go-diagnose.ts +222 -0
  54. package/src/opencode-go-grouping.ts +97 -0
  55. package/src/opencode-go-journal.ts +458 -0
  56. package/src/opencode-go-migrate.ts +1694 -0
  57. package/src/opencode-go-migration.ts +552 -0
  58. package/src/opencode-go-planner.ts +505 -0
  59. package/src/opencode-go-profile.ts +114 -0
  60. package/src/opencode-go-scheduler.ts +315 -0
  61. package/src/opencode-go-source.ts +424 -0
  62. package/src/opencode-go-store.ts +73 -0
  63. package/src/opencode-go-sync.ts +503 -0
  64. package/src/opencode-go-writer.ts +1189 -0
  65. package/src/provider-native.ts +3 -1
  66. package/src/remote-catalog.ts +293 -3
@@ -0,0 +1,222 @@
1
+ // P0 read-only diagnostic: compare the official opencode-go model set with
2
+ // the current profile's opencode-go route configuration and produce a
3
+ // structured report (added / missing / protocol ownership / entitlement).
4
+ //
5
+ // Hard boundary: this module only READS a settings descriptor. It never calls
6
+ // settings.mutate, never writes credentials, and never touches a profile file.
7
+ // Existing sync rounds and /model-sync are untouched by this code.
8
+ //
9
+ // Design: 07-design.md §6.3 (status), §16 P0, C7, final review §1.7.
10
+
11
+ import {
12
+ fetchGoOfficialModels,
13
+ type GoFetchLike,
14
+ type GoSourceResult,
15
+ type GoAuthStatus,
16
+ type GoSourceError,
17
+ } from './opencode-go-source.ts'
18
+ import {
19
+ GO_SLOT_ROUTES,
20
+ groupGoModelsByProtocol,
21
+ type GoGroupingResult,
22
+ type GoRouteSlot,
23
+ } from './opencode-go-grouping.ts'
24
+
25
+ /** Minimal settings-descriptor shape the diagnostic reads (no writer import). */
26
+ export interface GoSettingsDescriptor {
27
+ user?: Record<string, unknown>
28
+ }
29
+
30
+ /** One route's currently configured model ids, read from the profile. */
31
+ export interface GoRouteConfig {
32
+ slot: GoRouteSlot
33
+ route: string
34
+ configuredIds: string[]
35
+ }
36
+
37
+ /** The per-slot comparison in the report. */
38
+ export interface GoSlotDiff {
39
+ slot: GoRouteSlot
40
+ route: string
41
+ /** Ids the official entitled source places in this slot. */
42
+ official: string[]
43
+ /** Ids currently listed under this route in the profile. */
44
+ configured: string[]
45
+ /** Official ids absent from the profile. */
46
+ added: string[]
47
+ /** Profile ids absent from the official entitled set. */
48
+ missing: string[]
49
+ }
50
+
51
+ /** Structured diagnostic report emitted by P0. */
52
+ export interface GoDiagnosticReport {
53
+ source: 'opencode-go-official'
54
+ generatedAt: number
55
+ authStatus: GoAuthStatus
56
+ keyFingerprint: string
57
+ payloadHash?: string
58
+ error?: GoSourceError
59
+ routes: GoSlotDiff[]
60
+ unassigned: GoGroupingResult['unassigned']
61
+ }
62
+
63
+ /** Inputs to buildGoDiagnosticReport — all already-resolved (pure). */
64
+ export interface BuildGoDiagnosticInput {
65
+ source: GoSourceResult
66
+ grouping: GoGroupingResult
67
+ routes: readonly GoRouteConfig[]
68
+ generatedAt?: number
69
+ }
70
+
71
+ // ---------------------------------------------------------------------------
72
+ // Read-only profile extraction
73
+ // ---------------------------------------------------------------------------
74
+
75
+ function isRecord(value: unknown): value is Record<string, unknown> {
76
+ return typeof value === 'object' && value !== null && !Array.isArray(value)
77
+ }
78
+
79
+ function slotFromRouteId(route: string): GoRouteSlot | undefined {
80
+ if (route === GO_SLOT_ROUTES.chat) return 'chat'
81
+ if (route === GO_SLOT_ROUTES.msg) return 'msg'
82
+ if (route === GO_SLOT_ROUTES.resp) return 'resp'
83
+ if (route === GO_SLOT_ROUTES.google) return 'google'
84
+ return undefined
85
+ }
86
+
87
+ function toRouteConfig(slot: GoRouteSlot, route: string, providers: Record<string, unknown>): GoRouteConfig {
88
+ const routeData = providers[route]
89
+ const models = isRecord(routeData) ? routeData.models : undefined
90
+ const configuredIds = Array.isArray(models)
91
+ ? models
92
+ .map((entry) => isRecord(entry) && typeof entry.id === 'string' ? entry.id : undefined)
93
+ .filter((id): id is string => id !== undefined)
94
+ : []
95
+ return { slot, route, configuredIds }
96
+ }
97
+
98
+ /**
99
+ * Read the profile's opencode-go route model ids from a settings descriptor.
100
+ * The four default slots are always present (possibly empty); any additional
101
+ * `opencode-go*` route carrying a recognizable slot suffix is included too.
102
+ * This is a pure read — the descriptor is never modified.
103
+ */
104
+ export function readGoRoutesFromDescriptor(desc: GoSettingsDescriptor | undefined): GoRouteConfig[] {
105
+ const providers = desc?.user?.providers
106
+ const table = isRecord(providers) ? providers : {}
107
+ const configs: GoRouteConfig[] = []
108
+ const seen = new Set<string>()
109
+
110
+ for (const slot of ['chat', 'msg', 'resp', 'google'] as const) {
111
+ const route = GO_SLOT_ROUTES[slot]
112
+ configs.push(toRouteConfig(slot, route, table))
113
+ seen.add(route)
114
+ }
115
+ for (const route of Object.keys(table)) {
116
+ if (!route.startsWith('opencode-go') || seen.has(route)) continue
117
+ const slot = slotFromRouteId(route)
118
+ if (slot === undefined) continue
119
+ configs.push(toRouteConfig(slot, route, table))
120
+ }
121
+ return configs
122
+ }
123
+
124
+ // ---------------------------------------------------------------------------
125
+ // Pure diff + report
126
+ // ---------------------------------------------------------------------------
127
+
128
+ function missingFrom(left: readonly string[], right: readonly string[]): string[] {
129
+ const rightSet = new Set(right)
130
+ return left.filter((id) => !rightSet.has(id))
131
+ }
132
+
133
+ /**
134
+ * Build the structured report. When the source read failed (no key, auth,
135
+ entitlement, transport), the official side is empty and the diff is suppressed
136
+ * — reporting every configured model as "missing" from a failed read would be
137
+ * misleading. The failure is carried in authStatus / error instead.
138
+ */
139
+ export function buildGoDiagnosticReport(input: BuildGoDiagnosticInput): GoDiagnosticReport {
140
+ const available = input.source.ok
141
+ const routes: GoSlotDiff[] = input.routes.map((config) => {
142
+ const official = available ? (input.grouping.slots[config.slot] ?? []) : []
143
+ const configured = config.configuredIds
144
+ return {
145
+ slot: config.slot,
146
+ route: config.route,
147
+ official,
148
+ configured,
149
+ added: available ? missingFrom(official, configured) : [],
150
+ missing: available ? missingFrom(configured, official) : [],
151
+ }
152
+ })
153
+
154
+ const report: GoDiagnosticReport = {
155
+ source: 'opencode-go-official',
156
+ generatedAt: input.generatedAt ?? Date.now(),
157
+ authStatus: input.source.authStatus,
158
+ keyFingerprint: input.source.keyFingerprint,
159
+ routes,
160
+ unassigned: available ? input.grouping.unassigned : [],
161
+ }
162
+ if (input.source.payloadHash !== undefined) report.payloadHash = input.source.payloadHash
163
+ if (input.source.error !== undefined) report.error = input.source.error
164
+ return report
165
+ }
166
+
167
+ /** Human-readable rendering of the structured report (command output). */
168
+ export function formatGoDiagnosticReport(report: GoDiagnosticReport): string {
169
+ const lines: string[] = [
170
+ 'opencode-go official /v1/models diagnostic (read-only; no settings written)',
171
+ ` authStatus: ${report.authStatus}`,
172
+ ` keyFingerprint: ${report.keyFingerprint}`,
173
+ ]
174
+ if (report.payloadHash !== undefined) lines.push(` payloadHash: ${report.payloadHash}`)
175
+ if (report.error !== undefined) {
176
+ lines.push(` status: ${report.error.kind}${report.error.status !== undefined ? ` (HTTP ${report.error.status})` : ''}`)
177
+ lines.push(` reason: ${report.error.message}`)
178
+ }
179
+ for (const slot of report.routes) {
180
+ lines.push(
181
+ ` ${slot.route} [${slot.slot}]: official ${slot.official.length}, configured ${slot.configured.length}`
182
+ + `; +${slot.added.join(', +') || 'none'}`
183
+ + `; -${slot.missing.join(', -') || 'none'}`,
184
+ )
185
+ }
186
+ if (report.unassigned.length > 0) {
187
+ lines.push(` unassigned: ${report.unassigned.map((u) => `${u.id} (${u.reason})`).join(', ')}`)
188
+ }
189
+ return lines.join('\n')
190
+ }
191
+
192
+ // ---------------------------------------------------------------------------
193
+ // Orchestration (the only async entry; network stays injectable for tests)
194
+ // ---------------------------------------------------------------------------
195
+
196
+ /** Options for one diagnostic run. */
197
+ export interface RunGoDiagnosticOptions {
198
+ apiKey?: string
199
+ descriptor?: GoSettingsDescriptor | undefined
200
+ url?: string
201
+ timeoutMs?: number
202
+ fetchImpl?: GoFetchLike
203
+ generatedAt?: number
204
+ }
205
+
206
+ /**
207
+ * Run one read-only diagnostic: fetch official models, group by protocol,
208
+ * read the profile's route config, and return the structured report.
209
+ * Pass `fetchImpl` in tests so no network is ever touched.
210
+ */
211
+ export async function runGoDiagnostic(options: RunGoDiagnosticOptions = {}): Promise<GoDiagnosticReport> {
212
+ const source = await fetchGoOfficialModels({
213
+ apiKey: options.apiKey,
214
+ url: options.url,
215
+ timeoutMs: options.timeoutMs,
216
+ fetchImpl: options.fetchImpl,
217
+ observedAt: options.generatedAt,
218
+ })
219
+ const grouping = groupGoModelsByProtocol(source.entries)
220
+ const routes = readGoRoutesFromDescriptor(options.descriptor)
221
+ return buildGoDiagnosticReport({ source, grouping, routes, generatedAt: options.generatedAt })
222
+ }
@@ -0,0 +1,97 @@
1
+ // P0 protocol grouping: official api-dictionary keys → route slots.
2
+ //
3
+ // Pure logic (07-design.md §6.2.3). P0 uses it for the diagnostic's protocol
4
+ // ownership column; P1 will reuse it for route planning. No settings/IO here.
5
+
6
+ import type { GoModelRecord } from './opencode-go-source.ts'
7
+
8
+ /** The four route slots of the go topology (design §6.2.2). */
9
+ export type GoRouteSlot = 'chat' | 'msg' | 'resp' | 'google'
10
+
11
+ export const GO_ROUTE_SLOTS: readonly GoRouteSlot[] = ['chat', 'msg', 'resp', 'google']
12
+
13
+ /** Default route id per slot (design §6.2.6). */
14
+ export const GO_SLOT_ROUTES: Readonly<Record<GoRouteSlot, string>> = {
15
+ chat: 'opencode-go',
16
+ msg: 'opencode-go-msg',
17
+ resp: 'opencode-go-resp',
18
+ google: 'opencode-go-google',
19
+ }
20
+
21
+ /** Official protocol names this plugin knows how to place. */
22
+ export const GO_KNOWN_PROTOCOLS: ReadonlySet<string> = new Set([
23
+ 'chat-completions',
24
+ 'messages',
25
+ 'responses',
26
+ 'interactions',
27
+ 'generate-content',
28
+ ])
29
+
30
+ export interface GoUnassignedModel {
31
+ id: string
32
+ protocols: string[]
33
+ reason: 'unsupported-protocol' | 'unknown-protocol'
34
+ }
35
+
36
+ export interface GoGroupingResult {
37
+ slots: Record<GoRouteSlot, string[]>
38
+ /** Models that must not be written anywhere, with the reason why. */
39
+ unassigned: GoUnassignedModel[]
40
+ }
41
+
42
+ /** Map an official protocol key to a route slot. */
43
+ export function slotForProtocol(protocol: string): GoRouteSlot | undefined {
44
+ switch (protocol) {
45
+ case 'chat-completions': return 'chat'
46
+ case 'messages': return 'msg'
47
+ case 'responses': return 'resp'
48
+ case 'interactions':
49
+ case 'generate-content': return 'google'
50
+ default: return undefined
51
+ }
52
+ }
53
+
54
+ /**
55
+ * Group official model ids by protocol (design §6.2.3):
56
+ * - chat-completions present → chat (wins over any other protocol)
57
+ * - exactly {messages} → msg; exactly {responses} → resp
58
+ * - subset of {interactions, generate-content} → google
59
+ * - multiple non-chat protocols → recommended_api breaks the tie
60
+ * - empty / unknown / otherwise unsupported → unassigned (reported, never written)
61
+ */
62
+ export function groupGoModelsByProtocol(entries: readonly GoModelRecord[]): GoGroupingResult {
63
+ const slots: Record<GoRouteSlot, string[]> = { chat: [], msg: [], resp: [], google: [] }
64
+ const unassigned: GoUnassignedModel[] = []
65
+
66
+ for (const entry of entries) {
67
+ const protocols = entry.protocols
68
+ const unknown = protocols.filter((protocol) => !GO_KNOWN_PROTOCOLS.has(protocol))
69
+ if (unknown.length > 0) {
70
+ unassigned.push({ id: entry.id, protocols: [...protocols], reason: 'unknown-protocol' })
71
+ continue
72
+ }
73
+
74
+ let slot: GoRouteSlot | undefined
75
+ if (protocols.length === 0) {
76
+ // no placement
77
+ } else if (protocols.includes('chat-completions')) {
78
+ slot = 'chat'
79
+ } else if (protocols.length === 1 && protocols[0] === 'messages') {
80
+ slot = 'msg'
81
+ } else if (protocols.length === 1 && protocols[0] === 'responses') {
82
+ slot = 'resp'
83
+ } else if (protocols.every((p) => p === 'interactions' || p === 'generate-content')) {
84
+ slot = 'google'
85
+ } else if (protocols.length > 1 && entry.recommendedProtocol !== undefined) {
86
+ slot = slotForProtocol(entry.recommendedProtocol)
87
+ }
88
+
89
+ if (slot === undefined) {
90
+ unassigned.push({ id: entry.id, protocols: [...protocols], reason: 'unsupported-protocol' })
91
+ continue
92
+ }
93
+ slots[slot].push(entry.id)
94
+ }
95
+
96
+ return { slots, unassigned }
97
+ }