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.
82 lines (81 loc) • 3.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
// @ts-nocheck
const delegation_guard_1 = require("./delegation.guard");
const agent_1 = require("./agent");
const task_1 = require("../tasks/task");
jest.mock("openai", () => {
const mockOpenAI = jest.fn().mockImplementation(() => ({
chat: {
completions: {
create: jest
.fn()
.mockResolvedValue({ choices: [{ message: { content: "{}" } }] }),
},
},
}));
return {
__esModule: true,
default: mockOpenAI,
OpenAI: mockOpenAI,
};
});
describe("delegation.guard", () => {
let agent, task;
beforeEach(() => {
agent = new agent_1.Agent({
name: "Alice",
role: "Delegator",
goal: "Delegate things",
model: "gpt-4o-mini",
backstory: "",
});
task = new task_1.Task({
id: "t1",
name: "Do something",
description: "desc",
agent: "Alice",
outputFormat: "text",
});
});
it("returns canDelegate true if chain is empty", () => {
expect((0, delegation_guard_1.checkDelegationValidity)(agent, task)).toEqual({ canDelegate: true });
});
it("detects delegation cycle", () => {
task.executionContext.delegationChain = ["Alice"];
expect((0, delegation_guard_1.checkDelegationValidity)(agent, task)).toEqual({
canDelegate: false,
reason: "cycle_detected: Alice already in delegationChain",
});
});
it("detects max delegation hops exceeded", () => {
task.executionContext.delegationChain = ["A", "B", "C", "D", "E"]; // length 5
expect((0, delegation_guard_1.checkDelegationValidity)(agent, task)).toEqual({
canDelegate: false,
reason: "max_delegation_hops_exceeded (5)",
});
});
it("updates delegationChain on task", () => {
(0, delegation_guard_1.updateDelegationChain)(task, agent);
expect(task.executionContext.delegationChain).toContain("Alice");
});
describe("checkDelegationScore", () => {
it("returns weak if only DELEGATE present, short", () => {
const res = (0, delegation_guard_1.checkDelegationScore)('DELEGATE(Bob, "task")');
expect(res.isWeak).toBe(true);
expect(res.reason).toContain("only delegation");
expect(res.score).toBe(3);
});
it("returns weak if DELEGATE present but lacks explanation", () => {
const res = (0, delegation_guard_1.checkDelegationScore)('This is something. DELEGATE(Bob, "x")');
expect(res.isWeak).toBe(true);
expect(res.reason).toContain("lacks reasoning");
expect(res.score).toBe(5);
});
it("returns not weak for normal delegation output", () => {
const res = (0, delegation_guard_1.checkDelegationScore)('I cannot complete this, therefore DELEGATE(Bob, "task") because Bob is the expert.');
expect(res.isWeak).toBe(false);
expect(res.score).toBe(10);
});
});
});