UNPKG

taskforce-aiagent

Version:

TaskForce is a modular, open-source, production-ready TypeScript agent framework for orchestrating AI agents, LLM-powered autonomous agents, task pipelines, dynamic toolchains, RAG workflows and memory/retrieval systems.

122 lines (121 loc) 5.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); // @ts-nocheck const baseTool_1 = require("./baseTool"); class DummyTool extends baseTool_1.Tool { constructor() { super(...arguments); this.id = "dummy-tool"; this.name = "Dummy Tool"; this.description = "A dummy tool for testing."; this.inputSchema = { type: "string", required: true }; } async handler(input) { return "dummy output"; } } const toolRegistry_1 = require("./toolRegistry"); describe("ToolRegistry", () => { let tool; beforeEach(() => { tool = new DummyTool(); // Clear all tools before each test to isolate tests for (const key of [ ...toolRegistry_1.ToolRegistry.getAll().map((t) => t.id), ...toolRegistry_1.ToolRegistry.getAll().map((t) => t.name), ]) { toolRegistry_1.ToolRegistry.unregister(key); } }); it("registers a tool by id and name", () => { toolRegistry_1.ToolRegistry.register(tool, "custom", "test-source"); const byId = toolRegistry_1.ToolRegistry.getByKey(tool.id); const byName = toolRegistry_1.ToolRegistry.getByKey(tool.name); expect(byId).toBeDefined(); expect(byId?.id).toBe(tool.id); expect(byId?.name).toBe(tool.name); expect(byId?.category).toBe("custom"); expect(byId?.source).toBe("test-source"); expect(byId?.instance).toBe(tool); expect(byName).toBeDefined(); expect(byName).toEqual(byId); }); it("does not register the same tool twice", () => { toolRegistry_1.ToolRegistry.register(tool); toolRegistry_1.ToolRegistry.register(tool); // second registration ignored const allTools = toolRegistry_1.ToolRegistry.getAll(); // Should contain exactly one unique tool (id based) expect(allTools.filter((t) => t.id === tool.id).length).toBe(1); }); it("unregister removes tool by id and name", () => { toolRegistry_1.ToolRegistry.register(tool); toolRegistry_1.ToolRegistry.unregister(tool.id); expect(toolRegistry_1.ToolRegistry.getByKey(tool.id)).toBeUndefined(); expect(toolRegistry_1.ToolRegistry.getByKey(tool.name)).toBeDefined(); toolRegistry_1.ToolRegistry.unregister(tool.name); expect(toolRegistry_1.ToolRegistry.getByKey(tool.name)).toBeUndefined(); }); it("getAll returns unique tools and filters by category and source", () => { const tool1 = new DummyTool(); tool1.id = "id1"; tool1.name = "name1"; tool1.category = "custom"; tool1.source = "source1"; const tool2 = new DummyTool(); tool2.id = "id2"; tool2.name = "name2"; tool2.category = "search"; tool2.source = "source2"; toolRegistry_1.ToolRegistry.register(tool1, tool1.category, tool1.source); toolRegistry_1.ToolRegistry.register(tool2, tool2.category, tool2.source); // No filter const allTools = toolRegistry_1.ToolRegistry.getAll(); expect(allTools.length).toBe(2); // Filter by category const customTools = toolRegistry_1.ToolRegistry.getAll({ category: "custom" }); expect(customTools.length).toBe(1); expect(customTools[0].id).toBe("id1"); // Filter by source const source2Tools = toolRegistry_1.ToolRegistry.getAll({ source: "source2" }); expect(source2Tools.length).toBe(1); expect(source2Tools[0].id).toBe("id2"); }); it("update replaces an existing tool", () => { toolRegistry_1.ToolRegistry.register(tool); const newTool = new DummyTool(); newTool.id = tool.id; newTool.name = tool.name; newTool.description = "Updated description"; toolRegistry_1.ToolRegistry.update(newTool, "updated-category", "updated-source"); const retrieved = toolRegistry_1.ToolRegistry.getByKey(tool.id); expect(retrieved?.instance.description).toBe("Updated description"); expect(retrieved?.category).toBe("updated-category"); expect(retrieved?.source).toBe("updated-source"); }); it("registerBatch registers multiple tools with default external source", () => { const tool1 = new DummyTool(); tool1.id = "batch1"; tool1.name = "Batch Tool 1"; const tool2 = new DummyTool(); tool2.id = "batch2"; tool2.name = "Batch Tool 2"; toolRegistry_1.ToolRegistry.registerBatch([tool1, tool2]); expect(toolRegistry_1.ToolRegistry.getByKey("batch1")?.source).toBe("external"); expect(toolRegistry_1.ToolRegistry.getByKey("batch2")?.source).toBe("external"); }); it("listToolSummaries returns summary info of all tools", () => { toolRegistry_1.ToolRegistry.register(tool, "custom", "test-source"); const summaries = toolRegistry_1.ToolRegistry.listToolSummaries(); expect(Array.isArray(summaries)).toBe(true); const summary = summaries.find((s) => s.id === tool.id); expect(summary).toMatchObject({ id: tool.id, name: tool.name, description: tool.description, category: "custom", source: "test-source", cacheable: tool.cacheable, }); expect(summary.examples).toBeUndefined(); }); });