UNPKG

@xynehq/jaf

Version:

Juspay Agent Framework - A purely functional agent framework with immutable state and composable tools

115 lines 5.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.webSearchTool = exports.fileWriteTool = exports.fileReadTool = exports.mathTool = void 0; const zod_1 = require("zod"); const validation_js_1 = require("../policies/validation.js"); const readFileSchema = zod_1.z.object({ path: zod_1.z.string().describe("The file path to read"), encoding: zod_1.z.enum(['utf8', 'base64']).default('utf8').describe("File encoding") }); const writeFileSchema = zod_1.z.object({ path: zod_1.z.string().describe("The file path to write"), content: zod_1.z.string().describe("The content to write"), createDirs: zod_1.z.boolean().default(false).describe("Create directories if they don't exist") }); const calculateSchema = zod_1.z.object({ expression: zod_1.z.string().describe("Mathematical expression to evaluate (e.g., '2 + 2', 'sqrt(16)')"), }); const searchWebSchema = zod_1.z.object({ query: zod_1.z.string().describe("Search query"), maxResults: zod_1.z.number().default(5).describe("Maximum number of results") }); const baseMathTool = { schema: { name: "calculate", description: "Perform mathematical calculations", parameters: calculateSchema, }, execute: async (args) => { try { const sanitized = args.expression.replace(/[^0-9+\-*/.() ]/g, ''); if (sanitized.includes('sqrt')) { const sqrtMatch = sanitized.match(/sqrt\(([0-9.]+)\)/); if (sqrtMatch) { const num = parseFloat(sqrtMatch[1]); const result = Math.sqrt(num); return `√${num} = ${result}`; } } const result = Function(`"use strict"; return (${sanitized})`)(); return `${args.expression} = ${result}`; } catch (error) { return `Error calculating ${args.expression}: ${error instanceof Error ? error.message : 'Unknown error'}`; } }, }; const baseFileReadTool = { schema: { name: "read_file", description: "Read the contents of a file", parameters: readFileSchema, }, execute: async (args, context) => { try { const fs = require('fs').promises; const path = require('path'); const fullPath = path.resolve(context.workspace, args.path); if (!fullPath.startsWith(context.workspace)) { throw new Error('Path outside workspace not allowed'); } const content = await fs.readFile(fullPath, args.encoding); return `File contents of ${args.path}:\n\n${content}`; } catch (error) { return `Error reading file ${args.path}: ${error instanceof Error ? error.message : 'Unknown error'}`; } }, }; const baseFileWriteTool = { schema: { name: "write_file", description: "Write content to a file", parameters: writeFileSchema, }, execute: async (args, context) => { try { const fs = require('fs').promises; const path = require('path'); const fullPath = path.resolve(context.workspace, args.path); if (!fullPath.startsWith(context.workspace)) { throw new Error('Path outside workspace not allowed'); } if (args.createDirs) { await fs.mkdir(path.dirname(fullPath), { recursive: true }); } await fs.writeFile(fullPath, args.content, 'utf8'); return `Successfully wrote ${args.content.length} characters to ${args.path}`; } catch (error) { return `Error writing file ${args.path}: ${error instanceof Error ? error.message : 'Unknown error'}`; } }, }; const mockWebSearchTool = { schema: { name: "search_web", description: "Search the web for information", parameters: searchWebSchema, }, execute: async (args) => { const mockResults = [ `Search result 1 for "${args.query}": This is a mock search result with relevant information.`, `Search result 2 for "${args.query}": Another mock result containing useful details.`, `Search result 3 for "${args.query}": A third search result with additional context.`, ].slice(0, args.maxResults); return `Web search results for "${args.query}":\n\n${mockResults.join('\n\n')}`; }, }; const pathValidator = (0, validation_js_1.createPathValidator)(['/shared', '/public', '/tmp'], (ctx) => ({ permissions: ctx.permissions })); const adminPermissionValidator = (0, validation_js_1.createPermissionValidator)('admin', (ctx) => ({ permissions: ctx.permissions })); exports.mathTool = baseMathTool; exports.fileReadTool = (0, validation_js_1.withValidation)(baseFileReadTool, pathValidator); exports.fileWriteTool = (0, validation_js_1.withValidation)(baseFileWriteTool, adminPermissionValidator); exports.webSearchTool = mockWebSearchTool; //# sourceMappingURL=tools.js.map