diff --git a/packages/agent/src/core/toolAgent/toolAgentCore.ts b/packages/agent/src/core/toolAgent/toolAgentCore.ts index f61c73b..4609698 100644 --- a/packages/agent/src/core/toolAgent/toolAgentCore.ts +++ b/packages/agent/src/core/toolAgent/toolAgentCore.ts @@ -61,6 +61,34 @@ export const toolAgent = async ( interactions++; + // Check for messages from parent agent + // This assumes the context has an agentTracker and the current agent's ID + if (context.agentTracker && context.currentAgentId) { + const agentState = context.agentTracker.getAgentState( + context.currentAgentId, + ); + + // Process any new parent messages + if ( + agentState && + agentState.parentMessages && + agentState.parentMessages.length > 0 + ) { + // Get all parent messages and clear the queue + const parentMessages = [...agentState.parentMessages]; + agentState.parentMessages = []; + + // Add each message to the conversation + for (const message of parentMessages) { + logger.info(`Message from parent agent: ${message}`); + messages.push({ + role: 'user', + content: `[Message from parent agent]: ${message}`, + }); + } + } + } + // Convert tools to function definitions const functionDefinitions = tools.map((tool) => ({ name: tool.name, diff --git a/packages/agent/src/core/types.ts b/packages/agent/src/core/types.ts index dd26a71..3420220 100644 --- a/packages/agent/src/core/types.ts +++ b/packages/agent/src/core/types.ts @@ -26,6 +26,7 @@ export type ToolContext = { userPrompt?: boolean; agentId?: string; // Unique identifier for the agent, used for background tool tracking agentName?: string; // Name of the agent, used for browser tracker + currentAgentId?: string; // ID of the current agent, used for parent-to-subagent communication provider: ModelProvider; model?: string; baseUrl?: string; diff --git a/packages/agent/src/tools/agent/AgentTracker.ts b/packages/agent/src/tools/agent/AgentTracker.ts index 20b9a42..ed4c894 100644 --- a/packages/agent/src/tools/agent/AgentTracker.ts +++ b/packages/agent/src/tools/agent/AgentTracker.ts @@ -33,6 +33,7 @@ export interface AgentState { workingDirectory: string; tools: unknown[]; aborted: boolean; + parentMessages: string[]; // Messages from parent agent } export class AgentTracker { diff --git a/packages/agent/src/tools/agent/agentMessage.ts b/packages/agent/src/tools/agent/agentMessage.ts index d846a53..fde1593 100644 --- a/packages/agent/src/tools/agent/agentMessage.ts +++ b/packages/agent/src/tools/agent/agentMessage.ts @@ -33,6 +33,14 @@ const returnSchema = z.object({ .boolean() .optional() .describe('Whether the sub-agent was terminated by this message'), + messageSent: z + .boolean() + .optional() + .describe('Whether a message was sent to the sub-agent'), + messageCount: z + .number() + .optional() + .describe("The number of messages in the sub-agent's queue"), }); type Parameters = z.infer; @@ -68,6 +76,8 @@ export const agentMessageTool: Tool = { output: agentState.output || 'Sub-agent was previously terminated', completed: true, terminated: true, + messageSent: false, + messageCount: 0, }; } @@ -80,19 +90,24 @@ export const agentMessageTool: Tool = { output: agentState.output || 'Sub-agent terminated before completion', completed: true, terminated: true, + messageSent: false, + messageCount: 0, }; } - // Add guidance to the agent state for future implementation - // In a more advanced implementation, this could inject the guidance - // into the agent's execution context + // Add guidance to the agent state's parentMessages array + // The sub-agent will check for these messages on each iteration if (guidance) { logger.info( `Guidance provided to sub-agent ${instanceId}: ${guidance}`, ); - // This is a placeholder for future implementation - // In a real implementation, we would need to interrupt the agent's - // execution and inject this guidance + + // Add the guidance to the parentMessages array + agentState.parentMessages.push(guidance); + + logger.verbose( + `Added message to sub-agent ${instanceId}'s parentMessages queue. Total messages: ${agentState.parentMessages.length}`, + ); } // Get the current output @@ -103,6 +118,8 @@ export const agentMessageTool: Tool = { output, completed: agentState.completed, ...(agentState.error && { error: agentState.error }), + messageSent: guidance ? true : false, + messageCount: agentState.parentMessages.length, }; } catch (error) { if (error instanceof Error) { @@ -112,6 +129,8 @@ export const agentMessageTool: Tool = { output: '', completed: false, error: error.message, + messageSent: false, + messageCount: 0, }; } @@ -123,6 +142,8 @@ export const agentMessageTool: Tool = { output: '', completed: false, error: `Unknown error occurred: ${errorMessage}`, + messageSent: false, + messageCount: 0, }; } }, @@ -142,5 +163,11 @@ export const agentMessageTool: Tool = { } else { logger.info('Sub-agent is still running'); } + + if (output.messageSent) { + logger.info( + `Message sent to sub-agent. Queue now has ${output.messageCount || 0} message(s).`, + ); + } }, }; diff --git a/packages/agent/src/tools/agent/agentStart.ts b/packages/agent/src/tools/agent/agentStart.ts index 04f9232..5b4798a 100644 --- a/packages/agent/src/tools/agent/agentStart.ts +++ b/packages/agent/src/tools/agent/agentStart.ts @@ -116,6 +116,7 @@ export const agentStartTool: Tool = { workingDirectory: workingDirectory ?? context.workingDirectory, tools, aborted: false, + parentMessages: [], // Initialize empty array for parent messages }; // Register agent state with the tracker @@ -131,6 +132,7 @@ export const agentStartTool: Tool = { const result = await toolAgent(prompt, tools, agentConfig, { ...context, workingDirectory: workingDirectory ?? context.workingDirectory, + currentAgentId: instanceId, // Pass the agent's ID to the context }); // Update agent state with the result