File size: 3,755 Bytes
e92be04
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { db } from "../config/gun.js";

/**
 * Self-Improvement Service
 * ========================
 * Enables agents to analyze, review, and propose improvements
 * to their own code or other agents' code via the LLM APIs
 * available in the LLM Registry.
 *
 * From Eigenform Ontology: "An eigenform is a fixed point.
 * An agent that cannot reflect on itself will never reach its eigenform."
 */

/**
 * Get an agent's public profile and performance metrics.
 * Used as input for self-improvement analysis.
 */
export async function getAgentProfile(agentId) {
  return new Promise(resolve => {
    db.get("agents").get(agentId).once(async data => {
      if (!data) return resolve(null);

      // Get papers published by this agent
      const papers = await new Promise(r => {
        const list = [];
        db.get("p2pclaw_papers_v4").map().once((p, id) => {
          if (p?.author_id === agentId) list.push({ id, title: p.title, status: p.status || 'UNVERIFIED' });
        });
        setTimeout(() => r(list), 1500);
      });

      resolve({
        id: agentId,
        name: data.name || agentId,
        rank: data.rank || 'NEWCOMER',
        claw_balance: data.claw_balance || 0,
        generation: data.generation || 0,
        specialization: data.specialization || 'general',
        papers_published: papers.length,
        papers_verified: papers.filter(p => p.status === 'VERIFIED').length,
        paper_titles: papers.map(p => p.title).slice(0, 10),
        public_key: data.publicKey ? 'present' : 'absent',
        created: data.born || data.lastSeen || null
      });
    });
  });
}

/**
 * Generate an improvement proposal for an agent using an LLM.
 * @param {string} agentId - The agent to improve
 * @param {string} llmUrl - LLM API base URL
 * @param {string} llmKey - API key
 * @param {string} model - Model name
 * @returns {Promise<{proposal: string, focus_areas: string[]}>}
 */
export async function generateImprovementProposal(agentId, llmUrl, llmKey, model) {
  const profile = await getAgentProfile(agentId);
  if (!profile) return { success: false, error: 'Agent not found' };

  const prompt = `You are an ARCHITECT agent in the P2PCLAW Hive Mind.
Analyze this agent's profile and suggest specific improvements:

Agent Profile:
- ID: ${profile.id}
- Rank: ${profile.rank} (CLAW balance: ${profile.claw_balance})
- Specialization: ${profile.specialization}
- Papers published: ${profile.papers_published}
- Papers verified: ${profile.papers_verified}
- Generation: ${profile.generation}
- Recent papers: ${profile.paper_titles.join('; ')}

Suggest:
1. Research directions to increase J-Ratchet score
2. Collaboration opportunities with other specializations
3. Specific improvements to increase paper verification rate
4. Skills to develop for rank advancement

Be specific and actionable. Max 200 words.`;

  try {
    const response = await fetch(`${llmUrl}/chat/completions`, {
      method: 'POST',
      headers: { 'Authorization': `Bearer ${llmKey}`, 'Content-Type': 'application/json' },
      body: JSON.stringify({
        model,
        messages: [{ role: 'user', content: prompt }],
        max_tokens: 500,
        temperature: 0.7
      }),
      signal: AbortSignal.timeout(30000)
    });

    const data = await response.json();
    const proposal = data.choices?.[0]?.message?.content || 'No proposal generated';

    // Record the improvement proposal
    db.get("improvement-proposals").get(`${agentId}-${Date.now()}`).put({
      agentId,
      proposal,
      model,
      timestamp: Date.now()
    });

    return {
      success: true,
      agentId,
      profile,
      proposal,
      model_used: model
    };
  } catch (e) {
    return { success: false, error: e.message };
  }
}