@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.mjs CHANGED
@@ -1,19 +1,14 @@
1
- import {
2
- ProxyConfigProvider
3
- } from "./chunk-DKAWDNW5.mjs";
4
1
  import {
5
2
  AssistantContext,
6
3
  ContentPartContext,
7
4
  MessageContext,
8
5
  ThreadContext,
6
+ __export,
9
7
  useComposerContext,
10
8
  useContentPartContext,
11
9
  useMessageContext,
12
10
  useThreadContext
13
- } from "./chunk-CY4TTHR7.mjs";
14
- import {
15
- __export
16
- } from "./chunk-X4HBDEFP.mjs";
11
+ } from "./chunk-SCWPIDP6.mjs";
17
12
 
18
13
  // src/actions/useCopyMessage.tsx
19
14
  import { useCallback } from "react";
@@ -973,14 +968,312 @@ __export(contentPart_exports, {
973
968
  InProgressIndicator: () => ContentPartInProgressIndicator
974
969
  });
975
970
 
971
+ // src/runtime/local/useLocalRuntime.tsx
972
+ import { useInsertionEffect, useState as useState3 } from "react";
973
+
974
+ // src/utils/ModelConfigTypes.ts
975
+ var mergeModelConfigs = (configs) => {
976
+ return configs.reduce((acc, config) => {
977
+ if (config.system) {
978
+ if (acc.system) {
979
+ acc.system += `
980
+
981
+ ${config.system}`;
982
+ } else {
983
+ acc.system = config.system;
984
+ }
985
+ }
986
+ if (config.tools) {
987
+ acc.tools = { ...acc.tools, ...config.tools };
988
+ }
989
+ return acc;
990
+ }, {});
991
+ };
992
+
993
+ // src/runtime/utils/idUtils.tsx
994
+ import { customAlphabet } from "nanoid/non-secure";
995
+ var generateId = customAlphabet(
996
+ "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
997
+ 7
998
+ );
999
+ var optimisticPrefix = "__optimistic__";
1000
+ var generateOptimisticId = () => `${optimisticPrefix}${generateId()}`;
1001
+
1002
+ // src/runtime/utils/MessageRepository.tsx
1003
+ var findHead = (message) => {
1004
+ if (message.next) return findHead(message.next);
1005
+ return message;
1006
+ };
1007
+ var MessageRepository = class {
1008
+ messages = /* @__PURE__ */ new Map();
1009
+ // message_id -> item
1010
+ head = null;
1011
+ root = {
1012
+ children: []
1013
+ };
1014
+ performOp(newParent, child, operation) {
1015
+ const parentOrRoot = child.prev ?? this.root;
1016
+ const newParentOrRoot = newParent ?? this.root;
1017
+ if (operation === "relink" && parentOrRoot === newParentOrRoot) return;
1018
+ if (operation !== "link") {
1019
+ parentOrRoot.children = parentOrRoot.children.filter(
1020
+ (m) => m !== child.current.id
1021
+ );
1022
+ if (child.prev?.next === child) {
1023
+ const fallbackId = child.prev.children.at(-1);
1024
+ const fallback = fallbackId ? this.messages.get(fallbackId) : null;
1025
+ if (fallback === void 0) {
1026
+ throw new Error(
1027
+ "MessageRepository(performOp/cut): Fallback sibling message not found. This is likely an internal bug in assistant-ui."
1028
+ );
1029
+ }
1030
+ child.prev.next = fallback;
1031
+ }
1032
+ }
1033
+ if (operation !== "cut") {
1034
+ newParentOrRoot.children = [
1035
+ ...newParentOrRoot.children,
1036
+ child.current.id
1037
+ ];
1038
+ if (newParent && (findHead(child) === this.head || newParent.next === null)) {
1039
+ newParent.next = child;
1040
+ }
1041
+ child.prev = newParent;
1042
+ }
1043
+ }
1044
+ getMessages() {
1045
+ const messages = new Array(this.head?.level ?? 0);
1046
+ for (let current = this.head; current; current = current.prev) {
1047
+ messages[current.level] = current.current;
1048
+ }
1049
+ return messages;
1050
+ }
1051
+ addOrUpdateMessage(parentId, message) {
1052
+ const existingItem = this.messages.get(message.id);
1053
+ const prev = parentId ? this.messages.get(parentId) : null;
1054
+ if (prev === void 0)
1055
+ throw new Error(
1056
+ "MessageRepository(addOrUpdateMessage): Parent message not found. This is likely an internal bug in assistant-ui."
1057
+ );
1058
+ if (existingItem) {
1059
+ existingItem.current = message;
1060
+ this.performOp(prev, existingItem, "relink");
1061
+ return;
1062
+ }
1063
+ const newItem = {
1064
+ prev,
1065
+ current: message,
1066
+ next: null,
1067
+ children: [],
1068
+ level: prev ? prev.level + 1 : 0
1069
+ };
1070
+ this.messages.set(message.id, newItem);
1071
+ this.performOp(prev, newItem, "link");
1072
+ if (this.head === prev) {
1073
+ this.head = newItem;
1074
+ }
1075
+ }
1076
+ appendOptimisticMessage(parentId, message) {
1077
+ let optimisticId;
1078
+ do {
1079
+ optimisticId = generateOptimisticId();
1080
+ } while (this.messages.has(optimisticId));
1081
+ this.addOrUpdateMessage(parentId, {
1082
+ ...message,
1083
+ id: optimisticId,
1084
+ createdAt: /* @__PURE__ */ new Date(),
1085
+ ...message.role === "assistant" ? { status: "in_progress" } : void 0
1086
+ });
1087
+ return optimisticId;
1088
+ }
1089
+ deleteMessage(messageId, replacementId) {
1090
+ const message = this.messages.get(messageId);
1091
+ if (!message)
1092
+ throw new Error(
1093
+ "MessageRepository(deleteMessage): Optimistic message not found. This is likely an internal bug in assistant-ui."
1094
+ );
1095
+ const replacement = replacementId === void 0 ? message.prev : replacementId === null ? null : this.messages.get(replacementId);
1096
+ if (replacement === void 0)
1097
+ throw new Error(
1098
+ "MessageRepository(deleteMessage): Replacement not found. This is likely an internal bug in assistant-ui."
1099
+ );
1100
+ for (const child of message.children) {
1101
+ const childMessage = this.messages.get(child);
1102
+ if (!childMessage)
1103
+ throw new Error(
1104
+ "MessageRepository(deleteMessage): Child message not found. This is likely an internal bug in assistant-ui."
1105
+ );
1106
+ this.performOp(replacement, childMessage, "relink");
1107
+ }
1108
+ this.performOp(null, message, "cut");
1109
+ this.messages.delete(messageId);
1110
+ if (this.head === message) {
1111
+ this.head = replacement ? findHead(replacement) : null;
1112
+ }
1113
+ }
1114
+ getBranches(messageId) {
1115
+ const message = this.messages.get(messageId);
1116
+ if (!message)
1117
+ throw new Error(
1118
+ "MessageRepository(getBranches): Message not found. This is likely an internal bug in assistant-ui."
1119
+ );
1120
+ const { children } = message.prev ?? this.root;
1121
+ return children;
1122
+ }
1123
+ switchToBranch(messageId) {
1124
+ const message = this.messages.get(messageId);
1125
+ if (!message)
1126
+ throw new Error(
1127
+ "MessageRepository(switchToBranch): Branch not found. This is likely an internal bug in assistant-ui."
1128
+ );
1129
+ if (message.prev) {
1130
+ message.prev.next = message;
1131
+ }
1132
+ this.head = findHead(message);
1133
+ }
1134
+ resetHead(messageId) {
1135
+ if (messageId === null) {
1136
+ this.head = null;
1137
+ return;
1138
+ }
1139
+ const message = this.messages.get(messageId);
1140
+ if (!message)
1141
+ throw new Error(
1142
+ "MessageRepository(resetHead): Branch not found. This is likely an internal bug in assistant-ui."
1143
+ );
1144
+ this.head = message;
1145
+ for (let current = message; current; current = current.prev) {
1146
+ if (current.prev) {
1147
+ current.prev.next = current;
1148
+ }
1149
+ }
1150
+ }
1151
+ };
1152
+
1153
+ // src/runtime/local/LocalRuntime.tsx
1154
+ var LocalRuntime = class {
1155
+ constructor(adapter) {
1156
+ this.adapter = adapter;
1157
+ }
1158
+ _subscriptions = /* @__PURE__ */ new Set();
1159
+ _configProviders = /* @__PURE__ */ new Set();
1160
+ abortController = null;
1161
+ repository = new MessageRepository();
1162
+ get messages() {
1163
+ return this.repository.getMessages();
1164
+ }
1165
+ get isRunning() {
1166
+ return this.abortController != null;
1167
+ }
1168
+ getBranches(messageId) {
1169
+ return this.repository.getBranches(messageId);
1170
+ }
1171
+ switchToBranch(branchId) {
1172
+ this.repository.switchToBranch(branchId);
1173
+ this.notifySubscribers();
1174
+ }
1175
+ async append(message) {
1176
+ const userMessageId = generateId();
1177
+ const userMessage = {
1178
+ id: userMessageId,
1179
+ role: "user",
1180
+ content: message.content,
1181
+ createdAt: /* @__PURE__ */ new Date()
1182
+ };
1183
+ this.repository.addOrUpdateMessage(message.parentId, userMessage);
1184
+ await this.startRun(userMessageId);
1185
+ }
1186
+ async startRun(parentId) {
1187
+ const id = generateId();
1188
+ this.repository.resetHead(parentId);
1189
+ const messages = this.repository.getMessages();
1190
+ const message = {
1191
+ id,
1192
+ role: "assistant",
1193
+ status: "in_progress",
1194
+ content: [{ type: "text", text: "" }],
1195
+ createdAt: /* @__PURE__ */ new Date()
1196
+ };
1197
+ this.repository.addOrUpdateMessage(parentId, { ...message });
1198
+ this.abortController?.abort();
1199
+ this.abortController = new AbortController();
1200
+ this.notifySubscribers();
1201
+ try {
1202
+ const updateHandler = ({ content }) => {
1203
+ message.content = content;
1204
+ this.repository.addOrUpdateMessage(parentId, { ...message });
1205
+ this.notifySubscribers();
1206
+ };
1207
+ const result = await this.adapter.run({
1208
+ messages,
1209
+ abortSignal: this.abortController.signal,
1210
+ config: mergeModelConfigs([...this._configProviders].map((p) => p())),
1211
+ onUpdate: updateHandler
1212
+ });
1213
+ updateHandler(result);
1214
+ message.status = "done";
1215
+ this.repository.addOrUpdateMessage(parentId, { ...message });
1216
+ } catch (e) {
1217
+ message.status = "error";
1218
+ this.repository.addOrUpdateMessage(parentId, { ...message });
1219
+ console.error(e);
1220
+ } finally {
1221
+ this.abortController = null;
1222
+ this.notifySubscribers();
1223
+ }
1224
+ }
1225
+ cancelRun() {
1226
+ if (!this.abortController) return;
1227
+ this.abortController.abort();
1228
+ this.abortController = null;
1229
+ this.notifySubscribers();
1230
+ }
1231
+ notifySubscribers() {
1232
+ for (const callback of this._subscriptions) callback();
1233
+ }
1234
+ subscribe(callback) {
1235
+ this._subscriptions.add(callback);
1236
+ return () => this._subscriptions.delete(callback);
1237
+ }
1238
+ registerModelConfigProvider(provider) {
1239
+ this._configProviders.add(provider);
1240
+ return () => this._configProviders.delete(provider);
1241
+ }
1242
+ };
1243
+
1244
+ // src/runtime/local/useLocalRuntime.tsx
1245
+ var useLocalRuntime = (adapter) => {
1246
+ const [runtime] = useState3(() => new LocalRuntime(adapter));
1247
+ useInsertionEffect(() => {
1248
+ runtime.adapter = adapter;
1249
+ });
1250
+ return runtime;
1251
+ };
1252
+
976
1253
  // src/context/providers/AssistantRuntimeProvider.tsx
977
1254
  import { memo } from "react";
978
1255
 
979
1256
  // src/context/providers/AssistantProvider.tsx
980
- import { useEffect as useEffect7, useInsertionEffect as useInsertionEffect2, useRef as useRef5, useState as useState4 } from "react";
1257
+ import { useEffect as useEffect7, useInsertionEffect as useInsertionEffect3, useRef as useRef5, useState as useState5 } from "react";
981
1258
 
982
1259
  // src/context/stores/AssistantModelConfig.ts
983
1260
  import { create as create4 } from "zustand";
1261
+
1262
+ // src/utils/ProxyConfigProvider.ts
1263
+ var ProxyConfigProvider = class {
1264
+ _providers = /* @__PURE__ */ new Set();
1265
+ getModelConfig() {
1266
+ return mergeModelConfigs([...this._providers].map((p) => p()));
1267
+ }
1268
+ registerModelConfigProvider(provider) {
1269
+ this._providers.add(provider);
1270
+ return () => {
1271
+ this._providers.delete(provider);
1272
+ };
1273
+ }
1274
+ };
1275
+
1276
+ // src/context/stores/AssistantModelConfig.ts
984
1277
  var makeAssistantModelConfigStore = () => create4(() => {
985
1278
  const proxy = new ProxyConfigProvider();
986
1279
  return {
@@ -994,7 +1287,7 @@ var makeAssistantModelConfigStore = () => create4(() => {
994
1287
  });
995
1288
 
996
1289
  // src/context/providers/ThreadProvider.tsx
997
- import { useEffect as useEffect6, useInsertionEffect, useRef as useRef4, useState as useState3 } from "react";
1290
+ import { useEffect as useEffect6, useInsertionEffect as useInsertionEffect2, useRef as useRef4, useState as useState4 } from "react";
998
1291
 
999
1292
  // src/context/stores/Composer.ts
1000
1293
  import { create as create5 } from "zustand";
@@ -1070,10 +1363,10 @@ var ThreadProvider = ({
1070
1363
  runtime
1071
1364
  }) => {
1072
1365
  const runtimeRef = useRef4(runtime);
1073
- useInsertionEffect(() => {
1366
+ useInsertionEffect2(() => {
1074
1367
  runtimeRef.current = runtime;
1075
1368
  });
1076
- const [{ context, onRuntimeUpdate }] = useState3(() => {
1369
+ const [{ context, onRuntimeUpdate }] = useState4(() => {
1077
1370
  const { useThread, onRuntimeUpdate: onRuntimeUpdate2 } = makeThreadStore(runtimeRef);
1078
1371
  const useViewport = makeThreadViewportStore();
1079
1372
  const useComposer = makeComposerStore(useThread);
@@ -1101,10 +1394,10 @@ var ThreadProvider = ({
1101
1394
  import { jsx as jsx22 } from "react/jsx-runtime";
1102
1395
  var AssistantProvider = ({ children, runtime }) => {
1103
1396
  const runtimeRef = useRef5(runtime);
1104
- useInsertionEffect2(() => {
1397
+ useInsertionEffect3(() => {
1105
1398
  runtimeRef.current = runtime;
1106
1399
  });
1107
- const [context] = useState4(() => {
1400
+ const [context] = useState5(() => {
1108
1401
  const useModelConfig = makeAssistantModelConfigStore();
1109
1402
  return { useModelConfig };
1110
1403
  });
@@ -1121,18 +1414,27 @@ var AssistantRuntimeProviderImpl = ({ children, runtime }) => {
1121
1414
  return /* @__PURE__ */ jsx23(AssistantProvider, { runtime, children });
1122
1415
  };
1123
1416
  var AssistantRuntimeProvider = memo(AssistantRuntimeProviderImpl);
1417
+
1418
+ // src/internal.ts
1419
+ var internal_exports = {};
1420
+ __export(internal_exports, {
1421
+ MessageRepository: () => MessageRepository,
1422
+ ProxyConfigProvider: () => ProxyConfigProvider
1423
+ });
1124
1424
  export {
1125
1425
  actionBar_exports as ActionBarPrimitive,
1126
1426
  AssistantRuntimeProvider,
1127
1427
  branchPicker_exports as BranchPickerPrimitive,
1128
1428
  composer_exports as ComposerPrimitive,
1129
1429
  contentPart_exports as ContentPartPrimitive,
1430
+ internal_exports as INTERNAL,
1130
1431
  message_exports as MessagePrimitive,
1131
1432
  thread_exports as ThreadPrimitive,
1132
1433
  useBeginMessageEdit,
1133
1434
  useCopyMessage,
1134
1435
  useGoToNextBranch,
1135
1436
  useGoToPreviousBranch,
1437
+ useLocalRuntime,
1136
1438
  useReloadMessage
1137
1439
  };
1138
1440
  //# sourceMappingURL=index.mjs.map