@steerable/agent-shell-web 0.12.1 → 0.12.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@steerable/agent-shell-web",
3
- "version": "0.12.1",
3
+ "version": "0.12.3",
4
4
  "type": "module",
5
5
  "description": "Steerable framework — product-neutral renderer SPA source for @steerable/agent-shell (React + Vite). Consumed by product web entry points via the `@/` alias (anchored at this package's src/) and the createProductViteConfig factory. Published as a source package (src/), plus the compiled vite.base config factory (vite.base.js + .d.ts) for Node-side config loading.",
6
6
  "license": "Apache-2.0",
@@ -45,8 +45,8 @@
45
45
  "react-router-dom": "^6.30.0",
46
46
  "rehype-raw": "^7.0.0",
47
47
  "remark-gfm": "^4.0.1",
48
- "@steerable/agent-protocol": "0.12.1",
49
- "@steerable/agent-ui": "0.12.1"
48
+ "@steerable/agent-protocol": "0.12.3",
49
+ "@steerable/agent-ui": "0.12.3"
50
50
  },
51
51
  "devDependencies": {
52
52
  "@tailwindcss/vite": "^4.3.0",
@@ -59,7 +59,7 @@
59
59
  "typescript": "^5.8.3",
60
60
  "vite": "^5.4.21",
61
61
  "vitest": "^3.2.4",
62
- "@steerable/pack-sdk": "0.12.1"
62
+ "@steerable/pack-sdk": "0.12.3"
63
63
  },
64
64
  "scripts": {
65
65
  "gen": "echo agent-shell-web",
@@ -868,6 +868,11 @@ export const ChatInput = forwardRef<ChatInputHandle, ChatInputProps>(
868
868
  // While focused, a matching value must not rewrite contenteditable DOM —
869
869
  // that clobber is what turns the first Pinyin letter into ASCII.
870
870
  const lastPushedValueRef = useRef(value);
871
+ /**
872
+ * The newest draft this composer has accepted. Edits composed from it stay
873
+ * correct when two land in the same React commit, before `value` updates.
874
+ */
875
+ const latestValueRef = useRef(value);
871
876
  // Chromium can fire `input` for the first Pinyin letter *before*
872
877
  // `compositionstart`. A pending sync is tagged with this generation so
873
878
  // compositionstart can cancel it. Use a macrotask (not a microtask):
@@ -1168,6 +1173,10 @@ export const ChatInput = forwardRef<ChatInputHandle, ChatInputProps>(
1168
1173
  const pendingCaretOffset = pendingCaretOffsetRef.current;
1169
1174
  const parentChangedValue = value !== lastPushedValueRef.current;
1170
1175
 
1176
+ // A change from the parent (a send clearing the draft, a restored draft)
1177
+ // replaces whatever edit was composed locally.
1178
+ if (parentChangedValue) latestValueRef.current = value;
1179
+
1171
1180
  if (focused && !parentChangedValue && pendingCaretOffset === null) {
1172
1181
  updateEditorHeight(editor);
1173
1182
  return;
@@ -1359,21 +1368,50 @@ export const ChatInput = forwardRef<ChatInputHandle, ChatInputProps>(
1359
1368
  actualOnFilesChange(actualFiles.filter((_, idx) => idx !== indexToRemove));
1360
1369
  };
1361
1370
 
1371
+ /**
1372
+ * Compose against the latest draft, not the props snapshot.
1373
+ *
1374
+ * Two pastes in one React commit both read the same `value`, so the second
1375
+ * one rebuilt the pre-first-paste text and collapsed to a no-op: six rapid
1376
+ * Cmd+V inserted four copies. `latestValueRef` tracks every accepted edit.
1377
+ */
1378
+ const applyEdit = (edit: (current: string) => { next: string; caret: number }) => {
1379
+ const { next, caret } = edit(latestValueRef.current);
1380
+ latestValueRef.current = next;
1381
+ pendingCaretOffsetRef.current = caret;
1382
+ onChange(next);
1383
+ syncMentionQuery(next, caret);
1384
+ };
1385
+
1386
+ /**
1387
+ * Caret to edit at. The DOM still holds the previous draft until React
1388
+ * renders the last edit, so its selection describes text this composer no
1389
+ * longer considers current; `pendingCaretOffsetRef` carries that edit's
1390
+ * caret instead.
1391
+ */
1392
+ const editCaret = (editor: HTMLDivElement): { start: number; end: number } => {
1393
+ if (getEditableText(editor) === latestValueRef.current) {
1394
+ return getSelectionOffsets(editor);
1395
+ }
1396
+ const caret = pendingCaretOffsetRef.current ?? latestValueRef.current.length;
1397
+ return { start: caret, end: caret };
1398
+ };
1399
+
1362
1400
  const replaceSelection = (insertText: string) => {
1363
1401
  const editor = editorRef.current;
1364
1402
  if (!editor) return;
1365
- const { start, end } = getSelectionOffsets(editor);
1366
- const nextValue = `${value.slice(0, start)}${insertText}${value.slice(end)}`;
1367
- pendingCaretOffsetRef.current = start + insertText.length;
1368
- onChange(nextValue);
1369
- syncMentionQuery(nextValue, pendingCaretOffsetRef.current);
1403
+ const { start, end } = editCaret(editor);
1404
+ applyEdit((current) => ({
1405
+ next: `${current.slice(0, start)}${insertText}${current.slice(end)}`,
1406
+ caret: start + insertText.length,
1407
+ }));
1370
1408
  };
1371
1409
 
1372
1410
  const replaceRange = (start: number, end: number, insertText: string) => {
1373
- const nextValue = `${value.slice(0, start)}${insertText}${value.slice(end)}`;
1374
- pendingCaretOffsetRef.current = start + insertText.length;
1375
- onChange(nextValue);
1376
- syncMentionQuery(nextValue, pendingCaretOffsetRef.current);
1411
+ applyEdit((current) => ({
1412
+ next: `${current.slice(0, start)}${insertText}${current.slice(end)}`,
1413
+ caret: start + insertText.length,
1414
+ }));
1377
1415
  };
1378
1416
 
1379
1417
  const removeMentionToken = (start: number, end: number, ref?: MentionReference) => {
@@ -1442,6 +1480,7 @@ export const ChatInput = forwardRef<ChatInputHandle, ChatInputProps>(
1442
1480
  const { end } = getSelectionOffsets(editor);
1443
1481
  const nextValue = getEditableText(editor);
1444
1482
  lastPushedValueRef.current = nextValue;
1483
+ latestValueRef.current = nextValue;
1445
1484
  // The browser already rendered this text. Rewrite the DOM only when a
1446
1485
  // typed token must become a mention chip; any other rewrite detaches the
1447
1486
  // input method from the text node.
@@ -12,10 +12,24 @@ import {
12
12
 
13
13
  export type { HostClipboardFile } from './host-bridge';
14
14
 
15
- let pending = false;
16
15
  let lastEditable: HTMLElement | null = null;
17
16
  let composerPasteTarget: HTMLElement | null = null;
18
17
  let installed = false;
18
+ /**
19
+ * Pastes run one at a time so two reads cannot interleave, but a paste that
20
+ * arrives during a read is queued instead of dropped: dropping it loses the
21
+ * keystroke without any signal (a screenshot read used to take a second).
22
+ */
23
+ let pasteQueue: Promise<void> = Promise.resolve();
24
+
25
+ /**
26
+ * Content whose send remounts the composer. The keystroke was aimed at the
27
+ * field being replaced, so it waits for the replacement instead of being
28
+ * dropped. It expires so a stale pasteboard never lands in a later
29
+ * conversation.
30
+ */
31
+ let heldForComposer: { clipboard: HostClipboard; until: number } | null = null;
32
+ const HELD_PASTE_TTL_MS = 2_000;
19
33
 
20
34
  /**
21
35
  * Remember the live chat composer. A menu paste uses it when no other field
@@ -23,6 +37,10 @@ let installed = false;
23
37
  */
24
38
  export function setComposerPasteTarget(element: HTMLElement): void {
25
39
  composerPasteTarget = element;
40
+ const held = heldForComposer;
41
+ heldForComposer = null;
42
+ if (held === null || Date.now() > held.until) return;
43
+ insertHostClipboard(held.clipboard, element);
26
44
  }
27
45
 
28
46
  /** Drop the composer target when that element unmounts. */
@@ -30,7 +48,14 @@ export function releaseComposerPasteTarget(element: HTMLElement): void {
30
48
  if (composerPasteTarget === element) composerPasteTarget = null;
31
49
  }
32
50
 
33
- /** Remember the field that should receive a menu paste after focus moves. */
51
+ /**
52
+ * Remember the field that should receive a menu paste after focus moves.
53
+ *
54
+ * `lastEditable` exists for the menu shortcut: opening the app menu takes key
55
+ * focus, and the paste still belongs to the field the user was in. It is
56
+ * dropped once focus lands on another control in the page, so a paste after
57
+ * the user clicked elsewhere does not go back to a field they left.
58
+ */
34
59
  export function installHostPaste(): void {
35
60
  if (installed || typeof document === 'undefined') return;
36
61
  installed = true;
@@ -38,29 +63,43 @@ export function installHostPaste(): void {
38
63
  const target = event.target;
39
64
  if (isEditable(target)) lastEditable = target;
40
65
  });
66
+ document.addEventListener('focusout', (event) => {
67
+ if (event.target !== lastEditable) return;
68
+ // A menu opens before focus settles, so decide on the next task.
69
+ setTimeout(() => {
70
+ if (lastEditable === null) return;
71
+ const active = document.activeElement;
72
+ const movedInsidePage = active instanceof HTMLElement && active !== document.body;
73
+ if (movedInsidePage && !isEditable(active)) lastEditable = null;
74
+ }, 0);
75
+ });
41
76
  const bridge = getHostBridge();
42
77
  bridge?.onMenuPaste?.((clipboard) => {
43
78
  insertHostClipboard(clipboard);
44
79
  });
45
80
  }
46
81
 
47
- /** Read the OS pasteboard and insert it into `target` or the focused field. */
82
+ /**
83
+ * Read the OS pasteboard and insert it into `target` or the focused field.
84
+ *
85
+ * Reads are serialised. A request that arrives while one is in flight is
86
+ * queued, not dropped; the pasteboard is read when its turn comes so it
87
+ * reflects what is current then.
88
+ */
48
89
  export function requestHostPaste(target?: HTMLElement | null): void {
49
90
  const bridge = getHostBridge();
50
91
  const readClipboard = bridge?.readClipboard;
51
92
  const readText = bridge?.readClipboardText;
52
- if ((!readClipboard && !readText) || pending) return;
53
- pending = true;
54
- void readHostClipboard(readClipboard, readText)
55
- .then((clip) => {
56
- insertHostClipboard(clip, target ?? null);
57
- })
58
- .catch((error: unknown) => {
59
- console.warn('[host-paste] clipboard read failed', error);
60
- })
61
- .finally(() => {
62
- pending = false;
63
- });
93
+ if (!readClipboard && !readText) return;
94
+ pasteQueue = pasteQueue.then(() =>
95
+ readHostClipboard(readClipboard, readText)
96
+ .then((clip) => {
97
+ insertHostClipboard(clip, target ?? null);
98
+ })
99
+ .catch((error: unknown) => {
100
+ console.warn('[host-paste] clipboard read failed', error);
101
+ }),
102
+ );
64
103
  }
65
104
 
66
105
  /**
@@ -95,8 +134,15 @@ export function hostClipboardAvailable(): boolean {
95
134
  }
96
135
 
97
136
  function insertHostClipboard(clipboard: HostClipboard, target: HTMLElement | null = null): void {
98
- if (clipboard.files.length > 0) insertHostFiles(clipboard.files, target);
99
- if (clipboard.text) insertHostText(clipboard.text, target);
137
+ if (target !== null && !target.isConnected) {
138
+ // Sending the first message remounts the composer mid-read.
139
+ heldForComposer = { clipboard, until: Date.now() + HELD_PASTE_TTL_MS };
140
+ return;
141
+ }
142
+ const el = target ?? focusedEditable();
143
+ if (el === null) return;
144
+ if (clipboard.files.length > 0) insertHostFiles(clipboard.files, el);
145
+ if (clipboard.text) insertHostText(clipboard.text, el);
100
146
  }
101
147
 
102
148
  function isEditable(target: EventTarget | null): target is HTMLElement {
@@ -124,17 +170,13 @@ function focusedEditable(): HTMLElement | null {
124
170
  return null;
125
171
  }
126
172
 
127
- function insertHostFiles(files: HostClipboardFile[], preferred: HTMLElement | null): void {
128
- const el = preferred?.isConnected ? preferred : focusedEditable();
129
- if (!el) return;
173
+ function insertHostFiles(files: HostClipboardFile[], el: HTMLElement): void {
130
174
  if (el.isContentEditable || el.getAttribute('contenteditable') === 'true') {
131
175
  el.dispatchEvent(new CustomEvent('hostpastefiles', { detail: files, cancelable: true }));
132
176
  }
133
177
  }
134
178
 
135
- function insertHostText(text: string, preferred: HTMLElement | null): void {
136
- const el = preferred?.isConnected ? preferred : focusedEditable();
137
- if (!el) return;
179
+ function insertHostText(text: string, el: HTMLElement): void {
138
180
  if (el instanceof HTMLInputElement || el instanceof HTMLTextAreaElement) {
139
181
  if (el.readOnly || el.disabled) return;
140
182
  writeFormValue(el, text);