@assistant-ui/react 0.4.6 → 0.4.7
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/edge.d.mts +2 -2
- package/dist/edge.d.ts +2 -2
- package/dist/edge.js +6 -5
- package/dist/edge.js.map +1 -1
- package/dist/edge.mjs +6 -5
- package/dist/edge.mjs.map +1 -1
- package/dist/index.d.mts +280 -19
- package/dist/index.d.ts +280 -19
- package/dist/index.js +493 -445
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +430 -392
- package/dist/index.mjs.map +1 -1
- package/dist/tailwindcss/index.js +8 -8
- package/dist/tailwindcss/index.js.map +1 -1
- package/dist/tailwindcss/index.mjs +8 -8
- package/dist/tailwindcss/index.mjs.map +1 -1
- package/package.json +2 -1
package/dist/index.mjs
CHANGED
@@ -9,7 +9,7 @@ var __export = (target, all) => {
|
|
9
9
|
import { memo } from "react";
|
10
10
|
|
11
11
|
// src/context/providers/AssistantProvider.tsx
|
12
|
-
import { useEffect
|
12
|
+
import { useEffect, useInsertionEffect as useInsertionEffect2, useRef as useRef2, useState as useState2 } from "react";
|
13
13
|
|
14
14
|
// src/context/react/AssistantContext.ts
|
15
15
|
import { createContext, useContext } from "react";
|
@@ -67,6 +67,18 @@ ${config.system}`;
|
|
67
67
|
acc.tools[name] = tool;
|
68
68
|
}
|
69
69
|
}
|
70
|
+
if (config.config) {
|
71
|
+
acc.config = {
|
72
|
+
...acc.config,
|
73
|
+
...config.config
|
74
|
+
};
|
75
|
+
}
|
76
|
+
if (config.callSettings) {
|
77
|
+
acc.callSettings = {
|
78
|
+
...acc.callSettings,
|
79
|
+
...config.callSettings
|
80
|
+
};
|
81
|
+
}
|
70
82
|
return acc;
|
71
83
|
}, {});
|
72
84
|
};
|
@@ -131,14 +143,7 @@ var makeAssistantToolUIsStore = () => create2((set) => {
|
|
131
143
|
});
|
132
144
|
|
133
145
|
// src/context/providers/ThreadProvider.tsx
|
134
|
-
import {
|
135
|
-
useCallback,
|
136
|
-
useEffect,
|
137
|
-
useInsertionEffect,
|
138
|
-
useRef,
|
139
|
-
useState,
|
140
|
-
useSyncExternalStore
|
141
|
-
} from "react";
|
146
|
+
import { useCallback as useCallback2, useInsertionEffect, useState } from "react";
|
142
147
|
|
143
148
|
// src/context/react/ThreadContext.ts
|
144
149
|
import { createContext as createContext2, useContext as useContext2 } from "react";
|
@@ -204,7 +209,7 @@ var makeComposerStore = (useThreadMessages, useThreadActions) => {
|
|
204
209
|
import { create as create4 } from "zustand";
|
205
210
|
var makeThreadStore = (runtimeRef) => {
|
206
211
|
return create4(() => ({
|
207
|
-
isRunning: runtimeRef.
|
212
|
+
isRunning: runtimeRef.getState().isRunning
|
208
213
|
}));
|
209
214
|
};
|
210
215
|
|
@@ -230,19 +235,18 @@ var makeThreadViewportStore = () => {
|
|
230
235
|
|
231
236
|
// src/context/stores/ThreadActions.ts
|
232
237
|
import { create as create6 } from "zustand";
|
233
|
-
var makeThreadActionStore = (
|
238
|
+
var makeThreadActionStore = (runtimeStore) => {
|
234
239
|
return create6(
|
235
240
|
() => Object.freeze({
|
236
241
|
get capabilities() {
|
237
|
-
return
|
242
|
+
return runtimeStore.getState().capabilities;
|
238
243
|
},
|
239
|
-
getBranches: (messageId) =>
|
240
|
-
switchToBranch: (branchId) =>
|
241
|
-
startRun: (parentId) =>
|
242
|
-
append: (message) =>
|
243
|
-
cancelRun: () =>
|
244
|
-
addToolResult: (options) =>
|
245
|
-
getRuntime: () => runtimeRef.current
|
244
|
+
getBranches: (messageId) => runtimeStore.getState().getBranches(messageId),
|
245
|
+
switchToBranch: (branchId) => runtimeStore.getState().switchToBranch(branchId),
|
246
|
+
startRun: (parentId) => runtimeStore.getState().startRun(parentId),
|
247
|
+
append: (message) => runtimeStore.getState().append(message),
|
248
|
+
cancelRun: () => runtimeStore.getState().cancelRun(),
|
249
|
+
addToolResult: (options) => runtimeStore.getState().addToolResult(options)
|
246
250
|
})
|
247
251
|
);
|
248
252
|
};
|
@@ -250,60 +254,96 @@ var makeThreadActionStore = (runtimeRef) => {
|
|
250
254
|
// src/context/stores/ThreadMessages.ts
|
251
255
|
import { create as create7 } from "zustand";
|
252
256
|
var makeThreadMessagesStore = (runtimeRef) => {
|
253
|
-
return create7(() => runtimeRef.
|
257
|
+
return create7(() => runtimeRef.getState().messages);
|
258
|
+
};
|
259
|
+
|
260
|
+
// src/context/stores/ThreadRuntime.tsx
|
261
|
+
import { create as create8 } from "zustand";
|
262
|
+
var makeThreadRuntimeStore = (runtime) => {
|
263
|
+
return create8(() => runtime);
|
264
|
+
};
|
265
|
+
|
266
|
+
// src/utils/hooks/useManagedRef.ts
|
267
|
+
import { useCallback, useRef } from "react";
|
268
|
+
var useManagedRef = (callback) => {
|
269
|
+
const cleanupRef = useRef();
|
270
|
+
const ref = useCallback(
|
271
|
+
(el) => {
|
272
|
+
if (cleanupRef.current) {
|
273
|
+
cleanupRef.current();
|
274
|
+
}
|
275
|
+
if (el) {
|
276
|
+
cleanupRef.current = callback(el);
|
277
|
+
}
|
278
|
+
},
|
279
|
+
[callback]
|
280
|
+
);
|
281
|
+
return ref;
|
254
282
|
};
|
255
283
|
|
256
284
|
// src/context/providers/ThreadProvider.tsx
|
257
285
|
import { jsx, jsxs } from "react/jsx-runtime";
|
258
286
|
var ThreadProvider = ({
|
259
287
|
children,
|
260
|
-
|
288
|
+
provider
|
261
289
|
}) => {
|
262
|
-
const runtimeRef = useRef(runtime.thread);
|
263
290
|
const [context] = useState(() => {
|
264
|
-
const
|
265
|
-
const
|
266
|
-
const
|
291
|
+
const useThreadRuntime = makeThreadRuntimeStore(provider.thread);
|
292
|
+
const useThread = makeThreadStore(useThreadRuntime);
|
293
|
+
const useThreadMessages = makeThreadMessagesStore(useThreadRuntime);
|
294
|
+
const useThreadActions = makeThreadActionStore(useThreadRuntime);
|
267
295
|
const useViewport = makeThreadViewportStore();
|
268
296
|
const useComposer = makeComposerStore(useThreadMessages, useThreadActions);
|
269
297
|
return {
|
270
298
|
useThread,
|
299
|
+
useThreadRuntime,
|
271
300
|
useThreadMessages,
|
272
301
|
useThreadActions,
|
273
302
|
useComposer,
|
274
303
|
useViewport
|
275
304
|
};
|
276
305
|
});
|
277
|
-
const
|
278
|
-
|
279
|
-
|
280
|
-
|
306
|
+
const threadRef = useManagedRef(
|
307
|
+
useCallback2(
|
308
|
+
(thread) => {
|
309
|
+
const onThreadUpdate = () => {
|
310
|
+
context.useThread.setState(
|
311
|
+
Object.freeze({
|
312
|
+
isRunning: context.useThreadRuntime.getState().isRunning
|
313
|
+
}),
|
314
|
+
true
|
315
|
+
);
|
316
|
+
context.useThreadMessages.setState(thread.messages, true);
|
317
|
+
};
|
318
|
+
onThreadUpdate();
|
319
|
+
return thread.subscribe(onThreadUpdate);
|
320
|
+
},
|
321
|
+
[context]
|
322
|
+
)
|
281
323
|
);
|
282
324
|
useInsertionEffect(() => {
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
true
|
292
|
-
);
|
293
|
-
context.useThreadMessages.setState(Object.freeze(runtimeRef.current.messages), true);
|
325
|
+
const unsubscribe = provider.subscribe(() => {
|
326
|
+
context.useThreadRuntime.setState(provider.thread, true);
|
327
|
+
threadRef(provider.thread);
|
328
|
+
});
|
329
|
+
threadRef(provider.thread);
|
330
|
+
return () => {
|
331
|
+
unsubscribe();
|
332
|
+
threadRef(null);
|
294
333
|
};
|
295
|
-
|
296
|
-
|
297
|
-
|
334
|
+
}, [provider, context]);
|
335
|
+
const Synchronizer = context.useThreadRuntime(
|
336
|
+
(t) => t.unstable_synchronizer
|
337
|
+
);
|
298
338
|
return /* @__PURE__ */ jsxs(ThreadContext.Provider, { value: context, children: [
|
299
|
-
|
339
|
+
Synchronizer && /* @__PURE__ */ jsx(Synchronizer, {}),
|
300
340
|
children
|
301
341
|
] });
|
302
342
|
};
|
303
343
|
|
304
344
|
// src/context/stores/AssistantActions.tsx
|
305
|
-
import { create as
|
306
|
-
var makeAssistantActionsStore = (runtimeRef) =>
|
345
|
+
import { create as create9 } from "zustand";
|
346
|
+
var makeAssistantActionsStore = (runtimeRef) => create9(
|
307
347
|
() => Object.freeze({
|
308
348
|
switchToThread: () => runtimeRef.current.switchToThread(null),
|
309
349
|
getRuntime: () => runtimeRef.current
|
@@ -324,10 +364,10 @@ var AssistantProvider = ({ children, runtime }) => {
|
|
324
364
|
return { useModelConfig, useToolUIs, useAssistantActions };
|
325
365
|
});
|
326
366
|
const getModelConfig = context.useModelConfig();
|
327
|
-
|
367
|
+
useEffect(() => {
|
328
368
|
return runtime.registerModelConfigProvider(getModelConfig);
|
329
369
|
}, [runtime, getModelConfig]);
|
330
|
-
return /* @__PURE__ */ jsx2(AssistantContext.Provider, { value: context, children: /* @__PURE__ */ jsx2(ThreadProvider, { runtime, children }) });
|
370
|
+
return /* @__PURE__ */ jsx2(AssistantContext.Provider, { value: context, children: /* @__PURE__ */ jsx2(ThreadProvider, { provider: runtime, children }) });
|
331
371
|
};
|
332
372
|
|
333
373
|
// src/context/providers/AssistantRuntimeProvider.tsx
|
@@ -380,7 +420,7 @@ function useContentPartContext(options) {
|
|
380
420
|
}
|
381
421
|
|
382
422
|
// src/hooks/useAppendMessage.tsx
|
383
|
-
import { useCallback as
|
423
|
+
import { useCallback as useCallback3 } from "react";
|
384
424
|
var toAppendMessage = (useThreadMessages, message) => {
|
385
425
|
if (typeof message === "string") {
|
386
426
|
return {
|
@@ -397,7 +437,7 @@ var toAppendMessage = (useThreadMessages, message) => {
|
|
397
437
|
};
|
398
438
|
var useAppendMessage = () => {
|
399
439
|
const { useThreadMessages, useThreadActions, useViewport, useComposer } = useThreadContext();
|
400
|
-
const append =
|
440
|
+
const append = useCallback3(
|
401
441
|
(message) => {
|
402
442
|
const appendMessage = toAppendMessage(useThreadMessages, message);
|
403
443
|
useThreadActions.getState().append(appendMessage);
|
@@ -410,11 +450,11 @@ var useAppendMessage = () => {
|
|
410
450
|
};
|
411
451
|
|
412
452
|
// src/hooks/useSwitchToNewThread.tsx
|
413
|
-
import { useCallback as
|
453
|
+
import { useCallback as useCallback4 } from "react";
|
414
454
|
var useSwitchToNewThread = () => {
|
415
455
|
const { useAssistantActions } = useAssistantContext();
|
416
456
|
const { useComposer } = useThreadContext();
|
417
|
-
const switchToNewThread =
|
457
|
+
const switchToNewThread = useCallback4(() => {
|
418
458
|
useAssistantActions.getState().switchToThread(null);
|
419
459
|
useComposer.getState().focus();
|
420
460
|
}, [useAssistantActions, useComposer]);
|
@@ -422,14 +462,14 @@ var useSwitchToNewThread = () => {
|
|
422
462
|
};
|
423
463
|
|
424
464
|
// src/model-config/useAssistantTool.tsx
|
425
|
-
import { useEffect as
|
465
|
+
import { useEffect as useEffect2 } from "react";
|
426
466
|
var useAssistantTool = (tool) => {
|
427
467
|
const { useModelConfig, useToolUIs } = useAssistantContext();
|
428
468
|
const registerModelConfigProvider = useModelConfig(
|
429
469
|
(s) => s.registerModelConfigProvider
|
430
470
|
);
|
431
471
|
const setToolUI = useToolUIs((s) => s.setToolUI);
|
432
|
-
|
472
|
+
useEffect2(() => {
|
433
473
|
const { toolName, render, ...rest } = tool;
|
434
474
|
const config = {
|
435
475
|
tools: {
|
@@ -457,11 +497,11 @@ var makeAssistantTool = (tool) => {
|
|
457
497
|
};
|
458
498
|
|
459
499
|
// src/model-config/useAssistantToolUI.tsx
|
460
|
-
import { useEffect as
|
500
|
+
import { useEffect as useEffect3 } from "react";
|
461
501
|
var useAssistantToolUI = (tool) => {
|
462
502
|
const { useToolUIs } = useAssistantContext();
|
463
503
|
const setToolUI = useToolUIs((s) => s.setToolUI);
|
464
|
-
|
504
|
+
useEffect3(() => {
|
465
505
|
if (!tool) return;
|
466
506
|
const { toolName, render } = tool;
|
467
507
|
return setToolUI(toolName, render);
|
@@ -478,13 +518,13 @@ var makeAssistantToolUI = (tool) => {
|
|
478
518
|
};
|
479
519
|
|
480
520
|
// src/model-config/useAssistantInstructions.tsx
|
481
|
-
import { useEffect as
|
521
|
+
import { useEffect as useEffect4 } from "react";
|
482
522
|
var useAssistantInstructions = (instruction) => {
|
483
523
|
const { useModelConfig } = useAssistantContext();
|
484
524
|
const registerModelConfigProvider = useModelConfig(
|
485
525
|
(s) => s.registerModelConfigProvider
|
486
526
|
);
|
487
|
-
|
527
|
+
useEffect4(() => {
|
488
528
|
const config = {
|
489
529
|
system: instruction
|
490
530
|
};
|
@@ -493,13 +533,13 @@ var useAssistantInstructions = (instruction) => {
|
|
493
533
|
};
|
494
534
|
|
495
535
|
// src/primitive-hooks/actionBar/useActionBarCopy.tsx
|
496
|
-
import { useCallback as
|
536
|
+
import { useCallback as useCallback5 } from "react";
|
497
537
|
|
498
538
|
// src/utils/combined/useCombinedStore.ts
|
499
539
|
import { useMemo as useMemo2 } from "react";
|
500
540
|
|
501
541
|
// src/utils/combined/createCombinedStore.ts
|
502
|
-
import { useSyncExternalStore
|
542
|
+
import { useSyncExternalStore } from "react";
|
503
543
|
var createCombinedStore = (stores) => {
|
504
544
|
const subscribe = (callback) => {
|
505
545
|
const unsubscribes = stores.map((store) => store.subscribe(callback));
|
@@ -511,7 +551,7 @@ var createCombinedStore = (stores) => {
|
|
511
551
|
};
|
512
552
|
return (selector) => {
|
513
553
|
const getSnapshot = () => selector(...stores.map((store) => store.getState()));
|
514
|
-
return
|
554
|
+
return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
|
515
555
|
};
|
516
556
|
};
|
517
557
|
|
@@ -540,7 +580,7 @@ var useActionBarCopy = ({
|
|
540
580
|
return !c.isEditing && m.message.content.some((c2) => c2.type === "text" && c2.text.length > 0);
|
541
581
|
}
|
542
582
|
);
|
543
|
-
const callback =
|
583
|
+
const callback = useCallback5(() => {
|
544
584
|
const { message } = useMessage.getState();
|
545
585
|
const { setIsCopied } = useMessageUtils.getState();
|
546
586
|
const { isEditing, value: composerValue } = useEditComposer.getState();
|
@@ -555,14 +595,14 @@ var useActionBarCopy = ({
|
|
555
595
|
};
|
556
596
|
|
557
597
|
// src/primitive-hooks/actionBar/useActionBarEdit.tsx
|
558
|
-
import { useCallback as
|
598
|
+
import { useCallback as useCallback6 } from "react";
|
559
599
|
var useActionBarEdit = () => {
|
560
600
|
const { useMessage, useEditComposer } = useMessageContext();
|
561
601
|
const disabled = useCombinedStore(
|
562
602
|
[useMessage, useEditComposer],
|
563
603
|
(m, c) => m.message.role !== "user" || c.isEditing
|
564
604
|
);
|
565
|
-
const callback =
|
605
|
+
const callback = useCallback6(() => {
|
566
606
|
const { edit } = useEditComposer.getState();
|
567
607
|
edit();
|
568
608
|
}, [useEditComposer]);
|
@@ -571,7 +611,7 @@ var useActionBarEdit = () => {
|
|
571
611
|
};
|
572
612
|
|
573
613
|
// src/primitive-hooks/actionBar/useActionBarReload.tsx
|
574
|
-
import { useCallback as
|
614
|
+
import { useCallback as useCallback7 } from "react";
|
575
615
|
var useActionBarReload = () => {
|
576
616
|
const { useThread, useThreadActions, useComposer, useViewport } = useThreadContext();
|
577
617
|
const { useMessage } = useMessageContext();
|
@@ -579,7 +619,7 @@ var useActionBarReload = () => {
|
|
579
619
|
[useThread, useMessage],
|
580
620
|
(t, m) => t.isRunning || m.message.role !== "assistant"
|
581
621
|
);
|
582
|
-
const callback =
|
622
|
+
const callback = useCallback7(() => {
|
583
623
|
const { parentId } = useMessage.getState();
|
584
624
|
useThreadActions.getState().startRun(parentId);
|
585
625
|
useViewport.getState().scrollToBottom();
|
@@ -597,7 +637,7 @@ var useBranchPickerCount = () => {
|
|
597
637
|
};
|
598
638
|
|
599
639
|
// src/primitive-hooks/branchPicker/useBranchPickerNext.tsx
|
600
|
-
import { useCallback as
|
640
|
+
import { useCallback as useCallback8 } from "react";
|
601
641
|
var useBranchPickerNext = () => {
|
602
642
|
const { useThreadActions } = useThreadContext();
|
603
643
|
const { useMessage, useEditComposer } = useMessageContext();
|
@@ -605,7 +645,7 @@ var useBranchPickerNext = () => {
|
|
605
645
|
[useMessage, useEditComposer],
|
606
646
|
(m, c) => c.isEditing || m.branches.indexOf(m.message.id) + 1 >= m.branches.length
|
607
647
|
);
|
608
|
-
const callback =
|
648
|
+
const callback = useCallback8(() => {
|
609
649
|
const { message, branches } = useMessage.getState();
|
610
650
|
useThreadActions.getState().switchToBranch(branches[branches.indexOf(message.id) + 1]);
|
611
651
|
}, [useThreadActions, useMessage]);
|
@@ -621,7 +661,7 @@ var useBranchPickerNumber = () => {
|
|
621
661
|
};
|
622
662
|
|
623
663
|
// src/primitive-hooks/branchPicker/useBranchPickerPrevious.tsx
|
624
|
-
import { useCallback as
|
664
|
+
import { useCallback as useCallback9 } from "react";
|
625
665
|
var useBranchPickerPrevious = () => {
|
626
666
|
const { useThreadActions } = useThreadContext();
|
627
667
|
const { useMessage, useEditComposer } = useMessageContext();
|
@@ -629,7 +669,7 @@ var useBranchPickerPrevious = () => {
|
|
629
669
|
[useMessage, useEditComposer],
|
630
670
|
(m, c) => c.isEditing || m.branches.indexOf(m.message.id) <= 0
|
631
671
|
);
|
632
|
-
const callback =
|
672
|
+
const callback = useCallback9(() => {
|
633
673
|
const { message, branches } = useMessage.getState();
|
634
674
|
useThreadActions.getState().switchToBranch(branches[branches.indexOf(message.id) - 1]);
|
635
675
|
}, [useThreadActions, useMessage]);
|
@@ -638,11 +678,11 @@ var useBranchPickerPrevious = () => {
|
|
638
678
|
};
|
639
679
|
|
640
680
|
// src/primitive-hooks/composer/useComposerCancel.tsx
|
641
|
-
import { useCallback as
|
681
|
+
import { useCallback as useCallback10 } from "react";
|
642
682
|
var useComposerCancel = () => {
|
643
683
|
const { useComposer } = useComposerContext();
|
644
684
|
const disabled = useComposer((c) => !c.canCancel);
|
645
|
-
const callback =
|
685
|
+
const callback = useCallback10(() => {
|
646
686
|
const { cancel } = useComposer.getState();
|
647
687
|
cancel();
|
648
688
|
}, [useComposer]);
|
@@ -661,12 +701,12 @@ var useComposerIf = (props) => {
|
|
661
701
|
};
|
662
702
|
|
663
703
|
// src/primitive-hooks/composer/useComposerSend.tsx
|
664
|
-
import { useCallback as
|
704
|
+
import { useCallback as useCallback11 } from "react";
|
665
705
|
var useComposerSend = () => {
|
666
706
|
const { useViewport, useComposer: useNewComposer } = useThreadContext();
|
667
707
|
const { useComposer } = useComposerContext();
|
668
708
|
const disabled = useComposer((c) => !c.isEditing || c.value.length === 0);
|
669
|
-
const callback =
|
709
|
+
const callback = useCallback11(() => {
|
670
710
|
const composerState = useComposer.getState();
|
671
711
|
if (!composerState.isEditing) return;
|
672
712
|
composerState.send();
|
@@ -755,11 +795,11 @@ var useThreadEmpty = () => {
|
|
755
795
|
};
|
756
796
|
|
757
797
|
// src/primitive-hooks/thread/useThreadScrollToBottom.tsx
|
758
|
-
import { useCallback as
|
798
|
+
import { useCallback as useCallback12 } from "react";
|
759
799
|
var useThreadScrollToBottom = () => {
|
760
800
|
const { useComposer, useViewport } = useThreadContext();
|
761
801
|
const isAtBottom = useViewport((s) => s.isAtBottom);
|
762
|
-
const handleScrollToBottom =
|
802
|
+
const handleScrollToBottom = useCallback12(() => {
|
763
803
|
useViewport.getState().scrollToBottom();
|
764
804
|
useComposer.getState().focus();
|
765
805
|
}, [useViewport, useComposer]);
|
@@ -768,7 +808,7 @@ var useThreadScrollToBottom = () => {
|
|
768
808
|
};
|
769
809
|
|
770
810
|
// src/primitive-hooks/thread/useThreadSuggestion.tsx
|
771
|
-
import { useCallback as
|
811
|
+
import { useCallback as useCallback13 } from "react";
|
772
812
|
var useThreadSuggestion = ({
|
773
813
|
prompt,
|
774
814
|
autoSend
|
@@ -776,7 +816,7 @@ var useThreadSuggestion = ({
|
|
776
816
|
const { useThread, useComposer } = useThreadContext();
|
777
817
|
const append = useAppendMessage();
|
778
818
|
const disabled = useThread((t) => t.isRunning);
|
779
|
-
const callback =
|
819
|
+
const callback = useCallback13(() => {
|
780
820
|
const thread = useThread.getState();
|
781
821
|
const composer = useComposer.getState();
|
782
822
|
if (autoSend && !thread.isRunning) {
|
@@ -914,11 +954,11 @@ import { composeEventHandlers as composeEventHandlers2 } from "@radix-ui/primiti
|
|
914
954
|
|
915
955
|
// src/utils/hooks/useOnComposerFocus.tsx
|
916
956
|
import { useCallbackRef } from "@radix-ui/react-use-callback-ref";
|
917
|
-
import { useEffect as
|
957
|
+
import { useEffect as useEffect5 } from "react";
|
918
958
|
var useOnComposerFocus = (callback) => {
|
919
959
|
const callbackRef = useCallbackRef(callback);
|
920
960
|
const { useComposer } = useThreadContext();
|
921
|
-
|
961
|
+
useEffect5(() => {
|
922
962
|
return useComposer.getState().onFocus(() => {
|
923
963
|
callbackRef();
|
924
964
|
});
|
@@ -1080,26 +1120,6 @@ import {
|
|
1080
1120
|
forwardRef as forwardRef6,
|
1081
1121
|
useCallback as useCallback14
|
1082
1122
|
} from "react";
|
1083
|
-
|
1084
|
-
// src/utils/hooks/useManagedRef.ts
|
1085
|
-
import { useCallback as useCallback13, useRef as useRef3 } from "react";
|
1086
|
-
var useManagedRef = (callback) => {
|
1087
|
-
const cleanupRef = useRef3();
|
1088
|
-
const ref = useCallback13(
|
1089
|
-
(el) => {
|
1090
|
-
if (cleanupRef.current) {
|
1091
|
-
cleanupRef.current();
|
1092
|
-
}
|
1093
|
-
if (el) {
|
1094
|
-
cleanupRef.current = callback(el);
|
1095
|
-
}
|
1096
|
-
},
|
1097
|
-
[callback]
|
1098
|
-
);
|
1099
|
-
return ref;
|
1100
|
-
};
|
1101
|
-
|
1102
|
-
// src/primitives/message/MessageRoot.tsx
|
1103
1123
|
import { useComposedRefs } from "@radix-ui/react-compose-refs";
|
1104
1124
|
import { jsx as jsx12 } from "react/jsx-runtime";
|
1105
1125
|
var useIsHoveringRef = () => {
|
@@ -1146,8 +1166,8 @@ MessagePrimitiveIf.displayName = "MessagePrimitive.If";
|
|
1146
1166
|
import { memo as memo2 } from "react";
|
1147
1167
|
|
1148
1168
|
// src/context/providers/ContentPartProvider.tsx
|
1149
|
-
import { useEffect as
|
1150
|
-
import { create as
|
1169
|
+
import { useEffect as useEffect6, useState as useState4 } from "react";
|
1170
|
+
import { create as create10 } from "zustand";
|
1151
1171
|
import { jsx as jsx13 } from "react/jsx-runtime";
|
1152
1172
|
var DONE_STATUS = { type: "done" };
|
1153
1173
|
var syncContentPart = ({ message }, useContentPart, partIndex) => {
|
@@ -1167,13 +1187,13 @@ var syncContentPart = ({ message }, useContentPart, partIndex) => {
|
|
1167
1187
|
var useContentPartContext2 = (partIndex) => {
|
1168
1188
|
const { useMessage } = useMessageContext();
|
1169
1189
|
const [context] = useState4(() => {
|
1170
|
-
const useContentPart =
|
1190
|
+
const useContentPart = create10(
|
1171
1191
|
() => ({})
|
1172
1192
|
);
|
1173
1193
|
syncContentPart(useMessage.getState(), useContentPart, partIndex);
|
1174
1194
|
return { useContentPart };
|
1175
1195
|
});
|
1176
|
-
|
1196
|
+
useEffect6(() => {
|
1177
1197
|
syncContentPart(useMessage.getState(), context.useContentPart, partIndex);
|
1178
1198
|
return useMessage.subscribe((message) => {
|
1179
1199
|
syncContentPart(message, context.useContentPart, partIndex);
|
@@ -1194,7 +1214,7 @@ import { Primitive as Primitive4 } from "@radix-ui/react-primitive";
|
|
1194
1214
|
import { forwardRef as forwardRef7 } from "react";
|
1195
1215
|
|
1196
1216
|
// src/utils/hooks/useSmooth.tsx
|
1197
|
-
import { useEffect as
|
1217
|
+
import { useEffect as useEffect7, useRef as useRef3, useState as useState5 } from "react";
|
1198
1218
|
var TextStreamAnimator = class {
|
1199
1219
|
constructor(currentText, setText) {
|
1200
1220
|
this.currentText = currentText;
|
@@ -1242,12 +1262,12 @@ var TextStreamAnimator = class {
|
|
1242
1262
|
var useSmooth = (text, smooth = false) => {
|
1243
1263
|
const { useMessage } = useMessageContext();
|
1244
1264
|
const id = useMessage((m) => m.message.id);
|
1245
|
-
const idRef =
|
1265
|
+
const idRef = useRef3(id);
|
1246
1266
|
const [displayedText, setDisplayedText] = useState5(text);
|
1247
1267
|
const [animatorRef] = useState5(
|
1248
1268
|
new TextStreamAnimator(text, setDisplayedText)
|
1249
1269
|
);
|
1250
|
-
|
1270
|
+
useEffect7(() => {
|
1251
1271
|
if (!smooth) {
|
1252
1272
|
animatorRef.stop();
|
1253
1273
|
return;
|
@@ -1263,7 +1283,7 @@ var useSmooth = (text, smooth = false) => {
|
|
1263
1283
|
animatorRef.targetText = text;
|
1264
1284
|
animatorRef.start();
|
1265
1285
|
}, [animatorRef, id, smooth, text]);
|
1266
|
-
|
1286
|
+
useEffect7(() => {
|
1267
1287
|
return () => {
|
1268
1288
|
animatorRef.stop();
|
1269
1289
|
};
|
@@ -1449,8 +1469,8 @@ import { Slot } from "@radix-ui/react-slot";
|
|
1449
1469
|
import {
|
1450
1470
|
forwardRef as forwardRef11,
|
1451
1471
|
useCallback as useCallback15,
|
1452
|
-
useEffect as
|
1453
|
-
useRef as
|
1472
|
+
useEffect as useEffect8,
|
1473
|
+
useRef as useRef4
|
1454
1474
|
} from "react";
|
1455
1475
|
import TextareaAutosize from "react-textarea-autosize";
|
1456
1476
|
import { useEscapeKeydown } from "@radix-ui/react-use-escape-keydown";
|
@@ -1464,7 +1484,7 @@ var ComposerPrimitiveInput = forwardRef11(
|
|
1464
1484
|
return c.value;
|
1465
1485
|
});
|
1466
1486
|
const Component = asChild ? Slot : TextareaAutosize;
|
1467
|
-
const textareaRef =
|
1487
|
+
const textareaRef = useRef4(null);
|
1468
1488
|
const ref = useComposedRefs2(forwardedRef, textareaRef);
|
1469
1489
|
useEscapeKeydown((e) => {
|
1470
1490
|
const composer = useComposer.getState();
|
@@ -1493,7 +1513,7 @@ var ComposerPrimitiveInput = forwardRef11(
|
|
1493
1513
|
textareaRef.current.value.length
|
1494
1514
|
);
|
1495
1515
|
}, [autoFocusEnabled]);
|
1496
|
-
|
1516
|
+
useEffect8(() => focus(), [focus]);
|
1497
1517
|
useOnComposerFocus(() => {
|
1498
1518
|
if (type === "new") {
|
1499
1519
|
focus();
|
@@ -1610,7 +1630,7 @@ import { forwardRef as forwardRef14 } from "react";
|
|
1610
1630
|
|
1611
1631
|
// src/primitive-hooks/thread/useThreadViewportAutoScroll.tsx
|
1612
1632
|
import { useComposedRefs as useComposedRefs3 } from "@radix-ui/react-compose-refs";
|
1613
|
-
import { useRef as
|
1633
|
+
import { useRef as useRef5 } from "react";
|
1614
1634
|
|
1615
1635
|
// src/utils/hooks/useOnResizeContent.tsx
|
1616
1636
|
import { useCallbackRef as useCallbackRef2 } from "@radix-ui/react-use-callback-ref";
|
@@ -1654,11 +1674,11 @@ var useOnResizeContent = (callback) => {
|
|
1654
1674
|
|
1655
1675
|
// src/utils/hooks/useOnScrollToBottom.tsx
|
1656
1676
|
import { useCallbackRef as useCallbackRef3 } from "@radix-ui/react-use-callback-ref";
|
1657
|
-
import { useEffect as
|
1677
|
+
import { useEffect as useEffect9 } from "react";
|
1658
1678
|
var useOnScrollToBottom = (callback) => {
|
1659
1679
|
const callbackRef = useCallbackRef3(callback);
|
1660
1680
|
const { useViewport } = useThreadContext();
|
1661
|
-
|
1681
|
+
useEffect9(() => {
|
1662
1682
|
return useViewport.getState().onScrollToBottom(() => {
|
1663
1683
|
callbackRef();
|
1664
1684
|
});
|
@@ -1669,10 +1689,10 @@ var useOnScrollToBottom = (callback) => {
|
|
1669
1689
|
var useThreadViewportAutoScroll = ({
|
1670
1690
|
autoScroll = true
|
1671
1691
|
}) => {
|
1672
|
-
const divRef =
|
1692
|
+
const divRef = useRef5(null);
|
1673
1693
|
const { useViewport } = useThreadContext();
|
1674
|
-
const lastScrollTop =
|
1675
|
-
const isScrollingToBottomRef =
|
1694
|
+
const lastScrollTop = useRef5(0);
|
1695
|
+
const isScrollingToBottomRef = useRef5(false);
|
1676
1696
|
const scrollToBottom = (behavior) => {
|
1677
1697
|
const div = divRef.current;
|
1678
1698
|
if (!div || !autoScroll) return;
|
@@ -1731,15 +1751,15 @@ ThreadPrimitiveViewport.displayName = "ThreadPrimitive.Viewport";
|
|
1731
1751
|
import { memo as memo3 } from "react";
|
1732
1752
|
|
1733
1753
|
// src/context/providers/MessageProvider.tsx
|
1734
|
-
import { useEffect as
|
1735
|
-
import { create as
|
1754
|
+
import { useEffect as useEffect10, useState as useState6 } from "react";
|
1755
|
+
import { create as create13 } from "zustand";
|
1736
1756
|
|
1737
1757
|
// src/context/stores/EditComposer.ts
|
1738
|
-
import { create as
|
1758
|
+
import { create as create11 } from "zustand";
|
1739
1759
|
var makeEditComposerStore = ({
|
1740
1760
|
onEdit,
|
1741
1761
|
onSend
|
1742
|
-
}) =>
|
1762
|
+
}) => create11()((set, get, store) => ({
|
1743
1763
|
...makeBaseComposer(set, get, store),
|
1744
1764
|
canCancel: false,
|
1745
1765
|
isEditing: false,
|
@@ -1758,8 +1778,8 @@ var makeEditComposerStore = ({
|
|
1758
1778
|
}));
|
1759
1779
|
|
1760
1780
|
// src/context/stores/MessageUtils.ts
|
1761
|
-
import { create as
|
1762
|
-
var makeMessageUtilsStore = () =>
|
1781
|
+
import { create as create12 } from "zustand";
|
1782
|
+
var makeMessageUtilsStore = () => create12((set) => ({
|
1763
1783
|
isCopied: false,
|
1764
1784
|
setIsCopied: (value) => {
|
1765
1785
|
set({ isCopied: value });
|
@@ -1794,7 +1814,7 @@ var syncMessage = (messages, getBranches, useMessage, messageIndex) => {
|
|
1794
1814
|
var useMessageContext2 = (messageIndex) => {
|
1795
1815
|
const { useThreadMessages, useThreadActions } = useThreadContext();
|
1796
1816
|
const [context] = useState6(() => {
|
1797
|
-
const useMessage =
|
1817
|
+
const useMessage = create13(() => ({}));
|
1798
1818
|
const useMessageUtils = makeMessageUtilsStore();
|
1799
1819
|
const useEditComposer = makeEditComposerStore({
|
1800
1820
|
onEdit: () => {
|
@@ -1830,7 +1850,7 @@ var useMessageContext2 = (messageIndex) => {
|
|
1830
1850
|
);
|
1831
1851
|
return { useMessage, useMessageUtils, useEditComposer };
|
1832
1852
|
});
|
1833
|
-
|
1853
|
+
useEffect10(() => {
|
1834
1854
|
return useThreadMessages.subscribe((thread) => {
|
1835
1855
|
syncMessage(
|
1836
1856
|
thread,
|
@@ -2220,38 +2240,284 @@ var TooltipIconButton = forwardRef17(({ children, tooltip, side = "bottom", ...r
|
|
2220
2240
|
});
|
2221
2241
|
TooltipIconButton.displayName = "TooltipIconButton";
|
2222
2242
|
|
2223
|
-
// src/runtimes/edge/
|
2224
|
-
|
2225
|
-
|
2226
|
-
|
2227
|
-
|
2228
|
-
|
2229
|
-
|
2230
|
-
|
2231
|
-
|
2232
|
-
|
2233
|
-
|
2234
|
-
|
2235
|
-
|
2243
|
+
// src/runtimes/edge/converters/toLanguageModelMessages.ts
|
2244
|
+
var assistantMessageSplitter = () => {
|
2245
|
+
const stash = [];
|
2246
|
+
let assistantMessage = {
|
2247
|
+
role: "assistant",
|
2248
|
+
content: []
|
2249
|
+
};
|
2250
|
+
let toolMessage = {
|
2251
|
+
role: "tool",
|
2252
|
+
content: []
|
2253
|
+
};
|
2254
|
+
return {
|
2255
|
+
addTextContentPart: (part) => {
|
2256
|
+
if (toolMessage.content.length > 0) {
|
2257
|
+
stash.push(assistantMessage);
|
2258
|
+
stash.push(toolMessage);
|
2259
|
+
assistantMessage = {
|
2260
|
+
role: "assistant",
|
2261
|
+
content: []
|
2262
|
+
};
|
2263
|
+
toolMessage = {
|
2264
|
+
role: "tool",
|
2265
|
+
content: []
|
2266
|
+
};
|
2267
|
+
}
|
2268
|
+
assistantMessage.content.push(part);
|
2269
|
+
},
|
2270
|
+
addToolCallPart: (part) => {
|
2271
|
+
assistantMessage.content.push({
|
2272
|
+
type: "tool-call",
|
2273
|
+
toolCallId: part.toolCallId,
|
2274
|
+
toolName: part.toolName,
|
2275
|
+
args: part.args
|
2276
|
+
});
|
2277
|
+
if (part.result) {
|
2278
|
+
toolMessage.content.push({
|
2279
|
+
type: "tool-result",
|
2280
|
+
toolCallId: part.toolCallId,
|
2281
|
+
toolName: part.toolName,
|
2282
|
+
result: part.result
|
2283
|
+
// isError
|
2284
|
+
});
|
2285
|
+
}
|
2286
|
+
},
|
2287
|
+
getMessages: () => {
|
2288
|
+
if (toolMessage.content.length > 0) {
|
2289
|
+
return [...stash, assistantMessage, toolMessage];
|
2290
|
+
}
|
2291
|
+
return [...stash, assistantMessage];
|
2292
|
+
}
|
2293
|
+
};
|
2294
|
+
};
|
2295
|
+
function toLanguageModelMessages(message) {
|
2296
|
+
return message.flatMap((message2) => {
|
2297
|
+
const role = message2.role;
|
2298
|
+
switch (role) {
|
2299
|
+
case "system": {
|
2300
|
+
return [{ role: "system", content: message2.content[0].text }];
|
2301
|
+
}
|
2302
|
+
case "user": {
|
2303
|
+
const msg = {
|
2304
|
+
role: "user",
|
2305
|
+
content: message2.content.map(
|
2306
|
+
(part) => {
|
2307
|
+
const type = part.type;
|
2308
|
+
switch (type) {
|
2309
|
+
case "text": {
|
2310
|
+
return part;
|
2311
|
+
}
|
2312
|
+
case "image": {
|
2313
|
+
return {
|
2314
|
+
type: "image",
|
2315
|
+
image: new URL(part.image)
|
2316
|
+
};
|
2317
|
+
}
|
2318
|
+
default: {
|
2319
|
+
const unhandledType = type;
|
2320
|
+
throw new Error(
|
2321
|
+
`Unspported content part type: ${unhandledType}`
|
2322
|
+
);
|
2323
|
+
}
|
2324
|
+
}
|
2325
|
+
}
|
2326
|
+
)
|
2327
|
+
};
|
2328
|
+
return [msg];
|
2329
|
+
}
|
2330
|
+
case "assistant": {
|
2331
|
+
const splitter = assistantMessageSplitter();
|
2332
|
+
for (const part of message2.content) {
|
2333
|
+
const type = part.type;
|
2334
|
+
switch (type) {
|
2335
|
+
case "text": {
|
2336
|
+
splitter.addTextContentPart(part);
|
2337
|
+
break;
|
2338
|
+
}
|
2339
|
+
case "tool-call": {
|
2340
|
+
splitter.addToolCallPart(part);
|
2341
|
+
break;
|
2342
|
+
}
|
2343
|
+
default: {
|
2344
|
+
const unhandledType = type;
|
2345
|
+
throw new Error(`Unhandled content part type: ${unhandledType}`);
|
2346
|
+
}
|
2347
|
+
}
|
2236
2348
|
}
|
2237
|
-
return
|
2238
|
-
}
|
2239
|
-
|
2349
|
+
return splitter.getMessages();
|
2350
|
+
}
|
2351
|
+
default: {
|
2352
|
+
const unhandledRole = role;
|
2353
|
+
throw new Error(`Unknown message role: ${unhandledRole}`);
|
2354
|
+
}
|
2355
|
+
}
|
2240
2356
|
});
|
2241
|
-
}
|
2357
|
+
}
|
2242
2358
|
|
2243
|
-
// src/runtimes/edge/converters/
|
2244
|
-
|
2245
|
-
|
2246
|
-
|
2247
|
-
|
2248
|
-
|
2249
|
-
|
2250
|
-
|
2251
|
-
|
2252
|
-
|
2253
|
-
|
2254
|
-
|
2359
|
+
// src/runtimes/edge/converters/fromLanguageModelMessages.ts
|
2360
|
+
var fromLanguageModelMessages = (lm, { mergeRoundtrips }) => {
|
2361
|
+
const messages = [];
|
2362
|
+
for (const lmMessage of lm) {
|
2363
|
+
const role = lmMessage.role;
|
2364
|
+
switch (role) {
|
2365
|
+
case "system": {
|
2366
|
+
messages.push({
|
2367
|
+
role: "system",
|
2368
|
+
content: [
|
2369
|
+
{
|
2370
|
+
type: "text",
|
2371
|
+
text: lmMessage.content
|
2372
|
+
}
|
2373
|
+
]
|
2374
|
+
});
|
2375
|
+
break;
|
2376
|
+
}
|
2377
|
+
case "user": {
|
2378
|
+
messages.push({
|
2379
|
+
role: "user",
|
2380
|
+
content: lmMessage.content.map((part) => {
|
2381
|
+
const type = part.type;
|
2382
|
+
switch (type) {
|
2383
|
+
case "text": {
|
2384
|
+
return {
|
2385
|
+
type: "text",
|
2386
|
+
text: part.text
|
2387
|
+
};
|
2388
|
+
}
|
2389
|
+
case "image": {
|
2390
|
+
if (part.image instanceof URL) {
|
2391
|
+
return {
|
2392
|
+
type: "image",
|
2393
|
+
image: part.image.href
|
2394
|
+
};
|
2395
|
+
}
|
2396
|
+
throw new Error("Only images with URL data are supported");
|
2397
|
+
}
|
2398
|
+
default: {
|
2399
|
+
const unhandledType = type;
|
2400
|
+
throw new Error(`Unknown content part type: ${unhandledType}`);
|
2401
|
+
}
|
2402
|
+
}
|
2403
|
+
})
|
2404
|
+
});
|
2405
|
+
break;
|
2406
|
+
}
|
2407
|
+
case "assistant": {
|
2408
|
+
const newContent = lmMessage.content.map((part) => {
|
2409
|
+
if (part.type === "tool-call") {
|
2410
|
+
return {
|
2411
|
+
type: "tool-call",
|
2412
|
+
toolCallId: part.toolCallId,
|
2413
|
+
toolName: part.toolName,
|
2414
|
+
argsText: JSON.stringify(part.args),
|
2415
|
+
args: part.args
|
2416
|
+
};
|
2417
|
+
}
|
2418
|
+
return part;
|
2419
|
+
});
|
2420
|
+
if (mergeRoundtrips) {
|
2421
|
+
const previousMessage = messages[messages.length - 1];
|
2422
|
+
if (previousMessage?.role === "assistant") {
|
2423
|
+
previousMessage.content.push(...newContent);
|
2424
|
+
break;
|
2425
|
+
}
|
2426
|
+
}
|
2427
|
+
messages.push({
|
2428
|
+
role: "assistant",
|
2429
|
+
content: newContent
|
2430
|
+
});
|
2431
|
+
break;
|
2432
|
+
}
|
2433
|
+
case "tool": {
|
2434
|
+
const previousMessage = messages[messages.length - 1];
|
2435
|
+
if (previousMessage?.role !== "assistant")
|
2436
|
+
throw new Error(
|
2437
|
+
"A tool message must be preceded by an assistant message."
|
2438
|
+
);
|
2439
|
+
for (const tool of lmMessage.content) {
|
2440
|
+
const toolCall = previousMessage.content.find(
|
2441
|
+
(c) => c.type === "tool-call" && c.toolCallId === tool.toolCallId
|
2442
|
+
);
|
2443
|
+
if (!toolCall)
|
2444
|
+
throw new Error("Received tool result for an unknown tool call.");
|
2445
|
+
if (toolCall.toolName !== tool.toolName)
|
2446
|
+
throw new Error("Tool call name mismatch.");
|
2447
|
+
toolCall.result = tool.result;
|
2448
|
+
if (tool.isError) {
|
2449
|
+
toolCall.isError = true;
|
2450
|
+
}
|
2451
|
+
}
|
2452
|
+
break;
|
2453
|
+
}
|
2454
|
+
default: {
|
2455
|
+
const unhandledRole = role;
|
2456
|
+
throw new Error(`Unknown message role: ${unhandledRole}`);
|
2457
|
+
}
|
2458
|
+
}
|
2459
|
+
}
|
2460
|
+
return messages;
|
2461
|
+
};
|
2462
|
+
|
2463
|
+
// src/runtimes/edge/converters/fromCoreMessage.ts
|
2464
|
+
var fromCoreMessages = (message) => {
|
2465
|
+
return message.map((message2) => {
|
2466
|
+
return {
|
2467
|
+
id: generateId(),
|
2468
|
+
createdAt: /* @__PURE__ */ new Date(),
|
2469
|
+
...message2.role === "assistant" ? {
|
2470
|
+
status: { type: "done" }
|
2471
|
+
} : void 0,
|
2472
|
+
...message2
|
2473
|
+
};
|
2474
|
+
});
|
2475
|
+
};
|
2476
|
+
|
2477
|
+
// src/runtimes/edge/converters/toCoreMessages.ts
|
2478
|
+
var toCoreMessages = (message) => {
|
2479
|
+
return message.map((message2) => {
|
2480
|
+
return {
|
2481
|
+
role: message2.role,
|
2482
|
+
content: message2.content.map((part) => {
|
2483
|
+
if (part.type === "ui") throw new Error("UI parts are not supported");
|
2484
|
+
if (part.type === "tool-call") {
|
2485
|
+
const { argsText, ...rest } = part;
|
2486
|
+
return rest;
|
2487
|
+
}
|
2488
|
+
return part;
|
2489
|
+
})
|
2490
|
+
};
|
2491
|
+
});
|
2492
|
+
};
|
2493
|
+
|
2494
|
+
// src/runtimes/edge/converters/fromLanguageModelTools.ts
|
2495
|
+
var fromLanguageModelTools = (tools) => {
|
2496
|
+
return Object.fromEntries(
|
2497
|
+
tools.map((tool) => [
|
2498
|
+
tool.name,
|
2499
|
+
{
|
2500
|
+
description: tool.description,
|
2501
|
+
parameters: tool.parameters
|
2502
|
+
}
|
2503
|
+
])
|
2504
|
+
);
|
2505
|
+
};
|
2506
|
+
|
2507
|
+
// src/runtimes/edge/converters/toLanguageModelTools.ts
|
2508
|
+
import { z as z2 } from "zod";
|
2509
|
+
import zodToJsonSchema from "zod-to-json-schema";
|
2510
|
+
var toLanguageModelTools = (tools) => {
|
2511
|
+
return Object.entries(tools).map(([name, tool]) => ({
|
2512
|
+
type: "function",
|
2513
|
+
name,
|
2514
|
+
...tool.description ? { description: tool.description } : void 0,
|
2515
|
+
parameters: tool.parameters instanceof z2.ZodType ? zodToJsonSchema(tool.parameters) : tool.parameters
|
2516
|
+
}));
|
2517
|
+
};
|
2518
|
+
|
2519
|
+
// src/runtimes/edge/useEdgeRuntime.ts
|
2520
|
+
import { useState as useState7 } from "react";
|
2255
2521
|
|
2256
2522
|
// src/runtimes/edge/streams/assistantDecoderStream.ts
|
2257
2523
|
function assistantDecoderStream() {
|
@@ -2929,14 +3195,17 @@ var EdgeChatAdapter = class {
|
|
2929
3195
|
body: JSON.stringify({
|
2930
3196
|
system: config.system,
|
2931
3197
|
messages: toCoreMessages(messages),
|
2932
|
-
tools: toLanguageModelTools(
|
2933
|
-
config.tools
|
2934
|
-
),
|
3198
|
+
tools: config.tools ? toLanguageModelTools(config.tools) : [],
|
2935
3199
|
...config.callSettings,
|
2936
3200
|
...config.config
|
2937
3201
|
}),
|
2938
3202
|
signal: abortSignal
|
2939
3203
|
});
|
3204
|
+
if (result.status !== 200) {
|
3205
|
+
throw new Error(
|
3206
|
+
`Edge runtime returned status ${result.status}: ${await result.text()}`
|
3207
|
+
);
|
3208
|
+
}
|
2940
3209
|
const stream = result.body.pipeThrough(new TextDecoderStream()).pipeThrough(chunkByLineStream()).pipeThrough(assistantDecoderStream()).pipeThrough(toolResultStream(config.tools)).pipeThrough(runResultStream(initialContent));
|
2941
3210
|
let message;
|
2942
3211
|
let update;
|
@@ -2989,240 +3258,6 @@ var useEdgeRuntime = ({
|
|
2989
3258
|
return useLocalRuntime(adapter, { initialMessages });
|
2990
3259
|
};
|
2991
3260
|
|
2992
|
-
// src/runtimes/edge/converters/toLanguageModelMessages.ts
|
2993
|
-
var assistantMessageSplitter = () => {
|
2994
|
-
const stash = [];
|
2995
|
-
let assistantMessage = {
|
2996
|
-
role: "assistant",
|
2997
|
-
content: []
|
2998
|
-
};
|
2999
|
-
let toolMessage = {
|
3000
|
-
role: "tool",
|
3001
|
-
content: []
|
3002
|
-
};
|
3003
|
-
return {
|
3004
|
-
addTextContentPart: (part) => {
|
3005
|
-
if (toolMessage.content.length > 0) {
|
3006
|
-
stash.push(assistantMessage);
|
3007
|
-
stash.push(toolMessage);
|
3008
|
-
assistantMessage = {
|
3009
|
-
role: "assistant",
|
3010
|
-
content: []
|
3011
|
-
};
|
3012
|
-
toolMessage = {
|
3013
|
-
role: "tool",
|
3014
|
-
content: []
|
3015
|
-
};
|
3016
|
-
}
|
3017
|
-
assistantMessage.content.push(part);
|
3018
|
-
},
|
3019
|
-
addToolCallPart: (part) => {
|
3020
|
-
assistantMessage.content.push({
|
3021
|
-
type: "tool-call",
|
3022
|
-
toolCallId: part.toolCallId,
|
3023
|
-
toolName: part.toolName,
|
3024
|
-
args: part.args
|
3025
|
-
});
|
3026
|
-
if (part.result) {
|
3027
|
-
toolMessage.content.push({
|
3028
|
-
type: "tool-result",
|
3029
|
-
toolCallId: part.toolCallId,
|
3030
|
-
toolName: part.toolName,
|
3031
|
-
result: part.result
|
3032
|
-
// isError
|
3033
|
-
});
|
3034
|
-
}
|
3035
|
-
},
|
3036
|
-
getMessages: () => {
|
3037
|
-
if (toolMessage.content.length > 0) {
|
3038
|
-
return [...stash, assistantMessage, toolMessage];
|
3039
|
-
}
|
3040
|
-
return [...stash, assistantMessage];
|
3041
|
-
}
|
3042
|
-
};
|
3043
|
-
};
|
3044
|
-
function toLanguageModelMessages(message) {
|
3045
|
-
return message.flatMap((message2) => {
|
3046
|
-
const role = message2.role;
|
3047
|
-
switch (role) {
|
3048
|
-
case "system": {
|
3049
|
-
return [{ role: "system", content: message2.content[0].text }];
|
3050
|
-
}
|
3051
|
-
case "user": {
|
3052
|
-
const msg = {
|
3053
|
-
role: "user",
|
3054
|
-
content: message2.content.map(
|
3055
|
-
(part) => {
|
3056
|
-
const type = part.type;
|
3057
|
-
switch (type) {
|
3058
|
-
case "text": {
|
3059
|
-
return part;
|
3060
|
-
}
|
3061
|
-
case "image": {
|
3062
|
-
return {
|
3063
|
-
type: "image",
|
3064
|
-
image: new URL(part.image)
|
3065
|
-
};
|
3066
|
-
}
|
3067
|
-
default: {
|
3068
|
-
const unhandledType = type;
|
3069
|
-
throw new Error(
|
3070
|
-
`Unspported content part type: ${unhandledType}`
|
3071
|
-
);
|
3072
|
-
}
|
3073
|
-
}
|
3074
|
-
}
|
3075
|
-
)
|
3076
|
-
};
|
3077
|
-
return [msg];
|
3078
|
-
}
|
3079
|
-
case "assistant": {
|
3080
|
-
const splitter = assistantMessageSplitter();
|
3081
|
-
for (const part of message2.content) {
|
3082
|
-
const type = part.type;
|
3083
|
-
switch (type) {
|
3084
|
-
case "text": {
|
3085
|
-
splitter.addTextContentPart(part);
|
3086
|
-
break;
|
3087
|
-
}
|
3088
|
-
case "tool-call": {
|
3089
|
-
splitter.addToolCallPart(part);
|
3090
|
-
break;
|
3091
|
-
}
|
3092
|
-
default: {
|
3093
|
-
const unhandledType = type;
|
3094
|
-
throw new Error(`Unhandled content part type: ${unhandledType}`);
|
3095
|
-
}
|
3096
|
-
}
|
3097
|
-
}
|
3098
|
-
return splitter.getMessages();
|
3099
|
-
}
|
3100
|
-
default: {
|
3101
|
-
const unhandledRole = role;
|
3102
|
-
throw new Error(`Unknown message role: ${unhandledRole}`);
|
3103
|
-
}
|
3104
|
-
}
|
3105
|
-
});
|
3106
|
-
}
|
3107
|
-
|
3108
|
-
// src/runtimes/edge/converters/fromLanguageModelMessages.ts
|
3109
|
-
var fromLanguageModelMessages = (lm, mergeRoundtrips) => {
|
3110
|
-
const messages = [];
|
3111
|
-
for (const lmMessage of lm) {
|
3112
|
-
const role = lmMessage.role;
|
3113
|
-
switch (role) {
|
3114
|
-
case "system": {
|
3115
|
-
messages.push({
|
3116
|
-
role: "system",
|
3117
|
-
content: [
|
3118
|
-
{
|
3119
|
-
type: "text",
|
3120
|
-
text: lmMessage.content
|
3121
|
-
}
|
3122
|
-
]
|
3123
|
-
});
|
3124
|
-
break;
|
3125
|
-
}
|
3126
|
-
case "user": {
|
3127
|
-
messages.push({
|
3128
|
-
role: "user",
|
3129
|
-
content: lmMessage.content.map((part) => {
|
3130
|
-
const type = part.type;
|
3131
|
-
switch (type) {
|
3132
|
-
case "text": {
|
3133
|
-
return {
|
3134
|
-
type: "text",
|
3135
|
-
text: part.text
|
3136
|
-
};
|
3137
|
-
}
|
3138
|
-
case "image": {
|
3139
|
-
if (part.image instanceof URL) {
|
3140
|
-
return {
|
3141
|
-
type: "image",
|
3142
|
-
image: part.image.href
|
3143
|
-
};
|
3144
|
-
}
|
3145
|
-
throw new Error("Only images with URL data are supported");
|
3146
|
-
}
|
3147
|
-
default: {
|
3148
|
-
const unhandledType = type;
|
3149
|
-
throw new Error(`Unknown content part type: ${unhandledType}`);
|
3150
|
-
}
|
3151
|
-
}
|
3152
|
-
})
|
3153
|
-
});
|
3154
|
-
break;
|
3155
|
-
}
|
3156
|
-
case "assistant": {
|
3157
|
-
const newContent = lmMessage.content.map((part) => {
|
3158
|
-
if (part.type === "tool-call") {
|
3159
|
-
return {
|
3160
|
-
type: "tool-call",
|
3161
|
-
toolCallId: part.toolCallId,
|
3162
|
-
toolName: part.toolName,
|
3163
|
-
argsText: JSON.stringify(part.args),
|
3164
|
-
args: part.args
|
3165
|
-
};
|
3166
|
-
}
|
3167
|
-
return part;
|
3168
|
-
});
|
3169
|
-
if (mergeRoundtrips) {
|
3170
|
-
const previousMessage = messages[messages.length - 1];
|
3171
|
-
if (previousMessage?.role === "assistant") {
|
3172
|
-
previousMessage.content.push(...newContent);
|
3173
|
-
break;
|
3174
|
-
}
|
3175
|
-
}
|
3176
|
-
messages.push({
|
3177
|
-
role: "assistant",
|
3178
|
-
content: newContent
|
3179
|
-
});
|
3180
|
-
break;
|
3181
|
-
}
|
3182
|
-
case "tool": {
|
3183
|
-
const previousMessage = messages[messages.length - 1];
|
3184
|
-
if (previousMessage?.role !== "assistant")
|
3185
|
-
throw new Error(
|
3186
|
-
"A tool message must be preceded by an assistant message."
|
3187
|
-
);
|
3188
|
-
for (const tool of lmMessage.content) {
|
3189
|
-
const toolCall = previousMessage.content.find(
|
3190
|
-
(c) => c.type === "tool-call" && c.toolCallId === tool.toolCallId
|
3191
|
-
);
|
3192
|
-
if (!toolCall)
|
3193
|
-
throw new Error("Received tool result for an unknown tool call.");
|
3194
|
-
if (toolCall.toolName !== tool.toolName)
|
3195
|
-
throw new Error("Tool call name mismatch.");
|
3196
|
-
toolCall.result = tool.result;
|
3197
|
-
if (tool.isError) {
|
3198
|
-
toolCall.isError = true;
|
3199
|
-
}
|
3200
|
-
}
|
3201
|
-
break;
|
3202
|
-
}
|
3203
|
-
default: {
|
3204
|
-
const unhandledRole = role;
|
3205
|
-
throw new Error(`Unknown message role: ${unhandledRole}`);
|
3206
|
-
}
|
3207
|
-
}
|
3208
|
-
}
|
3209
|
-
return messages;
|
3210
|
-
};
|
3211
|
-
|
3212
|
-
// src/runtimes/edge/converters/fromCoreMessage.ts
|
3213
|
-
var fromCoreMessages = (message) => {
|
3214
|
-
return message.map((message2) => {
|
3215
|
-
return {
|
3216
|
-
id: generateId(),
|
3217
|
-
createdAt: /* @__PURE__ */ new Date(),
|
3218
|
-
...message2.role === "assistant" ? {
|
3219
|
-
status: { type: "done" }
|
3220
|
-
} : void 0,
|
3221
|
-
...message2
|
3222
|
-
};
|
3223
|
-
});
|
3224
|
-
};
|
3225
|
-
|
3226
3261
|
// src/runtimes/local/LocalRuntime.tsx
|
3227
3262
|
var LocalRuntime = class extends BaseAssistantRuntime {
|
3228
3263
|
_proxyConfigProvider;
|
@@ -4098,9 +4133,12 @@ export {
|
|
4098
4133
|
user_message_default as UserMessage,
|
4099
4134
|
fromCoreMessages,
|
4100
4135
|
fromLanguageModelMessages,
|
4136
|
+
fromLanguageModelTools,
|
4101
4137
|
makeAssistantTool,
|
4102
4138
|
makeAssistantToolUI,
|
4139
|
+
toCoreMessages,
|
4103
4140
|
toLanguageModelMessages,
|
4141
|
+
toLanguageModelTools,
|
4104
4142
|
useActionBarCopy,
|
4105
4143
|
useActionBarEdit,
|
4106
4144
|
useActionBarReload,
|