UNPKG

genkit-cli

Version:

CLI for interacting with the Google Genkit AI framework

81 lines 3.74 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.defineFlowTools = defineFlowTools; const manager_1 = require("@genkit-ai/tools-common/manager"); const utils_1 = require("@genkit-ai/tools-common/utils"); const zod_1 = __importDefault(require("zod")); const analytics_js_1 = require("./analytics.js"); const utils_js_1 = require("./utils.js"); function defineFlowTools(server, options) { server.registerTool('list_flows', { title: 'List Genkit Flows', description: 'Use this to discover available Genkit flows or inspect the input schema of Genkit flows to know how to successfully call them.', inputSchema: (0, utils_js_1.getCommonSchema)(options.explicitProjectRoot), }, async (opts) => { await (0, utils_1.record)(new analytics_js_1.McpRunToolEvent('list_flows')); const rootOrError = (0, utils_js_1.resolveProjectRoot)(options.explicitProjectRoot, opts, options.projectRoot); if (typeof rootOrError !== 'string') return rootOrError; const runtimeManager = await options.manager.getManager(rootOrError); const actions = await runtimeManager.listActions(); let flows = ''; for (const key of Object.keys(actions)) { if (key.startsWith('/flow/')) { flows += ' - Flow name: ' + key.substring('/flow/'.length) + '\n'; if (actions[key].description) { flows += ' Description: ' + actions[key].description + '\n'; } flows += ' Input schema: ' + JSON.stringify(actions[key].inputSchema, undefined, 2) + '\n\n'; } } return { content: [{ type: 'text', text: flows }] }; }); server.registerTool('run_flow', { title: 'Run Flow', description: 'Runs the flow with the provided input', inputSchema: (0, utils_js_1.getCommonSchema)(options.explicitProjectRoot, { flowName: zod_1.default.string().describe('name of the flow; type: string'), input: zod_1.default .string() .describe('Flow input as JSON object encoded as string (it will be passed through `JSON.parse`). Must conform to the schema; type: string.') .optional(), }), }, async (opts) => { await (0, utils_1.record)(new analytics_js_1.McpRunToolEvent('run_flow')); const rootOrError = (0, utils_js_1.resolveProjectRoot)(options.explicitProjectRoot, opts, options.projectRoot); if (typeof rootOrError !== 'string') return rootOrError; const { flowName, input } = opts; try { const runtimeManager = await options.manager.getManager(rootOrError); const response = await runtimeManager.runAction({ key: `/flow/${flowName}`, input: input !== undefined ? JSON.parse(input) : undefined, }); return { content: [ { type: 'text', text: JSON.stringify(response, undefined, 2) }, ], }; } catch (e) { const errStr = e instanceof manager_1.GenkitToolsError ? e.formatError() : JSON.stringify(e); return { isError: true, content: [ { type: 'text', text: `Error: ${errStr}`, }, ], }; } }); } //# sourceMappingURL=flows.js.map