@sixb/app 0.2.4 → 0.2.5

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 (247) hide show
  1. package/assets/connector-icons/README.md +1 -0
  2. package/assets/connector-icons/ctm.png +0 -0
  3. package/dist/agent/activity-label.d.ts.map +1 -1
  4. package/dist/agent/agent-chat.d.ts +3 -1
  5. package/dist/agent/agent-chat.d.ts.map +1 -1
  6. package/dist/agent/agent-panel.d.ts.map +1 -1
  7. package/dist/agent/artifact-panel.d.ts +3 -1
  8. package/dist/agent/artifact-panel.d.ts.map +1 -1
  9. package/dist/agent/components/composer-settings.d.ts +1 -1
  10. package/dist/agent/components/composer-settings.d.ts.map +1 -1
  11. package/dist/agent/components/composer.d.ts +22 -5
  12. package/dist/agent/components/composer.d.ts.map +1 -1
  13. package/dist/agent/components/context-meter.d.ts +1 -1
  14. package/dist/agent/components/context-meter.d.ts.map +1 -1
  15. package/dist/agent/components/conversation-panel.d.ts +5 -2
  16. package/dist/agent/components/conversation-panel.d.ts.map +1 -1
  17. package/dist/agent/components/message-parts.d.ts +2 -1
  18. package/dist/agent/components/message-parts.d.ts.map +1 -1
  19. package/dist/agent/components/message-view.d.ts.map +1 -1
  20. package/dist/agent/components/pending-messages.d.ts +20 -0
  21. package/dist/agent/components/pending-messages.d.ts.map +1 -0
  22. package/dist/agent/components/sandbox-recovery.d.ts +11 -2
  23. package/dist/agent/components/sandbox-recovery.d.ts.map +1 -1
  24. package/dist/agent/components/threads-button.d.ts +1 -1
  25. package/dist/agent/components/threads-button.d.ts.map +1 -1
  26. package/dist/agent/components/transcript.d.ts.map +1 -1
  27. package/dist/agent/conversation.d.ts +3 -6
  28. package/dist/agent/conversation.d.ts.map +1 -1
  29. package/dist/agent/delegation/interpret.d.ts +5 -0
  30. package/dist/agent/delegation/interpret.d.ts.map +1 -1
  31. package/dist/agent/delegation/use-children.d.ts.map +1 -1
  32. package/dist/agent/hooks/use-agent-conversation.d.ts +10 -4
  33. package/dist/agent/hooks/use-agent-conversation.d.ts.map +1 -1
  34. package/dist/agent/hooks/use-thread-lists.d.ts.map +1 -1
  35. package/dist/agent/hooks/use-thread-stream.d.ts.map +1 -1
  36. package/dist/agent/i18n/en.d.ts +68 -0
  37. package/dist/agent/i18n/en.d.ts.map +1 -1
  38. package/dist/agent/i18n/fr.d.ts.map +1 -1
  39. package/dist/agent/i18n/index.d.ts +114 -0
  40. package/dist/agent/i18n/index.d.ts.map +1 -1
  41. package/dist/agent/inbox-handoff.d.ts +10 -0
  42. package/dist/agent/inbox-handoff.d.ts.map +1 -0
  43. package/dist/agent/live-run.d.ts +8 -1
  44. package/dist/agent/live-run.d.ts.map +1 -1
  45. package/dist/agent/search/interpret.d.ts +101 -0
  46. package/dist/agent/search/interpret.d.ts.map +1 -0
  47. package/dist/agent/search/search-tool-view.d.ts +10 -0
  48. package/dist/agent/search/search-tool-view.d.ts.map +1 -0
  49. package/dist/agent/thread-state.d.ts +26 -0
  50. package/dist/agent/thread-state.d.ts.map +1 -0
  51. package/dist/agent/tool-views/built-in.d.ts.map +1 -1
  52. package/dist/agent/tool-views/registry.d.ts +5 -6
  53. package/dist/agent/tool-views/registry.d.ts.map +1 -1
  54. package/dist/agent/types.d.ts +5 -1
  55. package/dist/agent/types.d.ts.map +1 -1
  56. package/dist/agent/view-file/view-file-tool-view.d.ts +2 -1
  57. package/dist/agent/view-file/view-file-tool-view.d.ts.map +1 -1
  58. package/dist/artifacts/card-preview.d.ts.map +1 -1
  59. package/dist/artifacts/editor/block-handle.d.ts +20 -0
  60. package/dist/artifacts/editor/block-handle.d.ts.map +1 -0
  61. package/dist/artifacts/editor/blocks.d.ts +34 -0
  62. package/dist/artifacts/editor/blocks.d.ts.map +1 -0
  63. package/dist/artifacts/editor/callout-view.d.ts +7 -0
  64. package/dist/artifacts/editor/callout-view.d.ts.map +1 -0
  65. package/dist/artifacts/editor/document-editor.d.ts +30 -0
  66. package/dist/artifacts/editor/document-editor.d.ts.map +1 -0
  67. package/dist/artifacts/editor/figure-view.d.ts +12 -0
  68. package/dist/artifacts/editor/figure-view.d.ts.map +1 -0
  69. package/dist/artifacts/editor/find-blocks.d.ts +6 -0
  70. package/dist/artifacts/editor/find-blocks.d.ts.map +1 -0
  71. package/dist/artifacts/editor/markdown-document.d.ts +12 -0
  72. package/dist/artifacts/editor/markdown-document.d.ts.map +1 -0
  73. package/dist/artifacts/editor/messages.d.ts +4 -0
  74. package/dist/artifacts/editor/messages.d.ts.map +1 -0
  75. package/dist/artifacts/editor/schema.d.ts +21 -0
  76. package/dist/artifacts/editor/schema.d.ts.map +1 -0
  77. package/dist/artifacts/editor/selection-toolbar.d.ts +35 -0
  78. package/dist/artifacts/editor/selection-toolbar.d.ts.map +1 -0
  79. package/dist/artifacts/editor/slash-menu.d.ts +43 -0
  80. package/dist/artifacts/editor/slash-menu.d.ts.map +1 -0
  81. package/dist/artifacts/editor/upload.d.ts +12 -0
  82. package/dist/artifacts/editor/upload.d.ts.map +1 -0
  83. package/dist/artifacts/file-source.d.ts +5 -0
  84. package/dist/artifacts/file-source.d.ts.map +1 -1
  85. package/dist/artifacts/frame-editor/bundle.d.ts +7 -0
  86. package/dist/artifacts/frame-editor/bundle.d.ts.map +1 -0
  87. package/dist/artifacts/frame-editor/commands.d.ts +93 -0
  88. package/dist/artifacts/frame-editor/commands.d.ts.map +1 -0
  89. package/dist/artifacts/frame-editor/entry.d.ts +2 -0
  90. package/dist/artifacts/frame-editor/entry.d.ts.map +1 -0
  91. package/dist/artifacts/frame-editor/frame.d.ts +5 -0
  92. package/dist/artifacts/frame-editor/frame.d.ts.map +1 -0
  93. package/dist/artifacts/frame-editor/keys.d.ts +8 -0
  94. package/dist/artifacts/frame-editor/keys.d.ts.map +1 -0
  95. package/dist/artifacts/frame-editor/protocol.d.ts +186 -0
  96. package/dist/artifacts/frame-editor/protocol.d.ts.map +1 -0
  97. package/dist/artifacts/frame-editor/schema.d.ts +34 -0
  98. package/dist/artifacts/frame-editor/schema.d.ts.map +1 -0
  99. package/dist/artifacts/frame-editor/style.d.ts +2 -0
  100. package/dist/artifacts/frame-editor/style.d.ts.map +1 -0
  101. package/dist/artifacts/frame.d.ts +2 -2
  102. package/dist/artifacts/frame.d.ts.map +1 -1
  103. package/dist/artifacts/live/boards.d.ts +2 -0
  104. package/dist/artifacts/live/boards.d.ts.map +1 -1
  105. package/dist/artifacts/live/document.d.ts +32 -0
  106. package/dist/artifacts/live/document.d.ts.map +1 -0
  107. package/dist/artifacts/live/header.d.ts +4 -4
  108. package/dist/artifacts/live/header.d.ts.map +1 -1
  109. package/dist/artifacts/live/presence.d.ts.map +1 -1
  110. package/dist/artifacts/live/session.d.ts +2 -0
  111. package/dist/artifacts/live/session.d.ts.map +1 -1
  112. package/dist/artifacts/live/view.d.ts +4 -3
  113. package/dist/artifacts/live/view.d.ts.map +1 -1
  114. package/dist/artifacts/messages.d.ts +226 -2
  115. package/dist/artifacts/messages.d.ts.map +1 -1
  116. package/dist/artifacts/pdf.d.ts +8 -0
  117. package/dist/artifacts/pdf.d.ts.map +1 -0
  118. package/dist/artifacts/queries.d.ts +80 -0
  119. package/dist/artifacts/queries.d.ts.map +1 -1
  120. package/dist/artifacts/references/references.d.ts +3 -3
  121. package/dist/artifacts/references/references.d.ts.map +1 -1
  122. package/dist/artifacts/renderers/canvas/board-editor.d.ts.map +1 -1
  123. package/dist/artifacts/renderers/canvas/comment-pins.d.ts +4 -2
  124. package/dist/artifacts/renderers/canvas/comment-pins.d.ts.map +1 -1
  125. package/dist/artifacts/renderers/canvas/comments-panel.d.ts.map +1 -1
  126. package/dist/artifacts/renderers/canvas/comments.d.ts +2 -1
  127. package/dist/artifacts/renderers/canvas/comments.d.ts.map +1 -1
  128. package/dist/artifacts/renderers/designed-document.d.ts +28 -0
  129. package/dist/artifacts/renderers/designed-document.d.ts.map +1 -0
  130. package/dist/artifacts/renderers/document-tree.d.ts +2 -0
  131. package/dist/artifacts/renderers/document-tree.d.ts.map +1 -1
  132. package/dist/artifacts/renderers/document.d.ts +9 -1
  133. package/dist/artifacts/renderers/document.d.ts.map +1 -1
  134. package/dist/artifacts/renderers/live-frame.d.ts +145 -0
  135. package/dist/artifacts/renderers/live-frame.d.ts.map +1 -0
  136. package/dist/artifacts/renderers/page-editor.d.ts +16 -0
  137. package/dist/artifacts/renderers/page-editor.d.ts.map +1 -0
  138. package/dist/artifacts/renderers/page.d.ts +10 -1
  139. package/dist/artifacts/renderers/page.d.ts.map +1 -1
  140. package/dist/artifacts/viewer.d.ts.map +1 -1
  141. package/dist/{chunk-kymx9y5e.js → chunk-4fex83k2.js} +10874 -4690
  142. package/dist/chunk-4fex83k2.js.map +239 -0
  143. package/dist/{chunk-yrcm1pbg.js → chunk-4wm09prf.js} +491 -2128
  144. package/dist/chunk-4wm09prf.js.map +27 -0
  145. package/dist/{chunk-pxd07kvx.js → chunk-5sgac68y.js} +1 -1
  146. package/dist/{chunk-970s5nkn.js → chunk-69qgh20f.js} +1 -1
  147. package/dist/chunk-y51c4tsz.js +11 -0
  148. package/dist/chunk-y51c4tsz.js.map +10 -0
  149. package/dist/index.d.ts +2 -1
  150. package/dist/index.d.ts.map +1 -1
  151. package/dist/index.js +11 -6
  152. package/dist/index.js.map +5 -5
  153. package/dist/pages/console/data/messages.d.ts +6 -0
  154. package/dist/pages/console/data/messages.d.ts.map +1 -1
  155. package/dist/pages/console/data/shared.d.ts.map +1 -1
  156. package/dist/pages.js +12 -5
  157. package/dist/pages.js.map +6 -6
  158. package/dist/shell/search-command.d.ts.map +1 -1
  159. package/package.json +22 -5
  160. package/src/agent/activity-label.ts +3 -0
  161. package/src/agent/agent-chat.tsx +56 -37
  162. package/src/agent/agent-execution-trace.tsx +2 -2
  163. package/src/agent/agent-panel.tsx +6 -4
  164. package/src/agent/agent.css +254 -0
  165. package/src/agent/artifact-panel.tsx +77 -22
  166. package/src/agent/components/composer-settings.tsx +20 -10
  167. package/src/agent/components/composer.tsx +342 -104
  168. package/src/agent/components/context-meter.tsx +7 -3
  169. package/src/agent/components/conversation-panel.tsx +69 -29
  170. package/src/agent/components/message-parts.tsx +367 -59
  171. package/src/agent/components/message-view.tsx +12 -2
  172. package/src/agent/components/pending-messages.tsx +338 -0
  173. package/src/agent/components/sandbox-recovery.tsx +36 -17
  174. package/src/agent/components/threads-button.tsx +3 -2
  175. package/src/agent/components/transcript.tsx +63 -34
  176. package/src/agent/conversation.ts +203 -54
  177. package/src/agent/delegation/interpret.ts +31 -7
  178. package/src/agent/hooks/use-agent-conversation.ts +348 -100
  179. package/src/agent/hooks/use-thread-stream.ts +10 -0
  180. package/src/agent/i18n/en.ts +87 -11
  181. package/src/agent/i18n/fr.ts +83 -12
  182. package/src/agent/inbox-handoff.ts +47 -0
  183. package/src/agent/live-run.ts +23 -2
  184. package/src/agent/react-router.tsx +2 -2
  185. package/src/agent/search/interpret.ts +367 -0
  186. package/src/agent/search/search-tool-view.tsx +235 -0
  187. package/src/agent/thread-state.ts +114 -0
  188. package/src/agent/tool-views/built-in.tsx +16 -0
  189. package/src/agent/tool-views/registry.tsx +28 -2
  190. package/src/agent/types.ts +7 -0
  191. package/src/agent/view-file/view-file-tool-view.tsx +33 -19
  192. package/src/artifacts/artifacts.css +398 -0
  193. package/src/artifacts/card-preview.tsx +6 -1
  194. package/src/artifacts/editor/block-handle.tsx +278 -0
  195. package/src/artifacts/editor/blocks.ts +216 -0
  196. package/src/artifacts/editor/callout-view.tsx +31 -0
  197. package/src/artifacts/editor/document-editor.tsx +484 -0
  198. package/src/artifacts/editor/figure-view.tsx +66 -0
  199. package/src/artifacts/editor/find-blocks.ts +19 -0
  200. package/src/artifacts/editor/markdown-document.tsx +72 -0
  201. package/src/artifacts/editor/messages.ts +4 -0
  202. package/src/artifacts/editor/schema.ts +159 -0
  203. package/src/artifacts/editor/selection-toolbar.tsx +364 -0
  204. package/src/artifacts/editor/slash-menu.tsx +251 -0
  205. package/src/artifacts/editor/upload.ts +40 -0
  206. package/src/artifacts/file-source.ts +11 -0
  207. package/src/artifacts/frame-editor/bundle.ts +8 -0
  208. package/src/artifacts/frame-editor/commands.ts +565 -0
  209. package/src/artifacts/frame-editor/entry.ts +6 -0
  210. package/src/artifacts/frame-editor/frame.ts +847 -0
  211. package/src/artifacts/frame-editor/generated/editor.txt +149 -0
  212. package/src/artifacts/frame-editor/generated.d.ts +5 -0
  213. package/src/artifacts/frame-editor/keys.ts +323 -0
  214. package/src/artifacts/frame-editor/protocol.ts +151 -0
  215. package/src/artifacts/frame-editor/schema.ts +293 -0
  216. package/src/artifacts/frame-editor/style.ts +58 -0
  217. package/src/artifacts/frame.tsx +11 -3
  218. package/src/artifacts/live/boards.ts +46 -3
  219. package/src/artifacts/live/document.ts +89 -0
  220. package/src/artifacts/live/header.tsx +9 -14
  221. package/src/artifacts/live/presence.ts +5 -1
  222. package/src/artifacts/live/session.ts +4 -2
  223. package/src/artifacts/live/view.ts +14 -8
  224. package/src/artifacts/messages.ts +138 -2
  225. package/src/artifacts/pdf.ts +50 -0
  226. package/src/artifacts/references/references.tsx +46 -11
  227. package/src/artifacts/renderers/canvas/board-editor.tsx +11 -144
  228. package/src/artifacts/renderers/canvas/comment-pins.tsx +5 -0
  229. package/src/artifacts/renderers/canvas/comments-panel.tsx +58 -1
  230. package/src/artifacts/renderers/canvas/comments.ts +8 -4
  231. package/src/artifacts/renderers/designed-document.tsx +277 -0
  232. package/src/artifacts/renderers/document-tree.ts +9 -0
  233. package/src/artifacts/renderers/document.tsx +13 -102
  234. package/src/artifacts/renderers/live-frame.ts +163 -0
  235. package/src/artifacts/renderers/page-editor.tsx +613 -0
  236. package/src/artifacts/renderers/page.tsx +21 -4
  237. package/src/artifacts/viewer.tsx +87 -41
  238. package/src/index.ts +6 -0
  239. package/src/pages/console/data/messages.ts +6 -0
  240. package/src/pages/console/data/projections.tsx +4 -0
  241. package/src/pages/console/data/shared.tsx +1 -0
  242. package/src/pages/console/ontology/lineage/trail.tsx +3 -0
  243. package/src/shell/search-command.tsx +2 -0
  244. package/dist/chunk-kymx9y5e.js.map +0 -211
  245. package/dist/chunk-yrcm1pbg.js.map +0 -34
  246. /package/dist/{chunk-pxd07kvx.js.map → chunk-5sgac68y.js.map} +0 -0
  247. /package/dist/{chunk-970s5nkn.js.map → chunk-69qgh20f.js.map} +0 -0
@@ -1,6 +1,6 @@
1
1
  import { type FileSource, messageFileSource } from "../artifacts/file-source"
2
2
  import { isApprovalOutcomeMessage } from "./approvals"
3
- import type { LiveRunState } from "./live-run"
3
+ import type { LiveInboxRead, LiveRunState } from "./live-run"
4
4
  import { knownName, type MessageAuthor, principalKey } from "./message-authors"
5
5
  import {
6
6
  type NormalizedFile,
@@ -80,8 +80,9 @@ export interface AgentConversationMessage {
80
80
  readonly id: string | null
81
81
  readonly role: "user" | "assistant"
82
82
  /**
83
- * `pending`: sent, not saved yet. `streaming`: a reply that still comes from its run, until its
84
- * saved copy replaces it. `complete`: everything else.
83
+ * `pending`: sent, not saved yet. `streaming`: from the run's live stream until its saved copy
84
+ * replaces it: the reply, and a message the run read while answering, where it read it.
85
+ * `complete`: everything else.
85
86
  */
86
87
  readonly status: "pending" | "streaming" | "complete"
87
88
  /** The run that wrote a reply. */
@@ -153,26 +154,48 @@ export function conversationMessages(input: ConversationMessagesInput): AgentCon
153
154
  for (const run of input.runs ?? []) {
154
155
  if (run.status === "failed" || run.status === "cancelled") ended.set(run.id, run.status)
155
156
  }
157
+ // A turn that read messages as it answered holds a reply on each side of them: only its last one
158
+ // says how the turn ended, as its live copy did.
159
+ const lastReplyOfRun = new Map<string, string>()
160
+ for (const message of input.messages) {
161
+ if (message.role === "assistant" && message.runId !== null) {
162
+ lastReplyOfRun.set(message.runId, message.id)
163
+ }
164
+ }
156
165
 
166
+ let lastUserMessageId: string | null = null
157
167
  for (const message of input.messages) {
158
168
  if (message.role === "system") continue
159
169
 
160
170
  savedIds.add(message.id)
161
171
  if (message.role === "assistant" && message.runId !== null) answeredRuns.add(message.runId)
172
+ if (message.role === "user") lastUserMessageId = message.id
162
173
 
163
174
  // Continue writes the model a message the reader never wrote: nothing to draw. Neither is the
164
175
  // one a decision resumes a turn with: its calls' cards say how they ended, and the resumed
165
176
  // answer stays right under the one it continues.
166
177
  if (isContinuation(message) || isApprovalOutcomeMessage(message)) continue
167
178
 
168
- // One reply per run: a second one, which nothing writes today, keeps a key of its own.
179
+ // A run's reply after a message it read keeps the key its live copy had (see liveMessages).
169
180
  const preferred = savedKey(message, input.sentKeys)
170
- const key = keys.has(preferred) ? `message:${message.id}` : preferred
181
+ const after =
182
+ message.role === "assistant" && lastUserMessageId !== null
183
+ ? `${preferred}:after:${lastUserMessageId}`
184
+ : null
185
+ const key = !keys.has(preferred)
186
+ ? preferred
187
+ : after !== null && !keys.has(after)
188
+ ? after
189
+ : `message:${message.id}`
171
190
  keys.add(key)
172
191
 
173
192
  const saved = savedMessage(message, key, authorOf(message.authorPrincipal, input), input)
174
193
  const unfinished =
175
- message.role === "assistant" && message.runId !== null ? ended.get(message.runId) : undefined
194
+ message.role === "assistant" &&
195
+ message.runId !== null &&
196
+ lastReplyOfRun.get(message.runId) === message.id
197
+ ? ended.get(message.runId)
198
+ : undefined
176
199
  result.push(unfinished ? unfinishedMessage(saved, unfinished) : saved)
177
200
  }
178
201
 
@@ -181,8 +204,7 @@ export function conversationMessages(input: ConversationMessagesInput): AgentCon
181
204
  result.push(pendingMessage(pending, input))
182
205
  }
183
206
 
184
- const live = liveReply(input, savedIds, answeredRuns)
185
- if (live) result.push(live)
207
+ result.push(...liveMessages(input, savedIds, answeredRuns))
186
208
 
187
209
  return result
188
210
  }
@@ -239,7 +261,7 @@ interface SavedEntry {
239
261
  readonly message: AgentConversationMessage
240
262
  }
241
263
 
242
- const savedEntries = new WeakMap<AgentMessage, SavedEntry>()
264
+ const savedEntries = new WeakMap<AgentMessage, readonly SavedEntry[]>()
243
265
 
244
266
  function savedMessage(
245
267
  message: AgentMessage,
@@ -248,15 +270,11 @@ function savedMessage(
248
270
  input: ConversationMessagesInput
249
271
  ): AgentConversationMessage {
250
272
  const observed = Boolean(input.observed)
251
- const cached = savedEntries.get(message)
252
- if (
253
- cached &&
254
- cached.key === key &&
255
- cached.observed === observed &&
256
- sameAuthor(cached.author, author)
257
- ) {
258
- return cached.message
259
- }
273
+ const existing = savedEntries.get(message)
274
+ const hit = existing?.find(
275
+ (entry) => entry.key === key && entry.observed === observed && sameAuthor(entry.author, author)
276
+ )
277
+ if (hit) return hit.message
260
278
 
261
279
  const fileSource = (fileRef: AgentFileRef, path: string) =>
262
280
  messageFileSource({
@@ -284,7 +302,8 @@ function savedMessage(
284
302
  ...(message.compaction ? { compaction: message.compaction } : {}),
285
303
  }
286
304
 
287
- savedEntries.set(message, { key, author, observed, message: converted })
305
+ const nextEntry: SavedEntry = { key, author, observed, message: converted }
306
+ savedEntries.set(message, existing ? [nextEntry, ...existing.slice(0, 1)] : [nextEntry])
288
307
  return converted
289
308
  }
290
309
 
@@ -418,76 +437,186 @@ function pendingMessage(
418
437
  }
419
438
  }
420
439
 
421
- /** The reply streaming in, until its saved copy is in the conversation. */
422
- function liveReply(
440
+ /**
441
+ * The reply streaming in, until its saved copies are in the conversation. A message the run read as
442
+ * it answered sits where it read it, between the reply before and the reply after, as the thread
443
+ * holds them once the turn ends; each keeps the key its saved copy will have.
444
+ */
445
+ function liveMessages(
423
446
  input: ConversationMessagesInput,
424
447
  savedIds: ReadonlySet<string>,
425
448
  answeredRuns: ReadonlySet<string>
426
- ): AgentConversationMessage | null {
449
+ ): AgentConversationMessage[] {
427
450
  const live = input.live
428
- if (!live || live.runId === null || live.parts.length === 0) return null
429
- if (live.finalizedMessageId !== null && savedIds.has(live.finalizedMessageId)) return null
430
- if (answeredRuns.has(live.runId)) return null
451
+ if (!live || live.runId === null) return []
452
+ if (live.parts.length === 0 && live.inboxReads.length === 0) return []
453
+ if (live.finalizedMessageId !== null && savedIds.has(live.finalizedMessageId)) return []
454
+ if (answeredRuns.has(live.runId)) return []
431
455
 
432
456
  // A worker that stopped mid-turn saved none of what it streamed.
433
457
  const presentation = input.presentation
434
- if (presentation.kind === "interrupted" && !presentation.hasProgress) return null
458
+ if (presentation.kind === "interrupted" && !presentation.hasProgress) return []
435
459
 
460
+ const runId = live.runId
436
461
  const unfinished =
437
462
  live.finishStatus === "failed" || live.finishStatus === "cancelled"
438
463
  ? live.finishStatus
439
464
  : undefined
465
+ // Saved but not read back yet: the reply keeps its live shape until the copy replaces it.
466
+ const status = live.active || live.finalizedMessageId !== null ? "streaming" : "complete"
467
+
468
+ const result: AgentConversationMessage[] = []
469
+ let replied = false
470
+ let lastRead: string | null = null
471
+ const reply = (from: number, to: number, last: boolean) => {
472
+ const parts = liveParts(live, from, to, last)
473
+ if (parts.length === 0) return
474
+
475
+ result.push({
476
+ key: replied && lastRead !== null ? `run:${runId}:after:${lastRead}` : `run:${runId}`,
477
+ id: null,
478
+ role: "assistant",
479
+ status,
480
+ runId,
481
+ parts,
482
+ context: [],
483
+ diagnostics: [],
484
+ ...(last && unfinished ? { unfinished } : {}),
485
+ })
486
+ replied = true
487
+ }
488
+
489
+ let from = 0
490
+ for (const read of live.inboxReads) {
491
+ reply(from, read.at, false)
492
+ from = read.at
493
+
494
+ for (const message of read.messages) {
495
+ if (savedIds.has(message.id)) continue
496
+
497
+ result.push(readMessage(message, input))
498
+ lastRead = message.id
499
+ }
500
+ }
501
+ reply(from, live.parts.length, true)
502
+
503
+ return result
504
+ }
505
+
506
+ /** A message the run read as it answered, drawn as the thread will hold it. */
507
+ function readMessage(
508
+ message: LiveInboxRead["messages"][number],
509
+ input: ConversationMessagesInput
510
+ ): AgentConversationMessage {
511
+ const author = authorOf(message.authorPrincipal, input)
440
512
 
441
513
  return {
442
- key: `run:${live.runId}`,
514
+ key: `message:${message.id}`,
443
515
  id: null,
444
- role: "assistant",
445
- // Saved but not read back yet: the reply keeps its live shape until the copy replaces it.
446
- status: live.active || live.finalizedMessageId !== null ? "streaming" : "complete",
447
- runId: live.runId,
448
- parts: liveParts(live),
449
- context: [],
516
+ role: "user",
517
+ status: "streaming",
518
+ runId: null,
519
+ ...(author ? { author } : {}),
520
+ parts: message.parts.flatMap((part) =>
521
+ part.type === "text" ? [{ type: "text" as const, text: part.text, streaming: false }] : []
522
+ ),
523
+ context: message.parts.flatMap((part) =>
524
+ part.type === "context"
525
+ ? [{ context: part.context as AgentContextInput, origin: part.origin }]
526
+ : []
527
+ ),
450
528
  diagnostics: [],
451
- ...(unfinished ? { unfinished } : {}),
452
529
  }
453
530
  }
454
531
 
455
- /** The live parts. Text streams in spans the built-in chat joins, so they join here too. */
456
- function liveParts(live: LiveRunState): AgentConversationPart[] {
532
+ const liveNonTextParts = new WeakMap<
533
+ NormalizedPart,
534
+ { readonly key: string | undefined; readonly part: AgentConversationPart }
535
+ >()
536
+ const liveSettledTextParts = new WeakMap<NormalizedPart, AgentConversationPart>()
537
+
538
+ /**
539
+ * The live parts from `from` to `to`. Text streams in spans the built-in chat joins, so they join
540
+ * here too; the model is still writing the text the `last` ones end on.
541
+ */
542
+ function liveParts(
543
+ live: LiveRunState,
544
+ from: number,
545
+ to: number,
546
+ last: boolean
547
+ ): AgentConversationPart[] {
457
548
  const result: AgentConversationPart[] = []
458
549
 
459
- live.parts.forEach((part, index) => {
550
+ live.parts.slice(from, to).forEach((part, offset) => {
460
551
  switch (part.kind) {
461
552
  case "text": {
462
553
  const previous = result.at(-1)
463
554
  if (previous?.type === "text") {
464
555
  result[result.length - 1] = { ...previous, text: previous.text + part.text }
465
556
  } else {
466
- result.push({ type: "text", text: part.text, streaming: false })
557
+ const cached = liveSettledTextParts.get(part)
558
+ if (cached) {
559
+ result.push(cached)
560
+ } else {
561
+ const created: AgentConversationPart = {
562
+ type: "text",
563
+ text: part.text,
564
+ streaming: false,
565
+ }
566
+ liveSettledTextParts.set(part, created)
567
+ result.push(created)
568
+ }
467
569
  }
468
570
  return
469
571
  }
470
572
 
471
- case "reasoning":
472
- result.push({ type: "reasoning", text: part.text, streaming: part.streaming })
573
+ case "reasoning": {
574
+ const cached = liveNonTextParts.get(part)
575
+ if (cached) {
576
+ result.push(cached.part)
577
+ return
578
+ }
579
+ const created: AgentConversationPart = {
580
+ type: "reasoning",
581
+ text: part.text,
582
+ streaming: part.streaming,
583
+ }
584
+ liveNonTextParts.set(part, { key: undefined, part: created })
585
+ result.push(created)
473
586
  return
587
+ }
474
588
 
475
589
  case "tool": {
476
- const key = live.partKeys[index]
590
+ const key = live.partKeys[from + offset]
591
+ const cached = liveNonTextParts.get(part)
592
+ if (cached && cached.key === key) {
593
+ result.push(cached.part)
594
+ return
595
+ }
477
596
  const toolCallId = key?.startsWith("tool#") ? key.slice("tool#".length) : undefined
478
- result.push({
597
+ const created: AgentConversationPart = {
479
598
  type: "tool",
480
599
  toolName: part.tool.toolName,
481
600
  ...(toolCallId === undefined ? {} : { toolCallId }),
482
601
  ...toolViewProps(part.tool),
483
602
  ...(part.tool.files ? { files: part.tool.files.map(fileOf) } : {}),
484
- })
603
+ }
604
+ liveNonTextParts.set(part, { key, part: created })
605
+ result.push(created)
485
606
  return
486
607
  }
487
608
 
488
- case "file":
489
- result.push({ type: "file", ...fileOf(part) })
609
+ case "file": {
610
+ const cached = liveNonTextParts.get(part)
611
+ if (cached) {
612
+ result.push(cached.part)
613
+ return
614
+ }
615
+ const created: AgentConversationPart = { type: "file", ...fileOf(part) }
616
+ liveNonTextParts.set(part, { key: undefined, part: created })
617
+ result.push(created)
490
618
  return
619
+ }
491
620
 
492
621
  case "step-start":
493
622
  return
@@ -497,10 +626,9 @@ function liveParts(live: LiveRunState): AgentConversationPart[] {
497
626
  }
498
627
  })
499
628
 
500
- // The model is still writing the text it ended on.
501
- const last = result.at(-1)
502
- if (live.active && last?.type === "text") {
503
- result[result.length - 1] = { ...last, streaming: true }
629
+ const end = result.at(-1)
630
+ if (last && live.active && end?.type === "text") {
631
+ result[result.length - 1] = { ...end, streaming: true }
504
632
  }
505
633
 
506
634
  return result
@@ -516,31 +644,49 @@ function fileOf(file: NormalizedFile): AgentConversationFile {
516
644
  * The parts as the built-in chat draws them. Two text parts in a row were two steps, which the
517
645
  * chat keeps apart with the step boundary between them.
518
646
  */
647
+ const normalizedPartEntries = new WeakMap<AgentConversationPart, NormalizedPart>()
648
+ const normalizedToolEntries = new WeakMap<AgentConversationToolPart, NormalizedTool>()
649
+ const STEP_START_PART: NormalizedPart = { kind: "step-start" }
650
+
519
651
  export function normalizedParts(parts: readonly AgentConversationPart[]): NormalizedPart[] {
520
652
  const result: NormalizedPart[] = []
521
653
 
522
654
  for (const part of parts) {
655
+ if (part.type === "text" && result.at(-1)?.kind === "text") {
656
+ result.push(STEP_START_PART)
657
+ }
658
+
659
+ const cached = normalizedPartEntries.get(part)
660
+ if (cached) {
661
+ result.push(cached)
662
+ continue
663
+ }
664
+
665
+ let created: NormalizedPart
523
666
  switch (part.type) {
524
667
  case "text":
525
- if (result.at(-1)?.kind === "text") result.push({ kind: "step-start" })
526
- result.push({ kind: "text", text: part.text })
668
+ created = { kind: "text", text: part.text }
527
669
  break
528
670
 
529
671
  case "reasoning":
530
- result.push({ kind: "reasoning", text: part.text, streaming: part.streaming })
672
+ created = { kind: "reasoning", text: part.text, streaming: part.streaming }
531
673
  break
532
674
 
533
675
  case "tool":
534
- result.push({ kind: "tool", tool: normalizedTool(part) })
676
+ created = { kind: "tool", tool: normalizedTool(part) }
535
677
  break
536
678
 
537
679
  case "file":
538
- result.push({ kind: "file", ...fileOf(part) })
680
+ created = { kind: "file", ...fileOf(part) }
539
681
  break
540
682
 
541
683
  default:
542
684
  part satisfies never
685
+ continue
543
686
  }
687
+
688
+ normalizedPartEntries.set(part, created)
689
+ result.push(created)
544
690
  }
545
691
 
546
692
  return result
@@ -548,7 +694,10 @@ export function normalizedParts(parts: readonly AgentConversationPart[]): Normal
548
694
 
549
695
  /** A tool part as the built-in tool views read it. */
550
696
  export function normalizedTool(part: AgentConversationToolPart): NormalizedTool {
697
+ const cached = normalizedToolEntries.get(part)
698
+ if (cached) return cached
551
699
  const { type: _type, toolCallId: _toolCallId, ...tool } = part
700
+ normalizedToolEntries.set(part, tool)
552
701
  return tool
553
702
  }
554
703
 
@@ -91,17 +91,41 @@ export interface DelegationTurn {
91
91
  /** Earlier threads hold `wait_agent`; a `wait` or a `stop` for commands alone is left out. */
92
92
  const DELEGATION_TOOLS = new Set(["spawn_agent", "message_agent", "wait", "stop", "wait_agent"])
93
93
 
94
+ export interface DelegationTurnEntry {
95
+ readonly turn: DelegationTurn
96
+ readonly signature: string
97
+ }
98
+
99
+ const delegationTurnEntries = new WeakMap<AgentConversationMessage, DelegationTurnEntry>()
100
+
101
+ export function delegationTurnEntry(msg: AgentConversationMessage): DelegationTurnEntry | null {
102
+ if (msg.role !== "assistant") return null
103
+ const cached = delegationTurnEntries.get(msg)
104
+ if (cached) return cached
105
+
106
+ const tools: NormalizedTool[] = []
107
+ let sig = `${msg.runId ?? ""}:`
108
+ for (const part of msg.parts) {
109
+ if (part.type === "tool" && DELEGATION_TOOLS.has(part.toolName)) {
110
+ tools.push(normalizedTool(part))
111
+ sig += `${part.toolName}@${part.state};`
112
+ }
113
+ }
114
+ const entry = { turn: { runId: msg.runId, tools }, signature: sig }
115
+ delegationTurnEntries.set(msg, entry)
116
+ return entry
117
+ }
118
+
94
119
  /** The delegation calls of each reply, oldest first, the one streaming last. */
95
120
  export function delegationTurns(
96
121
  messages: readonly AgentConversationMessage[]
97
122
  ): readonly DelegationTurn[] {
98
- return messages.flatMap((message) => {
99
- if (message.role !== "assistant") return []
100
- const tools = message.parts.flatMap((part) =>
101
- part.type === "tool" && DELEGATION_TOOLS.has(part.toolName) ? [normalizedTool(part)] : []
102
- )
103
- return [{ runId: message.runId, tools }]
104
- })
123
+ const result: DelegationTurn[] = []
124
+ for (const message of messages) {
125
+ const entry = delegationTurnEntry(message)
126
+ if (entry) result.push(entry.turn)
127
+ }
128
+ return result
105
129
  }
106
130
 
107
131
  /**