@camox/api-contract 0.59.0 → 0.61.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.
package/dist/threads.d.ts CHANGED
@@ -5,7 +5,9 @@ import { Message } from "@earendil-works/pi-ai";
5
5
  //#region src/threads.d.ts
6
6
  /**
7
7
  * The kind of the transcript entry recording an editor's message, with its sender and the
8
- * operation id the studio sent it with as data (`editorMessageDataSchema`).
8
+ * operation id the studio sent it with as data (`editorMessageDataSchema`). Its first text part
9
+ * is what the editor wrote; a second one, which only the model reads, says where they were in the
10
+ * studio as they sent it (`editorTextOf`).
9
11
  */
10
12
  export declare const EDITOR_MESSAGE_ENTRY = "camox.editor-message";
11
13
  /** The kind of the transcript entry recording that an editor stopped a run, with them as data. */
@@ -52,6 +54,14 @@ export type PushOutcome = z.infer<typeof pushOutcomeSchema>;
52
54
  * call of it went through ends on the draft's card.
53
55
  */
54
56
  export declare const START_DRAFT_TOOL = "startDraft";
57
+ /** The agent's tool building its code changes mid-turn, which the end of a turn also does. */
58
+ export declare const BUILD_TOOL = "build";
59
+ /**
60
+ * The tools whose calls change a draft: the content tools that write, and the code tools that
61
+ * write files. The model calls the code tools, and code mode scripts call both. A test in the API
62
+ * keeps this in step with the tools themselves.
63
+ */
64
+ export declare const CHANGING_TOOLS: ReadonlySet<string>;
55
65
  /** The draft a `startDraft` call started, as its result's details name it for the card. */
56
66
  export declare const startedDraftCardSchema: z.ZodObject<{
57
67
  draftId: z.ZodString;
@@ -274,21 +284,24 @@ export type ThreadMessage = {
274
284
  subjects: string[];
275
285
  outcome: PushOutcome;
276
286
  };
287
+ /**
288
+ * What the agent is doing during a run, as editors see it, without the details of how it works:
289
+ * - `thinking`: the model is called, and hasn't started writing a tool call.
290
+ * - `exploring`: it uses tools that only read, from writing a call until the model is called again.
291
+ * - `changing`: a call of that round changes the draft (`CHANGING_TOOLS`), as soon as it's written
292
+ * or, in a code mode script, as soon as the script calls one.
293
+ * - `building`: its `build` call runs, or the end of the turn builds its code changes.
294
+ * - `fixing`: the end of the turn's build failed, and the agent fixes it.
295
+ * - `startingDraft`: on Live, its `startDraft` call runs.
296
+ */
297
+ export type AgentActivity = "thinking" | "exploring" | "changing" | "building" | "fixing" | "startingDraft";
277
298
  /** Everything the studio shows for a thread. */
278
299
  export type ThreadView = {
279
300
  messages: readonly ThreadMessage[];
280
301
  /** Whether a run is going: the agent is working on a turn, and stopping would end it. */
281
302
  running: boolean;
282
- /**
283
- * While a run is going, where the build of the code its turn changed stands: the end of the
284
- * turn is building it, or it failed and the agent is fixing it.
285
- */
286
- status?: BuildStatus;
287
- /**
288
- * While a run is going, whether the agent is using its tools, code mode included, rather than
289
- * thinking: from writing a call until the model is called again.
290
- */
291
- usingTools?: true;
303
+ /** While a run is going, what the agent is doing. */
304
+ activity?: AgentActivity;
292
305
  };
293
306
  export declare const EMPTY_THREAD_VIEW: ThreadView;
294
307
  /**
@@ -299,10 +312,18 @@ export declare const EMPTY_THREAD_VIEW: ThreadView;
299
312
  export type ThreadState = {
300
313
  steps: readonly ThreadStep[];
301
314
  running: boolean;
302
- /** The model's calls still running, by call id. A tool's own calls aren't the agent's. */
303
- toolCalls?: readonly string[];
315
+ /** The model's calls still running. A tool's own calls aren't the agent's. */
316
+ toolCalls?: readonly {
317
+ callId: string;
318
+ name: string;
319
+ }[];
304
320
  /** Whether the model's latest message makes tool calls: it writes them, then they run. */
305
321
  callingTools?: boolean;
322
+ /**
323
+ * Whether a call of the latest round changes the draft, the model's or a script's: from writing
324
+ * or making it until the model is called again.
325
+ */
326
+ changing?: boolean;
306
327
  /** The drafts the thread's cards name, as the thread last looked them up. */
307
328
  drafts?: DraftCardStates;
308
329
  };
@@ -356,11 +377,68 @@ type ThreadStep = {
356
377
  {
357
378
  type: "continued";
358
379
  });
380
+ /**
381
+ * What an editor had selected on the page as they sent a message, unless they cleared it from
382
+ * the message: its path as the studio names it, from the block down to the selection, with the
383
+ * ids the agent's tools take. A record is the one the selection shows, a field the selection's
384
+ * own, of the record when a record is selected.
385
+ */
386
+ export declare const editorSelectionSchema: z.ZodObject<{
387
+ path: z.ZodArray<z.ZodString>;
388
+ blockId: z.ZodOptional<z.ZodString>;
389
+ itemId: z.ZodOptional<z.ZodString>;
390
+ recordId: z.ZodOptional<z.ZodString>;
391
+ fieldName: z.ZodOptional<z.ZodString>;
392
+ }, z.core.$strip>;
393
+ export type EditorSelection = z.infer<typeof editorSelectionSchema>;
394
+ /**
395
+ * Where an editor was in the studio as they sent a message, in the Locale they viewed: a Page,
396
+ * by its path after the Locale's prefix and its title, with its id for a curated page and its
397
+ * Layout's for a layout page, and what they had selected on it, or the Canvas with no page
398
+ * selected. A hint for the agent, never what the message is about.
399
+ */
400
+ export declare const editorWhereSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
401
+ on: z.ZodLiteral<"page">;
402
+ locale: z.ZodString;
403
+ path: z.ZodString;
404
+ title: z.ZodString;
405
+ pageId: z.ZodOptional<z.ZodString>;
406
+ layoutId: z.ZodOptional<z.ZodString>;
407
+ selection: z.ZodOptional<z.ZodObject<{
408
+ path: z.ZodArray<z.ZodString>;
409
+ blockId: z.ZodOptional<z.ZodString>;
410
+ itemId: z.ZodOptional<z.ZodString>;
411
+ recordId: z.ZodOptional<z.ZodString>;
412
+ fieldName: z.ZodOptional<z.ZodString>;
413
+ }, z.core.$strip>>;
414
+ }, z.core.$strip>, z.ZodObject<{
415
+ on: z.ZodLiteral<"canvas">;
416
+ locale: z.ZodString;
417
+ }, z.core.$strip>], "on">;
418
+ export type EditorWhere = z.infer<typeof editorWhereSchema>;
359
419
  /** Studio → thread. */
360
420
  export declare const threadClientMessageSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
361
421
  type: z.ZodLiteral<"send">;
362
422
  operationId: z.ZodString;
363
423
  text: z.ZodString;
424
+ where: z.ZodOptional<z.ZodDiscriminatedUnion<[z.ZodObject<{
425
+ on: z.ZodLiteral<"page">;
426
+ locale: z.ZodString;
427
+ path: z.ZodString;
428
+ title: z.ZodString;
429
+ pageId: z.ZodOptional<z.ZodString>;
430
+ layoutId: z.ZodOptional<z.ZodString>;
431
+ selection: z.ZodOptional<z.ZodObject<{
432
+ path: z.ZodArray<z.ZodString>;
433
+ blockId: z.ZodOptional<z.ZodString>;
434
+ itemId: z.ZodOptional<z.ZodString>;
435
+ recordId: z.ZodOptional<z.ZodString>;
436
+ fieldName: z.ZodOptional<z.ZodString>;
437
+ }, z.core.$strip>>;
438
+ }, z.core.$strip>, z.ZodObject<{
439
+ on: z.ZodLiteral<"canvas">;
440
+ locale: z.ZodString;
441
+ }, z.core.$strip>], "on">>;
364
442
  }, z.core.$strip>, z.ZodObject<{
365
443
  type: z.ZodLiteral<"stop">;
366
444
  }, z.core.$strip>, z.ZodObject<{
@@ -417,6 +495,11 @@ export type ThreadServerMessage =
417
495
  export declare const THREAD_ENDED_CLOSE_CODE = 4000;
418
496
  /** The text of a message's content: its text parts, joined. */
419
497
  export declare const textOf: (content: Message["content"]) => string;
498
+ /**
499
+ * What an editor wrote, of their message entry's content: its first text part. The rest, where
500
+ * they were in the studio, only the model reads.
501
+ */
502
+ export declare const editorTextOf: (content: Message["content"]) => string;
420
503
  /**
421
504
  * What the studio shows for a thread. A turn starts with an editor's message, or Camox's own
422
505
  * when it began the turn itself, and messages sent
@@ -426,7 +509,7 @@ export declare const textOf: (content: Message["content"]) => string;
426
509
  * run is over, or a later message started another run after its last model call came back
427
510
  * unfinished. While Pi retries a failed call, the run goes on, so the turn shows as working.
428
511
  */
429
- export declare function threadViewOf({ steps, running, toolCalls, callingTools, drafts }: ThreadState): ThreadView;
512
+ export declare function threadViewOf({ steps, running, toolCalls, callingTools, changing, drafts }: ThreadState): ThreadView;
430
513
  /** Folds one of Pi's agent events into the thread's state. */
431
514
  export declare function reduceThreadState(state: ThreadState, event: AgentEvent): ThreadState;
432
515
  /** Folds a batch of Pi's agent events into the thread's state. */
package/dist/threads.js CHANGED
@@ -3,7 +3,9 @@ import { z } from "zod";
3
3
  //#region src/threads.ts
4
4
  /**
5
5
  * The kind of the transcript entry recording an editor's message, with its sender and the
6
- * operation id the studio sent it with as data (`editorMessageDataSchema`).
6
+ * operation id the studio sent it with as data (`editorMessageDataSchema`). Its first text part
7
+ * is what the editor wrote; a second one, which only the model reads, says where they were in the
8
+ * studio as they sent it (`editorTextOf`).
7
9
  */
8
10
  const EDITOR_MESSAGE_ENTRY = "camox.editor-message";
9
11
  /** The kind of the transcript entry recording that an editor stopped a run, with them as data. */
@@ -49,6 +51,36 @@ const pushOutcomeSchema = z.enum([
49
51
  * call of it went through ends on the draft's card.
50
52
  */
51
53
  const START_DRAFT_TOOL = "startDraft";
54
+ /** The agent's tool building its code changes mid-turn, which the end of a turn also does. */
55
+ const BUILD_TOOL = "build";
56
+ /**
57
+ * The tools whose calls change a draft: the content tools that write, and the code tools that
58
+ * write files. The model calls the code tools, and code mode scripts call both. A test in the API
59
+ * keeps this in step with the tools themselves.
60
+ */
61
+ const CHANGING_TOOLS = /* @__PURE__ */ new Set([
62
+ "addBackToLocale",
63
+ "addLocale",
64
+ "createBlock",
65
+ "createCollectionRecord",
66
+ "createPage",
67
+ "deleteBlock",
68
+ "deleteCollectionRecord",
69
+ "deletePage",
70
+ "editBlock",
71
+ "editCollectionRecord",
72
+ "moveBlock",
73
+ "removeLocale",
74
+ "setPageLayout",
75
+ "setPageSeo",
76
+ "updateFile",
77
+ "updatePage",
78
+ "uploadFile",
79
+ "edit",
80
+ "mv",
81
+ "rm",
82
+ "write"
83
+ ]);
52
84
  /** The draft a `startDraft` call started, as its result's details name it for the card. */
53
85
  const startedDraftCardSchema = z.object({
54
86
  draftId: z.string(),
@@ -140,12 +172,44 @@ const EMPTY_THREAD_STATE = {
140
172
  steps: [],
141
173
  running: false
142
174
  };
175
+ /**
176
+ * What an editor had selected on the page as they sent a message, unless they cleared it from
177
+ * the message: its path as the studio names it, from the block down to the selection, with the
178
+ * ids the agent's tools take. A record is the one the selection shows, a field the selection's
179
+ * own, of the record when a record is selected.
180
+ */
181
+ const editorSelectionSchema = z.object({
182
+ path: z.array(z.string().max(512)).min(1).max(16),
183
+ blockId: z.string().max(64).optional(),
184
+ itemId: z.string().max(64).optional(),
185
+ recordId: z.string().max(64).optional(),
186
+ fieldName: z.string().max(256).optional()
187
+ });
188
+ /**
189
+ * Where an editor was in the studio as they sent a message, in the Locale they viewed: a Page,
190
+ * by its path after the Locale's prefix and its title, with its id for a curated page and its
191
+ * Layout's for a layout page, and what they had selected on it, or the Canvas with no page
192
+ * selected. A hint for the agent, never what the message is about.
193
+ */
194
+ const editorWhereSchema = z.discriminatedUnion("on", [z.object({
195
+ on: z.literal("page"),
196
+ locale: z.string().max(64),
197
+ path: z.string().max(2048),
198
+ title: z.string().max(512),
199
+ pageId: z.string().max(64).optional(),
200
+ layoutId: z.string().max(256).optional(),
201
+ selection: editorSelectionSchema.optional()
202
+ }), z.object({
203
+ on: z.literal("canvas"),
204
+ locale: z.string().max(64)
205
+ })]);
143
206
  /** Studio → thread. */
144
207
  const threadClientMessageSchema = z.discriminatedUnion("type", [
145
208
  z.object({
146
209
  type: z.literal("send"),
147
210
  operationId: z.string().min(1),
148
- text: z.string().min(1)
211
+ text: z.string().min(1),
212
+ where: editorWhereSchema.optional()
149
213
  }),
150
214
  z.object({ type: z.literal("stop") }),
151
215
  z.object({ type: z.literal("resync") })
@@ -157,6 +221,11 @@ const threadClientMessageSchema = z.discriminatedUnion("type", [
157
221
  const THREAD_ENDED_CLOSE_CODE = 4e3;
158
222
  /** The text of a message's content: its text parts, joined. */
159
223
  const textOf = (content) => typeof content === "string" ? content : content.flatMap((part) => part.type === "text" ? [part.text] : []).join("").trim();
224
+ /**
225
+ * What an editor wrote, of their message entry's content: its first text part. The rest, where
226
+ * they were in the studio, only the model reads.
227
+ */
228
+ const editorTextOf = (content) => typeof content === "string" ? content : (content.find((part) => part.type === "text")?.text ?? "").trim();
160
229
  /** What an assistant message is to the thread: a final answer, a tool round, or neither. */
161
230
  function assistantStep(id, message) {
162
231
  if (message.content.some((part) => part.type === "toolCall")) return {
@@ -218,7 +287,7 @@ function threadStepOf(entry) {
218
287
  id,
219
288
  type: "editor",
220
289
  sender,
221
- text: textOf(message.content),
290
+ text: editorTextOf(message.content),
222
291
  ...handedOffTo && { handedOffTo }
223
292
  };
224
293
  }
@@ -262,7 +331,7 @@ function threadStepOf(entry) {
262
331
  * run is over, or a later message started another run after its last model call came back
263
332
  * unfinished. While Pi retries a failed call, the run goes on, so the turn shows as working.
264
333
  */
265
- function threadViewOf({ steps, running, toolCalls = [], callingTools = false, drafts = {} }) {
334
+ function threadViewOf({ steps, running, toolCalls = [], callingTools = false, changing = false, drafts = {} }) {
266
335
  const messages = [];
267
336
  let waiting;
268
337
  let unfinished = false;
@@ -378,14 +447,35 @@ function threadViewOf({ steps, running, toolCalls = [], callingTools = false, dr
378
447
  case "unfinished": unfinished = true;
379
448
  }
380
449
  if (!running) fail();
381
- const usingTools = running && (callingTools || toolCalls.length > 0);
450
+ if (!running) return {
451
+ messages,
452
+ running
453
+ };
382
454
  return {
383
455
  messages,
384
456
  running,
385
- ...running && status && { status },
386
- ...usingTools && { usingTools }
457
+ activity: activityOf({
458
+ status,
459
+ toolCalls,
460
+ callingTools,
461
+ changing
462
+ })
387
463
  };
388
464
  }
465
+ /** What the agent is doing during a run, from where the turn's build stands and its tool calls. */
466
+ function activityOf({ status, toolCalls, callingTools, changing }) {
467
+ const calling = (name) => toolCalls.some((call) => call.name === name);
468
+ if (status === "building" || calling("build")) return "building";
469
+ if (status === "fixing") return "fixing";
470
+ if (calling("startDraft")) return "startingDraft";
471
+ if (changing) return "changing";
472
+ if (callingTools || toolCalls.length > 0) return "exploring";
473
+ return "thinking";
474
+ }
475
+ /** Whether a tool call changes the draft. */
476
+ const changes = (name) => CHANGING_TOOLS.has(name);
477
+ /** Whether a block of the model's message is a call of a tool that changes the draft. */
478
+ const callsChangingTool = (block) => block.type === "toolCall" && changes(block.name);
389
479
  const append = (state, entry) => {
390
480
  const step = entry && threadStepOf(entry);
391
481
  if (!step || state.steps.some((known) => known.id === step.id)) return state;
@@ -397,13 +487,21 @@ const append = (state, entry) => {
397
487
  /** Folds one of Pi's agent events into the thread's state. */
398
488
  function reduceThreadState(state, event) {
399
489
  switch (event.type) {
400
- case "snapshot": return {
401
- ...state,
402
- steps: event.entries.flatMap((entry) => threadStepOf(entry) ?? []),
403
- running: event.run !== void 0,
404
- toolCalls: event.tools.flatMap((tool) => tool.status === "done" ? [] : [tool.callId]),
405
- callingTools: !!event.generation?.message?.content.some((part) => part.type === "toolCall")
406
- };
490
+ case "snapshot": {
491
+ const toolCalls = event.tools.flatMap((tool) => tool.status === "done" ? [] : [{
492
+ callId: tool.callId,
493
+ name: tool.name
494
+ }]);
495
+ const generated = event.generation?.message?.content ?? [];
496
+ return {
497
+ ...state,
498
+ steps: event.entries.flatMap((entry) => threadStepOf(entry) ?? []),
499
+ running: event.run !== void 0,
500
+ toolCalls,
501
+ callingTools: generated.some((part) => part.type === "toolCall"),
502
+ changing: generated.some(callsChangingTool) || toolCalls.some((call) => changes(call.name)) || event.nestedTools.some((nested) => changes(nested.name) && toolCalls.some((call) => nested.parentCallId === call.callId || nested.parentCallId.startsWith(`${call.callId}/`)))
503
+ };
504
+ }
407
505
  case "run_start": return {
408
506
  ...state,
409
507
  running: true
@@ -412,25 +510,41 @@ function reduceThreadState(state, event) {
412
510
  ...state,
413
511
  running: false,
414
512
  toolCalls: [],
415
- callingTools: false
513
+ callingTools: false,
514
+ changing: false
416
515
  };
417
516
  case "message_start": return event.message.role === "assistant" ? {
418
517
  ...state,
419
- callingTools: false
420
- } : state;
421
- case "message_update": return event.changes.some((change) => change.type === "toolcall_start") ? {
422
- ...state,
423
- callingTools: true
518
+ callingTools: false,
519
+ changing: false
424
520
  } : state;
425
- case "tool_execution_start":
426
- if (event.parentToolCallId !== void 0) return state;
521
+ case "message_update": {
522
+ const calls = event.changes.flatMap((change) => change.type === "toolcall_start" || change.type === "block" ? [change.block] : change.type === "message" ? change.message.content : []);
523
+ if (calls.length === 0) return state;
427
524
  return {
428
525
  ...state,
429
- toolCalls: [...state.toolCalls ?? [], event.toolCallId]
526
+ callingTools: state.callingTools || calls.some((block) => block.type === "toolCall"),
527
+ changing: state.changing || calls.some(callsChangingTool)
430
528
  };
529
+ }
530
+ case "tool_execution_start": {
531
+ const changing = state.changing || changes(event.toolName);
532
+ if (event.parentToolCallId !== void 0) return {
533
+ ...state,
534
+ changing
535
+ };
536
+ return {
537
+ ...state,
538
+ changing,
539
+ toolCalls: [...state.toolCalls ?? [], {
540
+ callId: event.toolCallId,
541
+ name: event.toolName
542
+ }]
543
+ };
544
+ }
431
545
  case "tool_execution_end": return append({
432
546
  ...state,
433
- toolCalls: state.toolCalls?.filter((callId) => callId !== event.toolCallId)
547
+ toolCalls: state.toolCalls?.filter((call) => call.callId !== event.toolCallId)
434
548
  }, event.entry);
435
549
  case "message_end":
436
550
  case "entry_appended": return append(state, event.entry);
@@ -452,4 +566,4 @@ function reduceThreadFrame(state, frame) {
452
566
  }
453
567
 
454
568
  //#endregion
455
- export { BRIEF_ENTRY, BUILD_STATUS_ENTRY, DRAFT_ENDED_ENTRY, EDITOR_MESSAGE_ENTRY, EMPTY_THREAD_STATE, EMPTY_THREAD_VIEW, PUSH_ENTRY, RECONCILE_ENTRY, RUN_STOPPED_ENTRY, START_DRAFT_TOOL, THREAD_ENDED_CLOSE_CODE, buildStatusDataSchema, buildStatusSchema, editorMessageDataSchema, isSender, pushDataSchema, pushOutcomeSchema, reconcileDataSchema, reduceThreadEvents, reduceThreadFrame, reduceThreadState, startedDraftCardSchema, startedDraftSchema, textOf, threadClientMessageSchema, threadSenderSchema, threadViewOf, touchKindSchema, touchSchema, touchedBatchSchema };
569
+ export { BRIEF_ENTRY, BUILD_STATUS_ENTRY, BUILD_TOOL, CHANGING_TOOLS, DRAFT_ENDED_ENTRY, EDITOR_MESSAGE_ENTRY, EMPTY_THREAD_STATE, EMPTY_THREAD_VIEW, PUSH_ENTRY, RECONCILE_ENTRY, RUN_STOPPED_ENTRY, START_DRAFT_TOOL, THREAD_ENDED_CLOSE_CODE, buildStatusDataSchema, buildStatusSchema, editorMessageDataSchema, editorSelectionSchema, editorTextOf, editorWhereSchema, isSender, pushDataSchema, pushOutcomeSchema, reconcileDataSchema, reduceThreadEvents, reduceThreadFrame, reduceThreadState, startedDraftCardSchema, startedDraftSchema, textOf, threadClientMessageSchema, threadSenderSchema, threadViewOf, touchKindSchema, touchSchema, touchedBatchSchema };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@camox/api-contract",
3
- "version": "0.59.0",
3
+ "version": "0.61.0",
4
4
  "license": "MIT",
5
5
  "files": [
6
6
  "src",
@@ -17,10 +17,13 @@ export function templateFields(template: string): string[] {
17
17
  */
18
18
  export function nonStringTemplateFields(
19
19
  template: string,
20
- properties: Readonly<Record<string, object | undefined>>,
20
+ properties: Readonly<Record<string, unknown>>,
21
21
  ): string[] {
22
- const isString = (field: object | undefined) =>
23
- field !== undefined && "fieldType" in field && field.fieldType === "String";
22
+ const isString = (field: unknown) =>
23
+ typeof field === "object" &&
24
+ field !== null &&
25
+ "fieldType" in field &&
26
+ field.fieldType === "String";
24
27
  return templateFields(template).filter((name) => !isString(properties[name]));
25
28
  }
26
29
 
@@ -15,8 +15,10 @@ export function isSystemOrderKey(key: string): key is SystemOrderKey {
15
15
  }
16
16
 
17
17
  /** Whether a reference list field is query-backed: code defines its records, nothing is stored. */
18
- export function isQueryBacked(field: object | null | undefined) {
19
- return !!field && (field as { query?: unknown }).query !== undefined;
18
+ export function isQueryBacked(field: unknown) {
19
+ return (
20
+ typeof field === "object" && field !== null && "query" in field && field.query !== undefined
21
+ );
20
22
  }
21
23
 
22
24
  /**