Skip to content

Commit 867755f

Browse files
committed
test: raise logLevel above info for tests to clean up output.
1 parent ddb3bfe commit 867755f

File tree

7 files changed

+107
-120
lines changed

7 files changed

+107
-120
lines changed

src/core/executeToolCall.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ export const executeToolCall = async (
1818

1919
const tool = tools.find((t) => t.name === toolCall.name);
2020
if (!tool) {
21-
logger.error(`Tool '${toolCall.name}' not found`);
2221
throw new Error(`No tool with the name '${toolCall.name}' exists.`);
2322
}
2423

src/core/toolAgent.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Tool } from "./types.js";
55
import { Logger } from "../utils/logger.js";
66
import { toolAgent } from "./toolAgent.js";
77

8-
const logger = new Logger({ name: "toolAgent" });
8+
const logger = new Logger({ name: "toolAgent", logLevel: "warn" });
99

1010
// Mock configuration for testing
1111
const testConfig = {
@@ -96,7 +96,7 @@ describe("toolAgent", () => {
9696
input: { input: "test" },
9797
},
9898
[mockTool],
99-
logger,
99+
logger
100100
);
101101

102102
expect(result.includes("Processed: test")).toBeTruthy();
@@ -111,8 +111,8 @@ describe("toolAgent", () => {
111111
input: {},
112112
},
113113
[mockTool],
114-
logger,
115-
),
114+
logger
115+
)
116116
).rejects.toThrow("No tool with the name 'nonexistentTool' exists.");
117117
});
118118

@@ -142,8 +142,8 @@ describe("toolAgent", () => {
142142
input: {},
143143
},
144144
[errorTool],
145-
logger,
146-
),
145+
logger
146+
)
147147
).rejects.toThrow("Deliberate failure");
148148
});
149149

@@ -153,7 +153,7 @@ describe("toolAgent", () => {
153153
"Test prompt",
154154
[sequenceCompleteTool],
155155
logger,
156-
testConfig,
156+
testConfig
157157
);
158158

159159
expect(result.result).toBe("Test complete");

src/core/toolAgent.ts

Lines changed: 96 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -169,126 +169,114 @@ export const toolAgent = async (
169169
let totalOutputTokens = 0;
170170
let interactions = 0;
171171

172-
try {
173-
const apiKey = process.env.ANTHROPIC_API_KEY;
174-
if (!apiKey) throw new Error(getAnthropicApiKeyError());
172+
const apiKey = process.env.ANTHROPIC_API_KEY;
173+
if (!apiKey) throw new Error(getAnthropicApiKeyError());
175174

176-
const client = new Anthropic({ apiKey });
177-
const messages: Message[] = [
178-
{
179-
role: "user",
180-
content: [{ type: "text", text: initialPrompt }],
181-
},
182-
];
175+
const client = new Anthropic({ apiKey });
176+
const messages: Message[] = [
177+
{
178+
role: "user",
179+
content: [{ type: "text", text: initialPrompt }],
180+
},
181+
];
183182

184-
logger.debug("User message:", initialPrompt);
183+
logger.debug("User message:", initialPrompt);
185184

186-
// Get the system prompt once at the start
187-
const systemPrompt = await config.getSystemPrompt();
185+
// Get the system prompt once at the start
186+
const systemPrompt = await config.getSystemPrompt();
188187

189-
for (let i = 0; i < config.maxIterations; i++) {
190-
logger.verbose(
191-
`Requesting completion ${i + 1} with ${messages.length} messages with ${
192-
JSON.stringify(messages).length
193-
} bytes`
194-
);
195-
196-
interactions++;
197-
const response = await client.messages.create({
198-
model: config.model,
199-
max_tokens: config.maxTokens,
200-
temperature: config.temperature,
201-
messages,
202-
system: systemPrompt,
203-
tools: tools.map((t) => ({
204-
name: t.name,
205-
description: t.description,
206-
input_schema: t.parameters as Anthropic.Tool.InputSchema,
207-
})),
208-
tool_choice: { type: "auto" },
209-
});
210-
211-
if (!response.content.length) {
212-
const result = {
213-
result:
214-
"Agent returned empty message implying it is done its given task",
215-
tokens: {
216-
input: totalInputTokens,
217-
output: totalOutputTokens,
218-
},
219-
interactions,
220-
};
221-
logger.verbose(
222-
`Agent completed with ${result.tokens.input} input tokens, ${result.tokens.output} output tokens in ${result.interactions} interactions`
223-
);
224-
return result;
225-
}
188+
for (let i = 0; i < config.maxIterations; i++) {
189+
logger.verbose(
190+
`Requesting completion ${i + 1} with ${messages.length} messages with ${
191+
JSON.stringify(messages).length
192+
} bytes`
193+
);
226194

227-
totalInputTokens += response.usage.input_tokens;
228-
totalOutputTokens += response.usage.output_tokens;
195+
interactions++;
196+
const response = await client.messages.create({
197+
model: config.model,
198+
max_tokens: config.maxTokens,
199+
temperature: config.temperature,
200+
messages,
201+
system: systemPrompt,
202+
tools: tools.map((t) => ({
203+
name: t.name,
204+
description: t.description,
205+
input_schema: t.parameters as Anthropic.Tool.InputSchema,
206+
})),
207+
tool_choice: { type: "auto" },
208+
});
209+
210+
if (!response.content.length) {
211+
const result = {
212+
result:
213+
"Agent returned empty message implying it is done its given task",
214+
tokens: {
215+
input: totalInputTokens,
216+
output: totalOutputTokens,
217+
},
218+
interactions,
219+
};
229220
logger.verbose(
230-
` Token usage: ${response.usage.input_tokens} input, ${response.usage.output_tokens} output`
221+
`Agent completed with ${result.tokens.input} input tokens, ${result.tokens.output} output tokens in ${result.interactions} interactions`
231222
);
223+
return result;
224+
}
232225

233-
const { content, toolCalls } = processResponse(response);
234-
messages.push({ role: "assistant", content });
235-
236-
// Log the assistant's message
237-
const assistantMessage = content
238-
.filter((c) => c.type === "text")
239-
.map((c) => (c as TextContent).text)
240-
.join("\\n");
241-
if (assistantMessage) {
242-
logger.info(assistantMessage);
243-
}
226+
totalInputTokens += response.usage.input_tokens;
227+
totalOutputTokens += response.usage.output_tokens;
228+
logger.verbose(
229+
` Token usage: ${response.usage.input_tokens} input, ${response.usage.output_tokens} output`
230+
);
244231

245-
const { sequenceCompleted, completionResult } = await executeTools(
246-
toolCalls,
247-
tools,
248-
messages,
249-
logger
250-
);
232+
const { content, toolCalls } = processResponse(response);
233+
messages.push({ role: "assistant", content });
251234

252-
if (sequenceCompleted) {
253-
const result = {
254-
result:
255-
completionResult ??
256-
"Sequence explicitly completed with an empty result",
257-
tokens: {
258-
input: totalInputTokens,
259-
output: totalOutputTokens,
260-
},
261-
interactions,
262-
};
263-
logger.verbose(
264-
`Agent completed with ${result.tokens.input} input tokens, ${result.tokens.output} output tokens in ${result.interactions} interactions`
265-
);
266-
return result;
267-
}
235+
// Log the assistant's message
236+
const assistantMessage = content
237+
.filter((c) => c.type === "text")
238+
.map((c) => (c as TextContent).text)
239+
.join("\\n");
240+
if (assistantMessage) {
241+
logger.info(assistantMessage);
268242
}
269243

270-
logger.warn("Maximum iterations reached");
271-
const result = {
272-
result:
273-
"Maximum sub-agent iterations reach without successful completion",
274-
tokens: {
275-
input: totalInputTokens,
276-
output: totalOutputTokens,
277-
},
278-
interactions,
279-
};
280-
logger.verbose(
281-
`Agent completed with ${result.tokens.input} input tokens, ${result.tokens.output} output tokens in ${result.interactions} interactions`
244+
const { sequenceCompleted, completionResult } = await executeTools(
245+
toolCalls,
246+
tools,
247+
messages,
248+
logger
282249
);
283-
return result;
284-
} catch (error) {
285-
const errorMessage =
286-
error instanceof Error ? error.message : "Unknown error";
287-
logger.error(
288-
"Agent execution failed",
289-
errorMessage,
290-
(error as Error)?.stack
291-
);
292-
throw error;
250+
251+
if (sequenceCompleted) {
252+
const result = {
253+
result:
254+
completionResult ??
255+
"Sequence explicitly completed with an empty result",
256+
tokens: {
257+
input: totalInputTokens,
258+
output: totalOutputTokens,
259+
},
260+
interactions,
261+
};
262+
logger.verbose(
263+
`Agent completed with ${result.tokens.input} input tokens, ${result.tokens.output} output tokens in ${result.interactions} interactions`
264+
);
265+
return result;
266+
}
293267
}
268+
269+
logger.warn("Maximum iterations reached");
270+
const result = {
271+
result: "Maximum sub-agent iterations reach without successful completion",
272+
tokens: {
273+
input: totalInputTokens,
274+
output: totalOutputTokens,
275+
},
276+
interactions,
277+
};
278+
logger.verbose(
279+
`Agent completed with ${result.tokens.input} input tokens, ${result.tokens.output} output tokens in ${result.interactions} interactions`
280+
);
281+
return result;
294282
};

src/tools/interaction/subAgent.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
22
import { subAgentTool } from "./subAgent.js";
33
import { Logger } from "../../utils/logger.js";
44

5-
const logger = new Logger({ name: "subAgent" });
5+
const logger = new Logger({ name: "subAgent", logLevel: "warn" });
66

77
// Mock Anthropic client response
88
const mockResponse = {

src/tools/io/readFile.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { describe, it, expect } from "vitest";
22
import { Logger } from "../../utils/logger.js";
33
import { readFileTool } from "./readFile.js";
44

5-
const logger = new Logger({ name: "readFile" });
5+
const logger = new Logger({ name: "readFile", logLevel: "warn" });
66

77
describe("readFile", () => {
88
it("should read a file", async () => {

src/tools/io/updateFile.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { updateFileTool } from "./updateFile.js";
99
import { readFileTool } from "./readFile.js";
1010
import { shellExecuteTool } from "../system/shellExecute.js";
1111

12-
const logger = new Logger({ name: "updateFile" });
12+
const logger = new Logger({ name: "updateFile", logLevel: "warn" });
1313

1414
describe("updateFile", () => {
1515
let testDir: string;

src/tools/system/shellExecute.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { describe, it, expect } from "vitest";
22
import { Logger } from "../../utils/logger.js";
33
import { shellExecuteTool } from "./shellExecute.js";
44

5-
const logger = new Logger({ name: "shellExecute" });
5+
const logger = new Logger({ name: "shellExecute", logLevel: "warn" });
66

77
describe("shellExecute", () => {
88
it("should execute shell commands", async () => {

0 commit comments

Comments
 (0)