@marimo-team/islands 0.26.1-dev108 → 0.26.1-dev118
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/{ErrorBoundary-BW4TciqU.js → ErrorBoundary-Gr2Kt5kj.js} +1 -1
- package/dist/{chat-ui-Be9X-W22.js → chat-ui-CMKYvKUz.js} +3 -3
- package/dist/{code-visibility-BKmktPMd.js → code-visibility-BFFe0rT7.js} +2 -2
- package/dist/{common-CSJAjJGa.js → common-7eNIhSN-.js} +4766 -4747
- package/dist/{data-grid-overlay-editor-DTibdR4H.js → data-grid-overlay-editor-B37-1-8n.js} +1 -1
- package/dist/{error-entries-23Z_1oDM.js → error-entries-B4LgfizR.js} +772 -751
- package/dist/{feedback-button-CPu5SV3b.js → feedback-button-CpQcgVuz.js} +3 -3
- package/dist/{formats-Y52bg800.js → formats-CeuXJ_1f.js} +32 -25
- package/dist/{glide-data-editor-CC9_NDNi.js → glide-data-editor-BxTlDHV5.js} +3 -3
- package/dist/{globals-BFlYTNoR.js → globals-ljOLdTJr.js} +1 -1
- package/dist/{html-to-image-Cw7o1IbE.js → html-to-image-CIkwg7mv.js} +1 -1
- package/dist/main.js +69 -66
- package/dist/{number-overlay-editor-qDcRKPE0.js → number-overlay-editor-DGKTstav.js} +1 -1
- package/dist/{process-output-DdBM0lmd.js → process-output-PS9YSLNu.js} +2 -2
- package/dist/{reveal-component-BE1xhtNc.js → reveal-component-BLJMkx2N.js} +2 -2
- package/dist/{useDeepCompareMemoize-Dn-ZJVTd.js → semaphore-8KhQouXt.js} +5 -15
- package/dist/style.css +1 -1
- package/dist/{vega-component-BIq0qzhQ.js → vega-component-CoJiBCaS.js} +11 -11
- package/package.json +1 -1
- package/src/__mocks__/executions.ts +1 -1
- package/src/__mocks__/intersection-observer.ts +5 -5
- package/src/components/editor/Output.tsx +3 -21
- package/src/components/editor/__tests__/cell-renders.test.tsx +327 -1
- package/src/components/editor/actions/__tests__/pair-with-agent-modal.test.tsx +27 -0
- package/src/components/editor/actions/pair-with-agent-modal.tsx +8 -1
- package/src/components/editor/ai/__tests__/completion-utils.test.ts +12 -20
- package/src/components/editor/ai/ai-completion-editor.tsx +391 -289
- package/src/components/editor/ai/completion-utils.ts +9 -7
- package/src/components/editor/cell/CellDeleteShoulder.tsx +82 -0
- package/src/components/editor/cell/DeleteButton.tsx +11 -2
- package/src/components/editor/cell/__tests__/CellDeleteShoulder.test.tsx +181 -0
- package/src/components/editor/cell/code/cell-editor.tsx +15 -25
- package/src/components/editor/chrome/panels/__tests__/logs-panel.test.tsx +26 -2
- package/src/components/editor/chrome/panels/logs-panel.tsx +27 -11
- package/src/components/editor/notebook-cell.tsx +20 -40
- package/src/components/editor/output/VegaOutput.tsx +35 -0
- package/src/components/find-replace/find-replace.tsx +1 -7
- package/src/core/ai/state.ts +6 -0
- package/src/core/cells/__tests__/cell-errors.test.tsx +179 -0
- package/src/core/cells/__tests__/cells.test.ts +34 -20
- package/src/core/cells/cell-log-chunks.ts +31 -0
- package/src/core/cells/cells.ts +34 -33
- package/src/core/codemirror/find-replace/__tests__/navigate.test.ts +18 -15
- package/src/core/codemirror/find-replace/__tests__/search-highlight.test.ts +90 -2
- package/src/core/codemirror/find-replace/__tests__/unmounted-cells.test.ts +44 -10
- package/src/core/codemirror/find-replace/navigate.ts +4 -1
- package/src/core/codemirror/find-replace/search-highlight.ts +54 -58
- package/src/core/codemirror/lsp/__tests__/log-messages.test.ts +1 -1
- package/src/core/codemirror/lsp/__tests__/transports.test.ts +1 -1
- package/src/core/executions/__tests__/atoms.test.ts +0 -2
- package/src/core/executions/__tests__/output-lifecycle.test.ts +16 -0
- package/src/core/executions/atoms.ts +33 -36
- package/src/core/export/__tests__/output-snapshot.test.tsx +240 -0
- package/src/core/export/__tests__/output-subscription.test.tsx +79 -0
- package/src/core/export/hooks.ts +29 -13
- package/src/core/islands/__tests__/bridge.test.ts +2 -5
- package/src/core/notebook/replica/frame.ts +8 -2
- package/src/core/notebook/replica/index.ts +5 -1
- package/src/core/notebook/replica/once-per-frame.ts +4 -3
- package/src/core/notebook/replica/resources/executions.ts +16 -1
- package/src/core/session/__tests__/useNotebookSession.hook.test.tsx +10 -1
- package/src/css/common.css +11 -0
- package/src/hooks/useFinePointer.ts +30 -0
- package/src/stories/log-viewer.stories.tsx +48 -44
- package/src/utils/shared-intersection-observer.ts +2 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/* Copyright 2026 Marimo. All rights reserved. */
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { Chat } from "@ai-sdk/react";
|
|
4
4
|
import { EditorView } from "@codemirror/view";
|
|
5
5
|
import {
|
|
6
6
|
CircleCheckIcon,
|
|
@@ -8,7 +8,14 @@ import {
|
|
|
8
8
|
SparklesIcon,
|
|
9
9
|
XIcon,
|
|
10
10
|
} from "lucide-react";
|
|
11
|
-
import React, {
|
|
11
|
+
import React, {
|
|
12
|
+
useEffect,
|
|
13
|
+
useId,
|
|
14
|
+
useLayoutEffect,
|
|
15
|
+
useRef,
|
|
16
|
+
useState,
|
|
17
|
+
} from "react";
|
|
18
|
+
import useEvent from "react-use-event-hook";
|
|
12
19
|
import CodeMirrorMerge from "react-codemirror-merge";
|
|
13
20
|
import { DefaultChatTransport } from "ai";
|
|
14
21
|
import { Button } from "@/components/ui/button";
|
|
@@ -33,18 +40,18 @@ import {
|
|
|
33
40
|
cellCompletionSchema,
|
|
34
41
|
type CompletionUIMessage,
|
|
35
42
|
} from "@/core/ai/completion-output";
|
|
36
|
-
import { AI_SDK_UI_THROTTLE_MS } from "@/core/ai/constants";
|
|
37
43
|
import { type AiCompletionCell, includeOtherCellsAtom } from "@/core/ai/state";
|
|
38
44
|
import type { CellId } from "@/core/cells/ids";
|
|
39
45
|
import { getCodes } from "@/core/codemirror/copilot/getCodes";
|
|
40
46
|
import type { LanguageAdapterType } from "@/core/codemirror/language/types";
|
|
41
47
|
import { selectAllText } from "@/core/codemirror/utils";
|
|
42
48
|
import { useRuntimeManager } from "@/core/runtime/config";
|
|
49
|
+
import type { RuntimeManager } from "@/core/runtime/runtime";
|
|
43
50
|
import type { AiCompletionRequest } from "@/core/network/types";
|
|
44
51
|
import { useTheme } from "@/theme/useTheme";
|
|
45
52
|
import { cn } from "@/utils/cn";
|
|
46
53
|
import { prettyError } from "@/utils/errors";
|
|
47
|
-
import {
|
|
54
|
+
import { focusCell } from "../navigation/focus-utils";
|
|
48
55
|
import { PromptInput } from "./prompt-input";
|
|
49
56
|
import {
|
|
50
57
|
AcceptCompletionButton,
|
|
@@ -61,243 +68,358 @@ const Modified = CodeMirrorMerge.Modified;
|
|
|
61
68
|
interface Props {
|
|
62
69
|
cellId: CellId;
|
|
63
70
|
aiCompletionCell: AiCompletionCell | null;
|
|
64
|
-
className?: string;
|
|
65
71
|
currentCode: string;
|
|
66
72
|
currentLanguageAdapter: LanguageAdapterType | undefined;
|
|
67
73
|
onChange: (code: string) => void;
|
|
68
|
-
|
|
69
|
-
|
|
74
|
+
close: () => void;
|
|
75
|
+
focusEditor: () => void;
|
|
70
76
|
runCell: () => void;
|
|
71
77
|
outputArea?: "above" | "below";
|
|
72
|
-
/**
|
|
73
|
-
* Children shown when there is no completion
|
|
74
|
-
*/
|
|
75
78
|
children: React.ReactNode;
|
|
76
79
|
}
|
|
77
80
|
|
|
78
81
|
const baseExtensions = [customPythonLanguageSupport(), EditorView.lineWrapping];
|
|
79
82
|
|
|
80
|
-
/**
|
|
81
|
-
* Editor for AI completions that goes above a cell to modify it.
|
|
82
|
-
*
|
|
83
|
-
* This shows a left/right split with the original and modified code.
|
|
84
|
-
*/
|
|
85
83
|
export const AiCompletionEditor: React.FC<Props> = ({
|
|
86
84
|
cellId,
|
|
87
85
|
aiCompletionCell,
|
|
88
|
-
className,
|
|
89
|
-
onChange,
|
|
90
|
-
currentLanguageAdapter,
|
|
91
86
|
currentCode,
|
|
92
|
-
|
|
93
|
-
|
|
87
|
+
currentLanguageAdapter,
|
|
88
|
+
onChange,
|
|
89
|
+
close,
|
|
90
|
+
focusEditor,
|
|
94
91
|
runCell,
|
|
95
|
-
outputArea,
|
|
92
|
+
outputArea = "below",
|
|
96
93
|
children,
|
|
97
94
|
}) => {
|
|
98
|
-
const
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
includeOtherCellsAtom,
|
|
102
|
-
);
|
|
103
|
-
const includeOtherCellsCheckboxId = useId();
|
|
95
|
+
const stagedCell = useStagedAICell(cellId);
|
|
96
|
+
const previousCode =
|
|
97
|
+
stagedCell?.type === "update_cell" ? stagedCell.previousCode : undefined;
|
|
104
98
|
|
|
105
|
-
const
|
|
106
|
-
|
|
107
|
-
const
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
const
|
|
113
|
-
|
|
114
|
-
const updatedCell = useStagedAICell(cellId);
|
|
115
|
-
let previousCellCode: string | undefined;
|
|
116
|
-
if (updatedCell?.type === "update_cell") {
|
|
117
|
-
previousCellCode = updatedCell.previousCode;
|
|
118
|
-
}
|
|
99
|
+
const [completion, setCompletion] = useState<string | null>(null);
|
|
100
|
+
const hasCompletion = completion !== null;
|
|
101
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
102
|
+
const [showInputPrompt, setShowInputPrompt] = useState(false);
|
|
103
|
+
const request = useRef<Chat<CompletionUIMessage> | null>(null);
|
|
104
|
+
const chatId = useRef<string>(undefined);
|
|
105
|
+
const restoreFocus = useRef(false);
|
|
106
|
+
const inputRef = useRef<ReactCodeMirrorRef>(null);
|
|
119
107
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
status,
|
|
137
|
-
} = useChat<CompletionUIMessage>({
|
|
138
|
-
throttle: AI_SDK_UI_THROTTLE_MS,
|
|
139
|
-
transport,
|
|
140
|
-
onData: (part) => {
|
|
141
|
-
if (part.type === CELL_COMPLETION_DATA_TYPE) {
|
|
142
|
-
const completion = cellCompletionSchema.parse(part.data);
|
|
143
|
-
setCompletion(completion.code.trimEnd());
|
|
144
|
-
}
|
|
145
|
-
},
|
|
146
|
-
onError: (error) => {
|
|
147
|
-
setCompletion("");
|
|
148
|
-
toast({
|
|
149
|
-
title: "Completion failed",
|
|
150
|
-
description: prettyError(error),
|
|
151
|
-
});
|
|
152
|
-
},
|
|
153
|
-
onFinish: ({ isAbort, isDisconnect, isError, finishReason }) => {
|
|
154
|
-
if (isAbort || isDisconnect || isError || finishReason !== "stop") {
|
|
155
|
-
setCompletion("");
|
|
108
|
+
useEffect(() => {
|
|
109
|
+
return () => {
|
|
110
|
+
const chat = request.current;
|
|
111
|
+
request.current = null;
|
|
112
|
+
void chat?.stop();
|
|
113
|
+
};
|
|
114
|
+
}, []);
|
|
115
|
+
|
|
116
|
+
const complete = useEvent(
|
|
117
|
+
async (
|
|
118
|
+
prompt: string,
|
|
119
|
+
includeOtherCells: boolean,
|
|
120
|
+
runtime: RuntimeManager,
|
|
121
|
+
) => {
|
|
122
|
+
if (request.current) {
|
|
123
|
+
return;
|
|
156
124
|
}
|
|
157
|
-
},
|
|
158
|
-
});
|
|
159
|
-
const isLoading = status === "submitted" || status === "streaming";
|
|
160
|
-
|
|
161
|
-
const stop = useCallback(() => {
|
|
162
|
-
setCompletion("");
|
|
163
|
-
void stopChat();
|
|
164
|
-
}, [stopChat]);
|
|
165
|
-
|
|
166
|
-
const complete = useCallback(
|
|
167
|
-
async (prompt: string) => {
|
|
168
|
-
await stopChat();
|
|
169
|
-
setCompletion("");
|
|
170
125
|
|
|
171
126
|
const body = {
|
|
172
|
-
...getAICompletionBody({
|
|
127
|
+
...getAICompletionBody({
|
|
128
|
+
input: prompt,
|
|
129
|
+
includeOtherCode: includeOtherCells ? getCodes(currentCode) : "",
|
|
130
|
+
}),
|
|
173
131
|
prompt,
|
|
174
|
-
includeOtherCode: includeOtherCells ? getCodes(currentCode) : "",
|
|
175
132
|
code: currentCode,
|
|
176
133
|
language: currentLanguageAdapter ?? "python",
|
|
177
134
|
} satisfies AiCompletionRequest;
|
|
178
135
|
|
|
179
|
-
|
|
136
|
+
// A request can finish while another cell owns the prompt.
|
|
137
|
+
const chat = new Chat<CompletionUIMessage>({
|
|
138
|
+
id: chatId.current,
|
|
139
|
+
transport: new DefaultChatTransport({
|
|
140
|
+
api: runtime.getAiURL("completion").toString(),
|
|
141
|
+
headers: () => runtime.headers(),
|
|
142
|
+
prepareSendMessagesRequest: ({ body, id }) => ({
|
|
143
|
+
body: { ...body, id },
|
|
144
|
+
}),
|
|
145
|
+
}),
|
|
146
|
+
onData: (part) => {
|
|
147
|
+
if (
|
|
148
|
+
request.current === chat &&
|
|
149
|
+
part.type === CELL_COMPLETION_DATA_TYPE
|
|
150
|
+
) {
|
|
151
|
+
const completion = cellCompletionSchema.parse(part.data);
|
|
152
|
+
setCompletion(completion.code.trimEnd());
|
|
153
|
+
}
|
|
154
|
+
},
|
|
155
|
+
onError: (error) => {
|
|
156
|
+
if (request.current === chat) {
|
|
157
|
+
setCompletion(null);
|
|
158
|
+
toast({
|
|
159
|
+
title: "Completion failed",
|
|
160
|
+
description: prettyError(error),
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
},
|
|
164
|
+
onFinish: ({ isAbort, isDisconnect, isError, finishReason }) => {
|
|
165
|
+
if (
|
|
166
|
+
request.current === chat &&
|
|
167
|
+
(isAbort || isDisconnect || isError || finishReason !== "stop")
|
|
168
|
+
) {
|
|
169
|
+
setCompletion(null);
|
|
170
|
+
}
|
|
171
|
+
},
|
|
172
|
+
});
|
|
173
|
+
chatId.current = chat.id;
|
|
174
|
+
request.current = chat;
|
|
175
|
+
setCompletion(null);
|
|
176
|
+
setIsLoading(true);
|
|
177
|
+
|
|
178
|
+
await chat.sendMessage({ text: prompt }, { body }).finally(() => {
|
|
179
|
+
if (request.current === chat) {
|
|
180
|
+
request.current = null;
|
|
181
|
+
setIsLoading(false);
|
|
182
|
+
}
|
|
183
|
+
});
|
|
180
184
|
},
|
|
181
|
-
[
|
|
182
|
-
currentCode,
|
|
183
|
-
currentLanguageAdapter,
|
|
184
|
-
includeOtherCells,
|
|
185
|
-
sendMessage,
|
|
186
|
-
stopChat,
|
|
187
|
-
],
|
|
188
185
|
);
|
|
189
186
|
|
|
190
|
-
const
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
if (triggerImmediately && !isLoading && initialPrompt) {
|
|
198
|
-
// Use complete to pass the prompt directly, else input might be empty
|
|
199
|
-
void complete(initialPrompt);
|
|
200
|
-
}
|
|
201
|
-
// oxlint-disable-next-line react-hooks/exhaustive-deps
|
|
202
|
-
}, [triggerImmediately]);
|
|
187
|
+
const stop = () => {
|
|
188
|
+
const chat = request.current;
|
|
189
|
+
request.current = null;
|
|
190
|
+
setCompletion(null);
|
|
191
|
+
setIsLoading(false);
|
|
192
|
+
void chat?.stop();
|
|
193
|
+
};
|
|
203
194
|
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
() => {
|
|
209
|
-
const input = inputRef.current;
|
|
210
|
-
if (input?.view) {
|
|
211
|
-
input.view.focus();
|
|
212
|
-
initialSubmit();
|
|
213
|
-
return true;
|
|
214
|
-
}
|
|
215
|
-
return false;
|
|
216
|
-
},
|
|
217
|
-
{ retries: 3, delay: 100, initialDelay: 100 },
|
|
218
|
-
); // Wait for animation to complete
|
|
195
|
+
const closeAndFocusEditor = () => {
|
|
196
|
+
restoreFocus.current = true;
|
|
197
|
+
close();
|
|
198
|
+
};
|
|
219
199
|
|
|
220
|
-
|
|
200
|
+
// Wait until the prompt leaves the DOM to restore focus.
|
|
201
|
+
useLayoutEffect(() => {
|
|
202
|
+
if (!aiCompletionCell && restoreFocus.current) {
|
|
203
|
+
restoreFocus.current = false;
|
|
204
|
+
if (previousCode !== undefined) {
|
|
205
|
+
focusCell(cellId);
|
|
206
|
+
} else {
|
|
207
|
+
focusEditor();
|
|
208
|
+
}
|
|
221
209
|
}
|
|
222
|
-
}, [
|
|
210
|
+
}, [aiCompletionCell, cellId, focusEditor, previousCode]);
|
|
223
211
|
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
setInput(initialPrompt || "");
|
|
212
|
+
const handleAccept = () => {
|
|
213
|
+
if (!hasCompletion) {
|
|
214
|
+
return;
|
|
228
215
|
}
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
const handleAcceptCompletion = () => {
|
|
234
|
-
acceptChange(completion);
|
|
235
|
-
setCompletion("");
|
|
216
|
+
onChange(completion);
|
|
217
|
+
setCompletion(null);
|
|
218
|
+
closeAndFocusEditor();
|
|
236
219
|
};
|
|
237
220
|
|
|
238
|
-
|
|
239
|
-
|
|
221
|
+
const showInput = Boolean(
|
|
222
|
+
aiCompletionCell &&
|
|
223
|
+
(!aiCompletionCell.triggerImmediately || showInputPrompt),
|
|
224
|
+
);
|
|
225
|
+
const handleReject = () => {
|
|
240
226
|
stop();
|
|
241
227
|
setShowInputPrompt(true);
|
|
242
|
-
|
|
228
|
+
if (showInput) {
|
|
229
|
+
inputRef.current?.view?.focus();
|
|
230
|
+
}
|
|
243
231
|
};
|
|
244
232
|
|
|
245
|
-
const
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
const completionBanner = (
|
|
233
|
+
const comparison = renderCompletionComparison(
|
|
234
|
+
currentCode,
|
|
235
|
+
aiCompletionCell ? completion : null,
|
|
236
|
+
previousCode,
|
|
237
|
+
onChange,
|
|
238
|
+
);
|
|
239
|
+
const showBanner = hasCompletion || isLoading;
|
|
240
|
+
const completionBanner = aiCompletionCell?.triggerImmediately && (
|
|
253
241
|
<div
|
|
254
242
|
className={cn(
|
|
255
243
|
"w-full bg-(--cm-background) flex justify-center transition-all duration-300 ease-in-out overflow-hidden",
|
|
256
|
-
|
|
244
|
+
showBanner
|
|
257
245
|
? "max-h-20 opacity-100 translate-y-0"
|
|
258
246
|
: "max-h-0 opacity-0 -translate-y-2",
|
|
259
247
|
)}
|
|
260
248
|
>
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
249
|
+
{showBanner && (
|
|
250
|
+
<CompletionBanner
|
|
251
|
+
status={isLoading ? "loading" : "generated"}
|
|
252
|
+
onAccept={handleAccept}
|
|
253
|
+
onReject={handleReject}
|
|
254
|
+
showInputPrompt={showInputPrompt}
|
|
255
|
+
setShowInputPrompt={setShowInputPrompt}
|
|
256
|
+
runCell={runCell}
|
|
257
|
+
className="mt-4 mb-3 w-lg"
|
|
258
|
+
/>
|
|
259
|
+
)}
|
|
270
260
|
</div>
|
|
271
261
|
);
|
|
272
262
|
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
263
|
+
return (
|
|
264
|
+
<div
|
|
265
|
+
data-ai-input-open={showInput}
|
|
266
|
+
className="flex flex-col w-full rounded-[inherit]"
|
|
267
|
+
>
|
|
268
|
+
{aiCompletionCell && (
|
|
269
|
+
<AiCompletionPrompt
|
|
270
|
+
target={aiCompletionCell}
|
|
271
|
+
inputRef={inputRef}
|
|
272
|
+
showInput={showInput}
|
|
273
|
+
complete={complete}
|
|
274
|
+
hasCompletion={hasCompletion}
|
|
275
|
+
isLoading={isLoading}
|
|
276
|
+
stop={stop}
|
|
277
|
+
onAccept={handleAccept}
|
|
278
|
+
onReject={handleReject}
|
|
279
|
+
onClose={() => {
|
|
280
|
+
stop();
|
|
281
|
+
closeAndFocusEditor();
|
|
282
|
+
}}
|
|
283
|
+
runCell={runCell}
|
|
286
284
|
/>
|
|
287
|
-
|
|
285
|
+
)}
|
|
286
|
+
{outputArea === "above" && completionBanner}
|
|
287
|
+
{comparison}
|
|
288
|
+
<div hidden={comparison !== null}>{children}</div>
|
|
289
|
+
{outputArea === "below" && completionBanner}
|
|
290
|
+
</div>
|
|
291
|
+
);
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
function renderCompletionComparison(
|
|
295
|
+
currentCode: string,
|
|
296
|
+
proposal: string | null,
|
|
297
|
+
previousCode: string | undefined,
|
|
298
|
+
onChange: (code: string) => void,
|
|
299
|
+
) {
|
|
300
|
+
if (proposal !== null) {
|
|
301
|
+
return (
|
|
302
|
+
<CompletionComparison
|
|
303
|
+
originalCode={currentCode}
|
|
304
|
+
modifiedCode={proposal}
|
|
305
|
+
onChange={onChange}
|
|
306
|
+
/>
|
|
288
307
|
);
|
|
289
|
-
}
|
|
308
|
+
}
|
|
309
|
+
if (previousCode !== undefined) {
|
|
310
|
+
return (
|
|
311
|
+
<CompletionComparison
|
|
312
|
+
originalCode={previousCode}
|
|
313
|
+
modifiedCode={currentCode}
|
|
314
|
+
/>
|
|
315
|
+
);
|
|
316
|
+
}
|
|
317
|
+
return null;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
function CompletionComparison({
|
|
321
|
+
originalCode,
|
|
322
|
+
modifiedCode,
|
|
323
|
+
onChange,
|
|
324
|
+
}: {
|
|
325
|
+
originalCode: string;
|
|
326
|
+
modifiedCode: string;
|
|
327
|
+
onChange?: (code: string) => void;
|
|
328
|
+
}) {
|
|
329
|
+
const { theme } = useTheme();
|
|
330
|
+
return (
|
|
331
|
+
<CodeMirrorMerge className="cm" theme={theme}>
|
|
332
|
+
<Original
|
|
333
|
+
onChange={onChange}
|
|
334
|
+
editable={onChange !== undefined}
|
|
335
|
+
readOnly={onChange === undefined}
|
|
336
|
+
value={originalCode}
|
|
337
|
+
extensions={baseExtensions}
|
|
338
|
+
/>
|
|
339
|
+
<Modified
|
|
340
|
+
value={modifiedCode}
|
|
341
|
+
editable={false}
|
|
342
|
+
readOnly={true}
|
|
343
|
+
extensions={baseExtensions}
|
|
344
|
+
/>
|
|
345
|
+
</CodeMirrorMerge>
|
|
346
|
+
);
|
|
347
|
+
}
|
|
290
348
|
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
349
|
+
function AiCompletionPrompt({
|
|
350
|
+
target,
|
|
351
|
+
inputRef,
|
|
352
|
+
showInput,
|
|
353
|
+
complete,
|
|
354
|
+
hasCompletion,
|
|
355
|
+
isLoading,
|
|
356
|
+
stop,
|
|
357
|
+
onAccept: handleAcceptCompletion,
|
|
358
|
+
onReject: handleDeclineCompletion,
|
|
359
|
+
onClose,
|
|
360
|
+
runCell,
|
|
361
|
+
}: {
|
|
362
|
+
target: AiCompletionCell;
|
|
363
|
+
inputRef: React.RefObject<ReactCodeMirrorRef | null>;
|
|
364
|
+
showInput: boolean;
|
|
365
|
+
complete: (
|
|
366
|
+
prompt: string,
|
|
367
|
+
includeOtherCells: boolean,
|
|
368
|
+
runtime: RuntimeManager,
|
|
369
|
+
) => Promise<void>;
|
|
370
|
+
hasCompletion: boolean;
|
|
371
|
+
isLoading: boolean;
|
|
372
|
+
stop: () => void;
|
|
373
|
+
onAccept: () => void;
|
|
374
|
+
onReject: () => void;
|
|
375
|
+
onClose: () => void;
|
|
376
|
+
runCell: () => void;
|
|
377
|
+
}) {
|
|
378
|
+
const [includeOtherCells, setIncludeOtherCells] = useAtom(
|
|
379
|
+
includeOtherCellsAtom,
|
|
380
|
+
);
|
|
381
|
+
const includeOtherCellsCheckboxId = useId();
|
|
382
|
+
const runtime = useRuntimeManager();
|
|
383
|
+
const [draft, setDraft] = useState<{
|
|
384
|
+
target: AiCompletionCell;
|
|
385
|
+
text: string;
|
|
386
|
+
}>();
|
|
387
|
+
const input =
|
|
388
|
+
draft?.target === target ? draft.text : (target.initialPrompt ?? "");
|
|
389
|
+
const setInput = useEvent((text: string) => setDraft({ target, text }));
|
|
390
|
+
const submit = useEvent((prompt: string) => {
|
|
391
|
+
void complete(prompt, includeOtherCells, runtime);
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
useEffect(() => {
|
|
395
|
+
const { initialPrompt, triggerImmediately } = target;
|
|
396
|
+
if (triggerImmediately && initialPrompt) {
|
|
397
|
+
const timer = setTimeout(() => submit(initialPrompt), 100);
|
|
398
|
+
return () => clearTimeout(timer);
|
|
294
399
|
}
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
400
|
+
}, [target, submit]);
|
|
401
|
+
|
|
402
|
+
useEffect(() => {
|
|
403
|
+
const prompt = inputRef.current;
|
|
404
|
+
if (!showInput || prompt?.view?.hasFocus) {
|
|
405
|
+
return;
|
|
299
406
|
}
|
|
300
|
-
|
|
407
|
+
const timer = setTimeout(() => {
|
|
408
|
+
const view = inputRef.current?.view;
|
|
409
|
+
if (view && !view.hasFocus) {
|
|
410
|
+
view.focus();
|
|
411
|
+
selectAllText(view);
|
|
412
|
+
}
|
|
413
|
+
}, 100);
|
|
414
|
+
const cancel = () => clearTimeout(timer);
|
|
415
|
+
prompt?.editor?.addEventListener("focusin", cancel, { once: true });
|
|
416
|
+
return () => {
|
|
417
|
+
cancel();
|
|
418
|
+
prompt?.editor?.removeEventListener("focusin", cancel);
|
|
419
|
+
};
|
|
420
|
+
}, [target, showInput, inputRef]);
|
|
421
|
+
|
|
422
|
+
const handleSubmit = () => submit(input);
|
|
301
423
|
|
|
302
424
|
const completionButtons = (
|
|
303
425
|
<>
|
|
@@ -322,115 +444,95 @@ export const AiCompletionEditor: React.FC<Props> = ({
|
|
|
322
444
|
|
|
323
445
|
return (
|
|
324
446
|
<div
|
|
325
|
-
|
|
326
|
-
|
|
447
|
+
className={cn(
|
|
448
|
+
"flex items-center gap-2 px-3 transition-all rounded-[inherit] rounded-b-none duration-300",
|
|
449
|
+
showInput && "max-h-[400px] border-b min-h-11 visible",
|
|
450
|
+
!showInput && "max-h-0 min-h-0 invisible",
|
|
451
|
+
)}
|
|
327
452
|
>
|
|
328
|
-
<
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
{
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
<AIModelDropdown
|
|
377
|
-
triggerClassName="h-7 text-xs"
|
|
378
|
-
iconSize="small"
|
|
379
|
-
forRole="edit"
|
|
380
|
-
displayIconOnly={true}
|
|
381
|
-
placeholder="Edit model"
|
|
382
|
-
/>
|
|
383
|
-
</div>
|
|
384
|
-
{completion && (
|
|
385
|
-
<div className="mt-1 flex items-center gap-1">
|
|
386
|
-
{completionButtons}
|
|
387
|
-
</div>
|
|
388
|
-
)}
|
|
389
|
-
</div>
|
|
390
|
-
|
|
391
|
-
<div className="h-full w-px bg-border mx-2" />
|
|
392
|
-
<Tooltip content="Include code from other cells">
|
|
393
|
-
<div className="flex flex-row items-start gap-1 overflow-hidden">
|
|
394
|
-
<Checkbox
|
|
395
|
-
data-testid="include-other-cells-checkbox"
|
|
396
|
-
id={includeOtherCellsCheckboxId}
|
|
397
|
-
checked={includeOtherCells}
|
|
398
|
-
onCheckedChange={(checked) =>
|
|
399
|
-
setIncludeOtherCells(Boolean(checked))
|
|
400
|
-
}
|
|
401
|
-
/>
|
|
402
|
-
<Label
|
|
403
|
-
htmlFor={includeOtherCellsCheckboxId}
|
|
404
|
-
className="text-muted-foreground text-xs whitespace-nowrap ellipsis"
|
|
405
|
-
>
|
|
406
|
-
All code
|
|
407
|
-
</Label>
|
|
408
|
-
</div>
|
|
409
|
-
</Tooltip>
|
|
410
|
-
<Button
|
|
411
|
-
aria-label="Discard AI completion"
|
|
412
|
-
data-testid="decline-completion-button"
|
|
413
|
-
variant="text"
|
|
414
|
-
size="icon"
|
|
415
|
-
disabled={isLoading}
|
|
416
|
-
onClick={() => {
|
|
417
|
-
stop();
|
|
418
|
-
declineChange();
|
|
419
|
-
}}
|
|
420
|
-
>
|
|
421
|
-
<XIcon className="text-(--red-10)" size={16} />
|
|
422
|
-
</Button>
|
|
423
|
-
</>
|
|
453
|
+
<SparklesIcon className="text-(--blue-10) shrink-0" size={16} />
|
|
454
|
+
<PromptInput
|
|
455
|
+
inputRef={inputRef}
|
|
456
|
+
className="h-full my-0 py-2 flex items-center"
|
|
457
|
+
onClose={onClose}
|
|
458
|
+
value={input}
|
|
459
|
+
onChange={setInput}
|
|
460
|
+
onSubmit={() => {
|
|
461
|
+
if (!isLoading) {
|
|
462
|
+
if (inputRef.current?.view) {
|
|
463
|
+
storePrompt(inputRef.current.view);
|
|
464
|
+
}
|
|
465
|
+
handleSubmit();
|
|
466
|
+
}
|
|
467
|
+
}}
|
|
468
|
+
onKeyDown={createAiCompletionOnKeydown({
|
|
469
|
+
handleAcceptCompletion,
|
|
470
|
+
handleDeclineCompletion,
|
|
471
|
+
isLoading,
|
|
472
|
+
hasCompletion,
|
|
473
|
+
})}
|
|
474
|
+
/>
|
|
475
|
+
|
|
476
|
+
<div className="-mr-1.5 py-1.5">
|
|
477
|
+
<div className="flex flex-row items-center justify-end gap-0.5">
|
|
478
|
+
<SendButton
|
|
479
|
+
isLoading={isLoading}
|
|
480
|
+
onStop={stop}
|
|
481
|
+
onSendClick={handleSubmit}
|
|
482
|
+
isEmpty={!input.trim()}
|
|
483
|
+
showStopLabel={true}
|
|
484
|
+
/>
|
|
485
|
+
<AddContextButton
|
|
486
|
+
handleAddContext={() => addContextCompletion(inputRef)}
|
|
487
|
+
isLoading={isLoading}
|
|
488
|
+
/>
|
|
489
|
+
<AIModelDropdown
|
|
490
|
+
triggerClassName="h-7 text-xs"
|
|
491
|
+
iconSize="small"
|
|
492
|
+
forRole="edit"
|
|
493
|
+
displayIconOnly={true}
|
|
494
|
+
placeholder="Edit model"
|
|
495
|
+
/>
|
|
496
|
+
</div>
|
|
497
|
+
{hasCompletion && (
|
|
498
|
+
<div className="mt-1 flex items-center gap-1">
|
|
499
|
+
{completionButtons}
|
|
500
|
+
</div>
|
|
424
501
|
)}
|
|
425
502
|
</div>
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
503
|
+
|
|
504
|
+
<div className="h-full w-px bg-border mx-2" />
|
|
505
|
+
<Tooltip content="Include code from other cells">
|
|
506
|
+
<div className="flex flex-row items-start gap-1 overflow-hidden">
|
|
507
|
+
<Checkbox
|
|
508
|
+
data-testid="include-other-cells-checkbox"
|
|
509
|
+
id={includeOtherCellsCheckboxId}
|
|
510
|
+
checked={includeOtherCells}
|
|
511
|
+
onCheckedChange={(checked) =>
|
|
512
|
+
setIncludeOtherCells(Boolean(checked))
|
|
513
|
+
}
|
|
514
|
+
/>
|
|
515
|
+
<Label
|
|
516
|
+
htmlFor={includeOtherCellsCheckboxId}
|
|
517
|
+
className="text-muted-foreground text-xs whitespace-nowrap ellipsis"
|
|
518
|
+
>
|
|
519
|
+
All code
|
|
520
|
+
</Label>
|
|
521
|
+
</div>
|
|
522
|
+
</Tooltip>
|
|
523
|
+
<Button
|
|
524
|
+
aria-label="Discard AI completion"
|
|
525
|
+
data-testid="decline-completion-button"
|
|
526
|
+
variant="text"
|
|
527
|
+
size="icon"
|
|
528
|
+
disabled={isLoading}
|
|
529
|
+
onClick={onClose}
|
|
530
|
+
>
|
|
531
|
+
<XIcon className="text-(--red-10)" size={16} />
|
|
532
|
+
</Button>
|
|
431
533
|
</div>
|
|
432
534
|
);
|
|
433
|
-
}
|
|
535
|
+
}
|
|
434
536
|
|
|
435
537
|
interface CompletionBannerProps {
|
|
436
538
|
status: "loading" | "generated";
|