@twinsearth/w2m-dsh-plugin 3.0.0 → 3.2.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.
@@ -216,8 +216,12 @@ export function registerDecisionCenterRoutes(ctx) {
216
216
  if (center === undefined || typeof center.ingest !== 'function') return unavailable();
217
217
  const body = await bodyOf(request);
218
218
  const out = center.ingest(body ?? {});
219
- const statusCode = out.ok ? 200
220
- : out.reason === 'title-required' || out.reason === 'options-required' || out.reason === 'gray-is-terminal' ? 400
219
+ const statusCode = out.ok
220
+ ? 200
221
+ : out.reason === 'title-required' ||
222
+ out.reason === 'options-required' ||
223
+ out.reason === 'gray-is-terminal'
224
+ ? 400
221
225
  : 409;
222
226
  return json(statusCode, out);
223
227
  });
@@ -228,7 +232,12 @@ export function registerDecisionCenterRoutes(ctx) {
228
232
  if (center === undefined || typeof center.list !== 'function') return unavailable();
229
233
  if (streams >= MAX_EVENT_STREAMS) return json(429, { ok: false, error: 'too-many-event-streams' });
230
234
  streams += 1;
231
- const response = sseResponse({ center, pump: () => () => { streams = Math.max(0, streams - 1); } });
235
+ const response = sseResponse({
236
+ center,
237
+ pump: () => () => {
238
+ streams = Math.max(0, streams - 1);
239
+ },
240
+ });
232
241
  return response;
233
242
  });
234
243
 
@@ -237,17 +246,25 @@ export function registerDecisionCenterRoutes(ctx) {
237
246
  if (center === undefined || typeof center.resolve !== 'function') return unavailable();
238
247
  const body = await bodyOf(request);
239
248
  if (Array.isArray(body?.ids)) {
240
- if (typeof center.resolveMany !== 'function') return json(503, { ok: false, error: 'decision-center-batch-unavailable' });
241
- return json(200, center.resolveMany(body.ids, { option: body?.option, by: body?.by ?? 'w2m-api', note: body?.note }));
249
+ if (typeof center.resolveMany !== 'function')
250
+ return json(503, { ok: false, error: 'decision-center-batch-unavailable' });
251
+ return json(
252
+ 200,
253
+ center.resolveMany(body.ids, { option: body?.option, by: body?.by ?? 'w2m-api', note: body?.note }),
254
+ );
242
255
  }
243
256
  const resolveOptions = { by: body?.by ?? 'w2m-api', note: body?.note };
244
257
  if (body?.custom !== undefined) resolveOptions.custom = body.custom;
245
- const out = (body?.actionId !== undefined && typeof center.resolveAction === 'function')
246
- ? center.resolveAction(body?.id, body.actionId, body?.payload, resolveOptions)
247
- : center.resolve(body?.id, body?.option, resolveOptions);
248
- const statusCode = out.ok ? 200
249
- : out.reason === 'not-found' ? 404
250
- : out.reason === 'option-required' || out.reason === 'unknown-option' ? 400
258
+ const out =
259
+ body?.actionId !== undefined && typeof center.resolveAction === 'function'
260
+ ? center.resolveAction(body?.id, body.actionId, body?.payload, resolveOptions)
261
+ : center.resolve(body?.id, body?.option, resolveOptions);
262
+ const statusCode = out.ok
263
+ ? 200
264
+ : out.reason === 'not-found'
265
+ ? 404
266
+ : out.reason === 'option-required' || out.reason === 'unknown-option'
267
+ ? 400
251
268
  : 409;
252
269
  return json(statusCode, out);
253
270
  });
@@ -257,11 +274,12 @@ export function registerDecisionCenterRoutes(ctx) {
257
274
  if (center === undefined || typeof center.abandon !== 'function') return unavailable();
258
275
  const body = await bodyOf(request);
259
276
  if (Array.isArray(body?.ids)) {
260
- if (typeof center.abandonMany !== 'function') return json(503, { ok: false, error: 'decision-center-batch-unavailable' });
277
+ if (typeof center.abandonMany !== 'function')
278
+ return json(503, { ok: false, error: 'decision-center-batch-unavailable' });
261
279
  return json(200, center.abandonMany(body.ids, { by: body?.by ?? 'w2m-api', note: body?.note }));
262
280
  }
263
281
  const out = center.abandon(body?.id, { by: body?.by ?? 'w2m-api', note: body?.note });
264
- return json(out.ok ? 200 : (out.reason === 'not-found' ? 404 : 409), out);
282
+ return json(out.ok ? 200 : out.reason === 'not-found' ? 404 : 409, out);
265
283
  });
266
284
 
267
285
  register(`${DECISION_ROUTE_PREFIX}/ai`, ['GET'], () => {
@@ -274,7 +292,7 @@ export function registerDecisionCenterRoutes(ctx) {
274
292
  const center = centerOf(ctx);
275
293
  if (center === undefined || typeof center.testAi !== 'function') return unavailable();
276
294
  const out = await center.testAi((await bodyOf(request)) ?? {});
277
- return json(out.ok ? 200 : (out.status === 429 ? 429 : 400), out);
295
+ return json(out.ok ? 200 : out.status === 429 ? 429 : 400, out);
278
296
  });
279
297
 
280
298
  register(`${DECISION_ROUTE_PREFIX}/ai/config`, ['POST'], async (request) => {
@@ -296,17 +314,21 @@ export function registerDecisionCenterRoutes(ctx) {
296
314
  send('start', { id });
297
315
  void (async () => {
298
316
  try {
299
- const out = typeof center.adviseStream === 'function'
300
- ? await center.adviseStream(id, (delta, text) => {
301
- if (!cancelled) send('delta', { delta, text });
302
- })
303
- : await center.advise(id);
304
- if (!cancelled) send(out.ok ? 'done' : 'error', out.ok ? { advice: out.advice, ticket: out.ticket } : out);
317
+ const out =
318
+ typeof center.adviseStream === 'function'
319
+ ? await center.adviseStream(id, (delta, text) => {
320
+ if (!cancelled) send('delta', { delta, text });
321
+ })
322
+ : await center.advise(id);
323
+ if (!cancelled)
324
+ send(out.ok ? 'done' : 'error', out.ok ? { advice: out.advice, ticket: out.ticket } : out);
305
325
  } catch (error) {
306
326
  if (!cancelled) send('error', { ok: false, error: String(error?.message ?? error) });
307
327
  }
308
328
  })();
309
- return () => { cancelled = true; };
329
+ return () => {
330
+ cancelled = true;
331
+ };
310
332
  },
311
333
  });
312
334
  });
@@ -316,11 +338,15 @@ export function registerDecisionCenterRoutes(ctx) {
316
338
  if (center === undefined || typeof center.advise !== 'function') return unavailable();
317
339
  const body = await bodyOf(request);
318
340
  const out = await center.advise(body?.id);
319
- const limited = out?.reason === 'ai-rate-limited' || out?.reason === 'ai-circuit-open' || out?.reason === 'ai-daily-quota-exceeded';
320
- const headers = limited && Number.isFinite(out?.retryAfterMs) && out.retryAfterMs > 0
321
- ? { 'retry-after': String(Math.ceil(out.retryAfterMs / 1000)) }
322
- : {};
323
- return json(out.ok ? 200 : (out.reason === 'not-found' ? 404 : limited ? 429 : 409), out, headers);
341
+ const limited =
342
+ out?.reason === 'ai-rate-limited' ||
343
+ out?.reason === 'ai-circuit-open' ||
344
+ out?.reason === 'ai-daily-quota-exceeded';
345
+ const headers =
346
+ limited && Number.isFinite(out?.retryAfterMs) && out.retryAfterMs > 0
347
+ ? { 'retry-after': String(Math.ceil(out.retryAfterMs / 1000)) }
348
+ : {};
349
+ return json(out.ok ? 200 : out.reason === 'not-found' ? 404 : limited ? 429 : 409, out, headers);
324
350
  });
325
351
 
326
352
  status.mounted = true;