UNPKG

adk-typescript

Version:

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

192 lines (191 loc) 7.84 kB
/** * A basic flow that calls the LLM in a loop until a final response is generated. * This flow ends when it transfers to another agent. */ import { InvocationContext } from '../../agents/InvocationContext'; import { BaseAgent } from '../../agents/BaseAgent'; import { Event } from '../../events/Event'; import { LlmRequest } from '../../models/LlmRequest'; import { LlmResponse } from '../../models/LlmResponse'; import { BaseLlmConnection } from '../../models/BaseLlmConnection'; import { BaseLlmRequestProcessor, BaseLlmResponseProcessor } from './BaseLlmProcessor'; import { ToolContext } from '../../tools/ToolContext'; declare module '../../telemetry' { interface Tracer { startAsCurrentSpan(name: string): { end: () => void; }; } } declare module '../../agents/BaseAgent' { interface BaseAgent { runLive(context: InvocationContext): AsyncGenerator<Event, void, unknown>; runAsync(context: InvocationContext): AsyncGenerator<Event, void, unknown>; rootAgent: BaseAgent; findAgent(name: any): BaseAgent | undefined; } } declare module '../../tools/BaseTool' { interface BaseTool { processLlmRequest(params: { toolContext: ToolContext; llmRequest: any; }): Promise<void>; } } declare module '../../agents/LiveRequestQueue' { interface LiveRequestQueue { close(): void; sendClose(): void; } } /** * A basic flow that calls the LLM in a loop until a final response is generated. * This flow ends when it transfers to another agent. */ export declare abstract class BaseLlmFlow { /** * List of request processors to run before LLM call */ protected requestProcessors: BaseLlmRequestProcessor[]; /** * List of response processors to run after LLM call */ protected responseProcessors: BaseLlmResponseProcessor[]; /** * Runs the flow using live API. * * @param invocationContext The invocation context * @returns An async generator of events */ runLive(invocationContext: InvocationContext): AsyncGenerator<Event, void, unknown>; /** * Sends data to the model in a loop. * * @param llmConnection The LLM connection * @param invocationContext The invocation context */ protected _sendToModel(llmConnection: BaseLlmConnection, invocationContext: InvocationContext): Promise<void>; /** * Receives data from the model and processes events. * * @param llmConnection The LLM connection * @param eventId The event ID * @param invocationContext The invocation context * @param llmRequest The LLM request * @returns An async generator of events */ protected _receiveFromModel(llmConnection: BaseLlmConnection, eventId: string, invocationContext: InvocationContext, llmRequest: LlmRequest): AsyncGenerator<Event, void, unknown>; /** * Runs the flow asynchronously. * * @param invocationContext The invocation context * @returns An async generator of events */ runAsync(invocationContext: InvocationContext): AsyncGenerator<Event, void, unknown>; /** * Runs one step of the flow asynchronously. * * @param invocationContext The invocation context * @returns An async generator of events */ protected _runOneStepAsync(invocationContext: InvocationContext): AsyncGenerator<Event, void, unknown>; /** * Preprocesses the request before calling the LLM. * * @param invocationContext The invocation context * @param llmRequest The LLM request * @returns An async generator of events */ protected _preprocessAsync(invocationContext: InvocationContext, llmRequest: LlmRequest): AsyncGenerator<Event, void, unknown>; /** * Postprocesses the response after the LLM call. * * @param invocationContext The invocation context * @param llmRequest The LLM request * @param llmResponse The LLM response * @param modelResponseEvent The model response event * @returns An async generator of events */ protected _postprocessAsync(invocationContext: InvocationContext, llmRequest: LlmRequest, llmResponse: LlmResponse, modelResponseEvent: Event): AsyncGenerator<Event, void, unknown>; /** * Postprocesses the response for the live API. * * @param invocationContext The invocation context * @param llmRequest The LLM request * @param llmResponse The LLM response * @param modelResponseEvent The model response event * @returns An async generator of events */ protected _postprocessLive(invocationContext: InvocationContext, llmRequest: LlmRequest, llmResponse: LlmResponse, modelResponseEvent: Event): AsyncGenerator<Event, void, unknown>; /** * Runs the response processors. * * @param invocationContext The invocation context * @param llmResponse The LLM response * @returns An async generator of events */ protected _postprocessRunProcessorsAsync(invocationContext: InvocationContext, llmResponse: LlmResponse): AsyncGenerator<Event, void, unknown>; /** * Handles function calls in the response. * * @param invocationContext The invocation context * @param functionCallEvent The function call event * @param llmRequest The LLM request * @returns An async generator of events */ protected _postprocessHandleFunctionCallsAsync(invocationContext: InvocationContext, functionCallEvent: Event, llmRequest: LlmRequest): AsyncGenerator<Event, void, unknown>; /** * Gets the agent to run for a transfer_to_agent function call. * * @param invocationContext The invocation context * @param transferToAgent The agent to transfer to * @returns The agent to run */ protected _getAgentToRun(invocationContext: InvocationContext, transferToAgent: string | { agent_name: string; } | Record<string, any>): BaseAgent; /** * Calls the LLM asynchronously. * * @param invocationContext The invocation context * @param llmRequest The LLM request * @param modelResponseEvent The model response event * @returns An async generator of LLM responses */ protected _callLlmAsync(invocationContext: InvocationContext, llmRequest: LlmRequest, modelResponseEvent: Event): AsyncGenerator<LlmResponse, void, unknown>; /** * Handles the before model callback. * * @param invocationContext The invocation context * @param llmRequest The LLM request * @param modelResponseEvent The model response event * @returns The callback response or undefined */ protected _handleBeforeModelCallback(invocationContext: InvocationContext, llmRequest: LlmRequest, modelResponseEvent: Event): LlmResponse | undefined; /** * Handles the after model callback. * * @param invocationContext The invocation context * @param llmResponse The LLM response * @param modelResponseEvent The model response event * @returns The altered LLM response or undefined */ protected _handleAfterModelCallback(invocationContext: InvocationContext, llmResponse: LlmResponse, modelResponseEvent: Event): LlmResponse | undefined; /** * Finalizes the model response event. * * @param llmRequest The LLM request * @param llmResponse The LLM response * @param modelResponseEvent The model response event * @returns The finalized event */ protected _finalizeModelResponseEvent(llmRequest: LlmRequest, llmResponse: LlmResponse, modelResponseEvent: Event): Event; /** * Gets the LLM from the invocation context. * * @param invocationContext The invocation context * @returns The LLM */ private _getLlm; }