@marimo-team/islands 0.23.10 → 0.23.11-dev1
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/{code-visibility-DrFozVlm.js → code-visibility-BDDI_K4x.js} +1 -1
- package/dist/main.js +2 -2
- package/dist/{reveal-component-3YQBHWuD.js → reveal-component-BOEk3vwx.js} +1 -1
- package/package.json +1 -1
- package/src/components/editor/ai/ai-completion-editor.tsx +6 -5
- package/src/core/ai/__tests__/strip-wrapping-backticks.test.ts +133 -0
- package/src/core/ai/stream-completion-text.ts +48 -0
- package/src/core/ai/strip-wrapping-backticks.ts +88 -0
- package/src/core/codemirror/ai/request.ts +2 -14
|
@@ -35981,7 +35981,7 @@ ${d}`,
|
|
|
35981
35981
|
return Logger.warn("Failed to get version from mount config"), null;
|
|
35982
35982
|
}
|
|
35983
35983
|
}
|
|
35984
|
-
marimoVersionAtom = atom(getVersionFromMountConfig() || "0.23.
|
|
35984
|
+
marimoVersionAtom = atom(getVersionFromMountConfig() || "0.23.11-dev1");
|
|
35985
35985
|
showCodeInRunModeAtom = atom(true);
|
|
35986
35986
|
atom(null);
|
|
35987
35987
|
var import_compiler_runtime = require_compiler_runtime();
|
package/dist/main.js
CHANGED
|
@@ -26,7 +26,7 @@ import { $ as reducer, B as safeExtractSetUIElementMessageBuffers, Bn as CircleA
|
|
|
26
26
|
import { __tla as __tla_1 } from "./chunk-5FQGJX7Z-BbqSm5gU.js";
|
|
27
27
|
import { o as useSize, s as Root$1, u as createLucideIcon } from "./dist-Dk6PV_d3.js";
|
|
28
28
|
import { A as SquareFunction, C as DEFAULT_COLOR_SCHEME, D as SCALE_TYPE_DESCRIPTIONS, E as EMPTY_VALUE$1, O as TIME_UNIT_DESCRIPTIONS, S as DEFAULT_AGGREGATION, T as DEFAULT_TIME_UNIT, _ as AGGREGATION_TYPE_DESCRIPTIONS, a as AGGREGATION_FNS$1, b as COLOR_SCHEMES, c as COLOR_BY_FIELDS, d as NONE_VALUE, f as SELECTABLE_DATA_TYPES, g as TIME_UNITS, h as STRING_AGGREGATION_FNS, i as convertDataTypeToSelectable, j as ChartColumn, k as escapeFieldName, l as COMBINED_TIME_UNITS, m as SORT_TYPES, n as createSpecWithoutData, o as BIN_AGGREGATION, p as SINGLE_TIME_UNITS, r as isFieldSet, s as CHART_TYPES, t as augmentSpecWithData, u as ChartType, v as AGGREGATION_TYPE_ICON, w as DEFAULT_MAX_BINS_FACET, x as COUNT_FIELD, y as CHART_TYPE_ICON } from "./spec-X7FwLJni.js";
|
|
29
|
-
import { $ as contextAwarePanelOpen, $t as $fae977aafc393c5c$export$6b862160d295c8e, A as prettifyRowCount, At as SELECT_COLUMN_ID, B as DatePicker, Bt as TabsContent, C as downloadSizeLimitAtom, Ct as DelayMount, D as ErrorState, Dt as loadTableAndRawData, E as EmptyState, Et as getPageIndexForRow, F as ContextMenuSeparator, Ft as Maps, G as CommandEmpty, Gt as ChartLoadingState, H as Combobox, Ht as TabsTrigger, I as ContextMenuTrigger, It as dateToLocalISODate, J as CommandList, Jt as RenderTextWithLinks, K as CommandInput, Kt as LazyVegaEmbed, L as useInternalStateWithSync, Lt as dateToLocalISODateTime, M as ContextMenu, Mt as toFieldTypes, N as ContextMenuContent, Nt as getMimeValues, O as LoadingState, Ot as loadTableData, P as ContextMenuItem, Pt as isNullishFilter, Q as PANEL_TYPES, Qt as $fae977aafc393c5c$export$588937bcd60ade55, R as useSelectList, Rt as dateToLocalISOTime, S as Filenames, St as ColumnChartSpecModel, T as ColumnPreviewContainer, Tt as usePrevious$1, U as ComboboxItem, Ut as ChartErrorState, V as DateRangePicker, Vt as TabsList, W as Command, Wt as ChartInfoState, X as smartMatch, Xt as HtmlOutput, Y as CommandSeparator, Yt as Kbd, Z as ContextAwarePanelItem, Zt as EmotionCacheProvider, _ as ADD_PRINTING_CLASS, _t as NAMELESS_COLUMN_PREFIX, an as Ellipsis, at as Toggle, b as downloadHTMLAsImage, bt as renderCellValue, c as Slide, cn as ChevronsUpDown, d as RadioGroupItem, dn as ChevronsDownUp, dt as Table, en as TextWrap, et as contextAwarePanelOwner, f as JsonOutput, fn as ChevronLeft, ft as TableBody, g as InstallPackageButton, gt as TableRow, h as DataTable, ht as TableHeader, it as slotsController, j as getColumnCountForDisplay, jt as TOO_MANY_ROWS, k as prettifyRowColumnCount, kt as INDEX_COLUMN_NAME, l as Switch, ln as ChevronsRight, lt as Fill, m as OutputRenderer, mt as TableHead, n as marimoVersionAtom, nn as Funnel, nt as isCellAwareAtom, o as SLIDE_TYPE_OPTIONS_BY_VALUE, on as Download, p as OutputArea, pn as ArrowDownWideNarrow, pt as TableCell, q as CommandItem, qt as useOverflowDetection, r as showCodeInRunModeAtom, rn as EyeOff, rt as SlotNames, sn as Code, t as useNotebookCodeAvailable, tn as GripHorizontal, tt as contextAwarePanelType, u as RadioGroup, un as ChevronsLeft, ut as Provider$1, v as downloadBlob, vt as generateColumns, w as ColumnName, wt as useIntersectionObserver, x as Progress, xt as ColumnChartContext, y as downloadByURL, yt as inferFieldTypes, z as CompactChipRow, zt as Tabs, __tla as __tla_2 } from "./code-visibility-
|
|
29
|
+
import { $ as contextAwarePanelOpen, $t as $fae977aafc393c5c$export$6b862160d295c8e, A as prettifyRowCount, At as SELECT_COLUMN_ID, B as DatePicker, Bt as TabsContent, C as downloadSizeLimitAtom, Ct as DelayMount, D as ErrorState, Dt as loadTableAndRawData, E as EmptyState, Et as getPageIndexForRow, F as ContextMenuSeparator, Ft as Maps, G as CommandEmpty, Gt as ChartLoadingState, H as Combobox, Ht as TabsTrigger, I as ContextMenuTrigger, It as dateToLocalISODate, J as CommandList, Jt as RenderTextWithLinks, K as CommandInput, Kt as LazyVegaEmbed, L as useInternalStateWithSync, Lt as dateToLocalISODateTime, M as ContextMenu, Mt as toFieldTypes, N as ContextMenuContent, Nt as getMimeValues, O as LoadingState, Ot as loadTableData, P as ContextMenuItem, Pt as isNullishFilter, Q as PANEL_TYPES, Qt as $fae977aafc393c5c$export$588937bcd60ade55, R as useSelectList, Rt as dateToLocalISOTime, S as Filenames, St as ColumnChartSpecModel, T as ColumnPreviewContainer, Tt as usePrevious$1, U as ComboboxItem, Ut as ChartErrorState, V as DateRangePicker, Vt as TabsList, W as Command, Wt as ChartInfoState, X as smartMatch, Xt as HtmlOutput, Y as CommandSeparator, Yt as Kbd, Z as ContextAwarePanelItem, Zt as EmotionCacheProvider, _ as ADD_PRINTING_CLASS, _t as NAMELESS_COLUMN_PREFIX, an as Ellipsis, at as Toggle, b as downloadHTMLAsImage, bt as renderCellValue, c as Slide, cn as ChevronsUpDown, d as RadioGroupItem, dn as ChevronsDownUp, dt as Table, en as TextWrap, et as contextAwarePanelOwner, f as JsonOutput, fn as ChevronLeft, ft as TableBody, g as InstallPackageButton, gt as TableRow, h as DataTable, ht as TableHeader, it as slotsController, j as getColumnCountForDisplay, jt as TOO_MANY_ROWS, k as prettifyRowColumnCount, kt as INDEX_COLUMN_NAME, l as Switch, ln as ChevronsRight, lt as Fill, m as OutputRenderer, mt as TableHead, n as marimoVersionAtom, nn as Funnel, nt as isCellAwareAtom, o as SLIDE_TYPE_OPTIONS_BY_VALUE, on as Download, p as OutputArea, pn as ArrowDownWideNarrow, pt as TableCell, q as CommandItem, qt as useOverflowDetection, r as showCodeInRunModeAtom, rn as EyeOff, rt as SlotNames, sn as Code, t as useNotebookCodeAvailable, tn as GripHorizontal, tt as contextAwarePanelType, u as RadioGroup, un as ChevronsLeft, ut as Provider$1, v as downloadBlob, vt as generateColumns, w as ColumnName, wt as useIntersectionObserver, x as Progress, xt as ColumnChartContext, y as downloadByURL, yt as inferFieldTypes, z as CompactChipRow, zt as Tabs, __tla as __tla_2 } from "./code-visibility-BDDI_K4x.js";
|
|
30
30
|
import { c as Calendar, i as createReducerAndAtoms, n as useOnUnmount, o as ToggleLeft, t as useOnMount } from "./useLifecycle-00mO3OSS.js";
|
|
31
31
|
import { t as Check } from "./check-BCaJeT-J.js";
|
|
32
32
|
import { A as Icon, C as logNever, D as $18f2051aff69b9bf$export$a54013f0d02a8f82, E as $18f2051aff69b9bf$export$43bb16f9c6d9e3f7, F as createCollection, I as X, M as clamp$2, N as usePrevious$2, P as useDirection, R as ChevronDown, S as assertNever, a as SelectGroup, c as SelectSeparator, d as NativeSelect, i as SelectContent, j as Trigger$1, l as SelectTrigger, n as capitalize, o as SelectItem, r as Select, s as SelectLabel, t as Strings, u as SelectValue, w as $a916eb452884faea$export$b7a616150fdb9f44 } from "./strings-J57tzLr3.js";
|
|
@@ -36187,7 +36187,7 @@ ${c}
|
|
|
36187
36187
|
function _temp2$2(e) {
|
|
36188
36188
|
e.target === e.currentTarget && e.key === "Enter" && (e.preventDefault(), e.stopPropagation(), e.currentTarget.click());
|
|
36189
36189
|
}
|
|
36190
|
-
var LazySlidesComponent = import_react.lazy(() => import("./reveal-component-
|
|
36190
|
+
var LazySlidesComponent = import_react.lazy(() => import("./reveal-component-BOEk3vwx.js"));
|
|
36191
36191
|
const SlidesLayoutRenderer = ({ layout: e, setLayout: r, cells: c, mode: l }) => {
|
|
36192
36192
|
var _a3;
|
|
36193
36193
|
let u = useAtomValue(kioskModeAtom), d = l === "read" || u, f = useAtomValue(numColumnsAtom) > 1, [p, m] = (0, import_react.useState)(null), { slideCells: h, skippedIds: g, noOutputIds: _, slideTypes: v, startCellIndex: y } = (0, import_react.useMemo)(() => computeSlideCellsInfo(c, e), [
|
|
@@ -9,7 +9,7 @@ import { t as require_compiler_runtime } from "./compiler-runtime-CEbnTgxf.js";
|
|
|
9
9
|
import { lt as kioskModeAtom } from "./html-to-image-BwfcJJxA.js";
|
|
10
10
|
import "./chunk-5FQGJX7Z-BbqSm5gU.js";
|
|
11
11
|
import { u as createLucideIcon } from "./dist-Dk6PV_d3.js";
|
|
12
|
-
import { a as DEFAULT_SLIDE_TYPE, c as Slide, ct as PanelResizeHandle, i as DEFAULT_DECK_TRANSITION, in as Expand, ot as Panel, rn as EyeOff, s as SlideSidebar, sn as Code, st as PanelGroup, t as useNotebookCodeAvailable } from "./code-visibility-
|
|
12
|
+
import { a as DEFAULT_SLIDE_TYPE, c as Slide, ct as PanelResizeHandle, i as DEFAULT_DECK_TRANSITION, in as Expand, ot as Panel, rn as EyeOff, s as SlideSidebar, sn as Code, st as PanelGroup, t as useNotebookCodeAvailable } from "./code-visibility-BDDI_K4x.js";
|
|
13
13
|
import { X as useDebouncedCallback } from "./input-OdWHkobi.js";
|
|
14
14
|
import "./toDate-d8RCRrRd.js";
|
|
15
15
|
import "./react-dom-BTJzcVJ9.js";
|
package/package.json
CHANGED
|
@@ -27,7 +27,7 @@ import { Label } from "@/components/ui/label";
|
|
|
27
27
|
import { Switch } from "@/components/ui/switch";
|
|
28
28
|
import { Tooltip } from "@/components/ui/tooltip";
|
|
29
29
|
import { toast } from "@/components/ui/use-toast";
|
|
30
|
-
import {
|
|
30
|
+
import { stripWrappingBackticks } from "@/core/ai/strip-wrapping-backticks";
|
|
31
31
|
import { type AiCompletionCell, includeOtherCellsAtom } from "@/core/ai/state";
|
|
32
32
|
import type { CellId } from "@/core/cells/ids";
|
|
33
33
|
import { getCodes } from "@/core/codemirror/copilot/getCodes";
|
|
@@ -45,6 +45,7 @@ import {
|
|
|
45
45
|
RejectCompletionButton,
|
|
46
46
|
} from "./completion-handlers";
|
|
47
47
|
import { addContextCompletion, getAICompletionBody } from "./completion-utils";
|
|
48
|
+
import { stagedAICellsAtom } from "@/core/ai/staged-cells";
|
|
48
49
|
|
|
49
50
|
const Original = CodeMirrorMerge.Original;
|
|
50
51
|
const Modified = CodeMirrorMerge.Modified;
|
|
@@ -123,7 +124,6 @@ export const AiCompletionEditor: React.FC<Props> = ({
|
|
|
123
124
|
api: runtimeManager.getAiURL("completion").toString(),
|
|
124
125
|
headers: runtimeManager.headers(),
|
|
125
126
|
initialInput: initialPrompt,
|
|
126
|
-
streamProtocol: "text",
|
|
127
127
|
// Throttle the messages and data updates to 100ms
|
|
128
128
|
experimental_throttle: 100,
|
|
129
129
|
body: {
|
|
@@ -143,13 +143,14 @@ export const AiCompletionEditor: React.FC<Props> = ({
|
|
|
143
143
|
});
|
|
144
144
|
},
|
|
145
145
|
onFinish: (_prompt, completion) => {
|
|
146
|
-
|
|
147
|
-
setCompletion(completion.trimEnd());
|
|
146
|
+
setCompletion(stripWrappingBackticks(completion).trimEnd());
|
|
148
147
|
},
|
|
149
148
|
});
|
|
150
149
|
|
|
151
150
|
const inputRef = React.useRef<ReactCodeMirrorRef>(null);
|
|
152
|
-
const completion = untrimmedCompletion
|
|
151
|
+
const completion = stripWrappingBackticks(untrimmedCompletion, {
|
|
152
|
+
streaming: isLoading,
|
|
153
|
+
}).trimEnd();
|
|
153
154
|
|
|
154
155
|
const initialSubmit = useCallback(() => {
|
|
155
156
|
if (triggerImmediately && !isLoading && initialPrompt) {
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/* Copyright 2026 Marimo. All rights reserved. */
|
|
2
|
+
|
|
3
|
+
import { describe, expect, it } from "vitest";
|
|
4
|
+
import { stripWrappingBackticks } from "../strip-wrapping-backticks";
|
|
5
|
+
|
|
6
|
+
// Cases aligned with `without_wrapping_backticks` on a complete string (single chunk).
|
|
7
|
+
const CASES: [string[], string][] = [
|
|
8
|
+
[["Hello", " world", "!"], "Hello world!"],
|
|
9
|
+
[["```", "print('hello')", "print('world')"], "print('hello')print('world')"],
|
|
10
|
+
[
|
|
11
|
+
["print('hello')", "print('world')", "```"],
|
|
12
|
+
"print('hello')print('world')```",
|
|
13
|
+
],
|
|
14
|
+
[["```", "print('hello')", "```"], "print('hello')"],
|
|
15
|
+
[["Hello", " ``` ", "world"], "Hello ``` world"],
|
|
16
|
+
[["``", "`print('hello')", "``", "`"], "print('hello')"],
|
|
17
|
+
[["``", "`", "\n", "print('hello')", "\n", "``", "`"], "print('hello')"],
|
|
18
|
+
[
|
|
19
|
+
["```\n", "print('hello')", "print('world')", "\n```"],
|
|
20
|
+
"print('hello')print('world')",
|
|
21
|
+
],
|
|
22
|
+
[
|
|
23
|
+
["```\nprint('hello')\n", "print('world')\n```"],
|
|
24
|
+
"print('hello')\nprint('world')",
|
|
25
|
+
],
|
|
26
|
+
[
|
|
27
|
+
["```\n", "def test():\n ", "return True\n```"],
|
|
28
|
+
"def test():\n return True",
|
|
29
|
+
],
|
|
30
|
+
[[], ""],
|
|
31
|
+
[["```idk```"], "idk"],
|
|
32
|
+
[["Hello world"], "Hello world"],
|
|
33
|
+
[
|
|
34
|
+
["```python\n", "def hello():\n ", "print('world')\n```"],
|
|
35
|
+
"def hello():\n print('world')",
|
|
36
|
+
],
|
|
37
|
+
[
|
|
38
|
+
["```python", "\ndef hello():\n ", "print('world')\n```"],
|
|
39
|
+
"def hello():\n print('world')",
|
|
40
|
+
],
|
|
41
|
+
[
|
|
42
|
+
["```sql", "SELECT * FROM table", " WHERE id = 1", "```"],
|
|
43
|
+
"SELECT * FROM table WHERE id = 1",
|
|
44
|
+
],
|
|
45
|
+
[
|
|
46
|
+
["```sql\n", "SELECT * FROM table\n", "WHERE id = 1\n```"],
|
|
47
|
+
"SELECT * FROM table\nWHERE id = 1",
|
|
48
|
+
],
|
|
49
|
+
[["```", "print('hello')", "``` "], "print('hello') "],
|
|
50
|
+
[["```python\n", "print('hello')", "\n``` "], "print('hello') "],
|
|
51
|
+
[["```", "code", "```\t\n"], "code\t\n"],
|
|
52
|
+
[[" ```", "code", "```"], "code"],
|
|
53
|
+
[[" ```python\n", "code", "```"], "code"],
|
|
54
|
+
[["\t```", "code", "```"], "code"],
|
|
55
|
+
[["\n", "\n", "```\n", "code", "\n```\n"], "code\n"],
|
|
56
|
+
[["\n", "\n", "```python\n", "code", "\n```\n"], "code\n"],
|
|
57
|
+
[["\n``", "`python\n", "code", "\n```\n"], "code\n"],
|
|
58
|
+
[["\n`", "`", "`python\n", "code", "\n```\n"], "code\n"],
|
|
59
|
+
[["```python ", "code", "```"], " code"],
|
|
60
|
+
[["```python\t", "code", "```"], "\tcode"],
|
|
61
|
+
[["```\n", "```"], ""],
|
|
62
|
+
[["```python\n", "```"], ""],
|
|
63
|
+
[["```\n", "code"], "code"],
|
|
64
|
+
[["```python\n", "code"], "code"],
|
|
65
|
+
[["code", "\n```"], "code\n```"],
|
|
66
|
+
[
|
|
67
|
+
["```\n", "x = 1\n", "```\n", "```\n", "y = 2\n", "```"],
|
|
68
|
+
"x = 1\n```\n```\ny = 2",
|
|
69
|
+
],
|
|
70
|
+
[["```python\n", "s = 'use `backticks`'\n", "```"], "s = 'use `backticks`'"],
|
|
71
|
+
[["``", "`\n", "code\n", "``", "`"], "code"],
|
|
72
|
+
[["```", "python\n", "code\n", "```"], "code"],
|
|
73
|
+
[["```", "code", "```"], "code"],
|
|
74
|
+
[["prefix ", "```\n", "code\n", "```"], "prefix ```\ncode\n```"],
|
|
75
|
+
[["```\n", "code\n", "```", " suffix"], "code\n``` suffix"],
|
|
76
|
+
[["```\n\n", "code\n\n", "```"], "\ncode\n"],
|
|
77
|
+
[["```\n", " code \n", "```"], " code "],
|
|
78
|
+
[["```\n", "\tcode\t\n", "```"], "\tcode\t"],
|
|
79
|
+
[
|
|
80
|
+
["```\n", "x\n", "```\n", "```python\n", "y\n", "```"],
|
|
81
|
+
"x\n```\n```python\ny",
|
|
82
|
+
],
|
|
83
|
+
[["```javascript\n", "console.log()\n", "```"], "javascript\nconsole.log()"],
|
|
84
|
+
[["```markdown\n", "# Title\n", "```"], "# Title"],
|
|
85
|
+
];
|
|
86
|
+
|
|
87
|
+
describe("stripWrappingBackticks", () => {
|
|
88
|
+
it.each(CASES)("strips fences for %j", (chunks, expected) => {
|
|
89
|
+
expect(stripWrappingBackticks(chunks.join(""))).toBe(expected);
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
// In streaming mode, the opening fence is stripped as soon as it is
|
|
94
|
+
// unambiguous, but the closing fence is left untouched (it may not have
|
|
95
|
+
// arrived yet, or a trailing "```" may be content).
|
|
96
|
+
const STREAMING_CASES: [string, string][] = [
|
|
97
|
+
// No fence: passthrough.
|
|
98
|
+
["import pandas as pd", "import pandas as pd"],
|
|
99
|
+
// Plain opening fence stripped once the first line is terminated.
|
|
100
|
+
["```\n", ""],
|
|
101
|
+
["```\ncode", "code"],
|
|
102
|
+
["```\ncode\nmore", "code\nmore"],
|
|
103
|
+
// Opening fence stripped, closing fence intentionally kept while streaming.
|
|
104
|
+
["```\ncode\n```", "code\n```"],
|
|
105
|
+
["```python\ncode\n```", "code\n```"],
|
|
106
|
+
// Language fences stripped as soon as the full identifier is present.
|
|
107
|
+
["```python", ""],
|
|
108
|
+
["```python\n", ""],
|
|
109
|
+
["```python\ncode", "code"],
|
|
110
|
+
["```sql\nSELECT 1", "SELECT 1"],
|
|
111
|
+
["```markdown\n# Title", "# Title"],
|
|
112
|
+
// Leading whitespace before the fence.
|
|
113
|
+
[" ```python\ncode", "code"],
|
|
114
|
+
// Partial language identifiers are left intact until they resolve.
|
|
115
|
+
["```", "```"],
|
|
116
|
+
["```p", "```p"],
|
|
117
|
+
["```py", "```py"],
|
|
118
|
+
["```pyth", "```pyth"],
|
|
119
|
+
["```s", "```s"],
|
|
120
|
+
["```mark", "```mark"],
|
|
121
|
+
// First-line content that is not a known language prefix is stripped as a
|
|
122
|
+
// plain fence even before a newline arrives.
|
|
123
|
+
["```x", "x"],
|
|
124
|
+
["```x = 1", "x = 1"],
|
|
125
|
+
// Unsupported language identifiers are kept as content (matches final mode).
|
|
126
|
+
["```json\ncode", "json\ncode"],
|
|
127
|
+
];
|
|
128
|
+
|
|
129
|
+
describe("stripWrappingBackticks (streaming)", () => {
|
|
130
|
+
it.each(STREAMING_CASES)("strips opening fence for %j", (text, expected) => {
|
|
131
|
+
expect(stripWrappingBackticks(text, { streaming: true })).toBe(expected);
|
|
132
|
+
});
|
|
133
|
+
});
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/* Copyright 2026 Marimo. All rights reserved. */
|
|
2
|
+
|
|
3
|
+
import { consumeStream, parseJsonEventStream, uiMessageChunkSchema } from "ai";
|
|
4
|
+
import { stripWrappingBackticks } from "./strip-wrapping-backticks";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Read an AI SDK UI message stream response and return the assistant text.
|
|
8
|
+
*/
|
|
9
|
+
export async function streamCompletionText(
|
|
10
|
+
response: Response,
|
|
11
|
+
): Promise<string> {
|
|
12
|
+
if (!response.ok) {
|
|
13
|
+
throw new Error(await response.text());
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (!response.body) {
|
|
17
|
+
throw new Error("Failed to get response body");
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
let result = "";
|
|
21
|
+
|
|
22
|
+
await consumeStream({
|
|
23
|
+
stream: parseJsonEventStream({
|
|
24
|
+
stream: response.body,
|
|
25
|
+
schema: uiMessageChunkSchema,
|
|
26
|
+
}).pipeThrough(
|
|
27
|
+
new TransformStream({
|
|
28
|
+
transform(part) {
|
|
29
|
+
if (!part.success) {
|
|
30
|
+
throw part.error;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const streamPart = part.value;
|
|
34
|
+
if (streamPart.type === "text-delta") {
|
|
35
|
+
result += streamPart.delta;
|
|
36
|
+
} else if (streamPart.type === "error") {
|
|
37
|
+
throw new Error(streamPart.errorText);
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
}),
|
|
41
|
+
),
|
|
42
|
+
onError: (error) => {
|
|
43
|
+
throw error;
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
return stripWrappingBackticks(result);
|
|
48
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/* Copyright 2026 Marimo. All rights reserved. */
|
|
2
|
+
|
|
3
|
+
const LANGS = ["python", "sql", "markdown"] as const;
|
|
4
|
+
|
|
5
|
+
interface StripOptions {
|
|
6
|
+
/**
|
|
7
|
+
* When true, the text is treated as a partial stream:
|
|
8
|
+
* - the opening fence is only stripped once we can be sure of its language
|
|
9
|
+
* identifier (e.g. "```py" is left untouched since it may still become
|
|
10
|
+
* "```python"), and
|
|
11
|
+
* - the closing fence is left in place, since it may not have arrived yet (or
|
|
12
|
+
* a trailing "```" may be part of the content).
|
|
13
|
+
*/
|
|
14
|
+
streaming?: boolean;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Removes wrapping markdown code fences from a completion string.
|
|
19
|
+
*/
|
|
20
|
+
export function stripWrappingBackticks(
|
|
21
|
+
text: string,
|
|
22
|
+
opts: StripOptions = {},
|
|
23
|
+
): string {
|
|
24
|
+
const { streaming = false } = opts;
|
|
25
|
+
const leadingWhitespace = text.match(/^\s*/)?.[0] ?? "";
|
|
26
|
+
const rest = text.slice(leadingWhitespace.length);
|
|
27
|
+
|
|
28
|
+
let body: string | null = null;
|
|
29
|
+
let strippedOpening = false;
|
|
30
|
+
|
|
31
|
+
for (const lang of LANGS) {
|
|
32
|
+
if (rest.startsWith(`\`\`\`${lang}`)) {
|
|
33
|
+
strippedOpening = true;
|
|
34
|
+
body = rest.slice(3 + lang.length);
|
|
35
|
+
if (body.startsWith("\n")) {
|
|
36
|
+
body = body.slice(1);
|
|
37
|
+
}
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (!strippedOpening && rest.startsWith("```")) {
|
|
43
|
+
const afterFence = rest.slice(3);
|
|
44
|
+
if (streaming && isPartialLanguageFence(afterFence)) {
|
|
45
|
+
return text;
|
|
46
|
+
}
|
|
47
|
+
strippedOpening = true;
|
|
48
|
+
body = afterFence;
|
|
49
|
+
if (body.startsWith("\n")) {
|
|
50
|
+
body = body.slice(1);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (!strippedOpening || body === null) {
|
|
55
|
+
return text;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// While streaming, the closing fence may not have arrived yet, so leave the
|
|
59
|
+
// body as-is once the opening fence is removed.
|
|
60
|
+
if (streaming) {
|
|
61
|
+
return body;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const strippedEnd = body.trimEnd();
|
|
65
|
+
const trailingSpace = body.slice(strippedEnd.length);
|
|
66
|
+
|
|
67
|
+
if (strippedEnd.endsWith("\n```")) {
|
|
68
|
+
return strippedEnd.slice(0, -4) + trailingSpace;
|
|
69
|
+
}
|
|
70
|
+
if (strippedEnd.endsWith("```")) {
|
|
71
|
+
return strippedEnd.slice(0, -3) + trailingSpace;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return body;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Returns true if the text after an opening "```" has no terminating newline
|
|
79
|
+
* yet and could still become a known language identifier.
|
|
80
|
+
*/
|
|
81
|
+
function isPartialLanguageFence(afterFence: string): boolean {
|
|
82
|
+
// Once the first line is terminated, we already know it is not a known
|
|
83
|
+
// language fence (those are handled above), so treat it as a plain fence.
|
|
84
|
+
if (afterFence.includes("\n")) {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
return LANGS.some((lang) => lang.startsWith(afterFence));
|
|
88
|
+
}
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
import { waitForConnectionOpen } from "@/core/network/connection";
|
|
4
4
|
import type { AiCompletionRequest } from "@/core/network/types";
|
|
5
|
+
import { streamCompletionText } from "@/core/ai/stream-completion-text";
|
|
5
6
|
import { getRuntimeManager } from "@/core/runtime/config";
|
|
6
7
|
import type { LanguageAdapterType } from "../language/types";
|
|
7
8
|
|
|
@@ -47,20 +48,7 @@ ${opts.codeAfter}
|
|
|
47
48
|
|
|
48
49
|
const firstLineIndent = opts.selection.match(/^\s*/)?.[0] || "";
|
|
49
50
|
|
|
50
|
-
|
|
51
|
-
if (!reader) {
|
|
52
|
-
throw new Error("Failed to get response reader");
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
let result = "";
|
|
56
|
-
// oxlint-disable-next-line no-constant-condition
|
|
57
|
-
while (true) {
|
|
58
|
-
const { done, value } = await reader.read();
|
|
59
|
-
if (done) {
|
|
60
|
-
break;
|
|
61
|
-
}
|
|
62
|
-
result += new TextDecoder().decode(value);
|
|
63
|
-
}
|
|
51
|
+
let result = await streamCompletionText(response);
|
|
64
52
|
|
|
65
53
|
// Add back the indent if it was stripped, which can happen with
|
|
66
54
|
// LLM responses
|