vzcode 2.35.0 → 2.36.0

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.
Files changed (44) hide show
  1. package/dist/assets/buildWorker-BZlya278.js +687 -0
  2. package/dist/assets/index-Cj-ERpBu.css +1 -0
  3. package/dist/assets/index-DD7vzp4U.js +364 -0
  4. package/dist/assets/{worker-D8p1JlHW.js → worker-1oQYaz-F.js} +251 -251
  5. package/dist/assets/worker-BfgPD8zR.js +125 -0
  6. package/dist/assets/{worker-k7HZ3t-V.js → worker-CoJaqtpi.js} +12 -12
  7. package/dist/index.html +2 -2
  8. package/dist/ot.js +180 -2
  9. package/dist/server/createServer.js +281 -0
  10. package/dist/server/index.js +6 -269
  11. package/package.json +14 -3
  12. package/src/client/CodeEditor/getOrCreateEditor.tsx +0 -14
  13. package/src/client/VZSidebar/AIChatWrapper.tsx +85 -0
  14. package/src/client/VZSidebar/VisualEditor/VisualEditor.tsx +13 -9
  15. package/src/client/VZSidebar/VisualEditor/colorUtils.ts +14 -0
  16. package/src/client/VZSidebar/index.tsx +2 -2
  17. package/src/client/useOpenDirectories.ts +0 -1
  18. package/src/llm-streaming-server/README.md +5 -3
  19. package/src/llm-streaming-ui/README.md +44 -42
  20. package/src/llm-streaming-ui/components/ChatInput.tsx +6 -19
  21. package/src/llm-streaming-ui/components/DiffView.tsx +6 -24
  22. package/src/llm-streaming-ui/components/FileEditingIndicator.tsx +1 -1
  23. package/src/llm-streaming-ui/components/Message.tsx +11 -27
  24. package/src/llm-streaming-ui/components/MessageList.tsx +15 -29
  25. package/src/llm-streaming-ui/components/Spinner.scss +37 -0
  26. package/src/llm-streaming-ui/components/Spinner.tsx +59 -0
  27. package/src/llm-streaming-ui/components/bootstrap.ts +16 -0
  28. package/src/llm-streaming-ui/components/icons.tsx +68 -0
  29. package/src/llm-streaming-ui/components/index.tsx +34 -30
  30. package/src/llm-streaming-ui/index.ts +13 -0
  31. package/src/llm-streaming-ui/props.ts +137 -0
  32. package/src/ot.ts +221 -4
  33. package/src/server/createServer.ts +413 -0
  34. package/src/server/index.ts +8 -333
  35. package/dist/assets/buildWorker-DylWm2VX.js +0 -687
  36. package/dist/assets/index-Dyt9tpmu.css +0 -1
  37. package/dist/assets/index-rg2Zvfo_.js +0 -364
  38. package/dist/assets/worker-b9bkZkXK.js +0 -116
  39. package/src/client/AIAssist/AIAssistCodeMirrorKeyMap.ts +0 -42
  40. package/src/client/AIAssist/generateFilesContext.ts +0 -25
  41. package/src/client/AIAssist/index.ts +0 -2
  42. package/src/client/CodeEditor/InteractiveWidgets/highlightWidgets.ts +0 -79
  43. /package/src/{client → llm-streaming-ui}/hooks/useAutoScroll.ts +0 -0
  44. /package/src/{client → llm-streaming-ui}/utils/scrollUtils.ts +0 -0
@@ -32,3 +32,16 @@ export { ChatInput } from './components/ChatInput.js';
32
32
 
33
33
  // Custom hooks
34
34
  export { useSpeechRecognition } from './components/useSpeechRecognition.js';
35
+ export { useAutoScroll } from './hooks/useAutoScroll.js';
36
+
37
+ // Public prop types
38
+ export type {
39
+ AIChatProps,
40
+ AIChatMode,
41
+ AdditionalWidgets,
42
+ ChatInputProps,
43
+ DiffViewProps,
44
+ HandleSendMessage,
45
+ MessageListProps,
46
+ MessageProps,
47
+ } from './props.js';
@@ -0,0 +1,137 @@
1
+ import type { ReactNode } from 'react';
2
+ import type {
3
+ VizContent,
4
+ VizChatMessage,
5
+ } from '@vizhub/viz-types';
6
+
7
+ /** Chat mode: ask a question, or request an edit. */
8
+ export type AIChatMode = 'ask' | 'edit';
9
+
10
+ /**
11
+ * Sends a chat message. Optionally accepts an explicit message to send
12
+ * (used by suggested prompts) and extra request options.
13
+ */
14
+ export type HandleSendMessage = (
15
+ messageToSend?: string,
16
+ options?: Record<string, string>,
17
+ ) => Promise<unknown>;
18
+
19
+ /**
20
+ * Host-provided renderer for extra widgets shown on completed AI
21
+ * messages (e.g. VizHub's "open visualization" actions).
22
+ */
23
+ export type AdditionalWidgets = (props: {
24
+ messageId: string;
25
+ chatId: string;
26
+ handleSendMessage?: HandleSendMessage;
27
+ }) => ReactNode;
28
+
29
+ /**
30
+ * Props for the main `AIChat` component.
31
+ *
32
+ * The component is fully controlled: it owns no application state and
33
+ * reads nothing from VZCode context. Wrap it with `AIChatWrapper` to
34
+ * bind these props to a host application's state.
35
+ */
36
+ export interface AIChatProps {
37
+ /** ShareDB content, used to read chats and resolve file links. */
38
+ content: VizContent | null;
39
+ /** Whether the chat input should steal focus. */
40
+ aiChatFocused?: boolean;
41
+ aiChatMode: AIChatMode;
42
+ setAIChatMode: (mode: AIChatMode) => void;
43
+ aiChatMessage: string;
44
+ setAIChatMessage: (message: string) => void;
45
+ aiErrorMessage: string | null;
46
+ setAIErrorMessage: (error: string | null) => void;
47
+ /** The chat currently being written to. */
48
+ currentChatId: string;
49
+ /** The chat selected for display (may be null). */
50
+ selectedChatId: string | null;
51
+ setSelectedChatId: (chatId: string | null) => void;
52
+ handleSendMessage: HandleSendMessage;
53
+ navigateMessageHistoryUp: () => void;
54
+ navigateMessageHistoryDown: () => void;
55
+ resetMessageHistoryNavigation: () => void;
56
+ /**
57
+ * Used to restore a prompt after a fork (e.g. VizHub). Returning null
58
+ * means there is nothing to restore.
59
+ */
60
+ getStoredAIPrompt?: () => {
61
+ prompt: string;
62
+ modelName: string;
63
+ } | null;
64
+ /** Called with a file name when a file link in a diff is clicked. */
65
+ onOpenFile?: (fileName: string) => void;
66
+ /** Extra widgets rendered on completed AI messages. */
67
+ additionalWidgets?: AdditionalWidgets;
68
+ /** Show the diff view for file edits. */
69
+ enableDiffView?: boolean;
70
+ /** Show the ask/edit mode toggle in the input. */
71
+ enableAskMode?: boolean;
72
+ /** Show the voice input microphone button. */
73
+ enableVoiceInput?: boolean;
74
+ /** Whether the minimal AI edit flow is enabled. */
75
+ enableMinimalEditFlow?: boolean;
76
+ }
77
+
78
+ /** Props for the chat input field. */
79
+ export interface ChatInputProps {
80
+ aiChatMessage: string;
81
+ setAIChatMessage: (message: string) => void;
82
+ onSendMessage: () => void;
83
+ focused: boolean;
84
+ aiChatMode: AIChatMode;
85
+ setAIChatMode: (mode: AIChatMode) => void;
86
+ navigateMessageHistoryUp: () => void;
87
+ navigateMessageHistoryDown: () => void;
88
+ resetMessageHistoryNavigation: () => void;
89
+ /** Show the ask/edit mode toggle. */
90
+ enableAskMode?: boolean;
91
+ /** Show the voice input microphone button. */
92
+ enableVoiceInput?: boolean;
93
+ }
94
+
95
+ /** Props for the unified diff view. */
96
+ export interface DiffViewProps {
97
+ diffData: import('../utils/fileDiff').UnifiedFilesDiff;
98
+ /** Called with a file name when the user clicks it in the diff. */
99
+ onOpenFile?: (fileName: string) => void;
100
+ }
101
+
102
+ /** Props for the scrollable message list. */
103
+ export interface MessageListProps {
104
+ messages: VizChatMessage[];
105
+ isLoading: boolean;
106
+ chatId?: string;
107
+ aiScratchpad?: string;
108
+ currentStatus?: string;
109
+ model?: string;
110
+ onNewEvent: (targetElement?: HTMLElement) => void;
111
+ onJumpToLatest: (targetElement?: HTMLElement) => void;
112
+ beforeRender: () => number;
113
+ afterRender: (prevScrollHeight: number) => void;
114
+ additionalWidgets?: AdditionalWidgets;
115
+ handleSendMessage?: HandleSendMessage;
116
+ enableDiffView?: boolean;
117
+ onOpenFile?: (fileName: string) => void;
118
+ }
119
+
120
+ /** Props for a single chat message. */
121
+ export interface MessageProps {
122
+ id: string;
123
+ role: 'user' | 'assistant';
124
+ content?: string;
125
+ timestamp: number;
126
+ events?: import('../types.js').StreamingEvent[];
127
+ isActive?: boolean;
128
+ chatId?: string;
129
+ showAdditionalWidgets?: boolean;
130
+ isStreaming?: boolean;
131
+ diffData?: import('../utils/fileDiff').UnifiedFilesDiff;
132
+ model?: string;
133
+ additionalWidgets?: AdditionalWidgets;
134
+ handleSendMessage?: HandleSendMessage;
135
+ enableDiffView?: boolean;
136
+ onOpenFile?: (fileName: string) => void;
137
+ }
package/src/ot.ts CHANGED
@@ -15,8 +15,6 @@ DiffMatchPatch.DIFF_DELETE = DIFF_DELETE;
15
15
  DiffMatchPatch.DIFF_INSERT = DIFF_INSERT;
16
16
  DiffMatchPatch.DIFF_EQUAL = DIFF_EQUAL;
17
17
 
18
- const diffMatchPatch = DiffMatchPatch;
19
-
20
18
  export const { json1Presence, textUnicode } =
21
19
  OTJSON1Presence;
22
20
 
@@ -26,11 +24,230 @@ export const otType = json1Presence.type;
26
24
  // Applies an OT op to an object.
27
25
  export const apply = otType.apply;
28
26
 
27
+ // ---------------------------------------------------------------------------
28
+ // Unicode-safe text diffing
29
+ // ---------------------------------------------------------------------------
30
+ //
31
+ // `json0-ot-diff` asks diff-match-patch for text patches and converts the
32
+ // resulting offsets into `textUnicode` (ot-text-unicode) operations. This is
33
+ // incorrect for text containing astral-plane characters (emoji, etc.):
34
+ //
35
+ // 1. diff-match-patch measures offsets in UTF-16 code units, while
36
+ // ot-text-unicode measures them in Unicode code points.
37
+ // 2. diff-match-patch can split a surrogate pair across diffs (it may treat
38
+ // the high and low halves of an emoji as independent characters).
39
+ //
40
+ // Both produce operations that either fail to apply ("The op is too long for
41
+ // this document", "Invalid offset - splits unicode bytes") or apply
42
+ // incorrectly.
43
+ //
44
+ // The fix: when either string contains astral characters, encode each astral
45
+ // code point as a single unused BMP placeholder before diffing. One UTF-16
46
+ // unit then equals one code point and surrogate pairs can never be split. The
47
+ // generated text operation is decoded back to the original characters.
48
+ // Strings without astral characters keep using the original (correct and
49
+ // faster) code path.
50
+
51
+ const hasSurrogates = (value: string): boolean =>
52
+ /[\uD800-\uDFFF]/.test(value);
53
+
54
+ const textDiffMatchPatch = new DiffMatchPatch();
55
+
56
+ // State describing the string currently being diffed by json0-ot-diff.
57
+ let currentOldValue = '';
58
+ let currentNewValue = '';
59
+ let pendingTextEdit = false;
60
+ let needsUnicodeSafeDiff = false;
61
+
62
+ interface AstralEncoding {
63
+ encodedOldValue: string;
64
+ encodedNewValue: string;
65
+ decode: (value: string) => string;
66
+ }
67
+
68
+ // `for...of` over a string iterates code points; an astral code point is
69
+ // represented as two UTF-16 code units (i.e. `char.length === 2`).
70
+ const encodeAstralCharacters = (
71
+ oldValue: string,
72
+ newValue: string,
73
+ ): AstralEncoding | null => {
74
+ if (
75
+ !hasSurrogates(oldValue) &&
76
+ !hasSurrogates(newValue)
77
+ ) {
78
+ return null;
79
+ }
80
+
81
+ // BMP characters already used by the input, so placeholders never collide.
82
+ const used = new Set<string>();
83
+ for (const char of oldValue) used.add(char);
84
+ for (const char of newValue) used.add(char);
85
+
86
+ const astralCharacters = new Set<string>();
87
+ for (const char of oldValue) {
88
+ if (char.length === 2) astralCharacters.add(char);
89
+ }
90
+ for (const char of newValue) {
91
+ if (char.length === 2) astralCharacters.add(char);
92
+ }
93
+
94
+ const astralToPlaceholder = new Map<string, string>();
95
+ const placeholderToAstral = new Map<string, string>();
96
+
97
+ // Assign each distinct astral code point an unused, non-surrogate BMP code
98
+ // point. The BMP has ~63k usable code points, far more than the number of
99
+ // distinct astral characters found in practice.
100
+ let candidate = 0xe000;
101
+ const nextPlaceholder = (): string => {
102
+ for (let attempts = 0; attempts < 0x10000; attempts++) {
103
+ if (candidate > 0xffff) candidate = 0x0020;
104
+ const placeholder = String.fromCharCode(candidate++);
105
+ if (!used.has(placeholder)) return placeholder;
106
+ }
107
+ throw new Error(
108
+ 'Unable to encode astral characters for diffing',
109
+ );
110
+ };
111
+
112
+ for (const astral of astralCharacters) {
113
+ const placeholder = nextPlaceholder();
114
+ astralToPlaceholder.set(astral, placeholder);
115
+ placeholderToAstral.set(placeholder, astral);
116
+ }
117
+
118
+ const encode = (value: string): string => {
119
+ let encoded = '';
120
+ for (const char of value) {
121
+ encoded +=
122
+ char.length === 2
123
+ ? (astralToPlaceholder.get(char) as string)
124
+ : char;
125
+ }
126
+ return encoded;
127
+ };
128
+
129
+ const decode = (value: string): string => {
130
+ let decoded = '';
131
+ for (const char of value) {
132
+ decoded += placeholderToAstral.get(char) ?? char;
133
+ }
134
+ return decoded;
135
+ };
136
+
137
+ return {
138
+ encodedOldValue: encode(oldValue),
139
+ encodedNewValue: encode(newValue),
140
+ decode,
141
+ };
142
+ };
143
+
144
+ // Builds an ot-text-unicode operation from diff-match-patch's diffs. Skips are
145
+ // counted in code points, which (after encoding) equal UTF-16 units.
146
+ const buildTextOp = (
147
+ oldValue: string,
148
+ newValue: string,
149
+ ): Array<unknown> => {
150
+ const encoding = encodeAstralCharacters(
151
+ oldValue,
152
+ newValue,
153
+ );
154
+ const oldText = encoding
155
+ ? encoding.encodedOldValue
156
+ : oldValue;
157
+ const newText = encoding
158
+ ? encoding.encodedNewValue
159
+ : newValue;
160
+ const decode = encoding
161
+ ? encoding.decode
162
+ : (value: string) => value;
163
+
164
+ const diffs = textDiffMatchPatch.diff_main(
165
+ oldText,
166
+ newText,
167
+ );
168
+
169
+ const components: Array<unknown> = [];
170
+ for (const [operation, text] of diffs) {
171
+ if (text.length === 0) continue;
172
+ if (operation === DIFF_EQUAL) {
173
+ components.push(text.length);
174
+ } else if (operation === DIFF_DELETE) {
175
+ components.push({ d: decode(text) });
176
+ } else if (operation === DIFF_INSERT) {
177
+ components.push(decode(text));
178
+ }
179
+ }
180
+
181
+ // Canonicalize: merge adjacent skips and drop a trailing skip, as required
182
+ // by ot-text-unicode's op validation.
183
+ const op: Array<unknown> = [];
184
+ for (const component of components) {
185
+ if (typeof component === 'number') {
186
+ if (component <= 0) continue;
187
+ const last = op[op.length - 1];
188
+ if (typeof last === 'number') {
189
+ op[op.length - 1] = last + component;
190
+ } else {
191
+ op.push(component);
192
+ }
193
+ } else {
194
+ op.push(component);
195
+ }
196
+ }
197
+ while (
198
+ op.length > 0 &&
199
+ typeof op[op.length - 1] === 'number'
200
+ ) {
201
+ op.pop();
202
+ }
203
+ return op;
204
+ };
205
+
206
+ class UnicodeSafeDiffMatchPatch extends DiffMatchPatch {
207
+ patch_make(a: any, b?: any): any {
208
+ currentOldValue = a;
209
+ currentNewValue = b;
210
+ pendingTextEdit = true;
211
+ needsUnicodeSafeDiff =
212
+ hasSurrogates(a) || hasSurrogates(b);
213
+ return b === undefined
214
+ ? super.patch_make(a)
215
+ : super.patch_make(a, b);
216
+ }
217
+ }
218
+
219
+ // Wrap the json1 type so text edits for strings containing astral characters
220
+ // are built by the unicode-safe routine above. All other operations (and text
221
+ // without astral characters) delegate unchanged.
222
+ const unicodeSafeJson1 = {
223
+ ...json1Presence,
224
+ editOp: (
225
+ path: Array<string | number>,
226
+ type: unknown,
227
+ op: unknown,
228
+ ) => {
229
+ if (type === textUnicode.type && needsUnicodeSafeDiff) {
230
+ if (pendingTextEdit) {
231
+ pendingTextEdit = false;
232
+ return json1Presence.editOp(
233
+ path,
234
+ type,
235
+ buildTextOp(currentOldValue, currentNewValue),
236
+ );
237
+ }
238
+ // The full text op was already emitted for this string; the
239
+ // remaining edits reported by json0-ot-diff are redundant.
240
+ return json1Presence.editOp(path, type, []);
241
+ }
242
+ return json1Presence.editOp(path, type, op);
243
+ },
244
+ };
245
+
29
246
  export const diff = (a, b) =>
30
247
  jsondiff(
31
248
  a,
32
249
  b,
33
- diffMatchPatch,
34
- json1Presence,
250
+ UnicodeSafeDiffMatchPatch,
251
+ unicodeSafeJson1,
35
252
  textUnicode,
36
253
  );