@sixb/app 0.2.4 → 0.2.6
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/assets/connector-icons/README.md +1 -0
- package/assets/connector-icons/ctm.png +0 -0
- package/dist/agent/activity-label.d.ts.map +1 -1
- package/dist/agent/agent-chat.d.ts +3 -1
- package/dist/agent/agent-chat.d.ts.map +1 -1
- package/dist/agent/agent-panel.d.ts.map +1 -1
- package/dist/agent/artifact-panel.d.ts +3 -1
- package/dist/agent/artifact-panel.d.ts.map +1 -1
- package/dist/agent/components/agent-threads.d.ts.map +1 -1
- package/dist/agent/components/composer-place.d.ts.map +1 -1
- package/dist/agent/components/composer-settings.d.ts +1 -1
- package/dist/agent/components/composer-settings.d.ts.map +1 -1
- package/dist/agent/components/composer.d.ts +22 -5
- package/dist/agent/components/composer.d.ts.map +1 -1
- package/dist/agent/components/computer-place.d.ts +24 -6
- package/dist/agent/components/computer-place.d.ts.map +1 -1
- package/dist/agent/components/context-meter.d.ts +1 -1
- package/dist/agent/components/context-meter.d.ts.map +1 -1
- package/dist/agent/components/conversation-panel.d.ts +5 -2
- package/dist/agent/components/conversation-panel.d.ts.map +1 -1
- package/dist/agent/components/message-parts.d.ts +2 -1
- package/dist/agent/components/message-parts.d.ts.map +1 -1
- package/dist/agent/components/message-view.d.ts +13 -3
- package/dist/agent/components/message-view.d.ts.map +1 -1
- package/dist/agent/components/pending-messages.d.ts +20 -0
- package/dist/agent/components/pending-messages.d.ts.map +1 -0
- package/dist/agent/components/sandbox-recovery.d.ts +11 -2
- package/dist/agent/components/sandbox-recovery.d.ts.map +1 -1
- package/dist/agent/components/threads-button.d.ts +1 -1
- package/dist/agent/components/threads-button.d.ts.map +1 -1
- package/dist/agent/components/transcript.d.ts +13 -0
- package/dist/agent/components/transcript.d.ts.map +1 -1
- package/dist/agent/conversation.d.ts +7 -6
- package/dist/agent/conversation.d.ts.map +1 -1
- package/dist/agent/delegation/interpret.d.ts +5 -0
- package/dist/agent/delegation/interpret.d.ts.map +1 -1
- package/dist/agent/delegation/use-children.d.ts.map +1 -1
- package/dist/agent/format.d.ts +7 -0
- package/dist/agent/format.d.ts.map +1 -1
- package/dist/agent/hooks/thread-queries.d.ts +1548 -0
- package/dist/agent/hooks/thread-queries.d.ts.map +1 -0
- package/dist/agent/hooks/use-agent-conversation.d.ts +51 -6
- package/dist/agent/hooks/use-agent-conversation.d.ts.map +1 -1
- package/dist/agent/hooks/use-thread-lists.d.ts +80 -3
- package/dist/agent/hooks/use-thread-lists.d.ts.map +1 -1
- package/dist/agent/hooks/use-thread-place.d.ts +49 -0
- package/dist/agent/hooks/use-thread-place.d.ts.map +1 -0
- package/dist/agent/hooks/use-thread-stream.d.ts.map +1 -1
- package/dist/agent/i18n/en.d.ts +107 -6
- package/dist/agent/i18n/en.d.ts.map +1 -1
- package/dist/agent/i18n/fr.d.ts.map +1 -1
- package/dist/agent/i18n/index.d.ts +180 -12
- package/dist/agent/i18n/index.d.ts.map +1 -1
- package/dist/agent/inbox-handoff.d.ts +10 -0
- package/dist/agent/inbox-handoff.d.ts.map +1 -0
- package/dist/agent/live-run.d.ts +8 -1
- package/dist/agent/live-run.d.ts.map +1 -1
- package/dist/agent/model-selection.d.ts +3 -2
- package/dist/agent/model-selection.d.ts.map +1 -1
- package/dist/agent/moves.d.ts +40 -0
- package/dist/agent/moves.d.ts.map +1 -0
- package/dist/agent/search/interpret.d.ts +101 -0
- package/dist/agent/search/interpret.d.ts.map +1 -0
- package/dist/agent/search/search-tool-view.d.ts +10 -0
- package/dist/agent/search/search-tool-view.d.ts.map +1 -0
- package/dist/agent/thread-reader-state.d.ts +30 -0
- package/dist/agent/thread-reader-state.d.ts.map +1 -0
- package/dist/agent/thread-state.d.ts +26 -0
- package/dist/agent/thread-state.d.ts.map +1 -0
- package/dist/agent/tool-views/built-in.d.ts.map +1 -1
- package/dist/agent/tool-views/registry.d.ts +5 -6
- package/dist/agent/tool-views/registry.d.ts.map +1 -1
- package/dist/agent/types.d.ts +9 -1
- package/dist/agent/types.d.ts.map +1 -1
- package/dist/agent/view-file/view-file-tool-view.d.ts +2 -1
- package/dist/agent/view-file/view-file-tool-view.d.ts.map +1 -1
- package/dist/artifacts/card-preview.d.ts.map +1 -1
- package/dist/artifacts/editor/block-handle.d.ts +20 -0
- package/dist/artifacts/editor/block-handle.d.ts.map +1 -0
- package/dist/artifacts/editor/blocks.d.ts +34 -0
- package/dist/artifacts/editor/blocks.d.ts.map +1 -0
- package/dist/artifacts/editor/callout-view.d.ts +7 -0
- package/dist/artifacts/editor/callout-view.d.ts.map +1 -0
- package/dist/artifacts/editor/document-editor.d.ts +30 -0
- package/dist/artifacts/editor/document-editor.d.ts.map +1 -0
- package/dist/artifacts/editor/figure-view.d.ts +12 -0
- package/dist/artifacts/editor/figure-view.d.ts.map +1 -0
- package/dist/artifacts/editor/find-blocks.d.ts +6 -0
- package/dist/artifacts/editor/find-blocks.d.ts.map +1 -0
- package/dist/artifacts/editor/markdown-document.d.ts +12 -0
- package/dist/artifacts/editor/markdown-document.d.ts.map +1 -0
- package/dist/artifacts/editor/messages.d.ts +4 -0
- package/dist/artifacts/editor/messages.d.ts.map +1 -0
- package/dist/artifacts/editor/schema.d.ts +21 -0
- package/dist/artifacts/editor/schema.d.ts.map +1 -0
- package/dist/artifacts/editor/selection-toolbar.d.ts +35 -0
- package/dist/artifacts/editor/selection-toolbar.d.ts.map +1 -0
- package/dist/artifacts/editor/slash-menu.d.ts +43 -0
- package/dist/artifacts/editor/slash-menu.d.ts.map +1 -0
- package/dist/artifacts/editor/upload.d.ts +12 -0
- package/dist/artifacts/editor/upload.d.ts.map +1 -0
- package/dist/artifacts/file-source.d.ts +5 -0
- package/dist/artifacts/file-source.d.ts.map +1 -1
- package/dist/artifacts/frame-editor/bundle.d.ts +7 -0
- package/dist/artifacts/frame-editor/bundle.d.ts.map +1 -0
- package/dist/artifacts/frame-editor/commands.d.ts +93 -0
- package/dist/artifacts/frame-editor/commands.d.ts.map +1 -0
- package/dist/artifacts/frame-editor/entry.d.ts +2 -0
- package/dist/artifacts/frame-editor/entry.d.ts.map +1 -0
- package/dist/artifacts/frame-editor/frame.d.ts +5 -0
- package/dist/artifacts/frame-editor/frame.d.ts.map +1 -0
- package/dist/artifacts/frame-editor/keys.d.ts +8 -0
- package/dist/artifacts/frame-editor/keys.d.ts.map +1 -0
- package/dist/artifacts/frame-editor/protocol.d.ts +186 -0
- package/dist/artifacts/frame-editor/protocol.d.ts.map +1 -0
- package/dist/artifacts/frame-editor/schema.d.ts +34 -0
- package/dist/artifacts/frame-editor/schema.d.ts.map +1 -0
- package/dist/artifacts/frame-editor/style.d.ts +2 -0
- package/dist/artifacts/frame-editor/style.d.ts.map +1 -0
- package/dist/artifacts/frame.d.ts +2 -2
- package/dist/artifacts/frame.d.ts.map +1 -1
- package/dist/artifacts/live/boards.d.ts +2 -0
- package/dist/artifacts/live/boards.d.ts.map +1 -1
- package/dist/artifacts/live/document.d.ts +32 -0
- package/dist/artifacts/live/document.d.ts.map +1 -0
- package/dist/artifacts/live/header.d.ts +4 -4
- package/dist/artifacts/live/header.d.ts.map +1 -1
- package/dist/artifacts/live/presence.d.ts.map +1 -1
- package/dist/artifacts/live/session.d.ts +2 -0
- package/dist/artifacts/live/session.d.ts.map +1 -1
- package/dist/artifacts/live/view.d.ts +4 -3
- package/dist/artifacts/live/view.d.ts.map +1 -1
- package/dist/artifacts/messages.d.ts +226 -2
- package/dist/artifacts/messages.d.ts.map +1 -1
- package/dist/artifacts/pdf.d.ts +8 -0
- package/dist/artifacts/pdf.d.ts.map +1 -0
- package/dist/artifacts/queries.d.ts +80 -0
- package/dist/artifacts/queries.d.ts.map +1 -1
- package/dist/artifacts/references/references.d.ts +3 -3
- package/dist/artifacts/references/references.d.ts.map +1 -1
- package/dist/artifacts/renderers/canvas/board-editor.d.ts.map +1 -1
- package/dist/artifacts/renderers/canvas/comment-pins.d.ts +4 -2
- package/dist/artifacts/renderers/canvas/comment-pins.d.ts.map +1 -1
- package/dist/artifacts/renderers/canvas/comments-panel.d.ts.map +1 -1
- package/dist/artifacts/renderers/canvas/comments.d.ts +2 -1
- package/dist/artifacts/renderers/canvas/comments.d.ts.map +1 -1
- package/dist/artifacts/renderers/designed-document.d.ts +28 -0
- package/dist/artifacts/renderers/designed-document.d.ts.map +1 -0
- package/dist/artifacts/renderers/document-tree.d.ts +2 -0
- package/dist/artifacts/renderers/document-tree.d.ts.map +1 -1
- package/dist/artifacts/renderers/document.d.ts +9 -1
- package/dist/artifacts/renderers/document.d.ts.map +1 -1
- package/dist/artifacts/renderers/live-frame.d.ts +145 -0
- package/dist/artifacts/renderers/live-frame.d.ts.map +1 -0
- package/dist/artifacts/renderers/page-editor.d.ts +16 -0
- package/dist/artifacts/renderers/page-editor.d.ts.map +1 -0
- package/dist/artifacts/renderers/page.d.ts +10 -1
- package/dist/artifacts/renderers/page.d.ts.map +1 -1
- package/dist/artifacts/viewer.d.ts.map +1 -1
- package/dist/{chunk-pxd07kvx.js → chunk-096d52h2.js} +1 -1
- package/dist/{chunk-yrcm1pbg.js → chunk-94d7enwc.js} +491 -2128
- package/dist/chunk-94d7enwc.js.map +27 -0
- package/dist/{chunk-970s5nkn.js → chunk-ezntd3yy.js} +1 -1
- package/dist/{chunk-kymx9y5e.js → chunk-qjw5r2vk.js} +12729 -5044
- package/dist/chunk-qjw5r2vk.js.map +242 -0
- package/dist/chunk-y51c4tsz.js +11 -0
- package/dist/chunk-y51c4tsz.js.map +10 -0
- package/dist/createCustomApp.d.ts +8 -1
- package/dist/createCustomApp.d.ts.map +1 -1
- package/dist/index.d.ts +4 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +13 -6
- package/dist/index.js.map +5 -5
- package/dist/pages/console/data/accounts.d.ts +94 -0
- package/dist/pages/console/data/accounts.d.ts.map +1 -0
- package/dist/pages/console/data/connectors.d.ts.map +1 -1
- package/dist/pages/console/data/live-updates.d.ts.map +1 -1
- package/dist/pages/console/data/messages.d.ts +52 -0
- package/dist/pages/console/data/messages.d.ts.map +1 -1
- package/dist/pages/console/data/shared.d.ts.map +1 -1
- package/dist/pages/console/data/sync-button.d.ts.map +1 -1
- package/dist/pages/console/data/syncs.d.ts.map +1 -1
- package/dist/pages/settings/usage/breakdown.d.ts.map +1 -1
- package/dist/pages/settings/usage/page.d.ts +4 -1
- package/dist/pages/settings/usage/page.d.ts.map +1 -1
- package/dist/pages/settings/usage/spend-model.d.ts +43 -19
- package/dist/pages/settings/usage/spend-model.d.ts.map +1 -1
- package/dist/pages.js +23044 -22570
- package/dist/pages.js.map +17 -16
- package/dist/server.d.ts +1 -1
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +40 -3
- package/dist/server.js.map +3 -3
- package/dist/shell/search-command.d.ts.map +1 -1
- package/package.json +22 -5
- package/src/agent/activity-label.ts +3 -0
- package/src/agent/agent-chat.tsx +58 -39
- package/src/agent/agent-execution-trace.tsx +2 -2
- package/src/agent/agent-panel.tsx +6 -4
- package/src/agent/agent.css +254 -0
- package/src/agent/artifact-panel.tsx +77 -22
- package/src/agent/components/agent-threads.tsx +42 -2
- package/src/agent/components/composer-place.tsx +2 -7
- package/src/agent/components/composer-settings.tsx +20 -10
- package/src/agent/components/composer.tsx +342 -104
- package/src/agent/components/computer-place.tsx +415 -70
- package/src/agent/components/context-meter.tsx +7 -3
- package/src/agent/components/conversation-panel.tsx +99 -54
- package/src/agent/components/message-parts.tsx +367 -59
- package/src/agent/components/message-view.tsx +428 -9
- package/src/agent/components/pending-messages.tsx +342 -0
- package/src/agent/components/sandbox-recovery.tsx +36 -17
- package/src/agent/components/threads-button.tsx +3 -2
- package/src/agent/components/transcript.tsx +198 -38
- package/src/agent/conversation.ts +219 -55
- package/src/agent/delegation/interpret.ts +31 -7
- package/src/agent/format.ts +52 -0
- package/src/agent/hooks/thread-queries.ts +75 -0
- package/src/agent/hooks/use-agent-conversation.ts +801 -211
- package/src/agent/hooks/use-thread-lists.ts +72 -7
- package/src/agent/hooks/use-thread-place.ts +239 -0
- package/src/agent/hooks/use-thread-stream.ts +10 -0
- package/src/agent/i18n/en.ts +150 -27
- package/src/agent/i18n/fr.ts +148 -31
- package/src/agent/inbox-handoff.ts +47 -0
- package/src/agent/live-run.ts +61 -2
- package/src/agent/model-selection.ts +3 -2
- package/src/agent/moves.ts +174 -0
- package/src/agent/react-router.tsx +2 -2
- package/src/agent/search/interpret.ts +367 -0
- package/src/agent/search/search-tool-view.tsx +235 -0
- package/src/agent/thread-reader-state.ts +75 -0
- package/src/agent/thread-state.ts +114 -0
- package/src/agent/tool-views/built-in.tsx +16 -0
- package/src/agent/tool-views/registry.tsx +28 -2
- package/src/agent/types.ts +13 -0
- package/src/agent/view-file/view-file-tool-view.tsx +33 -19
- package/src/artifacts/artifacts.css +398 -0
- package/src/artifacts/card-preview.tsx +6 -1
- package/src/artifacts/editor/block-handle.tsx +278 -0
- package/src/artifacts/editor/blocks.ts +216 -0
- package/src/artifacts/editor/callout-view.tsx +31 -0
- package/src/artifacts/editor/document-editor.tsx +484 -0
- package/src/artifacts/editor/figure-view.tsx +66 -0
- package/src/artifacts/editor/find-blocks.ts +19 -0
- package/src/artifacts/editor/markdown-document.tsx +72 -0
- package/src/artifacts/editor/messages.ts +4 -0
- package/src/artifacts/editor/schema.ts +159 -0
- package/src/artifacts/editor/selection-toolbar.tsx +364 -0
- package/src/artifacts/editor/slash-menu.tsx +251 -0
- package/src/artifacts/editor/upload.ts +40 -0
- package/src/artifacts/file-source.ts +11 -0
- package/src/artifacts/frame-editor/bundle.ts +8 -0
- package/src/artifacts/frame-editor/commands.ts +565 -0
- package/src/artifacts/frame-editor/entry.ts +6 -0
- package/src/artifacts/frame-editor/frame.ts +847 -0
- package/src/artifacts/frame-editor/generated/editor.txt +149 -0
- package/src/artifacts/frame-editor/generated.d.ts +5 -0
- package/src/artifacts/frame-editor/keys.ts +323 -0
- package/src/artifacts/frame-editor/protocol.ts +151 -0
- package/src/artifacts/frame-editor/schema.ts +293 -0
- package/src/artifacts/frame-editor/style.ts +58 -0
- package/src/artifacts/frame.tsx +11 -3
- package/src/artifacts/live/boards.ts +46 -3
- package/src/artifacts/live/document.ts +89 -0
- package/src/artifacts/live/header.tsx +9 -14
- package/src/artifacts/live/presence.ts +5 -1
- package/src/artifacts/live/session.ts +9 -2
- package/src/artifacts/live/view.ts +14 -8
- package/src/artifacts/messages.ts +138 -2
- package/src/artifacts/pdf.ts +50 -0
- package/src/artifacts/references/references.tsx +46 -11
- package/src/artifacts/renderers/canvas/board-editor.tsx +11 -144
- package/src/artifacts/renderers/canvas/comment-pins.tsx +5 -0
- package/src/artifacts/renderers/canvas/comments-panel.tsx +58 -1
- package/src/artifacts/renderers/canvas/comments.ts +8 -4
- package/src/artifacts/renderers/designed-document.tsx +277 -0
- package/src/artifacts/renderers/document-tree.ts +9 -0
- package/src/artifacts/renderers/document.tsx +13 -102
- package/src/artifacts/renderers/live-frame.ts +163 -0
- package/src/artifacts/renderers/page-editor.tsx +613 -0
- package/src/artifacts/renderers/page.tsx +21 -4
- package/src/artifacts/viewer.tsx +87 -41
- package/src/createCustomApp.ts +57 -3
- package/src/index.ts +10 -0
- package/src/pages/console/data/accounts.tsx +509 -0
- package/src/pages/console/data/connectors.tsx +7 -15
- package/src/pages/console/data/live-updates.ts +2 -0
- package/src/pages/console/data/messages.ts +79 -0
- package/src/pages/console/data/projections.tsx +4 -0
- package/src/pages/console/data/shared.tsx +1 -0
- package/src/pages/console/data/sync-button.tsx +56 -16
- package/src/pages/console/data/syncs.tsx +66 -41
- package/src/pages/console/ontology/lineage/trail.tsx +3 -0
- package/src/pages/section-ui.tsx +1 -1
- package/src/pages/settings/usage/breakdown.tsx +5 -2
- package/src/pages/settings/usage/filter-menu.tsx +1 -1
- package/src/pages/settings/usage/page.tsx +32 -13
- package/src/pages/settings/usage/spend-chart.tsx +2 -2
- package/src/pages/settings/usage/spend-model.ts +118 -50
- package/src/server.ts +1 -0
- package/src/shell/search-command.tsx +2 -0
- package/dist/chunk-kymx9y5e.js.map +0 -211
- package/dist/chunk-yrcm1pbg.js.map +0 -34
- /package/dist/{chunk-pxd07kvx.js.map → chunk-096d52h2.js.map} +0 -0
- /package/dist/{chunk-970s5nkn.js.map → chunk-ezntd3yy.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`:
|
|
84
|
-
*
|
|
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. */
|
|
@@ -95,6 +96,8 @@ export interface AgentConversationMessage {
|
|
|
95
96
|
readonly diagnostics: readonly AgentConversationDiagnostic[]
|
|
96
97
|
/** Earlier turns were condensed before this reply: the summary the agent read instead. */
|
|
97
98
|
readonly compaction?: NonNullable<AgentMessage["compaction"]>
|
|
99
|
+
/** When the saved message was created (ISO 8601). */
|
|
100
|
+
readonly createdAt?: string
|
|
98
101
|
/**
|
|
99
102
|
* A reply whose run ended before finishing it: it failed, or someone stopped it. The turn tells
|
|
100
103
|
* only of a turn that ended without any answer.
|
|
@@ -111,6 +114,8 @@ export interface PendingUserMessage {
|
|
|
111
114
|
readonly context: readonly AgentContextEntryInput[]
|
|
112
115
|
/** The saved message's id, once the API accepted it. */
|
|
113
116
|
readonly messageId: string | null
|
|
117
|
+
/** When editing an earlier message, the id of the saved message this replaces. */
|
|
118
|
+
readonly replacesMessageId?: string
|
|
114
119
|
}
|
|
115
120
|
|
|
116
121
|
export interface ConversationMessagesInput {
|
|
@@ -153,26 +158,53 @@ export function conversationMessages(input: ConversationMessagesInput): AgentCon
|
|
|
153
158
|
for (const run of input.runs ?? []) {
|
|
154
159
|
if (run.status === "failed" || run.status === "cancelled") ended.set(run.id, run.status)
|
|
155
160
|
}
|
|
156
|
-
|
|
161
|
+
// A turn that read messages as it answered holds a reply on each side of them: only its last one
|
|
162
|
+
// says how the turn ended, as its live copy did.
|
|
163
|
+
const lastReplyOfRun = new Map<string, string>()
|
|
157
164
|
for (const message of input.messages) {
|
|
165
|
+
if (message.role === "assistant" && message.runId !== null) {
|
|
166
|
+
lastReplyOfRun.set(message.runId, message.id)
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const replaceId = input.pending?.replacesMessageId
|
|
171
|
+
const replaceAt =
|
|
172
|
+
replaceId === undefined ? -1 : input.messages.findIndex((message) => message.id === replaceId)
|
|
173
|
+
const sourceMessages = replaceAt === -1 ? input.messages : input.messages.slice(0, replaceAt)
|
|
174
|
+
|
|
175
|
+
let lastUserMessageId: string | null = null
|
|
176
|
+
for (const message of sourceMessages) {
|
|
158
177
|
if (message.role === "system") continue
|
|
159
178
|
|
|
160
179
|
savedIds.add(message.id)
|
|
161
180
|
if (message.role === "assistant" && message.runId !== null) answeredRuns.add(message.runId)
|
|
181
|
+
if (message.role === "user") lastUserMessageId = message.id
|
|
162
182
|
|
|
163
183
|
// Continue writes the model a message the reader never wrote: nothing to draw. Neither is the
|
|
164
184
|
// one a decision resumes a turn with: its calls' cards say how they ended, and the resumed
|
|
165
185
|
// answer stays right under the one it continues.
|
|
166
186
|
if (isContinuation(message) || isApprovalOutcomeMessage(message)) continue
|
|
167
187
|
|
|
168
|
-
//
|
|
188
|
+
// A run's reply after a message it read keeps the key its live copy had (see liveMessages).
|
|
169
189
|
const preferred = savedKey(message, input.sentKeys)
|
|
170
|
-
const
|
|
190
|
+
const after =
|
|
191
|
+
message.role === "assistant" && lastUserMessageId !== null
|
|
192
|
+
? `${preferred}:after:${lastUserMessageId}`
|
|
193
|
+
: null
|
|
194
|
+
const key = !keys.has(preferred)
|
|
195
|
+
? preferred
|
|
196
|
+
: after !== null && !keys.has(after)
|
|
197
|
+
? after
|
|
198
|
+
: `message:${message.id}`
|
|
171
199
|
keys.add(key)
|
|
172
200
|
|
|
173
201
|
const saved = savedMessage(message, key, authorOf(message.authorPrincipal, input), input)
|
|
174
202
|
const unfinished =
|
|
175
|
-
message.role === "assistant" &&
|
|
203
|
+
message.role === "assistant" &&
|
|
204
|
+
message.runId !== null &&
|
|
205
|
+
lastReplyOfRun.get(message.runId) === message.id
|
|
206
|
+
? ended.get(message.runId)
|
|
207
|
+
: undefined
|
|
176
208
|
result.push(unfinished ? unfinishedMessage(saved, unfinished) : saved)
|
|
177
209
|
}
|
|
178
210
|
|
|
@@ -181,8 +213,7 @@ export function conversationMessages(input: ConversationMessagesInput): AgentCon
|
|
|
181
213
|
result.push(pendingMessage(pending, input))
|
|
182
214
|
}
|
|
183
215
|
|
|
184
|
-
|
|
185
|
-
if (live) result.push(live)
|
|
216
|
+
result.push(...liveMessages(input, savedIds, answeredRuns))
|
|
186
217
|
|
|
187
218
|
return result
|
|
188
219
|
}
|
|
@@ -239,7 +270,7 @@ interface SavedEntry {
|
|
|
239
270
|
readonly message: AgentConversationMessage
|
|
240
271
|
}
|
|
241
272
|
|
|
242
|
-
const savedEntries = new WeakMap<AgentMessage, SavedEntry>()
|
|
273
|
+
const savedEntries = new WeakMap<AgentMessage, readonly SavedEntry[]>()
|
|
243
274
|
|
|
244
275
|
function savedMessage(
|
|
245
276
|
message: AgentMessage,
|
|
@@ -248,15 +279,11 @@ function savedMessage(
|
|
|
248
279
|
input: ConversationMessagesInput
|
|
249
280
|
): AgentConversationMessage {
|
|
250
281
|
const observed = Boolean(input.observed)
|
|
251
|
-
const
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
sameAuthor(cached.author, author)
|
|
257
|
-
) {
|
|
258
|
-
return cached.message
|
|
259
|
-
}
|
|
282
|
+
const existing = savedEntries.get(message)
|
|
283
|
+
const hit = existing?.find(
|
|
284
|
+
(entry) => entry.key === key && entry.observed === observed && sameAuthor(entry.author, author)
|
|
285
|
+
)
|
|
286
|
+
if (hit) return hit.message
|
|
260
287
|
|
|
261
288
|
const fileSource = (fileRef: AgentFileRef, path: string) =>
|
|
262
289
|
messageFileSource({
|
|
@@ -282,9 +309,11 @@ function savedMessage(
|
|
|
282
309
|
),
|
|
283
310
|
diagnostics: message.annotations,
|
|
284
311
|
...(message.compaction ? { compaction: message.compaction } : {}),
|
|
312
|
+
createdAt: message.createdAt,
|
|
285
313
|
}
|
|
286
314
|
|
|
287
|
-
|
|
315
|
+
const nextEntry: SavedEntry = { key, author, observed, message: converted }
|
|
316
|
+
savedEntries.set(message, existing ? [nextEntry, ...existing.slice(0, 1)] : [nextEntry])
|
|
288
317
|
return converted
|
|
289
318
|
}
|
|
290
319
|
|
|
@@ -418,76 +447,191 @@ function pendingMessage(
|
|
|
418
447
|
}
|
|
419
448
|
}
|
|
420
449
|
|
|
421
|
-
/**
|
|
422
|
-
|
|
450
|
+
/**
|
|
451
|
+
* The reply streaming in, until its saved copies are in the conversation. A message the run read as
|
|
452
|
+
* it answered sits where it read it, between the reply before and the reply after, as the thread
|
|
453
|
+
* holds them once the turn ends; each keeps the key its saved copy will have.
|
|
454
|
+
*/
|
|
455
|
+
function liveMessages(
|
|
423
456
|
input: ConversationMessagesInput,
|
|
424
457
|
savedIds: ReadonlySet<string>,
|
|
425
458
|
answeredRuns: ReadonlySet<string>
|
|
426
|
-
): AgentConversationMessage
|
|
459
|
+
): AgentConversationMessage[] {
|
|
460
|
+
// While an in-place edit is replacing an earlier turn and its new run has not been accepted yet,
|
|
461
|
+
// suppress any live stream left over from the turn being rewound.
|
|
462
|
+
if (input.pending?.replacesMessageId !== undefined && input.pending.messageId === null) {
|
|
463
|
+
return []
|
|
464
|
+
}
|
|
427
465
|
const live = input.live
|
|
428
|
-
if (!live || live.runId === null
|
|
429
|
-
if (live.
|
|
430
|
-
if (
|
|
466
|
+
if (!live || live.runId === null) return []
|
|
467
|
+
if (live.parts.length === 0 && live.inboxReads.length === 0) return []
|
|
468
|
+
if (live.finalizedMessageId !== null && savedIds.has(live.finalizedMessageId)) return []
|
|
469
|
+
if (answeredRuns.has(live.runId)) return []
|
|
431
470
|
|
|
432
471
|
// A worker that stopped mid-turn saved none of what it streamed.
|
|
433
472
|
const presentation = input.presentation
|
|
434
|
-
if (presentation.kind === "interrupted" && !presentation.hasProgress) return
|
|
473
|
+
if (presentation.kind === "interrupted" && !presentation.hasProgress) return []
|
|
435
474
|
|
|
475
|
+
const runId = live.runId
|
|
436
476
|
const unfinished =
|
|
437
477
|
live.finishStatus === "failed" || live.finishStatus === "cancelled"
|
|
438
478
|
? live.finishStatus
|
|
439
479
|
: undefined
|
|
480
|
+
// Saved but not read back yet: the reply keeps its live shape until the copy replaces it.
|
|
481
|
+
const status = live.active || live.finalizedMessageId !== null ? "streaming" : "complete"
|
|
482
|
+
|
|
483
|
+
const result: AgentConversationMessage[] = []
|
|
484
|
+
let replied = false
|
|
485
|
+
let lastRead: string | null = null
|
|
486
|
+
const reply = (from: number, to: number, last: boolean) => {
|
|
487
|
+
const parts = liveParts(live, from, to, last)
|
|
488
|
+
if (parts.length === 0) return
|
|
489
|
+
|
|
490
|
+
result.push({
|
|
491
|
+
key: replied && lastRead !== null ? `run:${runId}:after:${lastRead}` : `run:${runId}`,
|
|
492
|
+
id: null,
|
|
493
|
+
role: "assistant",
|
|
494
|
+
status,
|
|
495
|
+
runId,
|
|
496
|
+
parts,
|
|
497
|
+
context: [],
|
|
498
|
+
diagnostics: [],
|
|
499
|
+
...(last && unfinished ? { unfinished } : {}),
|
|
500
|
+
})
|
|
501
|
+
replied = true
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
let from = 0
|
|
505
|
+
for (const read of live.inboxReads) {
|
|
506
|
+
reply(from, read.at, false)
|
|
507
|
+
from = read.at
|
|
508
|
+
|
|
509
|
+
for (const message of read.messages) {
|
|
510
|
+
if (savedIds.has(message.id)) continue
|
|
511
|
+
|
|
512
|
+
result.push(readMessage(message, input))
|
|
513
|
+
lastRead = message.id
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
reply(from, live.parts.length, true)
|
|
517
|
+
|
|
518
|
+
return result
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
/** A message the run read as it answered, drawn as the thread will hold it. */
|
|
522
|
+
function readMessage(
|
|
523
|
+
message: LiveInboxRead["messages"][number],
|
|
524
|
+
input: ConversationMessagesInput
|
|
525
|
+
): AgentConversationMessage {
|
|
526
|
+
const author = authorOf(message.authorPrincipal, input)
|
|
440
527
|
|
|
441
528
|
return {
|
|
442
|
-
key: `
|
|
529
|
+
key: `message:${message.id}`,
|
|
443
530
|
id: null,
|
|
444
|
-
role: "
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
parts:
|
|
449
|
-
|
|
531
|
+
role: "user",
|
|
532
|
+
status: "streaming",
|
|
533
|
+
runId: null,
|
|
534
|
+
...(author ? { author } : {}),
|
|
535
|
+
parts: message.parts.flatMap((part) =>
|
|
536
|
+
part.type === "text" ? [{ type: "text" as const, text: part.text, streaming: false }] : []
|
|
537
|
+
),
|
|
538
|
+
context: message.parts.flatMap((part) =>
|
|
539
|
+
part.type === "context"
|
|
540
|
+
? [{ context: part.context as AgentContextInput, origin: part.origin }]
|
|
541
|
+
: []
|
|
542
|
+
),
|
|
450
543
|
diagnostics: [],
|
|
451
|
-
...(unfinished ? { unfinished } : {}),
|
|
452
544
|
}
|
|
453
545
|
}
|
|
454
546
|
|
|
455
|
-
|
|
456
|
-
|
|
547
|
+
const liveNonTextParts = new WeakMap<
|
|
548
|
+
NormalizedPart,
|
|
549
|
+
{ readonly key: string | undefined; readonly part: AgentConversationPart }
|
|
550
|
+
>()
|
|
551
|
+
const liveSettledTextParts = new WeakMap<NormalizedPart, AgentConversationPart>()
|
|
552
|
+
|
|
553
|
+
/**
|
|
554
|
+
* The live parts from `from` to `to`. Text streams in spans the built-in chat joins, so they join
|
|
555
|
+
* here too; the model is still writing the text the `last` ones end on.
|
|
556
|
+
*/
|
|
557
|
+
function liveParts(
|
|
558
|
+
live: LiveRunState,
|
|
559
|
+
from: number,
|
|
560
|
+
to: number,
|
|
561
|
+
last: boolean
|
|
562
|
+
): AgentConversationPart[] {
|
|
457
563
|
const result: AgentConversationPart[] = []
|
|
458
564
|
|
|
459
|
-
live.parts.forEach((part,
|
|
565
|
+
live.parts.slice(from, to).forEach((part, offset) => {
|
|
460
566
|
switch (part.kind) {
|
|
461
567
|
case "text": {
|
|
462
568
|
const previous = result.at(-1)
|
|
463
569
|
if (previous?.type === "text") {
|
|
464
570
|
result[result.length - 1] = { ...previous, text: previous.text + part.text }
|
|
465
571
|
} else {
|
|
466
|
-
|
|
572
|
+
const cached = liveSettledTextParts.get(part)
|
|
573
|
+
if (cached) {
|
|
574
|
+
result.push(cached)
|
|
575
|
+
} else {
|
|
576
|
+
const created: AgentConversationPart = {
|
|
577
|
+
type: "text",
|
|
578
|
+
text: part.text,
|
|
579
|
+
streaming: false,
|
|
580
|
+
}
|
|
581
|
+
liveSettledTextParts.set(part, created)
|
|
582
|
+
result.push(created)
|
|
583
|
+
}
|
|
467
584
|
}
|
|
468
585
|
return
|
|
469
586
|
}
|
|
470
587
|
|
|
471
|
-
case "reasoning":
|
|
472
|
-
|
|
588
|
+
case "reasoning": {
|
|
589
|
+
const cached = liveNonTextParts.get(part)
|
|
590
|
+
if (cached) {
|
|
591
|
+
result.push(cached.part)
|
|
592
|
+
return
|
|
593
|
+
}
|
|
594
|
+
const created: AgentConversationPart = {
|
|
595
|
+
type: "reasoning",
|
|
596
|
+
text: part.text,
|
|
597
|
+
streaming: part.streaming,
|
|
598
|
+
}
|
|
599
|
+
liveNonTextParts.set(part, { key: undefined, part: created })
|
|
600
|
+
result.push(created)
|
|
473
601
|
return
|
|
602
|
+
}
|
|
474
603
|
|
|
475
604
|
case "tool": {
|
|
476
|
-
const key = live.partKeys[
|
|
605
|
+
const key = live.partKeys[from + offset]
|
|
606
|
+
const cached = liveNonTextParts.get(part)
|
|
607
|
+
if (cached && cached.key === key) {
|
|
608
|
+
result.push(cached.part)
|
|
609
|
+
return
|
|
610
|
+
}
|
|
477
611
|
const toolCallId = key?.startsWith("tool#") ? key.slice("tool#".length) : undefined
|
|
478
|
-
|
|
612
|
+
const created: AgentConversationPart = {
|
|
479
613
|
type: "tool",
|
|
480
614
|
toolName: part.tool.toolName,
|
|
481
615
|
...(toolCallId === undefined ? {} : { toolCallId }),
|
|
482
616
|
...toolViewProps(part.tool),
|
|
483
617
|
...(part.tool.files ? { files: part.tool.files.map(fileOf) } : {}),
|
|
484
|
-
}
|
|
618
|
+
}
|
|
619
|
+
liveNonTextParts.set(part, { key, part: created })
|
|
620
|
+
result.push(created)
|
|
485
621
|
return
|
|
486
622
|
}
|
|
487
623
|
|
|
488
|
-
case "file":
|
|
489
|
-
|
|
624
|
+
case "file": {
|
|
625
|
+
const cached = liveNonTextParts.get(part)
|
|
626
|
+
if (cached) {
|
|
627
|
+
result.push(cached.part)
|
|
628
|
+
return
|
|
629
|
+
}
|
|
630
|
+
const created: AgentConversationPart = { type: "file", ...fileOf(part) }
|
|
631
|
+
liveNonTextParts.set(part, { key: undefined, part: created })
|
|
632
|
+
result.push(created)
|
|
490
633
|
return
|
|
634
|
+
}
|
|
491
635
|
|
|
492
636
|
case "step-start":
|
|
493
637
|
return
|
|
@@ -497,10 +641,9 @@ function liveParts(live: LiveRunState): AgentConversationPart[] {
|
|
|
497
641
|
}
|
|
498
642
|
})
|
|
499
643
|
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
result[result.length - 1] = { ...last, streaming: true }
|
|
644
|
+
const end = result.at(-1)
|
|
645
|
+
if (last && live.active && end?.type === "text") {
|
|
646
|
+
result[result.length - 1] = { ...end, streaming: true }
|
|
504
647
|
}
|
|
505
648
|
|
|
506
649
|
return result
|
|
@@ -516,31 +659,49 @@ function fileOf(file: NormalizedFile): AgentConversationFile {
|
|
|
516
659
|
* The parts as the built-in chat draws them. Two text parts in a row were two steps, which the
|
|
517
660
|
* chat keeps apart with the step boundary between them.
|
|
518
661
|
*/
|
|
662
|
+
const normalizedPartEntries = new WeakMap<AgentConversationPart, NormalizedPart>()
|
|
663
|
+
const normalizedToolEntries = new WeakMap<AgentConversationToolPart, NormalizedTool>()
|
|
664
|
+
const STEP_START_PART: NormalizedPart = { kind: "step-start" }
|
|
665
|
+
|
|
519
666
|
export function normalizedParts(parts: readonly AgentConversationPart[]): NormalizedPart[] {
|
|
520
667
|
const result: NormalizedPart[] = []
|
|
521
668
|
|
|
522
669
|
for (const part of parts) {
|
|
670
|
+
if (part.type === "text" && result.at(-1)?.kind === "text") {
|
|
671
|
+
result.push(STEP_START_PART)
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
const cached = normalizedPartEntries.get(part)
|
|
675
|
+
if (cached) {
|
|
676
|
+
result.push(cached)
|
|
677
|
+
continue
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
let created: NormalizedPart
|
|
523
681
|
switch (part.type) {
|
|
524
682
|
case "text":
|
|
525
|
-
|
|
526
|
-
result.push({ kind: "text", text: part.text })
|
|
683
|
+
created = { kind: "text", text: part.text }
|
|
527
684
|
break
|
|
528
685
|
|
|
529
686
|
case "reasoning":
|
|
530
|
-
|
|
687
|
+
created = { kind: "reasoning", text: part.text, streaming: part.streaming }
|
|
531
688
|
break
|
|
532
689
|
|
|
533
690
|
case "tool":
|
|
534
|
-
|
|
691
|
+
created = { kind: "tool", tool: normalizedTool(part) }
|
|
535
692
|
break
|
|
536
693
|
|
|
537
694
|
case "file":
|
|
538
|
-
|
|
695
|
+
created = { kind: "file", ...fileOf(part) }
|
|
539
696
|
break
|
|
540
697
|
|
|
541
698
|
default:
|
|
542
699
|
part satisfies never
|
|
700
|
+
continue
|
|
543
701
|
}
|
|
702
|
+
|
|
703
|
+
normalizedPartEntries.set(part, created)
|
|
704
|
+
result.push(created)
|
|
544
705
|
}
|
|
545
706
|
|
|
546
707
|
return result
|
|
@@ -548,7 +709,10 @@ export function normalizedParts(parts: readonly AgentConversationPart[]): Normal
|
|
|
548
709
|
|
|
549
710
|
/** A tool part as the built-in tool views read it. */
|
|
550
711
|
export function normalizedTool(part: AgentConversationToolPart): NormalizedTool {
|
|
712
|
+
const cached = normalizedToolEntries.get(part)
|
|
713
|
+
if (cached) return cached
|
|
551
714
|
const { type: _type, toolCallId: _toolCallId, ...tool } = part
|
|
715
|
+
normalizedToolEntries.set(part, tool)
|
|
552
716
|
return tool
|
|
553
717
|
}
|
|
554
718
|
|
|
@@ -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
|
-
|
|
99
|
-
|
|
100
|
-
const
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
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
|
/**
|
package/src/agent/format.ts
CHANGED
|
@@ -70,6 +70,58 @@ export function relativeTime(
|
|
|
70
70
|
return null
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
+
/**
|
|
74
|
+
* Spoken relative age for a message's hover bar ("just now", "5 minutes ago", "3 days ago"),
|
|
75
|
+
* falling back to a short date after 30 days.
|
|
76
|
+
*/
|
|
77
|
+
export function formatMessageTimeAgo(
|
|
78
|
+
iso: string | undefined,
|
|
79
|
+
locale = "en",
|
|
80
|
+
justNow = en.contextMeter.justNow
|
|
81
|
+
): string {
|
|
82
|
+
if (!iso) return ""
|
|
83
|
+
const then = new Date(iso).getTime()
|
|
84
|
+
if (Number.isNaN(then)) return ""
|
|
85
|
+
|
|
86
|
+
const diffSeconds = Math.max(0, Math.round((Date.now() - then) / 1000))
|
|
87
|
+
if (diffSeconds < 45) return justNow
|
|
88
|
+
|
|
89
|
+
try {
|
|
90
|
+
const rtf = new Intl.RelativeTimeFormat(locale, { numeric: "always", style: "long" })
|
|
91
|
+
const minutes = Math.round(diffSeconds / 60)
|
|
92
|
+
if (minutes < 60) return rtf.format(-Math.max(1, minutes), "minute")
|
|
93
|
+
const hours = Math.round(minutes / 60)
|
|
94
|
+
if (hours < 24) return rtf.format(-Math.max(1, hours), "hour")
|
|
95
|
+
const days = Math.round(hours / 24)
|
|
96
|
+
if (days < 30) return rtf.format(-Math.max(1, days), "day")
|
|
97
|
+
} catch {
|
|
98
|
+
// Fall back to a calendar date if the runtime rejects the locale tag.
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const date = new Date(then)
|
|
102
|
+
const sameYear = date.getFullYear() === new Date().getFullYear()
|
|
103
|
+
return date.toLocaleDateString(locale, {
|
|
104
|
+
month: "short",
|
|
105
|
+
day: "numeric",
|
|
106
|
+
...(sameYear ? {} : { year: "numeric" }),
|
|
107
|
+
})
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Exact local timestamp for a message's tooltip, e.g. "Nov 14, 7:55 PM". */
|
|
111
|
+
export function formatMessageTimestamp(iso: string | undefined, locale = "en"): string {
|
|
112
|
+
if (!iso) return ""
|
|
113
|
+
const date = new Date(iso)
|
|
114
|
+
if (Number.isNaN(date.getTime())) return ""
|
|
115
|
+
const sameYear = date.getFullYear() === new Date().getFullYear()
|
|
116
|
+
return date.toLocaleString(locale, {
|
|
117
|
+
month: "short",
|
|
118
|
+
day: "numeric",
|
|
119
|
+
...(sameYear ? {} : { year: "numeric" }),
|
|
120
|
+
hour: "numeric",
|
|
121
|
+
minute: "2-digit",
|
|
122
|
+
})
|
|
123
|
+
}
|
|
124
|
+
|
|
73
125
|
/** A token count as a context meter shows it: `950`, `9.5k`, `38k`, `984k`, `1.2M`. */
|
|
74
126
|
export function formatTokenCount(tokens: number, locale = "en"): string {
|
|
75
127
|
const decimal = (value: number) => value.toLocaleString(locale, { maximumFractionDigits: 1 })
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getAgentThreadOptions,
|
|
3
|
+
listAgentThreadMessagesOptions,
|
|
4
|
+
listAgentThreadRunsOptions,
|
|
5
|
+
} from "@sixb/client/hooks"
|
|
6
|
+
import { type QueryClient, queryOptions, useQueryClient } from "@tanstack/react-query"
|
|
7
|
+
import { useCallback } from "react"
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* How long a thread's reads count as fresh. Opening a thread its prefetch just read shows them
|
|
11
|
+
* without reading them again; an event that changes the thread marks them stale, whatever their
|
|
12
|
+
* age (`markThreadReadsStale`).
|
|
13
|
+
*/
|
|
14
|
+
const THREAD_READS_FRESH_MS = 30_000
|
|
15
|
+
|
|
16
|
+
/** How long a thread's messages stay cached once nothing shows them: coming back is instant. */
|
|
17
|
+
const THREAD_MESSAGES_CACHE_MS = 30 * 60_000
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The reads opening a thread makes. Opening and prefetching both use these, so they share each
|
|
21
|
+
* query by its key and agree on when it is fresh.
|
|
22
|
+
*/
|
|
23
|
+
export function threadQueryOptions(threadId: string) {
|
|
24
|
+
const path = { threadId }
|
|
25
|
+
|
|
26
|
+
return {
|
|
27
|
+
thread: queryOptions({ ...getAgentThreadOptions({ path }), staleTime: THREAD_READS_FRESH_MS }),
|
|
28
|
+
messages: queryOptions({
|
|
29
|
+
...listAgentThreadMessagesOptions({ path, query: { order: "asc" } }),
|
|
30
|
+
staleTime: THREAD_READS_FRESH_MS,
|
|
31
|
+
gcTime: THREAD_MESSAGES_CACHE_MS,
|
|
32
|
+
}),
|
|
33
|
+
runs: queryOptions({
|
|
34
|
+
...listAgentThreadRunsOptions({ path, query: { order: "desc" } }),
|
|
35
|
+
staleTime: THREAD_READS_FRESH_MS,
|
|
36
|
+
}),
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Load a thread before it opens, such as when the pointer rests on its link, so opening it shows
|
|
42
|
+
* its messages at once. Reads still fresh are left as they are.
|
|
43
|
+
*/
|
|
44
|
+
export function usePrefetchAgentThread(): (threadId: string) => void {
|
|
45
|
+
const queryClient = useQueryClient()
|
|
46
|
+
|
|
47
|
+
return useCallback(
|
|
48
|
+
(threadId: string) => {
|
|
49
|
+
const reads = threadQueryOptions(threadId)
|
|
50
|
+
|
|
51
|
+
void queryClient.prefetchQuery(reads.thread)
|
|
52
|
+
void queryClient.prefetchQuery(reads.messages)
|
|
53
|
+
void queryClient.prefetchQuery(reads.runs)
|
|
54
|
+
},
|
|
55
|
+
[queryClient]
|
|
56
|
+
)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** The operations a thread's reads are cached under, each keyed by the thread it reads. */
|
|
60
|
+
const THREAD_READS = Object.values(threadQueryOptions("")).map((read) => read.queryKey[0]._id)
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Mark a thread's cached reads stale without reading them, or every thread's when no thread is
|
|
64
|
+
* named: whatever shows one next reads it again. A thread nobody has open learns of a change this
|
|
65
|
+
* way, from an activity event or the feed reconnecting, since its reads stay fresh for a while once
|
|
66
|
+
* read and nothing else would read them again.
|
|
67
|
+
*/
|
|
68
|
+
export function markThreadReadsStale(queryClient: QueryClient, threadId?: string): void {
|
|
69
|
+
for (const _id of THREAD_READS) {
|
|
70
|
+
void queryClient.invalidateQueries({
|
|
71
|
+
queryKey: [threadId === undefined ? { _id } : { _id, path: { threadId } }],
|
|
72
|
+
refetchType: "none",
|
|
73
|
+
})
|
|
74
|
+
}
|
|
75
|
+
}
|