@terreno/ai 57.12.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.
@@ -1,6 +1,6 @@
1
1
  import {afterEach, beforeAll, describe, expect, it, spyOn} from "bun:test";
2
2
  import {TerrenoApp} from "@terreno/api";
3
- import {askPromptSection} from "@terreno/blocks";
3
+ import {askPromptSection, blocksPromptSection} from "@terreno/blocks";
4
4
  import {jsonSchema, type LanguageModel, type Tool, tool} from "ai";
5
5
  import express from "express";
6
6
 
@@ -2933,6 +2933,31 @@ blocks:
2933
2933
  name: export_csv
2934
2934
  `;
2935
2935
 
2936
+ const stepperReply = (callbackName: string): string => `v: 1
2937
+ blocks:
2938
+ - type: stepper
2939
+ id: guests
2940
+ label: Number of people
2941
+ value: 5
2942
+ min: 1
2943
+ max: 20
2944
+ callback:
2945
+ name: ${callbackName}
2946
+ `;
2947
+
2948
+ const STEPPER_HOST_ACTIONS = {
2949
+ approve: {},
2950
+ scaleStepper: {handles: "stepper" as const},
2951
+ };
2952
+
2953
+ const blocksEventOf = (
2954
+ events: Record<string, unknown>[]
2955
+ ): {errors: {code: string; path: string}[]; ok: boolean} =>
2956
+ events.find((event) => "blocks" in event)?.blocks as {
2957
+ errors: {code: string; path: string}[];
2958
+ ok: boolean;
2959
+ };
2960
+
2936
2961
  describe("/gpt/prompt uiBlocks", () => {
2937
2962
  beforeAll(async () => {
2938
2963
  await ensureTestUsers();
@@ -3276,4 +3301,68 @@ blocks:
3276
3301
  expect(allowedBlocks.ok).toBe(true);
3277
3302
  expect(systemPromptOf(modelCall(allowedModel, 0))).toContain("cdn.example.com");
3278
3303
  });
3304
+
3305
+ it("offers the stepper by default when a host action handles steppers", async () => {
3306
+ const model = createScriptedModel({steps: [textStep(stepperReply("scaleStepper"))]});
3307
+ const agent = await authAsUser(
3308
+ buildApp({model, uiBlocks: {hostActions: STEPPER_HOST_ACTIONS}}),
3309
+ "notAdmin"
3310
+ );
3311
+
3312
+ const {events} = await streamPrompt(agent, {prompt: USER_PROMPT});
3313
+
3314
+ const system = String(systemPromptOf(modelCall(model, 0)));
3315
+ expect(system).toContain("details, stepper, checklist, gallery, list.");
3316
+ expect(system).toContain("A stepper callback name must be one of: scaleStepper.");
3317
+ expect(blocksEventOf(events)).toEqual({errors: [], ok: true, warnings: []});
3318
+ });
3319
+
3320
+ it("rejects a stepper whose callback is a host action that does not handle steppers", async () => {
3321
+ const model = createScriptedModel({steps: [textStep(stepperReply("approve"))]});
3322
+ const agent = await authAsUser(
3323
+ buildApp({model, uiBlocks: {hostActions: STEPPER_HOST_ACTIONS}}),
3324
+ "notAdmin"
3325
+ );
3326
+
3327
+ const {events} = await streamPrompt(agent, {prompt: USER_PROMPT});
3328
+
3329
+ const blocks = blocksEventOf(events);
3330
+ expect(blocks.ok).toBe(false);
3331
+ expect(blocks.errors.map((error) => ({code: error.code, path: error.path}))).toEqual([
3332
+ {code: "UNKNOWN_HOST_ACTION", path: "blocks[0].callback.name"},
3333
+ ]);
3334
+ const history = await loadHistory(await onlyHistoryId());
3335
+ expect(String(rowsOf(history).find((row) => row.type === "assistant")?.text)).toContain(
3336
+ "UNKNOWN_HOST_ACTION"
3337
+ );
3338
+ });
3339
+
3340
+ it("never mentions the stepper without a stepper action", async () => {
3341
+ const model = createScriptedModel({steps: [textStep(VALID_BLOCKS)]});
3342
+ const agent = await authAsUser(
3343
+ buildApp({model, uiBlocks: {hostActions: {approve: {}}}}),
3344
+ "notAdmin"
3345
+ );
3346
+
3347
+ await streamPrompt(agent, {prompt: USER_PROMPT});
3348
+
3349
+ expect(String(systemPromptOf(modelCall(model, 0)))).not.toContain("stepper");
3350
+ });
3351
+
3352
+ it("keeps the pre-rich-blocks prompt with richBlocks false but still accepts a stepper", async () => {
3353
+ const model = createScriptedModel({steps: [textStep(stepperReply("scaleStepper"))]});
3354
+ const agent = await authAsUser(
3355
+ buildApp({model, uiBlocks: {hostActions: STEPPER_HOST_ACTIONS, richBlocks: false}}),
3356
+ "notAdmin"
3357
+ );
3358
+
3359
+ const {events} = await streamPrompt(agent, {prompt: USER_PROMPT});
3360
+
3361
+ const system = String(systemPromptOf(modelCall(model, 0)));
3362
+ expect(system).not.toContain("stepper");
3363
+ expect(system).toContain(
3364
+ blocksPromptSection({hostActions: ["approve", "scaleStepper"], richBlocks: false})
3365
+ );
3366
+ expect(blocksEventOf(events)).toEqual({errors: [], ok: true, warnings: []});
3367
+ });
3279
3368
  });
@@ -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
  });
@@ -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
+ };