@marimo-team/islands 0.23.9-dev9 → 0.23.9

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 (101) hide show
  1. package/dist/{ConnectedDataExplorerComponent-OzrfMM5L.js → ConnectedDataExplorerComponent-CyV83R2m.js} +4 -4
  2. package/dist/assets/__vite-browser-external-Ci2ZQfXU.js +1 -0
  3. package/dist/assets/{worker-CpBbwbQo.js → worker-ip3AI_sN.js} +2 -2
  4. package/dist/{chat-ui-BDI3FMI8.js → chat-ui-ChD4VvCo.js} +3060 -3033
  5. package/dist/{code-visibility-DgHF4q8X.js → code-visibility-BkuwTYAm.js} +1368 -1204
  6. package/dist/{formats-DQ5qjo_Q.js → formats-DHxc-FdY.js} +1 -1
  7. package/dist/{glide-data-editor-DqRY9naW.js → glide-data-editor-BOmK9ETQ.js} +2 -2
  8. package/dist/{html-to-image-CiSinpSR.js → html-to-image-BHv7CEU_.js} +2145 -2153
  9. package/dist/{input-CZD2z6X2.js → input-_2sjvfne.js} +1 -1
  10. package/dist/main.js +680 -705
  11. package/dist/{mermaid-IU93XzmY.js → mermaid-lXOw5Py9.js} +2 -2
  12. package/dist/{process-output-5qJjMRKh.js → process-output-BvySRgli.js} +33 -25
  13. package/dist/{reveal-component-qpHJES_u.js → reveal-component-DeBkkDcg.js} +312 -291
  14. package/dist/{spec-a6DaqW__.js → spec-B96zNUEA.js} +1 -1
  15. package/dist/style.css +1 -1
  16. package/dist/{toDate-ZVVIBmdk.js → toDate-x-WRDCH7.js} +1 -1
  17. package/dist/{useAsyncData-C008zUPi.js → useAsyncData-iRgKDT5s.js} +1 -1
  18. package/dist/{useDeepCompareMemoize-BrA3_n61.js → useDeepCompareMemoize-CkQ57VS2.js} +1 -1
  19. package/dist/{useLifecycle-BNaoJ5a4.js → useLifecycle-BBO9PIph.js} +1 -1
  20. package/dist/{useTheme-7O0YWlE5.js → useTheme-DHIrRQOe.js} +34 -21
  21. package/dist/{vega-component-DJNmOdUj.js → vega-component-Dq-SH463.js} +5 -5
  22. package/package.json +1 -1
  23. package/src/components/ai/__tests__/ai-utils.test.ts +43 -38
  24. package/src/components/ai/ai-model-dropdown.tsx +2 -2
  25. package/src/components/app-config/ai-config.tsx +147 -16
  26. package/src/components/app-config/user-config-form.tsx +37 -1
  27. package/src/components/chat/__tests__/chat-utils.test.ts +269 -0
  28. package/src/components/chat/chat-panel.tsx +38 -5
  29. package/src/components/chat/chat-utils.ts +14 -58
  30. package/src/components/data-table/TableBottomBar.tsx +5 -8
  31. package/src/components/data-table/__tests__/column-explorer.test.tsx +128 -0
  32. package/src/components/data-table/__tests__/header-items.test.tsx +220 -10
  33. package/src/components/data-table/column-explorer-panel/column-explorer.tsx +95 -29
  34. package/src/components/data-table/column-header.tsx +17 -12
  35. package/src/components/data-table/data-table.tsx +4 -0
  36. package/src/components/data-table/export-actions.tsx +19 -12
  37. package/src/components/data-table/header-items.tsx +40 -16
  38. package/src/components/data-table/hooks/use-column-visibility.ts +14 -0
  39. package/src/components/data-table/schemas.ts +2 -2
  40. package/src/components/data-table/table-explorer-panel/table-explorer-panel.tsx +16 -6
  41. package/src/components/databases/display.tsx +2 -0
  42. package/src/components/datasources/__tests__/utils.test.ts +82 -0
  43. package/src/components/datasources/utils.ts +16 -15
  44. package/src/components/editor/Disconnected.tsx +1 -60
  45. package/src/components/editor/__tests__/viewer-banner.test.tsx +89 -0
  46. package/src/components/editor/actions/pair-with-agent-modal.tsx +1 -0
  47. package/src/components/editor/actions/useCellActionButton.tsx +3 -3
  48. package/src/components/editor/actions/useNotebookActions.tsx +5 -2
  49. package/src/components/editor/cell/code/cell-editor.tsx +25 -5
  50. package/src/components/editor/chrome/types.ts +13 -6
  51. package/src/components/editor/chrome/wrapper/app-chrome.tsx +6 -4
  52. package/src/components/editor/chrome/wrapper/footer-items/ai-status.tsx +10 -1
  53. package/src/components/editor/chrome/wrapper/sidebar.tsx +7 -5
  54. package/src/components/editor/errors/auto-fix.tsx +3 -3
  55. package/src/components/editor/header/__tests__/status.test.tsx +0 -15
  56. package/src/components/editor/header/app-header.tsx +1 -4
  57. package/src/components/editor/header/status.tsx +4 -13
  58. package/src/components/editor/navigation/__tests__/navigation.test.ts +15 -0
  59. package/src/components/editor/navigation/navigation.ts +5 -0
  60. package/src/components/editor/output/MarimoErrorOutput.tsx +103 -25
  61. package/src/components/editor/output/MarimoTracebackOutput.tsx +28 -39
  62. package/src/components/editor/renderers/cell-array.tsx +27 -24
  63. package/src/components/editor/renderers/slides-layout/__tests__/compute-slide-cells.test.ts +30 -17
  64. package/src/components/editor/renderers/slides-layout/compute-slide-cells.ts +17 -8
  65. package/src/components/editor/renderers/slides-layout/slides-layout.tsx +10 -12
  66. package/src/components/editor/viewer-banner.tsx +82 -0
  67. package/src/components/slides/minimap.tsx +45 -9
  68. package/src/components/slides/reveal-component.tsx +82 -37
  69. package/src/components/slides/slide-cell-view.tsx +12 -1
  70. package/src/components/slides/slide-form.tsx +11 -3
  71. package/src/components/static-html/static-banner.tsx +28 -22
  72. package/src/core/ai/__tests__/model-registry.test.ts +72 -60
  73. package/src/core/ai/model-registry.ts +33 -28
  74. package/src/core/cells/__tests__/actions.test.ts +48 -0
  75. package/src/core/cells/actions.ts +5 -6
  76. package/src/core/codemirror/__tests__/setup.test.ts +29 -0
  77. package/src/core/codemirror/cells/traceback-decorations.ts +1 -1
  78. package/src/core/codemirror/cm.ts +50 -3
  79. package/src/core/codemirror/completion/hints.ts +4 -1
  80. package/src/core/codemirror/format.ts +1 -0
  81. package/src/core/codemirror/keymaps/vim.ts +63 -0
  82. package/src/core/codemirror/language/languages/sql/sql.ts +1 -0
  83. package/src/core/codemirror/language/languages/sql/utils.ts +2 -0
  84. package/src/core/config/__tests__/config-schema.test.ts +4 -0
  85. package/src/core/config/config-schema.ts +4 -0
  86. package/src/core/config/config.ts +16 -0
  87. package/src/core/edit-app.tsx +3 -0
  88. package/src/core/islands/bootstrap.ts +2 -0
  89. package/src/core/kernel/__tests__/handlers.test.ts +5 -0
  90. package/src/core/websocket/__tests__/useMarimoKernelConnection.test.ts +0 -13
  91. package/src/core/websocket/types.ts +0 -6
  92. package/src/core/websocket/useMarimoKernelConnection.tsx +3 -12
  93. package/src/css/app/Cell.css +0 -1
  94. package/src/plugins/impl/DataTablePlugin.tsx +48 -22
  95. package/src/plugins/impl/chat/ChatPlugin.tsx +7 -1
  96. package/src/plugins/impl/chat/__tests__/chat-ui.test.ts +278 -0
  97. package/src/plugins/impl/chat/chat-ui.tsx +106 -59
  98. package/src/plugins/impl/chat/types.ts +5 -0
  99. package/src/utils/__tests__/json-parser.test.ts +1 -69
  100. package/src/utils/json/json-parser.ts +0 -30
  101. package/dist/assets/__vite-browser-external-CAdMKBac.js +0 -1
@@ -46,6 +46,7 @@ test("default UserConfig - empty", () => {
46
46
  {
47
47
  "ai": {
48
48
  "custom_providers": {},
49
+ "enabled": true,
49
50
  "inline_tooltip": false,
50
51
  "mode": "manual",
51
52
  "models": {
@@ -56,6 +57,7 @@ test("default UserConfig - empty", () => {
56
57
  },
57
58
  "completion": {
58
59
  "activate_on_typing": true,
60
+ "auto_close_pairs": true,
59
61
  "copilot": false,
60
62
  "signature_hint_on_typing": false,
61
63
  },
@@ -117,6 +119,7 @@ test("default UserConfig - one level", () => {
117
119
  {
118
120
  "ai": {
119
121
  "custom_providers": {},
122
+ "enabled": true,
120
123
  "inline_tooltip": false,
121
124
  "mode": "manual",
122
125
  "models": {
@@ -127,6 +130,7 @@ test("default UserConfig - one level", () => {
127
130
  },
128
131
  "completion": {
129
132
  "activate_on_typing": true,
133
+ "auto_close_pairs": true,
130
134
  "copilot": false,
131
135
  "signature_hint_on_typing": false,
132
136
  },
@@ -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)
@@ -157,7 +158,9 @@ export const UserConfigSchema = z
157
158
  .prefault({}),
158
159
  ai: z
159
160
  .looseObject({
161
+ enabled: z.boolean().prefault(true),
160
162
  rules: z.string().prefault(""),
163
+ max_tokens: z.number().int().positive().nullable().optional(),
161
164
  mode: z.enum(COPILOT_MODES).prefault("manual"),
162
165
  inline_tooltip: z.boolean().prefault(false),
163
166
  open_ai: AiConfigSchema.optional(),
@@ -207,6 +210,7 @@ export const UserConfigSchema = z
207
210
  .looseObject({
208
211
  html: z.boolean().optional(),
209
212
  wasm: z.boolean().optional(),
213
+ molab: z.boolean().optional(),
210
214
  })
211
215
  .optional(),
212
216
  mcp: z
@@ -78,6 +78,14 @@ export const aiEnabledAtom = atom<boolean>((get) => {
78
78
  return isAiEnabled(get(resolvedMarimoConfigAtom));
79
79
  });
80
80
 
81
+ export const aiModelConfiguredAtom = atom<boolean>((get) => {
82
+ return isAiModelConfigured(get(resolvedMarimoConfigAtom));
83
+ });
84
+
85
+ export const aiFeaturesEnabledAtom = atom<boolean>((get) => {
86
+ return isAiFeatureEnabled(get(resolvedMarimoConfigAtom));
87
+ });
88
+
81
89
  export const editorFontSizeAtom = atom<number>((get) => {
82
90
  return get(resolvedMarimoConfigAtom).display.code_editor_font_size;
83
91
  });
@@ -87,6 +95,10 @@ export const localeAtom = atom<string | null | undefined>((get) => {
87
95
  });
88
96
 
89
97
  export function isAiEnabled(config: UserConfig) {
98
+ return config.ai?.enabled !== false;
99
+ }
100
+
101
+ export function isAiModelConfigured(config: UserConfig) {
90
102
  return (
91
103
  Boolean(config.ai?.models?.chat_model) ||
92
104
  Boolean(config.ai?.models?.edit_model) ||
@@ -94,6 +106,10 @@ export function isAiEnabled(config: UserConfig) {
94
106
  );
95
107
  }
96
108
 
109
+ export function isAiFeatureEnabled(config: UserConfig) {
110
+ return isAiEnabled(config) && isAiModelConfigured(config);
111
+ }
112
+
97
113
  /**
98
114
  * Atom for storing the app config.
99
115
  */
@@ -12,6 +12,7 @@ import { Controls } from "@/components/editor/controls/Controls";
12
12
  import { AppHeader } from "@/components/editor/header/app-header";
13
13
  import { FilenameForm } from "@/components/editor/header/filename-form";
14
14
  import { MultiCellActionToolbar } from "@/components/editor/navigation/multi-cell-action-toolbar";
15
+ import { ViewerBanner } from "@/components/editor/viewer-banner";
15
16
  import { cn } from "@/utils/cn";
16
17
  import { Paths } from "@/utils/paths";
17
18
  import { AppContainer } from "../components/editor/app-container";
@@ -164,6 +165,8 @@ export const EditApp: React.FC<AppProps> = ({
164
165
  )}
165
166
  </AppHeader>
166
167
 
168
+ <ViewerBanner />
169
+
167
170
  {/* Don't render until we have a single cell */}
168
171
  {hasCells && (
169
172
  <CellsRenderer appConfig={appConfig} mode={viewState.mode}>
@@ -255,6 +255,8 @@ function handleMessage(
255
255
  handleWidgetMessage(MODEL_MANAGER, msg.data);
256
256
  return;
257
257
 
258
+ case "consumer-capabilities":
259
+ return;
258
260
  default:
259
261
  logNever(msg);
260
262
  return;
@@ -90,6 +90,7 @@ describe("buildCellData", () => {
90
90
  terminal: false,
91
91
  },
92
92
  auto_instantiated: false,
93
+ consumer_capabilities: { edit: true, interact: true },
93
94
  };
94
95
 
95
96
  const cells = buildCellData(kernelReadyData);
@@ -158,6 +159,7 @@ describe("buildCellData", () => {
158
159
  terminal: false,
159
160
  },
160
161
  auto_instantiated: false,
162
+ consumer_capabilities: { edit: true, interact: true },
161
163
  };
162
164
 
163
165
  const cells = buildCellData(kernelReadyData);
@@ -191,6 +193,7 @@ describe("buildCellData", () => {
191
193
  terminal: false,
192
194
  },
193
195
  auto_instantiated: false,
196
+ consumer_capabilities: { edit: true, interact: true },
194
197
  };
195
198
 
196
199
  const cells = buildCellData(kernelReadyData);
@@ -223,6 +226,7 @@ describe("buildLayoutState", () => {
223
226
  terminal: false,
224
227
  },
225
228
  auto_instantiated: false,
229
+ consumer_capabilities: { edit: true, interact: true },
226
230
  };
227
231
 
228
232
  const cells = buildCellData(kernelReadyData);
@@ -271,6 +275,7 @@ describe("buildLayoutState", () => {
271
275
  terminal: false,
272
276
  },
273
277
  auto_instantiated: false,
278
+ consumer_capabilities: { edit: true, interact: true },
274
279
  };
275
280
 
276
281
  const cells = buildCellData(kernelReadyData);
@@ -31,19 +31,6 @@ describe("classifyCloseEvent", () => {
31
31
  });
32
32
 
33
33
  describe("terminal closes (server-initiated)", () => {
34
- it("MARIMO_ALREADY_CONNECTED → terminal + closeTransport, with takeover", () => {
35
- const decision = classify("MARIMO_ALREADY_CONNECTED");
36
- expect(decision.kind).toBe("terminal");
37
- expect(decision.status).toMatchObject({
38
- state: WebSocketState.CLOSED,
39
- code: WebSocketClosedReason.ALREADY_RUNNING,
40
- canTakeover: true,
41
- });
42
- if (decision.kind === "terminal") {
43
- expect(decision.closeTransport).toBe(true);
44
- }
45
- });
46
-
47
34
  it.each([
48
35
  "MARIMO_WRONG_KERNEL_ID",
49
36
  "MARIMO_NO_FILE_KEY",
@@ -14,7 +14,6 @@ export type WebSocketState =
14
14
 
15
15
  export const WebSocketClosedReason = {
16
16
  KERNEL_DISCONNECTED: "KERNEL_DISCONNECTED",
17
- ALREADY_RUNNING: "ALREADY_RUNNING",
18
17
  MALFORMED_QUERY: "MALFORMED_QUERY",
19
18
  KERNEL_STARTUP_ERROR: "KERNEL_STARTUP_ERROR",
20
19
  } as const;
@@ -30,11 +29,6 @@ export type ConnectionStatus =
30
29
  * Human-readable reason for closing the connection.
31
30
  */
32
31
  reason: string;
33
- /**
34
- * Whether the current session can be taken over by another session,
35
- * since we only allow single-user editing.
36
- */
37
- canTakeover?: boolean;
38
32
  }
39
33
  | {
40
34
  state:
@@ -82,7 +82,6 @@ const SUPPORTS_LAZY_KERNELS = true;
82
82
  // (marimo/_server/api/endpoints/ws_endpoint.py and ws/*.py). Keep in sync with
83
83
  // the backend literals.
84
84
  export type CloseReason =
85
- | "MARIMO_ALREADY_CONNECTED"
86
85
  | "MARIMO_WRONG_KERNEL_ID"
87
86
  | "MARIMO_NO_FILE_KEY"
88
87
  | "MARIMO_NO_SESSION_ID"
@@ -99,17 +98,6 @@ export type CloseDecision =
99
98
 
100
99
  export function classifyCloseEvent(event: { reason?: string }): CloseDecision {
101
100
  switch (event.reason as CloseReason | undefined) {
102
- case "MARIMO_ALREADY_CONNECTED":
103
- return {
104
- kind: "terminal",
105
- status: {
106
- state: WebSocketState.CLOSED,
107
- code: WebSocketClosedReason.ALREADY_RUNNING,
108
- reason: "another browser tab is already connected to the kernel",
109
- canTakeover: true,
110
- },
111
- closeTransport: true,
112
- };
113
101
  case TRANSPORT_EXHAUSTED_REASON:
114
102
  return {
115
103
  kind: "gave-up",
@@ -421,6 +409,9 @@ export function useMarimoKernelConnection(opts: {
421
409
  case "notebook-document-transaction":
422
410
  handleDocumentTransaction(msg.data.transaction);
423
411
  return;
412
+ case "consumer-capabilities":
413
+ setKioskMode(!msg.data.consumer_capabilities.edit);
414
+ return;
424
415
  default:
425
416
  logNever(msg.data);
426
417
  }
@@ -214,7 +214,6 @@
214
214
  &.error-outline,
215
215
  &.error-outline:focus-within {
216
216
  box-shadow: 8px 8px 0 0 color-mix(in srgb, var(--error), transparent 80%);
217
- background-color: var(--red-2);
218
217
  }
219
218
 
220
219
  /* Needs Run */
@@ -8,6 +8,7 @@ import type {
8
8
  PaginationState,
9
9
  RowSelectionState,
10
10
  SortingState,
11
+ Table as TanstackTable,
11
12
  } from "@tanstack/react-table";
12
13
  import { Provider, useAtomValue } from "jotai";
13
14
  import { Table2Icon } from "lucide-react";
@@ -1040,6 +1041,52 @@ const DataTableComponent = ({
1040
1041
  const isInVscode = isInVscodeExtension();
1041
1042
  const isIslandsMode = isIslands();
1042
1043
 
1044
+ const renderTableExplorerPanel = useMemo(() => {
1045
+ if (!isAnyPanelOpen || !(showRowExplorer || canShowColumnExplorer)) {
1046
+ return undefined;
1047
+ }
1048
+ return (table: TanstackTable<unknown>) => (
1049
+ <ContextAwarePanelItem>
1050
+ <TableExplorerPanel
1051
+ rowIdx={viewedRowIdx}
1052
+ setRowIdx={setViewedRow}
1053
+ totalRows={totalRows}
1054
+ fieldTypes={memoizedUnclampedFieldTypes}
1055
+ getRow={getRow}
1056
+ isSelectable={isSelectable}
1057
+ isRowSelected={Boolean(rowSelection[viewedRowIdx])}
1058
+ handleRowSelectionChange={handleRowSelectionChange}
1059
+ previewColumn={preview_column}
1060
+ totalColumns={totalColumns}
1061
+ tableId={id}
1062
+ table={table}
1063
+ showRowExplorer={showRowExplorer && !isInVscode}
1064
+ showColumnExplorer={canShowColumnExplorer && !isInVscode}
1065
+ activeTab={panelType}
1066
+ onTabChange={setPanelType}
1067
+ />
1068
+ </ContextAwarePanelItem>
1069
+ );
1070
+ }, [
1071
+ isAnyPanelOpen,
1072
+ showRowExplorer,
1073
+ canShowColumnExplorer,
1074
+ viewedRowIdx,
1075
+ setViewedRow,
1076
+ totalRows,
1077
+ memoizedUnclampedFieldTypes,
1078
+ getRow,
1079
+ isSelectable,
1080
+ rowSelection,
1081
+ handleRowSelectionChange,
1082
+ preview_column,
1083
+ totalColumns,
1084
+ id,
1085
+ isInVscode,
1086
+ panelType,
1087
+ setPanelType,
1088
+ ]);
1089
+
1043
1090
  return (
1044
1091
  <>
1045
1092
  {/* When the totalRows is "too_many" and the pageSize is the same as the
@@ -1065,28 +1112,6 @@ const DataTableComponent = ({
1065
1112
  </Banner>
1066
1113
  )}
1067
1114
 
1068
- {isAnyPanelOpen && (showRowExplorer || canShowColumnExplorer) && (
1069
- <ContextAwarePanelItem>
1070
- <TableExplorerPanel
1071
- rowIdx={viewedRowIdx}
1072
- setRowIdx={setViewedRow}
1073
- totalRows={totalRows}
1074
- fieldTypes={memoizedUnclampedFieldTypes}
1075
- getRow={getRow}
1076
- isSelectable={isSelectable}
1077
- isRowSelected={Boolean(rowSelection[viewedRowIdx])}
1078
- handleRowSelectionChange={handleRowSelectionChange}
1079
- previewColumn={preview_column}
1080
- totalColumns={totalColumns}
1081
- tableId={id}
1082
- showRowExplorer={showRowExplorer && !isInVscode}
1083
- showColumnExplorer={canShowColumnExplorer && !isInVscode}
1084
- activeTab={panelType}
1085
- onTabChange={setPanelType}
1086
- />
1087
- </ContextAwarePanelItem>
1088
- )}
1089
-
1090
1115
  <ColumnChartContext value={chartSpecModel}>
1091
1116
  <Labeled label={label} align="top" fullWidth={true}>
1092
1117
  <DataTable
@@ -1143,6 +1168,7 @@ const DataTableComponent = ({
1143
1168
  isAnyPanelOpen={isAnyPanelOpen}
1144
1169
  viewedRowIdx={viewedRowIdx}
1145
1170
  onViewedRowChange={(rowIdx) => setViewedRowIdx(rowIdx)}
1171
+ renderTableExplorerPanel={renderTableExplorerPanel}
1146
1172
  />
1147
1173
  </Labeled>
1148
1174
  </ColumnChartContext>
@@ -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
+ });