@assistant-ui/react 0.1.1 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- package/package.json +6 -15
- package/dist/AssistantRuntime-BM_jVV3g.d.mts +0 -23
- package/dist/AssistantRuntime-CBMSAJqH.d.mts +0 -23
- package/dist/AssistantRuntime-C_BvM7ZT.d.ts +0 -23
- package/dist/AssistantRuntime-DELpXWfG.d.ts +0 -23
- package/dist/ModelConfigTypes-B9UY4zxv.d.mts +0 -55
- package/dist/ModelConfigTypes-B9UY4zxv.d.ts +0 -55
- package/dist/ModelConfigTypes-BF5HxVrH.d.mts +0 -55
- package/dist/ModelConfigTypes-BF5HxVrH.d.ts +0 -55
- package/dist/ModelConfigTypes-Cf3yjaDu.d.mts +0 -55
- package/dist/ModelConfigTypes-Cf3yjaDu.d.ts +0 -55
- package/dist/ModelConfigTypes-CzmXY3sn.d.mts +0 -55
- package/dist/ModelConfigTypes-CzmXY3sn.d.ts +0 -55
- package/dist/Thread-BMASJT4a.d.ts +0 -15
- package/dist/Thread-UEVsUmvl.d.mts +0 -15
- package/dist/chunk-3XZUKECF.mjs +0 -207
- package/dist/chunk-3XZUKECF.mjs.map +0 -1
- package/dist/chunk-4DQ2CIAD.mjs +0 -69
- package/dist/chunk-4DQ2CIAD.mjs.map +0 -1
- package/dist/chunk-5YONSDN4.mjs +0 -200
- package/dist/chunk-5YONSDN4.mjs.map +0 -1
- package/dist/chunk-CY4TTHR7.mjs +0 -76
- package/dist/chunk-CY4TTHR7.mjs.map +0 -1
- package/dist/chunk-DKAWDNW5.mjs +0 -22
- package/dist/chunk-DKAWDNW5.mjs.map +0 -1
- package/dist/chunk-GQKH2ADD.mjs +0 -165
- package/dist/chunk-GQKH2ADD.mjs.map +0 -1
- package/dist/chunk-J5LGTIGS.mjs +0 -10
- package/dist/chunk-J5LGTIGS.mjs.map +0 -1
- package/dist/chunk-NSBOH42A.mjs +0 -200
- package/dist/chunk-NSBOH42A.mjs.map +0 -1
- package/dist/chunk-X4HBDEFP.mjs +0 -30
- package/dist/chunk-X4HBDEFP.mjs.map +0 -1
- package/dist/experimental.d.mts +0 -86
- package/dist/experimental.d.ts +0 -86
- package/dist/experimental.js +0 -123
- package/dist/experimental.js.map +0 -1
- package/dist/experimental.mjs +0 -34
- package/dist/experimental.mjs.map +0 -1
- package/dist/index.d.mts +0 -289
- package/dist/index.d.ts +0 -289
- package/dist/index.js +0 -1500
- package/dist/index.js.map +0 -1
- package/dist/index.mjs +0 -1245
- package/dist/index.mjs.map +0 -1
- package/dist/internal-dlLjX30u.d.mts +0 -75
- package/dist/internal-dlLjX30u.d.ts +0 -75
- package/dist/internal.d.mts +0 -25
- package/dist/internal.d.ts +0 -25
- package/dist/internal.js +0 -225
- package/dist/internal.js.map +0 -1
- package/dist/internal.mjs +0 -10
- package/dist/internal.mjs.map +0 -1
package/dist/index.mjs
DELETED
@@ -1,1245 +0,0 @@
|
|
1
|
-
import {
|
2
|
-
MessageRepository,
|
3
|
-
ProxyConfigProvider,
|
4
|
-
generateId,
|
5
|
-
mergeModelConfigs
|
6
|
-
} from "./chunk-NSBOH42A.mjs";
|
7
|
-
import {
|
8
|
-
AssistantContext,
|
9
|
-
ContentPartContext,
|
10
|
-
MessageContext,
|
11
|
-
ThreadContext,
|
12
|
-
useComposerContext,
|
13
|
-
useContentPartContext,
|
14
|
-
useMessageContext,
|
15
|
-
useThreadContext
|
16
|
-
} from "./chunk-CY4TTHR7.mjs";
|
17
|
-
import {
|
18
|
-
__export
|
19
|
-
} from "./chunk-J5LGTIGS.mjs";
|
20
|
-
|
21
|
-
// src/actions/useCopyMessage.tsx
|
22
|
-
import { useCallback } from "react";
|
23
|
-
|
24
|
-
// src/utils/combined/useCombinedStore.ts
|
25
|
-
import { useMemo } from "react";
|
26
|
-
|
27
|
-
// src/utils/combined/createCombinedStore.ts
|
28
|
-
import { useSyncExternalStore } from "react";
|
29
|
-
var createCombinedStore = (stores) => {
|
30
|
-
const subscribe = (callback) => {
|
31
|
-
const unsubscribes = stores.map((store) => store.subscribe(callback));
|
32
|
-
return () => {
|
33
|
-
for (const unsub of unsubscribes) {
|
34
|
-
unsub();
|
35
|
-
}
|
36
|
-
};
|
37
|
-
};
|
38
|
-
return (selector) => {
|
39
|
-
const getSnapshot = () => selector(...stores.map((store) => store.getState()));
|
40
|
-
return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
|
41
|
-
};
|
42
|
-
};
|
43
|
-
|
44
|
-
// src/utils/combined/useCombinedStore.ts
|
45
|
-
var useCombinedStore = (stores, selector) => {
|
46
|
-
const useCombined = useMemo(() => createCombinedStore(stores), stores);
|
47
|
-
return useCombined(selector);
|
48
|
-
};
|
49
|
-
|
50
|
-
// src/utils/getMessageText.tsx
|
51
|
-
var getMessageText = (message) => {
|
52
|
-
const textParts = message.content.filter(
|
53
|
-
(part) => part.type === "text"
|
54
|
-
);
|
55
|
-
return textParts.map((part) => part.text).join("\n\n");
|
56
|
-
};
|
57
|
-
|
58
|
-
// src/actions/useCopyMessage.tsx
|
59
|
-
var useCopyMessage = ({ copiedDuration = 3e3 }) => {
|
60
|
-
const { useMessage, useComposer } = useMessageContext();
|
61
|
-
const hasCopyableContent = useCombinedStore(
|
62
|
-
[useMessage, useComposer],
|
63
|
-
(m, c) => {
|
64
|
-
return c.isEditing || m.message.content.some((c2) => c2.type === "text");
|
65
|
-
}
|
66
|
-
);
|
67
|
-
const callback = useCallback(() => {
|
68
|
-
const { isEditing, value: composerValue } = useComposer.getState();
|
69
|
-
const { message, setIsCopied } = useMessage.getState();
|
70
|
-
const valueToCopy = isEditing ? composerValue : getMessageText(message);
|
71
|
-
navigator.clipboard.writeText(valueToCopy);
|
72
|
-
setIsCopied(true);
|
73
|
-
setTimeout(() => setIsCopied(false), copiedDuration);
|
74
|
-
}, [useComposer, useMessage, copiedDuration]);
|
75
|
-
if (!hasCopyableContent) return null;
|
76
|
-
return callback;
|
77
|
-
};
|
78
|
-
|
79
|
-
// src/actions/useReloadMessage.tsx
|
80
|
-
import { useCallback as useCallback2 } from "react";
|
81
|
-
var useReloadMessage = () => {
|
82
|
-
const { useThread, useViewport } = useThreadContext();
|
83
|
-
const { useMessage } = useMessageContext();
|
84
|
-
const disabled = useCombinedStore(
|
85
|
-
[useThread, useMessage],
|
86
|
-
(t, m) => t.isRunning || m.message.role !== "assistant"
|
87
|
-
);
|
88
|
-
const callback = useCallback2(() => {
|
89
|
-
const { parentId } = useMessage.getState();
|
90
|
-
useThread.getState().startRun(parentId);
|
91
|
-
useViewport.getState().scrollToBottom();
|
92
|
-
}, [useMessage, useThread, useViewport]);
|
93
|
-
if (disabled) return null;
|
94
|
-
return callback;
|
95
|
-
};
|
96
|
-
|
97
|
-
// src/actions/useBeginMessageEdit.tsx
|
98
|
-
import { useCallback as useCallback3 } from "react";
|
99
|
-
var useBeginMessageEdit = () => {
|
100
|
-
const { useMessage, useComposer } = useMessageContext();
|
101
|
-
const disabled = useCombinedStore(
|
102
|
-
[useMessage, useComposer],
|
103
|
-
(m, c) => m.message.role !== "user" || c.isEditing
|
104
|
-
);
|
105
|
-
const callback = useCallback3(() => {
|
106
|
-
const { edit } = useComposer.getState();
|
107
|
-
edit();
|
108
|
-
}, [useComposer]);
|
109
|
-
if (disabled) return null;
|
110
|
-
return callback;
|
111
|
-
};
|
112
|
-
|
113
|
-
// src/actions/useGoToNextBranch.tsx
|
114
|
-
import { useCallback as useCallback4 } from "react";
|
115
|
-
var useGoToNextBranch = () => {
|
116
|
-
const { useThread } = useThreadContext();
|
117
|
-
const { useMessage, useComposer } = useMessageContext();
|
118
|
-
const disabled = useCombinedStore(
|
119
|
-
[useMessage, useComposer],
|
120
|
-
(m, c) => c.isEditing || m.branches.indexOf(m.message.id) + 1 >= m.branches.length
|
121
|
-
);
|
122
|
-
const callback = useCallback4(() => {
|
123
|
-
const { message, branches } = useMessage.getState();
|
124
|
-
useThread.getState().switchToBranch(branches[branches.indexOf(message.id) + 1]);
|
125
|
-
}, [useMessage, useThread]);
|
126
|
-
if (disabled) return null;
|
127
|
-
return callback;
|
128
|
-
};
|
129
|
-
|
130
|
-
// src/actions/useGoToPreviousBranch.tsx
|
131
|
-
import { useCallback as useCallback5 } from "react";
|
132
|
-
var useGoToPreviousBranch = () => {
|
133
|
-
const { useThread } = useThreadContext();
|
134
|
-
const { useMessage, useComposer } = useMessageContext();
|
135
|
-
const disabled = useCombinedStore(
|
136
|
-
[useMessage, useComposer],
|
137
|
-
(m, c) => c.isEditing || m.branches.indexOf(m.message.id) <= 0
|
138
|
-
);
|
139
|
-
const callback = useCallback5(() => {
|
140
|
-
const { message, branches } = useMessage.getState();
|
141
|
-
useThread.getState().switchToBranch(branches[branches.indexOf(message.id) - 1]);
|
142
|
-
}, [useMessage, useThread]);
|
143
|
-
if (disabled) return null;
|
144
|
-
return callback;
|
145
|
-
};
|
146
|
-
|
147
|
-
// src/primitives/thread/index.ts
|
148
|
-
var thread_exports = {};
|
149
|
-
__export(thread_exports, {
|
150
|
-
Empty: () => ThreadEmpty,
|
151
|
-
If: () => ThreadIf,
|
152
|
-
Messages: () => ThreadMessages,
|
153
|
-
Root: () => ThreadRoot,
|
154
|
-
ScrollToBottom: () => ThreadScrollToBottom,
|
155
|
-
Suggestion: () => ThreadSuggestion,
|
156
|
-
Viewport: () => ThreadViewport
|
157
|
-
});
|
158
|
-
|
159
|
-
// src/primitives/thread/ThreadRoot.tsx
|
160
|
-
import {
|
161
|
-
Primitive
|
162
|
-
} from "@radix-ui/react-primitive";
|
163
|
-
import { forwardRef } from "react";
|
164
|
-
import { jsx } from "react/jsx-runtime";
|
165
|
-
var ThreadRoot = forwardRef(
|
166
|
-
(props, ref) => {
|
167
|
-
return /* @__PURE__ */ jsx(Primitive.div, { ...props, ref });
|
168
|
-
}
|
169
|
-
);
|
170
|
-
|
171
|
-
// src/primitives/thread/ThreadIf.tsx
|
172
|
-
var useThreadIf = (props) => {
|
173
|
-
const { useThread } = useThreadContext();
|
174
|
-
return useThread((thread) => {
|
175
|
-
if (props.empty === true && thread.messages.length !== 0) return false;
|
176
|
-
if (props.empty === false && thread.messages.length === 0) return false;
|
177
|
-
if (props.running === true && !thread.isRunning) return false;
|
178
|
-
if (props.running === false && thread.isRunning) return false;
|
179
|
-
return true;
|
180
|
-
});
|
181
|
-
};
|
182
|
-
var ThreadIf = ({ children, ...query }) => {
|
183
|
-
const result = useThreadIf(query);
|
184
|
-
return result ? children : null;
|
185
|
-
};
|
186
|
-
|
187
|
-
// src/primitives/thread/ThreadEmpty.tsx
|
188
|
-
import { jsx as jsx2 } from "react/jsx-runtime";
|
189
|
-
var ThreadEmpty = ({ children }) => {
|
190
|
-
return /* @__PURE__ */ jsx2(ThreadIf, { empty: true, children });
|
191
|
-
};
|
192
|
-
|
193
|
-
// src/primitives/thread/ThreadViewport.tsx
|
194
|
-
import { composeEventHandlers } from "@radix-ui/primitive";
|
195
|
-
import { useComposedRefs } from "@radix-ui/react-compose-refs";
|
196
|
-
import {
|
197
|
-
Primitive as Primitive2
|
198
|
-
} from "@radix-ui/react-primitive";
|
199
|
-
import { forwardRef as forwardRef2, useRef } from "react";
|
200
|
-
|
201
|
-
// src/utils/hooks/useOnResizeContent.tsx
|
202
|
-
import { useCallbackRef } from "@radix-ui/react-use-callback-ref";
|
203
|
-
import { useEffect } from "react";
|
204
|
-
var useOnResizeContent = (ref, callback) => {
|
205
|
-
const callbackRef = useCallbackRef(callback);
|
206
|
-
useEffect(() => {
|
207
|
-
const el = ref.current;
|
208
|
-
if (!el) return;
|
209
|
-
const resizeObserver = new ResizeObserver(() => {
|
210
|
-
callbackRef();
|
211
|
-
});
|
212
|
-
const mutationObserver = new MutationObserver((mutations) => {
|
213
|
-
for (const mutation of mutations) {
|
214
|
-
for (const node of mutation.addedNodes) {
|
215
|
-
if (node instanceof Element) {
|
216
|
-
resizeObserver.observe(node);
|
217
|
-
}
|
218
|
-
}
|
219
|
-
for (const node of mutation.removedNodes) {
|
220
|
-
if (node instanceof Element) {
|
221
|
-
resizeObserver.unobserve(node);
|
222
|
-
}
|
223
|
-
}
|
224
|
-
}
|
225
|
-
callbackRef();
|
226
|
-
});
|
227
|
-
resizeObserver.observe(el);
|
228
|
-
mutationObserver.observe(el, { childList: true });
|
229
|
-
for (const child of el.children) {
|
230
|
-
resizeObserver.observe(child);
|
231
|
-
}
|
232
|
-
return () => {
|
233
|
-
resizeObserver.disconnect();
|
234
|
-
mutationObserver.disconnect();
|
235
|
-
};
|
236
|
-
}, [ref.current, callbackRef]);
|
237
|
-
};
|
238
|
-
|
239
|
-
// src/utils/hooks/useOnScrollToBottom.tsx
|
240
|
-
import { useCallbackRef as useCallbackRef2 } from "@radix-ui/react-use-callback-ref";
|
241
|
-
import { useEffect as useEffect2 } from "react";
|
242
|
-
var useOnScrollToBottom = (callback) => {
|
243
|
-
const callbackRef = useCallbackRef2(callback);
|
244
|
-
const { useViewport } = useThreadContext();
|
245
|
-
useEffect2(() => {
|
246
|
-
return useViewport.getState().onScrollToBottom(() => {
|
247
|
-
callbackRef();
|
248
|
-
});
|
249
|
-
}, [useViewport, callbackRef]);
|
250
|
-
};
|
251
|
-
|
252
|
-
// src/primitives/thread/ThreadViewport.tsx
|
253
|
-
import { jsx as jsx3, jsxs } from "react/jsx-runtime";
|
254
|
-
var ThreadViewport = forwardRef2(({ autoScroll = true, onScroll, children, ...rest }, forwardedRef) => {
|
255
|
-
const messagesEndRef = useRef(null);
|
256
|
-
const divRef = useRef(null);
|
257
|
-
const ref = useComposedRefs(forwardedRef, divRef);
|
258
|
-
const { useViewport } = useThreadContext();
|
259
|
-
const firstRenderRef = useRef(true);
|
260
|
-
const isScrollingToBottomRef = useRef(false);
|
261
|
-
const lastScrollTop = useRef(0);
|
262
|
-
const scrollToBottom = () => {
|
263
|
-
const div = messagesEndRef.current;
|
264
|
-
if (!div || !autoScroll) return;
|
265
|
-
const behavior = firstRenderRef.current ? "instant" : "auto";
|
266
|
-
firstRenderRef.current = false;
|
267
|
-
isScrollingToBottomRef.current = true;
|
268
|
-
div.scrollIntoView({ behavior });
|
269
|
-
};
|
270
|
-
useOnResizeContent(divRef, () => {
|
271
|
-
if (!isScrollingToBottomRef.current && !useViewport.getState().isAtBottom) {
|
272
|
-
handleScroll();
|
273
|
-
} else {
|
274
|
-
scrollToBottom();
|
275
|
-
}
|
276
|
-
});
|
277
|
-
useOnScrollToBottom(() => {
|
278
|
-
scrollToBottom();
|
279
|
-
});
|
280
|
-
const handleScroll = () => {
|
281
|
-
const div = divRef.current;
|
282
|
-
if (!div) return;
|
283
|
-
const isAtBottom = useViewport.getState().isAtBottom;
|
284
|
-
const newIsAtBottom = div.scrollHeight - div.scrollTop <= div.clientHeight;
|
285
|
-
if (!newIsAtBottom && lastScrollTop.current < div.scrollTop) {
|
286
|
-
} else if (newIsAtBottom !== isAtBottom) {
|
287
|
-
isScrollingToBottomRef.current = false;
|
288
|
-
useViewport.setState({ isAtBottom: newIsAtBottom });
|
289
|
-
}
|
290
|
-
lastScrollTop.current = div.scrollTop;
|
291
|
-
};
|
292
|
-
return /* @__PURE__ */ jsxs(
|
293
|
-
Primitive2.div,
|
294
|
-
{
|
295
|
-
...rest,
|
296
|
-
onScroll: composeEventHandlers(onScroll, handleScroll),
|
297
|
-
ref,
|
298
|
-
children: [
|
299
|
-
children,
|
300
|
-
/* @__PURE__ */ jsx3("div", { ref: messagesEndRef })
|
301
|
-
]
|
302
|
-
}
|
303
|
-
);
|
304
|
-
});
|
305
|
-
|
306
|
-
// src/context/providers/MessageProvider.tsx
|
307
|
-
import { useEffect as useEffect3, useState } from "react";
|
308
|
-
import { create as create2 } from "zustand";
|
309
|
-
|
310
|
-
// src/context/stores/MessageComposer.ts
|
311
|
-
import { create } from "zustand";
|
312
|
-
|
313
|
-
// src/context/stores/BaseComposer.ts
|
314
|
-
var makeBaseComposer = (set) => ({
|
315
|
-
value: "",
|
316
|
-
setValue: (value) => {
|
317
|
-
set({ value });
|
318
|
-
}
|
319
|
-
});
|
320
|
-
|
321
|
-
// src/context/stores/MessageComposer.ts
|
322
|
-
var makeEditComposerStore = ({
|
323
|
-
onEdit,
|
324
|
-
onSend
|
325
|
-
}) => create()((set, get, store) => ({
|
326
|
-
...makeBaseComposer(set, get, store),
|
327
|
-
isEditing: false,
|
328
|
-
edit: () => {
|
329
|
-
const value = onEdit();
|
330
|
-
set({ isEditing: true, value });
|
331
|
-
},
|
332
|
-
send: () => {
|
333
|
-
const value = get().value;
|
334
|
-
set({ isEditing: false });
|
335
|
-
onSend(value);
|
336
|
-
},
|
337
|
-
cancel: () => {
|
338
|
-
if (!get().isEditing) return false;
|
339
|
-
set({ isEditing: false });
|
340
|
-
return true;
|
341
|
-
}
|
342
|
-
}));
|
343
|
-
|
344
|
-
// src/context/providers/MessageProvider.tsx
|
345
|
-
import { jsx as jsx4 } from "react/jsx-runtime";
|
346
|
-
var getIsLast = (thread, message) => {
|
347
|
-
return thread.messages[thread.messages.length - 1]?.id === message.id;
|
348
|
-
};
|
349
|
-
var syncMessage = (thread, useMessage, messageIndex) => {
|
350
|
-
const parentId = thread.messages[messageIndex - 1]?.id ?? null;
|
351
|
-
const message = thread.messages[messageIndex];
|
352
|
-
if (!message) return;
|
353
|
-
const isLast = getIsLast(thread, message);
|
354
|
-
const branches = thread.getBranches(message.id);
|
355
|
-
const currentState = useMessage.getState();
|
356
|
-
if (currentState.message === message && currentState.parentId === parentId && currentState.branches === branches && currentState.isLast === isLast)
|
357
|
-
return;
|
358
|
-
useMessage.setState({
|
359
|
-
message,
|
360
|
-
parentId,
|
361
|
-
branches,
|
362
|
-
isLast
|
363
|
-
});
|
364
|
-
};
|
365
|
-
var useMessageContext2 = (messageIndex) => {
|
366
|
-
const { useThread } = useThreadContext();
|
367
|
-
const [context] = useState(() => {
|
368
|
-
const useMessage = create2((set) => ({
|
369
|
-
message: null,
|
370
|
-
parentId: null,
|
371
|
-
branches: [],
|
372
|
-
isLast: false,
|
373
|
-
inProgressIndicator: null,
|
374
|
-
isCopied: false,
|
375
|
-
isHovering: false,
|
376
|
-
setInProgressIndicator: (value) => {
|
377
|
-
set({ inProgressIndicator: value });
|
378
|
-
},
|
379
|
-
setIsCopied: (value) => {
|
380
|
-
set({ isCopied: value });
|
381
|
-
},
|
382
|
-
setIsHovering: (value) => {
|
383
|
-
set({ isHovering: value });
|
384
|
-
}
|
385
|
-
}));
|
386
|
-
const useComposer = makeEditComposerStore({
|
387
|
-
onEdit: () => {
|
388
|
-
const message = useMessage.getState().message;
|
389
|
-
if (message.role !== "user")
|
390
|
-
throw new Error(
|
391
|
-
"Tried to edit a non-user message. Editing is only supported for user messages. This is likely an internal bug in assistant-ui."
|
392
|
-
);
|
393
|
-
const text = getMessageText(message);
|
394
|
-
return text;
|
395
|
-
},
|
396
|
-
onSend: (text) => {
|
397
|
-
const { message, parentId } = useMessage.getState();
|
398
|
-
if (message.role !== "user")
|
399
|
-
throw new Error(
|
400
|
-
"Tried to edit a non-user message. Editing is only supported for user messages. This is likely an internal bug in assistant-ui."
|
401
|
-
);
|
402
|
-
const nonTextParts = message.content.filter(
|
403
|
-
(part) => part.type !== "text" && part.type !== "ui"
|
404
|
-
);
|
405
|
-
useThread.getState().append({
|
406
|
-
parentId,
|
407
|
-
content: [{ type: "text", text }, ...nonTextParts]
|
408
|
-
});
|
409
|
-
}
|
410
|
-
});
|
411
|
-
syncMessage(useThread.getState(), useMessage, messageIndex);
|
412
|
-
return { useMessage, useComposer };
|
413
|
-
});
|
414
|
-
useEffect3(() => {
|
415
|
-
return useThread.subscribe((thread) => {
|
416
|
-
syncMessage(thread, context.useMessage, messageIndex);
|
417
|
-
});
|
418
|
-
}, [context, useThread, messageIndex]);
|
419
|
-
return context;
|
420
|
-
};
|
421
|
-
var MessageProvider = ({
|
422
|
-
messageIndex,
|
423
|
-
children
|
424
|
-
}) => {
|
425
|
-
const context = useMessageContext2(messageIndex);
|
426
|
-
return /* @__PURE__ */ jsx4(MessageContext.Provider, { value: context, children });
|
427
|
-
};
|
428
|
-
|
429
|
-
// src/primitives/composer/ComposerIf.tsx
|
430
|
-
var useComposerIf = (props) => {
|
431
|
-
const { useComposer } = useComposerContext();
|
432
|
-
return useComposer((composer) => {
|
433
|
-
if (props.editing === true && !composer.isEditing) return false;
|
434
|
-
if (props.editing === false && composer.isEditing) return false;
|
435
|
-
return true;
|
436
|
-
});
|
437
|
-
};
|
438
|
-
var ComposerIf = ({ children, ...query }) => {
|
439
|
-
const result = useComposerIf(query);
|
440
|
-
return result ? children : null;
|
441
|
-
};
|
442
|
-
|
443
|
-
// src/primitives/message/MessageIf.tsx
|
444
|
-
var useMessageIf = (props) => {
|
445
|
-
const { useMessage } = useMessageContext();
|
446
|
-
return useMessage(({ message, branches, isLast, isCopied, isHovering }) => {
|
447
|
-
if (props.hasBranches === true && branches.length < 2) return false;
|
448
|
-
if (props.user && message.role !== "user") return false;
|
449
|
-
if (props.assistant && message.role !== "assistant") return false;
|
450
|
-
if (props.lastOrHover === true && !isHovering && !isLast) return false;
|
451
|
-
if (props.copied === true && !isCopied) return false;
|
452
|
-
if (props.copied === false && isCopied) return false;
|
453
|
-
return true;
|
454
|
-
});
|
455
|
-
};
|
456
|
-
var MessageIf = ({ children, ...query }) => {
|
457
|
-
const result = useMessageIf(query);
|
458
|
-
return result ? children : null;
|
459
|
-
};
|
460
|
-
|
461
|
-
// src/primitives/thread/ThreadMessages.tsx
|
462
|
-
import { Fragment, jsx as jsx5, jsxs as jsxs2 } from "react/jsx-runtime";
|
463
|
-
var getComponents = (components) => {
|
464
|
-
return {
|
465
|
-
EditComposer: components.EditComposer ?? components.UserMessage ?? components.Message,
|
466
|
-
UserMessage: components.UserMessage ?? components.Message,
|
467
|
-
AssistantMessage: components.AssistantMessage ?? components.Message
|
468
|
-
};
|
469
|
-
};
|
470
|
-
var ThreadMessages = ({ components }) => {
|
471
|
-
const { useThread } = useThreadContext();
|
472
|
-
const messagesLength = useThread((t) => t.messages.length);
|
473
|
-
const { UserMessage, EditComposer, AssistantMessage } = getComponents(components);
|
474
|
-
if (messagesLength === 0) return null;
|
475
|
-
return /* @__PURE__ */ jsx5(Fragment, { children: new Array(messagesLength).fill(null).map((_, idx) => {
|
476
|
-
const messageIndex = idx;
|
477
|
-
return /* @__PURE__ */ jsxs2(
|
478
|
-
MessageProvider,
|
479
|
-
{
|
480
|
-
messageIndex,
|
481
|
-
children: [
|
482
|
-
/* @__PURE__ */ jsxs2(MessageIf, { user: true, children: [
|
483
|
-
/* @__PURE__ */ jsx5(ComposerIf, { editing: false, children: /* @__PURE__ */ jsx5(UserMessage, {}) }),
|
484
|
-
/* @__PURE__ */ jsx5(ComposerIf, { editing: true, children: /* @__PURE__ */ jsx5(EditComposer, {}) })
|
485
|
-
] }),
|
486
|
-
/* @__PURE__ */ jsx5(MessageIf, { assistant: true, children: /* @__PURE__ */ jsx5(AssistantMessage, {}) })
|
487
|
-
]
|
488
|
-
},
|
489
|
-
messageIndex
|
490
|
-
);
|
491
|
-
}) });
|
492
|
-
};
|
493
|
-
|
494
|
-
// src/primitives/thread/ThreadScrollToBottom.tsx
|
495
|
-
import { composeEventHandlers as composeEventHandlers2 } from "@radix-ui/primitive";
|
496
|
-
import {
|
497
|
-
Primitive as Primitive3
|
498
|
-
} from "@radix-ui/react-primitive";
|
499
|
-
import { forwardRef as forwardRef3 } from "react";
|
500
|
-
import { jsx as jsx6 } from "react/jsx-runtime";
|
501
|
-
var ThreadScrollToBottom = forwardRef3(({ onClick, ...rest }, ref) => {
|
502
|
-
const { useViewport } = useThreadContext();
|
503
|
-
const isAtBottom = useViewport((s) => s.isAtBottom);
|
504
|
-
const handleScrollToBottom = () => {
|
505
|
-
useViewport.getState().scrollToBottom();
|
506
|
-
};
|
507
|
-
return /* @__PURE__ */ jsx6(
|
508
|
-
Primitive3.button,
|
509
|
-
{
|
510
|
-
...rest,
|
511
|
-
disabled: isAtBottom,
|
512
|
-
ref,
|
513
|
-
onClick: composeEventHandlers2(onClick, handleScrollToBottom)
|
514
|
-
}
|
515
|
-
);
|
516
|
-
});
|
517
|
-
|
518
|
-
// src/primitives/thread/ThreadSuggestion.tsx
|
519
|
-
import { composeEventHandlers as composeEventHandlers3 } from "@radix-ui/primitive";
|
520
|
-
import {
|
521
|
-
Primitive as Primitive4
|
522
|
-
} from "@radix-ui/react-primitive";
|
523
|
-
import { forwardRef as forwardRef4 } from "react";
|
524
|
-
import { jsx as jsx7 } from "react/jsx-runtime";
|
525
|
-
var ThreadSuggestion = forwardRef4(({ onClick, prompt, method, autoSend: send, ...rest }, ref) => {
|
526
|
-
const { useThread, useComposer } = useThreadContext();
|
527
|
-
const isDisabled = useThread((t) => t.isRunning);
|
528
|
-
const handleApplySuggestion = () => {
|
529
|
-
const thread = useThread.getState();
|
530
|
-
const composer = useComposer.getState();
|
531
|
-
composer.setValue(prompt);
|
532
|
-
if (send && !thread.isRunning) {
|
533
|
-
composer.send();
|
534
|
-
}
|
535
|
-
};
|
536
|
-
return /* @__PURE__ */ jsx7(
|
537
|
-
Primitive4.button,
|
538
|
-
{
|
539
|
-
...rest,
|
540
|
-
disabled: isDisabled,
|
541
|
-
ref,
|
542
|
-
onClick: composeEventHandlers3(onClick, handleApplySuggestion)
|
543
|
-
}
|
544
|
-
);
|
545
|
-
});
|
546
|
-
|
547
|
-
// src/primitives/composer/index.ts
|
548
|
-
var composer_exports = {};
|
549
|
-
__export(composer_exports, {
|
550
|
-
Cancel: () => ComposerCancel,
|
551
|
-
If: () => ComposerIf,
|
552
|
-
Input: () => ComposerInput,
|
553
|
-
Root: () => ComposerRoot,
|
554
|
-
Send: () => ComposerSend
|
555
|
-
});
|
556
|
-
|
557
|
-
// src/primitives/composer/ComposerRoot.tsx
|
558
|
-
import { composeEventHandlers as composeEventHandlers4 } from "@radix-ui/primitive";
|
559
|
-
import { useComposedRefs as useComposedRefs2 } from "@radix-ui/react-compose-refs";
|
560
|
-
import {
|
561
|
-
Primitive as Primitive5
|
562
|
-
} from "@radix-ui/react-primitive";
|
563
|
-
import { forwardRef as forwardRef5, useRef as useRef2 } from "react";
|
564
|
-
import { jsx as jsx8 } from "react/jsx-runtime";
|
565
|
-
var ComposerRoot = forwardRef5(
|
566
|
-
({ onSubmit, ...rest }, forwardedRef) => {
|
567
|
-
const { useViewport } = useThreadContext();
|
568
|
-
const { useComposer } = useComposerContext();
|
569
|
-
const formRef = useRef2(null);
|
570
|
-
const ref = useComposedRefs2(forwardedRef, formRef);
|
571
|
-
const handleSubmit = (e) => {
|
572
|
-
const composerState = useComposer.getState();
|
573
|
-
if (!composerState.isEditing) return;
|
574
|
-
e.preventDefault();
|
575
|
-
composerState.send();
|
576
|
-
useViewport.getState().scrollToBottom();
|
577
|
-
};
|
578
|
-
return /* @__PURE__ */ jsx8(
|
579
|
-
Primitive5.form,
|
580
|
-
{
|
581
|
-
...rest,
|
582
|
-
ref,
|
583
|
-
onSubmit: composeEventHandlers4(onSubmit, handleSubmit)
|
584
|
-
}
|
585
|
-
);
|
586
|
-
}
|
587
|
-
);
|
588
|
-
|
589
|
-
// src/primitives/composer/ComposerInput.tsx
|
590
|
-
import { composeEventHandlers as composeEventHandlers5 } from "@radix-ui/primitive";
|
591
|
-
import { useComposedRefs as useComposedRefs3 } from "@radix-ui/react-compose-refs";
|
592
|
-
import { Slot } from "@radix-ui/react-slot";
|
593
|
-
import {
|
594
|
-
forwardRef as forwardRef6,
|
595
|
-
useCallback as useCallback6,
|
596
|
-
useEffect as useEffect4,
|
597
|
-
useRef as useRef3
|
598
|
-
} from "react";
|
599
|
-
import TextareaAutosize from "react-textarea-autosize";
|
600
|
-
import { jsx as jsx9 } from "react/jsx-runtime";
|
601
|
-
var ComposerInput = forwardRef6(
|
602
|
-
({ autoFocus = false, asChild, disabled, onChange, onKeyDown, ...rest }, forwardedRef) => {
|
603
|
-
const { useThread, useViewport } = useThreadContext();
|
604
|
-
const { useComposer, type } = useComposerContext();
|
605
|
-
const value = useComposer((c) => {
|
606
|
-
if (!c.isEditing) return "";
|
607
|
-
return c.value;
|
608
|
-
});
|
609
|
-
const Component = asChild ? Slot : TextareaAutosize;
|
610
|
-
const handleKeyPress = (e) => {
|
611
|
-
if (disabled) return;
|
612
|
-
const composer = useComposer.getState();
|
613
|
-
if (e.key === "Escape") {
|
614
|
-
if (useComposer.getState().cancel()) {
|
615
|
-
e.preventDefault();
|
616
|
-
}
|
617
|
-
} else if (e.key === "Enter" && e.shiftKey === false) {
|
618
|
-
const isRunning = useThread.getState().isRunning;
|
619
|
-
if (!isRunning) {
|
620
|
-
e.preventDefault();
|
621
|
-
composer.send();
|
622
|
-
useViewport.getState().scrollToBottom();
|
623
|
-
}
|
624
|
-
}
|
625
|
-
};
|
626
|
-
const textareaRef = useRef3(null);
|
627
|
-
const ref = useComposedRefs3(forwardedRef, textareaRef);
|
628
|
-
const autoFocusEnabled = autoFocus && !disabled;
|
629
|
-
const focus = useCallback6(() => {
|
630
|
-
const textarea = textareaRef.current;
|
631
|
-
if (!textarea || !autoFocusEnabled) return;
|
632
|
-
textarea.focus();
|
633
|
-
textarea.setSelectionRange(
|
634
|
-
textareaRef.current.value.length,
|
635
|
-
textareaRef.current.value.length
|
636
|
-
);
|
637
|
-
}, [autoFocusEnabled]);
|
638
|
-
useEffect4(() => focus(), [focus]);
|
639
|
-
useOnScrollToBottom(() => {
|
640
|
-
if (type === "new") {
|
641
|
-
focus();
|
642
|
-
}
|
643
|
-
});
|
644
|
-
return /* @__PURE__ */ jsx9(
|
645
|
-
Component,
|
646
|
-
{
|
647
|
-
value,
|
648
|
-
...rest,
|
649
|
-
ref,
|
650
|
-
disabled,
|
651
|
-
onChange: composeEventHandlers5(onChange, (e) => {
|
652
|
-
const composerState = useComposer.getState();
|
653
|
-
if (!composerState.isEditing) return;
|
654
|
-
return composerState.setValue(e.target.value);
|
655
|
-
}),
|
656
|
-
onKeyDown: composeEventHandlers5(onKeyDown, handleKeyPress)
|
657
|
-
}
|
658
|
-
);
|
659
|
-
}
|
660
|
-
);
|
661
|
-
|
662
|
-
// src/primitives/composer/ComposerSend.tsx
|
663
|
-
import {
|
664
|
-
Primitive as Primitive6
|
665
|
-
} from "@radix-ui/react-primitive";
|
666
|
-
import { forwardRef as forwardRef7 } from "react";
|
667
|
-
import { jsx as jsx10 } from "react/jsx-runtime";
|
668
|
-
var ComposerSend = forwardRef7(
|
669
|
-
({ disabled, ...rest }, ref) => {
|
670
|
-
const { useComposer } = useComposerContext();
|
671
|
-
const hasValue = useComposer((c) => c.isEditing && c.value.length > 0);
|
672
|
-
return /* @__PURE__ */ jsx10(
|
673
|
-
Primitive6.button,
|
674
|
-
{
|
675
|
-
type: "submit",
|
676
|
-
...rest,
|
677
|
-
ref,
|
678
|
-
disabled: disabled || !hasValue
|
679
|
-
}
|
680
|
-
);
|
681
|
-
}
|
682
|
-
);
|
683
|
-
|
684
|
-
// src/primitives/composer/ComposerCancel.tsx
|
685
|
-
import { composeEventHandlers as composeEventHandlers6 } from "@radix-ui/primitive";
|
686
|
-
import {
|
687
|
-
Primitive as Primitive7
|
688
|
-
} from "@radix-ui/react-primitive";
|
689
|
-
import { forwardRef as forwardRef8 } from "react";
|
690
|
-
import { jsx as jsx11 } from "react/jsx-runtime";
|
691
|
-
var ComposerCancel = forwardRef8(({ onClick, ...rest }, ref) => {
|
692
|
-
const { useComposer } = useComposerContext();
|
693
|
-
const handleCancel = () => {
|
694
|
-
useComposer.getState().cancel();
|
695
|
-
};
|
696
|
-
return /* @__PURE__ */ jsx11(
|
697
|
-
Primitive7.button,
|
698
|
-
{
|
699
|
-
type: "button",
|
700
|
-
...rest,
|
701
|
-
ref,
|
702
|
-
onClick: composeEventHandlers6(onClick, handleCancel)
|
703
|
-
}
|
704
|
-
);
|
705
|
-
});
|
706
|
-
|
707
|
-
// src/primitives/message/index.ts
|
708
|
-
var message_exports = {};
|
709
|
-
__export(message_exports, {
|
710
|
-
Content: () => MessageContent,
|
711
|
-
If: () => MessageIf,
|
712
|
-
InProgress: () => MessageInProgress,
|
713
|
-
Root: () => MessageRoot
|
714
|
-
});
|
715
|
-
|
716
|
-
// src/primitives/message/MessageRoot.tsx
|
717
|
-
import { composeEventHandlers as composeEventHandlers7 } from "@radix-ui/primitive";
|
718
|
-
import {
|
719
|
-
Primitive as Primitive8
|
720
|
-
} from "@radix-ui/react-primitive";
|
721
|
-
import { forwardRef as forwardRef9 } from "react";
|
722
|
-
import { jsx as jsx12 } from "react/jsx-runtime";
|
723
|
-
var MessageRoot = forwardRef9(
|
724
|
-
({ onMouseEnter, onMouseLeave, ...rest }, ref) => {
|
725
|
-
const { useMessage } = useMessageContext();
|
726
|
-
const setIsHovering = useMessage((s) => s.setIsHovering);
|
727
|
-
const handleMouseEnter = () => {
|
728
|
-
setIsHovering(true);
|
729
|
-
};
|
730
|
-
const handleMouseLeave = () => {
|
731
|
-
setIsHovering(false);
|
732
|
-
};
|
733
|
-
return /* @__PURE__ */ jsx12(
|
734
|
-
Primitive8.div,
|
735
|
-
{
|
736
|
-
...rest,
|
737
|
-
ref,
|
738
|
-
onMouseEnter: composeEventHandlers7(onMouseEnter, handleMouseEnter),
|
739
|
-
onMouseLeave: composeEventHandlers7(onMouseLeave, handleMouseLeave)
|
740
|
-
}
|
741
|
-
);
|
742
|
-
}
|
743
|
-
);
|
744
|
-
|
745
|
-
// src/context/providers/ContentPartProvider.tsx
|
746
|
-
import { useEffect as useEffect5, useState as useState2 } from "react";
|
747
|
-
import { create as create3 } from "zustand";
|
748
|
-
import { jsx as jsx13 } from "react/jsx-runtime";
|
749
|
-
var syncContentPart = ({ message }, useContentPart, partIndex) => {
|
750
|
-
const part = message.content[partIndex];
|
751
|
-
if (!part) return;
|
752
|
-
const messageStatus = message.role === "assistant" ? message.status : "done";
|
753
|
-
const status = partIndex === message.content.length - 1 ? messageStatus : "done";
|
754
|
-
useContentPart.setState({ part, status });
|
755
|
-
};
|
756
|
-
var useContentPartContext2 = (partIndex) => {
|
757
|
-
const { useMessage } = useMessageContext();
|
758
|
-
const [context] = useState2(() => {
|
759
|
-
const useContentPart = create3(() => ({
|
760
|
-
part: { type: "text", text: "" },
|
761
|
-
status: "done"
|
762
|
-
}));
|
763
|
-
syncContentPart(useMessage.getState(), useContentPart, partIndex);
|
764
|
-
return { useContentPart };
|
765
|
-
});
|
766
|
-
useEffect5(() => {
|
767
|
-
return useMessage.subscribe((message) => {
|
768
|
-
syncContentPart(message, context.useContentPart, partIndex);
|
769
|
-
});
|
770
|
-
}, [context, useMessage, partIndex]);
|
771
|
-
return context;
|
772
|
-
};
|
773
|
-
var ContentPartProvider = ({
|
774
|
-
partIndex,
|
775
|
-
children
|
776
|
-
}) => {
|
777
|
-
const context = useContentPartContext2(partIndex);
|
778
|
-
return /* @__PURE__ */ jsx13(ContentPartContext.Provider, { value: context, children });
|
779
|
-
};
|
780
|
-
|
781
|
-
// src/primitives/contentPart/ContentPartInProgressIndicator.tsx
|
782
|
-
var ContentPartInProgressIndicator = () => {
|
783
|
-
const { useMessage } = useMessageContext();
|
784
|
-
const { useContentPart } = useContentPartContext();
|
785
|
-
const indicator = useCombinedStore(
|
786
|
-
[useMessage, useContentPart],
|
787
|
-
(m, c) => c.status === "in_progress" ? m.inProgressIndicator : null
|
788
|
-
);
|
789
|
-
return indicator;
|
790
|
-
};
|
791
|
-
|
792
|
-
// src/primitives/message/MessageContent.tsx
|
793
|
-
import { Fragment as Fragment2, jsx as jsx14, jsxs as jsxs3 } from "react/jsx-runtime";
|
794
|
-
var defaultComponents = {
|
795
|
-
Text: ({ part }) => /* @__PURE__ */ jsxs3(Fragment2, { children: [
|
796
|
-
part.text,
|
797
|
-
/* @__PURE__ */ jsx14(ContentPartInProgressIndicator, {})
|
798
|
-
] }),
|
799
|
-
Image: () => null,
|
800
|
-
UI: ({ part }) => part.display,
|
801
|
-
tools: {
|
802
|
-
Fallback: () => null
|
803
|
-
}
|
804
|
-
};
|
805
|
-
var MessageContent = ({
|
806
|
-
components: {
|
807
|
-
Text = defaultComponents.Text,
|
808
|
-
Image = defaultComponents.Image,
|
809
|
-
UI = defaultComponents.UI,
|
810
|
-
tools: { by_name = {}, Fallback = defaultComponents.tools.Fallback } = {}
|
811
|
-
} = {}
|
812
|
-
}) => {
|
813
|
-
const { useMessage } = useMessageContext();
|
814
|
-
const message = useMessage((s) => s.message);
|
815
|
-
const content = message.content;
|
816
|
-
return /* @__PURE__ */ jsx14(Fragment2, { children: content.map((part, i) => {
|
817
|
-
const partIndex = i;
|
818
|
-
const type = part.type;
|
819
|
-
let component = null;
|
820
|
-
switch (type) {
|
821
|
-
case "text":
|
822
|
-
component = /* @__PURE__ */ jsx14(Text, { part });
|
823
|
-
break;
|
824
|
-
case "image":
|
825
|
-
component = /* @__PURE__ */ jsx14(Image, { part });
|
826
|
-
break;
|
827
|
-
case "ui":
|
828
|
-
component = /* @__PURE__ */ jsx14(UI, { part });
|
829
|
-
break;
|
830
|
-
case "tool-call": {
|
831
|
-
const Tool = by_name[part.name] || Fallback;
|
832
|
-
component = /* @__PURE__ */ jsx14(Tool, { part });
|
833
|
-
break;
|
834
|
-
}
|
835
|
-
default:
|
836
|
-
throw new Error(`Unknown content part type: ${type}`);
|
837
|
-
}
|
838
|
-
return /* @__PURE__ */ jsx14(ContentPartProvider, { partIndex, children: component }, partIndex);
|
839
|
-
}) });
|
840
|
-
};
|
841
|
-
|
842
|
-
// src/primitives/message/MessageInProgress.tsx
|
843
|
-
import {
|
844
|
-
Primitive as Primitive9
|
845
|
-
} from "@radix-ui/react-primitive";
|
846
|
-
import { forwardRef as forwardRef10, useMemo as useMemo2 } from "react";
|
847
|
-
import { jsx as jsx15 } from "react/jsx-runtime";
|
848
|
-
var MessageInProgress = forwardRef10((props, ref) => {
|
849
|
-
const { useMessage } = useMessageContext();
|
850
|
-
useMemo2(() => {
|
851
|
-
useMessage.getState().setInProgressIndicator(/* @__PURE__ */ jsx15(Primitive9.span, { ...props, ref }));
|
852
|
-
}, [useMessage, props, ref]);
|
853
|
-
return null;
|
854
|
-
});
|
855
|
-
|
856
|
-
// src/primitives/branchPicker/index.ts
|
857
|
-
var branchPicker_exports = {};
|
858
|
-
__export(branchPicker_exports, {
|
859
|
-
Count: () => BranchPickerCount,
|
860
|
-
Next: () => BranchPickerNext,
|
861
|
-
Number: () => BranchPickerNumber,
|
862
|
-
Previous: () => BranchPickerPrevious,
|
863
|
-
Root: () => BranchPickerRoot
|
864
|
-
});
|
865
|
-
|
866
|
-
// src/utils/createActionButton.tsx
|
867
|
-
import { composeEventHandlers as composeEventHandlers8 } from "@radix-ui/primitive";
|
868
|
-
import {
|
869
|
-
Primitive as Primitive10
|
870
|
-
} from "@radix-ui/react-primitive";
|
871
|
-
import { forwardRef as forwardRef11 } from "react";
|
872
|
-
import { jsx as jsx16 } from "react/jsx-runtime";
|
873
|
-
var createActionButton = (useActionButton) => {
|
874
|
-
return forwardRef11(
|
875
|
-
(props, forwardedRef) => {
|
876
|
-
const onClick = useActionButton(props);
|
877
|
-
return /* @__PURE__ */ jsx16(
|
878
|
-
Primitive10.button,
|
879
|
-
{
|
880
|
-
type: "button",
|
881
|
-
disabled: !onClick,
|
882
|
-
...props,
|
883
|
-
ref: forwardedRef,
|
884
|
-
onClick: composeEventHandlers8(props.onClick, onClick ?? void 0)
|
885
|
-
}
|
886
|
-
);
|
887
|
-
}
|
888
|
-
);
|
889
|
-
};
|
890
|
-
|
891
|
-
// src/primitives/branchPicker/BranchPickerNext.tsx
|
892
|
-
var BranchPickerNext = createActionButton(useGoToNextBranch);
|
893
|
-
|
894
|
-
// src/primitives/branchPicker/BranchPickerPrevious.tsx
|
895
|
-
var BranchPickerPrevious = createActionButton(useGoToPreviousBranch);
|
896
|
-
|
897
|
-
// src/primitives/branchPicker/BranchPickerCount.tsx
|
898
|
-
import { Fragment as Fragment3, jsx as jsx17 } from "react/jsx-runtime";
|
899
|
-
var BranchPickerCount = () => {
|
900
|
-
const { useMessage } = useMessageContext();
|
901
|
-
const branchCount = useMessage((s) => s.branches.length);
|
902
|
-
return /* @__PURE__ */ jsx17(Fragment3, { children: branchCount });
|
903
|
-
};
|
904
|
-
|
905
|
-
// src/primitives/branchPicker/BranchPickerNumber.tsx
|
906
|
-
import { Fragment as Fragment4, jsx as jsx18 } from "react/jsx-runtime";
|
907
|
-
var BranchPickerNumber = () => {
|
908
|
-
const { useMessage } = useMessageContext();
|
909
|
-
const branchIdx = useMessage((s) => s.branches.indexOf(s.message.id));
|
910
|
-
return /* @__PURE__ */ jsx18(Fragment4, { children: branchIdx + 1 });
|
911
|
-
};
|
912
|
-
|
913
|
-
// src/primitives/branchPicker/BranchPickerRoot.tsx
|
914
|
-
import {
|
915
|
-
Primitive as Primitive11
|
916
|
-
} from "@radix-ui/react-primitive";
|
917
|
-
import { forwardRef as forwardRef12 } from "react";
|
918
|
-
import { jsx as jsx19 } from "react/jsx-runtime";
|
919
|
-
var BranchPickerRoot = forwardRef12(({ hideWhenSingleBranch, ...rest }, ref) => {
|
920
|
-
return /* @__PURE__ */ jsx19(MessageIf, { hasBranches: hideWhenSingleBranch ? true : void 0, children: /* @__PURE__ */ jsx19(Primitive11.div, { ...rest, ref }) });
|
921
|
-
});
|
922
|
-
|
923
|
-
// src/primitives/actionBar/index.ts
|
924
|
-
var actionBar_exports = {};
|
925
|
-
__export(actionBar_exports, {
|
926
|
-
Copy: () => ActionBarCopy,
|
927
|
-
Edit: () => ActionBarEdit,
|
928
|
-
Reload: () => ActionBarReload,
|
929
|
-
Root: () => ActionBarRoot
|
930
|
-
});
|
931
|
-
|
932
|
-
// src/primitives/actionBar/ActionBarRoot.tsx
|
933
|
-
import {
|
934
|
-
Primitive as Primitive12
|
935
|
-
} from "@radix-ui/react-primitive";
|
936
|
-
import { forwardRef as forwardRef13 } from "react";
|
937
|
-
import { jsx as jsx20 } from "react/jsx-runtime";
|
938
|
-
var ActionBarRoot = forwardRef13(({ hideWhenRunning, autohide, autohideFloat, ...rest }, ref) => {
|
939
|
-
const { useThread } = useThreadContext();
|
940
|
-
const { useMessage } = useMessageContext();
|
941
|
-
const hideAndfloatStatus = useCombinedStore(
|
942
|
-
[useThread, useMessage],
|
943
|
-
(t, m) => {
|
944
|
-
if (hideWhenRunning && t.isRunning) return "hidden" /* Hidden */;
|
945
|
-
const autohideEnabled = autohide === "always" || autohide === "not-last" && !m.isLast;
|
946
|
-
if (!autohideEnabled) return "normal" /* Normal */;
|
947
|
-
if (!m.isHovering) return "hidden" /* Hidden */;
|
948
|
-
if (autohideFloat === "always" || autohideFloat === "single-branch" && m.branches.length <= 1)
|
949
|
-
return "floating" /* Floating */;
|
950
|
-
return "normal" /* Normal */;
|
951
|
-
}
|
952
|
-
);
|
953
|
-
if (hideAndfloatStatus === "hidden" /* Hidden */) return null;
|
954
|
-
return /* @__PURE__ */ jsx20(
|
955
|
-
Primitive12.div,
|
956
|
-
{
|
957
|
-
"data-floating": hideAndfloatStatus === "floating" /* Floating */,
|
958
|
-
...rest,
|
959
|
-
ref
|
960
|
-
}
|
961
|
-
);
|
962
|
-
});
|
963
|
-
|
964
|
-
// src/primitives/actionBar/ActionBarCopy.tsx
|
965
|
-
var ActionBarCopy = createActionButton(useCopyMessage);
|
966
|
-
|
967
|
-
// src/primitives/actionBar/ActionBarReload.tsx
|
968
|
-
var ActionBarReload = createActionButton(useReloadMessage);
|
969
|
-
|
970
|
-
// src/primitives/actionBar/ActionBarEdit.tsx
|
971
|
-
var ActionBarEdit = createActionButton(useBeginMessageEdit);
|
972
|
-
|
973
|
-
// src/primitives/contentPart/index.ts
|
974
|
-
var contentPart_exports = {};
|
975
|
-
__export(contentPart_exports, {
|
976
|
-
InProgressIndicator: () => ContentPartInProgressIndicator
|
977
|
-
});
|
978
|
-
|
979
|
-
// src/runtime/local/useLocalRuntime.tsx
|
980
|
-
import { useInsertionEffect, useState as useState3 } from "react";
|
981
|
-
|
982
|
-
// src/runtime/local/LocalRuntime.tsx
|
983
|
-
var LocalRuntime = class {
|
984
|
-
constructor(adapter) {
|
985
|
-
this.adapter = adapter;
|
986
|
-
}
|
987
|
-
_subscriptions = /* @__PURE__ */ new Set();
|
988
|
-
_configProviders = /* @__PURE__ */ new Set();
|
989
|
-
abortController = null;
|
990
|
-
repository = new MessageRepository();
|
991
|
-
get messages() {
|
992
|
-
return this.repository.getMessages();
|
993
|
-
}
|
994
|
-
get isRunning() {
|
995
|
-
return this.abortController != null;
|
996
|
-
}
|
997
|
-
getBranches(messageId) {
|
998
|
-
return this.repository.getBranches(messageId);
|
999
|
-
}
|
1000
|
-
switchToBranch(branchId) {
|
1001
|
-
this.repository.switchToBranch(branchId);
|
1002
|
-
this.notifySubscribers();
|
1003
|
-
}
|
1004
|
-
async append(message) {
|
1005
|
-
const userMessageId = generateId();
|
1006
|
-
const userMessage = {
|
1007
|
-
id: userMessageId,
|
1008
|
-
role: "user",
|
1009
|
-
content: message.content,
|
1010
|
-
createdAt: /* @__PURE__ */ new Date()
|
1011
|
-
};
|
1012
|
-
this.repository.addOrUpdateMessage(message.parentId, userMessage);
|
1013
|
-
await this.startRun(userMessageId);
|
1014
|
-
}
|
1015
|
-
async startRun(parentId) {
|
1016
|
-
const id = generateId();
|
1017
|
-
this.repository.resetHead(parentId);
|
1018
|
-
const messages = this.repository.getMessages();
|
1019
|
-
const message = {
|
1020
|
-
id,
|
1021
|
-
role: "assistant",
|
1022
|
-
status: "in_progress",
|
1023
|
-
content: [{ type: "text", text: "" }],
|
1024
|
-
createdAt: /* @__PURE__ */ new Date()
|
1025
|
-
};
|
1026
|
-
this.repository.addOrUpdateMessage(parentId, { ...message });
|
1027
|
-
this.abortController?.abort();
|
1028
|
-
this.abortController = new AbortController();
|
1029
|
-
this.notifySubscribers();
|
1030
|
-
try {
|
1031
|
-
const updateHandler = ({ content }) => {
|
1032
|
-
message.content = content;
|
1033
|
-
this.repository.addOrUpdateMessage(parentId, { ...message });
|
1034
|
-
this.notifySubscribers();
|
1035
|
-
};
|
1036
|
-
const result = await this.adapter.run({
|
1037
|
-
messages,
|
1038
|
-
abortSignal: this.abortController.signal,
|
1039
|
-
config: mergeModelConfigs([...this._configProviders].map((p) => p())),
|
1040
|
-
onUpdate: updateHandler
|
1041
|
-
});
|
1042
|
-
updateHandler(result);
|
1043
|
-
message.status = "done";
|
1044
|
-
this.repository.addOrUpdateMessage(parentId, { ...message });
|
1045
|
-
} catch (e) {
|
1046
|
-
message.status = "error";
|
1047
|
-
this.repository.addOrUpdateMessage(parentId, { ...message });
|
1048
|
-
console.error(e);
|
1049
|
-
} finally {
|
1050
|
-
this.abortController = null;
|
1051
|
-
this.notifySubscribers();
|
1052
|
-
}
|
1053
|
-
}
|
1054
|
-
cancelRun() {
|
1055
|
-
if (!this.abortController) return;
|
1056
|
-
this.abortController.abort();
|
1057
|
-
this.abortController = null;
|
1058
|
-
this.notifySubscribers();
|
1059
|
-
}
|
1060
|
-
notifySubscribers() {
|
1061
|
-
for (const callback of this._subscriptions) callback();
|
1062
|
-
}
|
1063
|
-
subscribe(callback) {
|
1064
|
-
this._subscriptions.add(callback);
|
1065
|
-
return () => this._subscriptions.delete(callback);
|
1066
|
-
}
|
1067
|
-
registerModelConfigProvider(provider) {
|
1068
|
-
this._configProviders.add(provider);
|
1069
|
-
return () => this._configProviders.delete(provider);
|
1070
|
-
}
|
1071
|
-
};
|
1072
|
-
|
1073
|
-
// src/runtime/local/useLocalRuntime.tsx
|
1074
|
-
var useLocalRuntime = (adapter) => {
|
1075
|
-
const [runtime] = useState3(() => new LocalRuntime(adapter));
|
1076
|
-
useInsertionEffect(() => {
|
1077
|
-
runtime.adapter = adapter;
|
1078
|
-
});
|
1079
|
-
return runtime;
|
1080
|
-
};
|
1081
|
-
|
1082
|
-
// src/context/providers/AssistantRuntimeProvider.tsx
|
1083
|
-
import { memo } from "react";
|
1084
|
-
|
1085
|
-
// src/context/providers/AssistantProvider.tsx
|
1086
|
-
import { useEffect as useEffect7, useInsertionEffect as useInsertionEffect3, useRef as useRef5, useState as useState5 } from "react";
|
1087
|
-
|
1088
|
-
// src/context/stores/AssistantModelConfig.ts
|
1089
|
-
import { create as create4 } from "zustand";
|
1090
|
-
var makeAssistantModelConfigStore = () => create4(() => {
|
1091
|
-
const proxy = new ProxyConfigProvider();
|
1092
|
-
return {
|
1093
|
-
getModelConfig: () => {
|
1094
|
-
return proxy.getModelConfig();
|
1095
|
-
},
|
1096
|
-
registerModelConfigProvider: (provider) => {
|
1097
|
-
return proxy.registerModelConfigProvider(provider);
|
1098
|
-
}
|
1099
|
-
};
|
1100
|
-
});
|
1101
|
-
|
1102
|
-
// src/context/providers/ThreadProvider.tsx
|
1103
|
-
import { useEffect as useEffect6, useInsertionEffect as useInsertionEffect2, useRef as useRef4, useState as useState4 } from "react";
|
1104
|
-
|
1105
|
-
// src/context/stores/Composer.ts
|
1106
|
-
import { create as create5 } from "zustand";
|
1107
|
-
var makeComposerStore = (useThread) => create5()((set, get, store) => {
|
1108
|
-
return {
|
1109
|
-
...makeBaseComposer(set, get, store),
|
1110
|
-
isEditing: true,
|
1111
|
-
send: () => {
|
1112
|
-
const { setValue, value } = get();
|
1113
|
-
setValue("");
|
1114
|
-
useThread.getState().append({
|
1115
|
-
parentId: useThread.getState().messages.at(-1)?.id ?? null,
|
1116
|
-
content: [{ type: "text", text: value }]
|
1117
|
-
});
|
1118
|
-
},
|
1119
|
-
cancel: () => {
|
1120
|
-
const thread = useThread.getState();
|
1121
|
-
if (!thread.isRunning) return false;
|
1122
|
-
useThread.getState().cancelRun();
|
1123
|
-
return true;
|
1124
|
-
}
|
1125
|
-
};
|
1126
|
-
});
|
1127
|
-
|
1128
|
-
// src/context/stores/Thread.ts
|
1129
|
-
import { create as create6 } from "zustand";
|
1130
|
-
var makeThreadStore = (runtimeRef) => {
|
1131
|
-
const useThread = create6(() => ({
|
1132
|
-
messages: runtimeRef.current.messages,
|
1133
|
-
isRunning: runtimeRef.current.isRunning,
|
1134
|
-
getBranches: (messageId) => runtimeRef.current.getBranches(messageId),
|
1135
|
-
switchToBranch: (branchId) => runtimeRef.current.switchToBranch(branchId),
|
1136
|
-
startRun: (parentId) => runtimeRef.current.startRun(parentId),
|
1137
|
-
append: (message) => runtimeRef.current.append(message),
|
1138
|
-
cancelRun: () => runtimeRef.current.cancelRun()
|
1139
|
-
}));
|
1140
|
-
const onRuntimeUpdate = () => {
|
1141
|
-
useThread.setState({
|
1142
|
-
messages: runtimeRef.current.messages,
|
1143
|
-
isRunning: runtimeRef.current.isRunning
|
1144
|
-
});
|
1145
|
-
};
|
1146
|
-
return {
|
1147
|
-
useThread,
|
1148
|
-
onRuntimeUpdate
|
1149
|
-
};
|
1150
|
-
};
|
1151
|
-
|
1152
|
-
// src/context/stores/ThreadViewport.tsx
|
1153
|
-
import { create as create7 } from "zustand";
|
1154
|
-
var makeThreadViewportStore = () => {
|
1155
|
-
const scrollToBottomListeners = /* @__PURE__ */ new Set();
|
1156
|
-
return create7(() => ({
|
1157
|
-
isAtBottom: true,
|
1158
|
-
scrollToBottom: () => {
|
1159
|
-
for (const listener of scrollToBottomListeners) {
|
1160
|
-
listener();
|
1161
|
-
}
|
1162
|
-
},
|
1163
|
-
onScrollToBottom: (callback) => {
|
1164
|
-
scrollToBottomListeners.add(callback);
|
1165
|
-
return () => {
|
1166
|
-
scrollToBottomListeners.delete(callback);
|
1167
|
-
};
|
1168
|
-
}
|
1169
|
-
}));
|
1170
|
-
};
|
1171
|
-
|
1172
|
-
// src/context/providers/ThreadProvider.tsx
|
1173
|
-
import { jsx as jsx21, jsxs as jsxs4 } from "react/jsx-runtime";
|
1174
|
-
var ThreadProvider = ({
|
1175
|
-
children,
|
1176
|
-
runtime
|
1177
|
-
}) => {
|
1178
|
-
const runtimeRef = useRef4(runtime);
|
1179
|
-
useInsertionEffect2(() => {
|
1180
|
-
runtimeRef.current = runtime;
|
1181
|
-
});
|
1182
|
-
const [{ context, onRuntimeUpdate }] = useState4(() => {
|
1183
|
-
const { useThread, onRuntimeUpdate: onRuntimeUpdate2 } = makeThreadStore(runtimeRef);
|
1184
|
-
const useViewport = makeThreadViewportStore();
|
1185
|
-
const useComposer = makeComposerStore(useThread);
|
1186
|
-
return {
|
1187
|
-
context: {
|
1188
|
-
useViewport,
|
1189
|
-
useThread,
|
1190
|
-
useComposer
|
1191
|
-
},
|
1192
|
-
onRuntimeUpdate: onRuntimeUpdate2
|
1193
|
-
};
|
1194
|
-
});
|
1195
|
-
useEffect6(() => {
|
1196
|
-
onRuntimeUpdate();
|
1197
|
-
return runtime.subscribe(onRuntimeUpdate);
|
1198
|
-
}, [onRuntimeUpdate, runtime]);
|
1199
|
-
const RuntimeSynchronizer = runtime.unstable_synchronizer;
|
1200
|
-
return /* @__PURE__ */ jsxs4(ThreadContext.Provider, { value: context, children: [
|
1201
|
-
RuntimeSynchronizer && /* @__PURE__ */ jsx21(RuntimeSynchronizer, {}),
|
1202
|
-
children
|
1203
|
-
] });
|
1204
|
-
};
|
1205
|
-
|
1206
|
-
// src/context/providers/AssistantProvider.tsx
|
1207
|
-
import { jsx as jsx22 } from "react/jsx-runtime";
|
1208
|
-
var AssistantProvider = ({ children, runtime }) => {
|
1209
|
-
const runtimeRef = useRef5(runtime);
|
1210
|
-
useInsertionEffect3(() => {
|
1211
|
-
runtimeRef.current = runtime;
|
1212
|
-
});
|
1213
|
-
const [context] = useState5(() => {
|
1214
|
-
const useModelConfig = makeAssistantModelConfigStore();
|
1215
|
-
return { useModelConfig };
|
1216
|
-
});
|
1217
|
-
const getModelCOnfig = context.useModelConfig((c) => c.getModelConfig);
|
1218
|
-
useEffect7(() => {
|
1219
|
-
return runtime.registerModelConfigProvider(getModelCOnfig);
|
1220
|
-
}, [runtime, getModelCOnfig]);
|
1221
|
-
return /* @__PURE__ */ jsx22(AssistantContext.Provider, { value: context, children: /* @__PURE__ */ jsx22(ThreadProvider, { runtime, children }) });
|
1222
|
-
};
|
1223
|
-
|
1224
|
-
// src/context/providers/AssistantRuntimeProvider.tsx
|
1225
|
-
import { jsx as jsx23 } from "react/jsx-runtime";
|
1226
|
-
var AssistantRuntimeProviderImpl = ({ children, runtime }) => {
|
1227
|
-
return /* @__PURE__ */ jsx23(AssistantProvider, { runtime, children });
|
1228
|
-
};
|
1229
|
-
var AssistantRuntimeProvider = memo(AssistantRuntimeProviderImpl);
|
1230
|
-
export {
|
1231
|
-
actionBar_exports as ActionBarPrimitive,
|
1232
|
-
AssistantRuntimeProvider,
|
1233
|
-
branchPicker_exports as BranchPickerPrimitive,
|
1234
|
-
composer_exports as ComposerPrimitive,
|
1235
|
-
contentPart_exports as ContentPartPrimitive,
|
1236
|
-
message_exports as MessagePrimitive,
|
1237
|
-
thread_exports as ThreadPrimitive,
|
1238
|
-
useBeginMessageEdit,
|
1239
|
-
useCopyMessage,
|
1240
|
-
useGoToNextBranch,
|
1241
|
-
useGoToPreviousBranch,
|
1242
|
-
useLocalRuntime,
|
1243
|
-
useReloadMessage
|
1244
|
-
};
|
1245
|
-
//# sourceMappingURL=index.mjs.map
|