@nexface/tools 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/confirmation.d.ts +7 -20
- package/dist/confirmation.js +48 -48
- package/dist/runtime.d.ts +3 -3
- package/dist/runtime.js +47 -21
- 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/webmcp.js +30 -102
- package/package.json +1 -1
- package/dist/confirmation-protocol.d.ts +0 -40
- package/dist/confirmation-protocol.js +0 -83
package/dist/confirmation.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { AgentToolExecutor } from "./types.js";
|
|
1
|
+
import type { AgentToolDefinition, AgentToolExecutor } from "./types.js";
|
|
2
2
|
export type ConfirmationCallback<TInput> = (context: {
|
|
3
3
|
input: TInput;
|
|
4
4
|
signal: AbortSignal;
|
|
@@ -7,23 +7,10 @@ export type ConfirmationCallback<TInput> = (context: {
|
|
|
7
7
|
} | Promise<boolean | {
|
|
8
8
|
message: string;
|
|
9
9
|
}>;
|
|
10
|
-
|
|
10
|
+
export declare function wrapConfirmationInput(definition: AgentToolDefinition<any, any>): AgentToolDefinition<{
|
|
11
|
+
input: Record<string, unknown>;
|
|
12
|
+
}, unknown>;
|
|
13
|
+
export declare function executeWithConfirmation(input: unknown, context: Parameters<AgentToolExecutor<any, any>>[1], lifecycle: AbortSignal, callbacks: {
|
|
11
14
|
execute: AgentToolExecutor<any, any>;
|
|
12
|
-
confirm
|
|
13
|
-
}
|
|
14
|
-
export declare function createConfirmationRequests(lifecycle: AbortSignal): {
|
|
15
|
-
resolveInput(requestId: string): unknown;
|
|
16
|
-
decide(requestId: string, approved: boolean, context: Parameters<AgentToolExecutor<any, any>>[1]): Promise<any>;
|
|
17
|
-
start(input: unknown, context: Parameters<AgentToolExecutor<any, any>>[1], callbacks: Callbacks): Promise<{
|
|
18
|
-
kind: "completed";
|
|
19
|
-
result: any;
|
|
20
|
-
requestId?: never;
|
|
21
|
-
message?: never;
|
|
22
|
-
} | {
|
|
23
|
-
kind: "pending";
|
|
24
|
-
requestId: `${string}-${string}-${string}-${string}-${string}`;
|
|
25
|
-
message: string;
|
|
26
|
-
result?: never;
|
|
27
|
-
}>;
|
|
28
|
-
};
|
|
29
|
-
export {};
|
|
15
|
+
confirm: ConfirmationCallback<any>;
|
|
16
|
+
}, askHost?: (message: string, signal: AbortSignal) => Promise<boolean>): Promise<unknown>;
|
package/dist/confirmation.js
CHANGED
|
@@ -1,54 +1,54 @@
|
|
|
1
|
+
import { defineAgentTool } from "./definition.js";
|
|
1
2
|
import { AgentToolError } from "./shared/errors.js";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
3
|
+
export function wrapConfirmationInput(definition) {
|
|
4
|
+
return defineAgentTool({
|
|
5
|
+
...definition,
|
|
6
|
+
inputSchema: {
|
|
7
|
+
type: "object",
|
|
8
|
+
properties: { input: definition.inputSchema },
|
|
9
|
+
required: ["input"],
|
|
10
|
+
additionalProperties: false,
|
|
9
11
|
},
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
export async function executeWithConfirmation(input, context, lifecycle, callbacks, askHost) {
|
|
15
|
+
const signal = AbortSignal.any([context.signal, lifecycle]);
|
|
16
|
+
signal.throwIfAborted();
|
|
17
|
+
const snapshot = structuredClone(input);
|
|
18
|
+
const decision = await waitForDecision(() => callbacks.confirm({ input: structuredClone(snapshot), signal }), signal);
|
|
19
|
+
if (typeof decision !== "boolean" &&
|
|
20
|
+
(!decision ||
|
|
21
|
+
typeof decision !== "object" ||
|
|
22
|
+
typeof decision.message !== "string" ||
|
|
23
|
+
!decision.message.trim() ||
|
|
24
|
+
Object.keys(decision).some((key) => key !== "message"))) {
|
|
25
|
+
throw new AgentToolError("INVALID_CONFIRMATION_RESULT", "confirm must return a boolean or { message: non-empty string }.", { toolExecuted: false });
|
|
26
|
+
}
|
|
27
|
+
signal.throwIfAborted();
|
|
28
|
+
if (typeof decision === "boolean") {
|
|
29
|
+
if (!decision)
|
|
30
|
+
return { status: "rejected", toolExecuted: false };
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
if (!askHost) {
|
|
34
|
+
throw new AgentToolError("TOOL_CONFIRMATION_UNAVAILABLE", "This tool requires a host that can collect user confirmation.", { toolExecuted: false });
|
|
35
|
+
}
|
|
36
|
+
let approved;
|
|
37
|
+
try {
|
|
38
|
+
approved = await askHost(decision.message, signal);
|
|
30
39
|
signal.throwIfAborted();
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
44
|
-
if (pending.size >= 128) {
|
|
45
|
-
throw new AgentToolError("TOOL_CONFIRMATION_LIMIT", "Too many pending confirmations for this tool. Resolve or reject existing requests first.", { toolExecuted: false });
|
|
46
|
-
}
|
|
47
|
-
const requestId = crypto.randomUUID();
|
|
48
|
-
pending.set(requestId, { input: snapshot, execute: callbacks.execute });
|
|
49
|
-
return { kind: "pending", requestId, message: decision.message };
|
|
50
|
-
},
|
|
51
|
-
};
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
if (lifecycle.aborted && !context.signal.aborted)
|
|
43
|
+
throw new AgentToolError("TOOL_CONFIRMATION_INVALIDATED", "The Tool Binding changed while confirmation was pending.", { toolExecuted: false });
|
|
44
|
+
throw error;
|
|
45
|
+
}
|
|
46
|
+
if (approved !== true)
|
|
47
|
+
throw new AgentToolError("TOOL_CONFIRMATION_REJECTED", "The user rejected this tool call.", {
|
|
48
|
+
toolExecuted: false,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
return callbacks.execute(snapshot, context);
|
|
52
52
|
}
|
|
53
53
|
function waitForDecision(run, signal) {
|
|
54
54
|
return new Promise((resolve, reject) => {
|
package/dist/runtime.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type ConfirmationCallback } from "./confirmation.js";
|
|
2
2
|
import { type InternalExecutionOptions } from "./shared/runtime-execution.js";
|
|
3
3
|
import type { PageActionFeedback } from "./browser/feedback.js";
|
|
4
4
|
import { type TaskProgress } from "./shared/runtime-controller.js";
|
|
@@ -125,7 +125,7 @@ export declare class AgentContextError extends Error {
|
|
|
125
125
|
constructor(code: AgentContextErrorCode, message: string);
|
|
126
126
|
}
|
|
127
127
|
export interface InternalToolRuntime extends ToolRuntime, EventTarget {
|
|
128
|
-
|
|
128
|
+
hasWrappedConfirmationInput(tool: RuntimeAgentTool): boolean;
|
|
129
129
|
beginInternalOperation(options?: {
|
|
130
130
|
signal?: AbortSignal;
|
|
131
131
|
onCancel?: () => void;
|
|
@@ -195,7 +195,7 @@ export declare class ToolRuntimeImplementation extends EventTarget implements To
|
|
|
195
195
|
};
|
|
196
196
|
resolveTarget?: (input: unknown) => HTMLElement | undefined;
|
|
197
197
|
}): () => void;
|
|
198
|
-
|
|
198
|
+
hasWrappedConfirmationInput(tool: RuntimeAgentTool): boolean;
|
|
199
199
|
registerContext(input: AgentContextInput, options?: {
|
|
200
200
|
signal?: AbortSignal;
|
|
201
201
|
}): AgentContextRegistration;
|
package/dist/runtime.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { executeWithConfirmation, wrapConfirmationInput, } from "./confirmation.js";
|
|
2
2
|
import { searchNavigationTools, searchToolDefinition } from "./tool-search.js";
|
|
3
|
-
import { pageDecisionSymbol } from "./shared/runtime-execution.js";
|
|
3
|
+
import { executionStartSymbol, executionStartedSymbol, hostConfirmationSymbol, pageDecisionSymbol, } from "./shared/runtime-execution.js";
|
|
4
4
|
import { PageCommandError } from "./browser/locator.js";
|
|
5
5
|
import { measurePagePhase } from "./browser/diagnostics.js";
|
|
6
6
|
import { runtimeControllerSymbol, } from "./shared/runtime-controller.js";
|
|
@@ -154,9 +154,12 @@ export class ToolRuntimeImplementation extends EventTarget {
|
|
|
154
154
|
this.#pageActionFeedback = undefined;
|
|
155
155
|
};
|
|
156
156
|
}
|
|
157
|
-
#emitExecution(event) {
|
|
157
|
+
#emitExecution(event, executionStarted) {
|
|
158
158
|
this.#operations.observe(event);
|
|
159
|
-
|
|
159
|
+
const detail = executionStarted === undefined
|
|
160
|
+
? event
|
|
161
|
+
: { ...event, [executionStartedSymbol]: executionStarted };
|
|
162
|
+
this.dispatchEvent(new DetailEvent("toolexecution", detail));
|
|
160
163
|
}
|
|
161
164
|
constructor(options = {}) {
|
|
162
165
|
super();
|
|
@@ -251,7 +254,7 @@ export class ToolRuntimeImplementation extends EventTarget {
|
|
|
251
254
|
assertAgentToolInputSchema(definition.inputSchema, `Tool "${definition.name}" was not registered: inputSchema`);
|
|
252
255
|
return this.#bindTool(definition, execute, options);
|
|
253
256
|
}
|
|
254
|
-
|
|
257
|
+
hasWrappedConfirmationInput(tool) {
|
|
255
258
|
const binding = this.#bindings.get(tool.definition.name);
|
|
256
259
|
return binding?.runtimeTool === tool && binding.definition !== tool.definition;
|
|
257
260
|
}
|
|
@@ -260,12 +263,7 @@ export class ToolRuntimeImplementation extends EventTarget {
|
|
|
260
263
|
throw new AgentToolError("DUPLICATE_BINDING", `Tool "${definition.name}" already has an active binding.`);
|
|
261
264
|
}
|
|
262
265
|
const lifecycleController = new AbortController();
|
|
263
|
-
const exposedDefinition = options.getCallbacks
|
|
264
|
-
? confirmationDefinition(definition)
|
|
265
|
-
: definition;
|
|
266
|
-
const confirmationHandler = options.getCallbacks
|
|
267
|
-
? createConfirmationHandler(definition, lifecycleController.signal)
|
|
268
|
-
: undefined;
|
|
266
|
+
const exposedDefinition = options.getCallbacks ? wrapConfirmationInput(definition) : definition;
|
|
269
267
|
const runtimeTool = Object.freeze({
|
|
270
268
|
definition: exposedDefinition,
|
|
271
269
|
get confirmation() {
|
|
@@ -277,15 +275,14 @@ export class ToolRuntimeImplementation extends EventTarget {
|
|
|
277
275
|
const binding = {
|
|
278
276
|
...(options.resolveTarget
|
|
279
277
|
? {
|
|
280
|
-
resolveTarget: (input) => options.resolveTarget(
|
|
278
|
+
resolveTarget: (input) => options.resolveTarget(options.getCallbacks ? input?.input : input),
|
|
281
279
|
}
|
|
282
280
|
: {}),
|
|
283
281
|
definition,
|
|
284
282
|
runtimeTool,
|
|
285
|
-
execute
|
|
286
|
-
? (input, context) => confirmationHandler.execute(input, context, options.getCallbacks())
|
|
287
|
-
: execute,
|
|
283
|
+
execute,
|
|
288
284
|
...(options.getConfirmation ? { getConfirmation: options.getConfirmation } : {}),
|
|
285
|
+
...(options.getCallbacks ? { getCallbacks: options.getCallbacks } : {}),
|
|
289
286
|
lifecycleController,
|
|
290
287
|
};
|
|
291
288
|
this.#bindings.set(definition.name, binding);
|
|
@@ -487,6 +484,12 @@ export class ToolRuntimeImplementation extends EventTarget {
|
|
|
487
484
|
...(options.signal ? { signal: options.signal } : {}),
|
|
488
485
|
...(label ? { progressLabel: label } : {}),
|
|
489
486
|
...(options[pageDecisionSymbol] ? { [pageDecisionSymbol]: true } : {}),
|
|
487
|
+
...(options[hostConfirmationSymbol]
|
|
488
|
+
? { [hostConfirmationSymbol]: options[hostConfirmationSymbol] }
|
|
489
|
+
: {}),
|
|
490
|
+
...(options[executionStartSymbol]
|
|
491
|
+
? { [executionStartSymbol]: options[executionStartSymbol] }
|
|
492
|
+
: {}),
|
|
490
493
|
};
|
|
491
494
|
const definition = tool.definition;
|
|
492
495
|
const name = definition.name;
|
|
@@ -519,6 +522,7 @@ export class ToolRuntimeImplementation extends EventTarget {
|
|
|
519
522
|
return this.#enqueue((operationId) => this.#observeExecution(tool, input, options.signal, async (onFeedback, assertActive) => {
|
|
520
523
|
const signal = options.signal ?? new AbortController().signal;
|
|
521
524
|
signal.throwIfAborted();
|
|
525
|
+
options[executionStartSymbol]?.();
|
|
522
526
|
return browserHandler.execute(input, {
|
|
523
527
|
signal,
|
|
524
528
|
...(onFeedback ? { onFeedback } : {}),
|
|
@@ -619,8 +623,10 @@ export class ToolRuntimeImplementation extends EventTarget {
|
|
|
619
623
|
}
|
|
620
624
|
};
|
|
621
625
|
this.#emitExecution(running);
|
|
626
|
+
let executionStarted = false;
|
|
622
627
|
try {
|
|
623
628
|
assertActive();
|
|
629
|
+
executionStarted = true;
|
|
624
630
|
const result = await execute(async (feedback) => {
|
|
625
631
|
assertActive();
|
|
626
632
|
if (presentation)
|
|
@@ -645,14 +651,14 @@ export class ToolRuntimeImplementation extends EventTarget {
|
|
|
645
651
|
cancelled: signal?.aborted === true,
|
|
646
652
|
finishedAt: Date.now(),
|
|
647
653
|
error,
|
|
648
|
-
});
|
|
654
|
+
}, executionStarted);
|
|
649
655
|
throw error;
|
|
650
656
|
}
|
|
651
657
|
}
|
|
652
658
|
async #executeApplication(name, input, options, binding, operationId) {
|
|
653
659
|
const id = globalThis.crypto?.randomUUID?.() ?? `${Date.now()}-${Math.random()}`;
|
|
654
660
|
const startedAt = Date.now();
|
|
655
|
-
|
|
661
|
+
let running = {
|
|
656
662
|
...(operationId ? { operationId } : {}),
|
|
657
663
|
id,
|
|
658
664
|
name,
|
|
@@ -662,7 +668,11 @@ export class ToolRuntimeImplementation extends EventTarget {
|
|
|
662
668
|
startedAt,
|
|
663
669
|
input,
|
|
664
670
|
};
|
|
665
|
-
|
|
671
|
+
const askHost = options[hostConfirmationSymbol];
|
|
672
|
+
const waitBeforeShowingExecution = Boolean(binding.getCallbacks && askHost);
|
|
673
|
+
if (!waitBeforeShowingExecution)
|
|
674
|
+
this.#emitExecution(running);
|
|
675
|
+
let executionStarted = false;
|
|
666
676
|
try {
|
|
667
677
|
if (this.#bindings.get(name) !== binding) {
|
|
668
678
|
throw new AgentToolError("UNKNOWN_TOOL", `Tool "${name}" is no longer registered for this call.`);
|
|
@@ -673,10 +683,26 @@ export class ToolRuntimeImplementation extends EventTarget {
|
|
|
673
683
|
}
|
|
674
684
|
const signal = options.signal ?? new AbortController().signal;
|
|
675
685
|
signal.throwIfAborted();
|
|
676
|
-
const
|
|
686
|
+
const context = {
|
|
677
687
|
signal,
|
|
678
688
|
waitForTool: (definition, waitOptions = {}) => this.waitForTool(definition, { signal, ...waitOptions }),
|
|
679
|
-
}
|
|
689
|
+
};
|
|
690
|
+
const callbacks = binding.getCallbacks?.();
|
|
691
|
+
const executor = callbacks?.execute ?? binding.execute;
|
|
692
|
+
const execute = (input, context) => {
|
|
693
|
+
if (waitBeforeShowingExecution) {
|
|
694
|
+
running = { ...running, startedAt: Date.now() };
|
|
695
|
+
this.#emitExecution(running);
|
|
696
|
+
context.signal.throwIfAborted();
|
|
697
|
+
binding.lifecycleController.signal.throwIfAborted();
|
|
698
|
+
}
|
|
699
|
+
options[executionStartSymbol]?.();
|
|
700
|
+
executionStarted = true;
|
|
701
|
+
return executor(input, context);
|
|
702
|
+
};
|
|
703
|
+
const result = callbacks
|
|
704
|
+
? await executeWithConfirmation(input.input, context, binding.lifecycleController.signal, { confirm: callbacks.confirm, execute }, askHost)
|
|
705
|
+
: await execute(input, context);
|
|
680
706
|
signal.throwIfAborted();
|
|
681
707
|
this.#emitExecution({
|
|
682
708
|
...running,
|
|
@@ -693,7 +719,7 @@ export class ToolRuntimeImplementation extends EventTarget {
|
|
|
693
719
|
cancelled: options.signal?.aborted === true,
|
|
694
720
|
finishedAt: Date.now(),
|
|
695
721
|
error,
|
|
696
|
-
});
|
|
722
|
+
}, executionStarted);
|
|
697
723
|
throw error;
|
|
698
724
|
}
|
|
699
725
|
}
|
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 "../runtime.js";
|
|
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/webmcp.js
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { confirmationInstructions, isConfirmationRequest } from "./confirmation-protocol.js";
|
|
2
|
-
import { AgentToolError } from "./shared/errors.js";
|
|
3
1
|
function getWebMcpModelContext() {
|
|
4
2
|
if (typeof document === "undefined")
|
|
5
3
|
return undefined;
|
|
@@ -80,13 +78,6 @@ export function connectToolRuntimeToWebMcp(runtime) {
|
|
|
80
78
|
queueMicrotask(() => disposeUnusedConnection(runtime, current));
|
|
81
79
|
};
|
|
82
80
|
}
|
|
83
|
-
function evaluationConfirmationRequired() {
|
|
84
|
-
return {
|
|
85
|
-
isError: true,
|
|
86
|
-
_meta: { "x-nexface-eval": "host-confirmation-required" },
|
|
87
|
-
content: [{ type: "text", text: "Nexface evaluation requires user confirmation." }],
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
81
|
function createConnection(runtime, modelContext) {
|
|
91
82
|
const registrations = new Map();
|
|
92
83
|
let disposed = false;
|
|
@@ -95,12 +86,7 @@ function createConnection(runtime, modelContext) {
|
|
|
95
86
|
const begin = {
|
|
96
87
|
name: "nxf_beginOperation",
|
|
97
88
|
description: "Begin a continuous page operation before using page tools. Call nxf_endOperation with the returned operationId when all page work is done, before your final response. Repeated begin returns the same active operation. Omitting begin shows feedback only while each tool executes.",
|
|
98
|
-
inputSchema: {
|
|
99
|
-
"x-nexface": { version: 2, control: true },
|
|
100
|
-
type: "object",
|
|
101
|
-
properties: {},
|
|
102
|
-
additionalProperties: false,
|
|
103
|
-
},
|
|
89
|
+
inputSchema: { type: "object", properties: {}, additionalProperties: false },
|
|
104
90
|
execute: async (input, options = {}) => {
|
|
105
91
|
options.signal?.throwIfAborted();
|
|
106
92
|
controls.signal.throwIfAborted();
|
|
@@ -130,7 +116,6 @@ function createConnection(runtime, modelContext) {
|
|
|
130
116
|
description: "End the page operation identified by operationId after all page work. Waits for its admitted tools to settle. Does not stop an external agent or undo changes.",
|
|
131
117
|
inputSchema: {
|
|
132
118
|
type: "object",
|
|
133
|
-
"x-nexface": { version: 2, control: true },
|
|
134
119
|
properties: { operationId: { type: "string" } },
|
|
135
120
|
required: ["operationId"],
|
|
136
121
|
additionalProperties: false,
|
|
@@ -172,106 +157,49 @@ function createConnection(runtime, modelContext) {
|
|
|
172
157
|
};
|
|
173
158
|
const register = (tool) => {
|
|
174
159
|
const { definition } = tool;
|
|
175
|
-
const
|
|
160
|
+
const wrappedInput = runtime.hasWrappedConfirmationInput(tool);
|
|
176
161
|
const controller = new AbortController();
|
|
177
162
|
const registration = { controller, tool };
|
|
178
163
|
registrations.set(definition.name, registration);
|
|
179
164
|
let result;
|
|
180
165
|
try {
|
|
181
|
-
const business = confirmed
|
|
182
|
-
? definition.inputSchema.properties.input
|
|
183
|
-
: definition.inputSchema;
|
|
184
|
-
for (const key of ["nxf_progressLabel", "nxf_confirmation"]) {
|
|
185
|
-
if (Object.hasOwn(business.properties ?? {}, key))
|
|
186
|
-
throw new TypeError(`WebMCP reserves the top-level property "${key}".`);
|
|
187
|
-
}
|
|
188
|
-
const { enum: allowedInputs, ...businessSchema } = business;
|
|
189
|
-
const businessValues = allowedInputs?.filter((value) => !!value && typeof value === "object" && !Array.isArray(value));
|
|
190
|
-
const enumConstraint = businessValues
|
|
191
|
-
? {
|
|
192
|
-
anyOf: businessValues.length
|
|
193
|
-
? businessValues.map((value) => ({
|
|
194
|
-
properties: {
|
|
195
|
-
...Object.fromEntries(Object.entries(value).map(([key, item]) => [key, { const: item }])),
|
|
196
|
-
nxf_progressLabel: { type: "string" },
|
|
197
|
-
},
|
|
198
|
-
required: Object.keys(value),
|
|
199
|
-
additionalProperties: false,
|
|
200
|
-
}))
|
|
201
|
-
: [false],
|
|
202
|
-
}
|
|
203
|
-
: {};
|
|
204
|
-
const properties = {
|
|
205
|
-
...business.properties,
|
|
206
|
-
nxf_progressLabel: {
|
|
207
|
-
type: "string",
|
|
208
|
-
description: "Briefly describe the action in the user's language.",
|
|
209
|
-
},
|
|
210
|
-
};
|
|
211
|
-
const metadata = { version: 2, effect: definition.effect ?? "action" };
|
|
212
166
|
result = modelContext.registerTool({
|
|
213
|
-
annotations: { readOnlyHint: definition.effect === "read" },
|
|
214
167
|
name: definition.name,
|
|
215
168
|
description: definition.description,
|
|
216
169
|
inputSchema: {
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
oneOf: [
|
|
232
|
-
{
|
|
233
|
-
...enumConstraint,
|
|
234
|
-
required: business.required ?? [],
|
|
235
|
-
not: { required: ["nxf_confirmation"] },
|
|
236
|
-
},
|
|
237
|
-
{ required: ["nxf_confirmation"], maxProperties: 1 },
|
|
238
|
-
],
|
|
239
|
-
}
|
|
240
|
-
: { ...enumConstraint, not: { required: ["nxf_confirmation"] } }),
|
|
170
|
+
type: "object",
|
|
171
|
+
properties: {
|
|
172
|
+
...(wrappedInput
|
|
173
|
+
? definition.inputSchema.properties
|
|
174
|
+
: {
|
|
175
|
+
input: definition.inputSchema,
|
|
176
|
+
}),
|
|
177
|
+
progressLabel: {
|
|
178
|
+
type: "string",
|
|
179
|
+
description: "Briefly describe this tool action in the user’s language. Do not expose internal identifiers or claim success before completion.",
|
|
180
|
+
},
|
|
181
|
+
},
|
|
182
|
+
required: ["input"],
|
|
183
|
+
additionalProperties: false,
|
|
241
184
|
},
|
|
242
185
|
execute: async (parameters, options = {}) => {
|
|
243
|
-
// Native WebMCP invokes the current registration, which can change after discovery.
|
|
244
|
-
const evaluation = globalThis[Symbol.for("@nexface/evals/phase")];
|
|
245
|
-
if (evaluation === "forbidden")
|
|
246
|
-
throw new Error("Unexpected tool origin.");
|
|
247
|
-
if (evaluation === "navigate" && definition.effect !== "navigate")
|
|
248
|
-
throw new Error("Expected a navigation tool.");
|
|
249
|
-
if (evaluation &&
|
|
250
|
-
(tool.confirmation ||
|
|
251
|
-
(confirmed && Object.hasOwn(parameters ?? {}, "nxf_confirmation"))))
|
|
252
|
-
return evaluationConfirmationRequired();
|
|
253
|
-
if (evaluation === "read" && definition.effect !== "read")
|
|
254
|
-
throw new Error("Nexface evaluation requires a read-only tool.");
|
|
255
|
-
if (!parameters || typeof parameters !== "object" || Array.isArray(parameters))
|
|
256
|
-
throw new TypeError("WebMCP arguments must be an object.");
|
|
257
186
|
const args = parameters;
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
187
|
+
if (!args ||
|
|
188
|
+
typeof args !== "object" ||
|
|
189
|
+
Array.isArray(args) ||
|
|
190
|
+
!args.input ||
|
|
191
|
+
typeof args.input !== "object" ||
|
|
192
|
+
Array.isArray(args.input) ||
|
|
193
|
+
Object.keys(args).some((key) => key !== "input" && key !== "progressLabel") ||
|
|
194
|
+
(args.progressLabel !== undefined && typeof args.progressLabel !== "string")) {
|
|
195
|
+
throw new TypeError("WebMCP tools require { input: object, progressLabel?: string }.");
|
|
196
|
+
}
|
|
197
|
+
return runtime.execute(tool, wrappedInput ? { input: args.input } : args.input, {
|
|
266
198
|
...(options.signal ? { signal: options.signal } : {}),
|
|
267
|
-
...(typeof
|
|
199
|
+
...(typeof args.progressLabel === "string"
|
|
200
|
+
? { progressLabel: args.progressLabel }
|
|
201
|
+
: {}),
|
|
268
202
|
});
|
|
269
|
-
if (confirmed && isConfirmationRequest(result)) {
|
|
270
|
-
if (evaluation)
|
|
271
|
-
return evaluationConfirmationRequired();
|
|
272
|
-
return { ...result, nextAction: confirmationInstructions("nxf_confirmation") };
|
|
273
|
-
}
|
|
274
|
-
return result;
|
|
275
203
|
},
|
|
276
204
|
}, { signal: controller.signal });
|
|
277
205
|
}
|
package/package.json
CHANGED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import type { AgentToolDefinition } from "./types.js";
|
|
2
|
-
export declare function isConfirmationRequest(value: unknown): value is object;
|
|
3
|
-
export declare function confirmationInstructions(field: "confirmation" | "nxf_confirmation"): string;
|
|
4
|
-
export declare function confirmationDefinition(definition: AgentToolDefinition<any, any>): AgentToolDefinition<{
|
|
5
|
-
input?: Record<string, unknown>;
|
|
6
|
-
confirmation?: {
|
|
7
|
-
requestId: string;
|
|
8
|
-
approved: boolean;
|
|
9
|
-
};
|
|
10
|
-
}, unknown>;
|
|
11
|
-
export declare function createConfirmationHandler(definition: AgentToolDefinition<any, any>, lifecycle: AbortSignal): {
|
|
12
|
-
execute(wire: Record<string, unknown>, context: Parameters<(input: unknown, context: Parameters<import("./types.js").AgentToolExecutor<any, any>>[1], callbacks: {
|
|
13
|
-
execute: import("./types.js").AgentToolExecutor<any, any>;
|
|
14
|
-
confirm?: import("./confirmation.js").ConfirmationCallback<any>;
|
|
15
|
-
}) => Promise<{
|
|
16
|
-
kind: "completed";
|
|
17
|
-
result: any;
|
|
18
|
-
requestId?: never;
|
|
19
|
-
message?: never;
|
|
20
|
-
} | {
|
|
21
|
-
kind: "pending";
|
|
22
|
-
requestId: `${string}-${string}-${string}-${string}-${string}`;
|
|
23
|
-
message: string;
|
|
24
|
-
result?: never;
|
|
25
|
-
}>>[1], callbacks: Parameters<(input: unknown, context: Parameters<import("./types.js").AgentToolExecutor<any, any>>[1], callbacks: {
|
|
26
|
-
execute: import("./types.js").AgentToolExecutor<any, any>;
|
|
27
|
-
confirm?: import("./confirmation.js").ConfirmationCallback<any>;
|
|
28
|
-
}) => Promise<{
|
|
29
|
-
kind: "completed";
|
|
30
|
-
result: any;
|
|
31
|
-
requestId?: never;
|
|
32
|
-
message?: never;
|
|
33
|
-
} | {
|
|
34
|
-
kind: "pending";
|
|
35
|
-
requestId: `${string}-${string}-${string}-${string}-${string}`;
|
|
36
|
-
message: string;
|
|
37
|
-
result?: never;
|
|
38
|
-
}>>[2]): Promise<any>;
|
|
39
|
-
resolveInput(wire: unknown): unknown;
|
|
40
|
-
};
|
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
import { defineAgentTool } from "./definition.js";
|
|
2
|
-
import { AgentToolError } from "./shared/errors.js";
|
|
3
|
-
import { validateAgentToolInput } from "./shared/schema.js";
|
|
4
|
-
import { createConfirmationRequests } from "./confirmation.js";
|
|
5
|
-
const confirmationRequests = new WeakSet();
|
|
6
|
-
export function isConfirmationRequest(value) {
|
|
7
|
-
return typeof value === "object" && value !== null && confirmationRequests.has(value);
|
|
8
|
-
}
|
|
9
|
-
export function confirmationInstructions(field) {
|
|
10
|
-
return `Ask this message using the host's permitted user-input tool with Confirm/Reject choices, or chat if unavailable. Wait for an explicit answer to this question, then call the same tool with only { ${field}: { requestId, approved } }: true to approve, false to reject. Stop after rejection. If execution has an unknown outcome, verify its status before retrying.`;
|
|
11
|
-
}
|
|
12
|
-
export function confirmationDefinition(definition) {
|
|
13
|
-
const exposed = defineAgentTool({
|
|
14
|
-
...definition,
|
|
15
|
-
inputSchema: {
|
|
16
|
-
type: "object",
|
|
17
|
-
properties: {
|
|
18
|
-
input: {
|
|
19
|
-
...definition.inputSchema,
|
|
20
|
-
description: [
|
|
21
|
-
"Business arguments for the initial call. Omit confirmation.",
|
|
22
|
-
definition.inputSchema.description,
|
|
23
|
-
]
|
|
24
|
-
.filter(Boolean)
|
|
25
|
-
.join(" "),
|
|
26
|
-
},
|
|
27
|
-
confirmation: {
|
|
28
|
-
type: "object",
|
|
29
|
-
description: "Use after a confirmation request and the user's explicit answer. Omit input.",
|
|
30
|
-
properties: {
|
|
31
|
-
requestId: { type: "string", minLength: 1 },
|
|
32
|
-
approved: { type: "boolean" },
|
|
33
|
-
},
|
|
34
|
-
required: ["requestId", "approved"],
|
|
35
|
-
additionalProperties: false,
|
|
36
|
-
},
|
|
37
|
-
},
|
|
38
|
-
additionalProperties: false,
|
|
39
|
-
},
|
|
40
|
-
});
|
|
41
|
-
return exposed;
|
|
42
|
-
}
|
|
43
|
-
// The chat protocol owns wire arguments, schema and model-facing instructions.
|
|
44
|
-
export function createConfirmationHandler(definition, lifecycle) {
|
|
45
|
-
const requests = createConfirmationRequests(lifecycle);
|
|
46
|
-
const resolvedInputs = new WeakMap();
|
|
47
|
-
return {
|
|
48
|
-
async execute(wire, context, callbacks) {
|
|
49
|
-
const hasInput = Object.hasOwn(wire, "input");
|
|
50
|
-
const hasAnswer = Object.hasOwn(wire, "confirmation");
|
|
51
|
-
if (hasInput === hasAnswer) {
|
|
52
|
-
throw new AgentToolError("INVALID_INPUT", "Provide exactly one of input or confirmation.");
|
|
53
|
-
}
|
|
54
|
-
if (hasAnswer) {
|
|
55
|
-
const { requestId, approved } = wire.confirmation;
|
|
56
|
-
resolvedInputs.set(wire, requests.resolveInput(requestId));
|
|
57
|
-
return requests.decide(requestId, approved, context);
|
|
58
|
-
}
|
|
59
|
-
const errors = validateAgentToolInput(definition.inputSchema, wire.input);
|
|
60
|
-
if (errors.length)
|
|
61
|
-
throw new AgentToolError("INVALID_INPUT", errors.join(" "), { errors });
|
|
62
|
-
const outcome = await requests.start(wire.input, context, callbacks);
|
|
63
|
-
if (outcome.kind === "completed")
|
|
64
|
-
return outcome.result;
|
|
65
|
-
const request = {
|
|
66
|
-
status: "confirmation_required",
|
|
67
|
-
requestId: outcome.requestId,
|
|
68
|
-
message: outcome.message,
|
|
69
|
-
nextAction: confirmationInstructions("confirmation"),
|
|
70
|
-
};
|
|
71
|
-
confirmationRequests.add(request);
|
|
72
|
-
return request;
|
|
73
|
-
},
|
|
74
|
-
resolveInput(wire) {
|
|
75
|
-
if (!wire || typeof wire !== "object")
|
|
76
|
-
return undefined;
|
|
77
|
-
const args = wire;
|
|
78
|
-
return args.confirmation?.requestId
|
|
79
|
-
? (resolvedInputs.get(wire) ?? requests.resolveInput(args.confirmation.requestId))
|
|
80
|
-
: args.input;
|
|
81
|
-
},
|
|
82
|
-
};
|
|
83
|
-
}
|