Files
plan2code/plan2code-metrics/src/invoke-llm.ts
T
jparkerweb 1d88d787a3 Ingest community feedback submissions in plan2code-metrics
Adds a CLI flow that lists open community-feedback issues via gh, validates
and parses each METRICS_JSON payload, imports them deduped by run_id,
re-aggregates and closes the issue. Community runs cohort by Plan2Code
version rather than prompt fingerprint, and local cohorts stay preferred so
ingested feedback never displaces the maintainer's current generation. Adds
Devin CLI as an AI backend.

AI Assisted
2026-08-08 12:39:28 -07:00

115 lines
3.4 KiB
TypeScript

/**
* invoke-llm.ts
* Unified LLM invocation for plan2code-metrics.
* Supports Claude Code (temp file → stdin), Copilot CLI (stdin string), and
* Devin CLI (temp prompt file).
* Mirrors the agent pattern from plan2code-loop.
*/
import { execa } from 'execa';
import { writeFileSync, unlinkSync } from 'fs';
import { join } from 'path';
import { tmpdir } from 'os';
// ── Agent definitions ────────────────────────────────────────────────────────
export type AgentType = 'claude-code' | 'copilot-cli' | 'devin-cli';
export interface AgentDef {
name: AgentType;
displayName: string;
command: string;
defaultModel: string;
}
export const AGENTS: Record<AgentType, AgentDef> = {
'claude-code': {
name: 'claude-code',
displayName: 'Claude Code',
command: 'claude',
defaultModel: 'default',
},
'copilot-cli': {
name: 'copilot-cli',
displayName: 'GitHub Copilot CLI',
command: 'copilot',
defaultModel: 'claude-sonnet-4',
},
'devin-cli': {
name: 'devin-cli',
displayName: 'Devin CLI',
command: 'devin',
defaultModel: 'default',
},
};
// ── Invocation ───────────────────────────────────────────────────────────────
export interface InvokeLLMOptions {
prompt: string;
model: string;
agent: AgentType;
timeout?: number;
}
export async function invokeLLM(opts: InvokeLLMOptions): Promise<string> {
const { prompt, model, agent, timeout = 300_000 } = opts;
const def = AGENTS[agent];
if (agent === 'claude-code') {
// Write prompt to temp file — more reliable than stdin on Windows
const tempFile = join(tmpdir(), `plan2code-metrics-prompt-${Date.now()}.txt`);
writeFileSync(tempFile, prompt, 'utf-8');
try {
const args: string[] = [
'--print',
'--dangerously-skip-permissions',
];
// Only add --model if not using default
if (model && model !== 'default') {
args.push('--model', model);
}
const result = await execa(def.command, args, {
inputFile: tempFile,
timeout,
});
return result.stdout;
} finally {
try { unlinkSync(tempFile); } catch { /* ignore cleanup errors */ }
}
} else if (agent === 'devin-cli') {
// Devin CLI: load prompt from a temp file, run single-turn, auto-approve tool calls
const tempFile = join(tmpdir(), `plan2code-metrics-prompt-${Date.now()}.txt`);
writeFileSync(tempFile, prompt, 'utf-8');
try {
const args: string[] = ['--print', '--prompt-file', tempFile, '--permission-mode', 'dangerous'];
// Only add --model if not using default
if (model && model !== 'default') {
args.push('--model', model);
}
const result = await execa(def.command, args, { timeout });
return result.stdout;
} finally {
try { unlinkSync(tempFile); } catch { /* ignore cleanup errors */ }
}
} else {
// Copilot CLI: pipe prompt via stdin string
const args: string[] = [];
// Only add --model if not using default
if (model && model !== 'default') {
args.push('--model', model);
}
args.push('--allow-all-tools', '-s');
const result = await execa(def.command, args, {
input: prompt,
timeout,
});
return result.stdout;
}
}