@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/Composer.tsx
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// packages/studio/src/chat/Composer.tsx
|
|
2
|
-
import { useCallback, useEffect, useLayoutEffect, useRef, useState } from "react";
|
|
2
|
+
import { useCallback, useEffect, useLayoutEffect, useRef, useState, useSyncExternalStore } from "react";
|
|
3
3
|
import * as stylex from "@stylexjs/stylex";
|
|
4
4
|
import { docKey, type DocRef } from "../chrome/EditorFocusContext";
|
|
5
5
|
import { colors, radius, space, type } from "../styles/tokens.stylex";
|
|
@@ -8,7 +8,10 @@ import { Tooltip } from "../ui/Tooltip";
|
|
|
8
8
|
import { useChatSession, useChatUI } from "./ChatProvider";
|
|
9
9
|
import { MentionPopover, type MentionKey } from "./MentionPopover";
|
|
10
10
|
import { ModelPicker } from "./ModelPicker";
|
|
11
|
-
import { AtIcon, CloseIcon, FileIcon, FocusIcon, PointerIcon, SendIcon, StopIcon } from "./icons";
|
|
11
|
+
import { AtIcon, CloseIcon, FileIcon, FocusIcon, PaperclipIcon, PointerIcon, SendIcon, StopIcon } from "./icons";
|
|
12
|
+
import { CHAT_FILE_ACCEPT } from "./pending-files";
|
|
13
|
+
import { PendingFileRow, chatFileErrorText } from "./PendingFileRow";
|
|
14
|
+
import { CHAT_INSERT_EVENT } from "./ChatFiles";
|
|
12
15
|
import { mentionQueryAt } from "./chat-helpers";
|
|
13
16
|
import { PointerChip, pointerKey } from "./PointerChip";
|
|
14
17
|
import { POINTER_ARM_EVENT } from "./pointer-arm";
|
|
@@ -63,11 +66,14 @@ export function Composer({ autoFocus }: { autoFocus: boolean }) {
|
|
|
63
66
|
const [mention, setMention] = useState<{ start: number; query: string } | null>(null);
|
|
64
67
|
const [pointing, setPointing] = useState(false);
|
|
65
68
|
const ref = useRef<HTMLTextAreaElement>(null);
|
|
69
|
+
const pickRef = useRef<HTMLInputElement>(null);
|
|
70
|
+
const pending = useSyncExternalStore(ui.files.subscribe, ui.files.snapshot, ui.files.snapshot);
|
|
66
71
|
const boxRef = useRef<HTMLDivElement>(null);
|
|
67
72
|
const mentionKeys = useRef<((key: MentionKey) => void) | null>(null);
|
|
68
73
|
const updating = useCodeSessionContext()?.updating === true;
|
|
69
74
|
const busy = session.status === "streaming" || session.status === "submitted" || updating;
|
|
70
|
-
const
|
|
75
|
+
const hasMessage = text.trim().length > 0 || pending.items.length > 0;
|
|
76
|
+
const canSend = hasMessage && pending.ready && !busy && ui.aiAvailable === true;
|
|
71
77
|
|
|
72
78
|
// Auto-grow. Measuring must not leave a 1px swing: that resizes the thread
|
|
73
79
|
// on every key, and the thread then tries to scroll to the bottom.
|
|
@@ -100,6 +106,27 @@ export function Composer({ autoFocus }: { autoFocus: boolean }) {
|
|
|
100
106
|
return () => window.removeEventListener("backmatter:chat-focus", onFocus);
|
|
101
107
|
}, []);
|
|
102
108
|
|
|
109
|
+
// The Files list inserts a reference to a chat file at the caret.
|
|
110
|
+
useEffect(() => {
|
|
111
|
+
function onInsert(e: Event) {
|
|
112
|
+
const insert = (e as CustomEvent<{ text: string }>).detail?.text;
|
|
113
|
+
const el = ref.current;
|
|
114
|
+
if (!insert || !el) return;
|
|
115
|
+
const start = el.selectionStart ?? el.value.length;
|
|
116
|
+
const end = el.selectionEnd ?? start;
|
|
117
|
+
const before = el.value.slice(0, start);
|
|
118
|
+
const spaced = `${before && !/\s$/.test(before) ? " " : ""}${insert} `;
|
|
119
|
+
setText(`${before}${spaced}${el.value.slice(end)}`);
|
|
120
|
+
const caret = start + spaced.length;
|
|
121
|
+
requestAnimationFrame(() => {
|
|
122
|
+
el.focus();
|
|
123
|
+
el.setSelectionRange(caret, caret);
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
window.addEventListener(CHAT_INSERT_EVENT, onInsert);
|
|
127
|
+
return () => window.removeEventListener(CHAT_INSERT_EVENT, onInsert);
|
|
128
|
+
}, []);
|
|
129
|
+
|
|
103
130
|
// Suggestion chips ending with "…" prefill the composer.
|
|
104
131
|
useEffect(() => {
|
|
105
132
|
function onPrefill(e: Event) {
|
|
@@ -140,16 +167,17 @@ export function Composer({ autoFocus }: { autoFocus: boolean }) {
|
|
|
140
167
|
|
|
141
168
|
const submit = useCallback(() => {
|
|
142
169
|
const value = text.trim();
|
|
143
|
-
if (!
|
|
170
|
+
if (!canSend) return;
|
|
171
|
+
const parts = ui.files.take();
|
|
144
172
|
setText("");
|
|
145
173
|
setMention(null);
|
|
146
174
|
if (pointing) {
|
|
147
175
|
setPointing(false);
|
|
148
176
|
postPointer(false);
|
|
149
177
|
}
|
|
150
|
-
void session.send(value);
|
|
178
|
+
void session.send(value, parts);
|
|
151
179
|
ui.clearPointers();
|
|
152
|
-
}, [text,
|
|
180
|
+
}, [text, canSend, session, ui, pointing]);
|
|
153
181
|
|
|
154
182
|
function updateMention(el: HTMLTextAreaElement) {
|
|
155
183
|
setMention(mentionQueryAt(el.value, el.selectionStart ?? el.value.length));
|
|
@@ -222,6 +250,18 @@ export function Composer({ autoFocus }: { autoFocus: boolean }) {
|
|
|
222
250
|
</div>
|
|
223
251
|
) : null}
|
|
224
252
|
<div ref={boxRef} {...stylex.props(styles.box)}>
|
|
253
|
+
{pending.items.length > 0 ? (
|
|
254
|
+
<PendingFileRow
|
|
255
|
+
items={pending.items}
|
|
256
|
+
onRemove={(key) => ui.files.remove(key)}
|
|
257
|
+
onRetry={(key) => ui.files.retry(key)}
|
|
258
|
+
/>
|
|
259
|
+
) : null}
|
|
260
|
+
{pending.error ? (
|
|
261
|
+
<p role="alert" {...stylex.props(styles.fileError)}>
|
|
262
|
+
{chatFileErrorText(pending.error)}
|
|
263
|
+
</p>
|
|
264
|
+
) : null}
|
|
225
265
|
{ui.pointers.length > 0 ? (
|
|
226
266
|
<div {...stylex.props(styles.chips)} aria-label="Pointed elements">
|
|
227
267
|
{ui.pointers.map((pointer) => (
|
|
@@ -252,6 +292,12 @@ export function Composer({ autoFocus }: { autoFocus: boolean }) {
|
|
|
252
292
|
updateMention(e.target);
|
|
253
293
|
}}
|
|
254
294
|
onClick={(e) => updateMention(e.currentTarget)}
|
|
295
|
+
onPaste={(e) => {
|
|
296
|
+
const pasted = Array.from(e.clipboardData?.files ?? []);
|
|
297
|
+
if (pasted.length === 0) return;
|
|
298
|
+
e.preventDefault();
|
|
299
|
+
ui.files.add(pasted);
|
|
300
|
+
}}
|
|
255
301
|
onKeyDown={(e) => {
|
|
256
302
|
if (mention && isMentionKey(e.key)) {
|
|
257
303
|
e.preventDefault();
|
|
@@ -300,6 +346,32 @@ export function Composer({ autoFocus }: { autoFocus: boolean }) {
|
|
|
300
346
|
</button>
|
|
301
347
|
</Tooltip>
|
|
302
348
|
) : null}
|
|
349
|
+
<Tooltip label="Add images or files">
|
|
350
|
+
<button
|
|
351
|
+
type="button"
|
|
352
|
+
aria-label="Add files"
|
|
353
|
+
disabled={ui.aiAvailable !== true}
|
|
354
|
+
onClick={() => pickRef.current?.click()}
|
|
355
|
+
{...stylex.props(styles.iconBtn)}
|
|
356
|
+
>
|
|
357
|
+
<PaperclipIcon />
|
|
358
|
+
</button>
|
|
359
|
+
</Tooltip>
|
|
360
|
+
<input
|
|
361
|
+
ref={pickRef}
|
|
362
|
+
type="file"
|
|
363
|
+
multiple
|
|
364
|
+
hidden
|
|
365
|
+
accept={CHAT_FILE_ACCEPT}
|
|
366
|
+
aria-hidden="true"
|
|
367
|
+
tabIndex={-1}
|
|
368
|
+
onChange={(e) => {
|
|
369
|
+
const picked = Array.from(e.currentTarget.files ?? []);
|
|
370
|
+
e.currentTarget.value = "";
|
|
371
|
+
if (picked.length > 0) ui.files.add(picked);
|
|
372
|
+
ref.current?.focus();
|
|
373
|
+
}}
|
|
374
|
+
/>
|
|
303
375
|
<span {...stylex.props(styles.spacer)} />
|
|
304
376
|
{ui.pointer ? (
|
|
305
377
|
<Tooltip label={pointing ? "Stop pointing" : "Point at an element on the page"}>
|
|
@@ -533,6 +605,11 @@ const styles = stylex.create({
|
|
|
533
605
|
cursor: "not-allowed",
|
|
534
606
|
},
|
|
535
607
|
},
|
|
608
|
+
fileError: {
|
|
609
|
+
margin: 0,
|
|
610
|
+
fontSize: type.xs,
|
|
611
|
+
color: colors.danger,
|
|
612
|
+
},
|
|
536
613
|
footer: {
|
|
537
614
|
display: "flex",
|
|
538
615
|
alignItems: "center",
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
// packages/studio/src/chat/PendingFileRow.tsx
|
|
2
|
+
import * as stylex from "@stylexjs/stylex";
|
|
3
|
+
import { colors, radius, space, type } from "../styles/tokens.stylex";
|
|
4
|
+
import { CloseIcon, FileIcon } from "./icons";
|
|
5
|
+
import type { PendingChatFile } from "./pending-files";
|
|
6
|
+
|
|
7
|
+
const ERROR_TEXT: Record<string, string> = {
|
|
8
|
+
unsupported_type: "That file type can't be added. Use images, PDFs, or text files.",
|
|
9
|
+
too_large: "That file is too large. Images up to 20 MB, PDFs up to 10 MB, text up to 1 MB.",
|
|
10
|
+
too_many: "A message can hold up to 10 files.",
|
|
11
|
+
thread_full: "This chat holds up to 50 files. Start a new chat to add more.",
|
|
12
|
+
empty: "That file is empty.",
|
|
13
|
+
too_many_unsent: "You have too many files waiting to be sent. Send or remove some first.",
|
|
14
|
+
quota_exceeded: "This workspace has used its chat file storage.",
|
|
15
|
+
bad_model_copy: "That image couldn't be prepared. Try another format.",
|
|
16
|
+
upload_failed: "Upload failed. Try again.",
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export function chatFileErrorText(code: string): string {
|
|
20
|
+
return ERROR_TEXT[code] ?? ERROR_TEXT.upload_failed!;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function extension(name: string): string {
|
|
24
|
+
const ext = name.includes(".") ? name.split(".").pop() ?? "" : "";
|
|
25
|
+
return ext.slice(0, 4).toUpperCase() || "FILE";
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Files waiting in the composer, left to right in the order they were added. */
|
|
29
|
+
export function PendingFileRow({
|
|
30
|
+
items,
|
|
31
|
+
onRemove,
|
|
32
|
+
onRetry,
|
|
33
|
+
}: {
|
|
34
|
+
items: PendingChatFile[];
|
|
35
|
+
onRemove: (key: string) => void;
|
|
36
|
+
onRetry: (key: string) => void;
|
|
37
|
+
}) {
|
|
38
|
+
return (
|
|
39
|
+
<div {...stylex.props(styles.row)} aria-label="Files to send">
|
|
40
|
+
{items.map((item) => (
|
|
41
|
+
<div
|
|
42
|
+
key={item.key}
|
|
43
|
+
title={item.status === "error" ? `${item.name}: ${chatFileErrorText(item.error ?? "")}` : item.name}
|
|
44
|
+
{...stylex.props(styles.tile, item.status === "error" && styles.tileError)}
|
|
45
|
+
>
|
|
46
|
+
{item.previewUrl ? (
|
|
47
|
+
<img src={item.previewUrl} alt="" {...stylex.props(styles.thumb)} />
|
|
48
|
+
) : (
|
|
49
|
+
<span {...stylex.props(styles.doc)}>
|
|
50
|
+
<FileIcon size={16} />
|
|
51
|
+
<span {...stylex.props(styles.ext)}>{extension(item.name)}</span>
|
|
52
|
+
</span>
|
|
53
|
+
)}
|
|
54
|
+
<span {...stylex.props(styles.srOnly)}>{item.name}</span>
|
|
55
|
+
{item.status === "uploading" ? (
|
|
56
|
+
<span
|
|
57
|
+
role="progressbar"
|
|
58
|
+
aria-label={`Uploading ${item.name}`}
|
|
59
|
+
aria-valuemin={0}
|
|
60
|
+
aria-valuemax={100}
|
|
61
|
+
aria-valuenow={Math.round(item.progress * 100)}
|
|
62
|
+
{...stylex.props(styles.progressTrack)}
|
|
63
|
+
>
|
|
64
|
+
<span {...stylex.props(styles.progressFill)} style={{ width: `${Math.max(6, item.progress * 100)}%` }} />
|
|
65
|
+
</span>
|
|
66
|
+
) : null}
|
|
67
|
+
{item.status === "error" ? (
|
|
68
|
+
<button
|
|
69
|
+
type="button"
|
|
70
|
+
aria-label={`Retry ${item.name}`}
|
|
71
|
+
onClick={() => onRetry(item.key)}
|
|
72
|
+
{...stylex.props(styles.retry)}
|
|
73
|
+
>
|
|
74
|
+
Retry
|
|
75
|
+
</button>
|
|
76
|
+
) : null}
|
|
77
|
+
<button
|
|
78
|
+
type="button"
|
|
79
|
+
aria-label={`Remove ${item.name || "file"}`}
|
|
80
|
+
onClick={() => onRemove(item.key)}
|
|
81
|
+
{...stylex.props(styles.remove)}
|
|
82
|
+
>
|
|
83
|
+
<CloseIcon size={10} />
|
|
84
|
+
</button>
|
|
85
|
+
</div>
|
|
86
|
+
))}
|
|
87
|
+
</div>
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const styles = stylex.create({
|
|
92
|
+
row: {
|
|
93
|
+
display: "flex",
|
|
94
|
+
flexWrap: "wrap",
|
|
95
|
+
gap: space.sm,
|
|
96
|
+
},
|
|
97
|
+
tile: {
|
|
98
|
+
position: "relative",
|
|
99
|
+
width: 48,
|
|
100
|
+
height: 48,
|
|
101
|
+
flexShrink: 0,
|
|
102
|
+
overflow: "hidden",
|
|
103
|
+
borderRadius: radius.sm,
|
|
104
|
+
borderWidth: 1,
|
|
105
|
+
borderStyle: "solid",
|
|
106
|
+
borderColor: colors.border,
|
|
107
|
+
backgroundColor: colors.bgPanelInset,
|
|
108
|
+
},
|
|
109
|
+
tileError: {
|
|
110
|
+
borderColor: colors.danger,
|
|
111
|
+
},
|
|
112
|
+
thumb: {
|
|
113
|
+
display: "block",
|
|
114
|
+
width: "100%",
|
|
115
|
+
height: "100%",
|
|
116
|
+
objectFit: "cover",
|
|
117
|
+
},
|
|
118
|
+
doc: {
|
|
119
|
+
display: "flex",
|
|
120
|
+
flexDirection: "column",
|
|
121
|
+
alignItems: "center",
|
|
122
|
+
justifyContent: "center",
|
|
123
|
+
gap: 2,
|
|
124
|
+
width: "100%",
|
|
125
|
+
height: "100%",
|
|
126
|
+
color: colors.textMuted,
|
|
127
|
+
},
|
|
128
|
+
ext: {
|
|
129
|
+
fontSize: 9,
|
|
130
|
+
fontWeight: 600,
|
|
131
|
+
letterSpacing: "0.04em",
|
|
132
|
+
fontFamily: type.sans,
|
|
133
|
+
},
|
|
134
|
+
srOnly: {
|
|
135
|
+
position: "absolute",
|
|
136
|
+
width: 1,
|
|
137
|
+
height: 1,
|
|
138
|
+
overflow: "hidden",
|
|
139
|
+
clipPath: "inset(50%)",
|
|
140
|
+
whiteSpace: "nowrap",
|
|
141
|
+
},
|
|
142
|
+
progressTrack: {
|
|
143
|
+
position: "absolute",
|
|
144
|
+
left: 4,
|
|
145
|
+
right: 4,
|
|
146
|
+
bottom: 4,
|
|
147
|
+
height: 3,
|
|
148
|
+
borderRadius: 999,
|
|
149
|
+
backgroundColor: `color-mix(in srgb, ${colors.bgSurface} 70%, transparent)`,
|
|
150
|
+
overflow: "hidden",
|
|
151
|
+
},
|
|
152
|
+
progressFill: {
|
|
153
|
+
display: "block",
|
|
154
|
+
height: "100%",
|
|
155
|
+
borderRadius: 999,
|
|
156
|
+
backgroundColor: colors.text,
|
|
157
|
+
transitionProperty: "width",
|
|
158
|
+
transitionDuration: "120ms",
|
|
159
|
+
},
|
|
160
|
+
retry: {
|
|
161
|
+
position: "absolute",
|
|
162
|
+
inset: 0,
|
|
163
|
+
borderWidth: 0,
|
|
164
|
+
padding: 0,
|
|
165
|
+
backgroundColor: `color-mix(in srgb, ${colors.bgSurface} 75%, transparent)`,
|
|
166
|
+
color: colors.danger,
|
|
167
|
+
fontSize: type.xs,
|
|
168
|
+
fontWeight: 600,
|
|
169
|
+
cursor: "pointer",
|
|
170
|
+
},
|
|
171
|
+
remove: {
|
|
172
|
+
position: "absolute",
|
|
173
|
+
top: 2,
|
|
174
|
+
right: 2,
|
|
175
|
+
zIndex: 1,
|
|
176
|
+
display: "inline-flex",
|
|
177
|
+
alignItems: "center",
|
|
178
|
+
justifyContent: "center",
|
|
179
|
+
width: 16,
|
|
180
|
+
height: 16,
|
|
181
|
+
padding: 0,
|
|
182
|
+
borderWidth: 0,
|
|
183
|
+
borderRadius: 999,
|
|
184
|
+
backgroundColor: colors.text,
|
|
185
|
+
color: colors.textInverse,
|
|
186
|
+
cursor: "pointer",
|
|
187
|
+
},
|
|
188
|
+
});
|
package/src/chat/chat-context.ts
CHANGED
|
@@ -22,8 +22,12 @@ export type ChatPointer = {
|
|
|
22
22
|
column?: number | null;
|
|
23
23
|
/** Component names from the element toward the layout. */
|
|
24
24
|
stack?: string[];
|
|
25
|
+
/** Block prop the pointed text came from, such as `headline`. */
|
|
26
|
+
field?: string;
|
|
25
27
|
};
|
|
26
28
|
|
|
29
|
+
const POINTER_FIELD = /^[\w.$-]{1,80}$/;
|
|
30
|
+
|
|
27
31
|
export const POINTER_CAP = 8;
|
|
28
32
|
|
|
29
33
|
/** Request context sent with every chat message (mirrors the server's ChatRequestContext). */
|
|
@@ -48,7 +52,8 @@ function samePointer(a: ChatPointer, b: ChatPointer): boolean {
|
|
|
48
52
|
a.quote === b.quote &&
|
|
49
53
|
(a.component ?? null) === (b.component ?? null) &&
|
|
50
54
|
(a.file ?? null) === (b.file ?? null) &&
|
|
51
|
-
(a.line ?? null) === (b.line ?? null)
|
|
55
|
+
(a.line ?? null) === (b.line ?? null) &&
|
|
56
|
+
(a.field ?? null) === (b.field ?? null)
|
|
52
57
|
);
|
|
53
58
|
}
|
|
54
59
|
|
|
@@ -103,6 +108,7 @@ export function chatPointerFromGrab(
|
|
|
103
108
|
blockIndex: value.blockIndex,
|
|
104
109
|
quote: value.quote.replace(/\s+/g, " ").trim().slice(0, 240),
|
|
105
110
|
...placeFrom(value),
|
|
111
|
+
...(typeof value.field === "string" && POINTER_FIELD.test(value.field) ? { field: value.field } : {}),
|
|
106
112
|
};
|
|
107
113
|
}
|
|
108
114
|
|
package/src/chat/chat-helpers.ts
CHANGED
|
@@ -103,6 +103,30 @@ export function treeStatusPatches(output: unknown): TreeStatusPatch[] {
|
|
|
103
103
|
return patches;
|
|
104
104
|
}
|
|
105
105
|
|
|
106
|
+
/**
|
|
107
|
+
* Documents an open editor reloads after a finished tool: every document a
|
|
108
|
+
* write recorded, and every tree row that is not published. A published row
|
|
109
|
+
* stays on screen; head-changed reloads it once.
|
|
110
|
+
*/
|
|
111
|
+
export function docsChangedByTool(name: string, input: unknown, output: unknown): DocRef[] {
|
|
112
|
+
const refs: DocRef[] = [];
|
|
113
|
+
const add = (ref: DocRef) => {
|
|
114
|
+
if (!refs.some((row) => row.type === ref.type && row.slug === ref.slug)) refs.push(ref);
|
|
115
|
+
};
|
|
116
|
+
if (isWriteTool(name)) {
|
|
117
|
+
const found = changesInToolOutput(output);
|
|
118
|
+
const written = (input as { docRef?: Partial<DocRef> } | undefined)?.docRef;
|
|
119
|
+
if (found.length > 0 && written?.type && written.slug) add({ type: written.type, slug: written.slug });
|
|
120
|
+
for (const change of found) {
|
|
121
|
+
if (change.type && change.slug) add({ type: change.type, slug: change.slug });
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
for (const patch of treeStatusPatches(output)) {
|
|
125
|
+
if (patch.absent || patch.status !== "clean") add({ type: patch.type, slug: patch.slug });
|
|
126
|
+
}
|
|
127
|
+
return refs;
|
|
128
|
+
}
|
|
129
|
+
|
|
106
130
|
/** A deleted or absent row leaves the content tree. Other rows update status. */
|
|
107
131
|
export function detailForTreePatch(
|
|
108
132
|
workspaceId: string,
|
|
@@ -605,3 +629,28 @@ export function replaceEchoedDocList(
|
|
|
605
629
|
}
|
|
606
630
|
return changed ? out.join("\n") : text;
|
|
607
631
|
}
|
|
632
|
+
|
|
633
|
+
const CHAT_ERROR_TEXT: Record<string, string> = {
|
|
634
|
+
too_many: "Too many files. A message can hold up to 10, and a chat up to 50. Start a new chat to add more.",
|
|
635
|
+
unknown_chat_file: "A file in this message is no longer available. Remove it and add it again.",
|
|
636
|
+
bad_file_part: "A file in this message could not be sent. Remove it and add it again.",
|
|
637
|
+
ai_not_configured: "AI is not configured on this server.",
|
|
638
|
+
};
|
|
639
|
+
|
|
640
|
+
/**
|
|
641
|
+
* The text for a failed turn. The chat transport puts the response body in the error
|
|
642
|
+
* message, so a server refusal arrives as `{"error":"code"}`.
|
|
643
|
+
*/
|
|
644
|
+
export function chatErrorText(message: string): string {
|
|
645
|
+
const raw = message.trim();
|
|
646
|
+
if (!raw) return "Something went wrong.";
|
|
647
|
+
if (raw.startsWith("{")) {
|
|
648
|
+
try {
|
|
649
|
+
const code = (JSON.parse(raw) as { error?: unknown }).error;
|
|
650
|
+
if (typeof code === "string" && code) return CHAT_ERROR_TEXT[code] ?? code;
|
|
651
|
+
} catch {
|
|
652
|
+
// Not JSON after all; show it as it is.
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
return raw;
|
|
656
|
+
}
|
package/src/chat/chat-state.ts
CHANGED
|
@@ -2,20 +2,25 @@
|
|
|
2
2
|
//
|
|
3
3
|
// Panel state machine for the global chat. Pure so it can be unit tested.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
/** "side" is a column pinned to the right of the editor instead of a floating window. */
|
|
6
|
+
export type PanelMode = "closed" | "open" | "minimized" | "fullscreen" | "side";
|
|
7
|
+
export type OpenPanelMode = "open" | "fullscreen" | "side";
|
|
6
8
|
export type PanelView = "chat" | "history";
|
|
7
9
|
|
|
8
10
|
export type PanelState = {
|
|
9
11
|
mode: PanelMode;
|
|
10
12
|
view: PanelView;
|
|
11
13
|
/** Mode to return to when leaving fullscreen or un-minimizing. */
|
|
12
|
-
lastOpenMode:
|
|
14
|
+
lastOpenMode: OpenPanelMode;
|
|
15
|
+
/** Window layout to return to when leaving fullscreen. */
|
|
16
|
+
windowMode: "open" | "side";
|
|
13
17
|
};
|
|
14
18
|
|
|
15
19
|
export const initialPanelState: PanelState = {
|
|
16
20
|
mode: "closed",
|
|
17
21
|
view: "chat",
|
|
18
22
|
lastOpenMode: "open",
|
|
23
|
+
windowMode: "open",
|
|
19
24
|
};
|
|
20
25
|
|
|
21
26
|
export type PanelAction =
|
|
@@ -24,6 +29,7 @@ export type PanelAction =
|
|
|
24
29
|
| { type: "close" }
|
|
25
30
|
| { type: "minimize" }
|
|
26
31
|
| { type: "toggleFullscreen" }
|
|
32
|
+
| { type: "toggleSide" }
|
|
27
33
|
| { type: "showHistory" }
|
|
28
34
|
| { type: "showChat" };
|
|
29
35
|
|
|
@@ -40,9 +46,13 @@ export function panelReducer(state: PanelState, action: PanelAction): PanelState
|
|
|
40
46
|
case "minimize":
|
|
41
47
|
return state.mode === "closed" ? state : { ...state, mode: "minimized" };
|
|
42
48
|
case "toggleFullscreen": {
|
|
43
|
-
const next = state.mode === "fullscreen" ?
|
|
49
|
+
const next = state.mode === "fullscreen" ? state.windowMode : "fullscreen";
|
|
44
50
|
return { ...state, mode: next, lastOpenMode: next };
|
|
45
51
|
}
|
|
52
|
+
case "toggleSide": {
|
|
53
|
+
const next = state.mode === "side" ? "open" : "side";
|
|
54
|
+
return { ...state, mode: next, lastOpenMode: next, windowMode: next };
|
|
55
|
+
}
|
|
46
56
|
case "showHistory":
|
|
47
57
|
return {
|
|
48
58
|
...state,
|
|
@@ -55,16 +65,17 @@ export function panelReducer(state: PanelState, action: PanelAction): PanelState
|
|
|
55
65
|
}
|
|
56
66
|
|
|
57
67
|
export function isPanelVisible(mode: PanelMode): boolean {
|
|
58
|
-
return mode === "open" || mode === "fullscreen";
|
|
68
|
+
return mode === "open" || mode === "fullscreen" || mode === "side";
|
|
59
69
|
}
|
|
60
70
|
|
|
61
|
-
/** A docked column has no window chrome: it cannot close, minimize, or
|
|
71
|
+
/** A docked column has no window chrome: it cannot close, minimize, go fullscreen, or move to the side. */
|
|
62
72
|
export function allowsPanelAction(action: PanelAction, docked: boolean): boolean {
|
|
63
73
|
if (!docked) return true;
|
|
64
74
|
return (
|
|
65
75
|
action.type !== "toggle" &&
|
|
66
76
|
action.type !== "close" &&
|
|
67
77
|
action.type !== "minimize" &&
|
|
68
|
-
action.type !== "toggleFullscreen"
|
|
78
|
+
action.type !== "toggleFullscreen" &&
|
|
79
|
+
action.type !== "toggleSide"
|
|
69
80
|
);
|
|
70
81
|
}
|
package/src/chat/icons.tsx
CHANGED
|
@@ -67,6 +67,26 @@ export function ExpandIcon({ size = 14 }: Props) {
|
|
|
67
67
|
);
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
+
/** A window with its right column filled: pin the panel to the side. */
|
|
71
|
+
export function DockRightIcon({ size = 14 }: Props) {
|
|
72
|
+
return (
|
|
73
|
+
<svg {...base(size)}>
|
|
74
|
+
<rect x="2.5" y="3" width="11" height="10" rx="1.5" />
|
|
75
|
+
<path d="M9.5 3v10" />
|
|
76
|
+
</svg>
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** A floating window: take the panel off the side. */
|
|
81
|
+
export function FloatIcon({ size = 14 }: Props) {
|
|
82
|
+
return (
|
|
83
|
+
<svg {...base(size)}>
|
|
84
|
+
<rect x="2.5" y="3" width="11" height="10" rx="1.5" />
|
|
85
|
+
<rect x="8" y="8" width="3.5" height="3" rx="0.5" />
|
|
86
|
+
</svg>
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
|
|
70
90
|
export function CollapseIcon({ size = 14 }: Props) {
|
|
71
91
|
return (
|
|
72
92
|
<svg {...base(size)}>
|
|
@@ -189,3 +209,11 @@ export function FocusIcon({ size = 14 }: Props) {
|
|
|
189
209
|
</svg>
|
|
190
210
|
);
|
|
191
211
|
}
|
|
212
|
+
|
|
213
|
+
export function PaperclipIcon({ size = 14 }: Props) {
|
|
214
|
+
return (
|
|
215
|
+
<svg {...base(size)}>
|
|
216
|
+
<path d="m12.6 7.4-4.9 4.9a3 3 0 0 1-4.2-4.2l5.1-5.1a2 2 0 0 1 2.8 2.8l-5 5a1 1 0 0 1-1.4-1.4l4.6-4.6" />
|
|
217
|
+
</svg>
|
|
218
|
+
);
|
|
219
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// packages/studio/src/chat/model-copy.ts
|
|
2
|
+
//
|
|
3
|
+
// Providers shrink or refuse large images, and the server has no resizer, so the
|
|
4
|
+
// browser makes the copy the model sees. The original is what gets saved as media.
|
|
5
|
+
|
|
6
|
+
/** Longest side of the copy the model sees. */
|
|
7
|
+
export const MODEL_IMAGE_EDGE = 1568;
|
|
8
|
+
/** An original at or under this size and edge is sent as is. */
|
|
9
|
+
const SMALL_ENOUGH_BYTES = 1.5 * 1024 * 1024;
|
|
10
|
+
|
|
11
|
+
/** A WebP (or JPEG) copy at most {@link MODEL_IMAGE_EDGE} on its longest side, or null to send the original. */
|
|
12
|
+
export async function modelCopyOf(file: File): Promise<Blob | null> {
|
|
13
|
+
if (!file.type.startsWith("image/")) return null;
|
|
14
|
+
if (typeof createImageBitmap !== "function" || typeof OffscreenCanvas === "undefined") return null;
|
|
15
|
+
let bitmap: ImageBitmap;
|
|
16
|
+
try {
|
|
17
|
+
bitmap = await createImageBitmap(file);
|
|
18
|
+
} catch {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
try {
|
|
22
|
+
const longest = Math.max(bitmap.width, bitmap.height);
|
|
23
|
+
if (longest <= MODEL_IMAGE_EDGE && file.size <= SMALL_ENOUGH_BYTES) return null;
|
|
24
|
+
const scale = Math.min(1, MODEL_IMAGE_EDGE / longest);
|
|
25
|
+
const width = Math.max(1, Math.round(bitmap.width * scale));
|
|
26
|
+
const height = Math.max(1, Math.round(bitmap.height * scale));
|
|
27
|
+
const canvas = new OffscreenCanvas(width, height);
|
|
28
|
+
const context = canvas.getContext("2d");
|
|
29
|
+
if (!context) return null;
|
|
30
|
+
context.drawImage(bitmap, 0, 0, width, height);
|
|
31
|
+
// Safari cannot encode WebP and hands back PNG; JPEG keeps the copy small there.
|
|
32
|
+
const webp = await canvas.convertToBlob({ type: "image/webp", quality: 0.85 });
|
|
33
|
+
if (webp.type === "image/webp") return webp;
|
|
34
|
+
const jpeg = await canvas.convertToBlob({ type: "image/jpeg", quality: 0.85 });
|
|
35
|
+
return jpeg.type === "image/jpeg" ? jpeg : null;
|
|
36
|
+
} catch {
|
|
37
|
+
return null;
|
|
38
|
+
} finally {
|
|
39
|
+
bitmap.close();
|
|
40
|
+
}
|
|
41
|
+
}
|