adk-typescript
Version:
TypeScript port of Google's Agent Development Kit (ADK)
107 lines (106 loc) • 3.51 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ToolboxTool = void 0;
const axios_1 = __importDefault(require("axios"));
const FunctionTool_1 = require("./FunctionTool");
/**
* A simple Toolbox client implementation
*/
class SimpleToolboxClient {
/**
* Create a new Toolbox client
* @param url The URL of the Toolbox server
*/
constructor(url) {
this.baseUrl = url.endsWith('/') ? url : `${url}/`;
}
/**
* Load a tool from the Toolbox server
* @param toolName The name of the tool to load
* @returns The loaded tool
*/
async loadTool(toolName) {
const response = await axios_1.default.get(`${this.baseUrl}tools/${toolName}`);
return response.data;
}
/**
* Load a set of tools from the Toolbox server
* @param toolsetName The name of the toolset to load
* @returns The loaded tools
*/
async loadToolset(toolsetName) {
const response = await axios_1.default.get(`${this.baseUrl}toolsets/${toolsetName}`);
return response.data;
}
}
/**
* A class that provides access to toolbox tools.
*
* Example:
* ```typescript
* const toolbox = new ToolboxTool("http://127.0.0.1:5000");
* const tool = await toolbox.getTool("tool_name");
* const toolset = await toolbox.getToolset("toolset_name");
* ```
*/
class ToolboxTool {
/**
* Create a new toolbox tool
*
* @param url The URL of the toolbox server
* @param client Optional custom toolbox client
*/
constructor(url, client) {
this.toolboxClient = client || new SimpleToolboxClient(url);
}
/**
* Get a tool from the toolbox
*
* @param toolName The name of the tool to get
* @returns The tool as a FunctionTool
*/
async getTool(toolName) {
const tool = await this.toolboxClient.loadTool(toolName);
return this.convertToFunctionTool(tool);
}
/**
* Get a set of tools from the toolbox
*
* @param toolsetName The name of the toolset to get
* @returns The tools as FunctionTools
*/
async getToolset(toolsetName) {
const tools = await this.toolboxClient.loadToolset(toolsetName);
return Promise.all(tools.map(tool => this.convertToFunctionTool(tool)));
}
/**
* Convert a toolbox tool to a FunctionTool
*
* @param tool The toolbox tool to convert
* @returns The converted FunctionTool
*/
convertToFunctionTool(tool) {
return new FunctionTool_1.FunctionTool({
name: tool.name,
description: tool.description || '',
fn: async (params, context) => {
// Call the tool's run method
if (typeof tool.run === 'function') {
return await tool.run(params);
}
// If no run method, try to send a request to the toolbox server
// Safely access the baseUrl by checking if it's a SimpleToolboxClient
const client = this.toolboxClient;
const baseUrl = client.baseUrl || '';
const response = await axios_1.default.post(`${baseUrl}run/${tool.name}`, {
params
});
return response.data;
}
});
}
}
exports.ToolboxTool = ToolboxTool;