Skip to content

Commit f03b4d6

Browse files
committed
fix: adopt agentId, sessionId and shellId
1 parent b85d33b commit f03b4d6

31 files changed

+241
-326
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"threeify",
4545
"transpiling",
4646
"triggerdef",
47+
"uuidv",
4748
"vinxi"
4849
],
4950

packages/agent/src/core/toolAgent/__tests__/statusUpdates.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ describe('Status Updates', () => {
6565
const context = {
6666
agentTracker: {
6767
getAgents: vi.fn().mockReturnValue([
68-
{ id: 'agent1', goal: 'Task 1', status: AgentStatus.RUNNING },
69-
{ id: 'agent2', goal: 'Task 2', status: AgentStatus.RUNNING },
68+
{ agentId: 'agent1', goal: 'Task 1', status: AgentStatus.RUNNING },
69+
{ agentId: 'agent2', goal: 'Task 2', status: AgentStatus.RUNNING },
7070
]),
7171
},
7272
shellTracker: {
7373
getShells: vi.fn().mockReturnValue([
7474
{
75-
id: 'shell1',
75+
shellId: 'shell1',
7676
status: ShellStatus.RUNNING,
7777
metadata: { command: 'npm test' },
7878
},
@@ -81,7 +81,7 @@ describe('Status Updates', () => {
8181
browserTracker: {
8282
getSessionsByStatus: vi.fn().mockReturnValue([
8383
{
84-
id: 'session1',
84+
sessionId: 'session1',
8585
status: SessionStatus.RUNNING,
8686
metadata: { url: 'https://example.com' },
8787
},

packages/agent/src/core/toolAgent/statusUpdates.ts

Lines changed: 14 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,24 @@ export function generateStatusUpdate(
2424
: undefined;
2525

2626
// Get active sub-agents
27-
const activeAgents = context.agentTracker ? getActiveAgents(context) : [];
27+
const activeAgents = context.agentTracker
28+
? context.agentTracker.getAgents(AgentStatus.RUNNING)
29+
: [];
2830

2931
// Get active shell processes
30-
const activeShells = context.shellTracker ? getActiveShells(context) : [];
32+
const activeShells = context.shellTracker
33+
? context.shellTracker.getShells(ShellStatus.RUNNING)
34+
: [];
35+
36+
console.log('activeShells', activeShells);
3137

3238
// Get active browser sessions
3339
const activeSessions = context.browserTracker
34-
? getActiveSessions(context)
40+
? context.browserTracker.getSessionsByStatus(SessionStatus.RUNNING)
3541
: [];
3642

43+
console.log('activeSessions', activeSessions);
44+
3745
// Format the status message
3846
const statusContent = [
3947
`--- STATUS UPDATE ---`,
@@ -43,13 +51,13 @@ export function generateStatusUpdate(
4351
`Cost So Far: ${tokenTracker.getTotalCost()}`,
4452
``,
4553
`Active Sub-Agents: ${activeAgents.length}`,
46-
...activeAgents.map((a) => `- ${a.id}: ${a.description}`),
54+
...activeAgents.map((a) => `- ${a.agentId}: ${a.goal}`),
4755
``,
4856
`Active Shell Processes: ${activeShells.length}`,
49-
...activeShells.map((s) => `- ${s.id}: ${s.description}`),
57+
...activeShells.map((s) => `- ${s.shellId}: ${s.metadata.command}`),
5058
``,
5159
`Active Browser Sessions: ${activeSessions.length}`,
52-
...activeSessions.map((s) => `- ${s.id}: ${s.description}`),
60+
...activeSessions.map((s) => `- ${s.sessionId}: ${s.metadata.url ?? ''}`),
5361
``,
5462
usagePercentage !== undefined &&
5563
(usagePercentage >= 50
@@ -70,41 +78,3 @@ export function generateStatusUpdate(
7078
function formatNumber(num: number): string {
7179
return num.toLocaleString();
7280
}
73-
74-
/**
75-
* Get active agents from the agent tracker
76-
*/
77-
function getActiveAgents(context: ToolContext) {
78-
const agents = context.agentTracker.getAgents(AgentStatus.RUNNING);
79-
return agents.map((agent) => ({
80-
id: agent.id,
81-
description: agent.goal,
82-
status: agent.status,
83-
}));
84-
}
85-
86-
/**
87-
* Get active shells from the shell tracker
88-
*/
89-
function getActiveShells(context: ToolContext) {
90-
const shells = context.shellTracker.getShells(ShellStatus.RUNNING);
91-
return shells.map((shell) => ({
92-
id: shell.id,
93-
description: shell.metadata.command,
94-
status: shell.status,
95-
}));
96-
}
97-
98-
/**
99-
* Get active browser sessions from the session tracker
100-
*/
101-
function getActiveSessions(context: ToolContext) {
102-
const sessions = context.browserTracker.getSessionsByStatus(
103-
SessionStatus.RUNNING,
104-
);
105-
return sessions.map((session) => ({
106-
id: session.id,
107-
description: session.metadata.url || 'No URL',
108-
status: session.status,
109-
}));
110-
}

packages/agent/src/core/toolAgent/toolAgentCore.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { zodToJsonSchema } from 'zod-to-json-schema';
22

3+
import { userMessages } from '../../tools/interaction/userMessage.js';
34
import { utilityTools } from '../../tools/utility/index.js';
45
import { generateText } from '../llm/core.js';
56
import { createProvider } from '../llm/provider.js';
@@ -104,11 +105,6 @@ export const toolAgent = async (
104105
// Check for messages from user (for main agent only)
105106
// Import this at the top of the file
106107
try {
107-
// Dynamic import to avoid circular dependencies
108-
const { userMessages } = await import(
109-
'../../tools/interaction/userMessage.js'
110-
);
111-
112108
if (userMessages && userMessages.length > 0) {
113109
// Get all user messages and clear the queue
114110
const pendingUserMessages = [...userMessages];

packages/agent/src/tools/agent/AgentTracker.ts

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export enum AgentStatus {
1111
}
1212

1313
export interface Agent {
14-
id: string;
14+
agentId: string;
1515
status: AgentStatus;
1616
startTime: Date;
1717
endTime?: Date;
@@ -22,7 +22,7 @@ export interface Agent {
2222

2323
// Internal agent state tracking (similar to existing agentStates)
2424
export interface AgentState {
25-
id: string;
25+
agentId: string;
2626
goal: string;
2727
prompt: string;
2828
output: string;
@@ -45,32 +45,32 @@ export class AgentTracker {
4545

4646
// Register a new agent
4747
public registerAgent(goal: string): string {
48-
const id = uuidv4();
48+
const agentId = uuidv4();
4949

5050
// Create agent tracking entry
5151
const agent: Agent = {
52-
id,
52+
agentId: agentId,
5353
status: AgentStatus.RUNNING,
5454
startTime: new Date(),
5555
goal,
5656
};
5757

58-
this.agents.set(id, agent);
59-
return id;
58+
this.agents.set(agentId, agent);
59+
return agentId;
6060
}
6161

6262
// Register agent state
63-
public registerAgentState(id: string, state: AgentState): void {
64-
this.agentStates.set(id, state);
63+
public registerAgentState(agentId: string, state: AgentState): void {
64+
this.agentStates.set(agentId, state);
6565
}
6666

6767
// Update agent status
6868
public updateAgentStatus(
69-
id: string,
69+
agentId: string,
7070
status: AgentStatus,
7171
metadata?: { result?: string; error?: string },
7272
): boolean {
73-
const agent = this.agents.get(id);
73+
const agent = this.agents.get(agentId);
7474
if (!agent) {
7575
return false;
7676
}
@@ -94,13 +94,13 @@ export class AgentTracker {
9494
}
9595

9696
// Get a specific agent state
97-
public getAgentState(id: string): AgentState | undefined {
98-
return this.agentStates.get(id);
97+
public getAgentState(agentId: string): AgentState | undefined {
98+
return this.agentStates.get(agentId);
9999
}
100100

101101
// Get a specific agent tracking info
102-
public getAgent(id: string): Agent | undefined {
103-
return this.agents.get(id);
102+
public getAgent(agentId: string): Agent | undefined {
103+
return this.agents.get(agentId);
104104
}
105105

106106
// Get all agents with optional filtering
@@ -118,12 +118,12 @@ export class AgentTracker {
118118
* Get list of active agents with their descriptions
119119
*/
120120
public getActiveAgents(): Array<{
121-
id: string;
121+
agentId: string;
122122
description: string;
123123
status: AgentStatus;
124124
}> {
125125
return this.getAgents(AgentStatus.RUNNING).map((agent) => ({
126-
id: agent.id,
126+
agentId: agent.agentId,
127127
description: agent.goal,
128128
status: agent.status,
129129
}));
@@ -134,14 +134,14 @@ export class AgentTracker {
134134
const runningAgents = this.getAgents(AgentStatus.RUNNING);
135135

136136
await Promise.all(
137-
runningAgents.map((agent) => this.terminateAgent(agent.id)),
137+
runningAgents.map((agent) => this.terminateAgent(agent.agentId)),
138138
);
139139
}
140140

141141
// Terminate a specific agent
142-
public async terminateAgent(id: string): Promise<void> {
142+
public async terminateAgent(agentId: string): Promise<void> {
143143
try {
144-
const agentState = this.agentStates.get(id);
144+
const agentState = this.agentStates.get(agentId);
145145
if (agentState && !agentState.aborted) {
146146
// Set the agent as aborted and completed
147147
agentState.aborted = true;
@@ -152,9 +152,9 @@ export class AgentTracker {
152152
await agentState.context.shellTracker.cleanup();
153153
await agentState.context.browserTracker.cleanup();
154154
}
155-
this.updateAgentStatus(id, AgentStatus.TERMINATED);
155+
this.updateAgentStatus(agentId, AgentStatus.TERMINATED);
156156
} catch (error) {
157-
this.updateAgentStatus(id, AgentStatus.ERROR, {
157+
this.updateAgentStatus(agentId, AgentStatus.ERROR, {
158158
error: error instanceof Error ? error.message : String(error),
159159
});
160160
}

packages/agent/src/tools/agent/__tests__/logCapture.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ describe('Log Capture in AgentTracker', () => {
4646
);
4747

4848
// Get the agent state
49-
const agentState = agentTracker.getAgentState(startResult.instanceId);
49+
const agentState = agentTracker.getAgentState(startResult.agentId);
5050
expect(agentState).toBeDefined();
5151

5252
if (!agentState) return; // TypeScript guard
@@ -90,7 +90,7 @@ describe('Log Capture in AgentTracker', () => {
9090
// Get the agent message output
9191
const messageResult = await agentMessageTool.execute(
9292
{
93-
instanceId: startResult.instanceId,
93+
agentId: startResult.agentId,
9494
description: 'Get agent output',
9595
},
9696
context,
@@ -126,7 +126,7 @@ describe('Log Capture in AgentTracker', () => {
126126
// Get the agent message output without any logs
127127
const messageResult = await agentMessageTool.execute(
128128
{
129-
instanceId: startResult.instanceId,
129+
agentId: startResult.agentId,
130130
description: 'Get agent output',
131131
},
132132
context,

packages/agent/src/tools/agent/agentMessage.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Tool } from '../../core/types.js';
66
import { agentStates } from './agentStart.js';
77

88
const parameterSchema = z.object({
9-
instanceId: z.string().describe('The ID returned by agentStart'),
9+
agentId: z.string().describe('The ID returned by agentStart'),
1010
guidance: z
1111
.string()
1212
.optional()
@@ -57,17 +57,17 @@ export const agentMessageTool: Tool<Parameters, ReturnType> = {
5757
returnsJsonSchema: zodToJsonSchema(returnSchema),
5858

5959
execute: async (
60-
{ instanceId, guidance, terminate },
60+
{ agentId, guidance, terminate },
6161
{ logger, ..._ },
6262
): Promise<ReturnType> => {
6363
logger.debug(
64-
`Interacting with sub-agent ${instanceId}${guidance ? ' with guidance' : ''}${terminate ? ' with termination request' : ''}`,
64+
`Interacting with sub-agent ${agentId}${guidance ? ' with guidance' : ''}${terminate ? ' with termination request' : ''}`,
6565
);
6666

6767
try {
68-
const agentState = agentStates.get(instanceId);
68+
const agentState = agentStates.get(agentId);
6969
if (!agentState) {
70-
throw new Error(`No sub-agent found with ID ${instanceId}`);
70+
throw new Error(`No sub-agent found with ID ${agentId}`);
7171
}
7272

7373
// Check if the agent was already terminated
@@ -98,13 +98,13 @@ export const agentMessageTool: Tool<Parameters, ReturnType> = {
9898
// Add guidance to the agent state's parentMessages array
9999
// The sub-agent will check for these messages on each iteration
100100
if (guidance) {
101-
logger.log(`Guidance provided to sub-agent ${instanceId}: ${guidance}`);
101+
logger.log(`Guidance provided to sub-agent ${agentId}: ${guidance}`);
102102

103103
// Add the guidance to the parentMessages array
104104
agentState.parentMessages.push(guidance);
105105

106106
logger.debug(
107-
`Added message to sub-agent ${instanceId}'s parentMessages queue. Total messages: ${agentState.parentMessages.length}`,
107+
`Added message to sub-agent ${agentId}'s parentMessages queue. Total messages: ${agentState.parentMessages.length}`,
108108
);
109109
}
110110

@@ -121,7 +121,7 @@ export const agentMessageTool: Tool<Parameters, ReturnType> = {
121121

122122
// Log that we're returning captured logs
123123
logger.debug(
124-
`Returning ${agentState.capturedLogs.length} captured log messages for agent ${instanceId}`,
124+
`Returning ${agentState.capturedLogs.length} captured log messages for agent ${agentId}`,
125125
);
126126
}
127127
// Clear the captured logs after retrieving them
@@ -167,7 +167,7 @@ export const agentMessageTool: Tool<Parameters, ReturnType> = {
167167

168168
logParameters: (input, { logger }) => {
169169
logger.log(
170-
`Interacting with sub-agent ${input.instanceId}, ${input.description}${input.terminate ? ' (terminating)' : ''}`,
170+
`Interacting with sub-agent ${input.agentId}, ${input.description}${input.terminate ? ' (terminating)' : ''}`,
171171
);
172172
},
173173
logReturns: (output, { logger }) => {

0 commit comments

Comments
 (0)