@agenticforge/tools 1.0.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/dist/cjs/index.cjs +341 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/esm/index.js +329 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/types/AsyncToolExecutor.d.ts +30 -0
- package/dist/types/AsyncToolExecutor.d.ts.map +1 -0
- package/dist/types/Tool.d.ts +74 -0
- package/dist/types/Tool.d.ts.map +1 -0
- package/dist/types/ToolChain.d.ts +47 -0
- package/dist/types/ToolChain.d.ts.map +1 -0
- package/dist/types/ToolRegistry.d.ts +22 -0
- package/dist/types/ToolRegistry.d.ts.map +1 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/types.d.ts +13 -0
- package/dist/types/types.d.ts.map +1 -0
- package/package.json +43 -0
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
require('reflect-metadata');
|
|
4
|
+
var zod = require('zod');
|
|
5
|
+
|
|
6
|
+
var __defProp$3 = Object.defineProperty;
|
|
7
|
+
var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __publicField$3 = (obj, key, value) => __defNormalProp$3(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
9
|
+
const TOOL_ACTIONS_META_KEY = /* @__PURE__ */ Symbol("tool:actions");
|
|
10
|
+
function toolAction(key, description) {
|
|
11
|
+
return function(target, propertyKey, _descriptor) {
|
|
12
|
+
const existing = Reflect.getMetadata(TOOL_ACTIONS_META_KEY, target) ?? [];
|
|
13
|
+
existing.push({ key, description, method: propertyKey });
|
|
14
|
+
Reflect.defineMetadata(TOOL_ACTIONS_META_KEY, existing, target);
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
class Tool {
|
|
18
|
+
constructor(name, description, expandable = false) {
|
|
19
|
+
__publicField$3(this, "name");
|
|
20
|
+
__publicField$3(this, "description");
|
|
21
|
+
__publicField$3(this, "expandable");
|
|
22
|
+
this.name = name;
|
|
23
|
+
this.description = description;
|
|
24
|
+
this.expandable = expandable;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Validate that required parameters are present and have basic types.
|
|
28
|
+
* Returns true if valid.
|
|
29
|
+
*/
|
|
30
|
+
validateParameters(parameters) {
|
|
31
|
+
const params = this.getParameters();
|
|
32
|
+
for (const p of params) {
|
|
33
|
+
if (p.required && (parameters[p.name] === void 0 || parameters[p.name] === null)) {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Validate and coerce parameters, returning a Result object.
|
|
41
|
+
*/
|
|
42
|
+
validateAndNormalizeParameters(parameters) {
|
|
43
|
+
const params = this.getParameters();
|
|
44
|
+
const data = {};
|
|
45
|
+
for (const p of params) {
|
|
46
|
+
const val = parameters[p.name];
|
|
47
|
+
if (val === void 0 || val === null) {
|
|
48
|
+
if (p.required) {
|
|
49
|
+
return { success: false, error: `Missing required parameter: ${p.name}` };
|
|
50
|
+
}
|
|
51
|
+
data[p.name] = p.default;
|
|
52
|
+
} else {
|
|
53
|
+
data[p.name] = val;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
for (const [key, val] of Object.entries(parameters)) {
|
|
57
|
+
if (!(key in data)) data[key] = val;
|
|
58
|
+
}
|
|
59
|
+
return { success: true, data };
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Convert this Tool to an OpenAI function-calling schema.
|
|
63
|
+
*/
|
|
64
|
+
toOpenAISchema() {
|
|
65
|
+
const params = this.getParameters();
|
|
66
|
+
const properties = {};
|
|
67
|
+
const required = [];
|
|
68
|
+
for (const p of params) {
|
|
69
|
+
properties[p.name] = {
|
|
70
|
+
type: mapType(p.type),
|
|
71
|
+
description: p.description,
|
|
72
|
+
...p.default !== null && p.default !== void 0 ? { default: p.default } : {}
|
|
73
|
+
};
|
|
74
|
+
if (p.required) required.push(p.name);
|
|
75
|
+
}
|
|
76
|
+
return {
|
|
77
|
+
type: "function",
|
|
78
|
+
function: {
|
|
79
|
+
name: this.name,
|
|
80
|
+
description: this.description,
|
|
81
|
+
parameters: {
|
|
82
|
+
type: "object",
|
|
83
|
+
properties,
|
|
84
|
+
...required.length > 0 ? { required } : {}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Return a formatted description of all available tool actions (for system prompt).
|
|
91
|
+
*/
|
|
92
|
+
describe() {
|
|
93
|
+
const params = this.getParameters();
|
|
94
|
+
const paramStr = params.map(
|
|
95
|
+
(p) => ` - ${p.name} (${p.type}${p.required ? ", required" : ""}): ${p.description}`
|
|
96
|
+
).join("\n");
|
|
97
|
+
return `Tool: ${this.name}
|
|
98
|
+
Description: ${this.description}
|
|
99
|
+
Parameters:
|
|
100
|
+
${paramStr}`;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
function mapType(type) {
|
|
104
|
+
const t = (type ?? "string").toLowerCase();
|
|
105
|
+
if (["string", "number", "integer", "boolean", "array", "object"].includes(t)) {
|
|
106
|
+
return t;
|
|
107
|
+
}
|
|
108
|
+
return "string";
|
|
109
|
+
}
|
|
110
|
+
function defineFunctionTool(options) {
|
|
111
|
+
return options;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
var __defProp$2 = Object.defineProperty;
|
|
115
|
+
var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
116
|
+
var __publicField$2 = (obj, key, value) => __defNormalProp$2(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
117
|
+
class ToolChain {
|
|
118
|
+
constructor(name, description) {
|
|
119
|
+
__publicField$2(this, "name");
|
|
120
|
+
__publicField$2(this, "description");
|
|
121
|
+
__publicField$2(this, "steps", []);
|
|
122
|
+
this.name = name;
|
|
123
|
+
this.description = description;
|
|
124
|
+
}
|
|
125
|
+
addStep(toolName, inputTemplate, outputKey) {
|
|
126
|
+
this.steps.push({ toolName, inputTemplate, outputKey });
|
|
127
|
+
return this;
|
|
128
|
+
}
|
|
129
|
+
getSteps() {
|
|
130
|
+
return [...this.steps];
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Execute the chain against a registry.
|
|
134
|
+
* @param registry ToolRegistry that holds the referenced tools.
|
|
135
|
+
* @param input Initial `{input}` value.
|
|
136
|
+
* @returns The value stored under the last step's outputKey.
|
|
137
|
+
*/
|
|
138
|
+
async execute(registry, input) {
|
|
139
|
+
const context = { input };
|
|
140
|
+
for (const step of this.steps) {
|
|
141
|
+
const resolvedInput = interpolate(step.inputTemplate, context);
|
|
142
|
+
const result = await registry.execute(step.toolName, { input: resolvedInput });
|
|
143
|
+
context[step.outputKey] = result;
|
|
144
|
+
}
|
|
145
|
+
const lastStep = this.steps[this.steps.length - 1];
|
|
146
|
+
if (!lastStep) return input;
|
|
147
|
+
return context[lastStep.outputKey] ?? input;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
class ToolChainManager {
|
|
151
|
+
constructor(registry) {
|
|
152
|
+
__publicField$2(this, "chains", /* @__PURE__ */ new Map());
|
|
153
|
+
__publicField$2(this, "registry");
|
|
154
|
+
this.registry = registry;
|
|
155
|
+
}
|
|
156
|
+
registerChain(chain) {
|
|
157
|
+
this.chains.set(chain.name, chain);
|
|
158
|
+
}
|
|
159
|
+
getChain(name) {
|
|
160
|
+
return this.chains.get(name);
|
|
161
|
+
}
|
|
162
|
+
listChains() {
|
|
163
|
+
return Array.from(this.chains.keys());
|
|
164
|
+
}
|
|
165
|
+
async executeChain(chainName, input) {
|
|
166
|
+
const chain = this.chains.get(chainName);
|
|
167
|
+
if (!chain) throw new Error(`ToolChain not found: ${chainName}`);
|
|
168
|
+
return chain.execute(this.registry, input);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
function interpolate(template, context) {
|
|
172
|
+
return template.replace(/\{(\w+)\}/g, (_match, key) => {
|
|
173
|
+
return context[key] ?? `{${key}}`;
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
var __defProp$1 = Object.defineProperty;
|
|
178
|
+
var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
179
|
+
var __publicField$1 = (obj, key, value) => __defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
180
|
+
class ToolRegistry {
|
|
181
|
+
constructor() {
|
|
182
|
+
__publicField$1(this, "tools", /* @__PURE__ */ new Map());
|
|
183
|
+
__publicField$1(this, "functions", /* @__PURE__ */ new Map());
|
|
184
|
+
}
|
|
185
|
+
// ---------------------------------------------------------------------------
|
|
186
|
+
// Registration
|
|
187
|
+
// ---------------------------------------------------------------------------
|
|
188
|
+
registerTool(tool) {
|
|
189
|
+
this.tools.set(tool.name, tool);
|
|
190
|
+
}
|
|
191
|
+
unregisterTool(name) {
|
|
192
|
+
return this.tools.delete(name);
|
|
193
|
+
}
|
|
194
|
+
registerFunction(name, description, func, schema) {
|
|
195
|
+
this.functions.set(name, {
|
|
196
|
+
name,
|
|
197
|
+
description,
|
|
198
|
+
func,
|
|
199
|
+
schema
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
unregisterFunction(name) {
|
|
203
|
+
return this.functions.delete(name);
|
|
204
|
+
}
|
|
205
|
+
// ---------------------------------------------------------------------------
|
|
206
|
+
// Lookup
|
|
207
|
+
// ---------------------------------------------------------------------------
|
|
208
|
+
getTool(name) {
|
|
209
|
+
return this.tools.get(name);
|
|
210
|
+
}
|
|
211
|
+
getFunction(name) {
|
|
212
|
+
return this.functions.get(name);
|
|
213
|
+
}
|
|
214
|
+
getAllTools() {
|
|
215
|
+
return Array.from(this.tools.values());
|
|
216
|
+
}
|
|
217
|
+
listTools() {
|
|
218
|
+
return [
|
|
219
|
+
...Array.from(this.tools.keys()),
|
|
220
|
+
...Array.from(this.functions.keys())
|
|
221
|
+
];
|
|
222
|
+
}
|
|
223
|
+
hasTool(name) {
|
|
224
|
+
return this.tools.has(name) || this.functions.has(name);
|
|
225
|
+
}
|
|
226
|
+
// ---------------------------------------------------------------------------
|
|
227
|
+
// Execution
|
|
228
|
+
// ---------------------------------------------------------------------------
|
|
229
|
+
async execute(name, parameters) {
|
|
230
|
+
const tool = this.tools.get(name);
|
|
231
|
+
if (tool) {
|
|
232
|
+
return await tool.run(parameters);
|
|
233
|
+
}
|
|
234
|
+
const fn = this.functions.get(name);
|
|
235
|
+
if (fn) {
|
|
236
|
+
return await fn.func(parameters);
|
|
237
|
+
}
|
|
238
|
+
throw new Error(`Tool not found: ${name}`);
|
|
239
|
+
}
|
|
240
|
+
// ---------------------------------------------------------------------------
|
|
241
|
+
// Description (for system prompts)
|
|
242
|
+
// ---------------------------------------------------------------------------
|
|
243
|
+
getAvailableTools() {
|
|
244
|
+
const lines = [];
|
|
245
|
+
for (const tool of this.tools.values()) {
|
|
246
|
+
lines.push(tool.describe());
|
|
247
|
+
}
|
|
248
|
+
for (const fn of this.functions.values()) {
|
|
249
|
+
lines.push(`Tool: ${fn.name}
|
|
250
|
+
Description: ${fn.description}`);
|
|
251
|
+
}
|
|
252
|
+
if (lines.length === 0) return "\u6682\u65E0\u53EF\u7528\u5DE5\u5177";
|
|
253
|
+
return lines.join("\n\n");
|
|
254
|
+
}
|
|
255
|
+
getOpenAISchemas() {
|
|
256
|
+
const schemas = [];
|
|
257
|
+
for (const tool of this.tools.values()) {
|
|
258
|
+
schemas.push(tool.toOpenAISchema());
|
|
259
|
+
}
|
|
260
|
+
for (const fn of this.functions.values()) {
|
|
261
|
+
schemas.push({
|
|
262
|
+
type: "function",
|
|
263
|
+
function: {
|
|
264
|
+
name: fn.name,
|
|
265
|
+
description: fn.description,
|
|
266
|
+
parameters: {
|
|
267
|
+
type: "object",
|
|
268
|
+
properties: {
|
|
269
|
+
input: { type: "string", description: "\u8F93\u5165\u6587\u672C" }
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
return schemas;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
var __defProp = Object.defineProperty;
|
|
280
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
281
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
282
|
+
class AsyncToolExecutor {
|
|
283
|
+
constructor(registry, concurrency = 4) {
|
|
284
|
+
__publicField(this, "registry");
|
|
285
|
+
__publicField(this, "concurrency");
|
|
286
|
+
this.registry = registry;
|
|
287
|
+
this.concurrency = Math.max(1, concurrency);
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Execute a batch of tool calls with bounded concurrency.
|
|
291
|
+
*/
|
|
292
|
+
async executeBatch(requests) {
|
|
293
|
+
const results = [];
|
|
294
|
+
let idx = 0;
|
|
295
|
+
const worker = async () => {
|
|
296
|
+
while (idx < requests.length) {
|
|
297
|
+
const req = requests[idx++];
|
|
298
|
+
results.push(await this.executeSingle(req));
|
|
299
|
+
}
|
|
300
|
+
};
|
|
301
|
+
const workers = Array.from({ length: Math.min(this.concurrency, requests.length) }, worker);
|
|
302
|
+
await Promise.all(workers);
|
|
303
|
+
return results;
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Execute a single tool call.
|
|
307
|
+
*/
|
|
308
|
+
async executeSingle(request) {
|
|
309
|
+
const start = Date.now();
|
|
310
|
+
try {
|
|
311
|
+
const output = await this.registry.execute(request.toolName, request.parameters);
|
|
312
|
+
return {
|
|
313
|
+
id: request.id,
|
|
314
|
+
toolName: request.toolName,
|
|
315
|
+
output,
|
|
316
|
+
durationMs: Date.now() - start
|
|
317
|
+
};
|
|
318
|
+
} catch (error) {
|
|
319
|
+
return {
|
|
320
|
+
id: request.id,
|
|
321
|
+
toolName: request.toolName,
|
|
322
|
+
output: "",
|
|
323
|
+
error: error instanceof Error ? error.message : String(error),
|
|
324
|
+
durationMs: Date.now() - start
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
Object.defineProperty(exports, "z", {
|
|
331
|
+
enumerable: true,
|
|
332
|
+
get: function () { return zod.z; }
|
|
333
|
+
});
|
|
334
|
+
exports.AsyncToolExecutor = AsyncToolExecutor;
|
|
335
|
+
exports.Tool = Tool;
|
|
336
|
+
exports.ToolChain = ToolChain;
|
|
337
|
+
exports.ToolChainManager = ToolChainManager;
|
|
338
|
+
exports.ToolRegistry = ToolRegistry;
|
|
339
|
+
exports.defineFunctionTool = defineFunctionTool;
|
|
340
|
+
exports.toolAction = toolAction;
|
|
341
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../../src/Tool.ts","../../src/ToolChain.ts","../../src/ToolRegistry.ts","../../src/AsyncToolExecutor.ts"],"sourcesContent":["import \"reflect-metadata\";\nimport {z, type ZodType} from \"zod\";\nimport type {ToolParameter} from \"./types\";\n\nexport type {ToolParameter};\n\n// ---------------------------------------------------------------------------\n// OpenAI function-calling schema\n// ---------------------------------------------------------------------------\n\nexport interface OpenAIFunctionSchema {\n type: \"function\";\n function: {\n name: string;\n description: string;\n parameters: Record<string, unknown>;\n };\n}\n\n// ---------------------------------------------------------------------------\n// toolAction decorator\n// ---------------------------------------------------------------------------\n\nconst TOOL_ACTIONS_META_KEY = Symbol(\"tool:actions\");\n\nexport interface ToolActionMeta {\n key: string;\n description: string;\n method: string;\n}\n\n/**\n * Decorator that registers a method as a named tool action.\n * Usage: @toolAction(\"action_key\", \"Description\")\n */\nexport function toolAction(key: string, description: string) {\n return function (\n target: object,\n propertyKey: string,\n _descriptor: PropertyDescriptor,\n ): void {\n const existing: ToolActionMeta[] =\n Reflect.getMetadata(TOOL_ACTIONS_META_KEY, target) ?? [];\n existing.push({key, description, method: propertyKey});\n Reflect.defineMetadata(TOOL_ACTIONS_META_KEY, existing, target);\n };\n}\n\n// ---------------------------------------------------------------------------\n// Tool base class\n// ---------------------------------------------------------------------------\n\nexport abstract class Tool {\n readonly name: string;\n readonly description: string;\n readonly expandable: boolean;\n\n constructor(name: string, description: string, expandable = false) {\n this.name = name;\n this.description = description;\n this.expandable = expandable;\n }\n\n abstract run(parameters: Record<string, unknown>): Promise<string> | string;\n abstract getParameters(): ToolParameter[];\n\n /**\n * Validate that required parameters are present and have basic types.\n * Returns true if valid.\n */\n validateParameters(parameters: Record<string, unknown>): boolean {\n const params = this.getParameters();\n for (const p of params) {\n if (p.required && (parameters[p.name] === undefined || parameters[p.name] === null)) {\n return false;\n }\n }\n return true;\n }\n\n /**\n * Validate and coerce parameters, returning a Result object.\n */\n validateAndNormalizeParameters(\n parameters: Record<string, unknown>,\n ): {success: true; data: Record<string, unknown>} | {success: false; error: string} {\n const params = this.getParameters();\n const data: Record<string, unknown> = {};\n\n for (const p of params) {\n const val = parameters[p.name];\n if (val === undefined || val === null) {\n if (p.required) {\n return {success: false, error: `Missing required parameter: ${p.name}`};\n }\n data[p.name] = p.default;\n } else {\n data[p.name] = val;\n }\n }\n\n // pass through any extra parameters\n for (const [key, val] of Object.entries(parameters)) {\n if (!(key in data)) data[key] = val;\n }\n\n return {success: true, data};\n }\n\n /**\n * Convert this Tool to an OpenAI function-calling schema.\n */\n toOpenAISchema(): OpenAIFunctionSchema {\n const params = this.getParameters();\n const properties: Record<string, unknown> = {};\n const required: string[] = [];\n\n for (const p of params) {\n properties[p.name] = {\n type: mapType(p.type),\n description: p.description,\n ...(p.default !== null && p.default !== undefined\n ? {default: p.default}\n : {}),\n };\n if (p.required) required.push(p.name);\n }\n\n return {\n type: \"function\",\n function: {\n name: this.name,\n description: this.description,\n parameters: {\n type: \"object\",\n properties,\n ...(required.length > 0 ? {required} : {}),\n },\n },\n };\n }\n\n /**\n * Return a formatted description of all available tool actions (for system prompt).\n */\n describe(): string {\n const params = this.getParameters();\n const paramStr = params\n .map(\n (p) =>\n ` - ${p.name} (${p.type}${p.required ? \", required\" : \"\"}): ${p.description}`,\n )\n .join(\"\\n\");\n return `Tool: ${this.name}\\nDescription: ${this.description}\\nParameters:\\n${paramStr}`;\n }\n}\n\nfunction mapType(type: string): string {\n const t = (type ?? \"string\").toLowerCase();\n if ([\"string\", \"number\", \"integer\", \"boolean\", \"array\", \"object\"].includes(t)) {\n return t;\n }\n return \"string\";\n}\n\n// ---------------------------------------------------------------------------\n// FunctionTool — wraps a plain async/sync function\n// ---------------------------------------------------------------------------\n\nexport interface FunctionTool<TArgs = Record<string, unknown>> {\n name: string;\n description: string;\n func: (args: TArgs) => string | Promise<string>;\n schema?: ZodType<TArgs>;\n}\n\n/**\n * Convenience factory for creating type-safe function tools.\n *\n * ```ts\n * const myTool = defineFunctionTool({\n * name: \"myTool\",\n * description: \"...\",\n * schema: z.object({ input: z.string() }),\n * func: ({ input }) => input.toUpperCase(),\n * });\n * ```\n */\nexport function defineFunctionTool<TArgs extends Record<string, unknown>>(\n options: FunctionTool<TArgs>,\n): FunctionTool<TArgs> {\n return options;\n}\n\nexport {z};\n","import {ToolRegistry} from \"./ToolRegistry\";\n\n// ---------------------------------------------------------------------------\n// ToolChain step definition\n// ---------------------------------------------------------------------------\n\nexport interface ToolChainStep {\n /** Name of the tool/function to call */\n toolName: string;\n /** Input template string; use {variableName} to reference context variables */\n inputTemplate: string;\n /** Key under which the step output is stored in context */\n outputKey: string;\n}\n\n// ---------------------------------------------------------------------------\n// ToolChain\n// ---------------------------------------------------------------------------\n\n/**\n * A sequential chain of tool steps.\n * Each step can reference outputs of prior steps via `{key}` placeholders.\n *\n * ```ts\n * const chain = new ToolChain(\"my_chain\", \"Does X then Y\");\n * chain.addStep(\"search\", \"{input}\", \"search_result\");\n * chain.addStep(\"summarize\", \"{search_result}\", \"summary\");\n * ```\n */\nexport class ToolChain {\n readonly name: string;\n readonly description: string;\n private readonly steps: ToolChainStep[] = [];\n\n constructor(name: string, description: string) {\n this.name = name;\n this.description = description;\n }\n\n addStep(toolName: string, inputTemplate: string, outputKey: string): this {\n this.steps.push({toolName, inputTemplate, outputKey});\n return this;\n }\n\n getSteps(): ToolChainStep[] {\n return [...this.steps];\n }\n\n /**\n * Execute the chain against a registry.\n * @param registry ToolRegistry that holds the referenced tools.\n * @param input Initial `{input}` value.\n * @returns The value stored under the last step's outputKey.\n */\n async execute(registry: ToolRegistry, input: string): Promise<string> {\n const context: Record<string, string> = {input};\n\n for (const step of this.steps) {\n const resolvedInput = interpolate(step.inputTemplate, context);\n const result = await registry.execute(step.toolName, {input: resolvedInput});\n context[step.outputKey] = result;\n }\n\n const lastStep = this.steps[this.steps.length - 1];\n if (!lastStep) return input;\n return context[lastStep.outputKey] ?? input;\n }\n}\n\n// ---------------------------------------------------------------------------\n// ToolChainManager\n// ---------------------------------------------------------------------------\n\n/**\n * Manages multiple named ToolChains and executes them against a shared registry.\n */\nexport class ToolChainManager {\n private readonly chains = new Map<string, ToolChain>();\n private readonly registry: ToolRegistry;\n\n constructor(registry: ToolRegistry) {\n this.registry = registry;\n }\n\n registerChain(chain: ToolChain): void {\n this.chains.set(chain.name, chain);\n }\n\n getChain(name: string): ToolChain | undefined {\n return this.chains.get(name);\n }\n\n listChains(): string[] {\n return Array.from(this.chains.keys());\n }\n\n async executeChain(chainName: string, input: string): Promise<string> {\n const chain = this.chains.get(chainName);\n if (!chain) throw new Error(`ToolChain not found: ${chainName}`);\n return chain.execute(this.registry, input);\n }\n}\n\n// ---------------------------------------------------------------------------\n// Helpers\n// ---------------------------------------------------------------------------\n\n/** Replace `{key}` placeholders with values from context. */\nfunction interpolate(template: string, context: Record<string, string>): string {\n return template.replace(/\\{(\\w+)\\}/g, (_match, key: string) => {\n return context[key] ?? `{${key}}`;\n });\n}\n","import {Tool, type FunctionTool, type OpenAIFunctionSchema} from \"./Tool\";\n\n/**\n * Central registry that manages Tool instances and raw FunctionTools.\n * Supports registration, lookup, and execution.\n */\nexport class ToolRegistry {\n private readonly tools = new Map<string, Tool>();\n private readonly functions = new Map<string, FunctionTool<Record<string, unknown>>>();\n\n // ---------------------------------------------------------------------------\n // Registration\n // ---------------------------------------------------------------------------\n\n registerTool(tool: Tool): void {\n this.tools.set(tool.name, tool);\n }\n\n unregisterTool(name: string): boolean {\n return this.tools.delete(name);\n }\n\n registerFunction<TArgs extends Record<string, unknown>>(\n name: string,\n description: string,\n func: (args: TArgs) => string | Promise<string>,\n schema?: FunctionTool<TArgs>[\"schema\"],\n ): void {\n this.functions.set(name, {\n name,\n description,\n func: func as FunctionTool<Record<string, unknown>>[\"func\"],\n schema: schema as FunctionTool<Record<string, unknown>>[\"schema\"],\n });\n }\n\n unregisterFunction(name: string): boolean {\n return this.functions.delete(name);\n }\n\n // ---------------------------------------------------------------------------\n // Lookup\n // ---------------------------------------------------------------------------\n\n getTool(name: string): Tool | undefined {\n return this.tools.get(name);\n }\n\n getFunction(name: string): FunctionTool<Record<string, unknown>> | undefined {\n return this.functions.get(name);\n }\n\n getAllTools(): Tool[] {\n return Array.from(this.tools.values());\n }\n\n listTools(): string[] {\n return [\n ...Array.from(this.tools.keys()),\n ...Array.from(this.functions.keys()),\n ];\n }\n\n hasTool(name: string): boolean {\n return this.tools.has(name) || this.functions.has(name);\n }\n\n // ---------------------------------------------------------------------------\n // Execution\n // ---------------------------------------------------------------------------\n\n async execute(name: string, parameters: Record<string, unknown>): Promise<string> {\n const tool = this.tools.get(name);\n if (tool) {\n return await tool.run(parameters);\n }\n\n const fn = this.functions.get(name);\n if (fn) {\n return await fn.func(parameters);\n }\n\n throw new Error(`Tool not found: ${name}`);\n }\n\n // ---------------------------------------------------------------------------\n // Description (for system prompts)\n // ---------------------------------------------------------------------------\n\n getAvailableTools(): string {\n const lines: string[] = [];\n\n for (const tool of this.tools.values()) {\n lines.push(tool.describe());\n }\n\n for (const fn of this.functions.values()) {\n lines.push(`Tool: ${fn.name}\\nDescription: ${fn.description}`);\n }\n\n if (lines.length === 0) return \"暂无可用工具\";\n return lines.join(\"\\n\\n\");\n }\n\n getOpenAISchemas(): Array<OpenAIFunctionSchema | Record<string, unknown>> {\n const schemas: Array<OpenAIFunctionSchema | Record<string, unknown>> = [];\n\n for (const tool of this.tools.values()) {\n schemas.push(tool.toOpenAISchema());\n }\n\n for (const fn of this.functions.values()) {\n schemas.push({\n type: \"function\",\n function: {\n name: fn.name,\n description: fn.description,\n parameters: {\n type: \"object\",\n properties: {\n input: {type: \"string\", description: \"输入文本\"},\n },\n },\n },\n });\n }\n\n return schemas;\n }\n}\n","import {ToolRegistry} from \"./ToolRegistry\";\n\nexport interface ToolCallRequest {\n id: string;\n toolName: string;\n parameters: Record<string, unknown>;\n}\n\nexport interface ToolCallResult {\n id: string;\n toolName: string;\n output: string;\n error?: string;\n durationMs: number;\n}\n\n/**\n * Executes multiple tool calls concurrently and collects results.\n */\nexport class AsyncToolExecutor {\n private readonly registry: ToolRegistry;\n private readonly concurrency: number;\n\n constructor(registry: ToolRegistry, concurrency = 4) {\n this.registry = registry;\n this.concurrency = Math.max(1, concurrency);\n }\n\n /**\n * Execute a batch of tool calls with bounded concurrency.\n */\n async executeBatch(requests: ToolCallRequest[]): Promise<ToolCallResult[]> {\n const results: ToolCallResult[] = [];\n let idx = 0;\n\n const worker = async (): Promise<void> => {\n while (idx < requests.length) {\n const req = requests[idx++]!;\n results.push(await this.executeSingle(req));\n }\n };\n\n const workers = Array.from({length: Math.min(this.concurrency, requests.length)}, worker);\n await Promise.all(workers);\n return results;\n }\n\n /**\n * Execute a single tool call.\n */\n async executeSingle(request: ToolCallRequest): Promise<ToolCallResult> {\n const start = Date.now();\n try {\n const output = await this.registry.execute(request.toolName, request.parameters);\n return {\n id: request.id,\n toolName: request.toolName,\n output,\n durationMs: Date.now() - start,\n };\n } catch (error) {\n return {\n id: request.id,\n toolName: request.toolName,\n output: \"\",\n error: error instanceof Error ? error.message : String(error),\n durationMs: Date.now() - start,\n };\n }\n }\n}\n"],"names":["__publicField"],"mappings":";;;;;;;;AAuBA,MAAM,qBAAA,0BAA+B,cAAc,CAAA;AAY5C,SAAS,UAAA,CAAW,KAAa,WAAA,EAAqB;AAC3D,EAAA,OAAO,SACL,MAAA,EACA,WAAA,EACA,WAAA,EACM;AACN,IAAA,MAAM,WACJ,OAAA,CAAQ,WAAA,CAAY,qBAAA,EAAuB,MAAM,KAAK,EAAC;AACzD,IAAA,QAAA,CAAS,KAAK,EAAC,GAAA,EAAK,WAAA,EAAa,MAAA,EAAQ,aAAY,CAAA;AACrD,IAAA,OAAA,CAAQ,cAAA,CAAe,qBAAA,EAAuB,QAAA,EAAU,MAAM,CAAA;AAAA,EAChE,CAAA;AACF;AAMO,MAAe,IAAA,CAAK;AAAA,EAKzB,WAAA,CAAY,IAAA,EAAc,WAAA,EAAqB,UAAA,GAAa,KAAA,EAAO;AAJnE,IAAAA,eAAA,CAAA,IAAA,EAAS,MAAA,CAAA;AACT,IAAAA,eAAA,CAAA,IAAA,EAAS,aAAA,CAAA;AACT,IAAAA,eAAA,CAAA,IAAA,EAAS,YAAA,CAAA;AAGP,IAAA,IAAA,CAAK,IAAA,GAAO,IAAA;AACZ,IAAA,IAAA,CAAK,WAAA,GAAc,WAAA;AACnB,IAAA,IAAA,CAAK,UAAA,GAAa,UAAA;AAAA,EACpB;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,mBAAmB,UAAA,EAA8C;AAC/D,IAAA,MAAM,MAAA,GAAS,KAAK,aAAA,EAAc;AAClC,IAAA,KAAA,MAAW,KAAK,MAAA,EAAQ;AACtB,MAAA,IAAI,CAAA,CAAE,QAAA,KAAa,UAAA,CAAW,CAAA,CAAE,IAAI,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A,QACZ,UAAU,OAAA,CAAQ,QAAA;AAAA,QAClB,MAAA;AAAA,QACA,UAAA,EAAY,IAAA,CAAK,GAAA,EAAI,GAAI;AAAA,OAC3B;AAAA,IACF,SAAS,KAAA,EAAO;AACd,MAAA,OAAO;AAAA,QACL,IAAI,OAAA,CAAQ,EAAA;AAAA,QACZ,UAAU,OAAA,CAAQ,QAAA;AAAA,QAClB,MAAA,EAAQ,EAAA;AAAA,QACR,OAAO,KAAA,YAAiB,KAAA,GAAQ,KAAA,CAAM,OAAA,GAAU,OAAO,KAAK,CAAA;AAAA,QAC5D,UAAA,EAAY,IAAA,CAAK,GAAA,EAAI,GAAI;AAAA,OAC3B;AAAA,IACF;AAAA,EACF;AACF;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
import 'reflect-metadata';
|
|
2
|
+
export { z } from 'zod';
|
|
3
|
+
|
|
4
|
+
var __defProp$3 = Object.defineProperty;
|
|
5
|
+
var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
+
var __publicField$3 = (obj, key, value) => __defNormalProp$3(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
7
|
+
const TOOL_ACTIONS_META_KEY = /* @__PURE__ */ Symbol("tool:actions");
|
|
8
|
+
function toolAction(key, description) {
|
|
9
|
+
return function(target, propertyKey, _descriptor) {
|
|
10
|
+
const existing = Reflect.getMetadata(TOOL_ACTIONS_META_KEY, target) ?? [];
|
|
11
|
+
existing.push({ key, description, method: propertyKey });
|
|
12
|
+
Reflect.defineMetadata(TOOL_ACTIONS_META_KEY, existing, target);
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
class Tool {
|
|
16
|
+
constructor(name, description, expandable = false) {
|
|
17
|
+
__publicField$3(this, "name");
|
|
18
|
+
__publicField$3(this, "description");
|
|
19
|
+
__publicField$3(this, "expandable");
|
|
20
|
+
this.name = name;
|
|
21
|
+
this.description = description;
|
|
22
|
+
this.expandable = expandable;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Validate that required parameters are present and have basic types.
|
|
26
|
+
* Returns true if valid.
|
|
27
|
+
*/
|
|
28
|
+
validateParameters(parameters) {
|
|
29
|
+
const params = this.getParameters();
|
|
30
|
+
for (const p of params) {
|
|
31
|
+
if (p.required && (parameters[p.name] === void 0 || parameters[p.name] === null)) {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Validate and coerce parameters, returning a Result object.
|
|
39
|
+
*/
|
|
40
|
+
validateAndNormalizeParameters(parameters) {
|
|
41
|
+
const params = this.getParameters();
|
|
42
|
+
const data = {};
|
|
43
|
+
for (const p of params) {
|
|
44
|
+
const val = parameters[p.name];
|
|
45
|
+
if (val === void 0 || val === null) {
|
|
46
|
+
if (p.required) {
|
|
47
|
+
return { success: false, error: `Missing required parameter: ${p.name}` };
|
|
48
|
+
}
|
|
49
|
+
data[p.name] = p.default;
|
|
50
|
+
} else {
|
|
51
|
+
data[p.name] = val;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
for (const [key, val] of Object.entries(parameters)) {
|
|
55
|
+
if (!(key in data)) data[key] = val;
|
|
56
|
+
}
|
|
57
|
+
return { success: true, data };
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Convert this Tool to an OpenAI function-calling schema.
|
|
61
|
+
*/
|
|
62
|
+
toOpenAISchema() {
|
|
63
|
+
const params = this.getParameters();
|
|
64
|
+
const properties = {};
|
|
65
|
+
const required = [];
|
|
66
|
+
for (const p of params) {
|
|
67
|
+
properties[p.name] = {
|
|
68
|
+
type: mapType(p.type),
|
|
69
|
+
description: p.description,
|
|
70
|
+
...p.default !== null && p.default !== void 0 ? { default: p.default } : {}
|
|
71
|
+
};
|
|
72
|
+
if (p.required) required.push(p.name);
|
|
73
|
+
}
|
|
74
|
+
return {
|
|
75
|
+
type: "function",
|
|
76
|
+
function: {
|
|
77
|
+
name: this.name,
|
|
78
|
+
description: this.description,
|
|
79
|
+
parameters: {
|
|
80
|
+
type: "object",
|
|
81
|
+
properties,
|
|
82
|
+
...required.length > 0 ? { required } : {}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Return a formatted description of all available tool actions (for system prompt).
|
|
89
|
+
*/
|
|
90
|
+
describe() {
|
|
91
|
+
const params = this.getParameters();
|
|
92
|
+
const paramStr = params.map(
|
|
93
|
+
(p) => ` - ${p.name} (${p.type}${p.required ? ", required" : ""}): ${p.description}`
|
|
94
|
+
).join("\n");
|
|
95
|
+
return `Tool: ${this.name}
|
|
96
|
+
Description: ${this.description}
|
|
97
|
+
Parameters:
|
|
98
|
+
${paramStr}`;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
function mapType(type) {
|
|
102
|
+
const t = (type ?? "string").toLowerCase();
|
|
103
|
+
if (["string", "number", "integer", "boolean", "array", "object"].includes(t)) {
|
|
104
|
+
return t;
|
|
105
|
+
}
|
|
106
|
+
return "string";
|
|
107
|
+
}
|
|
108
|
+
function defineFunctionTool(options) {
|
|
109
|
+
return options;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
var __defProp$2 = Object.defineProperty;
|
|
113
|
+
var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
114
|
+
var __publicField$2 = (obj, key, value) => __defNormalProp$2(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
115
|
+
class ToolChain {
|
|
116
|
+
constructor(name, description) {
|
|
117
|
+
__publicField$2(this, "name");
|
|
118
|
+
__publicField$2(this, "description");
|
|
119
|
+
__publicField$2(this, "steps", []);
|
|
120
|
+
this.name = name;
|
|
121
|
+
this.description = description;
|
|
122
|
+
}
|
|
123
|
+
addStep(toolName, inputTemplate, outputKey) {
|
|
124
|
+
this.steps.push({ toolName, inputTemplate, outputKey });
|
|
125
|
+
return this;
|
|
126
|
+
}
|
|
127
|
+
getSteps() {
|
|
128
|
+
return [...this.steps];
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Execute the chain against a registry.
|
|
132
|
+
* @param registry ToolRegistry that holds the referenced tools.
|
|
133
|
+
* @param input Initial `{input}` value.
|
|
134
|
+
* @returns The value stored under the last step's outputKey.
|
|
135
|
+
*/
|
|
136
|
+
async execute(registry, input) {
|
|
137
|
+
const context = { input };
|
|
138
|
+
for (const step of this.steps) {
|
|
139
|
+
const resolvedInput = interpolate(step.inputTemplate, context);
|
|
140
|
+
const result = await registry.execute(step.toolName, { input: resolvedInput });
|
|
141
|
+
context[step.outputKey] = result;
|
|
142
|
+
}
|
|
143
|
+
const lastStep = this.steps[this.steps.length - 1];
|
|
144
|
+
if (!lastStep) return input;
|
|
145
|
+
return context[lastStep.outputKey] ?? input;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
class ToolChainManager {
|
|
149
|
+
constructor(registry) {
|
|
150
|
+
__publicField$2(this, "chains", /* @__PURE__ */ new Map());
|
|
151
|
+
__publicField$2(this, "registry");
|
|
152
|
+
this.registry = registry;
|
|
153
|
+
}
|
|
154
|
+
registerChain(chain) {
|
|
155
|
+
this.chains.set(chain.name, chain);
|
|
156
|
+
}
|
|
157
|
+
getChain(name) {
|
|
158
|
+
return this.chains.get(name);
|
|
159
|
+
}
|
|
160
|
+
listChains() {
|
|
161
|
+
return Array.from(this.chains.keys());
|
|
162
|
+
}
|
|
163
|
+
async executeChain(chainName, input) {
|
|
164
|
+
const chain = this.chains.get(chainName);
|
|
165
|
+
if (!chain) throw new Error(`ToolChain not found: ${chainName}`);
|
|
166
|
+
return chain.execute(this.registry, input);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
function interpolate(template, context) {
|
|
170
|
+
return template.replace(/\{(\w+)\}/g, (_match, key) => {
|
|
171
|
+
return context[key] ?? `{${key}}`;
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
var __defProp$1 = Object.defineProperty;
|
|
176
|
+
var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
177
|
+
var __publicField$1 = (obj, key, value) => __defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
178
|
+
class ToolRegistry {
|
|
179
|
+
constructor() {
|
|
180
|
+
__publicField$1(this, "tools", /* @__PURE__ */ new Map());
|
|
181
|
+
__publicField$1(this, "functions", /* @__PURE__ */ new Map());
|
|
182
|
+
}
|
|
183
|
+
// ---------------------------------------------------------------------------
|
|
184
|
+
// Registration
|
|
185
|
+
// ---------------------------------------------------------------------------
|
|
186
|
+
registerTool(tool) {
|
|
187
|
+
this.tools.set(tool.name, tool);
|
|
188
|
+
}
|
|
189
|
+
unregisterTool(name) {
|
|
190
|
+
return this.tools.delete(name);
|
|
191
|
+
}
|
|
192
|
+
registerFunction(name, description, func, schema) {
|
|
193
|
+
this.functions.set(name, {
|
|
194
|
+
name,
|
|
195
|
+
description,
|
|
196
|
+
func,
|
|
197
|
+
schema
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
unregisterFunction(name) {
|
|
201
|
+
return this.functions.delete(name);
|
|
202
|
+
}
|
|
203
|
+
// ---------------------------------------------------------------------------
|
|
204
|
+
// Lookup
|
|
205
|
+
// ---------------------------------------------------------------------------
|
|
206
|
+
getTool(name) {
|
|
207
|
+
return this.tools.get(name);
|
|
208
|
+
}
|
|
209
|
+
getFunction(name) {
|
|
210
|
+
return this.functions.get(name);
|
|
211
|
+
}
|
|
212
|
+
getAllTools() {
|
|
213
|
+
return Array.from(this.tools.values());
|
|
214
|
+
}
|
|
215
|
+
listTools() {
|
|
216
|
+
return [
|
|
217
|
+
...Array.from(this.tools.keys()),
|
|
218
|
+
...Array.from(this.functions.keys())
|
|
219
|
+
];
|
|
220
|
+
}
|
|
221
|
+
hasTool(name) {
|
|
222
|
+
return this.tools.has(name) || this.functions.has(name);
|
|
223
|
+
}
|
|
224
|
+
// ---------------------------------------------------------------------------
|
|
225
|
+
// Execution
|
|
226
|
+
// ---------------------------------------------------------------------------
|
|
227
|
+
async execute(name, parameters) {
|
|
228
|
+
const tool = this.tools.get(name);
|
|
229
|
+
if (tool) {
|
|
230
|
+
return await tool.run(parameters);
|
|
231
|
+
}
|
|
232
|
+
const fn = this.functions.get(name);
|
|
233
|
+
if (fn) {
|
|
234
|
+
return await fn.func(parameters);
|
|
235
|
+
}
|
|
236
|
+
throw new Error(`Tool not found: ${name}`);
|
|
237
|
+
}
|
|
238
|
+
// ---------------------------------------------------------------------------
|
|
239
|
+
// Description (for system prompts)
|
|
240
|
+
// ---------------------------------------------------------------------------
|
|
241
|
+
getAvailableTools() {
|
|
242
|
+
const lines = [];
|
|
243
|
+
for (const tool of this.tools.values()) {
|
|
244
|
+
lines.push(tool.describe());
|
|
245
|
+
}
|
|
246
|
+
for (const fn of this.functions.values()) {
|
|
247
|
+
lines.push(`Tool: ${fn.name}
|
|
248
|
+
Description: ${fn.description}`);
|
|
249
|
+
}
|
|
250
|
+
if (lines.length === 0) return "\u6682\u65E0\u53EF\u7528\u5DE5\u5177";
|
|
251
|
+
return lines.join("\n\n");
|
|
252
|
+
}
|
|
253
|
+
getOpenAISchemas() {
|
|
254
|
+
const schemas = [];
|
|
255
|
+
for (const tool of this.tools.values()) {
|
|
256
|
+
schemas.push(tool.toOpenAISchema());
|
|
257
|
+
}
|
|
258
|
+
for (const fn of this.functions.values()) {
|
|
259
|
+
schemas.push({
|
|
260
|
+
type: "function",
|
|
261
|
+
function: {
|
|
262
|
+
name: fn.name,
|
|
263
|
+
description: fn.description,
|
|
264
|
+
parameters: {
|
|
265
|
+
type: "object",
|
|
266
|
+
properties: {
|
|
267
|
+
input: { type: "string", description: "\u8F93\u5165\u6587\u672C" }
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
return schemas;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
var __defProp = Object.defineProperty;
|
|
278
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
279
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
280
|
+
class AsyncToolExecutor {
|
|
281
|
+
constructor(registry, concurrency = 4) {
|
|
282
|
+
__publicField(this, "registry");
|
|
283
|
+
__publicField(this, "concurrency");
|
|
284
|
+
this.registry = registry;
|
|
285
|
+
this.concurrency = Math.max(1, concurrency);
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Execute a batch of tool calls with bounded concurrency.
|
|
289
|
+
*/
|
|
290
|
+
async executeBatch(requests) {
|
|
291
|
+
const results = [];
|
|
292
|
+
let idx = 0;
|
|
293
|
+
const worker = async () => {
|
|
294
|
+
while (idx < requests.length) {
|
|
295
|
+
const req = requests[idx++];
|
|
296
|
+
results.push(await this.executeSingle(req));
|
|
297
|
+
}
|
|
298
|
+
};
|
|
299
|
+
const workers = Array.from({ length: Math.min(this.concurrency, requests.length) }, worker);
|
|
300
|
+
await Promise.all(workers);
|
|
301
|
+
return results;
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* Execute a single tool call.
|
|
305
|
+
*/
|
|
306
|
+
async executeSingle(request) {
|
|
307
|
+
const start = Date.now();
|
|
308
|
+
try {
|
|
309
|
+
const output = await this.registry.execute(request.toolName, request.parameters);
|
|
310
|
+
return {
|
|
311
|
+
id: request.id,
|
|
312
|
+
toolName: request.toolName,
|
|
313
|
+
output,
|
|
314
|
+
durationMs: Date.now() - start
|
|
315
|
+
};
|
|
316
|
+
} catch (error) {
|
|
317
|
+
return {
|
|
318
|
+
id: request.id,
|
|
319
|
+
toolName: request.toolName,
|
|
320
|
+
output: "",
|
|
321
|
+
error: error instanceof Error ? error.message : String(error),
|
|
322
|
+
durationMs: Date.now() - start
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
export { AsyncToolExecutor, Tool, ToolChain, ToolChainManager, ToolRegistry, defineFunctionTool, toolAction };
|
|
329
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../src/Tool.ts","../../src/ToolChain.ts","../../src/ToolRegistry.ts","../../src/AsyncToolExecutor.ts"],"sourcesContent":["import \"reflect-metadata\";\nimport {z, type ZodType} from \"zod\";\nimport type {ToolParameter} from \"./types\";\n\nexport type {ToolParameter};\n\n// ---------------------------------------------------------------------------\n// OpenAI function-calling schema\n// ---------------------------------------------------------------------------\n\nexport interface OpenAIFunctionSchema {\n type: \"function\";\n function: {\n name: string;\n description: string;\n parameters: Record<string, unknown>;\n };\n}\n\n// ---------------------------------------------------------------------------\n// toolAction decorator\n// ---------------------------------------------------------------------------\n\nconst TOOL_ACTIONS_META_KEY = Symbol(\"tool:actions\");\n\nexport interface ToolActionMeta {\n key: string;\n description: string;\n method: string;\n}\n\n/**\n * Decorator that registers a method as a named tool action.\n * Usage: @toolAction(\"action_key\", \"Description\")\n */\nexport function toolAction(key: string, description: string) {\n return function (\n target: object,\n propertyKey: string,\n _descriptor: PropertyDescriptor,\n ): void {\n const existing: ToolActionMeta[] =\n Reflect.getMetadata(TOOL_ACTIONS_META_KEY, target) ?? [];\n existing.push({key, description, method: propertyKey});\n Reflect.defineMetadata(TOOL_ACTIONS_META_KEY, existing, target);\n };\n}\n\n// ---------------------------------------------------------------------------\n// Tool base class\n// ---------------------------------------------------------------------------\n\nexport abstract class Tool {\n readonly name: string;\n readonly description: string;\n readonly expandable: boolean;\n\n constructor(name: string, description: string, expandable = false) {\n this.name = name;\n this.description = description;\n this.expandable = expandable;\n }\n\n abstract run(parameters: Record<string, unknown>): Promise<string> | string;\n abstract getParameters(): ToolParameter[];\n\n /**\n * Validate that required parameters are present and have basic types.\n * Returns true if valid.\n */\n validateParameters(parameters: Record<string, unknown>): boolean {\n const params = this.getParameters();\n for (const p of params) {\n if (p.required && (parameters[p.name] === undefined || parameters[p.name] === null)) {\n return false;\n }\n }\n return true;\n }\n\n /**\n * Validate and coerce parameters, returning a Result object.\n */\n validateAndNormalizeParameters(\n parameters: Record<string, unknown>,\n ): {success: true; data: Record<string, unknown>} | {success: false; error: string} {\n const params = this.getParameters();\n const data: Record<string, unknown> = {};\n\n for (const p of params) {\n const val = parameters[p.name];\n if (val === undefined || val === null) {\n if (p.required) {\n return {success: false, error: `Missing required parameter: ${p.name}`};\n }\n data[p.name] = p.default;\n } else {\n data[p.name] = val;\n }\n }\n\n // pass through any extra parameters\n for (const [key, val] of Object.entries(parameters)) {\n if (!(key in data)) data[key] = val;\n }\n\n return {success: true, data};\n }\n\n /**\n * Convert this Tool to an OpenAI function-calling schema.\n */\n toOpenAISchema(): OpenAIFunctionSchema {\n const params = this.getParameters();\n const properties: Record<string, unknown> = {};\n const required: string[] = [];\n\n for (const p of params) {\n properties[p.name] = {\n type: mapType(p.type),\n description: p.description,\n ...(p.default !== null && p.default !== undefined\n ? {default: p.default}\n : {}),\n };\n if (p.required) required.push(p.name);\n }\n\n return {\n type: \"function\",\n function: {\n name: this.name,\n description: this.description,\n parameters: {\n type: \"object\",\n properties,\n ...(required.length > 0 ? {required} : {}),\n },\n },\n };\n }\n\n /**\n * Return a formatted description of all available tool actions (for system prompt).\n */\n describe(): string {\n const params = this.getParameters();\n const paramStr = params\n .map(\n (p) =>\n ` - ${p.name} (${p.type}${p.required ? \", required\" : \"\"}): ${p.description}`,\n )\n .join(\"\\n\");\n return `Tool: ${this.name}\\nDescription: ${this.description}\\nParameters:\\n${paramStr}`;\n }\n}\n\nfunction mapType(type: string): string {\n const t = (type ?? \"string\").toLowerCase();\n if ([\"string\", \"number\", \"integer\", \"boolean\", \"array\", \"object\"].includes(t)) {\n return t;\n }\n return \"string\";\n}\n\n// ---------------------------------------------------------------------------\n// FunctionTool — wraps a plain async/sync function\n// ---------------------------------------------------------------------------\n\nexport interface FunctionTool<TArgs = Record<string, unknown>> {\n name: string;\n description: string;\n func: (args: TArgs) => string | Promise<string>;\n schema?: ZodType<TArgs>;\n}\n\n/**\n * Convenience factory for creating type-safe function tools.\n *\n * ```ts\n * const myTool = defineFunctionTool({\n * name: \"myTool\",\n * description: \"...\",\n * schema: z.object({ input: z.string() }),\n * func: ({ input }) => input.toUpperCase(),\n * });\n * ```\n */\nexport function defineFunctionTool<TArgs extends Record<string, unknown>>(\n options: FunctionTool<TArgs>,\n): FunctionTool<TArgs> {\n return options;\n}\n\nexport {z};\n","import {ToolRegistry} from \"./ToolRegistry\";\n\n// ---------------------------------------------------------------------------\n// ToolChain step definition\n// ---------------------------------------------------------------------------\n\nexport interface ToolChainStep {\n /** Name of the tool/function to call */\n toolName: string;\n /** Input template string; use {variableName} to reference context variables */\n inputTemplate: string;\n /** Key under which the step output is stored in context */\n outputKey: string;\n}\n\n// ---------------------------------------------------------------------------\n// ToolChain\n// ---------------------------------------------------------------------------\n\n/**\n * A sequential chain of tool steps.\n * Each step can reference outputs of prior steps via `{key}` placeholders.\n *\n * ```ts\n * const chain = new ToolChain(\"my_chain\", \"Does X then Y\");\n * chain.addStep(\"search\", \"{input}\", \"search_result\");\n * chain.addStep(\"summarize\", \"{search_result}\", \"summary\");\n * ```\n */\nexport class ToolChain {\n readonly name: string;\n readonly description: string;\n private readonly steps: ToolChainStep[] = [];\n\n constructor(name: string, description: string) {\n this.name = name;\n this.description = description;\n }\n\n addStep(toolName: string, inputTemplate: string, outputKey: string): this {\n this.steps.push({toolName, inputTemplate, outputKey});\n return this;\n }\n\n getSteps(): ToolChainStep[] {\n return [...this.steps];\n }\n\n /**\n * Execute the chain against a registry.\n * @param registry ToolRegistry that holds the referenced tools.\n * @param input Initial `{input}` value.\n * @returns The value stored under the last step's outputKey.\n */\n async execute(registry: ToolRegistry, input: string): Promise<string> {\n const context: Record<string, string> = {input};\n\n for (const step of this.steps) {\n const resolvedInput = interpolate(step.inputTemplate, context);\n const result = await registry.execute(step.toolName, {input: resolvedInput});\n context[step.outputKey] = result;\n }\n\n const lastStep = this.steps[this.steps.length - 1];\n if (!lastStep) return input;\n return context[lastStep.outputKey] ?? input;\n }\n}\n\n// ---------------------------------------------------------------------------\n// ToolChainManager\n// ---------------------------------------------------------------------------\n\n/**\n * Manages multiple named ToolChains and executes them against a shared registry.\n */\nexport class ToolChainManager {\n private readonly chains = new Map<string, ToolChain>();\n private readonly registry: ToolRegistry;\n\n constructor(registry: ToolRegistry) {\n this.registry = registry;\n }\n\n registerChain(chain: ToolChain): void {\n this.chains.set(chain.name, chain);\n }\n\n getChain(name: string): ToolChain | undefined {\n return this.chains.get(name);\n }\n\n listChains(): string[] {\n return Array.from(this.chains.keys());\n }\n\n async executeChain(chainName: string, input: string): Promise<string> {\n const chain = this.chains.get(chainName);\n if (!chain) throw new Error(`ToolChain not found: ${chainName}`);\n return chain.execute(this.registry, input);\n }\n}\n\n// ---------------------------------------------------------------------------\n// Helpers\n// ---------------------------------------------------------------------------\n\n/** Replace `{key}` placeholders with values from context. */\nfunction interpolate(template: string, context: Record<string, string>): string {\n return template.replace(/\\{(\\w+)\\}/g, (_match, key: string) => {\n return context[key] ?? `{${key}}`;\n });\n}\n","import {Tool, type FunctionTool, type OpenAIFunctionSchema} from \"./Tool\";\n\n/**\n * Central registry that manages Tool instances and raw FunctionTools.\n * Supports registration, lookup, and execution.\n */\nexport class ToolRegistry {\n private readonly tools = new Map<string, Tool>();\n private readonly functions = new Map<string, FunctionTool<Record<string, unknown>>>();\n\n // ---------------------------------------------------------------------------\n // Registration\n // ---------------------------------------------------------------------------\n\n registerTool(tool: Tool): void {\n this.tools.set(tool.name, tool);\n }\n\n unregisterTool(name: string): boolean {\n return this.tools.delete(name);\n }\n\n registerFunction<TArgs extends Record<string, unknown>>(\n name: string,\n description: string,\n func: (args: TArgs) => string | Promise<string>,\n schema?: FunctionTool<TArgs>[\"schema\"],\n ): void {\n this.functions.set(name, {\n name,\n description,\n func: func as FunctionTool<Record<string, unknown>>[\"func\"],\n schema: schema as FunctionTool<Record<string, unknown>>[\"schema\"],\n });\n }\n\n unregisterFunction(name: string): boolean {\n return this.functions.delete(name);\n }\n\n // ---------------------------------------------------------------------------\n // Lookup\n // ---------------------------------------------------------------------------\n\n getTool(name: string): Tool | undefined {\n return this.tools.get(name);\n }\n\n getFunction(name: string): FunctionTool<Record<string, unknown>> | undefined {\n return this.functions.get(name);\n }\n\n getAllTools(): Tool[] {\n return Array.from(this.tools.values());\n }\n\n listTools(): string[] {\n return [\n ...Array.from(this.tools.keys()),\n ...Array.from(this.functions.keys()),\n ];\n }\n\n hasTool(name: string): boolean {\n return this.tools.has(name) || this.functions.has(name);\n }\n\n // ---------------------------------------------------------------------------\n // Execution\n // ---------------------------------------------------------------------------\n\n async execute(name: string, parameters: Record<string, unknown>): Promise<string> {\n const tool = this.tools.get(name);\n if (tool) {\n return await tool.run(parameters);\n }\n\n const fn = this.functions.get(name);\n if (fn) {\n return await fn.func(parameters);\n }\n\n throw new Error(`Tool not found: ${name}`);\n }\n\n // ---------------------------------------------------------------------------\n // Description (for system prompts)\n // ---------------------------------------------------------------------------\n\n getAvailableTools(): string {\n const lines: string[] = [];\n\n for (const tool of this.tools.values()) {\n lines.push(tool.describe());\n }\n\n for (const fn of this.functions.values()) {\n lines.push(`Tool: ${fn.name}\\nDescription: ${fn.description}`);\n }\n\n if (lines.length === 0) return \"暂无可用工具\";\n return lines.join(\"\\n\\n\");\n }\n\n getOpenAISchemas(): Array<OpenAIFunctionSchema | Record<string, unknown>> {\n const schemas: Array<OpenAIFunctionSchema | Record<string, unknown>> = [];\n\n for (const tool of this.tools.values()) {\n schemas.push(tool.toOpenAISchema());\n }\n\n for (const fn of this.functions.values()) {\n schemas.push({\n type: \"function\",\n function: {\n name: fn.name,\n description: fn.description,\n parameters: {\n type: \"object\",\n properties: {\n input: {type: \"string\", description: \"输入文本\"},\n },\n },\n },\n });\n }\n\n return schemas;\n }\n}\n","import {ToolRegistry} from \"./ToolRegistry\";\n\nexport interface ToolCallRequest {\n id: string;\n toolName: string;\n parameters: Record<string, unknown>;\n}\n\nexport interface ToolCallResult {\n id: string;\n toolName: string;\n output: string;\n error?: string;\n durationMs: number;\n}\n\n/**\n * Executes multiple tool calls concurrently and collects results.\n */\nexport class AsyncToolExecutor {\n private readonly registry: ToolRegistry;\n private readonly concurrency: number;\n\n constructor(registry: ToolRegistry, concurrency = 4) {\n this.registry = registry;\n this.concurrency = Math.max(1, concurrency);\n }\n\n /**\n * Execute a batch of tool calls with bounded concurrency.\n */\n async executeBatch(requests: ToolCallRequest[]): Promise<ToolCallResult[]> {\n const results: ToolCallResult[] = [];\n let idx = 0;\n\n const worker = async (): Promise<void> => {\n while (idx < requests.length) {\n const req = requests[idx++]!;\n results.push(await this.executeSingle(req));\n }\n };\n\n const workers = Array.from({length: Math.min(this.concurrency, requests.length)}, worker);\n await Promise.all(workers);\n return results;\n }\n\n /**\n * Execute a single tool call.\n */\n async executeSingle(request: ToolCallRequest): Promise<ToolCallResult> {\n const start = Date.now();\n try {\n const output = await this.registry.execute(request.toolName, request.parameters);\n return {\n id: request.id,\n toolName: request.toolName,\n output,\n durationMs: Date.now() - start,\n };\n } catch (error) {\n return {\n id: request.id,\n toolName: request.toolName,\n output: \"\",\n error: error instanceof Error ? error.message : String(error),\n durationMs: Date.now() - start,\n };\n }\n }\n}\n"],"names":["__publicField"],"mappings":";;;;;;AAuBA,MAAM,qBAAA,0BAA+B,cAAc,CAAA;AAY5C,SAAS,UAAA,CAAW,KAAa,WAAA,EAAqB;AAC3D,EAAA,OAAO,SACL,MAAA,EACA,WAAA,EACA,WAAA,EACM;AACN,IAAA,MAAM,WACJ,OAAA,CAA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A,EAAI,GAAI;AAAA,OAC3B;AAAA,IACF;AAAA,EACF;AACF;;;;"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { ToolRegistry } from "./ToolRegistry";
|
|
2
|
+
export interface ToolCallRequest {
|
|
3
|
+
id: string;
|
|
4
|
+
toolName: string;
|
|
5
|
+
parameters: Record<string, unknown>;
|
|
6
|
+
}
|
|
7
|
+
export interface ToolCallResult {
|
|
8
|
+
id: string;
|
|
9
|
+
toolName: string;
|
|
10
|
+
output: string;
|
|
11
|
+
error?: string;
|
|
12
|
+
durationMs: number;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Executes multiple tool calls concurrently and collects results.
|
|
16
|
+
*/
|
|
17
|
+
export declare class AsyncToolExecutor {
|
|
18
|
+
private readonly registry;
|
|
19
|
+
private readonly concurrency;
|
|
20
|
+
constructor(registry: ToolRegistry, concurrency?: number);
|
|
21
|
+
/**
|
|
22
|
+
* Execute a batch of tool calls with bounded concurrency.
|
|
23
|
+
*/
|
|
24
|
+
executeBatch(requests: ToolCallRequest[]): Promise<ToolCallResult[]>;
|
|
25
|
+
/**
|
|
26
|
+
* Execute a single tool call.
|
|
27
|
+
*/
|
|
28
|
+
executeSingle(request: ToolCallRequest): Promise<ToolCallResult>;
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=AsyncToolExecutor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AsyncToolExecutor.d.ts","sourceRoot":"","sources":["../../src/AsyncToolExecutor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,YAAY,EAAC,MAAM,gBAAgB,CAAC;AAE5C,MAAM,WAAW,eAAe;IAC9B,EAAE,EAAE,MAAM,CAAC;IACX,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACrC;AAED,MAAM,WAAW,cAAc;IAC7B,EAAE,EAAE,MAAM,CAAC;IACX,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,MAAM,CAAC;CACpB;AAED;;GAEG;AACH,qBAAa,iBAAiB;IAC5B,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAe;IACxC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAS;gBAEzB,QAAQ,EAAE,YAAY,EAAE,WAAW,SAAI;IAKnD;;OAEG;IACG,YAAY,CAAC,QAAQ,EAAE,eAAe,EAAE,GAAG,OAAO,CAAC,cAAc,EAAE,CAAC;IAgB1E;;OAEG;IACG,aAAa,CAAC,OAAO,EAAE,eAAe,GAAG,OAAO,CAAC,cAAc,CAAC;CAoBvE"}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
import { z, type ZodType } from "zod";
|
|
3
|
+
import type { ToolParameter } from "./types";
|
|
4
|
+
export type { ToolParameter };
|
|
5
|
+
export interface OpenAIFunctionSchema {
|
|
6
|
+
type: "function";
|
|
7
|
+
function: {
|
|
8
|
+
name: string;
|
|
9
|
+
description: string;
|
|
10
|
+
parameters: Record<string, unknown>;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
export interface ToolActionMeta {
|
|
14
|
+
key: string;
|
|
15
|
+
description: string;
|
|
16
|
+
method: string;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Decorator that registers a method as a named tool action.
|
|
20
|
+
* Usage: @toolAction("action_key", "Description")
|
|
21
|
+
*/
|
|
22
|
+
export declare function toolAction(key: string, description: string): (target: object, propertyKey: string, _descriptor: PropertyDescriptor) => void;
|
|
23
|
+
export declare abstract class Tool {
|
|
24
|
+
readonly name: string;
|
|
25
|
+
readonly description: string;
|
|
26
|
+
readonly expandable: boolean;
|
|
27
|
+
constructor(name: string, description: string, expandable?: boolean);
|
|
28
|
+
abstract run(parameters: Record<string, unknown>): Promise<string> | string;
|
|
29
|
+
abstract getParameters(): ToolParameter[];
|
|
30
|
+
/**
|
|
31
|
+
* Validate that required parameters are present and have basic types.
|
|
32
|
+
* Returns true if valid.
|
|
33
|
+
*/
|
|
34
|
+
validateParameters(parameters: Record<string, unknown>): boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Validate and coerce parameters, returning a Result object.
|
|
37
|
+
*/
|
|
38
|
+
validateAndNormalizeParameters(parameters: Record<string, unknown>): {
|
|
39
|
+
success: true;
|
|
40
|
+
data: Record<string, unknown>;
|
|
41
|
+
} | {
|
|
42
|
+
success: false;
|
|
43
|
+
error: string;
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Convert this Tool to an OpenAI function-calling schema.
|
|
47
|
+
*/
|
|
48
|
+
toOpenAISchema(): OpenAIFunctionSchema;
|
|
49
|
+
/**
|
|
50
|
+
* Return a formatted description of all available tool actions (for system prompt).
|
|
51
|
+
*/
|
|
52
|
+
describe(): string;
|
|
53
|
+
}
|
|
54
|
+
export interface FunctionTool<TArgs = Record<string, unknown>> {
|
|
55
|
+
name: string;
|
|
56
|
+
description: string;
|
|
57
|
+
func: (args: TArgs) => string | Promise<string>;
|
|
58
|
+
schema?: ZodType<TArgs>;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Convenience factory for creating type-safe function tools.
|
|
62
|
+
*
|
|
63
|
+
* ```ts
|
|
64
|
+
* const myTool = defineFunctionTool({
|
|
65
|
+
* name: "myTool",
|
|
66
|
+
* description: "...",
|
|
67
|
+
* schema: z.object({ input: z.string() }),
|
|
68
|
+
* func: ({ input }) => input.toUpperCase(),
|
|
69
|
+
* });
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
export declare function defineFunctionTool<TArgs extends Record<string, unknown>>(options: FunctionTool<TArgs>): FunctionTool<TArgs>;
|
|
73
|
+
export { z };
|
|
74
|
+
//# sourceMappingURL=Tool.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"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OAEG;IACH,8BAA8B,CAC5B,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAClC;QAAC,OAAO,EAAE,IAAI,CAAC;QAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;KAAC,GAAG;QAAC,OAAO,EAAE,KAAK,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAC;IAwBnF;;OAEG;IACH,cAAc,IAAI,oBAAoB;IA8BtC;;OAEG;IACH,QAAQ,IAAI,MAAM;CAUnB;AAcD,MAAM,WAAW,YAAY,CAAC,KAAK,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAC3D,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,IAAI,EAAE,CAAC,IAAI,EAAE,KAAK,KAAK,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAChD,MAAM,CAAC,EAAE,OAAO,CAAC,KAAK,CAAC,CAAC;CACzB;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACtE,OAAO,EAAE,YAAY,CAAC,KAAK,CAAC,GAC3B,YAAY,CAAC,KAAK,CAAC,CAErB;AAED,OAAO,EAAC,CAAC,EAAC,CAAC"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { ToolRegistry } from "./ToolRegistry";
|
|
2
|
+
export interface ToolChainStep {
|
|
3
|
+
/** Name of the tool/function to call */
|
|
4
|
+
toolName: string;
|
|
5
|
+
/** Input template string; use {variableName} to reference context variables */
|
|
6
|
+
inputTemplate: string;
|
|
7
|
+
/** Key under which the step output is stored in context */
|
|
8
|
+
outputKey: string;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* A sequential chain of tool steps.
|
|
12
|
+
* Each step can reference outputs of prior steps via `{key}` placeholders.
|
|
13
|
+
*
|
|
14
|
+
* ```ts
|
|
15
|
+
* const chain = new ToolChain("my_chain", "Does X then Y");
|
|
16
|
+
* chain.addStep("search", "{input}", "search_result");
|
|
17
|
+
* chain.addStep("summarize", "{search_result}", "summary");
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export declare class ToolChain {
|
|
21
|
+
readonly name: string;
|
|
22
|
+
readonly description: string;
|
|
23
|
+
private readonly steps;
|
|
24
|
+
constructor(name: string, description: string);
|
|
25
|
+
addStep(toolName: string, inputTemplate: string, outputKey: string): this;
|
|
26
|
+
getSteps(): ToolChainStep[];
|
|
27
|
+
/**
|
|
28
|
+
* Execute the chain against a registry.
|
|
29
|
+
* @param registry ToolRegistry that holds the referenced tools.
|
|
30
|
+
* @param input Initial `{input}` value.
|
|
31
|
+
* @returns The value stored under the last step's outputKey.
|
|
32
|
+
*/
|
|
33
|
+
execute(registry: ToolRegistry, input: string): Promise<string>;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Manages multiple named ToolChains and executes them against a shared registry.
|
|
37
|
+
*/
|
|
38
|
+
export declare class ToolChainManager {
|
|
39
|
+
private readonly chains;
|
|
40
|
+
private readonly registry;
|
|
41
|
+
constructor(registry: ToolRegistry);
|
|
42
|
+
registerChain(chain: ToolChain): void;
|
|
43
|
+
getChain(name: string): ToolChain | undefined;
|
|
44
|
+
listChains(): string[];
|
|
45
|
+
executeChain(chainName: string, input: string): Promise<string>;
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=ToolChain.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ToolChain.d.ts","sourceRoot":"","sources":["../../src/ToolChain.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,YAAY,EAAC,MAAM,gBAAgB,CAAC;AAM5C,MAAM,WAAW,aAAa;IAC5B,wCAAwC;IACxC,QAAQ,EAAE,MAAM,CAAC;IACjB,+EAA+E;IAC/E,aAAa,EAAE,MAAM,CAAC;IACtB,2DAA2D;IAC3D,SAAS,EAAE,MAAM,CAAC;CACnB;AAMD;;;;;;;;;GASG;AACH,qBAAa,SAAS;IACpB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAuB;gBAEjC,IAAI,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM;IAK7C,OAAO,CAAC,QAAQ,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,IAAI;IAKzE,QAAQ,IAAI,aAAa,EAAE;IAI3B;;;;;OAKG;IACG,OAAO,CAAC,QAAQ,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;CAatE;AAMD;;GAEG;AACH,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAgC;IACvD,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAe;gBAE5B,QAAQ,EAAE,YAAY;IAIlC,aAAa,CAAC,KAAK,EAAE,SAAS,GAAG,IAAI;IAIrC,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,GAAG,SAAS;IAI7C,UAAU,IAAI,MAAM,EAAE;IAIhB,YAAY,CAAC,SAAS,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;CAKtE"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Tool, type FunctionTool, type OpenAIFunctionSchema } from "./Tool";
|
|
2
|
+
/**
|
|
3
|
+
* Central registry that manages Tool instances and raw FunctionTools.
|
|
4
|
+
* Supports registration, lookup, and execution.
|
|
5
|
+
*/
|
|
6
|
+
export declare class ToolRegistry {
|
|
7
|
+
private readonly tools;
|
|
8
|
+
private readonly functions;
|
|
9
|
+
registerTool(tool: Tool): void;
|
|
10
|
+
unregisterTool(name: string): boolean;
|
|
11
|
+
registerFunction<TArgs extends Record<string, unknown>>(name: string, description: string, func: (args: TArgs) => string | Promise<string>, schema?: FunctionTool<TArgs>["schema"]): void;
|
|
12
|
+
unregisterFunction(name: string): boolean;
|
|
13
|
+
getTool(name: string): Tool | undefined;
|
|
14
|
+
getFunction(name: string): FunctionTool<Record<string, unknown>> | undefined;
|
|
15
|
+
getAllTools(): Tool[];
|
|
16
|
+
listTools(): string[];
|
|
17
|
+
hasTool(name: string): boolean;
|
|
18
|
+
execute(name: string, parameters: Record<string, unknown>): Promise<string>;
|
|
19
|
+
getAvailableTools(): string;
|
|
20
|
+
getOpenAISchemas(): Array<OpenAIFunctionSchema | Record<string, unknown>>;
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=ToolRegistry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ToolRegistry.d.ts","sourceRoot":"","sources":["../../src/ToolRegistry.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,IAAI,EAAE,KAAK,YAAY,EAAE,KAAK,oBAAoB,EAAC,MAAM,QAAQ,CAAC;AAE1E;;;GAGG;AACH,qBAAa,YAAY;IACvB,OAAO,CAAC,QAAQ,CAAC,KAAK,CAA2B;IACjD,OAAO,CAAC,QAAQ,CAAC,SAAS,CAA4D;IAMtF,YAAY,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI;IAI9B,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAIrC,gBAAgB,CAAC,KAAK,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACpD,IAAI,EAAE,MAAM,EACZ,WAAW,EAAE,MAAM,EACnB,IAAI,EAAE,CAAC,IAAI,EAAE,KAAK,KAAK,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,EAC/C,MAAM,CAAC,EAAE,YAAY,CAAC,KAAK,CAAC,CAAC,QAAQ,CAAC,GACrC,IAAI;IASP,kBAAkB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAQzC,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS;IAIvC,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,YAAY,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,GAAG,SAAS;IAI5E,WAAW,IAAI,IAAI,EAAE;IAIrB,SAAS,IAAI,MAAM,EAAE;IAOrB,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAQxB,OAAO,CAAC,IAAI,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC;IAkBjF,iBAAiB,IAAI,MAAM;IAe3B,gBAAgB,IAAI,KAAK,CAAC,oBAAoB,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAyB1E"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAC;AACvB,cAAc,aAAa,CAAC;AAC5B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,qBAAqB,CAAC;AACpC,cAAc,SAAS,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export interface ToolParameter {
|
|
2
|
+
name: string;
|
|
3
|
+
type: string;
|
|
4
|
+
description: string;
|
|
5
|
+
required: boolean;
|
|
6
|
+
default: unknown;
|
|
7
|
+
}
|
|
8
|
+
export interface ToolResult {
|
|
9
|
+
success: boolean;
|
|
10
|
+
output: string;
|
|
11
|
+
error?: string;
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,QAAQ,EAAE,OAAO,CAAC;IAClB,OAAO,EAAE,OAAO,CAAC;CAClB;AAED,MAAM,WAAW,UAAU;IACzB,OAAO,EAAE,OAAO,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB"}
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@agenticforge/tools",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Tooling core for AgenticKIT",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"main": "./dist/cjs/index.cjs",
|
|
8
|
+
"module": "./dist/esm/index.js",
|
|
9
|
+
"types": "./dist/types/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/types/index.d.ts",
|
|
13
|
+
"import": "./dist/esm/index.js",
|
|
14
|
+
"require": "./dist/cjs/index.cjs"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"clean": "rimraf dist",
|
|
22
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
23
|
+
"build:types": "tsc -p tsconfig.build.json",
|
|
24
|
+
"build:js": "rollup -c",
|
|
25
|
+
"build": "pnpm run clean && pnpm run typecheck && pnpm run build:types && pnpm run build:js"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"reflect-metadata": "^0.2.2",
|
|
29
|
+
"zod": "^4.3.6"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@rollup/plugin-commonjs": "^28.0.8",
|
|
33
|
+
"@rollup/plugin-json": "^6.1.0",
|
|
34
|
+
"@rollup/plugin-node-resolve": "^16.0.1",
|
|
35
|
+
"@rollup/plugin-terser": "^0.4.4",
|
|
36
|
+
"@types/node": "^25.3.3",
|
|
37
|
+
"rimraf": "^6.0.1",
|
|
38
|
+
"rollup": "^4.50.1",
|
|
39
|
+
"rollup-plugin-esbuild": "^6.2.1",
|
|
40
|
+
"tsx": "^4.21.0",
|
|
41
|
+
"typescript": "~5.9.3"
|
|
42
|
+
}
|
|
43
|
+
}
|