UNPKG

adk-typescript

Version:

TypeScript port of Google's Agent Development Kit (ADK)

130 lines (129 loc) 4.51 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 __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; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.OpenAPIToolset = void 0; const yaml = __importStar(require("js-yaml")); const OpenApiSpecParser_1 = require("./OpenApiSpecParser"); const RestApiTool_1 = require("./RestApiTool"); /** * Class for parsing OpenAPI spec into a list of RestApiTool instances */ class OpenAPIToolset { /** * Create a new OpenAPIToolset * @param options The options for the toolset */ constructor(options) { /** * The tools in this toolset */ this.tools = []; let specDict = options.specDict; if (!specDict && options.specStr) { specDict = this._loadSpec(options.specStr, options.specStrType || 'json'); } if (specDict) { this.tools = this._parse(specDict); if (options.authScheme || options.authCredential) { this._configureAuthAll(options.authScheme, options.authCredential); } } } /** * Configure authentication for all tools * @param authScheme The authentication scheme * @param authCredential The authentication credential */ _configureAuthAll(authScheme, authCredential) { for (const tool of this.tools) { if (authScheme) { tool.configureAuthScheme(authScheme); } if (authCredential) { tool.configureAuthCredential(authCredential); } } } /** * Get all tools in the toolset * @returns All RestApiTool instances in this toolset */ getTools() { return this.tools; } /** * Get a tool by name * @param toolName The name of the tool to find * @returns The matching RestApiTool or undefined if not found */ getTool(toolName) { return this.tools.find(tool => tool.name === toolName); } /** * Load an OpenAPI spec from a string * @param specStr The OpenAPI spec string * @param specType The type of the spec string (json or yaml) * @returns The parsed spec object */ _loadSpec(specStr, specType) { if (specType === 'json') { return JSON.parse(specStr); } else if (specType === 'yaml') { return yaml.load(specStr); } else { throw new Error(`Unsupported spec type: ${specType}`); } } /** * Parse an OpenAPI spec into a list of RestApiTool instances * @param openApiSpecDict The OpenAPI spec dictionary * @returns A list of RestApiTool instances */ _parse(openApiSpecDict) { // Parse the OpenAPI spec into operations const operations = new OpenApiSpecParser_1.OpenApiSpecParser().parse(openApiSpecDict); // Create a RestApiTool for each operation const tools = []; for (const op of operations) { const tool = RestApiTool_1.RestApiTool.fromParsedOperation(op); console.log(`Parsed tool: ${tool.name}`); tools.push(tool); } return tools; } } exports.OpenAPIToolset = OpenAPIToolset;