UNPKG

tinyagent-ts

Version:

Modern TypeScript framework for building AI agents with pluggable tools and ReAct reasoning

129 lines โ€ข 5.34 kB
"use strict"; 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 __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 __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; }; })(); 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 }); exports.MathAgent = void 0; const dotenv = __importStar(require("dotenv")); dotenv.config(); const zod_1 = require("zod"); const decorators_1 = require("../src/decorators"); const agent_1 = require("../src/agent"); /** * MathAgent can perform basic math operations: add, subtract, multiply, divide. */ let MathAgent = class MathAgent extends agent_1.Agent { add({ a, b }) { return `${a} + ${b} = ${a + b}`; } subtract({ a, b }) { return `${a} - ${b} = ${a - b}`; } multiply({ a, b }) { return `${a} ร— ${b} = ${a * b}`; } divide({ a, b }) { if (b === 0) return 'Error: Division by zero'; return `${a} รท ${b} = ${a / b}`; } }; exports.MathAgent = MathAgent; __decorate([ (0, decorators_1.tool)('Add two 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) ], MathAgent.prototype, "add", null); __decorate([ (0, decorators_1.tool)('Subtract two 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) ], MathAgent.prototype, "subtract", null); __decorate([ (0, decorators_1.tool)('Multiply two 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) ], MathAgent.prototype, "multiply", null); __decorate([ (0, decorators_1.tool)('Divide two 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) ], MathAgent.prototype, "divide", null); exports.MathAgent = MathAgent = __decorate([ (0, decorators_1.model)('openai/gpt-4.1-nano') ], MathAgent); async function runMathAgentDemo() { if (!process.env.OPENROUTER_API_KEY) { console.error('๐Ÿ›‘ Error: OPENROUTER_API_KEY not set'); process.exit(1); } const agent = new MathAgent(); const questions = [ 'What is 15 plus 7?', 'What is 20 minus 8?', 'What is 6 multiplied by 4?', 'What is 15 divided by 3?' ]; console.log('๐Ÿงฎ Running MathAgent Demo with multiple operations...\n'); for (const question of questions) { console.log(`โ“ Question: "${question}"`); try { const result = await agent.run(question); const answer = typeof result === 'object' && result && 'answer' in result ? result.answer : String(result); console.log(`โœ… Answer: ${answer}\n`); } catch (error) { const message = error instanceof Error ? error.message : String(error); console.error(`โŒ Error: ${message}\n`); } } } if (require.main === module) { runMathAgentDemo(); } //# sourceMappingURL=math-agent.js.map