@nexface/tools 0.1.21-alpha.5 → 0.1.21-alpha.6

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.
@@ -1,4 +1,6 @@
1
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;
2
4
  export declare function confirmationDefinition(definition: AgentToolDefinition<any, any>): AgentToolDefinition<{
3
5
  input?: Record<string, unknown>;
4
6
  confirmation?: {
@@ -2,7 +2,13 @@ import { defineAgentTool } from "./definition.js";
2
2
  import { AgentToolError } from "./shared/errors.js";
3
3
  import { validateAgentToolInput } from "./shared/schema.js";
4
4
  import { createConfirmationRequests } from "./confirmation.js";
5
- const instructions = "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 { confirmation: { requestId, approved } }: true to approve, false to reject. Stop after rejection. If execution has an unknown outcome, verify its status before retrying.";
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
+ }
6
12
  export function confirmationDefinition(definition) {
7
13
  const exposed = defineAgentTool({
8
14
  ...definition,
@@ -56,12 +62,14 @@ export function createConfirmationHandler(definition, lifecycle) {
56
62
  const outcome = await requests.start(wire.input, context, callbacks);
57
63
  if (outcome.kind === "completed")
58
64
  return outcome.result;
59
- return {
65
+ const request = {
60
66
  status: "confirmation_required",
61
67
  requestId: outcome.requestId,
62
68
  message: outcome.message,
63
- nextAction: instructions,
69
+ nextAction: confirmationInstructions("confirmation"),
64
70
  };
71
+ confirmationRequests.add(request);
72
+ return request;
65
73
  },
66
74
  resolveInput(wire) {
67
75
  if (!wire || typeof wire !== "object")
@@ -5,7 +5,6 @@ interface AgentToolMetadataInput {
5
5
  effect?: "read" | "action" | "navigate";
6
6
  }
7
7
  export declare function isValidAgentContextName(name: string): boolean;
8
- export declare function assertAgentToolName(name: string): void;
9
8
  export declare function defineAgentTool<TResult = unknown>(metadata: AgentToolMetadataInput): AgentToolDefinition<Record<string, never>, TResult>;
10
9
  export declare function defineAgentTool<TInput = never, TResult = unknown, const TSchema extends AgentToolInputSchema = AgentToolInputSchema>(metadata: AgentToolMetadataInput & {
11
10
  inputSchema: TSchema;
@@ -15,7 +15,6 @@ const reservedContextNames = new Set([
15
15
  ]);
16
16
  const reservedToolNames = new Set([
17
17
  ...reservedContextNames,
18
- "nxf_showToolResults",
19
18
  "nxf_loadSkill",
20
19
  "nxf_beginOperation",
21
20
  "nxf_endOperation",
@@ -23,13 +22,13 @@ const reservedToolNames = new Set([
23
22
  export function isValidAgentContextName(name) {
24
23
  return agentNamePattern.test(name) && !reservedContextNames.has(name);
25
24
  }
26
- export function assertAgentToolName(name) {
27
- if (typeof name !== "string" || !agentNamePattern.test(name) || reservedToolNames.has(name)) {
25
+ function assertName(name) {
26
+ if (!agentNamePattern.test(name) || reservedToolNames.has(name)) {
28
27
  throw new Error(`Invalid or reserved agent tool name: ${name}.`);
29
28
  }
30
29
  }
31
30
  export function defineAgentTool(metadata) {
32
- assertAgentToolName(metadata.name);
31
+ assertName(metadata.name);
33
32
  return createAgentToolDefinition({
34
33
  ...metadata,
35
34
  inputSchema: metadata.inputSchema === undefined ? emptyInputSchema : metadata.inputSchema,
package/dist/index.d.ts CHANGED
@@ -2,7 +2,7 @@ export { defineAgentTool } from "./definition.js";
2
2
  export { defineAgentNavigation } from "./navigation.js";
3
3
  export { AgentOperationSurface, type AgentOperationSurfaceProps } from "./operation-surface.js";
4
4
  export { createToolRuntime, isToolRuntimeError, } from "./runtime.js";
5
- export { AgentContext, ToolsProvider, AgentUIAction, useAgentContext, useAgentNavigation, useAgentTool, useRenderTool, } from "./react.js";
5
+ export { AgentContext, ToolsProvider, AgentUIAction, useAgentContext, useAgentNavigation, useAgentTool, } from "./react.js";
6
6
  export type { AgentNavigation, AgentNavigationConfig, AgentNavigationExecutors, AgentNavigationRouteConfig, } from "./navigation.js";
7
7
  export type { AgentToolBinding, AgentToolChatConfirmationConfig, AgentToolPageConfirmationConfig, AgentUIActionChildProps, AgentUIActionEvent, AgentUIActionProps, UseAgentToolDefaultOptions, UseAgentToolPageConfirmationOptions, } from "./react.js";
8
8
  export type { AgentToolConfirmationConfig, RuntimeAgentTool, ToolRuntime, ToolOperation, ToolRuntimeOptions, ToolRuntimeToolChangeEvent, } from "./runtime.js";
package/dist/index.js CHANGED
@@ -2,5 +2,5 @@ export { defineAgentTool } from "./definition.js";
2
2
  export { defineAgentNavigation } from "./navigation.js";
3
3
  export { AgentOperationSurface } from "./operation-surface.js";
4
4
  export { createToolRuntime, isToolRuntimeError, } from "./runtime.js";
5
- export { AgentContext, ToolsProvider, AgentUIAction, useAgentContext, useAgentNavigation, useAgentTool, useRenderTool, } from "./react.js";
5
+ export { AgentContext, ToolsProvider, AgentUIAction, useAgentContext, useAgentNavigation, useAgentTool, } from "./react.js";
6
6
  export { registerAgentTool } from "./registration.js";
package/dist/react.d.ts CHANGED
@@ -22,13 +22,7 @@ export interface AgentToolPageConfirmationConfig {
22
22
  title?: string;
23
23
  description?: string;
24
24
  }
25
- type ToolResultData<T> = unknown extends T ? T : T extends string | number | boolean | null ? T : T extends readonly unknown[] ? {
26
- [K in keyof T]: ToolResultData<T[K]>;
27
- } : T extends (...args: never[]) => unknown ? never : T extends object ? {
28
- [K in keyof T]: ToolResultData<T[K]>;
29
- } : never;
30
- type ToolRenderResult<T> = unknown extends T ? unknown : [T] extends [void] ? null : [T] extends [ToolResultData<T>] ? T : unknown;
31
- export interface UseAgentToolDefaultOptions<TInput = unknown, TResult = unknown> {
25
+ export interface UseAgentToolDefaultOptions<TInput = unknown> {
32
26
  confirm?: (context: {
33
27
  input: TInput;
34
28
  signal: AbortSignal;
@@ -37,17 +31,6 @@ export interface UseAgentToolDefaultOptions<TInput = unknown, TResult = unknown>
37
31
  } | Promise<boolean | {
38
32
  message: string;
39
33
  }>;
40
- /**
41
- * @experimental
42
- * @warning Experimental API. Its signature and behavior may change.
43
- * @remarks Renders a successful result when referenced in the final chat reply. JSON-compatible results retain their types; void becomes null and other results are unknown.
44
- * @beta
45
- */
46
- _render?: (context: {
47
- input: TInput;
48
- status: "running" | "success" | "error" | "cancelled";
49
- result?: ToolRenderResult<TResult>;
50
- }) => ReactNode;
51
34
  /**
52
35
  * @experimental Enables human-in-the-loop Chat confirmation.
53
36
  * @beta
@@ -58,7 +41,7 @@ export interface UseAgentToolDefaultOptions<TInput = unknown, TResult = unknown>
58
41
  * @experimental Enables human-in-the-loop Page confirmation.
59
42
  * @beta
60
43
  */
61
- export interface UseAgentToolPageConfirmationOptions<TInput = unknown, TResult = unknown> extends Omit<UseAgentToolDefaultOptions<TInput, TResult>, "requiresConfirmation"> {
44
+ export interface UseAgentToolPageConfirmationOptions {
62
45
  confirm?: never;
63
46
  requiresConfirmation: AgentToolPageConfirmationConfig;
64
47
  }
@@ -79,14 +62,8 @@ export declare function getAgentToolBindingInternals(binding: AgentToolBinding):
79
62
  * @experimental The Page confirmation overload is experimental.
80
63
  * @beta
81
64
  */
82
- export declare function useAgentTool<TInput, TResult, TElement extends HTMLElement = HTMLElement, TOutput extends TResult = TResult>(definition: AgentToolDefinition<TInput, TResult>, execute: AgentToolExecutor<TInput, TOutput>, options: UseAgentToolPageConfirmationOptions<NoInfer<TInput>, NoInfer<TOutput>>): AgentToolBinding<TInput, TElement>;
83
- export declare function useAgentTool<TInput, TResult, TElement extends HTMLElement = HTMLElement, TOutput extends TResult = TResult>(definition: AgentToolDefinition<TInput, TResult>, execute: AgentToolExecutor<TInput, TOutput>, options?: UseAgentToolDefaultOptions<NoInfer<TInput>, NoInfer<TOutput>>): AgentToolBinding<TInput, TElement>;
84
- /**
85
- * @experimental
86
- * @beta
87
- * Registers a component for a successful server tool result referenced in Chat.
88
- */
89
- export declare function useRenderTool(name: string, render: NonNullable<UseAgentToolDefaultOptions<Record<string, unknown>>["_render"]>): void;
65
+ export declare function useAgentTool<TInput, TResult, TElement extends HTMLElement = HTMLElement>(definition: AgentToolDefinition<TInput, TResult>, execute: AgentToolExecutor<TInput, TResult>, options: UseAgentToolPageConfirmationOptions): AgentToolBinding<TInput, TElement>;
66
+ export declare function useAgentTool<TInput, TResult, TElement extends HTMLElement = HTMLElement>(definition: AgentToolDefinition<TInput, TResult>, execute: AgentToolExecutor<TInput, TResult>, options?: UseAgentToolDefaultOptions<TInput>): AgentToolBinding<TInput, TElement>;
90
67
  export declare function useAgentNavigation<TNavigation extends AgentNavigation, const TExecutors extends AgentNavigationExecutors<TNavigation>>(navigation: TNavigation, executors: TExecutors & Record<Exclude<keyof TExecutors, keyof AgentNavigationExecutors<TNavigation>>, never>): void;
91
68
  export declare function ToolsProvider({ children, toolRuntime, webMcp, }: {
92
69
  children: ReactNode;
package/dist/react.js CHANGED
@@ -55,21 +55,6 @@ function createAgentToolBinding(toolName) {
55
55
  function useAgentBinding(definition, execute, options) {
56
56
  const runtime = useInternalToolRuntime();
57
57
  const confirmation = options.requiresConfirmation;
58
- const confirmationProtocol = Boolean(options.confirm);
59
- const presentation = useMemo(() => {
60
- if (!options._render)
61
- return undefined;
62
- return {
63
- definition,
64
- ...(confirmationProtocol ? { confirmationProtocol: true } : {}),
65
- render: options._render,
66
- };
67
- }, [definition, options._render, confirmationProtocol]);
68
- const presentationRef = useRef(presentation);
69
- useLayoutEffect(() => {
70
- presentationRef.current = presentation;
71
- runtime.notifyToolPresentationChange();
72
- }, [runtime, presentation]);
73
58
  if (confirmation !== undefined &&
74
59
  (!confirmation ||
75
60
  typeof confirmation !== "object" ||
@@ -97,7 +82,6 @@ function useAgentBinding(definition, execute, options) {
97
82
  runtime.bindTool(definition, (input, context) => executeRef.current(input, context), {
98
83
  signal: controller.signal,
99
84
  getConfirmation: () => confirmationRef.current,
100
- getPresentation: () => presentationRef.current,
101
85
  ...(mode === "callback"
102
86
  ? {
103
87
  getCallbacks: () => ({
@@ -115,15 +99,6 @@ function useAgentBinding(definition, execute, options) {
115
99
  export function useAgentTool(definition, execute, options = {}) {
116
100
  return useAgentBinding(definition, execute, options);
117
101
  }
118
- /**
119
- * @experimental
120
- * @beta
121
- * Registers a component for a successful server tool result referenced in Chat.
122
- */
123
- export function useRenderTool(name, render) {
124
- const runtime = useInternalToolRuntime();
125
- useLayoutEffect(() => runtime.bindToolRenderer(name, { render: render }), [runtime, name, render]);
126
- }
127
102
  function getAgentNavigationHandler(handlers, key) {
128
103
  if (!handlers || typeof handlers !== "object" || !Object.hasOwn(handlers, key)) {
129
104
  throw new Error(`Missing navigation executor: ${key}.`);
package/dist/runtime.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import type { ConfirmationCallback } from "./confirmation.js";
2
- import { type ToolPresentation } from "./shared/tool-presentation.js";
3
2
  import { type InternalExecutionOptions } from "./shared/runtime-execution.js";
4
3
  import type { PageActionFeedback } from "./browser/feedback.js";
5
4
  import { type TaskProgress } from "./shared/runtime-controller.js";
@@ -126,8 +125,6 @@ export declare class AgentContextError extends Error {
126
125
  constructor(code: AgentContextErrorCode, message: string);
127
126
  }
128
127
  export interface InternalToolRuntime extends ToolRuntime, EventTarget {
129
- notifyToolPresentationChange(): void;
130
- bindToolRenderer(name: string, presentation: ToolPresentation): () => void;
131
128
  usesConfirmationProtocol(tool: RuntimeAgentTool): boolean;
132
129
  beginInternalOperation(options?: {
133
130
  signal?: AbortSignal;
@@ -147,7 +144,6 @@ export interface InternalToolRuntime extends ToolRuntime, EventTarget {
147
144
  bindTool<TInput, TResult>(definition: AgentToolDefinition<TInput, TResult>, execute: AgentToolExecutor<TInput, TResult>, options?: {
148
145
  signal?: AbortSignal;
149
146
  getConfirmation?: () => AgentToolConfirmationConfig | undefined;
150
- getPresentation?: () => ToolPresentation | undefined;
151
147
  getCallbacks?: () => {
152
148
  execute: AgentToolExecutor<any, any>;
153
149
  confirm: ConfirmationCallback<any>;
@@ -183,8 +179,6 @@ export declare class ToolRuntimeImplementation extends EventTarget implements To
183
179
  requestOperationCancel(id: string): void;
184
180
  setPageActionFeedback(listener: (event: AgentToolExecutionEvent, feedback: PageActionFeedback, signal?: AbortSignal) => void | Promise<void>): () => void;
185
181
  constructor(options?: ToolRuntimeOptions);
186
- notifyToolPresentationChange(): void;
187
- bindToolRenderer(name: string, presentation: ToolPresentation): () => void;
188
182
  on(eventName: "toolchange", listener: (event: ToolRuntimeToolChangeEvent) => void): () => void;
189
183
  on(eventName: "toolexecution", listener: (event: AgentToolExecutionEvent) => void): () => void;
190
184
  register<TInput, TResult>(definition: AgentToolDefinition<TInput, TResult>, options: {
@@ -195,7 +189,6 @@ export declare class ToolRuntimeImplementation extends EventTarget implements To
195
189
  bindTool<TInput, TResult>(definition: AgentToolDefinition<TInput, TResult>, execute: AgentToolExecutor<TInput, TResult>, options?: {
196
190
  signal?: AbortSignal;
197
191
  getConfirmation?: () => AgentToolConfirmationConfig | undefined;
198
- getPresentation?: () => ToolPresentation | undefined;
199
192
  getCallbacks?: () => {
200
193
  execute: AgentToolExecutor<any, any>;
201
194
  confirm: ConfirmationCallback<any>;
package/dist/runtime.js CHANGED
@@ -1,6 +1,5 @@
1
1
  import { confirmationDefinition, createConfirmationHandler } from "./confirmation-protocol.js";
2
2
  import { searchNavigationTools, searchToolDefinition } from "./tool-search.js";
3
- import { toolPresentationSymbol, } from "./shared/tool-presentation.js";
4
3
  import { pageDecisionSymbol } from "./shared/runtime-execution.js";
5
4
  import { PageCommandError } from "./browser/locator.js";
6
5
  import { measurePagePhase } from "./browser/diagnostics.js";
@@ -9,7 +8,7 @@ import { AgentToolError, TOOL_ERROR_CODES } from "./shared/errors.js";
9
8
  export { AgentToolError } from "./shared/errors.js";
10
9
  import { assertAgentToolRegistration } from "./registration.js";
11
10
  import { OperationTracker } from "./operation-runtime.js";
12
- import { assertAgentToolName, defineAgentTool, isValidAgentContextName } from "./definition.js";
11
+ import { defineAgentTool, isValidAgentContextName } from "./definition.js";
13
12
  import { assertAgentToolInputSchema, validateAgentToolInput } from "./shared/schema.js";
14
13
  import { createBrowserToolset } from "./browser/toolset.js";
15
14
  const CONTEXT_ERROR_CODES = [
@@ -123,7 +122,6 @@ export function getInternalToolRuntime(runtime) {
123
122
  export class ToolRuntimeImplementation extends EventTarget {
124
123
  [toolRuntimeBrand] = true;
125
124
  #bindings = new Map();
126
- #renderers = new Map();
127
125
  #routeSearch;
128
126
  #visibleNavigation = new Set();
129
127
  #contexts = new Map();
@@ -206,39 +204,6 @@ export class ToolRuntimeImplementation extends EventTarget {
206
204
  getRegisteredTools: () => this.getRegisteredTools(),
207
205
  });
208
206
  Object.defineProperty(this, runtimeControllerSymbol, { value: controller });
209
- const getPresentation = (name) => {
210
- const binding = this.#bindings.get(name);
211
- return binding ? binding.getPresentation?.() : this.#renderers.get(name);
212
- };
213
- const presentations = {
214
- get: getPresentation,
215
- names: () => [...new Set([...this.#bindings.keys(), ...this.#renderers.keys()])].filter((name) => getPresentation(name)),
216
- subscribe: (listener) => {
217
- this.addEventListener("toolchange", listener);
218
- this.addEventListener("toolpresentationchange", listener);
219
- return () => {
220
- this.removeEventListener("toolchange", listener);
221
- this.removeEventListener("toolpresentationchange", listener);
222
- };
223
- },
224
- };
225
- Object.defineProperty(this, toolPresentationSymbol, { value: Object.freeze(presentations) });
226
- }
227
- notifyToolPresentationChange() {
228
- this.dispatchEvent(new Event("toolpresentationchange"));
229
- }
230
- bindToolRenderer(name, presentation) {
231
- assertAgentToolName(name);
232
- if (this.#renderers.has(name))
233
- throw new AgentToolError("DUPLICATE_BINDING", `Renderer for tool "${name}" is already registered.`);
234
- this.#renderers.set(name, presentation);
235
- this.notifyToolPresentationChange();
236
- return () => {
237
- if (this.#renderers.get(name) !== presentation)
238
- return;
239
- this.#renderers.delete(name);
240
- this.notifyToolPresentationChange();
241
- };
242
207
  }
243
208
  on(eventName, listener) {
244
209
  const handleEvent = (event) => {
@@ -310,7 +275,6 @@ export class ToolRuntimeImplementation extends EventTarget {
310
275
  lifecycleSignal: lifecycleController.signal,
311
276
  });
312
277
  const binding = {
313
- ...(options.getPresentation ? { getPresentation: options.getPresentation } : {}),
314
278
  ...(options.resolveTarget
315
279
  ? {
316
280
  resolveTarget: (input) => options.resolveTarget(confirmationHandler ? confirmationHandler.resolveInput(input) : input),
package/dist/webmcp.js CHANGED
@@ -1,3 +1,5 @@
1
+ import { confirmationInstructions, isConfirmationRequest } from "./confirmation-protocol.js";
2
+ import { AgentToolError } from "./shared/errors.js";
1
3
  function getWebMcpModelContext() {
2
4
  if (typeof document === "undefined")
3
5
  return undefined;
@@ -78,6 +80,13 @@ export function connectToolRuntimeToWebMcp(runtime) {
78
80
  queueMicrotask(() => disposeUnusedConnection(runtime, current));
79
81
  };
80
82
  }
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
+ }
81
90
  function createConnection(runtime, modelContext) {
82
91
  const registrations = new Map();
83
92
  let disposed = false;
@@ -86,7 +95,12 @@ function createConnection(runtime, modelContext) {
86
95
  const begin = {
87
96
  name: "nxf_beginOperation",
88
97
  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.",
89
- inputSchema: { type: "object", properties: {}, additionalProperties: false },
98
+ inputSchema: {
99
+ "x-nexface": { version: 2, control: true },
100
+ type: "object",
101
+ properties: {},
102
+ additionalProperties: false,
103
+ },
90
104
  execute: async (input, options = {}) => {
91
105
  options.signal?.throwIfAborted();
92
106
  controls.signal.throwIfAborted();
@@ -116,6 +130,7 @@ function createConnection(runtime, modelContext) {
116
130
  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.",
117
131
  inputSchema: {
118
132
  type: "object",
133
+ "x-nexface": { version: 2, control: true },
119
134
  properties: { operationId: { type: "string" } },
120
135
  required: ["operationId"],
121
136
  additionalProperties: false,
@@ -163,47 +178,100 @@ function createConnection(runtime, modelContext) {
163
178
  registrations.set(definition.name, registration);
164
179
  let result;
165
180
  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" };
166
212
  result = modelContext.registerTool({
213
+ annotations: { readOnlyHint: definition.effect === "read" },
167
214
  name: definition.name,
168
215
  description: definition.description,
169
216
  inputSchema: {
170
- type: "object",
171
- properties: {
172
- ...(confirmed
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
- ...(confirmed ? {} : { required: ["input"] }),
183
- additionalProperties: false,
217
+ ...businessSchema,
218
+ "x-nexface": metadata,
219
+ properties: confirmed
220
+ ? {
221
+ ...properties,
222
+ nxf_confirmation: {
223
+ ...definition.inputSchema.properties.confirmation,
224
+ description: "After the user answers, send only this field.",
225
+ },
226
+ }
227
+ : properties,
228
+ ...(confirmed
229
+ ? {
230
+ required: [],
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"] } }),
184
241
  },
185
242
  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.");
186
257
  const args = parameters;
187
- if (!args ||
188
- typeof args !== "object" ||
189
- Array.isArray(args) ||
190
- (!confirmed &&
191
- (!args.input || typeof args.input !== "object" || Array.isArray(args.input))) ||
192
- Object.keys(args).some((key) => key !== "input" &&
193
- key !== "progressLabel" &&
194
- !(confirmed && key === "confirmation")) ||
195
- (args.progressLabel !== undefined && typeof args.progressLabel !== "string")) {
196
- throw new TypeError(confirmed
197
- ? "WebMCP confirmation tools require input or confirmation, with an optional string progressLabel."
198
- : "WebMCP tools require { input: object, progressLabel?: string }.");
199
- }
200
- const { progressLabel, ...payload } = args;
201
- return runtime.execute(tool, confirmed ? payload : args.input, {
258
+ const { nxf_progressLabel: label, nxf_confirmation: answer, ...input } = args;
259
+ const hasAnswer = Object.hasOwn(args, "nxf_confirmation");
260
+ if (hasAnswer && (!confirmed || Object.keys(args).length !== 1))
261
+ throw new AgentToolError("INVALID_INPUT", "Send nxf_confirmation alone to a confirmation tool.");
262
+ if (label !== undefined && typeof label !== "string")
263
+ throw new TypeError("nxf_progressLabel must be a string.");
264
+ const payload = hasAnswer ? { confirmation: answer } : confirmed ? { input } : input;
265
+ const result = await runtime.execute(tool, payload, {
202
266
  ...(options.signal ? { signal: options.signal } : {}),
203
- ...(typeof args.progressLabel === "string"
204
- ? { progressLabel: args.progressLabel }
205
- : {}),
267
+ ...(typeof label === "string" ? { progressLabel: label } : {}),
206
268
  });
269
+ if (confirmed && isConfirmationRequest(result)) {
270
+ if (evaluation)
271
+ return evaluationConfirmationRequired();
272
+ return { ...result, nextAction: confirmationInstructions("nxf_confirmation") };
273
+ }
274
+ return result;
207
275
  },
208
276
  }, { signal: controller.signal });
209
277
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nexface/tools",
3
- "version": "0.1.21-alpha.5",
3
+ "version": "0.1.21-alpha.6",
4
4
  "type": "module",
5
5
  "types": "./dist/index.d.ts",
6
6
  "exports": {
@@ -1,18 +0,0 @@
1
- import type { ToolRuntime } from "../runtime.js";
2
- export interface ToolRenderContext<TInput = unknown, TResult = unknown> {
3
- input: TInput;
4
- status: "running" | "success" | "error" | "cancelled";
5
- result?: TResult;
6
- }
7
- export interface ToolPresentation {
8
- definition?: ReturnType<ToolRuntime["getTools"]>[number]["definition"];
9
- confirmationProtocol?: true;
10
- render(context: ToolRenderContext): unknown;
11
- }
12
- export interface ToolPresentationController {
13
- get(name: string): ToolPresentation | undefined;
14
- names(): readonly string[];
15
- subscribe(listener: () => void): () => void;
16
- }
17
- export declare const toolPresentationSymbol: unique symbol;
18
- export declare function getToolPresentationController(runtime: ToolRuntime): ToolPresentationController | undefined;
@@ -1,5 +0,0 @@
1
- // Generated from shared/tool-presentation.ts. Do not edit manually.
2
- export const toolPresentationSymbol = Symbol.for("@nexface/tools/tool-presentation");
3
- export function getToolPresentationController(runtime) {
4
- return runtime[toolPresentationSymbol];
5
- }