MASSIVE IMPROVCE
This commit is contained in:
+8
-19
@@ -8,17 +8,12 @@ import { pipeline } from "node:stream/promises";
|
||||
import { query, type SDKMessage } from "@anthropic-ai/claude-agent-sdk";
|
||||
import packageJson from "../package.json" with { type: "json" };
|
||||
import { log } from "../utils/cli.ts";
|
||||
import { type Agent, instructions } from "./shared.ts";
|
||||
|
||||
let cachedCliPath: string | undefined;
|
||||
|
||||
export const claude: Agent = {
|
||||
install: async (): Promise<string> => {
|
||||
if (cachedCliPath) {
|
||||
log.info(`Using cached Claude Code CLI at ${cachedCliPath}`);
|
||||
return cachedCliPath;
|
||||
}
|
||||
import { agent, instructions } from "./shared.ts";
|
||||
|
||||
export const claude = agent({
|
||||
name: "claude",
|
||||
inputKey: "anthropic_api_key",
|
||||
install: async () => {
|
||||
// Get the SDK version from package.json and resolve to actual version
|
||||
const versionRange = packageJson.dependencies["@anthropic-ai/claude-agent-sdk"] || "latest";
|
||||
let sdkVersion: string;
|
||||
@@ -83,8 +78,6 @@ export const claude: Agent = {
|
||||
if (!existsSync(cliPath)) {
|
||||
throw new Error(`cli.js not found in extracted package at ${cliPath}`);
|
||||
}
|
||||
|
||||
cachedCliPath = cliPath;
|
||||
log.info(`✓ Claude Code CLI installed at ${cliPath}`);
|
||||
return cliPath;
|
||||
} catch (error) {
|
||||
@@ -97,19 +90,15 @@ export const claude: Agent = {
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
run: async ({ prompt, mcpServers, apiKey }) => {
|
||||
run: async ({ prompt, mcpServers, apiKey, cliPath }) => {
|
||||
process.env.ANTHROPIC_API_KEY = apiKey;
|
||||
|
||||
if (!cachedCliPath) {
|
||||
throw new Error("Claude CLI not installed. Call install() before run().");
|
||||
}
|
||||
|
||||
const queryInstance = query({
|
||||
prompt: `${instructions}\n\n****** USER PROMPT ******\n${prompt}`,
|
||||
options: {
|
||||
permissionMode: "bypassPermissions",
|
||||
mcpServers,
|
||||
pathToClaudeCodeExecutable: cachedCliPath,
|
||||
pathToClaudeCodeExecutable: cliPath,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -124,7 +113,7 @@ export const claude: Agent = {
|
||||
output: "",
|
||||
};
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
type SDKMessageType = SDKMessage["type"];
|
||||
|
||||
|
||||
+6
-4
@@ -1,10 +1,12 @@
|
||||
import { spawnSync } from "node:child_process";
|
||||
|
||||
import { findCliPath, log } from "../utils/cli.ts";
|
||||
import { type Agent, instructions } from "./shared.ts";
|
||||
import { agent, instructions } from "./shared.ts";
|
||||
|
||||
export const codex: Agent = {
|
||||
install: async (): Promise<string> => {
|
||||
export const codex = agent({
|
||||
name: "codex",
|
||||
inputKey: "openai_api_key",
|
||||
install: async () => {
|
||||
const globalCodexPath = findCliPath("codex");
|
||||
if (globalCodexPath) {
|
||||
log.info(`Using global Codex CLI at ${globalCodexPath}`);
|
||||
@@ -133,4 +135,4 @@ export const codex: Agent = {
|
||||
};
|
||||
}
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
+3
-3
@@ -1,9 +1,9 @@
|
||||
import type { AgentName } from "../main.ts";
|
||||
import { claude } from "./claude.ts";
|
||||
import { codex } from "./codex.ts";
|
||||
import type { Agent } from "./shared.ts";
|
||||
|
||||
export const agents = {
|
||||
claude,
|
||||
codex,
|
||||
} as const satisfies Record<AgentName, Agent>;
|
||||
} as const;
|
||||
|
||||
export type AgentInputKey = (typeof agents)[keyof typeof agents]["inputKey"];
|
||||
|
||||
+5
-2
@@ -23,12 +23,15 @@ export interface AgentConfig {
|
||||
cliPath: string;
|
||||
}
|
||||
|
||||
type InputKey = "anthropic_api_key" | "openai_api_key";
|
||||
export const agent = <const agent extends Agent>(agent: agent): agent => {
|
||||
return agent;
|
||||
};
|
||||
|
||||
export type Agent = {
|
||||
name: string;
|
||||
inputKey: string;
|
||||
install: () => Promise<string>;
|
||||
run: (config: AgentConfig) => Promise<AgentResult>;
|
||||
inputKey: InputKey;
|
||||
};
|
||||
|
||||
export const instructions = `
|
||||
|
||||
Reference in New Issue
Block a user