@goodandready/dsh-model-sync 0.2.14 → 0.3.1

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/http.js CHANGED
@@ -1,204 +1,214 @@
1
- import { validatePolicy } from './policy.js'
2
-
3
- const BASE = '/dsh-model-sync'
4
-
5
- function json(res, status, body) {
6
- res.writeHead(status, { 'content-type': 'application/json; charset=utf-8' })
7
- res.end(JSON.stringify(body))
8
- }
9
-
10
- function trusted(req) {
11
- if (req.headers?.['sec-fetch-site'] === 'cross-site') return false
12
- const origin = req.headers?.origin
13
- if (!origin) return true
14
- const host = req.headers?.host
15
- if (!host) return false
16
- try { return new URL(origin).host === host } catch { return false }
17
- }
18
-
19
- function readJson(req) {
20
- return new Promise((resolve, reject) => {
21
- let raw = ''
22
- req.on('data', (chunk) => {
23
- raw += chunk
24
- if (raw.length > 64 * 1024) reject(new Error('request body is too large'))
25
- })
26
- req.on('end', () => {
27
- try { resolve(JSON.parse(raw || '{}')) } catch (error) { reject(error) }
28
- })
29
- req.on('error', reject)
30
- })
31
- }
32
-
33
- export function normalizeRunRequest(body) {
34
- if (body === null || typeof body !== 'object' || Array.isArray(body)) throw new Error('request body must be an object')
35
- if (body.provider !== undefined && (typeof body.provider !== 'string' || body.provider.length > 128)) throw new Error('provider must be a short string')
36
- if (body.dryRun !== undefined && typeof body.dryRun !== 'boolean') throw new Error('dryRun must be boolean')
37
- if (body.removeMissing !== undefined && typeof body.removeMissing !== 'boolean') throw new Error('removeMissing must be boolean')
38
- return {
39
- ...(body.provider ? { provider: body.provider } : {}),
40
- dryRun: body.dryRun !== false,
41
- removeMissing: body.removeMissing === true,
42
- }
43
- }
44
-
45
- export function normalizeCredentialRequest(url = '') {
46
- const parsed = new URL(url || '/', 'http://dsh-model-sync.local')
47
- const provider = parsed.searchParams.get('provider') || undefined
48
- if (provider && (provider.length > 128 || !/^[A-Za-z0-9._:-]+$/.test(provider))) throw new Error('provider must be a short string')
49
- return provider ? { provider } : {}
50
- }
51
-
52
- export function normalizeNotificationRequest(url = '') {
53
- const parsed = new URL(url || '/', 'http://dsh-model-sync.local')
54
- return { includeAcknowledged: parsed.searchParams.get('includeAcknowledged') !== 'false' }
55
- }
56
-
57
- export function normalizeNotificationAction(body) {
58
- if (body === null || typeof body !== 'object' || Array.isArray(body)) throw new Error('request body must be an object')
59
- if (typeof body.id !== 'string' || !body.id || body.id.length > 128) throw new Error('notification id is invalid')
60
- return { id: body.id.trim() }
61
- }
62
-
63
- export function normalizeCredentialCheckRequest(body) {
64
- if (body === null || typeof body !== 'object' || Array.isArray(body)) throw new Error('request body must be an object')
65
- if (body.provider !== undefined && (typeof body.provider !== 'string' || body.provider.length > 128)) throw new Error('provider must be a short string')
66
- return body.provider ? { provider: body.provider.trim() } : {}
67
- }
68
-
69
- export function normalizeHealthRequest(body) {
70
- if (body === null || typeof body !== 'object' || Array.isArray(body)) throw new Error('request body must be an object')
71
- if (body.provider !== undefined && (typeof body.provider !== 'string' || body.provider.length > 128)) throw new Error('provider must be a short string')
72
- return body.provider ? { provider: body.provider } : {}
73
- }
74
-
75
- export function normalizePolicyRequest(body) {
76
- if (body === null || typeof body !== 'object' || Array.isArray(body)) throw new Error('request body must be an object')
77
- if (typeof body.provider !== 'string' || !body.provider || body.provider.length > 128) throw new Error('provider must be a short string')
78
- for (const field of ['include', 'exclude']) {
79
- if (body[field] !== undefined && (!Array.isArray(body[field]) || body[field].length > 100)) throw new Error(`${field} must be an array`)
80
- if (Array.isArray(body[field]) && body[field].some((value) => typeof value !== 'string' || value.length > 256)) throw new Error(`${field} patterns must be short strings`)
81
- }
82
- for (const field of ['requireCapabilities', 'denyCapabilities']) {
83
- if (body[field] !== undefined && (!body[field] || typeof body[field] !== 'object' || Array.isArray(body[field]))) throw new Error(`${field} must be an object`)
84
- if (body[field] && Object.keys(body[field]).length > 4) throw new Error(`${field} has too many capabilities`)
85
- if (body[field] && Object.values(body[field]).some((value) => typeof value !== 'boolean')) throw new Error(`${field} values must be boolean`)
86
- }
87
- return {
88
- provider: body.provider.trim(),
89
- policy: validatePolicy({
90
- include: body.include,
91
- exclude: body.exclude,
92
- requireCapabilities: body.requireCapabilities,
93
- denyCapabilities: body.denyCapabilities,
94
- }),
95
- }
96
- }
97
-
98
- export function normalizeHistoryRequest(url = '') {
99
- const parsed = new URL(url || '/', 'http://dsh-model-sync.local')
100
- const limitValue = Number(parsed.searchParams.get('limit') ?? 50)
101
- const limit = Number.isFinite(limitValue) ? Math.min(100, Math.max(1, Math.floor(limitValue))) : 50
102
- const provider = parsed.searchParams.get('provider') || undefined
103
- const historyId = parsed.searchParams.get('historyId') || undefined
104
- if (provider && provider.length > 128) throw new Error('provider must be a short string')
105
- if (historyId && historyId.length > 128) throw new Error('historyId must be a short string')
106
- return { limit, ...(provider ? { provider } : {}), ...(historyId ? { historyId } : {}), details: parsed.searchParams.get('details') === 'true' }
107
- }
108
-
109
- export function normalizeHistoryRollbackRequest(body) {
110
- if (body === null || typeof body !== 'object' || Array.isArray(body)) throw new Error('request body must be an object')
111
- if (typeof body.historyId !== 'string' || !body.historyId || body.historyId.length > 128) throw new Error('historyId must be a short string')
112
- if (typeof body.provider !== 'string' || !body.provider || body.provider.length > 128) throw new Error('provider must be a short string')
113
- return { historyId: body.historyId.trim(), provider: body.provider.trim() }
114
- }
115
-
116
- export function normalizeSelectionRequest(body) {
117
- if (body === null || typeof body !== 'object' || Array.isArray(body)) throw new Error('request body must be an object')
118
- if (typeof body.provider !== 'string' || !body.provider || body.provider.length > 128) throw new Error('provider must be a short string')
119
- if (!Array.isArray(body.models) || body.models.length > 5000) throw new Error('models must be an array')
120
- const models = []
121
- const seen = new Set()
122
- for (const model of body.models) {
123
- if (typeof model !== 'string' || model.length > 512) throw new Error('model ids must be short strings')
124
- const id = model.trim()
125
- if (id && !seen.has(id)) {
126
- seen.add(id)
127
- models.push(id)
128
- }
129
- }
130
- return { provider: body.provider, models }
131
- }
132
-
133
- export function registerHttpApi(ctx, synchronizer, { getSchedulerStatus = () => null } = {}) {
134
- const makeHandler = (route) => async (req, res) => {
135
- if (!trusted(req)) return json(res, 403, { error: { code: 'forbidden', message: 'dsh-model-sync: cross-origin request rejected' } })
136
- if (route === 'status') {
137
- if (req.method !== 'GET') return json(res, 405, { error: { code: 'method', message: 'GET only' } })
138
- return json(res, 200, { ...synchronizer.status(), scheduler: getSchedulerStatus(), providers: synchronizer.listProviders() })
139
- }
140
- if (route === 'history') {
141
- if (req.method !== 'GET') return json(res, 405, { error: { code: 'method', message: 'GET only' } })
142
- try { return json(res, 200, { history: synchronizer.history(normalizeHistoryRequest(req.url)) }) }
143
- catch (error) { return json(res, 400, { error: { code: 'bad-request', message: error instanceof Error ? error.message : String(error) } }) }
144
- }
145
- if (route === 'credentials') {
146
- if (req.method !== 'GET') return json(res, 405, { error: { code: 'method', message: 'GET only' } })
147
- try { return json(res, 200, await synchronizer.credentialDiagnostics(normalizeCredentialRequest(req.url))) }
148
- catch (error) { return json(res, 400, { error: { code: 'bad-request', message: error instanceof Error ? error.message : String(error) } }) }
149
- }
150
- if (route === 'report') {
151
- if (req.method !== 'GET') return json(res, 405, { error: { code: 'method', message: 'GET only' } })
152
- return json(res, 200, { report: synchronizer.report() })
153
- }
154
- if (route === 'notifications') {
155
- if (req.method !== 'GET') return json(res, 405, { error: { code: 'method', message: 'GET only' } })
156
- return json(res, 200, { notifications: synchronizer.notifications(normalizeNotificationRequest(req.url)) })
157
- }
158
- if (req.method !== 'POST') return json(res, 405, { error: { code: 'method', message: 'POST only' } })
159
- let options
160
- try {
161
- const body = await readJson(req)
162
- options = route === 'selection'
163
- ? normalizeSelectionRequest(body)
164
- : route === 'policy' ? normalizePolicyRequest(body)
165
- : route === 'health' ? normalizeHealthRequest(body)
166
- : route === 'credentials-check' ? normalizeCredentialCheckRequest(body)
167
- : route === 'notification-read' || route === 'notification-acknowledge' ? normalizeNotificationAction(body)
168
- : route === 'rollback' ? normalizeHistoryRollbackRequest(body)
169
- : normalizeRunRequest(body)
170
- }
171
- catch (error) { return json(res, 400, { error: { code: 'bad-request', message: error instanceof Error ? error.message : String(error) } }) }
172
- try {
173
- const result = route === 'selection'
174
- ? await synchronizer.setModelSelection(options.provider, options.models)
175
- : route === 'policy' ? await synchronizer.setModelPolicy(options.provider, options.policy)
176
- : route === 'health' ? await synchronizer.health(options)
177
- : route === 'credentials-check' ? { health: await synchronizer.health(options), credentials: await synchronizer.credentialDiagnostics(options) }
178
- : route === 'notification-read' ? await synchronizer.updateNotification(options.id, 'read')
179
- : route === 'notification-acknowledge' ? await synchronizer.updateNotification(options.id, 'acknowledged')
180
- : route === 'rollback' ? await synchronizer.rollbackHistory(options)
181
- : await synchronizer.run(options)
182
- return json(res, 200, { ...result, providers: synchronizer.listProviders() })
183
- }
184
- catch (error) {
185
- const code = error?.code === 'SETTINGS_CONFLICT' ? 409 : error?.code === 'SETTINGS_UNAVAILABLE' || error?.code === 'CONFIG_UNAVAILABLE' ? 503 : error?.code === 'PROVIDER_NOT_CONFIGURED' || error?.code === 'UNKNOWN_MODEL' || error?.code === 'INVALID_POLICY' || error?.code === 'INVALID_HISTORY' || error?.code === 'HISTORY_NOT_FOUND' || error?.code === 'INVALID_NOTIFICATION' || error?.code === 'NOTIFICATION_NOT_FOUND' ? 400 : 500
186
- return json(res, code, { error: { code: error?.code ?? 'run-failed', message: error instanceof Error ? error.message : String(error) } })
187
- }
188
- }
189
- return [
190
- ctx.webServer.register({ kind: 'exact', path: `${BASE}/status`, handler: makeHandler('status') }),
191
- ctx.webServer.register({ kind: 'exact', path: `${BASE}/run`, handler: makeHandler('run') }),
192
- ctx.webServer.register({ kind: 'exact', path: `${BASE}/health`, handler: makeHandler('health') }),
193
- ctx.webServer.register({ kind: 'exact', path: `${BASE}/selection`, handler: makeHandler('selection') }),
194
- ctx.webServer.register({ kind: 'exact', path: `${BASE}/policy`, handler: makeHandler('policy') }),
195
- ctx.webServer.register({ kind: 'exact', path: `${BASE}/history`, handler: makeHandler('history') }),
196
- ctx.webServer.register({ kind: 'exact', path: `${BASE}/history/rollback`, handler: makeHandler('rollback') }),
197
- ctx.webServer.register({ kind: 'exact', path: `${BASE}/credentials`, handler: makeHandler('credentials') }),
198
- ctx.webServer.register({ kind: 'exact', path: `${BASE}/credentials/check`, handler: makeHandler('credentials-check') }),
199
- ctx.webServer.register({ kind: 'exact', path: BASE + '/report', handler: makeHandler('report') }),
200
- ctx.webServer.register({ kind: 'exact', path: BASE + '/notifications', handler: makeHandler('notifications') }),
201
- ctx.webServer.register({ kind: 'exact', path: BASE + '/notifications/read', handler: makeHandler('notification-read') }),
202
- ctx.webServer.register({ kind: 'exact', path: BASE + '/notifications/acknowledge', handler: makeHandler('notification-acknowledge') }),
203
- ]
204
- }
1
+ import { validatePolicy } from './policy.js'
2
+
3
+ const BASE = '/dsh-model-sync'
4
+
5
+ function json(res, status, body) {
6
+ res.writeHead(status, { 'content-type': 'application/json; charset=utf-8' })
7
+ res.end(JSON.stringify(body))
8
+ }
9
+
10
+ function trusted(req) {
11
+ if (req.headers?.['sec-fetch-site'] === 'cross-site') return false
12
+ const origin = req.headers?.origin
13
+ if (!origin) return true
14
+ const host = req.headers?.host
15
+ if (!host) return false
16
+ try { return new URL(origin).host === host } catch { return false }
17
+ }
18
+
19
+ function readJson(req) {
20
+ return new Promise((resolve, reject) => {
21
+ let raw = ''
22
+ req.on('data', (chunk) => {
23
+ raw += chunk
24
+ if (raw.length > 64 * 1024) reject(new Error('request body is too large'))
25
+ })
26
+ req.on('end', () => {
27
+ try { resolve(JSON.parse(raw || '{}')) } catch (error) { reject(error) }
28
+ })
29
+ req.on('error', reject)
30
+ })
31
+ }
32
+
33
+ export function normalizeRunRequest(body) {
34
+ if (body === null || typeof body !== 'object' || Array.isArray(body)) throw new Error('request body must be an object')
35
+ if (body.provider !== undefined && (typeof body.provider !== 'string' || body.provider.length > 128)) throw new Error('provider must be a short string')
36
+ if (body.dryRun !== undefined && typeof body.dryRun !== 'boolean') throw new Error('dryRun must be boolean')
37
+ if (body.removeMissing !== undefined && typeof body.removeMissing !== 'boolean') throw new Error('removeMissing must be boolean')
38
+ return {
39
+ ...(body.provider ? { provider: body.provider } : {}),
40
+ dryRun: body.dryRun !== false,
41
+ removeMissing: body.removeMissing === true,
42
+ }
43
+ }
44
+
45
+ export function normalizeCredentialRequest(url = '') {
46
+ const parsed = new URL(url || '/', 'http://dsh-model-sync.local')
47
+ const provider = parsed.searchParams.get('provider') || undefined
48
+ if (provider && (provider.length > 128 || !/^[A-Za-z0-9._:-]+$/.test(provider))) throw new Error('provider must be a short string')
49
+ return provider ? { provider } : {}
50
+ }
51
+
52
+ export function normalizeNotificationRequest(url = '') {
53
+ const parsed = new URL(url || '/', 'http://dsh-model-sync.local')
54
+ return { includeAcknowledged: parsed.searchParams.get('includeAcknowledged') !== 'false' }
55
+ }
56
+
57
+ export function normalizeNotificationAction(body) {
58
+ if (body === null || typeof body !== 'object' || Array.isArray(body)) throw new Error('request body must be an object')
59
+ if (typeof body.id !== 'string' || !body.id || body.id.length > 128) throw new Error('notification id is invalid')
60
+ return { id: body.id.trim() }
61
+ }
62
+
63
+ export function normalizeCredentialCheckRequest(body) {
64
+ if (body === null || typeof body !== 'object' || Array.isArray(body)) throw new Error('request body must be an object')
65
+ if (body.provider !== undefined && (typeof body.provider !== 'string' || body.provider.length > 128)) throw new Error('provider must be a short string')
66
+ return body.provider ? { provider: body.provider.trim() } : {}
67
+ }
68
+
69
+ export function normalizeTryRequest(body) {
70
+ if (body === null || typeof body !== 'object' || Array.isArray(body)) throw new Error('request body must be an object')
71
+ if (typeof body.provider !== 'string' || !body.provider) throw new Error('provider is required')
72
+ if (typeof body.model !== 'string' || !body.model) throw new Error('model is required')
73
+ return { provider: body.provider, model: body.model }
74
+ }
75
+
76
+ export function normalizeHealthRequest(body) {
77
+ if (body === null || typeof body !== 'object' || Array.isArray(body)) throw new Error('request body must be an object')
78
+ if (body.provider !== undefined && (typeof body.provider !== 'string' || body.provider.length > 128)) throw new Error('provider must be a short string')
79
+ return body.provider ? { provider: body.provider } : {}
80
+ }
81
+
82
+ export function normalizePolicyRequest(body) {
83
+ if (body === null || typeof body !== 'object' || Array.isArray(body)) throw new Error('request body must be an object')
84
+ if (typeof body.provider !== 'string' || !body.provider || body.provider.length > 128) throw new Error('provider must be a short string')
85
+ for (const field of ['include', 'exclude']) {
86
+ if (body[field] !== undefined && (!Array.isArray(body[field]) || body[field].length > 100)) throw new Error(`${field} must be an array`)
87
+ if (Array.isArray(body[field]) && body[field].some((value) => typeof value !== 'string' || value.length > 256)) throw new Error(`${field} patterns must be short strings`)
88
+ }
89
+ for (const field of ['requireCapabilities', 'denyCapabilities']) {
90
+ if (body[field] !== undefined && (!body[field] || typeof body[field] !== 'object' || Array.isArray(body[field]))) throw new Error(`${field} must be an object`)
91
+ if (body[field] && Object.keys(body[field]).length > 4) throw new Error(`${field} has too many capabilities`)
92
+ if (body[field] && Object.values(body[field]).some((value) => typeof value !== 'boolean')) throw new Error(`${field} values must be boolean`)
93
+ }
94
+ return {
95
+ provider: body.provider.trim(),
96
+ policy: validatePolicy({
97
+ include: body.include,
98
+ exclude: body.exclude,
99
+ requireCapabilities: body.requireCapabilities,
100
+ denyCapabilities: body.denyCapabilities,
101
+ }),
102
+ }
103
+ }
104
+
105
+ export function normalizeHistoryRequest(url = '') {
106
+ const parsed = new URL(url || '/', 'http://dsh-model-sync.local')
107
+ const limitValue = Number(parsed.searchParams.get('limit') ?? 50)
108
+ const limit = Number.isFinite(limitValue) ? Math.min(100, Math.max(1, Math.floor(limitValue))) : 50
109
+ const provider = parsed.searchParams.get('provider') || undefined
110
+ const historyId = parsed.searchParams.get('historyId') || undefined
111
+ if (provider && provider.length > 128) throw new Error('provider must be a short string')
112
+ if (historyId && historyId.length > 128) throw new Error('historyId must be a short string')
113
+ return { limit, ...(provider ? { provider } : {}), ...(historyId ? { historyId } : {}), details: parsed.searchParams.get('details') === 'true' }
114
+ }
115
+
116
+ export function normalizeHistoryRollbackRequest(body) {
117
+ if (body === null || typeof body !== 'object' || Array.isArray(body)) throw new Error('request body must be an object')
118
+ if (typeof body.historyId !== 'string' || !body.historyId || body.historyId.length > 128) throw new Error('historyId must be a short string')
119
+ if (typeof body.provider !== 'string' || !body.provider || body.provider.length > 128) throw new Error('provider must be a short string')
120
+ return { historyId: body.historyId.trim(), provider: body.provider.trim() }
121
+ }
122
+
123
+ export function normalizeSelectionRequest(body) {
124
+ if (body === null || typeof body !== 'object' || Array.isArray(body)) throw new Error('request body must be an object')
125
+ if (typeof body.provider !== 'string' || !body.provider || body.provider.length > 128) throw new Error('provider must be a short string')
126
+ if (!Array.isArray(body.models) || body.models.length > 5000) throw new Error('models must be an array')
127
+ const models = []
128
+ const seen = new Set()
129
+ for (const model of body.models) {
130
+ if (typeof model !== 'string' || model.length > 512) throw new Error('model ids must be short strings')
131
+ const id = model.trim()
132
+ if (id && !seen.has(id)) {
133
+ seen.add(id)
134
+ models.push(id)
135
+ }
136
+ }
137
+ return { provider: body.provider, models }
138
+ }
139
+
140
+ export function registerHttpApi(ctx, synchronizer, { getSchedulerStatus = () => null } = {}) {
141
+ const makeHandler = (route) => async (req, res) => {
142
+ if (!trusted(req)) return json(res, 403, { error: { code: 'forbidden', message: 'dsh-model-sync: cross-origin request rejected' } })
143
+ if (route === 'status') {
144
+ if (req.method !== 'GET') return json(res, 405, { error: { code: 'method', message: 'GET only' } })
145
+ return json(res, 200, { ...synchronizer.status(), scheduler: getSchedulerStatus(), providers: synchronizer.listProviders() })
146
+ }
147
+ if (route === 'history') {
148
+ if (req.method !== 'GET') return json(res, 405, { error: { code: 'method', message: 'GET only' } })
149
+ try { return json(res, 200, { history: synchronizer.history(normalizeHistoryRequest(req.url)) }) }
150
+ catch (error) { return json(res, 400, { error: { code: 'bad-request', message: error instanceof Error ? error.message : String(error) } }) }
151
+ }
152
+ if (route === 'credentials') {
153
+ if (req.method !== 'GET') return json(res, 405, { error: { code: 'method', message: 'GET only' } })
154
+ try { return json(res, 200, await synchronizer.credentialDiagnostics(normalizeCredentialRequest(req.url))) }
155
+ catch (error) { return json(res, 400, { error: { code: 'bad-request', message: error instanceof Error ? error.message : String(error) } }) }
156
+ }
157
+ if (route === 'report') {
158
+ if (req.method !== 'GET') return json(res, 405, { error: { code: 'method', message: 'GET only' } })
159
+ return json(res, 200, { report: synchronizer.report() })
160
+ }
161
+ if (route === 'notifications') {
162
+ if (req.method !== 'GET') return json(res, 405, { error: { code: 'method', message: 'GET only' } })
163
+ return json(res, 200, { notifications: synchronizer.notifications(normalizeNotificationRequest(req.url)) })
164
+ }
165
+ if (req.method !== 'POST') return json(res, 405, { error: { code: 'method', message: 'POST only' } })
166
+ let options
167
+ try {
168
+ const body = await readJson(req)
169
+ options = route === 'selection'
170
+ ? normalizeSelectionRequest(body)
171
+ : route === 'policy' ? normalizePolicyRequest(body)
172
+ : route === 'try' ? normalizeTryRequest(body)
173
+ : route === 'health' ? normalizeHealthRequest(body)
174
+ : route === 'credentials-check' ? normalizeCredentialCheckRequest(body)
175
+ : route === 'notification-read' || route === 'notification-acknowledge' ? normalizeNotificationAction(body)
176
+ : route === 'rollback' ? normalizeHistoryRollbackRequest(body)
177
+ : normalizeRunRequest(body)
178
+ }
179
+ catch (error) { return json(res, 400, { error: { code: 'bad-request', message: error instanceof Error ? error.message : String(error) } }) }
180
+ try {
181
+ const result = route === 'selection'
182
+ ? await synchronizer.setModelSelection(options.provider, options.models)
183
+ : route === 'policy' ? await synchronizer.setModelPolicy(options.provider, options.policy)
184
+ : route === 'try' ? await synchronizer.tryModel(options)
185
+ : route === 'health' ? await synchronizer.health(options)
186
+ : route === 'credentials-check' ? { health: await synchronizer.health(options), credentials: await synchronizer.credentialDiagnostics(options) }
187
+ : route === 'notification-read' ? await synchronizer.updateNotification(options.id, 'read')
188
+ : route === 'notification-acknowledge' ? await synchronizer.updateNotification(options.id, 'acknowledged')
189
+ : route === 'rollback' ? await synchronizer.rollbackHistory(options)
190
+ : await synchronizer.run(options)
191
+ return json(res, 200, { ...result, providers: synchronizer.listProviders() })
192
+ }
193
+ catch (error) {
194
+ const code = error?.code === 'SETTINGS_CONFLICT' ? 409 : error?.code === 'SETTINGS_UNAVAILABLE' || error?.code === 'CONFIG_UNAVAILABLE' ? 503 : error?.code === 'PROVIDER_NOT_CONFIGURED' || error?.code === 'UNKNOWN_MODEL' || error?.code === 'INVALID_POLICY' || error?.code === 'INVALID_HISTORY' || error?.code === 'HISTORY_NOT_FOUND' || error?.code === 'INVALID_NOTIFICATION' || error?.code === 'NOTIFICATION_NOT_FOUND' ? 400 : 500
195
+ return json(res, code, { error: { code: error?.code ?? 'run-failed', message: error instanceof Error ? error.message : String(error) } })
196
+ }
197
+ }
198
+ return [
199
+ ctx.webServer.register({ kind: 'exact', path: `${BASE}/status`, handler: makeHandler('status') }),
200
+ ctx.webServer.register({ kind: 'exact', path: `${BASE}/run`, handler: makeHandler('run') }),
201
+ ctx.webServer.register({ kind: 'exact', path: `${BASE}/try`, handler: makeHandler('try') }),
202
+ ctx.webServer.register({ kind: 'exact', path: `${BASE}/health`, handler: makeHandler('health') }),
203
+ ctx.webServer.register({ kind: 'exact', path: `${BASE}/selection`, handler: makeHandler('selection') }),
204
+ ctx.webServer.register({ kind: 'exact', path: `${BASE}/policy`, handler: makeHandler('policy') }),
205
+ ctx.webServer.register({ kind: 'exact', path: `${BASE}/history`, handler: makeHandler('history') }),
206
+ ctx.webServer.register({ kind: 'exact', path: `${BASE}/history/rollback`, handler: makeHandler('rollback') }),
207
+ ctx.webServer.register({ kind: 'exact', path: `${BASE}/credentials`, handler: makeHandler('credentials') }),
208
+ ctx.webServer.register({ kind: 'exact', path: `${BASE}/credentials/check`, handler: makeHandler('credentials-check') }),
209
+ ctx.webServer.register({ kind: 'exact', path: BASE + '/report', handler: makeHandler('report') }),
210
+ ctx.webServer.register({ kind: 'exact', path: BASE + '/notifications', handler: makeHandler('notifications') }),
211
+ ctx.webServer.register({ kind: 'exact', path: BASE + '/notifications/read', handler: makeHandler('notification-read') }),
212
+ ctx.webServer.register({ kind: 'exact', path: BASE + '/notifications/acknowledge', handler: makeHandler('notification-acknowledge') }),
213
+ ]
214
+ }