tinyagent-ts
Version:
Modern TypeScript framework for building AI agents with pluggable tools and ReAct reasoning
123 lines • 6.2 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
const zod_1 = require("zod");
const agent_1 = require("../src/agent");
const decorators_1 = require("../src/decorators");
describe('final_answer enforcement', () => {
beforeAll(() => {
var _a;
(_a = process.env).OPENROUTER_API_KEY || (_a.OPENROUTER_API_KEY = 'test');
});
let CalcAgent = class CalcAgent extends agent_1.Agent {
add({ a, b }) {
return String(a + b);
}
};
__decorate([
(0, decorators_1.tool)('Add numbers', zod_1.z.object({ a: zod_1.z.number(), b: zod_1.z.number() })),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", void 0)
], CalcAgent.prototype, "add", null);
CalcAgent = __decorate([
(0, decorators_1.model)('test-model')
], CalcAgent);
it('returns final answer when model calls final_answer', async () => {
const agent = new CalcAgent();
const responses = [
{ choices: [{ message: { content: '{"tool":"add","args":{"a":1,"b":2}}' } }] },
{ choices: [{ message: { content: '{"tool":"final_answer","args":{"answer":"3"}}' } }] },
];
jest
.spyOn(agent, 'makeOpenRouterRequest')
.mockImplementation(async () => responses.shift());
const out = await agent.run('Add 1 and 2');
expect(out).toEqual({ answer: '3' });
});
it('continues after unknown tool and returns final answer', async () => {
const agent = new CalcAgent();
const responses = [
{ choices: [{ message: { content: '{"tool":"add","args":{"a":1,"b":2}}' } }] },
{ choices: [{ message: { content: '{"tool":"FINISH","args":{"answer":"3"}}' } }] },
{ choices: [{ message: { content: '{"tool":"final_answer","args":{"answer":"3"}}' } }] },
];
jest
.spyOn(agent, 'makeOpenRouterRequest')
.mockImplementation(async () => responses.shift());
const out = await agent.run('Add 1 and 2');
expect(out).toEqual({ answer: '3' });
});
it('stops after two unknown tool calls', async () => {
const agent = new CalcAgent();
const responses = [
{ choices: [{ message: { content: '{"tool":"bogus","args":{}}' } }] },
{ choices: [{ message: { content: '{"tool":"none","args":{}}' } }] },
{ choices: [{ message: { content: '{"tool":"final_answer","args":{"answer":"ok"}}' } }] },
];
jest
.spyOn(agent, 'makeOpenRouterRequest')
.mockImplementation(async () => responses.shift());
const out = await agent.run('Add 1 and 2');
expect(out.answer).toContain('too many bad tool calls');
});
it('stops after two invalid tool calls', async () => {
const agent = new CalcAgent();
const responses = [
{ choices: [{ message: { content: '{"tool":"add","args":{"a":1}}' } }] },
{ choices: [{ message: { content: '{"tool":"add","args":{"a":2}}' } }] },
{ choices: [{ message: { content: '{"tool":"final_answer","args":{"answer":"ok"}}' } }] },
];
jest.spyOn(agent, 'makeOpenRouterRequest').mockImplementation(async () => responses.shift());
const out = await agent.run('Add 1 and 2');
expect(out.answer).toContain('too many bad tool calls');
});
it('handles malformed final_answer JSON', async () => {
const agent = new CalcAgent();
const bad = { choices: [{ message: { content: '{"tool":"final_answer"}' } }] };
const goodObj = { tool: 'final_answer', args: { answer: 'fixed' } };
jest
.spyOn(agent, 'makeOpenRouterRequest')
.mockImplementationOnce(async () => bad)
.mockImplementationOnce(async () => ({ choices: [{ message: { content: JSON.stringify(goodObj) } }] }));
jest.spyOn(agent, 'retryWithFixRequest').mockResolvedValue({ fixed: JSON.stringify(goodObj), fixedParsed: goodObj });
const out = await agent.run('Add');
expect(out).toEqual({ answer: 'fixed' });
});
it('truncates long observations', async () => {
let BigAgent = class BigAgent extends agent_1.Agent {
big() {
return 'x'.repeat(4000);
}
};
__decorate([
(0, decorators_1.tool)('big', zod_1.z.object({})),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], BigAgent.prototype, "big", null);
BigAgent = __decorate([
(0, decorators_1.model)('test-model')
], BigAgent);
const agent = new BigAgent();
const responses = [
{ choices: [{ message: { content: '{"tool":"big","args":{}}' } }] },
{ choices: [{ message: { content: '{"tool":"final_answer","args":{"answer":"done"}}' } }] },
];
jest.spyOn(agent, 'makeOpenRouterRequest').mockImplementation(async () => responses.shift());
const out = await agent.run('do big');
const mem = agent.memory;
const last = mem[mem.length - 2]?.content || '';
expect(last).toContain('<too large to show>');
expect(out).toEqual({ answer: 'done' });
});
});
//# sourceMappingURL=agent.final-answer.test.js.map