Skip to content

Commit d73389a

Browse files
committed
chore: get tests to pass again
1 parent 1c44705 commit d73389a

File tree

6 files changed

+228
-61
lines changed

6 files changed

+228
-61
lines changed

docs/tools/agent-tools.md

+30-30
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ The `subAgent` tool creates a sub-agent that runs synchronously until completion
1212
```typescript
1313
subAgent({
1414
description: "A brief description of the sub-agent's purpose",
15-
goal: "The main objective that the sub-agent needs to achieve",
16-
projectContext: "Context about the problem or environment",
17-
workingDirectory: "/path/to/working/directory", // optional
18-
relevantFilesDirectories: "src/**/*.ts", // optional
15+
goal: 'The main objective that the sub-agent needs to achieve',
16+
projectContext: 'Context about the problem or environment',
17+
workingDirectory: '/path/to/working/directory', // optional
18+
relevantFilesDirectories: 'src/**/*.ts', // optional
1919
});
2020
```
2121

@@ -35,10 +35,10 @@ The `agentStart` tool creates a sub-agent and immediately returns an instance ID
3535
```typescript
3636
const { instanceId } = agentStart({
3737
description: "A brief description of the sub-agent's purpose",
38-
goal: "The main objective that the sub-agent needs to achieve",
39-
projectContext: "Context about the problem or environment",
40-
workingDirectory: "/path/to/working/directory", // optional
41-
relevantFilesDirectories: "src/**/*.ts", // optional
38+
goal: 'The main objective that the sub-agent needs to achieve',
39+
projectContext: 'Context about the problem or environment',
40+
workingDirectory: '/path/to/working/directory', // optional
41+
relevantFilesDirectories: 'src/**/*.ts', // optional
4242
enableUserPrompt: false, // optional, default: false
4343
});
4444
```
@@ -50,22 +50,22 @@ The `agentMessage` tool allows interaction with a running sub-agent. It can be u
5050
```typescript
5151
// Check agent progress
5252
const { output, completed } = agentMessage({
53-
instanceId: "agent-instance-id",
54-
description: "Checking agent progress",
53+
instanceId: 'agent-instance-id',
54+
description: 'Checking agent progress',
5555
});
5656

5757
// Provide guidance (note: guidance implementation is limited in the current version)
5858
agentMessage({
59-
instanceId: "agent-instance-id",
60-
guidance: "Focus on the task at hand and avoid unnecessary exploration",
61-
description: "Providing guidance to the agent",
59+
instanceId: 'agent-instance-id',
60+
guidance: 'Focus on the task at hand and avoid unnecessary exploration',
61+
description: 'Providing guidance to the agent',
6262
});
6363

6464
// Terminate the agent
6565
agentMessage({
66-
instanceId: "agent-instance-id",
66+
instanceId: 'agent-instance-id',
6767
terminate: true,
68-
description: "Terminating the agent",
68+
description: 'Terminating the agent',
6969
});
7070
```
7171

@@ -74,15 +74,15 @@ agentMessage({
7474
```typescript
7575
// Start multiple sub-agents
7676
const agent1 = agentStart({
77-
description: "Agent 1",
78-
goal: "Implement feature A",
79-
projectContext: "Project X",
77+
description: 'Agent 1',
78+
goal: 'Implement feature A',
79+
projectContext: 'Project X',
8080
});
8181

8282
const agent2 = agentStart({
83-
description: "Agent 2",
84-
goal: "Implement feature B",
85-
projectContext: "Project X",
83+
description: 'Agent 2',
84+
goal: 'Implement feature B',
85+
projectContext: 'Project X',
8686
});
8787

8888
// Check progress of both agents
@@ -93,27 +93,27 @@ while (!agent1Completed || !agent2Completed) {
9393
if (!agent1Completed) {
9494
const result1 = agentMessage({
9595
instanceId: agent1.instanceId,
96-
description: "Checking Agent 1 progress",
96+
description: 'Checking Agent 1 progress',
9797
});
9898
agent1Completed = result1.completed;
99-
99+
100100
if (agent1Completed) {
101-
console.log("Agent 1 completed with result:", result1.output);
101+
console.log('Agent 1 completed with result:', result1.output);
102102
}
103103
}
104-
104+
105105
if (!agent2Completed) {
106106
const result2 = agentMessage({
107107
instanceId: agent2.instanceId,
108-
description: "Checking Agent 2 progress",
108+
description: 'Checking Agent 2 progress',
109109
});
110110
agent2Completed = result2.completed;
111-
111+
112112
if (agent2Completed) {
113-
console.log("Agent 2 completed with result:", result2.output);
113+
console.log('Agent 2 completed with result:', result2.output);
114114
}
115115
}
116-
116+
117117
// Wait before checking again
118118
if (!agent1Completed || !agent2Completed) {
119119
sleep({ seconds: 5 });
@@ -127,4 +127,4 @@ while (!agent1Completed || !agent2Completed) {
127127
- Use `agentStart` and `agentMessage` for:
128128
- Parallel execution of multiple sub-agents
129129
- Tasks where you need to monitor progress
130-
- Situations where you may need to provide guidance or terminate early
130+
- Situations where you may need to provide guidance or terminate early

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

+22-8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { z } from 'zod';
22
import { zodToJsonSchema } from 'zod-to-json-schema';
33

44
import { Tool } from '../../core/types.js';
5+
56
import { agentStates } from './agentStart.js';
67

78
const parameterSchema = z.object({
@@ -21,9 +22,17 @@ const parameterSchema = z.object({
2122

2223
const returnSchema = z.object({
2324
output: z.string().describe('The current output from the sub-agent'),
24-
completed: z.boolean().describe('Whether the sub-agent has completed its task'),
25-
error: z.string().optional().describe('Error message if the sub-agent encountered an error'),
26-
terminated: z.boolean().optional().describe('Whether the sub-agent was terminated by this message'),
25+
completed: z
26+
.boolean()
27+
.describe('Whether the sub-agent has completed its task'),
28+
error: z
29+
.string()
30+
.optional()
31+
.describe('Error message if the sub-agent encountered an error'),
32+
terminated: z
33+
.boolean()
34+
.optional()
35+
.describe('Whether the sub-agent was terminated by this message'),
2736
});
2837

2938
type Parameters = z.infer<typeof parameterSchema>;
@@ -66,7 +75,7 @@ export const agentMessageTool: Tool<Parameters, ReturnType> = {
6675
if (terminate) {
6776
agentState.aborted = true;
6877
agentState.completed = true;
69-
78+
7079
return {
7180
output: agentState.output || 'Sub-agent terminated before completion',
7281
completed: true,
@@ -78,14 +87,17 @@ export const agentMessageTool: Tool<Parameters, ReturnType> = {
7887
// In a more advanced implementation, this could inject the guidance
7988
// into the agent's execution context
8089
if (guidance) {
81-
logger.info(`Guidance provided to sub-agent ${instanceId}: ${guidance}`);
90+
logger.info(
91+
`Guidance provided to sub-agent ${instanceId}: ${guidance}`,
92+
);
8293
// This is a placeholder for future implementation
8394
// In a real implementation, we would need to interrupt the agent's
8495
// execution and inject this guidance
8596
}
8697

8798
// Get the current output
88-
const output = agentState.result?.result || agentState.output || 'No output yet';
99+
const output =
100+
agentState.result?.result || agentState.output || 'No output yet';
89101

90102
return {
91103
output,
@@ -104,7 +116,9 @@ export const agentMessageTool: Tool<Parameters, ReturnType> = {
104116
}
105117

106118
const errorMessage = String(error);
107-
logger.error(`Unknown error during sub-agent interaction: ${errorMessage}`);
119+
logger.error(
120+
`Unknown error during sub-agent interaction: ${errorMessage}`,
121+
);
108122
return {
109123
output: '',
110124
completed: false,
@@ -129,4 +143,4 @@ export const agentMessageTool: Tool<Parameters, ReturnType> = {
129143
logger.info('Sub-agent is still running');
130144
}
131145
},
132-
};
146+
};

packages/agent/src/tools/interaction/agentStart.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
getModel,
88
} from '../../core/toolAgent/config.js';
99
import { toolAgent } from '../../core/toolAgent/toolAgentCore.js';
10-
import { Tool, ToolAgentResult, ToolContext } from '../../core/types.js';
10+
import { ToolAgentResult } from '../../core/toolAgent/types.js';
11+
import { Tool, ToolContext } from '../../core/types.js';
1112
import { getTools } from '../getTools.js';
1213

1314
// Define AgentState type
@@ -48,7 +49,9 @@ const parameterSchema = z.object({
4849
enableUserPrompt: z
4950
.boolean()
5051
.optional()
51-
.describe('Whether to allow the sub-agent to use the userPrompt tool (default: false)'),
52+
.describe(
53+
'Whether to allow the sub-agent to use the userPrompt tool (default: false)',
54+
),
5255
});
5356

5457
const returnSchema = z.object({
@@ -130,13 +133,14 @@ export const agentStartTool: Tool<Parameters, ReturnType> = {
130133
agentStates.set(instanceId, agentState);
131134

132135
// Start the agent in a separate promise that we don't await
136+
// eslint-disable-next-line promise/catch-or-return
133137
Promise.resolve().then(async () => {
134138
try {
135139
const result = await toolAgent(prompt, tools, subAgentConfig, {
136140
...context,
137141
workingDirectory: workingDirectory ?? context.workingDirectory,
138142
});
139-
143+
140144
// Update agent state with the result
141145
const state = agentStates.get(instanceId);
142146
if (state && !state.aborted) {
@@ -152,6 +156,7 @@ export const agentStartTool: Tool<Parameters, ReturnType> = {
152156
state.error = error instanceof Error ? error.message : String(error);
153157
}
154158
}
159+
return true;
155160
});
156161

157162
return {
@@ -165,4 +170,4 @@ export const agentStartTool: Tool<Parameters, ReturnType> = {
165170
logReturns: (output, { logger }) => {
166171
logger.info(`Sub-agent started with instance ID: ${output.instanceId}`);
167172
},
168-
};
173+
};

packages/agent/src/tools/interaction/__tests__/agentTools.test.ts packages/agent/src/tools/interaction/agentTools.test.ts

+17-19
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,29 @@
11
import { describe, expect, it, vi } from 'vitest';
22

3-
import { agentMessageTool } from '../agentMessage.js';
4-
import { agentStartTool, agentStates } from '../agentStart.js';
3+
import { TokenTracker } from '../../core/tokens.js';
4+
import { ToolContext } from '../../core/types.js';
5+
import { MockLogger } from '../../utils/mockLogger.js';
6+
7+
import { agentMessageTool } from './agentMessage.js';
8+
import { agentStartTool, agentStates } from './agentStart.js';
59

610
// Mock the toolAgent function
7-
vi.mock('../../../core/toolAgent/toolAgentCore.js', () => ({
11+
vi.mock('../../core/toolAgent/toolAgentCore.js', () => ({
812
toolAgent: vi.fn().mockResolvedValue({
913
result: 'Mock agent result',
1014
interactions: 1,
1115
}),
1216
}));
1317

1418
// Mock context
15-
const mockContext = {
16-
logger: {
17-
info: vi.fn(),
18-
verbose: vi.fn(),
19-
error: vi.fn(),
20-
debug: vi.fn(),
21-
warn: vi.fn(),
22-
},
23-
tokenTracker: {
24-
tokenUsage: {
25-
add: vi.fn(),
26-
},
27-
},
19+
const mockContext: ToolContext = {
20+
logger: new MockLogger(),
21+
tokenTracker: new TokenTracker(),
2822
workingDirectory: '/test',
23+
headless: true,
24+
userSession: false,
25+
pageFilter: 'none',
26+
githubMode: false,
2927
};
3028

3129
describe('Agent Tools', () => {
@@ -46,7 +44,7 @@ describe('Agent Tools', () => {
4644

4745
// Verify the agent state was created
4846
expect(agentStates.has(result.instanceId)).toBe(true);
49-
47+
5048
const state = agentStates.get(result.instanceId);
5149
expect(state).toHaveProperty('goal', 'Test the agent tools');
5250
expect(state).toHaveProperty('prompt');
@@ -116,11 +114,11 @@ describe('Agent Tools', () => {
116114

117115
expect(messageResult).toHaveProperty('terminated', true);
118116
expect(messageResult).toHaveProperty('completed', true);
119-
117+
120118
// Verify the agent state was updated
121119
const state = agentStates.get(startResult.instanceId);
122120
expect(state).toHaveProperty('aborted', true);
123121
expect(state).toHaveProperty('completed', true);
124122
});
125123
});
126-
});
124+
});

0 commit comments

Comments
 (0)