@nexface/tools 0.1.21-alpha.3 → 0.1.21-alpha.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/definition.d.ts +1 -0
- package/dist/definition.js +4 -3
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/react.d.ts +7 -1
- package/dist/react.js +15 -4
- package/dist/runtime.d.ts +2 -0
- package/dist/runtime.js +21 -2
- package/dist/shared/tool-presentation.d.ts +2 -1
- package/package.json +1 -1
package/dist/definition.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ 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;
|
|
8
9
|
export declare function defineAgentTool<TResult = unknown>(metadata: AgentToolMetadataInput): AgentToolDefinition<Record<string, never>, TResult>;
|
|
9
10
|
export declare function defineAgentTool<TInput = never, TResult = unknown, const TSchema extends AgentToolInputSchema = AgentToolInputSchema>(metadata: AgentToolMetadataInput & {
|
|
10
11
|
inputSchema: TSchema;
|
package/dist/definition.js
CHANGED
|
@@ -15,6 +15,7 @@ const reservedContextNames = new Set([
|
|
|
15
15
|
]);
|
|
16
16
|
const reservedToolNames = new Set([
|
|
17
17
|
...reservedContextNames,
|
|
18
|
+
"nxf_showToolResults",
|
|
18
19
|
"nxf_loadSkill",
|
|
19
20
|
"nxf_beginOperation",
|
|
20
21
|
"nxf_endOperation",
|
|
@@ -22,13 +23,13 @@ const reservedToolNames = new Set([
|
|
|
22
23
|
export function isValidAgentContextName(name) {
|
|
23
24
|
return agentNamePattern.test(name) && !reservedContextNames.has(name);
|
|
24
25
|
}
|
|
25
|
-
function
|
|
26
|
-
if (!agentNamePattern.test(name) || reservedToolNames.has(name)) {
|
|
26
|
+
export function assertAgentToolName(name) {
|
|
27
|
+
if (typeof name !== "string" || !agentNamePattern.test(name) || reservedToolNames.has(name)) {
|
|
27
28
|
throw new Error(`Invalid or reserved agent tool name: ${name}.`);
|
|
28
29
|
}
|
|
29
30
|
}
|
|
30
31
|
export function defineAgentTool(metadata) {
|
|
31
|
-
|
|
32
|
+
assertAgentToolName(metadata.name);
|
|
32
33
|
return createAgentToolDefinition({
|
|
33
34
|
...metadata,
|
|
34
35
|
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, } from "./react.js";
|
|
5
|
+
export { AgentContext, ToolsProvider, AgentUIAction, useAgentContext, useAgentNavigation, useAgentTool, useRenderTool, } 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, } from "./react.js";
|
|
5
|
+
export { AgentContext, ToolsProvider, AgentUIAction, useAgentContext, useAgentNavigation, useAgentTool, useRenderTool, } from "./react.js";
|
|
6
6
|
export { registerAgentTool } from "./registration.js";
|
package/dist/react.d.ts
CHANGED
|
@@ -40,7 +40,7 @@ export interface UseAgentToolDefaultOptions<TInput = unknown, TResult = unknown>
|
|
|
40
40
|
/**
|
|
41
41
|
* @experimental
|
|
42
42
|
* @warning Experimental API. Its signature and behavior may change.
|
|
43
|
-
* @remarks JSON-compatible results retain their types
|
|
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
44
|
* @beta
|
|
45
45
|
*/
|
|
46
46
|
_render?: (context: {
|
|
@@ -81,6 +81,12 @@ export declare function getAgentToolBindingInternals(binding: AgentToolBinding):
|
|
|
81
81
|
*/
|
|
82
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
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;
|
|
84
90
|
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;
|
|
85
91
|
export declare function ToolsProvider({ children, toolRuntime, webMcp, }: {
|
|
86
92
|
children: ReactNode;
|
package/dist/react.js
CHANGED
|
@@ -56,13 +56,15 @@ function useAgentBinding(definition, execute, options) {
|
|
|
56
56
|
const runtime = useInternalToolRuntime();
|
|
57
57
|
const confirmation = options.requiresConfirmation;
|
|
58
58
|
const confirmationProtocol = Boolean(options.confirm);
|
|
59
|
-
const presentation = useMemo(() =>
|
|
60
|
-
|
|
59
|
+
const presentation = useMemo(() => {
|
|
60
|
+
if (!options._render)
|
|
61
|
+
return undefined;
|
|
62
|
+
return {
|
|
61
63
|
definition,
|
|
62
64
|
...(confirmationProtocol ? { confirmationProtocol: true } : {}),
|
|
63
65
|
render: options._render,
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
+
};
|
|
67
|
+
}, [definition, options._render, confirmationProtocol]);
|
|
66
68
|
const presentationRef = useRef(presentation);
|
|
67
69
|
useLayoutEffect(() => {
|
|
68
70
|
presentationRef.current = presentation;
|
|
@@ -113,6 +115,15 @@ function useAgentBinding(definition, execute, options) {
|
|
|
113
115
|
export function useAgentTool(definition, execute, options = {}) {
|
|
114
116
|
return useAgentBinding(definition, execute, options);
|
|
115
117
|
}
|
|
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
|
+
}
|
|
116
127
|
function getAgentNavigationHandler(handlers, key) {
|
|
117
128
|
if (!handlers || typeof handlers !== "object" || !Object.hasOwn(handlers, key)) {
|
|
118
129
|
throw new Error(`Missing navigation executor: ${key}.`);
|
package/dist/runtime.d.ts
CHANGED
|
@@ -127,6 +127,7 @@ export declare class AgentContextError extends Error {
|
|
|
127
127
|
}
|
|
128
128
|
export interface InternalToolRuntime extends ToolRuntime, EventTarget {
|
|
129
129
|
notifyToolPresentationChange(): void;
|
|
130
|
+
bindToolRenderer(name: string, presentation: ToolPresentation): () => void;
|
|
130
131
|
usesConfirmationProtocol(tool: RuntimeAgentTool): boolean;
|
|
131
132
|
beginInternalOperation(options?: {
|
|
132
133
|
signal?: AbortSignal;
|
|
@@ -183,6 +184,7 @@ export declare class ToolRuntimeImplementation extends EventTarget implements To
|
|
|
183
184
|
setPageActionFeedback(listener: (event: AgentToolExecutionEvent, feedback: PageActionFeedback, signal?: AbortSignal) => void | Promise<void>): () => void;
|
|
184
185
|
constructor(options?: ToolRuntimeOptions);
|
|
185
186
|
notifyToolPresentationChange(): void;
|
|
187
|
+
bindToolRenderer(name: string, presentation: ToolPresentation): () => void;
|
|
186
188
|
on(eventName: "toolchange", listener: (event: ToolRuntimeToolChangeEvent) => void): () => void;
|
|
187
189
|
on(eventName: "toolexecution", listener: (event: AgentToolExecutionEvent) => void): () => void;
|
|
188
190
|
register<TInput, TResult>(definition: AgentToolDefinition<TInput, TResult>, options: {
|
package/dist/runtime.js
CHANGED
|
@@ -9,7 +9,7 @@ import { AgentToolError, TOOL_ERROR_CODES } from "./shared/errors.js";
|
|
|
9
9
|
export { AgentToolError } from "./shared/errors.js";
|
|
10
10
|
import { assertAgentToolRegistration } from "./registration.js";
|
|
11
11
|
import { OperationTracker } from "./operation-runtime.js";
|
|
12
|
-
import { defineAgentTool, isValidAgentContextName } from "./definition.js";
|
|
12
|
+
import { assertAgentToolName, defineAgentTool, isValidAgentContextName } from "./definition.js";
|
|
13
13
|
import { assertAgentToolInputSchema, validateAgentToolInput } from "./shared/schema.js";
|
|
14
14
|
import { createBrowserToolset } from "./browser/toolset.js";
|
|
15
15
|
const CONTEXT_ERROR_CODES = [
|
|
@@ -123,6 +123,7 @@ export function getInternalToolRuntime(runtime) {
|
|
|
123
123
|
export class ToolRuntimeImplementation extends EventTarget {
|
|
124
124
|
[toolRuntimeBrand] = true;
|
|
125
125
|
#bindings = new Map();
|
|
126
|
+
#renderers = new Map();
|
|
126
127
|
#routeSearch;
|
|
127
128
|
#visibleNavigation = new Set();
|
|
128
129
|
#contexts = new Map();
|
|
@@ -205,8 +206,13 @@ export class ToolRuntimeImplementation extends EventTarget {
|
|
|
205
206
|
getRegisteredTools: () => this.getRegisteredTools(),
|
|
206
207
|
});
|
|
207
208
|
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
|
+
};
|
|
208
213
|
const presentations = {
|
|
209
|
-
get:
|
|
214
|
+
get: getPresentation,
|
|
215
|
+
names: () => [...new Set([...this.#bindings.keys(), ...this.#renderers.keys()])].filter((name) => getPresentation(name)),
|
|
210
216
|
subscribe: (listener) => {
|
|
211
217
|
this.addEventListener("toolchange", listener);
|
|
212
218
|
this.addEventListener("toolpresentationchange", listener);
|
|
@@ -221,6 +227,19 @@ export class ToolRuntimeImplementation extends EventTarget {
|
|
|
221
227
|
notifyToolPresentationChange() {
|
|
222
228
|
this.dispatchEvent(new Event("toolpresentationchange"));
|
|
223
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
|
+
}
|
|
224
243
|
on(eventName, listener) {
|
|
225
244
|
const handleEvent = (event) => {
|
|
226
245
|
if (eventName === "toolchange") {
|
|
@@ -5,12 +5,13 @@ export interface ToolRenderContext<TInput = unknown, TResult = unknown> {
|
|
|
5
5
|
result?: TResult;
|
|
6
6
|
}
|
|
7
7
|
export interface ToolPresentation {
|
|
8
|
-
definition
|
|
8
|
+
definition?: ReturnType<ToolRuntime["getTools"]>[number]["definition"];
|
|
9
9
|
confirmationProtocol?: true;
|
|
10
10
|
render(context: ToolRenderContext): unknown;
|
|
11
11
|
}
|
|
12
12
|
export interface ToolPresentationController {
|
|
13
13
|
get(name: string): ToolPresentation | undefined;
|
|
14
|
+
names(): readonly string[];
|
|
14
15
|
subscribe(listener: () => void): () => void;
|
|
15
16
|
}
|
|
16
17
|
export declare const toolPresentationSymbol: unique symbol;
|