@terreno/ai 57.11.0 → 57.13.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 (69) hide show
  1. package/dist/aiApp.d.ts +5 -0
  2. package/dist/aiApp.js +4 -2
  3. package/dist/harness/commit.d.ts +17 -1
  4. package/dist/harness/commit.js +86 -2
  5. package/dist/harness/routes/approvals.js +2 -2
  6. package/dist/harness/routes/conversations.js +1 -1
  7. package/dist/harness/routes/tasks.js +85 -3
  8. package/dist/harness/runtime.js +7 -1
  9. package/dist/harness/subagent.d.ts +17 -2
  10. package/dist/harness/subagent.js +26 -2
  11. package/dist/index.d.ts +5 -1
  12. package/dist/index.js +11 -2
  13. package/dist/models/project.d.ts +13 -0
  14. package/dist/models/project.js +32 -2
  15. package/dist/observability/local/models/obsTrace.js +16 -2
  16. package/dist/observability/local/traceStore.d.ts +5 -0
  17. package/dist/observability/local/traceStore.js +5 -0
  18. package/dist/observability/routes/traces.js +2 -0
  19. package/dist/routes/gptActions.js +15 -2
  20. package/dist/routes/projects.js +3 -3
  21. package/dist/service/agentBlocks.d.ts +20 -0
  22. package/dist/service/agentBlocks.js +92 -0
  23. package/dist/service/chatTurn.d.ts +3 -1
  24. package/dist/service/chatTurn.js +34 -9
  25. package/dist/service/prompts.d.ts +12 -2
  26. package/dist/service/prompts.js +20 -4
  27. package/dist/service/scaleStepper.d.ts +8 -0
  28. package/dist/service/scaleStepper.js +29 -0
  29. package/dist/service/toggleChecklist.d.ts +9 -0
  30. package/dist/service/toggleChecklist.js +37 -0
  31. package/dist/types/harness.d.ts +28 -2
  32. package/dist/types/index.d.ts +19 -4
  33. package/dist/types/observability.d.ts +3 -0
  34. package/package.json +56 -8
  35. package/src/aiApp.test.ts +15 -0
  36. package/src/aiApp.ts +9 -1
  37. package/src/harness/commit.ts +103 -1
  38. package/src/harness/routes/approvals.ts +2 -2
  39. package/src/harness/routes/conversations.ts +1 -1
  40. package/src/harness/routes/tasks.test.ts +34 -0
  41. package/src/harness/routes/tasks.ts +98 -3
  42. package/src/harness/runtime.ts +8 -1
  43. package/src/harness/subagent.test.ts +124 -3
  44. package/src/harness/subagent.ts +43 -1
  45. package/src/index.test.ts +4 -0
  46. package/src/index.ts +5 -1
  47. package/src/isolated/lazyProjectModel.isolated.ts +25 -0
  48. package/src/models/project.ts +31 -1
  49. package/src/observability/local/models/obsTrace.ts +22 -5
  50. package/src/observability/local/traceStore.test.ts +7 -0
  51. package/src/observability/local/traceStore.ts +12 -0
  52. package/src/observability/routes/traces.test.ts +15 -0
  53. package/src/observability/routes/traces.ts +2 -0
  54. package/src/routes/gpt.test.ts +90 -1
  55. package/src/routes/gptActions.test.ts +282 -1
  56. package/src/routes/gptActions.ts +16 -2
  57. package/src/routes/projects.ts +4 -4
  58. package/src/service/agentBlocks.test.ts +95 -0
  59. package/src/service/agentBlocks.ts +114 -0
  60. package/src/service/chatTurn.ts +44 -15
  61. package/src/service/prompts.test.ts +21 -0
  62. package/src/service/prompts.ts +34 -7
  63. package/src/service/scaleStepper.test.ts +105 -0
  64. package/src/service/scaleStepper.ts +29 -0
  65. package/src/service/toggleChecklist.test.ts +126 -0
  66. package/src/service/toggleChecklist.ts +44 -0
  67. package/src/types/harness.ts +31 -2
  68. package/src/types/index.ts +22 -9
  69. package/src/types/observability.ts +3 -0
@@ -4,7 +4,17 @@ import type mongoose from "mongoose";
4
4
 
5
5
  import {AIRequest} from "../models/aiRequest";
6
6
  import {GptHistory} from "../models/gptHistory";
7
- import {buildApp, createScriptedModel} from "../tests/chatHarness";
7
+ import {scaleStepperHostAction} from "../service/scaleStepper";
8
+ import {toggleChecklistHostAction} from "../service/toggleChecklist";
9
+ import {
10
+ buildApp,
11
+ createScriptedModel,
12
+ modelCall,
13
+ streamPrompt,
14
+ systemPromptOf,
15
+ textStep,
16
+ USER_PROMPT,
17
+ } from "../tests/chatHarness";
8
18
  import {authAsUser, ensureTestUsers} from "../tests/helpers";
9
19
  import {HOST_ACTION_TIMEOUT_MS} from "./gptActions";
10
20
 
@@ -113,6 +123,14 @@ describe("POST /gpt/actions", () => {
113
123
  const logged = await AIRequest.findExactlyOne({requestType: "ui_action"});
114
124
  expect(logged?.requestType).toBe("ui_action");
115
125
  expect(logged?.error).toBeUndefined();
126
+ expect(logged?.response).toContain("Exporting");
127
+ expect(JSON.parse(logged?.prompt ?? "{}")).toEqual({
128
+ blockId: "row",
129
+ elementId: "run_btn",
130
+ historyId: history._id.toString(),
131
+ messageId: "m1",
132
+ name: "export_csv",
133
+ });
116
134
  });
117
135
 
118
136
  it("returns 504 and logs the timeout", async () => {
@@ -239,3 +257,266 @@ describe("POST /gpt/actions", () => {
239
257
  expect(res.body.title).toBe("Action returned an unknown replace value");
240
258
  });
241
259
  });
260
+
261
+ const ROAST_STEPPER = `v: 1
262
+ blocks:
263
+ - type: heading
264
+ text: Sunday roast
265
+ - type: card
266
+ children:
267
+ - type: stepper
268
+ id: guests
269
+ label: Number of people
270
+ unit: People
271
+ value: 5
272
+ min: 1
273
+ max: 20
274
+ callback:
275
+ name: scaleStepper
276
+ payload:
277
+ recipe: roast
278
+ items:
279
+ - {label: Lamb, amount: 2, unit: kg, decimals: 1}
280
+ - {label: Parsnips, amount: 7, round: up}
281
+ - {label: Broccoli, amount: 625, unit: g}
282
+ `;
283
+
284
+ describe("POST /gpt/actions with scaleStepperHostAction", () => {
285
+ afterEach(async () => {
286
+ await AIRequest.deleteMany({});
287
+ await GptHistory.deleteMany({});
288
+ });
289
+
290
+ const setup = async (
291
+ assistantPrompts: string[]
292
+ ): Promise<{agent: Awaited<ReturnType<typeof authAsUser>>; historyId: string}> => {
293
+ const created = await ensureTestUsers();
294
+ const userId = (created[1] as {_id: mongoose.Types.ObjectId})._id;
295
+ const history = await GptHistory.create({
296
+ prompts: [
297
+ {text: "Plan a roast", type: "user"},
298
+ ...assistantPrompts.map((text) => ({text, type: "assistant" as const})),
299
+ ],
300
+ userId,
301
+ });
302
+ const app = buildApp({
303
+ model,
304
+ uiBlocks: {hostActions: {scaleStepper: scaleStepperHostAction}},
305
+ });
306
+ return {agent: await authAsUser(app, "notAdmin"), historyId: history._id.toString()};
307
+ };
308
+
309
+ const tap = (
310
+ historyId: string,
311
+ fields: Record<string, unknown> = {}
312
+ ): Record<string, unknown> => ({
313
+ blockId: "guests",
314
+ elementId: "guests_increase",
315
+ historyId,
316
+ messageId: "msg-1",
317
+ name: "scaleStepper",
318
+ payload: {recipe: "roast", value: 6},
319
+ ...fields,
320
+ });
321
+
322
+ it("returns the scaled stepper and logs ids and value without the response", async () => {
323
+ const {agent, historyId} = await setup([ROAST_STEPPER]);
324
+ const res = await agent.post("/gpt/actions").send(tap(historyId));
325
+ expect(res.status).toBe(200);
326
+ expect(res.body.data.replace).toBe("block");
327
+ expect(res.body.data.blocks.v).toBe(1);
328
+ expect(res.body.data.blocks.blocks).toHaveLength(1);
329
+ const stepper = res.body.data.blocks.blocks[0];
330
+ expect(stepper.type).toBe("stepper");
331
+ expect(stepper.id).toBe("guests");
332
+ expect(stepper.value).toBe(6);
333
+ expect(stepper.items.map((item: {amount: number}) => item.amount)).toEqual([2.4, 9, 750]);
334
+
335
+ const logged = await AIRequest.findExactlyOne({requestType: "ui_action"});
336
+ expect(logged?.response).toBeUndefined();
337
+ expect(JSON.parse(logged?.prompt ?? "{}")).toEqual({
338
+ blockId: "guests",
339
+ elementId: "guests_increase",
340
+ historyId,
341
+ messageId: "msg-1",
342
+ name: "scaleStepper",
343
+ value: 6,
344
+ });
345
+ expect(logged?.prompt).not.toContain("roast");
346
+ });
347
+
348
+ it("returns 400 above max, 404 for an unknown block, and 409 when the block is ambiguous", async () => {
349
+ const {agent, historyId} = await setup([ROAST_STEPPER, ROAST_STEPPER]);
350
+ const tooMany = await agent
351
+ .post("/gpt/actions")
352
+ .send(tap(historyId, {payload: {recipe: "roast", value: 21}}));
353
+ expect(tooMany.status).toBe(400);
354
+
355
+ const unknown = await agent.post("/gpt/actions").send(tap(historyId, {blockId: "nobody"}));
356
+ expect(unknown.status).toBe(404);
357
+
358
+ const ambiguous = await agent.post("/gpt/actions").send(tap(historyId, {messageId: "msg-9"}));
359
+ expect(ambiguous.status).toBe(409);
360
+
361
+ const direct = await agent.post("/gpt/actions").send(tap(historyId, {messageId: "msg-2"}));
362
+ expect(direct.status).toBe(200);
363
+
364
+ const notANumber = await agent
365
+ .post("/gpt/actions")
366
+ .send(tap(historyId, {payload: {value: "6"}}));
367
+ expect(notANumber.status).toBe(400);
368
+ expect(notANumber.body.title).toBe("Invalid payload");
369
+
370
+ const errorRows = await AIRequest.find({error: {$exists: true}, requestType: "ui_action"});
371
+ for (const row of errorRows) {
372
+ expect(row.response).toBeUndefined();
373
+ }
374
+ });
375
+ });
376
+
377
+ const ROAST_CHECKLIST = `v: 1
378
+ blocks:
379
+ - type: heading
380
+ text: Sunday roast
381
+ - type: card
382
+ children:
383
+ - type: checklist
384
+ id: cook
385
+ title: Cooking plan
386
+ callback:
387
+ name: toggleChecklist
388
+ payload:
389
+ recipe: roast
390
+ items:
391
+ - {id: preheat, text: Preheat the oven, meta: "12:00"}
392
+ - {id: lamb, text: Lamb in, meta: "12:15"}
393
+ - {id: rest, text: Rest the lamb, meta: "13:45"}
394
+ `;
395
+
396
+ describe("POST /gpt/actions with toggleChecklistHostAction", () => {
397
+ afterEach(async () => {
398
+ await AIRequest.deleteMany({});
399
+ await GptHistory.deleteMany({});
400
+ });
401
+
402
+ const setup = async (
403
+ assistantPrompts: string[]
404
+ ): Promise<{agent: Awaited<ReturnType<typeof authAsUser>>; historyId: string}> => {
405
+ const created = await ensureTestUsers();
406
+ const userId = (created[1] as {_id: mongoose.Types.ObjectId})._id;
407
+ const history = await GptHistory.create({
408
+ prompts: [
409
+ {text: "Plan a roast", type: "user"},
410
+ ...assistantPrompts.map((text) => ({text, type: "assistant" as const})),
411
+ ],
412
+ userId,
413
+ });
414
+ const app = buildApp({
415
+ model,
416
+ uiBlocks: {hostActions: {toggleChecklist: toggleChecklistHostAction}},
417
+ });
418
+ return {agent: await authAsUser(app, "notAdmin"), historyId: history._id.toString()};
419
+ };
420
+
421
+ const tick = (
422
+ historyId: string,
423
+ fields: Record<string, unknown> = {}
424
+ ): Record<string, unknown> => ({
425
+ blockId: "cook",
426
+ elementId: "cook_lamb",
427
+ historyId,
428
+ messageId: "msg-1",
429
+ name: "toggleChecklist",
430
+ payload: {checked: true, itemId: "lamb", recipe: "roast", state: {lamb: true, preheat: true}},
431
+ ...fields,
432
+ });
433
+
434
+ it("returns the stored checklist with checked from state and logs only ids", async () => {
435
+ const {agent, historyId} = await setup([ROAST_CHECKLIST]);
436
+ const res = await agent.post("/gpt/actions").send(tick(historyId));
437
+ expect(res.status).toBe(200);
438
+ expect(res.body.data.replace).toBe("block");
439
+ expect(res.body.data.blocks.v).toBe(1);
440
+ expect(res.body.data.blocks.blocks).toHaveLength(1);
441
+ const checklist = res.body.data.blocks.blocks[0];
442
+ expect(checklist.type).toBe("checklist");
443
+ expect(checklist.id).toBe("cook");
444
+ expect(checklist.callback).toEqual({name: "toggleChecklist", payload: {recipe: "roast"}});
445
+ expect(
446
+ checklist.items.map((item: {checked: boolean; id: string}) => [item.id, item.checked])
447
+ ).toEqual([
448
+ ["preheat", true],
449
+ ["lamb", true],
450
+ ["rest", false],
451
+ ]);
452
+
453
+ const logged = await AIRequest.findExactlyOne({requestType: "ui_action"});
454
+ expect(logged?.response).toBeUndefined();
455
+ expect(JSON.parse(logged?.prompt ?? "{}")).toEqual({
456
+ blockId: "cook",
457
+ elementId: "cook_lamb",
458
+ historyId,
459
+ messageId: "msg-1",
460
+ name: "toggleChecklist",
461
+ });
462
+ expect(logged?.prompt).not.toContain("Preheat");
463
+ });
464
+
465
+ it("returns 400 for an unknown item id, 404 for an unknown block, and 409 when the block is ambiguous", async () => {
466
+ const {agent, historyId} = await setup([ROAST_CHECKLIST, ROAST_CHECKLIST]);
467
+ const unknownItem = await agent
468
+ .post("/gpt/actions")
469
+ .send(tick(historyId, {payload: {checked: true, itemId: "carve", state: {carve: true}}}));
470
+ expect(unknownItem.status).toBe(400);
471
+ expect(unknownItem.body.title).toBe("Unknown checklist item");
472
+
473
+ const unknownStateKey = await agent
474
+ .post("/gpt/actions")
475
+ .send(tick(historyId, {payload: {checked: true, itemId: "lamb", state: {carve: true}}}));
476
+ expect(unknownStateKey.status).toBe(400);
477
+
478
+ const malformed = await agent
479
+ .post("/gpt/actions")
480
+ .send(tick(historyId, {payload: {checked: "yes", itemId: "lamb", state: {}}}));
481
+ expect(malformed.status).toBe(400);
482
+ expect(malformed.body.title).toBe("Invalid payload");
483
+
484
+ const unknownBlock = await agent.post("/gpt/actions").send(tick(historyId, {blockId: "nope"}));
485
+ expect(unknownBlock.status).toBe(404);
486
+
487
+ const ambiguous = await agent.post("/gpt/actions").send(tick(historyId, {messageId: "msg-9"}));
488
+ expect(ambiguous.status).toBe(409);
489
+
490
+ const direct = await agent.post("/gpt/actions").send(tick(historyId, {messageId: "msg-2"}));
491
+ expect(direct.status).toBe(200);
492
+
493
+ const errorRows = await AIRequest.find({error: {$exists: true}, requestType: "ui_action"});
494
+ expect(errorRows.length).toBeGreaterThan(0);
495
+ for (const row of errorRows) {
496
+ expect(row.response).toBeUndefined();
497
+ }
498
+ });
499
+
500
+ it("names the registered action as the checklist callback in the chat prompt", async () => {
501
+ await ensureTestUsers();
502
+ const chatModel = createScriptedModel({steps: [textStep(ROAST_CHECKLIST)]});
503
+ const agent = await authAsUser(
504
+ buildApp({
505
+ model: chatModel,
506
+ uiBlocks: {hostActions: {toggleChecklist: toggleChecklistHostAction}},
507
+ }),
508
+ "notAdmin"
509
+ );
510
+
511
+ const {events} = await streamPrompt(agent, {prompt: USER_PROMPT});
512
+
513
+ const system = String(systemPromptOf(modelCall(chatModel, 0)));
514
+ expect(system).toContain("Set a checklist callback name to one of: toggleChecklist.");
515
+ expect(system).not.toContain("Leave callback out of a checklist");
516
+ expect(events.find((event) => "blocks" in event)?.blocks).toEqual({
517
+ errors: [],
518
+ ok: true,
519
+ warnings: [],
520
+ });
521
+ });
522
+ });
@@ -37,6 +37,12 @@ const payloadFields = (
37
37
  return fields;
38
38
  };
39
39
 
40
+ /** With `logResponse: false`, only a numeric `payload.value` joins the ids in the log. */
41
+ const loggedValue = (payload: unknown): {value?: number} => {
42
+ const value = (payload as {value?: unknown} | undefined)?.value;
43
+ return typeof value === "number" ? {value} : {};
44
+ };
45
+
40
46
  const withTimeout = async <T>(work: Promise<T>, timeoutMs: number): Promise<T> => {
41
47
  let timer: ReturnType<typeof setTimeout> | undefined;
42
48
  try {
@@ -183,7 +189,15 @@ export const addGptActionRoutes = (router: express.Router, options: GptRouteOpti
183
189
  }
184
190
 
185
191
  const started = DateTime.now();
186
- const prompt = JSON.stringify({blockId, elementId, historyId, messageId, name});
192
+ const isResponseLogged = action.logResponse !== false;
193
+ const prompt = JSON.stringify({
194
+ blockId,
195
+ elementId,
196
+ historyId,
197
+ messageId,
198
+ name,
199
+ ...(isResponseLogged ? {} : loggedValue(parsedPayload)),
200
+ });
187
201
  let result: HostActionResult | undefined;
188
202
  try {
189
203
  result = await withTimeout(
@@ -245,7 +259,7 @@ export const addGptActionRoutes = (router: express.Router, options: GptRouteOpti
245
259
  };
246
260
  await logAction({
247
261
  prompt,
248
- response: JSON.stringify(data),
262
+ response: isResponseLogged ? JSON.stringify(data) : undefined,
249
263
  responseTime: DateTime.now().toMillis() - started.toMillis(),
250
264
  userId,
251
265
  });
@@ -9,7 +9,7 @@ import {
9
9
  import type express from "express";
10
10
  import type mongoose from "mongoose";
11
11
 
12
- import {Project} from "../models/project";
12
+ import {getProjectModel} from "../models/project";
13
13
  import type {ProjectDocument} from "../types";
14
14
 
15
15
  export const addProjectRoutes = (
@@ -41,7 +41,7 @@ export const addProjectRoutes = (
41
41
  throw new APIError({status: 400, title: "text is required"});
42
42
  }
43
43
 
44
- const project = await Project.findById(id);
44
+ const project = await getProjectModel().findById(id);
45
45
  if (!project) {
46
46
  throw new APIError({status: 404, title: "Project not found"});
47
47
  }
@@ -73,7 +73,7 @@ export const addProjectRoutes = (
73
73
  const {id, memoryId} = req.params;
74
74
  const userId = (req.user as {_id?: mongoose.Types.ObjectId} | undefined)?._id;
75
75
 
76
- const project = await Project.findById(id);
76
+ const project = await getProjectModel().findById(id);
77
77
  if (!project) {
78
78
  throw new APIError({status: 404, title: "Project not found"});
79
79
  }
@@ -95,7 +95,7 @@ export const addProjectRoutes = (
95
95
 
96
96
  router.use(
97
97
  "/gpt/projects",
98
- modelRouter(Project, {
98
+ modelRouter(getProjectModel(), {
99
99
  ...options?.openApiOptions,
100
100
  permissions: {
101
101
  create: [Permissions.IsAuthenticated],
@@ -0,0 +1,95 @@
1
+ import {describe, expect, it} from "bun:test";
2
+ import {isAPIError} from "@terreno/api";
3
+
4
+ import {GptHistory} from "../models/gptHistory";
5
+ import type {GptHistoryDocument, GptHistoryPrompt} from "../types";
6
+ import {findAgentBlock} from "./agentBlocks";
7
+
8
+ const stepperYaml = ({id = "guests", value = 5}: {id?: string; value?: number} = {}): string =>
9
+ `v: 1
10
+ blocks:
11
+ - type: card
12
+ children:
13
+ - type: stepper
14
+ id: ${id}
15
+ label: Number of people
16
+ value: ${value}
17
+ min: 1
18
+ max: 20
19
+ callback:
20
+ name: scaleStepper
21
+ `;
22
+
23
+ const historyWith = (prompts: GptHistoryPrompt[]): GptHistoryDocument =>
24
+ new GptHistory({prompts, userId: "000000000000000000000001"}) as unknown as GptHistoryDocument;
25
+
26
+ const statusOf = (work: () => unknown): number | undefined => {
27
+ try {
28
+ work();
29
+ } catch (error) {
30
+ if (isAPIError(error)) {
31
+ return error.status;
32
+ }
33
+ throw error;
34
+ }
35
+ return undefined;
36
+ };
37
+
38
+ describe("findAgentBlock", () => {
39
+ it("reads history.prompts[n] for msg-<n> and finds a block nested in a card", () => {
40
+ const history = historyWith([
41
+ {text: "Plan a roast", type: "user"},
42
+ {text: stepperYaml({value: 4}), type: "assistant"},
43
+ {text: "More people", type: "user"},
44
+ {text: stepperYaml({value: 7}), type: "assistant"},
45
+ ]);
46
+ const found = findAgentBlock({blockId: "guests", history, messageId: "msg-3", type: "stepper"});
47
+ expect(found.value).toBe(7);
48
+ expect(found.type).toBe("stepper");
49
+ });
50
+
51
+ it("falls back to the only assistant prompt holding the block when msg-<n> misses", () => {
52
+ const history = historyWith([
53
+ {text: "Plan a roast", type: "user"},
54
+ {text: `\`\`\`yaml\n${stepperYaml({value: 5})}\`\`\``, type: "assistant"},
55
+ {text: "Thanks", type: "assistant"},
56
+ ]);
57
+ expect(
58
+ findAgentBlock({blockId: "guests", history, messageId: "msg-2", type: "stepper"}).value
59
+ ).toBe(5);
60
+ expect(
61
+ findAgentBlock({blockId: "guests", history, messageId: "host-id-7", type: "stepper"}).value
62
+ ).toBe(5);
63
+ });
64
+
65
+ it("returns 409 when several prompts hold the block and msg-<n> misses", () => {
66
+ const history = historyWith([
67
+ {text: stepperYaml({value: 4}), type: "assistant"},
68
+ {text: stepperYaml({value: 6}), type: "assistant"},
69
+ ]);
70
+ expect(
71
+ statusOf(() => findAgentBlock({blockId: "guests", history, messageId: "m1", type: "stepper"}))
72
+ ).toBe(409);
73
+ expect(
74
+ findAgentBlock({blockId: "guests", history, messageId: "msg-1", type: "stepper"}).value
75
+ ).toBe(6);
76
+ });
77
+
78
+ it("returns 404 for an unknown id, a user prompt, or a block of another type", () => {
79
+ const history = historyWith([
80
+ {text: stepperYaml(), type: "user"},
81
+ {text: "v: 1\nblocks:\n - type: heading\n id: guests\n text: Hi\n", type: "assistant"},
82
+ {text: "not a document", type: "assistant"},
83
+ ]);
84
+ expect(
85
+ statusOf(() =>
86
+ findAgentBlock({blockId: "guests", history, messageId: "msg-0", type: "stepper"})
87
+ )
88
+ ).toBe(404);
89
+ expect(
90
+ statusOf(() =>
91
+ findAgentBlock({blockId: "missing", history, messageId: "msg-1", type: "stepper"})
92
+ )
93
+ ).toBe(404);
94
+ });
95
+ });
@@ -0,0 +1,114 @@
1
+ import {APIError} from "@terreno/api";
2
+ import {type Block, type BlocksDocument, parseBlocks, validateBlocks} from "@terreno/blocks";
3
+
4
+ import type {GptHistoryDocument, GptHistoryPrompt} from "../types";
5
+
6
+ const MESSAGE_INDEX_PATTERN = /^msg-(\d+)$/;
7
+
8
+ /**
9
+ * A `{v: 1, blocks}` document. Built by assignment so `v` stays the first key, which
10
+ * `validateBlocks` requires (`KEY_ORDER`).
11
+ */
12
+ export const blocksDocument = (blocks: Block[]): BlocksDocument => {
13
+ const doc = {v: 1} as BlocksDocument;
14
+ doc.blocks = blocks;
15
+ return doc;
16
+ };
17
+
18
+ const isMapping = (value: unknown): value is Record<string, unknown> =>
19
+ typeof value === "object" && value !== null && !Array.isArray(value);
20
+
21
+ /** Depth-first search of a parsed document's blocks, including card and columns children. */
22
+ const findRawBlock = (
23
+ blocks: unknown,
24
+ type: string,
25
+ blockId: string
26
+ ): Record<string, unknown> | undefined => {
27
+ if (!Array.isArray(blocks)) {
28
+ return undefined;
29
+ }
30
+ for (const block of blocks) {
31
+ if (!isMapping(block)) {
32
+ continue;
33
+ }
34
+ if (block.type === type && block.id === blockId) {
35
+ return block;
36
+ }
37
+ const nested = findRawBlock(block.children, type, blockId);
38
+ if (nested !== undefined) {
39
+ return nested;
40
+ }
41
+ }
42
+ return undefined;
43
+ };
44
+
45
+ /**
46
+ * The block from one stored assistant prompt. The block is checked on its own, so a sibling
47
+ * that needs host options (html, https images) does not hide it.
48
+ */
49
+ const blockInPrompt = <T extends Block["type"]>(
50
+ prompt: GptHistoryPrompt | undefined,
51
+ type: T,
52
+ blockId: string
53
+ ): Extract<Block, {type: T}> | undefined => {
54
+ if (prompt?.type !== "assistant" || typeof prompt.text !== "string") {
55
+ return undefined;
56
+ }
57
+ const parsed = parseBlocks(prompt.text);
58
+ if (!parsed.ok || !isMapping(parsed.value)) {
59
+ return undefined;
60
+ }
61
+ const raw = findRawBlock(parsed.value.blocks, type, blockId);
62
+ if (raw === undefined) {
63
+ return undefined;
64
+ }
65
+ const validated = validateBlocks(blocksDocument([raw as unknown as Block]));
66
+ if (!validated.ok) {
67
+ return undefined;
68
+ }
69
+ return validated.doc.blocks[0] as Extract<Block, {type: T}>;
70
+ };
71
+
72
+ /**
73
+ * Loads the block the agent wrote, from the stored history rather than from the client.
74
+ * `msg-<n>` reads `history.prompts[n]` (stored prompts have no ids). Otherwise the only assistant
75
+ * prompt holding a block of that type and id is used: 409 when several do, 404 when none does.
76
+ */
77
+ export const findAgentBlock = <T extends Block["type"]>({
78
+ blockId,
79
+ history,
80
+ messageId,
81
+ type,
82
+ }: {
83
+ blockId: string;
84
+ history: GptHistoryDocument;
85
+ messageId: string;
86
+ type: T;
87
+ }): Extract<Block, {type: T}> => {
88
+ const indexMatch = MESSAGE_INDEX_PATTERN.exec(messageId);
89
+ if (indexMatch?.[1] !== undefined) {
90
+ const direct = blockInPrompt(history.prompts[Number(indexMatch[1])], type, blockId);
91
+ if (direct !== undefined) {
92
+ return direct;
93
+ }
94
+ }
95
+ const matches = history.prompts
96
+ .map((prompt) => blockInPrompt(prompt, type, blockId))
97
+ .filter((block): block is Extract<Block, {type: T}> => block !== undefined);
98
+ if (matches.length > 1) {
99
+ throw new APIError({
100
+ detail: `${matches.length} replies hold a ${type} with id ${blockId}.`,
101
+ status: 409,
102
+ title: "Block is ambiguous",
103
+ });
104
+ }
105
+ const [only] = matches;
106
+ if (only === undefined) {
107
+ throw new APIError({
108
+ detail: `No reply holds a ${type} with id ${blockId}.`,
109
+ status: 404,
110
+ title: "Block not found",
111
+ });
112
+ }
113
+ return only;
114
+ };