@robota-sdk/agent-tools 3.0.0-beta.76 → 3.0.0-beta.77
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/LICENSE +661 -21
- package/README.md +36 -25
- package/dist/browser/browser.d.ts +27 -70
- package/dist/browser/browser.d.ts.map +1 -1
- package/dist/browser/browser.js +1 -1
- package/dist/browser/browser.js.map +1 -1
- package/dist/node/index.cjs +212 -138
- package/dist/node/index.d.ts +44 -74
- package/dist/node/index.d.ts.map +1 -1
- package/dist/node/index.js +210 -139
- package/dist/node/index.js.map +1 -1
- package/package.json +6 -5
package/README.md
CHANGED
|
@@ -18,16 +18,14 @@ Peer dependency: `@robota-sdk/agent-core`
|
|
|
18
18
|
import { createZodFunctionTool } from '@robota-sdk/agent-tools';
|
|
19
19
|
import { z } from 'zod';
|
|
20
20
|
|
|
21
|
-
const weatherTool = createZodFunctionTool(
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
21
|
+
const weatherTool = createZodFunctionTool(
|
|
22
|
+
'get_weather',
|
|
23
|
+
'Get current weather for a city',
|
|
24
|
+
z.object({
|
|
25
25
|
city: z.string().describe('City name'),
|
|
26
26
|
}),
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}),
|
|
30
|
-
});
|
|
27
|
+
async (args) => JSON.stringify({ city: args['city'], temperature: 22, condition: 'sunny' }),
|
|
28
|
+
);
|
|
31
29
|
```
|
|
32
30
|
|
|
33
31
|
### Use Built-in Tools
|
|
@@ -35,7 +33,9 @@ const weatherTool = createZodFunctionTool({
|
|
|
35
33
|
```typescript
|
|
36
34
|
import { bashTool, readTool, globTool, grepTool } from '@robota-sdk/agent-tools';
|
|
37
35
|
import { Robota } from '@robota-sdk/agent-core';
|
|
36
|
+
import type { IAIProvider } from '@robota-sdk/agent-core';
|
|
38
37
|
|
|
38
|
+
declare const provider: IAIProvider;
|
|
39
39
|
const agent = new Robota({
|
|
40
40
|
name: 'DevAgent',
|
|
41
41
|
aiProviders: [provider],
|
|
@@ -44,18 +44,26 @@ const agent = new Robota({
|
|
|
44
44
|
});
|
|
45
45
|
```
|
|
46
46
|
|
|
47
|
-
## Built-in Tools
|
|
47
|
+
## Built-in Tools
|
|
48
|
+
|
|
49
|
+
| Export | Tool Name | Description |
|
|
50
|
+
| --------------------- | --------------- | ----------------------------------------------------------------------------- |
|
|
51
|
+
| `shellTool` | Shell | Execute host shell commands; OS-aware (POSIX `sh`/`bash`, Windows PowerShell) |
|
|
52
|
+
| `bashTool` | Bash | Model-familiar alias of `Shell` — same OS-aware implementation |
|
|
53
|
+
| `readTool` | Read | Read file contents with line numbers (cat -n) |
|
|
54
|
+
| `writeTool` | Write | Write content to a file (creates parent dirs) |
|
|
55
|
+
| `editTool` | Edit | Replace a specific string in a file |
|
|
56
|
+
| `globTool` | Glob | Find files matching a glob pattern (fast-glob) |
|
|
57
|
+
| `grepTool` | Grep | Search file contents with regex patterns |
|
|
58
|
+
| `webFetchTool` | WebFetch | Fetch URL content (HTML-to-text conversion) |
|
|
59
|
+
| `webSearchTool` | WebSearch | Web search via Brave Search API |
|
|
60
|
+
| `askUserQuestionTool` | AskUserQuestion | Model asks the user structured questions (options/multi-select/free text) |
|
|
48
61
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
| `editTool` | Edit | Replace a specific string in a file |
|
|
55
|
-
| `globTool` | Glob | Find files matching a glob pattern (fast-glob) |
|
|
56
|
-
| `grepTool` | Grep | Search file contents with regex patterns |
|
|
57
|
-
| `webFetchTool` | WebFetch | Fetch URL content (HTML-to-text conversion) |
|
|
58
|
-
| `webSearchTool` | WebSearch | Web search via Brave Search API |
|
|
62
|
+
`AskUserQuestion` lets the model ask the user 1–4 structured questions mid-turn through the injected
|
|
63
|
+
ask port (CMD-004); each environment renders it its own way (Ink dialog, web modal, programmatic
|
|
64
|
+
pre-answer), and headless runs get a structured `unavailable` result instead of a hang or a guess.
|
|
65
|
+
|
|
66
|
+
`Shell` and `Bash` are two registered names for one OS-aware implementation: the shell is resolved per-OS and the tool description names the active OS/shell so the model writes the right syntax (e.g. macOS BSD vs Linux GNU utilities differ).
|
|
59
67
|
|
|
60
68
|
Factory exports (`createBashTool`, `createReadTool`, `createWriteTool`, `createEditTool`) accept an optional `sandboxClient`. The default singleton exports keep host-local behavior.
|
|
61
69
|
|
|
@@ -63,6 +71,8 @@ Factory exports (`createBashTool`, `createReadTool`, `createWriteTool`, `createE
|
|
|
63
71
|
|
|
64
72
|
`ISandboxClient` is the provider-neutral execution-plane port used by sandbox-aware built-in tools:
|
|
65
73
|
|
|
74
|
+
<!-- doc-example-skip: requires the optional e2b dependency -->
|
|
75
|
+
|
|
66
76
|
```typescript
|
|
67
77
|
import { E2BSandboxClient, createBashTool, createReadTool } from '@robota-sdk/agent-tools';
|
|
68
78
|
import { Sandbox } from 'e2b';
|
|
@@ -80,6 +90,8 @@ The package does not depend on E2B directly. `E2BSandboxClient` adapts an E2B-co
|
|
|
80
90
|
|
|
81
91
|
`IWorkspaceManifest` declares the fresh sandbox workspace before a session starts. Paths are workspace-relative and cannot escape the target root.
|
|
82
92
|
|
|
93
|
+
<!-- doc-example-skip: requires the optional e2b dependency -->
|
|
94
|
+
|
|
83
95
|
```typescript
|
|
84
96
|
import { applyWorkspaceManifest, E2BSandboxClient } from '@robota-sdk/agent-tools';
|
|
85
97
|
import { Sandbox } from 'e2b';
|
|
@@ -112,18 +124,17 @@ Recent file tool updates keep write/edit behavior atomic and make Edit tool resu
|
|
|
112
124
|
| `createZodFunctionTool` | Factory with Zod validation and JSON Schema conversion |
|
|
113
125
|
| `OpenAPITool` | Tool generated from OpenAPI specification |
|
|
114
126
|
| `createOpenAPITool` | Factory for creating OpenAPI tools |
|
|
115
|
-
| `
|
|
116
|
-
| `TToolResult` | Result type for built-in CLI tool invocations |
|
|
127
|
+
| `IToolInvocationResult` | Result type for built-in CLI tool invocations |
|
|
117
128
|
| `ISandboxClient` | Provider-neutral sandbox execution port |
|
|
118
129
|
| `IWorkspaceManifest` | Declarative sandbox workspace setup contract |
|
|
119
130
|
| `applyWorkspaceManifest` | Generic manifest applicator for sandbox clients |
|
|
120
131
|
| `E2BSandboxClient` | Adapter for E2B-compatible sandbox instances and snapshots |
|
|
121
132
|
| `InMemorySandboxClient` | Deterministic sandbox client for tests |
|
|
122
133
|
|
|
123
|
-
##
|
|
134
|
+
## IToolInvocationResult Shape
|
|
124
135
|
|
|
125
136
|
```typescript
|
|
126
|
-
interface
|
|
137
|
+
interface IToolInvocationResult {
|
|
127
138
|
success: boolean;
|
|
128
139
|
output: string;
|
|
129
140
|
error?: string;
|
|
@@ -132,7 +143,7 @@ interface TToolResult {
|
|
|
132
143
|
}
|
|
133
144
|
```
|
|
134
145
|
|
|
135
|
-
`
|
|
146
|
+
`IToolInvocationResult` is the inner result type used by built-in tools. It is serialized to JSON and placed inside the `IToolResult.data` field before being returned to the Robota execution loop.
|
|
136
147
|
|
|
137
148
|
## Dependencies
|
|
138
149
|
|
|
@@ -144,4 +155,4 @@ interface TToolResult {
|
|
|
144
155
|
|
|
145
156
|
## License
|
|
146
157
|
|
|
147
|
-
|
|
158
|
+
Robota is dual-licensed under the [GNU AGPL-3.0](../../LICENSE) or a [commercial license](../../COMMERCIAL.md). See [LICENSING.md](../../LICENSING.md).
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { IEventService, IFunctionTool, IParameterValidationResult, ITool, IToolExecutionContext, IToolRegistry, IToolResult, IToolSchema, TToolExecutor, TToolParameters, TUniversalValue } from "@robota-sdk/agent-core";
|
|
2
|
+
import { TypeOf, ZodType } from "zod";
|
|
2
3
|
|
|
3
4
|
//#region src/types/tool-result.d.ts
|
|
4
5
|
/**
|
|
5
6
|
* Result returned by a CLI tool invocation
|
|
6
7
|
*/
|
|
7
|
-
interface
|
|
8
|
+
interface IToolInvocationResult {
|
|
8
9
|
success: boolean;
|
|
9
10
|
output: string;
|
|
10
11
|
error?: string;
|
|
@@ -66,70 +67,6 @@ declare class ToolRegistry implements IToolRegistry {
|
|
|
66
67
|
private validateToolSchema;
|
|
67
68
|
}
|
|
68
69
|
//#endregion
|
|
69
|
-
//#region src/implementations/function-tool/types.d.ts
|
|
70
|
-
/**
|
|
71
|
-
* Zod schema compatibility types
|
|
72
|
-
*
|
|
73
|
-
* Widened to `unknown` so that actual Zod schemas (ZodObject<...>) are structurally
|
|
74
|
-
* assignable without `as unknown as IZodSchema` casts at call sites.
|
|
75
|
-
*/
|
|
76
|
-
interface IZodParseResult {
|
|
77
|
-
success: boolean;
|
|
78
|
-
data?: unknown;
|
|
79
|
-
error?: unknown;
|
|
80
|
-
}
|
|
81
|
-
interface IZodSchemaDef {
|
|
82
|
-
typeName?: string;
|
|
83
|
-
innerType?: IZodSchema;
|
|
84
|
-
valueType?: IZodSchema;
|
|
85
|
-
checks?: Array<{
|
|
86
|
-
kind: string;
|
|
87
|
-
value?: TUniversalValue;
|
|
88
|
-
}>;
|
|
89
|
-
shape?: () => Record<string, IZodSchema>;
|
|
90
|
-
type?: IZodSchema;
|
|
91
|
-
values?: TUniversalValue[];
|
|
92
|
-
description?: string;
|
|
93
|
-
unknownKeys?: 'passthrough' | 'strip' | 'strict';
|
|
94
|
-
}
|
|
95
|
-
interface IZodSchema {
|
|
96
|
-
parse(value: unknown): unknown;
|
|
97
|
-
safeParse(value: unknown): IZodParseResult;
|
|
98
|
-
_def?: IZodSchemaDef;
|
|
99
|
-
}
|
|
100
|
-
/**
|
|
101
|
-
* Parameter type validation options
|
|
102
|
-
*/
|
|
103
|
-
interface IFunctionToolValidationOptions {
|
|
104
|
-
strict?: boolean;
|
|
105
|
-
allowUnknown?: boolean;
|
|
106
|
-
validateTypes?: boolean;
|
|
107
|
-
}
|
|
108
|
-
/**
|
|
109
|
-
* Schema conversion options
|
|
110
|
-
*/
|
|
111
|
-
interface ISchemaConversionOptions {
|
|
112
|
-
includeDescription?: boolean;
|
|
113
|
-
strictTypes?: boolean;
|
|
114
|
-
allowAdditionalProperties?: boolean;
|
|
115
|
-
}
|
|
116
|
-
/**
|
|
117
|
-
* Tool execution metadata
|
|
118
|
-
*/
|
|
119
|
-
interface IFunctionToolExecutionMetadata {
|
|
120
|
-
executionTime: number;
|
|
121
|
-
toolName: string;
|
|
122
|
-
parameters: TToolParameters;
|
|
123
|
-
}
|
|
124
|
-
/**
|
|
125
|
-
* Tool result with metadata
|
|
126
|
-
*/
|
|
127
|
-
interface IFunctionToolResult {
|
|
128
|
-
success: boolean;
|
|
129
|
-
data: TUniversalValue;
|
|
130
|
-
metadata?: IFunctionToolExecutionMetadata;
|
|
131
|
-
}
|
|
132
|
-
//#endregion
|
|
133
70
|
//#region src/implementations/function-tool.d.ts
|
|
134
71
|
/**
|
|
135
72
|
* Function tool implementation
|
|
@@ -181,13 +118,33 @@ declare function createFunctionTool(name: string, description: string, parameter
|
|
|
181
118
|
/**
|
|
182
119
|
* Helper function to create a function tool from Zod schema
|
|
183
120
|
*/
|
|
184
|
-
declare function createZodFunctionTool(name: string, description: string, zodSchema:
|
|
121
|
+
declare function createZodFunctionTool<S extends ZodType>(name: string, description: string, zodSchema: S, fn: TToolExecutor<TypeOf<S>>): FunctionTool;
|
|
185
122
|
//#endregion
|
|
186
|
-
//#region src/implementations/function-tool/
|
|
123
|
+
//#region src/implementations/function-tool/types.d.ts
|
|
187
124
|
/**
|
|
188
|
-
*
|
|
125
|
+
* Parameter type validation options
|
|
189
126
|
*/
|
|
190
|
-
|
|
127
|
+
interface IFunctionToolValidationOptions {
|
|
128
|
+
strict?: boolean;
|
|
129
|
+
allowUnknown?: boolean;
|
|
130
|
+
validateTypes?: boolean;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Tool execution metadata
|
|
134
|
+
*/
|
|
135
|
+
interface IFunctionToolExecutionMetadata {
|
|
136
|
+
executionTime: number;
|
|
137
|
+
toolName: string;
|
|
138
|
+
parameters: TToolParameters;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Tool result with metadata
|
|
142
|
+
*/
|
|
143
|
+
interface IFunctionToolResult {
|
|
144
|
+
success: boolean;
|
|
145
|
+
data: TUniversalValue;
|
|
146
|
+
metadata?: IFunctionToolExecutionMetadata;
|
|
147
|
+
}
|
|
191
148
|
//#endregion
|
|
192
|
-
export { FunctionTool, type IFunctionToolExecutionMetadata, type IFunctionToolResult, type IFunctionToolValidationOptions, type
|
|
149
|
+
export { FunctionTool, type IFunctionToolExecutionMetadata, type IFunctionToolResult, type IFunctionToolValidationOptions, type IToolInvocationResult, ToolRegistry, createFunctionTool, createZodFunctionTool };
|
|
193
150
|
//# sourceMappingURL=browser.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"browser.d.ts","names":[],"sources":["../../src/types/tool-result.ts","../../src/registry/tool-registry.ts","../../src/implementations/function-tool
|
|
1
|
+
{"version":3,"file":"browser.d.ts","names":[],"sources":["../../src/types/tool-result.ts","../../src/registry/tool-registry.ts","../../src/implementations/function-tool.ts","../../src/implementations/function-tool/types.ts"],"mappings":";;;;;;;UAGiB,qBAAA;EACf,OAAA;EACA,MAAA;EACA,KAAA;EACA,QAAA;EAHA;EAKA,SAAA;AAAA;;;;;AANF;;cCOa,YAAA,YAAwB,aAAA;EAAA,QAC3B,KAAA;EDPR;;;ECYA,QAAA,CAAS,IAAA,EAAM,KAAA;EDPf;;AAAS;ECoCT,UAAA,CAAW,IAAA;;;AAnCb;EAgDE,GAAA,CAAI,IAAA,WAAe,KAAA;;;;EAOnB,MAAA,IAAU,KAAA;EAOI;;;EAAd,UAAA,IAAc,WAAA;EA9DkC;;;EAkFhD,GAAA,CAAI,IAAA;EA5EJ;;;EAmFA,KAAA;EAtDW;;;EA+DX,YAAA;EA3CA;;;EAkDA,iBAAA,CAAkB,OAAA,WAAkB,MAAA,GAAS,KAAA;EAvB7C;;;EA+BA,IAAA;EARA;;;EAAA,QAeQ,kBAAA;AAAA;;;;AD/HV;;;;;;cEuBa,YAAA,YAAwB,aAAA;EAAA,SAC1B,MAAA,EAAQ,WAAA;EAAA,SACR,EAAA,EAAI,aAAA;EAAA,QACL,YAAA;cAEI,MAAA,EAAQ,WAAA,EAAa,EAAA,EAAI,aAAA;;;;EASrC,OAAA;ED9BwB;;;;;ECuCxB,eAAA,CAAgB,YAAA,EAAc,aAAA;EDkEM;;;EC3D9B,OAAA,CACJ,UAAA,EAAY,eAAA,EACZ,OAAA,GAAU,qBAAA,GACT,OAAA,CAAQ,WAAA;EDjDqC;;;ECoGhD,QAAA,CAAS,UAAA,EAAY,eAAA;ED9FN;;;EC4Gf,kBAAA,CAAmB,UAAA,EAAY,eAAA,GAAkB,0BAAA;EDlEjD;;;EC8EA,cAAA;EDvEU;;;EAAA,QC8EF,yBAAA;AAAA;;;;iBAkBM,kBAAA,CACd,IAAA,UACA,WAAA,UACA,UAAA,EAAY,WAAA,gBACZ,EAAA,EAAI,aAAA,GACH,YAAA;;;;iBAaa,qBAAA,WAAgC,OAAA,EAC9C,IAAA,UACA,WAAA,UACA,SAAA,EAAW,CAAA,EACX,EAAA,EAAI,aAAA,CAAc,MAAA,CAAO,CAAA,KACxB,YAAA;;;;;;UChLc,8BAAA;EACf,MAAA;EACA,YAAA;EACA,aAAA;AAAA;;;;UAMe,8BAAA;EACf,aAAA;EACA,QAAA;EACA,UAAA,EAAY,eAAe;AAAA;;AFV7B;;UEgBiB,mBAAA;EACf,OAAA;EACA,IAAA,EAAM,eAAA;EACN,QAAA,GAAW,8BAA8B;AAAA"}
|
package/dist/browser/browser.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{ToolExecutionError as e,ValidationError as t,logger as n}from"@robota-sdk/agent-core";var
|
|
1
|
+
import{ToolExecutionError as e,ValidationError as t,logger as n,zodToJsonSchema as r}from"@robota-sdk/agent-core";var i=class{tools=new Map;register(e){if(!e.schema?.name)throw new t(`Tool must have a valid schema with name`);let r=e.schema.name;this.validateToolSchema(e.schema),this.tools.has(r)&&n.warn(`Tool "${r}" is already registered, overriding`,{toolName:r,existingTool:this.tools.get(r)?.constructor.name}),this.tools.set(r,e),n.debug(`Tool "${r}" registered successfully`,{toolName:r,toolType:e.constructor.name,parameters:Object.keys(e.schema.parameters?.properties||{})})}unregister(e){if(!this.tools.has(e)){n.warn(`Attempted to unregister non-existent tool "${e}"`);return}this.tools.delete(e),n.debug(`Tool "${e}" unregistered successfully`)}get(e){return this.tools.get(e)}getAll(){return Array.from(this.tools.values())}getSchemas(){let e=this.getAll();return n.debug(`[TOOL-FLOW] ToolRegistry.getSchemas() - Tools before schema extraction`,{count:e.length,tools:e.map(e=>({name:e.schema?.name??`unnamed`,hasSchema:!!e.schema,schemaType:typeof e.schema,toolType:e.constructor?.name||`unknown`}))}),this.getAll().map(e=>e.schema)}has(e){return this.tools.has(e)}clear(){let e=this.tools.size;this.tools.clear(),n.debug(`Cleared ${e} tools from registry`)}getToolNames(){return Array.from(this.tools.keys())}getToolsByPattern(e){let t=typeof e==`string`?new RegExp(e):e;return this.getAll().filter(e=>t.test(e.schema.name))}size(){return this.tools.size}validateToolSchema(e){if(!e.name||typeof e.name!=`string`)throw new t(`Tool schema must have a valid name`);if(!e.description||typeof e.description!=`string`)throw new t(`Tool schema must have a description`);if(!e.parameters||typeof e.parameters!=`object`||e.parameters===null||Array.isArray(e.parameters))throw new t(`Tool schema must have parameters object`);if(e.parameters.type!==`object`)throw new t(`Tool parameters type must be "object"`);if(e.parameters.properties)for(let n of Object.keys(e.parameters.properties)){let r=e.parameters.properties[n];if(!r?.type)throw new t(`Parameter "${n}" must have a type`);if(![`string`,`number`,`boolean`,`array`,`object`].includes(r.type))throw new t(`Parameter "${n}" has invalid type "${r.type}"`)}if(e.parameters.required){let n=e.parameters.properties||{};for(let r of e.parameters.required)if(!n[r])throw new t(`Required parameter "${r}" is not defined in properties`)}}};function a(e,t,n){switch(n.type){case`string`:if(typeof t!=`string`)return`Parameter "${e}" must be a string, got ${typeof t}`;break;case`number`:if(typeof t!=`number`||isNaN(t))return`Parameter "${e}" must be a number, got ${typeof t}`;break;case`boolean`:if(typeof t!=`boolean`)return`Parameter "${e}" must be a boolean, got ${typeof t}`;break;case`array`:if(!Array.isArray(t))return`Parameter "${e}" must be an array, got ${typeof t}`;if(n.items)for(let r=0;r<t.length;r++){let i=a(`${e}[${r}]`,t[r],n.items);if(i)return i}break;case`object`:if(typeof t!=`object`||!t||Array.isArray(t))return`Parameter "${e}" must be an object, got ${typeof t}`;break}if(n.enum&&n.enum.length>0){let r=n.enum,i=!1;for(let e of r)if(t===e){i=!0;break}if(!i)return`Parameter "${e}" must be one of: ${r.join(`, `)}, got ${t}`}}function o(e,t,n,r){let i=[];for(let n of t)n in e||i.push(`Missing required parameter: ${n}`);for(let[t,o]of Object.entries(e)){let e=n[t];if(!e){if(r===!0)continue;if(r&&typeof r==`object`){let e=a(t,o,r);e&&i.push(e);continue}i.push(`Unknown parameter: ${t}`);continue}let s=a(t,o,e);s&&i.push(s)}return i}function s(e,t,n,r){let i=o(e,t,n,r);return{isValid:i.length===0,errors:i}}var c=class{schema;fn;eventService;constructor(e,t){this.schema=e,this.fn=t,this.validateConstructorInputs()}getName(){return this.schema.name}setEventService(e){this.eventService=e}async execute(n,r){let i=this.schema.name;if(!this.validate(n))throw new t(`Invalid parameters for tool "${i}": ${o(n,this.schema.parameters.required||[],this.schema.parameters.properties||{},this.schema.parameters.additionalProperties).join(`, `)}`);let a=Date.now(),s;try{s=await this.fn(n,r)}catch(a){throw a instanceof e||a instanceof t?a:new e(`Function tool execution failed: ${a instanceof Error?a.message:String(a)}`,i,a instanceof Error?a:Error(String(a)),{parameterCount:Object.keys(n||{}).length,hasContext:!!r})}let c=Date.now()-a;return{success:!0,data:s,metadata:{executionTime:c,toolName:i,parameters:n}}}validate(e){return o(e,this.schema.parameters.required||[],this.schema.parameters.properties||{},this.schema.parameters.additionalProperties).length===0}validateParameters(e){return s(e,this.schema.parameters.required||[],this.schema.parameters.properties||{},this.schema.parameters.additionalProperties)}getDescription(){return this.schema.description}validateConstructorInputs(){if(!this.schema)throw new t(`Tool schema is required`);if(!this.fn||typeof this.fn!=`function`)throw new t(`Tool function is required and must be a function`);if(!this.schema.name)throw new t(`Tool schema must have a name`)}};function l(e,t,n,r){return new c({name:e,description:t,parameters:n},r)}function u(e,n,i,a){return new c({name:e,description:n,parameters:r(i)},async(e,n)=>{let r=i.safeParse(e);if(!r.success)throw new t(`Zod validation failed: ${r.error}`);let o=await a(r.data,n);return typeof o==`string`?o:JSON.stringify(o)})}export{c as FunctionTool,i as ToolRegistry,l as createFunctionTool,u as createZodFunctionTool};
|
|
2
2
|
//# sourceMappingURL=browser.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"browser.js","names":[],"sources":["../../src/registry/tool-registry.ts","../../src/implementations/function-tool/parameter-validator.ts","../../src/implementations/function-tool/schema-converter.ts","../../src/implementations/function-tool.ts"],"sourcesContent":["import { ValidationError } from '@robota-sdk/agent-core';\nimport { logger } from '@robota-sdk/agent-core';\n\nimport type { ITool, IToolRegistry } from '@robota-sdk/agent-core';\nimport type { IToolSchema } from '@robota-sdk/agent-core';\n\n/**\n * Tool registry implementation\n * Manages tool registration, validation, and retrieval\n */\nexport class ToolRegistry implements IToolRegistry {\n private tools = new Map<string, ITool>();\n\n /**\n * Register a tool\n */\n register(tool: ITool): void {\n if (!tool.schema?.name) {\n throw new ValidationError('Tool must have a valid schema with name');\n }\n\n const toolName = tool.schema.name;\n\n // Validate tool schema\n this.validateToolSchema(tool.schema);\n\n // Check for duplicate registration\n if (this.tools.has(toolName)) {\n logger.warn(`Tool \"${toolName}\" is already registered, overriding`, {\n toolName,\n existingTool: this.tools.get(toolName)?.constructor.name,\n });\n }\n\n this.tools.set(toolName, tool);\n logger.debug(`Tool \"${toolName}\" registered successfully`, {\n toolName,\n toolType: tool.constructor.name,\n parameters: Object.keys(tool.schema.parameters?.properties || {}),\n });\n }\n\n /**\n * Unregister a tool\n */\n unregister(name: string): void {\n if (!this.tools.has(name)) {\n logger.warn(`Attempted to unregister non-existent tool \"${name}\"`);\n return;\n }\n\n this.tools.delete(name);\n logger.debug(`Tool \"${name}\" unregistered successfully`);\n }\n\n /**\n * Get tool by name\n */\n get(name: string): ITool | undefined {\n return this.tools.get(name);\n }\n\n /**\n * Get all registered tools\n */\n getAll(): ITool[] {\n return Array.from(this.tools.values());\n }\n\n /**\n * Get tool schemas\n */\n getSchemas(): IToolSchema[] {\n const tools = this.getAll();\n\n // 🔍 [TOOL-FLOW] ToolRegistry.getSchemas() - Extracting schemas from tools\n logger.debug('[TOOL-FLOW] ToolRegistry.getSchemas() - Tools before schema extraction', {\n count: tools.length,\n tools: tools.map((t) => ({\n name: t.schema?.name ?? 'unnamed',\n hasSchema: !!t.schema,\n schemaType: typeof t.schema,\n toolType: t.constructor?.name || 'unknown',\n })),\n });\n\n return this.getAll().map((tool) => tool.schema);\n }\n\n /**\n * Check if tool exists\n */\n has(name: string): boolean {\n return this.tools.has(name);\n }\n\n /**\n * Clear all tools\n */\n clear(): void {\n const toolCount = this.tools.size;\n this.tools.clear();\n logger.debug(`Cleared ${toolCount} tools from registry`);\n }\n\n /**\n * Get tool names\n */\n getToolNames(): string[] {\n return Array.from(this.tools.keys());\n }\n\n /**\n * Get tools by pattern\n */\n getToolsByPattern(pattern: string | RegExp): ITool[] {\n const regex = typeof pattern === 'string' ? new RegExp(pattern) : pattern;\n return this.getAll().filter((tool) => regex.test(tool.schema.name));\n }\n\n /**\n * Get tool count\n */\n size(): number {\n return this.tools.size;\n }\n\n /**\n * Validate tool schema\n */\n private validateToolSchema(schema: IToolSchema): void {\n if (!schema.name || typeof schema.name !== 'string') {\n throw new ValidationError('Tool schema must have a valid name');\n }\n\n if (!schema.description || typeof schema.description !== 'string') {\n throw new ValidationError('Tool schema must have a description');\n }\n\n if (\n !schema.parameters ||\n typeof schema.parameters !== 'object' ||\n schema.parameters === null ||\n Array.isArray(schema.parameters)\n ) {\n throw new ValidationError('Tool schema must have parameters object');\n }\n\n if (schema.parameters.type !== 'object') {\n throw new ValidationError('Tool parameters type must be \"object\"');\n }\n\n // Validate parameter properties\n if (schema.parameters.properties) {\n for (const propName of Object.keys(schema.parameters.properties)) {\n const propSchema = schema.parameters.properties[propName];\n if (!propSchema?.type) {\n throw new ValidationError(`Parameter \"${propName}\" must have a type`);\n }\n\n const validTypes = ['string', 'number', 'boolean', 'array', 'object'];\n if (!validTypes.includes(propSchema.type)) {\n throw new ValidationError(\n `Parameter \"${propName}\" has invalid type \"${propSchema.type}\"`,\n );\n }\n }\n }\n\n // Validate required fields exist in properties\n if (schema.parameters.required) {\n const properties = schema.parameters.properties || {};\n for (const requiredField of schema.parameters.required) {\n if (!properties[requiredField]) {\n throw new ValidationError(\n `Required parameter \"${requiredField}\" is not defined in properties`,\n );\n }\n }\n }\n }\n}\n","import type {\n IParameterSchema,\n TToolParameters,\n IParameterValidationResult,\n} from '@robota-sdk/agent-core';\nimport type { TUniversalValue } from '@robota-sdk/agent-core';\n\n/**\n * Validate individual parameter type against its schema.\n * Returns an error string if invalid, undefined if valid.\n */\nexport function validateParameterType(\n key: string,\n value: TUniversalValue,\n schema: IParameterSchema,\n): string | undefined {\n const expectedType = schema['type'];\n\n switch (expectedType) {\n case 'string':\n if (typeof value !== 'string') {\n return `Parameter \"${key}\" must be a string, got ${typeof value}`;\n }\n break;\n\n case 'number':\n if (typeof value !== 'number' || isNaN(value)) {\n return `Parameter \"${key}\" must be a number, got ${typeof value}`;\n }\n break;\n\n case 'boolean':\n if (typeof value !== 'boolean') {\n return `Parameter \"${key}\" must be a boolean, got ${typeof value}`;\n }\n break;\n\n case 'array':\n if (!Array.isArray(value)) {\n return `Parameter \"${key}\" must be an array, got ${typeof value}`;\n }\n // Check array items if specified\n if (schema.items) {\n for (let i = 0; i < value.length; i++) {\n const itemError = validateParameterType(`${key}[${i}]`, value[i], schema.items);\n if (itemError) {\n return itemError;\n }\n }\n }\n break;\n\n case 'object':\n if (typeof value !== 'object' || value === null || Array.isArray(value)) {\n return `Parameter \"${key}\" must be an object, got ${typeof value}`;\n }\n break;\n }\n\n // Check enum constraints\n if (schema.enum && schema.enum.length > 0) {\n const enumValues = schema.enum;\n let isValidEnum = false;\n\n // Type-safe enum checking based on JSONSchemaEnum type\n for (const enumValue of enumValues) {\n if (value === enumValue) {\n isValidEnum = true;\n break;\n }\n }\n\n if (!isValidEnum) {\n return `Parameter \"${key}\" must be one of: ${enumValues.join(', ')}, got ${value}`;\n }\n }\n\n return undefined;\n}\n\n/**\n * Collect all validation errors for the given parameters against a schema.\n */\nexport function getValidationErrors(\n parameters: TToolParameters,\n schemaRequired: string[],\n schemaProperties: Record<string, IParameterSchema>,\n additionalProperties?: boolean | IParameterSchema,\n): string[] {\n const errors: string[] = [];\n\n // Check required parameters\n for (const field of schemaRequired) {\n if (!(field in parameters)) {\n errors.push(`Missing required parameter: ${field}`);\n }\n }\n\n // Check parameter types and constraints\n for (const [key, value] of Object.entries(parameters)) {\n const paramSchema = schemaProperties[key];\n if (!paramSchema) {\n if (additionalProperties === true) {\n continue;\n }\n if (additionalProperties && typeof additionalProperties === 'object') {\n const additionalTypeError = validateParameterType(key, value, additionalProperties);\n if (additionalTypeError) errors.push(additionalTypeError);\n continue;\n }\n errors.push(`Unknown parameter: ${key}`);\n continue;\n }\n\n const typeError = validateParameterType(key, value, paramSchema);\n if (typeError) {\n errors.push(typeError);\n }\n }\n\n return errors;\n}\n\n/**\n * Validate parameters and return a structured result.\n */\nexport function validateToolParameters(\n parameters: TToolParameters,\n schemaRequired: string[],\n schemaProperties: Record<string, IParameterSchema>,\n additionalProperties?: boolean | IParameterSchema,\n): IParameterValidationResult {\n const errors = getValidationErrors(\n parameters,\n schemaRequired,\n schemaProperties,\n additionalProperties,\n );\n return {\n isValid: errors.length === 0,\n errors,\n };\n}\n","/**\n * FunctionTool - Schema conversion utilities for Facade pattern\n *\n * REASON: Complex Zod to JSON schema conversion requires isolated utility functions\n * ALTERNATIVES_CONSIDERED:\n * 1. Keep conversion logic in main class (violates single responsibility)\n * 2. Use third-party library (adds external dependency)\n * 3. Manual conversion each time (code duplication)\n * 4. Runtime type checking only (loses compile-time safety)\n * 5. Remove Zod support (breaks backward compatibility)\n * TODO: Consider caching conversion results for performance\n */\n\nimport type { IZodSchema, ISchemaConversionOptions } from './types';\nimport type {\n IToolSchema,\n IParameterSchema,\n TJSONSchemaEnum,\n TUniversalValue,\n} from '@robota-sdk/agent-core';\n\n/**\n * Convert Zod schema to JSON Schema format with safe undefined handling\n */\nexport function zodToJsonSchema(\n schema: IZodSchema,\n options: ISchemaConversionOptions = {},\n): IToolSchema['parameters'] {\n const properties: Record<string, IParameterSchema> = {};\n const required: string[] = [];\n\n // Safe access to schema definition (no fallback).\n const schemaDef = schema._def;\n if (!schemaDef) {\n throw new Error('Zod schema is missing _def; cannot convert to JSON schema.');\n }\n\n // Handle object schemas with shape\n if (schemaDef.typeName === 'ZodObject' && schemaDef.shape) {\n // In Zod v3, shape is a property, not a function\n const shape = typeof schemaDef.shape === 'function' ? schemaDef.shape() : schemaDef.shape;\n\n for (const [key, typeObj] of Object.entries(shape)) {\n const property = convertZodTypeToProperty(typeObj);\n properties[key] = property;\n\n // Check if field is required (not optional/nullable)\n if (isRequiredField(typeObj)) {\n required.push(key);\n }\n }\n }\n\n return {\n type: 'object',\n properties,\n required,\n ...((options.allowAdditionalProperties || schemaDef.unknownKeys === 'passthrough') && {\n additionalProperties: true,\n }),\n };\n}\n\n/**\n * Convert individual Zod type to parameter schema with safe undefined handling\n */\nfunction convertZodTypeToProperty(typeObj: IZodSchema): IParameterSchema {\n // Safe access to type definition\n const typeDef = typeObj._def;\n if (!typeDef) {\n throw new Error('Zod type is missing _def; cannot convert to JSON schema.');\n }\n\n const base: Partial<IParameterSchema> = {};\n\n // Add description if available\n if (typeDef.description) {\n base.description = typeDef.description;\n }\n\n // Handle different Zod types\n switch (typeDef.typeName) {\n case 'ZodString':\n return { type: 'string', ...base };\n\n case 'ZodNumber':\n return { type: 'number', ...base };\n\n case 'ZodBoolean':\n return { type: 'boolean', ...base };\n\n case 'ZodArray': {\n if (!typeDef.type) {\n throw new Error('ZodArray is missing item type; cannot convert to JSON schema.');\n }\n const arrayItems = convertZodTypeToProperty(typeDef.type);\n return {\n type: 'array',\n items: arrayItems,\n ...base,\n };\n }\n\n case 'ZodObject':\n return { type: 'object', ...base };\n\n case 'ZodEnum': {\n const enumValues = typeDef.values;\n if (!enumValues || !Array.isArray(enumValues)) {\n throw new Error('ZodEnum is missing enum values; cannot convert to JSON schema.');\n }\n return {\n type: 'string',\n enum: enumValues as TJSONSchemaEnum,\n ...base,\n };\n }\n\n case 'ZodOptional':\n // Handle optional types by recursion\n if (typeDef.innerType) {\n const innerProperty = convertZodTypeToProperty(typeDef.innerType);\n return { ...innerProperty, ...base };\n }\n throw new Error('ZodOptional is missing innerType; cannot convert to JSON schema.');\n\n case 'ZodNullable':\n // Handle nullable types\n if (typeDef.innerType) {\n const innerProperty = convertZodTypeToProperty(typeDef.innerType);\n return { ...innerProperty, ...base };\n }\n throw new Error('ZodNullable is missing innerType; cannot convert to JSON schema.');\n\n case 'ZodDefault':\n // Handle default values by processing the inner type\n if (typeDef.innerType) {\n const innerProperty = convertZodTypeToProperty(typeDef.innerType);\n return { ...innerProperty, ...base };\n }\n throw new Error('ZodDefault is missing innerType; cannot convert to JSON schema.');\n\n case 'ZodRecord':\n // Handle Record<string, T> → JSON Schema additionalProperties\n if (typeDef.valueType) {\n const valueProperty = convertZodTypeToProperty(typeDef.valueType);\n return { type: 'object', additionalProperties: valueProperty, ...base };\n }\n return { type: 'object', additionalProperties: { type: 'string' }, ...base };\n\n default:\n throw new Error(`Unsupported Zod type: ${String(typeDef.typeName)}`);\n }\n}\n\n/**\n * Check if a Zod field is required (not optional or nullable)\n */\nfunction isRequiredField(typeObj: IZodSchema): boolean {\n const typeDef = typeObj._def;\n if (!typeDef) {\n throw new Error('Zod schema is missing _def; cannot determine required fields.');\n }\n\n // Field is optional if it's ZodOptional, ZodNullable, or ZodDefault\n return (\n typeDef.typeName !== 'ZodOptional' &&\n typeDef.typeName !== 'ZodNullable' &&\n typeDef.typeName !== 'ZodDefault'\n );\n}\n\n/**\n * Safely extract enum values from Zod schema\n */\nexport function extractEnumValues(schema: IZodSchema): TUniversalValue[] {\n const typeDef = schema._def;\n if (!typeDef) {\n throw new Error('Zod schema is missing _def; cannot extract enum values.');\n }\n if (!typeDef.values || !Array.isArray(typeDef.values)) {\n throw new Error('ZodEnum schema is missing enum values; cannot extract enum values.');\n }\n return typeDef.values;\n}\n\n/**\n * Check if schema has validation constraints\n */\nexport function hasValidationConstraints(schema: IZodSchema): boolean {\n const typeDef = schema._def;\n if (!typeDef) {\n throw new Error('Zod schema is missing _def; cannot determine validation constraints.');\n }\n\n return !!(typeDef.checks && typeDef.checks.length > 0);\n}\n\n/**\n * Safe schema type name extraction\n */\nexport function getSchemaTypeName(schema: IZodSchema): string {\n const typeDef = schema._def;\n if (!typeDef) {\n throw new Error('Zod schema is missing _def; cannot determine schema type name.');\n }\n if (!typeDef.typeName) {\n throw new Error('Zod schema has empty typeName; cannot determine schema type name.');\n }\n return typeDef.typeName;\n}\n","import { ToolExecutionError, ValidationError } from '@robota-sdk/agent-core';\n\nimport { getValidationErrors, validateToolParameters } from './function-tool/parameter-validator';\nimport { zodToJsonSchema } from './function-tool/schema-converter';\n\nimport type { IZodSchema } from './function-tool/types';\nimport type {\n IFunctionTool,\n IToolResult,\n IToolExecutionContext,\n IParameterValidationResult,\n TToolExecutor,\n TToolParameters,\n IEventService,\n} from '@robota-sdk/agent-core';\nimport type { IToolSchema } from '@robota-sdk/agent-core';\nimport type { TUniversalValue } from '@robota-sdk/agent-core';\n\n// Import from Facade pattern modules for type safety\n\n/**\n * Function tool implementation\n * Wraps a JavaScript function as a tool with schema validation\n *\n * Implements IFunctionTool without extending AbstractTool to avoid\n * circular runtime dependency (tools → agents → tools).\n */\nexport class FunctionTool implements IFunctionTool {\n readonly schema: IToolSchema;\n readonly fn: TToolExecutor;\n private eventService: IEventService | undefined;\n\n constructor(schema: IToolSchema, fn: TToolExecutor) {\n this.schema = schema;\n this.fn = fn;\n this.validateConstructorInputs();\n }\n\n /**\n * Get tool name\n */\n getName(): string {\n return this.schema.name;\n }\n\n /**\n * Set EventService for post-construction injection.\n * Accepts EventService as-is without transformation.\n * Caller is responsible for providing properly configured EventService.\n */\n setEventService(eventService: IEventService | undefined): void {\n this.eventService = eventService;\n }\n\n /**\n * Execute the function tool\n */\n async execute(\n parameters: TToolParameters,\n context?: IToolExecutionContext,\n ): Promise<IToolResult> {\n const toolName = this.schema.name;\n\n // Validate parameters before execution\n if (!this.validate(parameters)) {\n const errors = getValidationErrors(\n parameters,\n this.schema.parameters.required || [],\n this.schema.parameters.properties || {},\n this.schema.parameters.additionalProperties,\n );\n throw new ValidationError(`Invalid parameters for tool \"${toolName}\": ${errors.join(', ')}`);\n }\n\n // Execute the function\n const startTime = Date.now();\n let result: TUniversalValue;\n try {\n result = await this.fn(parameters, context);\n } catch (error) {\n if (error instanceof ToolExecutionError || error instanceof ValidationError) {\n throw error;\n }\n\n throw new ToolExecutionError(\n `Function tool execution failed: ${error instanceof Error ? error.message : String(error)}`,\n toolName,\n error instanceof Error ? error : new Error(String(error)),\n {\n parameterCount: Object.keys(parameters || {}).length,\n hasContext: !!context,\n },\n );\n }\n\n const executionTime = Date.now() - startTime;\n\n return {\n success: true,\n data: result,\n metadata: {\n executionTime,\n toolName,\n parameters,\n },\n };\n }\n\n /**\n * Validate parameters (simple boolean result)\n */\n validate(parameters: TToolParameters): boolean {\n return (\n getValidationErrors(\n parameters,\n this.schema.parameters.required || [],\n this.schema.parameters.properties || {},\n this.schema.parameters.additionalProperties,\n ).length === 0\n );\n }\n\n /**\n * Validate tool parameters with detailed result\n */\n validateParameters(parameters: TToolParameters): IParameterValidationResult {\n return validateToolParameters(\n parameters,\n this.schema.parameters.required || [],\n this.schema.parameters.properties || {},\n this.schema.parameters.additionalProperties,\n );\n }\n\n /**\n * Get tool description\n */\n getDescription(): string {\n return this.schema.description;\n }\n\n /**\n * Validate constructor inputs\n */\n private validateConstructorInputs(): void {\n if (!this.schema) {\n throw new ValidationError('Tool schema is required');\n }\n\n if (!this.fn || typeof this.fn !== 'function') {\n throw new ValidationError('Tool function is required and must be a function');\n }\n\n if (!this.schema.name) {\n throw new ValidationError('Tool schema must have a name');\n }\n }\n}\n\n/**\n * Helper function to create a function tool from a simple function\n */\nexport function createFunctionTool(\n name: string,\n description: string,\n parameters: IToolSchema['parameters'],\n fn: TToolExecutor,\n): FunctionTool {\n const schema: IToolSchema = {\n name,\n description,\n parameters,\n };\n\n return new FunctionTool(schema, fn);\n}\n\n/**\n * Helper function to create a function tool from Zod schema\n */\nexport function createZodFunctionTool(\n name: string,\n description: string,\n zodSchema: IZodSchema,\n fn: TToolExecutor,\n): FunctionTool {\n // Use comprehensive Zod to JSON schema conversion\n const parameters = zodToJsonSchema(zodSchema);\n\n const schema: IToolSchema = {\n name,\n description,\n parameters,\n };\n\n // Wrap the function with validation and ensure proper parameter handling\n const wrappedFn: TToolExecutor = async (\n parameters: TToolParameters,\n context?: IToolExecutionContext,\n ): Promise<TUniversalValue> => {\n // Use Zod for runtime validation\n const parseResult = zodSchema.safeParse(parameters);\n if (!parseResult.success) {\n throw new ValidationError(`Zod validation failed: ${parseResult.error}`);\n }\n\n const result = await fn((parseResult.data as TToolParameters) || parameters, context);\n // Ensure result is always a string for consistency with core package\n return typeof result === 'string' ? result : JSON.stringify(result);\n };\n\n return new FunctionTool(schema, wrappedFn);\n}\n\n// zodToJsonSchema function moved to Facade pattern schema-converter module\n"],"mappings":"6FAUA,IAAa,EAAb,KAAmD,CACjD,MAAgB,IAAI,IAKpB,SAAS,EAAmB,CAC1B,GAAI,CAAC,EAAK,QAAQ,KAChB,MAAM,IAAI,EAAgB,yCAAyC,EAGrE,IAAM,EAAW,EAAK,OAAO,KAG7B,KAAK,mBAAmB,EAAK,MAAM,EAG/B,KAAK,MAAM,IAAI,C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KAAK,IAAM,OAAO,KAAK,IAAO,WACjC,MAAM,IAAI,EAAgB,kDAAkD,EAG9E,GAAI,CAAC,KAAK,OAAO,KACf,MAAM,IAAI,EAAgB,8BAA8B,CAE5D,CACF,EAKA,SAAgB,EACd,EACA,EACA,EACA,EACc,CAOd,OAAO,IAAI,EAAa,CALtB,OACA,cACA,YAG2B,EAAG,CAAE,CACpC,CAKA,SAAgB,EACd,EACA,EACA,EACA,EACc,CA0Bd,OAAO,IAAI,EAAa,CArBtB,OACA,cACA,WALiB,EAAgB,CAKxB,CAmBa,EAAQ,MAd9B,EACA,IAC6B,CAE7B,IAAM,EAAc,EAAU,UAAU,CAAU,EAClD,GAAI,CAAC,EAAY,QACf,MAAM,IAAI,EAAgB,0BAA0B,EAAY,OAAO,EAGzE,IAAM,EAAS,MAAM,EAAI,EAAY,MAA4B,EAAY,CAAO,EAEpF,OAAO,OAAO,GAAW,SAAW,EAAS,KAAK,UAAU,CAAM,CACpE,CAEyC,CAC3C"}
|
|
1
|
+
{"version":3,"file":"browser.js","names":[],"sources":["../../src/registry/tool-registry.ts","../../src/implementations/function-tool/parameter-validator.ts","../../src/implementations/function-tool.ts"],"sourcesContent":["import { ValidationError } from '@robota-sdk/agent-core';\nimport { logger } from '@robota-sdk/agent-core';\n\nimport type { ITool, IToolRegistry } from '@robota-sdk/agent-core';\nimport type { IToolSchema } from '@robota-sdk/agent-core';\n\n/**\n * Tool registry implementation\n * Manages tool registration, validation, and retrieval\n */\nexport class ToolRegistry implements IToolRegistry {\n private tools = new Map<string, ITool>();\n\n /**\n * Register a tool\n */\n register(tool: ITool): void {\n if (!tool.schema?.name) {\n throw new ValidationError('Tool must have a valid schema with name');\n }\n\n const toolName = tool.schema.name;\n\n // Validate tool schema\n this.validateToolSchema(tool.schema);\n\n // Check for duplicate registration\n if (this.tools.has(toolName)) {\n logger.warn(`Tool \"${toolName}\" is already registered, overriding`, {\n toolName,\n existingTool: this.tools.get(toolName)?.constructor.name,\n });\n }\n\n this.tools.set(toolName, tool);\n logger.debug(`Tool \"${toolName}\" registered successfully`, {\n toolName,\n toolType: tool.constructor.name,\n parameters: Object.keys(tool.schema.parameters?.properties || {}),\n });\n }\n\n /**\n * Unregister a tool\n */\n unregister(name: string): void {\n if (!this.tools.has(name)) {\n logger.warn(`Attempted to unregister non-existent tool \"${name}\"`);\n return;\n }\n\n this.tools.delete(name);\n logger.debug(`Tool \"${name}\" unregistered successfully`);\n }\n\n /**\n * Get tool by name\n */\n get(name: string): ITool | undefined {\n return this.tools.get(name);\n }\n\n /**\n * Get all registered tools\n */\n getAll(): ITool[] {\n return Array.from(this.tools.values());\n }\n\n /**\n * Get tool schemas\n */\n getSchemas(): IToolSchema[] {\n const tools = this.getAll();\n\n // 🔍 [TOOL-FLOW] ToolRegistry.getSchemas() - Extracting schemas from tools\n logger.debug('[TOOL-FLOW] ToolRegistry.getSchemas() - Tools before schema extraction', {\n count: tools.length,\n tools: tools.map((t) => ({\n name: t.schema?.name ?? 'unnamed',\n hasSchema: !!t.schema,\n schemaType: typeof t.schema,\n toolType: t.constructor?.name || 'unknown',\n })),\n });\n\n return this.getAll().map((tool) => tool.schema);\n }\n\n /**\n * Check if tool exists\n */\n has(name: string): boolean {\n return this.tools.has(name);\n }\n\n /**\n * Clear all tools\n */\n clear(): void {\n const toolCount = this.tools.size;\n this.tools.clear();\n logger.debug(`Cleared ${toolCount} tools from registry`);\n }\n\n /**\n * Get tool names\n */\n getToolNames(): string[] {\n return Array.from(this.tools.keys());\n }\n\n /**\n * Get tools by pattern\n */\n getToolsByPattern(pattern: string | RegExp): ITool[] {\n const regex = typeof pattern === 'string' ? new RegExp(pattern) : pattern;\n return this.getAll().filter((tool) => regex.test(tool.schema.name));\n }\n\n /**\n * Get tool count\n */\n size(): number {\n return this.tools.size;\n }\n\n /**\n * Validate tool schema\n */\n private validateToolSchema(schema: IToolSchema): void {\n if (!schema.name || typeof schema.name !== 'string') {\n throw new ValidationError('Tool schema must have a valid name');\n }\n\n if (!schema.description || typeof schema.description !== 'string') {\n throw new ValidationError('Tool schema must have a description');\n }\n\n if (\n !schema.parameters ||\n typeof schema.parameters !== 'object' ||\n schema.parameters === null ||\n Array.isArray(schema.parameters)\n ) {\n throw new ValidationError('Tool schema must have parameters object');\n }\n\n if (schema.parameters.type !== 'object') {\n throw new ValidationError('Tool parameters type must be \"object\"');\n }\n\n // Validate parameter properties\n if (schema.parameters.properties) {\n for (const propName of Object.keys(schema.parameters.properties)) {\n const propSchema = schema.parameters.properties[propName];\n if (!propSchema?.type) {\n throw new ValidationError(`Parameter \"${propName}\" must have a type`);\n }\n\n const validTypes = ['string', 'number', 'boolean', 'array', 'object'];\n if (!validTypes.includes(propSchema.type)) {\n throw new ValidationError(\n `Parameter \"${propName}\" has invalid type \"${propSchema.type}\"`,\n );\n }\n }\n }\n\n // Validate required fields exist in properties\n if (schema.parameters.required) {\n const properties = schema.parameters.properties || {};\n for (const requiredField of schema.parameters.required) {\n if (!properties[requiredField]) {\n throw new ValidationError(\n `Required parameter \"${requiredField}\" is not defined in properties`,\n );\n }\n }\n }\n }\n}\n","import type {\n IParameterSchema,\n TToolParameters,\n IParameterValidationResult,\n} from '@robota-sdk/agent-core';\nimport type { TUniversalValue } from '@robota-sdk/agent-core';\n\n/**\n * Validate individual parameter type against its schema.\n * Returns an error string if invalid, undefined if valid.\n */\nexport function validateParameterType(\n key: string,\n value: TUniversalValue,\n schema: IParameterSchema,\n): string | undefined {\n const expectedType = schema['type'];\n\n switch (expectedType) {\n case 'string':\n if (typeof value !== 'string') {\n return `Parameter \"${key}\" must be a string, got ${typeof value}`;\n }\n break;\n\n case 'number':\n if (typeof value !== 'number' || isNaN(value)) {\n return `Parameter \"${key}\" must be a number, got ${typeof value}`;\n }\n break;\n\n case 'boolean':\n if (typeof value !== 'boolean') {\n return `Parameter \"${key}\" must be a boolean, got ${typeof value}`;\n }\n break;\n\n case 'array':\n if (!Array.isArray(value)) {\n return `Parameter \"${key}\" must be an array, got ${typeof value}`;\n }\n // Check array items if specified\n if (schema.items) {\n for (let i = 0; i < value.length; i++) {\n const itemError = validateParameterType(`${key}[${i}]`, value[i], schema.items);\n if (itemError) {\n return itemError;\n }\n }\n }\n break;\n\n case 'object':\n if (typeof value !== 'object' || value === null || Array.isArray(value)) {\n return `Parameter \"${key}\" must be an object, got ${typeof value}`;\n }\n break;\n }\n\n // Check enum constraints\n if (schema.enum && schema.enum.length > 0) {\n const enumValues = schema.enum;\n let isValidEnum = false;\n\n // Type-safe enum checking based on JSONSchemaEnum type\n for (const enumValue of enumValues) {\n if (value === enumValue) {\n isValidEnum = true;\n break;\n }\n }\n\n if (!isValidEnum) {\n return `Parameter \"${key}\" must be one of: ${enumValues.join(', ')}, got ${value}`;\n }\n }\n\n return undefined;\n}\n\n/**\n * Collect all validation errors for the given parameters against a schema.\n */\nexport function getValidationErrors(\n parameters: TToolParameters,\n schemaRequired: string[],\n schemaProperties: Record<string, IParameterSchema>,\n additionalProperties?: boolean | IParameterSchema,\n): string[] {\n const errors: string[] = [];\n\n // Check required parameters\n for (const field of schemaRequired) {\n if (!(field in parameters)) {\n errors.push(`Missing required parameter: ${field}`);\n }\n }\n\n // Check parameter types and constraints\n for (const [key, value] of Object.entries(parameters)) {\n const paramSchema = schemaProperties[key];\n if (!paramSchema) {\n if (additionalProperties === true) {\n continue;\n }\n if (additionalProperties && typeof additionalProperties === 'object') {\n const additionalTypeError = validateParameterType(key, value, additionalProperties);\n if (additionalTypeError) errors.push(additionalTypeError);\n continue;\n }\n errors.push(`Unknown parameter: ${key}`);\n continue;\n }\n\n const typeError = validateParameterType(key, value, paramSchema);\n if (typeError) {\n errors.push(typeError);\n }\n }\n\n return errors;\n}\n\n/**\n * Validate parameters and return a structured result.\n */\nexport function validateToolParameters(\n parameters: TToolParameters,\n schemaRequired: string[],\n schemaProperties: Record<string, IParameterSchema>,\n additionalProperties?: boolean | IParameterSchema,\n): IParameterValidationResult {\n const errors = getValidationErrors(\n parameters,\n schemaRequired,\n schemaProperties,\n additionalProperties,\n );\n return {\n isValid: errors.length === 0,\n errors,\n };\n}\n","import { ToolExecutionError, ValidationError, zodToJsonSchema } from '@robota-sdk/agent-core';\n\nimport { getValidationErrors, validateToolParameters } from './function-tool/parameter-validator';\n\nimport type {\n IFunctionTool,\n IToolResult,\n IToolExecutionContext,\n IParameterValidationResult,\n TToolExecutor,\n TToolParameters,\n IEventService,\n} from '@robota-sdk/agent-core';\nimport type { IToolSchema } from '@robota-sdk/agent-core';\nimport type { TUniversalValue } from '@robota-sdk/agent-core';\nimport type { TypeOf, ZodType } from 'zod';\n\n// Import from Facade pattern modules for type safety\n\n/**\n * Function tool implementation\n * Wraps a JavaScript function as a tool with schema validation\n *\n * Implements IFunctionTool without extending AbstractTool to avoid\n * circular runtime dependency (tools → agents → tools).\n */\nexport class FunctionTool implements IFunctionTool {\n readonly schema: IToolSchema;\n readonly fn: TToolExecutor;\n private eventService: IEventService | undefined;\n\n constructor(schema: IToolSchema, fn: TToolExecutor) {\n this.schema = schema;\n this.fn = fn;\n this.validateConstructorInputs();\n }\n\n /**\n * Get tool name\n */\n getName(): string {\n return this.schema.name;\n }\n\n /**\n * Set EventService for post-construction injection.\n * Accepts EventService as-is without transformation.\n * Caller is responsible for providing properly configured EventService.\n */\n setEventService(eventService: IEventService | undefined): void {\n this.eventService = eventService;\n }\n\n /**\n * Execute the function tool\n */\n async execute(\n parameters: TToolParameters,\n context?: IToolExecutionContext,\n ): Promise<IToolResult> {\n const toolName = this.schema.name;\n\n // Validate parameters before execution\n if (!this.validate(parameters)) {\n const errors = getValidationErrors(\n parameters,\n this.schema.parameters.required || [],\n this.schema.parameters.properties || {},\n this.schema.parameters.additionalProperties,\n );\n throw new ValidationError(`Invalid parameters for tool \"${toolName}\": ${errors.join(', ')}`);\n }\n\n // Execute the function\n const startTime = Date.now();\n let result: TUniversalValue;\n try {\n result = await this.fn(parameters, context);\n } catch (error) {\n if (error instanceof ToolExecutionError || error instanceof ValidationError) {\n throw error;\n }\n\n throw new ToolExecutionError(\n `Function tool execution failed: ${error instanceof Error ? error.message : String(error)}`,\n toolName,\n error instanceof Error ? error : new Error(String(error)),\n {\n parameterCount: Object.keys(parameters || {}).length,\n hasContext: !!context,\n },\n );\n }\n\n const executionTime = Date.now() - startTime;\n\n return {\n success: true,\n data: result,\n metadata: {\n executionTime,\n toolName,\n parameters,\n },\n };\n }\n\n /**\n * Validate parameters (simple boolean result)\n */\n validate(parameters: TToolParameters): boolean {\n return (\n getValidationErrors(\n parameters,\n this.schema.parameters.required || [],\n this.schema.parameters.properties || {},\n this.schema.parameters.additionalProperties,\n ).length === 0\n );\n }\n\n /**\n * Validate tool parameters with detailed result\n */\n validateParameters(parameters: TToolParameters): IParameterValidationResult {\n return validateToolParameters(\n parameters,\n this.schema.parameters.required || [],\n this.schema.parameters.properties || {},\n this.schema.parameters.additionalProperties,\n );\n }\n\n /**\n * Get tool description\n */\n getDescription(): string {\n return this.schema.description;\n }\n\n /**\n * Validate constructor inputs\n */\n private validateConstructorInputs(): void {\n if (!this.schema) {\n throw new ValidationError('Tool schema is required');\n }\n\n if (!this.fn || typeof this.fn !== 'function') {\n throw new ValidationError('Tool function is required and must be a function');\n }\n\n if (!this.schema.name) {\n throw new ValidationError('Tool schema must have a name');\n }\n }\n}\n\n/**\n * Helper function to create a function tool from a simple function\n */\nexport function createFunctionTool(\n name: string,\n description: string,\n parameters: IToolSchema['parameters'],\n fn: TToolExecutor,\n): FunctionTool {\n const schema: IToolSchema = {\n name,\n description,\n parameters,\n };\n\n return new FunctionTool(schema, fn);\n}\n\n/**\n * Helper function to create a function tool from Zod schema\n */\nexport function createZodFunctionTool<S extends ZodType>(\n name: string,\n description: string,\n zodSchema: S,\n fn: TToolExecutor<TypeOf<S>>,\n): FunctionTool {\n // Use comprehensive Zod to JSON schema conversion\n const parameters = zodToJsonSchema(zodSchema);\n\n const schema: IToolSchema = {\n name,\n description,\n parameters,\n };\n\n // Wrap the function with validation and ensure proper parameter handling\n const wrappedFn: TToolExecutor = async (\n parameters: TToolParameters,\n context?: IToolExecutionContext,\n ): Promise<TUniversalValue> => {\n // Use Zod for runtime validation — the executor receives the PARSED, schema-typed value\n // (SDK-009): the runtime guarantee and the compile-time type now flow together.\n const parseResult = zodSchema.safeParse(parameters);\n if (!parseResult.success) {\n throw new ValidationError(`Zod validation failed: ${parseResult.error}`);\n }\n\n const result = await fn(parseResult.data as TypeOf<S>, context);\n // Ensure result is always a string for consistency with core package\n return typeof result === 'string' ? result : JSON.stringify(result);\n };\n\n return new FunctionTool(schema, wrappedFn);\n}\n\n// zodToJsonSchema function moved to Facade pattern schema-converter module\n"],"mappings":"kHAUA,IAAa,EAAb,KAAmD,CACjD,MAAgB,IAAI,IAKpB,SAAS,EAAmB,CAC1B,GAAI,CAAC,EAAK,QAAQ,KAChB,MAAM,IAAI,EAAgB,yCAAyC,EAGrE,IAAM,EAAW,EAAK,OAAO,KAG7B,KAAK,mBAAmB,EAAK,MAAM,EAG/B,KAAK,MAAM,IAAI,CAAQ,GACzB,EAAO,KAAK,SAAS,EAAS,qCAAsC,CAClE,WACA,aAAc,KAAK,MAAM,IAAI,CAAQ,CAAC,EAAE,YAAY,IACtD,CAAC,EAGH,KAAK,MAAM,IAAI,EAAU,CAAI,EAC7B,EAAO,MAAM,SAAS,EAAS,2BAA4B,CACzD,WACA,SAAU,EAAK,YAAY,KAC3B,WAAY,OAAO,KAAK,EAAK,OAAO,YAAY,YAAc,CAAC,CAAC,CAClE,CAAC,CACH,CAKA,WAAW,EAAoB,CAC7B,GAAI,CAAC,KAAK,MAAM,IAAI,CAAI,EAAG,CACzB,EAAO,KAAK,8CAA8C,EAAK,EAAE,EACjE,MACF,CAEA,KAAK,MAAM,OAAO,CAAI,EACtB,EAAO,MAAM,SAAS,EAAK,4BAA4B,CACzD,CAKA,IAAI,EAAiC,CACnC,OAAO,KAAK,MAAM,IAAI,CAAI,CAC5B,CAKA,QAAkB,CAChB,OAAO,MAAM,KAAK,KAAK,MAAM,OAAO,CAAC,CAC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