mirror of
https://github.com/musistudio/claude-code-router.git
synced 2026-02-03 07:10:51 +08:00
feat: non interactive spawn compatible
This commit is contained in:
@@ -1,21 +1,29 @@
|
|||||||
import { spawn } from "child_process";
|
import { spawn, type StdioOptions } from "child_process";
|
||||||
import {
|
|
||||||
incrementReferenceCount,
|
|
||||||
decrementReferenceCount,
|
|
||||||
} from "./processCheck";
|
|
||||||
import { closeService } from "./close";
|
|
||||||
import { readConfigFile } from ".";
|
import { readConfigFile } from ".";
|
||||||
|
import { closeService } from "./close";
|
||||||
|
import {
|
||||||
|
decrementReferenceCount,
|
||||||
|
incrementReferenceCount,
|
||||||
|
} from "./processCheck";
|
||||||
|
|
||||||
export async function executeCodeCommand(args: string[] = []) {
|
export async function executeCodeCommand(args: string[] = []) {
|
||||||
// Set environment variables
|
// Set environment variables
|
||||||
const config = await readConfigFile();
|
const config = await readConfigFile();
|
||||||
const env = {
|
const env: Record<string, string> = {
|
||||||
...process.env,
|
...process.env,
|
||||||
ANTHROPIC_AUTH_TOKEN: "test",
|
ANTHROPIC_AUTH_TOKEN: "test",
|
||||||
ANTHROPIC_BASE_URL: `http://127.0.0.1:${config.PORT || 3456}`,
|
ANTHROPIC_BASE_URL: `http://127.0.0.1:${config.PORT || 3456}`,
|
||||||
API_TIMEOUT_MS: String(config.API_TIMEOUT_MS ?? 600000), // Default to 10 minutes if not set
|
API_TIMEOUT_MS: String(config.API_TIMEOUT_MS ?? 600000), // Default to 10 minutes if not set
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Non-interactive mode for automation environments
|
||||||
|
if (config.NON_INTERACTIVE_MODE) {
|
||||||
|
env.CI = "true";
|
||||||
|
env.FORCE_COLOR = "0";
|
||||||
|
env.NODE_NO_READLINE = "1";
|
||||||
|
env.TERM = "dumb";
|
||||||
|
}
|
||||||
|
|
||||||
// Set ANTHROPIC_SMALL_FAST_MODEL if it exists in config
|
// Set ANTHROPIC_SMALL_FAST_MODEL if it exists in config
|
||||||
if (config?.ANTHROPIC_SMALL_FAST_MODEL) {
|
if (config?.ANTHROPIC_SMALL_FAST_MODEL) {
|
||||||
env.ANTHROPIC_SMALL_FAST_MODEL = config.ANTHROPIC_SMALL_FAST_MODEL;
|
env.ANTHROPIC_SMALL_FAST_MODEL = config.ANTHROPIC_SMALL_FAST_MODEL;
|
||||||
@@ -31,12 +39,23 @@ export async function executeCodeCommand(args: string[] = []) {
|
|||||||
|
|
||||||
// Execute claude command
|
// Execute claude command
|
||||||
const claudePath = process.env.CLAUDE_PATH || "claude";
|
const claudePath = process.env.CLAUDE_PATH || "claude";
|
||||||
|
|
||||||
|
// 🔥 CONFIG-DRIVEN: stdio configuration based on environment
|
||||||
|
const stdioConfig: StdioOptions = config.NON_INTERACTIVE_MODE
|
||||||
|
? ["pipe", "inherit", "inherit"] // Pipe stdin for non-interactive
|
||||||
|
: "inherit"; // Default inherited behavior
|
||||||
|
|
||||||
const claudeProcess = spawn(claudePath, args, {
|
const claudeProcess = spawn(claudePath, args, {
|
||||||
env,
|
env,
|
||||||
stdio: "inherit",
|
stdio: stdioConfig,
|
||||||
shell: true,
|
shell: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Close stdin for non-interactive mode
|
||||||
|
if (config.NON_INTERACTIVE_MODE) {
|
||||||
|
claudeProcess.stdin?.end();
|
||||||
|
}
|
||||||
|
|
||||||
claudeProcess.on("error", (error) => {
|
claudeProcess.on("error", (error) => {
|
||||||
console.error("Failed to start claude command:", error.message);
|
console.error("Failed to start claude command:", error.message);
|
||||||
console.log(
|
console.log(
|
||||||
|
|||||||
Reference in New Issue
Block a user