adk-typescript
Version:
TypeScript port of Google's Agent Development Kit (ADK)
131 lines (130 loc) • 4.5 kB
TypeScript
import { Content } from '../models/types';
import { Event } from '../events/Event';
import { CallbackContext } from './CallbackContext';
import { InvocationContext } from './InvocationContext';
/**
* Callback signature that is invoked before the agent run.
*
* @param callbackContext The callback context.
* @returns The content to return to the user. When set, the agent run will be skipped and
* the provided content will be returned to user.
*/
export type BeforeAgentCallback = (callbackContext: CallbackContext) => Content | undefined;
/**
* Callback signature that is invoked after the agent run.
*
* @param callbackContext The callback context.
* @returns The content to return to the user. When set, the agent run will be skipped and
* the provided content will be appended to event history as agent response.
*/
export type AfterAgentCallback = (callbackContext: CallbackContext) => Content | undefined;
/**
* Options for agent configuration.
*/
export interface AgentOptions {
/** The description of the agent */
description?: string;
/** The parent agent */
parentAgent?: BaseAgent;
/** Additional agent-specific options */
[key: string]: any;
}
/**
* Abstract base class for all agents.
*/
export declare abstract class BaseAgent {
/** The name of the agent */
name: string;
/** The description of the agent */
description?: string;
/** The parent agent of this agent */
parentAgent?: BaseAgent;
/** The sub-agents of this agent */
subAgents: BaseAgent[];
/** Callback invoked before the agent run */
beforeAgentCallback?: BeforeAgentCallback;
/** Callback invoked after the agent run */
afterAgentCallback?: AfterAgentCallback;
/**
* Creates a new agent.
*
* @param name The name of the agent
* @param options Options for the agent
*/
constructor(name: string, options?: AgentOptions);
/**
* Invokes the agent with the given context.
*
* @param invocationContext The invocation context
* @returns An async generator of events
*/
invoke(invocationContext: InvocationContext): AsyncGenerator<Event, void, unknown>;
/**
* Implementation of the agent's async invocation logic.
*
* @param invocationContext The invocation context
* @returns An async generator of events
*/
protected abstract runAsyncImpl(invocationContext: InvocationContext): AsyncGenerator<Event, void, unknown>;
/**
* Implementation of the agent's live invocation logic.
*
* @param invocationContext The invocation context
* @returns An async generator of events
*/
protected abstract runLiveImpl(invocationContext: InvocationContext): AsyncGenerator<Event, void, unknown>;
/**
* Sets the user content for the agent.
*
* @param content The user content
* @param invocationContext The invocation context
*/
abstract setUserContent(content: Content, invocationContext: InvocationContext): void;
/**
* Gets the root agent of the agent tree.
*
* @returns The root agent
*/
get rootAgent(): BaseAgent;
/**
* Finds a sub-agent by name among direct children.
*
* @param name The name of the sub-agent to find
* @returns The sub-agent if found, undefined otherwise
*/
findSubAgent(name: string): BaseAgent | undefined;
/**
* Sets the parent agent of this agent.
*
* @param parentAgent The parent agent
*/
setParentAgent(parentAgent: BaseAgent): void;
/**
* Adds a sub-agent to this agent.
*
* @param agent The sub-agent to add
* @returns This agent for method chaining
*/
addSubAgent(agent: BaseAgent): this;
/**
* Creates a new invocation context for this agent.
*
* @param parentContext The parent invocation context
* @returns A new invocation context
*/
protected createInvocationContext(parentContext: InvocationContext): InvocationContext;
/**
* Handles the before-agent callback.
*
* @param invocationContext The invocation context
* @returns The event if the callback returns content, undefined otherwise
*/
private handleBeforeAgentCallback;
/**
* Handles the after-agent callback.
*
* @param invocationContext The invocation context
* @returns The event if the callback returns content, undefined otherwise
*/
private handleAfterAgentCallback;
}