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.
224 lines (223 loc) • 8.29 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
// @ts-nocheck
const agent_1 = require("../agents/agent");
const enum_1 = require("../configs/enum");
const task_1 = require("../tasks/task");
const taskForce_1 = require("./taskForce");
const delegationGuard = __importStar(require("../agents/delegation.guard"));
jest.mock("openai", () => {
const mockOpenAI = jest.fn().mockImplementation(() => ({
chat: {
completions: {
create: jest
.fn()
.mockResolvedValue({ choices: [{ message: { content: "{}" } }] }),
},
},
}));
return {
__esModule: true,
default: mockOpenAI,
OpenAI: mockOpenAI,
};
});
jest.mock("../agents/smartManagerAgent", () => {
return {
SmartManagerAgent: jest.fn().mockImplementation(() => ({
setVerbose: jest.fn(),
planTasks: jest.fn((tasks) => tasks),
decomposeTask: jest.fn(),
assignAgent: jest.fn(),
evaluateTaskOutput: jest.fn(() => ({ action: "accept" })),
reviewFinalOutput: jest.fn(() => ({ action: "accept" })),
})),
};
});
jest.mock("../agents/smartManagerAgent", () => {
return {
SmartManagerAgent: jest.fn().mockImplementation(() => ({
setVerbose: jest.fn(),
planTasks: jest.fn((tasks) => tasks),
decomposeTask: jest.fn((mainTask, agents, verbose) => [mainTask]),
assignAgent: jest.fn(),
evaluateTaskOutput: jest.fn((task, finalOutput) => {
if (finalOutput.startsWith("DELEGATE")) {
return { action: "accept" };
}
return { action: "accept" };
}),
reviewFinalOutput: jest.fn(() => ({ action: "accept" })),
})),
};
});
class DummyAgent extends agent_1.Agent {
async runTask(...args) {
return "Dummy Output";
}
}
const dummyTask = new task_1.Task({
id: "t1",
name: "Test Task",
description: "Test the TaskForce run",
agent: "Dummy Agent",
outputFormat: "text",
});
describe("TaskForce", () => {
it("should run a single dummy task sequentially", async () => {
const dummyAgent = new DummyAgent({
name: "Dummy Agent",
role: "Test",
goal: "Testing",
model: "gpt-4o-mini",
backstory: "This is a dummy agent for testing.",
});
const tf = new taskForce_1.TaskForce({
agents: [dummyAgent],
tasks: [dummyTask],
executionMode: enum_1.ExecutionMode.Sequential,
verbose: false,
});
const { result, executedTaskIds } = await tf.run({ input: "test" });
expect(result).toEqual({ t1: "Dummy Output" });
expect(executedTaskIds).toEqual(["t1"]);
});
it("should trigger weak delegation score and retry", async () => {
const runTaskMock = jest
.fn()
.mockResolvedValueOnce('DELEGATE(X, "reason")')
.mockResolvedValueOnce("Delegation handled.");
class WeakDelegationAgent extends agent_1.Agent {
async runTask() {
return runTaskMock();
}
}
jest
.spyOn(delegationGuard, "checkDelegationScore")
.mockImplementation((output) => {
if (output.startsWith("DELEGATE")) {
return { isWeak: true, reason: "Weak delegation", score: 3 };
}
return { isWeak: false, score: 10 };
});
const weakAgent = new WeakDelegationAgent({
name: "WeakAgent",
role: "Test",
goal: "Testing",
model: "gpt-4o-mini",
backstory: "Weak delegation agent.",
});
const task = new task_1.Task({
id: "tweak",
name: "Test Weak Delegation",
description: "Test weak delegation scoring.",
agent: "WeakAgent",
outputFormat: "text",
});
const tf = new taskForce_1.TaskForce({
agents: [weakAgent],
tasks: [task],
executionMode: enum_1.ExecutionMode.Hierarchical,
verbose: false,
});
tf.managerAgent.evaluateTaskOutput = jest.fn((task, finalOutput) => {
if (finalOutput.startsWith("DELEGATE")) {
return { action: "accept", forceAccept: true };
}
return { action: "accept" };
});
const { result } = await tf.run({});
expect(result.tweak).toBe("Delegation handled.");
expect(runTaskMock).toHaveBeenCalledTimes(2);
});
it("should return correct agent by name", () => {
const dummyAgent = new DummyAgent({
name: "Dummy Agent",
role: "Test",
goal: "Testing",
model: "gpt-4o-mini",
backstory: "This is a dummy agent for testing.",
});
const tf = new taskForce_1.TaskForce({
agents: [dummyAgent],
tasks: [dummyTask],
executionMode: enum_1.ExecutionMode.Sequential,
verbose: false,
});
expect(tf.getAgentByName("Dummy Agent")).toBe(dummyAgent);
expect(tf.getAgentByName("Y")).toBeUndefined();
});
it("should list all agent names", () => {
const agents = [
new DummyAgent({
name: "Dummy Agent 1",
role: "Test",
goal: "Testing",
model: "gpt-4o-mini",
backstory: "This is a dummy agent for testing.",
}),
new DummyAgent({
name: "Dummy Agent 2",
role: "Test",
goal: "Testing",
model: "gpt-4o-mini",
backstory: "This is a dummy agent for testing.",
}),
];
const tf = new taskForce_1.TaskForce({
agents: agents,
tasks: [dummyTask],
executionMode: enum_1.ExecutionMode.Sequential,
verbose: false,
});
expect(tf.listAgentNames()).toEqual(["Dummy Agent 1", "Dummy Agent 2"]);
});
it("should throw error if agent not found", async () => {
const tf = new taskForce_1.TaskForce({
agents: [],
tasks: [dummyTask],
executionMode: enum_1.ExecutionMode.Sequential,
});
await expect(tf.run({ input: "test" })).resolves.toBeDefined();
// veya throw etmesini bekliyorsan .rejects.toThrow
});
it("should replan and then stop at replan limit", async () => {
// context __replanCount__ >= maxGlobalReplanLimit olduğunda tekrar replan yapmamalı
});
it("should use tool handler if TOOL() pattern present in output", async () => {
// ToolExecutor ve tool mock’ları ile
});
});