@assistant-ui/react 0.0.28 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. package/README.md +1 -1
  2. package/dist/AssistantRuntime-BM_jVV3g.d.mts +23 -0
  3. package/dist/AssistantRuntime-CBMSAJqH.d.mts +23 -0
  4. package/dist/AssistantRuntime-C_BvM7ZT.d.ts +23 -0
  5. package/dist/AssistantRuntime-DELpXWfG.d.ts +23 -0
  6. package/dist/ModelConfigTypes-B9UY4zxv.d.mts +55 -0
  7. package/dist/ModelConfigTypes-B9UY4zxv.d.ts +55 -0
  8. package/dist/ModelConfigTypes-BF5HxVrH.d.mts +55 -0
  9. package/dist/ModelConfigTypes-BF5HxVrH.d.ts +55 -0
  10. package/dist/ModelConfigTypes-Cf3yjaDu.d.mts +55 -0
  11. package/dist/ModelConfigTypes-Cf3yjaDu.d.ts +55 -0
  12. package/dist/chunk-3XZUKECF.mjs +207 -0
  13. package/dist/chunk-3XZUKECF.mjs.map +1 -0
  14. package/dist/chunk-4DQ2CIAD.mjs +69 -0
  15. package/dist/chunk-4DQ2CIAD.mjs.map +1 -0
  16. package/dist/chunk-5YONSDN4.mjs +200 -0
  17. package/dist/chunk-5YONSDN4.mjs.map +1 -0
  18. package/dist/chunk-CY4TTHR7.mjs +76 -0
  19. package/dist/chunk-CY4TTHR7.mjs.map +1 -0
  20. package/dist/chunk-DKAWDNW5.mjs +22 -0
  21. package/dist/chunk-DKAWDNW5.mjs.map +1 -0
  22. package/dist/chunk-GQKH2ADD.mjs +165 -0
  23. package/dist/chunk-GQKH2ADD.mjs.map +1 -0
  24. package/dist/chunk-J5LGTIGS.mjs +10 -0
  25. package/dist/chunk-J5LGTIGS.mjs.map +1 -0
  26. package/dist/chunk-X4HBDEFP.mjs +30 -0
  27. package/dist/chunk-X4HBDEFP.mjs.map +1 -0
  28. package/dist/experimental.d.mts +120 -0
  29. package/dist/experimental.d.ts +120 -0
  30. package/dist/experimental.js +407 -0
  31. package/dist/experimental.js.map +1 -0
  32. package/dist/experimental.mjs +144 -0
  33. package/dist/experimental.mjs.map +1 -0
  34. package/dist/index.d.mts +6 -266
  35. package/dist/index.d.ts +6 -266
  36. package/dist/index.js +77 -730
  37. package/dist/index.js.map +1 -1
  38. package/dist/index.mjs +62 -786
  39. package/dist/index.mjs.map +1 -1
  40. package/dist/internal-dlLjX30u.d.mts +75 -0
  41. package/dist/internal-dlLjX30u.d.ts +75 -0
  42. package/dist/internal.d.mts +25 -0
  43. package/dist/internal.d.ts +25 -0
  44. package/dist/internal.js +225 -0
  45. package/dist/internal.js.map +1 -0
  46. package/dist/internal.mjs +12 -0
  47. package/dist/internal.mjs.map +1 -0
  48. package/package.json +25 -5
package/dist/index.js CHANGED
@@ -37,24 +37,11 @@ __export(src_exports, {
37
37
  ContentPartPrimitive: () => contentPart_exports,
38
38
  MessagePrimitive: () => message_exports,
39
39
  ThreadPrimitive: () => thread_exports,
40
- VercelAIAssistantProvider: () => VercelAIAssistantProvider,
41
- VercelRSCAssistantProvider: () => VercelRSCAssistantProvider,
42
- getVercelAIMessage: () => getVercelAIMessage,
43
- getVercelRSCMessage: () => getVercelRSCMessage,
44
- unstable_MessageRepository: () => MessageRepository,
45
- unstable_useComposerContext: () => useComposerContext,
46
- unstable_useContentPartContext: () => useContentPartContext,
47
- unstable_useLocalRuntime: () => useLocalRuntime,
48
- unstable_useMessageContext: () => useMessageContext,
49
- unstable_useThreadContext: () => useThreadContext,
50
40
  useBeginMessageEdit: () => useBeginMessageEdit,
51
41
  useCopyMessage: () => useCopyMessage,
52
42
  useGoToNextBranch: () => useGoToNextBranch,
53
43
  useGoToPreviousBranch: () => useGoToPreviousBranch,
54
- useReloadMessage: () => useReloadMessage,
55
- useVercelRSCRuntime: () => useVercelRSCRuntime,
56
- useVercelUseAssistantRuntime: () => useVercelUseAssistantRuntime,
57
- useVercelUseChatRuntime: () => useVercelUseChatRuntime
44
+ useReloadMessage: () => useReloadMessage
58
45
  });
59
46
  module.exports = __toCommonJS(src_exports);
60
47
 
@@ -137,7 +124,7 @@ var ThreadContext = (0, import_react5.createContext)(null);
137
124
  var useThreadContext = () => {
138
125
  const context = (0, import_react5.useContext)(ThreadContext);
139
126
  if (!context)
140
- throw new Error("This component must be used within an AssistantProvider.");
127
+ throw new Error("This component must be used within an AssistantRuntimeProvider.");
141
128
  return context;
142
129
  };
143
130
 
@@ -1041,585 +1028,73 @@ __export(contentPart_exports, {
1041
1028
  InProgressIndicator: () => ContentPartInProgressIndicator
1042
1029
  });
1043
1030
 
1044
- // ../react-ai-sdk/src/rsc/useVercelRSCRuntime.tsx
1045
- var import_react30 = require("react");
1031
+ // src/context/providers/AssistantRuntimeProvider.tsx
1032
+ var import_react32 = require("react");
1046
1033
 
1047
- // ../react-ai-sdk/src/rsc/VercelRSCRuntime.tsx
1048
- var import_zustand4 = require("zustand");
1034
+ // src/context/providers/AssistantProvider.tsx
1035
+ var import_react31 = require("react");
1049
1036
 
1050
- // ../react-ai-sdk/src/rsc/useVercelRSCSync.tsx
1037
+ // src/context/AssistantContext.ts
1051
1038
  var import_react29 = require("react");
1039
+ var AssistantContext = (0, import_react29.createContext)(
1040
+ null
1041
+ );
1052
1042
 
1053
- // ../react-ai-sdk/src/utils/ThreadMessageConverter.ts
1054
- var ThreadMessageConverter = class {
1055
- cache = /* @__PURE__ */ new WeakMap();
1056
- convertMessages(messages, converter, keyMapper = (key) => key) {
1057
- return messages.map((m) => {
1058
- const key = keyMapper(m);
1059
- const cached = this.cache.get(key);
1060
- const newMessage = converter(m, cached);
1061
- this.cache.set(key, newMessage);
1062
- return newMessage;
1063
- });
1064
- }
1065
- };
1066
-
1067
- // ../react-ai-sdk/src/rsc/getVercelRSCMessage.tsx
1068
- var symbolInnerRSCMessage = Symbol("innerVercelRSCMessage");
1069
- var getVercelRSCMessage = (message) => {
1070
- return message[symbolInnerRSCMessage];
1071
- };
1072
-
1073
- // ../react-ai-sdk/src/rsc/useVercelRSCSync.tsx
1074
- var vercelToThreadMessage = (converter, rawMessage) => {
1075
- const message = converter(rawMessage);
1076
- return {
1077
- id: message.id,
1078
- role: message.role,
1079
- content: [{ type: "ui", display: message.display }],
1080
- createdAt: message.createdAt ?? /* @__PURE__ */ new Date(),
1081
- ...{ status: "done" },
1082
- [symbolInnerRSCMessage]: rawMessage
1083
- };
1084
- };
1085
- var useVercelRSCSync = (adapter, updateData) => {
1086
- const [converter, convertCallback] = (0, import_react29.useMemo)(() => {
1087
- const rscConverter = adapter.convertMessage ?? ((m) => m);
1088
- const convertCallback2 = (m, cache) => {
1089
- if (cache) return cache;
1090
- return vercelToThreadMessage(rscConverter, m);
1091
- };
1092
- return [new ThreadMessageConverter(), convertCallback2];
1093
- }, [adapter.convertMessage]);
1094
- (0, import_react29.useEffect)(() => {
1095
- updateData(converter.convertMessages(adapter.messages, convertCallback));
1096
- }, [updateData, converter, convertCallback, adapter.messages]);
1097
- };
1098
-
1099
- // ../react-ai-sdk/src/rsc/VercelRSCRuntime.tsx
1100
- var EMPTY_BRANCHES = Object.freeze([]);
1101
- var VercelRSCRuntime = class {
1102
- constructor(adapter) {
1103
- this.adapter = adapter;
1104
- this.useAdapter = (0, import_zustand4.create)(() => ({
1105
- adapter
1106
- }));
1107
- }
1108
- useAdapter;
1109
- _subscriptions = /* @__PURE__ */ new Set();
1110
- isRunning = false;
1111
- messages = [];
1112
- withRunning = (callback) => {
1113
- this.isRunning = true;
1114
- return callback.finally(() => {
1115
- this.isRunning = false;
1116
- });
1117
- };
1118
- getBranches() {
1119
- return EMPTY_BRANCHES;
1120
- }
1121
- switchToBranch() {
1122
- throw new Error(
1123
- "Branch switching is not supported by VercelRSCAssistantProvider."
1124
- );
1125
- }
1126
- async append(message) {
1127
- if (message.parentId !== (this.messages.at(-1)?.id ?? null)) {
1128
- if (!this.adapter.edit)
1129
- throw new Error(
1130
- "Message editing is not enabled, please provide an edit callback to VercelRSCAssistantProvider."
1131
- );
1132
- await this.withRunning(this.adapter.edit(message));
1133
- } else {
1134
- await this.withRunning(this.adapter.append(message));
1135
- }
1136
- }
1137
- async startRun(parentId) {
1138
- if (!this.adapter.reload)
1139
- throw new Error(
1140
- "Message reloading is not enabled, please provide a reload callback to VercelRSCAssistantProvider."
1141
- );
1142
- await this.withRunning(this.adapter.reload(parentId));
1143
- }
1144
- cancelRun() {
1145
- if (process.env["NODE_ENV"] === "development") {
1146
- console.warn(
1147
- "Run cancellation is not supported by VercelRSCAssistantProvider."
1148
- );
1149
- }
1150
- }
1151
- subscribe(callback) {
1152
- this._subscriptions.add(callback);
1153
- return () => this._subscriptions.delete(callback);
1154
- }
1155
- onAdapterUpdated() {
1156
- if (this.useAdapter.getState().adapter !== this.adapter) {
1157
- this.useAdapter.setState({ adapter: this.adapter });
1158
- }
1159
- }
1160
- updateData = (messages) => {
1161
- this.messages = messages;
1162
- for (const callback of this._subscriptions) callback();
1163
- };
1164
- unstable_synchronizer = () => {
1165
- const { adapter } = this.useAdapter();
1166
- useVercelRSCSync(adapter, this.updateData);
1167
- return null;
1168
- };
1169
- };
1170
-
1171
- // ../react-ai-sdk/src/rsc/useVercelRSCRuntime.tsx
1172
- var useVercelRSCRuntime = (adapter) => {
1173
- const [runtime] = (0, import_react30.useState)(() => new VercelRSCRuntime(adapter));
1174
- (0, import_react30.useInsertionEffect)(() => {
1175
- runtime.adapter = adapter;
1176
- });
1177
- (0, import_react30.useEffect)(() => {
1178
- runtime.onAdapterUpdated();
1179
- });
1180
- return runtime;
1181
- };
1182
-
1183
- // ../react-ai-sdk/src/ui/use-chat/useVercelUseChatRuntime.tsx
1184
- var import_react35 = require("react");
1185
-
1186
- // ../react-ai-sdk/src/ui/VercelAIRuntime.tsx
1187
- var import_zustand5 = require("zustand");
1188
-
1189
- // ../react-ai-sdk/src/ui/getVercelAIMessage.tsx
1190
- var symbolInnerAIMessage = Symbol("innerVercelAIUIMessage");
1191
- var getVercelAIMessage = (message) => {
1192
- return message[symbolInnerAIMessage];
1193
- };
1194
-
1195
- // ../react-ai-sdk/src/ui/utils/sliceMessagesUntil.tsx
1196
- var sliceMessagesUntil = (messages, messageId) => {
1197
- if (messageId == null) return [];
1198
- let messageIdx = messages.findIndex((m) => m.id === messageId);
1199
- if (messageIdx === -1)
1200
- throw new Error(
1201
- "useVercelAIThreadState: Message not found. This is liekly an internal bug in assistant-ui."
1202
- );
1203
- while (messages[messageIdx + 1]?.role === "assistant") {
1204
- messageIdx++;
1205
- }
1206
- return messages.slice(0, messageIdx + 1);
1207
- };
1043
+ // src/context/stores/AssistantModelConfig.ts
1044
+ var import_zustand4 = require("zustand");
1208
1045
 
1209
- // ../react-ai-sdk/src/ui/utils/useVercelAIComposerSync.tsx
1210
- var import_react32 = require("react");
1211
- var useVercelAIComposerSync = (vercel) => {
1212
- const { useComposer } = useThreadContext();
1213
- (0, import_react32.useEffect)(() => {
1214
- useComposer.setState({
1215
- value: vercel.input,
1216
- setValue: vercel.setInput
1217
- });
1218
- }, [useComposer, vercel.input, vercel.setInput]);
1219
- };
1046
+ // src/utils/ModelConfigTypes.ts
1047
+ var mergeModelConfigs = (configs) => {
1048
+ return configs.reduce((acc, config) => {
1049
+ if (config.system) {
1050
+ if (acc.system) {
1051
+ acc.system += `
1220
1052
 
1221
- // ../react-ai-sdk/src/ui/utils/useVercelAIThreadSync.tsx
1222
- var import_react33 = require("react");
1223
- var getIsRunning = (vercel) => {
1224
- if ("isLoading" in vercel) return vercel.isLoading;
1225
- return vercel.status === "in_progress";
1226
- };
1227
- var vercelToThreadMessage2 = (messages, status) => {
1228
- const firstMessage = messages[0];
1229
- if (!firstMessage) throw new Error("No messages found");
1230
- const common = {
1231
- id: firstMessage.id,
1232
- createdAt: firstMessage.createdAt ?? /* @__PURE__ */ new Date(),
1233
- [symbolInnerAIMessage]: messages
1234
- };
1235
- switch (firstMessage.role) {
1236
- case "user":
1237
- if (messages.length > 1) {
1238
- throw new Error(
1239
- "Multiple user messages found. This is likely an internal bug in assistant-ui."
1240
- );
1053
+ ${config.system}`;
1054
+ } else {
1055
+ acc.system = config.system;
1241
1056
  }
1242
- return {
1243
- ...common,
1244
- role: "user",
1245
- content: [{ type: "text", text: firstMessage.content }]
1246
- };
1247
- case "assistant":
1248
- return {
1249
- ...common,
1250
- role: "assistant",
1251
- content: messages.flatMap((message) => [
1252
- ...message.content ? [{ type: "text", text: message.content }] : [],
1253
- ...message.toolInvocations?.map(
1254
- (t) => ({
1255
- type: "tool-call",
1256
- name: t.toolName,
1257
- args: t.args,
1258
- result: "result" in t ? t.result : void 0
1259
- })
1260
- ) ?? []
1261
- ]),
1262
- status
1263
- };
1264
- default:
1265
- throw new Error(
1266
- `You have a message with an unsupported role. The role ${firstMessage.role} is not supported.`
1267
- );
1268
- }
1269
- };
1270
- var hasItems = (messages) => messages.length > 0;
1271
- var chunkedMessages = (messages) => {
1272
- const chunks = [];
1273
- let currentChunk = [];
1274
- for (const message of messages) {
1275
- if (message.role === "assistant") {
1276
- currentChunk.push(message);
1277
- } else {
1278
- if (hasItems(currentChunk)) {
1279
- chunks.push(currentChunk);
1280
- currentChunk = [];
1281
- }
1282
- chunks.push([message]);
1283
1057
  }
1284
- }
1285
- if (hasItems(currentChunk)) {
1286
- chunks.push(currentChunk);
1287
- }
1288
- return chunks;
1289
- };
1290
- var shallowArrayEqual = (a, b) => {
1291
- if (a.length !== b.length) return false;
1292
- for (let i = 0; i < a.length; i++) {
1293
- if (a[i] !== b[i]) return false;
1294
- }
1295
- return true;
1296
- };
1297
- var useVercelAIThreadSync = (vercel, updateData) => {
1298
- const isRunning = getIsRunning(vercel);
1299
- const converter = (0, import_react33.useMemo)(() => new ThreadMessageConverter(), []);
1300
- (0, import_react33.useEffect)(() => {
1301
- const lastMessageId = vercel.messages.at(-1)?.id;
1302
- const convertCallback = (messages2, cache) => {
1303
- const status = lastMessageId === messages2[0].id && isRunning ? "in_progress" : "done";
1304
- if (cache && shallowArrayEqual(cache.content, messages2) && (cache.role === "user" || cache.status === status))
1305
- return cache;
1306
- return vercelToThreadMessage2(messages2, status);
1307
- };
1308
- const messages = converter.convertMessages(
1309
- chunkedMessages(vercel.messages),
1310
- convertCallback,
1311
- (m) => m[0]
1312
- );
1313
- updateData(isRunning, messages);
1314
- }, [updateData, isRunning, vercel.messages, converter]);
1315
- };
1316
-
1317
- // ../react-ai-sdk/src/ui/VercelAIRuntime.tsx
1318
- var hasUpcomingMessage = (isRunning, messages) => {
1319
- return isRunning && messages[messages.length - 1]?.role !== "assistant";
1320
- };
1321
- var VercelAIRuntime = class {
1322
- constructor(vercel) {
1323
- this.vercel = vercel;
1324
- this.useVercel = (0, import_zustand5.create)(() => ({
1325
- vercel
1326
- }));
1327
- }
1328
- _subscriptions = /* @__PURE__ */ new Set();
1329
- repository = new MessageRepository();
1330
- assistantOptimisticId = null;
1331
- useVercel;
1332
- messages = [];
1333
- isRunning = false;
1334
- getBranches(messageId) {
1335
- return this.repository.getBranches(messageId);
1336
- }
1337
- switchToBranch(branchId) {
1338
- this.repository.switchToBranch(branchId);
1339
- this.updateVercelMessages(this.repository.getMessages());
1340
- }
1341
- async append(message) {
1342
- if (message.content.length !== 1 || message.content[0]?.type !== "text")
1343
- throw new Error("Only text content is supported by Vercel AI SDK.");
1344
- const newMessages = sliceMessagesUntil(
1345
- this.vercel.messages,
1346
- message.parentId
1347
- );
1348
- this.vercel.setMessages(newMessages);
1349
- await this.vercel.append({
1350
- role: "user",
1351
- content: message.content[0].text
1352
- });
1353
- }
1354
- async startRun(parentId) {
1355
- const reloadMaybe = "reload" in this.vercel ? this.vercel.reload : void 0;
1356
- if (!reloadMaybe)
1357
- throw new Error(
1358
- "Reload is not supported by Vercel AI SDK's useAssistant."
1359
- );
1360
- const newMessages = sliceMessagesUntil(this.vercel.messages, parentId);
1361
- this.vercel.setMessages(newMessages);
1362
- await reloadMaybe();
1363
- }
1364
- cancelRun() {
1365
- const previousMessage = this.vercel.messages.at(-1);
1366
- this.vercel.stop();
1367
- if (this.assistantOptimisticId) {
1368
- this.repository.deleteMessage(this.assistantOptimisticId);
1369
- this.assistantOptimisticId = null;
1370
- }
1371
- let messages = this.repository.getMessages();
1372
- if (previousMessage?.role === "user" && previousMessage.id === messages.at(-1)?.id) {
1373
- this.vercel.setInput(previousMessage.content);
1374
- this.repository.deleteMessage(previousMessage.id);
1375
- messages = this.repository.getMessages();
1376
- }
1377
- setTimeout(() => {
1378
- this.updateVercelMessages(messages);
1379
- }, 0);
1380
- }
1381
- subscribe(callback) {
1382
- this._subscriptions.add(callback);
1383
- return () => this._subscriptions.delete(callback);
1384
- }
1385
- updateVercelMessages = (messages) => {
1386
- this.vercel.setMessages(
1387
- messages.flatMap(getVercelAIMessage).filter((m) => m != null)
1388
- );
1389
- };
1390
- onVercelUpdated() {
1391
- if (this.useVercel.getState().vercel !== this.vercel) {
1392
- this.useVercel.setState({ vercel: this.vercel });
1393
- }
1394
- }
1395
- updateData = (isRunning, vm) => {
1396
- for (let i = 0; i < vm.length; i++) {
1397
- const message = vm[i];
1398
- const parent = vm[i - 1];
1399
- this.repository.addOrUpdateMessage(parent?.id ?? null, message);
1400
- }
1401
- if (this.assistantOptimisticId) {
1402
- this.repository.deleteMessage(this.assistantOptimisticId);
1403
- this.assistantOptimisticId = null;
1404
- }
1405
- if (hasUpcomingMessage(isRunning, vm)) {
1406
- this.assistantOptimisticId = this.repository.appendOptimisticMessage(
1407
- vm.at(-1)?.id ?? null,
1408
- {
1409
- role: "assistant",
1410
- content: [{ type: "text", text: "" }]
1411
- }
1412
- );
1058
+ if (config.tools) {
1059
+ acc.tools = { ...acc.tools, ...config.tools };
1413
1060
  }
1414
- this.repository.resetHead(
1415
- this.assistantOptimisticId ?? vm.at(-1)?.id ?? null
1416
- );
1417
- this.messages = this.repository.getMessages();
1418
- this.isRunning = isRunning;
1419
- for (const callback of this._subscriptions) callback();
1420
- };
1421
- unstable_synchronizer = () => {
1422
- const { vercel } = this.useVercel();
1423
- useVercelAIThreadSync(vercel, this.updateData);
1424
- useVercelAIComposerSync(vercel);
1425
- return null;
1426
- };
1061
+ return acc;
1062
+ }, {});
1427
1063
  };
1428
1064
 
1429
- // ../react-ai-sdk/src/ui/use-chat/useVercelUseChatRuntime.tsx
1430
- var useVercelUseChatRuntime = (chatHelpers) => {
1431
- const [runtime] = (0, import_react35.useState)(() => new VercelAIRuntime(chatHelpers));
1432
- (0, import_react35.useInsertionEffect)(() => {
1433
- runtime.vercel = chatHelpers;
1434
- });
1435
- (0, import_react35.useEffect)(() => {
1436
- runtime.onVercelUpdated();
1437
- });
1438
- return runtime;
1439
- };
1440
-
1441
- // ../react-ai-sdk/src/ui/use-assistant/useVercelUseAssistantRuntime.tsx
1442
- var import_react36 = require("react");
1443
- var useVercelUseAssistantRuntime = (assistantHelpers) => {
1444
- const [runtime] = (0, import_react36.useState)(() => new VercelAIRuntime(assistantHelpers));
1445
- (0, import_react36.useInsertionEffect)(() => {
1446
- runtime.vercel = assistantHelpers;
1447
- });
1448
- (0, import_react36.useEffect)(() => {
1449
- runtime.onVercelUpdated();
1450
- });
1451
- return runtime;
1452
- };
1453
-
1454
- // src/runtime/utils/idUtils.tsx
1455
- var import_non_secure = require("nanoid/non-secure");
1456
- var generateId = (0, import_non_secure.customAlphabet)(
1457
- "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
1458
- 7
1459
- );
1460
- var optimisticPrefix = "__optimistic__";
1461
- var generateOptimisticId = () => `${optimisticPrefix}${generateId()}`;
1462
-
1463
- // src/runtime/utils/MessageRepository.tsx
1464
- var findHead = (message) => {
1465
- if (message.next) return findHead(message.next);
1466
- return message;
1467
- };
1468
- var MessageRepository = class {
1469
- messages = /* @__PURE__ */ new Map();
1470
- // message_id -> item
1471
- head = null;
1472
- root = {
1473
- children: []
1474
- };
1475
- performOp(newParent, child, operation) {
1476
- const parentOrRoot = child.prev ?? this.root;
1477
- const newParentOrRoot = newParent ?? this.root;
1478
- if (operation === "relink" && parentOrRoot === newParentOrRoot) return;
1479
- if (operation !== "link") {
1480
- parentOrRoot.children = parentOrRoot.children.filter(
1481
- (m) => m !== child.current.id
1482
- );
1483
- if (child.prev?.next === child) {
1484
- const fallbackId = child.prev.children.at(-1);
1485
- const fallback = fallbackId ? this.messages.get(fallbackId) : null;
1486
- if (fallback === void 0) {
1487
- throw new Error(
1488
- "MessageRepository(performOp/cut): Fallback sibling message not found. This is likely an internal bug in assistant-ui."
1489
- );
1490
- }
1491
- child.prev.next = fallback;
1492
- }
1493
- }
1494
- if (operation !== "cut") {
1495
- newParentOrRoot.children = [
1496
- ...newParentOrRoot.children,
1497
- child.current.id
1498
- ];
1499
- if (newParent && (findHead(child) === this.head || newParent.next === null)) {
1500
- newParent.next = child;
1501
- }
1502
- child.prev = newParent;
1503
- }
1504
- }
1505
- getMessages() {
1506
- const messages = new Array(this.head?.level ?? 0);
1507
- for (let current = this.head; current; current = current.prev) {
1508
- messages[current.level] = current.current;
1509
- }
1510
- return messages;
1065
+ // src/utils/ProxyConfigProvider.ts
1066
+ var ProxyConfigProvider = class {
1067
+ _providers = /* @__PURE__ */ new Set();
1068
+ getModelConfig() {
1069
+ return mergeModelConfigs([...this._providers].map((p) => p()));
1511
1070
  }
1512
- addOrUpdateMessage(parentId, message) {
1513
- const existingItem = this.messages.get(message.id);
1514
- const prev = parentId ? this.messages.get(parentId) : null;
1515
- if (prev === void 0)
1516
- throw new Error(
1517
- "MessageRepository(addOrUpdateMessage): Parent message not found. This is likely an internal bug in assistant-ui."
1518
- );
1519
- if (existingItem) {
1520
- existingItem.current = message;
1521
- this.performOp(prev, existingItem, "relink");
1522
- return;
1523
- }
1524
- const newItem = {
1525
- prev,
1526
- current: message,
1527
- next: null,
1528
- children: [],
1529
- level: prev ? prev.level + 1 : 0
1071
+ registerModelConfigProvider(provider) {
1072
+ this._providers.add(provider);
1073
+ return () => {
1074
+ this._providers.delete(provider);
1530
1075
  };
1531
- this.messages.set(message.id, newItem);
1532
- this.performOp(prev, newItem, "link");
1533
- if (this.head === prev) {
1534
- this.head = newItem;
1535
- }
1536
- }
1537
- appendOptimisticMessage(parentId, message) {
1538
- let optimisticId;
1539
- do {
1540
- optimisticId = generateOptimisticId();
1541
- } while (this.messages.has(optimisticId));
1542
- this.addOrUpdateMessage(parentId, {
1543
- ...message,
1544
- id: optimisticId,
1545
- createdAt: /* @__PURE__ */ new Date(),
1546
- ...message.role === "assistant" ? { status: "in_progress" } : void 0
1547
- });
1548
- return optimisticId;
1549
- }
1550
- deleteMessage(messageId, replacementId) {
1551
- const message = this.messages.get(messageId);
1552
- if (!message)
1553
- throw new Error(
1554
- "MessageRepository(deleteMessage): Optimistic message not found. This is likely an internal bug in assistant-ui."
1555
- );
1556
- const replacement = replacementId === void 0 ? message.prev : replacementId === null ? null : this.messages.get(replacementId);
1557
- if (replacement === void 0)
1558
- throw new Error(
1559
- "MessageRepository(deleteMessage): Replacement not found. This is likely an internal bug in assistant-ui."
1560
- );
1561
- for (const child of message.children) {
1562
- const childMessage = this.messages.get(child);
1563
- if (!childMessage)
1564
- throw new Error(
1565
- "MessageRepository(deleteMessage): Child message not found. This is likely an internal bug in assistant-ui."
1566
- );
1567
- this.performOp(replacement, childMessage, "relink");
1568
- }
1569
- this.performOp(null, message, "cut");
1570
- this.messages.delete(messageId);
1571
- if (this.head === message) {
1572
- this.head = replacement ? findHead(replacement) : null;
1573
- }
1574
- }
1575
- getBranches(messageId) {
1576
- const message = this.messages.get(messageId);
1577
- if (!message)
1578
- throw new Error(
1579
- "MessageRepository(getBranches): Message not found. This is likely an internal bug in assistant-ui."
1580
- );
1581
- const { children } = message.prev ?? this.root;
1582
- return children;
1583
- }
1584
- switchToBranch(messageId) {
1585
- const message = this.messages.get(messageId);
1586
- if (!message)
1587
- throw new Error(
1588
- "MessageRepository(switchToBranch): Branch not found. This is likely an internal bug in assistant-ui."
1589
- );
1590
- if (message.prev) {
1591
- message.prev.next = message;
1592
- }
1593
- this.head = findHead(message);
1594
- }
1595
- resetHead(messageId) {
1596
- if (messageId === null) {
1597
- this.head = null;
1598
- return;
1599
- }
1600
- const message = this.messages.get(messageId);
1601
- if (!message)
1602
- throw new Error(
1603
- "MessageRepository(resetHead): Branch not found. This is likely an internal bug in assistant-ui."
1604
- );
1605
- this.head = message;
1606
- for (let current = message; current; current = current.prev) {
1607
- if (current.prev) {
1608
- current.prev.next = current;
1609
- }
1610
- }
1611
1076
  }
1612
1077
  };
1613
1078
 
1614
- // src/context/providers/AssistantRuntimeProvider.tsx
1615
- var import_react38 = require("react");
1079
+ // src/context/stores/AssistantModelConfig.ts
1080
+ var makeAssistantModelConfigStore = () => (0, import_zustand4.create)(() => {
1081
+ const proxy = new ProxyConfigProvider();
1082
+ return {
1083
+ getModelConfig: () => {
1084
+ return proxy.getModelConfig();
1085
+ },
1086
+ registerModelConfigProvider: (provider) => {
1087
+ return proxy.registerModelConfigProvider(provider);
1088
+ }
1089
+ };
1090
+ });
1616
1091
 
1617
1092
  // src/context/providers/ThreadProvider.tsx
1618
- var import_react37 = require("react");
1093
+ var import_react30 = require("react");
1619
1094
 
1620
1095
  // src/context/stores/Composer.ts
1621
- var import_zustand6 = require("zustand");
1622
- var makeComposerStore = (useThread) => (0, import_zustand6.create)()((set, get, store) => {
1096
+ var import_zustand5 = require("zustand");
1097
+ var makeComposerStore = (useThread) => (0, import_zustand5.create)()((set, get, store) => {
1623
1098
  return {
1624
1099
  ...makeBaseComposer(set, get, store),
1625
1100
  isEditing: true,
@@ -1641,9 +1116,9 @@ var makeComposerStore = (useThread) => (0, import_zustand6.create)()((set, get,
1641
1116
  });
1642
1117
 
1643
1118
  // src/context/stores/Thread.ts
1644
- var import_zustand7 = require("zustand");
1119
+ var import_zustand6 = require("zustand");
1645
1120
  var makeThreadStore = (runtimeRef) => {
1646
- const useThread = (0, import_zustand7.create)(() => ({
1121
+ const useThread = (0, import_zustand6.create)(() => ({
1647
1122
  messages: runtimeRef.current.messages,
1648
1123
  isRunning: runtimeRef.current.isRunning,
1649
1124
  getBranches: (messageId) => runtimeRef.current.getBranches(messageId),
@@ -1665,10 +1140,10 @@ var makeThreadStore = (runtimeRef) => {
1665
1140
  };
1666
1141
 
1667
1142
  // src/context/stores/ThreadViewport.tsx
1668
- var import_zustand8 = require("zustand");
1143
+ var import_zustand7 = require("zustand");
1669
1144
  var makeThreadViewportStore = () => {
1670
1145
  const scrollToBottomListeners = /* @__PURE__ */ new Set();
1671
- return (0, import_zustand8.create)(() => ({
1146
+ return (0, import_zustand7.create)(() => ({
1672
1147
  isAtBottom: true,
1673
1148
  scrollToBottom: () => {
1674
1149
  for (const listener of scrollToBottomListeners) {
@@ -1690,11 +1165,11 @@ var ThreadProvider = ({
1690
1165
  children,
1691
1166
  runtime
1692
1167
  }) => {
1693
- const runtimeRef = (0, import_react37.useRef)(runtime);
1694
- (0, import_react37.useInsertionEffect)(() => {
1168
+ const runtimeRef = (0, import_react30.useRef)(runtime);
1169
+ (0, import_react30.useInsertionEffect)(() => {
1695
1170
  runtimeRef.current = runtime;
1696
1171
  });
1697
- const [{ context, onRuntimeUpdate }] = (0, import_react37.useState)(() => {
1172
+ const [{ context, onRuntimeUpdate }] = (0, import_react30.useState)(() => {
1698
1173
  const { useThread, onRuntimeUpdate: onRuntimeUpdate2 } = makeThreadStore(runtimeRef);
1699
1174
  const useViewport = makeThreadViewportStore();
1700
1175
  const useComposer = makeComposerStore(useThread);
@@ -1707,7 +1182,7 @@ var ThreadProvider = ({
1707
1182
  onRuntimeUpdate: onRuntimeUpdate2
1708
1183
  };
1709
1184
  });
1710
- (0, import_react37.useEffect)(() => {
1185
+ (0, import_react30.useEffect)(() => {
1711
1186
  onRuntimeUpdate();
1712
1187
  return runtime.subscribe(onRuntimeUpdate);
1713
1188
  }, [onRuntimeUpdate, runtime]);
@@ -1718,145 +1193,30 @@ var ThreadProvider = ({
1718
1193
  ] });
1719
1194
  };
1720
1195
 
1721
- // src/context/providers/AssistantRuntimeProvider.tsx
1196
+ // src/context/providers/AssistantProvider.tsx
1722
1197
  var import_jsx_runtime22 = require("react/jsx-runtime");
1723
- var AssistantRuntimeProviderImpl = ({ children, runtime }) => {
1724
- return /* @__PURE__ */ (0, import_jsx_runtime22.jsx)(ThreadProvider, { runtime, children });
1198
+ var AssistantProvider = ({ children, runtime }) => {
1199
+ const runtimeRef = (0, import_react31.useRef)(runtime);
1200
+ (0, import_react31.useInsertionEffect)(() => {
1201
+ runtimeRef.current = runtime;
1202
+ });
1203
+ const [context] = (0, import_react31.useState)(() => {
1204
+ const useModelConfig = makeAssistantModelConfigStore();
1205
+ return { useModelConfig };
1206
+ });
1207
+ const getModelCOnfig = context.useModelConfig((c) => c.getModelConfig);
1208
+ (0, import_react31.useEffect)(() => {
1209
+ return runtime.registerModelConfigProvider(getModelCOnfig);
1210
+ }, [runtime, getModelCOnfig]);
1211
+ return /* @__PURE__ */ (0, import_jsx_runtime22.jsx)(AssistantContext.Provider, { value: context, children: /* @__PURE__ */ (0, import_jsx_runtime22.jsx)(ThreadProvider, { runtime, children }) });
1725
1212
  };
1726
- var AssistantRuntimeProvider = (0, import_react38.memo)(AssistantRuntimeProviderImpl);
1727
1213
 
1728
- // src/runtime/vercel-deprecated/VercelAIAssistantProvider.tsx
1214
+ // src/context/providers/AssistantRuntimeProvider.tsx
1729
1215
  var import_jsx_runtime23 = require("react/jsx-runtime");
1730
- var VercelUseChatRuntimeProvider = ({
1731
- chat,
1732
- children
1733
- }) => {
1734
- const runtime = useVercelUseChatRuntime(chat);
1735
- return /* @__PURE__ */ (0, import_jsx_runtime23.jsx)(AssistantRuntimeProvider, { runtime, children });
1736
- };
1737
- var VercelUseAssistantRuntimeProvider = ({
1738
- assistant,
1739
- children
1740
- }) => {
1741
- const runtime = useVercelUseAssistantRuntime(assistant);
1742
- return /* @__PURE__ */ (0, import_jsx_runtime23.jsx)(AssistantRuntimeProvider, { runtime, children });
1743
- };
1744
- var VercelAIAssistantProvider = ({
1745
- children,
1746
- ...rest
1747
- }) => {
1748
- if ("chat" in rest) {
1749
- return /* @__PURE__ */ (0, import_jsx_runtime23.jsx)(VercelUseChatRuntimeProvider, { chat: rest.chat, children });
1750
- }
1751
- return /* @__PURE__ */ (0, import_jsx_runtime23.jsx)(VercelUseAssistantRuntimeProvider, { assistant: rest.assistant, children });
1752
- };
1753
-
1754
- // src/runtime/vercel-deprecated/VercelRSCAssistantProvider.tsx
1755
- var import_jsx_runtime24 = require("react/jsx-runtime");
1756
- var VercelRSCAssistantProvider = ({
1757
- children,
1758
- ...adapter
1759
- }) => {
1760
- const runtime = useVercelRSCRuntime(adapter);
1761
- return /* @__PURE__ */ (0, import_jsx_runtime24.jsx)(AssistantRuntimeProvider, { runtime, children });
1762
- };
1763
-
1764
- // src/runtime/local/useLocalRuntime.tsx
1765
- var import_react39 = require("react");
1766
-
1767
- // src/runtime/local/LocalRuntime.tsx
1768
- var LocalRuntime = class {
1769
- constructor(adapter) {
1770
- this.adapter = adapter;
1771
- }
1772
- _subscriptions = /* @__PURE__ */ new Set();
1773
- abortController = null;
1774
- repository = new MessageRepository();
1775
- get messages() {
1776
- return this.repository.getMessages();
1777
- }
1778
- get isRunning() {
1779
- return this.abortController != null;
1780
- }
1781
- getBranches(messageId) {
1782
- return this.repository.getBranches(messageId);
1783
- }
1784
- switchToBranch(branchId) {
1785
- this.repository.switchToBranch(branchId);
1786
- this.notifySubscribers();
1787
- }
1788
- async append(message) {
1789
- const userMessageId = generateId();
1790
- const userMessage = {
1791
- id: userMessageId,
1792
- role: "user",
1793
- content: message.content,
1794
- createdAt: /* @__PURE__ */ new Date()
1795
- };
1796
- this.repository.addOrUpdateMessage(message.parentId, userMessage);
1797
- await this.startRun(userMessageId);
1798
- }
1799
- async startRun(parentId) {
1800
- const id = generateId();
1801
- this.repository.resetHead(parentId);
1802
- const messages = this.repository.getMessages();
1803
- const message = {
1804
- id,
1805
- role: "assistant",
1806
- status: "in_progress",
1807
- content: [{ type: "text", text: "" }],
1808
- createdAt: /* @__PURE__ */ new Date()
1809
- };
1810
- this.repository.addOrUpdateMessage(parentId, { ...message });
1811
- this.abortController?.abort();
1812
- this.abortController = new AbortController();
1813
- this.notifySubscribers();
1814
- try {
1815
- const updateHandler = ({ content }) => {
1816
- message.content = content;
1817
- this.repository.addOrUpdateMessage(parentId, { ...message });
1818
- this.notifySubscribers();
1819
- };
1820
- const result = await this.adapter.run({
1821
- messages,
1822
- abortSignal: this.abortController.signal,
1823
- onUpdate: updateHandler
1824
- });
1825
- updateHandler(result);
1826
- message.status = "done";
1827
- this.repository.addOrUpdateMessage(parentId, { ...message });
1828
- } catch (e) {
1829
- message.status = "error";
1830
- this.repository.addOrUpdateMessage(parentId, { ...message });
1831
- console.error(e);
1832
- } finally {
1833
- this.abortController = null;
1834
- this.notifySubscribers();
1835
- }
1836
- }
1837
- cancelRun() {
1838
- if (!this.abortController) return;
1839
- this.abortController.abort();
1840
- this.abortController = null;
1841
- this.notifySubscribers();
1842
- }
1843
- notifySubscribers() {
1844
- for (const callback of this._subscriptions) callback();
1845
- }
1846
- subscribe(callback) {
1847
- this._subscriptions.add(callback);
1848
- return () => this._subscriptions.delete(callback);
1849
- }
1850
- };
1851
-
1852
- // src/runtime/local/useLocalRuntime.tsx
1853
- var useLocalRuntime = (adapter) => {
1854
- const [runtime] = (0, import_react39.useState)(() => new LocalRuntime(adapter));
1855
- (0, import_react39.useInsertionEffect)(() => {
1856
- runtime.adapter = adapter;
1857
- });
1858
- return runtime;
1216
+ var AssistantRuntimeProviderImpl = ({ children, runtime }) => {
1217
+ return /* @__PURE__ */ (0, import_jsx_runtime23.jsx)(AssistantProvider, { runtime, children });
1859
1218
  };
1219
+ var AssistantRuntimeProvider = (0, import_react32.memo)(AssistantRuntimeProviderImpl);
1860
1220
  // Annotate the CommonJS export names for ESM import in node:
1861
1221
  0 && (module.exports = {
1862
1222
  ActionBarPrimitive,
@@ -1866,23 +1226,10 @@ var useLocalRuntime = (adapter) => {
1866
1226
  ContentPartPrimitive,
1867
1227
  MessagePrimitive,
1868
1228
  ThreadPrimitive,
1869
- VercelAIAssistantProvider,
1870
- VercelRSCAssistantProvider,
1871
- getVercelAIMessage,
1872
- getVercelRSCMessage,
1873
- unstable_MessageRepository,
1874
- unstable_useComposerContext,
1875
- unstable_useContentPartContext,
1876
- unstable_useLocalRuntime,
1877
- unstable_useMessageContext,
1878
- unstable_useThreadContext,
1879
1229
  useBeginMessageEdit,
1880
1230
  useCopyMessage,
1881
1231
  useGoToNextBranch,
1882
1232
  useGoToPreviousBranch,
1883
- useReloadMessage,
1884
- useVercelRSCRuntime,
1885
- useVercelUseAssistantRuntime,
1886
- useVercelUseChatRuntime
1233
+ useReloadMessage
1887
1234
  });
1888
1235
  //# sourceMappingURL=index.js.map