@assistant-ui/react 0.2.4 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.js CHANGED
@@ -210,11 +210,14 @@ var makeBaseComposer = (set) => ({
210
210
  });
211
211
 
212
212
  // src/context/stores/Composer.ts
213
- var makeComposerStore = (useThread, useThreadMessages, useThreadActions) => {
213
+ var makeComposerStore = (useThreadMessages, useThreadActions) => {
214
214
  const focusListeners = /* @__PURE__ */ new Set();
215
215
  return (0, import_zustand3.create)()((set, get, store) => {
216
216
  return {
217
217
  ...makeBaseComposer(set, get, store),
218
+ get canCancel() {
219
+ return useThreadActions.getState().capabilities.cancel;
220
+ },
218
221
  isEditing: true,
219
222
  send: () => {
220
223
  const { setValue, value } = get();
@@ -226,10 +229,7 @@ var makeComposerStore = (useThread, useThreadMessages, useThreadActions) => {
226
229
  });
227
230
  },
228
231
  cancel: () => {
229
- const thread = useThread.getState();
230
- if (!thread.isRunning) return false;
231
232
  useThreadActions.getState().cancelRun();
232
- return true;
233
233
  },
234
234
  focus: () => {
235
235
  for (const listener of focusListeners) {
@@ -279,6 +279,9 @@ var import_zustand6 = require("zustand");
279
279
  var makeThreadActionStore = (runtimeRef) => {
280
280
  return (0, import_zustand6.create)(
281
281
  () => Object.freeze({
282
+ get capabilities() {
283
+ return runtimeRef.current.capabilities;
284
+ },
282
285
  getBranches: (messageId) => runtimeRef.current.getBranches(messageId),
283
286
  switchToBranch: (branchId) => runtimeRef.current.switchToBranch(branchId),
284
287
  startRun: (parentId) => runtimeRef.current.startRun(parentId),
@@ -310,11 +313,7 @@ var ThreadProvider = ({
310
313
  const useThreadMessages = makeThreadMessagesStore(runtimeRef);
311
314
  const useThreadActions = makeThreadActionStore(runtimeRef);
312
315
  const useViewport = makeThreadViewportStore();
313
- const useComposer = makeComposerStore(
314
- useThread,
315
- useThreadMessages,
316
- useThreadActions
317
- );
316
+ const useComposer = makeComposerStore(useThreadMessages, useThreadActions);
318
317
  return {
319
318
  useThread,
320
319
  useThreadMessages,
@@ -584,7 +583,7 @@ var useActionBarCopy = ({
584
583
  const hasCopyableContent = useCombinedStore(
585
584
  [useMessage, useEditComposer],
586
585
  (m, c) => {
587
- return !c.isEditing && m.message.content.some((c2) => c2.type === "text");
586
+ return !c.isEditing && m.message.content.some((c2) => c2.type === "text" && c2.text.length > 0);
588
587
  }
589
588
  );
590
589
  const callback = (0, import_react16.useCallback)(() => {
@@ -687,7 +686,7 @@ var useBranchPickerPrevious = () => {
687
686
  var import_react21 = require("react");
688
687
  var useComposerCancel = () => {
689
688
  const { useComposer } = useComposerContext();
690
- const disabled = useComposer((c) => !c.isEditing);
689
+ const disabled = useComposer((c) => !c.canCancel);
691
690
  const callback = (0, import_react21.useCallback)(() => {
692
691
  const { cancel } = useComposer.getState();
693
692
  cancel();
@@ -757,7 +756,7 @@ var useContentPartText = () => {
757
756
  throw new Error(
758
757
  "ContentPartText can only be used inside text content parts."
759
758
  );
760
- return c.part.text;
759
+ return c;
761
760
  });
762
761
  return text;
763
762
  };
@@ -819,15 +818,18 @@ var useThreadSuggestion = ({
819
818
  autoSend
820
819
  }) => {
821
820
  const { useThread, useComposer } = useThreadContext();
821
+ const append = useAppendMessage();
822
822
  const disabled = useThread((t) => t.isRunning);
823
823
  const callback = (0, import_react24.useCallback)(() => {
824
824
  const thread = useThread.getState();
825
825
  const composer = useComposer.getState();
826
- composer.setValue(prompt);
827
826
  if (autoSend && !thread.isRunning) {
828
- composer.send();
827
+ append(prompt);
828
+ composer.setValue("");
829
+ } else {
830
+ composer.setValue(prompt);
829
831
  }
830
- }, [useThread, useComposer, prompt, autoSend]);
832
+ }, [useThread, useComposer, autoSend, append, prompt]);
831
833
  if (disabled) return null;
832
834
  return callback;
833
835
  };
@@ -1107,28 +1109,57 @@ __export(message_exports, {
1107
1109
  });
1108
1110
 
1109
1111
  // src/primitives/message/MessageRoot.tsx
1110
- var import_primitive4 = require("@radix-ui/primitive");
1111
1112
  var import_react_primitive3 = require("@radix-ui/react-primitive");
1113
+ var import_react33 = require("react");
1114
+
1115
+ // src/utils/hooks/useManagedRef.ts
1112
1116
  var import_react32 = require("react");
1117
+ var useManagedRef = (callback) => {
1118
+ const cleanupRef = (0, import_react32.useRef)();
1119
+ const ref = (0, import_react32.useCallback)(
1120
+ (el) => {
1121
+ if (cleanupRef.current) {
1122
+ cleanupRef.current();
1123
+ }
1124
+ if (el) {
1125
+ cleanupRef.current = callback(el);
1126
+ }
1127
+ },
1128
+ [callback]
1129
+ );
1130
+ return ref;
1131
+ };
1132
+
1133
+ // src/primitives/message/MessageRoot.tsx
1134
+ var import_react_compose_refs = require("@radix-ui/react-compose-refs");
1113
1135
  var import_jsx_runtime12 = require("react/jsx-runtime");
1114
- var MessagePrimitiveRoot = (0, import_react32.forwardRef)(({ onMouseEnter, onMouseLeave, ...rest }, ref) => {
1136
+ var useIsHoveringRef = () => {
1115
1137
  const { useMessageUtils } = useMessageContext();
1116
- const setIsHovering = useMessageUtils((s) => s.setIsHovering);
1117
- const handleMouseEnter = () => {
1118
- setIsHovering(true);
1119
- };
1120
- const handleMouseLeave = () => {
1121
- setIsHovering(false);
1122
- };
1123
- return /* @__PURE__ */ (0, import_jsx_runtime12.jsx)(
1124
- import_react_primitive3.Primitive.div,
1125
- {
1126
- ...rest,
1127
- ref,
1128
- onMouseEnter: (0, import_primitive4.composeEventHandlers)(onMouseEnter, handleMouseEnter),
1129
- onMouseLeave: (0, import_primitive4.composeEventHandlers)(onMouseLeave, handleMouseLeave)
1130
- }
1138
+ const callbackRef = (0, import_react33.useCallback)(
1139
+ (el) => {
1140
+ const setIsHovering = useMessageUtils.getState().setIsHovering;
1141
+ const handleMouseEnter = () => {
1142
+ setIsHovering(true);
1143
+ };
1144
+ const handleMouseLeave = () => {
1145
+ setIsHovering(false);
1146
+ };
1147
+ el.addEventListener("mouseenter", handleMouseEnter);
1148
+ el.addEventListener("mouseleave", handleMouseLeave);
1149
+ return () => {
1150
+ el.removeEventListener("mouseenter", handleMouseEnter);
1151
+ el.removeEventListener("mouseleave", handleMouseLeave);
1152
+ setIsHovering(false);
1153
+ };
1154
+ },
1155
+ [useMessageUtils]
1131
1156
  );
1157
+ return useManagedRef(callbackRef);
1158
+ };
1159
+ var MessagePrimitiveRoot = (0, import_react33.forwardRef)(({ onMouseEnter, onMouseLeave, ...rest }, forwardRef15) => {
1160
+ const isHoveringRef = useIsHoveringRef();
1161
+ const ref = (0, import_react_compose_refs.useComposedRefs)(forwardRef15, isHoveringRef);
1162
+ return /* @__PURE__ */ (0, import_jsx_runtime12.jsx)(import_react_primitive3.Primitive.div, { ...rest, ref });
1132
1163
  });
1133
1164
  MessagePrimitiveRoot.displayName = "MessagePrimitive.Root";
1134
1165
 
@@ -1143,10 +1174,10 @@ var MessagePrimitiveIf = ({
1143
1174
  MessagePrimitiveIf.displayName = "MessagePrimitive.If";
1144
1175
 
1145
1176
  // src/primitives/message/MessageContent.tsx
1146
- var import_react36 = require("react");
1177
+ var import_react37 = require("react");
1147
1178
 
1148
1179
  // src/context/providers/ContentPartProvider.tsx
1149
- var import_react33 = require("react");
1180
+ var import_react34 = require("react");
1150
1181
  var import_zustand9 = require("zustand");
1151
1182
  var import_jsx_runtime13 = require("react/jsx-runtime");
1152
1183
  var syncContentPart = ({ message }, useContentPart, partIndex) => {
@@ -1165,14 +1196,14 @@ var syncContentPart = ({ message }, useContentPart, partIndex) => {
1165
1196
  };
1166
1197
  var useContentPartContext2 = (partIndex) => {
1167
1198
  const { useMessage } = useMessageContext();
1168
- const [context] = (0, import_react33.useState)(() => {
1199
+ const [context] = (0, import_react34.useState)(() => {
1169
1200
  const useContentPart = (0, import_zustand9.create)(
1170
1201
  () => ({})
1171
1202
  );
1172
1203
  syncContentPart(useMessage.getState(), useContentPart, partIndex);
1173
1204
  return { useContentPart };
1174
1205
  });
1175
- (0, import_react33.useEffect)(() => {
1206
+ (0, import_react34.useEffect)(() => {
1176
1207
  syncContentPart(useMessage.getState(), context.useContentPart, partIndex);
1177
1208
  return useMessage.subscribe((message) => {
1178
1209
  syncContentPart(message, context.useContentPart, partIndex);
@@ -1188,6 +1219,29 @@ var ContentPartProvider = ({
1188
1219
  return /* @__PURE__ */ (0, import_jsx_runtime13.jsx)(ContentPartContext.Provider, { value: context, children });
1189
1220
  };
1190
1221
 
1222
+ // src/primitives/contentPart/ContentPartText.tsx
1223
+ var import_react_primitive4 = require("@radix-ui/react-primitive");
1224
+ var import_react35 = require("react");
1225
+ var import_jsx_runtime14 = require("react/jsx-runtime");
1226
+ var ContentPartPrimitiveText = (0, import_react35.forwardRef)((props, forwardedRef) => {
1227
+ const {
1228
+ part: { text },
1229
+ status
1230
+ } = useContentPartText();
1231
+ return /* @__PURE__ */ (0, import_jsx_runtime14.jsx)(import_react_primitive4.Primitive.span, { "data-status": status, ...props, ref: forwardedRef, children: text });
1232
+ });
1233
+ ContentPartPrimitiveText.displayName = "ContentPartPrimitive.Text";
1234
+
1235
+ // src/primitives/contentPart/ContentPartImage.tsx
1236
+ var import_react_primitive5 = require("@radix-ui/react-primitive");
1237
+ var import_react36 = require("react");
1238
+ var import_jsx_runtime15 = require("react/jsx-runtime");
1239
+ var ContentPartPrimitiveImage = (0, import_react36.forwardRef)((props, forwardedRef) => {
1240
+ const image = useContentPartImage();
1241
+ return /* @__PURE__ */ (0, import_jsx_runtime15.jsx)(import_react_primitive5.Primitive.img, { src: image, ...props, ref: forwardedRef });
1242
+ });
1243
+ ContentPartPrimitiveImage.displayName = "ContentPartPrimitive.Image";
1244
+
1191
1245
  // src/primitives/contentPart/ContentPartDisplay.tsx
1192
1246
  var ContentPartPrimitiveDisplay = () => {
1193
1247
  const display = useContentPartDisplay();
@@ -1195,61 +1249,31 @@ var ContentPartPrimitiveDisplay = () => {
1195
1249
  };
1196
1250
  ContentPartPrimitiveDisplay.displayName = "ContentPartPrimitive.Display";
1197
1251
 
1198
- // src/utils/OutPortal.tsx
1199
- var import_react34 = require("react");
1200
- var import_jsx_runtime14 = require("react/jsx-runtime");
1201
- var OutPortal = ({ node }) => {
1202
- const parentRef = (0, import_react34.useRef)(null);
1203
- (0, import_react34.useLayoutEffect)(() => {
1204
- const parent = parentRef.current;
1205
- if (!parent || !node) return;
1206
- parent.appendChild(node);
1207
- return () => {
1208
- parent.removeChild(node);
1209
- };
1210
- }, [node]);
1211
- if (!node) return null;
1212
- return /* @__PURE__ */ (0, import_jsx_runtime14.jsx)("span", { ref: parentRef });
1213
- };
1214
-
1215
- // src/primitives/contentPart/ContentPartInProgressIndicator.tsx
1216
- var import_jsx_runtime15 = require("react/jsx-runtime");
1217
- var ContentPartPrimitiveInProgressIndicator = () => {
1218
- const { useMessageUtils } = useMessageContext();
1252
+ // src/primitives/contentPart/ContentPartInProgress.tsx
1253
+ var ContentPartPrimitiveInProgress = ({
1254
+ children
1255
+ }) => {
1219
1256
  const { useContentPart } = useContentPartContext();
1220
- const indicator = useCombinedStore(
1221
- [useMessageUtils, useContentPart],
1222
- (m, c) => c.status === "in_progress" ? m.inProgressIndicator : null
1223
- );
1224
- return /* @__PURE__ */ (0, import_jsx_runtime15.jsx)(OutPortal, { node: indicator });
1257
+ const isInProgress = useContentPart((c) => c.status === "in_progress");
1258
+ return isInProgress ? children : null;
1225
1259
  };
1226
- ContentPartPrimitiveInProgressIndicator.displayName = "ContentPartPrimitive.InProgressIndicator";
1227
-
1228
- // src/primitives/contentPart/ContentPartText.tsx
1229
- var import_react_primitive4 = require("@radix-ui/react-primitive");
1230
- var import_react35 = require("react");
1231
- var import_jsx_runtime16 = require("react/jsx-runtime");
1232
- var ContentPartPrimitiveText = (0, import_react35.forwardRef)((props, forwardedRef) => {
1233
- const text = useContentPartText();
1234
- return /* @__PURE__ */ (0, import_jsx_runtime16.jsx)(import_react_primitive4.Primitive.span, { ...props, ref: forwardedRef, children: text });
1235
- });
1236
- ContentPartPrimitiveText.displayName = "ContentPartPrimitive.Text";
1260
+ ContentPartPrimitiveInProgress.displayName = "ContentPartPrimitive.InProgress";
1237
1261
 
1238
1262
  // src/primitives/message/MessageContent.tsx
1239
- var import_jsx_runtime17 = require("react/jsx-runtime");
1263
+ var import_jsx_runtime16 = require("react/jsx-runtime");
1240
1264
  var defaultComponents = {
1241
- Text: () => /* @__PURE__ */ (0, import_jsx_runtime17.jsxs)("p", { style: { whiteSpace: "pre-line" }, children: [
1242
- /* @__PURE__ */ (0, import_jsx_runtime17.jsx)(ContentPartPrimitiveText, {}),
1243
- /* @__PURE__ */ (0, import_jsx_runtime17.jsx)(ContentPartPrimitiveInProgressIndicator, {})
1265
+ Text: () => /* @__PURE__ */ (0, import_jsx_runtime16.jsxs)("p", { style: { whiteSpace: "pre-line" }, children: [
1266
+ /* @__PURE__ */ (0, import_jsx_runtime16.jsx)(ContentPartPrimitiveText, {}),
1267
+ /* @__PURE__ */ (0, import_jsx_runtime16.jsx)(ContentPartPrimitiveInProgress, { children: /* @__PURE__ */ (0, import_jsx_runtime16.jsx)("span", { style: { fontFamily: "revert" }, children: " \u25CF" }) })
1244
1268
  ] }),
1245
- Image: () => null,
1246
- UI: () => /* @__PURE__ */ (0, import_jsx_runtime17.jsx)(ContentPartPrimitiveDisplay, {}),
1269
+ Image: () => /* @__PURE__ */ (0, import_jsx_runtime16.jsx)(ContentPartPrimitiveImage, {}),
1270
+ UI: () => /* @__PURE__ */ (0, import_jsx_runtime16.jsx)(ContentPartPrimitiveDisplay, {}),
1247
1271
  tools: {
1248
1272
  Fallback: (props) => {
1249
1273
  const { useToolUIs } = useAssistantContext();
1250
1274
  const Render = useToolUIs((s) => s.getToolUI(props.part.toolName));
1251
1275
  if (!Render) return null;
1252
- return /* @__PURE__ */ (0, import_jsx_runtime17.jsx)(Render, { ...props });
1276
+ return /* @__PURE__ */ (0, import_jsx_runtime16.jsx)(Render, { ...props });
1253
1277
  }
1254
1278
  }
1255
1279
  };
@@ -1268,15 +1292,15 @@ var MessageContentPartComponent = ({
1268
1292
  const type = part.type;
1269
1293
  switch (type) {
1270
1294
  case "text":
1271
- return /* @__PURE__ */ (0, import_jsx_runtime17.jsx)(Text, { part, status });
1295
+ return /* @__PURE__ */ (0, import_jsx_runtime16.jsx)(Text, { part, status });
1272
1296
  case "image":
1273
- return /* @__PURE__ */ (0, import_jsx_runtime17.jsx)(Image, { part, status });
1297
+ return /* @__PURE__ */ (0, import_jsx_runtime16.jsx)(Image, { part, status });
1274
1298
  case "ui":
1275
- return /* @__PURE__ */ (0, import_jsx_runtime17.jsx)(UI, { part, status });
1299
+ return /* @__PURE__ */ (0, import_jsx_runtime16.jsx)(UI, { part, status });
1276
1300
  case "tool-call": {
1277
1301
  const Tool = by_name[part.toolName] || Fallback;
1278
1302
  const addResult = (result) => addToolResult(part.toolCallId, result);
1279
- return /* @__PURE__ */ (0, import_jsx_runtime17.jsx)(Tool, { part, status, addResult });
1303
+ return /* @__PURE__ */ (0, import_jsx_runtime16.jsx)(Tool, { part, status, addResult });
1280
1304
  }
1281
1305
  default:
1282
1306
  throw new Error(`Unknown content part type: ${type}`);
@@ -1286,9 +1310,9 @@ var MessageContentPartImpl = ({
1286
1310
  partIndex,
1287
1311
  components
1288
1312
  }) => {
1289
- return /* @__PURE__ */ (0, import_jsx_runtime17.jsx)(ContentPartProvider, { partIndex, children: /* @__PURE__ */ (0, import_jsx_runtime17.jsx)(MessageContentPartComponent, { components }) });
1313
+ return /* @__PURE__ */ (0, import_jsx_runtime16.jsx)(ContentPartProvider, { partIndex, children: /* @__PURE__ */ (0, import_jsx_runtime16.jsx)(MessageContentPartComponent, { components }) });
1290
1314
  };
1291
- var MessageContentPart = (0, import_react36.memo)(
1315
+ var MessageContentPart = (0, import_react37.memo)(
1292
1316
  MessageContentPartImpl,
1293
1317
  (prev, next) => prev.partIndex === next.partIndex && prev.components?.Text === next.components?.Text && prev.components?.Image === next.components?.Image && prev.components?.UI === next.components?.UI && prev.components?.tools === next.components?.tools
1294
1318
  );
@@ -1299,7 +1323,7 @@ var MessagePrimitiveContent = ({
1299
1323
  const contentLength = useMessage((s) => s.message.content.length);
1300
1324
  return new Array(contentLength).fill(null).map((_, idx) => {
1301
1325
  const partIndex = idx;
1302
- return /* @__PURE__ */ (0, import_jsx_runtime17.jsx)(
1326
+ return /* @__PURE__ */ (0, import_jsx_runtime16.jsx)(
1303
1327
  MessageContentPart,
1304
1328
  {
1305
1329
  partIndex,
@@ -1312,21 +1336,15 @@ var MessagePrimitiveContent = ({
1312
1336
  MessagePrimitiveContent.displayName = "MessagePrimitive.Content";
1313
1337
 
1314
1338
  // src/primitives/message/MessageInProgress.tsx
1315
- var import_react_dom = require("react-dom");
1316
- var import_react_primitive5 = require("@radix-ui/react-primitive");
1317
- var import_react37 = require("react");
1318
- var import_jsx_runtime18 = require("react/jsx-runtime");
1319
- var MessagePrimitiveInProgress = (0, import_react37.forwardRef)((props, ref) => {
1320
- const { useMessageUtils } = useMessageContext();
1321
- const portalNode = useMessageUtils((s) => s.inProgressIndicator);
1322
- return (0, import_react_dom.createPortal)(/* @__PURE__ */ (0, import_jsx_runtime18.jsx)(import_react_primitive5.Primitive.span, { ...props, ref }), portalNode);
1323
- });
1339
+ var MessagePrimitiveInProgress = () => {
1340
+ return null;
1341
+ };
1324
1342
  MessagePrimitiveInProgress.displayName = "MessagePrimitive.InProgress";
1325
1343
 
1326
1344
  // src/primitives/branchPicker/BranchPickerRoot.tsx
1327
- var import_jsx_runtime19 = require("react/jsx-runtime");
1345
+ var import_jsx_runtime17 = require("react/jsx-runtime");
1328
1346
  var BranchPickerPrimitiveRoot = (0, import_react38.forwardRef)(({ hideWhenSingleBranch, ...rest }, ref) => {
1329
- return /* @__PURE__ */ (0, import_jsx_runtime19.jsx)(MessagePrimitiveIf, { hasBranches: hideWhenSingleBranch ? true : void 0, children: /* @__PURE__ */ (0, import_jsx_runtime19.jsx)(import_react_primitive6.Primitive.div, { ...rest, ref }) });
1347
+ return /* @__PURE__ */ (0, import_jsx_runtime17.jsx)(MessagePrimitiveIf, { hasBranches: hideWhenSingleBranch ? true : void 0, children: /* @__PURE__ */ (0, import_jsx_runtime17.jsx)(import_react_primitive6.Primitive.div, { ...rest, ref }) });
1330
1348
  });
1331
1349
  BranchPickerPrimitiveRoot.displayName = "BranchPickerPrimitive.Root";
1332
1350
 
@@ -1341,10 +1359,10 @@ __export(composer_exports, {
1341
1359
  });
1342
1360
 
1343
1361
  // src/primitives/composer/ComposerRoot.tsx
1344
- var import_primitive5 = require("@radix-ui/primitive");
1362
+ var import_primitive4 = require("@radix-ui/primitive");
1345
1363
  var import_react_primitive7 = require("@radix-ui/react-primitive");
1346
1364
  var import_react39 = require("react");
1347
- var import_jsx_runtime20 = require("react/jsx-runtime");
1365
+ var import_jsx_runtime18 = require("react/jsx-runtime");
1348
1366
  var ComposerPrimitiveRoot = (0, import_react39.forwardRef)(({ onSubmit, ...rest }, forwardedRef) => {
1349
1367
  const send = useComposerSend();
1350
1368
  const handleSubmit = (e) => {
@@ -1352,25 +1370,25 @@ var ComposerPrimitiveRoot = (0, import_react39.forwardRef)(({ onSubmit, ...rest
1352
1370
  if (!send) return;
1353
1371
  send();
1354
1372
  };
1355
- return /* @__PURE__ */ (0, import_jsx_runtime20.jsx)(
1373
+ return /* @__PURE__ */ (0, import_jsx_runtime18.jsx)(
1356
1374
  import_react_primitive7.Primitive.form,
1357
1375
  {
1358
1376
  ...rest,
1359
1377
  ref: forwardedRef,
1360
- onSubmit: (0, import_primitive5.composeEventHandlers)(onSubmit, handleSubmit)
1378
+ onSubmit: (0, import_primitive4.composeEventHandlers)(onSubmit, handleSubmit)
1361
1379
  }
1362
1380
  );
1363
1381
  });
1364
1382
  ComposerPrimitiveRoot.displayName = "ComposerPrimitive.Root";
1365
1383
 
1366
1384
  // src/primitives/composer/ComposerInput.tsx
1367
- var import_primitive6 = require("@radix-ui/primitive");
1368
- var import_react_compose_refs = require("@radix-ui/react-compose-refs");
1385
+ var import_primitive5 = require("@radix-ui/primitive");
1386
+ var import_react_compose_refs2 = require("@radix-ui/react-compose-refs");
1369
1387
  var import_react_slot = require("@radix-ui/react-slot");
1370
1388
  var import_react40 = require("react");
1371
1389
  var import_react_textarea_autosize = __toESM(require("react-textarea-autosize"));
1372
1390
  var import_react_use_escape_keydown = require("@radix-ui/react-use-escape-keydown");
1373
- var import_jsx_runtime21 = require("react/jsx-runtime");
1391
+ var import_jsx_runtime19 = require("react/jsx-runtime");
1374
1392
  var ComposerPrimitiveInput = (0, import_react40.forwardRef)(
1375
1393
  ({ autoFocus = false, asChild, disabled, onChange, onKeyDown, ...rest }, forwardedRef) => {
1376
1394
  const { useThread } = useThreadContext();
@@ -1381,10 +1399,11 @@ var ComposerPrimitiveInput = (0, import_react40.forwardRef)(
1381
1399
  });
1382
1400
  const Component = asChild ? import_react_slot.Slot : import_react_textarea_autosize.default;
1383
1401
  const textareaRef = (0, import_react40.useRef)(null);
1384
- const ref = (0, import_react_compose_refs.useComposedRefs)(forwardedRef, textareaRef);
1402
+ const ref = (0, import_react_compose_refs2.useComposedRefs)(forwardedRef, textareaRef);
1385
1403
  (0, import_react_use_escape_keydown.useEscapeKeydown)((e) => {
1386
1404
  const composer = useComposer.getState();
1387
- if (composer.cancel()) {
1405
+ if (composer.canCancel) {
1406
+ composer.cancel();
1388
1407
  e.preventDefault();
1389
1408
  }
1390
1409
  });
@@ -1414,19 +1433,20 @@ var ComposerPrimitiveInput = (0, import_react40.forwardRef)(
1414
1433
  focus();
1415
1434
  }
1416
1435
  });
1417
- return /* @__PURE__ */ (0, import_jsx_runtime21.jsx)(
1436
+ return /* @__PURE__ */ (0, import_jsx_runtime19.jsx)(
1418
1437
  Component,
1419
1438
  {
1439
+ name: "input",
1420
1440
  value,
1421
1441
  ...rest,
1422
1442
  ref,
1423
1443
  disabled,
1424
- onChange: (0, import_primitive6.composeEventHandlers)(onChange, (e) => {
1444
+ onChange: (0, import_primitive5.composeEventHandlers)(onChange, (e) => {
1425
1445
  const composerState = useComposer.getState();
1426
1446
  if (!composerState.isEditing) return;
1427
1447
  return composerState.setValue(e.target.value);
1428
1448
  }),
1429
- onKeyDown: (0, import_primitive6.composeEventHandlers)(onKeyDown, handleKeyPress)
1449
+ onKeyDown: (0, import_primitive5.composeEventHandlers)(onKeyDown, handleKeyPress)
1430
1450
  }
1431
1451
  );
1432
1452
  }
@@ -1436,11 +1456,11 @@ ComposerPrimitiveInput.displayName = "ComposerPrimitive.Input";
1436
1456
  // src/primitives/composer/ComposerSend.tsx
1437
1457
  var import_react41 = require("react");
1438
1458
  var import_react_primitive8 = require("@radix-ui/react-primitive");
1439
- var import_jsx_runtime22 = require("react/jsx-runtime");
1459
+ var import_jsx_runtime20 = require("react/jsx-runtime");
1440
1460
  var ComposerPrimitiveSend = (0, import_react41.forwardRef)(({ disabled, ...rest }, ref) => {
1441
1461
  const { useComposer } = useComposerContext();
1442
1462
  const hasValue = useComposer((c) => c.isEditing && c.value.length > 0);
1443
- return /* @__PURE__ */ (0, import_jsx_runtime22.jsx)(
1463
+ return /* @__PURE__ */ (0, import_jsx_runtime20.jsx)(
1444
1464
  import_react_primitive8.Primitive.button,
1445
1465
  {
1446
1466
  type: "submit",
@@ -1473,20 +1493,10 @@ var contentPart_exports = {};
1473
1493
  __export(contentPart_exports, {
1474
1494
  Display: () => ContentPartPrimitiveDisplay,
1475
1495
  Image: () => ContentPartPrimitiveImage,
1476
- InProgressIndicator: () => ContentPartPrimitiveInProgressIndicator,
1496
+ InProgress: () => ContentPartPrimitiveInProgress,
1477
1497
  Text: () => ContentPartPrimitiveText
1478
1498
  });
1479
1499
 
1480
- // src/primitives/contentPart/ContentPartImage.tsx
1481
- var import_react_primitive9 = require("@radix-ui/react-primitive");
1482
- var import_react42 = require("react");
1483
- var import_jsx_runtime23 = require("react/jsx-runtime");
1484
- var ContentPartPrimitiveImage = (0, import_react42.forwardRef)((props, forwardedRef) => {
1485
- const image = useContentPartImage();
1486
- return /* @__PURE__ */ (0, import_jsx_runtime23.jsx)(import_react_primitive9.Primitive.img, { src: image, ...props, ref: forwardedRef });
1487
- });
1488
- ContentPartPrimitiveImage.displayName = "ContentPartPrimitive.Image";
1489
-
1490
1500
  // src/primitives/thread/index.ts
1491
1501
  var thread_exports = {};
1492
1502
  __export(thread_exports, {
@@ -1500,11 +1510,11 @@ __export(thread_exports, {
1500
1510
  });
1501
1511
 
1502
1512
  // src/primitives/thread/ThreadRoot.tsx
1503
- var import_react_primitive10 = require("@radix-ui/react-primitive");
1504
- var import_react43 = require("react");
1505
- var import_jsx_runtime24 = require("react/jsx-runtime");
1506
- var ThreadPrimitiveRoot = (0, import_react43.forwardRef)((props, ref) => {
1507
- return /* @__PURE__ */ (0, import_jsx_runtime24.jsx)(import_react_primitive10.Primitive.div, { ...props, ref });
1513
+ var import_react_primitive9 = require("@radix-ui/react-primitive");
1514
+ var import_react42 = require("react");
1515
+ var import_jsx_runtime21 = require("react/jsx-runtime");
1516
+ var ThreadPrimitiveRoot = (0, import_react42.forwardRef)((props, ref) => {
1517
+ return /* @__PURE__ */ (0, import_jsx_runtime21.jsx)(import_react_primitive9.Primitive.div, { ...props, ref });
1508
1518
  });
1509
1519
  ThreadPrimitiveRoot.displayName = "ThreadPrimitive.Root";
1510
1520
 
@@ -1528,40 +1538,20 @@ var ThreadPrimitiveIf = ({
1528
1538
  ThreadPrimitiveIf.displayName = "ThreadPrimitive.If";
1529
1539
 
1530
1540
  // src/primitives/thread/ThreadViewport.tsx
1531
- var import_react_compose_refs3 = require("@radix-ui/react-compose-refs");
1532
- var import_react_primitive11 = require("@radix-ui/react-primitive");
1533
- var import_react48 = require("react");
1541
+ var import_react_compose_refs4 = require("@radix-ui/react-compose-refs");
1542
+ var import_react_primitive10 = require("@radix-ui/react-primitive");
1543
+ var import_react46 = require("react");
1534
1544
 
1535
1545
  // src/primitive-hooks/thread/useThreadViewportAutoScroll.tsx
1536
- var import_react_compose_refs2 = require("@radix-ui/react-compose-refs");
1537
- var import_react47 = require("react");
1538
-
1539
- // src/utils/hooks/useOnResizeContent.tsx
1540
- var import_react_use_callback_ref2 = require("@radix-ui/react-use-callback-ref");
1546
+ var import_react_compose_refs3 = require("@radix-ui/react-compose-refs");
1541
1547
  var import_react45 = require("react");
1542
1548
 
1543
- // src/utils/hooks/useManagedRef.ts
1544
- var import_react44 = require("react");
1545
- var useManagedRef = (callback) => {
1546
- const cleanupRef = (0, import_react44.useRef)();
1547
- const ref = (0, import_react44.useCallback)(
1548
- (el) => {
1549
- if (cleanupRef.current) {
1550
- cleanupRef.current();
1551
- }
1552
- if (el) {
1553
- cleanupRef.current = callback(el);
1554
- }
1555
- },
1556
- [callback]
1557
- );
1558
- return ref;
1559
- };
1560
-
1561
1549
  // src/utils/hooks/useOnResizeContent.tsx
1550
+ var import_react_use_callback_ref2 = require("@radix-ui/react-use-callback-ref");
1551
+ var import_react43 = require("react");
1562
1552
  var useOnResizeContent = (callback) => {
1563
1553
  const callbackRef = (0, import_react_use_callback_ref2.useCallbackRef)(callback);
1564
- const refCallback = (0, import_react45.useCallback)(
1554
+ const refCallback = (0, import_react43.useCallback)(
1565
1555
  (el) => {
1566
1556
  const resizeObserver = new ResizeObserver(() => {
1567
1557
  callbackRef();
@@ -1598,11 +1588,11 @@ var useOnResizeContent = (callback) => {
1598
1588
 
1599
1589
  // src/utils/hooks/useOnScrollToBottom.tsx
1600
1590
  var import_react_use_callback_ref3 = require("@radix-ui/react-use-callback-ref");
1601
- var import_react46 = require("react");
1591
+ var import_react44 = require("react");
1602
1592
  var useOnScrollToBottom = (callback) => {
1603
1593
  const callbackRef = (0, import_react_use_callback_ref3.useCallbackRef)(callback);
1604
1594
  const { useViewport } = useThreadContext();
1605
- (0, import_react46.useEffect)(() => {
1595
+ (0, import_react44.useEffect)(() => {
1606
1596
  return useViewport.getState().onScrollToBottom(() => {
1607
1597
  callbackRef();
1608
1598
  });
@@ -1613,11 +1603,11 @@ var useOnScrollToBottom = (callback) => {
1613
1603
  var useThreadViewportAutoScroll = ({
1614
1604
  autoScroll = true
1615
1605
  }) => {
1616
- const divRef = (0, import_react47.useRef)(null);
1606
+ const divRef = (0, import_react45.useRef)(null);
1617
1607
  const { useViewport } = useThreadContext();
1618
- const firstRenderRef = (0, import_react47.useRef)(true);
1619
- const lastScrollTop = (0, import_react47.useRef)(0);
1620
- const isScrollingToBottomRef = (0, import_react47.useRef)(false);
1608
+ const firstRenderRef = (0, import_react45.useRef)(true);
1609
+ const lastScrollTop = (0, import_react45.useRef)(0);
1610
+ const isScrollingToBottomRef = (0, import_react45.useRef)(false);
1621
1611
  const scrollToBottom = () => {
1622
1612
  const div = divRef.current;
1623
1613
  if (!div || !autoScroll) return;
@@ -1655,7 +1645,7 @@ var useThreadViewportAutoScroll = ({
1655
1645
  el.removeEventListener("scroll", handleScroll);
1656
1646
  };
1657
1647
  });
1658
- const autoScrollRef = (0, import_react_compose_refs2.useComposedRefs)(resizeRef, scrollRef, divRef);
1648
+ const autoScrollRef = (0, import_react_compose_refs3.useComposedRefs)(resizeRef, scrollRef, divRef);
1659
1649
  useOnScrollToBottom(() => {
1660
1650
  scrollToBottom();
1661
1651
  });
@@ -1663,21 +1653,21 @@ var useThreadViewportAutoScroll = ({
1663
1653
  };
1664
1654
 
1665
1655
  // src/primitives/thread/ThreadViewport.tsx
1666
- var import_jsx_runtime25 = require("react/jsx-runtime");
1667
- var ThreadPrimitiveViewport = (0, import_react48.forwardRef)(({ autoScroll, onScroll, children, ...rest }, forwardedRef) => {
1656
+ var import_jsx_runtime22 = require("react/jsx-runtime");
1657
+ var ThreadPrimitiveViewport = (0, import_react46.forwardRef)(({ autoScroll, onScroll, children, ...rest }, forwardedRef) => {
1668
1658
  const autoScrollRef = useThreadViewportAutoScroll({
1669
1659
  autoScroll
1670
1660
  });
1671
- const ref = (0, import_react_compose_refs3.useComposedRefs)(forwardedRef, autoScrollRef);
1672
- return /* @__PURE__ */ (0, import_jsx_runtime25.jsx)(import_react_primitive11.Primitive.div, { ...rest, ref, children });
1661
+ const ref = (0, import_react_compose_refs4.useComposedRefs)(forwardedRef, autoScrollRef);
1662
+ return /* @__PURE__ */ (0, import_jsx_runtime22.jsx)(import_react_primitive10.Primitive.div, { ...rest, ref, children });
1673
1663
  });
1674
1664
  ThreadPrimitiveViewport.displayName = "ThreadPrimitive.Viewport";
1675
1665
 
1676
1666
  // src/primitives/thread/ThreadMessages.tsx
1677
- var import_react50 = require("react");
1667
+ var import_react48 = require("react");
1678
1668
 
1679
1669
  // src/context/providers/MessageProvider.tsx
1680
- var import_react49 = require("react");
1670
+ var import_react47 = require("react");
1681
1671
  var import_zustand12 = require("zustand");
1682
1672
 
1683
1673
  // src/context/stores/EditComposer.ts
@@ -1687,27 +1677,25 @@ var makeEditComposerStore = ({
1687
1677
  onSend
1688
1678
  }) => (0, import_zustand10.create)()((set, get, store) => ({
1689
1679
  ...makeBaseComposer(set, get, store),
1680
+ canCancel: false,
1690
1681
  isEditing: false,
1691
1682
  edit: () => {
1692
1683
  const value = onEdit();
1693
- set({ isEditing: true, value });
1684
+ set({ isEditing: true, canCancel: true, value });
1694
1685
  },
1695
1686
  send: () => {
1696
1687
  const value = get().value;
1697
- set({ isEditing: false });
1688
+ set({ isEditing: false, canCancel: false });
1698
1689
  onSend(value);
1699
1690
  },
1700
1691
  cancel: () => {
1701
- if (!get().isEditing) return false;
1702
- set({ isEditing: false });
1703
- return true;
1692
+ set({ isEditing: false, canCancel: false });
1704
1693
  }
1705
1694
  }));
1706
1695
 
1707
1696
  // src/context/stores/MessageUtils.ts
1708
1697
  var import_zustand11 = require("zustand");
1709
1698
  var makeMessageUtilsStore = () => (0, import_zustand11.create)((set) => ({
1710
- inProgressIndicator: document.createElement("span"),
1711
1699
  isCopied: false,
1712
1700
  setIsCopied: (value) => {
1713
1701
  set({ isCopied: value });
@@ -1719,7 +1707,7 @@ var makeMessageUtilsStore = () => (0, import_zustand11.create)((set) => ({
1719
1707
  }));
1720
1708
 
1721
1709
  // src/context/providers/MessageProvider.tsx
1722
- var import_jsx_runtime26 = require("react/jsx-runtime");
1710
+ var import_jsx_runtime23 = require("react/jsx-runtime");
1723
1711
  var getIsLast = (messages, message) => {
1724
1712
  return messages[messages.length - 1]?.id === message.id;
1725
1713
  };
@@ -1741,7 +1729,7 @@ var syncMessage = (messages, getBranches, useMessage, messageIndex) => {
1741
1729
  };
1742
1730
  var useMessageContext2 = (messageIndex) => {
1743
1731
  const { useThreadMessages, useThreadActions } = useThreadContext();
1744
- const [context] = (0, import_react49.useState)(() => {
1732
+ const [context] = (0, import_react47.useState)(() => {
1745
1733
  const useMessage = (0, import_zustand12.create)(() => ({}));
1746
1734
  const useMessageUtils = makeMessageUtilsStore();
1747
1735
  const useEditComposer = makeEditComposerStore({
@@ -1778,7 +1766,7 @@ var useMessageContext2 = (messageIndex) => {
1778
1766
  );
1779
1767
  return { useMessage, useMessageUtils, useEditComposer };
1780
1768
  });
1781
- (0, import_react49.useEffect)(() => {
1769
+ (0, import_react47.useEffect)(() => {
1782
1770
  return useThreadMessages.subscribe((thread) => {
1783
1771
  syncMessage(
1784
1772
  thread,
@@ -1795,11 +1783,11 @@ var MessageProvider = ({
1795
1783
  children
1796
1784
  }) => {
1797
1785
  const context = useMessageContext2(messageIndex);
1798
- return /* @__PURE__ */ (0, import_jsx_runtime26.jsx)(MessageContext.Provider, { value: context, children });
1786
+ return /* @__PURE__ */ (0, import_jsx_runtime23.jsx)(MessageContext.Provider, { value: context, children });
1799
1787
  };
1800
1788
 
1801
1789
  // src/primitives/thread/ThreadMessages.tsx
1802
- var import_jsx_runtime27 = require("react/jsx-runtime");
1790
+ var import_jsx_runtime24 = require("react/jsx-runtime");
1803
1791
  var getComponents = (components) => {
1804
1792
  return {
1805
1793
  EditComposer: components.EditComposer ?? components.UserMessage ?? components.Message,
@@ -1812,19 +1800,19 @@ var ThreadMessageImpl = ({
1812
1800
  components
1813
1801
  }) => {
1814
1802
  const { UserMessage, EditComposer, AssistantMessage } = getComponents(components);
1815
- return /* @__PURE__ */ (0, import_jsx_runtime27.jsxs)(MessageProvider, { messageIndex, children: [
1816
- /* @__PURE__ */ (0, import_jsx_runtime27.jsxs)(MessagePrimitiveIf, { user: true, children: [
1817
- /* @__PURE__ */ (0, import_jsx_runtime27.jsx)(ComposerPrimitiveIf, { editing: false, children: /* @__PURE__ */ (0, import_jsx_runtime27.jsx)(UserMessage, {}) }),
1818
- /* @__PURE__ */ (0, import_jsx_runtime27.jsx)(ComposerPrimitiveIf, { editing: true, children: /* @__PURE__ */ (0, import_jsx_runtime27.jsx)(EditComposer, {}) })
1803
+ return /* @__PURE__ */ (0, import_jsx_runtime24.jsxs)(MessageProvider, { messageIndex, children: [
1804
+ /* @__PURE__ */ (0, import_jsx_runtime24.jsxs)(MessagePrimitiveIf, { user: true, children: [
1805
+ /* @__PURE__ */ (0, import_jsx_runtime24.jsx)(ComposerPrimitiveIf, { editing: false, children: /* @__PURE__ */ (0, import_jsx_runtime24.jsx)(UserMessage, {}) }),
1806
+ /* @__PURE__ */ (0, import_jsx_runtime24.jsx)(ComposerPrimitiveIf, { editing: true, children: /* @__PURE__ */ (0, import_jsx_runtime24.jsx)(EditComposer, {}) })
1819
1807
  ] }),
1820
- /* @__PURE__ */ (0, import_jsx_runtime27.jsx)(MessagePrimitiveIf, { assistant: true, children: /* @__PURE__ */ (0, import_jsx_runtime27.jsx)(AssistantMessage, {}) })
1808
+ /* @__PURE__ */ (0, import_jsx_runtime24.jsx)(MessagePrimitiveIf, { assistant: true, children: /* @__PURE__ */ (0, import_jsx_runtime24.jsx)(AssistantMessage, {}) })
1821
1809
  ] });
1822
1810
  };
1823
- var ThreadMessage = (0, import_react50.memo)(
1811
+ var ThreadMessage = (0, import_react48.memo)(
1824
1812
  ThreadMessageImpl,
1825
- (prev, next) => prev.messageIndex === next.messageIndex && prev.components.UserMessage === next.components.UserMessage && prev.components.EditComposer === next.components.EditComposer && prev.components.AssistantMessage === next.components.AssistantMessage
1813
+ (prev, next) => prev.messageIndex === next.messageIndex && prev.components.Message === next.components.Message && prev.components.UserMessage === next.components.UserMessage && prev.components.EditComposer === next.components.EditComposer && prev.components.AssistantMessage === next.components.AssistantMessage
1826
1814
  );
1827
- var ThreadPrimitiveMessages = ({
1815
+ var ThreadPrimitiveMessagesImpl = ({
1828
1816
  components
1829
1817
  }) => {
1830
1818
  const { useThreadMessages } = useThreadContext();
@@ -1832,7 +1820,7 @@ var ThreadPrimitiveMessages = ({
1832
1820
  if (messagesLength === 0) return null;
1833
1821
  return new Array(messagesLength).fill(null).map((_, idx) => {
1834
1822
  const messageIndex = idx;
1835
- return /* @__PURE__ */ (0, import_jsx_runtime27.jsx)(
1823
+ return /* @__PURE__ */ (0, import_jsx_runtime24.jsx)(
1836
1824
  ThreadMessage,
1837
1825
  {
1838
1826
  messageIndex,
@@ -1842,7 +1830,11 @@ var ThreadPrimitiveMessages = ({
1842
1830
  );
1843
1831
  });
1844
1832
  };
1845
- ThreadPrimitiveMessages.displayName = "ThreadPrimitive.Messages";
1833
+ ThreadPrimitiveMessagesImpl.displayName = "ThreadPrimitive.Messages";
1834
+ var ThreadPrimitiveMessages = (0, import_react48.memo)(
1835
+ ThreadPrimitiveMessagesImpl,
1836
+ (prev, next) => prev.components?.Message === next.components?.Message && prev.components?.UserMessage === next.components?.UserMessage && prev.components?.EditComposer === next.components?.EditComposer && prev.components?.AssistantMessage === next.components?.AssistantMessage
1837
+ );
1846
1838
 
1847
1839
  // src/primitives/thread/ThreadScrollToBottom.tsx
1848
1840
  var ThreadPrimitiveScrollToBottom = createActionButton(
@@ -1857,7 +1849,7 @@ var ThreadPrimitiveSuggestion = createActionButton(
1857
1849
  );
1858
1850
 
1859
1851
  // src/runtime/local/useLocalRuntime.tsx
1860
- var import_react51 = require("react");
1852
+ var import_react49 = require("react");
1861
1853
 
1862
1854
  // src/runtime/utils/idUtils.tsx
1863
1855
  var import_non_secure = require("nanoid/non-secure");
@@ -2026,6 +2018,9 @@ var BaseAssistantRuntime = class {
2026
2018
  this._thread = _thread;
2027
2019
  this._unsubscribe = this._thread.subscribe(this.subscriptionHandler);
2028
2020
  }
2021
+ get capabilities() {
2022
+ return this._thread.capabilities;
2023
+ }
2029
2024
  _unsubscribe;
2030
2025
  get thread() {
2031
2026
  return this._thread;
@@ -2098,6 +2093,12 @@ var LocalRuntime = class extends BaseAssistantRuntime {
2098
2093
  );
2099
2094
  }
2100
2095
  };
2096
+ var CAPABILITIES = Object.freeze({
2097
+ edit: true,
2098
+ reload: true,
2099
+ cancel: true,
2100
+ copy: true
2101
+ });
2101
2102
  var LocalThreadRuntime = class {
2102
2103
  constructor(_configProviders, adapter) {
2103
2104
  this._configProviders = _configProviders;
@@ -2106,6 +2107,7 @@ var LocalThreadRuntime = class {
2106
2107
  _subscriptions = /* @__PURE__ */ new Set();
2107
2108
  abortController = null;
2108
2109
  repository = new MessageRepository();
2110
+ capabilities = CAPABILITIES;
2109
2111
  get messages() {
2110
2112
  return this.repository.getMessages();
2111
2113
  }
@@ -2189,8 +2191,8 @@ var LocalThreadRuntime = class {
2189
2191
 
2190
2192
  // src/runtime/local/useLocalRuntime.tsx
2191
2193
  var useLocalRuntime = (adapter) => {
2192
- const [runtime] = (0, import_react51.useState)(() => new LocalRuntime(adapter));
2193
- (0, import_react51.useInsertionEffect)(() => {
2194
+ const [runtime] = (0, import_react49.useState)(() => new LocalRuntime(adapter));
2195
+ (0, import_react49.useInsertionEffect)(() => {
2194
2196
  runtime.adapter = adapter;
2195
2197
  });
2196
2198
  return runtime;