@backmatter/studio 0.0.20 → 0.0.21
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/chunks/{UiKitApp-C269OA8v.js → UiKitApp-CMMcsaX4.js} +2 -2
- package/dist/chunks/{WorkspaceRoutes-B4B5jjje.js → WorkspaceRoutes-DW0sLdpJ.js} +27141 -26241
- package/dist/chunks/dashboard-By0P4Yny.js +1879 -0
- package/dist/chunks/{index-7-TtXaMq.js → index-DTPUqaHE.js} +1 -1
- package/dist/dashboard.js +2 -2
- package/dist/index.js +205 -194
- package/dist/site-widget.css +208 -0
- package/dist/site-widget.js +10495 -9947
- package/dist/studio.css +276 -32
- package/package.json +2 -2
- package/src/App.tsx +7 -1
- package/src/api.ts +106 -13
- package/src/chat/ChatFiles.tsx +261 -0
- package/src/chat/ChatMessage.tsx +5 -0
- package/src/chat/ChatPanel.tsx +218 -14
- package/src/chat/ChatProvider.tsx +72 -22
- package/src/chat/Composer.tsx +83 -6
- package/src/chat/PendingFileRow.tsx +188 -0
- package/src/chat/chat-context.ts +7 -1
- package/src/chat/chat-helpers.ts +49 -0
- package/src/chat/chat-state.ts +17 -6
- package/src/chat/icons.tsx +28 -0
- package/src/chat/model-copy.ts +41 -0
- package/src/chat/pending-files.ts +236 -0
- package/src/chat/side-width.ts +48 -0
- package/src/chrome/AppShell.tsx +9 -1
- package/src/chrome/CommandPalette.tsx +7 -0
- package/src/chrome/ContentExplorerSidebar.tsx +43 -74
- package/src/chrome/folder-create-context.ts +56 -11
- package/src/chrome/hotkeys.ts +8 -0
- package/src/fields/FieldControl.tsx +331 -115
- package/src/fields/LinkFieldInput.tsx +13 -6
- package/src/fields/array-item-title.ts +28 -0
- package/src/fields/field-label.ts +8 -0
- package/src/host-pointer.ts +2 -0
- package/src/install-preview-pointer.ts +1 -0
- package/src/pages/BlockPicker.tsx +362 -0
- package/src/pages/BlocksList.tsx +39 -67
- package/src/pages/CliAuthorizePage.tsx +138 -25
- package/src/pages/DocEditorPage.tsx +95 -18
- package/src/pages/EmbedPublishPage.tsx +40 -2
- package/src/pages/LoginPage.tsx +118 -31
- package/src/pages/SettingsSchemaPage.tsx +19 -3
- package/src/pages/WorkspaceLayout.tsx +16 -1
- package/src/pages/block-picker.ts +116 -0
- package/src/pages/editor-grab.ts +61 -13
- package/src/pages/preview-split.ts +24 -0
- package/src/pages/session-preview.ts +1 -0
- package/src/replica/types.ts +2 -0
- package/src/sign-in-link.ts +45 -0
- package/src/site-pointer.ts +14 -1
- package/src/site-widget.tsx +12 -1
- package/src/ui/Field.tsx +26 -0
- package/src/workspace/schema-source.tsx +126 -0
- package/dist/chunks/dashboard-DeNgythd.js +0 -1779
- package/src/site-devbar-overlay-inject.ts +0 -15
- package/src/site-publish-menu.tsx +0 -33
package/src/chat/ChatPanel.tsx
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
// packages/studio/src/chat/ChatPanel.tsx
|
|
2
2
|
//
|
|
3
3
|
// Floating chat window anchored bottom-right above the ChatBar. Supports
|
|
4
|
-
// minimize (collapses into the bar), fullscreen,
|
|
4
|
+
// minimize (collapses into the bar), fullscreen, a column pinned to the right
|
|
5
|
+
// of the editor (when the shell gives a side host), and a history view.
|
|
5
6
|
import { useEffect, useLayoutEffect, useRef, useState } from "react";
|
|
7
|
+
import { createPortal } from "react-dom";
|
|
6
8
|
import * as stylex from "@stylexjs/stylex";
|
|
7
9
|
import { colors, radius, space, type, z } from "../styles/tokens.stylex";
|
|
8
10
|
import { Tooltip } from "../ui/Tooltip";
|
|
@@ -13,25 +15,58 @@ import { LiveStatus } from "./LiveStatus";
|
|
|
13
15
|
import { motion } from "./motion";
|
|
14
16
|
import { Shimmer } from "./Shimmer";
|
|
15
17
|
import { ThreadHistory } from "./ThreadHistory";
|
|
18
|
+
import { ThreadFilesButton } from "./ChatFiles";
|
|
19
|
+
import { chatErrorText } from "./chat-helpers";
|
|
16
20
|
import { useStickToBottom, TURN_PEEK, turnSpacer } from "./useStickToBottom";
|
|
17
21
|
import {
|
|
18
22
|
CloseIcon,
|
|
19
23
|
CollapseIcon,
|
|
24
|
+
DockRightIcon,
|
|
20
25
|
ExpandIcon,
|
|
26
|
+
FloatIcon,
|
|
21
27
|
HistoryIcon,
|
|
22
28
|
MinimizeIcon,
|
|
23
29
|
PlusIcon,
|
|
24
30
|
SparkIcon,
|
|
25
31
|
} from "./icons";
|
|
26
32
|
import { isPanelVisible } from "./chat-state";
|
|
33
|
+
import {
|
|
34
|
+
SIDE_WIDTH_DEFAULT,
|
|
35
|
+
clampSideWidth,
|
|
36
|
+
persistSideWidth,
|
|
37
|
+
readSideWidth,
|
|
38
|
+
sideWidthFromPointer,
|
|
39
|
+
} from "./side-width";
|
|
27
40
|
|
|
28
41
|
const hover = "@media (hover: hover)";
|
|
29
42
|
|
|
30
|
-
export function ChatPanel({
|
|
43
|
+
export function ChatPanel({
|
|
44
|
+
fill = false,
|
|
45
|
+
site = false,
|
|
46
|
+
sideHost = null,
|
|
47
|
+
}: {
|
|
48
|
+
fill?: boolean;
|
|
49
|
+
site?: boolean;
|
|
50
|
+
/** Element beside the editor to render into when the panel is pinned to the side. */
|
|
51
|
+
sideHost?: HTMLElement | null;
|
|
52
|
+
} = {}) {
|
|
31
53
|
const ui = useChatUI();
|
|
32
54
|
const session = useChatSession();
|
|
33
55
|
const visible = isPanelVisible(ui.panel.mode);
|
|
34
56
|
const fullscreen = ui.panel.mode === "fullscreen";
|
|
57
|
+
const canSide = !fill && !site && sideHost !== null;
|
|
58
|
+
const side = canSide && ui.panel.mode === "side";
|
|
59
|
+
const [sideWidth, setSideWidth] = useState(readSideWidth);
|
|
60
|
+
const [resizing, setResizing] = useState(false);
|
|
61
|
+
const [dragging, setDragging] = useState(false);
|
|
62
|
+
// dragenter and dragleave fire for every child the pointer crosses.
|
|
63
|
+
const dragDepth = useRef(0);
|
|
64
|
+
const viewportWidth = () => (typeof window === "undefined" ? 1440 : window.innerWidth);
|
|
65
|
+
const shownSideWidth = clampSideWidth(sideWidth, viewportWidth());
|
|
66
|
+
|
|
67
|
+
useEffect(() => {
|
|
68
|
+
if (!resizing) persistSideWidth(sideWidth);
|
|
69
|
+
}, [sideWidth, resizing]);
|
|
35
70
|
const docked = Boolean(ui.codeSessionId);
|
|
36
71
|
const threadId = session.thread?.id ?? null;
|
|
37
72
|
const knownIds = useRef<Set<string> | null>(null);
|
|
@@ -143,6 +178,37 @@ export function ChatPanel({ fill = false, site = false }: { fill?: boolean; site
|
|
|
143
178
|
|
|
144
179
|
if (!visible) return null;
|
|
145
180
|
|
|
181
|
+
const acceptsDrop = ui.panel.view === "chat" && ui.aiAvailable === true;
|
|
182
|
+
const carriesFiles = (e: React.DragEvent) => Array.from(e.dataTransfer?.types ?? []).includes("Files");
|
|
183
|
+
const dropHandlers = acceptsDrop
|
|
184
|
+
? {
|
|
185
|
+
onDragEnter: (e: React.DragEvent) => {
|
|
186
|
+
if (!carriesFiles(e)) return;
|
|
187
|
+
e.preventDefault();
|
|
188
|
+
dragDepth.current += 1;
|
|
189
|
+
setDragging(true);
|
|
190
|
+
},
|
|
191
|
+
onDragOver: (e: React.DragEvent) => {
|
|
192
|
+
if (!carriesFiles(e)) return;
|
|
193
|
+
e.preventDefault();
|
|
194
|
+
e.dataTransfer.dropEffect = "copy";
|
|
195
|
+
},
|
|
196
|
+
onDragLeave: (e: React.DragEvent) => {
|
|
197
|
+
if (!carriesFiles(e)) return;
|
|
198
|
+
dragDepth.current = Math.max(0, dragDepth.current - 1);
|
|
199
|
+
if (dragDepth.current === 0) setDragging(false);
|
|
200
|
+
},
|
|
201
|
+
onDrop: (e: React.DragEvent) => {
|
|
202
|
+
if (!carriesFiles(e)) return;
|
|
203
|
+
e.preventDefault();
|
|
204
|
+
dragDepth.current = 0;
|
|
205
|
+
setDragging(false);
|
|
206
|
+
const dropped = Array.from(e.dataTransfer.files ?? []);
|
|
207
|
+
if (dropped.length > 0) ui.files.add(dropped);
|
|
208
|
+
},
|
|
209
|
+
}
|
|
210
|
+
: {};
|
|
211
|
+
|
|
146
212
|
const title =
|
|
147
213
|
ui.panel.view === "history"
|
|
148
214
|
? "Chat history"
|
|
@@ -150,26 +216,88 @@ export function ChatPanel({ fill = false, site = false }: { fill?: boolean; site
|
|
|
150
216
|
const lastMessage = session.messages[session.messages.length - 1];
|
|
151
217
|
const liveParts = lastMessage?.role === "assistant" ? lastMessage.parts : undefined;
|
|
152
218
|
|
|
153
|
-
|
|
219
|
+
function resizeFromPointer(e: React.PointerEvent<HTMLDivElement>) {
|
|
220
|
+
const panelEl = e.currentTarget.parentElement;
|
|
221
|
+
if (!panelEl) return;
|
|
222
|
+
setSideWidth(
|
|
223
|
+
sideWidthFromPointer({
|
|
224
|
+
panelRight: panelEl.getBoundingClientRect().right,
|
|
225
|
+
clientX: e.clientX,
|
|
226
|
+
viewport: viewportWidth(),
|
|
227
|
+
}),
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function onResizeKeyDown(e: React.KeyboardEvent<HTMLDivElement>) {
|
|
232
|
+
const step = e.shiftKey ? 64 : 16;
|
|
233
|
+
// The handle is the panel's left edge: moving it left widens the panel.
|
|
234
|
+
const delta = e.key === "ArrowLeft" ? step : e.key === "ArrowRight" ? -step : 0;
|
|
235
|
+
if (delta === 0) return;
|
|
236
|
+
e.preventDefault();
|
|
237
|
+
setSideWidth((w) => clampSideWidth(clampSideWidth(w, viewportWidth()) + delta, viewportWidth()));
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const panelSx = stylex.props(
|
|
241
|
+
fill
|
|
242
|
+
? ui.codeSessionId
|
|
243
|
+
? styles.panelColumnFill
|
|
244
|
+
: styles.panelFill
|
|
245
|
+
: styles.panel,
|
|
246
|
+
site && styles.panelSite,
|
|
247
|
+
!fill && fullscreen && styles.panelFull,
|
|
248
|
+
side && styles.panelSide,
|
|
249
|
+
);
|
|
250
|
+
|
|
251
|
+
const panel = (
|
|
154
252
|
<section
|
|
155
|
-
role="dialog"
|
|
253
|
+
role={side ? "complementary" : "dialog"}
|
|
156
254
|
aria-label="AI chat"
|
|
157
255
|
data-backmatter-panel=""
|
|
158
|
-
{
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
? styles.panelColumnFill
|
|
162
|
-
: styles.panelFill
|
|
163
|
-
: styles.panel,
|
|
164
|
-
site && styles.panelSite,
|
|
165
|
-
!fill && fullscreen && styles.panelFull,
|
|
166
|
-
)}
|
|
256
|
+
className={panelSx.className}
|
|
257
|
+
style={side ? { ...panelSx.style, width: shownSideWidth } : panelSx.style}
|
|
258
|
+
{...dropHandlers}
|
|
167
259
|
>
|
|
260
|
+
{dragging && acceptsDrop ? (
|
|
261
|
+
<div aria-hidden="true" {...stylex.props(styles.dropOverlay)}>
|
|
262
|
+
<span {...stylex.props(styles.dropLabel)}>Drop files</span>
|
|
263
|
+
</div>
|
|
264
|
+
) : null}
|
|
265
|
+
{side ? (
|
|
266
|
+
<div
|
|
267
|
+
role="separator"
|
|
268
|
+
aria-orientation="vertical"
|
|
269
|
+
aria-label="Resize chat"
|
|
270
|
+
aria-valuenow={shownSideWidth}
|
|
271
|
+
tabIndex={0}
|
|
272
|
+
onPointerDown={(e) => {
|
|
273
|
+
if (e.button !== 0) return;
|
|
274
|
+
e.preventDefault();
|
|
275
|
+
e.currentTarget.focus();
|
|
276
|
+
e.currentTarget.setPointerCapture(e.pointerId);
|
|
277
|
+
setResizing(true);
|
|
278
|
+
}}
|
|
279
|
+
onPointerMove={(e) => {
|
|
280
|
+
if (e.currentTarget.hasPointerCapture(e.pointerId)) resizeFromPointer(e);
|
|
281
|
+
}}
|
|
282
|
+
onPointerUp={(e) => {
|
|
283
|
+
if (e.currentTarget.hasPointerCapture(e.pointerId)) {
|
|
284
|
+
e.currentTarget.releasePointerCapture(e.pointerId);
|
|
285
|
+
}
|
|
286
|
+
setResizing(false);
|
|
287
|
+
}}
|
|
288
|
+
onPointerCancel={() => setResizing(false)}
|
|
289
|
+
onLostPointerCapture={() => setResizing(false)}
|
|
290
|
+
onDoubleClick={() => setSideWidth(SIDE_WIDTH_DEFAULT)}
|
|
291
|
+
onKeyDown={onResizeKeyDown}
|
|
292
|
+
{...stylex.props(styles.resizeHandle, resizing && styles.resizeHandleActive)}
|
|
293
|
+
/>
|
|
294
|
+
) : null}
|
|
168
295
|
<header {...stylex.props(styles.header)}>
|
|
169
296
|
<h2 {...stylex.props(styles.title)}>{title}</h2>
|
|
170
297
|
<div {...stylex.props(styles.headerActions)}>
|
|
171
298
|
{ui.panel.view === "chat" ? (
|
|
172
299
|
<>
|
|
300
|
+
<ThreadFilesButton files={ui.threadFiles} />
|
|
173
301
|
<Tooltip label="New chat">
|
|
174
302
|
<button
|
|
175
303
|
type="button"
|
|
@@ -213,6 +341,19 @@ export function ChatPanel({ fill = false, site = false }: { fill?: boolean; site
|
|
|
213
341
|
<MinimizeIcon />
|
|
214
342
|
</button>
|
|
215
343
|
</Tooltip>
|
|
344
|
+
{canSide ? (
|
|
345
|
+
<Tooltip label={side ? "Float window" : "Pin to the right"}>
|
|
346
|
+
<button
|
|
347
|
+
type="button"
|
|
348
|
+
aria-label={side ? "Float window" : "Pin to the right"}
|
|
349
|
+
aria-pressed={side}
|
|
350
|
+
onClick={() => ui.dispatch({ type: "toggleSide" })}
|
|
351
|
+
{...stylex.props(styles.iconBtn)}
|
|
352
|
+
>
|
|
353
|
+
{side ? <FloatIcon /> : <DockRightIcon />}
|
|
354
|
+
</button>
|
|
355
|
+
</Tooltip>
|
|
356
|
+
) : null}
|
|
216
357
|
<Tooltip label={fullscreen ? "Exit fullscreen" : "Fullscreen"}>
|
|
217
358
|
<button
|
|
218
359
|
type="button"
|
|
@@ -278,7 +419,7 @@ export function ChatPanel({ fill = false, site = false }: { fill?: boolean; site
|
|
|
278
419
|
)}
|
|
279
420
|
{session.error ? (
|
|
280
421
|
<div {...stylex.props(styles.error)} role="alert">
|
|
281
|
-
<span>{session.error.message
|
|
422
|
+
<span>{chatErrorText(session.error.message ?? "")}</span>
|
|
282
423
|
<button
|
|
283
424
|
type="button"
|
|
284
425
|
onClick={() => session.clearError()}
|
|
@@ -297,6 +438,7 @@ export function ChatPanel({ fill = false, site = false }: { fill?: boolean; site
|
|
|
297
438
|
)}
|
|
298
439
|
</section>
|
|
299
440
|
);
|
|
441
|
+
return side && sideHost ? createPortal(panel, sideHost) : panel;
|
|
300
442
|
}
|
|
301
443
|
|
|
302
444
|
function EmptyState() {
|
|
@@ -344,6 +486,47 @@ const styles = stylex.create({
|
|
|
344
486
|
width: "auto",
|
|
345
487
|
height: "auto",
|
|
346
488
|
},
|
|
489
|
+
// A column in the page layout, so it sits beside the editor instead of over it.
|
|
490
|
+
panelSide: {
|
|
491
|
+
position: "relative",
|
|
492
|
+
right: "auto",
|
|
493
|
+
bottom: "auto",
|
|
494
|
+
height: "auto",
|
|
495
|
+
flexShrink: 0,
|
|
496
|
+
borderWidth: 0,
|
|
497
|
+
borderLeftWidth: 1,
|
|
498
|
+
borderRadius: 0,
|
|
499
|
+
boxShadow: "none",
|
|
500
|
+
// Square corners need no clipping, and the resize handle straddles the border.
|
|
501
|
+
overflow: "visible",
|
|
502
|
+
},
|
|
503
|
+
// Sits over the panel's left border; wider than it looks so it is easy to grab.
|
|
504
|
+
resizeHandle: {
|
|
505
|
+
position: "absolute",
|
|
506
|
+
top: 0,
|
|
507
|
+
bottom: 0,
|
|
508
|
+
left: -4,
|
|
509
|
+
zIndex: 2,
|
|
510
|
+
width: 8,
|
|
511
|
+
cursor: "col-resize",
|
|
512
|
+
touchAction: "none",
|
|
513
|
+
backgroundColor: "transparent",
|
|
514
|
+
backgroundImage: `linear-gradient(${colors.accent}, ${colors.accent})`,
|
|
515
|
+
backgroundRepeat: "no-repeat",
|
|
516
|
+
backgroundPosition: "center",
|
|
517
|
+
backgroundSize: "0px 100%",
|
|
518
|
+
transition: "background-size 120ms ease",
|
|
519
|
+
[hover]: {
|
|
520
|
+
":hover": { backgroundSize: "2px 100%" },
|
|
521
|
+
},
|
|
522
|
+
":focus-visible": {
|
|
523
|
+
backgroundSize: "2px 100%",
|
|
524
|
+
outlineWidth: 0,
|
|
525
|
+
},
|
|
526
|
+
},
|
|
527
|
+
resizeHandleActive: {
|
|
528
|
+
backgroundSize: "2px 100%",
|
|
529
|
+
},
|
|
347
530
|
panelSite: {
|
|
348
531
|
right: 16,
|
|
349
532
|
bottom: "calc(52px + env(safe-area-inset-bottom, 0px))",
|
|
@@ -374,6 +557,8 @@ const styles = stylex.create({
|
|
|
374
557
|
backgroundColor: colors.bgPanel,
|
|
375
558
|
},
|
|
376
559
|
header: {
|
|
560
|
+
// The Files menu anchors here so it stays inside the panel.
|
|
561
|
+
position: "relative",
|
|
377
562
|
display: "flex",
|
|
378
563
|
alignItems: "center",
|
|
379
564
|
gap: space.sm,
|
|
@@ -393,6 +578,25 @@ const styles = stylex.create({
|
|
|
393
578
|
fontWeight: 500,
|
|
394
579
|
color: colors.text,
|
|
395
580
|
},
|
|
581
|
+
dropOverlay: {
|
|
582
|
+
position: "absolute",
|
|
583
|
+
inset: 6,
|
|
584
|
+
zIndex: 10,
|
|
585
|
+
display: "flex",
|
|
586
|
+
alignItems: "center",
|
|
587
|
+
justifyContent: "center",
|
|
588
|
+
borderWidth: 2,
|
|
589
|
+
borderStyle: "dashed",
|
|
590
|
+
borderColor: colors.textMuted,
|
|
591
|
+
borderRadius: 12,
|
|
592
|
+
backgroundColor: `color-mix(in srgb, ${colors.bgSurface} 88%, transparent)`,
|
|
593
|
+
pointerEvents: "none",
|
|
594
|
+
},
|
|
595
|
+
dropLabel: {
|
|
596
|
+
fontSize: type.md,
|
|
597
|
+
fontWeight: 500,
|
|
598
|
+
color: colors.text,
|
|
599
|
+
},
|
|
396
600
|
headerActions: {
|
|
397
601
|
display: "flex",
|
|
398
602
|
alignItems: "center",
|
|
@@ -24,6 +24,7 @@ import {
|
|
|
24
24
|
createThread,
|
|
25
25
|
fetchAiModels,
|
|
26
26
|
fetchThread,
|
|
27
|
+
fetchThreadFiles,
|
|
27
28
|
fetchThreads,
|
|
28
29
|
keepAllChanges,
|
|
29
30
|
keepChange,
|
|
@@ -32,11 +33,13 @@ import {
|
|
|
32
33
|
notifyTreeChanged,
|
|
33
34
|
undoAllChanges,
|
|
34
35
|
undoChange,
|
|
36
|
+
uploadChatFile,
|
|
35
37
|
type AiModelInfo,
|
|
36
38
|
type AiTier,
|
|
37
39
|
type ApiError,
|
|
38
40
|
type ChatChangeSummary,
|
|
39
41
|
type ChatThread,
|
|
42
|
+
type ThreadChatFile,
|
|
40
43
|
} from "../api";
|
|
41
44
|
import { apiCredentials, apiHeaders } from "../studio-config";
|
|
42
45
|
import {
|
|
@@ -61,6 +64,7 @@ import {
|
|
|
61
64
|
checkoutWriteReloads,
|
|
62
65
|
isWriteTool,
|
|
63
66
|
changesInToolOutput,
|
|
67
|
+
docsChangedByTool,
|
|
64
68
|
detailForTreePatch,
|
|
65
69
|
treeStatusPatches,
|
|
66
70
|
branchToolReloads,
|
|
@@ -74,6 +78,8 @@ import {
|
|
|
74
78
|
type PanelState,
|
|
75
79
|
} from "./chat-state";
|
|
76
80
|
import { fetchNotifyingSessionExpiry } from "./session-expiry";
|
|
81
|
+
import { modelCopyOf } from "./model-copy";
|
|
82
|
+
import { PendingChatFiles, type ChatFilePart } from "./pending-files";
|
|
77
83
|
|
|
78
84
|
// ---------------------------------------------------------------------------
|
|
79
85
|
// UI-level context (panel, thread selection, model, chips)
|
|
@@ -114,6 +120,10 @@ export type ChatUIValue = {
|
|
|
114
120
|
bumpThreads: () => void;
|
|
115
121
|
/** Set when this provider is scoped to a code session checkout. */
|
|
116
122
|
codeSessionId: string | null;
|
|
123
|
+
/** Files added to the composer and not sent yet. */
|
|
124
|
+
files: PendingChatFiles;
|
|
125
|
+
/** The open thread's chat files, oldest first. Refreshed after each turn. */
|
|
126
|
+
threadFiles: ThreadChatFile[];
|
|
117
127
|
/** Preview route included in chat context, or null when dismissed. */
|
|
118
128
|
route: string | null;
|
|
119
129
|
/** The document that route resolves to, when a model claims it. */
|
|
@@ -121,6 +131,9 @@ export type ChatUIValue = {
|
|
|
121
131
|
dismissRoute: () => void;
|
|
122
132
|
};
|
|
123
133
|
|
|
134
|
+
/** The first message of a thread, held until the thread exists. */
|
|
135
|
+
type PendingSend = { text: string; files: ChatFilePart[] };
|
|
136
|
+
|
|
124
137
|
const ChatUIContext = createContext<ChatUIValue | null>(null);
|
|
125
138
|
|
|
126
139
|
export function useChatUI(): ChatUIValue {
|
|
@@ -146,7 +159,7 @@ export type ChatSessionValue = {
|
|
|
146
159
|
status: "submitted" | "streaming" | "ready" | "error";
|
|
147
160
|
error: Error | undefined;
|
|
148
161
|
clearError: () => void;
|
|
149
|
-
send: (text: string) => Promise<void>;
|
|
162
|
+
send: (text: string, files?: ChatFilePart[]) => Promise<void>;
|
|
150
163
|
stop: () => void;
|
|
151
164
|
changes: Record<string, ChatChangeSummary>;
|
|
152
165
|
keep: (changeId: string) => Promise<void>;
|
|
@@ -188,11 +201,6 @@ function publishTreePatches(workspaceId: string, patches: TreeStatusPatch[]) {
|
|
|
188
201
|
let published = false;
|
|
189
202
|
for (const patch of patches) {
|
|
190
203
|
notifyTreeChanged(detailForTreePatch(workspaceId, patch));
|
|
191
|
-
// A published file stays on screen; head-changed reloads it once. A file
|
|
192
|
-
// that disappeared still needs the open editor to drop it.
|
|
193
|
-
if (patch.absent || patch.status !== "clean") {
|
|
194
|
-
notifyDocChanged({ type: patch.type, slug: patch.slug });
|
|
195
|
-
}
|
|
196
204
|
if (patch.status === "clean") published = true;
|
|
197
205
|
}
|
|
198
206
|
if (published) notifyHeadChanged();
|
|
@@ -251,7 +259,7 @@ export function ChatProvider({
|
|
|
251
259
|
const [model, setModelState] = useState<string | null>(null);
|
|
252
260
|
const [threadId, setThreadId] = useState<string | null>(null);
|
|
253
261
|
const [freshThread, setFreshThread] = useState(false);
|
|
254
|
-
const [pendingText, setPendingText] = useState<
|
|
262
|
+
const [pendingText, setPendingText] = useState<PendingSend | null>(null);
|
|
255
263
|
const [mentions, setMentions] = useState<DocRef[]>([]);
|
|
256
264
|
const [pointers, setPointers] = useState<ChatPointer[]>([]);
|
|
257
265
|
const [previewPointer, setPreviewPointer] = useState(false);
|
|
@@ -261,6 +269,19 @@ export function ChatProvider({
|
|
|
261
269
|
// Bumped when the person picks a chat, so a late session list cannot replace it.
|
|
262
270
|
const restore = useRef(0);
|
|
263
271
|
const { focus } = useEditorFocus();
|
|
272
|
+
const files = useMemo(
|
|
273
|
+
() =>
|
|
274
|
+
new PendingChatFiles(async (file, onProgress) => {
|
|
275
|
+
const copy = await modelCopyOf(file);
|
|
276
|
+
const res = await uploadChatFile(workspaceId, file, copy, onProgress);
|
|
277
|
+
return res.file;
|
|
278
|
+
}),
|
|
279
|
+
[workspaceId],
|
|
280
|
+
);
|
|
281
|
+
const [threadFiles, setThreadFiles] = useState<ThreadChatFile[]>([]);
|
|
282
|
+
useEffect(() => {
|
|
283
|
+
files.setThreadFileCount(threadFiles.length);
|
|
284
|
+
}, [files, threadFiles]);
|
|
264
285
|
|
|
265
286
|
// Models + availability.
|
|
266
287
|
useEffect(() => {
|
|
@@ -291,6 +312,7 @@ export function ChatProvider({
|
|
|
291
312
|
setThreadId(null);
|
|
292
313
|
setFreshThread(false);
|
|
293
314
|
setPendingText(null);
|
|
315
|
+
files.take();
|
|
294
316
|
setMentions([]);
|
|
295
317
|
setPointers([]);
|
|
296
318
|
setIncludeFocus(!codeSessionId);
|
|
@@ -309,7 +331,27 @@ export function ChatProvider({
|
|
|
309
331
|
return () => {
|
|
310
332
|
restore.current += 1;
|
|
311
333
|
};
|
|
312
|
-
}, [workspaceId, codeSessionId]);
|
|
334
|
+
}, [workspaceId, codeSessionId, files]);
|
|
335
|
+
|
|
336
|
+
// The thread file list. A turn bumps threadsVersion when it ends, which may have
|
|
337
|
+
// added files or saved one as media.
|
|
338
|
+
useEffect(() => {
|
|
339
|
+
if (!threadId) {
|
|
340
|
+
setThreadFiles([]);
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
let cancelled = false;
|
|
344
|
+
fetchThreadFiles(workspaceId, threadId)
|
|
345
|
+
.then((res) => {
|
|
346
|
+
if (!cancelled) setThreadFiles(res.files);
|
|
347
|
+
})
|
|
348
|
+
.catch(() => {
|
|
349
|
+
if (!cancelled) setThreadFiles([]);
|
|
350
|
+
});
|
|
351
|
+
return () => {
|
|
352
|
+
cancelled = true;
|
|
353
|
+
};
|
|
354
|
+
}, [workspaceId, threadId, threadsVersion]);
|
|
313
355
|
|
|
314
356
|
const setModel = useCallback(
|
|
315
357
|
(id: string) => {
|
|
@@ -326,9 +368,10 @@ export function ChatProvider({
|
|
|
326
368
|
setThreadId(thread.id);
|
|
327
369
|
setFreshThread(false);
|
|
328
370
|
setPendingText(null);
|
|
371
|
+
files.take();
|
|
329
372
|
dispatch({ type: "showChat" });
|
|
330
373
|
},
|
|
331
|
-
[codeSessionId],
|
|
374
|
+
[codeSessionId, files],
|
|
332
375
|
);
|
|
333
376
|
|
|
334
377
|
const newThread = useCallback(() => {
|
|
@@ -336,10 +379,11 @@ export function ChatProvider({
|
|
|
336
379
|
setThreadId(null);
|
|
337
380
|
setFreshThread(false);
|
|
338
381
|
setPendingText(null);
|
|
382
|
+
files.take();
|
|
339
383
|
setMentions([]);
|
|
340
384
|
setPointers([]);
|
|
341
385
|
dispatch({ type: "showChat" });
|
|
342
|
-
}, []);
|
|
386
|
+
}, [files]);
|
|
343
387
|
|
|
344
388
|
const addMention = useCallback((ref: DocRef) => {
|
|
345
389
|
setMentions((prev) => addMentionTo(prev, ref));
|
|
@@ -373,13 +417,13 @@ export function ChatProvider({
|
|
|
373
417
|
|
|
374
418
|
// Creating a thread lazily on first send.
|
|
375
419
|
const startThread = useCallback(
|
|
376
|
-
async (text: string) => {
|
|
420
|
+
async (text: string, parts: ChatFilePart[] = []) => {
|
|
377
421
|
restore.current += 1;
|
|
378
422
|
const res = await createThread(
|
|
379
423
|
workspaceId,
|
|
380
424
|
codeSessionId ? { codeSessionId } : undefined,
|
|
381
425
|
);
|
|
382
|
-
setPendingText(text);
|
|
426
|
+
setPendingText({ text, files: parts });
|
|
383
427
|
setFreshThread(true);
|
|
384
428
|
setThreadId(res.thread.id);
|
|
385
429
|
bumpThreads();
|
|
@@ -421,6 +465,8 @@ export function ChatProvider({
|
|
|
421
465
|
threadsVersion,
|
|
422
466
|
bumpThreads,
|
|
423
467
|
codeSessionId,
|
|
468
|
+
files,
|
|
469
|
+
threadFiles,
|
|
424
470
|
route,
|
|
425
471
|
viewed,
|
|
426
472
|
dismissRoute,
|
|
@@ -452,6 +498,8 @@ export function ChatProvider({
|
|
|
452
498
|
threadsVersion,
|
|
453
499
|
bumpThreads,
|
|
454
500
|
codeSessionId,
|
|
501
|
+
files,
|
|
502
|
+
threadFiles,
|
|
455
503
|
route,
|
|
456
504
|
viewed,
|
|
457
505
|
dismissRoute,
|
|
@@ -512,7 +560,7 @@ function EmptySession({
|
|
|
512
560
|
onFirstSend,
|
|
513
561
|
children,
|
|
514
562
|
}: {
|
|
515
|
-
onFirstSend: (text: string) => Promise<void>;
|
|
563
|
+
onFirstSend: (text: string, files?: ChatFilePart[]) => Promise<void>;
|
|
516
564
|
children: React.ReactNode;
|
|
517
565
|
}) {
|
|
518
566
|
const [error, setError] = useState<Error | undefined>();
|
|
@@ -526,10 +574,10 @@ function EmptySession({
|
|
|
526
574
|
status,
|
|
527
575
|
error,
|
|
528
576
|
clearError: () => setError(undefined),
|
|
529
|
-
send: async (text) => {
|
|
577
|
+
send: async (text, files) => {
|
|
530
578
|
setStatus("submitted");
|
|
531
579
|
try {
|
|
532
|
-
await onFirstSend(text);
|
|
580
|
+
await onFirstSend(text, files);
|
|
533
581
|
} catch (err) {
|
|
534
582
|
setError(err instanceof Error ? err : new Error(String(err)));
|
|
535
583
|
setStatus("error");
|
|
@@ -552,7 +600,7 @@ type SessionProps = {
|
|
|
552
600
|
workspaceId: string;
|
|
553
601
|
threadId: string;
|
|
554
602
|
fresh: boolean;
|
|
555
|
-
pendingText:
|
|
603
|
+
pendingText: PendingSend | null;
|
|
556
604
|
onPendingConsumed: () => void;
|
|
557
605
|
getContext: () => ChatRequestContext;
|
|
558
606
|
getModel: () => string | null;
|
|
@@ -683,11 +731,12 @@ function LiveSession({
|
|
|
683
731
|
const { messages, status, sendMessage } = chat;
|
|
684
732
|
|
|
685
733
|
const send = useCallback(
|
|
686
|
-
async (text: string) => {
|
|
734
|
+
async (text: string, files: ChatFilePart[] = []) => {
|
|
687
735
|
const context = getContext();
|
|
688
736
|
await sendMessage(
|
|
689
737
|
{
|
|
690
738
|
text,
|
|
739
|
+
...(files.length ? { files } : {}),
|
|
691
740
|
metadata: context.pointers.length ? { pointers: context.pointers } : undefined,
|
|
692
741
|
},
|
|
693
742
|
{ body: { model: getModel(), context } },
|
|
@@ -702,7 +751,7 @@ function LiveSession({
|
|
|
702
751
|
if (pendingText === null || sentPending.current) return;
|
|
703
752
|
sentPending.current = true;
|
|
704
753
|
onPendingConsumed();
|
|
705
|
-
void send(pendingText);
|
|
754
|
+
void send(pendingText.text, pendingText.files);
|
|
706
755
|
}, [pendingText, onPendingConsumed, send]);
|
|
707
756
|
|
|
708
757
|
// Watch tool parts: mark the focused doc as "AI editing" while a write tool
|
|
@@ -749,10 +798,11 @@ function LiveSession({
|
|
|
749
798
|
for (const change of found) next[change.id] = change;
|
|
750
799
|
return next;
|
|
751
800
|
});
|
|
752
|
-
if (patches.length === 0)
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
801
|
+
if (patches.length === 0) notifyTreeChanged();
|
|
802
|
+
}
|
|
803
|
+
// The open editor reloads its form and pushes the result to the preview.
|
|
804
|
+
for (const changed of docsChangedByTool(name, part.input, part.output)) {
|
|
805
|
+
notifyDocChanged(changed);
|
|
756
806
|
}
|
|
757
807
|
}
|
|
758
808
|
}
|