@assistant-ui/react 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/index.d.mts +41 -15
- package/dist/index.d.ts +41 -15
- package/dist/index.js +291 -259
- package/dist/index.mjs +279 -247
- package/package.json +2 -1
package/dist/index.js
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
"use strict";
|
2
|
+
var __create = Object.create;
|
2
3
|
var __defProp = Object.defineProperty;
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
4
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
5
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
6
8
|
var __export = (target, all) => {
|
7
9
|
for (var name in all)
|
@@ -15,18 +17,34 @@ var __copyProps = (to, from, except, desc) => {
|
|
15
17
|
}
|
16
18
|
return to;
|
17
19
|
};
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
26
|
+
mod
|
27
|
+
));
|
18
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
19
29
|
|
20
30
|
// src/index.ts
|
21
31
|
var src_exports = {};
|
22
32
|
__export(src_exports, {
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
33
|
+
ActionBarPrimitive: () => actionBar_exports,
|
34
|
+
BranchPickerPrimitive: () => branchPicker_exports,
|
35
|
+
ComposerPrimitive: () => composer_exports,
|
36
|
+
EditBarPrimitive: () => editBar_exports,
|
37
|
+
MessagePrimitive: () => message_exports,
|
38
|
+
ThreadPrimitive: () => thread_exports,
|
39
|
+
VercelAIThreadProvider: () => VercelAIThreadProvider,
|
40
|
+
unstable_useMessageContext: () => useMessageContext,
|
41
|
+
useBeginMessageEdit: () => useBeginMessageEdit,
|
42
|
+
useCancelMessageEdit: () => useCancelMessageEdit,
|
43
|
+
useCopyMessage: () => useCopyMessage,
|
44
|
+
useGoToNextBranch: () => useGoToNextBranch,
|
45
|
+
useGoToPreviousBranch: () => useGoToPreviousBranch,
|
46
|
+
useReloadMessage: () => useReloadMessage,
|
47
|
+
useSaveMessageEdit: () => useSaveMessageEdit
|
30
48
|
});
|
31
49
|
module.exports = __toCommonJS(src_exports);
|
32
50
|
|
@@ -36,131 +54,18 @@ __export(thread_exports, {
|
|
36
54
|
Empty: () => ThreadEmpty,
|
37
55
|
If: () => ThreadIf,
|
38
56
|
Messages: () => ThreadMessages,
|
39
|
-
Provider: () => ThreadProvider,
|
40
57
|
Root: () => ThreadRoot,
|
41
58
|
Viewport: () => ThreadViewport
|
42
59
|
});
|
43
60
|
|
44
|
-
// src/
|
61
|
+
// src/primitives/thread/ThreadRoot.tsx
|
45
62
|
var import_react = require("react");
|
46
|
-
var
|
47
|
-
var
|
48
|
-
|
49
|
-
|
50
|
-
const child = messages[i];
|
51
|
-
const childId = child.id;
|
52
|
-
const parentId = messages[i - 1]?.id ?? ROOT_ID;
|
53
|
-
data.parentMap.set(childId, parentId);
|
54
|
-
let parentArray = data.branchMap.get(parentId);
|
55
|
-
if (!parentArray) {
|
56
|
-
data.branchMap.set(parentId, [childId]);
|
57
|
-
} else if (!parentArray.includes(childId)) {
|
58
|
-
parentArray.push(childId);
|
59
|
-
}
|
60
|
-
data.snapshots.set(childId, messages);
|
61
|
-
}
|
62
|
-
};
|
63
|
-
var getParentId = (data, messages, message) => {
|
64
|
-
if (message.id === UPCOMING_MESSAGE_ID) {
|
65
|
-
const parent = messages.at(-1);
|
66
|
-
if (!parent)
|
67
|
-
return ROOT_ID;
|
68
|
-
return parent.id;
|
63
|
+
var import_react_primitive = require("@radix-ui/react-primitive");
|
64
|
+
var ThreadRoot = (0, import_react.forwardRef)(
|
65
|
+
(props, ref) => {
|
66
|
+
return /* @__PURE__ */ React.createElement(import_react_primitive.Primitive.div, { ...props, ref });
|
69
67
|
}
|
70
|
-
|
71
|
-
if (!parentId)
|
72
|
-
throw new Error("Unexpected: Message parent not found");
|
73
|
-
return parentId;
|
74
|
-
};
|
75
|
-
var getBranchStateImpl = (data, messages, message) => {
|
76
|
-
const parentId = getParentId(data, messages, message);
|
77
|
-
const branches = data.branchMap.get(parentId) ?? [];
|
78
|
-
const branchId = message.id === UPCOMING_MESSAGE_ID ? branches.length : branches.indexOf(message.id);
|
79
|
-
if (branchId === -1)
|
80
|
-
throw new Error("Unexpected: Message not found in parent children");
|
81
|
-
const upcomingOffset = message.id === UPCOMING_MESSAGE_ID ? 1 : 0;
|
82
|
-
return {
|
83
|
-
branchId,
|
84
|
-
branchCount: branches.length + upcomingOffset
|
85
|
-
};
|
86
|
-
};
|
87
|
-
var switchToBranchImpl = (data, messages, message, branchId) => {
|
88
|
-
const parentId = getParentId(data, messages, message);
|
89
|
-
const branches = data.branchMap.get(parentId);
|
90
|
-
if (!branches)
|
91
|
-
throw new Error("Unexpected: Parent children not found");
|
92
|
-
const newMessageId = branches[branchId];
|
93
|
-
if (!newMessageId)
|
94
|
-
throw new Error("Unexpected: Requested branch not found");
|
95
|
-
if (branchId < 0 || branchId >= branches.length)
|
96
|
-
throw new Error("Switch to branch called with a branch index out of range");
|
97
|
-
if (newMessageId === message.id)
|
98
|
-
return messages;
|
99
|
-
const snapshot = data.snapshots.get(newMessageId);
|
100
|
-
if (!snapshot)
|
101
|
-
throw new Error("Unexpected: Branch snapshot not found");
|
102
|
-
return snapshot;
|
103
|
-
};
|
104
|
-
var sliceMessagesUntil = (messages, message) => {
|
105
|
-
if (message.id === UPCOMING_MESSAGE_ID)
|
106
|
-
return messages;
|
107
|
-
const messageIdx = messages.findIndex((m) => m.id === message.id);
|
108
|
-
if (messageIdx === -1)
|
109
|
-
throw new Error("Unexpected: Message not found");
|
110
|
-
return messages.slice(0, messageIdx);
|
111
|
-
};
|
112
|
-
var useChatWithBranches = (chat) => {
|
113
|
-
const data = (0, import_react.useRef)({
|
114
|
-
parentMap: /* @__PURE__ */ new Map(),
|
115
|
-
branchMap: /* @__PURE__ */ new Map(),
|
116
|
-
snapshots: /* @__PURE__ */ new Map()
|
117
|
-
}).current;
|
118
|
-
updateBranchData(data, chat.messages);
|
119
|
-
const getBranchState = (0, import_react.useCallback)(
|
120
|
-
(message) => {
|
121
|
-
return getBranchStateImpl(data, chat.messages, message);
|
122
|
-
},
|
123
|
-
[chat.messages]
|
124
|
-
);
|
125
|
-
const switchToBranch = (0, import_react.useCallback)(
|
126
|
-
(message, branchId) => {
|
127
|
-
const newMessages = switchToBranchImpl(
|
128
|
-
data,
|
129
|
-
chat.messages,
|
130
|
-
message,
|
131
|
-
branchId
|
132
|
-
);
|
133
|
-
chat.setMessages(newMessages);
|
134
|
-
},
|
135
|
-
[chat.messages, chat.setMessages]
|
136
|
-
);
|
137
|
-
const reloadAt = (0, import_react.useCallback)(
|
138
|
-
async (message) => {
|
139
|
-
const newMessages = sliceMessagesUntil(chat.messages, message);
|
140
|
-
chat.setMessages(newMessages);
|
141
|
-
await chat.reload();
|
142
|
-
},
|
143
|
-
[chat.messages, chat.setMessages, chat.reload]
|
144
|
-
);
|
145
|
-
const editAt = (0, import_react.useCallback)(
|
146
|
-
async (message, newMessage) => {
|
147
|
-
const newMessages = sliceMessagesUntil(chat.messages, message);
|
148
|
-
chat.setMessages(newMessages);
|
149
|
-
await chat.append(newMessage);
|
150
|
-
},
|
151
|
-
[chat.messages, chat.setMessages, chat.append]
|
152
|
-
);
|
153
|
-
return (0, import_react.useMemo)(
|
154
|
-
() => ({
|
155
|
-
...chat,
|
156
|
-
getBranchState,
|
157
|
-
switchToBranch,
|
158
|
-
editAt,
|
159
|
-
reloadAt
|
160
|
-
}),
|
161
|
-
[chat, getBranchState, switchToBranch, editAt, reloadAt]
|
162
|
-
);
|
163
|
-
};
|
68
|
+
);
|
164
69
|
|
165
70
|
// src/utils/context/createStoreContext.tsx
|
166
71
|
var import_react2 = require("react");
|
@@ -200,7 +105,9 @@ var createStoreContext = (providerName) => {
|
|
200
105
|
return () => listeners.delete(cb);
|
201
106
|
},
|
202
107
|
emit: () => {
|
203
|
-
|
108
|
+
for (const listener of listeners) {
|
109
|
+
listener();
|
110
|
+
}
|
204
111
|
},
|
205
112
|
snapshot: () => {
|
206
113
|
return state;
|
@@ -237,21 +144,6 @@ var createStoreContext = (providerName) => {
|
|
237
144
|
// src/utils/context/ThreadContext.ts
|
238
145
|
var [ThreadContextProvider, useThreadContext] = createStoreContext("Thread.Provider");
|
239
146
|
|
240
|
-
// src/primitives/thread/ThreadProvider.tsx
|
241
|
-
var ThreadProvider = ({ chat, children }) => {
|
242
|
-
const branches = useChatWithBranches(chat);
|
243
|
-
return /* @__PURE__ */ React.createElement(ThreadContextProvider, { chat: branches }, children);
|
244
|
-
};
|
245
|
-
|
246
|
-
// src/primitives/thread/ThreadRoot.tsx
|
247
|
-
var import_react3 = require("react");
|
248
|
-
var import_react_primitive = require("@radix-ui/react-primitive");
|
249
|
-
var ThreadRoot = (0, import_react3.forwardRef)(
|
250
|
-
(props, ref) => {
|
251
|
-
return /* @__PURE__ */ React.createElement(import_react_primitive.Primitive.div, { ...props, ref });
|
252
|
-
}
|
253
|
-
);
|
254
|
-
|
255
147
|
// src/primitives/thread/ThreadIf.tsx
|
256
148
|
var useThreadIf = (props) => {
|
257
149
|
return useThreadContext("Thread.If", (s) => {
|
@@ -278,51 +170,57 @@ var ThreadEmpty = ({ children }) => {
|
|
278
170
|
};
|
279
171
|
|
280
172
|
// src/primitives/thread/ThreadViewport.tsx
|
281
|
-
var
|
173
|
+
var import_react4 = require("react");
|
282
174
|
var import_react_primitive2 = require("@radix-ui/react-primitive");
|
283
175
|
var import_react_compose_refs = require("@radix-ui/react-compose-refs");
|
284
176
|
var import_primitive = require("@radix-ui/primitive");
|
285
177
|
|
286
178
|
// src/utils/hooks/useOnResizeContent.tsx
|
287
|
-
var
|
179
|
+
var import_react3 = require("react");
|
288
180
|
var useOnResizeContent = (ref, callback) => {
|
289
|
-
const callbackRef = (0,
|
181
|
+
const callbackRef = (0, import_react3.useRef)(callback);
|
290
182
|
callbackRef.current = callback;
|
291
|
-
(0,
|
183
|
+
(0, import_react3.useLayoutEffect)(() => {
|
292
184
|
const el = ref.current;
|
293
185
|
if (!el)
|
294
186
|
return;
|
295
187
|
const resizeObserver = new ResizeObserver(() => {
|
296
188
|
callbackRef.current();
|
189
|
+
console.log("resize observed");
|
297
190
|
});
|
298
191
|
const mutationObserver = new MutationObserver((mutations) => {
|
299
|
-
|
300
|
-
mutation.addedNodes
|
301
|
-
if (node instanceof
|
192
|
+
for (const mutation of mutations) {
|
193
|
+
for (const node of mutation.addedNodes) {
|
194
|
+
if (node instanceof Element) {
|
302
195
|
resizeObserver.observe(node);
|
303
196
|
}
|
304
|
-
}
|
305
|
-
mutation.removedNodes
|
306
|
-
if (node instanceof
|
197
|
+
}
|
198
|
+
for (const node of mutation.removedNodes) {
|
199
|
+
if (node instanceof Element) {
|
307
200
|
resizeObserver.unobserve(node);
|
308
201
|
}
|
309
|
-
}
|
310
|
-
}
|
202
|
+
}
|
203
|
+
}
|
311
204
|
callbackRef.current();
|
312
205
|
});
|
313
206
|
mutationObserver.observe(el, { childList: true });
|
207
|
+
for (const child of el.children) {
|
208
|
+
resizeObserver.observe(child);
|
209
|
+
console.log("observing child", child);
|
210
|
+
}
|
314
211
|
return () => {
|
212
|
+
console.log("disconnecting");
|
315
213
|
resizeObserver.disconnect();
|
316
214
|
mutationObserver.disconnect();
|
317
215
|
};
|
318
|
-
}, []);
|
216
|
+
}, [ref.current]);
|
319
217
|
};
|
320
218
|
|
321
219
|
// src/primitives/thread/ThreadViewport.tsx
|
322
|
-
var ThreadViewport = (0,
|
323
|
-
const divRef = (0,
|
220
|
+
var ThreadViewport = (0, import_react4.forwardRef)(({ onScroll, children, ...rest }, forwardedRef) => {
|
221
|
+
const divRef = (0, import_react4.useRef)(null);
|
324
222
|
const ref = (0, import_react_compose_refs.useComposedRefs)(forwardedRef, divRef);
|
325
|
-
const [isAtBottom, setIsAtBottom] = (0,
|
223
|
+
const [isAtBottom, setIsAtBottom] = (0, import_react4.useState)(true);
|
326
224
|
useOnResizeContent(divRef, () => {
|
327
225
|
const div = divRef.current;
|
328
226
|
if (!div || !isAtBottom)
|
@@ -333,8 +231,9 @@ var ThreadViewport = (0, import_react5.forwardRef)(({ onScroll, children, ...res
|
|
333
231
|
const div = divRef.current;
|
334
232
|
if (!div)
|
335
233
|
return;
|
336
|
-
setIsAtBottom(div.scrollHeight - div.scrollTop <= div.clientHeight);
|
234
|
+
setIsAtBottom(div.scrollHeight - div.scrollTop <= div.clientHeight + 50);
|
337
235
|
};
|
236
|
+
console.log(isAtBottom);
|
338
237
|
return /* @__PURE__ */ React.createElement(
|
339
238
|
import_react_primitive2.Primitive.div,
|
340
239
|
{
|
@@ -346,6 +245,127 @@ var ThreadViewport = (0, import_react5.forwardRef)(({ onScroll, children, ...res
|
|
346
245
|
);
|
347
246
|
});
|
348
247
|
|
248
|
+
// src/utils/hooks/useBranches.tsx
|
249
|
+
var import_react5 = require("react");
|
250
|
+
var ROOT_ID = "__ROOT_ID__";
|
251
|
+
var UPCOMING_MESSAGE_ID = "__UPCOMING_MESSAGE_ID__";
|
252
|
+
var updateBranchData = (data, messages) => {
|
253
|
+
for (let i = 0; i < messages.length; i++) {
|
254
|
+
const child = messages[i];
|
255
|
+
const childId = child.id;
|
256
|
+
const parentId = messages[i - 1]?.id ?? ROOT_ID;
|
257
|
+
data.parentMap.set(childId, parentId);
|
258
|
+
const parentArray = data.branchMap.get(parentId);
|
259
|
+
if (!parentArray) {
|
260
|
+
data.branchMap.set(parentId, [childId]);
|
261
|
+
} else if (!parentArray.includes(childId)) {
|
262
|
+
parentArray.push(childId);
|
263
|
+
}
|
264
|
+
data.snapshots.set(childId, messages);
|
265
|
+
}
|
266
|
+
};
|
267
|
+
var getParentId = (data, messages, message) => {
|
268
|
+
if (message.id === UPCOMING_MESSAGE_ID) {
|
269
|
+
const parent = messages.at(-1);
|
270
|
+
if (!parent)
|
271
|
+
return ROOT_ID;
|
272
|
+
return parent.id;
|
273
|
+
}
|
274
|
+
const parentId = data.parentMap.get(message.id);
|
275
|
+
if (!parentId)
|
276
|
+
throw new Error("Unexpected: Message parent not found");
|
277
|
+
return parentId;
|
278
|
+
};
|
279
|
+
var getBranchStateImpl = (data, messages, message) => {
|
280
|
+
const parentId = getParentId(data, messages, message);
|
281
|
+
const branches = data.branchMap.get(parentId) ?? [];
|
282
|
+
const branchId = message.id === UPCOMING_MESSAGE_ID ? branches.length : branches.indexOf(message.id);
|
283
|
+
if (branchId === -1)
|
284
|
+
throw new Error("Unexpected: Message not found in parent children");
|
285
|
+
const upcomingOffset = message.id === UPCOMING_MESSAGE_ID ? 1 : 0;
|
286
|
+
return {
|
287
|
+
branchId,
|
288
|
+
branchCount: branches.length + upcomingOffset
|
289
|
+
};
|
290
|
+
};
|
291
|
+
var switchToBranchImpl = (data, messages, message, branchId) => {
|
292
|
+
const parentId = getParentId(data, messages, message);
|
293
|
+
const branches = data.branchMap.get(parentId);
|
294
|
+
if (!branches)
|
295
|
+
throw new Error("Unexpected: Parent children not found");
|
296
|
+
const newMessageId = branches[branchId];
|
297
|
+
if (!newMessageId)
|
298
|
+
throw new Error("Unexpected: Requested branch not found");
|
299
|
+
if (branchId < 0 || branchId >= branches.length)
|
300
|
+
throw new Error("Switch to branch called with a branch index out of range");
|
301
|
+
if (newMessageId === message.id)
|
302
|
+
return messages;
|
303
|
+
const snapshot = data.snapshots.get(newMessageId);
|
304
|
+
if (!snapshot)
|
305
|
+
throw new Error("Unexpected: Branch snapshot not found");
|
306
|
+
return snapshot;
|
307
|
+
};
|
308
|
+
var sliceMessagesUntil = (messages, message) => {
|
309
|
+
if (message.id === UPCOMING_MESSAGE_ID)
|
310
|
+
return messages;
|
311
|
+
const messageIdx = messages.findIndex((m) => m.id === message.id);
|
312
|
+
if (messageIdx === -1)
|
313
|
+
throw new Error("Unexpected: Message not found");
|
314
|
+
return messages.slice(0, messageIdx);
|
315
|
+
};
|
316
|
+
var useChatWithBranches = (chat) => {
|
317
|
+
const data = (0, import_react5.useRef)({
|
318
|
+
parentMap: /* @__PURE__ */ new Map(),
|
319
|
+
branchMap: /* @__PURE__ */ new Map(),
|
320
|
+
snapshots: /* @__PURE__ */ new Map()
|
321
|
+
}).current;
|
322
|
+
updateBranchData(data, chat.messages);
|
323
|
+
const getBranchState = (0, import_react5.useCallback)(
|
324
|
+
(message) => {
|
325
|
+
return getBranchStateImpl(data, chat.messages, message);
|
326
|
+
},
|
327
|
+
[data, chat.messages]
|
328
|
+
);
|
329
|
+
const switchToBranch = (0, import_react5.useCallback)(
|
330
|
+
(message, branchId) => {
|
331
|
+
const newMessages = switchToBranchImpl(
|
332
|
+
data,
|
333
|
+
chat.messages,
|
334
|
+
message,
|
335
|
+
branchId
|
336
|
+
);
|
337
|
+
chat.setMessages(newMessages);
|
338
|
+
},
|
339
|
+
[data, chat.messages, chat.setMessages]
|
340
|
+
);
|
341
|
+
const reloadAt = (0, import_react5.useCallback)(
|
342
|
+
async (message) => {
|
343
|
+
const newMessages = sliceMessagesUntil(chat.messages, message);
|
344
|
+
chat.setMessages(newMessages);
|
345
|
+
await chat.reload();
|
346
|
+
},
|
347
|
+
[chat.messages, chat.setMessages, chat.reload]
|
348
|
+
);
|
349
|
+
const editAt = (0, import_react5.useCallback)(
|
350
|
+
async (message, newMessage) => {
|
351
|
+
const newMessages = sliceMessagesUntil(chat.messages, message);
|
352
|
+
chat.setMessages(newMessages);
|
353
|
+
await chat.append(newMessage);
|
354
|
+
},
|
355
|
+
[chat.messages, chat.setMessages, chat.append]
|
356
|
+
);
|
357
|
+
return (0, import_react5.useMemo)(
|
358
|
+
() => ({
|
359
|
+
...chat,
|
360
|
+
getBranchState,
|
361
|
+
switchToBranch,
|
362
|
+
editAt,
|
363
|
+
reloadAt
|
364
|
+
}),
|
365
|
+
[chat, getBranchState, switchToBranch, editAt, reloadAt]
|
366
|
+
);
|
367
|
+
};
|
368
|
+
|
349
369
|
// src/primitives/message/index.ts
|
350
370
|
var message_exports = {};
|
351
371
|
__export(message_exports, {
|
@@ -466,35 +486,10 @@ var MessageContent = () => {
|
|
466
486
|
};
|
467
487
|
|
468
488
|
// src/primitives/message/MessageEditableContent.tsx
|
469
|
-
var import_react_compose_refs2 = require("@radix-ui/react-compose-refs");
|
470
|
-
var import_react9 = require("react");
|
471
|
-
|
472
|
-
// src/utils/hooks/useAutosize.tsx
|
473
489
|
var import_react8 = require("react");
|
474
|
-
var useAutosize = (ref) => {
|
475
|
-
const el = ref.current;
|
476
|
-
(0, import_react8.useLayoutEffect)(() => {
|
477
|
-
const el2 = ref.current;
|
478
|
-
if (!el2)
|
479
|
-
return;
|
480
|
-
const callback = () => {
|
481
|
-
el2.style.height = "0px";
|
482
|
-
el2.style.height = el2.scrollHeight + "px";
|
483
|
-
};
|
484
|
-
el2.addEventListener("input", callback);
|
485
|
-
callback();
|
486
|
-
return () => {
|
487
|
-
el2.removeEventListener("input", callback);
|
488
|
-
};
|
489
|
-
}, [ref, el]);
|
490
|
-
};
|
491
|
-
|
492
|
-
// src/primitives/message/MessageEditableContent.tsx
|
493
490
|
var import_primitive3 = require("@radix-ui/primitive");
|
494
|
-
var
|
495
|
-
|
496
|
-
const ref = (0, import_react_compose_refs2.useComposedRefs)(forwardedRef, textareaRef);
|
497
|
-
useAutosize(textareaRef);
|
491
|
+
var import_react_textarea_autosize = __toESM(require("react-textarea-autosize"));
|
492
|
+
var MessageEditableContent = (0, import_react8.forwardRef)(({ onChange, value, ...rest }, forwardedRef) => {
|
498
493
|
const [editState, setEditState] = useMessageContext(
|
499
494
|
"Message.EditableContent",
|
500
495
|
(s) => [s.editState, s.setEditState]
|
@@ -507,10 +502,10 @@ var MessageEditableContent = (0, import_react9.forwardRef)(({ onChange, value, .
|
|
507
502
|
"Message.EditableContent may only be rendered when edit mode is enabled. Consider wrapping the component in <Message.If editing>."
|
508
503
|
);
|
509
504
|
return /* @__PURE__ */ React.createElement(
|
510
|
-
|
505
|
+
import_react_textarea_autosize.default,
|
511
506
|
{
|
512
507
|
...rest,
|
513
|
-
ref,
|
508
|
+
ref: forwardedRef,
|
514
509
|
onChange: (0, import_primitive3.composeEventHandlers)(onChange, handleChange),
|
515
510
|
value: editState.value || value
|
516
511
|
}
|
@@ -518,15 +513,23 @@ var MessageEditableContent = (0, import_react9.forwardRef)(({ onChange, value, .
|
|
518
513
|
});
|
519
514
|
|
520
515
|
// src/primitives/thread/ThreadMessages.tsx
|
521
|
-
var
|
522
|
-
components
|
523
|
-
|
516
|
+
var getComponents = (components) => {
|
517
|
+
if ("Message" in components) {
|
518
|
+
return {
|
519
|
+
UserMessage: components.Message,
|
520
|
+
AssistantMessage: components.Message
|
521
|
+
};
|
522
|
+
}
|
523
|
+
return components;
|
524
|
+
};
|
525
|
+
var ThreadMessages = ({ components }) => {
|
524
526
|
const chat = useThreadContext("Thread.Messages", (s) => s.chat);
|
525
527
|
const messages = chat.messages;
|
528
|
+
const { UserMessage, AssistantMessage } = getComponents(components);
|
526
529
|
if (messages.length === 0)
|
527
530
|
return null;
|
528
|
-
return /* @__PURE__ */ React.createElement(React.Fragment, null, messages.map((message
|
529
|
-
return /* @__PURE__ */ React.createElement(MessageProvider, { key:
|
531
|
+
return /* @__PURE__ */ React.createElement(React.Fragment, null, messages.map((message) => {
|
532
|
+
return /* @__PURE__ */ React.createElement(MessageProvider, { key: message.id, message }, message.role === "assistant" ? /* @__PURE__ */ React.createElement(AssistantMessage, null) : /* @__PURE__ */ React.createElement(UserMessage, null));
|
530
533
|
}), chat.isLoading && chat.messages[chat.messages.length - 1]?.role !== "assistant" && /* @__PURE__ */ React.createElement(
|
531
534
|
MessageProvider,
|
532
535
|
{
|
@@ -536,7 +539,7 @@ var ThreadMessages = ({
|
|
536
539
|
content: "..."
|
537
540
|
}
|
538
541
|
},
|
539
|
-
/* @__PURE__ */ React.createElement(
|
542
|
+
/* @__PURE__ */ React.createElement(AssistantMessage, null)
|
540
543
|
));
|
541
544
|
};
|
542
545
|
|
@@ -550,13 +553,13 @@ __export(composer_exports, {
|
|
550
553
|
});
|
551
554
|
|
552
555
|
// src/primitives/composer/ComposerRoot.tsx
|
553
|
-
var
|
556
|
+
var import_react9 = require("react");
|
554
557
|
var import_react_primitive4 = require("@radix-ui/react-primitive");
|
555
558
|
var import_primitive4 = require("@radix-ui/primitive");
|
556
|
-
var
|
557
|
-
var ComposerContext = (0,
|
559
|
+
var import_react_compose_refs2 = require("@radix-ui/react-compose-refs");
|
560
|
+
var ComposerContext = (0, import_react9.createContext)(null);
|
558
561
|
var useComposerContext = () => {
|
559
|
-
const context = (0,
|
562
|
+
const context = (0, import_react9.useContext)(ComposerContext);
|
560
563
|
if (!context) {
|
561
564
|
throw new Error(
|
562
565
|
"Composer compound components cannot be rendered outside the Composer component"
|
@@ -564,15 +567,15 @@ var useComposerContext = () => {
|
|
564
567
|
}
|
565
568
|
return context;
|
566
569
|
};
|
567
|
-
var ComposerRoot = (0,
|
570
|
+
var ComposerRoot = (0, import_react9.forwardRef)(
|
568
571
|
({ onSubmit, ...rest }, forwardedRef) => {
|
569
572
|
const handleSubmit = useThreadContext(
|
570
573
|
"Composer.Root",
|
571
574
|
(s) => s.chat.handleSubmit
|
572
575
|
);
|
573
|
-
const formRef = (0,
|
574
|
-
const ref = (0,
|
575
|
-
const composerContextValue = (0,
|
576
|
+
const formRef = (0, import_react9.useRef)(null);
|
577
|
+
const ref = (0, import_react_compose_refs2.useComposedRefs)(forwardedRef, formRef);
|
578
|
+
const composerContextValue = (0, import_react9.useMemo)(
|
576
579
|
() => ({
|
577
580
|
submit: () => formRef.current?.dispatchEvent(
|
578
581
|
new Event("submit", { cancelable: true, bubbles: true })
|
@@ -592,11 +595,11 @@ var ComposerRoot = (0, import_react10.forwardRef)(
|
|
592
595
|
);
|
593
596
|
|
594
597
|
// src/primitives/composer/ComposerInput.tsx
|
595
|
-
var
|
598
|
+
var import_react10 = require("react");
|
596
599
|
var import_react_slot = require("@radix-ui/react-slot");
|
597
600
|
var import_primitive5 = require("@radix-ui/primitive");
|
598
|
-
var
|
599
|
-
var ComposerInput = (0,
|
601
|
+
var import_react_textarea_autosize2 = __toESM(require("react-textarea-autosize"));
|
602
|
+
var ComposerInput = (0, import_react10.forwardRef)(({ asChild, onChange, onKeyDown, ...rest }, forwardedRef) => {
|
600
603
|
const chat = useThreadContext(
|
601
604
|
"Composer.Input",
|
602
605
|
({ chat: { input, handleInputChange, isLoading } }) => ({
|
@@ -605,15 +608,12 @@ var ComposerInput = (0, import_react11.forwardRef)(({ asChild, onChange, onKeyDo
|
|
605
608
|
isLoading
|
606
609
|
})
|
607
610
|
);
|
608
|
-
const Component = asChild ? import_react_slot.Slot :
|
609
|
-
const textareaRef = (0, import_react11.useRef)(null);
|
610
|
-
const ref = (0, import_react_compose_refs4.useComposedRefs)(forwardedRef, textareaRef);
|
611
|
-
useAutosize(textareaRef);
|
611
|
+
const Component = asChild ? import_react_slot.Slot : import_react_textarea_autosize2.default;
|
612
612
|
const composer = useComposerContext();
|
613
613
|
const handleKeyPress = (e) => {
|
614
614
|
if (chat.isLoading || rest.disabled)
|
615
615
|
return;
|
616
|
-
if (e.key === "Enter" && e.shiftKey
|
616
|
+
if (e.key === "Enter" && e.shiftKey === false) {
|
617
617
|
e.preventDefault();
|
618
618
|
composer.submit();
|
619
619
|
}
|
@@ -623,7 +623,7 @@ var ComposerInput = (0, import_react11.forwardRef)(({ asChild, onChange, onKeyDo
|
|
623
623
|
{
|
624
624
|
value: chat.input,
|
625
625
|
...rest,
|
626
|
-
ref,
|
626
|
+
ref: forwardedRef,
|
627
627
|
onChange: (0, import_primitive5.composeEventHandlers)(onChange, chat.handleInputChange),
|
628
628
|
onKeyDown: (0, import_primitive5.composeEventHandlers)(onKeyDown, handleKeyPress)
|
629
629
|
}
|
@@ -631,9 +631,9 @@ var ComposerInput = (0, import_react11.forwardRef)(({ asChild, onChange, onKeyDo
|
|
631
631
|
});
|
632
632
|
|
633
633
|
// src/primitives/composer/ComposerSend.tsx
|
634
|
-
var
|
634
|
+
var import_react11 = require("react");
|
635
635
|
var import_react_primitive5 = require("@radix-ui/react-primitive");
|
636
|
-
var ComposerSend = (0,
|
636
|
+
var ComposerSend = (0, import_react11.forwardRef)(
|
637
637
|
({ disabled, ...rest }, ref) => {
|
638
638
|
const input = useThreadContext("Composer.Send", (s) => s.chat.input);
|
639
639
|
return /* @__PURE__ */ React.createElement(
|
@@ -649,11 +649,11 @@ var ComposerSend = (0, import_react12.forwardRef)(
|
|
649
649
|
);
|
650
650
|
|
651
651
|
// src/utils/createActionButton.tsx
|
652
|
-
var
|
652
|
+
var import_react12 = require("react");
|
653
653
|
var import_react_primitive6 = require("@radix-ui/react-primitive");
|
654
654
|
var import_primitive6 = require("@radix-ui/primitive");
|
655
655
|
var createActionButton = (useActionButton) => {
|
656
|
-
return (0,
|
656
|
+
return (0, import_react12.forwardRef)(
|
657
657
|
(props, forwardedRef) => {
|
658
658
|
const onClick = useActionButton(props);
|
659
659
|
return /* @__PURE__ */ React.createElement(
|
@@ -692,8 +692,8 @@ __export(branchPicker_exports, {
|
|
692
692
|
Root: () => BranchPickerRoot
|
693
693
|
});
|
694
694
|
|
695
|
-
// src/
|
696
|
-
var
|
695
|
+
// src/actions/useGoToNextBranch.tsx
|
696
|
+
var useGoToNextBranch = () => {
|
697
697
|
const switchToBranch = useThreadContext(
|
698
698
|
"BranchPicker.Next",
|
699
699
|
(s) => s.chat.switchToBranch
|
@@ -708,10 +708,12 @@ var useBranchPickerNext = () => {
|
|
708
708
|
switchToBranch(message, branchId + 1);
|
709
709
|
};
|
710
710
|
};
|
711
|
-
var BranchPickerNext = createActionButton(useBranchPickerNext);
|
712
711
|
|
713
|
-
// src/primitives/branchPicker/
|
714
|
-
var
|
712
|
+
// src/primitives/branchPicker/BranchPickerNext.tsx
|
713
|
+
var BranchPickerNext = createActionButton(useGoToNextBranch);
|
714
|
+
|
715
|
+
// src/actions/useGoToPreviousBranch.tsx
|
716
|
+
var useGoToPreviousBranch = () => {
|
715
717
|
const switchToBranch = useThreadContext(
|
716
718
|
"BranchPicker.Previous",
|
717
719
|
(s) => s.chat.switchToBranch
|
@@ -726,7 +728,9 @@ var useBranchPickerPrevious = () => {
|
|
726
728
|
switchToBranch(message, branchId - 1);
|
727
729
|
};
|
728
730
|
};
|
729
|
-
|
731
|
+
|
732
|
+
// src/primitives/branchPicker/BranchPickerPrevious.tsx
|
733
|
+
var BranchPickerPrevious = createActionButton(useGoToPreviousBranch);
|
730
734
|
|
731
735
|
// src/primitives/branchPicker/BranchPickerCount.tsx
|
732
736
|
var BranchPickerCount = () => {
|
@@ -747,10 +751,10 @@ var BranchPickerNumber = () => {
|
|
747
751
|
};
|
748
752
|
|
749
753
|
// src/primitives/branchPicker/BranchPickerRoot.tsx
|
750
|
-
var import_react14 = require("react");
|
751
754
|
var import_react_primitive7 = require("@radix-ui/react-primitive");
|
752
|
-
var
|
753
|
-
|
755
|
+
var import_react13 = require("react");
|
756
|
+
var BranchPickerRoot = (0, import_react13.forwardRef)(({ hideWhenSingleBranch, ...rest }, ref) => {
|
757
|
+
return /* @__PURE__ */ React.createElement(MessageIf, { hasBranches: hideWhenSingleBranch ? true : void 0 }, /* @__PURE__ */ React.createElement(import_react_primitive7.Primitive.div, { ...rest, ref }));
|
754
758
|
});
|
755
759
|
|
756
760
|
// src/primitives/actionBar/index.ts
|
@@ -763,16 +767,14 @@ __export(actionBar_exports, {
|
|
763
767
|
});
|
764
768
|
|
765
769
|
// src/primitives/actionBar/ActionBarRoot.tsx
|
766
|
-
var import_react15 = require("react");
|
767
770
|
var import_react_primitive8 = require("@radix-ui/react-primitive");
|
768
|
-
var
|
769
|
-
|
770
|
-
|
771
|
-
|
772
|
-
);
|
771
|
+
var import_react14 = require("react");
|
772
|
+
var ActionBarRoot = (0, import_react14.forwardRef)(({ hideWhenEditing = false, ...rest }, ref) => {
|
773
|
+
return /* @__PURE__ */ React.createElement(MessageIf, { editing: hideWhenEditing ? false : void 0 }, /* @__PURE__ */ React.createElement(import_react_primitive8.Primitive.div, { ...rest, ref }));
|
774
|
+
});
|
773
775
|
|
774
|
-
// src/
|
775
|
-
var
|
776
|
+
// src/actions/useCopyMessage.tsx
|
777
|
+
var useCopyMessage = ({ copiedDuration = 3e3 }) => {
|
776
778
|
const [messageContent, setIsCopied] = useMessageContext(
|
777
779
|
"ActionBar.Copy",
|
778
780
|
(s) => [s.message.content, s.setIsCopied]
|
@@ -783,20 +785,27 @@ var useActionBarCopy = ({ copiedDuration = 3e3 }) => {
|
|
783
785
|
setTimeout(() => setIsCopied(false), copiedDuration);
|
784
786
|
};
|
785
787
|
};
|
786
|
-
var ActionBarCopy = createActionButton(useActionBarCopy);
|
787
788
|
|
788
|
-
// src/primitives/actionBar/
|
789
|
-
var
|
790
|
-
|
789
|
+
// src/primitives/actionBar/ActionBarCopy.tsx
|
790
|
+
var ActionBarCopy = createActionButton(useCopyMessage);
|
791
|
+
|
792
|
+
// src/actions/useReloadMessage.tsx
|
793
|
+
var useReloadMessage = () => {
|
794
|
+
const [isLoading, reloadAt] = useThreadContext("ActionBar.Reload", (s) => [
|
795
|
+
s.chat.isLoading,
|
796
|
+
s.chat.reloadAt
|
797
|
+
]);
|
791
798
|
const message = useMessageContext("ActionBar.Reload", (s) => s.message);
|
792
|
-
if (message.role !== "assistant" ||
|
799
|
+
if (message.role !== "assistant" || isLoading)
|
793
800
|
return null;
|
794
|
-
return () =>
|
801
|
+
return () => reloadAt(message);
|
795
802
|
};
|
796
|
-
var ActionBarReload = createActionButton(useActionBarReload);
|
797
803
|
|
798
|
-
// src/primitives/actionBar/
|
799
|
-
var
|
804
|
+
// src/primitives/actionBar/ActionBarReload.tsx
|
805
|
+
var ActionBarReload = createActionButton(useReloadMessage);
|
806
|
+
|
807
|
+
// src/actions/useBeginMessageEdit.tsx
|
808
|
+
var useBeginMessageEdit = () => {
|
800
809
|
const [editState, messageContent, setEditState] = useMessageContext(
|
801
810
|
"ActionBar.Edit",
|
802
811
|
(s) => [s.editState, s.message.content, s.setEditState]
|
@@ -807,7 +816,9 @@ var useActionBarEdit = () => {
|
|
807
816
|
setEditState({ isEditing: true, value: messageContent });
|
808
817
|
};
|
809
818
|
};
|
810
|
-
|
819
|
+
|
820
|
+
// src/primitives/actionBar/ActionBarEdit.tsx
|
821
|
+
var ActionBarEdit = createActionButton(useBeginMessageEdit);
|
811
822
|
|
812
823
|
// src/primitives/editBar/index.ts
|
813
824
|
var editBar_exports = {};
|
@@ -818,16 +829,16 @@ __export(editBar_exports, {
|
|
818
829
|
});
|
819
830
|
|
820
831
|
// src/primitives/editBar/EditBarRoot.tsx
|
821
|
-
var import_react16 = require("react");
|
822
832
|
var import_react_primitive9 = require("@radix-ui/react-primitive");
|
823
|
-
var
|
824
|
-
|
825
|
-
|
833
|
+
var import_react15 = require("react");
|
834
|
+
var EditBarRoot = (0, import_react15.forwardRef)(
|
835
|
+
({ hideWhenNotEditing, ...rest }, ref) => {
|
836
|
+
return /* @__PURE__ */ React.createElement(MessageIf, { editing: hideWhenNotEditing ? true : void 0 }, /* @__PURE__ */ React.createElement(import_react_primitive9.Primitive.div, { ...rest, ref }));
|
826
837
|
}
|
827
838
|
);
|
828
839
|
|
829
|
-
// src/
|
830
|
-
var
|
840
|
+
// src/actions/useSaveMessageEdit.tsx
|
841
|
+
var useSaveMessageEdit = () => {
|
831
842
|
const chat = useThreadContext("EditBar.Save", (s) => s.chat);
|
832
843
|
const [editState, message, setEditState] = useMessageContext(
|
833
844
|
"EditBar.Save",
|
@@ -847,10 +858,12 @@ var useEditBarSave = () => {
|
|
847
858
|
setEditState({ isEditing: false });
|
848
859
|
};
|
849
860
|
};
|
850
|
-
var EditBarSave = createActionButton(useEditBarSave);
|
851
861
|
|
852
|
-
// src/primitives/editBar/
|
853
|
-
var
|
862
|
+
// src/primitives/editBar/EditBarSave.tsx
|
863
|
+
var EditBarSave = createActionButton(useSaveMessageEdit);
|
864
|
+
|
865
|
+
// src/actions/useCancelMessageEdit.tsx
|
866
|
+
var useCancelMessageEdit = () => {
|
854
867
|
const [isEditing, setEditState] = useMessageContext("EditBar.Cancel", (s) => [
|
855
868
|
s.editState.isEditing,
|
856
869
|
s.setEditState
|
@@ -861,14 +874,33 @@ var useEditBarCancel = () => {
|
|
861
874
|
setEditState({ isEditing: false });
|
862
875
|
};
|
863
876
|
};
|
864
|
-
|
877
|
+
|
878
|
+
// src/primitives/editBar/EditBarCancel.tsx
|
879
|
+
var EditBarCancel = createActionButton(useCancelMessageEdit);
|
880
|
+
|
881
|
+
// src/vercel/VercelAIThreadProvider.tsx
|
882
|
+
var VercelAIThreadProvider = ({
|
883
|
+
chat,
|
884
|
+
children
|
885
|
+
}) => {
|
886
|
+
const branches = useChatWithBranches(chat);
|
887
|
+
return /* @__PURE__ */ React.createElement(ThreadContextProvider, { chat: branches }, children);
|
888
|
+
};
|
865
889
|
// Annotate the CommonJS export names for ESM import in node:
|
866
890
|
0 && (module.exports = {
|
867
|
-
|
868
|
-
|
869
|
-
|
870
|
-
|
871
|
-
|
872
|
-
|
873
|
-
|
891
|
+
ActionBarPrimitive,
|
892
|
+
BranchPickerPrimitive,
|
893
|
+
ComposerPrimitive,
|
894
|
+
EditBarPrimitive,
|
895
|
+
MessagePrimitive,
|
896
|
+
ThreadPrimitive,
|
897
|
+
VercelAIThreadProvider,
|
898
|
+
unstable_useMessageContext,
|
899
|
+
useBeginMessageEdit,
|
900
|
+
useCancelMessageEdit,
|
901
|
+
useCopyMessage,
|
902
|
+
useGoToNextBranch,
|
903
|
+
useGoToPreviousBranch,
|
904
|
+
useReloadMessage,
|
905
|
+
useSaveMessageEdit
|
874
906
|
});
|