@huyooo/ai-chat-core 0.2.13 → 0.2.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,2846 +1 @@
1
- import { GoogleGenAI } from '@google/genai';
2
-
3
- // src/agent.ts
4
-
5
- // src/constants.ts
6
- var DEFAULT_ARK_URL = "https://ark.cn-beijing.volces.com/api/v3";
7
- var DEFAULT_QWEN_URL = "https://dashscope.aliyuncs.com/api/v1";
8
- var DEFAULT_QWEN_NATIVE_URL = "https://dashscope.aliyuncs.com/api/v1";
9
- var DEFAULT_OPENROUTER_URL = "https://openrouter.ai/api/v1";
10
- var DOUBAO_SEED_1_8 = {
11
- id: "doubao-seed-1-8-251215"};
12
- var DOUBAO_SEED_1_6 = {
13
- id: "doubao-seed-1-6-250615",
14
- displayName: "\u8C46\u5305 Seed 1.6",
15
- provider: "\u8C46\u5305"
16
- };
17
- var DOUBAO_SEED_1_6_LATEST = {
18
- id: "doubao-seed-1-6-251015"};
19
- var DOUBAO_SEED_1_6_FLASH = {
20
- id: "doubao-seed-1-6-flash-250617"};
21
- var DEEPSEEK_V3 = {
22
- id: "deepseek-v3-1-terminus",
23
- displayName: "DeepSeek V3",
24
- provider: "DeepSeek"
25
- };
26
- var QWEN_MAX = { id: "qwen-max"};
27
- var QWEN_MAX_LATEST = { id: "qwen-max-latest"};
28
- var QWEN_PLUS = { id: "qwen-plus"};
29
- var QWEN_PLUS_LATEST = { id: "qwen-plus-latest"};
30
- var QWEN_TURBO = { id: "qwen-turbo"};
31
- var QWEN_TURBO_LATEST = { id: "qwen-turbo-latest"};
32
- var QWEN3_235B = { id: "qwen3-235b-a22b"};
33
- var QWEN3_MAX_PREVIEW = {
34
- id: "qwen3-max-preview",
35
- displayName: "\u901A\u4E49\u5343\u95EE 3 Max",
36
- provider: "\u901A\u4E49\u5343\u95EE"
37
- };
38
- var GEMINI_2_5_FLASH = { id: "gemini-2.5-flash-preview-05-20"};
39
- var GEMINI_2_5_PRO = { id: "gemini-2.5-pro-preview-05-06"};
40
- var GEMINI_2_0_FLASH = { id: "gemini-2.0-flash"};
41
- var GEMINI_2_0_FLASH_LITE = { id: "gemini-2.0-flash-lite"};
42
- var GEMINI_3_PRO = {
43
- id: "gemini-3-pro-preview",
44
- displayName: "Gemini 3 Pro",
45
- provider: "Gemini"
46
- };
47
- var GEMINI_IMAGE_MODEL = "gemini-2.0-flash";
48
- var GEMINI_IMAGE_GEN_MODEL = "gemini-3-pro-image-preview";
49
- var OR_GEMINI_2_5_FLASH = {
50
- id: "google/gemini-2.5-flash-preview"};
51
- var OR_GEMINI_2_5_PRO = {
52
- id: "google/gemini-2.5-pro-preview"};
53
- var OR_GEMINI_2_0_FLASH = {
54
- id: "google/gemini-2.0-flash-001"};
55
- var OR_GEMINI_3_PRO = {
56
- id: "google/gemini-3-pro-preview",
57
- displayName: "Gemini 3 Pro",
58
- isOpenRouter: true,
59
- provider: "Gemini"
60
- };
61
- var OR_GPT_5_2 = {
62
- id: "openai/gpt-5.2",
63
- displayName: "GPT-5.2",
64
- isOpenRouter: true,
65
- provider: "OpenAI"
66
- };
67
- var OR_CLAUDE_OPUS_4_5 = {
68
- id: "anthropic/claude-opus-4.5",
69
- displayName: "Claude Opus 4.5",
70
- isOpenRouter: true,
71
- provider: "Anthropic"
72
- };
73
- var OR_QWEN3_MAX = {
74
- id: "qwen/qwen3-max",
75
- displayName: "\u901A\u4E49\u5343\u95EE Max",
76
- isOpenRouter: true,
77
- provider: "\u901A\u4E49\u5343\u95EE"
78
- };
79
- var OR_DEEPSEEK_R1 = {
80
- id: "deepseek/deepseek-r1"};
81
- var OR_DEEPSEEK_CHAT_V3 = {
82
- id: "deepseek/deepseek-chat-v3-0324"};
83
- var OR_DEEPSEEK_V3_2 = {
84
- id: "deepseek/deepseek-v3.2",
85
- displayName: "DeepSeek V3.2",
86
- isOpenRouter: true,
87
- provider: "DeepSeek"
88
- };
89
- var DISPLAY_MODELS = [
90
- // 原生模型
91
- DOUBAO_SEED_1_6,
92
- DEEPSEEK_V3,
93
- QWEN3_MAX_PREVIEW,
94
- GEMINI_3_PRO,
95
- // OpenRouter 模型
96
- OR_GPT_5_2,
97
- OR_CLAUDE_OPUS_4_5,
98
- OR_GEMINI_3_PRO,
99
- OR_QWEN3_MAX,
100
- OR_DEEPSEEK_V3_2
101
- ];
102
- var DEFAULT_MODEL = DOUBAO_SEED_1_6.id;
103
- var PLAN_MODE_PROMPT = "\u5206\u6790\u9700\u6C42\u5E76\u5236\u5B9A\u6267\u884C\u8BA1\u5212\uFF0C\u4EE5 Markdown \u683C\u5F0F\u8F93\u51FA\u6B65\u9AA4\uFF0C\u7B49\u5F85\u7528\u6237\u786E\u8BA4\u540E\u518D\u6267\u884C\u3002";
104
- var AGENT_MODE_PROMPT = `\u4F60\u662F\u4E00\u4E2A\u4E13\u4E1A\u7684 AI \u52A9\u624B\u3002\u8BF7\u9075\u5FAA\u4EE5\u4E0B\u89C4\u5219\uFF1A
105
- - \u8FD4\u56DE\u7684\u5185\u5BB9\u5C3D\u91CF\u4E0D\u8981\u4F7F\u7528\u8868\u60C5\u7B26\u53F7\uFF08emoji\uFF09\uFF0C\u9664\u975E\u7528\u6237\u8981\u6C42\u4F7F\u7528
106
- - \u4F7F\u7528 Markdown \u683C\u5F0F\u7EC4\u7EC7\u5185\u5BB9
107
- - \u4EE3\u7801\u5757\u4F7F\u7528\u6B63\u786E\u7684\u8BED\u8A00\u6807\u8BC6`;
108
-
109
- // src/types.ts
110
- var MODELS = DISPLAY_MODELS.map((m) => ({
111
- modelId: m.id,
112
- displayName: m.displayName,
113
- group: m.isOpenRouter ? "OpenRouter" : "\u539F\u751F\u6A21\u578B",
114
- // 后端决定分组
115
- isOpenRouter: m.isOpenRouter,
116
- provider: m.provider
117
- }));
118
- function getNativeModels() {
119
- return MODELS.filter((m) => !m.isOpenRouter);
120
- }
121
- function getOpenRouterModels() {
122
- return MODELS.filter((m) => m.isOpenRouter);
123
- }
124
- function getModelByModelId(modelId) {
125
- return MODELS.find((m) => m.modelId === modelId);
126
- }
127
- function tool(t) {
128
- return { tools: [t] };
129
- }
130
- function tools(ts) {
131
- return { tools: ts };
132
- }
133
- async function resolveTools(items) {
134
- const tools2 = [];
135
- for (const item of items) {
136
- const resolved = await item;
137
- if ("tools" in resolved && Array.isArray(resolved.tools)) {
138
- tools2.push(...resolved.tools);
139
- } else {
140
- console.warn("\u5DE5\u5177\u914D\u7F6E\u9879\u5FC5\u987B\u662F ToolPlugin \u5F62\u5F0F:", resolved);
141
- }
142
- }
143
- return tools2;
144
- }
145
-
146
- // src/events.ts
147
- function createThinkingStart() {
148
- return {
149
- type: "thinking_start",
150
- data: { startedAt: Date.now() }
151
- };
152
- }
153
- function createThinkingDelta(content) {
154
- return {
155
- type: "thinking_delta",
156
- data: { content }
157
- };
158
- }
159
- function createThinkingEnd(startedAt) {
160
- const endedAt = Date.now();
161
- return {
162
- type: "thinking_end",
163
- data: {
164
- endedAt,
165
- duration: endedAt - startedAt
166
- }
167
- };
168
- }
169
- function createSearchStart(query) {
170
- return {
171
- type: "search_start",
172
- data: { query, startedAt: Date.now() }
173
- };
174
- }
175
- function createSearchResult(results, startedAt) {
176
- const endedAt = Date.now();
177
- return {
178
- type: "search_result",
179
- data: {
180
- results,
181
- endedAt,
182
- duration: endedAt - startedAt
183
- }
184
- };
185
- }
186
- function createSearchEnd(success, startedAt, error) {
187
- const endedAt = Date.now();
188
- return {
189
- type: "search_end",
190
- data: {
191
- success,
192
- error,
193
- endedAt,
194
- duration: endedAt - startedAt
195
- }
196
- };
197
- }
198
- function createToolCallStart(id, name, args) {
199
- return {
200
- type: "tool_call_start",
201
- data: { id, name, args, startedAt: Date.now() }
202
- };
203
- }
204
- function createToolCallResult(id, name, result, success, startedAt, error, sideEffects) {
205
- const endedAt = Date.now();
206
- return {
207
- type: "tool_call_result",
208
- data: {
209
- id,
210
- name,
211
- result,
212
- success,
213
- error,
214
- endedAt,
215
- duration: endedAt - startedAt,
216
- sideEffects
217
- }
218
- };
219
- }
220
- function createTextDelta(content) {
221
- return {
222
- type: "text_delta",
223
- data: { content }
224
- };
225
- }
226
- function createStreamStart(model, requestId) {
227
- return {
228
- type: "stream_start",
229
- data: {
230
- model,
231
- requestId,
232
- startedAt: Date.now()
233
- }
234
- };
235
- }
236
- function createDone(text, usage, duration) {
237
- return {
238
- type: "done",
239
- data: { text, usage, duration }
240
- };
241
- }
242
- function createError(details) {
243
- return {
244
- type: "error",
245
- data: details
246
- };
247
- }
248
- function createApiError(message, options = {}) {
249
- return createError({
250
- category: "api",
251
- message,
252
- ...options
253
- });
254
- }
255
- function createRateLimitError(message, retryAfter, context) {
256
- return createError({
257
- category: "rate_limit",
258
- message,
259
- code: "RATE_LIMIT",
260
- statusCode: 429,
261
- retryable: true,
262
- retryAfter,
263
- context
264
- });
265
- }
266
- function createToolError(message, toolName, cause) {
267
- return createError({
268
- category: "tool",
269
- message,
270
- code: "TOOL_ERROR",
271
- context: toolName,
272
- cause,
273
- retryable: false
274
- });
275
- }
276
- function createTimeoutError(message, context) {
277
- return createError({
278
- category: "timeout",
279
- message,
280
- code: "TIMEOUT",
281
- retryable: true,
282
- context
283
- });
284
- }
285
- function createParseError(message, cause) {
286
- return createError({
287
- category: "parse",
288
- message,
289
- code: "PARSE_ERROR",
290
- cause,
291
- retryable: false
292
- });
293
- }
294
- function createAbort(reason) {
295
- return {
296
- type: "abort",
297
- data: {
298
- reason,
299
- abortedAt: Date.now()
300
- }
301
- };
302
- }
303
- function createStepStart(stepNumber, description) {
304
- return {
305
- type: "step_start",
306
- data: { stepNumber, description, startedAt: Date.now() }
307
- };
308
- }
309
- function createStepEnd(stepNumber, startedAt) {
310
- const endedAt = Date.now();
311
- return {
312
- type: "step_end",
313
- data: {
314
- stepNumber,
315
- endedAt,
316
- duration: endedAt - startedAt
317
- }
318
- };
319
- }
320
- function createImage(options) {
321
- return {
322
- type: "image",
323
- data: options
324
- };
325
- }
326
- function createVideo(path) {
327
- return {
328
- type: "video",
329
- data: { path }
330
- };
331
- }
332
- function isThinkingEvent(event) {
333
- return event.type.startsWith("thinking_");
334
- }
335
- function isSearchEvent(event) {
336
- return event.type.startsWith("search_");
337
- }
338
- function isToolEvent(event) {
339
- return event.type.startsWith("tool_");
340
- }
341
- function isTextEvent(event) {
342
- return event.type === "text_delta";
343
- }
344
- function isMediaEvent(event) {
345
- return event.type === "image" || event.type === "video";
346
- }
347
- function isStatusEvent(event) {
348
- return event.type === "stream_start" || event.type === "done" || event.type === "error" || event.type === "abort";
349
- }
350
- function isErrorEvent(event) {
351
- return event.type === "error";
352
- }
353
- function isAbortEvent(event) {
354
- return event.type === "abort";
355
- }
356
- function isStepEvent(event) {
357
- return event.type.startsWith("step_");
358
- }
359
- function isRetryableError(event) {
360
- if (event.type !== "error") return false;
361
- return event.data.retryable === true;
362
- }
363
-
364
- // src/tools.ts
365
- var DANGEROUS_COMMANDS = [
366
- "rm -rf /",
367
- "rm -rf ~",
368
- "format",
369
- "mkfs",
370
- "dd if=",
371
- "shutdown",
372
- "reboot",
373
- "sudo rm",
374
- "sudo format"
375
- ];
376
- function isDangerousCommand(command) {
377
- const lowerCommand = command.toLowerCase().trim();
378
- return DANGEROUS_COMMANDS.some(
379
- (dangerous) => lowerCommand.includes(dangerous.toLowerCase())
380
- );
381
- }
382
- function createDefaultToolExecutor(defaultCwd = process.cwd()) {
383
- return {
384
- async executeCommand(command, cwd, signal) {
385
- if (signal?.aborted) {
386
- return { success: false, error: "\u64CD\u4F5C\u5DF2\u53D6\u6D88" };
387
- }
388
- if (isDangerousCommand(command)) {
389
- return { success: false, error: "\u8BE5\u547D\u4EE4\u88AB\u5B89\u5168\u7B56\u7565\u963B\u6B62" };
390
- }
391
- try {
392
- const { spawn } = await import('child_process');
393
- return new Promise((resolve) => {
394
- let stdout = "";
395
- let stderr = "";
396
- let killed = false;
397
- const child = spawn("sh", ["-c", command], {
398
- cwd: cwd || defaultCwd,
399
- env: process.env
400
- });
401
- const timeout = setTimeout(() => {
402
- if (!killed) {
403
- killed = true;
404
- child.kill("SIGTERM");
405
- resolve({ success: false, error: "\u547D\u4EE4\u6267\u884C\u8D85\u65F6\uFF0830\u79D2\uFF09" });
406
- }
407
- }, 3e4);
408
- const abortHandler = () => {
409
- if (!killed) {
410
- killed = true;
411
- child.kill("SIGTERM");
412
- clearTimeout(timeout);
413
- resolve({ success: false, error: "\u64CD\u4F5C\u5DF2\u53D6\u6D88" });
414
- }
415
- };
416
- signal?.addEventListener("abort", abortHandler);
417
- child.stdout?.on("data", (data) => {
418
- stdout += data.toString();
419
- });
420
- child.stderr?.on("data", (data) => {
421
- stderr += data.toString();
422
- });
423
- child.on("close", (code) => {
424
- clearTimeout(timeout);
425
- signal?.removeEventListener("abort", abortHandler);
426
- if (killed) return;
427
- const output = stdout.trim();
428
- const errOutput = stderr.trim();
429
- if (code === 0) {
430
- resolve({ success: true, output: output || errOutput || "\u6267\u884C\u6210\u529F" });
431
- } else {
432
- if (output) {
433
- const warning = errOutput ? `
434
- [\u8B66\u544A: ${errOutput}]` : "";
435
- resolve({ success: true, output: output + warning });
436
- } else if (errOutput) {
437
- resolve({ success: false, error: errOutput });
438
- } else {
439
- resolve({ success: false, error: `\u547D\u4EE4\u9000\u51FA\u7801: ${code}` });
440
- }
441
- }
442
- });
443
- child.on("error", (err) => {
444
- clearTimeout(timeout);
445
- signal?.removeEventListener("abort", abortHandler);
446
- if (!killed) {
447
- resolve({ success: false, error: err.message });
448
- }
449
- });
450
- });
451
- } catch (error) {
452
- return { success: false, error: error instanceof Error ? error.message : String(error) };
453
- }
454
- }
455
- };
456
- }
457
-
458
- // src/router.ts
459
- var MODEL_ROUTES = [
460
- // === 高优先级:OpenRouter 格式(包含 /)===
461
- // 所有包含 / 的模型名都路由到 OpenRouter
462
- // 例如:qwen/qwen3-max, anthropic/claude-opus-4.5, openai/gpt-5.2
463
- {
464
- type: "contains",
465
- pattern: "/",
466
- provider: "openrouter",
467
- priority: 100,
468
- description: "OpenRouter \u683C\u5F0F\u6A21\u578B\uFF08vendor/model\uFF09"
469
- },
470
- // === 中优先级:原生 Provider 前缀匹配 ===
471
- {
472
- type: "prefix",
473
- pattern: "qwen",
474
- provider: "qwen",
475
- priority: 50,
476
- description: "Qwen (DashScope) \u539F\u751F\u6A21\u578B"
477
- },
478
- {
479
- type: "prefix",
480
- pattern: "gemini",
481
- provider: "gemini",
482
- priority: 50,
483
- description: "Gemini (Google AI) \u539F\u751F\u6A21\u578B"
484
- },
485
- // === 低优先级:ARK 火山引擎模型 ===
486
- {
487
- type: "prefix",
488
- pattern: "doubao",
489
- provider: "ark",
490
- priority: 30,
491
- description: "\u8C46\u5305\u6A21\u578B\uFF08\u706B\u5C71\u5F15\u64CE\uFF09"
492
- },
493
- {
494
- type: "prefix",
495
- pattern: "deepseek",
496
- provider: "ark",
497
- priority: 30,
498
- description: "DeepSeek \u6A21\u578B\uFF08\u706B\u5C71\u5F15\u64CE\uFF09"
499
- }
500
- ];
501
- var DEFAULT_PROVIDER = "ark";
502
- var sortedRulesCache = null;
503
- function getSortedRules() {
504
- if (!sortedRulesCache) {
505
- sortedRulesCache = [...MODEL_ROUTES].sort((a, b) => b.priority - a.priority);
506
- }
507
- return sortedRulesCache;
508
- }
509
- function matchRule(model, rule) {
510
- const lowerPattern = rule.pattern.toLowerCase();
511
- switch (rule.type) {
512
- case "exact":
513
- return model === lowerPattern;
514
- case "prefix":
515
- return model.startsWith(lowerPattern);
516
- case "contains":
517
- return model.includes(lowerPattern);
518
- case "regex":
519
- return new RegExp(rule.pattern, "i").test(model);
520
- default:
521
- return false;
522
- }
523
- }
524
- function routeModelToProvider(model) {
525
- return routeModelWithDetails(model).provider;
526
- }
527
- function routeModelWithDetails(model) {
528
- const lowerModel = model.toLowerCase();
529
- const sortedRules = getSortedRules();
530
- for (const rule of sortedRules) {
531
- if (matchRule(lowerModel, rule)) {
532
- return {
533
- provider: rule.provider,
534
- matchedRule: rule,
535
- isDefault: false
536
- };
537
- }
538
- }
539
- return {
540
- provider: DEFAULT_PROVIDER,
541
- isDefault: true
542
- };
543
- }
544
- function getRouteRules() {
545
- return MODEL_ROUTES;
546
- }
547
- function getDefaultProvider() {
548
- return DEFAULT_PROVIDER;
549
- }
550
- function isModelForProvider(model, provider) {
551
- return routeModelToProvider(model) === provider;
552
- }
553
-
554
- // src/providers/orchestrator.ts
555
- var DEFAULT_MAX_ITERATIONS = 10;
556
- var ChatOrchestrator = class {
557
- config;
558
- constructor(config) {
559
- this.config = {
560
- maxIterations: DEFAULT_MAX_ITERATIONS,
561
- ...config
562
- };
563
- }
564
- /**
565
- * 执行聊天
566
- *
567
- * @param adapter Provider 适配器
568
- * @param message 用户消息
569
- * @param context Orchestrator 上下文
570
- * @param options 选项
571
- */
572
- async *chat(adapter, message, context, options) {
573
- const startedAt = Date.now();
574
- const maxIterations = this.config.maxIterations ?? DEFAULT_MAX_ITERATIONS;
575
- const messages = this.buildMessages(context, message);
576
- let iterations = 0;
577
- let finalText = "";
578
- let thinkingText = "";
579
- let thinkingStartedAt = 0;
580
- let thinkingComplete = !options.enableThinking;
581
- let searchResults = [];
582
- let searchStartedAt = 0;
583
- let searchStarted = false;
584
- if (options.enableThinking) {
585
- thinkingStartedAt = Date.now();
586
- yield createThinkingStart();
587
- }
588
- while (iterations < maxIterations) {
589
- if (context.signal.aborted) {
590
- yield createAbort("\u8BF7\u6C42\u5DF2\u53D6\u6D88");
591
- return;
592
- }
593
- iterations++;
594
- try {
595
- const pendingToolCalls = [];
596
- let hasToolCalls = false;
597
- const stream = adapter.streamOnce(messages, context.tools, {
598
- model: options.model,
599
- enableThinking: options.enableThinking && iterations === 1,
600
- // 只在第一轮启用思考
601
- enableSearch: options.enableSearch && iterations === 1,
602
- signal: context.signal
603
- });
604
- for await (const chunk of stream) {
605
- switch (chunk.type) {
606
- case "text":
607
- if (chunk.text) {
608
- if (!thinkingComplete) {
609
- thinkingComplete = true;
610
- yield createThinkingEnd(thinkingStartedAt);
611
- }
612
- finalText += chunk.text;
613
- yield createTextDelta(chunk.text);
614
- }
615
- break;
616
- case "thinking":
617
- if (chunk.thinking) {
618
- thinkingText += chunk.thinking;
619
- yield createThinkingDelta(chunk.thinking);
620
- }
621
- break;
622
- case "thinking_done":
623
- if (!thinkingComplete) {
624
- thinkingComplete = true;
625
- yield createThinkingEnd(thinkingStartedAt);
626
- }
627
- break;
628
- case "tool_call":
629
- if (chunk.toolCall) {
630
- pendingToolCalls.push(chunk.toolCall);
631
- hasToolCalls = true;
632
- }
633
- break;
634
- case "search_result":
635
- if (chunk.searchResults) {
636
- if (!searchStarted) {
637
- searchStarted = true;
638
- searchStartedAt = Date.now();
639
- yield createSearchStart(message);
640
- }
641
- searchResults = chunk.searchResults;
642
- yield createSearchResult(searchResults, searchStartedAt);
643
- }
644
- break;
645
- case "done":
646
- if (chunk.finishReason === "tool_calls") {
647
- hasToolCalls = true;
648
- }
649
- break;
650
- case "error":
651
- yield createApiError(chunk.error ?? "\u672A\u77E5\u9519\u8BEF");
652
- return;
653
- }
654
- }
655
- if (!thinkingComplete) {
656
- thinkingComplete = true;
657
- yield createThinkingEnd(thinkingStartedAt);
658
- }
659
- if (hasToolCalls && pendingToolCalls.length > 0) {
660
- const assistantMessage = {
661
- role: "assistant",
662
- content: finalText,
663
- toolCalls: pendingToolCalls
664
- };
665
- messages.push(assistantMessage);
666
- for (const toolCall of pendingToolCalls) {
667
- const toolStartedAt = Date.now();
668
- let args;
669
- try {
670
- args = JSON.parse(toolCall.arguments || "{}");
671
- } catch {
672
- messages.push({
673
- role: "tool",
674
- content: "\u53C2\u6570\u89E3\u6790\u9519\u8BEF",
675
- toolCallId: toolCall.id
676
- });
677
- continue;
678
- }
679
- const autoRunConfig = this.config.getAutoRunConfig ? await this.config.getAutoRunConfig() : options.autoRunConfig || this.config.autoRunConfig;
680
- console.log("[Orchestrator] \u68C0\u67E5\u5DE5\u5177\u6279\u51C6:", {
681
- toolName: toolCall.name,
682
- autoRunConfigMode: autoRunConfig?.mode,
683
- hasCallback: !!this.config.onToolApprovalRequest
684
- });
685
- if (autoRunConfig?.mode === "manual" && this.config.onToolApprovalRequest) {
686
- console.log("[Orchestrator] \u53D1\u9001\u5DE5\u5177\u6279\u51C6\u8BF7\u6C42:", toolCall.name);
687
- const approvalRequest = {
688
- type: "tool_approval_request",
689
- data: {
690
- id: toolCall.id,
691
- name: toolCall.name,
692
- args,
693
- requestedAt: Date.now()
694
- }
695
- };
696
- yield approvalRequest;
697
- const approved = await this.config.onToolApprovalRequest({
698
- id: toolCall.id,
699
- name: toolCall.name,
700
- args
701
- });
702
- if (!approved) {
703
- const result2 = JSON.stringify({ skipped: true, message: "\u7528\u6237\u8DF3\u8FC7\u4E86\u6B64\u5DE5\u5177" });
704
- yield createToolCallResult(toolCall.id, toolCall.name, result2, false, toolStartedAt);
705
- messages.push({
706
- role: "tool",
707
- content: result2,
708
- toolCallId: toolCall.id,
709
- toolName: toolCall.name
710
- });
711
- continue;
712
- }
713
- }
714
- yield createToolCallStart(toolCall.id, toolCall.name, args);
715
- let result;
716
- let dynamicSideEffects;
717
- let success = true;
718
- try {
719
- const executeResult = await this.config.executeTool(toolCall.name, args, context.signal);
720
- if (typeof executeResult === "string") {
721
- result = executeResult;
722
- } else {
723
- result = executeResult.result;
724
- dynamicSideEffects = executeResult.sideEffects;
725
- }
726
- } catch (error) {
727
- success = false;
728
- const errorMessage = error instanceof Error ? error.message : String(error);
729
- result = `\u9519\u8BEF: ${errorMessage}`;
730
- }
731
- const tool2 = this.config.tools?.get(toolCall.name);
732
- const sideEffects = dynamicSideEffects ?? tool2?.sideEffects;
733
- yield createToolCallResult(toolCall.id, toolCall.name, result, success, toolStartedAt, void 0, sideEffects);
734
- messages.push({
735
- role: "tool",
736
- content: result,
737
- toolCallId: toolCall.id,
738
- toolName: toolCall.name
739
- // Gemini 需要工具名称
740
- });
741
- if (context.signal.aborted) {
742
- yield createAbort("\u8BF7\u6C42\u5DF2\u53D6\u6D88");
743
- return;
744
- }
745
- }
746
- finalText = "";
747
- continue;
748
- }
749
- break;
750
- } catch (error) {
751
- if (context.signal.aborted) {
752
- yield createAbort("\u8BF7\u6C42\u5DF2\u53D6\u6D88");
753
- } else {
754
- yield createApiError(error instanceof Error ? error.message : String(error));
755
- }
756
- return;
757
- }
758
- }
759
- if (searchStarted) {
760
- yield createSearchEnd(true, searchStartedAt);
761
- }
762
- const duration = Date.now() - startedAt;
763
- yield createDone(finalText, void 0, duration);
764
- }
765
- /**
766
- * 构建标准化消息列表
767
- */
768
- buildMessages(context, message) {
769
- const messages = [];
770
- if (context.systemPrompt) {
771
- messages.push({
772
- role: "system",
773
- content: context.systemPrompt
774
- });
775
- }
776
- for (const msg of context.history) {
777
- const standardMsg = {
778
- role: msg.role,
779
- content: msg.content
780
- };
781
- if (msg.tool_calls) {
782
- standardMsg.toolCalls = msg.tool_calls.map((tc) => ({
783
- id: tc.id,
784
- name: tc.function.name,
785
- arguments: tc.function.arguments
786
- }));
787
- }
788
- messages.push(standardMsg);
789
- }
790
- messages.push({
791
- role: "user",
792
- content: message,
793
- images: context.images
794
- });
795
- return messages;
796
- }
797
- };
798
- function createOrchestrator(config) {
799
- return new ChatOrchestrator(config);
800
- }
801
-
802
- // src/providers/adapters/ark.ts
803
- var ARK_MODELS = [
804
- DOUBAO_SEED_1_8.id,
805
- DOUBAO_SEED_1_6.id,
806
- DOUBAO_SEED_1_6_LATEST.id,
807
- DOUBAO_SEED_1_6_FLASH.id,
808
- "doubao-1-5-pro-32k-250115",
809
- "doubao-1-5-pro-256k-250115",
810
- "doubao-1-5-lite-32k-250115"
811
- ];
812
- var ArkAdapter = class {
813
- name = "ark";
814
- supportedModels = ARK_MODELS;
815
- apiKey;
816
- apiUrl;
817
- constructor(config) {
818
- this.apiKey = config.apiKey;
819
- this.apiUrl = config.apiUrl ?? DEFAULT_ARK_URL;
820
- }
821
- supportsModel(model) {
822
- return this.supportedModels.some((m) => model.includes(m) || m.includes(model));
823
- }
824
- /**
825
- * 单次流式调用
826
- */
827
- async *streamOnce(messages, tools2, options) {
828
- const input = this.convertMessages(messages);
829
- const requestBody = {
830
- model: options.model,
831
- stream: true,
832
- max_output_tokens: 32768,
833
- input
834
- };
835
- const apiTools = [];
836
- if (options.enableSearch) {
837
- apiTools.push({ type: "web_search", max_keyword: 5, limit: 20 });
838
- }
839
- if (tools2.length > 0) {
840
- for (const t of tools2) {
841
- apiTools.push({
842
- type: "function",
843
- name: t.name,
844
- description: t.description,
845
- parameters: t.parameters
846
- });
847
- }
848
- }
849
- if (apiTools.length > 0) {
850
- requestBody.tools = apiTools;
851
- }
852
- if (options.enableThinking) {
853
- requestBody.thinking = { type: "enabled" };
854
- }
855
- const response = await fetch(`${this.apiUrl}/responses`, {
856
- method: "POST",
857
- headers: {
858
- "Authorization": `Bearer ${this.apiKey}`,
859
- "Content-Type": "application/json"
860
- },
861
- body: JSON.stringify(requestBody),
862
- signal: options.signal
863
- });
864
- if (!response.ok) {
865
- const errorText = await response.text();
866
- yield { type: "error", error: `ARK API \u9519\u8BEF: ${response.status} - ${errorText}` };
867
- return;
868
- }
869
- const reader = response.body?.getReader();
870
- if (!reader) {
871
- yield { type: "error", error: "\u65E0\u6CD5\u83B7\u53D6\u54CD\u5E94\u6D41" };
872
- return;
873
- }
874
- yield* this.parseStream(reader);
875
- }
876
- /**
877
- * 转换标准消息为 ARK Responses API 格式
878
- */
879
- convertMessages(messages) {
880
- const input = [];
881
- for (const msg of messages) {
882
- switch (msg.role) {
883
- case "system":
884
- input.push({ role: "system", content: msg.content });
885
- break;
886
- case "user": {
887
- const content = [{ type: "input_text", text: msg.content }];
888
- if (msg.images?.length) {
889
- for (const img of msg.images) {
890
- content.push({
891
- type: "input_image",
892
- image_url: img.startsWith("data:") ? img : `data:image/jpeg;base64,${img}`
893
- });
894
- }
895
- }
896
- input.push({ role: "user", content });
897
- break;
898
- }
899
- case "assistant":
900
- if (msg.toolCalls?.length) {
901
- for (const tc of msg.toolCalls) {
902
- input.push({
903
- type: "function_call",
904
- call_id: tc.id,
905
- name: tc.name,
906
- arguments: tc.arguments
907
- });
908
- }
909
- } else {
910
- input.push({
911
- role: "developer",
912
- content: `[\u4E0A\u4E00\u8F6EAI\u56DE\u590D]: ${msg.content}`
913
- });
914
- }
915
- break;
916
- case "tool":
917
- input.push({
918
- type: "function_call_output",
919
- call_id: msg.toolCallId,
920
- output: msg.content
921
- });
922
- break;
923
- }
924
- }
925
- return input;
926
- }
927
- /**
928
- * 解析 ARK 流式响应
929
- */
930
- async *parseStream(reader) {
931
- const decoder = new TextDecoder();
932
- let buffer = "";
933
- const pendingToolCalls = /* @__PURE__ */ new Map();
934
- let currentFunctionCallId = null;
935
- const searchResults = [];
936
- while (true) {
937
- const { done, value } = await reader.read();
938
- if (done) break;
939
- buffer += decoder.decode(value, { stream: true });
940
- const lines = buffer.split("\n");
941
- buffer = lines.pop() || "";
942
- for (const line of lines) {
943
- if (!line.startsWith("data:")) continue;
944
- const data = line.slice(5).trim();
945
- if (data === "[DONE]") {
946
- if (pendingToolCalls.size > 0) {
947
- for (const tc of pendingToolCalls.values()) {
948
- yield { type: "tool_call", toolCall: tc };
949
- }
950
- yield { type: "done", finishReason: "tool_calls" };
951
- } else {
952
- yield { type: "done", finishReason: "stop" };
953
- }
954
- return;
955
- }
956
- try {
957
- const event = JSON.parse(data);
958
- switch (event.type) {
959
- case "response.output_item.added": {
960
- const item = event.item;
961
- if (item?.type === "function_call") {
962
- const callId = item.call_id;
963
- if (!callId) {
964
- console.warn("[ARK] function_call \u7F3A\u5C11 call_id");
965
- break;
966
- }
967
- currentFunctionCallId = callId;
968
- pendingToolCalls.set(callId, {
969
- id: callId,
970
- name: item.name || "",
971
- arguments: item.arguments || ""
972
- });
973
- }
974
- break;
975
- }
976
- case "response.function_call_arguments.delta": {
977
- if (currentFunctionCallId) {
978
- const call = pendingToolCalls.get(currentFunctionCallId);
979
- if (call) {
980
- call.arguments += event.delta || "";
981
- }
982
- }
983
- break;
984
- }
985
- case "response.function_call_arguments.done":
986
- case "response.output_item.done": {
987
- const item = event.item;
988
- if (item?.type === "function_call" && item.call_id) {
989
- const callId = item.call_id;
990
- const existing = pendingToolCalls.get(callId);
991
- pendingToolCalls.set(callId, {
992
- id: callId,
993
- name: item.name || existing?.name || "",
994
- arguments: item.arguments || existing?.arguments || "{}"
995
- });
996
- }
997
- break;
998
- }
999
- case "response.output_text.annotation.added": {
1000
- const annotation = event.annotation;
1001
- const isUrlCitation = annotation?.type === "url_citation" || annotation?.type === "citation" || annotation?.url;
1002
- if (isUrlCitation && annotation?.url) {
1003
- const exists = searchResults.some((r) => r.url === annotation.url);
1004
- if (!exists) {
1005
- searchResults.push({
1006
- title: annotation.title || annotation.text || "",
1007
- url: annotation.url,
1008
- snippet: annotation.summary || annotation.snippet || ""
1009
- });
1010
- yield { type: "search_result", searchResults: [...searchResults] };
1011
- }
1012
- }
1013
- break;
1014
- }
1015
- case "response.output_text.delta":
1016
- if (event.delta) {
1017
- yield { type: "text", text: event.delta };
1018
- }
1019
- break;
1020
- case "response.reasoning_summary_text.delta":
1021
- if (event.delta) {
1022
- yield { type: "thinking", thinking: event.delta };
1023
- }
1024
- break;
1025
- case "response.reasoning_summary_text.done":
1026
- yield { type: "thinking_done" };
1027
- break;
1028
- }
1029
- } catch {
1030
- }
1031
- }
1032
- }
1033
- if (pendingToolCalls.size > 0) {
1034
- for (const tc of pendingToolCalls.values()) {
1035
- yield { type: "tool_call", toolCall: tc };
1036
- }
1037
- yield { type: "done", finishReason: "tool_calls" };
1038
- } else {
1039
- yield { type: "done", finishReason: "stop" };
1040
- }
1041
- }
1042
- };
1043
- function createArkAdapter(config) {
1044
- return new ArkAdapter(config);
1045
- }
1046
-
1047
- // src/providers/adapters/openrouter.ts
1048
- var OPENROUTER_MODELS = [
1049
- OR_GEMINI_2_5_FLASH.id,
1050
- OR_GEMINI_2_5_PRO.id,
1051
- OR_GEMINI_2_0_FLASH.id,
1052
- "anthropic/claude-sonnet-4",
1053
- "anthropic/claude-3.5-sonnet",
1054
- "openai/gpt-4o",
1055
- "openai/gpt-4o-mini",
1056
- OR_DEEPSEEK_R1.id,
1057
- OR_DEEPSEEK_CHAT_V3.id
1058
- ];
1059
- var OpenRouterAdapter = class {
1060
- name = "openrouter";
1061
- supportedModels = OPENROUTER_MODELS;
1062
- apiKey;
1063
- apiUrl;
1064
- constructor(config) {
1065
- this.apiKey = config.apiKey;
1066
- this.apiUrl = config.apiUrl ?? DEFAULT_OPENROUTER_URL;
1067
- }
1068
- supportsModel(model) {
1069
- return this.supportedModels.some((m) => model.includes(m) || m.includes(model));
1070
- }
1071
- /**
1072
- * 单次流式调用
1073
- */
1074
- async *streamOnce(messages, tools2, options) {
1075
- const apiMessages = this.convertMessages(messages);
1076
- const requestBody = {
1077
- model: options.model,
1078
- messages: apiMessages,
1079
- stream: true,
1080
- max_tokens: 32768
1081
- };
1082
- if (tools2.length > 0) {
1083
- requestBody.tools = tools2.map((t) => ({
1084
- type: "function",
1085
- function: {
1086
- name: t.name,
1087
- description: t.description,
1088
- parameters: t.parameters
1089
- }
1090
- }));
1091
- }
1092
- if (options.model.includes("gemini") && options.enableThinking) {
1093
- requestBody.reasoning = { effort: "high" };
1094
- }
1095
- if (options.model.includes("claude") && options.enableThinking) {
1096
- requestBody.reasoning = { effort: "high", exclude: false };
1097
- }
1098
- if (options.enableSearch) {
1099
- const isSmallContext = options.model.includes("deepseek") || options.model.includes("qwen");
1100
- const maxResults = isSmallContext ? 2 : 3;
1101
- requestBody.plugins = [{ id: "web", max_results: maxResults }];
1102
- }
1103
- const response = await fetch(`${this.apiUrl}/chat/completions`, {
1104
- method: "POST",
1105
- headers: {
1106
- "Authorization": `Bearer ${this.apiKey}`,
1107
- "Content-Type": "application/json",
1108
- "HTTP-Referer": "https://ai-chat.local",
1109
- "X-Title": "AI Chat"
1110
- },
1111
- body: JSON.stringify(requestBody),
1112
- signal: options.signal
1113
- });
1114
- if (!response.ok) {
1115
- const errorText = await response.text();
1116
- yield { type: "error", error: `OpenRouter API \u9519\u8BEF: ${response.status} - ${errorText}` };
1117
- return;
1118
- }
1119
- const reader = response.body?.getReader();
1120
- if (!reader) {
1121
- yield { type: "error", error: "\u65E0\u6CD5\u83B7\u53D6\u54CD\u5E94\u6D41" };
1122
- return;
1123
- }
1124
- yield* this.parseStream(reader);
1125
- }
1126
- /**
1127
- * 转换标准消息为 OpenAI 格式
1128
- */
1129
- convertMessages(messages) {
1130
- const apiMessages = [];
1131
- for (const msg of messages) {
1132
- switch (msg.role) {
1133
- case "system":
1134
- apiMessages.push({ role: "system", content: msg.content });
1135
- break;
1136
- case "user": {
1137
- if (msg.images?.length) {
1138
- const content = [{ type: "text", text: msg.content }];
1139
- for (const img of msg.images) {
1140
- content.push({
1141
- type: "image_url",
1142
- image_url: { url: img.startsWith("data:") ? img : `data:image/jpeg;base64,${img}` }
1143
- });
1144
- }
1145
- apiMessages.push({ role: "user", content });
1146
- } else {
1147
- apiMessages.push({ role: "user", content: msg.content });
1148
- }
1149
- break;
1150
- }
1151
- case "assistant":
1152
- if (msg.toolCalls?.length) {
1153
- apiMessages.push({
1154
- role: "assistant",
1155
- content: msg.content || null,
1156
- tool_calls: msg.toolCalls.map((tc) => ({
1157
- id: tc.id,
1158
- type: "function",
1159
- function: {
1160
- name: tc.name,
1161
- arguments: tc.arguments
1162
- }
1163
- }))
1164
- });
1165
- } else {
1166
- apiMessages.push({ role: "assistant", content: msg.content });
1167
- }
1168
- break;
1169
- case "tool":
1170
- apiMessages.push({
1171
- role: "tool",
1172
- tool_call_id: msg.toolCallId,
1173
- content: msg.content
1174
- });
1175
- break;
1176
- }
1177
- }
1178
- return apiMessages;
1179
- }
1180
- /**
1181
- * 解析 OpenAI 兼容流式响应
1182
- */
1183
- async *parseStream(reader) {
1184
- const decoder = new TextDecoder();
1185
- let buffer = "";
1186
- const toolCallsMap = /* @__PURE__ */ new Map();
1187
- let toolCallCounter = 0;
1188
- let finishReason = null;
1189
- const searchResults = [];
1190
- let hasThinking = false;
1191
- let thinkingDone = false;
1192
- while (true) {
1193
- const { done, value } = await reader.read();
1194
- if (done) break;
1195
- buffer += decoder.decode(value, { stream: true });
1196
- const lines = buffer.split("\n");
1197
- buffer = lines.pop() || "";
1198
- for (const line of lines) {
1199
- if (!line.startsWith("data:")) continue;
1200
- const data = line.slice(5).trim();
1201
- if (data === "[DONE]") {
1202
- if (searchResults.length > 0) {
1203
- yield { type: "search_result", searchResults: [...searchResults] };
1204
- }
1205
- if (toolCallsMap.size > 0) {
1206
- for (const tc of toolCallsMap.values()) {
1207
- if (tc.name) {
1208
- yield { type: "tool_call", toolCall: tc };
1209
- }
1210
- }
1211
- yield { type: "done", finishReason: "tool_calls" };
1212
- } else {
1213
- yield { type: "done", finishReason: "stop" };
1214
- }
1215
- return;
1216
- }
1217
- try {
1218
- const json = JSON.parse(data);
1219
- const choice = json.choices?.[0];
1220
- const annotations = json.annotations;
1221
- if (annotations?.length) {
1222
- for (const annotation of annotations) {
1223
- if (annotation.type === "url_citation" && annotation.url_citation?.url) {
1224
- const exists = searchResults.some((r) => r.url === annotation.url_citation.url);
1225
- if (!exists) {
1226
- searchResults.push({
1227
- url: annotation.url_citation.url,
1228
- title: annotation.url_citation.title || "",
1229
- snippet: annotation.url_citation.content || ""
1230
- });
1231
- yield { type: "search_result", searchResults: [...searchResults] };
1232
- }
1233
- }
1234
- }
1235
- }
1236
- if (!choice) continue;
1237
- const delta = choice.delta || {};
1238
- finishReason = choice.finish_reason;
1239
- if (delta.reasoning) {
1240
- hasThinking = true;
1241
- yield { type: "thinking", thinking: delta.reasoning };
1242
- }
1243
- if (delta.content) {
1244
- if (hasThinking && !thinkingDone) {
1245
- thinkingDone = true;
1246
- yield { type: "thinking_done" };
1247
- }
1248
- yield { type: "text", text: delta.content };
1249
- }
1250
- if (delta.tool_calls?.length) {
1251
- for (const tc of delta.tool_calls) {
1252
- const index = tc.index;
1253
- const existing = toolCallsMap.get(index);
1254
- if (existing) {
1255
- if (tc.function?.arguments) {
1256
- existing.arguments += tc.function.arguments;
1257
- }
1258
- if (tc.function?.name) {
1259
- existing.name = tc.function.name;
1260
- }
1261
- if (tc.id) {
1262
- existing.id = tc.id;
1263
- }
1264
- } else {
1265
- toolCallsMap.set(index, {
1266
- id: tc.id || `call_${Date.now()}_${toolCallCounter++}`,
1267
- name: tc.function?.name || "",
1268
- arguments: tc.function?.arguments || ""
1269
- });
1270
- }
1271
- }
1272
- }
1273
- if (finishReason === "tool_calls") {
1274
- for (const tc of toolCallsMap.values()) {
1275
- if (tc.name) {
1276
- yield { type: "tool_call", toolCall: tc };
1277
- }
1278
- }
1279
- yield { type: "done", finishReason: "tool_calls" };
1280
- return;
1281
- }
1282
- } catch {
1283
- }
1284
- }
1285
- }
1286
- if (searchResults.length > 0) {
1287
- yield { type: "search_result", searchResults: [...searchResults] };
1288
- }
1289
- if (toolCallsMap.size > 0) {
1290
- for (const tc of toolCallsMap.values()) {
1291
- if (tc.name) {
1292
- yield { type: "tool_call", toolCall: tc };
1293
- }
1294
- }
1295
- yield { type: "done", finishReason: "tool_calls" };
1296
- } else {
1297
- yield { type: "done", finishReason: "stop" };
1298
- }
1299
- }
1300
- };
1301
- function createOpenRouterAdapter(config) {
1302
- return new OpenRouterAdapter(config);
1303
- }
1304
-
1305
- // src/providers/adapters/qwen.ts
1306
- var QWEN_MODELS = [
1307
- QWEN_MAX.id,
1308
- QWEN_MAX_LATEST.id,
1309
- QWEN_PLUS.id,
1310
- QWEN_PLUS_LATEST.id,
1311
- QWEN_TURBO.id,
1312
- QWEN_TURBO_LATEST.id,
1313
- QWEN3_235B.id
1314
- ];
1315
- var QwenAdapter = class {
1316
- name = "qwen";
1317
- supportedModels = QWEN_MODELS;
1318
- apiKey;
1319
- apiUrl;
1320
- constructor(config) {
1321
- this.apiKey = config.apiKey;
1322
- this.apiUrl = config.apiUrl ?? DEFAULT_QWEN_URL;
1323
- }
1324
- supportsModel(model) {
1325
- return this.supportedModels.some((m) => model.includes(m) || m.includes(model));
1326
- }
1327
- /**
1328
- * 单次流式调用
1329
- */
1330
- async *streamOnce(messages, tools2, options) {
1331
- const apiMessages = this.convertMessages(messages);
1332
- const requestBody = {
1333
- model: options.model,
1334
- input: { messages: apiMessages },
1335
- parameters: {
1336
- result_format: "message",
1337
- incremental_output: true,
1338
- enable_search: options.enableSearch,
1339
- // 深度思考支持
1340
- ...options.enableThinking && {
1341
- enable_thinking: true,
1342
- thinking_budget: 38400
1343
- }
1344
- }
1345
- };
1346
- if (tools2.length > 0) {
1347
- requestBody.parameters = {
1348
- ...requestBody.parameters,
1349
- tools: tools2.map((t) => ({
1350
- type: "function",
1351
- function: {
1352
- name: t.name,
1353
- description: t.description,
1354
- parameters: t.parameters
1355
- }
1356
- }))
1357
- };
1358
- }
1359
- const response = await fetch(`${this.apiUrl}/services/aigc/text-generation/generation`, {
1360
- method: "POST",
1361
- headers: {
1362
- "Authorization": `Bearer ${this.apiKey}`,
1363
- "Content-Type": "application/json",
1364
- "X-DashScope-SSE": "enable"
1365
- },
1366
- body: JSON.stringify(requestBody),
1367
- signal: options.signal
1368
- });
1369
- if (!response.ok) {
1370
- const errorText = await response.text();
1371
- yield { type: "error", error: `Qwen API \u9519\u8BEF: ${response.status} - ${errorText}` };
1372
- return;
1373
- }
1374
- const reader = response.body?.getReader();
1375
- if (!reader) {
1376
- yield { type: "error", error: "\u65E0\u6CD5\u83B7\u53D6\u54CD\u5E94\u6D41" };
1377
- return;
1378
- }
1379
- yield* this.parseStream(reader);
1380
- }
1381
- /**
1382
- * 转换标准消息为 Qwen 格式
1383
- */
1384
- convertMessages(messages) {
1385
- const apiMessages = [];
1386
- for (const msg of messages) {
1387
- switch (msg.role) {
1388
- case "system":
1389
- apiMessages.push({ role: "system", content: msg.content });
1390
- break;
1391
- case "user": {
1392
- if (msg.images?.length) {
1393
- const content = [{ type: "text", text: msg.content }];
1394
- for (const img of msg.images) {
1395
- content.push({
1396
- type: "image_url",
1397
- image_url: { url: img.startsWith("data:") ? img : `data:image/jpeg;base64,${img}` }
1398
- });
1399
- }
1400
- apiMessages.push({ role: "user", content });
1401
- } else {
1402
- apiMessages.push({ role: "user", content: msg.content });
1403
- }
1404
- break;
1405
- }
1406
- case "assistant":
1407
- if (msg.toolCalls?.length) {
1408
- apiMessages.push({
1409
- role: "assistant",
1410
- content: msg.content || "",
1411
- tool_calls: msg.toolCalls.map((tc) => ({
1412
- id: tc.id,
1413
- type: "function",
1414
- function: {
1415
- name: tc.name,
1416
- arguments: tc.arguments
1417
- }
1418
- }))
1419
- });
1420
- } else {
1421
- apiMessages.push({ role: "assistant", content: msg.content });
1422
- }
1423
- break;
1424
- case "tool":
1425
- apiMessages.push({
1426
- role: "tool",
1427
- tool_call_id: msg.toolCallId,
1428
- content: msg.content
1429
- });
1430
- break;
1431
- }
1432
- }
1433
- return apiMessages;
1434
- }
1435
- /**
1436
- * 解析 Qwen 流式响应
1437
- */
1438
- async *parseStream(reader) {
1439
- const decoder = new TextDecoder();
1440
- let buffer = "";
1441
- const toolCallsMap = /* @__PURE__ */ new Map();
1442
- let toolCallCounter = 0;
1443
- const searchResults = [];
1444
- let hasThinking = false;
1445
- let thinkingDone = false;
1446
- while (true) {
1447
- const { done, value } = await reader.read();
1448
- if (done) break;
1449
- buffer += decoder.decode(value, { stream: true });
1450
- const lines = buffer.split("\n");
1451
- buffer = lines.pop() || "";
1452
- for (const line of lines) {
1453
- if (!line.startsWith("data:")) continue;
1454
- const data = line.slice(5).trim();
1455
- if (!data || data === "[DONE]") continue;
1456
- try {
1457
- const json = JSON.parse(data);
1458
- const output = json.output;
1459
- if (!output) continue;
1460
- const choice = output.choices?.[0];
1461
- const message = choice?.message || {};
1462
- const finishReason = choice?.finish_reason;
1463
- if (message.reasoning_content) {
1464
- hasThinking = true;
1465
- yield { type: "thinking", thinking: message.reasoning_content };
1466
- }
1467
- if (message.content) {
1468
- if (!thinkingDone && hasThinking) {
1469
- thinkingDone = true;
1470
- yield { type: "thinking_done" };
1471
- }
1472
- yield { type: "text", text: message.content };
1473
- }
1474
- const searchInfo = json.output?.search_info;
1475
- if (searchInfo?.search_results?.length) {
1476
- for (const result of searchInfo.search_results) {
1477
- const exists = searchResults.some((r) => r.url === result.url);
1478
- if (!exists) {
1479
- searchResults.push({
1480
- title: result.title || "",
1481
- url: result.url || "",
1482
- snippet: result.snippet || ""
1483
- });
1484
- }
1485
- }
1486
- yield { type: "search_result", searchResults: [...searchResults] };
1487
- }
1488
- if (message.tool_calls?.length) {
1489
- for (const tc of message.tool_calls) {
1490
- const index = toolCallsMap.size;
1491
- const existing = toolCallsMap.get(index);
1492
- if (existing) {
1493
- if (tc.function?.arguments) {
1494
- existing.arguments += tc.function.arguments;
1495
- }
1496
- } else {
1497
- toolCallsMap.set(index, {
1498
- id: tc.id || `call_${Date.now()}_${toolCallCounter++}`,
1499
- name: tc.function.name,
1500
- arguments: tc.function.arguments
1501
- });
1502
- }
1503
- }
1504
- }
1505
- if (finishReason === "stop" || finishReason === "length" || finishReason === "tool_calls") {
1506
- if (toolCallsMap.size > 0) {
1507
- for (const tc of toolCallsMap.values()) {
1508
- yield { type: "tool_call", toolCall: tc };
1509
- }
1510
- yield { type: "done", finishReason: "tool_calls" };
1511
- } else {
1512
- yield { type: "done", finishReason: "stop" };
1513
- }
1514
- return;
1515
- }
1516
- } catch {
1517
- }
1518
- }
1519
- }
1520
- if (toolCallsMap.size > 0) {
1521
- for (const tc of toolCallsMap.values()) {
1522
- yield { type: "tool_call", toolCall: tc };
1523
- }
1524
- yield { type: "done", finishReason: "tool_calls" };
1525
- } else {
1526
- yield { type: "done", finishReason: "stop" };
1527
- }
1528
- }
1529
- };
1530
- function createQwenAdapter(config) {
1531
- return new QwenAdapter(config);
1532
- }
1533
-
1534
- // src/providers/adapters/gemini.ts
1535
- var GEMINI_MODELS = [
1536
- GEMINI_2_5_FLASH.id,
1537
- GEMINI_2_5_PRO.id,
1538
- GEMINI_2_0_FLASH.id,
1539
- GEMINI_2_0_FLASH_LITE.id,
1540
- GEMINI_3_PRO.id
1541
- ];
1542
- var GeminiAdapter = class {
1543
- name = "gemini";
1544
- supportedModels = GEMINI_MODELS;
1545
- client = null;
1546
- apiKey;
1547
- constructor(config) {
1548
- this.apiKey = config.apiKey;
1549
- }
1550
- /** 懒加载客户端 */
1551
- async getClient() {
1552
- if (!this.client) {
1553
- const { GoogleGenAI: GoogleGenAI2 } = await import('@google/genai');
1554
- this.client = new GoogleGenAI2({ apiKey: this.apiKey });
1555
- }
1556
- return this.client;
1557
- }
1558
- supportsModel(model) {
1559
- return this.supportedModels.some((m) => model.includes(m) || m.includes(model)) || model.startsWith("gemini-");
1560
- }
1561
- /**
1562
- * 单次流式调用
1563
- */
1564
- async *streamOnce(messages, tools2, options) {
1565
- const client = await this.getClient();
1566
- const geminiMessages = this.convertMessages(messages);
1567
- const generateConfig = {};
1568
- const toolsConfig = [];
1569
- if (tools2.length > 0) {
1570
- toolsConfig.push({
1571
- functionDeclarations: tools2.map((t) => ({
1572
- name: t.name,
1573
- description: t.description,
1574
- parameters: t.parameters
1575
- }))
1576
- });
1577
- }
1578
- if (options.enableSearch) {
1579
- toolsConfig.push({ googleSearch: {} });
1580
- }
1581
- if (toolsConfig.length > 0) {
1582
- generateConfig.tools = toolsConfig;
1583
- }
1584
- if (options.enableThinking) {
1585
- generateConfig.thinkingConfig = {
1586
- thinkingLevel: "high",
1587
- includeThoughts: true
1588
- };
1589
- }
1590
- try {
1591
- const streamResponse = await client.models.generateContentStream({
1592
- model: options.model,
1593
- contents: geminiMessages,
1594
- config: generateConfig
1595
- });
1596
- const pendingToolCalls = [];
1597
- let toolCallCounter = 0;
1598
- let hasThinking = false;
1599
- let thinkingDone = false;
1600
- for await (const chunk of streamResponse) {
1601
- if (options.signal.aborted) {
1602
- yield { type: "error", error: "\u8BF7\u6C42\u5DF2\u53D6\u6D88" };
1603
- return;
1604
- }
1605
- const candidate = chunk.candidates?.[0];
1606
- if (!candidate) continue;
1607
- const parts = candidate.content?.parts || [];
1608
- for (const part of parts) {
1609
- if (part.thought && part.text) {
1610
- hasThinking = true;
1611
- yield { type: "thinking", thinking: part.text };
1612
- continue;
1613
- }
1614
- if (part.text && !part.thought) {
1615
- if (hasThinking && !thinkingDone) {
1616
- thinkingDone = true;
1617
- yield { type: "thinking_done" };
1618
- }
1619
- yield { type: "text", text: part.text };
1620
- }
1621
- if (part.functionCall && part.functionCall.name) {
1622
- pendingToolCalls.push({
1623
- id: `call_${Date.now()}_${toolCallCounter++}`,
1624
- name: part.functionCall.name,
1625
- arguments: JSON.stringify(part.functionCall.args || {})
1626
- });
1627
- }
1628
- }
1629
- }
1630
- if (pendingToolCalls.length > 0) {
1631
- for (const tc of pendingToolCalls) {
1632
- yield { type: "tool_call", toolCall: tc };
1633
- }
1634
- yield { type: "done", finishReason: "tool_calls" };
1635
- } else {
1636
- yield { type: "done", finishReason: "stop" };
1637
- }
1638
- } catch (error) {
1639
- const errorStr = String(error);
1640
- if (errorStr.includes("User location is not supported") || errorStr.includes("FAILED_PRECONDITION")) {
1641
- yield {
1642
- type: "error",
1643
- error: "Gemini API \u4E0D\u652F\u6301\u5F53\u524D\u5730\u533A\uFF08\u4E2D\u56FD\u5927\u9646\u65E0\u6CD5\u8BBF\u95EE\uFF09\u3002\u8BF7\u4F7F\u7528\u5176\u4ED6\u6A21\u578B\u3002"
1644
- };
1645
- } else {
1646
- yield { type: "error", error: errorStr };
1647
- }
1648
- }
1649
- }
1650
- /**
1651
- * 转换标准消息为 Gemini 格式
1652
- */
1653
- convertMessages(messages) {
1654
- const geminiMessages = [];
1655
- for (const msg of messages) {
1656
- switch (msg.role) {
1657
- case "system":
1658
- geminiMessages.push(
1659
- { role: "user", parts: [{ text: msg.content }] },
1660
- { role: "model", parts: [{ text: "OK" }] }
1661
- );
1662
- break;
1663
- case "user": {
1664
- const parts = [{ text: msg.content }];
1665
- if (msg.images?.length) {
1666
- for (const img of msg.images) {
1667
- const base64 = img.startsWith("data:") ? img.split(",")[1] : img;
1668
- parts.push({
1669
- inlineData: {
1670
- mimeType: "image/jpeg",
1671
- data: base64
1672
- }
1673
- });
1674
- }
1675
- }
1676
- geminiMessages.push({ role: "user", parts });
1677
- break;
1678
- }
1679
- case "assistant":
1680
- if (msg.toolCalls?.length) {
1681
- const parts = [];
1682
- if (msg.content) {
1683
- parts.push({ text: msg.content });
1684
- }
1685
- for (const tc of msg.toolCalls) {
1686
- parts.push({
1687
- functionCall: {
1688
- name: tc.name,
1689
- args: JSON.parse(tc.arguments || "{}")
1690
- }
1691
- });
1692
- }
1693
- geminiMessages.push({ role: "model", parts });
1694
- } else {
1695
- geminiMessages.push({
1696
- role: "model",
1697
- parts: [{ text: msg.content }]
1698
- });
1699
- }
1700
- break;
1701
- case "tool":
1702
- geminiMessages.push({
1703
- role: "user",
1704
- parts: [{
1705
- functionResponse: {
1706
- name: msg.toolName || "unknown_tool",
1707
- response: { result: msg.content }
1708
- }
1709
- }]
1710
- });
1711
- break;
1712
- }
1713
- }
1714
- return geminiMessages;
1715
- }
1716
- };
1717
- function createGeminiAdapter(config) {
1718
- return new GeminiAdapter(config);
1719
- }
1720
-
1721
- // src/agent.ts
1722
- var HybridAgent = class {
1723
- config;
1724
- adapters = /* @__PURE__ */ new Map();
1725
- orchestrator;
1726
- geminiClient;
1727
- toolExecutor;
1728
- abortController = null;
1729
- /** 已注册的工具 */
1730
- tools = /* @__PURE__ */ new Map();
1731
- /** 工具配置(用于异步初始化) */
1732
- toolConfig;
1733
- constructor(config, toolExecutor) {
1734
- this.config = {
1735
- arkApiKey: config.arkApiKey,
1736
- arkApiUrl: config.arkApiUrl || DEFAULT_ARK_URL,
1737
- qwenApiKey: config.qwenApiKey || "",
1738
- qwenApiUrl: config.qwenApiUrl || DEFAULT_QWEN_NATIVE_URL,
1739
- openrouterApiKey: config.openrouterApiKey || "",
1740
- openrouterApiUrl: config.openrouterApiUrl || DEFAULT_OPENROUTER_URL,
1741
- geminiApiKey: config.geminiApiKey,
1742
- cwd: config.cwd || process.cwd(),
1743
- planPrompt: config.planPrompt || PLAN_MODE_PROMPT
1744
- };
1745
- this.geminiClient = new GoogleGenAI({ apiKey: this.config.geminiApiKey });
1746
- this.toolExecutor = toolExecutor || createDefaultToolExecutor(this.config.cwd);
1747
- this.toolConfig = config.tools;
1748
- this.tools = /* @__PURE__ */ new Map();
1749
- this.initializeAdapters();
1750
- this.orchestrator = new ChatOrchestrator({
1751
- maxIterations: 10,
1752
- executeTool: this.executeTool.bind(this),
1753
- tools: this.tools,
1754
- // 传入工具列表,用于获取 sideEffects
1755
- // autoRunConfig 在每次 chat 调用时通过 options 传递
1756
- onToolApprovalRequest: config.onToolApprovalRequest,
1757
- // 传递工具批准回调
1758
- getAutoRunConfig: config.getAutoRunConfig
1759
- // 传递动态获取配置回调
1760
- });
1761
- }
1762
- /** 异步初始化工具(在第一次 chat 前调用) */
1763
- async asyncInit() {
1764
- if (this.toolConfig && this.tools.size === 0) {
1765
- const resolvedTools = await resolveTools(this.toolConfig);
1766
- for (const tool2 of resolvedTools) {
1767
- this.tools.set(tool2.name, tool2);
1768
- }
1769
- }
1770
- }
1771
- /** 初始化所有 Adapter */
1772
- initializeAdapters() {
1773
- if (this.config.arkApiKey) {
1774
- this.adapters.set("ark", new ArkAdapter({
1775
- apiKey: this.config.arkApiKey,
1776
- apiUrl: this.config.arkApiUrl
1777
- }));
1778
- }
1779
- if (this.config.qwenApiKey) {
1780
- this.adapters.set("qwen", new QwenAdapter({
1781
- apiKey: this.config.qwenApiKey,
1782
- apiUrl: this.config.qwenApiUrl
1783
- }));
1784
- }
1785
- if (this.config.geminiApiKey) {
1786
- this.adapters.set("gemini", new GeminiAdapter({
1787
- apiKey: this.config.geminiApiKey
1788
- }));
1789
- }
1790
- if (this.config.openrouterApiKey) {
1791
- this.adapters.set("openrouter", new OpenRouterAdapter({
1792
- apiKey: this.config.openrouterApiKey,
1793
- apiUrl: this.config.openrouterApiUrl
1794
- }));
1795
- }
1796
- }
1797
- /**
1798
- * 判断模型提供商
1799
- */
1800
- getModelProvider(model) {
1801
- return routeModelToProvider(model);
1802
- }
1803
- /** 获取 Adapter */
1804
- getAdapter(model) {
1805
- const providerName = this.getModelProvider(model);
1806
- return this.adapters.get(providerName);
1807
- }
1808
- /**
1809
- * 调试:获取模型路由信息
1810
- */
1811
- getModelRouteInfo(model) {
1812
- const result = routeModelWithDetails(model);
1813
- return {
1814
- ...result,
1815
- available: this.adapters.has(result.provider)
1816
- };
1817
- }
1818
- /** 构建系统提示词 */
1819
- buildSystemPrompt(options) {
1820
- if (options.mode === "plan") {
1821
- return this.config.planPrompt;
1822
- }
1823
- return AGENT_MODE_PROMPT;
1824
- }
1825
- /** 获取默认深度思考模式 */
1826
- getDefaultThinkingMode() {
1827
- return "disabled";
1828
- }
1829
- /** 创建工具执行上下文 */
1830
- createToolContext(signal) {
1831
- return {
1832
- cwd: this.config.cwd,
1833
- geminiClient: this.geminiClient,
1834
- executeCommand: (command, cwd) => this.toolExecutor.executeCommand(command, cwd || this.config.cwd, signal),
1835
- signal
1836
- };
1837
- }
1838
- /** 执行工具 */
1839
- async executeTool(name, args, signal) {
1840
- const tool2 = this.tools.get(name);
1841
- if (!tool2) {
1842
- return `\u672A\u77E5\u5DE5\u5177: ${name}`;
1843
- }
1844
- return await tool2.execute(args, this.createToolContext(signal));
1845
- }
1846
- /** 获取工具定义列表 */
1847
- getToolDefinitions() {
1848
- return Array.from(this.tools.values()).map((tool2) => ({
1849
- name: tool2.name,
1850
- description: tool2.description,
1851
- parameters: tool2.parameters
1852
- }));
1853
- }
1854
- /**
1855
- * 聊天入口
1856
- *
1857
- * 使用 ChatOrchestrator 统一处理所有 Provider 的工具调用循环
1858
- */
1859
- async *chat(message, options = {}, images) {
1860
- await this.asyncInit();
1861
- this.abortController = new AbortController();
1862
- const signal = this.abortController.signal;
1863
- const model = options.model || DEFAULT_MODEL;
1864
- const adapter = this.getAdapter(model);
1865
- if (!adapter) {
1866
- const providerName = this.getModelProvider(model);
1867
- yield createApiError(`\u7F3A\u5C11 ${providerName} Provider \u7684 API Key`, { code: "MISSING_API_KEY" });
1868
- return;
1869
- }
1870
- const thinkingMode = options.thinkingMode ?? this.getDefaultThinkingMode();
1871
- const systemPrompt = this.buildSystemPrompt(options);
1872
- const tools2 = options.mode === "ask" ? [] : this.getToolDefinitions();
1873
- const history = options.history || [];
1874
- const context = {
1875
- systemPrompt,
1876
- history,
1877
- tools: tools2,
1878
- signal,
1879
- images
1880
- };
1881
- try {
1882
- yield* this.orchestrator.chat(adapter, message, context, {
1883
- model,
1884
- enableThinking: thinkingMode === "enabled",
1885
- enableSearch: options.enableWebSearch,
1886
- autoRunConfig: options.autoRunConfig
1887
- });
1888
- } finally {
1889
- this.abortController = null;
1890
- }
1891
- }
1892
- /** 中断当前请求 */
1893
- abort() {
1894
- if (this.abortController) {
1895
- this.abortController.abort();
1896
- }
1897
- }
1898
- /** 设置当前工作目录 */
1899
- setCwd(dir) {
1900
- this.config.cwd = dir;
1901
- }
1902
- /** 获取当前配置 */
1903
- getConfig() {
1904
- return { ...this.config };
1905
- }
1906
- /** 获取所有支持的模型 */
1907
- getSupportedModels() {
1908
- return MODELS;
1909
- }
1910
- };
1911
-
1912
- // src/builtin-tools/get-cwd.ts
1913
- function getCwdTool() {
1914
- return tool({
1915
- name: "get_cwd",
1916
- description: "\u83B7\u53D6\u7528\u6237\u5F53\u524D\u5DE5\u4F5C\u76EE\u5F55\u7684\u7EDD\u5BF9\u8DEF\u5F84\u3002\u5F53\u4F60\u9700\u8981\u77E5\u9053\u7528\u6237\u5728\u54EA\u4E2A\u76EE\u5F55\u4E0B\u5DE5\u4F5C\u65F6\u4F7F\u7528\u3002",
1917
- parameters: {
1918
- type: "object",
1919
- properties: {},
1920
- required: []
1921
- },
1922
- execute: async (_args, context) => {
1923
- return JSON.stringify({ cwd: context.cwd });
1924
- }
1925
- });
1926
- }
1927
-
1928
- // src/builtin-tools/get-platform.ts
1929
- function getPlatformTool() {
1930
- return tool({
1931
- name: "get_platform",
1932
- description: "\u83B7\u53D6\u64CD\u4F5C\u7CFB\u7EDF\u4FE1\u606F\uFF08macOS/Windows/Linux\uFF09\u3002\u5F53\u4F60\u9700\u8981\u786E\u5B9A\u4F7F\u7528\u54EA\u79CD\u547D\u4EE4\u8BED\u6CD5\u65F6\u8C03\u7528\uFF08\u5982 ls vs dir\uFF09\u3002",
1933
- parameters: {
1934
- type: "object",
1935
- properties: {},
1936
- required: []
1937
- },
1938
- execute: async () => {
1939
- const os = await import('os');
1940
- const platform = os.default.platform();
1941
- const platformName = platform === "darwin" ? "macOS" : platform === "win32" ? "Windows" : "Linux";
1942
- const shell = platform === "win32" ? "powershell" : "bash/zsh";
1943
- const pathSeparator = platform === "win32" ? "\\" : "/";
1944
- return JSON.stringify({
1945
- platform,
1946
- platformName,
1947
- shell,
1948
- pathSeparator
1949
- });
1950
- }
1951
- });
1952
- }
1953
-
1954
- // src/builtin-tools/execute-command.ts
1955
- function executeCommandTool() {
1956
- return tool({
1957
- name: "execute_command",
1958
- description: "\u6267\u884C shell \u547D\u4EE4\u3002\u6267\u884C\u524D\u5EFA\u8BAE\u5148\u7528 get_cwd \u83B7\u53D6\u5F53\u524D\u76EE\u5F55\u3001get_platform \u83B7\u53D6\u7CFB\u7EDF\u7C7B\u578B\uFF0C\u4EE5\u786E\u4FDD\u4F7F\u7528\u6B63\u786E\u7684\u8DEF\u5F84\u548C\u547D\u4EE4\u8BED\u6CD5\u3002",
1959
- parameters: {
1960
- type: "object",
1961
- properties: {
1962
- command: { type: "string", description: "\u8981\u6267\u884C\u7684\u547D\u4EE4" },
1963
- cwd: { type: "string", description: "\u6267\u884C\u547D\u4EE4\u7684\u76EE\u5F55\uFF08\u4E0D\u4F20\u5219\u4F7F\u7528\u7528\u6237\u5F53\u524D\u5DE5\u4F5C\u76EE\u5F55\uFF09" }
1964
- },
1965
- required: ["command"]
1966
- },
1967
- execute: async (args, context) => {
1968
- const cwd = args.cwd || context.cwd;
1969
- const result = await context.executeCommand(args.command, cwd);
1970
- if (result.success) {
1971
- return result.output || "\u6267\u884C\u6210\u529F";
1972
- }
1973
- if (result.error === "\u64CD\u4F5C\u5DF2\u53D6\u6D88") {
1974
- return JSON.stringify({ success: false, error: "\u64CD\u4F5C\u5DF2\u53D6\u6D88", cancelled: true });
1975
- }
1976
- return `\u9519\u8BEF: ${result.error}`;
1977
- }
1978
- });
1979
- }
1980
-
1981
- // src/gemini.ts
1982
- function getMimeType(ext) {
1983
- const types = {
1984
- ".jpg": "image/jpeg",
1985
- ".jpeg": "image/jpeg",
1986
- ".png": "image/png",
1987
- ".gif": "image/gif",
1988
- ".webp": "image/webp"
1989
- };
1990
- return types[ext] || "image/jpeg";
1991
- }
1992
- function getVideoMimeType(ext) {
1993
- const types = {
1994
- ".mp4": "video/mp4",
1995
- ".mov": "video/quicktime",
1996
- ".avi": "video/x-msvideo",
1997
- ".webm": "video/webm"
1998
- };
1999
- return types[ext] || "video/mp4";
2000
- }
2001
- async function analyzeImage(geminiClient, imagePath, question, signal) {
2002
- try {
2003
- if (signal?.aborted) {
2004
- return JSON.stringify({ success: false, error: "\u64CD\u4F5C\u5DF2\u53D6\u6D88", cancelled: true });
2005
- }
2006
- const fs = await import('fs/promises');
2007
- const path = await import('path');
2008
- const imageData = await fs.readFile(imagePath);
2009
- const base64 = imageData.toString("base64");
2010
- const ext = path.extname(imagePath).toLowerCase();
2011
- const mimeType = getMimeType(ext);
2012
- const response = await geminiClient.models.generateContent({
2013
- model: GEMINI_IMAGE_MODEL,
2014
- contents: [{
2015
- role: "user",
2016
- parts: [
2017
- { text: question || "\u8BF7\u8BE6\u7EC6\u63CF\u8FF0\u8FD9\u5F20\u56FE\u7247\u7684\u5185\u5BB9" },
2018
- { inlineData: { mimeType, data: base64 } }
2019
- ]
2020
- }],
2021
- config: { abortSignal: signal }
2022
- });
2023
- return response.text || "\u65E0\u6CD5\u5206\u6790\u56FE\u7247";
2024
- } catch (error) {
2025
- if (signal?.aborted) return JSON.stringify({ success: false, error: "\u64CD\u4F5C\u5DF2\u53D6\u6D88", cancelled: true });
2026
- return `\u56FE\u7247\u5206\u6790\u5931\u8D25: ${error}`;
2027
- }
2028
- }
2029
- async function generateImage(geminiClient, cwd, prompt, outputPath, signal) {
2030
- try {
2031
- if (signal?.aborted) {
2032
- return JSON.stringify({ success: false, error: "\u64CD\u4F5C\u5DF2\u53D6\u6D88" });
2033
- }
2034
- const response = await geminiClient.models.generateContent({
2035
- model: GEMINI_IMAGE_GEN_MODEL,
2036
- contents: [{ role: "user", parts: [{ text: prompt }] }],
2037
- config: { abortSignal: signal }
2038
- });
2039
- for (const part of response.candidates?.[0]?.content?.parts || []) {
2040
- if (part.inlineData) {
2041
- const imageData = Buffer.from(part.inlineData.data, "base64");
2042
- const mimeType = part.inlineData.mimeType || "image/png";
2043
- const ext = mimeType === "image/jpeg" ? ".jpg" : mimeType === "image/webp" ? ".webp" : ".png";
2044
- const fs = await import('fs/promises');
2045
- const savePath = outputPath || `${cwd}/generated_${Date.now()}${ext}`;
2046
- await fs.writeFile(savePath, imageData);
2047
- return JSON.stringify({
2048
- success: true,
2049
- path: savePath,
2050
- format: mimeType
2051
- });
2052
- }
2053
- }
2054
- return JSON.stringify({ success: false, error: "\u56FE\u7247\u751F\u6210\u5931\u8D25" });
2055
- } catch (error) {
2056
- if (signal?.aborted) {
2057
- return JSON.stringify({ success: false, error: "\u64CD\u4F5C\u5DF2\u53D6\u6D88", cancelled: true });
2058
- }
2059
- return JSON.stringify({ success: false, error: `\u56FE\u7247\u751F\u6210\u5931\u8D25: ${error}` });
2060
- }
2061
- }
2062
- async function analyzeVideo(geminiClient, videoPath, question, signal) {
2063
- try {
2064
- if (signal?.aborted) {
2065
- return JSON.stringify({ success: false, error: "\u64CD\u4F5C\u5DF2\u53D6\u6D88", cancelled: true });
2066
- }
2067
- const fs = await import('fs/promises');
2068
- const path = await import('path');
2069
- const videoData = await fs.readFile(videoPath);
2070
- const base64 = videoData.toString("base64");
2071
- const ext = path.extname(videoPath).toLowerCase();
2072
- const mimeType = getVideoMimeType(ext);
2073
- const response = await geminiClient.models.generateContent({
2074
- model: GEMINI_IMAGE_MODEL,
2075
- contents: [{
2076
- role: "user",
2077
- parts: [
2078
- { text: question || "\u8BF7\u8BE6\u7EC6\u63CF\u8FF0\u8FD9\u4E2A\u89C6\u9891\u7684\u5185\u5BB9" },
2079
- { inlineData: { mimeType, data: base64 } }
2080
- ]
2081
- }],
2082
- config: { abortSignal: signal }
2083
- });
2084
- return response.text || "\u65E0\u6CD5\u5206\u6790\u89C6\u9891";
2085
- } catch (error) {
2086
- if (signal?.aborted) return JSON.stringify({ success: false, error: "\u64CD\u4F5C\u5DF2\u53D6\u6D88", cancelled: true });
2087
- return `\u89C6\u9891\u5206\u6790\u5931\u8D25: ${error}`;
2088
- }
2089
- }
2090
-
2091
- // src/builtin-tools/analyze-image.ts
2092
- function analyzeImageTool() {
2093
- return tool({
2094
- name: "analyze_image",
2095
- description: "\u5206\u6790\u56FE\u7247\u5185\u5BB9\uFF08\u8BC6\u522B\u7269\u4F53\u3001\u573A\u666F\u3001\u6587\u5B57\u7B49\uFF09",
2096
- parameters: {
2097
- type: "object",
2098
- properties: {
2099
- imagePath: { type: "string", description: "\u56FE\u7247\u6587\u4EF6\u8DEF\u5F84" },
2100
- question: { type: "string", description: "\u5173\u4E8E\u56FE\u7247\u7684\u95EE\u9898\uFF08\u53EF\u9009\uFF09" }
2101
- },
2102
- required: ["imagePath"]
2103
- },
2104
- execute: async (args, context) => {
2105
- if (!context.geminiClient) {
2106
- return "\u9519\u8BEF: \u672A\u914D\u7F6E Gemini API Key\uFF0C\u65E0\u6CD5\u4F7F\u7528\u56FE\u7247\u5206\u6790\u529F\u80FD";
2107
- }
2108
- return await analyzeImage(
2109
- context.geminiClient,
2110
- args.imagePath,
2111
- args.question,
2112
- context.signal
2113
- );
2114
- }
2115
- });
2116
- }
2117
-
2118
- // src/builtin-tools/generate-image.ts
2119
- function generateImageTool() {
2120
- return tool({
2121
- name: "generate_image",
2122
- description: "\u6839\u636E\u6587\u5B57\u63CF\u8FF0\u751F\u6210\u56FE\u7247\u5E76\u4FDD\u5B58\u5230\u672C\u5730\u3002\u8FD4\u56DE\u4FDD\u5B58\u8DEF\u5F84\u3002",
2123
- parameters: {
2124
- type: "object",
2125
- properties: {
2126
- prompt: { type: "string", description: "\u56FE\u7247\u63CF\u8FF0\uFF0C\u4F7F\u7528\u8BE6\u7EC6\u7684\u82F1\u6587\u63CF\u8FF0\u6548\u679C\u66F4\u597D" },
2127
- outputPath: { type: "string", description: "\u4FDD\u5B58\u8DEF\u5F84\uFF08\u53EF\u9009\uFF09" }
2128
- },
2129
- required: ["prompt"]
2130
- },
2131
- // 图片生成完成后通知用户
2132
- sideEffects: [{ type: "notification", success: true, message: "\u{1F3A8} \u56FE\u7247\u751F\u6210\u5B8C\u6210" }],
2133
- execute: async (args, context) => {
2134
- if (!context.geminiClient) {
2135
- return "\u9519\u8BEF: \u672A\u914D\u7F6E Gemini API Key\uFF0C\u65E0\u6CD5\u4F7F\u7528\u56FE\u7247\u751F\u6210\u529F\u80FD";
2136
- }
2137
- return await generateImage(
2138
- context.geminiClient,
2139
- context.cwd,
2140
- args.prompt,
2141
- args.outputPath,
2142
- context.signal
2143
- );
2144
- }
2145
- });
2146
- }
2147
-
2148
- // src/builtin-tools/analyze-video.ts
2149
- function analyzeVideoTool() {
2150
- return tool({
2151
- name: "analyze_video",
2152
- description: "\u5206\u6790\u89C6\u9891\u5185\u5BB9\uFF08\u8BC6\u522B\u573A\u666F\u3001\u52A8\u4F5C\u3001\u7269\u4F53\u7B49\uFF09",
2153
- parameters: {
2154
- type: "object",
2155
- properties: {
2156
- videoPath: { type: "string", description: "\u89C6\u9891\u6587\u4EF6\u8DEF\u5F84" },
2157
- question: { type: "string", description: "\u5173\u4E8E\u89C6\u9891\u7684\u95EE\u9898\uFF08\u53EF\u9009\uFF09" }
2158
- },
2159
- required: ["videoPath"]
2160
- },
2161
- execute: async (args, context) => {
2162
- if (!context.geminiClient) {
2163
- return "\u9519\u8BEF: \u672A\u914D\u7F6E Gemini API Key\uFF0C\u65E0\u6CD5\u4F7F\u7528\u89C6\u9891\u5206\u6790\u529F\u80FD";
2164
- }
2165
- return await analyzeVideo(
2166
- context.geminiClient,
2167
- args.videoPath,
2168
- args.question,
2169
- context.signal
2170
- );
2171
- }
2172
- });
2173
- }
2174
-
2175
- // src/builtin-tools/weather/city-adcodes.ts
2176
- var CITY_ADCODE_MAP = {
2177
- // 直辖市
2178
- "\u5317\u4EAC": "110000",
2179
- "\u5929\u6D25": "120000",
2180
- "\u4E0A\u6D77": "310000",
2181
- "\u91CD\u5E86": "500000",
2182
- // 河北省
2183
- "\u77F3\u5BB6\u5E84": "130100",
2184
- "\u5510\u5C71": "130200",
2185
- "\u79E6\u7687\u5C9B": "130300",
2186
- "\u90AF\u90F8": "130400",
2187
- "\u90A2\u53F0": "130500",
2188
- "\u4FDD\u5B9A": "130600",
2189
- "\u5F20\u5BB6\u53E3": "130700",
2190
- "\u627F\u5FB7": "130800",
2191
- "\u6CA7\u5DDE": "130900",
2192
- "\u5ECA\u574A": "131000",
2193
- "\u8861\u6C34": "131100",
2194
- // 山西省
2195
- "\u592A\u539F": "140100",
2196
- "\u5927\u540C": "140200",
2197
- "\u9633\u6CC9": "140300",
2198
- "\u957F\u6CBB": "140400",
2199
- "\u664B\u57CE": "140500",
2200
- "\u6714\u5DDE": "140600",
2201
- "\u664B\u4E2D": "140700",
2202
- "\u8FD0\u57CE": "140800",
2203
- "\u5FFB\u5DDE": "140900",
2204
- "\u4E34\u6C7E": "141000",
2205
- "\u5415\u6881": "141100",
2206
- // 内蒙古自治区
2207
- "\u547C\u548C\u6D69\u7279": "150100",
2208
- "\u5305\u5934": "150200",
2209
- "\u4E4C\u6D77": "150300",
2210
- "\u8D64\u5CF0": "150400",
2211
- "\u901A\u8FBD": "150500",
2212
- "\u9102\u5C14\u591A\u65AF": "150600",
2213
- "\u547C\u4F26\u8D1D\u5C14": "150700",
2214
- "\u5DF4\u5F66\u6DD6\u5C14": "150800",
2215
- "\u4E4C\u5170\u5BDF\u5E03": "150900",
2216
- "\u5174\u5B89\u76DF": "152200",
2217
- "\u9521\u6797\u90ED\u52D2\u76DF": "152500",
2218
- "\u963F\u62C9\u5584\u76DF": "152900",
2219
- // 辽宁省
2220
- "\u6C88\u9633": "210100",
2221
- "\u5927\u8FDE": "210200",
2222
- "\u978D\u5C71": "210300",
2223
- "\u629A\u987A": "210400",
2224
- "\u672C\u6EAA": "210500",
2225
- "\u4E39\u4E1C": "210600",
2226
- "\u9526\u5DDE": "210700",
2227
- "\u8425\u53E3": "210800",
2228
- "\u961C\u65B0": "210900",
2229
- "\u8FBD\u9633": "211000",
2230
- "\u76D8\u9526": "211100",
2231
- "\u94C1\u5CAD": "211200",
2232
- "\u671D\u9633": "211300",
2233
- "\u846B\u82A6\u5C9B": "211400",
2234
- // 吉林省
2235
- "\u957F\u6625": "220100",
2236
- "\u5409\u6797": "220200",
2237
- "\u56DB\u5E73": "220300",
2238
- "\u8FBD\u6E90": "220400",
2239
- "\u901A\u5316": "220500",
2240
- "\u767D\u5C71": "220600",
2241
- "\u677E\u539F": "220700",
2242
- "\u767D\u57CE": "220800",
2243
- "\u5EF6\u8FB9\u671D\u9C9C\u65CF\u81EA\u6CBB\u5DDE": "222400",
2244
- // 黑龙江省
2245
- "\u54C8\u5C14\u6EE8": "230100",
2246
- "\u9F50\u9F50\u54C8\u5C14": "230200",
2247
- "\u9E21\u897F": "230300",
2248
- "\u9E64\u5C97": "230400",
2249
- "\u53CC\u9E2D\u5C71": "230500",
2250
- "\u5927\u5E86": "230600",
2251
- "\u4F0A\u6625": "230700",
2252
- "\u4F73\u6728\u65AF": "230800",
2253
- "\u4E03\u53F0\u6CB3": "230900",
2254
- "\u7261\u4E39\u6C5F": "231000",
2255
- "\u9ED1\u6CB3": "231100",
2256
- "\u7EE5\u5316": "231200",
2257
- "\u5927\u5174\u5B89\u5CAD\u5730\u533A": "232700",
2258
- // 江苏省
2259
- "\u5357\u4EAC": "320100",
2260
- "\u65E0\u9521": "320200",
2261
- "\u5F90\u5DDE": "320300",
2262
- "\u5E38\u5DDE": "320400",
2263
- "\u82CF\u5DDE": "320500",
2264
- "\u5357\u901A": "320600",
2265
- "\u8FDE\u4E91\u6E2F": "320700",
2266
- "\u6DEE\u5B89": "320800",
2267
- "\u76D0\u57CE": "320900",
2268
- "\u626C\u5DDE": "321000",
2269
- "\u9547\u6C5F": "321100",
2270
- "\u6CF0\u5DDE": "321200",
2271
- "\u5BBF\u8FC1": "321300",
2272
- // 浙江省
2273
- "\u676D\u5DDE": "330100",
2274
- "\u5B81\u6CE2": "330200",
2275
- "\u6E29\u5DDE": "330300",
2276
- "\u5609\u5174": "330400",
2277
- "\u6E56\u5DDE": "330500",
2278
- "\u7ECD\u5174": "330600",
2279
- "\u91D1\u534E": "330700",
2280
- "\u8862\u5DDE": "330800",
2281
- "\u821F\u5C71": "330900",
2282
- "\u53F0\u5DDE": "331000",
2283
- "\u4E3D\u6C34": "331100",
2284
- // 安徽省
2285
- "\u5408\u80A5": "340100",
2286
- "\u829C\u6E56": "340200",
2287
- "\u868C\u57E0": "340300",
2288
- "\u6DEE\u5357": "340400",
2289
- "\u9A6C\u978D\u5C71": "340500",
2290
- "\u6DEE\u5317": "340600",
2291
- "\u94DC\u9675": "340700",
2292
- "\u5B89\u5E86": "340800",
2293
- "\u9EC4\u5C71": "341000",
2294
- "\u6EC1\u5DDE": "341100",
2295
- "\u961C\u9633": "341200",
2296
- "\u5BBF\u5DDE": "341300",
2297
- "\u516D\u5B89": "341500",
2298
- "\u4EB3\u5DDE": "341600",
2299
- "\u6C60\u5DDE": "341700",
2300
- "\u5BA3\u57CE": "341800",
2301
- // 福建省
2302
- "\u798F\u5DDE": "350100",
2303
- "\u53A6\u95E8": "350200",
2304
- "\u8386\u7530": "350300",
2305
- "\u4E09\u660E": "350400",
2306
- "\u6CC9\u5DDE": "350500",
2307
- "\u6F33\u5DDE": "350600",
2308
- "\u5357\u5E73": "350700",
2309
- "\u9F99\u5CA9": "350800",
2310
- "\u5B81\u5FB7": "350900",
2311
- // 江西省
2312
- "\u5357\u660C": "360100",
2313
- "\u666F\u5FB7\u9547": "360200",
2314
- "\u840D\u4E61": "360300",
2315
- "\u4E5D\u6C5F": "360400",
2316
- "\u65B0\u4F59": "360500",
2317
- "\u9E70\u6F6D": "360600",
2318
- "\u8D63\u5DDE": "360700",
2319
- "\u5409\u5B89": "360800",
2320
- "\u5B9C\u6625": "360900",
2321
- "\u629A\u5DDE": "361000",
2322
- "\u4E0A\u9976": "361100",
2323
- // 山东省
2324
- "\u6D4E\u5357": "370100",
2325
- "\u9752\u5C9B": "370200",
2326
- "\u6DC4\u535A": "370300",
2327
- "\u67A3\u5E84": "370400",
2328
- "\u4E1C\u8425": "370500",
2329
- "\u70DF\u53F0": "370600",
2330
- "\u6F4D\u574A": "370700",
2331
- "\u6D4E\u5B81": "370800",
2332
- "\u6CF0\u5B89": "370900",
2333
- "\u5A01\u6D77": "371000",
2334
- "\u65E5\u7167": "371100",
2335
- "\u4E34\u6C82": "371300",
2336
- "\u5FB7\u5DDE": "371400",
2337
- "\u804A\u57CE": "371500",
2338
- "\u6EE8\u5DDE": "371600",
2339
- "\u83CF\u6CFD": "371700",
2340
- // 河南省
2341
- "\u90D1\u5DDE": "410100",
2342
- "\u5F00\u5C01": "410200",
2343
- "\u6D1B\u9633": "410300",
2344
- "\u5E73\u9876\u5C71": "410400",
2345
- "\u5B89\u9633": "410500",
2346
- "\u9E64\u58C1": "410600",
2347
- "\u65B0\u4E61": "410700",
2348
- "\u7126\u4F5C": "410800",
2349
- "\u6FEE\u9633": "410900",
2350
- "\u8BB8\u660C": "411000",
2351
- "\u6F2F\u6CB3": "411100",
2352
- "\u4E09\u95E8\u5CE1": "411200",
2353
- "\u5357\u9633": "411300",
2354
- "\u5546\u4E18": "411400",
2355
- "\u4FE1\u9633": "411500",
2356
- "\u5468\u53E3": "411600",
2357
- "\u9A7B\u9A6C\u5E97": "411700",
2358
- // 湖北省
2359
- "\u6B66\u6C49": "420100",
2360
- "\u9EC4\u77F3": "420200",
2361
- "\u5341\u5830": "420300",
2362
- "\u5B9C\u660C": "420500",
2363
- "\u8944\u9633": "420600",
2364
- "\u9102\u5DDE": "420700",
2365
- "\u8346\u95E8": "420800",
2366
- "\u5B5D\u611F": "420900",
2367
- "\u8346\u5DDE": "421000",
2368
- "\u9EC4\u5188": "421100",
2369
- "\u54B8\u5B81": "421200",
2370
- "\u968F\u5DDE": "421300",
2371
- "\u6069\u65BD\u571F\u5BB6\u65CF\u82D7\u65CF\u81EA\u6CBB\u5DDE": "422800",
2372
- // 湖南省
2373
- "\u957F\u6C99": "430100",
2374
- "\u682A\u6D32": "430200",
2375
- "\u6E58\u6F6D": "430300",
2376
- "\u8861\u9633": "430400",
2377
- "\u90B5\u9633": "430500",
2378
- "\u5CB3\u9633": "430600",
2379
- "\u5E38\u5FB7": "430700",
2380
- "\u5F20\u5BB6\u754C": "430800",
2381
- "\u76CA\u9633": "430900",
2382
- "\u90F4\u5DDE": "431000",
2383
- "\u6C38\u5DDE": "431100",
2384
- "\u6000\u5316": "431200",
2385
- "\u5A04\u5E95": "431300",
2386
- "\u6E58\u897F\u571F\u5BB6\u65CF\u82D7\u65CF\u81EA\u6CBB\u5DDE": "433100",
2387
- // 广东省
2388
- "\u5E7F\u5DDE": "440100",
2389
- "\u97F6\u5173": "440200",
2390
- "\u6DF1\u5733": "440300",
2391
- "\u73E0\u6D77": "440400",
2392
- "\u6C55\u5934": "440500",
2393
- "\u4F5B\u5C71": "440600",
2394
- "\u6C5F\u95E8": "440700",
2395
- "\u6E5B\u6C5F": "440800",
2396
- "\u8302\u540D": "440900",
2397
- "\u8087\u5E86": "441200",
2398
- "\u60E0\u5DDE": "441300",
2399
- "\u6885\u5DDE": "441400",
2400
- "\u6C55\u5C3E": "441500",
2401
- "\u6CB3\u6E90": "441600",
2402
- "\u9633\u6C5F": "441700",
2403
- "\u6E05\u8FDC": "441800",
2404
- "\u4E1C\u839E": "441900",
2405
- "\u4E2D\u5C71": "442000",
2406
- "\u6F6E\u5DDE": "445100",
2407
- "\u63ED\u9633": "445200",
2408
- "\u4E91\u6D6E": "445300",
2409
- // 广西壮族自治区
2410
- "\u5357\u5B81": "450100",
2411
- "\u67F3\u5DDE": "450200",
2412
- "\u6842\u6797": "450300",
2413
- "\u68A7\u5DDE": "450400",
2414
- "\u5317\u6D77": "450500",
2415
- "\u9632\u57CE\u6E2F": "450600",
2416
- "\u94A6\u5DDE": "450700",
2417
- "\u8D35\u6E2F": "450800",
2418
- "\u7389\u6797": "450900",
2419
- "\u767E\u8272": "451000",
2420
- "\u8D3A\u5DDE": "451100",
2421
- "\u6CB3\u6C60": "451200",
2422
- "\u6765\u5BBE": "451300",
2423
- "\u5D07\u5DE6": "451400",
2424
- // 海南省
2425
- "\u6D77\u53E3": "460100",
2426
- "\u4E09\u4E9A": "460200",
2427
- "\u4E09\u6C99": "460300",
2428
- "\u510B\u5DDE": "460400",
2429
- // 四川省
2430
- "\u6210\u90FD": "510100",
2431
- "\u81EA\u8D21": "510300",
2432
- "\u6500\u679D\u82B1": "510400",
2433
- "\u6CF8\u5DDE": "510500",
2434
- "\u5FB7\u9633": "510600",
2435
- "\u7EF5\u9633": "510700",
2436
- "\u5E7F\u5143": "510800",
2437
- "\u9042\u5B81": "510900",
2438
- "\u5185\u6C5F": "511000",
2439
- "\u4E50\u5C71": "511100",
2440
- "\u5357\u5145": "511300",
2441
- "\u7709\u5C71": "511400",
2442
- "\u5B9C\u5BBE": "511500",
2443
- "\u5E7F\u5B89": "511600",
2444
- "\u8FBE\u5DDE": "511700",
2445
- "\u96C5\u5B89": "511800",
2446
- "\u5DF4\u4E2D": "511900",
2447
- "\u8D44\u9633": "512000",
2448
- "\u963F\u575D\u85CF\u65CF\u7F8C\u65CF\u81EA\u6CBB\u5DDE": "513200",
2449
- "\u7518\u5B5C\u85CF\u65CF\u81EA\u6CBB\u5DDE": "513300",
2450
- "\u51C9\u5C71\u5F5D\u65CF\u81EA\u6CBB\u5DDE": "513400",
2451
- // 贵州省
2452
- "\u8D35\u9633": "520100",
2453
- "\u516D\u76D8\u6C34": "520200",
2454
- "\u9075\u4E49": "520300",
2455
- "\u5B89\u987A": "520400",
2456
- "\u6BD5\u8282": "520500",
2457
- "\u94DC\u4EC1": "520600",
2458
- "\u9ED4\u897F\u5357\u5E03\u4F9D\u65CF\u82D7\u65CF\u81EA\u6CBB\u5DDE": "522300",
2459
- "\u9ED4\u4E1C\u5357\u82D7\u65CF\u4F97\u65CF\u81EA\u6CBB\u5DDE": "522600",
2460
- "\u9ED4\u5357\u5E03\u4F9D\u65CF\u82D7\u65CF\u81EA\u6CBB\u5DDE": "522700",
2461
- // 云南省
2462
- "\u6606\u660E": "530100",
2463
- "\u66F2\u9756": "530300",
2464
- "\u7389\u6EAA": "530400",
2465
- "\u4FDD\u5C71": "530500",
2466
- "\u662D\u901A": "530600",
2467
- "\u4E3D\u6C5F": "530700",
2468
- "\u666E\u6D31": "530800",
2469
- "\u4E34\u6CA7": "530900",
2470
- "\u695A\u96C4\u5F5D\u65CF\u81EA\u6CBB\u5DDE": "532300",
2471
- "\u7EA2\u6CB3\u54C8\u5C3C\u65CF\u5F5D\u65CF\u81EA\u6CBB\u5DDE": "532500",
2472
- "\u6587\u5C71\u58EE\u65CF\u82D7\u65CF\u81EA\u6CBB\u5DDE": "532600",
2473
- "\u897F\u53CC\u7248\u7EB3\u50A3\u65CF\u81EA\u6CBB\u5DDE": "532800",
2474
- "\u5927\u7406\u767D\u65CF\u81EA\u6CBB\u5DDE": "532900",
2475
- "\u5FB7\u5B8F\u50A3\u65CF\u666F\u9887\u65CF\u81EA\u6CBB\u5DDE": "533100",
2476
- "\u6012\u6C5F\u5088\u50F3\u65CF\u81EA\u6CBB\u5DDE": "533300",
2477
- "\u8FEA\u5E86\u85CF\u65CF\u81EA\u6CBB\u5DDE": "533400",
2478
- // 西藏自治区
2479
- "\u62C9\u8428": "540100",
2480
- "\u65E5\u5580\u5219": "540200",
2481
- "\u660C\u90FD": "540300",
2482
- "\u6797\u829D": "540400",
2483
- "\u5C71\u5357": "540500",
2484
- "\u90A3\u66F2": "540600",
2485
- "\u963F\u91CC\u5730\u533A": "542500",
2486
- // 陕西省
2487
- "\u897F\u5B89": "610100",
2488
- "\u94DC\u5DDD": "610200",
2489
- "\u5B9D\u9E21": "610300",
2490
- "\u54B8\u9633": "610400",
2491
- "\u6E2D\u5357": "610500",
2492
- "\u5EF6\u5B89": "610600",
2493
- "\u6C49\u4E2D": "610700",
2494
- "\u6986\u6797": "610800",
2495
- "\u5B89\u5EB7": "610900",
2496
- "\u5546\u6D1B": "611000",
2497
- // 甘肃省
2498
- "\u5170\u5DDE": "620100",
2499
- "\u5609\u5CEA\u5173": "620200",
2500
- "\u91D1\u660C": "620300",
2501
- "\u767D\u94F6": "620400",
2502
- "\u5929\u6C34": "620500",
2503
- "\u6B66\u5A01": "620600",
2504
- "\u5F20\u6396": "620700",
2505
- "\u5E73\u51C9": "620800",
2506
- "\u9152\u6CC9": "620900",
2507
- "\u5E86\u9633": "621000",
2508
- "\u5B9A\u897F": "621100",
2509
- "\u9647\u5357": "621200",
2510
- "\u4E34\u590F\u56DE\u65CF\u81EA\u6CBB\u5DDE": "622900",
2511
- "\u7518\u5357\u85CF\u65CF\u81EA\u6CBB\u5DDE": "623000",
2512
- // 青海省
2513
- "\u897F\u5B81": "630100",
2514
- "\u6D77\u4E1C": "630200",
2515
- "\u6D77\u5317\u85CF\u65CF\u81EA\u6CBB\u5DDE": "632200",
2516
- "\u9EC4\u5357\u85CF\u65CF\u81EA\u6CBB\u5DDE": "632300",
2517
- "\u6D77\u5357\u85CF\u65CF\u81EA\u6CBB\u5DDE": "632500",
2518
- "\u679C\u6D1B\u85CF\u65CF\u81EA\u6CBB\u5DDE": "632600",
2519
- "\u7389\u6811\u85CF\u65CF\u81EA\u6CBB\u5DDE": "632700",
2520
- "\u6D77\u897F\u8499\u53E4\u65CF\u85CF\u65CF\u81EA\u6CBB\u5DDE": "632800",
2521
- // 宁夏回族自治区
2522
- "\u94F6\u5DDD": "640100",
2523
- "\u77F3\u5634\u5C71": "640200",
2524
- "\u5434\u5FE0": "640300",
2525
- "\u56FA\u539F": "640400",
2526
- "\u4E2D\u536B": "640500",
2527
- // 新疆维吾尔自治区
2528
- "\u4E4C\u9C81\u6728\u9F50": "650100",
2529
- "\u514B\u62C9\u739B\u4F9D": "650200",
2530
- "\u5410\u9C81\u756A": "650400",
2531
- "\u54C8\u5BC6": "650500",
2532
- "\u660C\u5409\u56DE\u65CF\u81EA\u6CBB\u5DDE": "652300",
2533
- "\u535A\u5C14\u5854\u62C9\u8499\u53E4\u81EA\u6CBB\u5DDE": "652700",
2534
- "\u5DF4\u97F3\u90ED\u695E\u8499\u53E4\u81EA\u6CBB\u5DDE": "652800",
2535
- "\u963F\u514B\u82CF\u5730\u533A": "652900",
2536
- "\u514B\u5B5C\u52D2\u82CF\u67EF\u5C14\u514B\u5B5C\u81EA\u6CBB\u5DDE": "653000",
2537
- "\u5580\u4EC0\u5730\u533A": "653100",
2538
- "\u548C\u7530\u5730\u533A": "653200",
2539
- "\u4F0A\u7281\u54C8\u8428\u514B\u81EA\u6CBB\u5DDE": "654000",
2540
- "\u5854\u57CE\u5730\u533A": "654200",
2541
- "\u963F\u52D2\u6CF0\u5730\u533A": "654300"
2542
- };
2543
- function getCityAdcode(cityName) {
2544
- if (CITY_ADCODE_MAP[cityName]) {
2545
- return CITY_ADCODE_MAP[cityName];
2546
- }
2547
- const normalized = cityName.replace(/[市省区]$/, "");
2548
- if (CITY_ADCODE_MAP[normalized]) {
2549
- return CITY_ADCODE_MAP[normalized];
2550
- }
2551
- for (const [name, code] of Object.entries(CITY_ADCODE_MAP)) {
2552
- if (name.startsWith(cityName) || cityName.startsWith(name.slice(0, 2))) {
2553
- return code;
2554
- }
2555
- }
2556
- return null;
2557
- }
2558
-
2559
- // src/builtin-tools/weather/get-weather.ts
2560
- var AMAP_KEY = "16924e39cfd78c645f0035c1bc290961";
2561
- function getWeatherTool() {
2562
- return tool({
2563
- name: "get_weather",
2564
- description: "\u83B7\u53D6\u6307\u5B9A\u57CE\u5E02\u7684\u5B9E\u65F6\u5929\u6C14\u4FE1\u606F\u3002\u652F\u6301\u5168\u56FD 337 \u4E2A\u5730\u7EA7\u5E02\uFF0C\u5305\u62EC\uFF1A\u5317\u4EAC\u3001\u4E0A\u6D77\u3001\u5E7F\u5DDE\u3001\u6DF1\u5733\u3001\u676D\u5DDE\u3001\u6210\u90FD\u3001\u6B66\u6C49\u3001\u897F\u5B89\u3001\u5357\u4EAC\u3001\u82CF\u5DDE\u3001\u91CD\u5E86\u3001\u5929\u6D25\u7B49\u3002\u8FD4\u56DE\u6E29\u5EA6\u3001\u5929\u6C14\u72B6\u51B5\u3001\u6E7F\u5EA6\u3001\u98CE\u5411\u98CE\u529B\u7B49\u4FE1\u606F\u3002",
2565
- parameters: {
2566
- type: "object",
2567
- properties: {
2568
- city: {
2569
- type: "string",
2570
- description: '\u57CE\u5E02\u540D\u79F0\uFF0C\u5982 "\u5317\u4EAC"\u3001"\u4E0A\u6D77"\u3001"\u6DF1\u5733"\u3001"\u676D\u5DDE"'
2571
- }
2572
- },
2573
- required: ["city"]
2574
- },
2575
- execute: async (args, context) => {
2576
- const city = args.city;
2577
- if (context.signal?.aborted) {
2578
- return JSON.stringify({ success: false, error: "\u64CD\u4F5C\u5DF2\u53D6\u6D88", cancelled: true });
2579
- }
2580
- const adcode = getCityAdcode(city);
2581
- if (!adcode) {
2582
- return JSON.stringify({
2583
- error: `\u4E0D\u652F\u6301\u7684\u57CE\u5E02: ${city}`,
2584
- tip: "\u8BF7\u8F93\u5165\u5730\u7EA7\u5E02\u540D\u79F0\uFF0C\u5982\uFF1A\u5317\u4EAC\u3001\u4E0A\u6D77\u3001\u5E7F\u5DDE\u3001\u6DF1\u5733"
2585
- });
2586
- }
2587
- try {
2588
- const url = `https://restapi.amap.com/v3/weather/weatherInfo?key=${AMAP_KEY}&city=${adcode}&extensions=base&output=JSON`;
2589
- const response = await fetch(url, { signal: context.signal });
2590
- if (!response.ok) {
2591
- return JSON.stringify({ error: `\u5929\u6C14\u670D\u52A1\u8BF7\u6C42\u5931\u8D25: ${response.status}` });
2592
- }
2593
- const data = await response.json();
2594
- if (data.status !== "1" || data.infocode !== "10000") {
2595
- return JSON.stringify({ error: `\u5929\u6C14\u670D\u52A1\u8FD4\u56DE\u9519\u8BEF: ${data.info}` });
2596
- }
2597
- if (!data.lives?.length) {
2598
- return JSON.stringify({ error: "\u672A\u83B7\u53D6\u5230\u5929\u6C14\u6570\u636E" });
2599
- }
2600
- const live = data.lives[0];
2601
- return JSON.stringify({
2602
- city: live.city,
2603
- temperature: parseInt(live.temperature, 10),
2604
- condition: live.weather,
2605
- humidity: parseInt(live.humidity, 10),
2606
- wind: `${live.winddirection}\u98CE ${live.windpower}\u7EA7`,
2607
- reportTime: live.reporttime,
2608
- province: live.province
2609
- });
2610
- } catch (error) {
2611
- if (context.signal?.aborted) {
2612
- return JSON.stringify({ success: false, error: "\u64CD\u4F5C\u5DF2\u53D6\u6D88", cancelled: true });
2613
- }
2614
- return JSON.stringify({
2615
- success: false,
2616
- error: `\u83B7\u53D6\u5929\u6C14\u5931\u8D25: ${error instanceof Error ? error.message : "\u7F51\u7EDC\u9519\u8BEF"}`
2617
- });
2618
- }
2619
- }
2620
- });
2621
- }
2622
-
2623
- // src/builtin-tools/ui-actions/show-toast.ts
2624
- function getStringArg(args, key) {
2625
- const value = args[key];
2626
- return typeof value === "string" ? value : null;
2627
- }
2628
- function showToastTool() {
2629
- return tool({
2630
- name: "show_toast",
2631
- description: "\u5728\u7528\u6237\u754C\u9762\u4E0A\u663E\u793A\u4E00\u6761Toast \u901A\u77E5\u6D88\u606F\uFF0C\u7528\u4E8E\u544A\u77E5\u7528\u6237\u64CD\u4F5C\u7ED3\u679C\u6216\u91CD\u8981\u63D0\u793A\u4FE1\u606F\uFF0C\u6D88\u606F\u4F1A\u5728\u51E0\u79D2\u540E\u81EA\u52A8\u6D88\u5931",
2632
- parameters: {
2633
- type: "object",
2634
- properties: {
2635
- message: { type: "string", description: "\u8981\u663E\u793A\u7ED9\u7528\u6237\u7684\u901A\u77E5\u5185\u5BB9" }
2636
- },
2637
- required: ["message"]
2638
- },
2639
- execute: async (args) => {
2640
- const message = getStringArg(args, "message");
2641
- if (!message) throw new Error("\u53C2\u6570\u9519\u8BEF\uFF1Amessage \u5FC5\u987B\u662F string");
2642
- return {
2643
- result: JSON.stringify({ success: true }),
2644
- sideEffects: [{ type: "notification", success: true, message }]
2645
- };
2646
- }
2647
- });
2648
- }
2649
-
2650
- // src/builtin-tools/ui-actions/open-confirm-dialog.ts
2651
- function getStringArg2(args, key) {
2652
- const value = args[key];
2653
- return typeof value === "string" ? value : null;
2654
- }
2655
- function openConfirmDialogTool() {
2656
- return tool({
2657
- name: "open_confirm_dialog",
2658
- description: "\u5728\u7528\u6237\u754C\u9762\u4E0A\u5F39\u51FA\u4E00\u4E2A\u6A21\u6001\u786E\u8BA4\u5BF9\u8BDD\u6846\u3002",
2659
- parameters: {
2660
- type: "object",
2661
- properties: {
2662
- title: { type: "string", description: "\u5BF9\u8BDD\u6846\u6807\u9898\uFF0C\u7B80\u8981\u8BF4\u660E\u786E\u8BA4\u7684\u64CD\u4F5C" },
2663
- message: { type: "string", description: "\u8BE6\u7EC6\u63CF\u8FF0\u9700\u8981\u7528\u6237\u786E\u8BA4\u7684\u5185\u5BB9" },
2664
- confirmText: { type: "string", description: '\u786E\u8BA4\u6309\u94AE\u6587\u5B57\uFF0C\u9ED8\u8BA4\u4E3A"\u786E\u8BA4"' },
2665
- cancelText: { type: "string", description: '\u53D6\u6D88\u6309\u94AE\u6587\u5B57\uFF0C\u9ED8\u8BA4\u4E3A"\u53D6\u6D88"' }
2666
- },
2667
- required: ["title", "message"]
2668
- },
2669
- execute: async (args) => {
2670
- const title = getStringArg2(args, "title");
2671
- const message = getStringArg2(args, "message");
2672
- if (!title) throw new Error("\u53C2\u6570\u9519\u8BEF\uFF1Atitle \u5FC5\u987B\u662F string");
2673
- if (!message) throw new Error("\u53C2\u6570\u9519\u8BEF\uFF1Amessage \u5FC5\u987B\u662F string");
2674
- const confirmText = getStringArg2(args, "confirmText") ?? "\u786E\u8BA4";
2675
- const cancelText = getStringArg2(args, "cancelText") ?? "\u53D6\u6D88";
2676
- return {
2677
- result: JSON.stringify({ success: true }),
2678
- sideEffects: [
2679
- {
2680
- type: "ui:confirm_dialog",
2681
- success: true,
2682
- data: {
2683
- title,
2684
- message,
2685
- confirmText,
2686
- cancelText
2687
- }
2688
- }
2689
- ]
2690
- };
2691
- }
2692
- });
2693
- }
2694
-
2695
- // src/builtin-tools/ui-actions/open-file-preview.ts
2696
- function getStringArg3(args, key) {
2697
- const value = args[key];
2698
- return typeof value === "string" ? value : null;
2699
- }
2700
- function getMediaTypeArg(args, key) {
2701
- const value = args[key];
2702
- if (value === "image" || value === "video" || value === "audio") return value;
2703
- return null;
2704
- }
2705
- function openFilePreviewTool() {
2706
- return tool({
2707
- name: "open_file_preview",
2708
- description: "\u5728\u7528\u6237\u754C\u9762\u4E0A\u6253\u5F00\u6587\u4EF6\u9884\u89C8\u7A97\u53E3\uFF0C\u8BA9\u7528\u6237\u67E5\u770B\u6307\u5B9A\u6587\u4EF6\u7684\u5185\u5BB9\uFF08\u5BBF\u4E3B\u9700\u5904\u7406 ui:file_preview sideEffect\uFF09\u3002",
2709
- parameters: {
2710
- type: "object",
2711
- properties: {
2712
- path: { type: "string", description: "\u8981\u9884\u89C8\u7684\u6587\u4EF6\u7684\u5B8C\u6574\u8DEF\u5F84" },
2713
- mediaType: {
2714
- type: "string",
2715
- description: "\u9884\u89C8\u7C7B\u578B\uFF08\u5FC5\u586B\uFF09\uFF1Aimage/video/audio\u3002\u5BBF\u4E3B\u4E0D\u4F1A\u518D\u6839\u636E\u540E\u7F00\u63A8\u65AD\u3002",
2716
- enum: ["image", "video", "audio"]
2717
- }
2718
- },
2719
- required: ["path", "mediaType"]
2720
- },
2721
- execute: async (args) => {
2722
- const path = getStringArg3(args, "path");
2723
- if (!path) throw new Error("\u53C2\u6570\u9519\u8BEF\uFF1Apath \u5FC5\u987B\u662F string");
2724
- const mediaTypeValue = getMediaTypeArg(args, "mediaType");
2725
- if (!mediaTypeValue) throw new Error('\u53C2\u6570\u9519\u8BEF\uFF1AmediaType \u5FC5\u987B\u662F "image" | "video" | "audio"');
2726
- return {
2727
- result: JSON.stringify({ success: true, path, mediaType: mediaTypeValue }),
2728
- sideEffects: [
2729
- {
2730
- type: "ui:file_preview",
2731
- success: true,
2732
- data: { path, mediaType: mediaTypeValue }
2733
- }
2734
- ]
2735
- };
2736
- }
2737
- });
2738
- }
2739
-
2740
- // src/builtin-tools/ui-actions/navigate-to-directory.ts
2741
- function getStringArg4(args, key) {
2742
- const value = args[key];
2743
- return typeof value === "string" ? value : null;
2744
- }
2745
- function navigateToDirectoryTool() {
2746
- return tool({
2747
- name: "navigate_to_directory",
2748
- description: "\u63A7\u5236\u7528\u6237\u754C\u9762\u5DE6\u4FA7\u7684\u6587\u4EF6\u6D4F\u89C8\u5668\u8DF3\u8F6C\u5230\u6307\u5B9A\u76EE\u5F55\uFF0C\u8BA9\u7528\u6237\u53EF\u4EE5\u76F4\u63A5\u770B\u5230\u8BE5\u76EE\u5F55\u4E0B\u7684\u6587\u4EF6\u5217\u8868",
2749
- parameters: {
2750
- type: "object",
2751
- properties: {
2752
- path: { type: "string", description: "\u8981\u8DF3\u8F6C\u5230\u7684\u76EE\u5F55\u7684\u5B8C\u6574\u8DEF\u5F84" }
2753
- },
2754
- required: ["path"]
2755
- },
2756
- execute: async (args) => {
2757
- const path = getStringArg4(args, "path");
2758
- if (!path) throw new Error("\u53C2\u6570\u9519\u8BEF\uFF1Apath \u5FC5\u987B\u662F string");
2759
- return {
2760
- result: JSON.stringify({ success: true, path }),
2761
- sideEffects: [
2762
- {
2763
- type: "ui:navigate",
2764
- success: true,
2765
- data: { path }
2766
- }
2767
- ]
2768
- };
2769
- }
2770
- });
2771
- }
2772
-
2773
- // src/builtin-tools/ui-actions/index.ts
2774
- var uiActionTools = [
2775
- showToastTool(),
2776
- openConfirmDialogTool(),
2777
- openFilePreviewTool(),
2778
- navigateToDirectoryTool()
2779
- ];
2780
-
2781
- // src/builtin-tools/search-documents.ts
2782
- var aiSearchModule = null;
2783
- async function loadAiSearch() {
2784
- if (aiSearchModule) return aiSearchModule;
2785
- try {
2786
- const dynamicRequire = new Function(
2787
- "moduleName",
2788
- "return import(moduleName)"
2789
- );
2790
- const mod = await dynamicRequire("@huyooo/ai-search");
2791
- aiSearchModule = mod;
2792
- return aiSearchModule;
2793
- } catch {
2794
- return null;
2795
- }
2796
- }
2797
- async function createDocumentSearchTools(options) {
2798
- const mod = await loadAiSearch();
2799
- if (!mod) {
2800
- return [createPlaceholderTool()];
2801
- }
2802
- const dataDir = options?.dataDir || "./.search-data";
2803
- const plugin = await mod.searchPlugin({
2804
- dataDir,
2805
- // 如果配置了默认目录且自动索引,使用第一个目录作为工作空间
2806
- workspace: options?.autoIndex && options?.defaultDirectories?.length ? expandPath(options.defaultDirectories[0]) : void 0
2807
- });
2808
- return plugin.tools;
2809
- }
2810
- function createPlaceholderTool() {
2811
- return {
2812
- name: "search_local_documents",
2813
- description: "\u641C\u7D22\u672C\u5730\u6587\u6863\uFF08\u9700\u8981\u5B89\u88C5 @huyooo/ai-search\uFF09",
2814
- parameters: {
2815
- type: "object",
2816
- properties: {
2817
- query: {
2818
- type: "string",
2819
- description: "\u641C\u7D22\u5185\u5BB9"
2820
- }
2821
- },
2822
- required: ["query"]
2823
- },
2824
- execute: async () => {
2825
- return JSON.stringify({
2826
- error: "\u6587\u6863\u641C\u7D22\u529F\u80FD\u672A\u542F\u7528",
2827
- solution: "\u8BF7\u5B89\u88C5 @huyooo/ai-search: npm install @huyooo/ai-search"
2828
- });
2829
- }
2830
- };
2831
- }
2832
- function expandPath(p) {
2833
- if (p.startsWith("~")) {
2834
- return p.replace("~", process.env.HOME || "");
2835
- }
2836
- return p;
2837
- }
2838
- async function getDocumentSearchInstance(dataDir = "./.search-data", workspace) {
2839
- const mod = await loadAiSearch();
2840
- if (!mod) return null;
2841
- return await mod.searchPlugin({ dataDir, workspace });
2842
- }
2843
-
2844
- export { ARK_MODELS, ArkAdapter, ChatOrchestrator, DEFAULT_MODEL, GEMINI_MODELS, GeminiAdapter, HybridAgent, MODELS, OPENROUTER_MODELS, OpenRouterAdapter, QWEN_MODELS, QwenAdapter, analyzeImageTool, analyzeVideoTool, createAbort, createApiError, createArkAdapter, createDefaultToolExecutor, createDocumentSearchTools, createDone, createError, createGeminiAdapter, createImage, createOpenRouterAdapter, createOrchestrator, createParseError, createQwenAdapter, createRateLimitError, createSearchEnd, createSearchResult, createSearchStart, createStepEnd, createStepStart, createStreamStart, createTextDelta, createThinkingDelta, createThinkingEnd, createThinkingStart, createTimeoutError, createToolCallResult, createToolCallStart, createToolError, createVideo, executeCommandTool, generateImageTool, getCwdTool, getDefaultProvider, getDocumentSearchInstance, getModelByModelId, getNativeModels, getOpenRouterModels, getPlatformTool, getRouteRules, getWeatherTool, isAbortEvent, isErrorEvent, isMediaEvent, isModelForProvider, isRetryableError, isSearchEvent, isStatusEvent, isStepEvent, isTextEvent, isThinkingEvent, isToolEvent, navigateToDirectoryTool, openConfirmDialogTool, openFilePreviewTool, resolveTools, routeModelToProvider, routeModelWithDetails, showToastTool, tool, tools, uiActionTools };
2845
- //# sourceMappingURL=index.js.map
2846
- //# sourceMappingURL=index.js.map
1
+ import{GoogleGenAI as e}from"@google/genai";import{createGateway as t,streamText as n,tool as o}from"ai";import{z as s}from"zod";var i={id:"doubao",displayName:"豆包",supportsVision:!0,supportsThinking:!0,thinkingFormat:"reasoning",supportsNativeSearch:!0,searchStrategy:"native_web_search",toolCallFormat:"responses",defaultMaxTokens:32768},a={id:"deepseek",displayName:"DeepSeek",supportsVision:!1,supportsThinking:!0,thinkingFormat:"reasoning",supportsNativeSearch:!0,searchStrategy:"native_web_search",toolCallFormat:"responses",defaultMaxTokens:32768},r={id:"qwen",displayName:"通义千问",supportsVision:!1,supportsThinking:!0,thinkingFormat:"thinking_enabled",supportsNativeSearch:!1,searchStrategy:"tool_based",toolCallFormat:"openai",defaultMaxTokens:32768},l={id:"gemini",displayName:"Gemini",supportsVision:!0,supportsThinking:!0,thinkingFormat:"thought_signature",supportsNativeSearch:!1,searchStrategy:"tool_based",toolCallFormat:"gemini",defaultMaxTokens:65536,requiresSpecialHandling:["thought_signature"]},c={id:"gpt",displayName:"GPT",supportsVision:!0,supportsThinking:!0,thinkingFormat:"reasoning",supportsNativeSearch:!1,searchStrategy:"tool_based",toolCallFormat:"openai",defaultMaxTokens:128e3},u={id:"claude",displayName:"Claude",supportsVision:!0,supportsThinking:!0,thinkingFormat:"reasoning",supportsNativeSearch:!1,searchStrategy:"tool_based",toolCallFormat:"openai",defaultMaxTokens:2e5},p={doubao:i,deepseek:a,qwen:r,gemini:l,gpt:c,claude:u},d=[{id:"doubao-seed-1-6-250615",displayName:"豆包 Seed 1.6",family:"doubao",protocol:"ark",visible:!0,supportsVision:!0,contextWindow:"256K",pricing:["输入 0.8 元/百万tokens","输出 2-8 元/百万tokens"]},{id:"doubao-seed-1-8-251215",displayName:"豆包 Seed 1.8",family:"doubao",protocol:"ark",contextWindow:"256K",pricing:["输入 0.8 元/百万tokens","输出 2-8 元/百万tokens"]},{id:"deepseek-v3-2-251201",displayName:"DeepSeek V3.2",family:"deepseek",protocol:"deepseek",visible:!0,supportsVision:!1,contextWindow:"128K",pricing:["输入 2 元/百万tokens","输出 3 元/百万tokens"]},{id:"qwen3-vl-plus",displayName:"通义千问 3 VL",family:"qwen",protocol:"qwen",visible:!0,supportsVision:!0,contextWindow:"128K",pricing:["输入 1 元/百万tokens","输出 10 元/百万tokens"]},{id:"gemini-3-pro-preview",displayName:"Gemini 3 Pro",family:"gemini",protocol:"gemini",visible:!0,supportsVision:!0,contextWindow:"1M",pricing:["输入 1.25 元/百万tokens","输出 10 元/百万tokens"]},{id:"gemini-2.5-flash-preview-05-20",displayName:"Gemini 2.5 Flash",family:"gemini",protocol:"gemini",contextWindow:"1M",pricing:["输入 0.15 元/百万tokens","输出 0.6 元/百万tokens"]},{id:"gemini-2.5-pro-preview-05-06",displayName:"Gemini 2.5 Pro",family:"gemini",protocol:"gemini",contextWindow:"1M",pricing:["输入 1.25 元/百万tokens","输出 10 元/百万tokens"]},{id:"openai/gpt-5.2",displayName:"GPT-5.2",family:"gpt",protocol:"openai",visible:!0,supportsVision:!0,contextWindow:"400K",pricing:["输入 12.6 元/百万tokens","输出 100.8 元/百万tokens"]},{id:"anthropic/claude-opus-4.5",displayName:"Claude Opus 4.5",family:"claude",protocol:"anthropic",visible:!0,supportsVision:!0,contextWindow:"200K",pricing:["输入 36 元/百万tokens","输出 180 元/百万tokens"]}];function y(e){return d.find(t=>t.id===e||e.includes(t.id))}function g(e){const t=y(e);if(t)return p[t.family]}function m(e){const t=y(e);return t?.protocol}function h(){return d.filter(e=>e.visible)}function f(e){return d.filter(t=>t.family===e)}function _(e){return d.filter(t=>t.protocol===e)}function b(e){const t=g(e);return t?.supportsThinking??!1}function k(e){const t=g(e);return t?.supportsNativeSearch??!1}function w(e){const t=y(e);if(!t)return!1;if("boolean"==typeof t.supportsVision)return t.supportsVision;const n=g(e);return n?.supportsVision??!1}function S(e){const t=g(e);return t?.searchStrategy??"tool_based"}function v(e){const t=g(e.id),n=w(e.id),o=t?.supportsThinking??!1,s=t?.supportsNativeSearch??!1,i=[];return n&&i.push("多模态"),o&&i.push("深度思考"),e.contextWindow&&i.push(`长上下文(${e.contextWindow})`),s&&i.push("联网搜索"),i}var C=h().map(e=>{const t=g(e.id),n=t?.supportsThinking??!1,o=w(e.id);return{modelId:e.id,displayName:e.displayName,supportsThinking:n,supportsVision:o,tooltip:{features:v(e),cost:e.pricing}}});function A(e){return C.find(t=>t.modelId===e)}function T(e){return{tools:[e]}}function x(e){return{tools:e}}async function R(e){const t=[];for(const n of e){const e=await n;"tools"in e&&Array.isArray(e.tools)&&t.push(...e.tools)}return t}function K(){return{type:"thinking_start",data:{startedAt:Date.now()}}}function N(e){return{type:"thinking_delta",data:{content:e}}}function I(e){const t=Date.now();return{type:"thinking_end",data:{endedAt:t,duration:t-e}}}function M(e){return{type:"search_start",data:{query:e,startedAt:Date.now()}}}function q(e,t){const n=Date.now();return{type:"search_result",data:{results:e,endedAt:n,duration:n-t}}}function O(e,t,n){const o=Date.now();return{type:"search_end",data:{success:e,error:n,endedAt:o,duration:o-t}}}function $(e,t,n){return{type:"tool_call_start",data:{id:e,name:t,args:n,startedAt:Date.now()}}}function D(e,t,n,o,s,i,a,r){const l=Date.now();return{type:"tool_call_result",data:{id:e,name:t,result:n,success:o,error:i,endedAt:l,duration:l-s,sideEffects:a,resultType:r}}}function U(e,t,n,o){return{type:"tool_call_output",data:{id:e,name:t,stream:n,chunk:o,at:Date.now()}}}function E(e){return{type:"text_delta",data:{content:e}}}function P(e,t){return{type:"stream_start",data:{model:e,requestId:t,startedAt:Date.now()}}}function J(e,t,n){return{type:"done",data:{text:e,usage:t,duration:n}}}function W(e){return{type:"error",data:e}}function j(e,t={}){return W({category:"api",message:e,...t})}function F(e,t,n){return W({category:"rate_limit",message:e,code:"RATE_LIMIT",statusCode:429,retryable:!0,retryAfter:t,context:n})}function G(e,t,n){return W({category:"tool",message:e,code:"TOOL_ERROR",context:t,cause:n,retryable:!1})}function V(e,t){return W({category:"timeout",message:e,code:"TIMEOUT",retryable:!0,context:t})}function B(e,t){return W({category:"parse",message:e,code:"PARSE_ERROR",cause:t,retryable:!1})}function L(e){return{type:"abort",data:{reason:e,abortedAt:Date.now()}}}function z(e,t){return{type:"step_start",data:{stepNumber:e,description:t,startedAt:Date.now()}}}function H(e,t){const n=Date.now();return{type:"step_end",data:{stepNumber:e,endedAt:n,duration:n-t}}}function Q(e){return{type:"image",data:e}}function Z(e){return{type:"video",data:{path:e}}}function X(e){return e.type.startsWith("thinking_")}function Y(e){return e.type.startsWith("search_")}function ee(e){return e.type.startsWith("tool_")}function te(e){return"text_delta"===e.type}function ne(e){return"image"===e.type||"video"===e.type}function oe(e){return"stream_start"===e.type||"done"===e.type||"error"===e.type||"abort"===e.type}function se(e){return"error"===e.type}function ie(e){return"abort"===e.type}function ae(e){return e.type.startsWith("step_")}function re(e){return"error"===e.type&&!0===e.data.retryable}var le=["rm -rf /","rm -rf ~","format","mkfs","dd if=","shutdown","reboot","sudo rm","sudo format"];function ce(e=process.cwd()){return{async executeCommand(t,n,o,s){if(o?.aborted)return{success:!1,error:"操作已取消"};if(function(e){const t=e.toLowerCase().trim();return le.some(e=>t.includes(e.toLowerCase()))}(t))return{success:!1,error:"该命令被安全策略阻止"};try{const{spawn:i}=await import("child_process");return new Promise(a=>{let r="",l="",c=!1,u=null;const p=i("sh",["-c",t],{cwd:n||e,env:process.env,detached:"win32"!==process.platform}),d=e=>{if(!c){c=!0;try{"win32"!==process.platform&&"number"==typeof p.pid?process.kill(-p.pid,e):p.kill(e)}catch{try{p.kill(e)}catch{}}u&&clearTimeout(u),u=setTimeout(()=>{try{"win32"!==process.platform&&"number"==typeof p.pid?process.kill(-p.pid,"SIGKILL"):p.kill("SIGKILL")}catch{}},1500)}},y=setTimeout(()=>{c||(d("SIGTERM"),a({success:!1,error:"命令执行超时(30秒)"}))},3e4),g=()=>{c||(d("SIGTERM"),clearTimeout(y),a({success:!1,error:"操作已取消"}))};o?.addEventListener("abort",g),p.stdout?.on("data",e=>{const t=e.toString();r+=t,s?.onStdout?.(t)}),p.stderr?.on("data",e=>{const t=e.toString();l+=t,s?.onStderr?.(t)}),p.on("close",e=>{if(clearTimeout(y),o?.removeEventListener("abort",g),u&&(clearTimeout(u),u=null),c)return;const t=r.trim(),n=l.trim();if(0===e)a({success:!0,output:t||n||"执行成功"});else if(t){a({success:!0,output:t+(n?`\n[警告: ${n}]`:"")})}else a(n?{success:!1,error:n}:{success:!1,error:`命令退出码: ${e}`})}),p.on("error",e=>{clearTimeout(y),o?.removeEventListener("abort",g),u&&(clearTimeout(u),u=null),c||a({success:!1,error:e.message})})})}catch(e){return{success:!1,error:e instanceof Error?e.message:String(e)}}}}}var ue="https://ark.cn-beijing.volces.com/api/v3",pe="https://openrouter.ai/api/v1",de="doubao-seed-1-6-250615";function ye(e){return ge(e).provider}function ge(e){const t=y(e);return t?{provider:t.protocol,registryEntry:t,familyConfig:g(e),isDefault:!1}:{provider:"ark",isDefault:!0}}function me(){return"ark"}function he(e,t){return ye(e)===t}var fe=!1,_e=null,be=[],ke=null,we=null,Se=!1;function ve(){if(null!==we)return we;if(Se)return null;Se=!0;try{if("function"==typeof globalThis.require)return we=globalThis.require("fs");const e=process;if(void 0!==e&&e.mainModule?.require)return we=e.mainModule.require("fs");try{const e=new Function("moduleName","return require(moduleName)");return we=e("fs")}catch{}return null}catch{return null}}function Ce(){if(!_e||0===be.length)return;const e=ve();if(e)try{const t=be.map(e=>JSON.stringify(e)).join("\n")+"\n";e.appendFileSync(_e,t,"utf-8"),be=[]}catch(e){}}function Ae(e,t,n,o){if(!fe)return;const s=function(e,t,n,o){return{timestamp:(new Date).toISOString(),level:e,module:t,message:n,...void 0!==o?{data:o}:{}}}(e,t,n,o),i=`[${s.timestamp.slice(11,23)}] [${t}]`,a="error"===e?console.error:"warn"===e?console.warn:console.log;void 0!==o?a(`${i} ${n}`,"object"==typeof o?JSON.stringify(o):o):a(`${i} ${n}`),_e&&(be.push(s),ke||(ke=setTimeout(()=>{ke=null,Ce()},100)))}var Te={enable(e=!0){fe=e},isEnabled:()=>fe,setFsModule(e){we=e},setLogFile(e){if(Ce(),_e=e,e){const t=ve();if(t)try{const n={type:"session_start",timestamp:(new Date).toISOString(),version:"1.0"};t.writeFileSync(e,JSON.stringify(n)+"\n","utf-8")}catch(e){_e=null}else _e=null}},getLogFile:()=>_e,flush(){ke&&(clearTimeout(ke),ke=null),Ce()},module:e=>({debug:(t,n)=>Ae("debug",e,t,n),info:(t,n)=>Ae("info",e,t,n),warn:(t,n)=>Ae("warn",e,t,n),error:(t,n)=>Ae("error",e,t,n)}),debug:(e,t,n)=>Ae("debug",e,t,n),info:(e,t,n)=>Ae("info",e,t,n),warn:(e,t,n)=>Ae("warn",e,t,n),error:(e,t,n)=>Ae("error",e,t,n)};try{"undefined"!=typeof process&&process.env&&("true"===process.env.AI_CHAT_DEBUG&&(fe=!0),process.env.AI_CHAT_DEBUG_FILE&&Te.setLogFile(process.env.AI_CHAT_DEBUG_FILE))}catch{}var xe=Te.module("Orchestrator");function Re(e){const t=new Map;for(const n of e){const e=String(n?.url??"").trim();if(!e)continue;const o=String(n?.snippet??"").trim();let s=String(n?.title??"").trim();if(!s)try{s=new URL(e).hostname}catch{s=e}const i=t.get(e);i?(!i.snippet&&o&&(i.snippet=o),!i.title&&s&&(i.title=s)):t.set(e,{title:s,url:e,snippet:o})}return Array.from(t.values())}var Ke=class{config;constructor(e){this.config={maxIterations:10,...e}}async*chat(e,t,n,o){const s=Date.now(),i=this.config.maxIterations??10,a=this.buildMessages(n,t);let r=0,l="",c=[],u=0,p=!1,d=!1;for(;r<i;){if(n.signal.aborted)return void(yield L("请求已取消"));r++,xe.info(`======= 第 ${r} 轮开始 =======`);let s="",i=0,y=!1,g=!1;const m={};let h=!1;const f=e=>{m[e]=(m[e]||0)+1};try{const _=[];let b=!1;const k=!0===o.enableThinking,w=!0===o.enableSearch;xe.debug("调用 adapter.streamOnce",{enableThinking:k,enableSearch:w});const S=e.streamOnce(a,n.tools,{model:o.model,enableThinking:k,enableSearch:w,signal:n.signal});for await(const e of S)switch(f(e.type),e.type){case"text":e.text&&(h||(h=!0,xe.debug("首次收到 text",{thinkingStarted:y,thinkingComplete:g})),k&&y&&!g&&(xe.debug("text 触发 thinking_end"),g=!0,yield I(i)),l+=e.text,yield E(e.text));break;case"thinking":if(!k)break;if(e.thinking)if(g)xe.warn("⚠️ thinkingComplete=true 但收到 thinking",e.thinking.slice(0,30)),xe.warn("当前状态",{textStarted:h,chunkCounts:m});else if(h)xe.warn("⚠️ text 开始后收到 thinking",e.thinking.slice(0,30));else{let t=e.thinking;if(!y&&(t=t.replace(/^(?:\r?\n)+/,""),!t))break;y||(y=!0,i=Date.now(),xe.debug("发送 thinking_start"),yield K()),s+=t,yield N(t)}break;case"thinking_done":if(!k)break;xe.info("收到 thinking_done",{thinkingStarted:y,thinkingComplete:g}),y&&!g?(g=!0,xe.debug("发送 thinking_end"),yield I(i)):y||xe.warn("⚠️ 收到 thinking_done 但 thinkingStarted=false");break;case"tool_call":if(e.toolCall){if(!n.tools||0===n.tools.length){xe.warn("收到 tool_call 但当前未注入工具,已忽略",{toolName:e.toolCall.name});break}_.push(e.toolCall),b=!0}break;case"search_result":if(!w)break;e.searchResults&&(p||(p=!0,u=Date.now(),yield M(t)),c=Re(e.searchResults),yield q(c,u));break;case"done":xe.info("收到 done",{finishReason:e.finishReason}),xe.info(`第 ${r} 轮 chunk 统计`,m),"tool_calls"===e.finishReason&&(b=!0);break;case"error":return xe.error("收到 error",e.error),void(yield j(e.error??"未知错误"))}if(xe.info(`第 ${r} 轮 for-await 循环结束`),xe.debug("状态",{thinkingStarted:y,thinkingComplete:g}),k&&y&&!g&&(xe.debug("补发 thinking_end"),g=!0,yield I(i)),b&&_.length>0){const e={role:"assistant",content:l,toolCalls:_};a.push(e);for(const e of _){const s=Date.now();let i;try{i=JSON.parse(e.arguments||"{}")}catch{a.push({role:"tool",content:"参数解析错误",toolCallId:e.id});continue}if("web_search"===e.name){("string"==typeof i.query?i.query:"").trim()||(i.query=t)}const r=this.config.getAutoRunConfig?await this.config.getAutoRunConfig():o.autoRunConfig||this.config.autoRunConfig;if(xe.debug("检查工具批准",{toolName:e.name,autoRunConfigMode:r?.mode,hasCallback:!!this.config.onToolApprovalRequest}),"manual"===r?.mode&&this.config.onToolApprovalRequest){xe.info("发送工具批准请求",e.name);const n={type:"tool_approval_request",data:{id:e.id,name:e.name,args:i,requestedAt:Date.now()}};yield n;if(!await this.config.onToolApprovalRequest({id:e.id,name:e.name,args:i})){const n=JSON.stringify({skipped:!0,message:"用户跳过了此工具"});if("web_search"===e.name&&w){if(!p){p=!0,u=Date.now();const e="string"==typeof i.query?i.query:t;yield M(e)}d=!0,yield O(!1,u,"用户跳过了 web_search")}else yield D(e.id,e.name,n,!1,s);a.push({role:"tool",content:n,toolCallId:e.id,toolName:e.name});continue}}const l="web_search"===e.name;if(l){if(w&&!p){p=!0,u=Date.now();const e="string"==typeof i.query?i.query:t;yield M(e)}}else yield $(e.id,e.name,i);let y,g,m=!0;try{const t=[];let o=null;const s=()=>o?.(),a=e=>{t.push(e),s()};let r,l,c=!1;const u={toolCallId:e.id,toolName:e.name,onStdout:t=>{t&&a(U(e.id,e.name,"stdout",t))},onStderr:t=>{t&&a(U(e.id,e.name,"stderr",t))}};for(this.config.executeTool(e.name,i,n.signal,u).then(e=>{r=e,c=!0,s()}).catch(e=>{l=e,c=!0,s()});!c||t.length>0;){for(;t.length>0;){const e=t.shift();e&&(yield e)}if(c)break;await new Promise(e=>o=e),o=null}if(l)throw l;const p=r;"string"==typeof p?y=p:(y=p.result,g=p.sideEffects)}catch(e){m=!1;y=`错误: ${e instanceof Error?e.message:String(e)}`}const h=this.config.tools?.get(e.name),f=g??h?.sideEffects,_=m?h?.resultType:void 0;if(l){if(w){let e,t=[];try{const n=JSON.parse(y||"{}");t=(Array.isArray(n?.results)?n.results:[]).map(e=>({title:"string"==typeof e?.title?e.title:"",url:"string"==typeof e?.url?e.url:"",snippet:"string"==typeof e?.snippet?e.snippet:""})).filter(e=>!!e.url),"string"==typeof n?.error&&n.error&&(e=n.error,m=!1)}catch{e="web_search 返回结果解析失败",m=!1}t.length>0&&(c=Re(t),yield q(c,u)),d=!0,yield O(m,u,e)}}else yield D(e.id,e.name,y,m,s,void 0,f,_);if(a.push({role:"tool",content:y,toolCallId:e.id,toolName:e.name}),n.signal.aborted)return void(yield L("请求已取消"))}l="";continue}break}catch(e){return void(n.signal.aborted?yield L("请求已取消"):yield j(e instanceof Error?e.message:String(e)))}}p&&!d&&(yield O(!0,u));const y=Date.now()-s;yield J(l,void 0,y)}buildMessages(e,t){const n=[];e.systemPrompt&&n.push({role:"system",content:e.systemPrompt});for(const t of e.history){const e={role:t.role,content:t.content};t.tool_calls&&(e.toolCalls=t.tool_calls.map(e=>({id:e.id,name:e.function.name,arguments:e.function.arguments,thought_signature:e.thought_signature}))),n.push(e)}return n.push({role:"user",content:t,images:e.images}),n}};function Ne(e){return new Ke(e)}var Ie=Te.module("ArkProtocol"),Me=class{name="ark";apiKey;apiUrl;constructor(e){this.apiKey=e.apiKey,this.apiUrl=e.apiUrl??ue}async*stream(e,t,n){const o=this.buildRequestBody(e,t,n);Ie.debug("发送 ARK 请求",{url:`${this.apiUrl}/responses`,model:n.model,enableSearch:n.enableSearch,enableThinking:n.enableThinking,toolsCount:t.length});const s=await fetch(`${this.apiUrl}/responses`,{method:"POST",headers:{Authorization:`Bearer ${this.apiKey}`,"Content-Type":"application/json"},body:JSON.stringify(o),signal:n.signal});if(!s.ok){const e=await s.text();return Ie.error("ARK API 错误",{status:s.status,body:e.slice(0,500)}),void(yield{type:"error",error:`ARK API 错误: ${s.status} - ${e}`})}const i=s.body?.getReader();i?yield*this.parseSSE(i):yield{type:"error",error:"无法获取响应流"}}buildRequestBody(e,t,n){const o=this.convertMessages(e),s={model:n.model,stream:!0,max_output_tokens:n.familyConfig.defaultMaxTokens??32768,input:o},i=[];n.enableSearch&&i.push({type:"web_search",max_keyword:5,limit:20});for(const e of t)i.push({type:"function",name:e.name,description:e.description,parameters:e.parameters});return i.length>0&&(s.tools=i),n.enableThinking&&(s.thinking={type:"enabled"}),s}convertMessages(e){const t=[];for(const n of e)switch(n.role){case"system":t.push({role:"system",content:n.content});break;case"user":{const e=[{type:"input_text",text:n.content||(n.images?.length?"请分析这张图片":"")}];if(n.images?.length)for(const t of n.images)e.push({type:"input_image",image_url:t.startsWith("data:")?t:`data:image/jpeg;base64,${t}`});t.push({role:"user",content:e});break}case"assistant":if(n.toolCalls?.length)for(const e of n.toolCalls)t.push({type:"function_call",call_id:e.id,name:e.name,arguments:e.arguments});else t.push({role:"developer",content:`[上一轮AI回复]: ${n.content}`});break;case"tool":t.push({type:"function_call_output",call_id:n.toolCallId,output:n.content})}return t}async*parseSSE(e){const t=new TextDecoder;let n="";const o=new Map;let s=null;const i=[];let a=!1,r=!1,l=!1;for(;;){const{done:c,value:u}=await e.read();if(c)break;n+=t.decode(u,{stream:!0});const p=n.split("\n");n=p.pop()||"";for(const e of p){if(a)continue;if(!e.startsWith("data:"))continue;const t=e.slice(5).trim();if("[DONE]"===t){if(a=!0,o.size>0){for(const e of o.values())yield{type:"tool_call_done",toolCall:e};yield{type:"done",finishReason:"tool_calls"}}else yield{type:"done",finishReason:"stop"};return}try{const e=JSON.parse(t);switch(e.type){case"response.output_item.added":{const t=e.item;"function_call"===t?.type&&t.call_id&&(s=t.call_id,o.set(t.call_id,{id:t.call_id,name:t.name||"",arguments:t.arguments||""}),yield{type:"tool_call_start",toolCall:{id:t.call_id,name:t.name||""}});break}case"response.function_call_arguments.delta":if(s){const t=o.get(s);t&&(t.arguments+=e.delta||"",yield{type:"tool_call_delta",toolCall:{id:s,arguments:e.delta||""}})}break;case"response.function_call_arguments.done":case"response.output_item.done":{const t=e.item;if("function_call"===t?.type&&t.call_id){const e=o.get(t.call_id);o.set(t.call_id,{id:t.call_id,name:t.name||e?.name||"",arguments:t.arguments||e?.arguments||"{}"})}break}case"response.output_text.annotation.added":{const t=e.annotation;if(t?.url){i.some(e=>e.url===t.url)||(i.push({title:t.title||t.text||"",url:t.url,snippet:t.summary||t.snippet||""}),yield{type:"search_result",searchResults:[...i]})}break}case"response.output_text.delta":e.delta&&(l||(l=!0,r||(r=!0,yield{type:"thinking_done"})),yield{type:"text_delta",delta:e.delta});break;case"response.reasoning_summary_text.delta":e.delta&&!r&&(yield{type:"thinking_delta",delta:e.delta})}}catch{}}}if(!a)if(o.size>0){for(const e of o.values())yield{type:"tool_call_done",toolCall:e};yield{type:"done",finishReason:"tool_calls"}}else yield{type:"done",finishReason:"stop"}}};function qe(e){return new Me(e)}var Oe=Te.module("DeepSeekProtocol"),$e=class{name="deepseek";apiKey;apiUrl;constructor(e){this.apiKey=e.apiKey,this.apiUrl=e.apiUrl??ue}async*stream(e,t,n){const o=this.buildRequestBody(e,t,n),s=`${this.apiUrl}/responses`;Oe.debug("发送 DeepSeek 请求",{url:s,model:n.model,enableSearch:n.enableSearch,enableThinking:n.enableThinking,toolsCount:t.length});const i=await fetch(s,{method:"POST",headers:{Authorization:`Bearer ${this.apiKey}`,"Content-Type":"application/json"},body:JSON.stringify(o),signal:n.signal});if(!i.ok){const e=await i.text();return Oe.error("DeepSeek API 错误",{status:i.status,body:e.slice(0,500)}),void(yield{type:"error",error:`DeepSeek API 错误: ${i.status} - ${e}`})}const a=i.body?.getReader();a?yield*this.parseSSE(a,n.enableThinking):yield{type:"error",error:"无法获取响应流"}}buildRequestBody(e,t,n){const o=this.convertMessages(e),s={model:n.model,stream:!0,max_output_tokens:n.familyConfig.defaultMaxTokens??32768,input:o},i=[];n.enableSearch&&i.push({type:"web_search",max_keyword:5,limit:20});for(const e of t)i.push({type:"function",name:e.name,description:e.description,parameters:e.parameters});return i.length>0&&(s.tools=i),n.enableThinking&&(s.thinking={type:"enabled"}),s}convertMessages(e){const t=[];for(const n of e)switch(n.role){case"system":t.push({role:"system",content:n.content});break;case"user":{const e=[{type:"input_text",text:n.content||(n.images?.length?"请分析这张图片":"")}];if(n.images?.length)for(const t of n.images)e.push({type:"input_image",image_url:t.startsWith("data:")?t:`data:image/jpeg;base64,${t}`});t.push({role:"user",content:e});break}case"assistant":if(n.toolCalls?.length)for(const e of n.toolCalls)t.push({type:"function_call",call_id:e.id,name:e.name,arguments:e.arguments});else t.push({role:"developer",content:`[上一轮AI回复]: ${n.content}`});break;case"tool":t.push({type:"function_call_output",call_id:n.toolCallId,output:n.content})}return t}async*parseSSE(e,t){const n=new TextDecoder;let o="";const s=new Map;let i=null;const a=[];let r=!1,l=!1,c=!1;for(;;){const{done:u,value:p}=await e.read();if(u)break;o+=n.decode(p,{stream:!0});const d=o.split("\n");o=d.pop()||"";for(const e of d){if(r)continue;if(!e.startsWith("data:"))continue;const n=e.slice(5).trim();if("[DONE]"===n){if(r=!0,s.size>0){for(const e of s.values())yield{type:"tool_call_done",toolCall:e};yield{type:"done",finishReason:"tool_calls"}}else yield{type:"done",finishReason:"stop"};return}try{const e=JSON.parse(n);switch(e.type){case"response.output_item.added":{const t=e.item;"function_call"===t?.type&&t.call_id&&(i=t.call_id,s.set(t.call_id,{id:t.call_id,name:t.name||"",arguments:t.arguments||""}),yield{type:"tool_call_start",toolCall:{id:t.call_id,name:t.name||""}});break}case"response.function_call_arguments.delta":if(i){const t=s.get(i);t&&(t.arguments+=e.delta||"",yield{type:"tool_call_delta",toolCall:{id:i,arguments:e.delta||""}})}break;case"response.function_call_arguments.done":case"response.output_item.done":{const t=e.item;if("function_call"===t?.type&&t.call_id){const e=s.get(t.call_id);s.set(t.call_id,{id:t.call_id,name:t.name||e?.name||"",arguments:t.arguments||e?.arguments||"{}"})}break}case"response.output_text.annotation.added":{const t=e.annotation;if(t?.url){a.some(e=>e.url===t.url)||(a.push({title:t.title||t.text||"",url:t.url,snippet:t.summary||t.snippet||""}),yield{type:"search_result",searchResults:[...a]})}break}case"response.output_text.delta":e.delta&&(c||(c=!0,t&&!l&&(l=!0,yield{type:"thinking_done"})),yield{type:"text_delta",delta:e.delta});break;case"response.reasoning_summary_text.delta":t&&e.delta&&!l&&(yield{type:"thinking_delta",delta:e.delta})}}catch{}}}if(!r)if(s.size>0){for(const e of s.values())yield{type:"tool_call_done",toolCall:e};yield{type:"done",finishReason:"tool_calls"}}else yield{type:"done",finishReason:"stop"}}};function De(e){return new $e(e)}var Ue=Te.module("QwenProtocol"),Ee=class{name="qwen";apiKey;apiUrl;constructor(e){this.apiKey=e.apiKey,this.apiUrl=e.apiUrl??"https://dashscope.aliyuncs.com/compatible-mode/v1"}async*stream(e,t,n){const o=this.buildRequestBody(e,t,n),s=`${this.apiUrl}/chat/completions`;Ue.debug("发送 Qwen 请求(兼容模式)",{url:s,model:n.model,enableThinking:n.enableThinking,toolsCount:t.length});const i=await fetch(s,{method:"POST",headers:{Authorization:`Bearer ${this.apiKey}`,"Content-Type":"application/json"},body:JSON.stringify(o),signal:n.signal});if(!i.ok){const e=await i.text();return Ue.error("Qwen API 错误",{status:i.status,body:e.slice(0,500)}),void(yield{type:"error",error:`Qwen API 错误: ${i.status} - ${e}`})}const a=i.body?.getReader();a?yield*this.parseSSE(a,n.enableThinking):yield{type:"error",error:"无法获取响应流"}}buildRequestBody(e,t,n){const o=this.convertMessages(e),s={model:n.model,messages:o,stream:!0};return n.enableThinking&&(s.enable_thinking=!0,s.thinking_budget=38400),t.length>0&&(s.tools=t.map(e=>({type:"function",function:{name:e.name,description:e.description,parameters:e.parameters}}))),s}convertMessages(e){const t=[];for(const n of e)switch(n.role){case"system":t.push({role:"system",content:n.content});break;case"user":{const e=n.content||(n.images?.length?"请分析这张图片":"");if(n.images?.length){const o=[{type:"text",text:e}];for(const e of n.images)o.push({type:"image_url",image_url:{url:e.startsWith("data:")?e:`data:image/jpeg;base64,${e}`}});t.push({role:"user",content:o})}else t.push({role:"user",content:e});break}case"assistant":n.toolCalls?.length?t.push({role:"assistant",content:n.content||null,tool_calls:n.toolCalls.map(e=>({id:e.id,type:"function",function:{name:e.name,arguments:e.arguments}}))}):t.push({role:"assistant",content:n.content});break;case"tool":t.push({role:"tool",tool_call_id:n.toolCallId,content:n.content})}return t}async*parseSSE(e,t){const n=new TextDecoder;let o="";const s=new Map;let i=!1,a=!1;for(;;){const{done:r,value:l}=await e.read();if(r)break;o+=n.decode(l,{stream:!0});const c=o.split("\n");o=c.pop()||"";for(const e of c){if(!e.startsWith("data:"))continue;const n=e.slice(5).trim();if(n&&"[DONE]"!==n)try{const e=JSON.parse(n),o=e.choices?.[0];if(!o)continue;const r=o.delta;if(!r)continue;if(t&&r.reasoning_content&&!a&&(yield{type:"thinking_delta",delta:r.reasoning_content}),r.content&&(!t||i||a||(a=!0,yield{type:"thinking_done"}),i=!0,yield{type:"text_delta",delta:r.content}),r.tool_calls?.length)for(const e of r.tool_calls){const t=e.index??0,n=s.get(t);n?e.function?.arguments&&(n.arguments+=e.function.arguments):(s.set(t,{id:e.id||`call_${t}`,name:e.function?.name||"",arguments:e.function?.arguments||""}),yield{type:"tool_call_start",toolCall:{id:e.id||`call_${t}`,name:e.function?.name||""}})}if(o.finish_reason){const e=Array.from(s.values());if(e.length>0){for(const t of e)yield{type:"tool_call_done",toolCall:t};yield{type:"done",finishReason:"tool_calls"}}else yield{type:"done",finishReason:o.finish_reason};return}}catch{}else if("[DONE]"===n){const e=Array.from(s.values());if(e.length>0){for(const t of e)yield{type:"tool_call_done",toolCall:t};yield{type:"done",finishReason:"tool_calls"}}else yield{type:"done",finishReason:"stop"};return}}}const r=Array.from(s.values());if(r.length>0){for(const e of r)yield{type:"tool_call_done",toolCall:e};yield{type:"done",finishReason:"tool_calls"}}else yield{type:"done",finishReason:"stop"}}};function Pe(e){return new Ee(e)}var Je=Te.module("GeminiProtocol"),We=class{name="gemini";apiKey;apiUrl;constructor(e){this.apiKey=e.apiKey,this.apiUrl=e.apiUrl??"https://generativelanguage.googleapis.com/v1beta"}async*stream(e,t,n){const o=this.buildRequestBody(e,t,n),s=`${this.apiUrl}/models/${n.model}:streamGenerateContent?key=${this.apiKey}&alt=sse`;Je.debug("发送 Gemini 请求",{url:s.replace(this.apiKey,"***"),model:n.model,enableSearch:n.enableSearch,enableThinking:n.enableThinking,toolsCount:t.length});const i=await fetch(s,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(o),signal:n.signal});if(!i.ok){const e=await i.text();return Je.error("Gemini API 错误",{status:i.status,body:e.slice(0,500)}),void(yield{type:"error",error:`Gemini API 错误: ${i.status} - ${e}`})}const a=i.body?.getReader();a?yield*this.parseSSE(a):yield{type:"error",error:"无法获取响应流"}}buildRequestBody(e,t,n){const{systemInstruction:o,contents:s}=this.convertMessages(e),i={contents:s,generationConfig:{maxOutputTokens:n.familyConfig.defaultMaxTokens??65536}};o&&(i.systemInstruction=o),n.enableThinking&&(i.generationConfig.thinkingConfig={thinkingBudget:24576,includeThoughts:!0});const a=[];return t.length>0?a.push({functionDeclarations:t.map(e=>({name:e.name,description:e.description,parameters:e.parameters}))}):n.enableSearch&&a.push({googleSearch:{}}),a.length>0&&(i.tools=a),i}convertMessages(e){let t;const n=[];for(const o of e)switch(o.role){case"system":t={parts:[{text:o.content}]};break;case"user":{const e=[{text:o.content||(o.images?.length?"请分析这张图片":"")}];if(o.images?.length)for(const t of o.images)if(t.startsWith("data:")){const n=t.match(/^data:([^;]+);base64,(.+)$/);n&&e.push({inlineData:{mimeType:n[1],data:n[2]}})}else e.push({inlineData:{mimeType:"image/jpeg",data:t}});n.push({role:"user",parts:e});break}case"assistant":if(o.toolCalls?.length){const e=[];for(const t of o.toolCalls){const n={functionCall:{name:t.name,args:JSON.parse(t.arguments||"{}")}};t.thoughtSignature&&(n.thoughtSignature=t.thoughtSignature),e.push(n)}n.push({role:"model",parts:e})}else n.push({role:"model",parts:[{text:o.content}]});break;case"tool":n.push({role:"user",parts:[{functionResponse:{name:o.toolName||"unknown",response:{result:o.content}}}]})}return{systemInstruction:t,contents:n}}async*parseSSE(e){const t=new TextDecoder;let n="";const o=new Map;let s=!1,i=0;for(;;){const{done:a,value:r}=await e.read();if(a)break;n+=t.decode(r,{stream:!0});const l=n.split("\n");n=l.pop()||"";for(const e of l){if(!e.startsWith("data:"))continue;const t=e.slice(5).trim();if(t)try{const e=JSON.parse(t),n=e.candidates?.[0];if(!n?.content?.parts)continue;for(const e of n.content.parts)if(e.text&&(!0===e.thought?yield{type:"thinking_delta",delta:e.text}:(s||(s=!0,yield{type:"thinking_done"}),yield{type:"text_delta",delta:e.text})),e.functionCall){const t="gemini-"+i++,n={id:t,name:e.functionCall.name,arguments:JSON.stringify(e.functionCall.args||{})};e.thoughtSignature&&(n.thoughtSignature=e.thoughtSignature),o.set(t,n),yield{type:"tool_call_start",toolCall:{id:t,name:n.name}},yield{type:"tool_call_done",toolCall:n}}const a=n.groundingMetadata;if(a?.groundingChunks?.length){const e=[];for(const t of a.groundingChunks)t.web?.uri&&e.push({title:t.web.title||"",url:t.web.uri,snippet:""});e.length>0&&(yield{type:"search_result",searchResults:e})}if(n.finishReason)return void(o.size>0?yield{type:"done",finishReason:"tool_calls"}:yield{type:"done",finishReason:"stop"})}catch{}}}o.size>0?yield{type:"done",finishReason:"tool_calls"}:yield{type:"done",finishReason:"stop"}}};function je(e){return new We(e)}var Fe=Te.module("OpenAIProtocol"),Ge=class{name="openai";apiKey;apiUrl;constructor(e){this.apiKey=e.apiKey,this.apiUrl=e.apiUrl??pe}async*stream(e,t,n){const o=this.buildRequestBody(e,t,n),s=`${this.apiUrl}/responses`;Fe.debug("发送 OpenAI 请求",{url:s,model:n.model,toolsCount:t.length});const i=await fetch(s,{method:"POST",headers:{Authorization:`Bearer ${this.apiKey}`,"Content-Type":"application/json","HTTP-Referer":"https://ai-chat.local","X-Title":"AI Chat"},body:JSON.stringify(o),signal:n.signal});if(!i.ok){const e=await i.text();return Fe.error("OpenAI API 错误",{status:i.status,body:e.slice(0,500)}),void(yield{type:"error",error:`OpenAI API 错误: ${i.status} - ${e}`})}const a=i.body?.getReader();a?yield*this.parseSSE(a,n.enableThinking):yield{type:"error",error:"无法获取响应流"}}buildRequestBody(e,t,n){const o=this.convertMessages(e),s={model:n.model,stream:!0,input:o};return n.enableThinking&&(s.reasoning={effort:"high"}),t.length>0&&(s.tools=t.map(e=>({type:"function",name:e.name,description:e.description,parameters:e.parameters}))),s}convertMessages(e){const t=[];for(const n of e)switch(n.role){case"system":t.push({role:"system",content:n.content});break;case"user":{const e=[{type:"input_text",text:n.content||(n.images?.length?"请分析这张图片":"")}];if(n.images?.length)for(const t of n.images)e.push({type:"input_image",image_url:t.startsWith("data:")?t:`data:image/jpeg;base64,${t}`,detail:"auto"});t.push({role:"user",content:e});break}case"assistant":if(n.toolCalls?.length)for(const e of n.toolCalls)t.push({type:"function_call",call_id:e.id,name:e.name,arguments:e.arguments});else t.push({role:"developer",content:`[上一轮AI回复]: ${n.content}`});break;case"tool":t.push({type:"function_call_output",call_id:n.toolCallId,output:n.content})}return t}async*parseSSE(e,t){const n=new TextDecoder;let o="";const s=new Map;let i=null;const a=[];let r=!1,l=!1,c=!1;for(;;){const{done:u,value:p}=await e.read();if(u)break;o+=n.decode(p,{stream:!0});const d=o.split("\n");o=d.pop()||"";for(const e of d){if(r)continue;if(!e.startsWith("data:"))continue;const n=e.slice(5).trim();if("[DONE]"===n){if(r=!0,s.size>0){for(const e of s.values())yield{type:"tool_call_done",toolCall:e};yield{type:"done",finishReason:"tool_calls"}}else yield{type:"done",finishReason:"stop"};return}try{const e=JSON.parse(n);switch(Fe.debug("SSE 事件",{type:e.type,event:JSON.stringify(e).slice(0,200)}),e.type){case"response.output_item.added":{const t=e.item,n=t?.call_id||t?.id;if("function_call"===t?.type&&n){i=n;let e=t.arguments||t.input||"";"object"==typeof e&&(e=JSON.stringify(e)),s.set(n,{id:n,name:t.name||t.function?.name||"",arguments:e}),Fe.debug("工具调用开始",{id:n,name:t.name,args:e.slice(0,100)}),yield{type:"tool_call_start",toolCall:{id:n,name:t.name||t.function?.name||""}}}break}case"response.function_call_arguments.delta":{const t=e.delta;if(i&&t){const e=s.get(i);e&&(e.arguments+=t,yield{type:"tool_call_delta",toolCall:{id:i,arguments:t}})}break}case"response.function_call_arguments.done":{const t=e.item_id||e.call_id||i;if(t){const n=s.get(t);if(n){e.arguments&&(n.arguments=e.arguments);try{JSON.parse(n.arguments)}catch{Fe.warn("工具参数解析失败,使用空对象",{args:n.arguments.slice(0,100)}),n.arguments="{}"}}}break}case"response.output_item.done":{const t=e.item,n=t?.call_id||t?.id;if("function_call"===t?.type&&n){const e=s.get(n);let o=t.arguments||t.input||e?.arguments||"{}";"object"==typeof o&&(o=JSON.stringify(o)),s.set(n,{id:n,name:t.name||t.function?.name||e?.name||"",arguments:o}),Fe.debug("工具调用完成",{id:n,name:t.name,args:o.slice(0,100)})}break}case"response.output_text.annotation.added":{const t=e.annotation;if(t?.url){a.some(e=>e.url===t.url)||(a.push({title:t.title||t.text||"",url:t.url,snippet:t.summary||t.snippet||""}),yield{type:"search_result",searchResults:[...a]})}break}case"response.output_text.delta":e.delta&&(c||(c=!0,t&&!l&&(l=!0,yield{type:"thinking_done"})),yield{type:"text_delta",delta:e.delta});break;case"response.reasoning_summary_text.delta":t&&e.delta&&!l&&(yield{type:"thinking_delta",delta:e.delta});break;case"response.completed":{const t=e.response;if(t?.output?.length)for(const e of t.output)if("function_call"===e.type&&e.call_id){const t=s.get(e.call_id);let n=e.arguments||t?.arguments||"{}";"object"==typeof n&&(n=JSON.stringify(n)),s.set(e.call_id,{id:e.call_id,name:e.name||t?.name||"",arguments:n}),Fe.debug("response.completed: 更新工具参数",{id:e.call_id,args:n.slice(0,100)})}if(r=!0,s.size>0){for(const e of s.values())Fe.debug("response.completed: 发出 tool_call_done",e),yield{type:"tool_call_done",toolCall:e};yield{type:"done",finishReason:"tool_calls"}}else yield{type:"done",finishReason:"stop"};return}}}catch(t){Fe.warn("SSE 解析错误",{line:e.slice(0,100),error:String(t)})}}}if(!r)if(Fe.debug("流结束,执行兜底逻辑",{pendingToolCalls:s.size}),s.size>0){for(const e of s.values())yield{type:"tool_call_done",toolCall:e};yield{type:"done",finishReason:"tool_calls"}}else yield{type:"done",finishReason:"stop"}}};function Ve(e){return new Ge(e)}var Be=Te.module("AnthropicProtocol"),Le=class{name="anthropic";gateway;constructor(e){this.gateway=t({apiKey:e.apiKey})}async*stream(e,t,o){Be.debug("使用 Vercel AI Gateway 调用 Claude",{model:o.model,enableThinking:o.enableThinking,toolsCount:t.length});try{const s=this.convertMessages(e),i={model:this.gateway(o.model),messages:s,maxOutputTokens:o.familyConfig.defaultMaxTokens,abortSignal:o.signal};t.length>0&&(i.tools=this.convertTools(t)),o.enableThinking&&(i.providerOptions={anthropic:{thinking:{type:"enabled",budgetTokens:1e4}}}),Be.debug("发送请求到 Vercel AI Gateway",{model:o.model,messagesCount:s.length,toolsCount:t.length,enableThinking:o.enableThinking});const a=n(i);yield*this.parseStream(a)}catch(e){const t=e instanceof Error?e.message:"Unknown error";Be.error("Vercel AI Gateway 错误",{error:t}),yield{type:"error",error:t}}}convertMessages(e){const t=[];for(const n of e)switch(n.role){case"system":t.push({role:"system",content:n.content});break;case"user":{const e=n.content||(n.images?.length?"请分析这张图片":"");if(n.images?.length){const o=[{type:"text",text:e}];for(const e of n.images)o.push({type:"image",image:e.startsWith("data:")?e:`data:image/jpeg;base64,${e}`});t.push({role:"user",content:o})}else t.push({role:"user",content:e});break}case"assistant":n.content&&t.push({role:"assistant",content:n.content});break;case"tool":t.push({role:"user",content:`[工具 ${n.toolName||"unknown"} 返回结果]: ${n.content}`})}return t}convertTools(e){const t={};for(const n of e){const e=this.jsonSchemaToZod(n.parameters);t[n.name]=o({description:n.description,inputSchema:e})}return t}jsonSchemaToZod(e){const t={};for(const[n,o]of Object.entries(e.properties)){let i;switch(o.type){case"string":i=o.enum?s.enum(o.enum):s.string();break;case"number":case"integer":i=s.number();break;case"boolean":i=s.boolean();break;case"array":i=s.array(s.string());break;default:i=s.unknown()}o.description&&(i=i.describe(o.description)),e.required.includes(n)||(i=i.optional()),t[n]=i}return s.object(t)}async*parseStream(e){const t=new Map;let n=!1,o=!1;try{for await(const s of e.fullStream)switch(s.type){case"reasoning-delta":"text"in s&&s.text&&(n=!0,yield{type:"thinking_delta",delta:s.text});break;case"reasoning-end":n&&!o&&(o=!0,yield{type:"thinking_done"});break;case"text-delta":n&&!o&&(o=!0,yield{type:"thinking_done"}),"text"in s&&s.text&&(yield{type:"text_delta",delta:s.text});break;case"tool-call":if("toolCallId"in s&&"toolName"in s){const e=s.toolCallId,n=s.toolName,o="input"in s?s.input:null,i=o?"string"==typeof o?o:JSON.stringify(o):"{}";yield{type:"tool_call_start",toolCall:{id:e,name:n}},t.set(e,{id:e,name:n,arguments:i}),yield{type:"tool_call_done",toolCall:{id:e,name:n,arguments:i}}}break;case"finish":const e=t.size>0?"tool_calls":"finishReason"in s&&"stop"===s.finishReason?"stop":"finishReason"in s&&"length"===s.finishReason?"length":"stop";return void(yield{type:"done",finishReason:e})}yield{type:"done",finishReason:"stop"}}catch(e){const t=e instanceof Error?e.message:"Stream error";Be.error("流式处理错误",{error:t}),yield{type:"error",error:t}}}};function ze(e){return new Le(e)}var He=Te.module("UnifiedAdapter"),Qe=class{name="unified";get supportedModels(){return h().map(e=>e.id)}protocols=new Map;constructor(e){e.arkApiKey&&(this.protocols.set("ark",qe({apiKey:e.arkApiKey,apiUrl:e.arkApiUrl})),this.protocols.set("deepseek",De({apiKey:e.arkApiKey,apiUrl:e.arkApiUrl}))),e.qwenApiKey&&this.protocols.set("qwen",Pe({apiKey:e.qwenApiKey,apiUrl:e.qwenApiUrl})),e.geminiApiKey&&this.protocols.set("gemini",je({apiKey:e.geminiApiKey,apiUrl:e.geminiApiUrl})),e.openrouterApiKey&&this.protocols.set("openai",Ve({apiKey:e.openrouterApiKey,apiUrl:e.openrouterApiUrl})),e.vercelApiKey&&this.protocols.set("anthropic",ze({apiKey:e.vercelApiKey}))}supportsModel(e){const t=m(e);return!!t&&this.protocols.has(t)}getModelFamilyConfig(e){return g(e)}async*streamOnce(e,t,n){const{model:o,enableThinking:s=!1,enableSearch:i=!1,signal:a}=n,r=e.map(e=>({role:e.role,content:e.content,images:e.images,toolCalls:e.toolCalls?.map(e=>({id:e.id,name:e.name,arguments:e.arguments,thoughtSignature:e.thought_signature})),toolCallId:e.toolCallId,toolName:e.toolName})),l=t;yield*this.stream(r,l,{model:o,enableThinking:s,enableSearch:i,signal:a})}async*stream(e,t,n){const{model:o,enableThinking:s=!1,enableSearch:i=!1,signal:a}=n,r=m(o),l=g(o);if(!r)return void(yield{type:"error",error:`未知模型: ${o}`});if(!l)return void(yield{type:"error",error:`模型 ${o} 缺少家族配置`});const c=this.protocols.get(r);if(!c)return void(yield{type:"error",error:`Protocol ${r} 未配置`});He.debug("开始流式调用",{model:o,protocol:r,family:l.id,enableThinking:s,enableSearch:i});const u=c.stream(e,t,{model:o,familyConfig:l,enableThinking:s,enableSearch:i,signal:a});yield*this.transformEvents(u,l,s,i)}async*transformEvents(e,t,n,o){let s=!1;for await(const i of e)switch(i.type){case"thinking_delta":if(n&&t.supportsThinking&&i.delta){let e=i.delta;s||(e=e.replace(/^\n+/,""),e&&(s=!0)),e&&(yield{type:"thinking",thinking:e})}break;case"thinking_done":n&&t.supportsThinking&&(yield{type:"thinking_done"});break;case"text_delta":i.delta&&(yield{type:"text",text:i.delta});break;case"tool_call_done":if(i.toolCall){const e={id:i.toolCall.id||"",name:i.toolCall.name||"",arguments:i.toolCall.arguments||"{}"};i.toolCall.thoughtSignature&&(e.thought_signature=i.toolCall.thoughtSignature),yield{type:"tool_call",toolCall:e}}break;case"search_result":if(o&&i.searchResults){const e=i.searchResults.map(e=>({title:e.title,url:e.url,snippet:e.snippet}));yield{type:"search_result",searchResults:e}}break;case"done":yield{type:"done",finishReason:i.finishReason||"stop"};break;case"error":yield{type:"error",error:i.error||"未知错误"}}}};function Ze(e){return new Qe(e)}var Xe=class{config;adapter;orchestrator;geminiClient;toolExecutor;abortController=null;tools=new Map;toolConfig;constructor(t,n){this.config={arkApiKey:t.arkApiKey,arkApiUrl:t.arkApiUrl||ue,qwenApiKey:t.qwenApiKey||"",qwenApiUrl:t.qwenApiUrl||"https://dashscope.aliyuncs.com/compatible-mode/v1",openrouterApiKey:t.openrouterApiKey||"",openrouterApiUrl:t.openrouterApiUrl||pe,vercelApiKey:t.vercelApiKey||"",tavilyApiKey:t.tavilyApiKey||"",geminiApiKey:t.geminiApiKey,cwd:t.cwd||process.cwd()},this.geminiClient=new e({apiKey:this.config.geminiApiKey}),this.toolExecutor=n||ce(this.config.cwd),this.toolConfig=t.tools,this.tools=new Map,this.adapter=new Qe({arkApiKey:this.config.arkApiKey,arkApiUrl:this.config.arkApiUrl,qwenApiKey:this.config.qwenApiKey,qwenApiUrl:this.config.qwenApiUrl,geminiApiKey:this.config.geminiApiKey,openrouterApiKey:this.config.openrouterApiKey,openrouterApiUrl:this.config.openrouterApiUrl,vercelApiKey:this.config.vercelApiKey}),this.orchestrator=new Ke({maxIterations:10,executeTool:this.executeTool.bind(this),tools:this.tools,onToolApprovalRequest:t.onToolApprovalRequest,getAutoRunConfig:t.getAutoRunConfig})}async asyncInit(){if(this.toolConfig&&0===this.tools.size){const e=await R(this.toolConfig);for(const t of e)this.tools.set(t.name,t)}var e;this.config.tavilyApiKey&&!this.tools.has("web_search")&&this.tools.set("web_search",(e=this.config.tavilyApiKey,{name:"web_search",description:"联网搜索工具。输入 query(搜索关键词/问题),返回搜索结果列表(title/url/snippet)。用于获取实时信息与可引用来源。",parameters:{type:"object",properties:{query:{type:"string",description:"搜索关键词或问题(必填)"},max_results:{type:"number",description:"最大返回结果数(可选,默认 5)"}},required:["query"]},resultType:"search_results",execute:async(t,n)=>{const o="string"==typeof t.query?t.query:"",s="number"==typeof t.max_results&&Number.isFinite(t.max_results)?Math.max(1,Math.min(10,Math.floor(t.max_results))):5;if(!o.trim())return JSON.stringify({query:"",results:[],error:"缺少 query"});if(!e)return JSON.stringify({query:o,results:[],error:"缺少 Tavily API Key"});const i=await fetch("https://api.tavily.com/search",{method:"POST",headers:{Authorization:`Bearer ${e}`,"Content-Type":"application/json"},body:JSON.stringify({query:o,max_results:s,search_depth:"basic",include_answer:!1,include_raw_content:!1}),signal:n.signal});if(!i.ok){const e=await i.text().catch(()=>"");return JSON.stringify({query:o,results:[],error:`Tavily /search 错误: ${i.status} ${e}`.trim()})}const a=await i.json().catch(()=>null),r=[],l=a&&"object"==typeof a&&Array.isArray(a.results)?a.results:[];for(const e of l){const t="string"==typeof e?.url?e.url:"";if(!t)continue;const n="string"==typeof e?.title?e.title:"",o="string"==typeof e?.content?e.content:"";r.push({title:n,url:t,snippet:o})}return JSON.stringify({query:o,results:r})}}))}getModelFamilyConfig(e){return g(e)}getModelProvider(e){return ye(e)}getModelRouteInfo(e){return{...ge(e),available:this.adapter.supportsModel(e),familyConfig:g(e)}}buildSystemPrompt(e){const t=e.model||de,n=g(t);let o="你是一个专业的 AI 助手。请遵循以下规则:\n- 返回的内容尽量不要使用表情符号(emoji),除非用户要求使用\n- 使用 Markdown 格式组织内容\n- 代码块使用正确的语言标识";return e.enableWebSearch&&n&&!k(t)&&(o+="\n\n【联网搜索】当用户问题需要实时信息/最新事实/可引用来源时,请先调用 web_search 工具获取结果,然后基于返回的 title/url/snippet 作答,并在回答中给出来源链接。"),o}getDefaultThinkingMode(){return"disabled"}createToolContext(e,t){return{cwd:this.config.cwd,geminiClient:this.geminiClient,executeCommand:(n,o)=>this.toolExecutor.executeCommand(n,o||this.config.cwd,e,{onStdout:t?.onStdout,onStderr:t?.onStderr}),signal:e}}async executeTool(e,t,n,o){const s=this.tools.get(e);return s?await s.execute(t,this.createToolContext(n,o)):`未知工具: ${e}`}getToolDefinitions(e,t){const n=Array.from(this.tools.values());let o=void 0!==e?n.filter(t=>e.includes(t.name)):n;if(t?.length)for(const e of t)if(!o.some(t=>t.name===e)){const t=this.tools.get(e);t&&(o=[...o,t])}return o.map(e=>({name:e.name,description:e.description,parameters:e.parameters}))}getAllTools(){return Array.from(this.tools.values()).map(e=>({name:e.name,description:e.description}))}async*chat(e,t={},n){await this.asyncInit(),this.abortController=new AbortController;const o=this.abortController.signal,s=t.model||de;if(!this.adapter.supportsModel(s)){return void(g(s)?yield j(`模型 ${s} 缺少 API Key 配置`,{code:"MISSING_API_KEY"}):yield j(`未知模型: ${s}`,{code:"MODEL_NOT_SUPPORTED"}))}const i=g(s),a="enabled"===(t.thinkingMode??this.getDefaultThinkingMode())?"enabled":"disabled",r=this.buildSystemPrompt(t),l="ask"===t.mode,c=!l&&!!t.enableWebSearch,u=c&&i&&!k(s)?["web_search"]:void 0,p=l?[]:this.getToolDefinitions(t.enabledTools,u),d=!l&&"enabled"===a&&(i?.supportsThinking??!1),y={systemPrompt:r,history:t.history||[],tools:p,signal:o,images:n};try{yield*this.orchestrator.chat(this.adapter,e,y,{model:s,enableThinking:d,enableSearch:c,autoRunConfig:t.autoRunConfig})}finally{this.abortController=null}}abort(){this.abortController&&this.abortController.abort()}setCwd(e){this.config.cwd=e}getConfig(){return{...this.config}}getSupportedModels(){return C}};export{Le as AnthropicProtocol,Me as ArkProtocol,u as CLAUDE_FAMILY,Ke as ChatOrchestrator,a as DEEPSEEK_FAMILY,de as DEFAULT_MODEL,i as DOUBAO_FAMILY,Te as DebugLogger,$e as DeepSeekProtocol,l as GEMINI_FAMILY,c as GPT_FAMILY,We as GeminiProtocol,Xe as HybridAgent,C as MODELS,p as MODEL_FAMILIES,d as MODEL_REGISTRY,Ge as OpenAIProtocol,r as QWEN_FAMILY,Ee as QwenProtocol,Qe as UnifiedAdapter,L as createAbort,ze as createAnthropicProtocol,j as createApiError,qe as createArkProtocol,De as createDeepSeekProtocol,ce as createDefaultToolExecutor,J as createDone,W as createError,je as createGeminiProtocol,Q as createImage,Ve as createOpenAIProtocol,Ne as createOrchestrator,B as createParseError,Pe as createQwenProtocol,F as createRateLimitError,O as createSearchEnd,q as createSearchResult,M as createSearchStart,H as createStepEnd,z as createStepStart,P as createStreamStart,E as createTextDelta,N as createThinkingDelta,I as createThinkingEnd,K as createThinkingStart,V as createTimeoutError,D as createToolCallResult,$ as createToolCallStart,G as createToolError,Ze as createUnifiedAdapter,Z as createVideo,me as getDefaultProvider,A as getModelByModelId,y as getModelEntry,g as getModelFamily,m as getModelProtocol,S as getModelSearchStrategy,f as getModelsByFamily,_ as getModelsByProtocol,h as getVisibleModels,ie as isAbortEvent,se as isErrorEvent,ne as isMediaEvent,he as isModelForProvider,re as isRetryableError,Y as isSearchEvent,oe as isStatusEvent,ae as isStepEvent,te as isTextEvent,X as isThinkingEvent,ee as isToolEvent,k as modelSupportsNativeSearch,b as modelSupportsThinking,R as resolveTools,ye as routeModelToProvider,ge as routeModelWithDetails,T as tool,x as tools};