@assistant-ui/react 0.4.4 → 0.4.6
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/edge.d.mts +109 -4
- package/dist/edge.d.ts +109 -4
- package/dist/edge.js +743 -15
- package/dist/edge.js.map +1 -1
- package/dist/edge.mjs +733 -15
- package/dist/edge.mjs.map +1 -1
- package/dist/index.d.mts +81 -31
- package/dist/index.d.ts +81 -31
- package/dist/index.js +267 -182
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +267 -182
- package/dist/index.mjs.map +1 -1
- package/dist/tailwindcss/index.d.mts +1 -1
- package/dist/tailwindcss/index.d.ts +1 -1
- package/dist/tailwindcss/index.js +7 -4
- package/dist/tailwindcss/index.js.map +1 -1
- package/dist/tailwindcss/index.mjs +7 -4
- package/dist/tailwindcss/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
@@ -29,6 +29,21 @@ function useAssistantContext(options) {
|
|
29
29
|
import { create } from "zustand";
|
30
30
|
|
31
31
|
// src/types/ModelConfigTypes.ts
|
32
|
+
import { z } from "zod";
|
33
|
+
var LanguageModelV1CallSettingsSchema = z.object({
|
34
|
+
maxTokens: z.number().int().positive().optional(),
|
35
|
+
temperature: z.number().optional(),
|
36
|
+
topP: z.number().optional(),
|
37
|
+
presencePenalty: z.number().optional(),
|
38
|
+
frequencyPenalty: z.number().optional(),
|
39
|
+
seed: z.number().int().optional(),
|
40
|
+
headers: z.record(z.string().optional()).optional()
|
41
|
+
});
|
42
|
+
var LanguageModelConfigSchema = z.object({
|
43
|
+
apiKey: z.string().optional(),
|
44
|
+
baseUrl: z.string().optional(),
|
45
|
+
modelName: z.string().optional()
|
46
|
+
});
|
32
47
|
var mergeModelConfigs = (configSet) => {
|
33
48
|
const configs = Array.from(configSet).map((c) => c.getModelConfig()).sort((a, b) => (b.priority ?? 0) - (a.priority ?? 0));
|
34
49
|
return configs.reduce((acc, config) => {
|
@@ -1902,7 +1917,7 @@ var ThreadPrimitiveSuggestion = createActionButton(
|
|
1902
1917
|
);
|
1903
1918
|
|
1904
1919
|
// src/runtimes/local/useLocalRuntime.tsx
|
1905
|
-
import { useInsertionEffect as useInsertionEffect3, useState as
|
1920
|
+
import { useInsertionEffect as useInsertionEffect3, useState as useState8 } from "react";
|
1906
1921
|
|
1907
1922
|
// src/utils/idUtils.tsx
|
1908
1923
|
import { customAlphabet } from "nanoid/non-secure";
|
@@ -2205,163 +2220,8 @@ var TooltipIconButton = forwardRef17(({ children, tooltip, side = "bottom", ...r
|
|
2205
2220
|
});
|
2206
2221
|
TooltipIconButton.displayName = "TooltipIconButton";
|
2207
2222
|
|
2208
|
-
// src/runtimes/local/LocalRuntime.tsx
|
2209
|
-
var LocalRuntime = class extends BaseAssistantRuntime {
|
2210
|
-
_proxyConfigProvider;
|
2211
|
-
constructor(adapter) {
|
2212
|
-
const proxyConfigProvider = new ProxyConfigProvider();
|
2213
|
-
super(new LocalThreadRuntime(proxyConfigProvider, adapter));
|
2214
|
-
this._proxyConfigProvider = proxyConfigProvider;
|
2215
|
-
}
|
2216
|
-
set adapter(adapter) {
|
2217
|
-
this.thread.adapter = adapter;
|
2218
|
-
}
|
2219
|
-
registerModelConfigProvider(provider) {
|
2220
|
-
return this._proxyConfigProvider.registerModelConfigProvider(provider);
|
2221
|
-
}
|
2222
|
-
switchToThread(threadId) {
|
2223
|
-
if (threadId) {
|
2224
|
-
throw new Error("LocalRuntime does not yet support switching threads");
|
2225
|
-
}
|
2226
|
-
return this.thread = new LocalThreadRuntime(
|
2227
|
-
this._proxyConfigProvider,
|
2228
|
-
this.thread.adapter
|
2229
|
-
);
|
2230
|
-
}
|
2231
|
-
};
|
2232
|
-
var CAPABILITIES = Object.freeze({
|
2233
|
-
edit: true,
|
2234
|
-
reload: true,
|
2235
|
-
cancel: true,
|
2236
|
-
copy: true
|
2237
|
-
});
|
2238
|
-
var LocalThreadRuntime = class {
|
2239
|
-
constructor(configProvider, adapter) {
|
2240
|
-
this.configProvider = configProvider;
|
2241
|
-
this.adapter = adapter;
|
2242
|
-
}
|
2243
|
-
_subscriptions = /* @__PURE__ */ new Set();
|
2244
|
-
abortController = null;
|
2245
|
-
repository = new MessageRepository();
|
2246
|
-
capabilities = CAPABILITIES;
|
2247
|
-
get messages() {
|
2248
|
-
return this.repository.getMessages();
|
2249
|
-
}
|
2250
|
-
get isRunning() {
|
2251
|
-
return this.abortController != null;
|
2252
|
-
}
|
2253
|
-
getBranches(messageId) {
|
2254
|
-
return this.repository.getBranches(messageId);
|
2255
|
-
}
|
2256
|
-
switchToBranch(branchId) {
|
2257
|
-
this.repository.switchToBranch(branchId);
|
2258
|
-
this.notifySubscribers();
|
2259
|
-
}
|
2260
|
-
async append(message) {
|
2261
|
-
if (message.role !== "user")
|
2262
|
-
throw new Error(
|
2263
|
-
"Only appending user messages are supported in LocalRuntime. This is likely an internal bug in assistant-ui."
|
2264
|
-
);
|
2265
|
-
const userMessageId = generateId();
|
2266
|
-
const userMessage = {
|
2267
|
-
id: userMessageId,
|
2268
|
-
role: "user",
|
2269
|
-
content: message.content,
|
2270
|
-
createdAt: /* @__PURE__ */ new Date()
|
2271
|
-
};
|
2272
|
-
this.repository.addOrUpdateMessage(message.parentId, userMessage);
|
2273
|
-
await this.startRun(userMessageId);
|
2274
|
-
}
|
2275
|
-
async startRun(parentId) {
|
2276
|
-
this.repository.resetHead(parentId);
|
2277
|
-
const messages = this.repository.getMessages();
|
2278
|
-
const message = {
|
2279
|
-
id: generateId(),
|
2280
|
-
role: "assistant",
|
2281
|
-
status: { type: "in_progress" },
|
2282
|
-
content: [{ type: "text", text: "" }],
|
2283
|
-
createdAt: /* @__PURE__ */ new Date()
|
2284
|
-
};
|
2285
|
-
this.repository.addOrUpdateMessage(parentId, { ...message });
|
2286
|
-
this.abortController?.abort();
|
2287
|
-
this.abortController = new AbortController();
|
2288
|
-
this.notifySubscribers();
|
2289
|
-
try {
|
2290
|
-
const updateHandler = ({ content }) => {
|
2291
|
-
message.content = content;
|
2292
|
-
this.repository.addOrUpdateMessage(parentId, { ...message });
|
2293
|
-
this.notifySubscribers();
|
2294
|
-
};
|
2295
|
-
const result = await this.adapter.run({
|
2296
|
-
messages,
|
2297
|
-
abortSignal: this.abortController.signal,
|
2298
|
-
config: this.configProvider.getModelConfig(),
|
2299
|
-
onUpdate: updateHandler
|
2300
|
-
});
|
2301
|
-
if (result !== void 0) {
|
2302
|
-
updateHandler(result);
|
2303
|
-
}
|
2304
|
-
if (result.status?.type === "in_progress")
|
2305
|
-
throw new Error(
|
2306
|
-
"Unexpected in_progress status returned from ChatModelAdapter"
|
2307
|
-
);
|
2308
|
-
message.status = result.status ?? { type: "done" };
|
2309
|
-
this.repository.addOrUpdateMessage(parentId, { ...message });
|
2310
|
-
} catch (e) {
|
2311
|
-
message.status = { type: "error", error: e };
|
2312
|
-
this.repository.addOrUpdateMessage(parentId, { ...message });
|
2313
|
-
throw e;
|
2314
|
-
} finally {
|
2315
|
-
this.abortController = null;
|
2316
|
-
this.notifySubscribers();
|
2317
|
-
}
|
2318
|
-
}
|
2319
|
-
cancelRun() {
|
2320
|
-
if (!this.abortController) return;
|
2321
|
-
this.abortController.abort();
|
2322
|
-
this.abortController = null;
|
2323
|
-
}
|
2324
|
-
notifySubscribers() {
|
2325
|
-
for (const callback of this._subscriptions) callback();
|
2326
|
-
}
|
2327
|
-
subscribe(callback) {
|
2328
|
-
this._subscriptions.add(callback);
|
2329
|
-
return () => this._subscriptions.delete(callback);
|
2330
|
-
}
|
2331
|
-
addToolResult({ messageId, toolCallId, result }) {
|
2332
|
-
const { parentId, message } = this.repository.getMessage(messageId);
|
2333
|
-
if (message.role !== "assistant")
|
2334
|
-
throw new Error("Tried to add tool result to non-assistant message");
|
2335
|
-
let found = false;
|
2336
|
-
const newContent = message.content.map((c) => {
|
2337
|
-
if (c.type !== "tool-call") return c;
|
2338
|
-
if (c.toolCallId !== toolCallId) return c;
|
2339
|
-
found = true;
|
2340
|
-
return {
|
2341
|
-
...c,
|
2342
|
-
result
|
2343
|
-
};
|
2344
|
-
});
|
2345
|
-
if (!found)
|
2346
|
-
throw new Error("Tried to add tool result to non-existing tool call");
|
2347
|
-
this.repository.addOrUpdateMessage(parentId, {
|
2348
|
-
...message,
|
2349
|
-
content: newContent
|
2350
|
-
});
|
2351
|
-
}
|
2352
|
-
};
|
2353
|
-
|
2354
|
-
// src/runtimes/local/useLocalRuntime.tsx
|
2355
|
-
var useLocalRuntime = (adapter) => {
|
2356
|
-
const [runtime] = useState7(() => new LocalRuntime(adapter));
|
2357
|
-
useInsertionEffect3(() => {
|
2358
|
-
runtime.adapter = adapter;
|
2359
|
-
});
|
2360
|
-
return runtime;
|
2361
|
-
};
|
2362
|
-
|
2363
2223
|
// src/runtimes/edge/useEdgeRuntime.ts
|
2364
|
-
import { useState as
|
2224
|
+
import { useState as useState7 } from "react";
|
2365
2225
|
|
2366
2226
|
// src/runtimes/edge/converters/toCoreMessages.ts
|
2367
2227
|
var toCoreMessages = (message) => {
|
@@ -2370,6 +2230,10 @@ var toCoreMessages = (message) => {
|
|
2370
2230
|
role: message2.role,
|
2371
2231
|
content: message2.content.map((part) => {
|
2372
2232
|
if (part.type === "ui") throw new Error("UI parts are not supported");
|
2233
|
+
if (part.type === "tool-call") {
|
2234
|
+
const { argsText, ...rest } = part;
|
2235
|
+
return rest;
|
2236
|
+
}
|
2373
2237
|
return part;
|
2374
2238
|
})
|
2375
2239
|
};
|
@@ -2377,7 +2241,7 @@ var toCoreMessages = (message) => {
|
|
2377
2241
|
};
|
2378
2242
|
|
2379
2243
|
// src/runtimes/edge/converters/toLanguageModelTools.ts
|
2380
|
-
import { z } from "zod";
|
2244
|
+
import { z as z2 } from "zod";
|
2381
2245
|
import zodToJsonSchema from "zod-to-json-schema";
|
2382
2246
|
var toLanguageModelTools = (tools) => {
|
2383
2247
|
if (!tools) return [];
|
@@ -2385,12 +2249,13 @@ var toLanguageModelTools = (tools) => {
|
|
2385
2249
|
type: "function",
|
2386
2250
|
name,
|
2387
2251
|
...tool.description ? { description: tool.description } : void 0,
|
2388
|
-
parameters: tool.parameters instanceof
|
2252
|
+
parameters: tool.parameters instanceof z2.ZodType ? zodToJsonSchema(tool.parameters) : tool.parameters
|
2389
2253
|
}));
|
2390
2254
|
};
|
2391
2255
|
|
2392
2256
|
// src/runtimes/edge/streams/assistantDecoderStream.ts
|
2393
2257
|
function assistantDecoderStream() {
|
2258
|
+
const toolCallNames = /* @__PURE__ */ new Map();
|
2394
2259
|
let currentToolCall;
|
2395
2260
|
return new TransformStream({
|
2396
2261
|
transform(chunk, controller) {
|
@@ -2415,6 +2280,7 @@ function assistantDecoderStream() {
|
|
2415
2280
|
}
|
2416
2281
|
case "1" /* ToolCallBegin */: {
|
2417
2282
|
const { id, name } = value;
|
2283
|
+
toolCallNames.set(id, name);
|
2418
2284
|
currentToolCall = { id, name, argsText: "" };
|
2419
2285
|
break;
|
2420
2286
|
}
|
@@ -2430,6 +2296,16 @@ function assistantDecoderStream() {
|
|
2430
2296
|
});
|
2431
2297
|
break;
|
2432
2298
|
}
|
2299
|
+
case "3" /* ToolCallResult */: {
|
2300
|
+
controller.enqueue({
|
2301
|
+
type: "tool-result",
|
2302
|
+
toolCallType: "function",
|
2303
|
+
toolCallId: value.id,
|
2304
|
+
toolName: toolCallNames.get(value.id),
|
2305
|
+
result: value.result
|
2306
|
+
});
|
2307
|
+
break;
|
2308
|
+
}
|
2433
2309
|
case "F" /* Finish */: {
|
2434
2310
|
controller.enqueue({
|
2435
2311
|
type: "finish",
|
@@ -2816,9 +2692,9 @@ var parsePartialJson = (json) => {
|
|
2816
2692
|
};
|
2817
2693
|
|
2818
2694
|
// src/runtimes/edge/streams/runResultStream.ts
|
2819
|
-
function runResultStream() {
|
2695
|
+
function runResultStream(initialContent) {
|
2820
2696
|
let message = {
|
2821
|
-
content:
|
2697
|
+
content: initialContent
|
2822
2698
|
};
|
2823
2699
|
const currentToolCall = { toolCallId: "", argsText: "" };
|
2824
2700
|
return new TransformStream({
|
@@ -2950,7 +2826,7 @@ var appendOrUpdateFinish = (message, chunk) => {
|
|
2950
2826
|
};
|
2951
2827
|
|
2952
2828
|
// src/runtimes/edge/streams/toolResultStream.ts
|
2953
|
-
import { z as
|
2829
|
+
import { z as z3 } from "zod";
|
2954
2830
|
import sjson2 from "secure-json-parse";
|
2955
2831
|
function toolResultStream(tools) {
|
2956
2832
|
const toolCallExecutions = /* @__PURE__ */ new Map();
|
@@ -2964,12 +2840,16 @@ function toolResultStream(tools) {
|
|
2964
2840
|
const tool = tools?.[toolName];
|
2965
2841
|
if (!tool || !tool.execute) return;
|
2966
2842
|
const args = sjson2.parse(argsText);
|
2967
|
-
if (tool.parameters instanceof
|
2843
|
+
if (tool.parameters instanceof z3.ZodType) {
|
2968
2844
|
const result = tool.parameters.safeParse(args);
|
2969
2845
|
if (!result.success) {
|
2970
2846
|
controller.enqueue({
|
2971
|
-
type: "
|
2972
|
-
|
2847
|
+
type: "tool-result",
|
2848
|
+
toolCallType,
|
2849
|
+
toolCallId,
|
2850
|
+
toolName,
|
2851
|
+
result: "Function parameter validation failed. " + JSON.stringify(result.error.issues),
|
2852
|
+
isError: true
|
2973
2853
|
});
|
2974
2854
|
return;
|
2975
2855
|
} else {
|
@@ -2986,9 +2866,14 @@ function toolResultStream(tools) {
|
|
2986
2866
|
result: result2
|
2987
2867
|
});
|
2988
2868
|
} catch (error) {
|
2869
|
+
console.error("Error: ", error);
|
2989
2870
|
controller.enqueue({
|
2990
|
-
type: "
|
2991
|
-
|
2871
|
+
type: "tool-result",
|
2872
|
+
toolCallType,
|
2873
|
+
toolCallId,
|
2874
|
+
toolName,
|
2875
|
+
result: "Error: " + error,
|
2876
|
+
isError: true
|
2992
2877
|
});
|
2993
2878
|
} finally {
|
2994
2879
|
toolCallExecutions.delete(toolCallId);
|
@@ -3001,6 +2886,7 @@ function toolResultStream(tools) {
|
|
3001
2886
|
}
|
3002
2887
|
case "text-delta":
|
3003
2888
|
case "tool-call-delta":
|
2889
|
+
case "tool-result":
|
3004
2890
|
case "finish":
|
3005
2891
|
case "error":
|
3006
2892
|
break;
|
@@ -3034,7 +2920,7 @@ var EdgeChatAdapter = class {
|
|
3034
2920
|
constructor(options) {
|
3035
2921
|
this.options = options;
|
3036
2922
|
}
|
3037
|
-
async
|
2923
|
+
async roundtrip(initialContent, { messages, abortSignal, config, onUpdate }) {
|
3038
2924
|
const result = await fetch(this.options.api, {
|
3039
2925
|
method: "POST",
|
3040
2926
|
headers: {
|
@@ -3045,25 +2931,62 @@ var EdgeChatAdapter = class {
|
|
3045
2931
|
messages: toCoreMessages(messages),
|
3046
2932
|
tools: toLanguageModelTools(
|
3047
2933
|
config.tools
|
3048
|
-
)
|
2934
|
+
),
|
2935
|
+
...config.callSettings,
|
2936
|
+
...config.config
|
3049
2937
|
}),
|
3050
2938
|
signal: abortSignal
|
3051
2939
|
});
|
3052
|
-
const stream = result.body.pipeThrough(new TextDecoderStream()).pipeThrough(chunkByLineStream()).pipeThrough(assistantDecoderStream()).pipeThrough(toolResultStream(config.tools)).pipeThrough(runResultStream());
|
2940
|
+
const stream = result.body.pipeThrough(new TextDecoderStream()).pipeThrough(chunkByLineStream()).pipeThrough(assistantDecoderStream()).pipeThrough(toolResultStream(config.tools)).pipeThrough(runResultStream(initialContent));
|
2941
|
+
let message;
|
3053
2942
|
let update;
|
3054
2943
|
for await (update of asAsyncIterable(stream)) {
|
3055
|
-
onUpdate(update);
|
2944
|
+
message = onUpdate(update);
|
3056
2945
|
}
|
3057
2946
|
if (update === void 0)
|
3058
2947
|
throw new Error("No data received from Edge Runtime");
|
3059
|
-
return update;
|
2948
|
+
return [message, update];
|
2949
|
+
}
|
2950
|
+
async run({ messages, abortSignal, config, onUpdate }) {
|
2951
|
+
let roundtripAllowance = this.options.maxToolRoundtrips ?? 1;
|
2952
|
+
let usage = {
|
2953
|
+
promptTokens: 0,
|
2954
|
+
completionTokens: 0
|
2955
|
+
};
|
2956
|
+
let result;
|
2957
|
+
let assistantMessage;
|
2958
|
+
do {
|
2959
|
+
[assistantMessage, result] = await this.roundtrip(result?.content ?? [], {
|
2960
|
+
messages: assistantMessage ? [...messages, assistantMessage] : messages,
|
2961
|
+
abortSignal,
|
2962
|
+
config,
|
2963
|
+
onUpdate
|
2964
|
+
});
|
2965
|
+
if (result.status?.type === "done") {
|
2966
|
+
usage.promptTokens += result.status.usage?.promptTokens ?? 0;
|
2967
|
+
usage.completionTokens += result.status.usage?.completionTokens ?? 0;
|
2968
|
+
}
|
2969
|
+
} while (result.status?.type === "done" && result.status.finishReason === "tool-calls" && result.content.every((c) => c.type !== "tool-call" || !!c.result) && roundtripAllowance-- > 0);
|
2970
|
+
if (result.status?.type === "done" && usage.promptTokens > 0) {
|
2971
|
+
result = {
|
2972
|
+
...result,
|
2973
|
+
status: {
|
2974
|
+
...result.status,
|
2975
|
+
usage
|
2976
|
+
}
|
2977
|
+
};
|
2978
|
+
}
|
2979
|
+
return result;
|
3060
2980
|
}
|
3061
2981
|
};
|
3062
2982
|
|
3063
2983
|
// src/runtimes/edge/useEdgeRuntime.ts
|
3064
|
-
var useEdgeRuntime = (
|
3065
|
-
|
3066
|
-
|
2984
|
+
var useEdgeRuntime = ({
|
2985
|
+
initialMessages,
|
2986
|
+
...options
|
2987
|
+
}) => {
|
2988
|
+
const [adapter] = useState7(() => new EdgeChatAdapter(options));
|
2989
|
+
return useLocalRuntime(adapter, { initialMessages });
|
3067
2990
|
};
|
3068
2991
|
|
3069
2992
|
// src/runtimes/edge/converters/toLanguageModelMessages.ts
|
@@ -3238,7 +3161,7 @@ var fromLanguageModelMessages = (lm, mergeRoundtrips) => {
|
|
3238
3161
|
toolCallId: part.toolCallId,
|
3239
3162
|
toolName: part.toolName,
|
3240
3163
|
argsText: JSON.stringify(part.args),
|
3241
|
-
args:
|
3164
|
+
args: part.args
|
3242
3165
|
};
|
3243
3166
|
}
|
3244
3167
|
return part;
|
@@ -3290,16 +3213,181 @@ var fromLanguageModelMessages = (lm, mergeRoundtrips) => {
|
|
3290
3213
|
var fromCoreMessages = (message) => {
|
3291
3214
|
return message.map((message2) => {
|
3292
3215
|
return {
|
3293
|
-
...message2,
|
3294
3216
|
id: generateId(),
|
3295
3217
|
createdAt: /* @__PURE__ */ new Date(),
|
3296
3218
|
...message2.role === "assistant" ? {
|
3297
3219
|
status: { type: "done" }
|
3298
|
-
} : void 0
|
3220
|
+
} : void 0,
|
3221
|
+
...message2
|
3299
3222
|
};
|
3300
3223
|
});
|
3301
3224
|
};
|
3302
3225
|
|
3226
|
+
// src/runtimes/local/LocalRuntime.tsx
|
3227
|
+
var LocalRuntime = class extends BaseAssistantRuntime {
|
3228
|
+
_proxyConfigProvider;
|
3229
|
+
constructor(adapter, options) {
|
3230
|
+
const proxyConfigProvider = new ProxyConfigProvider();
|
3231
|
+
super(new LocalThreadRuntime(proxyConfigProvider, adapter, options));
|
3232
|
+
this._proxyConfigProvider = proxyConfigProvider;
|
3233
|
+
}
|
3234
|
+
set adapter(adapter) {
|
3235
|
+
this.thread.adapter = adapter;
|
3236
|
+
}
|
3237
|
+
registerModelConfigProvider(provider) {
|
3238
|
+
return this._proxyConfigProvider.registerModelConfigProvider(provider);
|
3239
|
+
}
|
3240
|
+
switchToThread(threadId) {
|
3241
|
+
if (threadId) {
|
3242
|
+
throw new Error("LocalRuntime does not yet support switching threads");
|
3243
|
+
}
|
3244
|
+
return this.thread = new LocalThreadRuntime(
|
3245
|
+
this._proxyConfigProvider,
|
3246
|
+
this.thread.adapter
|
3247
|
+
);
|
3248
|
+
}
|
3249
|
+
};
|
3250
|
+
var CAPABILITIES = Object.freeze({
|
3251
|
+
edit: true,
|
3252
|
+
reload: true,
|
3253
|
+
cancel: true,
|
3254
|
+
copy: true
|
3255
|
+
});
|
3256
|
+
var LocalThreadRuntime = class {
|
3257
|
+
constructor(configProvider, adapter, options) {
|
3258
|
+
this.configProvider = configProvider;
|
3259
|
+
this.adapter = adapter;
|
3260
|
+
if (options?.initialMessages) {
|
3261
|
+
let parentId = null;
|
3262
|
+
const messages = fromCoreMessages(options.initialMessages);
|
3263
|
+
for (const message of messages) {
|
3264
|
+
this.repository.addOrUpdateMessage(parentId, message);
|
3265
|
+
parentId = message.id;
|
3266
|
+
}
|
3267
|
+
}
|
3268
|
+
}
|
3269
|
+
_subscriptions = /* @__PURE__ */ new Set();
|
3270
|
+
abortController = null;
|
3271
|
+
repository = new MessageRepository();
|
3272
|
+
capabilities = CAPABILITIES;
|
3273
|
+
get messages() {
|
3274
|
+
return this.repository.getMessages();
|
3275
|
+
}
|
3276
|
+
get isRunning() {
|
3277
|
+
return this.abortController != null;
|
3278
|
+
}
|
3279
|
+
getBranches(messageId) {
|
3280
|
+
return this.repository.getBranches(messageId);
|
3281
|
+
}
|
3282
|
+
switchToBranch(branchId) {
|
3283
|
+
this.repository.switchToBranch(branchId);
|
3284
|
+
this.notifySubscribers();
|
3285
|
+
}
|
3286
|
+
async append(message) {
|
3287
|
+
if (message.role !== "user")
|
3288
|
+
throw new Error(
|
3289
|
+
"Only appending user messages are supported in LocalRuntime. This is likely an internal bug in assistant-ui."
|
3290
|
+
);
|
3291
|
+
const userMessageId = generateId();
|
3292
|
+
const userMessage = {
|
3293
|
+
id: userMessageId,
|
3294
|
+
role: "user",
|
3295
|
+
content: message.content,
|
3296
|
+
createdAt: /* @__PURE__ */ new Date()
|
3297
|
+
};
|
3298
|
+
this.repository.addOrUpdateMessage(message.parentId, userMessage);
|
3299
|
+
await this.startRun(userMessageId);
|
3300
|
+
}
|
3301
|
+
async startRun(parentId) {
|
3302
|
+
this.repository.resetHead(parentId);
|
3303
|
+
const messages = this.repository.getMessages();
|
3304
|
+
let message = {
|
3305
|
+
id: generateId(),
|
3306
|
+
role: "assistant",
|
3307
|
+
status: { type: "in_progress" },
|
3308
|
+
content: [{ type: "text", text: "" }],
|
3309
|
+
createdAt: /* @__PURE__ */ new Date()
|
3310
|
+
};
|
3311
|
+
this.abortController?.abort();
|
3312
|
+
this.abortController = new AbortController();
|
3313
|
+
this.repository.addOrUpdateMessage(parentId, { ...message });
|
3314
|
+
this.notifySubscribers();
|
3315
|
+
const updateMessage = (m) => {
|
3316
|
+
message = {
|
3317
|
+
...message,
|
3318
|
+
...m
|
3319
|
+
};
|
3320
|
+
this.repository.addOrUpdateMessage(parentId, message);
|
3321
|
+
this.notifySubscribers();
|
3322
|
+
return message;
|
3323
|
+
};
|
3324
|
+
try {
|
3325
|
+
const result = await this.adapter.run({
|
3326
|
+
messages,
|
3327
|
+
abortSignal: this.abortController.signal,
|
3328
|
+
config: this.configProvider.getModelConfig(),
|
3329
|
+
onUpdate: updateMessage
|
3330
|
+
});
|
3331
|
+
if (result.status?.type === "in_progress")
|
3332
|
+
throw new Error(
|
3333
|
+
"Unexpected in_progress status returned from ChatModelAdapter"
|
3334
|
+
);
|
3335
|
+
this.abortController = null;
|
3336
|
+
updateMessage({ status: { type: "done" }, ...result });
|
3337
|
+
this.repository.addOrUpdateMessage(parentId, { ...message });
|
3338
|
+
} catch (e) {
|
3339
|
+
const isAbortError = e instanceof Error && e.name === "AbortError";
|
3340
|
+
this.abortController = null;
|
3341
|
+
updateMessage({
|
3342
|
+
status: isAbortError ? { type: "cancelled" } : { type: "error", error: e }
|
3343
|
+
});
|
3344
|
+
if (!isAbortError) throw e;
|
3345
|
+
}
|
3346
|
+
}
|
3347
|
+
cancelRun() {
|
3348
|
+
if (!this.abortController) return;
|
3349
|
+
this.abortController.abort();
|
3350
|
+
this.abortController = null;
|
3351
|
+
}
|
3352
|
+
notifySubscribers() {
|
3353
|
+
for (const callback of this._subscriptions) callback();
|
3354
|
+
}
|
3355
|
+
subscribe(callback) {
|
3356
|
+
this._subscriptions.add(callback);
|
3357
|
+
return () => this._subscriptions.delete(callback);
|
3358
|
+
}
|
3359
|
+
addToolResult({ messageId, toolCallId, result }) {
|
3360
|
+
const { parentId, message } = this.repository.getMessage(messageId);
|
3361
|
+
if (message.role !== "assistant")
|
3362
|
+
throw new Error("Tried to add tool result to non-assistant message");
|
3363
|
+
let found = false;
|
3364
|
+
const newContent = message.content.map((c) => {
|
3365
|
+
if (c.type !== "tool-call") return c;
|
3366
|
+
if (c.toolCallId !== toolCallId) return c;
|
3367
|
+
found = true;
|
3368
|
+
return {
|
3369
|
+
...c,
|
3370
|
+
result
|
3371
|
+
};
|
3372
|
+
});
|
3373
|
+
if (!found)
|
3374
|
+
throw new Error("Tried to add tool result to non-existing tool call");
|
3375
|
+
this.repository.addOrUpdateMessage(parentId, {
|
3376
|
+
...message,
|
3377
|
+
content: newContent
|
3378
|
+
});
|
3379
|
+
}
|
3380
|
+
};
|
3381
|
+
|
3382
|
+
// src/runtimes/local/useLocalRuntime.tsx
|
3383
|
+
var useLocalRuntime = (adapter, options) => {
|
3384
|
+
const [runtime] = useState8(() => new LocalRuntime(adapter, options));
|
3385
|
+
useInsertionEffect3(() => {
|
3386
|
+
runtime.adapter = adapter;
|
3387
|
+
});
|
3388
|
+
return runtime;
|
3389
|
+
};
|
3390
|
+
|
3303
3391
|
// src/ui/thread-config.tsx
|
3304
3392
|
import { createContext as createContext5, useContext as useContext5 } from "react";
|
3305
3393
|
import { Fragment as Fragment3, jsx as jsx29 } from "react/jsx-runtime";
|
@@ -3895,10 +3983,7 @@ var ThreadScrollToBottom = forwardRef26((props, ref) => {
|
|
3895
3983
|
thread: { scrollToBottom: { tooltip = "Scroll to bottom" } = {} } = {}
|
3896
3984
|
} = {}
|
3897
3985
|
} = useThreadConfig();
|
3898
|
-
return /* @__PURE__ */ jsx41(thread_exports.ScrollToBottom, { asChild: true, children: /* @__PURE__ */
|
3899
|
-
"|",
|
3900
|
-
props.children ?? /* @__PURE__ */ jsx41(ArrowDownIcon, {})
|
3901
|
-
] }) });
|
3986
|
+
return /* @__PURE__ */ jsx41(thread_exports.ScrollToBottom, { asChild: true, children: /* @__PURE__ */ jsx41(ThreadScrollToBottomIconButton, { tooltip, ...props, ref, children: props.children ?? /* @__PURE__ */ jsx41(ArrowDownIcon, {}) }) });
|
3902
3987
|
});
|
3903
3988
|
ThreadScrollToBottom.displayName = "ThreadScrollToBottom";
|
3904
3989
|
var exports10 = {
|