@nexface/chat 0.1.15-alpha.1 → 0.1.15-alpha.2
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/assistant-attachments.js +60 -17
- package/dist/assistant-components.d.ts +3 -3
- package/dist/assistant-components.js +77 -29
- package/dist/assistant-runtime.d.ts +1 -1
- package/dist/assistant-runtime.js +100 -43
- package/dist/attachments.js +36 -15
- package/dist/client.js +309 -77
- package/dist/config.js +4 -5
- package/dist/default-chat.d.ts +1 -1
- package/dist/default-chat.js +2102 -728
- package/dist/errors.d.ts +1 -0
- package/dist/errors.js +69 -18
- package/dist/index.js +0 -1
- package/dist/messages.js +0 -1
- package/dist/page-confirmation.js +3 -4
- package/dist/protocol-messages.js +290 -66
- package/dist/protocol-response.js +38 -18
- package/dist/react.d.ts +1 -1
- package/dist/react.js +147 -50
- package/dist/shared/errors.d.ts +1 -1
- package/dist/shared/errors.js +0 -1
- package/dist/shared/runtime-controller.d.ts +1 -0
- package/dist/shared/runtime-controller.js +0 -1
- package/dist/shared/runtime-execution.js +0 -1
- package/dist/shared/schema.js +45 -19
- package/dist/storage.js +130 -53
- package/dist/styles.css +99 -20
- package/dist/tool-result.js +7 -3
- package/dist/types.js +0 -1
- package/dist/ui/attachment.aui.d.ts +1 -1
- package/dist/ui/attachment.aui.js +16 -20
- package/dist/ui/avatar.js +0 -1
- package/dist/ui/button.js +0 -1
- package/dist/ui/code-block.js +2 -3
- package/dist/ui/collapsible.js +2 -3
- package/dist/ui/composer-voice.js +3 -2
- package/dist/ui/dialog.d.ts +1 -1
- package/dist/ui/dialog.js +9 -4
- package/dist/ui/dropdown-menu.js +2 -3
- package/dist/ui/home-demo/action-bar.js +0 -1
- package/dist/ui/home-demo/composer.js +7 -4
- package/dist/ui/home-demo/greeting.d.ts +1 -1
- package/dist/ui/home-demo/greeting.js +1 -2
- package/dist/ui/home-demo/messages.d.ts +1 -1
- package/dist/ui/home-demo/messages.js +4 -5
- package/dist/ui/home-demo/styles.js +0 -1
- package/dist/ui/home-demo/suggestions.d.ts +1 -1
- package/dist/ui/home-demo/suggestions.js +5 -3
- package/dist/ui/home-demo/surfaces.js +3 -2
- package/dist/ui/home-demo/thread.d.ts +1 -1
- package/dist/ui/home-demo/thread.js +1 -2
- package/dist/ui/home-demo/trace-line.d.ts +1 -1
- package/dist/ui/home-demo/trace-line.js +2 -3
- package/dist/ui/home-demo/trace.d.ts +1 -1
- package/dist/ui/home-demo/trace.js +28 -9
- package/dist/ui/home-demo/type.js +0 -1
- package/dist/ui/input.js +0 -1
- package/dist/ui/markdown-text.js +2 -3
- package/dist/ui/reasoning.aui.js +2 -3
- package/dist/ui/reasoning.d.ts +1 -1
- package/dist/ui/reasoning.js +6 -9
- package/dist/ui/surface.js +27 -8
- package/dist/ui/thread-list.aui.d.ts +2 -2
- package/dist/ui/thread-list.aui.js +18 -12
- package/dist/ui/tool-group.aui.js +2 -3
- package/dist/ui/tooltip-icon-button.js +1 -2
- package/dist/ui/tooltip.js +0 -1
- package/dist/ui/use-attachment-src.js +1 -2
- package/dist/ui/use-copy-to-clipboard.d.ts +1 -1
- package/dist/ui/use-copy-to-clipboard.js +1 -2
- package/dist/ui/utils.js +9 -3
- package/dist/use-next-message.js +7 -4
- package/dist/voice/types.js +0 -1
- package/dist/voice/use-voice.js +35 -13
- package/package.json +8 -4
- package/dist/assistant-attachments.js.map +0 -1
- package/dist/assistant-components.js.map +0 -1
- package/dist/assistant-runtime.js.map +0 -1
- package/dist/attachments.js.map +0 -1
- package/dist/client.js.map +0 -1
- package/dist/config.js.map +0 -1
- package/dist/default-chat.js.map +0 -7
- package/dist/errors.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/messages.js.map +0 -1
- package/dist/page-confirmation.js.map +0 -1
- package/dist/protocol-messages.js.map +0 -1
- package/dist/protocol-response.js.map +0 -1
- package/dist/react.js.map +0 -1
- package/dist/shared/errors.js.map +0 -1
- package/dist/shared/runtime-controller.js.map +0 -1
- package/dist/shared/runtime-execution.js.map +0 -1
- package/dist/shared/schema.js.map +0 -1
- package/dist/storage.js.map +0 -1
- package/dist/tool-result.js.map +0 -1
- package/dist/types.js.map +0 -1
- package/dist/ui/attachment.aui.js.map +0 -1
- package/dist/ui/avatar.js.map +0 -1
- package/dist/ui/button.js.map +0 -1
- package/dist/ui/code-block.js.map +0 -1
- package/dist/ui/collapsible.js.map +0 -1
- package/dist/ui/composer-voice.js.map +0 -1
- package/dist/ui/dialog.js.map +0 -1
- package/dist/ui/dropdown-menu.js.map +0 -1
- package/dist/ui/home-demo/action-bar.js.map +0 -1
- package/dist/ui/home-demo/composer.js.map +0 -1
- package/dist/ui/home-demo/greeting.js.map +0 -1
- package/dist/ui/home-demo/messages.js.map +0 -1
- package/dist/ui/home-demo/styles.js.map +0 -1
- package/dist/ui/home-demo/suggestions.js.map +0 -1
- package/dist/ui/home-demo/surfaces.js.map +0 -1
- package/dist/ui/home-demo/thread.js.map +0 -1
- package/dist/ui/home-demo/trace-line.js.map +0 -1
- package/dist/ui/home-demo/trace.js.map +0 -1
- package/dist/ui/home-demo/type.js.map +0 -1
- package/dist/ui/input.js.map +0 -1
- package/dist/ui/markdown-text.js.map +0 -1
- package/dist/ui/reasoning.aui.js.map +0 -1
- package/dist/ui/reasoning.js.map +0 -1
- package/dist/ui/surface.js.map +0 -1
- package/dist/ui/thread-list.aui.js.map +0 -1
- package/dist/ui/tool-group.aui.js.map +0 -1
- package/dist/ui/tooltip-icon-button.js.map +0 -1
- package/dist/ui/tooltip.js.map +0 -1
- package/dist/ui/use-attachment-src.js.map +0 -1
- package/dist/ui/use-copy-to-clipboard.js.map +0 -1
- package/dist/ui/utils.js.map +0 -1
- package/dist/use-next-message.js.map +0 -1
- package/dist/voice/types.js.map +0 -1
- package/dist/voice/use-voice.js.map +0 -1
package/dist/react.js
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import {
|
|
2
|
+
import { reportError } from "./errors.js";
|
|
3
|
+
import { createContext, useContext, useEffect, useLayoutEffect, useRef, useReducer, useCallback, } from "react";
|
|
3
4
|
import { ToolsProvider } from "@nexface/tools";
|
|
4
|
-
import { PageConfirmationProvider } from "./page-confirmation.js";
|
|
5
|
-
import { createChatClient, getClientController, ClientError } from "./client.js";
|
|
5
|
+
import { PageConfirmationProvider, } from "./page-confirmation.js";
|
|
6
|
+
import { createChatClient, getClientController, ClientError, } from "./client.js";
|
|
6
7
|
import { attachmentContent } from "./attachments.js";
|
|
7
8
|
import { projectMessages } from "./protocol-messages.js";
|
|
8
9
|
import { chatStorageKey, readChatStorage, writeChatStorage } from "./storage.js";
|
|
9
10
|
export function isChatError(error) {
|
|
10
11
|
const value = error;
|
|
11
|
-
return !!value &&
|
|
12
|
+
return (!!value &&
|
|
13
|
+
value.type === "chat_error" &&
|
|
14
|
+
typeof value.code === "string" &&
|
|
15
|
+
typeof value.message === "string");
|
|
12
16
|
}
|
|
13
17
|
const sendGuards = new WeakMap();
|
|
14
18
|
// Read the live Store at draft submission, including updates before React commits.
|
|
@@ -17,21 +21,32 @@ export function assertChatSendable(chat, sessionId) {
|
|
|
17
21
|
}
|
|
18
22
|
const ChatContext = createContext(null);
|
|
19
23
|
const createId = () => globalThis.crypto.randomUUID();
|
|
20
|
-
const textOf = (message) => message.parts
|
|
24
|
+
const textOf = (message) => message.parts
|
|
25
|
+
.filter((part) => part.type === "text")
|
|
26
|
+
.map((part) => part.text)
|
|
27
|
+
.join("");
|
|
21
28
|
const asError = (error) => {
|
|
22
29
|
if (isChatError(error))
|
|
23
30
|
return error;
|
|
24
31
|
const value = error;
|
|
25
|
-
return new ClientError(typeof value?.code === "string"
|
|
32
|
+
return new ClientError(typeof value?.code === "string"
|
|
33
|
+
? value.code
|
|
34
|
+
: typeof value?.name === "string"
|
|
35
|
+
? value.name
|
|
36
|
+
: "CHAT_RUN_ERROR", typeof value?.message === "string" ? value.message : String(error));
|
|
26
37
|
};
|
|
27
38
|
export function NexfaceProvider(props) {
|
|
28
39
|
const { toolRuntime, children, webMcp = true } = props;
|
|
29
40
|
const onError = useRef(props.onError);
|
|
30
|
-
useLayoutEffect(() => {
|
|
41
|
+
useLayoutEffect(() => {
|
|
42
|
+
onError.current = props.onError;
|
|
43
|
+
});
|
|
31
44
|
const runtime = useRef(toolRuntime);
|
|
32
45
|
if (runtime.current !== toolRuntime)
|
|
33
46
|
throw new ClientError("CHAT_RUNTIME_CHANGED", "NexfaceProvider requires a stable ToolRuntime. Remount it to change Runtime.");
|
|
34
|
-
if (Boolean(props.agent) === Boolean(props.run) ||
|
|
47
|
+
if (Boolean(props.agent) === Boolean(props.run) ||
|
|
48
|
+
(props.agent &&
|
|
49
|
+
("toolRuntime" in props.agent || "toolBridge" in props.agent || "run" in props.agent)))
|
|
35
50
|
throw new ClientError("INVALID_INPUT", "Provide exactly one agent configuration or run function.");
|
|
36
51
|
const pageTools = useRef(new Set());
|
|
37
52
|
const [, render] = useReducer((n) => n + 1, 0);
|
|
@@ -40,29 +55,57 @@ export function NexfaceProvider(props) {
|
|
|
40
55
|
const changed = useCallback(() => render(), []);
|
|
41
56
|
const createSessionValue = useCallback((saved) => {
|
|
42
57
|
const current = props;
|
|
43
|
-
const client = createChatClient({
|
|
58
|
+
const client = createChatClient({
|
|
59
|
+
...(current.run
|
|
60
|
+
? { toolRuntime, run: current.run }
|
|
61
|
+
: { toolRuntime, agent: current.agent }),
|
|
62
|
+
onError: (event) => reportError(event, onError.current),
|
|
63
|
+
});
|
|
44
64
|
const controller = getClientController(client);
|
|
45
65
|
if (saved)
|
|
46
66
|
controller.id = saved.id;
|
|
47
67
|
controller.setPageResolver((name) => pageTools.current.has(name));
|
|
48
68
|
if (saved)
|
|
49
69
|
controller.restore(saved.messages);
|
|
50
|
-
return {
|
|
70
|
+
return {
|
|
71
|
+
id: saved?.id ?? controller.id,
|
|
72
|
+
client,
|
|
73
|
+
...(saved?.title ? { title: saved.title } : {}),
|
|
74
|
+
unsubscribe: client.subscribe(changed),
|
|
75
|
+
};
|
|
51
76
|
}, [props.agent, props.run, toolRuntime, changed]);
|
|
52
77
|
if (!storeRef.current) {
|
|
53
78
|
const first = createSessionValue();
|
|
54
|
-
storeRef.current = {
|
|
79
|
+
storeRef.current = {
|
|
80
|
+
sessions: [first],
|
|
81
|
+
activeId: first.id,
|
|
82
|
+
loaded: false,
|
|
83
|
+
switching: false,
|
|
84
|
+
version: { value: null },
|
|
85
|
+
};
|
|
55
86
|
}
|
|
56
87
|
const store = storeRef.current;
|
|
57
88
|
const active = () => store.sessions.find((session) => session.id === store.activeId);
|
|
58
|
-
const find = (id) => {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
const
|
|
65
|
-
|
|
89
|
+
const find = (id) => {
|
|
90
|
+
const session = store.sessions.find((item) => item.id === id);
|
|
91
|
+
if (!session)
|
|
92
|
+
throw new ClientError("UNKNOWN_SESSION", "Unknown chat session.");
|
|
93
|
+
return session;
|
|
94
|
+
};
|
|
95
|
+
const busy = () => store.switching ||
|
|
96
|
+
store.sessions.some((session) => getClientController(session.client).busy && session.client.getStatus() !== "waiting");
|
|
97
|
+
const assertIdle = (code = "CHAT_RUN_ACTIVE") => {
|
|
98
|
+
if (busy())
|
|
99
|
+
throw new ClientError(code, "Wait for the active task to finish.");
|
|
100
|
+
};
|
|
101
|
+
const release = (session) => {
|
|
102
|
+
session.unsubscribe();
|
|
103
|
+
session.client.dispose();
|
|
104
|
+
};
|
|
105
|
+
const cancelWaiting = () => {
|
|
106
|
+
if (active().client.getStatus() === "waiting")
|
|
107
|
+
active().client.cancel();
|
|
108
|
+
};
|
|
66
109
|
useEffect(() => {
|
|
67
110
|
if (store.loaded)
|
|
68
111
|
return;
|
|
@@ -85,9 +128,17 @@ export function NexfaceProvider(props) {
|
|
|
85
128
|
const persist = () => {
|
|
86
129
|
if (store.warning)
|
|
87
130
|
return;
|
|
88
|
-
const stored = {
|
|
131
|
+
const stored = {
|
|
132
|
+
activeSessionId: store.activeId,
|
|
133
|
+
sessions: store.sessions.map((session) => ({
|
|
134
|
+
id: session.id,
|
|
135
|
+
...(session.title ? { title: session.title } : {}),
|
|
136
|
+
messages: [...projectMessages(session.client.getMessages())],
|
|
137
|
+
})),
|
|
138
|
+
};
|
|
89
139
|
if (writeChatStorage(chatStorageKey, stored, store.version) === "conflict") {
|
|
90
|
-
store.warning =
|
|
140
|
+
store.warning =
|
|
141
|
+
"其他标签页已更新历史会话,本页已暂停保存。请先复制本页需要保留的新消息,再刷新以加载最新历史。";
|
|
91
142
|
changed();
|
|
92
143
|
}
|
|
93
144
|
};
|
|
@@ -103,8 +154,10 @@ export function NexfaceProvider(props) {
|
|
|
103
154
|
mounted.current = false;
|
|
104
155
|
window.removeEventListener("pagehide", flush);
|
|
105
156
|
flush();
|
|
106
|
-
queueMicrotask(() => {
|
|
107
|
-
|
|
157
|
+
queueMicrotask(() => {
|
|
158
|
+
if (!mounted.current)
|
|
159
|
+
store.sessions.forEach(release);
|
|
160
|
+
});
|
|
108
161
|
};
|
|
109
162
|
}, []);
|
|
110
163
|
const createSession = useCallback(() => {
|
|
@@ -152,7 +205,7 @@ export function NexfaceProvider(props) {
|
|
|
152
205
|
if (previous.title)
|
|
153
206
|
session.title = previous.title;
|
|
154
207
|
release(previous);
|
|
155
|
-
store.sessions = store.sessions.map((item) => item === previous ? session : item);
|
|
208
|
+
store.sessions = store.sessions.map((item) => (item === previous ? session : item));
|
|
156
209
|
if (store.activeId === id)
|
|
157
210
|
store.activeId = session.id;
|
|
158
211
|
changed();
|
|
@@ -175,11 +228,14 @@ export function NexfaceProvider(props) {
|
|
|
175
228
|
const replacement = { ...session, client, unsubscribe: client.subscribe(changed) };
|
|
176
229
|
previous = session;
|
|
177
230
|
retainedCount = client.getMessages().length;
|
|
178
|
-
store.sessions = store.sessions.map((item) => item === session ? replacement : item);
|
|
231
|
+
store.sessions = store.sessions.map((item) => (item === session ? replacement : item));
|
|
179
232
|
session = replacement;
|
|
180
233
|
delete store.completion;
|
|
181
234
|
}
|
|
182
|
-
const parts = [
|
|
235
|
+
const parts = [
|
|
236
|
+
...(content ? [{ type: "text", text: content }] : []),
|
|
237
|
+
...attachments.flatMap(attachmentContent),
|
|
238
|
+
];
|
|
183
239
|
try {
|
|
184
240
|
const messageCount = session.client.getMessages().length;
|
|
185
241
|
const pending = getClientController(session.client).send(attachments.length ? parts : parts[0]?.type === "text" ? parts[0].text : content, { id: createId(), role: "user", parts: [{ type: "text", text: content }], attachments });
|
|
@@ -195,7 +251,7 @@ export function NexfaceProvider(props) {
|
|
|
195
251
|
// Keep the original conversation if validation or Runtime acquisition rejected the edit.
|
|
196
252
|
if (previous && session.client.getMessages().length === retainedCount) {
|
|
197
253
|
release(session);
|
|
198
|
-
store.sessions = store.sessions.map((item) => item === session ? previous : item);
|
|
254
|
+
store.sessions = store.sessions.map((item) => (item === session ? previous : item));
|
|
199
255
|
previous = undefined;
|
|
200
256
|
}
|
|
201
257
|
changed();
|
|
@@ -219,29 +275,69 @@ export function NexfaceProvider(props) {
|
|
|
219
275
|
const approval = client.getApproval();
|
|
220
276
|
const error = client.getError();
|
|
221
277
|
const value = {
|
|
222
|
-
sessions: store.sessions
|
|
278
|
+
sessions: store.sessions
|
|
279
|
+
.map((item, index) => {
|
|
223
280
|
const messages = projectMessages(item.client.getMessages());
|
|
224
281
|
const first = messages.find((message) => message.role === "user");
|
|
225
282
|
const title = first ? textOf(first).trim() : "";
|
|
226
|
-
return {
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
283
|
+
return {
|
|
284
|
+
id: item.id,
|
|
285
|
+
title: item.title ||
|
|
286
|
+
(title ? (title.length > 24 ? title.slice(0, 24) + "…" : title) : `会话 ${index + 1}`),
|
|
287
|
+
messageCount: messages.length,
|
|
288
|
+
};
|
|
289
|
+
})
|
|
290
|
+
.reverse(),
|
|
291
|
+
activeSessionId: session.id,
|
|
292
|
+
messages: projectMessages(client.getMessages()),
|
|
293
|
+
status: store.switching
|
|
294
|
+
? "switching-model"
|
|
295
|
+
: controller.cancelling
|
|
296
|
+
? "cancelling"
|
|
297
|
+
: client.getStatus(),
|
|
230
298
|
model: controller.agent?.model ?? { provider: "server", model: "server" },
|
|
231
|
-
...(approval ? { interrupt: approval } : {}),
|
|
299
|
+
...(approval ? { interrupt: approval } : {}),
|
|
300
|
+
...(store.warning ? { storageWarning: store.warning } : {}),
|
|
232
301
|
...(error || store.error ? { error: error ? asError(error) : store.error } : {}),
|
|
233
|
-
...(store.completion?.sessionId === session.id
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
302
|
+
...(store.completion?.sessionId === session.id
|
|
303
|
+
? { completed: { id: store.completion.id, result: store.completion.result } }
|
|
304
|
+
: {}),
|
|
305
|
+
consumeCompletion(id) {
|
|
306
|
+
if (store.consumed === id)
|
|
307
|
+
return false;
|
|
308
|
+
store.consumed = id;
|
|
309
|
+
return true;
|
|
310
|
+
},
|
|
311
|
+
sendMessage,
|
|
312
|
+
createSession,
|
|
313
|
+
switchSession,
|
|
314
|
+
deleteSession,
|
|
315
|
+
clearSession,
|
|
316
|
+
clearAllSessions() {
|
|
317
|
+
assertIdle();
|
|
318
|
+
store.sessions.forEach(release);
|
|
319
|
+
const first = createSessionValue();
|
|
320
|
+
store.sessions = [first];
|
|
321
|
+
store.activeId = first.id;
|
|
322
|
+
changed();
|
|
323
|
+
},
|
|
324
|
+
renameSession(id, title) {
|
|
325
|
+
if (!title.trim())
|
|
326
|
+
throw new ClientError("INVALID_INPUT", "Session title is required.");
|
|
327
|
+
find(id).title = title.trim().slice(0, 80);
|
|
328
|
+
changed();
|
|
329
|
+
},
|
|
330
|
+
cancel() {
|
|
331
|
+
active().client.cancel();
|
|
332
|
+
},
|
|
333
|
+
confirmInterrupt() {
|
|
334
|
+
if (approval?.mode === "chat")
|
|
335
|
+
void client.respondToApproval(approval.id, { approved: true }).catch(() => undefined);
|
|
336
|
+
},
|
|
337
|
+
rejectInterrupt() {
|
|
338
|
+
if (approval?.mode === "chat")
|
|
339
|
+
void client.respondToApproval(approval.id, { approved: false }).catch(() => undefined);
|
|
340
|
+
},
|
|
245
341
|
async switchModel(model) {
|
|
246
342
|
if (busy() || active().client.getApproval())
|
|
247
343
|
throw new ClientError("CHAT_MODEL_SWITCH_BLOCKED", "The chat is busy.");
|
|
@@ -260,10 +356,12 @@ export function NexfaceProvider(props) {
|
|
|
260
356
|
}
|
|
261
357
|
},
|
|
262
358
|
};
|
|
263
|
-
return _jsx(ChatContext.Provider, { value: value, children: _jsx(ToolsProvider, { toolRuntime: toolRuntime, webMcp: webMcp, children: _jsx(PageConfirmationProvider, { interrupt: approval, onConfirm: () => getClientController(active().client).confirmPage(), onBindingRemoved: () => getClientController(active().client).invalidate("Page confirmation entry was removed."), onBindingRegistration: (name, registered) => {
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
359
|
+
return (_jsx(ChatContext.Provider, { value: value, children: _jsx(ToolsProvider, { toolRuntime: toolRuntime, webMcp: webMcp, children: _jsx(PageConfirmationProvider, { interrupt: approval, onConfirm: () => getClientController(active().client).confirmPage(), onBindingRemoved: () => getClientController(active().client).invalidate("Page confirmation entry was removed."), onBindingRegistration: (name, registered) => {
|
|
360
|
+
if (registered)
|
|
361
|
+
pageTools.current.add(name);
|
|
362
|
+
else
|
|
363
|
+
pageTools.current.delete(name);
|
|
364
|
+
}, children: children }) }) }));
|
|
267
365
|
}
|
|
268
366
|
export function useChat() {
|
|
269
367
|
const chat = useContext(ChatContext);
|
|
@@ -271,4 +369,3 @@ export function useChat() {
|
|
|
271
369
|
throw new Error("useChat must be used inside NexfaceProvider.");
|
|
272
370
|
return chat;
|
|
273
371
|
}
|
|
274
|
-
//# sourceMappingURL=react.js.map
|
package/dist/shared/errors.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export declare const TOOL_ERROR_CODES: readonly ["EXECUTION_OUTCOME_UNKNOWN", "ACTION_IN_FLIGHT", "ACTION_NOT_HANDLED", "ACTION_TARGET_UNAVAILABLE", "BROWSER_EXECUTION_FAILED", "BROWSER_SNAPSHOT_REQUIRED", "BROWSER_TOOLS_UNAVAILABLE", "CONTRACT_MISMATCH", "DUPLICATE_BINDING", "INVALID_INPUT", "INVALID_TOOL_RUNTIME", "OPERATION_BUSY", "TOOL_CANCELLED", "TOOL_CONFIRMATION_INVALIDATED", "TOOL_CONFIRMATION_REJECTED", "TOOL_ERROR", "TOOL_REGISTRATION_TIMEOUT", "TOOL_SKIPPED", "TOOL_UNAVAILABLE", "UNKNOWN_TOOL"];
|
|
2
|
-
type AgentToolErrorCode = typeof TOOL_ERROR_CODES[number];
|
|
2
|
+
type AgentToolErrorCode = (typeof TOOL_ERROR_CODES)[number];
|
|
3
3
|
export declare class AgentToolError extends Error {
|
|
4
4
|
readonly code: AgentToolErrorCode;
|
|
5
5
|
readonly details?: unknown | undefined;
|
package/dist/shared/errors.js
CHANGED
|
@@ -12,6 +12,7 @@ interface OperationSnapshot {
|
|
|
12
12
|
readonly taskProgress?: TaskProgress;
|
|
13
13
|
}
|
|
14
14
|
export interface RuntimeController {
|
|
15
|
+
getRegisteredTools(): ReturnType<ToolRuntime["getTools"]>;
|
|
15
16
|
beginInternalOperation(options?: {
|
|
16
17
|
signal?: AbortSignal;
|
|
17
18
|
onCancel?: () => void;
|
package/dist/shared/schema.js
CHANGED
|
@@ -3,8 +3,17 @@ const types = new Set(["object", "array", "string", "number", "integer", "boolea
|
|
|
3
3
|
const counts = ["minLength", "maxLength", "minItems", "maxItems"];
|
|
4
4
|
const bounds = ["minimum", "maximum", "exclusiveMinimum", "exclusiveMaximum"];
|
|
5
5
|
const keywords = new Set([
|
|
6
|
-
"type",
|
|
7
|
-
|
|
6
|
+
"type",
|
|
7
|
+
"description",
|
|
8
|
+
"properties",
|
|
9
|
+
"required",
|
|
10
|
+
"items",
|
|
11
|
+
"enum",
|
|
12
|
+
"additionalProperties",
|
|
13
|
+
...counts,
|
|
14
|
+
...bounds,
|
|
15
|
+
"pattern",
|
|
16
|
+
"uniqueItems",
|
|
8
17
|
]);
|
|
9
18
|
const checkedSchemas = new WeakSet();
|
|
10
19
|
const patterns = new WeakMap();
|
|
@@ -24,7 +33,9 @@ function assertJson(value, path, ancestors = new Set()) {
|
|
|
24
33
|
invalid(path, "must be JSON-compatible.");
|
|
25
34
|
if (ancestors.has(value))
|
|
26
35
|
invalid(path, "must not contain circular references.");
|
|
27
|
-
if (!Array.isArray(value) &&
|
|
36
|
+
if (!Array.isArray(value) &&
|
|
37
|
+
Object.getPrototypeOf(value) !== Object.prototype &&
|
|
38
|
+
Object.getPrototypeOf(value) !== null) {
|
|
28
39
|
invalid(path, "must be a plain JSON object.");
|
|
29
40
|
}
|
|
30
41
|
if (Object.getOwnPropertySymbols(value).length)
|
|
@@ -85,7 +96,8 @@ function assertSchema(value, path) {
|
|
|
85
96
|
invalid(`${path}.${key}`, "must be a non-negative integer.");
|
|
86
97
|
}
|
|
87
98
|
for (const key of bounds) {
|
|
88
|
-
if (value[key] !== undefined &&
|
|
99
|
+
if (value[key] !== undefined &&
|
|
100
|
+
(typeof value[key] !== "number" || !Number.isFinite(value[key])))
|
|
89
101
|
invalid(`${path}.${key}`, "must be a finite number.");
|
|
90
102
|
}
|
|
91
103
|
if (value.pattern !== undefined) {
|
|
@@ -95,7 +107,8 @@ function assertSchema(value, path) {
|
|
|
95
107
|
}
|
|
96
108
|
if (value.enum !== undefined && (!Array.isArray(value.enum) || value.enum.length === 0))
|
|
97
109
|
invalid(`${path}.enum`, "must be a non-empty array.");
|
|
98
|
-
if (Array.isArray(value.enum) &&
|
|
110
|
+
if (Array.isArray(value.enum) &&
|
|
111
|
+
value.enum.some((item, index, entries) => entries.slice(0, index).some((other) => jsonEqual(item, other))))
|
|
99
112
|
invalid(`${path}.enum`, "must contain unique values.");
|
|
100
113
|
if (value.properties !== undefined) {
|
|
101
114
|
if (value.type !== "object" || !isRecord(value.properties))
|
|
@@ -104,7 +117,10 @@ function assertSchema(value, path) {
|
|
|
104
117
|
assertSchema(property, `${path}.properties.${name}`);
|
|
105
118
|
}
|
|
106
119
|
if (value.required !== undefined) {
|
|
107
|
-
if (value.type !== "object" ||
|
|
120
|
+
if (value.type !== "object" ||
|
|
121
|
+
!Array.isArray(value.required) ||
|
|
122
|
+
value.required.some((name) => typeof name !== "string") ||
|
|
123
|
+
new Set(value.required).size !== value.required.length)
|
|
108
124
|
invalid(`${path}.required`, "must contain unique property names.");
|
|
109
125
|
const properties = value.properties;
|
|
110
126
|
if (value.required.some((name) => !properties || !Object.hasOwn(properties, name)))
|
|
@@ -118,7 +134,7 @@ function assertSchema(value, path) {
|
|
|
118
134
|
}
|
|
119
135
|
export function assertAgentToolInputSchema(value, path = "inputSchema") {
|
|
120
136
|
if (!isRecord(value) || value.type !== "object")
|
|
121
|
-
invalid(path,
|
|
137
|
+
invalid(path, "root type must be object.");
|
|
122
138
|
if (checkedSchemas.has(value))
|
|
123
139
|
return;
|
|
124
140
|
const frozen = assertJson(value, path);
|
|
@@ -130,21 +146,29 @@ function jsonEqual(left, right) {
|
|
|
130
146
|
if (left === right)
|
|
131
147
|
return true;
|
|
132
148
|
if (Array.isArray(left) && Array.isArray(right))
|
|
133
|
-
return left.length === right.length && left.every((item, index) => jsonEqual(item, right[index]));
|
|
149
|
+
return (left.length === right.length && left.every((item, index) => jsonEqual(item, right[index])));
|
|
134
150
|
if (!isRecord(left) || !isRecord(right))
|
|
135
151
|
return false;
|
|
136
152
|
const keys = Object.keys(left);
|
|
137
|
-
return keys.length === Object.keys(right).length &&
|
|
153
|
+
return (keys.length === Object.keys(right).length &&
|
|
154
|
+
keys.every((key) => Object.hasOwn(right, key) && jsonEqual(left[key], right[key])));
|
|
138
155
|
}
|
|
139
156
|
function typeMatches(schema, value) {
|
|
140
157
|
switch (schema.type) {
|
|
141
|
-
case undefined:
|
|
142
|
-
|
|
143
|
-
case "
|
|
144
|
-
|
|
145
|
-
case "
|
|
146
|
-
|
|
147
|
-
|
|
158
|
+
case undefined:
|
|
159
|
+
return true;
|
|
160
|
+
case "null":
|
|
161
|
+
return value === null;
|
|
162
|
+
case "array":
|
|
163
|
+
return Array.isArray(value);
|
|
164
|
+
case "object":
|
|
165
|
+
return isRecord(value);
|
|
166
|
+
case "integer":
|
|
167
|
+
return typeof value === "number" && Number.isInteger(value);
|
|
168
|
+
case "number":
|
|
169
|
+
return typeof value === "number" && Number.isFinite(value);
|
|
170
|
+
default:
|
|
171
|
+
return typeof value === schema.type;
|
|
148
172
|
}
|
|
149
173
|
}
|
|
150
174
|
export function validateAgentToolInput(schema, value, path = "input") {
|
|
@@ -177,7 +201,8 @@ export function validateAgentToolInput(schema, value, path = "input") {
|
|
|
177
201
|
errors.push(`${path} must have at least ${schema.minItems} items.`);
|
|
178
202
|
if (schema.maxItems !== undefined && value.length > schema.maxItems)
|
|
179
203
|
errors.push(`${path} must have at most ${schema.maxItems} items.`);
|
|
180
|
-
if (schema.uniqueItems &&
|
|
204
|
+
if (schema.uniqueItems &&
|
|
205
|
+
value.some((item, index) => value.slice(0, index).some((other) => jsonEqual(item, other))))
|
|
181
206
|
errors.push(`${path} must contain unique items.`);
|
|
182
207
|
if (schema.items)
|
|
183
208
|
value.forEach((item, index) => errors.push(...validateAgentToolInput(schema.items, item, `${path}[${index}]`)));
|
|
@@ -188,7 +213,9 @@ export function validateAgentToolInput(schema, value, path = "input") {
|
|
|
188
213
|
errors.push(`${path}.${key} is required.`);
|
|
189
214
|
}
|
|
190
215
|
for (const [key, item] of Object.entries(value)) {
|
|
191
|
-
const property = schema.properties && Object.hasOwn(schema.properties, key)
|
|
216
|
+
const property = schema.properties && Object.hasOwn(schema.properties, key)
|
|
217
|
+
? schema.properties[key]
|
|
218
|
+
: undefined;
|
|
192
219
|
if (property)
|
|
193
220
|
errors.push(...validateAgentToolInput(property, item, `${path}.${key}`));
|
|
194
221
|
else if (schema.additionalProperties === false)
|
|
@@ -197,4 +224,3 @@ export function validateAgentToolInput(schema, value, path = "input") {
|
|
|
197
224
|
}
|
|
198
225
|
return errors;
|
|
199
226
|
}
|
|
200
|
-
//# sourceMappingURL=schema.js.map
|