File size: 9,964 Bytes
c2c8c8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import { EventEmitter } from 'events';
import {
  AgentType, AgentResult, AgentProgressEvent, Finding, FullReviewResult,
  QuickReviewResult, PRFixSet, FileChange, AgentOptions,
  SEVERITY_ORDER, estimateTokens, ALL_AGENTS,
} from '@glmpilot/shared';
import { SecurityAgent } from './security.agent.js';
import { PerformanceAgent } from './performance.agent.js';
import { StyleAgent } from './style.agent.js';
import { DocumentationAgent } from './documentation.agent.js';
import { BaseAgent } from './base.agent.js';
import { glm } from '../services/glm-client.js';
import { logger } from '../utils/logger.js';

export class AgentOrchestrator extends EventEmitter {
  private agents: Map<AgentType, BaseAgent>;

  constructor() {
    super();
    this.agents = new Map<AgentType, BaseAgent>([
      ['security', new SecurityAgent()],
      ['performance', new PerformanceAgent()],
      ['style', new StyleAgent()],
      ['documentation', new DocumentationAgent()],
    ]);
  }

  async reviewCodebase(
    files: Map<string, string>,
    options?: { agents?: AgentType[]; mode?: 'full' | 'quick' }
  ): Promise<FullReviewResult> {
    const startTime = Date.now();
    const selectedAgents = options?.agents || (ALL_AGENTS as unknown as AgentType[]);
    const totalFiles = files.size;

    // Chunk files if too large
    const batches = this.chunkFiles(files);
    const allResults: Map<AgentType, AgentResult[]> = new Map();

    for (let batchIndex = 0; batchIndex < batches.length; batchIndex++) {
      const batch = batches[batchIndex];

      // Run all selected agents in parallel per batch
      const promises = selectedAgents.map(async (agentType) => {
        const agent = this.agents.get(agentType);
        if (!agent) return null;

        this.emitProgress(agentType, 'started', 0, totalFiles, batchIndex, batches.length);

        try {
          this.emitProgress(agentType, 'analyzing', batch.size, totalFiles, batchIndex, batches.length);
          const agentOptions: AgentOptions = options?.mode === 'quick' ? { quickMode: true, maxTokens: 4096 } : {};
          const result = await agent.analyze(batch, agentOptions);
          this.emitProgress(agentType, 'complete', batch.size, totalFiles, batchIndex, batches.length);
          return { agentType, result };
        } catch (error) {
          logger.error(`Agent ${agentType} failed`, { error: (error as Error).message });
          this.emitProgress(agentType, 'error', 0, totalFiles, batchIndex, batches.length);
          return {
            agentType,
            result: {
              agent: agentType,
              findings: [],
              summary: { critical: 0, high: 0, medium: 0, low: 0 },
              overallRiskScore: 0,
              executionTimeMs: 0,
              error: (error as Error).message,
            } as AgentResult,
          };
        }
      });

      const results = await Promise.allSettled(promises);
      for (const settled of results) {
        if (settled.status === 'fulfilled' && settled.value) {
          const { agentType, result } = settled.value;
          if (!allResults.has(agentType)) allResults.set(agentType, []);
          allResults.get(agentType)!.push(result);
        }
      }
    }

    // Merge results across batches
    const agentResults: Record<string, AgentResult> = {} as Record<AgentType, AgentResult>;
    const overallScores: Record<string, number> = {} as Record<AgentType, number>;
    let mergedFindings: Finding[] = [];
    let totalTokens = 0;

    for (const [agentType, batchResults] of allResults) {
      const merged = this.mergeAgentResults(agentType, batchResults);
      agentResults[agentType] = merged;
      overallScores[agentType] = merged.overallRiskScore;
      mergedFindings.push(...merged.findings);
      totalTokens += merged.tokensUsed || 0;
    }

    // Deduplicate findings
    mergedFindings = this.deduplicateFindings(mergedFindings);

    // Sort by severity
    mergedFindings.sort((a, b) => (SEVERITY_ORDER[a.severity] || 99) - (SEVERITY_ORDER[b.severity] || 99));

    // Calculate aggregate score
    const scores = Object.values(overallScores);
    overallScores['aggregate'] = scores.length > 0 ? Math.round(scores.reduce((a, b) => a + b, 0) / scores.length) : 0;

    return {
      agentResults: agentResults as Record<AgentType, AgentResult>,
      mergedFindings,
      overallScores: overallScores as Record<AgentType, number> & { aggregate: number },
      metadata: {
        totalFiles,
        totalTokensUsed: totalTokens,
        executionTimeMs: Date.now() - startTime,
        timestamp: new Date().toISOString(),
      },
    };
  }

  async reviewSingleFile(filePath: string, content: string): Promise<QuickReviewResult> {
    const files = new Map([[filePath, content]]);
    const agents: AgentType[] = ['security', 'performance', 'style'];

    const results = await Promise.allSettled(
      agents.map(async (agentType) => {
        const agent = this.agents.get(agentType);
        if (!agent) return null;
        return agent.analyze(files, { quickMode: true, maxTokens: 4096 });
      })
    );

    let allFindings: Finding[] = [];
    for (const result of results) {
      if (result.status === 'fulfilled' && result.value) {
        allFindings.push(...result.value.findings);
      }
    }

    allFindings.sort((a, b) => (SEVERITY_ORDER[a.severity] || 99) - (SEVERITY_ORDER[b.severity] || 99));

    return {
      findings: allFindings,
      summary: {
        critical: allFindings.filter(f => f.severity === 'critical').length,
        high: allFindings.filter(f => f.severity === 'high').length,
        medium: allFindings.filter(f => f.severity === 'medium').length,
        low: allFindings.filter(f => f.severity === 'low').length,
      },
      filePath,
    };
  }

  async generatePRFixes(findings: Finding[]): Promise<PRFixSet> {
    const topFindings = findings
      .sort((a, b) => (SEVERITY_ORDER[a.severity] || 99) - (SEVERITY_ORDER[b.severity] || 99))
      .slice(0, 20);

    const prompt = `You are a code fixing assistant. Given these code review findings with their current code and suggested fixes, generate a unified set of file changes.

Findings:
${JSON.stringify(topFindings, null, 2)}

Respond with valid JSON:
{
  "fileChanges": [{ "path": "file.tsx", "originalContent": "...", "fixedContent": "..." }],
  "commitMessage": "fix: ...",
  "prTitle": "...",
  "prBody": "..."
}`;

    const response = await glm.chat(
      [{ role: 'system', content: 'You are a precise code fixing assistant.' }, { role: 'user', content: prompt }],
      { temperature: 0.2 }
    );

    const parsed = JSON.parse(response.choices[0]?.message?.content || '{}');

    return {
      branchName: `glmpilot/fix-${Date.now()}`,
      commitMessage: parsed.commitMessage || 'fix: apply code review fixes',
      prTitle: parsed.prTitle || 'GLMPilot: Code Review Fixes',
      prBody: parsed.prBody || 'Automated fixes from GLMPilot code review.',
      fileChanges: parsed.fileChanges || [],
    };
  }

  private chunkFiles(files: Map<string, string>): Map<string, string>[] {
    const concatenated = Array.from(files.values()).join('');
    const totalTokens = estimateTokens(concatenated);

    if (totalTokens <= 30000) return [files];

    // Group by directory
    const groups = new Map<string, Map<string, string>>();
    for (const [path, content] of files) {
      const dir = path.split('/').slice(0, -1).join('/') || '/';
      if (!groups.has(dir)) groups.set(dir, new Map());
      groups.get(dir)!.set(path, content);
    }

    // Merge small groups into batches under 30k tokens
    const batches: Map<string, string>[] = [];
    let currentBatch = new Map<string, string>();
    let currentTokens = 0;

    for (const [, groupFiles] of groups) {
      const groupContent = Array.from(groupFiles.values()).join('');
      const groupTokens = estimateTokens(groupContent);

      if (currentTokens + groupTokens > 30000 && currentBatch.size > 0) {
        batches.push(currentBatch);
        currentBatch = new Map();
        currentTokens = 0;
      }

      for (const [path, content] of groupFiles) {
        currentBatch.set(path, content);
      }
      currentTokens += groupTokens;
    }

    if (currentBatch.size > 0) batches.push(currentBatch);
    return batches;
  }

  private mergeAgentResults(agentType: AgentType, results: AgentResult[]): AgentResult {
    const findings: Finding[] = [];
    let totalTime = 0;
    let totalTokens = 0;

    for (const r of results) {
      findings.push(...r.findings);
      totalTime += r.executionTimeMs;
      totalTokens += r.tokensUsed || 0;
    }

    const summary = {
      critical: findings.filter(f => f.severity === 'critical').length,
      high: findings.filter(f => f.severity === 'high').length,
      medium: findings.filter(f => f.severity === 'medium').length,
      low: findings.filter(f => f.severity === 'low').length,
    };

    const riskScores = results.map(r => r.overallRiskScore).filter(s => s > 0);
    const avgRisk = riskScores.length > 0 ? Math.round(riskScores.reduce((a, b) => a + b, 0) / riskScores.length) : 0;

    return { agent: agentType, findings, summary, overallRiskScore: avgRisk, executionTimeMs: totalTime, tokensUsed: totalTokens };
  }

  private deduplicateFindings(findings: Finding[]): Finding[] {
    const seen = new Set<string>();
    return findings.filter((f) => {
      const key = `${f.file}:${f.lineStart}-${f.lineEnd}:${f.category}`;
      if (seen.has(key)) return false;
      seen.add(key);
      return true;
    });
  }

  private emitProgress(agent: string, status: AgentProgressEvent['status'], filesProcessed: number, totalFiles: number, batchIndex: number, totalBatches: number) {
    const event: AgentProgressEvent = { agent, status, filesProcessed, totalFiles, batchIndex, totalBatches };
    this.emit('progress', event);
  }
}

export const orchestrator = new AgentOrchestrator();