Move agent override to env
This commit is contained in:
+51
-8
@@ -25,10 +25,20 @@ export const cursor = agent({
|
|||||||
|
|
||||||
log.info("Running Cursor CLI...");
|
log.info("Running Cursor CLI...");
|
||||||
|
|
||||||
|
const startTime = Date.now();
|
||||||
|
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
const child = spawn(
|
const child = spawn(
|
||||||
cliPath,
|
cliPath,
|
||||||
["--print", fullPrompt, "--output-format", "text", "--approve-mcps", "--force"],
|
[
|
||||||
|
"--print",
|
||||||
|
fullPrompt,
|
||||||
|
"--output-format",
|
||||||
|
"stream-json",
|
||||||
|
"--stream-partial-output",
|
||||||
|
"--approve-mcps",
|
||||||
|
"--force",
|
||||||
|
],
|
||||||
{
|
{
|
||||||
cwd: process.cwd(),
|
cwd: process.cwd(),
|
||||||
env: {
|
env: {
|
||||||
@@ -44,16 +54,36 @@ export const cursor = agent({
|
|||||||
|
|
||||||
let stdout = "";
|
let stdout = "";
|
||||||
let stderr = "";
|
let stderr = "";
|
||||||
|
let jsonBuffer = "";
|
||||||
|
|
||||||
child.on("spawn", () => {
|
child.on("spawn", () => {
|
||||||
log.debug("Cursor CLI process spawned");
|
log.debug("Cursor CLI process spawned");
|
||||||
});
|
});
|
||||||
|
|
||||||
child.stdout?.on("data", (data) => {
|
child.stdout?.on("data", async (data) => {
|
||||||
const text = data.toString();
|
const text = data.toString();
|
||||||
stdout += text;
|
stdout += text;
|
||||||
// Stream output in real-time
|
jsonBuffer += text;
|
||||||
process.stdout.write(text);
|
|
||||||
|
// parse ndjson (newline-delimited json)
|
||||||
|
const lines = jsonBuffer.split("\n");
|
||||||
|
// keep last incomplete line in buffer
|
||||||
|
jsonBuffer = lines.pop() || "";
|
||||||
|
|
||||||
|
for (const line of lines) {
|
||||||
|
const trimmedLine = line.trim();
|
||||||
|
if (!trimmedLine) continue;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const event = JSON.parse(trimmedLine);
|
||||||
|
// log everything for now - we'll infer types from real output
|
||||||
|
log.debug(`[cursor event] ${JSON.stringify(event, null, 2)}`);
|
||||||
|
} catch {
|
||||||
|
// if json parse fails, might be partial line or non-json output
|
||||||
|
// log debug info but don't crash
|
||||||
|
log.debug(`failed to parse json line: ${trimmedLine.substring(0, 100)}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
child.stderr?.on("data", (data) => {
|
child.stderr?.on("data", (data) => {
|
||||||
@@ -63,20 +93,32 @@ export const cursor = agent({
|
|||||||
log.warning(text);
|
log.warning(text);
|
||||||
});
|
});
|
||||||
|
|
||||||
child.on("close", (code, signal) => {
|
child.on("close", async (code, signal) => {
|
||||||
|
// process any remaining buffered json
|
||||||
|
if (jsonBuffer.trim()) {
|
||||||
|
try {
|
||||||
|
const event = JSON.parse(jsonBuffer.trim());
|
||||||
|
log.debug(`[cursor event] ${JSON.stringify(event, null, 2)}`);
|
||||||
|
} catch {
|
||||||
|
// ignore parse errors for final buffer
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (signal) {
|
if (signal) {
|
||||||
log.warning(`Cursor CLI terminated by signal: ${signal}`);
|
log.warning(`Cursor CLI terminated by signal: ${signal}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const duration = ((Date.now() - startTime) / 1000).toFixed(1);
|
||||||
|
|
||||||
if (code === 0) {
|
if (code === 0) {
|
||||||
log.success("Cursor CLI completed successfully");
|
log.success(`Cursor CLI completed successfully in ${duration}s`);
|
||||||
resolve({
|
resolve({
|
||||||
success: true,
|
success: true,
|
||||||
output: stdout.trim(),
|
output: stdout.trim(),
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
const errorMessage = stderr || `Cursor CLI exited with code ${code}`;
|
const errorMessage = stderr || `Cursor CLI exited with code ${code}`;
|
||||||
log.error(`Cursor CLI failed: ${errorMessage}`);
|
log.error(`Cursor CLI failed after ${duration}s: ${errorMessage}`);
|
||||||
resolve({
|
resolve({
|
||||||
success: false,
|
success: false,
|
||||||
error: errorMessage,
|
error: errorMessage,
|
||||||
@@ -86,8 +128,9 @@ export const cursor = agent({
|
|||||||
});
|
});
|
||||||
|
|
||||||
child.on("error", (error) => {
|
child.on("error", (error) => {
|
||||||
|
const duration = ((Date.now() - startTime) / 1000).toFixed(1);
|
||||||
const errorMessage = error.message || String(error);
|
const errorMessage = error.message || String(error);
|
||||||
log.error(`Cursor CLI execution failed: ${errorMessage}`);
|
log.error(`Cursor CLI execution failed after ${duration}s: ${errorMessage}`);
|
||||||
resolve({
|
resolve({
|
||||||
success: false,
|
success: false,
|
||||||
error: errorMessage,
|
error: errorMessage,
|
||||||
|
|||||||
@@ -24,7 +24,6 @@ import { setupGitAuth, setupGitBranch, setupGitConfig } from "./utils/setup.ts";
|
|||||||
// runtime validation using agents (needed for ArkType)
|
// runtime validation using agents (needed for ArkType)
|
||||||
// Note: The AgentName type is defined in external.ts, this is the runtime validator
|
// Note: The AgentName type is defined in external.ts, this is the runtime validator
|
||||||
|
|
||||||
const AGENT_OVERRIDE: AgentName | null = "cursor";
|
|
||||||
export const AgentInputKey = type.enumerated(
|
export const AgentInputKey = type.enumerated(
|
||||||
...Object.values(agents).flatMap((agent) => agent.apiKeyNames)
|
...Object.values(agents).flatMap((agent) => agent.apiKeyNames)
|
||||||
);
|
);
|
||||||
@@ -218,15 +217,15 @@ async function resolveAgent(
|
|||||||
repoContext,
|
repoContext,
|
||||||
});
|
});
|
||||||
|
|
||||||
const configuredAgentName =
|
const agentOverride = process.env.AGENT_OVERRIDE as AgentName | undefined;
|
||||||
(process.env.NODE_ENV === "development" && AGENT_OVERRIDE) ||
|
const configuredAgentName = agentOverride || payload.agent || repoSettings.defaultAgent || null;
|
||||||
payload.agent ||
|
|
||||||
repoSettings.defaultAgent ||
|
|
||||||
null;
|
|
||||||
|
|
||||||
if (configuredAgentName) {
|
if (configuredAgentName) {
|
||||||
const agentName = configuredAgentName;
|
const agentName = configuredAgentName;
|
||||||
const agent = agents[agentName];
|
const agent = agents[agentName];
|
||||||
|
if (!agent) {
|
||||||
|
throw new Error(`invalid agent name: ${agentName}`);
|
||||||
|
}
|
||||||
log.info(`Selected configured agent: ${agentName}`);
|
log.info(`Selected configured agent: ${agentName}`);
|
||||||
return { agentName, agent };
|
return { agentName, agent };
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user