@assistant-ui/react 0.1.6 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.js CHANGED
@@ -126,7 +126,9 @@ var ThreadContext = (0, import_react5.createContext)(null);
126
126
  var useThreadContext = () => {
127
127
  const context = (0, import_react5.useContext)(ThreadContext);
128
128
  if (!context)
129
- throw new Error("This component must be used within an AssistantRuntimeProvider.");
129
+ throw new Error(
130
+ "This component must be used within an AssistantRuntimeProvider."
131
+ );
130
132
  return context;
131
133
  };
132
134
 
@@ -218,6 +220,7 @@ var ThreadRoot = (0, import_react10.forwardRef)(
218
220
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_primitive.Primitive.div, { ...props, ref });
219
221
  }
220
222
  );
223
+ ThreadRoot.displayName = "ThreadRoot";
221
224
 
222
225
  // src/primitives/thread/ThreadIf.tsx
223
226
  var useThreadIf = (props) => {
@@ -252,8 +255,8 @@ var import_react_use_callback_ref = require("@radix-ui/react-use-callback-ref");
252
255
  var import_react11 = require("react");
253
256
  var useOnResizeContent = (ref, callback) => {
254
257
  const callbackRef = (0, import_react_use_callback_ref.useCallbackRef)(callback);
258
+ const el = ref.current;
255
259
  (0, import_react11.useEffect)(() => {
256
- const el = ref.current;
257
260
  if (!el) return;
258
261
  const resizeObserver = new ResizeObserver(() => {
259
262
  callbackRef();
@@ -282,7 +285,7 @@ var useOnResizeContent = (ref, callback) => {
282
285
  resizeObserver.disconnect();
283
286
  mutationObserver.disconnect();
284
287
  };
285
- }, [ref.current, callbackRef]);
288
+ }, [el, callbackRef]);
286
289
  };
287
290
 
288
291
  // src/utils/hooks/useOnScrollToBottom.tsx
@@ -351,6 +354,7 @@ var ThreadViewport = (0, import_react13.forwardRef)(({ autoScroll = true, onScro
351
354
  }
352
355
  );
353
356
  });
357
+ ThreadViewport.displayName = "ThreadViewport";
354
358
 
355
359
  // src/primitives/thread/ThreadMessages.tsx
356
360
  var import_react16 = require("react");
@@ -588,6 +592,7 @@ var ThreadScrollToBottom = (0, import_react17.forwardRef)(({ onClick, ...rest },
588
592
  }
589
593
  );
590
594
  });
595
+ ThreadScrollToBottom.displayName = "ThreadScrollToBottom";
591
596
 
592
597
  // src/primitives/thread/ThreadSuggestion.tsx
593
598
  var import_primitive3 = require("@radix-ui/primitive");
@@ -615,6 +620,7 @@ var ThreadSuggestion = (0, import_react18.forwardRef)(({ onClick, prompt, method
615
620
  }
616
621
  );
617
622
  });
623
+ ThreadSuggestion.displayName = "ThreadSuggestion";
618
624
 
619
625
  // src/primitives/composer/index.ts
620
626
  var composer_exports = {};
@@ -655,6 +661,7 @@ var ComposerRoot = (0, import_react19.forwardRef)(
655
661
  );
656
662
  }
657
663
  );
664
+ ComposerRoot.displayName = "ComposerRoot";
658
665
 
659
666
  // src/primitives/composer/ComposerInput.tsx
660
667
  var import_primitive5 = require("@radix-ui/primitive");
@@ -724,6 +731,7 @@ var ComposerInput = (0, import_react20.forwardRef)(
724
731
  );
725
732
  }
726
733
  );
734
+ ComposerInput.displayName = "ComposerInput";
727
735
 
728
736
  // src/primitives/composer/ComposerSend.tsx
729
737
  var import_react_primitive6 = require("@radix-ui/react-primitive");
@@ -744,6 +752,7 @@ var ComposerSend = (0, import_react21.forwardRef)(
744
752
  );
745
753
  }
746
754
  );
755
+ ComposerSend.displayName = "ComposerSend";
747
756
 
748
757
  // src/primitives/composer/ComposerCancel.tsx
749
758
  var import_primitive6 = require("@radix-ui/primitive");
@@ -765,6 +774,7 @@ var ComposerCancel = (0, import_react22.forwardRef)(({ onClick, ...rest }, ref)
765
774
  }
766
775
  );
767
776
  });
777
+ ComposerCancel.displayName = "ComposerCancel";
768
778
 
769
779
  // src/primitives/message/index.ts
770
780
  var message_exports = {};
@@ -801,6 +811,7 @@ var MessageRoot = (0, import_react23.forwardRef)(
801
811
  );
802
812
  }
803
813
  );
814
+ MessageRoot.displayName = "MessageRoot";
804
815
 
805
816
  // src/primitives/message/MessageContent.tsx
806
817
  var import_react28 = require("react");
@@ -910,6 +921,7 @@ var ContentPartText = (0, import_react27.forwardRef)((props, forwardedRef) => {
910
921
  });
911
922
  return /* @__PURE__ */ (0, import_jsx_runtime14.jsx)(import_react_primitive9.Primitive.span, { ...props, ref: forwardedRef, children: text });
912
923
  });
924
+ ContentPartText.displayName = "ContentPartText";
913
925
 
914
926
  // src/primitives/message/MessageContent.tsx
915
927
  var import_jsx_runtime15 = require("react/jsx-runtime");
@@ -922,10 +934,8 @@ var defaultComponents = {
922
934
  UI: () => /* @__PURE__ */ (0, import_jsx_runtime15.jsx)(ContentPartDisplay, {}),
923
935
  tools: {
924
936
  Fallback: (props) => {
925
- const { useToolRenderers } = useAssistantContext();
926
- const Render = useToolRenderers(
927
- (s) => s.getToolRenderer(props.part.toolName)
928
- );
937
+ const { useToolUIs } = useAssistantContext();
938
+ const Render = useToolUIs((s) => s.getToolUI(props.part.toolName));
929
939
  if (!Render) return null;
930
940
  return /* @__PURE__ */ (0, import_jsx_runtime15.jsx)(Render, { ...props });
931
941
  }
@@ -939,6 +949,8 @@ var MessageContentPartComponent = ({
939
949
  tools: { by_name = {}, Fallback = defaultComponents.tools.Fallback } = {}
940
950
  } = {}
941
951
  }) => {
952
+ const { useThread } = useThreadContext();
953
+ const addToolResult = useThread((t) => t.addToolResult);
942
954
  const { useContentPart } = useContentPartContext();
943
955
  const { part, status } = useContentPart();
944
956
  const type = part.type;
@@ -951,7 +963,8 @@ var MessageContentPartComponent = ({
951
963
  return /* @__PURE__ */ (0, import_jsx_runtime15.jsx)(UI, { part, status });
952
964
  case "tool-call": {
953
965
  const Tool = by_name[part.toolName] || Fallback;
954
- return /* @__PURE__ */ (0, import_jsx_runtime15.jsx)(Tool, { part, status });
966
+ const addResult = (result) => addToolResult(part.toolCallId, result);
967
+ return /* @__PURE__ */ (0, import_jsx_runtime15.jsx)(Tool, { part, status, addResult });
955
968
  }
956
969
  default:
957
970
  throw new Error(`Unknown content part type: ${type}`);
@@ -994,6 +1007,7 @@ var MessageInProgress = (0, import_react29.forwardRef)((props, ref) => {
994
1007
  }, [useMessage, props, ref]);
995
1008
  return null;
996
1009
  });
1010
+ MessageInProgress.displayName = "MessageInProgress";
997
1011
 
998
1012
  // src/primitives/branchPicker/index.ts
999
1013
  var branchPicker_exports = {};
@@ -1011,21 +1025,21 @@ var import_react_primitive11 = require("@radix-ui/react-primitive");
1011
1025
  var import_react30 = require("react");
1012
1026
  var import_jsx_runtime17 = require("react/jsx-runtime");
1013
1027
  var createActionButton = (useActionButton) => {
1014
- return (0, import_react30.forwardRef)(
1015
- (props, forwardedRef) => {
1016
- const onClick = useActionButton(props);
1017
- return /* @__PURE__ */ (0, import_jsx_runtime17.jsx)(
1018
- import_react_primitive11.Primitive.button,
1019
- {
1020
- type: "button",
1021
- disabled: !onClick,
1022
- ...props,
1023
- ref: forwardedRef,
1024
- onClick: (0, import_primitive8.composeEventHandlers)(props.onClick, onClick ?? void 0)
1025
- }
1026
- );
1027
- }
1028
- );
1028
+ const ActionButton = (0, import_react30.forwardRef)((props, forwardedRef) => {
1029
+ const onClick = useActionButton(props);
1030
+ return /* @__PURE__ */ (0, import_jsx_runtime17.jsx)(
1031
+ import_react_primitive11.Primitive.button,
1032
+ {
1033
+ type: "button",
1034
+ disabled: !onClick,
1035
+ ...props,
1036
+ ref: forwardedRef,
1037
+ onClick: (0, import_primitive8.composeEventHandlers)(props.onClick, onClick ?? void 0)
1038
+ }
1039
+ );
1040
+ });
1041
+ ActionButton.displayName = "ActionButton";
1042
+ return ActionButton;
1029
1043
  };
1030
1044
 
1031
1045
  // src/primitives/branchPicker/BranchPickerNext.tsx
@@ -1057,6 +1071,7 @@ var import_jsx_runtime20 = require("react/jsx-runtime");
1057
1071
  var BranchPickerRoot = (0, import_react31.forwardRef)(({ hideWhenSingleBranch, ...rest }, ref) => {
1058
1072
  return /* @__PURE__ */ (0, import_jsx_runtime20.jsx)(MessageIf, { hasBranches: hideWhenSingleBranch ? true : void 0, children: /* @__PURE__ */ (0, import_jsx_runtime20.jsx)(import_react_primitive12.Primitive.div, { ...rest, ref }) });
1059
1073
  });
1074
+ BranchPickerRoot.displayName = "BranchPickerRoot";
1060
1075
 
1061
1076
  // src/primitives/actionBar/index.ts
1062
1077
  var actionBar_exports = {};
@@ -1096,6 +1111,7 @@ var ActionBarRoot = (0, import_react32.forwardRef)(({ hideWhenRunning, autohide,
1096
1111
  }
1097
1112
  );
1098
1113
  });
1114
+ ActionBarRoot.displayName = "ActionBarRoot";
1099
1115
 
1100
1116
  // src/primitives/actionBar/ActionBarCopy.tsx
1101
1117
  var ActionBarCopy = createActionButton(useCopyMessage);
@@ -1130,6 +1146,7 @@ var ContentPartImage = (0, import_react33.forwardRef)((props, forwardedRef) => {
1130
1146
  });
1131
1147
  return /* @__PURE__ */ (0, import_jsx_runtime22.jsx)(import_react_primitive14.Primitive.img, { src: image, ...props, ref: forwardedRef });
1132
1148
  });
1149
+ ContentPartImage.displayName = "ContentPartImage";
1133
1150
 
1134
1151
  // src/runtime/local/useLocalRuntime.tsx
1135
1152
  var import_react34 = require("react");
@@ -1411,6 +1428,9 @@ var LocalRuntime = class {
1411
1428
  this._configProviders.add(provider);
1412
1429
  return () => this._configProviders.delete(provider);
1413
1430
  }
1431
+ addToolResult() {
1432
+ throw new Error("LocalRuntime does not yet support tool results");
1433
+ }
1414
1434
  };
1415
1435
 
1416
1436
  // src/runtime/local/useLocalRuntime.tsx
@@ -1458,18 +1478,18 @@ var makeAssistantModelConfigStore = () => (0, import_zustand4.create)(() => {
1458
1478
  };
1459
1479
  });
1460
1480
 
1461
- // src/context/stores/AssistantToolRenderers.ts
1481
+ // src/context/stores/AssistantToolUIs.ts
1462
1482
  var import_zustand5 = require("zustand");
1463
- var makeAssistantToolRenderersStore = () => (0, import_zustand5.create)((set) => {
1483
+ var makeAssistantToolUIsStore = () => (0, import_zustand5.create)((set) => {
1464
1484
  const renderers = /* @__PURE__ */ new Map();
1465
1485
  return {
1466
- getToolRenderer: (name) => {
1486
+ getToolUI: (name) => {
1467
1487
  const arr = renderers.get(name);
1468
1488
  const last = arr?.at(-1);
1469
1489
  if (last) return last;
1470
1490
  return null;
1471
1491
  },
1472
- setToolRenderer: (name, render) => {
1492
+ setToolUI: (name, render) => {
1473
1493
  let arr = renderers.get(name);
1474
1494
  if (!arr) {
1475
1495
  arr = [];
@@ -1524,7 +1544,8 @@ var makeThreadStore = (runtimeRef) => {
1524
1544
  switchToBranch: (branchId) => runtimeRef.current.switchToBranch(branchId),
1525
1545
  startRun: (parentId) => runtimeRef.current.startRun(parentId),
1526
1546
  append: (message) => runtimeRef.current.append(message),
1527
- cancelRun: () => runtimeRef.current.cancelRun()
1547
+ cancelRun: () => runtimeRef.current.cancelRun(),
1548
+ addToolResult: (toolCallId, result) => runtimeRef.current.addToolResult(toolCallId, result)
1528
1549
  }));
1529
1550
  const onRuntimeUpdate = () => {
1530
1551
  useThread.setState({
@@ -1601,8 +1622,8 @@ var AssistantProvider = ({ children, runtime }) => {
1601
1622
  });
1602
1623
  const [context] = (0, import_react36.useState)(() => {
1603
1624
  const useModelConfig = makeAssistantModelConfigStore();
1604
- const useToolRenderers = makeAssistantToolRenderersStore();
1605
- return { useModelConfig, useToolRenderers };
1625
+ const useToolUIs = makeAssistantToolUIsStore();
1626
+ return { useModelConfig, useToolUIs };
1606
1627
  });
1607
1628
  const getModelCOnfig = context.useModelConfig((c) => c.getModelConfig);
1608
1629
  (0, import_react36.useEffect)(() => {