UNPKG

tinyagent-ts

Version:

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

62 lines 2.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.META_KEYS = void 0; exports.model = model; exports.tool = tool; // src/decorators.ts require("reflect-metadata"); const zod_1 = require("zod"); /** * Defines symbols used as keys for storing metadata via `reflect-metadata`. */ exports.META_KEYS = { /** Symbol for storing the model name metadata on an agent class. */ MODEL: Symbol("model"), /** Symbol for storing the list of tool metadata on an agent class. */ TOOLS: Symbol("tools"), }; /** * Class decorator to associate an LLM model name with an agent class. * This name is used by the agent to make API calls to the specified model. * * @param name - The identifier of the LLM model (e.g., "qwen/qwen2-72b-instruct"). * @returns A class decorator function. * @example * ```typescript * @model("qwen/qwen2-72b-instruct") * class MyAgent extends Agent {} * ``` */ function model(name) { return (target) => Reflect.defineMetadata(exports.META_KEYS.MODEL, name, target); } /** * Property decorator to define a class method as an agent tool. * It stores metadata about the tool, including its description and parameter schema. * * @param description - A human-readable description of the tool's purpose. * @param paramSchema - A Zod schema for validating the tool's input parameters. Defaults to an empty object schema. * @returns A property decorator function. * @example * ```typescript * class MyAgent extends Agent { * @tool('Adds two numbers', z.object({ a: z.number(), b: z.number() })) * add({ a, b }: { a: number; b: number }): string { * return String(a + b); * } * } * ``` */ function tool(description, paramSchema = zod_1.z.object({})) { return (target, propertyKey) => { const list = Reflect.getMetadata(exports.META_KEYS.TOOLS, target.constructor) || []; list.push({ name: String(propertyKey), description, method: String(propertyKey), schema: paramSchema, }); Reflect.defineMetadata(exports.META_KEYS.TOOLS, list, target.constructor); }; } //# sourceMappingURL=decorators.js.map