@assistant-ui/react 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. package/dist/{ModelConfigTypes-BF5HxVrH.d.mts → Thread-By2aBNAG.d.mts} +14 -2
  2. package/dist/{ModelConfigTypes-BF5HxVrH.d.ts → Thread-By2aBNAG.d.ts} +14 -2
  3. package/dist/{chunk-CY4TTHR7.mjs → chunk-SCWPIDP6.mjs} +8 -1
  4. package/dist/{chunk-CY4TTHR7.mjs.map → chunk-SCWPIDP6.mjs.map} +1 -1
  5. package/dist/experimental.d.mts +3 -38
  6. package/dist/experimental.d.ts +3 -38
  7. package/dist/experimental.js +17 -301
  8. package/dist/experimental.js.map +1 -1
  9. package/dist/experimental.mjs +1 -112
  10. package/dist/experimental.mjs.map +1 -1
  11. package/dist/index.d.mts +73 -5
  12. package/dist/index.d.ts +73 -5
  13. package/dist/index.js +297 -23
  14. package/dist/index.js.map +1 -1
  15. package/dist/index.mjs +315 -13
  16. package/dist/index.mjs.map +1 -1
  17. package/package.json +5 -15
  18. package/dist/AssistantRuntime-BM_jVV3g.d.mts +0 -23
  19. package/dist/AssistantRuntime-CBMSAJqH.d.mts +0 -23
  20. package/dist/AssistantRuntime-C_BvM7ZT.d.ts +0 -23
  21. package/dist/AssistantRuntime-DELpXWfG.d.ts +0 -23
  22. package/dist/ModelConfigTypes-B9UY4zxv.d.mts +0 -55
  23. package/dist/ModelConfigTypes-B9UY4zxv.d.ts +0 -55
  24. package/dist/ModelConfigTypes-Cf3yjaDu.d.mts +0 -55
  25. package/dist/ModelConfigTypes-Cf3yjaDu.d.ts +0 -55
  26. package/dist/chunk-3XZUKECF.mjs +0 -207
  27. package/dist/chunk-3XZUKECF.mjs.map +0 -1
  28. package/dist/chunk-4DQ2CIAD.mjs +0 -69
  29. package/dist/chunk-4DQ2CIAD.mjs.map +0 -1
  30. package/dist/chunk-5YONSDN4.mjs +0 -200
  31. package/dist/chunk-5YONSDN4.mjs.map +0 -1
  32. package/dist/chunk-DKAWDNW5.mjs +0 -22
  33. package/dist/chunk-DKAWDNW5.mjs.map +0 -1
  34. package/dist/chunk-GQKH2ADD.mjs +0 -165
  35. package/dist/chunk-GQKH2ADD.mjs.map +0 -1
  36. package/dist/chunk-J5LGTIGS.mjs +0 -10
  37. package/dist/chunk-J5LGTIGS.mjs.map +0 -1
  38. package/dist/chunk-X4HBDEFP.mjs +0 -30
  39. package/dist/chunk-X4HBDEFP.mjs.map +0 -1
  40. package/dist/internal-dlLjX30u.d.mts +0 -75
  41. package/dist/internal-dlLjX30u.d.ts +0 -75
  42. package/dist/internal.d.mts +0 -25
  43. package/dist/internal.d.ts +0 -25
  44. package/dist/internal.js +0 -225
  45. package/dist/internal.js.map +0 -1
  46. package/dist/internal.mjs +0 -12
  47. package/dist/internal.mjs.map +0 -1
package/dist/index.js CHANGED
@@ -35,12 +35,14 @@ __export(src_exports, {
35
35
  BranchPickerPrimitive: () => branchPicker_exports,
36
36
  ComposerPrimitive: () => composer_exports,
37
37
  ContentPartPrimitive: () => contentPart_exports,
38
+ INTERNAL: () => internal_exports,
38
39
  MessagePrimitive: () => message_exports,
39
40
  ThreadPrimitive: () => thread_exports,
40
41
  useBeginMessageEdit: () => useBeginMessageEdit,
41
42
  useCopyMessage: () => useCopyMessage,
42
43
  useGoToNextBranch: () => useGoToNextBranch,
43
44
  useGoToPreviousBranch: () => useGoToPreviousBranch,
45
+ useLocalRuntime: () => useLocalRuntime,
44
46
  useReloadMessage: () => useReloadMessage
45
47
  });
46
48
  module.exports = __toCommonJS(src_exports);
@@ -1028,20 +1030,8 @@ __export(contentPart_exports, {
1028
1030
  InProgressIndicator: () => ContentPartInProgressIndicator
1029
1031
  });
1030
1032
 
1031
- // src/context/providers/AssistantRuntimeProvider.tsx
1032
- var import_react32 = require("react");
1033
-
1034
- // src/context/providers/AssistantProvider.tsx
1035
- var import_react31 = require("react");
1036
-
1037
- // src/context/AssistantContext.ts
1033
+ // src/runtime/local/useLocalRuntime.tsx
1038
1034
  var import_react29 = require("react");
1039
- var AssistantContext = (0, import_react29.createContext)(
1040
- null
1041
- );
1042
-
1043
- // src/context/stores/AssistantModelConfig.ts
1044
- var import_zustand4 = require("zustand");
1045
1035
 
1046
1036
  // src/utils/ModelConfigTypes.ts
1047
1037
  var mergeModelConfigs = (configs) => {
@@ -1062,6 +1052,281 @@ ${config.system}`;
1062
1052
  }, {});
1063
1053
  };
1064
1054
 
1055
+ // src/runtime/utils/idUtils.tsx
1056
+ var import_non_secure = require("nanoid/non-secure");
1057
+ var generateId = (0, import_non_secure.customAlphabet)(
1058
+ "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
1059
+ 7
1060
+ );
1061
+ var optimisticPrefix = "__optimistic__";
1062
+ var generateOptimisticId = () => `${optimisticPrefix}${generateId()}`;
1063
+
1064
+ // src/runtime/utils/MessageRepository.tsx
1065
+ var findHead = (message) => {
1066
+ if (message.next) return findHead(message.next);
1067
+ return message;
1068
+ };
1069
+ var MessageRepository = class {
1070
+ messages = /* @__PURE__ */ new Map();
1071
+ // message_id -> item
1072
+ head = null;
1073
+ root = {
1074
+ children: []
1075
+ };
1076
+ performOp(newParent, child, operation) {
1077
+ const parentOrRoot = child.prev ?? this.root;
1078
+ const newParentOrRoot = newParent ?? this.root;
1079
+ if (operation === "relink" && parentOrRoot === newParentOrRoot) return;
1080
+ if (operation !== "link") {
1081
+ parentOrRoot.children = parentOrRoot.children.filter(
1082
+ (m) => m !== child.current.id
1083
+ );
1084
+ if (child.prev?.next === child) {
1085
+ const fallbackId = child.prev.children.at(-1);
1086
+ const fallback = fallbackId ? this.messages.get(fallbackId) : null;
1087
+ if (fallback === void 0) {
1088
+ throw new Error(
1089
+ "MessageRepository(performOp/cut): Fallback sibling message not found. This is likely an internal bug in assistant-ui."
1090
+ );
1091
+ }
1092
+ child.prev.next = fallback;
1093
+ }
1094
+ }
1095
+ if (operation !== "cut") {
1096
+ newParentOrRoot.children = [
1097
+ ...newParentOrRoot.children,
1098
+ child.current.id
1099
+ ];
1100
+ if (newParent && (findHead(child) === this.head || newParent.next === null)) {
1101
+ newParent.next = child;
1102
+ }
1103
+ child.prev = newParent;
1104
+ }
1105
+ }
1106
+ getMessages() {
1107
+ const messages = new Array(this.head?.level ?? 0);
1108
+ for (let current = this.head; current; current = current.prev) {
1109
+ messages[current.level] = current.current;
1110
+ }
1111
+ return messages;
1112
+ }
1113
+ addOrUpdateMessage(parentId, message) {
1114
+ const existingItem = this.messages.get(message.id);
1115
+ const prev = parentId ? this.messages.get(parentId) : null;
1116
+ if (prev === void 0)
1117
+ throw new Error(
1118
+ "MessageRepository(addOrUpdateMessage): Parent message not found. This is likely an internal bug in assistant-ui."
1119
+ );
1120
+ if (existingItem) {
1121
+ existingItem.current = message;
1122
+ this.performOp(prev, existingItem, "relink");
1123
+ return;
1124
+ }
1125
+ const newItem = {
1126
+ prev,
1127
+ current: message,
1128
+ next: null,
1129
+ children: [],
1130
+ level: prev ? prev.level + 1 : 0
1131
+ };
1132
+ this.messages.set(message.id, newItem);
1133
+ this.performOp(prev, newItem, "link");
1134
+ if (this.head === prev) {
1135
+ this.head = newItem;
1136
+ }
1137
+ }
1138
+ appendOptimisticMessage(parentId, message) {
1139
+ let optimisticId;
1140
+ do {
1141
+ optimisticId = generateOptimisticId();
1142
+ } while (this.messages.has(optimisticId));
1143
+ this.addOrUpdateMessage(parentId, {
1144
+ ...message,
1145
+ id: optimisticId,
1146
+ createdAt: /* @__PURE__ */ new Date(),
1147
+ ...message.role === "assistant" ? { status: "in_progress" } : void 0
1148
+ });
1149
+ return optimisticId;
1150
+ }
1151
+ deleteMessage(messageId, replacementId) {
1152
+ const message = this.messages.get(messageId);
1153
+ if (!message)
1154
+ throw new Error(
1155
+ "MessageRepository(deleteMessage): Optimistic message not found. This is likely an internal bug in assistant-ui."
1156
+ );
1157
+ const replacement = replacementId === void 0 ? message.prev : replacementId === null ? null : this.messages.get(replacementId);
1158
+ if (replacement === void 0)
1159
+ throw new Error(
1160
+ "MessageRepository(deleteMessage): Replacement not found. This is likely an internal bug in assistant-ui."
1161
+ );
1162
+ for (const child of message.children) {
1163
+ const childMessage = this.messages.get(child);
1164
+ if (!childMessage)
1165
+ throw new Error(
1166
+ "MessageRepository(deleteMessage): Child message not found. This is likely an internal bug in assistant-ui."
1167
+ );
1168
+ this.performOp(replacement, childMessage, "relink");
1169
+ }
1170
+ this.performOp(null, message, "cut");
1171
+ this.messages.delete(messageId);
1172
+ if (this.head === message) {
1173
+ this.head = replacement ? findHead(replacement) : null;
1174
+ }
1175
+ }
1176
+ getBranches(messageId) {
1177
+ const message = this.messages.get(messageId);
1178
+ if (!message)
1179
+ throw new Error(
1180
+ "MessageRepository(getBranches): Message not found. This is likely an internal bug in assistant-ui."
1181
+ );
1182
+ const { children } = message.prev ?? this.root;
1183
+ return children;
1184
+ }
1185
+ switchToBranch(messageId) {
1186
+ const message = this.messages.get(messageId);
1187
+ if (!message)
1188
+ throw new Error(
1189
+ "MessageRepository(switchToBranch): Branch not found. This is likely an internal bug in assistant-ui."
1190
+ );
1191
+ if (message.prev) {
1192
+ message.prev.next = message;
1193
+ }
1194
+ this.head = findHead(message);
1195
+ }
1196
+ resetHead(messageId) {
1197
+ if (messageId === null) {
1198
+ this.head = null;
1199
+ return;
1200
+ }
1201
+ const message = this.messages.get(messageId);
1202
+ if (!message)
1203
+ throw new Error(
1204
+ "MessageRepository(resetHead): Branch not found. This is likely an internal bug in assistant-ui."
1205
+ );
1206
+ this.head = message;
1207
+ for (let current = message; current; current = current.prev) {
1208
+ if (current.prev) {
1209
+ current.prev.next = current;
1210
+ }
1211
+ }
1212
+ }
1213
+ };
1214
+
1215
+ // src/runtime/local/LocalRuntime.tsx
1216
+ var LocalRuntime = class {
1217
+ constructor(adapter) {
1218
+ this.adapter = adapter;
1219
+ }
1220
+ _subscriptions = /* @__PURE__ */ new Set();
1221
+ _configProviders = /* @__PURE__ */ new Set();
1222
+ abortController = null;
1223
+ repository = new MessageRepository();
1224
+ get messages() {
1225
+ return this.repository.getMessages();
1226
+ }
1227
+ get isRunning() {
1228
+ return this.abortController != null;
1229
+ }
1230
+ getBranches(messageId) {
1231
+ return this.repository.getBranches(messageId);
1232
+ }
1233
+ switchToBranch(branchId) {
1234
+ this.repository.switchToBranch(branchId);
1235
+ this.notifySubscribers();
1236
+ }
1237
+ async append(message) {
1238
+ const userMessageId = generateId();
1239
+ const userMessage = {
1240
+ id: userMessageId,
1241
+ role: "user",
1242
+ content: message.content,
1243
+ createdAt: /* @__PURE__ */ new Date()
1244
+ };
1245
+ this.repository.addOrUpdateMessage(message.parentId, userMessage);
1246
+ await this.startRun(userMessageId);
1247
+ }
1248
+ async startRun(parentId) {
1249
+ const id = generateId();
1250
+ this.repository.resetHead(parentId);
1251
+ const messages = this.repository.getMessages();
1252
+ const message = {
1253
+ id,
1254
+ role: "assistant",
1255
+ status: "in_progress",
1256
+ content: [{ type: "text", text: "" }],
1257
+ createdAt: /* @__PURE__ */ new Date()
1258
+ };
1259
+ this.repository.addOrUpdateMessage(parentId, { ...message });
1260
+ this.abortController?.abort();
1261
+ this.abortController = new AbortController();
1262
+ this.notifySubscribers();
1263
+ try {
1264
+ const updateHandler = ({ content }) => {
1265
+ message.content = content;
1266
+ this.repository.addOrUpdateMessage(parentId, { ...message });
1267
+ this.notifySubscribers();
1268
+ };
1269
+ const result = await this.adapter.run({
1270
+ messages,
1271
+ abortSignal: this.abortController.signal,
1272
+ config: mergeModelConfigs([...this._configProviders].map((p) => p())),
1273
+ onUpdate: updateHandler
1274
+ });
1275
+ updateHandler(result);
1276
+ message.status = "done";
1277
+ this.repository.addOrUpdateMessage(parentId, { ...message });
1278
+ } catch (e) {
1279
+ message.status = "error";
1280
+ this.repository.addOrUpdateMessage(parentId, { ...message });
1281
+ console.error(e);
1282
+ } finally {
1283
+ this.abortController = null;
1284
+ this.notifySubscribers();
1285
+ }
1286
+ }
1287
+ cancelRun() {
1288
+ if (!this.abortController) return;
1289
+ this.abortController.abort();
1290
+ this.abortController = null;
1291
+ this.notifySubscribers();
1292
+ }
1293
+ notifySubscribers() {
1294
+ for (const callback of this._subscriptions) callback();
1295
+ }
1296
+ subscribe(callback) {
1297
+ this._subscriptions.add(callback);
1298
+ return () => this._subscriptions.delete(callback);
1299
+ }
1300
+ registerModelConfigProvider(provider) {
1301
+ this._configProviders.add(provider);
1302
+ return () => this._configProviders.delete(provider);
1303
+ }
1304
+ };
1305
+
1306
+ // src/runtime/local/useLocalRuntime.tsx
1307
+ var useLocalRuntime = (adapter) => {
1308
+ const [runtime] = (0, import_react29.useState)(() => new LocalRuntime(adapter));
1309
+ (0, import_react29.useInsertionEffect)(() => {
1310
+ runtime.adapter = adapter;
1311
+ });
1312
+ return runtime;
1313
+ };
1314
+
1315
+ // src/context/providers/AssistantRuntimeProvider.tsx
1316
+ var import_react33 = require("react");
1317
+
1318
+ // src/context/providers/AssistantProvider.tsx
1319
+ var import_react32 = require("react");
1320
+
1321
+ // src/context/AssistantContext.ts
1322
+ var import_react30 = require("react");
1323
+ var AssistantContext = (0, import_react30.createContext)(
1324
+ null
1325
+ );
1326
+
1327
+ // src/context/stores/AssistantModelConfig.ts
1328
+ var import_zustand4 = require("zustand");
1329
+
1065
1330
  // src/utils/ProxyConfigProvider.ts
1066
1331
  var ProxyConfigProvider = class {
1067
1332
  _providers = /* @__PURE__ */ new Set();
@@ -1090,7 +1355,7 @@ var makeAssistantModelConfigStore = () => (0, import_zustand4.create)(() => {
1090
1355
  });
1091
1356
 
1092
1357
  // src/context/providers/ThreadProvider.tsx
1093
- var import_react30 = require("react");
1358
+ var import_react31 = require("react");
1094
1359
 
1095
1360
  // src/context/stores/Composer.ts
1096
1361
  var import_zustand5 = require("zustand");
@@ -1165,11 +1430,11 @@ var ThreadProvider = ({
1165
1430
  children,
1166
1431
  runtime
1167
1432
  }) => {
1168
- const runtimeRef = (0, import_react30.useRef)(runtime);
1169
- (0, import_react30.useInsertionEffect)(() => {
1433
+ const runtimeRef = (0, import_react31.useRef)(runtime);
1434
+ (0, import_react31.useInsertionEffect)(() => {
1170
1435
  runtimeRef.current = runtime;
1171
1436
  });
1172
- const [{ context, onRuntimeUpdate }] = (0, import_react30.useState)(() => {
1437
+ const [{ context, onRuntimeUpdate }] = (0, import_react31.useState)(() => {
1173
1438
  const { useThread, onRuntimeUpdate: onRuntimeUpdate2 } = makeThreadStore(runtimeRef);
1174
1439
  const useViewport = makeThreadViewportStore();
1175
1440
  const useComposer = makeComposerStore(useThread);
@@ -1182,7 +1447,7 @@ var ThreadProvider = ({
1182
1447
  onRuntimeUpdate: onRuntimeUpdate2
1183
1448
  };
1184
1449
  });
1185
- (0, import_react30.useEffect)(() => {
1450
+ (0, import_react31.useEffect)(() => {
1186
1451
  onRuntimeUpdate();
1187
1452
  return runtime.subscribe(onRuntimeUpdate);
1188
1453
  }, [onRuntimeUpdate, runtime]);
@@ -1196,16 +1461,16 @@ var ThreadProvider = ({
1196
1461
  // src/context/providers/AssistantProvider.tsx
1197
1462
  var import_jsx_runtime22 = require("react/jsx-runtime");
1198
1463
  var AssistantProvider = ({ children, runtime }) => {
1199
- const runtimeRef = (0, import_react31.useRef)(runtime);
1200
- (0, import_react31.useInsertionEffect)(() => {
1464
+ const runtimeRef = (0, import_react32.useRef)(runtime);
1465
+ (0, import_react32.useInsertionEffect)(() => {
1201
1466
  runtimeRef.current = runtime;
1202
1467
  });
1203
- const [context] = (0, import_react31.useState)(() => {
1468
+ const [context] = (0, import_react32.useState)(() => {
1204
1469
  const useModelConfig = makeAssistantModelConfigStore();
1205
1470
  return { useModelConfig };
1206
1471
  });
1207
1472
  const getModelCOnfig = context.useModelConfig((c) => c.getModelConfig);
1208
- (0, import_react31.useEffect)(() => {
1473
+ (0, import_react32.useEffect)(() => {
1209
1474
  return runtime.registerModelConfigProvider(getModelCOnfig);
1210
1475
  }, [runtime, getModelCOnfig]);
1211
1476
  return /* @__PURE__ */ (0, import_jsx_runtime22.jsx)(AssistantContext.Provider, { value: context, children: /* @__PURE__ */ (0, import_jsx_runtime22.jsx)(ThreadProvider, { runtime, children }) });
@@ -1216,7 +1481,14 @@ var import_jsx_runtime23 = require("react/jsx-runtime");
1216
1481
  var AssistantRuntimeProviderImpl = ({ children, runtime }) => {
1217
1482
  return /* @__PURE__ */ (0, import_jsx_runtime23.jsx)(AssistantProvider, { runtime, children });
1218
1483
  };
1219
- var AssistantRuntimeProvider = (0, import_react32.memo)(AssistantRuntimeProviderImpl);
1484
+ var AssistantRuntimeProvider = (0, import_react33.memo)(AssistantRuntimeProviderImpl);
1485
+
1486
+ // src/internal.ts
1487
+ var internal_exports = {};
1488
+ __export(internal_exports, {
1489
+ MessageRepository: () => MessageRepository,
1490
+ ProxyConfigProvider: () => ProxyConfigProvider
1491
+ });
1220
1492
  // Annotate the CommonJS export names for ESM import in node:
1221
1493
  0 && (module.exports = {
1222
1494
  ActionBarPrimitive,
@@ -1224,12 +1496,14 @@ var AssistantRuntimeProvider = (0, import_react32.memo)(AssistantRuntimeProvider
1224
1496
  BranchPickerPrimitive,
1225
1497
  ComposerPrimitive,
1226
1498
  ContentPartPrimitive,
1499
+ INTERNAL,
1227
1500
  MessagePrimitive,
1228
1501
  ThreadPrimitive,
1229
1502
  useBeginMessageEdit,
1230
1503
  useCopyMessage,
1231
1504
  useGoToNextBranch,
1232
1505
  useGoToPreviousBranch,
1506
+ useLocalRuntime,
1233
1507
  useReloadMessage
1234
1508
  });
1235
1509
  //# sourceMappingURL=index.js.map