Files
plan2code/plan2code-bot/src/session-runner.ts
T
jparkerweb c92865c395 feat: add plan2code-bot with --resume capability and state cleanup
Add the autonomous workflow test runner (plan2code-bot) that uses the
Claude Agent SDK to run plan2code end-to-end. Includes --resume flag
to continue incomplete runs from saved state, and automatic state file
cleanup on successful completion.
2026-03-01 21:30:45 -08:00

61 lines
1.8 KiB
TypeScript

import { query } from '@anthropic-ai/claude-agent-sdk';
import { createAutoResponder } from './auto-responder.js';
import type { BotConfig, StepName } from './types.js';
export interface SessionOptions {
prompt: string;
config: BotConfig;
step: StepName;
maxTurns?: number;
}
export interface SessionResult {
sessionId: string | null;
output: string;
success: boolean;
duration: number;
}
export async function runSession(options: SessionOptions): Promise<SessionResult> {
const { prompt, config, step, maxTurns = 50 } = options;
const startTime = Date.now();
let output = '';
let sessionId: string | null = null;
try {
const session = query({
prompt,
options: {
cwd: config.projectDir,
maxTurns,
permissionMode: 'bypassPermissions',
allowDangerouslySkipPermissions: true,
canUseTool: createAutoResponder(config, step),
systemPrompt: { type: 'preset', preset: 'claude_code' },
settingSources: ['user', 'project'],
},
});
for await (const message of session) {
if (message.type === 'assistant') {
sessionId = message.session_id ?? sessionId;
for (const block of message.message.content) {
if (block.type === 'text') {
output += block.text + '\n';
}
}
} else if (message.type === 'result') {
sessionId = message.session_id ?? sessionId;
}
}
const duration = Date.now() - startTime;
const hasOutput = output.trim().length > 0;
return { sessionId, output, success: hasOutput, duration };
} catch (error) {
const duration = Date.now() - startTime;
const errorMsg = error instanceof Error ? error.message : String(error);
return { sessionId, output, success: false, duration };
}
}