-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
1058 lines (961 loc) · 45.6 KB
/
Copy pathindex.js
File metadata and controls
1058 lines (961 loc) · 45.6 KB
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env node
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
import net from "net";
import yaml from "js-yaml";
import { existsSync, unlinkSync, readFileSync, writeFileSync, mkdirSync } from "fs";
import { execSync } from "child_process";
import { fileURLToPath } from "url";
import { dirname, join } from "path";
const __dirname = dirname(fileURLToPath(import.meta.url));
const PKG_VERSION = JSON.parse(readFileSync(join(__dirname, "package.json"), "utf-8")).version;
// ── 加载 .env ──
try {
const envFile = readFileSync(join(__dirname, ".env"), "utf-8");
for (const line of envFile.split("\n")) {
const m = line.match(/^\s*([\w]+)\s*=\s*"?([^"#\n]*)"?\s*$/);
if (m && !process.env[m[1]]) process.env[m[1]] = m[2].trim();
}
} catch {}
// ── 加载配置 ──
let configPath = join(__dirname, "config.yaml");
if (!existsSync(configPath)) {
configPath = join(__dirname, "config.example.yaml");
if (!existsSync(configPath)) {
console.error("❌ config.yaml not found. Copy config.example.yaml and configure your API keys.");
process.exit(1);
}
console.error("ℹ Using default config (config.example.yaml). Create config.yaml to customize.");
}
const config = yaml.load(readFileSync(configPath, "utf-8"));
const defaults = config.defaults || {};
const TIMEOUT = defaults.timeout_ms || 60000;
const MAX_RETRIES = defaults.max_retries ?? 2;
const DEFAULT_MAX_TOKENS = defaults.max_tokens || 4000;
const DEFAULT_TEMP = defaults.temperature || 0.7;
const MAX_HISTORY_TURNS = defaults.max_history_turns || 10;
const CONVERSATION_EXPIRY = 30 * 60 * 1000; // 30 minutes
const CACHE_TTL = defaults.cache_ttl_ms || 0; // 0 = disabled
const DAILY_BUDGET = defaults.daily_budget_usd ?? Infinity;
// ── 对话历史管理 ──
const conversations = new Map(); // id -> { [modelKey]: { messages: [], lastAccess } }
function getHistory(conversationId, modelKey) {
if (!conversationId) return [];
const conv = conversations.get(conversationId);
return conv?.[modelKey]?.messages || [];
}
function saveHistory(conversationId, modelKey, userMsg, assistantMsg) {
if (!conversationId) return;
if (!conversations.has(conversationId)) conversations.set(conversationId, {});
const conv = conversations.get(conversationId);
if (!conv[modelKey]) conv[modelKey] = { messages: [], lastAccess: 0 };
const h = conv[modelKey];
h.messages.push({ role: "user", content: userMsg }, { role: "assistant", content: assistantMsg });
h.lastAccess = Date.now();
while (h.messages.length > MAX_HISTORY_TURNS * 2) h.messages.splice(0, 2);
}
setInterval(() => {
const now = Date.now();
for (const [id, conv] of conversations) {
const last = Math.max(0, ...Object.values(conv).map(c => c.lastAccess || 0));
if (now - last > CONVERSATION_EXPIRY) conversations.delete(id);
}
}, 60_000);
// ── Response cache ──
const responseCache = new Map(); // cacheKey -> { result, expires }
function makeCacheKey(modelKey, prompt, systemPrompt) {
return `${modelKey}\0${prompt}\0${systemPrompt || ""}`;
}
// 定期清理过期缓存
setInterval(() => {
const now = Date.now();
for (const [k, v] of responseCache) {
if (now >= v.expires) responseCache.delete(k);
}
}, 60_000);
// ── Budget tracking ──
const budget = { date: new Date().toDateString(), spent: 0 };
function checkBudget() {
const today = new Date().toDateString();
if (today !== budget.date) { budget.spent = 0; budget.date = today; }
if (budget.spent >= DAILY_BUDGET) {
throw new Error(`Daily budget ($${DAILY_BUDGET}) exceeded. Spent today: $${budget.spent.toFixed(4)}`);
}
}
// ── API key 申请链接 ──
const API_KEY_URLS = {
OPENAI_API_KEY: "https://platform.openai.com/api-keys",
GEMINI_API_KEY: "https://aistudio.google.com/apikey",
XAI_API_KEY: "https://console.x.ai/",
PERPLEXITY_API_KEY: "https://www.perplexity.ai/settings/api",
MISTRAL_API_KEY: "https://console.mistral.ai/api-keys",
GROQ_API_KEY: "https://console.groq.com/keys",
OPENROUTER_API_KEY: "https://openrouter.ai/keys",
DEEPSEEK_API_KEY: "https://platform.deepseek.com/api_keys",
DASHSCOPE_API_KEY: "https://dashscope.console.aliyun.com/apiKey",
ZHIPUAI_API_KEY: "https://open.bigmodel.cn/usercenter/proj-mgmt/apikeys",
KIMI_API_KEY: "https://platform.moonshot.cn/console/api-keys",
TOGETHER_API_KEY: "https://api.together.ai/settings/api-keys",
};
// 解析模型配置
const models = {};
const skippedModels = []; // { name, envVar, url }
for (const [key, cfg] of Object.entries(config.models || {})) {
const apiKey = cfg.api_key_env ? process.env[cfg.api_key_env] : "";
if (cfg.api_key_env && !apiKey) {
skippedModels.push({ name: cfg.name, envVar: cfg.api_key_env, url: API_KEY_URLS[cfg.api_key_env] });
console.error(`⚠️ ${cfg.name}: ${cfg.api_key_env} not set, skipped`);
continue;
}
const pricing = cfg.pricing || {};
models[key] = { ...cfg, key, apiKey, fallbackTo: cfg.fallback_to || null, pricing: { input: pricing.input || 0, output: pricing.output || 0 } };
}
const modelKeys = Object.keys(models);
if (modelKeys.length === 0) {
console.error("❌ No models available. Check your API keys and config.yaml");
process.exit(1);
}
console.error(`✅ Loaded ${modelKeys.length} models: ${modelKeys.join(", ")}`);
// ── Agent Monitor: UDS 事件广播 ──
const SOCKET_PATH = "/tmp/agent-monitor.sock";
const monitorClients = new Set();
let ownsSocket = false;
const udsServer = net.createServer((client) => {
monitorClients.add(client);
client.on("close", () => monitorClients.delete(client));
client.on("error", () => monitorClients.delete(client));
});
function listenSocket() {
if (ownsSocket) return; // 已经在监听
udsServer.listen(SOCKET_PATH, () => {
ownsSocket = true;
console.error("📡 Monitor socket: " + SOCKET_PATH);
});
udsServer.once("error", (err) => {
if (err.code === "EADDRINUSE") {
// probe: 连得上说明有活进程在用;连不上说明是残留,清理重建
const probe = net.createConnection(SOCKET_PATH);
probe.on("connect", () => {
probe.destroy();
console.error("📡 Monitor socket in use by another instance, will retry in 30s");
});
probe.on("error", () => {
try { unlinkSync(SOCKET_PATH); } catch {}
// 立即重试一次
udsServer.listen(SOCKET_PATH, () => {
ownsSocket = true;
console.error("📡 Monitor socket: " + SOCKET_PATH);
});
});
}
});
}
// 定期重试,直到拿到 socket
const socketRetryInterval = setInterval(() => {
if (ownsSocket) { clearInterval(socketRetryInterval); return; }
if (existsSync(SOCKET_PATH)) {
// 探测是否还活着
const probe = net.createConnection(SOCKET_PATH);
probe.on("connect", () => probe.destroy()); // 还在用,下次再试
probe.on("error", () => {
try { unlinkSync(SOCKET_PATH); } catch {}
listenSocket();
});
} else {
listenSocket();
}
}, 30_000);
listenSocket();
function emitEvent(event) {
const line = JSON.stringify({ ...event, timestamp: Date.now() }) + "\n";
for (const client of monitorClients) {
try { client.write(line); } catch { monitorClients.delete(client); }
}
}
// ── 带超时和重试的 fetch ──
async function fetchWithRetry(url, options, { agent, retries = MAX_RETRIES } = {}) {
for (let attempt = 0; attempt <= retries; attempt++) {
try {
const res = await fetch(url, { ...options, signal: AbortSignal.timeout(TIMEOUT) });
if ((res.status === 429 || res.status >= 500) && attempt < retries) {
const wait = Math.min(1000 * 2 ** attempt, 8000);
emitEvent({ type: "AGENT_RETRY", agent, status: res.status, attempt: attempt + 1, wait });
await new Promise(r => setTimeout(r, wait));
continue;
}
if (!res.ok) {
const errMsg = await res.text();
const errType = res.status === 429 ? "rate_limit" : res.status >= 500 ? "server_error" : "api_error";
emitEvent({ type: "AGENT_ERROR", agent, error: errMsg, status: res.status, errType });
throw new Error(`API error (${res.status}): ${errMsg}`);
}
return await res.json();
} catch (e) {
if (e.name === "TimeoutError" || e.name === "AbortError") {
emitEvent({ type: "AGENT_ERROR", agent, error: `Request timeout (${TIMEOUT}ms)`, errType: "timeout" });
throw new Error(`请求超时 (${TIMEOUT / 1000}s)`);
}
if (e.message?.includes("fetch failed") || e.cause?.code === "ECONNREFUSED") {
if (attempt < retries) {
const wait = 1000 * 2 ** attempt;
emitEvent({ type: "AGENT_RETRY", agent, error: e.message, attempt: attempt + 1, wait });
await new Promise(r => setTimeout(r, wait));
continue;
}
emitEvent({ type: "AGENT_ERROR", agent, error: e.message, errType: "network" });
throw new Error(`网络连接失败: ${e.message}`);
}
throw e;
}
}
}
// ── SSE 流式解析 ──
async function* parseSSE(response) {
const reader = response.body.getReader();
const decoder = new TextDecoder();
let buffer = "";
try {
while (true) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split("\n");
buffer = lines.pop();
for (const line of lines) {
if (line.startsWith("data: ")) {
const data = line.slice(6).trim();
if (data === "[DONE]") return;
try { yield JSON.parse(data); } catch {}
}
}
}
} finally {
reader.releaseLock();
}
}
async function fetchStream(url, options, { agent } = {}) {
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), TIMEOUT);
try {
const res = await fetch(url, { ...options, signal: controller.signal });
clearTimeout(timeout);
if (!res.ok) {
const errMsg = await res.text();
const errType = res.status === 429 ? "rate_limit" : res.status >= 500 ? "server_error" : "api_error";
emitEvent({ type: "AGENT_ERROR", agent, error: errMsg, status: res.status, errType });
throw new Error(`API error (${res.status}): ${errMsg}`);
}
return res;
} catch (e) {
clearTimeout(timeout);
if (e.name === "AbortError") {
emitEvent({ type: "AGENT_ERROR", agent, error: `Request timeout (${TIMEOUT}ms)`, errType: "timeout" });
throw new Error(`请求超时 (${TIMEOUT / 1000}s)`);
}
throw e;
}
}
// ── 适配器:OpenAI 兼容 ──
async function adapterOpenAI(modelCfg, prompt, systemPrompt, maxTokens, history = [], extra = {}) {
// Image generation: gpt-image-* / dall-e-* 走 /v1/images/generations,
// schema 与 chat completions 不同(无 messages / 无流式)。
if (modelCfg.image_generation) {
const imgCfg = extra.imageConfig || {};
const imgUrl = modelCfg.endpoint.replace(/\/chat\/completions$/, "/images/generations");
const imgBody = {
model: modelCfg.model,
prompt,
n: imgCfg.n || 1,
size: imgCfg.size || modelCfg.default_size || "1024x1024",
quality: imgCfg.quality || modelCfg.default_quality || "medium",
};
const data = await fetchWithRetry(imgUrl,
{ method: "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer ${modelCfg.apiKey}` }, body: JSON.stringify(imgBody) },
{ agent: modelCfg.key }
);
const items = data.data || [];
const images = items.map(it => ({ mimeType: "image/png", data: it.b64_json }));
const usage = data.usage || {};
return {
content: `Generated ${images.length} image(s)`,
images,
model: modelCfg.model,
tokens: {
prompt: usage.input_tokens || 0,
completion: usage.output_tokens || 0,
total: usage.total_tokens || 0,
},
};
}
const messages = [];
const sysParts = [modelCfg.system_prefix, systemPrompt].filter(Boolean).join("\n\n");
if (sysParts) messages.push({ role: "system", content: sysParts });
if (history.length) messages.push(...history);
messages.push({ role: "user", content: prompt });
// OpenAI GPT-5 / o1 / o3 / o4 系列要求 max_completion_tokens,旧 max_tokens 不支持。
// DeepSeek / Kimi 仍用 max_tokens。按 model 名前缀分支。
const useNewTokenParam = /^(gpt-5|o1-|o3-|o4-)/.test(modelCfg.model);
// Reasoning model 通常只支持 temperature=1,自定义会 400 报错。
// 优先看 config 的 `reasoning: true` flag(显式声明),fallback 走已知 reasoning 模型名正则。
// 已知:OpenAI GPT-5.5 / o1 / o3 / o4,Moonshot Kimi K2.6 等。
const isReasoningModel = modelCfg.reasoning === true
|| /^(gpt-5\.5|o1-|o3-|o4-)/.test(modelCfg.model);
const reqBody = {
model: modelCfg.model,
messages,
[useNewTokenParam ? 'max_completion_tokens' : 'max_tokens']: maxTokens,
};
if (!isReasoningModel) {
reqBody.temperature = extra.temperature ?? modelCfg.temperature ?? DEFAULT_TEMP;
if (extra.topP != null) reqBody.top_p = extra.topP;
}
const features = modelCfg.features || [];
const hasToolLoop = features.includes("web_search");
if (hasToolLoop) {
reqBody.tools = [{ type: "builtin_function", function: { name: "$web_search" } }];
}
const headers = { "Content-Type": "application/json" };
if (modelCfg.apiKey) headers.Authorization = `Bearer ${modelCfg.apiKey}`;
// 非流式分支:web_search tool loop 或显式 force_non_streaming(如 DeepSeek V4 Pro 思考模式)
// 思考模型流式 chunk 只走 delta.reasoning_content,delta.content 长时间空,且 token 易被 reasoning 吃完
// 导致最终可见输出为空。强制非流式可正常读 message.content。
if (hasToolLoop || modelCfg.force_non_streaming) {
const fetchOpts = { method: "POST", headers, body: JSON.stringify(reqBody) };
let data = await fetchWithRetry(modelCfg.endpoint, fetchOpts, { agent: modelCfg.key });
let choice = data.choices?.[0];
const loopMax = modelCfg.tool_loop_max || 5;
let rounds = 0;
while (choice?.finish_reason === "tool_calls" && choice?.message?.tool_calls && rounds < loopMax) {
rounds++;
messages.push(choice.message);
for (const tc of choice.message.tool_calls) {
messages.push({ role: "tool", tool_call_id: tc.id, name: tc.function.name, content: tc.function.arguments || "{}" });
}
reqBody.messages = messages;
data = await fetchWithRetry(modelCfg.endpoint,
{ method: "POST", headers, body: JSON.stringify(reqBody) },
{ agent: modelCfg.key }
);
choice = data.choices?.[0];
}
const usage = data.usage || {};
return {
content: choice?.message?.content || "",
model: data.model || modelCfg.model,
tokens: { prompt: usage.prompt_tokens || 0, completion: usage.completion_tokens || 0, total: usage.total_tokens || 0 },
};
}
// 流式请求
reqBody.stream = true;
reqBody.stream_options = { include_usage: true };
const res = await fetchStream(modelCfg.endpoint,
{ method: "POST", headers, body: JSON.stringify(reqBody) },
{ agent: modelCfg.key }
);
let content = "";
let usage = {};
let model = modelCfg.model;
for await (const chunk of parseSSE(res)) {
const delta = chunk.choices?.[0]?.delta?.content || "";
if (delta) {
content += delta;
emitEvent({ type: "AGENT_CHUNK", agent: modelCfg.key, delta });
}
if (chunk.usage) usage = chunk.usage;
if (chunk.model) model = chunk.model;
}
return {
content,
model,
tokens: { prompt: usage.prompt_tokens || 0, completion: usage.completion_tokens || 0, total: usage.total_tokens || 0 },
};
}
// ── 适配器:Gemini ──
async function adapterGemini(modelCfg, prompt, systemPrompt, maxTokens, history = [], extra = {}) {
const contents = history.map(msg => ({
role: msg.role === "assistant" ? "model" : "user",
parts: [{ text: msg.content }],
}));
contents.push({ role: "user", parts: [{ text: prompt }] });
const body = {
contents,
generationConfig: { maxOutputTokens: maxTokens, temperature: extra.temperature ?? DEFAULT_TEMP },
};
if (extra.topP != null) body.generationConfig.topP = extra.topP;
const features = modelCfg.features || [];
if (features.includes("google_search")) {
body.tools = [{ google_search: {} }];
}
if (systemPrompt) {
body.systemInstruction = { parts: [{ text: systemPrompt }] };
}
// Image generation: 区分两套 API
// - Imagen 系列 (imagen-*) 走 :predict + instances/parameters
// - Gemini 原生图像 (gemini-*-image-*) 走 :generateContent + responseModalities
if (modelCfg.image_generation) {
const isImagenAPI = /^imagen-/.test(modelCfg.model);
const imgCfg = extra.imageConfig || {};
if (isImagenAPI) {
const predictBody = {
instances: [{ prompt }],
parameters: {
sampleCount: imgCfg.sampleCount || 1,
...(imgCfg.aspectRatio && { aspectRatio: imgCfg.aspectRatio }),
...(imgCfg.personGeneration && { personGeneration: imgCfg.personGeneration }),
},
};
const url = `${modelCfg.endpoint}/models/${modelCfg.model}:predict?key=${modelCfg.apiKey}`;
const data = await fetchWithRetry(url,
{ method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(predictBody) },
{ agent: modelCfg.key }
);
const predictions = data.predictions || [];
const images = predictions.map(p => ({
mimeType: p.mimeType || "image/png",
data: p.bytesBase64Encoded,
}));
return {
content: `Generated ${images.length} image(s)`,
images,
model: modelCfg.model,
tokens: { prompt: 0, completion: 0, total: 0 },
};
}
// Gemini 原生图像(responseModalities 路径)
body.generationConfig.responseModalities = ["TEXT", "IMAGE"];
if (Object.keys(imgCfg).length) body.generationConfig.imageConfig = imgCfg;
const url = `${modelCfg.endpoint}/models/${modelCfg.model}:generateContent?key=${modelCfg.apiKey}`;
const data = await fetchWithRetry(url,
{ method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body) },
{ agent: modelCfg.key }
);
let content = "";
const images = [];
const parts = data.candidates?.[0]?.content?.parts || [];
for (const part of parts) {
if (part.text) content += part.text;
if (part.inlineData) images.push({ mimeType: part.inlineData.mimeType, data: part.inlineData.data });
}
const usage = data.usageMetadata || {};
return {
content, images,
model: modelCfg.model,
tokens: { prompt: usage.promptTokenCount || 0, completion: usage.candidatesTokenCount || 0, total: usage.totalTokenCount || 0 },
};
}
// 流式:streamGenerateContent + alt=sse
const url = `${modelCfg.endpoint}/models/${modelCfg.model}:streamGenerateContent?key=${modelCfg.apiKey}&alt=sse`;
const res = await fetchStream(url,
{ method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body) },
{ agent: modelCfg.key }
);
let content = "";
let usage = {};
for await (const chunk of parseSSE(res)) {
const parts = chunk.candidates?.[0]?.content?.parts || [];
for (const part of parts) {
if (part.text) {
content += part.text;
emitEvent({ type: "AGENT_CHUNK", agent: modelCfg.key, delta: part.text });
}
}
if (chunk.usageMetadata) usage = chunk.usageMetadata;
}
return {
content,
model: modelCfg.model,
tokens: { prompt: usage.promptTokenCount || 0, completion: usage.candidatesTokenCount || 0, total: usage.totalTokenCount || 0 },
};
}
// ── Veo 视频生成(异步轮询) ──
async function generateVeoVideo(modelCfg, prompt, { aspectRatio = "16:9", durationSeconds = 8 } = {}) {
const apiKey = modelCfg.apiKey;
const model = modelCfg.model;
const baseUrl = modelCfg.endpoint;
// Step 1: 提交生成请求
const submitUrl = `${baseUrl}/models/${model}:predictLongRunning?key=${apiKey}`;
emitEvent({ type: "AGENT_START", agent: modelCfg.key, model, prompt });
const submitBody = {
instances: [{ prompt }],
parameters: { sampleCount: 1, durationSeconds, aspectRatio },
};
const submitRes = await fetchWithRetry(submitUrl, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(submitBody),
}, { agent: modelCfg.key });
const opName = submitRes.name;
if (!opName) throw new Error("No operation name in response: " + JSON.stringify(submitRes).slice(0, 500));
emitEvent({ type: "AGENT_CHUNK", agent: modelCfg.key, delta: `[submitted: ${opName}]` });
// Step 2: 轮询等待完成
const VIDEO_TIMEOUT = 180_000; // 3 min
const POLL_INTERVAL = 5_000; // 5s
const t0 = Date.now();
while (Date.now() - t0 < VIDEO_TIMEOUT) {
await new Promise(r => setTimeout(r, POLL_INTERVAL));
const pollUrl = `${baseUrl}/${opName}?key=${apiKey}`;
const pollRes = await fetch(pollUrl, { signal: AbortSignal.timeout(30_000) });
if (!pollRes.ok) {
const errText = await pollRes.text();
throw new Error(`Video poll error (${pollRes.status}): ${errText}`);
}
const status = await pollRes.json();
if (status.error) {
throw new Error(`Video generation failed: ${JSON.stringify(status.error)}`);
}
const elapsed = Math.round((Date.now() - t0) / 1000);
emitEvent({ type: "AGENT_CHUNK", agent: modelCfg.key, delta: `[polling ${elapsed}s...]` });
if (status.done) {
// 尝试多种可能的响应格式
const resp = status.response || {};
const samples = resp.generateVideoResponse?.generatedSamples
|| resp.generatedVideos
|| [];
if (!samples.length) throw new Error("Video generation completed but no samples: " + JSON.stringify(status).slice(0, 1000));
const videos = [];
for (const sample of samples) {
const videoObj = sample.video || sample;
const uri = videoObj.uri;
if (!uri) continue;
// 下载视频文件
const dlUrl = uri.includes("key=") ? uri : `${uri}${uri.includes("?") ? "&" : "?"}key=${apiKey}`;
const dlRes = await fetch(dlUrl, { signal: AbortSignal.timeout(60_000) });
if (!dlRes.ok) throw new Error(`Video download failed (${dlRes.status}): ${await dlRes.text()}`);
const buffer = Buffer.from(await dlRes.arrayBuffer());
const encoding = videoObj.encoding || "video/mp4";
const ext = encoding.includes("webm") ? "webm" : "mp4";
videos.push({ buffer, ext });
}
if (!videos.length) throw new Error("No downloadable videos in response");
const duration_ms = Date.now() - t0;
emitEvent({ type: "AGENT_END", agent: modelCfg.key, model, duration_ms, content: `[${videos.length} video(s) generated]` });
return { videos, duration_ms };
}
}
throw new Error(`Video generation timed out (${VIDEO_TIMEOUT / 1000}s)`);
}
// ── 通用调用入口 ──
const adapters = { openai: adapterOpenAI, gemini: adapterGemini };
async function callModel(key, prompt, { systemPrompt = "", maxTokens = DEFAULT_MAX_TOKENS, conversationId = "", _isFallback = false, _skipCache = false, imageConfig, temperature, topP } = {}) {
const cfg = models[key];
if (!cfg) throw new Error(`模型 "${key}" 未配置或 API key 缺失`);
const adapter = adapters[cfg.adapter];
if (!adapter) throw new Error(`未知的 adapter 类型: ${cfg.adapter}`);
// Budget check
checkBudget();
// Cache lookup (skip for conversations and explicit bypass)
const ck = makeCacheKey(key, prompt, systemPrompt);
if (CACHE_TTL > 0 && !conversationId && !_skipCache) {
const cached = responseCache.get(ck);
if (cached && Date.now() < cached.expires) {
emitEvent({ type: "CACHE_HIT", agent: key, prompt: prompt.slice(0, 100) });
return { ...cached.result };
}
}
const history = getHistory(conversationId, key);
const t0 = Date.now();
emitEvent({ type: "AGENT_START", agent: key, model: cfg.model, prompt, systemPrompt, conversationId: conversationId || undefined, historyTurns: history.length / 2 });
try {
const result = await adapter(cfg, prompt, systemPrompt, maxTokens, history, { imageConfig, temperature, topP });
result.duration_ms = Date.now() - t0;
result.cost_usd = calcCost(result.tokens, cfg.pricing);
budget.spent += result.cost_usd;
saveHistory(conversationId, key, prompt, result.content);
emitEvent({ type: "AGENT_END", agent: key, model: result.model, content: result.content, tokens: result.tokens, duration_ms: result.duration_ms, cost_usd: result.cost_usd });
// Cache store
if (CACHE_TTL > 0 && !conversationId && !_skipCache) {
responseCache.set(ck, { result: { ...result }, expires: Date.now() + CACHE_TTL });
}
return result;
} catch (e) {
const duration = Date.now() - t0;
emitEvent({ type: "AGENT_ERROR", agent: key, error: e.message, duration_ms: duration });
// Fallback: if not already a fallback attempt and a fallback model is configured
if (!_isFallback && cfg.fallbackTo && models[cfg.fallbackTo]) {
emitEvent({ type: "AGENT_FALLBACK", from: key, to: cfg.fallbackTo, error: e.message });
return callModel(cfg.fallbackTo, prompt, { systemPrompt, maxTokens, conversationId, _isFallback: true });
}
throw e;
}
}
// ── 成本计算 ──
function calcCost(tokens, pricing) {
if (!pricing) return 0;
return (tokens.prompt * pricing.input + tokens.completion * pricing.output) / 1_000_000;
}
// ── 格式化 ──
function fmt(name, r) {
const sec = (r.duration_ms / 1000).toFixed(1);
const costStr = r.cost_usd > 0 ? ` | $${r.cost_usd.toFixed(6)}` : "";
return `━━━ ${name} (${r.model}) ━━━\n${r.content}\n[tokens: ${r.tokens.prompt} in → ${r.tokens.completion} out, total ${r.tokens.total} | ${sec}s${costStr}]`;
}
function costSummary(results, keys) {
const items = [];
let totalCost = 0;
let totalTokens = 0;
for (let i = 0; i < results.length; i++) {
if (results[i].status !== "fulfilled") continue;
const r = results[i].value;
const name = models[keys[i]]?.name || keys[i];
totalCost += r.cost_usd || 0;
totalTokens += r.tokens?.total || 0;
if (r.cost_usd > 0) items.push(`${name} $${r.cost_usd.toFixed(6)}`);
}
const count = results.filter(r => r.status === "fulfilled").length;
const costLine = items.length ? items.join(" + ") + ` = $${totalCost.toFixed(6)}` : "no cost data";
return `\n📊 ${count} models called | ${totalTokens} tokens | ${costLine}`;
}
// ── MCP Server ──
const server = new McpServer({ name: "mcp-multi-model", version: PKG_VERSION }, { capabilities: { logging: {} } });
// 动态注册每个模型的 ask_{key} 工具
for (const [key, cfg] of Object.entries(models)) {
server.tool(`ask_${key}`, cfg.description || `向 ${cfg.name} 发送请求。`, {
prompt: z.string().describe("提示词"),
system_prompt: z.string().optional().describe("系统提示词"),
max_tokens: z.number().optional().default(DEFAULT_MAX_TOKENS).describe("最大 token 数"),
conversation_id: z.string().optional().describe("对话 ID,传入相同 ID 可保持多轮上下文"),
}, { readOnlyHint: true, openWorldHint: true }, async ({ prompt, system_prompt, max_tokens, conversation_id }) => {
try {
const r = await callModel(key, prompt, { systemPrompt: system_prompt || "", maxTokens: max_tokens, conversationId: conversation_id || "" });
const parts = [{ type: "text", text: fmt(cfg.name, r) }];
if (r.images?.length) {
for (const img of r.images) parts.push({ type: "image", data: img.data, mimeType: img.mimeType });
}
return { content: parts };
} catch (e) {
return { content: [{ type: "text", text: `${cfg.name} 错误: ${e.message}` }], isError: true };
}
});
}
// ask_ai — 通用入口,指定模型 + 推理参数
const modelEnum = z.enum(modelKeys);
server.tool("ask_ai", `Unified entry point: send a prompt to any configured model (${modelKeys.join(", ")}). Supports per-call inference parameters.`, {
model: modelEnum.describe(`Target model key: ${modelKeys.join(", ")}`),
prompt: z.string().describe("The prompt to send"),
system_prompt: z.string().optional().describe("Optional system prompt"),
max_tokens: z.number().optional().default(DEFAULT_MAX_TOKENS).describe("Max output tokens"),
temperature: z.number().min(0).max(2).optional().describe("Sampling temperature (0-2). Overrides model default for this call."),
top_p: z.number().min(0).max(1).optional().describe("Top-p (nucleus sampling, 0-1). Overrides model default for this call."),
conversation_id: z.string().optional().describe("Conversation ID for multi-turn context"),
}, { readOnlyHint: true, openWorldHint: true }, async ({ model, prompt, system_prompt, max_tokens, temperature, top_p, conversation_id }) => {
try {
const r = await callModel(model, prompt, {
systemPrompt: system_prompt || "",
maxTokens: max_tokens,
conversationId: conversation_id || "",
temperature,
topP: top_p,
});
const cfg = models[model];
const parts = [{ type: "text", text: fmt(cfg.name, r) }];
if (r.images?.length) {
for (const img of r.images) parts.push({ type: "image", data: img.data, mimeType: img.mimeType });
}
return { content: parts };
} catch (e) {
return { content: [{ type: "text", text: `${models[model]?.name || model} error: ${e.message}` }], isError: true };
}
});
// check_health — 检查所有模型健康状态
server.tool("check_health", "Ping all configured models and report online/offline status with latency. Generation-only models (image/video) are skipped to avoid wrong endpoint pings and unintended billing.", {}, { readOnlyHint: true, openWorldHint: true }, async () => {
const results = await Promise.allSettled(
modelKeys.map(async (key) => {
const cfg = models[key];
// 生成型模型(视频/图像)走专用 endpoint:streamGenerateContent/chat 不接受它们
// ping 既会误报 offline 又会真实扣费(图像生成),直接跳过
if (cfg.video_generation || cfg.image_generation) {
const kind = cfg.video_generation ? "video" : "image";
return { key, name: cfg.name, status: "skipped", latency_ms: 0, note: `${kind}-only, not ping-tested` };
}
const adapter = adapters[cfg.adapter];
const t0 = Date.now();
try {
await adapter(cfg, "Hi", "", 5, []);
return { key, name: cfg.name, status: "online", latency_ms: Date.now() - t0 };
} catch (e) {
return { key, name: cfg.name, status: "offline", latency_ms: Date.now() - t0, error: e.message };
}
})
);
const lines = results.map(r => {
const v = r.status === "fulfilled" ? r.value : { name: "?", status: "error", latency_ms: 0, error: r.reason?.message };
const icon = v.status === "online" ? "✅" : v.status === "skipped" ? "⏭️ " : "❌";
const detail = v.note ? ` — ${v.note}` : v.error ? ` — ${v.error}` : "";
const latency = v.status === "skipped" ? "" : ` (${v.latency_ms}ms)`;
return `${icon} ${v.name}: ${v.status}${latency}${detail}`;
});
return { content: [{ type: "text", text: lines.join("\n") }] };
});
// ask_all — 并行调用所有模型
if (modelKeys.length >= 2) {
server.tool("ask_all", `Query all configured models in parallel and return side-by-side comparison (并行查询所有配置模型,返回对比结果). Models: ${modelKeys.map(k => models[k].name).join(", ")}.`, {
prompt: z.string().describe("通用提示词"),
system_prompt: z.string().optional().describe("共用系统提示词"),
conversation_id: z.string().optional().describe("对话 ID,传入相同 ID 可保持多轮上下文"),
}, { readOnlyHint: true, openWorldHint: true }, async ({ prompt, system_prompt, conversation_id }) => {
const opts = { systemPrompt: system_prompt || "", conversationId: conversation_id || "" };
const results = await Promise.allSettled(
modelKeys.map(k => callModel(k, prompt, opts))
);
const parts = results.map((r, i) =>
r.status === "fulfilled"
? fmt(models[modelKeys[i]].name, r.value)
: `━━━ ${models[modelKeys[i]].name} 错误 ━━━\n${r.reason?.message}`
);
parts.push(costSummary(results, modelKeys));
return { content: [{ type: "text", text: parts.join("\n\n" + "═".repeat(50) + "\n\n") }] };
});
}
// ask_both — 并行调用任意两个模型
if (modelKeys.length >= 2) {
const modelEnum = z.enum(modelKeys);
server.tool("ask_both", "Query two specified models in parallel and return side-by-side comparison (并行查询两个指定模型,返回对比结果).", {
prompt: z.string().describe("通用提示词"),
model_a: modelEnum.optional().default(modelKeys[0]).describe("第一个模型"),
model_b: modelEnum.optional().default(modelKeys[1]).describe("第二个模型"),
system_prompt: z.string().optional().describe("共用系统提示词"),
conversation_id: z.string().optional().describe("对话 ID,传入相同 ID 可保持多轮上下文"),
}, { readOnlyHint: true, openWorldHint: true }, async ({ prompt, model_a, model_b, system_prompt, conversation_id }) => {
const opts = { systemPrompt: system_prompt || "", conversationId: conversation_id || "" };
const results = await Promise.allSettled([
callModel(model_a, prompt, opts),
callModel(model_b, prompt, opts),
]);
const keys = [model_a, model_b];
const names = [models[model_a]?.name || model_a, models[model_b]?.name || model_b];
const parts = [];
parts.push(results[0].status === "fulfilled" ? fmt(names[0], results[0].value) : `━━━ ${names[0]} 错误 ━━━\n${results[0].reason?.message}`);
parts.push("\n" + "═".repeat(50) + "\n");
parts.push(results[1].status === "fulfilled" ? fmt(names[1], results[1].value) : `━━━ ${names[1]} 错误 ━━━\n${results[1].reason?.message}`);
parts.push(costSummary(results, keys));
return { content: [{ type: "text", text: parts.join("\n") }] };
});
}
// delegate — 智能委派路由工具
const routingCfg = config.routing;
if (routingCfg) {
const categories = routingCfg.categories || {};
const categoryNames = Object.keys(categories);
const fallbackModel = routingCfg.fallback || modelKeys[0];
// 关键词匹配:扫描 task 文本,返回 { category, model, reason }
function routeTask(task, hintCategory) {
// 如果有明确的 category hint,直接使用
if (hintCategory && categories[hintCategory]) {
const cat = categories[hintCategory];
const target = models[cat.delegate_to] ? cat.delegate_to : fallbackModel;
return { category: hintCategory, model: target, reason: cat.reason };
}
// 关键词扫描
const taskLower = task.toLowerCase();
let bestMatch = null;
let bestScore = 0;
for (const [catName, cat] of Object.entries(categories)) {
if (!models[cat.delegate_to]) continue;
let score = 0;
for (const kw of cat.keywords || []) {
if (taskLower.includes(kw.toLowerCase())) score++;
}
if (score > bestScore) {
bestScore = score;
bestMatch = { category: catName, model: cat.delegate_to, reason: cat.reason };
}
}
if (bestMatch) return bestMatch;
return { category: "default", model: fallbackModel, reason: "无明确匹配,使用默认模型" };
}
const categoryEnum = categoryNames.length > 0
? z.enum([...categoryNames, "auto"]).optional().default("auto")
: z.string().optional().default("auto");
server.tool("delegate", "智能委派:根据任务内容自动选择最合适的模型执行。Claude 不想干的活丢过来。", {
task: z.string().describe("任务描述,详细说明需要做什么"),
category: categoryEnum.describe(`任务类别提示(${categoryNames.join("/")}),auto 为自动判断`),
system_prompt: z.string().optional().describe("额外的系统提示词"),
max_tokens: z.number().optional().default(DEFAULT_MAX_TOKENS).describe("最大 token 数"),
conversation_id: z.string().optional().describe("对话 ID"),
}, { readOnlyHint: true, openWorldHint: true }, async ({ task, category, system_prompt, max_tokens, conversation_id }) => {
try {
const route = routeTask(task, category === "auto" ? null : category);
const opts = {
systemPrompt: system_prompt || "",
maxTokens: max_tokens,
conversationId: conversation_id || "",
};
emitEvent({ type: "DELEGATE_ROUTE", task: task.slice(0, 200), category: route.category, model: route.model, reason: route.reason });
const r = await callModel(route.model, task, opts);
const header = `🎯 委派路由: ${route.category} → ${models[route.model].name} (${route.reason})`;
return { content: [{ type: "text", text: `${header}\n\n${fmt(models[route.model].name, r)}` }] };
} catch (e) {
return { content: [{ type: "text", text: `委派错误: ${e.message}` }], isError: true };
}
});
}
// translate — 翻译工具
const translateCfg = config.tools?.translate;
if (translateCfg && models[translateCfg.model]) {
server.tool("translate", translateCfg.description || "中英互译。", {
text: z.string().describe("需要翻译的文本"),
target_language: z.enum(["中文", "英文", "auto"]).optional().default("auto").describe("目标语言"),
style: z.enum(["formal", "casual", "technical"]).optional().default("formal").describe("翻译风格"),
}, { readOnlyHint: true, openWorldHint: true }, async ({ text, target_language, style }) => {
const styles = { formal: "正式专业", casual: "口语化", technical: "技术文档风格,保留专有名词" };
const sys = `你是专业翻译。风格:${styles[style]}。目标语言:${target_language === "auto" ? "自动检测,中↔英互译" : target_language}。只输出翻译结果。`;
try {
const r = await callModel(translateCfg.model, text, { systemPrompt: sys });
return { content: [{ type: "text", text: `翻译结果 (${style}):\n\n${r.content}\n\n[${models[translateCfg.model].name} · ${r.tokens.total} tokens · ${(r.duration_ms / 1000).toFixed(1)}s]` }] };
} catch (e) {
return { content: [{ type: "text", text: `翻译错误: ${e.message}` }], isError: true };
}
});
}
// research — 调研工具
const researchCfg = config.tools?.research;
if (researchCfg && models[researchCfg.model]) {
server.tool("research", researchCfg.description || "技术调研和分析。", {
topic: z.string().describe("研究主题"),
depth: z.enum(["brief", "standard", "deep"]).optional().default("standard").describe("研究深度"),
language: z.enum(["中文", "英文"]).optional().default("中文").describe("输出语言"),
}, { readOnlyHint: true, openWorldHint: true }, async ({ topic, depth, language }) => {
const depths = { brief: "200字以内", standard: "500字左右", deep: "1000字以上,含背景、现状、优劣、建议" };
const sys = `你是资深技术研究员。深度:${depths[depth]}。语言:${language}。结构化输出。`;
try {
const r = await callModel(researchCfg.model, topic, { systemPrompt: sys, maxTokens: depth === "deep" ? 8000 : DEFAULT_MAX_TOKENS });
return { content: [{ type: "text", text: `研究结果:\n\n${r.content}\n\n[${models[researchCfg.model].name} · ${r.tokens.total} tokens · ${(r.duration_ms / 1000).toFixed(1)}s]` }] };
} catch (e) {
return { content: [{ type: "text", text: `研究错误: ${e.message}` }], isError: true };
}
});
}
// generate_image — 图片生成工具
const imgGenCfg = config.tools?.generate_image;
if (imgGenCfg && models[imgGenCfg.model]) {
server.tool("generate_image", imgGenCfg.description || "Generate images from text descriptions.", {
prompt: z.string().describe("Image description / what to generate"),
aspect_ratio: z.enum(["1:1", "3:2", "4:3", "16:9", "9:16"]).optional().default("1:1").describe("Image aspect ratio"),
save_path: z.string().optional().describe("Save image to this path. If omitted, saves to /tmp/mcp-images/ and auto-opens."),
}, { readOnlyHint: false, destructiveHint: false, idempotentHint: false, openWorldHint: true }, async ({ prompt, aspect_ratio, save_path }) => {
try {
const modelKey = imgGenCfg.model;
const r = await callModel(modelKey, prompt, {
imageConfig: { aspectRatio: aspect_ratio },
_skipCache: true,
});
const parts = [];
if (r.content) parts.push({ type: "text", text: r.content });
// Save images to disk
const savedPaths = [];
if (r.images?.length) {
for (let i = 0; i < r.images.length; i++) {
const img = r.images[i];
const ext = img.mimeType?.includes("png") ? "png" : "jpg";
let filePath;
if (save_path) {
filePath = r.images.length === 1 ? save_path : save_path.replace(/(\.\w+)$/, `_${i}$1`);
} else {
const tmpDir = "/tmp/mcp-media/images";
mkdirSync(tmpDir, { recursive: true });
const ts = new Date().toISOString().replace(/[:.]/g, "-").slice(0, 19);
filePath = join(tmpDir, `img_${ts}${r.images.length > 1 ? `_${i}` : ""}.${ext}`);
}
writeFileSync(filePath, Buffer.from(img.data, "base64"));
savedPaths.push(filePath);
}
// Auto-open when saving to /tmp (no explicit save_path)
if (!save_path) {
try { execSync(`open "${savedPaths[0]}"`); } catch {}
}
}
const sec = (r.duration_ms / 1000).toFixed(1);
const costStr = r.cost_usd > 0 ? ` · $${r.cost_usd.toFixed(6)}` : "";
const pathStr = savedPaths.length ? ` · ${savedPaths.join(", ")}` : "";
parts.push({ type: "text", text: `[${models[modelKey].name} · ${r.tokens.total} tokens · ${sec}s${costStr}${pathStr}]` });
return { content: parts };
} catch (e) {
return { content: [{ type: "text", text: `Image generation error: ${e.message}` }], isError: true };
}
});
}
// generate_video — 视频生成工具
const vidGenCfg = config.tools?.generate_video;
if (vidGenCfg && models[vidGenCfg.model]) {
server.tool("generate_video", vidGenCfg.description || "Generate videos from text descriptions.", {
prompt: z.string().describe("Video description / what to generate"),
aspect_ratio: z.enum(["16:9", "9:16", "1:1"]).optional().default("16:9").describe("Video aspect ratio"),
duration: z.union([z.literal(4), z.literal(6), z.literal(8)]).optional().default(8).describe("Duration in seconds (4, 6, or 8)"),
save_path: z.string().optional().describe("Save video to this path. If omitted, saves to /tmp/mcp-media/videos/ and auto-opens."),
}, { readOnlyHint: false, destructiveHint: false, idempotentHint: false, openWorldHint: true }, async ({ prompt, aspect_ratio, duration, save_path }) => {
try {
const modelKey = vidGenCfg.model;
const cfg = models[modelKey];
const r = await generateVeoVideo(cfg, prompt, {
aspectRatio: aspect_ratio,
durationSeconds: duration,
});
const savedPaths = [];
for (let i = 0; i < r.videos.length; i++) {
const vid = r.videos[i];
let filePath;
if (save_path) {
filePath = r.videos.length === 1 ? save_path : save_path.replace(/(\.\w+)$/, `_${i}$1`);
} else {
const tmpDir = "/tmp/mcp-media/videos";
mkdirSync(tmpDir, { recursive: true });
const ts = new Date().toISOString().replace(/[:.]/g, "-").slice(0, 19);
filePath = join(tmpDir, `vid_${ts}${r.videos.length > 1 ? `_${i}` : ""}.${vid.ext}`);
}
writeFileSync(filePath, vid.buffer);
savedPaths.push(filePath);
}