@marimo-team/frontend 0.23.9-dev13 → 0.23.9-dev17
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/assets/{CellStatus-CYnTqpDu.js → CellStatus-cST_KROR.js} +1 -1
- package/dist/assets/{ConnectedDataExplorerComponent-CKOLvj_e.js → ConnectedDataExplorerComponent-CojLBX8C.js} +1 -1
- package/dist/assets/{JsonOutput-BEIaEFVx.js → JsonOutput-BwgMLuPn.js} +1 -1
- package/dist/assets/{MarimoErrorOutput-BrtF-EJX.js → MarimoErrorOutput-DIbgMg0T.js} +1 -1
- package/dist/assets/{RenderHTML-ozbWHhKy.js → RenderHTML-CH8QrLVl.js} +1 -1
- package/dist/assets/{RunButton-Cf9ByHXe.js → RunButton-BDOMYqej.js} +1 -1
- package/dist/assets/{add-cell-with-ai-D80JrWq3.js → add-cell-with-ai-DDmHtapY.js} +21 -21
- package/dist/assets/{add-connection-dialog-Dit1I7Se.js → add-connection-dialog-CRbrpdQu.js} +1 -1
- package/dist/assets/{agent-panel-C-u_vOoE.js → agent-panel-Ci6kmMWq.js} +1 -1
- package/dist/assets/{ai-model-dropdown-2YDLOBgP.js → ai-model-dropdown-CNobct-x.js} +3 -3
- package/dist/assets/{app-config-button-ClcNCSVm.js → app-config-button-DrlCOjyd.js} +1 -1
- package/dist/assets/{cell-editor-n3uoR3fW.js → cell-editor-Bhvnqdc-.js} +7 -7
- package/dist/assets/{cell-link-xmpxJpC6.js → cell-link-rh-JpkCu.js} +1 -1
- package/dist/assets/{cells-DfBJ0VnJ.js → cells-Cwex_Zk1.js} +1 -1
- package/dist/assets/{chat-display-Ddsm0CG8.js → chat-display-DzBxD2g1.js} +1 -1
- package/dist/assets/{chat-panel-75Zw468n.js → chat-panel-ipOkNc7y.js} +1 -1
- package/dist/assets/chat-ui-DqHdyg0V.js +4 -0
- package/dist/assets/{column-preview-B0fkY_W8.js → column-preview-WtwlsutV.js} +1 -1
- package/dist/assets/{command-palette-B-4BGife.js → command-palette-C5YuwxOo.js} +1 -1
- package/dist/assets/{common-CPsAf0Xi.js → common-B3E9c-gU.js} +1 -1
- package/dist/assets/{components-CRWZ9jxm.js → components-DFkvRpxg.js} +1 -1
- package/dist/assets/{components-DptWjGr1.js → components-wm4TXPI6.js} +1 -1
- package/dist/assets/{config-C1gn3-tv.js → config-CasRXHAM.js} +1 -1
- package/dist/assets/{datasource-D1itEHCP.js → datasource-rDrNZEmo.js} +1 -1
- package/dist/assets/{dependency-graph-panel-BpJ5fvBf.js → dependency-graph-panel-BWe9yDlP.js} +1 -1
- package/dist/assets/{documentation-panel-CeNyn2VS.js → documentation-panel-BAxMFFrB.js} +1 -1
- package/dist/assets/{download-BQQnYuPC.js → download-NRqwJhRT.js} +1 -1
- package/dist/assets/{edit-page-OW7p7hhZ.js → edit-page-Dpl8DrGv.js} +3 -3
- package/dist/assets/{error-panel-BqJy7htp.js → error-panel-CByNONxp.js} +1 -1
- package/dist/assets/{file-explorer-panel-T78K2mhI.js → file-explorer-panel-DcCFx-Mu.js} +1 -1
- package/dist/assets/{file-icons-DUEFtaqk.js → file-icons-CV0odlbf.js} +1 -1
- package/dist/assets/{floating-outline-L6NC-sSl.js → floating-outline-DmddQK6b.js} +1 -1
- package/dist/assets/{focus-BTk8fi9t.js → focus-DLnEejrn.js} +1 -1
- package/dist/assets/{form-D0jBoXOL.js → form-Dnx_5kyZ.js} +1 -1
- package/dist/assets/{glide-data-editor-BAApVyo5.js → glide-data-editor-CTMu1iy8.js} +1 -1
- package/dist/assets/{globals-DggqXXLE.js → globals-CDDbRvHE.js} +1 -1
- package/dist/assets/{home-page-B79oM4YL.js → home-page-ffvNN1h7.js} +1 -1
- package/dist/assets/{hooks-C1JamGaP.js → hooks-DuHQuYPv.js} +1 -1
- package/dist/assets/{html-to-image-BJNmIfR_.js → html-to-image-CzpeYelq.js} +1 -1
- package/dist/assets/{index-f36_4Wqc.js → index-BONuktPB.js} +7 -7
- package/dist/assets/{kiosk-mode-DObJuod3.js → kiosk-mode-C6QWFFID.js} +1 -1
- package/dist/assets/{layout-CURagHSL.js → layout-dxaLq-eR.js} +3 -3
- package/dist/assets/{logs-panel-6wZKPXbW.js → logs-panel-9W91k1zJ.js} +1 -1
- package/dist/assets/{markdown-renderer-Bhg3x2dD.js → markdown-renderer-B-iPeXPA.js} +1 -1
- package/dist/assets/{mermaid-D619LWHW.js → mermaid-6e36Mqc3.js} +1 -1
- package/dist/assets/{name-cell-input-lIZhjdfv.js → name-cell-input-uFfmqWYT.js} +1 -1
- package/dist/assets/{outline-panel-5qkCPv_h.js → outline-panel-BAWIWb20.js} +1 -1
- package/dist/assets/{packages-panel-PygqATjl.js → packages-panel-DncGPBWC.js} +1 -1
- package/dist/assets/{panels-Cmkyo_Ic.js → panels-Bu8o6Qyj.js} +1 -1
- package/dist/assets/{process-output-CnW6OJhK.js → process-output-D06o1mmH.js} +1 -1
- package/dist/assets/{radio-group-DkjQgwSt.js → radio-group-DUTOVun5.js} +1 -1
- package/dist/assets/{readonly-python-code-CQ2k62cK.js → readonly-python-code-BnaYQo0K.js} +1 -1
- package/dist/assets/{renderShortcut-U2SzoZCS.js → renderShortcut-BrHz5aiT.js} +1 -1
- package/dist/assets/{reveal-component-bk9gf4uY.js → reveal-component-BXfhZjrl.js} +1 -1
- package/dist/assets/{run-page-BPJqsadV.js → run-page-BKurXtY7.js} +1 -1
- package/dist/assets/{scratchpad-panel-M7BhoesO.js → scratchpad-panel-D8kI0OKl.js} +1 -1
- package/dist/assets/{session-panel-DedYL518.js → session-panel-07dCccS4.js} +1 -1
- package/dist/assets/{snippets-panel-BwG5R102.js → snippets-panel-s3hpmPTY.js} +1 -1
- package/dist/assets/{state-Ba2SVwFW.js → state-B5L8iIIr.js} +1 -1
- package/dist/assets/{state-BmbZU1Ii.js → state-DIFGjBXT.js} +1 -1
- package/dist/assets/{switch-C8Pev-_F.js → switch-BrbMxTq7.js} +1 -1
- package/dist/assets/{terminal-HJ3qH9Tb.js → terminal-DXnxLykO.js} +1 -1
- package/dist/assets/{textarea-DqCLSCio.js → textarea-D0VOSGcU.js} +1 -1
- package/dist/assets/{tracing-eVEFFaeS.js → tracing-CSRhEdtZ.js} +1 -1
- package/dist/assets/{tracing-panel-8wrZPouc.js → tracing-panel-DuaSjEhP.js} +2 -2
- package/dist/assets/{useBoolean-DRITaoFR.js → useBoolean-CtP63TnG.js} +1 -1
- package/dist/assets/{useCellActionButton-qqJPqBNt.js → useCellActionButton-CtygwcqH.js} +1 -1
- package/dist/assets/{useDeleteCell-CIjjkrUZ.js → useDeleteCell-DeZ7T5A3.js} +1 -1
- package/dist/assets/{useDependencyPanelTab-DCLj8N8-.js → useDependencyPanelTab-Bpa86UXQ.js} +1 -1
- package/dist/assets/{useHotkey-CKk0vHze.js → useHotkey-D2dzNgpx.js} +1 -1
- package/dist/assets/{useNotebookActions-DFRxa9MH.js → useNotebookActions-DuMI8tMy.js} +1 -1
- package/dist/assets/{useRunCells-CjBIWw0W.js → useRunCells-CScbBaxj.js} +1 -1
- package/dist/assets/{useSplitCell-DrwP_UaG.js → useSplitCell-CBlrUeHM.js} +1 -1
- package/dist/assets/{useTheme-TeWD29Uz.js → useTheme-BzHkS4pu.js} +1 -1
- package/dist/assets/utils-Cp7O5e1E.js +1 -0
- package/dist/assets/{vega-component-CBW7fUCI.js → vega-component-CJdVnPJQ.js} +1 -1
- package/dist/index.html +29 -29
- package/package.json +1 -1
- package/src/components/app-config/user-config-form.tsx +36 -0
- package/src/components/chat/__tests__/chat-utils.test.ts +269 -0
- package/src/components/chat/chat-utils.ts +14 -58
- package/src/core/codemirror/__tests__/setup.test.ts +29 -0
- package/src/core/codemirror/cm.ts +3 -2
- package/src/core/config/__tests__/config-schema.test.ts +2 -0
- package/src/core/config/config-schema.ts +1 -0
- package/src/plugins/impl/chat/ChatPlugin.tsx +7 -1
- package/src/plugins/impl/chat/__tests__/chat-ui.test.ts +278 -0
- package/src/plugins/impl/chat/chat-ui.tsx +106 -59
- package/src/plugins/impl/chat/types.ts +5 -0
- package/dist/assets/chat-ui-Dk62XQcq.js +0 -4
- package/dist/assets/utils-Wvjk_Y4h.js +0 -1
|
@@ -5,7 +5,8 @@ import {
|
|
|
5
5
|
type ChatAddToolOutputFunction,
|
|
6
6
|
type FileUIPart,
|
|
7
7
|
isToolUIPart,
|
|
8
|
-
|
|
8
|
+
lastAssistantMessageIsCompleteWithApprovalResponses,
|
|
9
|
+
lastAssistantMessageIsCompleteWithToolCalls,
|
|
9
10
|
type UIMessage,
|
|
10
11
|
} from "ai";
|
|
11
12
|
import { useState } from "react";
|
|
@@ -17,7 +18,6 @@ import type {
|
|
|
17
18
|
InvokeAiToolRequest,
|
|
18
19
|
InvokeAiToolResponse,
|
|
19
20
|
} from "@/core/network/types";
|
|
20
|
-
import { logNever } from "@/utils/assertNever";
|
|
21
21
|
import { blobToString } from "@/utils/fileToBase64";
|
|
22
22
|
import { Logger } from "@/utils/Logger";
|
|
23
23
|
import { getAICompletionBodyWithAttachments } from "../editor/ai/completion-utils";
|
|
@@ -169,69 +169,25 @@ export async function handleToolCall({
|
|
|
169
169
|
}
|
|
170
170
|
|
|
171
171
|
/**
|
|
172
|
-
*
|
|
173
|
-
*
|
|
174
|
-
*
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
switch (state) {
|
|
178
|
-
case "output-available":
|
|
179
|
-
case "output-error":
|
|
180
|
-
case "output-denied":
|
|
181
|
-
case "approval-responded":
|
|
182
|
-
return true;
|
|
183
|
-
case "input-streaming":
|
|
184
|
-
case "input-available":
|
|
185
|
-
case "approval-requested":
|
|
186
|
-
return false;
|
|
187
|
-
default:
|
|
188
|
-
logNever(state);
|
|
189
|
-
return false;
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
/**
|
|
194
|
-
* Checks if we should send a message automatically based on the messages.
|
|
195
|
-
* We auto-send when every tool call on the last assistant message has either
|
|
196
|
-
* finished (output-available/error/denied) or has just received a user
|
|
197
|
-
* approval response, and the assistant hasn't replied yet.
|
|
172
|
+
* Auto-send the next turn when the last assistant message ends with a
|
|
173
|
+
* tool call ready to round-trip. Any non-tool trailing part (text, file,
|
|
174
|
+
* source-*, reasoning, data-*, new step-start) means the assistant has
|
|
175
|
+
* already answered, so we leave the next turn to the user. State checks
|
|
176
|
+
* are delegated to the SDK to stay in sync with upstream.
|
|
198
177
|
*/
|
|
199
178
|
export function hasPendingToolCalls(messages: UIMessage[]): boolean {
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
const lastMessage = messages[messages.length - 1];
|
|
205
|
-
const parts = lastMessage.parts;
|
|
206
|
-
|
|
207
|
-
if (parts.length === 0) {
|
|
208
|
-
return false;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
// Only auto-send if the last message is an assistant message
|
|
212
|
-
// Because assistant messages are the ones that can have tool calls
|
|
213
|
-
if (lastMessage.role !== "assistant") {
|
|
179
|
+
const lastMessage = messages.at(-1);
|
|
180
|
+
if (!lastMessage || lastMessage.role !== "assistant") {
|
|
214
181
|
return false;
|
|
215
182
|
}
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
if (toolParts.length === 0) {
|
|
183
|
+
const lastPart = lastMessage.parts.at(-1);
|
|
184
|
+
if (!lastPart || !isToolUIPart(lastPart)) {
|
|
220
185
|
return false;
|
|
221
186
|
}
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
187
|
+
return (
|
|
188
|
+
lastAssistantMessageIsCompleteWithToolCalls({ messages }) ||
|
|
189
|
+
lastAssistantMessageIsCompleteWithApprovalResponses({ messages })
|
|
225
190
|
);
|
|
226
|
-
|
|
227
|
-
// Check if the last part has any text content
|
|
228
|
-
const lastPart = parts[parts.length - 1];
|
|
229
|
-
const hasTextContent =
|
|
230
|
-
lastPart.type === "text" && lastPart.text?.trim().length > 0;
|
|
231
|
-
|
|
232
|
-
Logger.debug("All tool calls ready to send: %s", allToolCallsReady);
|
|
233
|
-
|
|
234
|
-
return allToolCallsReady && !hasTextContent;
|
|
235
191
|
}
|
|
236
192
|
|
|
237
193
|
export function useFileState() {
|
|
@@ -134,6 +134,35 @@ describe("snapshot all duplicate keymaps", () => {
|
|
|
134
134
|
});
|
|
135
135
|
});
|
|
136
136
|
|
|
137
|
+
test("auto_close_pairs: false removes closeBrackets keymaps", () => {
|
|
138
|
+
const withAutoClose = EditorState.create({
|
|
139
|
+
extensions: setup(),
|
|
140
|
+
});
|
|
141
|
+
const withoutAutoClose = EditorState.create({
|
|
142
|
+
extensions: setup({
|
|
143
|
+
completionConfig: {
|
|
144
|
+
...getOpts().completionConfig,
|
|
145
|
+
auto_close_pairs: false,
|
|
146
|
+
},
|
|
147
|
+
}),
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
const keysWith = withAutoClose.facet(keymap).flat();
|
|
151
|
+
const keysWithout = withoutAutoClose.facet(keymap).flat();
|
|
152
|
+
|
|
153
|
+
// closeBracketsKeymap contributes Backspace and Enter handlers
|
|
154
|
+
expect(keysWith.length).toBeGreaterThan(keysWithout.length);
|
|
155
|
+
|
|
156
|
+
const hasBracketPairHandler = (state: EditorState) =>
|
|
157
|
+
state
|
|
158
|
+
.facet(keymap)
|
|
159
|
+
.flat()
|
|
160
|
+
.some((k) => k.run?.name === "deleteBracketPair");
|
|
161
|
+
|
|
162
|
+
expect(hasBracketPairHandler(withAutoClose)).toBe(true);
|
|
163
|
+
expect(hasBracketPairHandler(withoutAutoClose)).toBe(false);
|
|
164
|
+
});
|
|
165
|
+
|
|
137
166
|
test("placeholder adds another extension", () => {
|
|
138
167
|
const opts = getOpts();
|
|
139
168
|
const withAI = new PythonLanguageAdapter()
|
|
@@ -182,6 +182,7 @@ export const basicBundle = (opts: CodeMirrorSetupOpts): Extension[] => {
|
|
|
182
182
|
diagnosticsConfig,
|
|
183
183
|
} = opts;
|
|
184
184
|
const placeholderType = getPlaceholderType(opts);
|
|
185
|
+
const autoClosePairs = completionConfig.auto_close_pairs !== false;
|
|
185
186
|
|
|
186
187
|
return [
|
|
187
188
|
///// View
|
|
@@ -208,10 +209,10 @@ export const basicBundle = (opts: CodeMirrorSetupOpts): Extension[] => {
|
|
|
208
209
|
copilotBundle(completionConfig),
|
|
209
210
|
foldGutter(),
|
|
210
211
|
stringsAutoCloseBraces(),
|
|
211
|
-
closeBrackets(),
|
|
212
|
+
autoClosePairs ? closeBrackets() : [],
|
|
212
213
|
completionKeymap(acceptCompletionOnEnter),
|
|
213
214
|
// to avoid clash with charDeleteBackward keymap
|
|
214
|
-
Prec.high(keymap.of(closeBracketsKeymap)),
|
|
215
|
+
autoClosePairs ? Prec.high(keymap.of(closeBracketsKeymap)) : [],
|
|
215
216
|
bracketMatching(),
|
|
216
217
|
indentOnInput(),
|
|
217
218
|
indentUnit.of(" "),
|
|
@@ -56,6 +56,7 @@ test("default UserConfig - empty", () => {
|
|
|
56
56
|
},
|
|
57
57
|
"completion": {
|
|
58
58
|
"activate_on_typing": true,
|
|
59
|
+
"auto_close_pairs": true,
|
|
59
60
|
"copilot": false,
|
|
60
61
|
"signature_hint_on_typing": false,
|
|
61
62
|
},
|
|
@@ -127,6 +128,7 @@ test("default UserConfig - one level", () => {
|
|
|
127
128
|
},
|
|
128
129
|
"completion": {
|
|
129
130
|
"activate_on_typing": true,
|
|
131
|
+
"auto_close_pairs": true,
|
|
130
132
|
"copilot": false,
|
|
131
133
|
"signature_hint_on_typing": false,
|
|
132
134
|
},
|
|
@@ -75,6 +75,7 @@ export const UserConfigSchema = z
|
|
|
75
75
|
.object({
|
|
76
76
|
activate_on_typing: z.boolean().prefault(true),
|
|
77
77
|
signature_hint_on_typing: z.boolean().prefault(false),
|
|
78
|
+
auto_close_pairs: z.boolean().prefault(true),
|
|
78
79
|
copilot: z
|
|
79
80
|
.union([z.boolean(), z.enum(["github", "codeium", "custom"])])
|
|
80
81
|
.prefault(false)
|
|
@@ -6,7 +6,7 @@ import { z } from "zod";
|
|
|
6
6
|
import { createPlugin } from "@/plugins/core/builder";
|
|
7
7
|
import { rpc } from "@/plugins/core/rpc";
|
|
8
8
|
import { Arrays } from "@/utils/arrays";
|
|
9
|
-
import type { SendMessageRequest } from "./types";
|
|
9
|
+
import type { CancelPromptRequest, SendMessageRequest } from "./types";
|
|
10
10
|
|
|
11
11
|
const LazyChatbot = React.lazy(() =>
|
|
12
12
|
import("./chat-ui").then((m) => ({ default: m.Chatbot })),
|
|
@@ -18,6 +18,7 @@ export type PluginFunctions = {
|
|
|
18
18
|
delete_chat_history: (req: {}) => Promise<null>;
|
|
19
19
|
delete_chat_message: (req: { index: number }) => Promise<null>;
|
|
20
20
|
send_prompt: (req: SendMessageRequest) => Promise<unknown>;
|
|
21
|
+
cancel_prompt: (req: CancelPromptRequest) => Promise<null>;
|
|
21
22
|
};
|
|
22
23
|
|
|
23
24
|
const messageSchema = z.array(
|
|
@@ -65,11 +66,15 @@ export const ChatPlugin = createPlugin<{ messages: UIMessage[] }>(
|
|
|
65
66
|
send_prompt: rpc
|
|
66
67
|
.input(
|
|
67
68
|
z.object({
|
|
69
|
+
request_id: z.string(),
|
|
68
70
|
messages: messageSchema,
|
|
69
71
|
config: configSchema,
|
|
70
72
|
}),
|
|
71
73
|
)
|
|
72
74
|
.output(z.unknown()),
|
|
75
|
+
cancel_prompt: rpc
|
|
76
|
+
.input(z.object({ request_id: z.string() }))
|
|
77
|
+
.output(z.null()),
|
|
73
78
|
})
|
|
74
79
|
.renderer((props) => (
|
|
75
80
|
<Suspense>
|
|
@@ -84,6 +89,7 @@ export const ChatPlugin = createPlugin<{ messages: UIMessage[] }>(
|
|
|
84
89
|
delete_chat_history={props.functions.delete_chat_history}
|
|
85
90
|
delete_chat_message={props.functions.delete_chat_message}
|
|
86
91
|
send_prompt={props.functions.send_prompt}
|
|
92
|
+
cancel_prompt={props.functions.cancel_prompt}
|
|
87
93
|
value={props.value?.messages || Arrays.EMPTY}
|
|
88
94
|
setValue={(messages) => props.setValue({ messages })}
|
|
89
95
|
host={props.host}
|
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
/* Copyright 2026 Marimo. All rights reserved. */
|
|
2
|
+
|
|
3
|
+
import type { UIMessageChunk } from "ai";
|
|
4
|
+
import { describe, expect, it, vi } from "vitest";
|
|
5
|
+
import { routeIncomingChatChunk } from "../chat-ui";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The stale-chunk filter prevents chunks from an aborted run from being enqueued into a new run's stream.
|
|
9
|
+
*
|
|
10
|
+
* It triggers when:
|
|
11
|
+
* 1. User sends a prompt (request_id = OLD), kernel starts emitting chunks
|
|
12
|
+
* 2. User clicks Stop — frontend tears down its controller, fires cancel_prompt
|
|
13
|
+
* 3. Kernel hasn't received the cancel yet and is still emitting chunks
|
|
14
|
+
* 4. User sends a new prompt (request_id = NEW), new controller opens
|
|
15
|
+
* 5. Late chunks tagged OLD arrive after NEW's controller is in place
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const makeChunk = (opts: {
|
|
19
|
+
messageId: string;
|
|
20
|
+
content: unknown;
|
|
21
|
+
isFinal?: boolean;
|
|
22
|
+
}): Parameters<typeof routeIncomingChatChunk>[0] => ({
|
|
23
|
+
type: "stream_chunk",
|
|
24
|
+
message_id: opts.messageId,
|
|
25
|
+
content: opts.content as UIMessageChunk | null,
|
|
26
|
+
is_final: opts.isFinal ?? false,
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
const makeRefs = () => ({
|
|
30
|
+
controllerRef: {
|
|
31
|
+
current: null as ReadableStreamDefaultController<UIMessageChunk> | null,
|
|
32
|
+
},
|
|
33
|
+
activeRequestIdRef: { current: null as string | null },
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const makeMockController = () => {
|
|
37
|
+
return {
|
|
38
|
+
enqueue: vi.fn(),
|
|
39
|
+
close: vi.fn(),
|
|
40
|
+
error: vi.fn(),
|
|
41
|
+
desiredSize: 0,
|
|
42
|
+
} as unknown as ReadableStreamDefaultController<UIMessageChunk> & {
|
|
43
|
+
enqueue: ReturnType<typeof vi.fn>;
|
|
44
|
+
close: ReturnType<typeof vi.fn>;
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
describe("routeIncomingChatChunk", () => {
|
|
49
|
+
it("drops chunks when there is no active controller", () => {
|
|
50
|
+
const refs = makeRefs();
|
|
51
|
+
|
|
52
|
+
const result = routeIncomingChatChunk(
|
|
53
|
+
makeChunk({
|
|
54
|
+
messageId: "req-A",
|
|
55
|
+
content: { type: "text-delta", id: "t1", delta: "hi" },
|
|
56
|
+
}),
|
|
57
|
+
refs,
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
expect(result).toBe("dropped-no-controller");
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("enqueues chunks that match the active request_id", () => {
|
|
64
|
+
const refs = makeRefs();
|
|
65
|
+
const controller = makeMockController();
|
|
66
|
+
refs.controllerRef.current = controller;
|
|
67
|
+
refs.activeRequestIdRef.current = "req-A";
|
|
68
|
+
|
|
69
|
+
const chunk = { type: "text-delta", id: "t1", delta: "hi" } as const;
|
|
70
|
+
const result = routeIncomingChatChunk(
|
|
71
|
+
makeChunk({ messageId: "req-A", content: chunk }),
|
|
72
|
+
refs,
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
expect(result).toBe("enqueued");
|
|
76
|
+
expect(controller.enqueue).toHaveBeenCalledWith(chunk);
|
|
77
|
+
expect(controller.close).not.toHaveBeenCalled();
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("closes the controller and clears refs on is_final", () => {
|
|
81
|
+
const refs = makeRefs();
|
|
82
|
+
const controller = makeMockController();
|
|
83
|
+
refs.controllerRef.current = controller;
|
|
84
|
+
refs.activeRequestIdRef.current = "req-A";
|
|
85
|
+
|
|
86
|
+
const result = routeIncomingChatChunk(
|
|
87
|
+
makeChunk({ messageId: "req-A", content: null, isFinal: true }),
|
|
88
|
+
refs,
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
expect(result).toBe("closed");
|
|
92
|
+
expect(controller.close).toHaveBeenCalledTimes(1);
|
|
93
|
+
expect(refs.controllerRef.current).toBeNull();
|
|
94
|
+
expect(refs.activeRequestIdRef.current).toBeNull();
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("drops chunks whose message_id does not match the active run", () => {
|
|
98
|
+
// Simulates the bug: kernel hasn't received cancel for OLD yet but the
|
|
99
|
+
// user has already started a NEW run. A reasoning-delta for OLD arrives
|
|
100
|
+
// here; it must not be enqueued into NEW's stream.
|
|
101
|
+
const refs = makeRefs();
|
|
102
|
+
const controller = makeMockController();
|
|
103
|
+
refs.controllerRef.current = controller;
|
|
104
|
+
refs.activeRequestIdRef.current = "req-NEW";
|
|
105
|
+
|
|
106
|
+
const staleChunk = {
|
|
107
|
+
type: "reasoning-delta",
|
|
108
|
+
id: "r-old",
|
|
109
|
+
delta: "...",
|
|
110
|
+
} as const;
|
|
111
|
+
const result = routeIncomingChatChunk(
|
|
112
|
+
makeChunk({ messageId: "req-OLD", content: staleChunk }),
|
|
113
|
+
refs,
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
expect(result).toBe("dropped-stale");
|
|
117
|
+
expect(controller.enqueue).not.toHaveBeenCalled();
|
|
118
|
+
expect(controller.close).not.toHaveBeenCalled();
|
|
119
|
+
expect(refs.activeRequestIdRef.current).toBe("req-NEW");
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it("drops is_final from a stale run without closing the active stream", () => {
|
|
123
|
+
// Belt-and-suspenders: an `is_final` for OLD that races in after NEW
|
|
124
|
+
// started must not tear down NEW's controller.
|
|
125
|
+
const refs = makeRefs();
|
|
126
|
+
const controller = makeMockController();
|
|
127
|
+
refs.controllerRef.current = controller;
|
|
128
|
+
refs.activeRequestIdRef.current = "req-NEW";
|
|
129
|
+
|
|
130
|
+
const result = routeIncomingChatChunk(
|
|
131
|
+
makeChunk({ messageId: "req-OLD", content: null, isFinal: true }),
|
|
132
|
+
refs,
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
expect(result).toBe("dropped-stale");
|
|
136
|
+
expect(controller.close).not.toHaveBeenCalled();
|
|
137
|
+
expect(refs.controllerRef.current).toBe(controller);
|
|
138
|
+
expect(refs.activeRequestIdRef.current).toBe("req-NEW");
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it("forwards reasoning-start/delta/end sequences when ids match", () => {
|
|
142
|
+
// Walks the canonical happy path for a reasoning stream end-to-end.
|
|
143
|
+
const refs = makeRefs();
|
|
144
|
+
const controller = makeMockController();
|
|
145
|
+
refs.controllerRef.current = controller;
|
|
146
|
+
refs.activeRequestIdRef.current = "req-A";
|
|
147
|
+
|
|
148
|
+
const sequence = [
|
|
149
|
+
{ type: "reasoning-start", id: "r1" },
|
|
150
|
+
{ type: "reasoning-delta", id: "r1", delta: "thinking" },
|
|
151
|
+
{ type: "reasoning-end", id: "r1" },
|
|
152
|
+
] as const;
|
|
153
|
+
for (const chunk of sequence) {
|
|
154
|
+
const result = routeIncomingChatChunk(
|
|
155
|
+
makeChunk({ messageId: "req-A", content: chunk }),
|
|
156
|
+
refs,
|
|
157
|
+
);
|
|
158
|
+
expect(result).toBe("enqueued");
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
expect(controller.enqueue).toHaveBeenCalledTimes(3);
|
|
162
|
+
expect(controller.enqueue).toHaveBeenNthCalledWith(1, sequence[0]);
|
|
163
|
+
expect(controller.enqueue).toHaveBeenNthCalledWith(2, sequence[1]);
|
|
164
|
+
expect(controller.enqueue).toHaveBeenNthCalledWith(3, sequence[2]);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it(
|
|
168
|
+
"drops stale reasoning-delta after Stop → new run sequence " +
|
|
169
|
+
"(regression for missing reasoning part error)",
|
|
170
|
+
() => {
|
|
171
|
+
// Full scenario: A runs, A is stopped, B starts, A's late chunk arrives.
|
|
172
|
+
const refs = makeRefs();
|
|
173
|
+
|
|
174
|
+
// 1. Run A starts: controller A active.
|
|
175
|
+
const controllerA = makeMockController();
|
|
176
|
+
refs.controllerRef.current = controllerA;
|
|
177
|
+
refs.activeRequestIdRef.current = "req-A";
|
|
178
|
+
|
|
179
|
+
// First reasoning chunks for A flow through.
|
|
180
|
+
routeIncomingChatChunk(
|
|
181
|
+
makeChunk({
|
|
182
|
+
messageId: "req-A",
|
|
183
|
+
content: { type: "reasoning-start", id: "rA" },
|
|
184
|
+
}),
|
|
185
|
+
refs,
|
|
186
|
+
);
|
|
187
|
+
routeIncomingChatChunk(
|
|
188
|
+
makeChunk({
|
|
189
|
+
messageId: "req-A",
|
|
190
|
+
content: {
|
|
191
|
+
type: "reasoning-delta",
|
|
192
|
+
id: "rA",
|
|
193
|
+
delta: "thinking",
|
|
194
|
+
},
|
|
195
|
+
}),
|
|
196
|
+
refs,
|
|
197
|
+
);
|
|
198
|
+
expect(controllerA.enqueue).toHaveBeenCalledTimes(2);
|
|
199
|
+
|
|
200
|
+
// 2. User clicks Stop: abort handler clears refs (simulated).
|
|
201
|
+
refs.controllerRef.current = null;
|
|
202
|
+
refs.activeRequestIdRef.current = null;
|
|
203
|
+
|
|
204
|
+
// A late chunk for A arrives in this window — must be a no-op.
|
|
205
|
+
const between = routeIncomingChatChunk(
|
|
206
|
+
makeChunk({
|
|
207
|
+
messageId: "req-A",
|
|
208
|
+
content: {
|
|
209
|
+
type: "reasoning-delta",
|
|
210
|
+
id: "rA",
|
|
211
|
+
delta: "leftover",
|
|
212
|
+
},
|
|
213
|
+
}),
|
|
214
|
+
refs,
|
|
215
|
+
);
|
|
216
|
+
expect(between).toBe("dropped-no-controller");
|
|
217
|
+
|
|
218
|
+
// 3. User sends Run B: new controller, new active id.
|
|
219
|
+
const controllerB = makeMockController();
|
|
220
|
+
refs.controllerRef.current = controllerB;
|
|
221
|
+
refs.activeRequestIdRef.current = "req-B";
|
|
222
|
+
|
|
223
|
+
// 4. Another late chunk for A arrives AFTER B opened. This is the
|
|
224
|
+
// case that previously threw `Received reasoning-delta for missing
|
|
225
|
+
// reasoning part with ID "rA"` in the SDK parser.
|
|
226
|
+
const stale = routeIncomingChatChunk(
|
|
227
|
+
makeChunk({
|
|
228
|
+
messageId: "req-A",
|
|
229
|
+
content: {
|
|
230
|
+
type: "reasoning-delta",
|
|
231
|
+
id: "rA",
|
|
232
|
+
delta: "still leaking",
|
|
233
|
+
},
|
|
234
|
+
}),
|
|
235
|
+
refs,
|
|
236
|
+
);
|
|
237
|
+
expect(stale).toBe("dropped-stale");
|
|
238
|
+
expect(controllerB.enqueue).not.toHaveBeenCalled();
|
|
239
|
+
|
|
240
|
+
// 5. B's own chunks flow normally.
|
|
241
|
+
routeIncomingChatChunk(
|
|
242
|
+
makeChunk({
|
|
243
|
+
messageId: "req-B",
|
|
244
|
+
content: { type: "reasoning-start", id: "rB" },
|
|
245
|
+
}),
|
|
246
|
+
refs,
|
|
247
|
+
);
|
|
248
|
+
routeIncomingChatChunk(
|
|
249
|
+
makeChunk({
|
|
250
|
+
messageId: "req-B",
|
|
251
|
+
content: { type: "reasoning-delta", id: "rB", delta: "fresh" },
|
|
252
|
+
}),
|
|
253
|
+
refs,
|
|
254
|
+
);
|
|
255
|
+
expect(controllerB.enqueue).toHaveBeenCalledTimes(2);
|
|
256
|
+
},
|
|
257
|
+
);
|
|
258
|
+
|
|
259
|
+
it("enqueues content alongside is_final and then closes", () => {
|
|
260
|
+
// Sanity: a single chunk that carries both `content` and `is_final` (rare
|
|
261
|
+
// but legal — backend may bundle final content with the terminator)
|
|
262
|
+
// should enqueue then close.
|
|
263
|
+
const refs = makeRefs();
|
|
264
|
+
const controller = makeMockController();
|
|
265
|
+
refs.controllerRef.current = controller;
|
|
266
|
+
refs.activeRequestIdRef.current = "req-A";
|
|
267
|
+
|
|
268
|
+
const chunk = { type: "text-delta", id: "t1", delta: "bye" } as const;
|
|
269
|
+
const result = routeIncomingChatChunk(
|
|
270
|
+
makeChunk({ messageId: "req-A", content: chunk, isFinal: true }),
|
|
271
|
+
refs,
|
|
272
|
+
);
|
|
273
|
+
|
|
274
|
+
expect(result).toBe("closed");
|
|
275
|
+
expect(controller.enqueue).toHaveBeenCalledWith(chunk);
|
|
276
|
+
expect(controller.close).toHaveBeenCalledTimes(1);
|
|
277
|
+
});
|
|
278
|
+
});
|