@nexface/chat 0.1.13-alpha.4 → 0.1.13-alpha.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/assistant-attachments.js +60 -16
- package/dist/assistant-components.d.ts +3 -3
- package/dist/assistant-components.js +77 -28
- package/dist/assistant-runtime.d.ts +1 -1
- package/dist/assistant-runtime.js +100 -42
- package/dist/attachments.js +36 -14
- package/dist/client.js +312 -72
- package/dist/config.js +4 -4
- package/dist/default-chat.d.ts +1 -1
- package/dist/default-chat.js +2102 -727
- package/dist/errors.js +55 -17
- package/dist/page-confirmation.js +3 -3
- package/dist/protocol-messages.js +290 -65
- package/dist/protocol-response.js +38 -17
- package/dist/react.js +146 -49
- package/dist/shared/errors.d.ts +1 -1
- package/dist/shared/schema.js +45 -18
- package/dist/storage.js +130 -52
- package/dist/styles.css +99 -20
- package/dist/tool-result.js +7 -2
- package/dist/ui/attachment.aui.d.ts +1 -1
- package/dist/ui/attachment.aui.js +16 -19
- package/dist/ui/code-block.js +2 -2
- package/dist/ui/collapsible.js +2 -2
- package/dist/ui/composer-voice.js +3 -1
- package/dist/ui/dialog.d.ts +1 -1
- package/dist/ui/dialog.js +9 -3
- package/dist/ui/dropdown-menu.js +2 -2
- package/dist/ui/home-demo/composer.js +7 -3
- package/dist/ui/home-demo/greeting.d.ts +1 -1
- package/dist/ui/home-demo/greeting.js +1 -1
- package/dist/ui/home-demo/messages.d.ts +1 -1
- package/dist/ui/home-demo/messages.js +4 -4
- package/dist/ui/home-demo/suggestions.d.ts +1 -1
- package/dist/ui/home-demo/suggestions.js +5 -2
- package/dist/ui/home-demo/surfaces.js +3 -1
- package/dist/ui/home-demo/thread.d.ts +1 -1
- package/dist/ui/home-demo/thread.js +1 -1
- package/dist/ui/home-demo/trace-line.d.ts +1 -1
- package/dist/ui/home-demo/trace-line.js +2 -2
- package/dist/ui/home-demo/trace.d.ts +1 -1
- package/dist/ui/home-demo/trace.js +28 -8
- package/dist/ui/markdown-text.js +2 -2
- package/dist/ui/reasoning.aui.js +2 -2
- package/dist/ui/reasoning.d.ts +1 -1
- package/dist/ui/reasoning.js +6 -8
- package/dist/ui/surface.js +27 -7
- package/dist/ui/thread-list.aui.d.ts +2 -2
- package/dist/ui/thread-list.aui.js +18 -11
- package/dist/ui/tool-group.aui.js +2 -2
- package/dist/ui/tooltip-icon-button.js +1 -1
- package/dist/ui/use-attachment-src.js +1 -1
- package/dist/ui/use-copy-to-clipboard.d.ts +1 -1
- package/dist/ui/use-copy-to-clipboard.js +1 -1
- package/dist/ui/utils.js +9 -2
- package/dist/use-next-message.js +7 -3
- package/dist/voice/use-voice.js +35 -12
- package/package.json +3 -3
|
@@ -1,17 +1,22 @@
|
|
|
1
1
|
export function decodeAgentResponse(messages, snapshot, terminal) {
|
|
2
2
|
const state = snapshot;
|
|
3
3
|
const continuation = state?.nexface;
|
|
4
|
-
if (!continuation ||
|
|
4
|
+
if (!continuation ||
|
|
5
|
+
continuation.version !== 3 ||
|
|
6
|
+
continuation.threadId !== terminal.threadId ||
|
|
7
|
+
!Array.isArray(continuation.timeline)) {
|
|
5
8
|
throw new Error("Invalid Agent recovery state or protocol version. Upgrade client and server together and start a new conversation.");
|
|
6
9
|
}
|
|
7
10
|
const legacy = new Set(["callAppTool", "getSnapshot", "executeCode", "loadSkill", "finish"]);
|
|
8
|
-
if (messages.some((message) => message.role === "assistant" &&
|
|
9
|
-
|
|
11
|
+
if (messages.some((message) => message.role === "assistant" &&
|
|
12
|
+
message.toolCalls?.some((call) => legacy.has(call.function.name))) ||
|
|
13
|
+
continuation.pending?.calls?.some((call) => legacy.has(call.name)))
|
|
10
14
|
throw new Error("Unsupported legacy tool history. Start a new conversation.");
|
|
11
15
|
const history = messages.filter((message) => message.role !== "activity" && message.role !== "reasoning");
|
|
12
16
|
const references = continuation.timeline.flatMap((entry) => {
|
|
13
17
|
if (entry && "internal" in entry) {
|
|
14
|
-
if ("messageId" in entry ||
|
|
18
|
+
if ("messageId" in entry ||
|
|
19
|
+
(entry.internal?.role !== "system" && entry.internal?.role !== "application_context"))
|
|
15
20
|
throw new Error("Invalid internal history reference.");
|
|
16
21
|
return [];
|
|
17
22
|
}
|
|
@@ -19,8 +24,9 @@ export function decodeAgentResponse(messages, snapshot, terminal) {
|
|
|
19
24
|
throw new Error("Invalid Agent history reference.");
|
|
20
25
|
return [entry.messageId];
|
|
21
26
|
});
|
|
22
|
-
if (new Set(messages.map((message) => message.id)).size !== messages.length
|
|
23
|
-
|
|
27
|
+
if (new Set(messages.map((message) => message.id)).size !== messages.length ||
|
|
28
|
+
references.length !== history.length ||
|
|
29
|
+
references.some((id, index) => id !== history[index].id)) {
|
|
24
30
|
throw new Error("Agent messages and recovery state do not match.");
|
|
25
31
|
}
|
|
26
32
|
const outcome = terminal.outcome;
|
|
@@ -35,18 +41,27 @@ export function decodeAgentResponse(messages, snapshot, terminal) {
|
|
|
35
41
|
const interrupt = outcome.interrupts[0];
|
|
36
42
|
const extension = interrupt.metadata?.nexface;
|
|
37
43
|
const call = pending?.calls?.[0];
|
|
38
|
-
if (!extension ||
|
|
39
|
-
|
|
44
|
+
if (!extension ||
|
|
45
|
+
!pending ||
|
|
46
|
+
!Array.isArray(pending.calls) ||
|
|
47
|
+
!call ||
|
|
48
|
+
pending.interruptId !== interrupt.id ||
|
|
49
|
+
interrupt.toolCallId !== call.id ||
|
|
50
|
+
pending.kind !== extension.kind) {
|
|
40
51
|
throw new Error("Invalid remote interrupt.");
|
|
41
52
|
}
|
|
42
|
-
const ownerIndex = history.reduce((last, message, index) => message.role === "assistant" ? index : last, -1);
|
|
53
|
+
const ownerIndex = history.reduce((last, message, index) => (message.role === "assistant" ? index : last), -1);
|
|
43
54
|
const owner = history[ownerIndex];
|
|
44
|
-
const calls = owner?.role === "assistant" ? owner.toolCalls ?? [] : [];
|
|
55
|
+
const calls = owner?.role === "assistant" ? (owner.toolCalls ?? []) : [];
|
|
45
56
|
const first = calls.findIndex((item) => item.id === call.id);
|
|
46
|
-
if (first < 0 ||
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
57
|
+
if (first < 0 ||
|
|
58
|
+
calls.length - first !== pending.calls.length ||
|
|
59
|
+
pending.calls.some((item, index) => item.id !== calls[first + index].id ||
|
|
60
|
+
item.name !== calls[first + index].function.name ||
|
|
61
|
+
item.arguments !== calls[first + index].function.arguments) ||
|
|
62
|
+
history
|
|
63
|
+
.slice(ownerIndex + 1)
|
|
64
|
+
.some((message) => message.role === "tool" && pending.calls.some((item) => item.id === message.toolCallId))) {
|
|
50
65
|
throw new Error("Pending calls do not match their assistant message.");
|
|
51
66
|
}
|
|
52
67
|
const args = JSON.parse(call.arguments);
|
|
@@ -54,14 +69,20 @@ export function decodeAgentResponse(messages, snapshot, terminal) {
|
|
|
54
69
|
throw new Error("Invalid pending tool arguments.");
|
|
55
70
|
const input = args;
|
|
56
71
|
const name = call.name === "nxf_callAppTool" ? input.name : call.name;
|
|
57
|
-
const toolInput = call.name === "nxf_callAppTool"
|
|
72
|
+
const toolInput = call.name === "nxf_callAppTool"
|
|
73
|
+
? input.input
|
|
74
|
+
: Object.fromEntries(Object.entries(input).filter(([key]) => key !== "progressLabel"));
|
|
58
75
|
if (extension.kind !== "client-tool-execution")
|
|
59
76
|
throw new Error("Unsupported remote interrupt.");
|
|
60
77
|
const action = extension.action;
|
|
61
|
-
if (!action ||
|
|
78
|
+
if (!action ||
|
|
79
|
+
action.toolCallId !== call.id ||
|
|
80
|
+
name !== action.definition?.name ||
|
|
81
|
+
JSON.stringify(toolInput) !== JSON.stringify(action.input)) {
|
|
62
82
|
throw new Error("Browser action does not match its original call.");
|
|
63
83
|
}
|
|
64
|
-
if (action.pageDecision !== undefined &&
|
|
84
|
+
if (action.pageDecision !== undefined &&
|
|
85
|
+
(action.pageDecision !== true || name !== "nxf_getSnapshot")) {
|
|
65
86
|
throw new Error("Invalid page decision observation context.");
|
|
66
87
|
}
|
|
67
88
|
return { type: "client-tool-execution", interruptId: interrupt.id, action };
|
package/dist/react.js
CHANGED
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import { createContext, useContext, useEffect, useLayoutEffect, useRef, useReducer, useCallback } from "react";
|
|
2
|
+
import { createContext, useContext, useEffect, useLayoutEffect, useRef, useReducer, useCallback, } from "react";
|
|
3
3
|
import { ToolsProvider } from "@nexface/tools";
|
|
4
|
-
import { PageConfirmationProvider } from "./page-confirmation.js";
|
|
5
|
-
import { createChatClient, getClientController, ClientError } from "./client.js";
|
|
4
|
+
import { PageConfirmationProvider, } from "./page-confirmation.js";
|
|
5
|
+
import { createChatClient, getClientController, ClientError, } from "./client.js";
|
|
6
6
|
import { attachmentContent } from "./attachments.js";
|
|
7
7
|
import { projectMessages } from "./protocol-messages.js";
|
|
8
8
|
import { chatStorageKey, readChatStorage, writeChatStorage } from "./storage.js";
|
|
9
9
|
export function isChatError(error) {
|
|
10
10
|
const value = error;
|
|
11
|
-
return !!value &&
|
|
11
|
+
return (!!value &&
|
|
12
|
+
value.type === "chat_error" &&
|
|
13
|
+
typeof value.code === "string" &&
|
|
14
|
+
typeof value.message === "string");
|
|
12
15
|
}
|
|
13
16
|
const sendGuards = new WeakMap();
|
|
14
17
|
// Read the live Store at draft submission, including updates before React commits.
|
|
@@ -17,21 +20,32 @@ export function assertChatSendable(chat, sessionId) {
|
|
|
17
20
|
}
|
|
18
21
|
const ChatContext = createContext(null);
|
|
19
22
|
const createId = () => globalThis.crypto.randomUUID();
|
|
20
|
-
const textOf = (message) => message.parts
|
|
23
|
+
const textOf = (message) => message.parts
|
|
24
|
+
.filter((part) => part.type === "text")
|
|
25
|
+
.map((part) => part.text)
|
|
26
|
+
.join("");
|
|
21
27
|
const asError = (error) => {
|
|
22
28
|
if (isChatError(error))
|
|
23
29
|
return error;
|
|
24
30
|
const value = error;
|
|
25
|
-
return new ClientError(typeof value?.code === "string"
|
|
31
|
+
return new ClientError(typeof value?.code === "string"
|
|
32
|
+
? value.code
|
|
33
|
+
: typeof value?.name === "string"
|
|
34
|
+
? value.name
|
|
35
|
+
: "CHAT_RUN_ERROR", typeof value?.message === "string" ? value.message : String(error));
|
|
26
36
|
};
|
|
27
37
|
export function NexfaceProvider(props) {
|
|
28
38
|
const { toolRuntime, children, webMcp = true } = props;
|
|
29
39
|
const onError = useRef(props.onError);
|
|
30
|
-
useLayoutEffect(() => {
|
|
40
|
+
useLayoutEffect(() => {
|
|
41
|
+
onError.current = props.onError;
|
|
42
|
+
});
|
|
31
43
|
const runtime = useRef(toolRuntime);
|
|
32
44
|
if (runtime.current !== toolRuntime)
|
|
33
45
|
throw new ClientError("CHAT_RUNTIME_CHANGED", "NexfaceProvider requires a stable ToolRuntime. Remount it to change Runtime.");
|
|
34
|
-
if (Boolean(props.agent) === Boolean(props.run) ||
|
|
46
|
+
if (Boolean(props.agent) === Boolean(props.run) ||
|
|
47
|
+
(props.agent &&
|
|
48
|
+
("toolRuntime" in props.agent || "toolBridge" in props.agent || "run" in props.agent)))
|
|
35
49
|
throw new ClientError("INVALID_INPUT", "Provide exactly one agent configuration or run function.");
|
|
36
50
|
const pageTools = useRef(new Set());
|
|
37
51
|
const [, render] = useReducer((n) => n + 1, 0);
|
|
@@ -40,29 +54,57 @@ export function NexfaceProvider(props) {
|
|
|
40
54
|
const changed = useCallback(() => render(), []);
|
|
41
55
|
const createSessionValue = useCallback((saved) => {
|
|
42
56
|
const current = props;
|
|
43
|
-
const client = createChatClient({
|
|
57
|
+
const client = createChatClient({
|
|
58
|
+
...(current.run
|
|
59
|
+
? { toolRuntime, run: current.run }
|
|
60
|
+
: { toolRuntime, agent: current.agent }),
|
|
61
|
+
onError: (event) => onError.current?.(event),
|
|
62
|
+
});
|
|
44
63
|
const controller = getClientController(client);
|
|
45
64
|
if (saved)
|
|
46
65
|
controller.id = saved.id;
|
|
47
66
|
controller.setPageResolver((name) => pageTools.current.has(name));
|
|
48
67
|
if (saved)
|
|
49
68
|
controller.restore(saved.messages);
|
|
50
|
-
return {
|
|
69
|
+
return {
|
|
70
|
+
id: saved?.id ?? controller.id,
|
|
71
|
+
client,
|
|
72
|
+
...(saved?.title ? { title: saved.title } : {}),
|
|
73
|
+
unsubscribe: client.subscribe(changed),
|
|
74
|
+
};
|
|
51
75
|
}, [props.agent, props.run, toolRuntime, changed]);
|
|
52
76
|
if (!storeRef.current) {
|
|
53
77
|
const first = createSessionValue();
|
|
54
|
-
storeRef.current = {
|
|
78
|
+
storeRef.current = {
|
|
79
|
+
sessions: [first],
|
|
80
|
+
activeId: first.id,
|
|
81
|
+
loaded: false,
|
|
82
|
+
switching: false,
|
|
83
|
+
version: { value: null },
|
|
84
|
+
};
|
|
55
85
|
}
|
|
56
86
|
const store = storeRef.current;
|
|
57
87
|
const active = () => store.sessions.find((session) => session.id === store.activeId);
|
|
58
|
-
const find = (id) => {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
const
|
|
65
|
-
|
|
88
|
+
const find = (id) => {
|
|
89
|
+
const session = store.sessions.find((item) => item.id === id);
|
|
90
|
+
if (!session)
|
|
91
|
+
throw new ClientError("UNKNOWN_SESSION", "Unknown chat session.");
|
|
92
|
+
return session;
|
|
93
|
+
};
|
|
94
|
+
const busy = () => store.switching ||
|
|
95
|
+
store.sessions.some((session) => getClientController(session.client).busy && session.client.getStatus() !== "waiting");
|
|
96
|
+
const assertIdle = (code = "CHAT_RUN_ACTIVE") => {
|
|
97
|
+
if (busy())
|
|
98
|
+
throw new ClientError(code, "Wait for the active task to finish.");
|
|
99
|
+
};
|
|
100
|
+
const release = (session) => {
|
|
101
|
+
session.unsubscribe();
|
|
102
|
+
session.client.dispose();
|
|
103
|
+
};
|
|
104
|
+
const cancelWaiting = () => {
|
|
105
|
+
if (active().client.getStatus() === "waiting")
|
|
106
|
+
active().client.cancel();
|
|
107
|
+
};
|
|
66
108
|
useEffect(() => {
|
|
67
109
|
if (store.loaded)
|
|
68
110
|
return;
|
|
@@ -85,9 +127,17 @@ export function NexfaceProvider(props) {
|
|
|
85
127
|
const persist = () => {
|
|
86
128
|
if (store.warning)
|
|
87
129
|
return;
|
|
88
|
-
const stored = {
|
|
130
|
+
const stored = {
|
|
131
|
+
activeSessionId: store.activeId,
|
|
132
|
+
sessions: store.sessions.map((session) => ({
|
|
133
|
+
id: session.id,
|
|
134
|
+
...(session.title ? { title: session.title } : {}),
|
|
135
|
+
messages: [...projectMessages(session.client.getMessages())],
|
|
136
|
+
})),
|
|
137
|
+
};
|
|
89
138
|
if (writeChatStorage(chatStorageKey, stored, store.version) === "conflict") {
|
|
90
|
-
store.warning =
|
|
139
|
+
store.warning =
|
|
140
|
+
"其他标签页已更新历史会话,本页已暂停保存。请先复制本页需要保留的新消息,再刷新以加载最新历史。";
|
|
91
141
|
changed();
|
|
92
142
|
}
|
|
93
143
|
};
|
|
@@ -103,8 +153,10 @@ export function NexfaceProvider(props) {
|
|
|
103
153
|
mounted.current = false;
|
|
104
154
|
window.removeEventListener("pagehide", flush);
|
|
105
155
|
flush();
|
|
106
|
-
queueMicrotask(() => {
|
|
107
|
-
|
|
156
|
+
queueMicrotask(() => {
|
|
157
|
+
if (!mounted.current)
|
|
158
|
+
store.sessions.forEach(release);
|
|
159
|
+
});
|
|
108
160
|
};
|
|
109
161
|
}, []);
|
|
110
162
|
const createSession = useCallback(() => {
|
|
@@ -152,7 +204,7 @@ export function NexfaceProvider(props) {
|
|
|
152
204
|
if (previous.title)
|
|
153
205
|
session.title = previous.title;
|
|
154
206
|
release(previous);
|
|
155
|
-
store.sessions = store.sessions.map((item) => item === previous ? session : item);
|
|
207
|
+
store.sessions = store.sessions.map((item) => (item === previous ? session : item));
|
|
156
208
|
if (store.activeId === id)
|
|
157
209
|
store.activeId = session.id;
|
|
158
210
|
changed();
|
|
@@ -175,11 +227,14 @@ export function NexfaceProvider(props) {
|
|
|
175
227
|
const replacement = { ...session, client, unsubscribe: client.subscribe(changed) };
|
|
176
228
|
previous = session;
|
|
177
229
|
retainedCount = client.getMessages().length;
|
|
178
|
-
store.sessions = store.sessions.map((item) => item === session ? replacement : item);
|
|
230
|
+
store.sessions = store.sessions.map((item) => (item === session ? replacement : item));
|
|
179
231
|
session = replacement;
|
|
180
232
|
delete store.completion;
|
|
181
233
|
}
|
|
182
|
-
const parts = [
|
|
234
|
+
const parts = [
|
|
235
|
+
...(content ? [{ type: "text", text: content }] : []),
|
|
236
|
+
...attachments.flatMap(attachmentContent),
|
|
237
|
+
];
|
|
183
238
|
try {
|
|
184
239
|
const messageCount = session.client.getMessages().length;
|
|
185
240
|
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 +250,7 @@ export function NexfaceProvider(props) {
|
|
|
195
250
|
// Keep the original conversation if validation or Runtime acquisition rejected the edit.
|
|
196
251
|
if (previous && session.client.getMessages().length === retainedCount) {
|
|
197
252
|
release(session);
|
|
198
|
-
store.sessions = store.sessions.map((item) => item === session ? previous : item);
|
|
253
|
+
store.sessions = store.sessions.map((item) => (item === session ? previous : item));
|
|
199
254
|
previous = undefined;
|
|
200
255
|
}
|
|
201
256
|
changed();
|
|
@@ -219,29 +274,69 @@ export function NexfaceProvider(props) {
|
|
|
219
274
|
const approval = client.getApproval();
|
|
220
275
|
const error = client.getError();
|
|
221
276
|
const value = {
|
|
222
|
-
sessions: store.sessions
|
|
277
|
+
sessions: store.sessions
|
|
278
|
+
.map((item, index) => {
|
|
223
279
|
const messages = projectMessages(item.client.getMessages());
|
|
224
280
|
const first = messages.find((message) => message.role === "user");
|
|
225
281
|
const title = first ? textOf(first).trim() : "";
|
|
226
|
-
return {
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
282
|
+
return {
|
|
283
|
+
id: item.id,
|
|
284
|
+
title: item.title ||
|
|
285
|
+
(title ? (title.length > 24 ? title.slice(0, 24) + "…" : title) : `会话 ${index + 1}`),
|
|
286
|
+
messageCount: messages.length,
|
|
287
|
+
};
|
|
288
|
+
})
|
|
289
|
+
.reverse(),
|
|
290
|
+
activeSessionId: session.id,
|
|
291
|
+
messages: projectMessages(client.getMessages()),
|
|
292
|
+
status: store.switching
|
|
293
|
+
? "switching-model"
|
|
294
|
+
: controller.cancelling
|
|
295
|
+
? "cancelling"
|
|
296
|
+
: client.getStatus(),
|
|
230
297
|
model: controller.agent?.model ?? { provider: "server", model: "server" },
|
|
231
|
-
...(approval ? { interrupt: approval } : {}),
|
|
298
|
+
...(approval ? { interrupt: approval } : {}),
|
|
299
|
+
...(store.warning ? { storageWarning: store.warning } : {}),
|
|
232
300
|
...(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
|
-
|
|
301
|
+
...(store.completion?.sessionId === session.id
|
|
302
|
+
? { completed: { id: store.completion.id, result: store.completion.result } }
|
|
303
|
+
: {}),
|
|
304
|
+
consumeCompletion(id) {
|
|
305
|
+
if (store.consumed === id)
|
|
306
|
+
return false;
|
|
307
|
+
store.consumed = id;
|
|
308
|
+
return true;
|
|
309
|
+
},
|
|
310
|
+
sendMessage,
|
|
311
|
+
createSession,
|
|
312
|
+
switchSession,
|
|
313
|
+
deleteSession,
|
|
314
|
+
clearSession,
|
|
315
|
+
clearAllSessions() {
|
|
316
|
+
assertIdle();
|
|
317
|
+
store.sessions.forEach(release);
|
|
318
|
+
const first = createSessionValue();
|
|
319
|
+
store.sessions = [first];
|
|
320
|
+
store.activeId = first.id;
|
|
321
|
+
changed();
|
|
322
|
+
},
|
|
323
|
+
renameSession(id, title) {
|
|
324
|
+
if (!title.trim())
|
|
325
|
+
throw new ClientError("INVALID_INPUT", "Session title is required.");
|
|
326
|
+
find(id).title = title.trim().slice(0, 80);
|
|
327
|
+
changed();
|
|
328
|
+
},
|
|
329
|
+
cancel() {
|
|
330
|
+
active().client.cancel();
|
|
331
|
+
},
|
|
332
|
+
confirmInterrupt() {
|
|
333
|
+
if (approval?.mode === "chat")
|
|
334
|
+
void client.respondToApproval(approval.id, { approved: true }).catch(() => undefined);
|
|
335
|
+
},
|
|
336
|
+
rejectInterrupt() {
|
|
337
|
+
if (approval?.mode === "chat")
|
|
338
|
+
void client.respondToApproval(approval.id, { approved: false }).catch(() => undefined);
|
|
339
|
+
},
|
|
245
340
|
async switchModel(model) {
|
|
246
341
|
if (busy() || active().client.getApproval())
|
|
247
342
|
throw new ClientError("CHAT_MODEL_SWITCH_BLOCKED", "The chat is busy.");
|
|
@@ -260,10 +355,12 @@ export function NexfaceProvider(props) {
|
|
|
260
355
|
}
|
|
261
356
|
},
|
|
262
357
|
};
|
|
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
|
-
|
|
358
|
+
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) => {
|
|
359
|
+
if (registered)
|
|
360
|
+
pageTools.current.add(name);
|
|
361
|
+
else
|
|
362
|
+
pageTools.current.delete(name);
|
|
363
|
+
}, children: children }) }) }));
|
|
267
364
|
}
|
|
268
365
|
export function useChat() {
|
|
269
366
|
const chat = useContext(ChatContext);
|
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/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)
|