@nexface/tools 0.1.19 → 0.1.20-alpha.0

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.
@@ -13,6 +13,7 @@ export declare class PageController {
13
13
  private readonly limits;
14
14
  private readonly animator;
15
15
  private readonly executions;
16
+ inputObserver: ((element: Element, command: PageCommand) => void) | undefined;
16
17
  constructor(limits: ExecutionLimits, animator: ActionFeedback);
17
18
  beginExecution(executionId: string, registry: SnapshotRegistry): void;
18
19
  cancelExecution(executionId: string): void;
@@ -291,6 +291,7 @@ export class PageController {
291
291
  limits;
292
292
  animator;
293
293
  executions = new Map();
294
+ inputObserver;
294
295
  constructor(limits, animator) {
295
296
  this.limits = limits;
296
297
  this.animator = animator;
@@ -678,6 +679,8 @@ export class PageController {
678
679
  }
679
680
  ensureActionable(element);
680
681
  this.requireStillActive(record);
682
+ if (command.method === "fill")
683
+ this.inputObserver?.(element, command);
681
684
  const result = this.performAction(element, command);
682
685
  this.recordCompletedAction(record, command);
683
686
  return result;
@@ -0,0 +1,11 @@
1
+ import type { ExecutionResult, PageCommand } from "./types.js";
2
+ type Feedback = NonNullable<ExecutionResult["inputFeedback"]>;
3
+ export declare class InputObservation {
4
+ private readonly maxResultChars;
5
+ private readonly edits;
6
+ constructor(maxResultChars: number);
7
+ record(element: Element, command: PageCommand): void;
8
+ read(): Feedback;
9
+ clear(): void;
10
+ }
11
+ export {};
@@ -0,0 +1,65 @@
1
+ import { isAgentIgnored } from "./dom.js";
2
+ const sensitive = (element) => isAgentIgnored(element) ||
3
+ (element instanceof HTMLInputElement && ["password", "file", "hidden"].includes(element.type));
4
+ // Scoped to one execution; no DOM references are included in the receipt.
5
+ export class InputObservation {
6
+ maxResultChars;
7
+ edits = new Map();
8
+ constructor(maxResultChars) {
9
+ this.maxResultChars = maxResultChars;
10
+ }
11
+ record(element, command) {
12
+ if (command.method !== "fill")
13
+ return;
14
+ this.edits.delete(element);
15
+ this.edits.set(element, {
16
+ callIndex: command.commandSeq,
17
+ value: sensitive(element) ? "" : String(command.args[0] ?? ""),
18
+ redacted: sensitive(element),
19
+ });
20
+ }
21
+ read() {
22
+ const feedback = [];
23
+ let budget = Math.floor(this.maxResultChars / 4);
24
+ for (const [element, edit] of this.edits) {
25
+ const entry = { callIndex: edit.callIndex };
26
+ try {
27
+ if (edit.redacted || sensitive(element))
28
+ entry.unavailable = "redacted";
29
+ else if (!element.isConnected)
30
+ entry.unavailable = "detached";
31
+ else {
32
+ const observed = element instanceof HTMLInputElement || element instanceof HTMLTextAreaElement
33
+ ? element.value
34
+ : element instanceof HTMLElement && element.isContentEditable
35
+ ? (element.textContent ?? "")
36
+ : undefined;
37
+ if (observed === undefined)
38
+ entry.unavailable = "not-readable";
39
+ else {
40
+ entry.requestedValue = edit.value.slice(0, 256);
41
+ entry.observedValue = observed.slice(0, 256);
42
+ entry.valueMatches = observed === edit.value;
43
+ if (observed.length > 256 || edit.value.length > 256)
44
+ entry.truncated = true;
45
+ }
46
+ }
47
+ }
48
+ catch {
49
+ delete entry.requestedValue;
50
+ delete entry.observedValue;
51
+ delete entry.valueMatches;
52
+ entry.unavailable = "not-readable";
53
+ }
54
+ const size = JSON.stringify(entry).length + 1;
55
+ if (size > budget)
56
+ break;
57
+ budget -= size;
58
+ feedback.push(entry);
59
+ }
60
+ return feedback;
61
+ }
62
+ clear() {
63
+ this.edits.clear();
64
+ }
65
+ }
@@ -415,7 +415,7 @@ function renderTree(nodes, context) {
415
415
  const name = node.name ? ` ${JSON.stringify(normalizeText(node.name))}` : "";
416
416
  const state = serializeState(node.state);
417
417
  const scrollability = serializeScrollability(node.scrollable);
418
- const nextRef = (node.actionable || node.scrollable) && (!node.genericAction || context.genericRefs < 64)
418
+ const nextRef = (node.actionable || node.scrollable) && (!node.genericAction || context.genericRefs < 150)
419
419
  ? `@${context.refs.size + 1}`
420
420
  : undefined;
421
421
  const refText = nextRef ? ` [ref=${nextRef}]` : "";
@@ -3,6 +3,7 @@ import { AgentToolError } from "../runtime.js";
3
3
  import { validateAgentToolInput } from "../shared/schema.js";
4
4
  import { getSnapshotTool, executeActionTool, applyActionTool } from "../shared/page-tools.js";
5
5
  export { getSnapshotTool, executeActionTool, applyActionTool } from "../shared/page-tools.js";
6
+ import { InputObservation } from "./input-observation.js";
6
7
  import { PageController } from "./controller.js";
7
8
  import { ActionFeedback } from "./feedback.js";
8
9
  import { createSnapshot } from "./snapshot.js";
@@ -288,6 +289,8 @@ export function createBrowserToolset(options = {}) {
288
289
  const onAbort = () => cancel();
289
290
  signal.addEventListener("abort", onAbort, { once: true });
290
291
  const tracker = startPageTransitionTracker(mode);
292
+ const inputs = new InputObservation(limits.maxResultChars);
293
+ controller.inputObserver = (element, command) => inputs.record(element, command);
291
294
  feedback.listener = onFeedback;
292
295
  feedback.assertActive = assertActive;
293
296
  let result;
@@ -353,10 +356,21 @@ export function createBrowserToolset(options = {}) {
353
356
  throw new AgentToolError("EXECUTION_OUTCOME_UNKNOWN", error instanceof Error ? error.message : "Browser execution failed.", { refsExpired: true });
354
357
  }
355
358
  finally {
356
- feedback.listener = undefined;
357
- feedback.assertActive = undefined;
358
- tracker.dispose();
359
- signal.removeEventListener("abort", onAbort);
359
+ try {
360
+ if (result) {
361
+ const inputFeedback = inputs.read();
362
+ if (inputFeedback.length)
363
+ result.inputFeedback = inputFeedback;
364
+ }
365
+ }
366
+ finally {
367
+ controller.inputObserver = undefined;
368
+ inputs.clear();
369
+ feedback.listener = undefined;
370
+ feedback.assertActive = undefined;
371
+ tracker.dispose();
372
+ signal.removeEventListener("abort", onAbort);
373
+ }
360
374
  }
361
375
  };
362
376
  const executeCode = {
@@ -94,6 +94,14 @@ export interface ExecutionResult {
94
94
  statement?: string;
95
95
  };
96
96
  snapshot?: PageSnapshot;
97
+ inputFeedback?: {
98
+ callIndex: number;
99
+ requestedValue?: string;
100
+ observedValue?: string;
101
+ valueMatches?: boolean;
102
+ unavailable?: "detached" | "redacted" | "not-readable";
103
+ truncated?: boolean;
104
+ }[];
97
105
  snapshotUnavailable?: boolean;
98
106
  postSnapshotTimedOut?: boolean;
99
107
  refsExpired?: boolean;
package/dist/react.d.ts CHANGED
@@ -22,7 +22,24 @@ export interface AgentToolPageConfirmationConfig {
22
22
  title?: string;
23
23
  description?: string;
24
24
  }
25
- export interface UseAgentToolDefaultOptions {
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> {
32
+ /**
33
+ * @experimental
34
+ * @warning Experimental API. Its signature and behavior may change.
35
+ * @remarks JSON-compatible results retain their types. Other results are unknown after serialization; void results are null.
36
+ * @beta
37
+ */
38
+ _render?: (context: {
39
+ input: TInput;
40
+ status: "running" | "success" | "error" | "cancelled";
41
+ result?: ToolRenderResult<TResult>;
42
+ }) => ReactNode;
26
43
  /**
27
44
  * @experimental Enables human-in-the-loop Chat confirmation.
28
45
  * @beta
@@ -33,7 +50,7 @@ export interface UseAgentToolDefaultOptions {
33
50
  * @experimental Enables human-in-the-loop Page confirmation.
34
51
  * @beta
35
52
  */
36
- export interface UseAgentToolPageConfirmationOptions {
53
+ export interface UseAgentToolPageConfirmationOptions<TInput = unknown, TResult = unknown> extends Omit<UseAgentToolDefaultOptions<TInput, TResult>, "requiresConfirmation"> {
37
54
  requiresConfirmation: AgentToolPageConfirmationConfig;
38
55
  }
39
56
  export interface AgentToolBinding<TInput = unknown, TElement extends HTMLElement = HTMLElement> {
@@ -53,8 +70,8 @@ export declare function getAgentToolBindingInternals(binding: AgentToolBinding):
53
70
  * @experimental The Page confirmation overload is experimental.
54
71
  * @beta
55
72
  */
56
- export declare function useAgentTool<TInput, TResult, TElement extends HTMLElement = HTMLElement>(definition: AgentToolDefinition<TInput, TResult>, execute: AgentToolExecutor<TInput, TResult>, options: UseAgentToolPageConfirmationOptions): AgentToolBinding<TInput, TElement>;
57
- export declare function useAgentTool<TInput, TResult, TElement extends HTMLElement = HTMLElement>(definition: AgentToolDefinition<TInput, TResult>, execute: AgentToolExecutor<TInput, TResult>, options?: UseAgentToolDefaultOptions): AgentToolBinding<TInput, TElement>;
73
+ 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>;
74
+ 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>;
58
75
  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;
59
76
  export declare function ToolsProvider({ children, toolRuntime, webMcp, }: {
60
77
  children: ReactNode;
package/dist/react.js CHANGED
@@ -55,6 +55,17 @@ function createAgentToolBinding(toolName) {
55
55
  function useAgentBinding(definition, execute, options) {
56
56
  const runtime = useInternalToolRuntime();
57
57
  const confirmation = options.requiresConfirmation;
58
+ const presentation = useMemo(() => options._render
59
+ ? {
60
+ definition,
61
+ render: options._render,
62
+ }
63
+ : undefined, [definition, options._render]);
64
+ const presentationRef = useRef(presentation);
65
+ useLayoutEffect(() => {
66
+ presentationRef.current = presentation;
67
+ runtime.notifyToolPresentationChange();
68
+ }, [runtime, presentation]);
58
69
  if (confirmation !== undefined &&
59
70
  (!confirmation ||
60
71
  typeof confirmation !== "object" ||
@@ -72,6 +83,7 @@ function useAgentBinding(definition, execute, options) {
72
83
  runtime.bindTool(definition, (input, context) => executeRef.current(input, context), {
73
84
  signal: controller.signal,
74
85
  getConfirmation: () => confirmationRef.current,
86
+ getPresentation: () => presentationRef.current,
75
87
  resolveTarget: (input) => getAgentToolBindingInternals(binding).resolveTarget(input),
76
88
  });
77
89
  return () => controller.abort();
package/dist/runtime.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { type ToolPresentation } from "./shared/tool-presentation.js";
1
2
  import { type InternalExecutionOptions } from "./shared/runtime-execution.js";
2
3
  import type { PageActionFeedback } from "./browser/feedback.js";
3
4
  import { type TaskProgress } from "./shared/runtime-controller.js";
@@ -116,6 +117,7 @@ export declare class AgentContextError extends Error {
116
117
  constructor(code: AgentContextErrorCode, message: string);
117
118
  }
118
119
  export interface InternalToolRuntime extends ToolRuntime, EventTarget {
120
+ notifyToolPresentationChange(): void;
119
121
  beginInternalOperation(options?: {
120
122
  signal?: AbortSignal;
121
123
  onCancel?: () => void;
@@ -134,6 +136,7 @@ export interface InternalToolRuntime extends ToolRuntime, EventTarget {
134
136
  bindTool<TInput, TResult>(definition: AgentToolDefinition<TInput, TResult>, execute: AgentToolExecutor<TInput, TResult>, options?: {
135
137
  signal?: AbortSignal;
136
138
  getConfirmation?: () => AgentToolConfirmationConfig | undefined;
139
+ getPresentation?: () => ToolPresentation | undefined;
137
140
  resolveTarget?: (input: unknown) => HTMLElement | undefined;
138
141
  }): () => void;
139
142
  registerContext(input: AgentContextInput, options?: {
@@ -165,6 +168,7 @@ export declare class ToolRuntimeImplementation extends EventTarget implements To
165
168
  requestOperationCancel(id: string): void;
166
169
  setPageActionFeedback(listener: (event: AgentToolExecutionEvent, feedback: PageActionFeedback, signal?: AbortSignal) => void | Promise<void>): () => void;
167
170
  constructor(options?: ToolRuntimeOptions);
171
+ notifyToolPresentationChange(): void;
168
172
  on(eventName: "toolchange", listener: (event: ToolRuntimeToolChangeEvent) => void): () => void;
169
173
  on(eventName: "toolexecution", listener: (event: AgentToolExecutionEvent) => void): () => void;
170
174
  register<TInput, TResult>(definition: AgentToolDefinition<TInput, TResult>, options: {
@@ -174,6 +178,7 @@ export declare class ToolRuntimeImplementation extends EventTarget implements To
174
178
  bindTool<TInput, TResult>(definition: AgentToolDefinition<TInput, TResult>, execute: AgentToolExecutor<TInput, TResult>, options?: {
175
179
  signal?: AbortSignal;
176
180
  getConfirmation?: () => AgentToolConfirmationConfig | undefined;
181
+ getPresentation?: () => ToolPresentation | undefined;
177
182
  resolveTarget?: (input: unknown) => HTMLElement | undefined;
178
183
  }): () => void;
179
184
  registerContext(input: AgentContextInput, options?: {
package/dist/runtime.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { searchNavigationTools, searchToolDefinition } from "./tool-search.js";
2
+ import { toolPresentationSymbol, } from "./shared/tool-presentation.js";
2
3
  import { pageDecisionSymbol } from "./shared/runtime-execution.js";
3
4
  import { PageCommandError } from "./browser/locator.js";
4
5
  import { measurePagePhase } from "./browser/diagnostics.js";
@@ -203,6 +204,21 @@ export class ToolRuntimeImplementation extends EventTarget {
203
204
  getRegisteredTools: () => this.getRegisteredTools(),
204
205
  });
205
206
  Object.defineProperty(this, runtimeControllerSymbol, { value: controller });
207
+ const presentations = {
208
+ get: (name) => this.#bindings.get(name)?.getPresentation?.(),
209
+ subscribe: (listener) => {
210
+ this.addEventListener("toolchange", listener);
211
+ this.addEventListener("toolpresentationchange", listener);
212
+ return () => {
213
+ this.removeEventListener("toolchange", listener);
214
+ this.removeEventListener("toolpresentationchange", listener);
215
+ };
216
+ },
217
+ };
218
+ Object.defineProperty(this, toolPresentationSymbol, { value: Object.freeze(presentations) });
219
+ }
220
+ notifyToolPresentationChange() {
221
+ this.dispatchEvent(new Event("toolpresentationchange"));
206
222
  }
207
223
  on(eventName, listener) {
208
224
  const handleEvent = (event) => {
@@ -255,6 +271,7 @@ export class ToolRuntimeImplementation extends EventTarget {
255
271
  lifecycleSignal: lifecycleController.signal,
256
272
  });
257
273
  const binding = {
274
+ ...(options.getPresentation ? { getPresentation: options.getPresentation } : {}),
258
275
  ...(options.resolveTarget ? { resolveTarget: options.resolveTarget } : {}),
259
276
  definition,
260
277
  runtimeTool,
@@ -0,0 +1,16 @@
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
+ render(context: ToolRenderContext): unknown;
10
+ }
11
+ export interface ToolPresentationController {
12
+ get(name: string): ToolPresentation | undefined;
13
+ subscribe(listener: () => void): () => void;
14
+ }
15
+ export declare const toolPresentationSymbol: unique symbol;
16
+ export declare function getToolPresentationController(runtime: ToolRuntime): ToolPresentationController | undefined;
@@ -0,0 +1,5 @@
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
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nexface/tools",
3
- "version": "0.1.19",
3
+ "version": "0.1.20-alpha.0",
4
4
  "type": "module",
5
5
  "types": "./dist/index.d.ts",
6
6
  "exports": {