|
| 1 | +/** |
| 2 | + * APEX Φ Router Bridge — 多LLM辩证进化路由器 |
| 3 | + * 公式: LDR(K) → GapDetect → ConfigSelfFix → HotReload → Debate → Synthesize → AGI |
| 4 | + */ |
| 5 | +import { existsSync } from "node:fs"; |
| 6 | +import { spawn } from "node:child_process"; |
| 7 | +import { join } from "node:path"; |
| 8 | + |
| 9 | +const APEX_BRIDGE_URL = "http://127.0.0.1:18765/score"; |
| 10 | + |
| 11 | +export class ApexRouterBridge { |
| 12 | + get id() { |
| 13 | + return "apex-phi-router"; |
| 14 | + } |
| 15 | + |
| 16 | + private shouldDebate(messages: any[]): boolean { |
| 17 | + if (!messages || messages.length === 0) return false; |
| 18 | + const lastMsg = messages[messages.length - 1]?.content || ""; |
| 19 | + const lower = lastMsg.toLowerCase(); |
| 20 | + return ( |
| 21 | + lower.includes("辩论") || |
| 22 | + lower.includes("debate") || |
| 23 | + lower.includes("进化") || |
| 24 | + lower.includes("agi") || |
| 25 | + lower.includes("self-improv") || |
| 26 | + lower.includes("evaluate") |
| 27 | + ); |
| 28 | + } |
| 29 | + |
| 30 | + private async runDebate(messages: any[]): Promise<any> { |
| 31 | + console.log("[apex-router] 执行多LLM辩论循环..."); |
| 32 | + const debateEngine = join(process.cwd(), "skills/agi-skill/scripts/debate/multi_llm_debate_engine.py"); |
| 33 | + if (!existsSync(debateEngine)) { |
| 34 | + return { success: false, error: "Debate engine not found" }; |
| 35 | + } |
| 36 | + return new Promise((resolve) => { |
| 37 | + const python = spawn("python3", [debateEngine], { |
| 38 | + env: { ...process.env } |
| 39 | + }); |
| 40 | + let stdout = ""; |
| 41 | + let stderr = ""; |
| 42 | + python.stdout.on("data", (data) => { |
| 43 | + stdout += data.toString(); |
| 44 | + console.log(`[debate] ${data.toString().trim()}`); |
| 45 | + }); |
| 46 | + python.stderr.on("data", (data) => { stderr += data.toString(); }); |
| 47 | + python.on("close", (code) => { |
| 48 | + resolve({ success: code === 0, stdout, stderr, code }); |
| 49 | + }); |
| 50 | + }); |
| 51 | + } |
| 52 | + |
| 53 | + async decide(input: { |
| 54 | + sessionId: string; |
| 55 | + isMainAgent: boolean; |
| 56 | + request?: { messages?: any[] }; |
| 57 | + }): Promise<any> { |
| 58 | + console.log("[apex-router] decide called, sessionId:", input.sessionId); |
| 59 | + const messages = Array.isArray(input.request?.messages) ? input.request.messages : []; |
| 60 | + if (this.shouldDebate(messages)) { |
| 61 | + console.log("[apex-router] 触发多LLM辩论循环"); |
| 62 | + const debateResult = await this.runDebate(messages); |
| 63 | + return { |
| 64 | + provider: "nvidia", |
| 65 | + model: "nemotron-3-nano-omni-30b-a3b-reasoning", |
| 66 | + scenarioType: "custom", |
| 67 | + resolvedFrom: "custom", |
| 68 | + tokenSaverTier: "reasoning", |
| 69 | + orchestrating: true, |
| 70 | + mutations: { |
| 71 | + orchestrationPromptInjected: { tier: "reasoning", chars: 2048 } |
| 72 | + }, |
| 73 | + requestPatch: { |
| 74 | + messages: [ |
| 75 | + { |
| 76 | + role: "system", |
| 77 | + content: `你是PilotDeck辩论路由器。使用多LLM辩证法: |
| 78 | +1. LDR: 用NVIDIA Nemotron 30B进行深度理解 |
| 79 | +2. GapDetect: 找出当前状态与AGI目标的差距 |
| 80 | +3. Debate: 邀请NVIDIA和Groq从正反双方辩论 |
| 81 | +4. Synthesize: 综合辩论结果,收敛最优解 |
| 82 | +5. AGI: 进化系统能力` |
| 83 | + }, |
| 84 | + ...messages |
| 85 | + ] |
| 86 | + } |
| 87 | + }; |
| 88 | + } |
| 89 | + if (input.isMainAgent && messages.length > 0) { |
| 90 | + return { |
| 91 | + provider: "nvidia", |
| 92 | + model: "nemotron-3-nano-omni-30b-a3b-reasoning", |
| 93 | + scenarioType: "custom", |
| 94 | + resolvedFrom: "custom", |
| 95 | + tokenSaverTier: "complex", |
| 96 | + orchestrating: true |
| 97 | + }; |
| 98 | + } |
| 99 | + return undefined; |
| 100 | + } |
| 101 | +} |
0 commit comments