Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions packages/agent/src/core/toolAgent/toolAgentCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions packages/agent/src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
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;
Expand All @@ -37,7 +38,7 @@
browserTracker: SessionTracker;
};

export type Tool<TParams = Record<string, any>, TReturn = any> = {

Check warning on line 41 in packages/agent/src/core/types.ts

View workflow job for this annotation

GitHub Actions / ci

Unexpected any. Specify a different type

Check warning on line 41 in packages/agent/src/core/types.ts

View workflow job for this annotation

GitHub Actions / ci

Unexpected any. Specify a different type
name: string;
description: string;
parameters: z.ZodType<TParams>;
Expand Down
1 change: 1 addition & 0 deletions packages/agent/src/tools/agent/AgentTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export interface AgentState {
workingDirectory: string;
tools: unknown[];
aborted: boolean;
parentMessages: string[]; // Messages from parent agent
}

export class AgentTracker {
Expand Down
39 changes: 33 additions & 6 deletions packages/agent/src/tools/agent/agentMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof parameterSchema>;
Expand Down Expand Up @@ -68,6 +76,8 @@ export const agentMessageTool: Tool<Parameters, ReturnType> = {
output: agentState.output || 'Sub-agent was previously terminated',
completed: true,
terminated: true,
messageSent: false,
messageCount: 0,
};
}

Expand All @@ -80,19 +90,24 @@ export const agentMessageTool: Tool<Parameters, ReturnType> = {
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
Expand All @@ -103,6 +118,8 @@ export const agentMessageTool: Tool<Parameters, ReturnType> = {
output,
completed: agentState.completed,
...(agentState.error && { error: agentState.error }),
messageSent: guidance ? true : false,
messageCount: agentState.parentMessages.length,
};
} catch (error) {
if (error instanceof Error) {
Expand All @@ -112,6 +129,8 @@ export const agentMessageTool: Tool<Parameters, ReturnType> = {
output: '',
completed: false,
error: error.message,
messageSent: false,
messageCount: 0,
};
}

Expand All @@ -123,6 +142,8 @@ export const agentMessageTool: Tool<Parameters, ReturnType> = {
output: '',
completed: false,
error: `Unknown error occurred: ${errorMessage}`,
messageSent: false,
messageCount: 0,
};
}
},
Expand All @@ -142,5 +163,11 @@ export const agentMessageTool: Tool<Parameters, ReturnType> = {
} 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).`,
);
}
},
};
2 changes: 2 additions & 0 deletions packages/agent/src/tools/agent/agentStart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export const agentStartTool: Tool<Parameters, ReturnType> = {
workingDirectory: workingDirectory ?? context.workingDirectory,
tools,
aborted: false,
parentMessages: [], // Initialize empty array for parent messages
};

// Register agent state with the tracker
Expand All @@ -131,6 +132,7 @@ export const agentStartTool: Tool<Parameters, ReturnType> = {
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
Expand Down
Loading