@antipopp/agno-react 0.11.0 → 0.12.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.
- package/README.md +45 -0
- package/dist/index.d.mts +54 -5
- package/dist/index.d.ts +54 -5
- package/dist/index.js +72 -2
- package/dist/index.mjs +70 -2
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -274,6 +274,51 @@ function InitComponent() {
|
|
|
274
274
|
}
|
|
275
275
|
```
|
|
276
276
|
|
|
277
|
+
### useAgnoToolExecution()
|
|
278
|
+
|
|
279
|
+
Hook for Human-in-the-Loop (HITL) frontend tool execution.
|
|
280
|
+
|
|
281
|
+
```tsx
|
|
282
|
+
import {
|
|
283
|
+
createToolArgsValidatorFromSafeParse,
|
|
284
|
+
createValidatedToolHandler,
|
|
285
|
+
type ToolHandler,
|
|
286
|
+
useAgnoToolExecution,
|
|
287
|
+
} from '@antipopp/agno-react';
|
|
288
|
+
import { z } from 'zod';
|
|
289
|
+
|
|
290
|
+
const navigateSchema = z.object({
|
|
291
|
+
url: z.string().url(),
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
const navigateValidator = createToolArgsValidatorFromSafeParse((args) =>
|
|
295
|
+
navigateSchema.safeParse(args)
|
|
296
|
+
);
|
|
297
|
+
|
|
298
|
+
const toolHandlers: Record<string, ToolHandler> = {
|
|
299
|
+
navigate_to_page: createValidatedToolHandler(navigateValidator, (args) => {
|
|
300
|
+
window.location.href = args.url;
|
|
301
|
+
return { success: true, url: args.url };
|
|
302
|
+
}),
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
function ChatWithTools() {
|
|
306
|
+
const { isPaused, isExecuting, pendingTools, executionError } =
|
|
307
|
+
useAgnoToolExecution(toolHandlers, true);
|
|
308
|
+
|
|
309
|
+
return <div>{isExecuting ? `Executing ${pendingTools.length} tools...` : null}</div>;
|
|
310
|
+
}
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
Scope of this hook:
|
|
314
|
+
- Listens to paused run events and tracks execution state for your UI
|
|
315
|
+
- Executes local/global tool handlers and continues paused runs
|
|
316
|
+
- Supports auto execution or manual approval flows
|
|
317
|
+
- Hydrates tool-call UI components when sessions are loaded
|
|
318
|
+
|
|
319
|
+
Validation note:
|
|
320
|
+
- Tool arguments are runtime input. Prefer `createValidatedToolHandler()` with schema validation to avoid repeating manual guards.
|
|
321
|
+
|
|
277
322
|
## Complete Example
|
|
278
323
|
|
|
279
324
|
```tsx
|
package/dist/index.d.mts
CHANGED
|
@@ -41,8 +41,14 @@ declare function useAgnoClient(): AgnoClient;
|
|
|
41
41
|
|
|
42
42
|
/**
|
|
43
43
|
* Tool handler function type (now supports generative UI)
|
|
44
|
+
* - Receives normalized object arguments
|
|
45
|
+
* - Can return sync or async values
|
|
44
46
|
*/
|
|
45
|
-
type
|
|
47
|
+
type MaybePromise$1<T> = T | Promise<T>;
|
|
48
|
+
type ToolHandler<TArgs extends ToolCall["tool_args"] = ToolCall["tool_args"], TResult = unknown> = {
|
|
49
|
+
bivarianceHack(args: TArgs): MaybePromise$1<TResult>;
|
|
50
|
+
}["bivarianceHack"];
|
|
51
|
+
type ToolHandlers = Record<string, ToolHandler>;
|
|
46
52
|
/**
|
|
47
53
|
* Get a custom render function by key
|
|
48
54
|
*/
|
|
@@ -85,7 +91,7 @@ interface ToolExecutionEvent {
|
|
|
85
91
|
* const { isPaused, isExecuting, pendingTools } = useAgnoToolExecution(toolHandlers);
|
|
86
92
|
* ```
|
|
87
93
|
*/
|
|
88
|
-
declare function useAgnoToolExecution(handlers?:
|
|
94
|
+
declare function useAgnoToolExecution(handlers?: ToolHandlers, autoExecute?: boolean): {
|
|
89
95
|
/** Whether the run is currently paused awaiting tool execution */
|
|
90
96
|
isPaused: boolean;
|
|
91
97
|
/** Whether tools are currently being executed */
|
|
@@ -109,12 +115,12 @@ declare function useAgnoToolExecution(handlers?: Record<string, ToolHandler>, au
|
|
|
109
115
|
* Context value for tool handler registry
|
|
110
116
|
*/
|
|
111
117
|
interface ToolHandlerContextValue {
|
|
112
|
-
handlers:
|
|
118
|
+
handlers: ToolHandlers;
|
|
113
119
|
registerHandler: (name: string, handler: ToolHandler) => void;
|
|
114
120
|
unregisterHandler: (name: string) => void;
|
|
115
121
|
}
|
|
116
122
|
interface ToolHandlerProviderProps {
|
|
117
|
-
handlers?:
|
|
123
|
+
handlers?: ToolHandlers;
|
|
118
124
|
children: React.ReactNode;
|
|
119
125
|
}
|
|
120
126
|
/**
|
|
@@ -275,6 +281,49 @@ declare function registerChartComponent(name: string, renderer: ComponentRendere
|
|
|
275
281
|
*/
|
|
276
282
|
declare function getChartComponent(name: string): ComponentRenderer | undefined;
|
|
277
283
|
|
|
284
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
285
|
+
interface ToolArgsValidationSuccess<TArgs> {
|
|
286
|
+
success: true;
|
|
287
|
+
data: TArgs;
|
|
288
|
+
}
|
|
289
|
+
interface ToolArgsValidationFailure<TIssues = unknown> {
|
|
290
|
+
success: false;
|
|
291
|
+
message?: string;
|
|
292
|
+
issues?: TIssues;
|
|
293
|
+
}
|
|
294
|
+
type ToolArgsValidationResult<TArgs, TIssues = unknown> = ToolArgsValidationSuccess<TArgs> | ToolArgsValidationFailure<TIssues>;
|
|
295
|
+
type ToolArgsValidator<TArgs, TIssues = unknown> = (args: Record<string, unknown>) => MaybePromise<ToolArgsValidationResult<TArgs, TIssues>>;
|
|
296
|
+
interface ToolValidationError<TIssues = unknown> {
|
|
297
|
+
success: false;
|
|
298
|
+
code: "INVALID_TOOL_ARGS";
|
|
299
|
+
error: string;
|
|
300
|
+
issues?: TIssues;
|
|
301
|
+
}
|
|
302
|
+
interface CreateValidatedToolHandlerOptions<TIssues = unknown, TValidationError = ToolValidationError<TIssues>> {
|
|
303
|
+
errorMessage?: string;
|
|
304
|
+
mapValidationError?: (failure: ToolArgsValidationFailure<TIssues>, args: Record<string, unknown>) => TValidationError;
|
|
305
|
+
}
|
|
306
|
+
type SafeParseResult<TArgs, TIssues = unknown> = {
|
|
307
|
+
success: true;
|
|
308
|
+
data: TArgs;
|
|
309
|
+
} | {
|
|
310
|
+
success: false;
|
|
311
|
+
error: TIssues;
|
|
312
|
+
};
|
|
313
|
+
interface CreateValidatorFromSafeParseOptions<TIssues = unknown> {
|
|
314
|
+
getErrorMessage?: (issues: TIssues) => string;
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Create a ToolArgsValidator from libraries that expose safeParse-like APIs.
|
|
318
|
+
*
|
|
319
|
+
* This is validator-agnostic and works with Zod, Valibot, ArkType adapters, etc.
|
|
320
|
+
*/
|
|
321
|
+
declare function createToolArgsValidatorFromSafeParse<TArgs, TIssues = unknown>(safeParse: (args: Record<string, unknown>) => MaybePromise<SafeParseResult<TArgs, TIssues>>, options?: CreateValidatorFromSafeParseOptions<TIssues>): ToolArgsValidator<TArgs, TIssues>;
|
|
322
|
+
/**
|
|
323
|
+
* Wrap a tool handler with runtime argument validation.
|
|
324
|
+
*/
|
|
325
|
+
declare function createValidatedToolHandler<TArgs, TResult = unknown, TIssues = unknown, TValidationError = ToolValidationError<TIssues>>(validator: ToolArgsValidator<TArgs, TIssues>, handler: (args: TArgs) => MaybePromise<TResult>, options?: CreateValidatedToolHandlerOptions<TIssues, TValidationError>): ToolHandler<Record<string, unknown>, TResult | TValidationError | ToolValidationError<TIssues>>;
|
|
326
|
+
|
|
278
327
|
/**
|
|
279
328
|
* Helper utilities for creating UI component specifications
|
|
280
329
|
*
|
|
@@ -430,4 +479,4 @@ declare function resultWithCardGrid(cards: CardData[], options?: CardGridHelperO
|
|
|
430
479
|
*/
|
|
431
480
|
declare function resultWithTable(data: DataRow[], columns: TableColumn[], options?: TableHelperOptions): ToolHandlerResult;
|
|
432
481
|
|
|
433
|
-
export { AgnoProvider, type AgnoProviderProps, type CardGridHelperOptions, type ChartHelperOptions, ComponentRegistry, type ComponentRenderer, GenerativeUIRenderer, type GenerativeUIRendererProps, type TableHelperOptions, type ToolExecutionEvent, type ToolHandler, type ToolHandlerContextValue, ToolHandlerProvider, type ToolHandlerProviderProps, clearCustomRenderRegistry, createAreaChart, createArtifact, createBarChart, createCard, createCardGrid, createColumn, createLineChart, createMarkdown, createPieChart, createSmartChart, createTable, createToolResult, getChartComponent, getComponentRegistry, getCustomRender, registerChartComponent, resultWithBarChart, resultWithCardGrid, resultWithSmartChart, resultWithTable, useAgnoActions, useAgnoChat, useAgnoClient, useAgnoSession, useAgnoToolExecution, useToolHandlers };
|
|
482
|
+
export { AgnoProvider, type AgnoProviderProps, type CardGridHelperOptions, type ChartHelperOptions, ComponentRegistry, type ComponentRenderer, type CreateValidatedToolHandlerOptions, type CreateValidatorFromSafeParseOptions, GenerativeUIRenderer, type GenerativeUIRendererProps, type SafeParseResult, type TableHelperOptions, type ToolArgsValidationFailure, type ToolArgsValidationResult, type ToolArgsValidationSuccess, type ToolArgsValidator, type ToolExecutionEvent, type ToolHandler, type ToolHandlerContextValue, ToolHandlerProvider, type ToolHandlerProviderProps, type ToolHandlers, type ToolValidationError, clearCustomRenderRegistry, createAreaChart, createArtifact, createBarChart, createCard, createCardGrid, createColumn, createLineChart, createMarkdown, createPieChart, createSmartChart, createTable, createToolArgsValidatorFromSafeParse, createToolResult, createValidatedToolHandler, getChartComponent, getComponentRegistry, getCustomRender, registerChartComponent, resultWithBarChart, resultWithCardGrid, resultWithSmartChart, resultWithTable, useAgnoActions, useAgnoChat, useAgnoClient, useAgnoSession, useAgnoToolExecution, useToolHandlers };
|
package/dist/index.d.ts
CHANGED
|
@@ -41,8 +41,14 @@ declare function useAgnoClient(): AgnoClient;
|
|
|
41
41
|
|
|
42
42
|
/**
|
|
43
43
|
* Tool handler function type (now supports generative UI)
|
|
44
|
+
* - Receives normalized object arguments
|
|
45
|
+
* - Can return sync or async values
|
|
44
46
|
*/
|
|
45
|
-
type
|
|
47
|
+
type MaybePromise$1<T> = T | Promise<T>;
|
|
48
|
+
type ToolHandler<TArgs extends ToolCall["tool_args"] = ToolCall["tool_args"], TResult = unknown> = {
|
|
49
|
+
bivarianceHack(args: TArgs): MaybePromise$1<TResult>;
|
|
50
|
+
}["bivarianceHack"];
|
|
51
|
+
type ToolHandlers = Record<string, ToolHandler>;
|
|
46
52
|
/**
|
|
47
53
|
* Get a custom render function by key
|
|
48
54
|
*/
|
|
@@ -85,7 +91,7 @@ interface ToolExecutionEvent {
|
|
|
85
91
|
* const { isPaused, isExecuting, pendingTools } = useAgnoToolExecution(toolHandlers);
|
|
86
92
|
* ```
|
|
87
93
|
*/
|
|
88
|
-
declare function useAgnoToolExecution(handlers?:
|
|
94
|
+
declare function useAgnoToolExecution(handlers?: ToolHandlers, autoExecute?: boolean): {
|
|
89
95
|
/** Whether the run is currently paused awaiting tool execution */
|
|
90
96
|
isPaused: boolean;
|
|
91
97
|
/** Whether tools are currently being executed */
|
|
@@ -109,12 +115,12 @@ declare function useAgnoToolExecution(handlers?: Record<string, ToolHandler>, au
|
|
|
109
115
|
* Context value for tool handler registry
|
|
110
116
|
*/
|
|
111
117
|
interface ToolHandlerContextValue {
|
|
112
|
-
handlers:
|
|
118
|
+
handlers: ToolHandlers;
|
|
113
119
|
registerHandler: (name: string, handler: ToolHandler) => void;
|
|
114
120
|
unregisterHandler: (name: string) => void;
|
|
115
121
|
}
|
|
116
122
|
interface ToolHandlerProviderProps {
|
|
117
|
-
handlers?:
|
|
123
|
+
handlers?: ToolHandlers;
|
|
118
124
|
children: React.ReactNode;
|
|
119
125
|
}
|
|
120
126
|
/**
|
|
@@ -275,6 +281,49 @@ declare function registerChartComponent(name: string, renderer: ComponentRendere
|
|
|
275
281
|
*/
|
|
276
282
|
declare function getChartComponent(name: string): ComponentRenderer | undefined;
|
|
277
283
|
|
|
284
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
285
|
+
interface ToolArgsValidationSuccess<TArgs> {
|
|
286
|
+
success: true;
|
|
287
|
+
data: TArgs;
|
|
288
|
+
}
|
|
289
|
+
interface ToolArgsValidationFailure<TIssues = unknown> {
|
|
290
|
+
success: false;
|
|
291
|
+
message?: string;
|
|
292
|
+
issues?: TIssues;
|
|
293
|
+
}
|
|
294
|
+
type ToolArgsValidationResult<TArgs, TIssues = unknown> = ToolArgsValidationSuccess<TArgs> | ToolArgsValidationFailure<TIssues>;
|
|
295
|
+
type ToolArgsValidator<TArgs, TIssues = unknown> = (args: Record<string, unknown>) => MaybePromise<ToolArgsValidationResult<TArgs, TIssues>>;
|
|
296
|
+
interface ToolValidationError<TIssues = unknown> {
|
|
297
|
+
success: false;
|
|
298
|
+
code: "INVALID_TOOL_ARGS";
|
|
299
|
+
error: string;
|
|
300
|
+
issues?: TIssues;
|
|
301
|
+
}
|
|
302
|
+
interface CreateValidatedToolHandlerOptions<TIssues = unknown, TValidationError = ToolValidationError<TIssues>> {
|
|
303
|
+
errorMessage?: string;
|
|
304
|
+
mapValidationError?: (failure: ToolArgsValidationFailure<TIssues>, args: Record<string, unknown>) => TValidationError;
|
|
305
|
+
}
|
|
306
|
+
type SafeParseResult<TArgs, TIssues = unknown> = {
|
|
307
|
+
success: true;
|
|
308
|
+
data: TArgs;
|
|
309
|
+
} | {
|
|
310
|
+
success: false;
|
|
311
|
+
error: TIssues;
|
|
312
|
+
};
|
|
313
|
+
interface CreateValidatorFromSafeParseOptions<TIssues = unknown> {
|
|
314
|
+
getErrorMessage?: (issues: TIssues) => string;
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Create a ToolArgsValidator from libraries that expose safeParse-like APIs.
|
|
318
|
+
*
|
|
319
|
+
* This is validator-agnostic and works with Zod, Valibot, ArkType adapters, etc.
|
|
320
|
+
*/
|
|
321
|
+
declare function createToolArgsValidatorFromSafeParse<TArgs, TIssues = unknown>(safeParse: (args: Record<string, unknown>) => MaybePromise<SafeParseResult<TArgs, TIssues>>, options?: CreateValidatorFromSafeParseOptions<TIssues>): ToolArgsValidator<TArgs, TIssues>;
|
|
322
|
+
/**
|
|
323
|
+
* Wrap a tool handler with runtime argument validation.
|
|
324
|
+
*/
|
|
325
|
+
declare function createValidatedToolHandler<TArgs, TResult = unknown, TIssues = unknown, TValidationError = ToolValidationError<TIssues>>(validator: ToolArgsValidator<TArgs, TIssues>, handler: (args: TArgs) => MaybePromise<TResult>, options?: CreateValidatedToolHandlerOptions<TIssues, TValidationError>): ToolHandler<Record<string, unknown>, TResult | TValidationError | ToolValidationError<TIssues>>;
|
|
326
|
+
|
|
278
327
|
/**
|
|
279
328
|
* Helper utilities for creating UI component specifications
|
|
280
329
|
*
|
|
@@ -430,4 +479,4 @@ declare function resultWithCardGrid(cards: CardData[], options?: CardGridHelperO
|
|
|
430
479
|
*/
|
|
431
480
|
declare function resultWithTable(data: DataRow[], columns: TableColumn[], options?: TableHelperOptions): ToolHandlerResult;
|
|
432
481
|
|
|
433
|
-
export { AgnoProvider, type AgnoProviderProps, type CardGridHelperOptions, type ChartHelperOptions, ComponentRegistry, type ComponentRenderer, GenerativeUIRenderer, type GenerativeUIRendererProps, type TableHelperOptions, type ToolExecutionEvent, type ToolHandler, type ToolHandlerContextValue, ToolHandlerProvider, type ToolHandlerProviderProps, clearCustomRenderRegistry, createAreaChart, createArtifact, createBarChart, createCard, createCardGrid, createColumn, createLineChart, createMarkdown, createPieChart, createSmartChart, createTable, createToolResult, getChartComponent, getComponentRegistry, getCustomRender, registerChartComponent, resultWithBarChart, resultWithCardGrid, resultWithSmartChart, resultWithTable, useAgnoActions, useAgnoChat, useAgnoClient, useAgnoSession, useAgnoToolExecution, useToolHandlers };
|
|
482
|
+
export { AgnoProvider, type AgnoProviderProps, type CardGridHelperOptions, type ChartHelperOptions, ComponentRegistry, type ComponentRenderer, type CreateValidatedToolHandlerOptions, type CreateValidatorFromSafeParseOptions, GenerativeUIRenderer, type GenerativeUIRendererProps, type SafeParseResult, type TableHelperOptions, type ToolArgsValidationFailure, type ToolArgsValidationResult, type ToolArgsValidationSuccess, type ToolArgsValidator, type ToolExecutionEvent, type ToolHandler, type ToolHandlerContextValue, ToolHandlerProvider, type ToolHandlerProviderProps, type ToolHandlers, type ToolValidationError, clearCustomRenderRegistry, createAreaChart, createArtifact, createBarChart, createCard, createCardGrid, createColumn, createLineChart, createMarkdown, createPieChart, createSmartChart, createTable, createToolArgsValidatorFromSafeParse, createToolResult, createValidatedToolHandler, getChartComponent, getComponentRegistry, getCustomRender, registerChartComponent, resultWithBarChart, resultWithCardGrid, resultWithSmartChart, resultWithTable, useAgnoActions, useAgnoChat, useAgnoClient, useAgnoSession, useAgnoToolExecution, useToolHandlers };
|
package/dist/index.js
CHANGED
|
@@ -46,7 +46,9 @@ __export(src_exports, {
|
|
|
46
46
|
createPieChart: () => createPieChart,
|
|
47
47
|
createSmartChart: () => createSmartChart,
|
|
48
48
|
createTable: () => createTable,
|
|
49
|
+
createToolArgsValidatorFromSafeParse: () => createToolArgsValidatorFromSafeParse,
|
|
49
50
|
createToolResult: () => createToolResult,
|
|
51
|
+
createValidatedToolHandler: () => createValidatedToolHandler,
|
|
50
52
|
getChartComponent: () => getChartComponent,
|
|
51
53
|
getComponentRegistry: () => getComponentRegistry,
|
|
52
54
|
getCustomRender: () => getCustomRender,
|
|
@@ -216,6 +218,22 @@ function useToolHandlers() {
|
|
|
216
218
|
function isRecord(value) {
|
|
217
219
|
return typeof value === "object" && value !== null;
|
|
218
220
|
}
|
|
221
|
+
function normalizeToolArgs(rawArgs) {
|
|
222
|
+
if (isRecord(rawArgs)) {
|
|
223
|
+
return rawArgs;
|
|
224
|
+
}
|
|
225
|
+
if (typeof rawArgs !== "string") {
|
|
226
|
+
return {};
|
|
227
|
+
}
|
|
228
|
+
try {
|
|
229
|
+
const parsed = JSON.parse(rawArgs);
|
|
230
|
+
if (isRecord(parsed)) {
|
|
231
|
+
return parsed;
|
|
232
|
+
}
|
|
233
|
+
} catch {
|
|
234
|
+
}
|
|
235
|
+
return { content: rawArgs };
|
|
236
|
+
}
|
|
219
237
|
function getCustomRenderFunction(value) {
|
|
220
238
|
if (value.type !== "custom") {
|
|
221
239
|
return void 0;
|
|
@@ -251,7 +269,7 @@ async function executeToolCall(tool, handlers) {
|
|
|
251
269
|
};
|
|
252
270
|
}
|
|
253
271
|
try {
|
|
254
|
-
const result = await handler(tool.tool_args);
|
|
272
|
+
const result = await handler(normalizeToolArgs(tool.tool_args));
|
|
255
273
|
const { resultData, uiComponent } = processToolResult(result, tool);
|
|
256
274
|
return {
|
|
257
275
|
...tool,
|
|
@@ -277,7 +295,7 @@ async function hydrateToolUIForSession(tools, handlers, onHydrate) {
|
|
|
277
295
|
continue;
|
|
278
296
|
}
|
|
279
297
|
try {
|
|
280
|
-
const result = await handler(tool.tool_args);
|
|
298
|
+
const result = await handler(normalizeToolArgs(tool.tool_args));
|
|
281
299
|
const { uiComponent } = processToolResult(result, tool);
|
|
282
300
|
if (uiComponent) {
|
|
283
301
|
onHydrate(tool.tool_call_id, uiComponent);
|
|
@@ -922,6 +940,56 @@ function useAgnoSession() {
|
|
|
922
940
|
};
|
|
923
941
|
}
|
|
924
942
|
|
|
943
|
+
// src/utils/tool-handler-validation.ts
|
|
944
|
+
function toDefaultValidationError(failure, options) {
|
|
945
|
+
return {
|
|
946
|
+
success: false,
|
|
947
|
+
code: "INVALID_TOOL_ARGS",
|
|
948
|
+
error: failure.message ?? options?.errorMessage ?? "Invalid tool arguments",
|
|
949
|
+
...failure.issues !== void 0 ? { issues: failure.issues } : {}
|
|
950
|
+
};
|
|
951
|
+
}
|
|
952
|
+
function createToolArgsValidatorFromSafeParse(safeParse, options) {
|
|
953
|
+
return async (args) => {
|
|
954
|
+
const parsed = await safeParse(args);
|
|
955
|
+
if (parsed.success) {
|
|
956
|
+
return {
|
|
957
|
+
success: true,
|
|
958
|
+
data: parsed.data
|
|
959
|
+
};
|
|
960
|
+
}
|
|
961
|
+
return {
|
|
962
|
+
success: false,
|
|
963
|
+
message: options?.getErrorMessage?.(parsed.error),
|
|
964
|
+
issues: parsed.error
|
|
965
|
+
};
|
|
966
|
+
};
|
|
967
|
+
}
|
|
968
|
+
function createValidatedToolHandler(validator, handler, options) {
|
|
969
|
+
return async (args) => {
|
|
970
|
+
let validationResult;
|
|
971
|
+
try {
|
|
972
|
+
validationResult = await validator(args);
|
|
973
|
+
} catch (error) {
|
|
974
|
+
const thrownFailure = {
|
|
975
|
+
success: false,
|
|
976
|
+
message: error instanceof Error ? error.message : "Tool arguments validation threw an unexpected error"
|
|
977
|
+
};
|
|
978
|
+
if (options?.mapValidationError) {
|
|
979
|
+
return options.mapValidationError(thrownFailure, args);
|
|
980
|
+
}
|
|
981
|
+
return toDefaultValidationError(thrownFailure, options);
|
|
982
|
+
}
|
|
983
|
+
if (!validationResult.success) {
|
|
984
|
+
if (options?.mapValidationError) {
|
|
985
|
+
return options.mapValidationError(validationResult, args);
|
|
986
|
+
}
|
|
987
|
+
return toDefaultValidationError(validationResult, options);
|
|
988
|
+
}
|
|
989
|
+
return handler(validationResult.data);
|
|
990
|
+
};
|
|
991
|
+
}
|
|
992
|
+
|
|
925
993
|
// src/utils/ui-helpers.ts
|
|
926
994
|
function createBarChart(data, xKey, bars, options) {
|
|
927
995
|
return {
|
|
@@ -1182,7 +1250,9 @@ function resultWithTable(data, columns, options) {
|
|
|
1182
1250
|
createPieChart,
|
|
1183
1251
|
createSmartChart,
|
|
1184
1252
|
createTable,
|
|
1253
|
+
createToolArgsValidatorFromSafeParse,
|
|
1185
1254
|
createToolResult,
|
|
1255
|
+
createValidatedToolHandler,
|
|
1186
1256
|
getChartComponent,
|
|
1187
1257
|
getComponentRegistry,
|
|
1188
1258
|
getCustomRender,
|
package/dist/index.mjs
CHANGED
|
@@ -150,6 +150,22 @@ function useToolHandlers() {
|
|
|
150
150
|
function isRecord(value) {
|
|
151
151
|
return typeof value === "object" && value !== null;
|
|
152
152
|
}
|
|
153
|
+
function normalizeToolArgs(rawArgs) {
|
|
154
|
+
if (isRecord(rawArgs)) {
|
|
155
|
+
return rawArgs;
|
|
156
|
+
}
|
|
157
|
+
if (typeof rawArgs !== "string") {
|
|
158
|
+
return {};
|
|
159
|
+
}
|
|
160
|
+
try {
|
|
161
|
+
const parsed = JSON.parse(rawArgs);
|
|
162
|
+
if (isRecord(parsed)) {
|
|
163
|
+
return parsed;
|
|
164
|
+
}
|
|
165
|
+
} catch {
|
|
166
|
+
}
|
|
167
|
+
return { content: rawArgs };
|
|
168
|
+
}
|
|
153
169
|
function getCustomRenderFunction(value) {
|
|
154
170
|
if (value.type !== "custom") {
|
|
155
171
|
return void 0;
|
|
@@ -185,7 +201,7 @@ async function executeToolCall(tool, handlers) {
|
|
|
185
201
|
};
|
|
186
202
|
}
|
|
187
203
|
try {
|
|
188
|
-
const result = await handler(tool.tool_args);
|
|
204
|
+
const result = await handler(normalizeToolArgs(tool.tool_args));
|
|
189
205
|
const { resultData, uiComponent } = processToolResult(result, tool);
|
|
190
206
|
return {
|
|
191
207
|
...tool,
|
|
@@ -211,7 +227,7 @@ async function hydrateToolUIForSession(tools, handlers, onHydrate) {
|
|
|
211
227
|
continue;
|
|
212
228
|
}
|
|
213
229
|
try {
|
|
214
|
-
const result = await handler(tool.tool_args);
|
|
230
|
+
const result = await handler(normalizeToolArgs(tool.tool_args));
|
|
215
231
|
const { uiComponent } = processToolResult(result, tool);
|
|
216
232
|
if (uiComponent) {
|
|
217
233
|
onHydrate(tool.tool_call_id, uiComponent);
|
|
@@ -856,6 +872,56 @@ function useAgnoSession() {
|
|
|
856
872
|
};
|
|
857
873
|
}
|
|
858
874
|
|
|
875
|
+
// src/utils/tool-handler-validation.ts
|
|
876
|
+
function toDefaultValidationError(failure, options) {
|
|
877
|
+
return {
|
|
878
|
+
success: false,
|
|
879
|
+
code: "INVALID_TOOL_ARGS",
|
|
880
|
+
error: failure.message ?? options?.errorMessage ?? "Invalid tool arguments",
|
|
881
|
+
...failure.issues !== void 0 ? { issues: failure.issues } : {}
|
|
882
|
+
};
|
|
883
|
+
}
|
|
884
|
+
function createToolArgsValidatorFromSafeParse(safeParse, options) {
|
|
885
|
+
return async (args) => {
|
|
886
|
+
const parsed = await safeParse(args);
|
|
887
|
+
if (parsed.success) {
|
|
888
|
+
return {
|
|
889
|
+
success: true,
|
|
890
|
+
data: parsed.data
|
|
891
|
+
};
|
|
892
|
+
}
|
|
893
|
+
return {
|
|
894
|
+
success: false,
|
|
895
|
+
message: options?.getErrorMessage?.(parsed.error),
|
|
896
|
+
issues: parsed.error
|
|
897
|
+
};
|
|
898
|
+
};
|
|
899
|
+
}
|
|
900
|
+
function createValidatedToolHandler(validator, handler, options) {
|
|
901
|
+
return async (args) => {
|
|
902
|
+
let validationResult;
|
|
903
|
+
try {
|
|
904
|
+
validationResult = await validator(args);
|
|
905
|
+
} catch (error) {
|
|
906
|
+
const thrownFailure = {
|
|
907
|
+
success: false,
|
|
908
|
+
message: error instanceof Error ? error.message : "Tool arguments validation threw an unexpected error"
|
|
909
|
+
};
|
|
910
|
+
if (options?.mapValidationError) {
|
|
911
|
+
return options.mapValidationError(thrownFailure, args);
|
|
912
|
+
}
|
|
913
|
+
return toDefaultValidationError(thrownFailure, options);
|
|
914
|
+
}
|
|
915
|
+
if (!validationResult.success) {
|
|
916
|
+
if (options?.mapValidationError) {
|
|
917
|
+
return options.mapValidationError(validationResult, args);
|
|
918
|
+
}
|
|
919
|
+
return toDefaultValidationError(validationResult, options);
|
|
920
|
+
}
|
|
921
|
+
return handler(validationResult.data);
|
|
922
|
+
};
|
|
923
|
+
}
|
|
924
|
+
|
|
859
925
|
// src/utils/ui-helpers.ts
|
|
860
926
|
function createBarChart(data, xKey, bars, options) {
|
|
861
927
|
return {
|
|
@@ -1115,7 +1181,9 @@ export {
|
|
|
1115
1181
|
createPieChart,
|
|
1116
1182
|
createSmartChart,
|
|
1117
1183
|
createTable,
|
|
1184
|
+
createToolArgsValidatorFromSafeParse,
|
|
1118
1185
|
createToolResult,
|
|
1186
|
+
createValidatedToolHandler,
|
|
1119
1187
|
getChartComponent,
|
|
1120
1188
|
getComponentRegistry,
|
|
1121
1189
|
getCustomRender,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@antipopp/agno-react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0",
|
|
4
4
|
"description": "React hooks for Agno client with frontend tool execution (HITL) support",
|
|
5
5
|
"author": "antipopp",
|
|
6
6
|
"license": "MIT",
|
|
@@ -34,8 +34,8 @@
|
|
|
34
34
|
"README.md"
|
|
35
35
|
],
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@antipopp/agno-
|
|
38
|
-
"@antipopp/agno-
|
|
37
|
+
"@antipopp/agno-client": "0.12.0",
|
|
38
|
+
"@antipopp/agno-types": "0.12.0"
|
|
39
39
|
},
|
|
40
40
|
"peerDependencies": {
|
|
41
41
|
"react": "^18.0.0 || ^19.0.0"
|