summaryrefslogtreecommitdiff
path: root/admin-panel/src/server/agents/critic.ts
blob: b0613fb01f37a730daa2669d3c998a26811f5afd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { BaseAgent, type AgentContext, type AgentResult } from './base-agent.js';
import type { ClaudeResponse } from '../services/claude.service.js';

export class CriticAgent extends BaseAgent {
  name = 'critic';
  templateFile = 'critic.md';
  model = 'claude-sonnet-4-20250514';

  protected getSystemPrompt(): string {
    return 'You are a thorough plan reviewer. Be constructive but rigorous. Approve plans that are solid enough to implement, reject plans with significant gaps or flaws.';
  }

  protected parseResponse(response: ClaudeResponse, ctx: AgentContext): AgentResult {
    const content = response.content;
    const firstLine = content.split('\n')[0].trim().toUpperCase();
    const approved = firstLine.includes('APPROVE');
    const rejected = firstLine.includes('REJECT');
    const verdict: 'approve' | 'reject' = approved ? 'approve' : 'reject';

    // Build review entry
    const reviewEntry = {
      agent: 'critic',
      verdict,
      content,
      timestamp: new Date().toISOString(),
    };

    const updates: Record<string, unknown> = {};
    const currentReviews = ctx.reviewComments ? JSON.parse(ctx.reviewComments || '[]') : [];
    currentReviews.push(reviewEntry);
    updates.plan_review_comments = JSON.stringify(currentReviews);

    if (approved) {
      updates.status = 'impl';
      updates.plan_review_count = (ctx as unknown as Record<string, number>).plan_review_count
        ? (ctx as unknown as Record<string, number>).plan_review_count + 1
        : 1;
    } else if (rejected) {
      updates.status = 'plan'; // Send back to planning
      updates.plan_review_count = (ctx as unknown as Record<string, number>).plan_review_count
        ? (ctx as unknown as Record<string, number>).plan_review_count + 1
        : 1;
    }

    return {
      success: true,
      content,
      tokensUsed: response.tokensUsed,
      model: response.model,
      updates,
      verdict,
      message: `Plan review: ${verdict.toUpperCase()}`,
    };
  }
}