Skip to content

Commit 61f9d3d

Browse files
author
Xuanji
committed
fix(apex-router): prevent crash when messages is undefined
- Add guard in shouldDebate() to check messages existence before accessing length - Use Array.isArray() in decide() for safer type check on input.request.messages - Fixes: Cannot read properties of undefined (reading 'length')
1 parent ccc1f2c commit 61f9d3d

5 files changed

Lines changed: 172 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* APEX Router Bridge — HTTP client for APEX Φ scoring service
3+
*/
4+
const APEX_BRIDGE_URL = "http://127.0.0.1:18765/score";
5+
6+
export class ApexRouterBridge {
7+
get id() { return "apex-phi-router"; }
8+
9+
async decide(input) {
10+
console.log("[apex-router] decide called, sessionId:", input.sessionId);
11+
try {
12+
const controller = new AbortController();
13+
const timer = setTimeout(() => controller.abort(), 25000);
14+
const resp = await fetch(APEX_BRIDGE_URL, {
15+
method: "POST",
16+
headers: { "Content-Type": "application/json" },
17+
body: JSON.stringify({
18+
sessionId: input.sessionId,
19+
isMainAgent: input.isMainAgent,
20+
messages: input.request?.messages ?? [],
21+
}),
22+
signal: controller.signal,
23+
});
24+
clearTimeout(timer);
25+
console.log("[apex-router] response status:", resp.status);
26+
if (!resp.ok) return undefined;
27+
const data = await resp.json();
28+
console.log("[apex-router] data:", JSON.stringify(data));
29+
return data;
30+
} catch (err) {
31+
console.error("[apex-router] error:", err.message);
32+
return undefined;
33+
}
34+
}
35+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
}

plugins/apex-router/plugin.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "apex-router",
3+
"version": "1.0.0",
4+
"description": "APEX Φ formula custom router for PilotDeck"
5+
}

plugins/apex-router/router.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* APEX Φ Router — PilotDeck CustomRouter plugin contribution
3+
*/
4+
import { ApexRouterBridge } from "./apex_router_bridge.js";
5+
6+
let _instance;
7+
8+
export const apexRouterContribution = {
9+
id: "apex-phi-router",
10+
description: "APEX Φ formula self-evolution LLM router",
11+
createCustomRouter() {
12+
if (!_instance) _instance = new ApexRouterBridge();
13+
return _instance;
14+
},
15+
};

plugins/apex-router/router.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* APEX Φ Router — PilotDeck CustomRouter plugin contribution
3+
*/
4+
import type { RouterContribution } from ../../../extension/contributions/RouterContribution.js;
5+
import { ApexRouterBridge } from ./apex_router_bridge.js;
6+
7+
let _instance: ApexRouterBridge | undefined;
8+
9+
export const apexRouterContribution: RouterContribution = {
10+
id: apex-phi-router,
11+
description: APEX Φ formula self-evolution LLM router,
12+
createCustomRouter() {
13+
if (!_instance) _instance = new ApexRouterBridge();
14+
return _instance;
15+
},
16+
};

0 commit comments

Comments
 (0)