adk-typescript
Version:
TypeScript port of Google's Agent Development Kit (ADK)
120 lines (119 loc) • 4.25 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ToolContext = void 0;
const CallbackContext_1 = require("../agents/CallbackContext");
const EventActions_1 = require("../events/EventActions");
const AuthHandler_1 = require("../auth/AuthHandler");
/**
* The context for a tool execution.
*
* This class provides the context for a tool invocation, including access to
* the invocation context, function call ID, event actions, and authentication
* response. It also provides methods for requesting credentials, retrieving
* authentication responses, listing artifacts, and searching memory.
*/
class ToolContext extends CallbackContext_1.CallbackContext {
/**
* Create a new tool context
*
* @param invocationContext The invocation context
* @param functionCallId The function call ID
* @param eventActions The event actions
*/
constructor(invocationContext, functionCallId, eventActions) {
super(invocationContext, eventActions);
this.functionCallId = functionCallId;
this.toolEventActions = eventActions || new EventActions_1.EventActions();
}
/**
* Get the event actions for this tool call
*/
get actions() {
return this.toolEventActions;
}
/**
* Request credential using the given auth config
*
* @param authConfig The auth config to use
* @throws Error if function call ID is not set
*/
requestCredential(authConfig) {
if (!this.functionCallId) {
throw new Error('functionCallId is not set.');
}
const authHandler = new AuthHandler_1.AuthHandler(authConfig);
const authRequest = authHandler.generateAuthRequest();
this.toolEventActions.requestedAuthConfigs.set(this.functionCallId, authRequest);
}
/**
* Get the auth response for the given auth config
*
* @param authConfig The auth config to use
* @returns The auth credential
*/
getAuthResponse(authConfig) {
const authHandler = new AuthHandler_1.AuthHandler(authConfig);
const response = authHandler.getAuthResponse(this.state);
if (!response) {
throw new Error('No auth response available for the given auth config.');
}
return response;
}
/**
* List artifacts attached to the current session
*
* @returns List of artifact filenames
* @throws Error if artifact service is not initialized
*/
listArtifacts() {
if (!this.invocationContext.artifactService) {
throw new Error('Artifact service is not initialized.');
}
return this.invocationContext.artifactService.listArtifactKeys({
appName: this.invocationContext.appName,
userId: this.invocationContext.userId,
sessionId: this.invocationContext.session.id,
// TS requires filename, but in this case we're listing artifacts so we don't need it
// Using an empty string as a placeholder
filename: ''
});
}
/**
* Search the memory for the given query
*
* @param query The search query
* @returns The search results
* @throws Error if memory service is not available
*/
searchMemory(query) {
if (!this.invocationContext.memoryService) {
throw new Error('Memory service is not available.');
}
return this.invocationContext.memoryService.searchMemory(this.invocationContext.appName, this.invocationContext.userId, query);
}
// Maintain backward compatibility with the original ToolContext interface
/**
* Check if the context has a specific property
*/
has(key) {
return key in this && this[key] !== undefined;
}
/**
* Get a value from the context
*/
get(key, defaultValue) {
if (key === 'session') {
return this.invocationContext.session;
}
return this.has(key) ? this[key] : defaultValue;
}
/**
* Set a value in the context
*/
set(key, value) {
if (key !== 'session' && key !== 'invocationContext') {
this[key] = value;
}
}
}
exports.ToolContext = ToolContext;