adk-typescript
Version:
TypeScript port of Google's Agent Development Kit (ADK)
104 lines (103 loc) • 3.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseTool = void 0;
/**
* Base class for all tools
*/
class BaseTool {
/**
* Create a new base tool
* @param options Options for the base tool
*/
constructor(options) {
this.name = options.name;
this.description = options.description;
this.isLongRunning = options.isLongRunning || false;
}
/**
* Internal method to get the function declaration
* @returns The function declaration for this tool
*/
_getDeclaration() {
// By default, return null. Subclasses should override to provide
// a function declaration if needed
return null;
}
/**
* Get the declaration for this tool
* @returns The function declaration for this tool
*/
getDeclaration() {
return this._getDeclaration();
}
/**
* Get the parameters for this tool
* @returns The parameters for this tool
*/
getParameters() {
const declaration = this.getDeclaration();
return declaration ? declaration.parameters : {};
}
/**
* Process the LLM request for this tool
*
* This is used to modify the LLM request before it's sent out,
* typically to add this tool to the LLM's available tools.
*
* @param params Parameters for processing
* @param params.toolContext Context information for the tool
* @param params.llmRequest The outgoing LLM request to modify
*/
async processLlmRequest({ toolContext, llmRequest }) {
const functionDeclaration = this._getDeclaration();
if (!functionDeclaration) {
return;
}
// Add this tool to the LLM request's tools
if (!llmRequest.config) {
llmRequest.config = {};
}
if (!llmRequest.config.tools) {
llmRequest.config.tools = [];
}
// Store tool reference for later use
if (!llmRequest.toolsDict) {
llmRequest.toolsDict = {};
}
llmRequest.toolsDict[this.name] = this;
// Add to function declarations
const toolWithDeclarations = this._findToolWithFunctionDeclarations(llmRequest);
if (toolWithDeclarations) {
if (!toolWithDeclarations.functionDeclarations) {
toolWithDeclarations.functionDeclarations = [];
}
toolWithDeclarations.functionDeclarations.push(functionDeclaration);
}
else {
// Add new tool entry
llmRequest.config.tools.push({
functionDeclarations: [functionDeclaration]
});
}
}
/**
* Find a tool in the LLM request that has function declarations
* @param llmRequest The LLM request to search in
* @returns The tool with function declarations, or null if not found
*/
_findToolWithFunctionDeclarations(llmRequest) {
if (!llmRequest.config || !llmRequest.config.tools) {
return null;
}
return llmRequest.config.tools.find((tool) => tool.functionDeclarations) || null;
}
/**
* Get the API variant (Vertex AI or Google AI)
*/
get _apiVariant() {
const useVertexAi = process.env.GOOGLE_GENAI_USE_VERTEXAI === 'true' ||
process.env.GOOGLE_GENAI_USE_VERTEXAI === '1';
return useVertexAi ? 'VERTEX_AI' : 'GOOGLE_AI';
}
}
exports.BaseTool = BaseTool;