File size: 2,742 Bytes
34367da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { getDatabase } from '../../database/index.js';
import { AgentRunReport } from '@widget-tdc/mcp-types';

export class EvolutionRepository {
  private get db() {
    return getDatabase();
  }

  createPrompt(agentId: string, promptText: string, createdBy: string = 'evolution-agent'): number {
    // Get current max version for this agent
    const maxVersionRow = this.db.prepare(`

      SELECT MAX(version) as max_version FROM agent_prompts WHERE agent_id = ?

    `).get(agentId) as any;

    const nextVersion = (maxVersionRow?.max_version || 0) + 1;

    const result = this.db.prepare(`

      INSERT INTO agent_prompts (agent_id, version, prompt_text, created_by)

      VALUES (?, ?, ?, ?)

    `).run(agentId, nextVersion, promptText, createdBy);

    return result.lastInsertRowid as number;
  }

  getLatestPrompt(agentId: string): any {
    return this.db.prepare(`

      SELECT * FROM agent_prompts

      WHERE agent_id = ?

      ORDER BY version DESC

      LIMIT 1

    `).get(agentId);
  }

  getAllPrompts(agentId: string): any[] {
    return this.db.prepare(`

      SELECT * FROM agent_prompts

      WHERE agent_id = ?

      ORDER BY version DESC

    `).all(agentId);
  }

  recordRun(report: AgentRunReport): number {
    const result = this.db.prepare(`

      INSERT INTO agent_runs (

        agent_id, prompt_version, input_summary, output_summary,

        kpi_name, kpi_delta, run_context

      )

      VALUES (?, ?, ?, ?, ?, ?, ?)

    `).run(
      report.agentId,
      report.promptVersion,
      report.inputSummary,
      report.outputSummary,
      report.kpiName,
      report.kpiDelta,
      JSON.stringify(report.runContext)
    );

    return result.lastInsertRowid as number;
  }

  getRecentRuns(agentId: string, limit: number = 10): any[] {
    const rows = this.db.prepare(`

      SELECT * FROM agent_runs

      WHERE agent_id = ?

      ORDER BY created_at DESC

      LIMIT ?

    `).all(agentId, limit);

    return rows.map((row: any) => {
      let runContext = {};
      try {
        runContext = JSON.parse(row.run_context || '{}');
      } catch (error) {
        console.error('Error parsing run_context JSON:', error);
        runContext = {};
      }
      return {
        ...row,
        run_context: runContext,
      };
    });
  }

  getAverageKpiDelta(agentId: string, limit: number = 10): number {
    const result = this.db.prepare(`

      SELECT AVG(kpi_delta) as avg_delta

      FROM (

        SELECT kpi_delta FROM agent_runs

        WHERE agent_id = ?

        ORDER BY created_at DESC

        LIMIT ?

      )

    `).get(agentId, limit) as any;

    return result?.avg_delta || 0;
  }
}