@assistant-ui/react 0.11.6 → 0.11.8
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/legacy-runtime/runtime/ThreadRuntime.d.ts +8 -1
- package/dist/legacy-runtime/runtime/ThreadRuntime.d.ts.map +1 -1
- package/dist/legacy-runtime/runtime/ThreadRuntime.js +5 -1
- package/dist/legacy-runtime/runtime/ThreadRuntime.js.map +1 -1
- package/dist/legacy-runtime/runtime-cores/assistant-transport/commandQueue.d.ts +12 -0
- package/dist/legacy-runtime/runtime-cores/assistant-transport/commandQueue.d.ts.map +1 -0
- package/dist/legacy-runtime/runtime-cores/assistant-transport/commandQueue.js +52 -0
- package/dist/legacy-runtime/runtime-cores/assistant-transport/commandQueue.js.map +1 -0
- package/dist/legacy-runtime/runtime-cores/assistant-transport/index.d.ts +3 -0
- package/dist/legacy-runtime/runtime-cores/assistant-transport/index.d.ts.map +1 -0
- package/dist/legacy-runtime/runtime-cores/assistant-transport/index.js +6 -0
- package/dist/legacy-runtime/runtime-cores/assistant-transport/index.js.map +1 -0
- package/dist/legacy-runtime/runtime-cores/assistant-transport/runManager.d.ts +11 -0
- package/dist/legacy-runtime/runtime-cores/assistant-transport/runManager.d.ts.map +1 -0
- package/dist/legacy-runtime/runtime-cores/assistant-transport/runManager.js +55 -0
- package/dist/legacy-runtime/runtime-cores/assistant-transport/runManager.js.map +1 -0
- package/dist/legacy-runtime/runtime-cores/assistant-transport/types.d.ts +66 -0
- package/dist/legacy-runtime/runtime-cores/assistant-transport/types.d.ts.map +1 -0
- package/dist/legacy-runtime/runtime-cores/assistant-transport/types.js +1 -0
- package/dist/legacy-runtime/runtime-cores/assistant-transport/types.js.map +1 -0
- package/dist/legacy-runtime/runtime-cores/assistant-transport/useAssistantTransportRuntime.d.ts +7 -0
- package/dist/legacy-runtime/runtime-cores/assistant-transport/useAssistantTransportRuntime.d.ts.map +1 -0
- package/dist/legacy-runtime/runtime-cores/assistant-transport/useAssistantTransportRuntime.js +191 -0
- package/dist/legacy-runtime/runtime-cores/assistant-transport/useAssistantTransportRuntime.js.map +1 -0
- package/dist/legacy-runtime/runtime-cores/assistant-transport/useConvertedState.d.ts +3 -0
- package/dist/legacy-runtime/runtime-cores/assistant-transport/useConvertedState.d.ts.map +1 -0
- package/dist/legacy-runtime/runtime-cores/assistant-transport/useConvertedState.js +12 -0
- package/dist/legacy-runtime/runtime-cores/assistant-transport/useConvertedState.js.map +1 -0
- package/dist/legacy-runtime/runtime-cores/assistant-transport/useLatestRef.d.ts +4 -0
- package/dist/legacy-runtime/runtime-cores/assistant-transport/useLatestRef.d.ts.map +1 -0
- package/dist/legacy-runtime/runtime-cores/assistant-transport/useLatestRef.js +13 -0
- package/dist/legacy-runtime/runtime-cores/assistant-transport/useLatestRef.js.map +1 -0
- package/dist/legacy-runtime/runtime-cores/assistant-transport/useToolInvocations.d.ts +13 -0
- package/dist/legacy-runtime/runtime-cores/assistant-transport/useToolInvocations.d.ts.map +1 -0
- package/dist/legacy-runtime/runtime-cores/assistant-transport/useToolInvocations.js +138 -0
- package/dist/legacy-runtime/runtime-cores/assistant-transport/useToolInvocations.js.map +1 -0
- package/dist/legacy-runtime/runtime-cores/assistant-transport/utils.d.ts +13 -0
- package/dist/legacy-runtime/runtime-cores/assistant-transport/utils.d.ts.map +1 -0
- package/dist/legacy-runtime/runtime-cores/assistant-transport/utils.js +32 -0
- package/dist/legacy-runtime/runtime-cores/assistant-transport/utils.js.map +1 -0
- package/dist/legacy-runtime/runtime-cores/core/BaseThreadRuntimeCore.d.ts +1 -0
- package/dist/legacy-runtime/runtime-cores/core/BaseThreadRuntimeCore.d.ts.map +1 -1
- package/dist/legacy-runtime/runtime-cores/core/BaseThreadRuntimeCore.js.map +1 -1
- package/dist/legacy-runtime/runtime-cores/core/ThreadRuntimeCore.d.ts +2 -1
- package/dist/legacy-runtime/runtime-cores/core/ThreadRuntimeCore.d.ts.map +1 -1
- package/dist/legacy-runtime/runtime-cores/external-store/ExternalStoreAdapter.d.ts +3 -1
- package/dist/legacy-runtime/runtime-cores/external-store/ExternalStoreAdapter.d.ts.map +1 -1
- package/dist/legacy-runtime/runtime-cores/external-store/ExternalStoreThreadRuntimeCore.d.ts +3 -2
- package/dist/legacy-runtime/runtime-cores/external-store/ExternalStoreThreadRuntimeCore.d.ts.map +1 -1
- package/dist/legacy-runtime/runtime-cores/external-store/ExternalStoreThreadRuntimeCore.js +9 -2
- package/dist/legacy-runtime/runtime-cores/external-store/ExternalStoreThreadRuntimeCore.js.map +1 -1
- package/dist/legacy-runtime/runtime-cores/external-store/ThreadMessageLike.d.ts.map +1 -1
- package/dist/legacy-runtime/runtime-cores/external-store/ThreadMessageLike.js +2 -2
- package/dist/legacy-runtime/runtime-cores/external-store/ThreadMessageLike.js.map +1 -1
- package/dist/legacy-runtime/runtime-cores/index.d.ts +1 -0
- package/dist/legacy-runtime/runtime-cores/index.d.ts.map +1 -1
- package/dist/legacy-runtime/runtime-cores/index.js +1 -0
- package/dist/legacy-runtime/runtime-cores/index.js.map +1 -1
- package/dist/legacy-runtime/runtime-cores/local/LocalThreadRuntimeCore.d.ts +1 -0
- package/dist/legacy-runtime/runtime-cores/local/LocalThreadRuntimeCore.d.ts.map +1 -1
- package/dist/legacy-runtime/runtime-cores/local/LocalThreadRuntimeCore.js +5 -0
- package/dist/legacy-runtime/runtime-cores/local/LocalThreadRuntimeCore.js.map +1 -1
- package/dist/legacy-runtime/runtime-cores/remote-thread-list/EMPTY_THREAD_CORE.d.ts.map +1 -1
- package/dist/legacy-runtime/runtime-cores/remote-thread-list/EMPTY_THREAD_CORE.js +3 -0
- package/dist/legacy-runtime/runtime-cores/remote-thread-list/EMPTY_THREAD_CORE.js.map +1 -1
- package/dist/legacy-runtime/runtime-cores/remote-thread-list/RemoteThreadListHookInstanceManager.d.ts +2 -0
- package/dist/legacy-runtime/runtime-cores/remote-thread-list/RemoteThreadListHookInstanceManager.d.ts.map +1 -1
- package/dist/legacy-runtime/runtime-cores/remote-thread-list/RemoteThreadListThreadListRuntimeCore.d.ts +2 -0
- package/dist/legacy-runtime/runtime-cores/remote-thread-list/RemoteThreadListThreadListRuntimeCore.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/legacy-runtime/runtime/ThreadRuntime.ts +14 -2
- package/src/legacy-runtime/runtime-cores/assistant-transport/commandQueue.ts +62 -0
- package/src/legacy-runtime/runtime-cores/assistant-transport/index.ts +2 -0
- package/src/legacy-runtime/runtime-cores/assistant-transport/runManager.ts +67 -0
- package/src/legacy-runtime/runtime-cores/assistant-transport/types.ts +96 -0
- package/src/legacy-runtime/runtime-cores/assistant-transport/useAssistantTransport.spec.md +125 -0
- package/src/legacy-runtime/runtime-cores/assistant-transport/useAssistantTransportRuntime.tsx +239 -0
- package/src/legacy-runtime/runtime-cores/assistant-transport/useConvertedState.ts +18 -0
- package/src/legacy-runtime/runtime-cores/assistant-transport/useLatestRef.ts +9 -0
- package/src/legacy-runtime/runtime-cores/assistant-transport/useToolInvocations.ts +173 -0
- package/src/legacy-runtime/runtime-cores/assistant-transport/utils.ts +42 -0
- package/src/legacy-runtime/runtime-cores/core/BaseThreadRuntimeCore.tsx +1 -0
- package/src/legacy-runtime/runtime-cores/core/ThreadRuntimeCore.tsx +2 -1
- package/src/legacy-runtime/runtime-cores/external-store/ExternalStoreAdapter.tsx +3 -0
- package/src/legacy-runtime/runtime-cores/external-store/ExternalStoreThreadRuntimeCore.tsx +13 -2
- package/src/legacy-runtime/runtime-cores/external-store/ThreadMessageLike.tsx +2 -5
- package/src/legacy-runtime/runtime-cores/index.ts +1 -0
- package/src/legacy-runtime/runtime-cores/local/LocalThreadRuntimeCore.tsx +6 -0
- package/src/legacy-runtime/runtime-cores/remote-thread-list/EMPTY_THREAD_CORE.tsx +4 -0
@@ -0,0 +1,173 @@
|
|
1
|
+
import { useEffect, useRef, useState } from "react";
|
2
|
+
import {
|
3
|
+
createAssistantStreamController,
|
4
|
+
ToolCallStreamController,
|
5
|
+
ToolResponse,
|
6
|
+
unstable_toolResultStream,
|
7
|
+
type Tool,
|
8
|
+
} from "assistant-stream";
|
9
|
+
import type {
|
10
|
+
AssistantTransportCommand,
|
11
|
+
AssistantTransportState,
|
12
|
+
} from "./types";
|
13
|
+
import {
|
14
|
+
AssistantMetaTransformStream,
|
15
|
+
ReadonlyJSONValue,
|
16
|
+
} from "assistant-stream/utils";
|
17
|
+
|
18
|
+
const isArgsTextComplete = (argsText: string) => {
|
19
|
+
try {
|
20
|
+
JSON.parse(argsText);
|
21
|
+
return true;
|
22
|
+
} catch {
|
23
|
+
return false;
|
24
|
+
}
|
25
|
+
};
|
26
|
+
|
27
|
+
type UseToolInvocationsParams = {
|
28
|
+
state: AssistantTransportState;
|
29
|
+
getTools: () => Record<string, Tool> | undefined;
|
30
|
+
onResult: (command: AssistantTransportCommand) => void;
|
31
|
+
};
|
32
|
+
|
33
|
+
export function useToolInvocations({
|
34
|
+
state,
|
35
|
+
getTools,
|
36
|
+
onResult,
|
37
|
+
}: UseToolInvocationsParams) {
|
38
|
+
const lastToolStates = useRef<
|
39
|
+
Record<
|
40
|
+
string,
|
41
|
+
{
|
42
|
+
argsText: string;
|
43
|
+
hasResult: boolean;
|
44
|
+
controller: ToolCallStreamController;
|
45
|
+
}
|
46
|
+
>
|
47
|
+
>({});
|
48
|
+
|
49
|
+
const acRef = useRef<AbortController>(new AbortController());
|
50
|
+
const [controller] = useState(() => {
|
51
|
+
const [stream, controller] = createAssistantStreamController();
|
52
|
+
const transform = unstable_toolResultStream(
|
53
|
+
getTools,
|
54
|
+
() => acRef.current?.signal ?? new AbortController().signal,
|
55
|
+
);
|
56
|
+
stream
|
57
|
+
.pipeThrough(transform)
|
58
|
+
.pipeThrough(new AssistantMetaTransformStream())
|
59
|
+
.pipeTo(
|
60
|
+
new WritableStream({
|
61
|
+
write(chunk) {
|
62
|
+
if (chunk.type === "result") {
|
63
|
+
// the tool call result was already set by the backend
|
64
|
+
if (lastToolStates.current[chunk.meta.toolCallId]?.hasResult)
|
65
|
+
return;
|
66
|
+
|
67
|
+
onResult({
|
68
|
+
type: "add-tool-result",
|
69
|
+
toolCallId: chunk.meta.toolCallId,
|
70
|
+
toolName: chunk.meta.toolName,
|
71
|
+
result: chunk.result,
|
72
|
+
isError: chunk.isError,
|
73
|
+
...(chunk.artifact && { artifact: chunk.artifact }),
|
74
|
+
});
|
75
|
+
}
|
76
|
+
},
|
77
|
+
}),
|
78
|
+
);
|
79
|
+
|
80
|
+
return controller;
|
81
|
+
});
|
82
|
+
|
83
|
+
const ignoredToolIds = useRef<Set<string>>(new Set());
|
84
|
+
const isInititialState = useRef(true);
|
85
|
+
|
86
|
+
useEffect(() => {
|
87
|
+
if (isInititialState.current) {
|
88
|
+
state.messages.forEach((message) => {
|
89
|
+
message.content.forEach((content) => {
|
90
|
+
if (content.type === "tool-call") {
|
91
|
+
ignoredToolIds.current.add(content.toolCallId);
|
92
|
+
}
|
93
|
+
});
|
94
|
+
});
|
95
|
+
isInititialState.current = false;
|
96
|
+
} else {
|
97
|
+
state.messages.forEach((message) => {
|
98
|
+
message.content.forEach((content) => {
|
99
|
+
if (content.type === "tool-call") {
|
100
|
+
if (ignoredToolIds.current.has(content.toolCallId)) {
|
101
|
+
return;
|
102
|
+
}
|
103
|
+
let lastState = lastToolStates.current[content.toolCallId];
|
104
|
+
if (!lastState) {
|
105
|
+
const toolCallController = controller.addToolCallPart({
|
106
|
+
toolName: content.toolName,
|
107
|
+
toolCallId: content.toolCallId,
|
108
|
+
});
|
109
|
+
lastState = {
|
110
|
+
argsText: "",
|
111
|
+
hasResult: false,
|
112
|
+
controller: toolCallController,
|
113
|
+
};
|
114
|
+
lastToolStates.current[content.toolCallId] = lastState;
|
115
|
+
}
|
116
|
+
|
117
|
+
if (content.argsText !== lastState.argsText) {
|
118
|
+
if (!content.argsText.startsWith(lastState.argsText)) {
|
119
|
+
throw new Error(
|
120
|
+
`Tool call argsText can only be appended, not updated: ${content.argsText} does not start with ${lastState.argsText}`,
|
121
|
+
);
|
122
|
+
}
|
123
|
+
|
124
|
+
const argsTextDelta = content.argsText.slice(
|
125
|
+
lastState.argsText.length,
|
126
|
+
);
|
127
|
+
lastState.controller.argsText.append(argsTextDelta);
|
128
|
+
|
129
|
+
if (isArgsTextComplete(content.argsText)) {
|
130
|
+
lastState.controller.argsText.close();
|
131
|
+
}
|
132
|
+
|
133
|
+
lastToolStates.current[content.toolCallId] = {
|
134
|
+
argsText: content.argsText,
|
135
|
+
hasResult: lastState.hasResult,
|
136
|
+
controller: lastState.controller,
|
137
|
+
};
|
138
|
+
}
|
139
|
+
|
140
|
+
if (content.result !== undefined && !lastState.hasResult) {
|
141
|
+
lastState.controller.setResponse(
|
142
|
+
new ToolResponse({
|
143
|
+
result: content.result as ReadonlyJSONValue,
|
144
|
+
artifact: content.artifact as ReadonlyJSONValue | undefined,
|
145
|
+
isError: content.isError,
|
146
|
+
}),
|
147
|
+
);
|
148
|
+
lastState.controller.close();
|
149
|
+
|
150
|
+
lastToolStates.current[content.toolCallId] = {
|
151
|
+
hasResult: true,
|
152
|
+
argsText: lastState.argsText,
|
153
|
+
controller: lastState.controller,
|
154
|
+
};
|
155
|
+
}
|
156
|
+
}
|
157
|
+
});
|
158
|
+
});
|
159
|
+
}
|
160
|
+
}, [state]);
|
161
|
+
|
162
|
+
return {
|
163
|
+
reset: () => {
|
164
|
+
acRef.current.abort();
|
165
|
+
acRef.current = new AbortController();
|
166
|
+
isInititialState.current = true;
|
167
|
+
},
|
168
|
+
abort: () => {
|
169
|
+
acRef.current.abort();
|
170
|
+
acRef.current = new AbortController();
|
171
|
+
},
|
172
|
+
};
|
173
|
+
}
|
@@ -0,0 +1,42 @@
|
|
1
|
+
import { Tool } from "assistant-stream";
|
2
|
+
import { JSONSchema7 } from "json-schema";
|
3
|
+
import { z } from "zod";
|
4
|
+
|
5
|
+
// Convert tools to AI SDK format
|
6
|
+
export const toAISDKTools = (tools: Record<string, Tool>) => {
|
7
|
+
return Object.fromEntries(
|
8
|
+
Object.entries(tools).map(([name, tool]) => [
|
9
|
+
name,
|
10
|
+
{
|
11
|
+
...(tool.description ? { description: tool.description } : undefined),
|
12
|
+
parameters: (tool.parameters instanceof z.ZodType
|
13
|
+
? z.toJSONSchema(tool.parameters)
|
14
|
+
: tool.parameters) as JSONSchema7,
|
15
|
+
},
|
16
|
+
]),
|
17
|
+
);
|
18
|
+
};
|
19
|
+
|
20
|
+
// Filter enabled tools
|
21
|
+
export const getEnabledTools = (tools: Record<string, Tool>) => {
|
22
|
+
return Object.fromEntries(
|
23
|
+
Object.entries(tools).filter(
|
24
|
+
([, tool]) => !tool.disabled && tool.type !== "backend",
|
25
|
+
),
|
26
|
+
);
|
27
|
+
};
|
28
|
+
|
29
|
+
// Create headers for fetch request
|
30
|
+
export const createRequestHeaders = async (
|
31
|
+
headersValue:
|
32
|
+
| Record<string, string>
|
33
|
+
| Headers
|
34
|
+
| (() => Promise<Record<string, string> | Headers>),
|
35
|
+
): Promise<Headers> => {
|
36
|
+
const resolvedHeaders =
|
37
|
+
typeof headersValue === "function" ? await headersValue() : headersValue;
|
38
|
+
|
39
|
+
const headers = new Headers(resolvedHeaders);
|
40
|
+
headers.set("Content-Type", "application/json");
|
41
|
+
return headers;
|
42
|
+
};
|
@@ -47,6 +47,7 @@ export abstract class BaseThreadRuntimeCore implements ThreadRuntimeCore {
|
|
47
47
|
public abstract resumeRun(config: ResumeRunConfig): void;
|
48
48
|
public abstract addToolResult(options: AddToolResultOptions): void;
|
49
49
|
public abstract cancelRun(): void;
|
50
|
+
public abstract unstable_loadExternalState(state: any): void;
|
50
51
|
|
51
52
|
public get messages() {
|
52
53
|
return this.repository.getMessages();
|
@@ -63,7 +63,7 @@ export type StartRunConfig = {
|
|
63
63
|
};
|
64
64
|
|
65
65
|
export type ResumeRunConfig = StartRunConfig & {
|
66
|
-
stream
|
66
|
+
stream?: (
|
67
67
|
options: ChatModelRunOptions,
|
68
68
|
) => AsyncGenerator<ChatModelRunResult, void, unknown>;
|
69
69
|
};
|
@@ -122,4 +122,5 @@ export type ThreadRuntimeCore = Readonly<{
|
|
122
122
|
reset(initialMessages?: readonly ThreadMessageLike[]): void;
|
123
123
|
|
124
124
|
unstable_on(event: ThreadRuntimeEventType, callback: () => void): Unsubscribe;
|
125
|
+
unstable_loadExternalState: (state: any) => void;
|
125
126
|
}>;
|
@@ -2,6 +2,7 @@ import { AppendMessage, ThreadMessage } from "../../../types";
|
|
2
2
|
import { AttachmentAdapter } from "../adapters/attachment";
|
3
3
|
import {
|
4
4
|
AddToolResultOptions,
|
5
|
+
ResumeRunConfig,
|
5
6
|
StartRunConfig,
|
6
7
|
ThreadSuggestion,
|
7
8
|
} from "../core/ThreadRuntimeCore";
|
@@ -63,11 +64,13 @@ type ExternalStoreAdapterBase<T> = {
|
|
63
64
|
|
64
65
|
setMessages?: ((messages: readonly T[]) => void) | undefined;
|
65
66
|
onImport?: ((messages: readonly ThreadMessage[]) => void) | undefined;
|
67
|
+
onLoadExternalState?: ((state: any) => void) | undefined;
|
66
68
|
onNew: (message: AppendMessage) => Promise<void>;
|
67
69
|
onEdit?: ((message: AppendMessage) => Promise<void>) | undefined;
|
68
70
|
onReload?: // TODO: remove parentId in 0.8.0
|
69
71
|
| ((parentId: string | null, config: StartRunConfig) => Promise<void>)
|
70
72
|
| undefined;
|
73
|
+
onResume?: ((config: ResumeRunConfig) => Promise<void>) | undefined;
|
71
74
|
onCancel?: (() => Promise<void>) | undefined;
|
72
75
|
onAddToolResult?:
|
73
76
|
| ((options: AddToolResultOptions) => Promise<void> | void)
|
@@ -1,5 +1,6 @@
|
|
1
1
|
import {
|
2
2
|
AddToolResultOptions,
|
3
|
+
ResumeRunConfig,
|
3
4
|
StartRunConfig,
|
4
5
|
ThreadSuggestion,
|
5
6
|
} from "../core/ThreadRuntimeCore";
|
@@ -244,8 +245,18 @@ export class ExternalStoreThreadRuntimeCore
|
|
244
245
|
await this._store.onReload(config.parentId, config);
|
245
246
|
}
|
246
247
|
|
247
|
-
public async resumeRun(): Promise<void> {
|
248
|
-
|
248
|
+
public async resumeRun(config: ResumeRunConfig): Promise<void> {
|
249
|
+
if (!this._store.onResume)
|
250
|
+
throw new Error("Runtime does not support resuming runs.");
|
251
|
+
|
252
|
+
await this._store.onResume(config);
|
253
|
+
}
|
254
|
+
|
255
|
+
public unstable_loadExternalState(state: any): void {
|
256
|
+
if (!this._store.onLoadExternalState)
|
257
|
+
throw new Error("Runtime does not support importing states.");
|
258
|
+
|
259
|
+
this._store.onLoadExternalState(state);
|
249
260
|
}
|
250
261
|
|
251
262
|
public cancelRun(): void {
|
@@ -133,15 +133,12 @@ export const fromThreadMessageLike = (
|
|
133
133
|
return {
|
134
134
|
...commonProps,
|
135
135
|
args: part.args,
|
136
|
-
argsText: JSON.stringify(part.args),
|
136
|
+
argsText: part.argsText ?? JSON.stringify(part.args),
|
137
137
|
};
|
138
138
|
}
|
139
139
|
return {
|
140
140
|
...commonProps,
|
141
|
-
args:
|
142
|
-
part.args ??
|
143
|
-
parsePartialJsonObject(part.argsText ?? "") ??
|
144
|
-
{},
|
141
|
+
args: parsePartialJsonObject(part.argsText ?? "") ?? {},
|
145
142
|
argsText: part.argsText ?? "",
|
146
143
|
};
|
147
144
|
}
|
@@ -166,9 +166,15 @@ export class LocalThreadRuntimeCore
|
|
166
166
|
}
|
167
167
|
|
168
168
|
public resumeRun({ stream, ...startConfig }: ResumeRunConfig): Promise<void> {
|
169
|
+
if (!stream)
|
170
|
+
throw new Error("You must pass a stream parameter to resume runs.");
|
169
171
|
return this.startRun(startConfig, stream);
|
170
172
|
}
|
171
173
|
|
174
|
+
public unstable_loadExternalState(): void {
|
175
|
+
throw new Error("Runtime does not support importing external states.");
|
176
|
+
}
|
177
|
+
|
172
178
|
public async startRun(
|
173
179
|
{ parentId, runConfig }: StartRunConfig,
|
174
180
|
runCallback?: ChatModelAdapter["run"],
|