@assistant-ui/react 0.4.3 → 0.4.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -43,6 +43,7 @@ __export(src_exports, {
43
43
  ComposerPrimitive: () => composer_exports,
44
44
  ContentPart: () => content_part_default,
45
45
  ContentPartPrimitive: () => contentPart_exports,
46
+ EdgeChatAdapter: () => EdgeChatAdapter,
46
47
  EditComposer: () => edit_composer_default,
47
48
  INTERNAL: () => internal_exports,
48
49
  MessagePrimitive: () => message_exports,
@@ -52,8 +53,11 @@ __export(src_exports, {
52
53
  ThreadWelcome: () => thread_welcome_default,
53
54
  UserActionBar: () => user_action_bar_default,
54
55
  UserMessage: () => user_message_default,
56
+ fromCoreMessages: () => fromCoreMessages,
57
+ fromLanguageModelMessages: () => fromLanguageModelMessages,
55
58
  makeAssistantTool: () => makeAssistantTool,
56
59
  makeAssistantToolUI: () => makeAssistantToolUI,
60
+ toLanguageModelMessages: () => toLanguageModelMessages,
57
61
  useActionBarCopy: () => useActionBarCopy,
58
62
  useActionBarEdit: () => useActionBarEdit,
59
63
  useActionBarReload: () => useActionBarReload,
@@ -113,7 +117,7 @@ var import_zustand = require("zustand");
113
117
 
114
118
  // src/types/ModelConfigTypes.ts
115
119
  var mergeModelConfigs = (configSet) => {
116
- const configs = Array.from(configSet).map((c) => c()).sort((a, b) => (b.priority ?? 0) - (a.priority ?? 0));
120
+ const configs = Array.from(configSet).map((c) => c.getModelConfig()).sort((a, b) => (b.priority ?? 0) - (a.priority ?? 0));
117
121
  return configs.reduce((acc, config) => {
118
122
  if (config.system) {
119
123
  if (acc.system) {
@@ -302,7 +306,8 @@ var makeThreadActionStore = (runtimeRef) => {
302
306
  startRun: (parentId) => runtimeRef.current.startRun(parentId),
303
307
  append: (message) => runtimeRef.current.append(message),
304
308
  cancelRun: () => runtimeRef.current.cancelRun(),
305
- addToolResult: (options) => runtimeRef.current.addToolResult(options)
309
+ addToolResult: (options) => runtimeRef.current.addToolResult(options),
310
+ getRuntime: () => runtimeRef.current
306
311
  })
307
312
  );
308
313
  };
@@ -319,10 +324,7 @@ var ThreadProvider = ({
319
324
  children,
320
325
  runtime
321
326
  }) => {
322
- const runtimeRef = (0, import_react3.useRef)(runtime);
323
- (0, import_react3.useInsertionEffect)(() => {
324
- runtimeRef.current = runtime;
325
- });
327
+ const runtimeRef = (0, import_react3.useRef)(runtime.thread);
326
328
  const [context] = (0, import_react3.useState)(() => {
327
329
  const useThread = makeThreadStore(runtimeRef);
328
330
  const useThreadMessages = makeThreadMessagesStore(runtimeRef);
@@ -337,8 +339,16 @@ var ThreadProvider = ({
337
339
  useViewport
338
340
  };
339
341
  });
342
+ const thread = (0, import_react3.useSyncExternalStore)(
343
+ (0, import_react3.useCallback)((c) => runtime.subscribe(c), [runtime]),
344
+ () => runtime.thread,
345
+ () => runtime.thread
346
+ );
347
+ (0, import_react3.useInsertionEffect)(() => {
348
+ runtimeRef.current = thread;
349
+ });
340
350
  (0, import_react3.useEffect)(() => {
341
- const onRuntimeUpdate = () => {
351
+ const onThreadUpdate = () => {
342
352
  context.useThread.setState(
343
353
  Object.freeze({
344
354
  isRunning: runtimeRef.current.isRunning
@@ -347,20 +357,11 @@ var ThreadProvider = ({
347
357
  );
348
358
  context.useThreadMessages.setState(Object.freeze(runtimeRef.current.messages), true);
349
359
  };
350
- onRuntimeUpdate();
351
- return runtime.subscribe(onRuntimeUpdate);
352
- }, [context, runtime]);
353
- const subscribe = (0, import_react3.useCallback)(
354
- (c) => runtime.subscribe(c),
355
- [runtime]
356
- );
357
- const RuntimeSynchronizer = (0, import_react3.useSyncExternalStore)(
358
- subscribe,
359
- () => runtime.unstable_synchronizer,
360
- () => void 0
361
- );
360
+ onThreadUpdate();
361
+ return thread.subscribe(onThreadUpdate);
362
+ }, [context, thread]);
362
363
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(ThreadContext.Provider, { value: context, children: [
363
- RuntimeSynchronizer && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(RuntimeSynchronizer, {}),
364
+ thread.unstable_synchronizer && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(thread.unstable_synchronizer, {}),
364
365
  children
365
366
  ] });
366
367
  };
@@ -369,7 +370,8 @@ var ThreadProvider = ({
369
370
  var import_zustand8 = require("zustand");
370
371
  var makeAssistantActionsStore = (runtimeRef) => (0, import_zustand8.create)(
371
372
  () => Object.freeze({
372
- switchToThread: () => runtimeRef.current.switchToThread(null)
373
+ switchToThread: () => runtimeRef.current.switchToThread(null),
374
+ getRuntime: () => runtimeRef.current
373
375
  })
374
376
  );
375
377
 
@@ -386,10 +388,10 @@ var AssistantProvider = ({ children, runtime }) => {
386
388
  const useAssistantActions = makeAssistantActionsStore(runtimeRef);
387
389
  return { useModelConfig, useToolUIs, useAssistantActions };
388
390
  });
389
- const getModelCOnfig = context.useModelConfig((c) => c.getModelConfig);
391
+ const getModelConfig = context.useModelConfig();
390
392
  (0, import_react4.useEffect)(() => {
391
- return runtime.registerModelConfigProvider(getModelCOnfig);
392
- }, [runtime, getModelCOnfig]);
393
+ return runtime.registerModelConfigProvider(getModelConfig);
394
+ }, [runtime, getModelConfig]);
393
395
  return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(AssistantContext.Provider, { value: context, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(ThreadProvider, { runtime, children }) });
394
396
  };
395
397
 
@@ -499,7 +501,9 @@ var useAssistantTool = (tool) => {
499
501
  [tool.toolName]: rest
500
502
  }
501
503
  };
502
- const unsub1 = registerModelConfigProvider(() => config);
504
+ const unsub1 = registerModelConfigProvider({
505
+ getModelConfig: () => config
506
+ });
503
507
  const unsub2 = render ? setToolUI(toolName, render) : void 0;
504
508
  return () => {
505
509
  unsub1();
@@ -549,7 +553,7 @@ var useAssistantInstructions = (instruction) => {
549
553
  const config = {
550
554
  system: instruction
551
555
  };
552
- return registerModelConfigProvider(() => config);
556
+ return registerModelConfigProvider({ getModelConfig: () => config });
553
557
  }, [registerModelConfigProvider, instruction]);
554
558
  };
555
559
 
@@ -1968,7 +1972,7 @@ var ThreadPrimitiveSuggestion = createActionButton(
1968
1972
  );
1969
1973
 
1970
1974
  // src/runtimes/local/useLocalRuntime.tsx
1971
- var import_react50 = require("react");
1975
+ var import_react54 = require("react");
1972
1976
 
1973
1977
  // src/utils/idUtils.tsx
1974
1978
  var import_non_secure = require("nanoid/non-secure");
@@ -2146,45 +2150,14 @@ var BaseAssistantRuntime = class {
2146
2150
  constructor(_thread) {
2147
2151
  this._thread = _thread;
2148
2152
  this._thread = _thread;
2149
- this._unsubscribe = this._thread.subscribe(this.subscriptionHandler);
2150
- }
2151
- get capabilities() {
2152
- return this._thread.capabilities;
2153
2153
  }
2154
- _unsubscribe;
2155
2154
  get thread() {
2156
2155
  return this._thread;
2157
2156
  }
2158
2157
  set thread(thread) {
2159
- this._unsubscribe();
2160
2158
  this._thread = thread;
2161
- this._unsubscribe = this._thread.subscribe(this.subscriptionHandler);
2162
2159
  this.subscriptionHandler();
2163
2160
  }
2164
- get messages() {
2165
- return this.thread.messages;
2166
- }
2167
- get isRunning() {
2168
- return this.thread.isRunning;
2169
- }
2170
- getBranches(messageId) {
2171
- return this.thread.getBranches(messageId);
2172
- }
2173
- switchToBranch(branchId) {
2174
- return this.thread.switchToBranch(branchId);
2175
- }
2176
- append(message) {
2177
- return this.thread.append(message);
2178
- }
2179
- startRun(parentId) {
2180
- return this.thread.startRun(parentId);
2181
- }
2182
- cancelRun() {
2183
- return this.thread.cancelRun();
2184
- }
2185
- addToolResult(options) {
2186
- return this.thread.addToolResult(options);
2187
- }
2188
2161
  _subscriptions = /* @__PURE__ */ new Set();
2189
2162
  subscribe(callback) {
2190
2163
  this._subscriptions.add(callback);
@@ -2193,179 +2166,156 @@ var BaseAssistantRuntime = class {
2193
2166
  subscriptionHandler = () => {
2194
2167
  for (const callback of this._subscriptions) callback();
2195
2168
  };
2196
- get unstable_synchronizer() {
2197
- return this.thread.unstable_synchronizer;
2198
- }
2199
2169
  };
2200
2170
 
2201
- // src/runtimes/local/LocalRuntime.tsx
2202
- var LocalRuntime = class extends BaseAssistantRuntime {
2203
- _configProviders;
2204
- constructor(adapter) {
2205
- const configProviders = /* @__PURE__ */ new Set();
2206
- super(new LocalThreadRuntime(configProviders, adapter));
2207
- this._configProviders = configProviders;
2208
- }
2209
- set adapter(adapter) {
2210
- this.thread.adapter = adapter;
2211
- }
2212
- registerModelConfigProvider(provider) {
2213
- this._configProviders.add(provider);
2214
- return () => this._configProviders.delete(provider);
2215
- }
2216
- switchToThread(threadId) {
2217
- if (threadId) {
2218
- throw new Error("LocalRuntime does not yet support switching threads");
2171
+ // src/internal.ts
2172
+ var internal_exports = {};
2173
+ __export(internal_exports, {
2174
+ BaseAssistantRuntime: () => BaseAssistantRuntime,
2175
+ MessageRepository: () => MessageRepository,
2176
+ ProxyConfigProvider: () => ProxyConfigProvider,
2177
+ TooltipIconButton: () => TooltipIconButton,
2178
+ generateId: () => generateId,
2179
+ useSmooth: () => useSmooth
2180
+ });
2181
+
2182
+ // src/ui/base/tooltip-icon-button.tsx
2183
+ var import_react52 = require("react");
2184
+
2185
+ // src/ui/base/tooltip.tsx
2186
+ var TooltipPrimitive = __toESM(require("@radix-ui/react-tooltip"));
2187
+
2188
+ // src/ui/utils/withDefaults.tsx
2189
+ var import_react50 = require("react");
2190
+ var import_classnames = __toESM(require("classnames"));
2191
+ var import_jsx_runtime25 = require("react/jsx-runtime");
2192
+ var withDefaultProps = ({
2193
+ className,
2194
+ ...defaultProps
2195
+ }) => ({ className: classNameProp, ...props }) => {
2196
+ return {
2197
+ className: (0, import_classnames.default)(className, classNameProp),
2198
+ ...defaultProps,
2199
+ ...props
2200
+ };
2201
+ };
2202
+ var withDefaults = (Component, defaultProps) => {
2203
+ const getProps = withDefaultProps(defaultProps);
2204
+ const WithDefaults = (0, import_react50.forwardRef)(
2205
+ (props, ref) => {
2206
+ const ComponentAsAny = Component;
2207
+ return /* @__PURE__ */ (0, import_jsx_runtime25.jsx)(ComponentAsAny, { ...getProps(props), ref });
2219
2208
  }
2220
- return this.thread = new LocalThreadRuntime(
2221
- this._configProviders,
2222
- this.thread.adapter
2223
- );
2224
- }
2209
+ );
2210
+ WithDefaults.displayName = "withDefaults(" + (typeof Component === "string" ? Component : Component.displayName) + ")";
2211
+ return WithDefaults;
2225
2212
  };
2226
- var CAPABILITIES = Object.freeze({
2227
- edit: true,
2228
- reload: true,
2229
- cancel: true,
2230
- copy: true
2213
+
2214
+ // src/ui/base/tooltip.tsx
2215
+ var import_jsx_runtime26 = require("react/jsx-runtime");
2216
+ var Tooltip = (props) => {
2217
+ return /* @__PURE__ */ (0, import_jsx_runtime26.jsx)(TooltipPrimitive.Provider, { children: /* @__PURE__ */ (0, import_jsx_runtime26.jsx)(TooltipPrimitive.Root, { ...props }) });
2218
+ };
2219
+ Tooltip.displayName = "Tooltip";
2220
+ var TooltipTrigger = TooltipPrimitive.Trigger;
2221
+ var TooltipContent = withDefaults(TooltipPrimitive.Content, {
2222
+ sideOffset: 4,
2223
+ className: "aui-tooltip-content"
2231
2224
  });
2232
- var LocalThreadRuntime = class {
2233
- constructor(_configProviders, adapter) {
2234
- this._configProviders = _configProviders;
2235
- this.adapter = adapter;
2236
- }
2237
- _subscriptions = /* @__PURE__ */ new Set();
2238
- abortController = null;
2239
- repository = new MessageRepository();
2240
- capabilities = CAPABILITIES;
2241
- get messages() {
2242
- return this.repository.getMessages();
2243
- }
2244
- get isRunning() {
2245
- return this.abortController != null;
2246
- }
2247
- getBranches(messageId) {
2248
- return this.repository.getBranches(messageId);
2249
- }
2250
- switchToBranch(branchId) {
2251
- this.repository.switchToBranch(branchId);
2252
- this.notifySubscribers();
2253
- }
2254
- async append(message) {
2255
- const userMessageId = generateId();
2256
- const userMessage = {
2257
- id: userMessageId,
2258
- role: "user",
2259
- content: message.content,
2260
- createdAt: /* @__PURE__ */ new Date()
2261
- };
2262
- this.repository.addOrUpdateMessage(message.parentId, userMessage);
2263
- await this.startRun(userMessageId);
2264
- }
2265
- async startRun(parentId) {
2266
- const id = generateId();
2267
- this.repository.resetHead(parentId);
2268
- const messages = this.repository.getMessages();
2269
- const message = {
2270
- id,
2271
- role: "assistant",
2272
- status: { type: "in_progress" },
2273
- content: [{ type: "text", text: "" }],
2274
- createdAt: /* @__PURE__ */ new Date()
2275
- };
2276
- this.repository.addOrUpdateMessage(parentId, { ...message });
2277
- this.abortController?.abort();
2278
- this.abortController = new AbortController();
2279
- this.notifySubscribers();
2280
- try {
2281
- const updateHandler = ({ content }) => {
2282
- message.content = content;
2283
- this.repository.addOrUpdateMessage(parentId, { ...message });
2284
- this.notifySubscribers();
2285
- };
2286
- const result = await this.adapter.run({
2287
- messages,
2288
- abortSignal: this.abortController.signal,
2289
- config: mergeModelConfigs(this._configProviders),
2290
- onUpdate: updateHandler
2291
- });
2292
- if (result !== void 0) {
2293
- updateHandler(result);
2294
- }
2295
- if (result.status?.type === "in_progress")
2296
- throw new Error(
2297
- "Unexpected in_progress status returned from ChatModelAdapter"
2298
- );
2299
- message.status = result.status ?? { type: "done" };
2300
- this.repository.addOrUpdateMessage(parentId, { ...message });
2301
- } catch (e) {
2302
- message.status = { type: "error", error: e };
2303
- this.repository.addOrUpdateMessage(parentId, { ...message });
2304
- console.error(e);
2305
- } finally {
2306
- this.abortController = null;
2307
- this.notifySubscribers();
2225
+ TooltipContent.displayName = "TooltipContent";
2226
+
2227
+ // src/ui/base/button.tsx
2228
+ var import_class_variance_authority = require("class-variance-authority");
2229
+ var import_react_primitive11 = require("@radix-ui/react-primitive");
2230
+ var import_react51 = require("react");
2231
+ var import_jsx_runtime27 = require("react/jsx-runtime");
2232
+ var buttonVariants = (0, import_class_variance_authority.cva)("aui-button", {
2233
+ variants: {
2234
+ variant: {
2235
+ default: "aui-button-primary",
2236
+ outline: "aui-button-outline",
2237
+ ghost: "aui-button-ghost"
2238
+ },
2239
+ size: {
2240
+ default: "aui-button-medium",
2241
+ icon: "aui-button-icon"
2308
2242
  }
2243
+ },
2244
+ defaultVariants: {
2245
+ variant: "default",
2246
+ size: "default"
2309
2247
  }
2310
- cancelRun() {
2311
- if (!this.abortController) return;
2312
- this.abortController.abort();
2313
- this.abortController = null;
2314
- this.notifySubscribers();
2315
- }
2316
- notifySubscribers() {
2317
- for (const callback of this._subscriptions) callback();
2318
- }
2319
- subscribe(callback) {
2320
- this._subscriptions.add(callback);
2321
- return () => this._subscriptions.delete(callback);
2322
- }
2323
- addToolResult({ messageId, toolCallId, result }) {
2324
- const { parentId, message } = this.repository.getMessage(messageId);
2325
- if (message.role !== "assistant")
2326
- throw new Error("Tried to add tool re^sult to non-assistant message");
2327
- let found = false;
2328
- const newContent = message.content.map((c) => {
2329
- if (c.type !== "tool-call") return c;
2330
- if (c.toolCallId !== toolCallId) return c;
2331
- found = true;
2332
- return {
2333
- ...c,
2334
- result
2335
- };
2336
- });
2337
- if (!found)
2338
- throw new Error("Tried to add tool result to non-existing tool call");
2339
- this.repository.addOrUpdateMessage(parentId, {
2340
- ...message,
2341
- content: newContent
2342
- });
2248
+ });
2249
+ var Button = (0, import_react51.forwardRef)(
2250
+ ({ className, variant, size, ...props }, ref) => {
2251
+ return /* @__PURE__ */ (0, import_jsx_runtime27.jsx)(
2252
+ import_react_primitive11.Primitive.button,
2253
+ {
2254
+ className: buttonVariants({ variant, size, className }),
2255
+ ...props,
2256
+ ref
2257
+ }
2258
+ );
2343
2259
  }
2344
- };
2260
+ );
2261
+ Button.displayName = "Button";
2345
2262
 
2346
- // src/runtimes/local/useLocalRuntime.tsx
2347
- var useLocalRuntime = (adapter) => {
2348
- const [runtime] = (0, import_react50.useState)(() => new LocalRuntime(adapter));
2349
- (0, import_react50.useInsertionEffect)(() => {
2350
- runtime.adapter = adapter;
2263
+ // src/ui/base/tooltip-icon-button.tsx
2264
+ var import_jsx_runtime28 = require("react/jsx-runtime");
2265
+ var TooltipIconButton = (0, import_react52.forwardRef)(({ children, tooltip, side = "bottom", ...rest }, ref) => {
2266
+ return /* @__PURE__ */ (0, import_jsx_runtime28.jsxs)(Tooltip, { children: [
2267
+ /* @__PURE__ */ (0, import_jsx_runtime28.jsx)(TooltipTrigger, { asChild: true, children: /* @__PURE__ */ (0, import_jsx_runtime28.jsxs)(Button, { variant: "ghost", size: "icon", ...rest, ref, children: [
2268
+ children,
2269
+ /* @__PURE__ */ (0, import_jsx_runtime28.jsx)("span", { className: "aui-sr-only", children: tooltip })
2270
+ ] }) }),
2271
+ /* @__PURE__ */ (0, import_jsx_runtime28.jsx)(TooltipContent, { side, children: tooltip })
2272
+ ] });
2273
+ });
2274
+ TooltipIconButton.displayName = "TooltipIconButton";
2275
+
2276
+ // src/runtimes/edge/useEdgeRuntime.ts
2277
+ var import_react53 = require("react");
2278
+
2279
+ // src/runtimes/edge/converters/toCoreMessages.ts
2280
+ var toCoreMessages = (message) => {
2281
+ return message.map((message2) => {
2282
+ return {
2283
+ role: message2.role,
2284
+ content: message2.content.map((part) => {
2285
+ if (part.type === "ui") throw new Error("UI parts are not supported");
2286
+ return part;
2287
+ })
2288
+ };
2351
2289
  });
2352
- return runtime;
2290
+ };
2291
+
2292
+ // src/runtimes/edge/converters/toLanguageModelTools.ts
2293
+ var import_zod = require("zod");
2294
+ var import_zod_to_json_schema = __toESM(require("zod-to-json-schema"));
2295
+ var toLanguageModelTools = (tools) => {
2296
+ if (!tools) return [];
2297
+ return Object.entries(tools).map(([name, tool]) => ({
2298
+ type: "function",
2299
+ name,
2300
+ ...tool.description ? { description: tool.description } : void 0,
2301
+ parameters: tool.parameters instanceof import_zod.z.ZodType ? (0, import_zod_to_json_schema.default)(tool.parameters) : tool.parameters
2302
+ }));
2353
2303
  };
2354
2304
 
2355
2305
  // src/runtimes/edge/streams/assistantDecoderStream.ts
2356
2306
  function assistantDecoderStream() {
2307
+ const toolCallNames = /* @__PURE__ */ new Map();
2357
2308
  let currentToolCall;
2358
2309
  return new TransformStream({
2359
2310
  transform(chunk, controller) {
2360
- const [code, valueJson] = parseStreamPart(chunk);
2361
- const value = JSON.parse(valueJson);
2311
+ const [code, value] = parseStreamPart(chunk);
2362
2312
  if (currentToolCall && code !== "2" /* ToolCallArgsTextDelta */ && code !== "E" /* Error */) {
2363
2313
  controller.enqueue({
2364
2314
  type: "tool-call",
2365
2315
  toolCallType: "function",
2366
2316
  toolCallId: currentToolCall.id,
2367
2317
  toolName: currentToolCall.name,
2368
- args: JSON.parse(currentToolCall.argsText)
2318
+ args: currentToolCall.argsText
2369
2319
  });
2370
2320
  currentToolCall = void 0;
2371
2321
  }
@@ -2378,12 +2328,13 @@ function assistantDecoderStream() {
2378
2328
  break;
2379
2329
  }
2380
2330
  case "1" /* ToolCallBegin */: {
2381
- const { id, name } = JSON.parse(value);
2331
+ const { id, name } = value;
2332
+ toolCallNames.set(id, name);
2382
2333
  currentToolCall = { id, name, argsText: "" };
2383
2334
  break;
2384
2335
  }
2385
2336
  case "2" /* ToolCallArgsTextDelta */: {
2386
- const delta = JSON.parse(value);
2337
+ const delta = value;
2387
2338
  currentToolCall.argsText += delta;
2388
2339
  controller.enqueue({
2389
2340
  type: "tool-call-delta",
@@ -2394,17 +2345,27 @@ function assistantDecoderStream() {
2394
2345
  });
2395
2346
  break;
2396
2347
  }
2397
- case "F" /* Finish */: {
2348
+ case "3" /* ToolCallResult */: {
2398
2349
  controller.enqueue({
2399
- type: "finish",
2400
- ...JSON.parse(value)
2350
+ type: "tool-result",
2351
+ toolCallType: "function",
2352
+ toolCallId: value.id,
2353
+ toolName: toolCallNames.get(value.id),
2354
+ result: value.result
2401
2355
  });
2402
2356
  break;
2403
2357
  }
2404
- case "E" /* Error */: {
2358
+ case "F" /* Finish */: {
2359
+ controller.enqueue({
2360
+ type: "finish",
2361
+ ...value
2362
+ });
2363
+ break;
2364
+ }
2365
+ case "E" /* Error */: {
2405
2366
  controller.enqueue({
2406
2367
  type: "error",
2407
- error: JSON.parse(value)
2368
+ error: value
2408
2369
  });
2409
2370
  break;
2410
2371
  }
@@ -2421,7 +2382,7 @@ var parseStreamPart = (part) => {
2421
2382
  if (index === -1) throw new Error("Invalid stream part");
2422
2383
  return [
2423
2384
  part.slice(0, index),
2424
- part.slice(index + 1)
2385
+ JSON.parse(part.slice(index + 1))
2425
2386
  ];
2426
2387
  };
2427
2388
 
@@ -2780,9 +2741,9 @@ var parsePartialJson = (json) => {
2780
2741
  };
2781
2742
 
2782
2743
  // src/runtimes/edge/streams/runResultStream.ts
2783
- function runResultStream() {
2744
+ function runResultStream(initialContent) {
2784
2745
  let message = {
2785
- content: []
2746
+ content: initialContent
2786
2747
  };
2787
2748
  const currentToolCall = { toolCallId: "", argsText: "" };
2788
2749
  return new TransformStream({
@@ -2806,7 +2767,7 @@ function runResultStream() {
2806
2767
  message,
2807
2768
  toolCallId,
2808
2769
  toolName,
2809
- parsePartialJson(currentToolCall.argsText)
2770
+ currentToolCall.argsText
2810
2771
  );
2811
2772
  controller.enqueue(message);
2812
2773
  break;
@@ -2854,7 +2815,7 @@ var appendOrUpdateText = (message, textDelta) => {
2854
2815
  content: contentParts.concat([contentPart])
2855
2816
  };
2856
2817
  };
2857
- var appendOrUpdateToolCall = (message, toolCallId, toolName, args) => {
2818
+ var appendOrUpdateToolCall = (message, toolCallId, toolName, argsText) => {
2858
2819
  let contentParts = message.content;
2859
2820
  let contentPart = message.content.at(-1);
2860
2821
  if (contentPart?.type !== "tool-call" || contentPart.toolCallId !== toolCallId) {
@@ -2862,13 +2823,15 @@ var appendOrUpdateToolCall = (message, toolCallId, toolName, args) => {
2862
2823
  type: "tool-call",
2863
2824
  toolCallId,
2864
2825
  toolName,
2865
- args
2826
+ argsText,
2827
+ args: parsePartialJson(argsText)
2866
2828
  };
2867
2829
  } else {
2868
2830
  contentParts = contentParts.slice(0, -1);
2869
2831
  contentPart = {
2870
2832
  ...contentPart,
2871
- args
2833
+ argsText,
2834
+ args: parsePartialJson(argsText)
2872
2835
  };
2873
2836
  }
2874
2837
  return {
@@ -2911,11 +2874,8 @@ var appendOrUpdateFinish = (message, chunk) => {
2911
2874
  };
2912
2875
  };
2913
2876
 
2914
- // src/runtimes/edge/useEdgeRuntime.ts
2915
- var import_react51 = require("react");
2916
-
2917
2877
  // src/runtimes/edge/streams/toolResultStream.ts
2918
- var import_zod = require("zod");
2878
+ var import_zod2 = require("zod");
2919
2879
  var import_secure_json_parse2 = __toESM(require("secure-json-parse"));
2920
2880
  function toolResultStream(tools) {
2921
2881
  const toolCallExecutions = /* @__PURE__ */ new Map();
@@ -2927,9 +2887,9 @@ function toolResultStream(tools) {
2927
2887
  case "tool-call": {
2928
2888
  const { toolCallId, toolCallType, toolName, args: argsText } = chunk;
2929
2889
  const tool = tools?.[toolName];
2930
- if (!tool) return;
2890
+ if (!tool || !tool.execute) return;
2931
2891
  const args = import_secure_json_parse2.default.parse(argsText);
2932
- if (tool.parameters instanceof import_zod.z.ZodType) {
2892
+ if (tool.parameters instanceof import_zod2.z.ZodType) {
2933
2893
  const result = tool.parameters.safeParse(args);
2934
2894
  if (!result.success) {
2935
2895
  controller.enqueue({
@@ -2966,6 +2926,7 @@ function toolResultStream(tools) {
2966
2926
  }
2967
2927
  case "text-delta":
2968
2928
  case "tool-call-delta":
2929
+ case "tool-result":
2969
2930
  case "finish":
2970
2931
  case "error":
2971
2932
  break;
@@ -2981,9 +2942,7 @@ function toolResultStream(tools) {
2981
2942
  });
2982
2943
  }
2983
2944
 
2984
- // src/runtimes/edge/useEdgeRuntime.ts
2985
- var import_zod2 = require("zod");
2986
- var import_zod_to_json_schema = require("zod-to-json-schema");
2945
+ // src/runtimes/edge/EdgeChatAdapter.ts
2987
2946
  function asAsyncIterable(source) {
2988
2947
  return {
2989
2948
  [Symbol.asyncIterator]: () => {
@@ -2997,170 +2956,502 @@ function asAsyncIterable(source) {
2997
2956
  }
2998
2957
  };
2999
2958
  }
3000
- var toSerializableTools = (tools) => {
3001
- if (!tools) return [];
3002
- return Object.entries(tools).map(([name, tool]) => ({
3003
- type: "function",
3004
- name,
3005
- ...tool.description ? { description: tool.description } : void 0,
3006
- parameters: tool.parameters instanceof import_zod2.z.ZodType ? (0, import_zod_to_json_schema.zodToJsonSchema)(tool.parameters) : tool.parameters
3007
- }));
3008
- };
3009
- var createEdgeChatAdapter = ({
3010
- api
3011
- }) => ({
3012
- run: async ({ messages, abortSignal, config, onUpdate }) => {
3013
- const result = await fetch(api, {
2959
+ var EdgeChatAdapter = class {
2960
+ constructor(options) {
2961
+ this.options = options;
2962
+ }
2963
+ async roundtrip(initialContent, { messages, abortSignal, config, onUpdate }) {
2964
+ const result = await fetch(this.options.api, {
3014
2965
  method: "POST",
3015
2966
  headers: {
3016
2967
  "Content-Type": "application/json"
3017
2968
  },
3018
2969
  body: JSON.stringify({
3019
2970
  system: config.system,
3020
- messages,
3021
- tools: toSerializableTools(
2971
+ messages: toCoreMessages(messages),
2972
+ tools: toLanguageModelTools(
3022
2973
  config.tools
3023
2974
  )
3024
2975
  }),
3025
2976
  signal: abortSignal
3026
2977
  });
3027
- const stream = result.body.pipeThrough(new TextDecoderStream()).pipeThrough(chunkByLineStream()).pipeThrough(assistantDecoderStream()).pipeThrough(toolResultStream(config.tools)).pipeThrough(runResultStream());
2978
+ const stream = result.body.pipeThrough(new TextDecoderStream()).pipeThrough(chunkByLineStream()).pipeThrough(assistantDecoderStream()).pipeThrough(toolResultStream(config.tools)).pipeThrough(runResultStream(initialContent));
2979
+ let message;
3028
2980
  let update;
3029
2981
  for await (update of asAsyncIterable(stream)) {
3030
- onUpdate(update);
2982
+ message = onUpdate(update);
3031
2983
  }
3032
2984
  if (update === void 0)
3033
2985
  throw new Error("No data received from Edge Runtime");
3034
- return update;
2986
+ return [message, update];
2987
+ }
2988
+ async run({ messages, abortSignal, config, onUpdate }) {
2989
+ let roundtripAllowance = this.options.maxToolRoundtrips ?? 1;
2990
+ let usage = {
2991
+ promptTokens: 0,
2992
+ completionTokens: 0
2993
+ };
2994
+ let result;
2995
+ let assistantMessage;
2996
+ do {
2997
+ [assistantMessage, result] = await this.roundtrip(result?.content ?? [], {
2998
+ messages: assistantMessage ? [...messages, assistantMessage] : messages,
2999
+ abortSignal,
3000
+ config,
3001
+ onUpdate
3002
+ });
3003
+ if (result.status?.type === "done") {
3004
+ usage.promptTokens += result.status.usage?.promptTokens ?? 0;
3005
+ usage.completionTokens += result.status.usage?.completionTokens ?? 0;
3006
+ }
3007
+ } while (result.status?.type === "done" && result.status.finishReason === "tool-calls" && result.content.every((c) => c.type !== "tool-call" || !!c.result) && roundtripAllowance-- > 0);
3008
+ if (result.status?.type === "done" && usage.promptTokens > 0) {
3009
+ result = {
3010
+ ...result,
3011
+ status: {
3012
+ ...result.status,
3013
+ usage
3014
+ }
3015
+ };
3016
+ }
3017
+ return result;
3018
+ }
3019
+ };
3020
+
3021
+ // src/runtimes/edge/useEdgeRuntime.ts
3022
+ var useEdgeRuntime = ({
3023
+ initialMessages,
3024
+ ...options
3025
+ }) => {
3026
+ const [adapter] = (0, import_react53.useState)(() => new EdgeChatAdapter(options));
3027
+ return useLocalRuntime(adapter, { initialMessages });
3028
+ };
3029
+
3030
+ // src/runtimes/edge/converters/toLanguageModelMessages.ts
3031
+ var assistantMessageSplitter = () => {
3032
+ const stash = [];
3033
+ let assistantMessage = {
3034
+ role: "assistant",
3035
+ content: []
3036
+ };
3037
+ let toolMessage = {
3038
+ role: "tool",
3039
+ content: []
3040
+ };
3041
+ return {
3042
+ addTextContentPart: (part) => {
3043
+ if (toolMessage.content.length > 0) {
3044
+ stash.push(assistantMessage);
3045
+ stash.push(toolMessage);
3046
+ assistantMessage = {
3047
+ role: "assistant",
3048
+ content: []
3049
+ };
3050
+ toolMessage = {
3051
+ role: "tool",
3052
+ content: []
3053
+ };
3054
+ }
3055
+ assistantMessage.content.push(part);
3056
+ },
3057
+ addToolCallPart: (part) => {
3058
+ assistantMessage.content.push({
3059
+ type: "tool-call",
3060
+ toolCallId: part.toolCallId,
3061
+ toolName: part.toolName,
3062
+ args: part.args
3063
+ });
3064
+ if (part.result) {
3065
+ toolMessage.content.push({
3066
+ type: "tool-result",
3067
+ toolCallId: part.toolCallId,
3068
+ toolName: part.toolName,
3069
+ result: part.result
3070
+ // isError
3071
+ });
3072
+ }
3073
+ },
3074
+ getMessages: () => {
3075
+ if (toolMessage.content.length > 0) {
3076
+ return [...stash, assistantMessage, toolMessage];
3077
+ }
3078
+ return [...stash, assistantMessage];
3079
+ }
3080
+ };
3081
+ };
3082
+ function toLanguageModelMessages(message) {
3083
+ return message.flatMap((message2) => {
3084
+ const role = message2.role;
3085
+ switch (role) {
3086
+ case "system": {
3087
+ return [{ role: "system", content: message2.content[0].text }];
3088
+ }
3089
+ case "user": {
3090
+ const msg = {
3091
+ role: "user",
3092
+ content: message2.content.map(
3093
+ (part) => {
3094
+ const type = part.type;
3095
+ switch (type) {
3096
+ case "text": {
3097
+ return part;
3098
+ }
3099
+ case "image": {
3100
+ return {
3101
+ type: "image",
3102
+ image: new URL(part.image)
3103
+ };
3104
+ }
3105
+ default: {
3106
+ const unhandledType = type;
3107
+ throw new Error(
3108
+ `Unspported content part type: ${unhandledType}`
3109
+ );
3110
+ }
3111
+ }
3112
+ }
3113
+ )
3114
+ };
3115
+ return [msg];
3116
+ }
3117
+ case "assistant": {
3118
+ const splitter = assistantMessageSplitter();
3119
+ for (const part of message2.content) {
3120
+ const type = part.type;
3121
+ switch (type) {
3122
+ case "text": {
3123
+ splitter.addTextContentPart(part);
3124
+ break;
3125
+ }
3126
+ case "tool-call": {
3127
+ splitter.addToolCallPart(part);
3128
+ break;
3129
+ }
3130
+ default: {
3131
+ const unhandledType = type;
3132
+ throw new Error(`Unhandled content part type: ${unhandledType}`);
3133
+ }
3134
+ }
3135
+ }
3136
+ return splitter.getMessages();
3137
+ }
3138
+ default: {
3139
+ const unhandledRole = role;
3140
+ throw new Error(`Unknown message role: ${unhandledRole}`);
3141
+ }
3142
+ }
3143
+ });
3144
+ }
3145
+
3146
+ // src/runtimes/edge/converters/fromLanguageModelMessages.ts
3147
+ var fromLanguageModelMessages = (lm, mergeRoundtrips) => {
3148
+ const messages = [];
3149
+ for (const lmMessage of lm) {
3150
+ const role = lmMessage.role;
3151
+ switch (role) {
3152
+ case "system": {
3153
+ messages.push({
3154
+ role: "system",
3155
+ content: [
3156
+ {
3157
+ type: "text",
3158
+ text: lmMessage.content
3159
+ }
3160
+ ]
3161
+ });
3162
+ break;
3163
+ }
3164
+ case "user": {
3165
+ messages.push({
3166
+ role: "user",
3167
+ content: lmMessage.content.map((part) => {
3168
+ const type = part.type;
3169
+ switch (type) {
3170
+ case "text": {
3171
+ return {
3172
+ type: "text",
3173
+ text: part.text
3174
+ };
3175
+ }
3176
+ case "image": {
3177
+ if (part.image instanceof URL) {
3178
+ return {
3179
+ type: "image",
3180
+ image: part.image.href
3181
+ };
3182
+ }
3183
+ throw new Error("Only images with URL data are supported");
3184
+ }
3185
+ default: {
3186
+ const unhandledType = type;
3187
+ throw new Error(`Unknown content part type: ${unhandledType}`);
3188
+ }
3189
+ }
3190
+ })
3191
+ });
3192
+ break;
3193
+ }
3194
+ case "assistant": {
3195
+ const newContent = lmMessage.content.map((part) => {
3196
+ if (part.type === "tool-call") {
3197
+ return {
3198
+ type: "tool-call",
3199
+ toolCallId: part.toolCallId,
3200
+ toolName: part.toolName,
3201
+ argsText: JSON.stringify(part.args),
3202
+ args: typeof part.args === "string" ? part.args : part.args
3203
+ };
3204
+ }
3205
+ return part;
3206
+ });
3207
+ if (mergeRoundtrips) {
3208
+ const previousMessage = messages[messages.length - 1];
3209
+ if (previousMessage?.role === "assistant") {
3210
+ previousMessage.content.push(...newContent);
3211
+ break;
3212
+ }
3213
+ }
3214
+ messages.push({
3215
+ role: "assistant",
3216
+ content: newContent
3217
+ });
3218
+ break;
3219
+ }
3220
+ case "tool": {
3221
+ const previousMessage = messages[messages.length - 1];
3222
+ if (previousMessage?.role !== "assistant")
3223
+ throw new Error(
3224
+ "A tool message must be preceded by an assistant message."
3225
+ );
3226
+ for (const tool of lmMessage.content) {
3227
+ const toolCall = previousMessage.content.find(
3228
+ (c) => c.type === "tool-call" && c.toolCallId === tool.toolCallId
3229
+ );
3230
+ if (!toolCall)
3231
+ throw new Error("Received tool result for an unknown tool call.");
3232
+ if (toolCall.toolName !== tool.toolName)
3233
+ throw new Error("Tool call name mismatch.");
3234
+ toolCall.result = tool.result;
3235
+ if (tool.isError) {
3236
+ toolCall.isError = true;
3237
+ }
3238
+ }
3239
+ break;
3240
+ }
3241
+ default: {
3242
+ const unhandledRole = role;
3243
+ throw new Error(`Unknown message role: ${unhandledRole}`);
3244
+ }
3245
+ }
3246
+ }
3247
+ return messages;
3248
+ };
3249
+
3250
+ // src/runtimes/edge/converters/fromCoreMessage.ts
3251
+ var fromCoreMessages = (message) => {
3252
+ return message.map((message2) => {
3253
+ return {
3254
+ id: generateId(),
3255
+ createdAt: /* @__PURE__ */ new Date(),
3256
+ ...message2.role === "assistant" ? {
3257
+ status: { type: "done" }
3258
+ } : void 0,
3259
+ ...message2
3260
+ };
3261
+ });
3262
+ };
3263
+
3264
+ // src/runtimes/local/LocalRuntime.tsx
3265
+ var LocalRuntime = class extends BaseAssistantRuntime {
3266
+ _proxyConfigProvider;
3267
+ constructor(adapter, options) {
3268
+ const proxyConfigProvider = new ProxyConfigProvider();
3269
+ super(new LocalThreadRuntime(proxyConfigProvider, adapter, options));
3270
+ this._proxyConfigProvider = proxyConfigProvider;
3035
3271
  }
3272
+ set adapter(adapter) {
3273
+ this.thread.adapter = adapter;
3274
+ }
3275
+ registerModelConfigProvider(provider) {
3276
+ return this._proxyConfigProvider.registerModelConfigProvider(provider);
3277
+ }
3278
+ switchToThread(threadId) {
3279
+ if (threadId) {
3280
+ throw new Error("LocalRuntime does not yet support switching threads");
3281
+ }
3282
+ return this.thread = new LocalThreadRuntime(
3283
+ this._proxyConfigProvider,
3284
+ this.thread.adapter
3285
+ );
3286
+ }
3287
+ };
3288
+ var CAPABILITIES = Object.freeze({
3289
+ edit: true,
3290
+ reload: true,
3291
+ cancel: true,
3292
+ copy: true
3036
3293
  });
3037
- var useEdgeRuntime = (options) => {
3038
- const adapter = (0, import_react51.useMemo)(() => createEdgeChatAdapter(options), [options]);
3039
- return useLocalRuntime(adapter);
3294
+ var LocalThreadRuntime = class {
3295
+ constructor(configProvider, adapter, options) {
3296
+ this.configProvider = configProvider;
3297
+ this.adapter = adapter;
3298
+ if (options?.initialMessages) {
3299
+ let parentId = null;
3300
+ const messages = fromCoreMessages(options.initialMessages);
3301
+ for (const message of messages) {
3302
+ this.repository.addOrUpdateMessage(parentId, message);
3303
+ parentId = message.id;
3304
+ }
3305
+ }
3306
+ }
3307
+ _subscriptions = /* @__PURE__ */ new Set();
3308
+ abortController = null;
3309
+ repository = new MessageRepository();
3310
+ capabilities = CAPABILITIES;
3311
+ get messages() {
3312
+ return this.repository.getMessages();
3313
+ }
3314
+ get isRunning() {
3315
+ return this.abortController != null;
3316
+ }
3317
+ getBranches(messageId) {
3318
+ return this.repository.getBranches(messageId);
3319
+ }
3320
+ switchToBranch(branchId) {
3321
+ this.repository.switchToBranch(branchId);
3322
+ this.notifySubscribers();
3323
+ }
3324
+ async append(message) {
3325
+ if (message.role !== "user")
3326
+ throw new Error(
3327
+ "Only appending user messages are supported in LocalRuntime. This is likely an internal bug in assistant-ui."
3328
+ );
3329
+ const userMessageId = generateId();
3330
+ const userMessage = {
3331
+ id: userMessageId,
3332
+ role: "user",
3333
+ content: message.content,
3334
+ createdAt: /* @__PURE__ */ new Date()
3335
+ };
3336
+ this.repository.addOrUpdateMessage(message.parentId, userMessage);
3337
+ await this.startRun(userMessageId);
3338
+ }
3339
+ async startRun(parentId) {
3340
+ this.repository.resetHead(parentId);
3341
+ const messages = this.repository.getMessages();
3342
+ const message = {
3343
+ id: generateId(),
3344
+ role: "assistant",
3345
+ status: { type: "in_progress" },
3346
+ content: [{ type: "text", text: "" }],
3347
+ createdAt: /* @__PURE__ */ new Date()
3348
+ };
3349
+ this.repository.addOrUpdateMessage(parentId, { ...message });
3350
+ this.abortController?.abort();
3351
+ this.abortController = new AbortController();
3352
+ this.notifySubscribers();
3353
+ try {
3354
+ const updateHandler = ({ content }) => {
3355
+ message.content = content;
3356
+ const newMessage = { ...message };
3357
+ this.repository.addOrUpdateMessage(parentId, newMessage);
3358
+ this.notifySubscribers();
3359
+ return newMessage;
3360
+ };
3361
+ const result = await this.adapter.run({
3362
+ messages,
3363
+ abortSignal: this.abortController.signal,
3364
+ config: this.configProvider.getModelConfig(),
3365
+ onUpdate: updateHandler
3366
+ });
3367
+ if (result !== void 0) {
3368
+ updateHandler(result);
3369
+ }
3370
+ if (result.status?.type === "in_progress")
3371
+ throw new Error(
3372
+ "Unexpected in_progress status returned from ChatModelAdapter"
3373
+ );
3374
+ message.status = result.status ?? { type: "done" };
3375
+ this.repository.addOrUpdateMessage(parentId, { ...message });
3376
+ } catch (e) {
3377
+ message.status = { type: "error", error: e };
3378
+ this.repository.addOrUpdateMessage(parentId, { ...message });
3379
+ throw e;
3380
+ } finally {
3381
+ this.abortController = null;
3382
+ this.notifySubscribers();
3383
+ }
3384
+ }
3385
+ cancelRun() {
3386
+ if (!this.abortController) return;
3387
+ this.abortController.abort();
3388
+ this.abortController = null;
3389
+ }
3390
+ notifySubscribers() {
3391
+ for (const callback of this._subscriptions) callback();
3392
+ }
3393
+ subscribe(callback) {
3394
+ this._subscriptions.add(callback);
3395
+ return () => this._subscriptions.delete(callback);
3396
+ }
3397
+ addToolResult({ messageId, toolCallId, result }) {
3398
+ const { parentId, message } = this.repository.getMessage(messageId);
3399
+ if (message.role !== "assistant")
3400
+ throw new Error("Tried to add tool result to non-assistant message");
3401
+ let found = false;
3402
+ const newContent = message.content.map((c) => {
3403
+ if (c.type !== "tool-call") return c;
3404
+ if (c.toolCallId !== toolCallId) return c;
3405
+ found = true;
3406
+ return {
3407
+ ...c,
3408
+ result
3409
+ };
3410
+ });
3411
+ if (!found)
3412
+ throw new Error("Tried to add tool result to non-existing tool call");
3413
+ this.repository.addOrUpdateMessage(parentId, {
3414
+ ...message,
3415
+ content: newContent
3416
+ });
3417
+ }
3418
+ };
3419
+
3420
+ // src/runtimes/local/useLocalRuntime.tsx
3421
+ var useLocalRuntime = (adapter, options) => {
3422
+ const [runtime] = (0, import_react54.useState)(() => new LocalRuntime(adapter, options));
3423
+ (0, import_react54.useInsertionEffect)(() => {
3424
+ runtime.adapter = adapter;
3425
+ });
3426
+ return runtime;
3040
3427
  };
3041
3428
 
3042
3429
  // src/ui/thread-config.tsx
3043
- var import_react52 = require("react");
3044
- var import_jsx_runtime25 = require("react/jsx-runtime");
3045
- var ThreadConfigContext = (0, import_react52.createContext)({});
3430
+ var import_react55 = require("react");
3431
+ var import_jsx_runtime29 = require("react/jsx-runtime");
3432
+ var ThreadConfigContext = (0, import_react55.createContext)({});
3046
3433
  var useThreadConfig = () => {
3047
- return (0, import_react52.useContext)(ThreadConfigContext);
3434
+ return (0, import_react55.useContext)(ThreadConfigContext);
3048
3435
  };
3049
3436
  var ThreadConfigProvider = ({
3050
3437
  children,
3051
3438
  config
3052
3439
  }) => {
3053
3440
  const assistant = useAssistantContext({ optional: true });
3054
- const configProvider = config && Object.keys(config ?? {}).length > 0 ? /* @__PURE__ */ (0, import_jsx_runtime25.jsx)(ThreadConfigContext.Provider, { value: config, children }) : /* @__PURE__ */ (0, import_jsx_runtime25.jsx)(import_jsx_runtime25.Fragment, { children });
3441
+ const configProvider = config && Object.keys(config ?? {}).length > 0 ? /* @__PURE__ */ (0, import_jsx_runtime29.jsx)(ThreadConfigContext.Provider, { value: config, children }) : /* @__PURE__ */ (0, import_jsx_runtime29.jsx)(import_jsx_runtime29.Fragment, { children });
3055
3442
  if (!config?.runtime) return configProvider;
3056
3443
  if (assistant) {
3057
3444
  throw new Error(
3058
3445
  "You provided a runtime to <Thread> while simulataneously using <AssistantRuntimeProvider>. This is not allowed."
3059
3446
  );
3060
3447
  }
3061
- return /* @__PURE__ */ (0, import_jsx_runtime25.jsx)(AssistantRuntimeProvider, { runtime: config.runtime, children: configProvider });
3448
+ return /* @__PURE__ */ (0, import_jsx_runtime29.jsx)(AssistantRuntimeProvider, { runtime: config.runtime, children: configProvider });
3062
3449
  };
3063
3450
  ThreadConfigProvider.displayName = "ThreadConfigProvider";
3064
3451
 
3065
3452
  // src/ui/assistant-action-bar.tsx
3066
3453
  var import_react56 = require("react");
3067
3454
  var import_lucide_react = require("lucide-react");
3068
-
3069
- // src/ui/base/tooltip-icon-button.tsx
3070
- var import_react55 = require("react");
3071
-
3072
- // src/ui/base/tooltip.tsx
3073
- var TooltipPrimitive = __toESM(require("@radix-ui/react-tooltip"));
3074
-
3075
- // src/ui/utils/withDefaults.tsx
3076
- var import_react53 = require("react");
3077
- var import_classnames = __toESM(require("classnames"));
3078
- var import_jsx_runtime26 = require("react/jsx-runtime");
3079
- var withDefaultProps = ({
3080
- className,
3081
- ...defaultProps
3082
- }) => ({ className: classNameProp, ...props }) => {
3083
- return {
3084
- className: (0, import_classnames.default)(className, classNameProp),
3085
- ...defaultProps,
3086
- ...props
3087
- };
3088
- };
3089
- var withDefaults = (Component, defaultProps) => {
3090
- const getProps = withDefaultProps(defaultProps);
3091
- const WithDefaults = (0, import_react53.forwardRef)(
3092
- (props, ref) => {
3093
- const ComponentAsAny = Component;
3094
- return /* @__PURE__ */ (0, import_jsx_runtime26.jsx)(ComponentAsAny, { ...getProps(props), ref });
3095
- }
3096
- );
3097
- WithDefaults.displayName = "withDefaults(" + (typeof Component === "string" ? Component : Component.displayName) + ")";
3098
- return WithDefaults;
3099
- };
3100
-
3101
- // src/ui/base/tooltip.tsx
3102
- var import_jsx_runtime27 = require("react/jsx-runtime");
3103
- var Tooltip = (props) => {
3104
- return /* @__PURE__ */ (0, import_jsx_runtime27.jsx)(TooltipPrimitive.Provider, { children: /* @__PURE__ */ (0, import_jsx_runtime27.jsx)(TooltipPrimitive.Root, { ...props }) });
3105
- };
3106
- Tooltip.displayName = "Tooltip";
3107
- var TooltipTrigger = TooltipPrimitive.Trigger;
3108
- var TooltipContent = withDefaults(TooltipPrimitive.Content, {
3109
- sideOffset: 4,
3110
- className: "aui-tooltip-content"
3111
- });
3112
- TooltipContent.displayName = "TooltipContent";
3113
-
3114
- // src/ui/base/button.tsx
3115
- var import_class_variance_authority = require("class-variance-authority");
3116
- var import_react_primitive11 = require("@radix-ui/react-primitive");
3117
- var import_react54 = require("react");
3118
- var import_jsx_runtime28 = require("react/jsx-runtime");
3119
- var buttonVariants = (0, import_class_variance_authority.cva)("aui-button", {
3120
- variants: {
3121
- variant: {
3122
- default: "aui-button-primary",
3123
- outline: "aui-button-outline",
3124
- ghost: "aui-button-ghost"
3125
- },
3126
- size: {
3127
- default: "aui-button-medium",
3128
- icon: "aui-button-icon"
3129
- }
3130
- },
3131
- defaultVariants: {
3132
- variant: "default",
3133
- size: "default"
3134
- }
3135
- });
3136
- var Button = (0, import_react54.forwardRef)(
3137
- ({ className, variant, size, ...props }, ref) => {
3138
- return /* @__PURE__ */ (0, import_jsx_runtime28.jsx)(
3139
- import_react_primitive11.Primitive.button,
3140
- {
3141
- className: buttonVariants({ variant, size, className }),
3142
- ...props,
3143
- ref
3144
- }
3145
- );
3146
- }
3147
- );
3148
- Button.displayName = "Button";
3149
-
3150
- // src/ui/base/tooltip-icon-button.tsx
3151
- var import_jsx_runtime29 = require("react/jsx-runtime");
3152
- var TooltipIconButton = (0, import_react55.forwardRef)(({ children, tooltip, side = "bottom", ...rest }, ref) => {
3153
- return /* @__PURE__ */ (0, import_jsx_runtime29.jsxs)(Tooltip, { children: [
3154
- /* @__PURE__ */ (0, import_jsx_runtime29.jsx)(TooltipTrigger, { asChild: true, children: /* @__PURE__ */ (0, import_jsx_runtime29.jsxs)(Button, { variant: "ghost", size: "icon", ...rest, ref, children: [
3155
- children,
3156
- /* @__PURE__ */ (0, import_jsx_runtime29.jsx)("span", { className: "aui-sr-only", children: tooltip })
3157
- ] }) }),
3158
- /* @__PURE__ */ (0, import_jsx_runtime29.jsx)(TooltipContent, { side, children: tooltip })
3159
- ] });
3160
- });
3161
- TooltipIconButton.displayName = "TooltipIconButton";
3162
-
3163
- // src/ui/assistant-action-bar.tsx
3164
3455
  var import_jsx_runtime30 = require("react/jsx-runtime");
3165
3456
  var useAllowCopy = () => {
3166
3457
  const { assistantMessage: { allowCopy = true } = {} } = useThreadConfig();
@@ -3204,10 +3495,10 @@ var AssistantActionBarCopy = (0, import_react56.forwardRef)((props, ref) => {
3204
3495
  } = useThreadConfig();
3205
3496
  const allowCopy = useAllowCopy();
3206
3497
  if (!allowCopy) return null;
3207
- return /* @__PURE__ */ (0, import_jsx_runtime30.jsx)(actionBar_exports.Copy, { asChild: true, children: /* @__PURE__ */ (0, import_jsx_runtime30.jsxs)(TooltipIconButton, { tooltip, ...props, ref, children: [
3498
+ return /* @__PURE__ */ (0, import_jsx_runtime30.jsx)(actionBar_exports.Copy, { asChild: true, children: /* @__PURE__ */ (0, import_jsx_runtime30.jsx)(TooltipIconButton, { tooltip, ...props, ref, children: props.children ?? /* @__PURE__ */ (0, import_jsx_runtime30.jsxs)(import_jsx_runtime30.Fragment, { children: [
3208
3499
  /* @__PURE__ */ (0, import_jsx_runtime30.jsx)(message_exports.If, { copied: true, children: /* @__PURE__ */ (0, import_jsx_runtime30.jsx)(import_lucide_react.CheckIcon, {}) }),
3209
3500
  /* @__PURE__ */ (0, import_jsx_runtime30.jsx)(message_exports.If, { copied: false, children: /* @__PURE__ */ (0, import_jsx_runtime30.jsx)(import_lucide_react.CopyIcon, {}) })
3210
- ] }) });
3501
+ ] }) }) });
3211
3502
  });
3212
3503
  AssistantActionBarCopy.displayName = "AssistantActionBarCopy";
3213
3504
  var AssistantActionBarReload = (0, import_react56.forwardRef)((props, ref) => {
@@ -3256,7 +3547,7 @@ var BranchPickerPrevious2 = (0, import_react57.forwardRef)((props, ref) => {
3256
3547
  branchPicker: { previous: { tooltip = "Previous" } = {} } = {}
3257
3548
  } = {}
3258
3549
  } = useThreadConfig();
3259
- return /* @__PURE__ */ (0, import_jsx_runtime31.jsx)(branchPicker_exports.Previous, { asChild: true, children: /* @__PURE__ */ (0, import_jsx_runtime31.jsx)(TooltipIconButton, { tooltip, ...props, ref, children: /* @__PURE__ */ (0, import_jsx_runtime31.jsx)(import_lucide_react2.ChevronLeftIcon, {}) }) });
3550
+ return /* @__PURE__ */ (0, import_jsx_runtime31.jsx)(branchPicker_exports.Previous, { asChild: true, children: /* @__PURE__ */ (0, import_jsx_runtime31.jsx)(TooltipIconButton, { tooltip, ...props, ref, children: props.children ?? /* @__PURE__ */ (0, import_jsx_runtime31.jsx)(import_lucide_react2.ChevronLeftIcon, {}) }) });
3260
3551
  });
3261
3552
  BranchPickerPrevious2.displayName = "BranchPickerPrevious";
3262
3553
  var BranchPickerStateWrapper = withDefaults("span", {
@@ -3274,7 +3565,7 @@ var BranchPickerNext = (0, import_react57.forwardRef)((props, ref) => {
3274
3565
  const {
3275
3566
  strings: { branchPicker: { next: { tooltip = "Next" } = {} } = {} } = {}
3276
3567
  } = useThreadConfig();
3277
- return /* @__PURE__ */ (0, import_jsx_runtime31.jsx)(branchPicker_exports.Next, { asChild: true, children: /* @__PURE__ */ (0, import_jsx_runtime31.jsx)(TooltipIconButton, { tooltip, ...props, ref, children: /* @__PURE__ */ (0, import_jsx_runtime31.jsx)(import_lucide_react2.ChevronRightIcon, {}) }) });
3568
+ return /* @__PURE__ */ (0, import_jsx_runtime31.jsx)(branchPicker_exports.Next, { asChild: true, children: /* @__PURE__ */ (0, import_jsx_runtime31.jsx)(TooltipIconButton, { tooltip, ...props, ref, children: props.children ?? /* @__PURE__ */ (0, import_jsx_runtime31.jsx)(import_lucide_react2.ChevronRightIcon, {}) }) });
3278
3569
  });
3279
3570
  BranchPickerNext.displayName = "BranchPickerNext";
3280
3571
  var exports3 = {
@@ -3449,7 +3740,7 @@ var ComposerSend = (0, import_react59.forwardRef)((props, ref) => {
3449
3740
  const {
3450
3741
  strings: { composer: { send: { tooltip = "Send" } = {} } = {} } = {}
3451
3742
  } = useThreadConfig();
3452
- return /* @__PURE__ */ (0, import_jsx_runtime36.jsx)(composer_exports.Send, { asChild: true, children: /* @__PURE__ */ (0, import_jsx_runtime36.jsx)(ComposerSendButton, { tooltip, ...props, ref, children: /* @__PURE__ */ (0, import_jsx_runtime36.jsx)(import_lucide_react3.SendHorizonalIcon, {}) }) });
3743
+ return /* @__PURE__ */ (0, import_jsx_runtime36.jsx)(composer_exports.Send, { asChild: true, children: /* @__PURE__ */ (0, import_jsx_runtime36.jsx)(ComposerSendButton, { tooltip, ...props, ref, children: props.children ?? /* @__PURE__ */ (0, import_jsx_runtime36.jsx)(import_lucide_react3.SendHorizontalIcon, {}) }) });
3453
3744
  });
3454
3745
  ComposerSend.displayName = "ComposerSend";
3455
3746
  var ComposerCancelButton = withDefaults(TooltipIconButton, {
@@ -3460,7 +3751,7 @@ var ComposerCancel = (0, import_react59.forwardRef)((props, ref) => {
3460
3751
  const {
3461
3752
  strings: { composer: { cancel: { tooltip = "Cancel" } = {} } = {} } = {}
3462
3753
  } = useThreadConfig();
3463
- return /* @__PURE__ */ (0, import_jsx_runtime36.jsx)(composer_exports.Cancel, { asChild: true, children: /* @__PURE__ */ (0, import_jsx_runtime36.jsx)(ComposerCancelButton, { tooltip, ...props, ref, children: /* @__PURE__ */ (0, import_jsx_runtime36.jsx)(CircleStopIcon, {}) }) });
3754
+ return /* @__PURE__ */ (0, import_jsx_runtime36.jsx)(composer_exports.Cancel, { asChild: true, children: /* @__PURE__ */ (0, import_jsx_runtime36.jsx)(ComposerCancelButton, { tooltip, ...props, ref, children: props.children ?? /* @__PURE__ */ (0, import_jsx_runtime36.jsx)(CircleStopIcon, {}) }) });
3464
3755
  });
3465
3756
  ComposerCancel.displayName = "ComposerCancel";
3466
3757
  var exports6 = {
@@ -3575,7 +3866,7 @@ var UserActionBarEdit = (0, import_react61.forwardRef)((props, ref) => {
3575
3866
  } = useThreadConfig();
3576
3867
  const allowEdit = useAllowEdit();
3577
3868
  if (!allowEdit) return null;
3578
- return /* @__PURE__ */ (0, import_jsx_runtime38.jsx)(actionBar_exports.Edit, { asChild: true, children: /* @__PURE__ */ (0, import_jsx_runtime38.jsx)(TooltipIconButton, { tooltip, ...props, ref, children: /* @__PURE__ */ (0, import_jsx_runtime38.jsx)(import_lucide_react4.PencilIcon, {}) }) });
3869
+ return /* @__PURE__ */ (0, import_jsx_runtime38.jsx)(actionBar_exports.Edit, { asChild: true, children: /* @__PURE__ */ (0, import_jsx_runtime38.jsx)(TooltipIconButton, { tooltip, ...props, ref, children: props.children ?? /* @__PURE__ */ (0, import_jsx_runtime38.jsx)(import_lucide_react4.PencilIcon, {}) }) });
3579
3870
  });
3580
3871
  UserActionBarEdit.displayName = "UserActionBarEdit";
3581
3872
  var exports8 = {
@@ -3653,7 +3944,7 @@ var EditComposerCancel = (0, import_react63.forwardRef)(
3653
3944
  editComposer: { cancel: { label = "Cancel" } = {} } = {}
3654
3945
  } = {}
3655
3946
  } = useThreadConfig();
3656
- return /* @__PURE__ */ (0, import_jsx_runtime40.jsx)(composer_exports.Cancel, { asChild: true, children: /* @__PURE__ */ (0, import_jsx_runtime40.jsx)(Button, { variant: "ghost", ...props, ref, children: label }) });
3947
+ return /* @__PURE__ */ (0, import_jsx_runtime40.jsx)(composer_exports.Cancel, { asChild: true, children: /* @__PURE__ */ (0, import_jsx_runtime40.jsx)(Button, { variant: "ghost", ...props, ref, children: props.children ?? label }) });
3657
3948
  }
3658
3949
  );
3659
3950
  EditComposerCancel.displayName = "EditComposerCancel";
@@ -3662,7 +3953,7 @@ var EditComposerSend = (0, import_react63.forwardRef)(
3662
3953
  const {
3663
3954
  strings: { editComposer: { send: { label = "Send" } = {} } = {} } = {}
3664
3955
  } = useThreadConfig();
3665
- return /* @__PURE__ */ (0, import_jsx_runtime40.jsx)(composer_exports.Send, { asChild: true, children: /* @__PURE__ */ (0, import_jsx_runtime40.jsx)(Button, { ...props, ref, children: label }) });
3956
+ return /* @__PURE__ */ (0, import_jsx_runtime40.jsx)(composer_exports.Send, { asChild: true, children: /* @__PURE__ */ (0, import_jsx_runtime40.jsx)(Button, { ...props, ref, children: props.children ?? label }) });
3666
3957
  }
3667
3958
  );
3668
3959
  EditComposerSend.displayName = "EditComposerSend";
@@ -3730,7 +4021,7 @@ var ThreadScrollToBottom = (0, import_react64.forwardRef)((props, ref) => {
3730
4021
  thread: { scrollToBottom: { tooltip = "Scroll to bottom" } = {} } = {}
3731
4022
  } = {}
3732
4023
  } = useThreadConfig();
3733
- return /* @__PURE__ */ (0, import_jsx_runtime41.jsx)(thread_exports.ScrollToBottom, { asChild: true, children: /* @__PURE__ */ (0, import_jsx_runtime41.jsx)(ThreadScrollToBottomIconButton, { tooltip, ...props, ref, children: /* @__PURE__ */ (0, import_jsx_runtime41.jsx)(import_lucide_react5.ArrowDownIcon, {}) }) });
4024
+ return /* @__PURE__ */ (0, import_jsx_runtime41.jsx)(thread_exports.ScrollToBottom, { asChild: true, children: /* @__PURE__ */ (0, import_jsx_runtime41.jsx)(ThreadScrollToBottomIconButton, { tooltip, ...props, ref, children: props.children ?? /* @__PURE__ */ (0, import_jsx_runtime41.jsx)(import_lucide_react5.ArrowDownIcon, {}) }) });
3734
4025
  });
3735
4026
  ThreadScrollToBottom.displayName = "ThreadScrollToBottom";
3736
4027
  var exports11 = {
@@ -3781,7 +4072,7 @@ var AssistantModalButton = (0, import_react65.forwardRef)(({ "data-state": state
3781
4072
  } = {}
3782
4073
  } = useThreadConfig();
3783
4074
  const tooltip = state === "open" ? openTooltip : closedTooltip;
3784
- return /* @__PURE__ */ (0, import_jsx_runtime42.jsxs)(
4075
+ return /* @__PURE__ */ (0, import_jsx_runtime42.jsx)(
3785
4076
  ModalButtonStyled,
3786
4077
  {
3787
4078
  side: "left",
@@ -3789,17 +4080,22 @@ var AssistantModalButton = (0, import_react65.forwardRef)(({ "data-state": state
3789
4080
  "data-state": state,
3790
4081
  ...rest,
3791
4082
  ref,
3792
- children: [
3793
- /* @__PURE__ */ (0, import_jsx_runtime42.jsx)(import_lucide_react6.BotIcon, { "data-state": state, className: "aui-modal-button-closed-icon" }),
4083
+ children: rest.children ?? /* @__PURE__ */ (0, import_jsx_runtime42.jsxs)(import_jsx_runtime42.Fragment, { children: [
4084
+ /* @__PURE__ */ (0, import_jsx_runtime42.jsx)(
4085
+ import_lucide_react6.BotIcon,
4086
+ {
4087
+ "data-state": state,
4088
+ className: "aui-modal-button-closed-icon"
4089
+ }
4090
+ ),
3794
4091
  /* @__PURE__ */ (0, import_jsx_runtime42.jsx)(
3795
4092
  import_lucide_react6.ChevronDownIcon,
3796
4093
  {
3797
4094
  "data-state": state,
3798
4095
  className: "aui-modal-button-open-icon"
3799
4096
  }
3800
- ),
3801
- /* @__PURE__ */ (0, import_jsx_runtime42.jsx)("span", { className: "aui-sr-only", children: tooltip })
3802
- ]
4097
+ )
4098
+ ] })
3803
4099
  }
3804
4100
  );
3805
4101
  });
@@ -3815,16 +4111,6 @@ var exports12 = {
3815
4111
  Content: AssistantModalContent
3816
4112
  };
3817
4113
  var assistant_modal_default = Object.assign(AssistantModal, exports12);
3818
-
3819
- // src/internal.ts
3820
- var internal_exports = {};
3821
- __export(internal_exports, {
3822
- BaseAssistantRuntime: () => BaseAssistantRuntime,
3823
- MessageRepository: () => MessageRepository,
3824
- ProxyConfigProvider: () => ProxyConfigProvider,
3825
- TooltipIconButton: () => TooltipIconButton,
3826
- useSmooth: () => useSmooth
3827
- });
3828
4114
  // Annotate the CommonJS export names for ESM import in node:
3829
4115
  0 && (module.exports = {
3830
4116
  ActionBarPrimitive,
@@ -3839,6 +4125,7 @@ __export(internal_exports, {
3839
4125
  ComposerPrimitive,
3840
4126
  ContentPart,
3841
4127
  ContentPartPrimitive,
4128
+ EdgeChatAdapter,
3842
4129
  EditComposer,
3843
4130
  INTERNAL,
3844
4131
  MessagePrimitive,
@@ -3848,8 +4135,11 @@ __export(internal_exports, {
3848
4135
  ThreadWelcome,
3849
4136
  UserActionBar,
3850
4137
  UserMessage,
4138
+ fromCoreMessages,
4139
+ fromLanguageModelMessages,
3851
4140
  makeAssistantTool,
3852
4141
  makeAssistantToolUI,
4142
+ toLanguageModelMessages,
3853
4143
  useActionBarCopy,
3854
4144
  useActionBarEdit,
3855
4145
  useActionBarReload,