adk-typescript
Version:
TypeScript port of Google's Agent Development Kit (ADK)
87 lines (86 loc) • 2.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.LangchainTool = void 0;
exports.createLangchainTool = createLangchainTool;
const FunctionTool_1 = require("./FunctionTool");
/**
* A tool that wraps a Langchain tool
*/
class LangchainTool extends FunctionTool_1.FunctionTool {
/**
* Creates a new Langchain tool
*
* @param tool The Langchain tool to wrap
*/
constructor(tool) {
// Create a function that wraps the tool.run method
const runFunction = async (params, context) => {
// Call the tool's run method
try {
// If the input is a string, pass it directly
if (typeof params === 'string') {
return await tool.run(params);
}
// If it's a simple object with a single 'input' key, pass the value
if (params && typeof params === 'object' && 'input' in params && Object.keys(params).length === 1) {
return await tool.run(params.input);
}
// Otherwise, pass the whole params object
return await tool.run(params);
}
catch (error) {
return {
error: true,
message: `Error running tool ${tool.name}: ${error.message || 'Unknown error'}`
};
}
};
// Initialize the FunctionTool with the run function
super({
name: tool.name,
description: tool.description,
fn: runFunction
});
this.tool = tool;
}
/**
* Get the function declaration for the tool
*
* @returns The function declaration
*/
getFunctionDeclaration() {
// If the tool has a schema, use it to build the function declaration
if (this.tool.argsSchema) {
return {
name: this.name,
description: this.description,
parameters: this.tool.argsSchema
};
}
// Default declaration for tools without a schema
return {
name: this.name,
description: this.description,
parameters: {
type: 'object',
properties: {
input: {
type: 'string',
description: 'The input to the tool'
}
},
required: ['input']
}
};
}
}
exports.LangchainTool = LangchainTool;
/**
* Create a new Langchain tool
*
* @param tool The Langchain tool to wrap
* @returns A new LangchainTool instance
*/
function createLangchainTool(tool) {
return new LangchainTool(tool);
}