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
10 changes: 5 additions & 5 deletions src/commands/$default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const builder = (yargs) => {
};

export const handler = async (argv: ArgumentsCamelCase<Options>) => {
const logger = new Logger({ name: "Default", color: "white" });
const logger = new Logger({ name: "Default" });
const require = createRequire(import.meta.url);
const packageInfo = require("../../package.json");

Expand All @@ -33,7 +33,7 @@ export const handler = async (argv: ArgumentsCamelCase<Options>) => {
"WARNING: This tool can do anything on your command line that you ask it to.",
"It can delete files, install software, and even send data to remote servers.",
"It is a powerful tool that should be used with caution.",
"By using this tool, you agree that the authors and contributors are not responsible for any damage that may occur as a result of using this tool."
"By using this tool, you agree that the authors and contributors are not responsible for any damage that may occur as a result of using this tool.",
);
try {
// Early API key check
Expand All @@ -50,7 +50,7 @@ export const handler = async (argv: ArgumentsCamelCase<Options>) => {
prompt = await fs.readFile(argv.file, "utf-8");
} catch (error: any) {
logger.error(
`Failed to read prompt file: ${argv.file}, ${error?.message}`
`Failed to read prompt file: ${argv.file}, ${error?.message}`,
);
process.exit(1);
}
Expand All @@ -65,7 +65,7 @@ export const handler = async (argv: ArgumentsCamelCase<Options>) => {

try {
logger.info(
"Type your request below or 'help' for usage information. Use Ctrl+C to exit."
"Type your request below or 'help' for usage information. Use Ctrl+C to exit.",
);
prompt = await readline.question("\n> ");
} finally {
Expand All @@ -78,7 +78,7 @@ export const handler = async (argv: ArgumentsCamelCase<Options>) => {

if (!prompt) {
logger.error(
"No prompt provided. Either specify a prompt, use --promptFile, or run in --interactive mode."
"No prompt provided. Either specify a prompt, use --promptFile, or run in --interactive mode.",
);
process.exit(1);
}
Expand Down
13 changes: 6 additions & 7 deletions src/commands/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ function formatSchema(schema: {
if (schema.properties) {
for (const [paramName, param] of Object.entries(schema.properties)) {
const required = schema.required?.includes(paramName)
? " (required)"
? ""
: " (optional)";
const description =
(param as any).description || "No description available";
const description = (param as any).description || "";
output += `${paramName}${required}: ${description}\n`;

if ((param as any).type) {
Expand Down Expand Up @@ -61,8 +60,8 @@ export const handler = async () => {
tool.parameters as {
properties?: Record<string, JsonSchema7Type>;
required?: string[];
}
)
},
),
);

// Returns section
Expand All @@ -73,8 +72,8 @@ export const handler = async () => {
tool.returns as {
properties?: Record<string, JsonSchema7Type>;
required?: string[];
}
)
},
),
);
} else {
console.log(" Type: any");
Expand Down
16 changes: 8 additions & 8 deletions src/core/toolAgent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { executeToolCall } from "./executeToolCall.js";
import { Tool } from "./types.js";
import { Logger } from "../utils/logger.js";
import { toolAgent } from "./toolAgent.js";
import { MockLogger } from "../utils/mockLogger.js";

const logger = new Logger({ name: "toolAgent", logLevel: "warn" });
const logger = new MockLogger();

// Mock configuration for testing
const testConfig = {
Expand Down Expand Up @@ -96,7 +96,7 @@ describe("toolAgent", () => {
input: { input: "test" },
},
[mockTool],
logger,
logger
);

expect(result.includes("Processed: test")).toBeTruthy();
Expand All @@ -111,8 +111,8 @@ describe("toolAgent", () => {
input: {},
},
[mockTool],
logger,
),
logger
)
).rejects.toThrow("No tool with the name 'nonexistentTool' exists.");
});

Expand Down Expand Up @@ -142,8 +142,8 @@ describe("toolAgent", () => {
input: {},
},
[errorTool],
logger,
),
logger
)
).rejects.toThrow("Deliberate failure");
});

Expand All @@ -153,7 +153,7 @@ describe("toolAgent", () => {
"Test prompt",
[sequenceCompleteTool],
logger,
testConfig,
testConfig
);

expect(result.result).toBe("Test complete");
Expand Down
14 changes: 10 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as dotenv from "dotenv";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import { Logger } from "./utils/logger.js";
import { Logger, LogLevel } from "./utils/logger.js";
import { createRequire } from "module";
import { join } from "path";
import { fileURLToPath } from "url";
Expand All @@ -16,14 +16,21 @@ import { getTools } from "./tools/getTools.js";

sourceMapSupport.install();

const nameToLogIndex = (logLevelName: string) => {
// look up the log level name in the enum to get the value
return LogLevel[logLevelName as keyof typeof LogLevel];
};

const main = async () => {
dotenv.config();

const logger = new Logger({ name: "Main", color: "white" });
const logger = new Logger({ name: "Main" });

const updateMessage = await checkForUpdates();
if (updateMessage) {
console.log();
logger.info(updateMessage);
console.log();
}

// Error handling
Expand Down Expand Up @@ -60,8 +67,7 @@ const main = async () => {
// Set up logger with the specified log level
argv.logger = new Logger({
name: packageInfo.name!,
color: "white",
logLevel: argv.log,
logLevel: nameToLogIndex(argv.log),
});
argv.tools = await getTools();
})
Expand Down
5 changes: 0 additions & 5 deletions src/test-eslint.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/tools/interaction/subAgent.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { subAgentTool } from "./subAgent.js";
import { Logger } from "../../utils/logger.js";
import { MockLogger } from "../../utils/mockLogger.js";

const logger = new Logger({ name: "subAgent", logLevel: "warn" });
const logger = new MockLogger();

// Mock Anthropic client response
const mockResponse = {
Expand Down
4 changes: 2 additions & 2 deletions src/tools/io/readFile.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { describe, it, expect } from "vitest";
import { Logger } from "../../utils/logger.js";
import { readFileTool } from "./readFile.js";
import { MockLogger } from "../../utils/mockLogger.js";

const logger = new Logger({ name: "readFile", logLevel: "warn" });
const logger = new MockLogger();

describe("readFile", () => {
it("should read a file", async () => {
Expand Down
4 changes: 2 additions & 2 deletions src/tools/io/updateFile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { join } from "path";
import { randomUUID } from "crypto";
import { mkdtemp } from "fs/promises";
import { tmpdir } from "os";
import { Logger } from "../../utils/logger.js";
import { updateFileTool } from "./updateFile.js";
import { readFileTool } from "./readFile.js";
import { shellExecuteTool } from "../system/shellExecute.js";
import { MockLogger } from "../../utils/mockLogger.js";

const logger = new Logger({ name: "updateFile", logLevel: "warn" });
const logger = new MockLogger();

describe("updateFile", () => {
let testDir: string;
Expand Down
4 changes: 2 additions & 2 deletions src/tools/system/shellExecute.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { describe, it, expect } from "vitest";
import { Logger } from "../../utils/logger.js";
import { shellExecuteTool } from "./shellExecute.js";
import { MockLogger } from "../../utils/mockLogger.js";

const logger = new Logger({ name: "shellExecute", logLevel: "warn" });
const logger = new MockLogger();

describe("shellExecute", () => {
it("should execute shell commands", async () => {
Expand Down
32 changes: 16 additions & 16 deletions src/tools/system/shellMessage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import { processStates, shellStartTool } from "./shellStart.js";
import { MockLogger } from "../../utils/mockLogger.js";
import { shellMessageTool } from "./shellMessage.js";

const logger = new MockLogger();

// Helper function to get instanceId from shellStart result
const getInstanceId = (
result: Awaited<ReturnType<typeof shellStartTool.execute>>
result: Awaited<ReturnType<typeof shellStartTool.execute>>,
) => {
if (result.mode === "async") {
return result.instanceId;
Expand All @@ -15,8 +17,6 @@ const getInstanceId = (

// eslint-disable-next-line max-lines-per-function
describe("shellMessageTool", () => {
const mockLogger = new MockLogger();

let testInstanceId = "";

beforeEach(() => {
Expand All @@ -38,7 +38,7 @@ describe("shellMessageTool", () => {
description: "Test interactive process",
timeout: 50, // Force async mode for interactive process
},
{ logger: mockLogger }
{ logger },
);

testInstanceId = getInstanceId(startResult);
Expand All @@ -50,7 +50,7 @@ describe("shellMessageTool", () => {
stdin: "hello world",
description: "Test interaction",
},
{ logger: mockLogger }
{ logger },
);

expect(result.stdout).toBe("hello world");
Expand All @@ -64,7 +64,7 @@ describe("shellMessageTool", () => {
instanceId: "nonexistent-id",
description: "Test invalid process",
},
{ logger: mockLogger }
{ logger },
);

expect(result.error).toBeDefined();
Expand All @@ -79,7 +79,7 @@ describe("shellMessageTool", () => {
description: "Test completion",
timeout: 0, // Force async mode
},
{ logger: mockLogger }
{ logger },
);

const instanceId = getInstanceId(startResult);
Expand All @@ -92,7 +92,7 @@ describe("shellMessageTool", () => {
instanceId,
description: "Check completion",
},
{ logger: mockLogger }
{ logger },
);

expect(result.completed).toBe(true);
Expand All @@ -108,7 +108,7 @@ describe("shellMessageTool", () => {
description: "Test SIGTERM handling",
timeout: 0, // Force async mode
},
{ logger: mockLogger }
{ logger },
);

const instanceId = getInstanceId(startResult);
Expand All @@ -119,7 +119,7 @@ describe("shellMessageTool", () => {
signal: "SIGTERM",
description: "Send SIGTERM",
},
{ logger: mockLogger }
{ logger },
);
expect(result.signaled).toBe(true);

Expand All @@ -130,7 +130,7 @@ describe("shellMessageTool", () => {
instanceId,
description: "Check on status",
},
{ logger: mockLogger }
{ logger },
);

expect(result2.completed).toBe(true);
Expand All @@ -145,7 +145,7 @@ describe("shellMessageTool", () => {
description: "Test signal handling on terminated process",
timeout: 0, // Force async mode
},
{ logger: mockLogger }
{ logger },
);

const instanceId = getInstanceId(startResult);
Expand All @@ -157,7 +157,7 @@ describe("shellMessageTool", () => {
signal: "SIGTERM",
description: "Send signal to terminated process",
},
{ logger: mockLogger }
{ logger },
);

expect(result.signaled).toBe(true);
Expand All @@ -172,7 +172,7 @@ describe("shellMessageTool", () => {
description: "Test signal flag verification",
timeout: 0, // Force async mode
},
{ logger: mockLogger }
{ logger },
);

const instanceId = getInstanceId(startResult);
Expand All @@ -184,7 +184,7 @@ describe("shellMessageTool", () => {
signal: "SIGTERM",
description: "Send SIGTERM",
},
{ logger: mockLogger }
{ logger },
);

await new Promise((resolve) => setTimeout(resolve, 50));
Expand All @@ -195,7 +195,7 @@ describe("shellMessageTool", () => {
instanceId,
description: "Check signal state",
},
{ logger: mockLogger }
{ logger },
);

expect(checkResult.signaled).toBe(true);
Expand Down
8 changes: 4 additions & 4 deletions src/tools/system/shellMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ const returnSchema = z
signaled: z.boolean().optional(),
})
.describe(
"Process interaction results including stdout, stderr, and completion status"
"Process interaction results including stdout, stderr, and completion status",
);

type Parameters = z.infer<typeof parameterSchema>;
Expand All @@ -112,10 +112,10 @@ export const shellMessageTool: Tool<Parameters, ReturnType> = {

execute: async (
{ instanceId, stdin, signal },
{ logger }
{ logger },
): Promise<ReturnType> => {
logger.verbose(
`Interacting with shell process ${instanceId}${stdin ? " with input" : ""}${signal ? ` with signal ${signal}` : ""}`
`Interacting with shell process ${instanceId}${stdin ? " with input" : ""}${signal ? ` with signal ${signal}` : ""}`,
);

try {
Expand Down Expand Up @@ -197,7 +197,7 @@ export const shellMessageTool: Tool<Parameters, ReturnType> = {

logParameters: (input, { logger }) => {
logger.info(
`Interacting with process ${input.instanceId}, ${input.description}`
`Interacting with process ${input.instanceId}, ${input.description}`,
);
},
logReturns: () => {},
Expand Down
Loading