UNPKG

adk-typescript

Version:

TypeScript port of Google's Agent Development Kit (ADK)

195 lines (194 loc) 7.44 kB
/** * Implementation of an agent that uses an LLM flow. */ import { BaseAgent, AgentOptions } from './BaseAgent'; import { InvocationContext } from './InvocationContext'; import { ReadonlyContext } from './ReadonlyContext'; import { CallbackContext } from './CallbackContext'; import { Event } from '../events/Event'; import { BaseLlmFlow } from '../flows/llm_flows/BaseLlmFlow'; import { Content } from '../models/types'; import { BaseLlm } from '../models/BaseLlm'; import { LlmRequest } from '../models/LlmRequest'; import { LlmResponse } from '../models/LlmResponse'; import { BaseTool } from '../tools/BaseTool'; import { FunctionToolOptions } from '../tools/FunctionTool'; import { ToolContext } from '../tools/ToolContext'; import { Session, SessionOptions } from '../sessions/Session'; import { BasePlanner } from '../planners/BasePlanner'; import { BaseCodeExecutor } from '../code-executors/BaseCodeExecutor'; /** * Base interface for example providers */ interface BaseExampleProvider { getExamples(): Promise<Example[]>; } /** * Represents an example for the agent */ interface Example { input: string; output: string; [key: string]: any; } type InstructionProvider = (context: ReadonlyContext) => string; type ToolUnion = BaseTool | (((...args: any[]) => any) & FunctionToolOptions); type ExamplesUnion = Example[] | BaseExampleProvider; type BeforeModelCallback = (context: CallbackContext, request: LlmRequest) => LlmResponse | undefined; type AfterModelCallback = (context: CallbackContext, response: LlmResponse) => LlmResponse | undefined; type BeforeToolCallback = (tool: BaseTool, args: Record<string, any>, toolContext: ToolContext) => Record<string, any> | undefined | Promise<Record<string, any> | undefined>; type AfterToolCallback = (tool: BaseTool, args: Record<string, any>, toolContext: ToolContext, response: Record<string, any>) => Record<string, any> | undefined | Promise<Record<string, any> | undefined>; /** * Extended options for LLM agents. */ export interface LlmAgentOptions extends AgentOptions { /** The name of the agent */ name: string; /** The LLM flow to use */ flow?: BaseLlmFlow; /** The LLM model to use */ model?: string | BaseLlm; /** Whether to disallow transfers to the parent agent */ disallowTransferToParent?: boolean; /** Whether to disallow transfers to peer agents */ disallowTransferToPeers?: boolean; /** Tools available to this agent */ tools?: ToolUnion[]; /** The instruction template for the agent */ instruction?: string | InstructionProvider; /** Global instruction for all agents in the tree */ globalInstruction?: string | InstructionProvider; /** Content generation configuration */ generateContentConfig?: any; /** Include contents setting */ includeContents?: 'default' | 'none'; /** Input schema for validation */ inputSchema?: any; /** Output schema for validation */ outputSchema?: any; /** Output key for state storage */ outputKey?: string; /** Planner for step-by-step execution */ planner?: BasePlanner; /** Code executor for running code blocks */ codeExecutor?: BaseCodeExecutor; /** Examples for the agent */ examples?: ExamplesUnion; /** Callback before model invocation */ beforeModelCallback?: BeforeModelCallback; /** Callback after model invocation */ afterModelCallback?: AfterModelCallback; /** Callback before tool invocation */ beforeToolCallback?: BeforeToolCallback; /** Callback after tool invocation */ afterToolCallback?: AfterToolCallback; } /** * An agent that uses an LLM flow to process requests. */ export declare class LlmAgent extends BaseAgent { /** Optional custom flow for this agent */ private customFlow?; /** The LLM model used by this agent */ model: string | BaseLlm; /** The instruction template for the agent */ instruction: string | InstructionProvider; /** Global instruction for all agents in the tree */ globalInstruction: string | InstructionProvider; /** Tools available to this agent */ tools: ToolUnion[]; /** Content generation configuration */ generateContentConfig?: any; /** Whether to disallow transfers to the parent agent */ disallowTransferToParent: boolean; /** Whether to disallow transfers to peer agents */ disallowTransferToPeers: boolean; /** Include contents setting */ includeContents: 'default' | 'none'; /** Input schema for validation */ inputSchema?: any; /** Output schema for validation */ outputSchema?: any; /** Output key for state storage */ outputKey?: string; /** Planner for step-by-step execution */ planner?: BasePlanner; /** Code executor for running code blocks */ codeExecutor?: BaseCodeExecutor; /** Examples for the agent */ examples?: ExamplesUnion; /** Callback before model invocation */ beforeModelCallback?: BeforeModelCallback; /** Callback after model invocation */ afterModelCallback?: AfterModelCallback; /** Callback before tool invocation */ beforeToolCallback?: BeforeToolCallback; /** Callback after tool invocation */ afterToolCallback?: AfterToolCallback; /** * Creates a new LLM agent. * * @param options Options for the agent including name */ constructor(options: LlmAgentOptions); /** * Creates a new session for this agent * * @returns A promise resolving to a new Session object */ createSession(options?: Partial<SessionOptions>): Promise<Session>; /** * Implementation of the agent's async invocation logic. * * @param invocationContext The invocation context * @returns An async generator of events */ protected 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 runLiveImpl(invocationContext: InvocationContext): AsyncGenerator<Event, void, unknown>; /** * Sets the user content for the agent. * * @param content The user content * @param invocationContext The invocation context */ setUserContent(content: Content, invocationContext: InvocationContext): void; /** * Gets the resolved model as a BaseLlm. * This method is only for use by Agent Development Kit. */ get canonicalModel(): BaseLlm; /** * Gets the resolved instruction for this agent. * This method is only for use by Agent Development Kit. */ canonicalInstruction(ctx: ReadonlyContext): string; /** * Gets the resolved global instruction. * This method is only for use by Agent Development Kit. */ canonicalGlobalInstruction(ctx: ReadonlyContext): string; /** * Gets the resolved tools as BaseTool instances. * This method is only for use by Agent Development Kit. */ get canonicalTools(): BaseTool[]; /** * Returns the LLM flow to use for this agent. */ private get llmFlow(); /** * Saves the model output to state if needed. */ private maybeSaveOutputToState; /** * Validates the output schema configuration. */ private validateOutputSchema; } export {};