tabby-ai-assistant 1.0.11 → 1.0.13
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/README.md +113 -55
- package/dist/index.js +1 -1
- package/package.json +1 -1
- package/src/services/chat/ai-sidebar.service.ts +1 -1
- package/src/services/core/config-provider.service.ts +4 -12
- package/src/services/core/toast.service.ts +70 -0
- package/src/services/providers/anthropic-provider.service.ts +0 -172
- package/src/services/providers/base-provider.service.ts +225 -14
- package/src/services/providers/glm-provider.service.ts +0 -195
- package/src/services/providers/minimax-provider.service.ts +0 -198
- package/src/services/providers/ollama-provider.service.ts +0 -160
- package/src/services/providers/openai-compatible.service.ts +1 -143
- package/src/services/providers/openai-provider.service.ts +1 -143
- package/src/services/providers/vllm-provider.service.ts +0 -160
- package/src/styles/ai-assistant.scss +23 -0
- package/src/types/provider.types.ts +206 -75
- package/src/index.ts.backup +0 -165
- package/src/services/chat/chat-history.service.ts.backup +0 -239
- package/webpack.config.js.backup +0 -57
|
@@ -273,14 +273,6 @@ export class GlmProviderService extends BaseAiProvider {
|
|
|
273
273
|
return result;
|
|
274
274
|
}
|
|
275
275
|
|
|
276
|
-
/**
|
|
277
|
-
* 获取默认基础URL
|
|
278
|
-
* GLM提供与Anthropic兼容的API端点
|
|
279
|
-
*/
|
|
280
|
-
protected getDefaultBaseURL(): string {
|
|
281
|
-
return 'https://open.bigmodel.cn/api/anthropic';
|
|
282
|
-
}
|
|
283
|
-
|
|
284
276
|
/**
|
|
285
277
|
* 转换消息格式(Anthropic兼容)
|
|
286
278
|
*/
|
|
@@ -312,191 +304,4 @@ export class GlmProviderService extends BaseAiProvider {
|
|
|
312
304
|
} : undefined
|
|
313
305
|
};
|
|
314
306
|
}
|
|
315
|
-
|
|
316
|
-
/**
|
|
317
|
-
* 构建命令生成提示
|
|
318
|
-
*/
|
|
319
|
-
private buildCommandPrompt(request: CommandRequest): string {
|
|
320
|
-
let prompt = `请将以下自然语言描述转换为准确的终端命令:\n\n"${request.naturalLanguage}"\n\n`;
|
|
321
|
-
|
|
322
|
-
if (request.context) {
|
|
323
|
-
prompt += `当前环境:\n`;
|
|
324
|
-
if (request.context.currentDirectory) {
|
|
325
|
-
prompt += `- 当前目录:${request.context.currentDirectory}\n`;
|
|
326
|
-
}
|
|
327
|
-
if (request.context.operatingSystem) {
|
|
328
|
-
prompt += `- 操作系统:${request.context.operatingSystem}\n`;
|
|
329
|
-
}
|
|
330
|
-
if (request.context.shell) {
|
|
331
|
-
prompt += `- Shell:${request.context.shell}\n`;
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
prompt += `\n请直接返回JSON格式:\n`;
|
|
336
|
-
prompt += `{\n`;
|
|
337
|
-
prompt += ` "command": "具体命令",\n`;
|
|
338
|
-
prompt += ` "explanation": "命令解释",\n`;
|
|
339
|
-
prompt += ` "confidence": 0.95\n`;
|
|
340
|
-
prompt += `}\n`;
|
|
341
|
-
|
|
342
|
-
return prompt;
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
/**
|
|
346
|
-
* 构建命令解释提示
|
|
347
|
-
*/
|
|
348
|
-
private buildExplainPrompt(request: ExplainRequest): string {
|
|
349
|
-
let prompt = `请详细解释以下终端命令:\n\n\`${request.command}\`\n\n`;
|
|
350
|
-
|
|
351
|
-
if (request.context?.currentDirectory) {
|
|
352
|
-
prompt += `当前目录:${request.context.currentDirectory}\n`;
|
|
353
|
-
}
|
|
354
|
-
if (request.context?.operatingSystem) {
|
|
355
|
-
prompt += `操作系统:${request.context.operatingSystem}\n`;
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
prompt += `\n请按以下JSON格式返回:\n`;
|
|
359
|
-
prompt += `{\n`;
|
|
360
|
-
prompt += ` "explanation": "整体解释",\n`;
|
|
361
|
-
prompt += ` "breakdown": [\n`;
|
|
362
|
-
prompt += ` {"part": "命令部分", "description": "说明"}\n`;
|
|
363
|
-
prompt += ` ],\n`;
|
|
364
|
-
prompt += ` "examples": ["使用示例"]\n`;
|
|
365
|
-
prompt += `}\n`;
|
|
366
|
-
|
|
367
|
-
return prompt;
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
/**
|
|
371
|
-
* 构建结果分析提示
|
|
372
|
-
*/
|
|
373
|
-
private buildAnalysisPrompt(request: AnalysisRequest): string {
|
|
374
|
-
let prompt = `请分析以下命令执行结果:\n\n`;
|
|
375
|
-
prompt += `命令:${request.command}\n`;
|
|
376
|
-
prompt += `退出码:${request.exitCode}\n`;
|
|
377
|
-
prompt += `输出:\n${request.output}\n\n`;
|
|
378
|
-
|
|
379
|
-
if (request.context?.workingDirectory) {
|
|
380
|
-
prompt += `工作目录:${request.context.workingDirectory}\n`;
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
prompt += `\n请按以下JSON格式返回:\n`;
|
|
384
|
-
prompt += `{\n`;
|
|
385
|
-
prompt += ` "summary": "结果总结",\n`;
|
|
386
|
-
prompt += ` "insights": ["洞察1", "洞察2"],\n`;
|
|
387
|
-
prompt += ` "success": true/false,\n`;
|
|
388
|
-
prompt += ` "issues": [\n`;
|
|
389
|
-
prompt += ` {"severity": "warning|error|info", "message": "问题描述", "suggestion": "建议"}\n`;
|
|
390
|
-
prompt += ` ]\n`;
|
|
391
|
-
prompt += `}\n`;
|
|
392
|
-
|
|
393
|
-
return prompt;
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
/**
|
|
397
|
-
* 解析命令响应
|
|
398
|
-
*/
|
|
399
|
-
private parseCommandResponse(content: string): CommandResponse {
|
|
400
|
-
try {
|
|
401
|
-
const match = content.match(/\{[\s\S]*\}/);
|
|
402
|
-
if (match) {
|
|
403
|
-
const parsed = JSON.parse(match[0]);
|
|
404
|
-
return {
|
|
405
|
-
command: parsed.command || '',
|
|
406
|
-
explanation: parsed.explanation || '',
|
|
407
|
-
confidence: parsed.confidence || 0.5
|
|
408
|
-
};
|
|
409
|
-
}
|
|
410
|
-
} catch (error) {
|
|
411
|
-
this.logger.warn('Failed to parse command response as JSON', error);
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
// 备用解析
|
|
415
|
-
const lines = content.split('\n').map(l => l.trim()).filter(l => l);
|
|
416
|
-
return {
|
|
417
|
-
command: lines[0] || '',
|
|
418
|
-
explanation: lines.slice(1).join(' ') || 'AI生成的命令',
|
|
419
|
-
confidence: 0.5
|
|
420
|
-
};
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
/**
|
|
424
|
-
* 解析解释响应
|
|
425
|
-
*/
|
|
426
|
-
private parseExplainResponse(content: string): ExplainResponse {
|
|
427
|
-
try {
|
|
428
|
-
const match = content.match(/\{[\s\S]*\}/);
|
|
429
|
-
if (match) {
|
|
430
|
-
const parsed = JSON.parse(match[0]);
|
|
431
|
-
return {
|
|
432
|
-
explanation: parsed.explanation || '',
|
|
433
|
-
breakdown: parsed.breakdown || [],
|
|
434
|
-
examples: parsed.examples || []
|
|
435
|
-
};
|
|
436
|
-
}
|
|
437
|
-
} catch (error) {
|
|
438
|
-
this.logger.warn('Failed to parse explain response as JSON', error);
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
return {
|
|
442
|
-
explanation: content,
|
|
443
|
-
breakdown: []
|
|
444
|
-
};
|
|
445
|
-
}
|
|
446
|
-
|
|
447
|
-
/**
|
|
448
|
-
* 解析分析响应
|
|
449
|
-
*/
|
|
450
|
-
private parseAnalysisResponse(content: string): AnalysisResponse {
|
|
451
|
-
try {
|
|
452
|
-
const match = content.match(/\{[\s\S]*\}/);
|
|
453
|
-
if (match) {
|
|
454
|
-
const parsed = JSON.parse(match[0]);
|
|
455
|
-
return {
|
|
456
|
-
summary: parsed.summary || '',
|
|
457
|
-
insights: parsed.insights || [],
|
|
458
|
-
success: parsed.success !== false,
|
|
459
|
-
issues: parsed.issues || []
|
|
460
|
-
};
|
|
461
|
-
}
|
|
462
|
-
} catch (error) {
|
|
463
|
-
this.logger.warn('Failed to parse analysis response as JSON', error);
|
|
464
|
-
}
|
|
465
|
-
|
|
466
|
-
return {
|
|
467
|
-
summary: content,
|
|
468
|
-
insights: [],
|
|
469
|
-
success: true
|
|
470
|
-
};
|
|
471
|
-
}
|
|
472
|
-
|
|
473
|
-
private getDefaultSystemPrompt(): string {
|
|
474
|
-
return `你是一个专业的终端命令助手,运行在 Tabby 终端中。
|
|
475
|
-
|
|
476
|
-
## 核心能力
|
|
477
|
-
你可以通过以下工具直接操作终端:
|
|
478
|
-
- write_to_terminal: 向终端写入并执行命令
|
|
479
|
-
- read_terminal_output: 读取终端输出
|
|
480
|
-
- get_terminal_list: 获取所有终端列表
|
|
481
|
-
- get_terminal_cwd: 获取当前工作目录
|
|
482
|
-
- focus_terminal: 切换到指定索引的终端(需要参数 terminal_index)
|
|
483
|
-
- get_terminal_selection: 获取终端中选中的文本
|
|
484
|
-
|
|
485
|
-
## 重要规则
|
|
486
|
-
1. 当用户请求执行命令(如"查看当前目录"、"列出文件"等),你必须使用 write_to_terminal 工具来执行
|
|
487
|
-
2. **当用户请求切换终端(如"切换到终端0"、"打开终端4"等),你必须使用 focus_terminal 工具**
|
|
488
|
-
3. 不要只是描述你"将要做什么",而是直接调用工具执行
|
|
489
|
-
4. 执行命令后,使用 read_terminal_output 读取结果并报告给用户
|
|
490
|
-
5. 如果不确定当前目录或终端状态,先使用 get_terminal_cwd 或 get_terminal_list 获取信息
|
|
491
|
-
6. **永远不要假装执行了操作,必须真正调用工具**
|
|
492
|
-
|
|
493
|
-
## 示例
|
|
494
|
-
用户:"查看当前目录的文件"
|
|
495
|
-
正确做法:调用 write_to_terminal 工具,参数 { "command": "dir", "execute": true }
|
|
496
|
-
错误做法:仅回复文字"我将执行 dir 命令"
|
|
497
|
-
|
|
498
|
-
用户:"切换到终端4"
|
|
499
|
-
正确做法:调用 focus_terminal 工具,参数 { "terminal_index": 4 }
|
|
500
|
-
错误做法:仅回复文字"已切换到终端4"(不调用工具)`;
|
|
501
|
-
}
|
|
502
307
|
}
|
|
@@ -334,14 +334,6 @@ export class MinimaxProviderService extends BaseAiProvider {
|
|
|
334
334
|
return result;
|
|
335
335
|
}
|
|
336
336
|
|
|
337
|
-
/**
|
|
338
|
-
* 获取默认基础URL
|
|
339
|
-
*/
|
|
340
|
-
protected getDefaultBaseURL(): string {
|
|
341
|
-
// 支持中国和国际端点
|
|
342
|
-
return 'https://api.minimaxi.com/anthropic';
|
|
343
|
-
}
|
|
344
|
-
|
|
345
337
|
/**
|
|
346
338
|
* 转换消息格式
|
|
347
339
|
* Anthropic API 支持两种格式:
|
|
@@ -440,194 +432,4 @@ export class MinimaxProviderService extends BaseAiProvider {
|
|
|
440
432
|
|
|
441
433
|
return result;
|
|
442
434
|
}
|
|
443
|
-
|
|
444
|
-
/**
|
|
445
|
-
* 构建命令生成提示
|
|
446
|
-
*/
|
|
447
|
-
private buildCommandPrompt(request: CommandRequest): string {
|
|
448
|
-
let prompt = `请将以下自然语言描述转换为准确的终端命令:\n\n"${request.naturalLanguage}"\n\n`;
|
|
449
|
-
|
|
450
|
-
if (request.context) {
|
|
451
|
-
prompt += `当前环境:\n`;
|
|
452
|
-
if (request.context.currentDirectory) {
|
|
453
|
-
prompt += `- 当前目录:${request.context.currentDirectory}\n`;
|
|
454
|
-
}
|
|
455
|
-
if (request.context.operatingSystem) {
|
|
456
|
-
prompt += `- 操作系统:${request.context.operatingSystem}\n`;
|
|
457
|
-
}
|
|
458
|
-
if (request.context.shell) {
|
|
459
|
-
prompt += `- Shell:${request.context.shell}\n`;
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
prompt += `\n请直接返回JSON格式:\n`;
|
|
464
|
-
prompt += `{\n`;
|
|
465
|
-
prompt += ` "command": "具体命令",\n`;
|
|
466
|
-
prompt += ` "explanation": "命令解释",\n`;
|
|
467
|
-
prompt += ` "confidence": 0.95\n`;
|
|
468
|
-
prompt += `}\n`;
|
|
469
|
-
|
|
470
|
-
return prompt;
|
|
471
|
-
}
|
|
472
|
-
|
|
473
|
-
/**
|
|
474
|
-
* 构建命令解释提示
|
|
475
|
-
*/
|
|
476
|
-
private buildExplainPrompt(request: ExplainRequest): string {
|
|
477
|
-
let prompt = `请详细解释以下终端命令:\n\n\`${request.command}\`\n\n`;
|
|
478
|
-
|
|
479
|
-
if (request.context?.currentDirectory) {
|
|
480
|
-
prompt += `当前目录:${request.context.currentDirectory}\n`;
|
|
481
|
-
}
|
|
482
|
-
if (request.context?.operatingSystem) {
|
|
483
|
-
prompt += `操作系统:${request.context.operatingSystem}\n`;
|
|
484
|
-
}
|
|
485
|
-
|
|
486
|
-
prompt += `\n请按以下JSON格式返回:\n`;
|
|
487
|
-
prompt += `{\n`;
|
|
488
|
-
prompt += ` "explanation": "整体解释",\n`;
|
|
489
|
-
prompt += ` "breakdown": [\n`;
|
|
490
|
-
prompt += ` {"part": "命令部分", "description": "说明"}\n`;
|
|
491
|
-
prompt += ` ],\n`;
|
|
492
|
-
prompt += ` "examples": ["使用示例"]\n`;
|
|
493
|
-
prompt += `}\n`;
|
|
494
|
-
|
|
495
|
-
return prompt;
|
|
496
|
-
}
|
|
497
|
-
|
|
498
|
-
/**
|
|
499
|
-
* 构建结果分析提示
|
|
500
|
-
*/
|
|
501
|
-
private buildAnalysisPrompt(request: AnalysisRequest): string {
|
|
502
|
-
let prompt = `请分析以下命令执行结果:\n\n`;
|
|
503
|
-
prompt += `命令:${request.command}\n`;
|
|
504
|
-
prompt += `退出码:${request.exitCode}\n`;
|
|
505
|
-
prompt += `输出:\n${request.output}\n\n`;
|
|
506
|
-
|
|
507
|
-
if (request.context?.workingDirectory) {
|
|
508
|
-
prompt += `工作目录:${request.context.workingDirectory}\n`;
|
|
509
|
-
}
|
|
510
|
-
|
|
511
|
-
prompt += `\n请按以下JSON格式返回:\n`;
|
|
512
|
-
prompt += `{\n`;
|
|
513
|
-
prompt += ` "summary": "结果总结",\n`;
|
|
514
|
-
prompt += ` "insights": ["洞察1", "洞察2"],\n`;
|
|
515
|
-
prompt += ` "success": true/false,\n`;
|
|
516
|
-
prompt += ` "issues": [\n`;
|
|
517
|
-
prompt += ` {"severity": "warning|error|info", "message": "问题描述", "suggestion": "建议"}\n`;
|
|
518
|
-
prompt += ` ]\n`;
|
|
519
|
-
prompt += `}\n`;
|
|
520
|
-
|
|
521
|
-
return prompt;
|
|
522
|
-
}
|
|
523
|
-
|
|
524
|
-
/**
|
|
525
|
-
* 解析命令响应
|
|
526
|
-
*/
|
|
527
|
-
private parseCommandResponse(content: string): CommandResponse {
|
|
528
|
-
try {
|
|
529
|
-
const match = content.match(/\{[\s\S]*\}/);
|
|
530
|
-
if (match) {
|
|
531
|
-
const parsed = JSON.parse(match[0]);
|
|
532
|
-
return {
|
|
533
|
-
command: parsed.command || '',
|
|
534
|
-
explanation: parsed.explanation || '',
|
|
535
|
-
confidence: parsed.confidence || 0.5
|
|
536
|
-
};
|
|
537
|
-
}
|
|
538
|
-
} catch (error) {
|
|
539
|
-
this.logger.warn('Failed to parse command response as JSON', error);
|
|
540
|
-
}
|
|
541
|
-
|
|
542
|
-
// 备用解析
|
|
543
|
-
const lines = content.split('\n').map(l => l.trim()).filter(l => l);
|
|
544
|
-
return {
|
|
545
|
-
command: lines[0] || '',
|
|
546
|
-
explanation: lines.slice(1).join(' ') || 'AI生成的命令',
|
|
547
|
-
confidence: 0.5
|
|
548
|
-
};
|
|
549
|
-
}
|
|
550
|
-
|
|
551
|
-
/**
|
|
552
|
-
* 解析解释响应
|
|
553
|
-
*/
|
|
554
|
-
private parseExplainResponse(content: string): ExplainResponse {
|
|
555
|
-
try {
|
|
556
|
-
const match = content.match(/\{[\s\S]*\}/);
|
|
557
|
-
if (match) {
|
|
558
|
-
const parsed = JSON.parse(match[0]);
|
|
559
|
-
return {
|
|
560
|
-
explanation: parsed.explanation || '',
|
|
561
|
-
breakdown: parsed.breakdown || [],
|
|
562
|
-
examples: parsed.examples || []
|
|
563
|
-
};
|
|
564
|
-
}
|
|
565
|
-
} catch (error) {
|
|
566
|
-
this.logger.warn('Failed to parse explain response as JSON', error);
|
|
567
|
-
}
|
|
568
|
-
|
|
569
|
-
return {
|
|
570
|
-
explanation: content,
|
|
571
|
-
breakdown: []
|
|
572
|
-
};
|
|
573
|
-
}
|
|
574
|
-
|
|
575
|
-
/**
|
|
576
|
-
* 解析分析响应
|
|
577
|
-
*/
|
|
578
|
-
private parseAnalysisResponse(content: string): AnalysisResponse {
|
|
579
|
-
try {
|
|
580
|
-
const match = content.match(/\{[\s\S]*\}/);
|
|
581
|
-
if (match) {
|
|
582
|
-
const parsed = JSON.parse(match[0]);
|
|
583
|
-
return {
|
|
584
|
-
summary: parsed.summary || '',
|
|
585
|
-
insights: parsed.insights || [],
|
|
586
|
-
success: parsed.success !== false,
|
|
587
|
-
issues: parsed.issues || []
|
|
588
|
-
};
|
|
589
|
-
}
|
|
590
|
-
} catch (error) {
|
|
591
|
-
this.logger.warn('Failed to parse analysis response as JSON', error);
|
|
592
|
-
}
|
|
593
|
-
|
|
594
|
-
return {
|
|
595
|
-
summary: content,
|
|
596
|
-
insights: [],
|
|
597
|
-
success: true
|
|
598
|
-
};
|
|
599
|
-
}
|
|
600
|
-
|
|
601
|
-
/**
|
|
602
|
-
* 获取默认系统提示
|
|
603
|
-
*/
|
|
604
|
-
private getDefaultSystemPrompt(): string {
|
|
605
|
-
return `你是一个专业的终端命令助手,运行在 Tabby 终端中。
|
|
606
|
-
|
|
607
|
-
## 核心能力
|
|
608
|
-
你可以通过以下工具直接操作终端:
|
|
609
|
-
- write_to_terminal: 向终端写入并执行命令
|
|
610
|
-
- read_terminal_output: 读取终端输出
|
|
611
|
-
- get_terminal_list: 获取所有终端列表
|
|
612
|
-
- get_terminal_cwd: 获取当前工作目录
|
|
613
|
-
- focus_terminal: 切换到指定索引的终端(需要参数 terminal_index)
|
|
614
|
-
- get_terminal_selection: 获取终端中选中的文本
|
|
615
|
-
|
|
616
|
-
## 重要规则
|
|
617
|
-
1. 当用户请求执行命令(如"查看当前目录"、"列出文件"等),你必须使用 write_to_terminal 工具来执行
|
|
618
|
-
2. **当用户请求切换终端(如"切换到终端0"、"打开终端4"等),你必须使用 focus_terminal 工具**
|
|
619
|
-
3. 不要只是描述你"将要做什么",而是直接调用工具执行
|
|
620
|
-
4. 执行命令后,使用 read_terminal_output 读取结果并报告给用户
|
|
621
|
-
5. 如果不确定当前目录或终端状态,先使用 get_terminal_cwd 或 get_terminal_list 获取信息
|
|
622
|
-
6. **永远不要假装执行了操作,必须真正调用工具**
|
|
623
|
-
|
|
624
|
-
## 示例
|
|
625
|
-
用户:"查看当前目录的文件"
|
|
626
|
-
正确做法:调用 write_to_terminal 工具,参数 { "command": "dir", "execute": true }
|
|
627
|
-
错误做法:仅回复文字"我将执行 dir 命令"
|
|
628
|
-
|
|
629
|
-
用户:"切换到终端4"
|
|
630
|
-
正确做法:调用 focus_terminal 工具,参数 { "terminal_index": 4 }
|
|
631
|
-
错误做法:仅回复文字"已切换到终端4"(不调用工具)`;
|
|
632
|
-
}
|
|
633
435
|
}
|
|
@@ -28,10 +28,6 @@ export class OllamaProviderService extends BaseAiProvider {
|
|
|
28
28
|
super(logger);
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
protected getDefaultBaseURL(): string {
|
|
32
|
-
return 'http://localhost:11434/v1';
|
|
33
|
-
}
|
|
34
|
-
|
|
35
31
|
/**
|
|
36
32
|
* 非流式聊天
|
|
37
33
|
*/
|
|
@@ -286,160 +282,4 @@ export class OllamaProviderService extends BaseAiProvider {
|
|
|
286
282
|
content: typeof msg.content === 'string' ? msg.content : JSON.stringify(msg.content)
|
|
287
283
|
}));
|
|
288
284
|
}
|
|
289
|
-
|
|
290
|
-
/**
|
|
291
|
-
* 构建命令生成提示
|
|
292
|
-
*/
|
|
293
|
-
private buildCommandPrompt(request: CommandRequest): string {
|
|
294
|
-
let prompt = `请将以下自然语言描述转换为准确的终端命令:\n\n"${request.naturalLanguage}"\n\n`;
|
|
295
|
-
|
|
296
|
-
if (request.context) {
|
|
297
|
-
prompt += `当前环境:\n`;
|
|
298
|
-
if (request.context.currentDirectory) {
|
|
299
|
-
prompt += `- 当前目录:${request.context.currentDirectory}\n`;
|
|
300
|
-
}
|
|
301
|
-
if (request.context.operatingSystem) {
|
|
302
|
-
prompt += `- 操作系统:${request.context.operatingSystem}\n`;
|
|
303
|
-
}
|
|
304
|
-
if (request.context.shell) {
|
|
305
|
-
prompt += `- Shell:${request.context.shell}\n`;
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
prompt += `\n请直接返回JSON格式:\n`;
|
|
310
|
-
prompt += `{\n`;
|
|
311
|
-
prompt += ` "command": "具体命令",\n`;
|
|
312
|
-
prompt += ` "explanation": "命令解释",\n`;
|
|
313
|
-
prompt += ` "confidence": 0.95\n`;
|
|
314
|
-
prompt += `}\n`;
|
|
315
|
-
|
|
316
|
-
return prompt;
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
/**
|
|
320
|
-
* 构建命令解释提示
|
|
321
|
-
*/
|
|
322
|
-
private buildExplainPrompt(request: ExplainRequest): string {
|
|
323
|
-
let prompt = `请详细解释以下终端命令:\n\n\`${request.command}\`\n\n`;
|
|
324
|
-
|
|
325
|
-
if (request.context?.currentDirectory) {
|
|
326
|
-
prompt += `当前目录:${request.context.currentDirectory}\n`;
|
|
327
|
-
}
|
|
328
|
-
if (request.context?.operatingSystem) {
|
|
329
|
-
prompt += `操作系统:${request.context.operatingSystem}\n`;
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
prompt += `\n请按以下JSON格式返回:\n`;
|
|
333
|
-
prompt += `{\n`;
|
|
334
|
-
prompt += ` "explanation": "整体解释",\n`;
|
|
335
|
-
prompt += ` "breakdown": [\n`;
|
|
336
|
-
prompt += ` {"part": "命令部分", "description": "说明"}\n`;
|
|
337
|
-
prompt += ` ],\n`;
|
|
338
|
-
prompt += ` "examples": ["使用示例"]\n`;
|
|
339
|
-
prompt += `}\n`;
|
|
340
|
-
|
|
341
|
-
return prompt;
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
/**
|
|
345
|
-
* 构建结果分析提示
|
|
346
|
-
*/
|
|
347
|
-
private buildAnalysisPrompt(request: AnalysisRequest): string {
|
|
348
|
-
let prompt = `请分析以下命令执行结果:\n\n`;
|
|
349
|
-
prompt += `命令:${request.command}\n`;
|
|
350
|
-
prompt += `退出码:${request.exitCode}\n`;
|
|
351
|
-
prompt += `输出:\n${request.output}\n\n`;
|
|
352
|
-
|
|
353
|
-
if (request.context?.workingDirectory) {
|
|
354
|
-
prompt += `工作目录:${request.context.workingDirectory}\n`;
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
prompt += `\n请按以下JSON格式返回:\n`;
|
|
358
|
-
prompt += `{\n`;
|
|
359
|
-
prompt += ` "summary": "结果总结",\n`;
|
|
360
|
-
prompt += ` "insights": ["洞察1", "洞察2"],\n`;
|
|
361
|
-
prompt += ` "success": true/false,\n`;
|
|
362
|
-
prompt += ` "issues": [\n`;
|
|
363
|
-
prompt += ` {"severity": "warning|error|info", "message": "问题描述", "suggestion": "建议"}\n`;
|
|
364
|
-
prompt += ` ]\n`;
|
|
365
|
-
prompt += `}\n`;
|
|
366
|
-
|
|
367
|
-
return prompt;
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
/**
|
|
371
|
-
* 解析命令响应
|
|
372
|
-
*/
|
|
373
|
-
private parseCommandResponse(content: string): CommandResponse {
|
|
374
|
-
try {
|
|
375
|
-
const match = content.match(/\{[\s\S]*\}/);
|
|
376
|
-
if (match) {
|
|
377
|
-
const parsed = JSON.parse(match[0]);
|
|
378
|
-
return {
|
|
379
|
-
command: parsed.command || '',
|
|
380
|
-
explanation: parsed.explanation || '',
|
|
381
|
-
confidence: parsed.confidence || 0.5
|
|
382
|
-
};
|
|
383
|
-
}
|
|
384
|
-
} catch (error) {
|
|
385
|
-
this.logger.warn('Failed to parse Ollama command response as JSON', error);
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
const lines = content.split('\n').map(l => l.trim()).filter(l => l);
|
|
389
|
-
return {
|
|
390
|
-
command: lines[0] || '',
|
|
391
|
-
explanation: lines.slice(1).join(' ') || 'AI生成的命令',
|
|
392
|
-
confidence: 0.5
|
|
393
|
-
};
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
/**
|
|
397
|
-
* 解析解释响应
|
|
398
|
-
*/
|
|
399
|
-
private parseExplainResponse(content: string): ExplainResponse {
|
|
400
|
-
try {
|
|
401
|
-
const match = content.match(/\{[\s\S]*\}/);
|
|
402
|
-
if (match) {
|
|
403
|
-
const parsed = JSON.parse(match[0]);
|
|
404
|
-
return {
|
|
405
|
-
explanation: parsed.explanation || '',
|
|
406
|
-
breakdown: parsed.breakdown || [],
|
|
407
|
-
examples: parsed.examples || []
|
|
408
|
-
};
|
|
409
|
-
}
|
|
410
|
-
} catch (error) {
|
|
411
|
-
this.logger.warn('Failed to parse Ollama explain response as JSON', error);
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
return {
|
|
415
|
-
explanation: content,
|
|
416
|
-
breakdown: []
|
|
417
|
-
};
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
/**
|
|
421
|
-
* 解析分析响应
|
|
422
|
-
*/
|
|
423
|
-
private parseAnalysisResponse(content: string): AnalysisResponse {
|
|
424
|
-
try {
|
|
425
|
-
const match = content.match(/\{[\s\S]*\}/);
|
|
426
|
-
if (match) {
|
|
427
|
-
const parsed = JSON.parse(match[0]);
|
|
428
|
-
return {
|
|
429
|
-
summary: parsed.summary || '',
|
|
430
|
-
insights: parsed.insights || [],
|
|
431
|
-
success: parsed.success !== false,
|
|
432
|
-
issues: parsed.issues || []
|
|
433
|
-
};
|
|
434
|
-
}
|
|
435
|
-
} catch (error) {
|
|
436
|
-
this.logger.warn('Failed to parse Ollama analysis response as JSON', error);
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
return {
|
|
440
|
-
summary: content,
|
|
441
|
-
insights: [],
|
|
442
|
-
success: true
|
|
443
|
-
};
|
|
444
|
-
}
|
|
445
285
|
}
|