@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/index.js CHANGED
@@ -55,9 +55,12 @@ __export(src_exports, {
55
55
  UserMessage: () => user_message_default,
56
56
  fromCoreMessages: () => fromCoreMessages,
57
57
  fromLanguageModelMessages: () => fromLanguageModelMessages,
58
+ fromLanguageModelTools: () => fromLanguageModelTools,
58
59
  makeAssistantTool: () => makeAssistantTool,
59
60
  makeAssistantToolUI: () => makeAssistantToolUI,
61
+ toCoreMessages: () => toCoreMessages,
60
62
  toLanguageModelMessages: () => toLanguageModelMessages,
63
+ toLanguageModelTools: () => toLanguageModelTools,
61
64
  useActionBarCopy: () => useActionBarCopy,
62
65
  useActionBarEdit: () => useActionBarEdit,
63
66
  useActionBarReload: () => useActionBarReload,
@@ -93,10 +96,10 @@ __export(src_exports, {
93
96
  module.exports = __toCommonJS(src_exports);
94
97
 
95
98
  // src/context/providers/AssistantRuntimeProvider.tsx
96
- var import_react5 = require("react");
99
+ var import_react6 = require("react");
97
100
 
98
101
  // src/context/providers/AssistantProvider.tsx
99
- var import_react4 = require("react");
102
+ var import_react5 = require("react");
100
103
 
101
104
  // src/context/react/AssistantContext.ts
102
105
  var import_react = require("react");
@@ -154,6 +157,18 @@ ${config.system}`;
154
157
  acc.tools[name] = tool;
155
158
  }
156
159
  }
160
+ if (config.config) {
161
+ acc.config = {
162
+ ...acc.config,
163
+ ...config.config
164
+ };
165
+ }
166
+ if (config.callSettings) {
167
+ acc.callSettings = {
168
+ ...acc.callSettings,
169
+ ...config.callSettings
170
+ };
171
+ }
157
172
  return acc;
158
173
  }, {});
159
174
  };
@@ -218,7 +233,7 @@ var makeAssistantToolUIsStore = () => (0, import_zustand2.create)((set) => {
218
233
  });
219
234
 
220
235
  // src/context/providers/ThreadProvider.tsx
221
- var import_react3 = require("react");
236
+ var import_react4 = require("react");
222
237
 
223
238
  // src/context/react/ThreadContext.ts
224
239
  var import_react2 = require("react");
@@ -284,7 +299,7 @@ var makeComposerStore = (useThreadMessages, useThreadActions) => {
284
299
  var import_zustand4 = require("zustand");
285
300
  var makeThreadStore = (runtimeRef) => {
286
301
  return (0, import_zustand4.create)(() => ({
287
- isRunning: runtimeRef.current.isRunning
302
+ isRunning: runtimeRef.getState().isRunning
288
303
  }));
289
304
  };
290
305
 
@@ -310,19 +325,18 @@ var makeThreadViewportStore = () => {
310
325
 
311
326
  // src/context/stores/ThreadActions.ts
312
327
  var import_zustand6 = require("zustand");
313
- var makeThreadActionStore = (runtimeRef) => {
328
+ var makeThreadActionStore = (runtimeStore) => {
314
329
  return (0, import_zustand6.create)(
315
330
  () => Object.freeze({
316
331
  get capabilities() {
317
- return runtimeRef.current.capabilities;
332
+ return runtimeStore.getState().capabilities;
318
333
  },
319
- getBranches: (messageId) => runtimeRef.current.getBranches(messageId),
320
- switchToBranch: (branchId) => runtimeRef.current.switchToBranch(branchId),
321
- startRun: (parentId) => runtimeRef.current.startRun(parentId),
322
- append: (message) => runtimeRef.current.append(message),
323
- cancelRun: () => runtimeRef.current.cancelRun(),
324
- addToolResult: (options) => runtimeRef.current.addToolResult(options),
325
- getRuntime: () => runtimeRef.current
334
+ getBranches: (messageId) => runtimeStore.getState().getBranches(messageId),
335
+ switchToBranch: (branchId) => runtimeStore.getState().switchToBranch(branchId),
336
+ startRun: (parentId) => runtimeStore.getState().startRun(parentId),
337
+ append: (message) => runtimeStore.getState().append(message),
338
+ cancelRun: () => runtimeStore.getState().cancelRun(),
339
+ addToolResult: (options) => runtimeStore.getState().addToolResult(options)
326
340
  })
327
341
  );
328
342
  };
@@ -330,60 +344,96 @@ var makeThreadActionStore = (runtimeRef) => {
330
344
  // src/context/stores/ThreadMessages.ts
331
345
  var import_zustand7 = require("zustand");
332
346
  var makeThreadMessagesStore = (runtimeRef) => {
333
- return (0, import_zustand7.create)(() => runtimeRef.current.messages);
347
+ return (0, import_zustand7.create)(() => runtimeRef.getState().messages);
348
+ };
349
+
350
+ // src/context/stores/ThreadRuntime.tsx
351
+ var import_zustand8 = require("zustand");
352
+ var makeThreadRuntimeStore = (runtime) => {
353
+ return (0, import_zustand8.create)(() => runtime);
354
+ };
355
+
356
+ // src/utils/hooks/useManagedRef.ts
357
+ var import_react3 = require("react");
358
+ var useManagedRef = (callback) => {
359
+ const cleanupRef = (0, import_react3.useRef)();
360
+ const ref = (0, import_react3.useCallback)(
361
+ (el) => {
362
+ if (cleanupRef.current) {
363
+ cleanupRef.current();
364
+ }
365
+ if (el) {
366
+ cleanupRef.current = callback(el);
367
+ }
368
+ },
369
+ [callback]
370
+ );
371
+ return ref;
334
372
  };
335
373
 
336
374
  // src/context/providers/ThreadProvider.tsx
337
375
  var import_jsx_runtime = require("react/jsx-runtime");
338
376
  var ThreadProvider = ({
339
377
  children,
340
- runtime
378
+ provider
341
379
  }) => {
342
- const runtimeRef = (0, import_react3.useRef)(runtime.thread);
343
- const [context] = (0, import_react3.useState)(() => {
344
- const useThread = makeThreadStore(runtimeRef);
345
- const useThreadMessages = makeThreadMessagesStore(runtimeRef);
346
- const useThreadActions = makeThreadActionStore(runtimeRef);
380
+ const [context] = (0, import_react4.useState)(() => {
381
+ const useThreadRuntime = makeThreadRuntimeStore(provider.thread);
382
+ const useThread = makeThreadStore(useThreadRuntime);
383
+ const useThreadMessages = makeThreadMessagesStore(useThreadRuntime);
384
+ const useThreadActions = makeThreadActionStore(useThreadRuntime);
347
385
  const useViewport = makeThreadViewportStore();
348
386
  const useComposer = makeComposerStore(useThreadMessages, useThreadActions);
349
387
  return {
350
388
  useThread,
389
+ useThreadRuntime,
351
390
  useThreadMessages,
352
391
  useThreadActions,
353
392
  useComposer,
354
393
  useViewport
355
394
  };
356
395
  });
357
- const thread = (0, import_react3.useSyncExternalStore)(
358
- (0, import_react3.useCallback)((c) => runtime.subscribe(c), [runtime]),
359
- () => runtime.thread,
360
- () => runtime.thread
396
+ const threadRef = useManagedRef(
397
+ (0, import_react4.useCallback)(
398
+ (thread) => {
399
+ const onThreadUpdate = () => {
400
+ context.useThread.setState(
401
+ Object.freeze({
402
+ isRunning: context.useThreadRuntime.getState().isRunning
403
+ }),
404
+ true
405
+ );
406
+ context.useThreadMessages.setState(thread.messages, true);
407
+ };
408
+ onThreadUpdate();
409
+ return thread.subscribe(onThreadUpdate);
410
+ },
411
+ [context]
412
+ )
361
413
  );
362
- (0, import_react3.useInsertionEffect)(() => {
363
- runtimeRef.current = thread;
364
- });
365
- (0, import_react3.useEffect)(() => {
366
- const onThreadUpdate = () => {
367
- context.useThread.setState(
368
- Object.freeze({
369
- isRunning: runtimeRef.current.isRunning
370
- }),
371
- true
372
- );
373
- context.useThreadMessages.setState(Object.freeze(runtimeRef.current.messages), true);
414
+ (0, import_react4.useInsertionEffect)(() => {
415
+ const unsubscribe = provider.subscribe(() => {
416
+ context.useThreadRuntime.setState(provider.thread, true);
417
+ threadRef(provider.thread);
418
+ });
419
+ threadRef(provider.thread);
420
+ return () => {
421
+ unsubscribe();
422
+ threadRef(null);
374
423
  };
375
- onThreadUpdate();
376
- return thread.subscribe(onThreadUpdate);
377
- }, [context, thread]);
424
+ }, [provider, context]);
425
+ const Synchronizer = context.useThreadRuntime(
426
+ (t) => t.unstable_synchronizer
427
+ );
378
428
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(ThreadContext.Provider, { value: context, children: [
379
- thread.unstable_synchronizer && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(thread.unstable_synchronizer, {}),
429
+ Synchronizer && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Synchronizer, {}),
380
430
  children
381
431
  ] });
382
432
  };
383
433
 
384
434
  // src/context/stores/AssistantActions.tsx
385
- var import_zustand8 = require("zustand");
386
- var makeAssistantActionsStore = (runtimeRef) => (0, import_zustand8.create)(
435
+ var import_zustand9 = require("zustand");
436
+ var makeAssistantActionsStore = (runtimeRef) => (0, import_zustand9.create)(
387
437
  () => Object.freeze({
388
438
  switchToThread: () => runtimeRef.current.switchToThread(null),
389
439
  getRuntime: () => runtimeRef.current
@@ -393,21 +443,21 @@ var makeAssistantActionsStore = (runtimeRef) => (0, import_zustand8.create)(
393
443
  // src/context/providers/AssistantProvider.tsx
394
444
  var import_jsx_runtime2 = require("react/jsx-runtime");
395
445
  var AssistantProvider = ({ children, runtime }) => {
396
- const runtimeRef = (0, import_react4.useRef)(runtime);
397
- (0, import_react4.useInsertionEffect)(() => {
446
+ const runtimeRef = (0, import_react5.useRef)(runtime);
447
+ (0, import_react5.useInsertionEffect)(() => {
398
448
  runtimeRef.current = runtime;
399
449
  });
400
- const [context] = (0, import_react4.useState)(() => {
450
+ const [context] = (0, import_react5.useState)(() => {
401
451
  const useModelConfig = makeAssistantModelConfigStore();
402
452
  const useToolUIs = makeAssistantToolUIsStore();
403
453
  const useAssistantActions = makeAssistantActionsStore(runtimeRef);
404
454
  return { useModelConfig, useToolUIs, useAssistantActions };
405
455
  });
406
456
  const getModelConfig = context.useModelConfig();
407
- (0, import_react4.useEffect)(() => {
457
+ (0, import_react5.useEffect)(() => {
408
458
  return runtime.registerModelConfigProvider(getModelConfig);
409
459
  }, [runtime, getModelConfig]);
410
- return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(AssistantContext.Provider, { value: context, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(ThreadProvider, { runtime, children }) });
460
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(AssistantContext.Provider, { value: context, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(ThreadProvider, { provider: runtime, children }) });
411
461
  };
412
462
 
413
463
  // src/context/providers/AssistantRuntimeProvider.tsx
@@ -415,16 +465,16 @@ var import_jsx_runtime3 = require("react/jsx-runtime");
415
465
  var AssistantRuntimeProviderImpl = ({ children, runtime }) => {
416
466
  return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(AssistantProvider, { runtime, children });
417
467
  };
418
- var AssistantRuntimeProvider = (0, import_react5.memo)(AssistantRuntimeProviderImpl);
468
+ var AssistantRuntimeProvider = (0, import_react6.memo)(AssistantRuntimeProviderImpl);
419
469
 
420
470
  // src/context/react/ComposerContext.ts
421
- var import_react7 = require("react");
471
+ var import_react8 = require("react");
422
472
 
423
473
  // src/context/react/MessageContext.ts
424
- var import_react6 = require("react");
425
- var MessageContext = (0, import_react6.createContext)(null);
474
+ var import_react7 = require("react");
475
+ var MessageContext = (0, import_react7.createContext)(null);
426
476
  function useMessageContext(options) {
427
- const context = (0, import_react6.useContext)(MessageContext);
477
+ const context = (0, import_react7.useContext)(MessageContext);
428
478
  if (!options?.optional && !context)
429
479
  throw new Error(
430
480
  "This component can only be used inside a component passed to <ThreadPrimitive.Messages components={...} />."
@@ -436,7 +486,7 @@ function useMessageContext(options) {
436
486
  var useComposerContext = () => {
437
487
  const { useComposer } = useThreadContext();
438
488
  const { useEditComposer } = useMessageContext({ optional: true }) ?? {};
439
- return (0, import_react7.useMemo)(
489
+ return (0, import_react8.useMemo)(
440
490
  () => ({
441
491
  useComposer: useEditComposer ?? useComposer,
442
492
  type: useEditComposer ? "edit" : "new"
@@ -446,12 +496,12 @@ var useComposerContext = () => {
446
496
  };
447
497
 
448
498
  // src/context/react/ContentPartContext.ts
449
- var import_react8 = require("react");
450
- var ContentPartContext = (0, import_react8.createContext)(
499
+ var import_react9 = require("react");
500
+ var ContentPartContext = (0, import_react9.createContext)(
451
501
  null
452
502
  );
453
503
  function useContentPartContext(options) {
454
- const context = (0, import_react8.useContext)(ContentPartContext);
504
+ const context = (0, import_react9.useContext)(ContentPartContext);
455
505
  if (!options?.optional && !context)
456
506
  throw new Error(
457
507
  "This component can only be used inside a component passed to <MessagePrimitive.Content components={...} >."
@@ -460,7 +510,7 @@ function useContentPartContext(options) {
460
510
  }
461
511
 
462
512
  // src/hooks/useAppendMessage.tsx
463
- var import_react9 = require("react");
513
+ var import_react10 = require("react");
464
514
  var toAppendMessage = (useThreadMessages, message) => {
465
515
  if (typeof message === "string") {
466
516
  return {
@@ -477,7 +527,7 @@ var toAppendMessage = (useThreadMessages, message) => {
477
527
  };
478
528
  var useAppendMessage = () => {
479
529
  const { useThreadMessages, useThreadActions, useViewport, useComposer } = useThreadContext();
480
- const append = (0, import_react9.useCallback)(
530
+ const append = (0, import_react10.useCallback)(
481
531
  (message) => {
482
532
  const appendMessage = toAppendMessage(useThreadMessages, message);
483
533
  useThreadActions.getState().append(appendMessage);
@@ -490,11 +540,11 @@ var useAppendMessage = () => {
490
540
  };
491
541
 
492
542
  // src/hooks/useSwitchToNewThread.tsx
493
- var import_react10 = require("react");
543
+ var import_react11 = require("react");
494
544
  var useSwitchToNewThread = () => {
495
545
  const { useAssistantActions } = useAssistantContext();
496
546
  const { useComposer } = useThreadContext();
497
- const switchToNewThread = (0, import_react10.useCallback)(() => {
547
+ const switchToNewThread = (0, import_react11.useCallback)(() => {
498
548
  useAssistantActions.getState().switchToThread(null);
499
549
  useComposer.getState().focus();
500
550
  }, [useAssistantActions, useComposer]);
@@ -502,14 +552,14 @@ var useSwitchToNewThread = () => {
502
552
  };
503
553
 
504
554
  // src/model-config/useAssistantTool.tsx
505
- var import_react11 = require("react");
555
+ var import_react12 = require("react");
506
556
  var useAssistantTool = (tool) => {
507
557
  const { useModelConfig, useToolUIs } = useAssistantContext();
508
558
  const registerModelConfigProvider = useModelConfig(
509
559
  (s) => s.registerModelConfigProvider
510
560
  );
511
561
  const setToolUI = useToolUIs((s) => s.setToolUI);
512
- (0, import_react11.useEffect)(() => {
562
+ (0, import_react12.useEffect)(() => {
513
563
  const { toolName, render, ...rest } = tool;
514
564
  const config = {
515
565
  tools: {
@@ -537,11 +587,11 @@ var makeAssistantTool = (tool) => {
537
587
  };
538
588
 
539
589
  // src/model-config/useAssistantToolUI.tsx
540
- var import_react12 = require("react");
590
+ var import_react13 = require("react");
541
591
  var useAssistantToolUI = (tool) => {
542
592
  const { useToolUIs } = useAssistantContext();
543
593
  const setToolUI = useToolUIs((s) => s.setToolUI);
544
- (0, import_react12.useEffect)(() => {
594
+ (0, import_react13.useEffect)(() => {
545
595
  if (!tool) return;
546
596
  const { toolName, render } = tool;
547
597
  return setToolUI(toolName, render);
@@ -558,13 +608,13 @@ var makeAssistantToolUI = (tool) => {
558
608
  };
559
609
 
560
610
  // src/model-config/useAssistantInstructions.tsx
561
- var import_react13 = require("react");
611
+ var import_react14 = require("react");
562
612
  var useAssistantInstructions = (instruction) => {
563
613
  const { useModelConfig } = useAssistantContext();
564
614
  const registerModelConfigProvider = useModelConfig(
565
615
  (s) => s.registerModelConfigProvider
566
616
  );
567
- (0, import_react13.useEffect)(() => {
617
+ (0, import_react14.useEffect)(() => {
568
618
  const config = {
569
619
  system: instruction
570
620
  };
@@ -573,13 +623,13 @@ var useAssistantInstructions = (instruction) => {
573
623
  };
574
624
 
575
625
  // src/primitive-hooks/actionBar/useActionBarCopy.tsx
576
- var import_react16 = require("react");
626
+ var import_react17 = require("react");
577
627
 
578
628
  // src/utils/combined/useCombinedStore.ts
579
- var import_react15 = require("react");
629
+ var import_react16 = require("react");
580
630
 
581
631
  // src/utils/combined/createCombinedStore.ts
582
- var import_react14 = require("react");
632
+ var import_react15 = require("react");
583
633
  var createCombinedStore = (stores) => {
584
634
  const subscribe = (callback) => {
585
635
  const unsubscribes = stores.map((store) => store.subscribe(callback));
@@ -591,13 +641,13 @@ var createCombinedStore = (stores) => {
591
641
  };
592
642
  return (selector) => {
593
643
  const getSnapshot = () => selector(...stores.map((store) => store.getState()));
594
- return (0, import_react14.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
644
+ return (0, import_react15.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
595
645
  };
596
646
  };
597
647
 
598
648
  // src/utils/combined/useCombinedStore.ts
599
649
  var useCombinedStore = (stores, selector) => {
600
- const useCombined = (0, import_react15.useMemo)(() => createCombinedStore(stores), stores);
650
+ const useCombined = (0, import_react16.useMemo)(() => createCombinedStore(stores), stores);
601
651
  return useCombined(selector);
602
652
  };
603
653
 
@@ -620,7 +670,7 @@ var useActionBarCopy = ({
620
670
  return !c.isEditing && m.message.content.some((c2) => c2.type === "text" && c2.text.length > 0);
621
671
  }
622
672
  );
623
- const callback = (0, import_react16.useCallback)(() => {
673
+ const callback = (0, import_react17.useCallback)(() => {
624
674
  const { message } = useMessage.getState();
625
675
  const { setIsCopied } = useMessageUtils.getState();
626
676
  const { isEditing, value: composerValue } = useEditComposer.getState();
@@ -635,14 +685,14 @@ var useActionBarCopy = ({
635
685
  };
636
686
 
637
687
  // src/primitive-hooks/actionBar/useActionBarEdit.tsx
638
- var import_react17 = require("react");
688
+ var import_react18 = require("react");
639
689
  var useActionBarEdit = () => {
640
690
  const { useMessage, useEditComposer } = useMessageContext();
641
691
  const disabled = useCombinedStore(
642
692
  [useMessage, useEditComposer],
643
693
  (m, c) => m.message.role !== "user" || c.isEditing
644
694
  );
645
- const callback = (0, import_react17.useCallback)(() => {
695
+ const callback = (0, import_react18.useCallback)(() => {
646
696
  const { edit } = useEditComposer.getState();
647
697
  edit();
648
698
  }, [useEditComposer]);
@@ -651,7 +701,7 @@ var useActionBarEdit = () => {
651
701
  };
652
702
 
653
703
  // src/primitive-hooks/actionBar/useActionBarReload.tsx
654
- var import_react18 = require("react");
704
+ var import_react19 = require("react");
655
705
  var useActionBarReload = () => {
656
706
  const { useThread, useThreadActions, useComposer, useViewport } = useThreadContext();
657
707
  const { useMessage } = useMessageContext();
@@ -659,7 +709,7 @@ var useActionBarReload = () => {
659
709
  [useThread, useMessage],
660
710
  (t, m) => t.isRunning || m.message.role !== "assistant"
661
711
  );
662
- const callback = (0, import_react18.useCallback)(() => {
712
+ const callback = (0, import_react19.useCallback)(() => {
663
713
  const { parentId } = useMessage.getState();
664
714
  useThreadActions.getState().startRun(parentId);
665
715
  useViewport.getState().scrollToBottom();
@@ -677,7 +727,7 @@ var useBranchPickerCount = () => {
677
727
  };
678
728
 
679
729
  // src/primitive-hooks/branchPicker/useBranchPickerNext.tsx
680
- var import_react19 = require("react");
730
+ var import_react20 = require("react");
681
731
  var useBranchPickerNext = () => {
682
732
  const { useThreadActions } = useThreadContext();
683
733
  const { useMessage, useEditComposer } = useMessageContext();
@@ -685,7 +735,7 @@ var useBranchPickerNext = () => {
685
735
  [useMessage, useEditComposer],
686
736
  (m, c) => c.isEditing || m.branches.indexOf(m.message.id) + 1 >= m.branches.length
687
737
  );
688
- const callback = (0, import_react19.useCallback)(() => {
738
+ const callback = (0, import_react20.useCallback)(() => {
689
739
  const { message, branches } = useMessage.getState();
690
740
  useThreadActions.getState().switchToBranch(branches[branches.indexOf(message.id) + 1]);
691
741
  }, [useThreadActions, useMessage]);
@@ -701,7 +751,7 @@ var useBranchPickerNumber = () => {
701
751
  };
702
752
 
703
753
  // src/primitive-hooks/branchPicker/useBranchPickerPrevious.tsx
704
- var import_react20 = require("react");
754
+ var import_react21 = require("react");
705
755
  var useBranchPickerPrevious = () => {
706
756
  const { useThreadActions } = useThreadContext();
707
757
  const { useMessage, useEditComposer } = useMessageContext();
@@ -709,7 +759,7 @@ var useBranchPickerPrevious = () => {
709
759
  [useMessage, useEditComposer],
710
760
  (m, c) => c.isEditing || m.branches.indexOf(m.message.id) <= 0
711
761
  );
712
- const callback = (0, import_react20.useCallback)(() => {
762
+ const callback = (0, import_react21.useCallback)(() => {
713
763
  const { message, branches } = useMessage.getState();
714
764
  useThreadActions.getState().switchToBranch(branches[branches.indexOf(message.id) - 1]);
715
765
  }, [useThreadActions, useMessage]);
@@ -718,11 +768,11 @@ var useBranchPickerPrevious = () => {
718
768
  };
719
769
 
720
770
  // src/primitive-hooks/composer/useComposerCancel.tsx
721
- var import_react21 = require("react");
771
+ var import_react22 = require("react");
722
772
  var useComposerCancel = () => {
723
773
  const { useComposer } = useComposerContext();
724
774
  const disabled = useComposer((c) => !c.canCancel);
725
- const callback = (0, import_react21.useCallback)(() => {
775
+ const callback = (0, import_react22.useCallback)(() => {
726
776
  const { cancel } = useComposer.getState();
727
777
  cancel();
728
778
  }, [useComposer]);
@@ -741,12 +791,12 @@ var useComposerIf = (props) => {
741
791
  };
742
792
 
743
793
  // src/primitive-hooks/composer/useComposerSend.tsx
744
- var import_react22 = require("react");
794
+ var import_react23 = require("react");
745
795
  var useComposerSend = () => {
746
796
  const { useViewport, useComposer: useNewComposer } = useThreadContext();
747
797
  const { useComposer } = useComposerContext();
748
798
  const disabled = useComposer((c) => !c.isEditing || c.value.length === 0);
749
- const callback = (0, import_react22.useCallback)(() => {
799
+ const callback = (0, import_react23.useCallback)(() => {
750
800
  const composerState = useComposer.getState();
751
801
  if (!composerState.isEditing) return;
752
802
  composerState.send();
@@ -835,11 +885,11 @@ var useThreadEmpty = () => {
835
885
  };
836
886
 
837
887
  // src/primitive-hooks/thread/useThreadScrollToBottom.tsx
838
- var import_react23 = require("react");
888
+ var import_react24 = require("react");
839
889
  var useThreadScrollToBottom = () => {
840
890
  const { useComposer, useViewport } = useThreadContext();
841
891
  const isAtBottom = useViewport((s) => s.isAtBottom);
842
- const handleScrollToBottom = (0, import_react23.useCallback)(() => {
892
+ const handleScrollToBottom = (0, import_react24.useCallback)(() => {
843
893
  useViewport.getState().scrollToBottom();
844
894
  useComposer.getState().focus();
845
895
  }, [useViewport, useComposer]);
@@ -848,7 +898,7 @@ var useThreadScrollToBottom = () => {
848
898
  };
849
899
 
850
900
  // src/primitive-hooks/thread/useThreadSuggestion.tsx
851
- var import_react24 = require("react");
901
+ var import_react25 = require("react");
852
902
  var useThreadSuggestion = ({
853
903
  prompt,
854
904
  autoSend
@@ -856,7 +906,7 @@ var useThreadSuggestion = ({
856
906
  const { useThread, useComposer } = useThreadContext();
857
907
  const append = useAppendMessage();
858
908
  const disabled = useThread((t) => t.isRunning);
859
- const callback = (0, import_react24.useCallback)(() => {
909
+ const callback = (0, import_react25.useCallback)(() => {
860
910
  const thread = useThread.getState();
861
911
  const composer = useComposer.getState();
862
912
  if (autoSend && !thread.isRunning) {
@@ -881,7 +931,7 @@ __export(actionBar_exports, {
881
931
 
882
932
  // src/primitives/actionBar/ActionBarRoot.tsx
883
933
  var import_react_primitive = require("@radix-ui/react-primitive");
884
- var import_react25 = require("react");
934
+ var import_react26 = require("react");
885
935
 
886
936
  // src/primitives/actionBar/useActionBarFloatStatus.tsx
887
937
  var useActionBarFloatStatus = ({
@@ -907,7 +957,7 @@ var useActionBarFloatStatus = ({
907
957
 
908
958
  // src/primitives/actionBar/ActionBarRoot.tsx
909
959
  var import_jsx_runtime4 = require("react/jsx-runtime");
910
- var ActionBarPrimitiveRoot = (0, import_react25.forwardRef)(({ hideWhenRunning, autohide, autohideFloat, ...rest }, ref) => {
960
+ var ActionBarPrimitiveRoot = (0, import_react26.forwardRef)(({ hideWhenRunning, autohide, autohideFloat, ...rest }, ref) => {
911
961
  const hideAndfloatStatus = useActionBarFloatStatus({
912
962
  hideWhenRunning,
913
963
  autohide,
@@ -926,12 +976,12 @@ var ActionBarPrimitiveRoot = (0, import_react25.forwardRef)(({ hideWhenRunning,
926
976
  ActionBarPrimitiveRoot.displayName = "ActionBarPrimitive.Root";
927
977
 
928
978
  // src/utils/createActionButton.tsx
929
- var import_react26 = require("react");
979
+ var import_react27 = require("react");
930
980
  var import_react_primitive2 = require("@radix-ui/react-primitive");
931
981
  var import_primitive = require("@radix-ui/primitive");
932
982
  var import_jsx_runtime5 = require("react/jsx-runtime");
933
983
  var createActionButton = (displayName, useActionButton, forwardProps = []) => {
934
- const ActionButton = (0, import_react26.forwardRef)((props, forwardedRef) => {
984
+ const ActionButton = (0, import_react27.forwardRef)((props, forwardedRef) => {
935
985
  const forwardedProps = {};
936
986
  const primitiveProps = {};
937
987
  Object.keys(props).forEach((key) => {
@@ -988,17 +1038,17 @@ __export(assistantModal_exports, {
988
1038
  });
989
1039
 
990
1040
  // src/primitives/assistantModal/AssistantModalRoot.tsx
991
- var import_react28 = require("react");
1041
+ var import_react29 = require("react");
992
1042
  var PopoverPrimitive2 = __toESM(require("@radix-ui/react-popover"));
993
1043
  var import_primitive2 = require("@radix-ui/primitive");
994
1044
 
995
1045
  // src/utils/hooks/useOnComposerFocus.tsx
996
1046
  var import_react_use_callback_ref = require("@radix-ui/react-use-callback-ref");
997
- var import_react27 = require("react");
1047
+ var import_react28 = require("react");
998
1048
  var useOnComposerFocus = (callback) => {
999
1049
  const callbackRef = (0, import_react_use_callback_ref.useCallbackRef)(callback);
1000
1050
  const { useComposer } = useThreadContext();
1001
- (0, import_react27.useEffect)(() => {
1051
+ (0, import_react28.useEffect)(() => {
1002
1052
  return useComposer.getState().onFocus(() => {
1003
1053
  callbackRef();
1004
1054
  });
@@ -1012,7 +1062,7 @@ var usePopoverScope = PopoverPrimitive.createPopoverScope();
1012
1062
  // src/primitives/assistantModal/AssistantModalRoot.tsx
1013
1063
  var import_jsx_runtime6 = require("react/jsx-runtime");
1014
1064
  var useAssistantModalOpenState = (defaultOpen = false) => {
1015
- const state = (0, import_react28.useState)(defaultOpen);
1065
+ const state = (0, import_react29.useState)(defaultOpen);
1016
1066
  const [, setOpen] = state;
1017
1067
  useOnComposerFocus(() => {
1018
1068
  setOpen(true);
@@ -1041,10 +1091,10 @@ var AssistantModalPrimitiveRoot = ({
1041
1091
  AssistantModalPrimitiveRoot.displayName = "AssistantModalPrimitive.Root";
1042
1092
 
1043
1093
  // src/primitives/assistantModal/AssistantModalTrigger.tsx
1044
- var import_react29 = require("react");
1094
+ var import_react30 = require("react");
1045
1095
  var PopoverPrimitive3 = __toESM(require("@radix-ui/react-popover"));
1046
1096
  var import_jsx_runtime7 = require("react/jsx-runtime");
1047
- var AssistantModalPrimitiveTrigger = (0, import_react29.forwardRef)(
1097
+ var AssistantModalPrimitiveTrigger = (0, import_react30.forwardRef)(
1048
1098
  ({
1049
1099
  __scopeAssistantModal,
1050
1100
  ...rest
@@ -1056,11 +1106,11 @@ var AssistantModalPrimitiveTrigger = (0, import_react29.forwardRef)(
1056
1106
  AssistantModalPrimitiveTrigger.displayName = "AssistantModalPrimitive.Trigger";
1057
1107
 
1058
1108
  // src/primitives/assistantModal/AssistantModalContent.tsx
1059
- var import_react30 = require("react");
1109
+ var import_react31 = require("react");
1060
1110
  var PopoverPrimitive4 = __toESM(require("@radix-ui/react-popover"));
1061
1111
  var import_primitive3 = require("@radix-ui/primitive");
1062
1112
  var import_jsx_runtime8 = require("react/jsx-runtime");
1063
- var AssistantModalPrimitiveContent = (0, import_react30.forwardRef)(
1113
+ var AssistantModalPrimitiveContent = (0, import_react31.forwardRef)(
1064
1114
  ({
1065
1115
  __scopeAssistantModal,
1066
1116
  side,
@@ -1089,10 +1139,10 @@ var AssistantModalPrimitiveContent = (0, import_react30.forwardRef)(
1089
1139
  AssistantModalPrimitiveContent.displayName = "AssistantModalPrimitive.Content";
1090
1140
 
1091
1141
  // src/primitives/assistantModal/AssistantModalAnchor.tsx
1092
- var import_react31 = require("react");
1142
+ var import_react32 = require("react");
1093
1143
  var PopoverPrimitive5 = __toESM(require("@radix-ui/react-popover"));
1094
1144
  var import_jsx_runtime9 = require("react/jsx-runtime");
1095
- var AssistantModalPrimitiveAnchor = (0, import_react31.forwardRef)(
1145
+ var AssistantModalPrimitiveAnchor = (0, import_react32.forwardRef)(
1096
1146
  ({
1097
1147
  __scopeAssistantModal,
1098
1148
  ...rest
@@ -1157,26 +1207,6 @@ __export(message_exports, {
1157
1207
  // src/primitives/message/MessageRoot.tsx
1158
1208
  var import_react_primitive3 = require("@radix-ui/react-primitive");
1159
1209
  var import_react33 = require("react");
1160
-
1161
- // src/utils/hooks/useManagedRef.ts
1162
- var import_react32 = require("react");
1163
- var useManagedRef = (callback) => {
1164
- const cleanupRef = (0, import_react32.useRef)();
1165
- const ref = (0, import_react32.useCallback)(
1166
- (el) => {
1167
- if (cleanupRef.current) {
1168
- cleanupRef.current();
1169
- }
1170
- if (el) {
1171
- cleanupRef.current = callback(el);
1172
- }
1173
- },
1174
- [callback]
1175
- );
1176
- return ref;
1177
- };
1178
-
1179
- // src/primitives/message/MessageRoot.tsx
1180
1210
  var import_react_compose_refs = require("@radix-ui/react-compose-refs");
1181
1211
  var import_jsx_runtime12 = require("react/jsx-runtime");
1182
1212
  var useIsHoveringRef = () => {
@@ -1224,7 +1254,7 @@ var import_react38 = require("react");
1224
1254
 
1225
1255
  // src/context/providers/ContentPartProvider.tsx
1226
1256
  var import_react34 = require("react");
1227
- var import_zustand9 = require("zustand");
1257
+ var import_zustand10 = require("zustand");
1228
1258
  var import_jsx_runtime13 = require("react/jsx-runtime");
1229
1259
  var DONE_STATUS = { type: "done" };
1230
1260
  var syncContentPart = ({ message }, useContentPart, partIndex) => {
@@ -1244,7 +1274,7 @@ var syncContentPart = ({ message }, useContentPart, partIndex) => {
1244
1274
  var useContentPartContext2 = (partIndex) => {
1245
1275
  const { useMessage } = useMessageContext();
1246
1276
  const [context] = (0, import_react34.useState)(() => {
1247
- const useContentPart = (0, import_zustand9.create)(
1277
+ const useContentPart = (0, import_zustand10.create)(
1248
1278
  () => ({})
1249
1279
  );
1250
1280
  syncContentPart(useMessage.getState(), useContentPart, partIndex);
@@ -1802,14 +1832,14 @@ var import_react49 = require("react");
1802
1832
 
1803
1833
  // src/context/providers/MessageProvider.tsx
1804
1834
  var import_react48 = require("react");
1805
- var import_zustand12 = require("zustand");
1835
+ var import_zustand13 = require("zustand");
1806
1836
 
1807
1837
  // src/context/stores/EditComposer.ts
1808
- var import_zustand10 = require("zustand");
1838
+ var import_zustand11 = require("zustand");
1809
1839
  var makeEditComposerStore = ({
1810
1840
  onEdit,
1811
1841
  onSend
1812
- }) => (0, import_zustand10.create)()((set, get, store) => ({
1842
+ }) => (0, import_zustand11.create)()((set, get, store) => ({
1813
1843
  ...makeBaseComposer(set, get, store),
1814
1844
  canCancel: false,
1815
1845
  isEditing: false,
@@ -1828,8 +1858,8 @@ var makeEditComposerStore = ({
1828
1858
  }));
1829
1859
 
1830
1860
  // src/context/stores/MessageUtils.ts
1831
- var import_zustand11 = require("zustand");
1832
- var makeMessageUtilsStore = () => (0, import_zustand11.create)((set) => ({
1861
+ var import_zustand12 = require("zustand");
1862
+ var makeMessageUtilsStore = () => (0, import_zustand12.create)((set) => ({
1833
1863
  isCopied: false,
1834
1864
  setIsCopied: (value) => {
1835
1865
  set({ isCopied: value });
@@ -1864,7 +1894,7 @@ var syncMessage = (messages, getBranches, useMessage, messageIndex) => {
1864
1894
  var useMessageContext2 = (messageIndex) => {
1865
1895
  const { useThreadMessages, useThreadActions } = useThreadContext();
1866
1896
  const [context] = (0, import_react48.useState)(() => {
1867
- const useMessage = (0, import_zustand12.create)(() => ({}));
1897
+ const useMessage = (0, import_zustand13.create)(() => ({}));
1868
1898
  const useMessageUtils = makeMessageUtilsStore();
1869
1899
  const useEditComposer = makeEditComposerStore({
1870
1900
  onEdit: () => {
@@ -2288,86 +2318,332 @@ var TooltipIconButton = (0, import_react52.forwardRef)(({ children, tooltip, sid
2288
2318
  });
2289
2319
  TooltipIconButton.displayName = "TooltipIconButton";
2290
2320
 
2291
- // src/runtimes/edge/useEdgeRuntime.ts
2292
- var import_react53 = require("react");
2293
-
2294
- // src/runtimes/edge/converters/toCoreMessages.ts
2295
- var toCoreMessages = (message) => {
2296
- return message.map((message2) => {
2297
- return {
2298
- role: message2.role,
2299
- content: message2.content.map((part) => {
2300
- if (part.type === "ui") throw new Error("UI parts are not supported");
2301
- if (part.type === "tool-call") {
2302
- const { argsText, ...rest } = part;
2303
- return rest;
2304
- }
2305
- return part;
2306
- })
2307
- };
2308
- });
2309
- };
2310
-
2311
- // src/runtimes/edge/converters/toLanguageModelTools.ts
2312
- var import_zod2 = require("zod");
2313
- var import_zod_to_json_schema = __toESM(require("zod-to-json-schema"));
2314
- var toLanguageModelTools = (tools) => {
2315
- if (!tools) return [];
2316
- return Object.entries(tools).map(([name, tool]) => ({
2317
- type: "function",
2318
- name,
2319
- ...tool.description ? { description: tool.description } : void 0,
2320
- parameters: tool.parameters instanceof import_zod2.z.ZodType ? (0, import_zod_to_json_schema.default)(tool.parameters) : tool.parameters
2321
- }));
2322
- };
2323
-
2324
- // src/runtimes/edge/streams/assistantDecoderStream.ts
2325
- function assistantDecoderStream() {
2326
- const toolCallNames = /* @__PURE__ */ new Map();
2327
- let currentToolCall;
2328
- return new TransformStream({
2329
- transform(chunk, controller) {
2330
- const [code, value] = parseStreamPart(chunk);
2331
- if (currentToolCall && code !== "2" /* ToolCallArgsTextDelta */ && code !== "E" /* Error */) {
2332
- controller.enqueue({
2333
- type: "tool-call",
2334
- toolCallType: "function",
2335
- toolCallId: currentToolCall.id,
2336
- toolName: currentToolCall.name,
2337
- args: currentToolCall.argsText
2321
+ // src/runtimes/edge/converters/toLanguageModelMessages.ts
2322
+ var assistantMessageSplitter = () => {
2323
+ const stash = [];
2324
+ let assistantMessage = {
2325
+ role: "assistant",
2326
+ content: []
2327
+ };
2328
+ let toolMessage = {
2329
+ role: "tool",
2330
+ content: []
2331
+ };
2332
+ return {
2333
+ addTextContentPart: (part) => {
2334
+ if (toolMessage.content.length > 0) {
2335
+ stash.push(assistantMessage);
2336
+ stash.push(toolMessage);
2337
+ assistantMessage = {
2338
+ role: "assistant",
2339
+ content: []
2340
+ };
2341
+ toolMessage = {
2342
+ role: "tool",
2343
+ content: []
2344
+ };
2345
+ }
2346
+ assistantMessage.content.push(part);
2347
+ },
2348
+ addToolCallPart: (part) => {
2349
+ assistantMessage.content.push({
2350
+ type: "tool-call",
2351
+ toolCallId: part.toolCallId,
2352
+ toolName: part.toolName,
2353
+ args: part.args
2354
+ });
2355
+ if (part.result) {
2356
+ toolMessage.content.push({
2357
+ type: "tool-result",
2358
+ toolCallId: part.toolCallId,
2359
+ toolName: part.toolName,
2360
+ result: part.result
2361
+ // isError
2338
2362
  });
2339
- currentToolCall = void 0;
2340
2363
  }
2341
- switch (code) {
2342
- case "0" /* TextDelta */: {
2343
- controller.enqueue({
2344
- type: "text-delta",
2345
- textDelta: value
2346
- });
2347
- break;
2348
- }
2349
- case "1" /* ToolCallBegin */: {
2350
- const { id, name } = value;
2351
- toolCallNames.set(id, name);
2352
- currentToolCall = { id, name, argsText: "" };
2353
- break;
2354
- }
2355
- case "2" /* ToolCallArgsTextDelta */: {
2356
- const delta = value;
2357
- currentToolCall.argsText += delta;
2358
- controller.enqueue({
2359
- type: "tool-call-delta",
2360
- toolCallType: "function",
2361
- toolCallId: currentToolCall.id,
2362
- toolName: currentToolCall.name,
2363
- argsTextDelta: delta
2364
- });
2365
- break;
2366
- }
2367
- case "3" /* ToolCallResult */: {
2368
- controller.enqueue({
2369
- type: "tool-result",
2370
- toolCallType: "function",
2364
+ },
2365
+ getMessages: () => {
2366
+ if (toolMessage.content.length > 0) {
2367
+ return [...stash, assistantMessage, toolMessage];
2368
+ }
2369
+ return [...stash, assistantMessage];
2370
+ }
2371
+ };
2372
+ };
2373
+ function toLanguageModelMessages(message) {
2374
+ return message.flatMap((message2) => {
2375
+ const role = message2.role;
2376
+ switch (role) {
2377
+ case "system": {
2378
+ return [{ role: "system", content: message2.content[0].text }];
2379
+ }
2380
+ case "user": {
2381
+ const msg = {
2382
+ role: "user",
2383
+ content: message2.content.map(
2384
+ (part) => {
2385
+ const type = part.type;
2386
+ switch (type) {
2387
+ case "text": {
2388
+ return part;
2389
+ }
2390
+ case "image": {
2391
+ return {
2392
+ type: "image",
2393
+ image: new URL(part.image)
2394
+ };
2395
+ }
2396
+ default: {
2397
+ const unhandledType = type;
2398
+ throw new Error(
2399
+ `Unspported content part type: ${unhandledType}`
2400
+ );
2401
+ }
2402
+ }
2403
+ }
2404
+ )
2405
+ };
2406
+ return [msg];
2407
+ }
2408
+ case "assistant": {
2409
+ const splitter = assistantMessageSplitter();
2410
+ for (const part of message2.content) {
2411
+ const type = part.type;
2412
+ switch (type) {
2413
+ case "text": {
2414
+ splitter.addTextContentPart(part);
2415
+ break;
2416
+ }
2417
+ case "tool-call": {
2418
+ splitter.addToolCallPart(part);
2419
+ break;
2420
+ }
2421
+ default: {
2422
+ const unhandledType = type;
2423
+ throw new Error(`Unhandled content part type: ${unhandledType}`);
2424
+ }
2425
+ }
2426
+ }
2427
+ return splitter.getMessages();
2428
+ }
2429
+ default: {
2430
+ const unhandledRole = role;
2431
+ throw new Error(`Unknown message role: ${unhandledRole}`);
2432
+ }
2433
+ }
2434
+ });
2435
+ }
2436
+
2437
+ // src/runtimes/edge/converters/fromLanguageModelMessages.ts
2438
+ var fromLanguageModelMessages = (lm, { mergeRoundtrips }) => {
2439
+ const messages = [];
2440
+ for (const lmMessage of lm) {
2441
+ const role = lmMessage.role;
2442
+ switch (role) {
2443
+ case "system": {
2444
+ messages.push({
2445
+ role: "system",
2446
+ content: [
2447
+ {
2448
+ type: "text",
2449
+ text: lmMessage.content
2450
+ }
2451
+ ]
2452
+ });
2453
+ break;
2454
+ }
2455
+ case "user": {
2456
+ messages.push({
2457
+ role: "user",
2458
+ content: lmMessage.content.map((part) => {
2459
+ const type = part.type;
2460
+ switch (type) {
2461
+ case "text": {
2462
+ return {
2463
+ type: "text",
2464
+ text: part.text
2465
+ };
2466
+ }
2467
+ case "image": {
2468
+ if (part.image instanceof URL) {
2469
+ return {
2470
+ type: "image",
2471
+ image: part.image.href
2472
+ };
2473
+ }
2474
+ throw new Error("Only images with URL data are supported");
2475
+ }
2476
+ default: {
2477
+ const unhandledType = type;
2478
+ throw new Error(`Unknown content part type: ${unhandledType}`);
2479
+ }
2480
+ }
2481
+ })
2482
+ });
2483
+ break;
2484
+ }
2485
+ case "assistant": {
2486
+ const newContent = lmMessage.content.map((part) => {
2487
+ if (part.type === "tool-call") {
2488
+ return {
2489
+ type: "tool-call",
2490
+ toolCallId: part.toolCallId,
2491
+ toolName: part.toolName,
2492
+ argsText: JSON.stringify(part.args),
2493
+ args: part.args
2494
+ };
2495
+ }
2496
+ return part;
2497
+ });
2498
+ if (mergeRoundtrips) {
2499
+ const previousMessage = messages[messages.length - 1];
2500
+ if (previousMessage?.role === "assistant") {
2501
+ previousMessage.content.push(...newContent);
2502
+ break;
2503
+ }
2504
+ }
2505
+ messages.push({
2506
+ role: "assistant",
2507
+ content: newContent
2508
+ });
2509
+ break;
2510
+ }
2511
+ case "tool": {
2512
+ const previousMessage = messages[messages.length - 1];
2513
+ if (previousMessage?.role !== "assistant")
2514
+ throw new Error(
2515
+ "A tool message must be preceded by an assistant message."
2516
+ );
2517
+ for (const tool of lmMessage.content) {
2518
+ const toolCall = previousMessage.content.find(
2519
+ (c) => c.type === "tool-call" && c.toolCallId === tool.toolCallId
2520
+ );
2521
+ if (!toolCall)
2522
+ throw new Error("Received tool result for an unknown tool call.");
2523
+ if (toolCall.toolName !== tool.toolName)
2524
+ throw new Error("Tool call name mismatch.");
2525
+ toolCall.result = tool.result;
2526
+ if (tool.isError) {
2527
+ toolCall.isError = true;
2528
+ }
2529
+ }
2530
+ break;
2531
+ }
2532
+ default: {
2533
+ const unhandledRole = role;
2534
+ throw new Error(`Unknown message role: ${unhandledRole}`);
2535
+ }
2536
+ }
2537
+ }
2538
+ return messages;
2539
+ };
2540
+
2541
+ // src/runtimes/edge/converters/fromCoreMessage.ts
2542
+ var fromCoreMessages = (message) => {
2543
+ return message.map((message2) => {
2544
+ return {
2545
+ id: generateId(),
2546
+ createdAt: /* @__PURE__ */ new Date(),
2547
+ ...message2.role === "assistant" ? {
2548
+ status: { type: "done" }
2549
+ } : void 0,
2550
+ ...message2
2551
+ };
2552
+ });
2553
+ };
2554
+
2555
+ // src/runtimes/edge/converters/toCoreMessages.ts
2556
+ var toCoreMessages = (message) => {
2557
+ return message.map((message2) => {
2558
+ return {
2559
+ role: message2.role,
2560
+ content: message2.content.map((part) => {
2561
+ if (part.type === "ui") throw new Error("UI parts are not supported");
2562
+ if (part.type === "tool-call") {
2563
+ const { argsText, ...rest } = part;
2564
+ return rest;
2565
+ }
2566
+ return part;
2567
+ })
2568
+ };
2569
+ });
2570
+ };
2571
+
2572
+ // src/runtimes/edge/converters/fromLanguageModelTools.ts
2573
+ var fromLanguageModelTools = (tools) => {
2574
+ return Object.fromEntries(
2575
+ tools.map((tool) => [
2576
+ tool.name,
2577
+ {
2578
+ description: tool.description,
2579
+ parameters: tool.parameters
2580
+ }
2581
+ ])
2582
+ );
2583
+ };
2584
+
2585
+ // src/runtimes/edge/converters/toLanguageModelTools.ts
2586
+ var import_zod2 = require("zod");
2587
+ var import_zod_to_json_schema = __toESM(require("zod-to-json-schema"));
2588
+ var toLanguageModelTools = (tools) => {
2589
+ return Object.entries(tools).map(([name, tool]) => ({
2590
+ type: "function",
2591
+ name,
2592
+ ...tool.description ? { description: tool.description } : void 0,
2593
+ parameters: tool.parameters instanceof import_zod2.z.ZodType ? (0, import_zod_to_json_schema.default)(tool.parameters) : tool.parameters
2594
+ }));
2595
+ };
2596
+
2597
+ // src/runtimes/edge/useEdgeRuntime.ts
2598
+ var import_react53 = require("react");
2599
+
2600
+ // src/runtimes/edge/streams/assistantDecoderStream.ts
2601
+ function assistantDecoderStream() {
2602
+ const toolCallNames = /* @__PURE__ */ new Map();
2603
+ let currentToolCall;
2604
+ return new TransformStream({
2605
+ transform(chunk, controller) {
2606
+ const [code, value] = parseStreamPart(chunk);
2607
+ if (currentToolCall && code !== "2" /* ToolCallArgsTextDelta */ && code !== "E" /* Error */) {
2608
+ controller.enqueue({
2609
+ type: "tool-call",
2610
+ toolCallType: "function",
2611
+ toolCallId: currentToolCall.id,
2612
+ toolName: currentToolCall.name,
2613
+ args: currentToolCall.argsText
2614
+ });
2615
+ currentToolCall = void 0;
2616
+ }
2617
+ switch (code) {
2618
+ case "0" /* TextDelta */: {
2619
+ controller.enqueue({
2620
+ type: "text-delta",
2621
+ textDelta: value
2622
+ });
2623
+ break;
2624
+ }
2625
+ case "1" /* ToolCallBegin */: {
2626
+ const { id, name } = value;
2627
+ toolCallNames.set(id, name);
2628
+ currentToolCall = { id, name, argsText: "" };
2629
+ break;
2630
+ }
2631
+ case "2" /* ToolCallArgsTextDelta */: {
2632
+ const delta = value;
2633
+ currentToolCall.argsText += delta;
2634
+ controller.enqueue({
2635
+ type: "tool-call-delta",
2636
+ toolCallType: "function",
2637
+ toolCallId: currentToolCall.id,
2638
+ toolName: currentToolCall.name,
2639
+ argsTextDelta: delta
2640
+ });
2641
+ break;
2642
+ }
2643
+ case "3" /* ToolCallResult */: {
2644
+ controller.enqueue({
2645
+ type: "tool-result",
2646
+ toolCallType: "function",
2371
2647
  toolCallId: value.id,
2372
2648
  toolName: toolCallNames.get(value.id),
2373
2649
  result: value.result
@@ -2997,14 +3273,17 @@ var EdgeChatAdapter = class {
2997
3273
  body: JSON.stringify({
2998
3274
  system: config.system,
2999
3275
  messages: toCoreMessages(messages),
3000
- tools: toLanguageModelTools(
3001
- config.tools
3002
- ),
3276
+ tools: config.tools ? toLanguageModelTools(config.tools) : [],
3003
3277
  ...config.callSettings,
3004
3278
  ...config.config
3005
3279
  }),
3006
3280
  signal: abortSignal
3007
3281
  });
3282
+ if (result.status !== 200) {
3283
+ throw new Error(
3284
+ `Edge runtime returned status ${result.status}: ${await result.text()}`
3285
+ );
3286
+ }
3008
3287
  const stream = result.body.pipeThrough(new TextDecoderStream()).pipeThrough(chunkByLineStream()).pipeThrough(assistantDecoderStream()).pipeThrough(toolResultStream(config.tools)).pipeThrough(runResultStream(initialContent));
3009
3288
  let message;
3010
3289
  let update;
@@ -3057,240 +3336,6 @@ var useEdgeRuntime = ({
3057
3336
  return useLocalRuntime(adapter, { initialMessages });
3058
3337
  };
3059
3338
 
3060
- // src/runtimes/edge/converters/toLanguageModelMessages.ts
3061
- var assistantMessageSplitter = () => {
3062
- const stash = [];
3063
- let assistantMessage = {
3064
- role: "assistant",
3065
- content: []
3066
- };
3067
- let toolMessage = {
3068
- role: "tool",
3069
- content: []
3070
- };
3071
- return {
3072
- addTextContentPart: (part) => {
3073
- if (toolMessage.content.length > 0) {
3074
- stash.push(assistantMessage);
3075
- stash.push(toolMessage);
3076
- assistantMessage = {
3077
- role: "assistant",
3078
- content: []
3079
- };
3080
- toolMessage = {
3081
- role: "tool",
3082
- content: []
3083
- };
3084
- }
3085
- assistantMessage.content.push(part);
3086
- },
3087
- addToolCallPart: (part) => {
3088
- assistantMessage.content.push({
3089
- type: "tool-call",
3090
- toolCallId: part.toolCallId,
3091
- toolName: part.toolName,
3092
- args: part.args
3093
- });
3094
- if (part.result) {
3095
- toolMessage.content.push({
3096
- type: "tool-result",
3097
- toolCallId: part.toolCallId,
3098
- toolName: part.toolName,
3099
- result: part.result
3100
- // isError
3101
- });
3102
- }
3103
- },
3104
- getMessages: () => {
3105
- if (toolMessage.content.length > 0) {
3106
- return [...stash, assistantMessage, toolMessage];
3107
- }
3108
- return [...stash, assistantMessage];
3109
- }
3110
- };
3111
- };
3112
- function toLanguageModelMessages(message) {
3113
- return message.flatMap((message2) => {
3114
- const role = message2.role;
3115
- switch (role) {
3116
- case "system": {
3117
- return [{ role: "system", content: message2.content[0].text }];
3118
- }
3119
- case "user": {
3120
- const msg = {
3121
- role: "user",
3122
- content: message2.content.map(
3123
- (part) => {
3124
- const type = part.type;
3125
- switch (type) {
3126
- case "text": {
3127
- return part;
3128
- }
3129
- case "image": {
3130
- return {
3131
- type: "image",
3132
- image: new URL(part.image)
3133
- };
3134
- }
3135
- default: {
3136
- const unhandledType = type;
3137
- throw new Error(
3138
- `Unspported content part type: ${unhandledType}`
3139
- );
3140
- }
3141
- }
3142
- }
3143
- )
3144
- };
3145
- return [msg];
3146
- }
3147
- case "assistant": {
3148
- const splitter = assistantMessageSplitter();
3149
- for (const part of message2.content) {
3150
- const type = part.type;
3151
- switch (type) {
3152
- case "text": {
3153
- splitter.addTextContentPart(part);
3154
- break;
3155
- }
3156
- case "tool-call": {
3157
- splitter.addToolCallPart(part);
3158
- break;
3159
- }
3160
- default: {
3161
- const unhandledType = type;
3162
- throw new Error(`Unhandled content part type: ${unhandledType}`);
3163
- }
3164
- }
3165
- }
3166
- return splitter.getMessages();
3167
- }
3168
- default: {
3169
- const unhandledRole = role;
3170
- throw new Error(`Unknown message role: ${unhandledRole}`);
3171
- }
3172
- }
3173
- });
3174
- }
3175
-
3176
- // src/runtimes/edge/converters/fromLanguageModelMessages.ts
3177
- var fromLanguageModelMessages = (lm, mergeRoundtrips) => {
3178
- const messages = [];
3179
- for (const lmMessage of lm) {
3180
- const role = lmMessage.role;
3181
- switch (role) {
3182
- case "system": {
3183
- messages.push({
3184
- role: "system",
3185
- content: [
3186
- {
3187
- type: "text",
3188
- text: lmMessage.content
3189
- }
3190
- ]
3191
- });
3192
- break;
3193
- }
3194
- case "user": {
3195
- messages.push({
3196
- role: "user",
3197
- content: lmMessage.content.map((part) => {
3198
- const type = part.type;
3199
- switch (type) {
3200
- case "text": {
3201
- return {
3202
- type: "text",
3203
- text: part.text
3204
- };
3205
- }
3206
- case "image": {
3207
- if (part.image instanceof URL) {
3208
- return {
3209
- type: "image",
3210
- image: part.image.href
3211
- };
3212
- }
3213
- throw new Error("Only images with URL data are supported");
3214
- }
3215
- default: {
3216
- const unhandledType = type;
3217
- throw new Error(`Unknown content part type: ${unhandledType}`);
3218
- }
3219
- }
3220
- })
3221
- });
3222
- break;
3223
- }
3224
- case "assistant": {
3225
- const newContent = lmMessage.content.map((part) => {
3226
- if (part.type === "tool-call") {
3227
- return {
3228
- type: "tool-call",
3229
- toolCallId: part.toolCallId,
3230
- toolName: part.toolName,
3231
- argsText: JSON.stringify(part.args),
3232
- args: part.args
3233
- };
3234
- }
3235
- return part;
3236
- });
3237
- if (mergeRoundtrips) {
3238
- const previousMessage = messages[messages.length - 1];
3239
- if (previousMessage?.role === "assistant") {
3240
- previousMessage.content.push(...newContent);
3241
- break;
3242
- }
3243
- }
3244
- messages.push({
3245
- role: "assistant",
3246
- content: newContent
3247
- });
3248
- break;
3249
- }
3250
- case "tool": {
3251
- const previousMessage = messages[messages.length - 1];
3252
- if (previousMessage?.role !== "assistant")
3253
- throw new Error(
3254
- "A tool message must be preceded by an assistant message."
3255
- );
3256
- for (const tool of lmMessage.content) {
3257
- const toolCall = previousMessage.content.find(
3258
- (c) => c.type === "tool-call" && c.toolCallId === tool.toolCallId
3259
- );
3260
- if (!toolCall)
3261
- throw new Error("Received tool result for an unknown tool call.");
3262
- if (toolCall.toolName !== tool.toolName)
3263
- throw new Error("Tool call name mismatch.");
3264
- toolCall.result = tool.result;
3265
- if (tool.isError) {
3266
- toolCall.isError = true;
3267
- }
3268
- }
3269
- break;
3270
- }
3271
- default: {
3272
- const unhandledRole = role;
3273
- throw new Error(`Unknown message role: ${unhandledRole}`);
3274
- }
3275
- }
3276
- }
3277
- return messages;
3278
- };
3279
-
3280
- // src/runtimes/edge/converters/fromCoreMessage.ts
3281
- var fromCoreMessages = (message) => {
3282
- return message.map((message2) => {
3283
- return {
3284
- id: generateId(),
3285
- createdAt: /* @__PURE__ */ new Date(),
3286
- ...message2.role === "assistant" ? {
3287
- status: { type: "done" }
3288
- } : void 0,
3289
- ...message2
3290
- };
3291
- });
3292
- };
3293
-
3294
3339
  // src/runtimes/local/LocalRuntime.tsx
3295
3340
  var LocalRuntime = class extends BaseAssistantRuntime {
3296
3341
  _proxyConfigProvider;
@@ -4167,9 +4212,12 @@ var assistant_modal_default = Object.assign(AssistantModal, exports12);
4167
4212
  UserMessage,
4168
4213
  fromCoreMessages,
4169
4214
  fromLanguageModelMessages,
4215
+ fromLanguageModelTools,
4170
4216
  makeAssistantTool,
4171
4217
  makeAssistantToolUI,
4218
+ toCoreMessages,
4172
4219
  toLanguageModelMessages,
4220
+ toLanguageModelTools,
4173
4221
  useActionBarCopy,
4174
4222
  useActionBarEdit,
4175
4223
  useActionBarReload,