UNPKG

adk-typescript

Version:

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

129 lines (128 loc) 4.42 kB
"use strict"; /** * Handles NL planning related logic. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.responseProcessor = exports.requestProcessor = void 0; const CallbackContext_1 = require("../../agents/CallbackContext"); const ReadonlyContext_1 = require("../../agents/ReadonlyContext"); const Event_1 = require("../../events/Event"); const BasePlanner_1 = require("../../planners/BasePlanner"); const BuiltInPlanner_1 = require("../../planners/BuiltInPlanner"); const PlanReActPlanner_1 = require("../../planners/PlanReActPlanner"); const LlmAgent_1 = require("../../agents/LlmAgent"); /** * Processor for NL planning request. */ class NlPlanningRequestProcessor { /** * Runs the processor asynchronously. * * @param invocationContext The invocation context * @param llmRequest The LLM request to process * @returns An async generator yielding events */ async *runAsync(invocationContext, llmRequest) { const planner = getPlanner(invocationContext); if (!planner) { return; } if (planner instanceof BuiltInPlanner_1.BuiltInPlanner) { planner.applyThinkingConfig(llmRequest); } const planningInstruction = planner.buildPlanningInstruction(new ReadonlyContext_1.ReadonlyContext(invocationContext), llmRequest); if (planningInstruction) { llmRequest.appendInstructions([planningInstruction]); } removeThoughtFromRequest(llmRequest); // Maintain async generator behavior by returning early return; // The code below is unreachable but satisfies the AsyncGenerator return type yield {}; } } /** * The exported request processor instance. */ exports.requestProcessor = new NlPlanningRequestProcessor(); /** * Processor for NL planning response. */ class NlPlanningResponseProcessor { /** * Processes the LLM response asynchronously. * * @param invocationContext The invocation context * @param llmResponse The LLM response to process * @returns An async generator yielding events */ async *runAsync(invocationContext, llmResponse) { if (!llmResponse || !llmResponse.content || !llmResponse.content.parts) { return; } const planner = getPlanner(invocationContext); if (!planner) { return; } // Postprocess the LLM response const callbackContext = new CallbackContext_1.CallbackContext(invocationContext); const processedParts = planner.processPlanningResponse(callbackContext, llmResponse.content.parts); if (processedParts) { llmResponse.content.parts = processedParts; } // Check if the state has changed and create an event if needed // The Python has_delta() is equivalent to checking if the state delta is not empty if (Object.keys(callbackContext['eventActions'].stateDelta).length > 0) { const stateUpdateEvent = new Event_1.Event({ invocationId: invocationContext.invocationId, author: invocationContext.agent.name, branch: invocationContext.branch, actions: callbackContext['eventActions'] }); yield stateUpdateEvent; } } } /** * The exported response processor instance. */ exports.responseProcessor = new NlPlanningResponseProcessor(); /** * Gets the planner from the invocation context. * * @param invocationContext The invocation context * @returns The planner, or undefined if not found */ function getPlanner(invocationContext) { const agent = invocationContext.agent; if (!(agent instanceof LlmAgent_1.LlmAgent)) { return undefined; } if (!agent.planner) { return undefined; } if (agent.planner instanceof BasePlanner_1.BasePlanner) { return agent.planner; } return new PlanReActPlanner_1.PlanReActPlanner(); } /** * Removes thought from the request. * * @param llmRequest The LLM request */ function removeThoughtFromRequest(llmRequest) { if (!llmRequest.contents) { return; } for (const content of llmRequest.contents) { if (!content.parts) { continue; } for (const part of content.parts) { part.thought = undefined; } } }