@nexface/chat 0.1.21-alpha.7 → 0.1.21-alpha.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/client.js +85 -47
- package/dist/default-chat.js +20544 -20571
- package/dist/react.js +0 -81
- package/dist/shared/errors.d.ts +1 -1
- package/dist/shared/errors.js +1 -1
- package/dist/shared/runtime-execution.d.ts +6 -0
- package/dist/shared/runtime-execution.js +3 -0
- package/dist/styles.css +3 -0
- package/dist/tool-confirmation-card.d.ts +1 -2
- package/dist/tool-confirmation-card.js +7 -27
- package/dist/tool-confirmation.d.ts +1 -23
- package/dist/tool-confirmation.js +1 -9
- package/package.json +3 -3
package/dist/react.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import { confirmationControllers, confirmationRequest, } from "./tool-confirmation.js";
|
|
3
2
|
import { reportError } from "./errors.js";
|
|
4
3
|
import { createContext, useContext, useEffect, useLayoutEffect, useRef, useReducer, useCallback, } from "react";
|
|
5
4
|
import { ToolsProvider } from "@nexface/tools";
|
|
@@ -77,10 +76,6 @@ export function NexfaceProvider(props) {
|
|
|
77
76
|
if (saved)
|
|
78
77
|
controller.restore(saved.messages, saved.appendOnly);
|
|
79
78
|
return {
|
|
80
|
-
confirmations: {
|
|
81
|
-
restored: new Set(saved?.messages.flatMap((message) => message.parts.flatMap((part) => (part.type === "tool" ? [part.toolCallId] : []))) ?? []),
|
|
82
|
-
decisions: new Map(),
|
|
83
|
-
},
|
|
84
79
|
id: saved?.id ?? controller.id,
|
|
85
80
|
client,
|
|
86
81
|
...(saved?.title ? { title: saved.title } : {}),
|
|
@@ -283,82 +278,6 @@ export function NexfaceProvider(props) {
|
|
|
283
278
|
throw new ClientError("CHAT_RUN_ACTIVE", "A task is already running. Try sending again later.");
|
|
284
279
|
getClientController(active().client).assertRecoveryAvailable();
|
|
285
280
|
});
|
|
286
|
-
const confirmationState = (sessionId, request) => {
|
|
287
|
-
const session = store.sessions.find((item) => item.id === sessionId);
|
|
288
|
-
if (!session)
|
|
289
|
-
return "expired";
|
|
290
|
-
const decided = session.confirmations.decisions.get(request.toolCallId);
|
|
291
|
-
if (decided)
|
|
292
|
-
return decided;
|
|
293
|
-
if (session.confirmations.restored.has(request.toolCallId))
|
|
294
|
-
return "expired";
|
|
295
|
-
const messages = projectMessages(session.client.getMessages());
|
|
296
|
-
const lastUser = messages.reduce((last, message, index) => (message.role === "user" ? index : last), -1);
|
|
297
|
-
const owner = messages.findIndex((message) => message.parts.some((part) => {
|
|
298
|
-
const actual = confirmationRequest(part);
|
|
299
|
-
return (actual?.toolCallId === request.toolCallId &&
|
|
300
|
-
actual.requestId === request.requestId &&
|
|
301
|
-
actual.toolName === request.toolName);
|
|
302
|
-
}));
|
|
303
|
-
for (const message of messages)
|
|
304
|
-
for (const part of message.parts) {
|
|
305
|
-
if (part.type !== "tool" || part.name !== request.toolName)
|
|
306
|
-
continue;
|
|
307
|
-
try {
|
|
308
|
-
const decision = JSON.parse(part.arguments)?.confirmation;
|
|
309
|
-
if (decision?.requestId === request.requestId && typeof decision.approved === "boolean")
|
|
310
|
-
return decision.approved ? "approved" : "rejected";
|
|
311
|
-
}
|
|
312
|
-
catch {
|
|
313
|
-
/* Streaming arguments may be incomplete. */
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
return owner > lastUser && store.activeId === sessionId ? "pending" : "expired";
|
|
317
|
-
};
|
|
318
|
-
confirmationControllers.set(sendMessage, {
|
|
319
|
-
state: confirmationState,
|
|
320
|
-
async respond(sessionId, request, approved) {
|
|
321
|
-
sendGuards.get(sendMessage)?.(sessionId);
|
|
322
|
-
if (confirmationState(sessionId, request) !== "pending")
|
|
323
|
-
throw new ClientError("INVALID_INPUT", "This confirmation is no longer available.");
|
|
324
|
-
const session = active();
|
|
325
|
-
const controller = getClientController(session.client);
|
|
326
|
-
const messageId = createId();
|
|
327
|
-
session.confirmations.decisions.set(request.toolCallId, "sending");
|
|
328
|
-
changed();
|
|
329
|
-
try {
|
|
330
|
-
const decision = approved ? "确认" : "拒绝";
|
|
331
|
-
const pending = controller.send(JSON.stringify({
|
|
332
|
-
userDecision: {
|
|
333
|
-
toolName: request.toolName,
|
|
334
|
-
toolCallId: request.toolCallId,
|
|
335
|
-
requestId: request.requestId,
|
|
336
|
-
approved,
|
|
337
|
-
},
|
|
338
|
-
message: `${decision}:${request.message}`,
|
|
339
|
-
}), {
|
|
340
|
-
id: messageId,
|
|
341
|
-
role: "user",
|
|
342
|
-
parts: [{ type: "text", text: `${decision}此操作` }],
|
|
343
|
-
});
|
|
344
|
-
if (controller.getMessages().some((message) => message.id === messageId)) {
|
|
345
|
-
session.confirmations.decisions.set(request.toolCallId, approved ? "approved" : "rejected");
|
|
346
|
-
delete store.completion;
|
|
347
|
-
changed();
|
|
348
|
-
}
|
|
349
|
-
const result = await pending;
|
|
350
|
-
store.completion = { id: createId(), sessionId, result };
|
|
351
|
-
}
|
|
352
|
-
finally {
|
|
353
|
-
// An admitted reply remains answered even if the following model run fails.
|
|
354
|
-
if (controller.getMessages().some((message) => message.id === messageId))
|
|
355
|
-
session.confirmations.decisions.set(request.toolCallId, approved ? "approved" : "rejected");
|
|
356
|
-
else
|
|
357
|
-
session.confirmations.decisions.delete(request.toolCallId);
|
|
358
|
-
changed();
|
|
359
|
-
}
|
|
360
|
-
},
|
|
361
|
-
});
|
|
362
281
|
const session = active();
|
|
363
282
|
const client = session.client;
|
|
364
283
|
const controller = getClientController(client);
|
package/dist/shared/errors.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
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_CONFIRMATION_RESULT", "
|
|
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_CONFIRMATION_RESULT", "INVALID_TOOL_RUNTIME", "OPERATION_BUSY", "TOOL_CANCELLED", "TOOL_CONFIRMATION_INVALIDATED", "TOOL_CONFIRMATION_UNAVAILABLE", "TOOL_CONFIRMATION_REJECTED", "TOOL_ERROR", "TOOL_REGISTRATION_TIMEOUT", "TOOL_SKIPPED", "TOOL_UNAVAILABLE", "UNKNOWN_TOOL"];
|
|
2
2
|
type AgentToolErrorCode = (typeof TOOL_ERROR_CODES)[number];
|
|
3
3
|
export declare class AgentToolError extends Error {
|
|
4
4
|
readonly code: AgentToolErrorCode;
|
package/dist/shared/errors.js
CHANGED
|
@@ -11,11 +11,11 @@ export const TOOL_ERROR_CODES = [
|
|
|
11
11
|
"DUPLICATE_BINDING",
|
|
12
12
|
"INVALID_INPUT",
|
|
13
13
|
"INVALID_CONFIRMATION_RESULT",
|
|
14
|
-
"TOOL_CONFIRMATION_LIMIT",
|
|
15
14
|
"INVALID_TOOL_RUNTIME",
|
|
16
15
|
"OPERATION_BUSY",
|
|
17
16
|
"TOOL_CANCELLED",
|
|
18
17
|
"TOOL_CONFIRMATION_INVALIDATED",
|
|
18
|
+
"TOOL_CONFIRMATION_UNAVAILABLE",
|
|
19
19
|
"TOOL_CONFIRMATION_REJECTED",
|
|
20
20
|
"TOOL_ERROR",
|
|
21
21
|
"TOOL_REGISTRATION_TIMEOUT",
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import type { ToolRuntime } from "@nexface/tools";
|
|
2
2
|
export declare const pageDecisionSymbol: unique symbol;
|
|
3
|
+
export declare const hostConfirmationSymbol: unique symbol;
|
|
4
|
+
export declare const executionStartSymbol: unique symbol;
|
|
5
|
+
export declare const executionStartedSymbol: unique symbol;
|
|
6
|
+
export type HostConfirmation = (message: string, signal: AbortSignal) => Promise<boolean>;
|
|
3
7
|
export type InternalExecutionOptions = NonNullable<Parameters<ToolRuntime["execute"]>[2]> & {
|
|
4
8
|
readonly [pageDecisionSymbol]?: boolean;
|
|
9
|
+
readonly [hostConfirmationSymbol]?: HostConfirmation;
|
|
10
|
+
readonly [executionStartSymbol]?: () => void;
|
|
5
11
|
};
|
|
@@ -1,2 +1,5 @@
|
|
|
1
1
|
// Generated from shared/runtime-execution.ts. Do not edit manually.
|
|
2
2
|
export const pageDecisionSymbol = Symbol.for("@nexface/tools/page-decision");
|
|
3
|
+
export const hostConfirmationSymbol = Symbol.for("@nexface/tools/host-confirmation");
|
|
4
|
+
export const executionStartSymbol = Symbol.for("@nexface/tools/execution-start");
|
|
5
|
+
export const executionStartedSymbol = Symbol.for("@nexface/tools/execution-started");
|
package/dist/styles.css
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import type { UIMessagePart } from "./messages.js";
|
|
2
|
-
export declare function
|
|
3
|
-
sessionId: string;
|
|
2
|
+
export declare function ExpiredToolConfirmationCards({ parts }: {
|
|
4
3
|
parts: readonly UIMessagePart[];
|
|
5
4
|
}): import("react").JSX.Element;
|
|
@@ -1,33 +1,13 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
-
import {
|
|
3
|
-
import { useChat } from "./react.js";
|
|
4
|
-
import { confirmationControllers, confirmationRequest } from "./tool-confirmation.js";
|
|
5
|
-
import { Button } from "./ui/button.js";
|
|
2
|
+
import { parseConfirmationResult } from "./tool-confirmation.js";
|
|
6
3
|
import { cn } from "./ui/utils.js";
|
|
7
|
-
export function
|
|
8
|
-
const chat = useChat();
|
|
9
|
-
const [error, setError] = useState();
|
|
10
|
-
const controller = confirmationControllers.get(chat.sendMessage);
|
|
4
|
+
export function ExpiredToolConfirmationCards({ parts }) {
|
|
11
5
|
return (_jsx(_Fragment, { children: parts.map((part) => {
|
|
12
|
-
|
|
13
|
-
if (!request || !controller)
|
|
6
|
+
if (part.type !== "tool" || part.isError || !part.result)
|
|
14
7
|
return null;
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
void controller.respond(sessionId, request, approved).catch(() => {
|
|
20
|
-
if (controller.state(sessionId, request) === "pending")
|
|
21
|
-
setError({
|
|
22
|
-
toolCallId: request.toolCallId,
|
|
23
|
-
message: "Could not send your response. Please try again.",
|
|
24
|
-
});
|
|
25
|
-
});
|
|
26
|
-
};
|
|
27
|
-
return (_jsxs("section", { "aria-label": "\u64CD\u4F5C\u786E\u8BA4", className: cn("my-3 rounded-lg border p-4"), children: [_jsx("p", { className: cn("mb-3 whitespace-pre-wrap"), children: request.message }), state === "pending" || state === "sending" ? (_jsxs("div", { className: cn("flex gap-2"), children: [_jsx(Button, { disabled: disabled, onClick: () => respond(true), children: "\u786E\u8BA4" }), _jsx(Button, { variant: "outline", disabled: disabled, onClick: () => respond(false), children: "\u62D2\u7EDD" })] })) : (_jsx("p", { role: "status", children: state === "approved"
|
|
28
|
-
? "已确认"
|
|
29
|
-
: state === "rejected"
|
|
30
|
-
? "已拒绝"
|
|
31
|
-
: "此确认已失效,请重新发起操作。" })), error?.toolCallId === request.toolCallId ? _jsx("p", { role: "alert", children: error.message }) : null] }, request.toolCallId));
|
|
8
|
+
const message = parseConfirmationResult(part.result);
|
|
9
|
+
if (!message)
|
|
10
|
+
return null;
|
|
11
|
+
return (_jsxs("section", { "aria-label": "\u64CD\u4F5C\u786E\u8BA4", className: cn("my-3 rounded-lg border p-4"), children: [_jsx("p", { className: cn("mb-3 whitespace-pre-wrap"), children: message }), _jsx("p", { role: "status", children: "\u6B64\u786E\u8BA4\u5DF2\u5931\u6548\uFF0C\u8BF7\u91CD\u65B0\u53D1\u8D77\u64CD\u4F5C\u3002" })] }, part.toolCallId));
|
|
32
12
|
}) }));
|
|
33
13
|
}
|
|
@@ -1,23 +1 @@
|
|
|
1
|
-
|
|
2
|
-
export type ConfirmationRequest = {
|
|
3
|
-
toolCallId: string;
|
|
4
|
-
toolName: string;
|
|
5
|
-
requestId: string;
|
|
6
|
-
message: string;
|
|
7
|
-
};
|
|
8
|
-
export declare function parseConfirmationResult(content: string): {
|
|
9
|
-
requestId: string;
|
|
10
|
-
message: string;
|
|
11
|
-
} | undefined;
|
|
12
|
-
export declare function confirmationRequest(part: UIMessagePart): ConfirmationRequest | undefined;
|
|
13
|
-
export type ConfirmationState = "pending" | "sending" | "approved" | "rejected" | "expired";
|
|
14
|
-
export type ConfirmationSession = {
|
|
15
|
-
restored: Set<string>;
|
|
16
|
-
decisions: Map<string, ConfirmationState>;
|
|
17
|
-
};
|
|
18
|
-
type Controller = {
|
|
19
|
-
state(sessionId: string, request: ConfirmationRequest): ConfirmationState;
|
|
20
|
-
respond(sessionId: string, request: ConfirmationRequest, approved: boolean): Promise<void>;
|
|
21
|
-
};
|
|
22
|
-
export declare const confirmationControllers: WeakMap<(text: string, attachments?: import("./attachments.js").Attachment[], revisionOf?: string) => Promise<import("@nexface/agent").AgentRunResult>, Controller>;
|
|
23
|
-
export {};
|
|
1
|
+
export declare function parseConfirmationResult(content: string): string | undefined;
|
|
@@ -8,18 +8,10 @@ export function parseConfirmationResult(content) {
|
|
|
8
8
|
typeof result.message === "string" &&
|
|
9
9
|
result.message.trim() &&
|
|
10
10
|
!parsed.error) {
|
|
11
|
-
return
|
|
11
|
+
return result.message;
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
14
|
catch {
|
|
15
15
|
/* Other tool results are not confirmation requests. */
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
|
-
export function confirmationRequest(part) {
|
|
19
|
-
if (part.type !== "tool" || part.isError || !part.result)
|
|
20
|
-
return;
|
|
21
|
-
const result = parseConfirmationResult(part.result);
|
|
22
|
-
return result ? { ...result, toolCallId: part.toolCallId, toolName: part.name } : undefined;
|
|
23
|
-
}
|
|
24
|
-
// Internal bridge: the public useChat and Chat interfaces stay unchanged.
|
|
25
|
-
export const confirmationControllers = new WeakMap();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nexface/chat",
|
|
3
|
-
"version": "0.1.21-alpha.
|
|
3
|
+
"version": "0.1.21-alpha.8",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"types": "./dist/index.d.ts",
|
|
6
6
|
"sideEffects": [
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
21
|
"@ag-ui/core": "0.0.59",
|
|
22
|
-
"@nexface/agent": "0.1.21-alpha.
|
|
22
|
+
"@nexface/agent": "0.1.21-alpha.8",
|
|
23
23
|
"class-variance-authority": "0.7.1",
|
|
24
24
|
"clsx": "2.1.1",
|
|
25
25
|
"fast-json-patch": "3.1.1",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"use-sync-external-store": "1.7.0"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
|
31
|
-
"@nexface/tools": "0.1.21-alpha.
|
|
31
|
+
"@nexface/tools": "0.1.21-alpha.8",
|
|
32
32
|
"react": ">=17",
|
|
33
33
|
"react-dom": ">=17"
|
|
34
34
|
},
|