@assistant-ui/react 0.0.29 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/AssistantRuntime-BM_jVV3g.d.mts +23 -0
- package/dist/AssistantRuntime-CBMSAJqH.d.mts +23 -0
- package/dist/AssistantRuntime-C_BvM7ZT.d.ts +23 -0
- package/dist/AssistantRuntime-DELpXWfG.d.ts +23 -0
- package/dist/ModelConfigTypes-B9UY4zxv.d.mts +55 -0
- package/dist/ModelConfigTypes-B9UY4zxv.d.ts +55 -0
- package/dist/ModelConfigTypes-BF5HxVrH.d.mts +55 -0
- package/dist/ModelConfigTypes-BF5HxVrH.d.ts +55 -0
- package/dist/ModelConfigTypes-Cf3yjaDu.d.mts +55 -0
- package/dist/ModelConfigTypes-Cf3yjaDu.d.ts +55 -0
- package/dist/ModelConfigTypes-CzmXY3sn.d.mts +55 -0
- package/dist/ModelConfigTypes-CzmXY3sn.d.ts +55 -0
- package/dist/Thread-BMASJT4a.d.ts +15 -0
- package/dist/Thread-UEVsUmvl.d.mts +15 -0
- package/dist/chunk-3XZUKECF.mjs +207 -0
- package/dist/chunk-3XZUKECF.mjs.map +1 -0
- package/dist/chunk-4DQ2CIAD.mjs +69 -0
- package/dist/chunk-4DQ2CIAD.mjs.map +1 -0
- package/dist/chunk-5YONSDN4.mjs +200 -0
- package/dist/chunk-5YONSDN4.mjs.map +1 -0
- package/dist/chunk-CY4TTHR7.mjs +76 -0
- package/dist/chunk-CY4TTHR7.mjs.map +1 -0
- package/dist/chunk-DKAWDNW5.mjs +22 -0
- package/dist/chunk-DKAWDNW5.mjs.map +1 -0
- package/dist/chunk-GQKH2ADD.mjs +165 -0
- package/dist/chunk-GQKH2ADD.mjs.map +1 -0
- package/dist/chunk-J5LGTIGS.mjs +10 -0
- package/dist/chunk-J5LGTIGS.mjs.map +1 -0
- package/dist/chunk-NSBOH42A.mjs +200 -0
- package/dist/chunk-NSBOH42A.mjs.map +1 -0
- package/dist/chunk-X4HBDEFP.mjs +30 -0
- package/dist/chunk-X4HBDEFP.mjs.map +1 -0
- package/dist/experimental.d.mts +86 -0
- package/dist/experimental.d.ts +86 -0
- package/dist/experimental.js +123 -0
- package/dist/experimental.js.map +1 -0
- package/dist/experimental.mjs +34 -0
- package/dist/experimental.mjs.map +1 -0
- package/dist/index.d.mts +11 -230
- package/dist/index.d.ts +11 -230
- package/dist/index.js +189 -577
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +140 -757
- package/dist/index.mjs.map +1 -1
- package/dist/internal-dlLjX30u.d.mts +75 -0
- package/dist/internal-dlLjX30u.d.ts +75 -0
- package/dist/internal.d.mts +25 -0
- package/dist/internal.d.ts +25 -0
- package/dist/internal.js +225 -0
- package/dist/internal.js.map +1 -0
- package/dist/internal.mjs +10 -0
- package/dist/internal.mjs.map +1 -0
- package/package.json +23 -3
package/dist/index.js
CHANGED
@@ -37,24 +37,12 @@ __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
|
-
|
55
|
-
|
56
|
-
useVercelUseAssistantRuntime: () => useVercelUseAssistantRuntime,
|
57
|
-
useVercelUseChatRuntime: () => useVercelUseChatRuntime
|
44
|
+
useLocalRuntime: () => useLocalRuntime,
|
45
|
+
useReloadMessage: () => useReloadMessage
|
58
46
|
});
|
59
47
|
module.exports = __toCommonJS(src_exports);
|
60
48
|
|
@@ -137,7 +125,7 @@ var ThreadContext = (0, import_react5.createContext)(null);
|
|
137
125
|
var useThreadContext = () => {
|
138
126
|
const context = (0, import_react5.useContext)(ThreadContext);
|
139
127
|
if (!context)
|
140
|
-
throw new Error("This component must be used within an
|
128
|
+
throw new Error("This component must be used within an AssistantRuntimeProvider.");
|
141
129
|
return context;
|
142
130
|
};
|
143
131
|
|
@@ -1041,414 +1029,26 @@ __export(contentPart_exports, {
|
|
1041
1029
|
InProgressIndicator: () => ContentPartInProgressIndicator
|
1042
1030
|
});
|
1043
1031
|
|
1044
|
-
//
|
1045
|
-
var import_react30 = require("react");
|
1046
|
-
|
1047
|
-
// ../react-ai-sdk/src/rsc/VercelRSCRuntime.tsx
|
1048
|
-
var import_zustand4 = require("zustand");
|
1049
|
-
|
1050
|
-
// ../react-ai-sdk/src/rsc/useVercelRSCSync.tsx
|
1032
|
+
// src/runtime/local/useLocalRuntime.tsx
|
1051
1033
|
var import_react29 = require("react");
|
1052
1034
|
|
1053
|
-
//
|
1054
|
-
var
|
1055
|
-
|
1056
|
-
|
1057
|
-
|
1058
|
-
|
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
|
-
};
|
1208
|
-
|
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
|
-
};
|
1035
|
+
// src/utils/ModelConfigTypes.ts
|
1036
|
+
var mergeModelConfigs = (configs) => {
|
1037
|
+
return configs.reduce((acc, config) => {
|
1038
|
+
if (config.system) {
|
1039
|
+
if (acc.system) {
|
1040
|
+
acc.system += `
|
1220
1041
|
|
1221
|
-
|
1222
|
-
|
1223
|
-
|
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
|
-
);
|
1241
|
-
}
|
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 = [];
|
1042
|
+
${config.system}`;
|
1043
|
+
} else {
|
1044
|
+
acc.system = config.system;
|
1281
1045
|
}
|
1282
|
-
chunks.push([message]);
|
1283
1046
|
}
|
1284
|
-
|
1285
|
-
|
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
|
-
);
|
1047
|
+
if (config.tools) {
|
1048
|
+
acc.tools = { ...acc.tools, ...config.tools };
|
1413
1049
|
}
|
1414
|
-
|
1415
|
-
|
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
|
-
};
|
1427
|
-
};
|
1428
|
-
|
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;
|
1050
|
+
return acc;
|
1051
|
+
}, {});
|
1452
1052
|
};
|
1453
1053
|
|
1454
1054
|
// src/runtime/utils/idUtils.tsx
|
@@ -1611,15 +1211,154 @@ var MessageRepository = class {
|
|
1611
1211
|
}
|
1612
1212
|
};
|
1613
1213
|
|
1214
|
+
// src/runtime/local/LocalRuntime.tsx
|
1215
|
+
var LocalRuntime = class {
|
1216
|
+
constructor(adapter) {
|
1217
|
+
this.adapter = adapter;
|
1218
|
+
}
|
1219
|
+
_subscriptions = /* @__PURE__ */ new Set();
|
1220
|
+
_configProviders = /* @__PURE__ */ new Set();
|
1221
|
+
abortController = null;
|
1222
|
+
repository = new MessageRepository();
|
1223
|
+
get messages() {
|
1224
|
+
return this.repository.getMessages();
|
1225
|
+
}
|
1226
|
+
get isRunning() {
|
1227
|
+
return this.abortController != null;
|
1228
|
+
}
|
1229
|
+
getBranches(messageId) {
|
1230
|
+
return this.repository.getBranches(messageId);
|
1231
|
+
}
|
1232
|
+
switchToBranch(branchId) {
|
1233
|
+
this.repository.switchToBranch(branchId);
|
1234
|
+
this.notifySubscribers();
|
1235
|
+
}
|
1236
|
+
async append(message) {
|
1237
|
+
const userMessageId = generateId();
|
1238
|
+
const userMessage = {
|
1239
|
+
id: userMessageId,
|
1240
|
+
role: "user",
|
1241
|
+
content: message.content,
|
1242
|
+
createdAt: /* @__PURE__ */ new Date()
|
1243
|
+
};
|
1244
|
+
this.repository.addOrUpdateMessage(message.parentId, userMessage);
|
1245
|
+
await this.startRun(userMessageId);
|
1246
|
+
}
|
1247
|
+
async startRun(parentId) {
|
1248
|
+
const id = generateId();
|
1249
|
+
this.repository.resetHead(parentId);
|
1250
|
+
const messages = this.repository.getMessages();
|
1251
|
+
const message = {
|
1252
|
+
id,
|
1253
|
+
role: "assistant",
|
1254
|
+
status: "in_progress",
|
1255
|
+
content: [{ type: "text", text: "" }],
|
1256
|
+
createdAt: /* @__PURE__ */ new Date()
|
1257
|
+
};
|
1258
|
+
this.repository.addOrUpdateMessage(parentId, { ...message });
|
1259
|
+
this.abortController?.abort();
|
1260
|
+
this.abortController = new AbortController();
|
1261
|
+
this.notifySubscribers();
|
1262
|
+
try {
|
1263
|
+
const updateHandler = ({ content }) => {
|
1264
|
+
message.content = content;
|
1265
|
+
this.repository.addOrUpdateMessage(parentId, { ...message });
|
1266
|
+
this.notifySubscribers();
|
1267
|
+
};
|
1268
|
+
const result = await this.adapter.run({
|
1269
|
+
messages,
|
1270
|
+
abortSignal: this.abortController.signal,
|
1271
|
+
config: mergeModelConfigs([...this._configProviders].map((p) => p())),
|
1272
|
+
onUpdate: updateHandler
|
1273
|
+
});
|
1274
|
+
updateHandler(result);
|
1275
|
+
message.status = "done";
|
1276
|
+
this.repository.addOrUpdateMessage(parentId, { ...message });
|
1277
|
+
} catch (e) {
|
1278
|
+
message.status = "error";
|
1279
|
+
this.repository.addOrUpdateMessage(parentId, { ...message });
|
1280
|
+
console.error(e);
|
1281
|
+
} finally {
|
1282
|
+
this.abortController = null;
|
1283
|
+
this.notifySubscribers();
|
1284
|
+
}
|
1285
|
+
}
|
1286
|
+
cancelRun() {
|
1287
|
+
if (!this.abortController) return;
|
1288
|
+
this.abortController.abort();
|
1289
|
+
this.abortController = null;
|
1290
|
+
this.notifySubscribers();
|
1291
|
+
}
|
1292
|
+
notifySubscribers() {
|
1293
|
+
for (const callback of this._subscriptions) callback();
|
1294
|
+
}
|
1295
|
+
subscribe(callback) {
|
1296
|
+
this._subscriptions.add(callback);
|
1297
|
+
return () => this._subscriptions.delete(callback);
|
1298
|
+
}
|
1299
|
+
registerModelConfigProvider(provider) {
|
1300
|
+
this._configProviders.add(provider);
|
1301
|
+
return () => this._configProviders.delete(provider);
|
1302
|
+
}
|
1303
|
+
};
|
1304
|
+
|
1305
|
+
// src/runtime/local/useLocalRuntime.tsx
|
1306
|
+
var useLocalRuntime = (adapter) => {
|
1307
|
+
const [runtime] = (0, import_react29.useState)(() => new LocalRuntime(adapter));
|
1308
|
+
(0, import_react29.useInsertionEffect)(() => {
|
1309
|
+
runtime.adapter = adapter;
|
1310
|
+
});
|
1311
|
+
return runtime;
|
1312
|
+
};
|
1313
|
+
|
1614
1314
|
// src/context/providers/AssistantRuntimeProvider.tsx
|
1615
|
-
var
|
1315
|
+
var import_react33 = require("react");
|
1316
|
+
|
1317
|
+
// src/context/providers/AssistantProvider.tsx
|
1318
|
+
var import_react32 = require("react");
|
1319
|
+
|
1320
|
+
// src/context/AssistantContext.ts
|
1321
|
+
var import_react30 = require("react");
|
1322
|
+
var AssistantContext = (0, import_react30.createContext)(
|
1323
|
+
null
|
1324
|
+
);
|
1325
|
+
|
1326
|
+
// src/context/stores/AssistantModelConfig.ts
|
1327
|
+
var import_zustand4 = require("zustand");
|
1328
|
+
|
1329
|
+
// src/utils/ProxyConfigProvider.ts
|
1330
|
+
var ProxyConfigProvider = class {
|
1331
|
+
_providers = /* @__PURE__ */ new Set();
|
1332
|
+
getModelConfig() {
|
1333
|
+
return mergeModelConfigs([...this._providers].map((p) => p()));
|
1334
|
+
}
|
1335
|
+
registerModelConfigProvider(provider) {
|
1336
|
+
this._providers.add(provider);
|
1337
|
+
return () => {
|
1338
|
+
this._providers.delete(provider);
|
1339
|
+
};
|
1340
|
+
}
|
1341
|
+
};
|
1342
|
+
|
1343
|
+
// src/context/stores/AssistantModelConfig.ts
|
1344
|
+
var makeAssistantModelConfigStore = () => (0, import_zustand4.create)(() => {
|
1345
|
+
const proxy = new ProxyConfigProvider();
|
1346
|
+
return {
|
1347
|
+
getModelConfig: () => {
|
1348
|
+
return proxy.getModelConfig();
|
1349
|
+
},
|
1350
|
+
registerModelConfigProvider: (provider) => {
|
1351
|
+
return proxy.registerModelConfigProvider(provider);
|
1352
|
+
}
|
1353
|
+
};
|
1354
|
+
});
|
1616
1355
|
|
1617
1356
|
// src/context/providers/ThreadProvider.tsx
|
1618
|
-
var
|
1357
|
+
var import_react31 = require("react");
|
1619
1358
|
|
1620
1359
|
// src/context/stores/Composer.ts
|
1621
|
-
var
|
1622
|
-
var makeComposerStore = (useThread) => (0,
|
1360
|
+
var import_zustand5 = require("zustand");
|
1361
|
+
var makeComposerStore = (useThread) => (0, import_zustand5.create)()((set, get, store) => {
|
1623
1362
|
return {
|
1624
1363
|
...makeBaseComposer(set, get, store),
|
1625
1364
|
isEditing: true,
|
@@ -1641,9 +1380,9 @@ var makeComposerStore = (useThread) => (0, import_zustand6.create)()((set, get,
|
|
1641
1380
|
});
|
1642
1381
|
|
1643
1382
|
// src/context/stores/Thread.ts
|
1644
|
-
var
|
1383
|
+
var import_zustand6 = require("zustand");
|
1645
1384
|
var makeThreadStore = (runtimeRef) => {
|
1646
|
-
const useThread = (0,
|
1385
|
+
const useThread = (0, import_zustand6.create)(() => ({
|
1647
1386
|
messages: runtimeRef.current.messages,
|
1648
1387
|
isRunning: runtimeRef.current.isRunning,
|
1649
1388
|
getBranches: (messageId) => runtimeRef.current.getBranches(messageId),
|
@@ -1665,10 +1404,10 @@ var makeThreadStore = (runtimeRef) => {
|
|
1665
1404
|
};
|
1666
1405
|
|
1667
1406
|
// src/context/stores/ThreadViewport.tsx
|
1668
|
-
var
|
1407
|
+
var import_zustand7 = require("zustand");
|
1669
1408
|
var makeThreadViewportStore = () => {
|
1670
1409
|
const scrollToBottomListeners = /* @__PURE__ */ new Set();
|
1671
|
-
return (0,
|
1410
|
+
return (0, import_zustand7.create)(() => ({
|
1672
1411
|
isAtBottom: true,
|
1673
1412
|
scrollToBottom: () => {
|
1674
1413
|
for (const listener of scrollToBottomListeners) {
|
@@ -1690,11 +1429,11 @@ var ThreadProvider = ({
|
|
1690
1429
|
children,
|
1691
1430
|
runtime
|
1692
1431
|
}) => {
|
1693
|
-
const runtimeRef = (0,
|
1694
|
-
(0,
|
1432
|
+
const runtimeRef = (0, import_react31.useRef)(runtime);
|
1433
|
+
(0, import_react31.useInsertionEffect)(() => {
|
1695
1434
|
runtimeRef.current = runtime;
|
1696
1435
|
});
|
1697
|
-
const [{ context, onRuntimeUpdate }] = (0,
|
1436
|
+
const [{ context, onRuntimeUpdate }] = (0, import_react31.useState)(() => {
|
1698
1437
|
const { useThread, onRuntimeUpdate: onRuntimeUpdate2 } = makeThreadStore(runtimeRef);
|
1699
1438
|
const useViewport = makeThreadViewportStore();
|
1700
1439
|
const useComposer = makeComposerStore(useThread);
|
@@ -1707,7 +1446,7 @@ var ThreadProvider = ({
|
|
1707
1446
|
onRuntimeUpdate: onRuntimeUpdate2
|
1708
1447
|
};
|
1709
1448
|
});
|
1710
|
-
(0,
|
1449
|
+
(0, import_react31.useEffect)(() => {
|
1711
1450
|
onRuntimeUpdate();
|
1712
1451
|
return runtime.subscribe(onRuntimeUpdate);
|
1713
1452
|
}, [onRuntimeUpdate, runtime]);
|
@@ -1718,145 +1457,30 @@ var ThreadProvider = ({
|
|
1718
1457
|
] });
|
1719
1458
|
};
|
1720
1459
|
|
1721
|
-
// src/context/providers/
|
1460
|
+
// src/context/providers/AssistantProvider.tsx
|
1722
1461
|
var import_jsx_runtime22 = require("react/jsx-runtime");
|
1723
|
-
var
|
1724
|
-
|
1462
|
+
var AssistantProvider = ({ children, runtime }) => {
|
1463
|
+
const runtimeRef = (0, import_react32.useRef)(runtime);
|
1464
|
+
(0, import_react32.useInsertionEffect)(() => {
|
1465
|
+
runtimeRef.current = runtime;
|
1466
|
+
});
|
1467
|
+
const [context] = (0, import_react32.useState)(() => {
|
1468
|
+
const useModelConfig = makeAssistantModelConfigStore();
|
1469
|
+
return { useModelConfig };
|
1470
|
+
});
|
1471
|
+
const getModelCOnfig = context.useModelConfig((c) => c.getModelConfig);
|
1472
|
+
(0, import_react32.useEffect)(() => {
|
1473
|
+
return runtime.registerModelConfigProvider(getModelCOnfig);
|
1474
|
+
}, [runtime, getModelCOnfig]);
|
1475
|
+
return /* @__PURE__ */ (0, import_jsx_runtime22.jsx)(AssistantContext.Provider, { value: context, children: /* @__PURE__ */ (0, import_jsx_runtime22.jsx)(ThreadProvider, { runtime, children }) });
|
1725
1476
|
};
|
1726
|
-
var AssistantRuntimeProvider = (0, import_react38.memo)(AssistantRuntimeProviderImpl);
|
1727
1477
|
|
1728
|
-
// src/
|
1478
|
+
// src/context/providers/AssistantRuntimeProvider.tsx
|
1729
1479
|
var import_jsx_runtime23 = require("react/jsx-runtime");
|
1730
|
-
var
|
1731
|
-
|
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;
|
1480
|
+
var AssistantRuntimeProviderImpl = ({ children, runtime }) => {
|
1481
|
+
return /* @__PURE__ */ (0, import_jsx_runtime23.jsx)(AssistantProvider, { runtime, children });
|
1859
1482
|
};
|
1483
|
+
var AssistantRuntimeProvider = (0, import_react33.memo)(AssistantRuntimeProviderImpl);
|
1860
1484
|
// Annotate the CommonJS export names for ESM import in node:
|
1861
1485
|
0 && (module.exports = {
|
1862
1486
|
ActionBarPrimitive,
|
@@ -1866,23 +1490,11 @@ var useLocalRuntime = (adapter) => {
|
|
1866
1490
|
ContentPartPrimitive,
|
1867
1491
|
MessagePrimitive,
|
1868
1492
|
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
1493
|
useBeginMessageEdit,
|
1880
1494
|
useCopyMessage,
|
1881
1495
|
useGoToNextBranch,
|
1882
1496
|
useGoToPreviousBranch,
|
1883
|
-
|
1884
|
-
|
1885
|
-
useVercelUseAssistantRuntime,
|
1886
|
-
useVercelUseChatRuntime
|
1497
|
+
useLocalRuntime,
|
1498
|
+
useReloadMessage
|
1887
1499
|
});
|
1888
1500
|
//# sourceMappingURL=index.js.map
|