UNPKG

adk-typescript

Version:

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

127 lines (126 loc) 4.46 kB
import { APIHubClient } from './clients'; import { RestApiTool } from '../openapi-tool'; import { AuthCredential, AuthScheme } from '../openapi-tool/auth/AuthTypes'; /** * APIHubToolset generates tools from a given API Hub resource. * * Examples: * * ```typescript * const apihubToolset = new APIHubToolset({ * apihubResourceName: "projects/test-project/locations/us-central1/apis/test-api", * serviceAccountJson: "...", * }); * * // Get all available tools * const agent = new LlmAgent({ tools: apihubToolset.getTools() }); * * // Get a specific tool * const agent = new LlmAgent({ * tools: [ * ... * apihubToolset.getTool('my_tool'), * ] * }); * ``` * * **apihubResourceName** is the resource name from API Hub. It must include * API name, and can optionally include API version and spec name. * - If apihubResourceName includes a spec resource name, the content of that * spec will be used for generating the tools. * - If apihubResourceName includes only an api or a version name, the * first spec of the first version of that API will be used. */ export declare class APIHubToolset { private name; private description; private apihubResourceName; private lazyLoadSpec; private apihubClient; private generatedTools; private authScheme?; private authCredential?; /** * Initializes the APIHubToolset with the given parameters. * * Examples: * ```typescript * const apihubToolset = new APIHubToolset({ * apihubResourceName: "projects/test-project/locations/us-central1/apis/test-api", * serviceAccountJson: "...", * }); * * // Get all available tools * const agent = new LlmAgent({ tools: apihubToolset.getTools() }); * * // Get a specific tool * const agent = new LlmAgent({ * tools: [ * ... * apihubToolset.getTool('my_tool'), * ] * }); * ``` * * @param params Configuration parameters * @param params.apihubResourceName The resource name of the API in API Hub. Example: `projects/test-project/locations/us-central1/apis/test-api`. * @param params.accessToken Google Access token. Generate with gcloud cli `gcloud auth print-access-token`. Used for fetching API Specs from API Hub. * @param params.serviceAccountJson The service account config as a json string. Required if not using default service credential. Used for creating the API Hub client and fetching API Specs from API Hub. * @param params.apihubClient Optional custom API Hub client. * @param params.name Name of the toolset. Optional. * @param params.description Description of the toolset. Optional. * @param params.authScheme Auth scheme that applies to all the tool in the toolset. * @param params.authCredential Auth credential that applies to all the tool in the toolset. * @param params.lazyLoadSpec If true, the spec will be loaded lazily when needed. Otherwise, the spec will be loaded immediately and the tools will be generated during initialization. */ constructor(params: { apihubResourceName: string; accessToken?: string; serviceAccountJson?: string; name?: string; description?: string; lazyLoadSpec?: boolean; authScheme?: AuthScheme; authCredential?: AuthCredential; apihubClient?: APIHubClient; }); /** * Retrieves a specific tool by its name. * * Example: * ```typescript * const apihubTool = apihubToolset.getTool('my_tool'); * ``` * * @param name The name of the tool to retrieve. * @returns The tool with the given name, or undefined if no such tool exists. */ getTool(name: string): RestApiTool | undefined; /** * Retrieves all available tools. * * @returns A list of all available RestApiTool objects. */ getTools(): RestApiTool[]; /** * Checks if tools are ready for use * * @returns True if tools are ready, false otherwise * @private */ private areToolsReady; /** * Fetches the spec from API Hub and generates the tools. * * @private */ private prepareTools; /** * Parses the spec string to a list of RestApiTool * * @param specStr The spec string to parse * @returns A list of RestApiTool objects * @private */ private parseSpecToTools; }