@opensumi/ide-ai-native 3.9.1-next-1749540423.0 → 3.9.1-next-1749546307.0
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/lib/browser/ai-core.contribution.d.ts.map +1 -1
- package/lib/browser/ai-core.contribution.js +9 -4
- package/lib/browser/ai-core.contribution.js.map +1 -1
- package/lib/browser/chat/apply.service.d.ts +3 -0
- package/lib/browser/chat/apply.service.d.ts.map +1 -1
- package/lib/browser/chat/apply.service.js +47 -0
- package/lib/browser/chat/apply.service.js.map +1 -1
- package/lib/browser/chat/chat-manager.service.d.ts +1 -0
- package/lib/browser/chat/chat-manager.service.d.ts.map +1 -1
- package/lib/browser/chat/chat-manager.service.js +9 -3
- package/lib/browser/chat/chat-manager.service.js.map +1 -1
- package/lib/browser/chat/chat-model.d.ts +8 -1
- package/lib/browser/chat/chat-model.d.ts.map +1 -1
- package/lib/browser/chat/chat-model.js +113 -76
- package/lib/browser/chat/chat-model.js.map +1 -1
- package/lib/browser/chat/chat-proxy.service.d.ts +0 -2
- package/lib/browser/chat/chat-proxy.service.d.ts.map +1 -1
- package/lib/browser/chat/chat-proxy.service.js +50 -57
- package/lib/browser/chat/chat-proxy.service.js.map +1 -1
- package/lib/browser/chat/chat.feature.registry.d.ts +4 -1
- package/lib/browser/chat/chat.feature.registry.d.ts.map +1 -1
- package/lib/browser/chat/chat.feature.registry.js +6 -0
- package/lib/browser/chat/chat.feature.registry.js.map +1 -1
- package/lib/browser/chat/chat.view.d.ts.map +1 -1
- package/lib/browser/chat/chat.view.js +49 -10
- package/lib/browser/chat/chat.view.js.map +1 -1
- package/lib/browser/components/ChatEditor.js +2 -2
- package/lib/browser/components/ChatEditor.js.map +1 -1
- package/lib/browser/components/ChatHistory.d.ts.map +1 -1
- package/lib/browser/components/ChatHistory.js +2 -1
- package/lib/browser/components/ChatHistory.js.map +1 -1
- package/lib/browser/components/ChatMentionInput.d.ts.map +1 -1
- package/lib/browser/components/ChatMentionInput.js +148 -30
- package/lib/browser/components/ChatMentionInput.js.map +1 -1
- package/lib/browser/components/ChatReply.js +2 -2
- package/lib/browser/components/ChatReply.js.map +1 -1
- package/lib/browser/components/ChatToolRender.d.ts.map +1 -1
- package/lib/browser/components/ChatToolRender.js +7 -2
- package/lib/browser/components/ChatToolRender.js.map +1 -1
- package/lib/browser/components/ChatToolRender.module.less +25 -0
- package/lib/browser/components/components.module.less +37 -8
- package/lib/browser/components/mention-input/mention-input.d.ts.map +1 -1
- package/lib/browser/components/mention-input/mention-input.js +150 -14
- package/lib/browser/components/mention-input/mention-input.js.map +1 -1
- package/lib/browser/components/mention-input/mention-input.module.less +165 -1
- package/lib/browser/components/mention-input/mention-select.d.ts +28 -0
- package/lib/browser/components/mention-input/mention-select.d.ts.map +1 -0
- package/lib/browser/components/mention-input/mention-select.js +136 -0
- package/lib/browser/components/mention-input/mention-select.js.map +1 -0
- package/lib/browser/components/mention-input/mention-select.module.less +297 -0
- package/lib/browser/components/mention-input/types.d.ts +28 -1
- package/lib/browser/components/mention-input/types.d.ts.map +1 -1
- package/lib/browser/components/mention-input/types.js +1 -0
- package/lib/browser/components/mention-input/types.js.map +1 -1
- package/lib/browser/components/utils.d.ts +2 -2
- package/lib/browser/context/llm-context.service.d.ts +21 -2
- package/lib/browser/context/llm-context.service.d.ts.map +1 -1
- package/lib/browser/context/llm-context.service.js +162 -20
- package/lib/browser/context/llm-context.service.js.map +1 -1
- package/lib/browser/contrib/intelligent-completions/decoration/additions-deletions.decoration.d.ts.map +1 -1
- package/lib/browser/contrib/intelligent-completions/decoration/additions-deletions.decoration.js.map +1 -1
- package/lib/browser/contrib/intelligent-completions/diff-computer.js +1 -1
- package/lib/browser/contrib/intelligent-completions/diff-computer.js.map +1 -1
- package/lib/browser/contrib/terminal/terminal.feature.registry.js.map +1 -1
- package/lib/browser/index.d.ts.map +1 -1
- package/lib/browser/index.js +7 -0
- package/lib/browser/index.js.map +1 -1
- package/lib/browser/layout/ai-layout.d.ts.map +1 -1
- package/lib/browser/layout/ai-layout.js +6 -4
- package/lib/browser/layout/ai-layout.js.map +1 -1
- package/lib/browser/layout/tabbar.view.d.ts +1 -1
- package/lib/browser/layout/tabbar.view.d.ts.map +1 -1
- package/lib/browser/layout/tabbar.view.js +5 -12
- package/lib/browser/layout/tabbar.view.js.map +1 -1
- package/lib/browser/mcp/base-apply.service.d.ts +5 -4
- package/lib/browser/mcp/base-apply.service.d.ts.map +1 -1
- package/lib/browser/mcp/base-apply.service.js +23 -5
- package/lib/browser/mcp/base-apply.service.js.map +1 -1
- package/lib/browser/mcp/mcp-server-proxy.service.d.ts +3 -1
- package/lib/browser/mcp/mcp-server-proxy.service.d.ts.map +1 -1
- package/lib/browser/mcp/mcp-server-proxy.service.js +4 -0
- package/lib/browser/mcp/mcp-server-proxy.service.js.map +1 -1
- package/lib/browser/mcp/mcp-server.feature.registry.d.ts.map +1 -1
- package/lib/browser/mcp/mcp-server.feature.registry.js +7 -1
- package/lib/browser/mcp/mcp-server.feature.registry.js.map +1 -1
- package/lib/browser/mcp/tools/createNewFileWithText.d.ts +1 -3
- package/lib/browser/mcp/tools/createNewFileWithText.d.ts.map +1 -1
- package/lib/browser/mcp/tools/createNewFileWithText.js +14 -40
- package/lib/browser/mcp/tools/createNewFileWithText.js.map +1 -1
- package/lib/browser/mcp/tools/fileSearch.d.ts.map +1 -1
- package/lib/browser/mcp/tools/fileSearch.js +9 -5
- package/lib/browser/mcp/tools/fileSearch.js.map +1 -1
- package/lib/browser/mcp/tools/getDiagnosticsByPath.js.map +1 -1
- package/lib/browser/mcp/tools/getOpenEditorFileDiagnostics.js.map +1 -1
- package/lib/browser/mcp/tools/grepSearch.d.ts.map +1 -1
- package/lib/browser/mcp/tools/grepSearch.js +22 -10
- package/lib/browser/mcp/tools/grepSearch.js.map +1 -1
- package/lib/browser/mcp/tools/handlers/CreateNewFileWithText.d.ts +15 -0
- package/lib/browser/mcp/tools/handlers/CreateNewFileWithText.d.ts.map +1 -0
- package/lib/browser/mcp/tools/handlers/CreateNewFileWithText.js +53 -0
- package/lib/browser/mcp/tools/handlers/CreateNewFileWithText.js.map +1 -0
- package/lib/browser/mcp/tools/handlers/ListDir.js +1 -1
- package/lib/browser/mcp/tools/handlers/ListDir.js.map +1 -1
- package/lib/browser/mcp/tools/handlers/ReadFile.js +1 -1
- package/lib/browser/mcp/tools/handlers/ReadFile.js.map +1 -1
- package/lib/browser/mcp/tools/handlers/RunCommand.d.ts +11 -1
- package/lib/browser/mcp/tools/handlers/RunCommand.d.ts.map +1 -1
- package/lib/browser/mcp/tools/handlers/RunCommand.js +11 -4
- package/lib/browser/mcp/tools/handlers/RunCommand.js.map +1 -1
- package/lib/browser/mcp/tools/listDir.d.ts.map +1 -1
- package/lib/browser/mcp/tools/listDir.js +19 -15
- package/lib/browser/mcp/tools/listDir.js.map +1 -1
- package/lib/browser/mcp/tools/runTerminalCmd.js.map +1 -1
- package/lib/browser/model/msg-history-manager.d.ts +47 -1
- package/lib/browser/model/msg-history-manager.d.ts.map +1 -1
- package/lib/browser/model/msg-history-manager.js +127 -2
- package/lib/browser/model/msg-history-manager.js.map +1 -1
- package/lib/browser/preferences/schema.d.ts.map +1 -1
- package/lib/browser/preferences/schema.js +5 -0
- package/lib/browser/preferences/schema.js.map +1 -1
- package/lib/browser/rules/rules.contribution.d.ts +29 -0
- package/lib/browser/rules/rules.contribution.d.ts.map +1 -0
- package/lib/browser/rules/rules.contribution.js +94 -0
- package/lib/browser/rules/rules.contribution.js.map +1 -0
- package/lib/browser/rules/rules.module.less +175 -0
- package/lib/browser/rules/rules.service.d.ts +25 -0
- package/lib/browser/rules/rules.service.d.ts.map +1 -0
- package/lib/browser/rules/rules.service.js +180 -0
- package/lib/browser/rules/rules.service.js.map +1 -0
- package/lib/browser/rules/rules.view.d.ts +3 -0
- package/lib/browser/rules/rules.view.d.ts.map +1 -0
- package/lib/browser/rules/rules.view.js +76 -0
- package/lib/browser/rules/rules.view.js.map +1 -0
- package/lib/browser/types.d.ts +12 -1
- package/lib/browser/types.d.ts.map +1 -1
- package/lib/browser/types.js.map +1 -1
- package/lib/browser/widget/inline-stream-diff/live-preview.component.d.ts.map +1 -1
- package/lib/browser/widget/inline-stream-diff/live-preview.component.js.map +1 -1
- package/lib/common/image-compression.d.ts +25 -0
- package/lib/common/image-compression.d.ts.map +1 -0
- package/lib/common/image-compression.js +153 -0
- package/lib/common/image-compression.js.map +1 -0
- package/lib/common/index.d.ts +5 -1
- package/lib/common/index.d.ts.map +1 -1
- package/lib/common/index.js +2 -0
- package/lib/common/index.js.map +1 -1
- package/lib/common/llm-context.d.ts +19 -0
- package/lib/common/llm-context.d.ts.map +1 -1
- package/lib/common/llm-context.js.map +1 -1
- package/lib/common/mdc-parser.d.ts +60 -0
- package/lib/common/mdc-parser.d.ts.map +1 -0
- package/lib/common/mdc-parser.js +246 -0
- package/lib/common/mdc-parser.js.map +1 -0
- package/lib/common/model.d.ts +1 -0
- package/lib/common/model.d.ts.map +1 -1
- package/lib/common/model.js.map +1 -1
- package/lib/common/prompts/context-prompt-provider.d.ts +0 -2
- package/lib/common/prompts/context-prompt-provider.d.ts.map +1 -1
- package/lib/common/prompts/context-prompt-provider.js +35 -29
- package/lib/common/prompts/context-prompt-provider.js.map +1 -1
- package/lib/common/prompts/system-prompt.d.ts +2 -0
- package/lib/common/prompts/system-prompt.d.ts.map +1 -0
- package/lib/common/prompts/system-prompt.js +5 -0
- package/lib/common/prompts/system-prompt.js.map +1 -0
- package/lib/common/types.d.ts +21 -0
- package/lib/common/types.d.ts.map +1 -1
- package/lib/common/types.js.map +1 -1
- package/lib/common/utils.d.ts +1 -0
- package/lib/common/utils.d.ts.map +1 -1
- package/lib/common/utils.js +5 -2
- package/lib/common/utils.js.map +1 -1
- package/lib/node/anthropic/anthropic-language-model.d.ts +1 -1
- package/lib/node/base-language-model.d.ts +2 -1
- package/lib/node/base-language-model.d.ts.map +1 -1
- package/lib/node/base-language-model.js +12 -2
- package/lib/node/base-language-model.js.map +1 -1
- package/lib/node/deepseek/deepseek-language-model.d.ts +1 -1
- package/lib/node/mcp/sumi-mcp-server.d.ts +3 -1
- package/lib/node/mcp/sumi-mcp-server.d.ts.map +1 -1
- package/lib/node/mcp/sumi-mcp-server.js +7 -1
- package/lib/node/mcp/sumi-mcp-server.js.map +1 -1
- package/lib/node/mcp-server-manager-impl.d.ts +3 -1
- package/lib/node/mcp-server-manager-impl.d.ts.map +1 -1
- package/lib/node/mcp-server-manager-impl.js +14 -2
- package/lib/node/mcp-server-manager-impl.js.map +1 -1
- package/lib/node/mcp-server.sse.d.ts +187 -1
- package/lib/node/mcp-server.sse.d.ts.map +1 -1
- package/lib/node/mcp-server.sse.js +2 -2
- package/lib/node/mcp-server.sse.js.map +1 -1
- package/lib/node/mcp-server.stdio.d.ts +187 -1
- package/lib/node/mcp-server.stdio.d.ts.map +1 -1
- package/package.json +27 -26
- package/src/browser/ai-core.contribution.ts +14 -4
- package/src/browser/chat/apply.service.ts +62 -1
- package/src/browser/chat/chat-manager.service.ts +16 -7
- package/src/browser/chat/chat-model.ts +130 -73
- package/src/browser/chat/chat-proxy.service.ts +68 -81
- package/src/browser/chat/chat.feature.registry.ts +17 -1
- package/src/browser/chat/chat.view.tsx +73 -12
- package/src/browser/components/ChatEditor.tsx +1 -1
- package/src/browser/components/ChatHistory.tsx +2 -1
- package/src/browser/components/ChatMentionInput.tsx +180 -35
- package/src/browser/components/ChatReply.tsx +4 -4
- package/src/browser/components/ChatToolRender.module.less +25 -0
- package/src/browser/components/ChatToolRender.tsx +10 -2
- package/src/browser/components/components.module.less +37 -8
- package/src/browser/components/mention-input/mention-input.module.less +165 -1
- package/src/browser/components/mention-input/mention-input.tsx +232 -31
- package/src/browser/components/mention-input/mention-select.module.less +297 -0
- package/src/browser/components/mention-input/mention-select.tsx +256 -0
- package/src/browser/components/mention-input/types.ts +29 -0
- package/src/browser/context/llm-context.service.ts +182 -21
- package/src/browser/contrib/intelligent-completions/decoration/additions-deletions.decoration.ts +1 -1
- package/src/browser/contrib/intelligent-completions/diff-computer.ts +1 -1
- package/src/browser/contrib/terminal/terminal.feature.registry.ts +1 -1
- package/src/browser/index.ts +8 -0
- package/src/browser/layout/ai-layout.tsx +12 -8
- package/src/browser/layout/tabbar.view.tsx +10 -23
- package/src/browser/mcp/base-apply.service.ts +30 -10
- package/src/browser/mcp/mcp-server-proxy.service.ts +6 -1
- package/src/browser/mcp/mcp-server.feature.registry.ts +6 -1
- package/src/browser/mcp/tools/createNewFileWithText.ts +17 -46
- package/src/browser/mcp/tools/fileSearch.ts +8 -5
- package/src/browser/mcp/tools/getDiagnosticsByPath.ts +1 -1
- package/src/browser/mcp/tools/getOpenEditorFileDiagnostics.ts +1 -1
- package/src/browser/mcp/tools/grepSearch.ts +32 -21
- package/src/browser/mcp/tools/handlers/CreateNewFileWithText.ts +49 -0
- package/src/browser/mcp/tools/handlers/ListDir.ts +2 -2
- package/src/browser/mcp/tools/handlers/ReadFile.ts +2 -2
- package/src/browser/mcp/tools/handlers/RunCommand.ts +21 -14
- package/src/browser/mcp/tools/listDir.ts +15 -12
- package/src/browser/mcp/tools/runTerminalCmd.ts +1 -1
- package/src/browser/model/msg-history-manager.ts +181 -2
- package/src/browser/preferences/schema.ts +5 -0
- package/src/browser/rules/rules.contribution.ts +105 -0
- package/src/browser/rules/rules.module.less +175 -0
- package/src/browser/rules/rules.service.ts +189 -0
- package/src/browser/rules/rules.view.tsx +127 -0
- package/src/browser/types.ts +18 -0
- package/src/browser/widget/inline-stream-diff/live-preview.component.tsx +0 -1
- package/src/common/image-compression.ts +174 -0
- package/src/common/index.ts +6 -1
- package/src/common/llm-context.ts +23 -0
- package/src/common/mdc-parser.ts +295 -0
- package/src/common/model.ts +1 -0
- package/src/common/prompts/context-prompt-provider.ts +55 -40
- package/src/common/prompts/system-prompt.ts +2 -0
- package/src/common/types.ts +18 -0
- package/src/common/utils.ts +4 -1
- package/src/node/base-language-model.ts +11 -14
- package/src/node/mcp/sumi-mcp-server.ts +10 -2
- package/src/node/mcp-server-manager-impl.ts +17 -2
- package/src/node/mcp-server.sse.ts +1 -2
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MDC 文件的 frontmatter 接口定义
|
|
3
|
+
*/
|
|
4
|
+
export interface IMDCFrontmatter {
|
|
5
|
+
description?: string;
|
|
6
|
+
globs?: string | string[];
|
|
7
|
+
alwaysApply?: boolean;
|
|
8
|
+
[key: string]: any;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* MDC 文件解析结果接口
|
|
13
|
+
*/
|
|
14
|
+
export interface IMDCParseResult {
|
|
15
|
+
frontmatter: IMDCFrontmatter;
|
|
16
|
+
content: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* MDC 文件内容结构
|
|
21
|
+
*/
|
|
22
|
+
export interface IMDCContent {
|
|
23
|
+
frontmatter: IMDCFrontmatter;
|
|
24
|
+
content: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 解析简单的 YAML 值
|
|
29
|
+
* @param value - 要解析的值字符串
|
|
30
|
+
* @returns 解析后的值
|
|
31
|
+
*/
|
|
32
|
+
function parseYamlValue(value: string): any {
|
|
33
|
+
const trimmed = value.trim();
|
|
34
|
+
|
|
35
|
+
// 布尔值
|
|
36
|
+
if (trimmed === 'true') {
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
if (trimmed === 'false') {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// null/undefined
|
|
44
|
+
if (trimmed === 'null' || trimmed === '~' || trimmed === '') {
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// 数字
|
|
49
|
+
if (/^-?\d+(\.\d+)?$/.test(trimmed)) {
|
|
50
|
+
return trimmed.includes('.') ? parseFloat(trimmed) : parseInt(trimmed, 10);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// 数组 (简单格式: [item1, item2, item3] 或 - item 格式)
|
|
54
|
+
if (trimmed.startsWith('[') && trimmed.endsWith(']')) {
|
|
55
|
+
const arrayContent = trimmed.slice(1, -1).trim();
|
|
56
|
+
if (!arrayContent) {
|
|
57
|
+
return [];
|
|
58
|
+
}
|
|
59
|
+
return arrayContent.split(',').map((item) => parseYamlValue(item));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// 字符串 (移除引号)
|
|
63
|
+
if ((trimmed.startsWith('"') && trimmed.endsWith('"')) || (trimmed.startsWith("'") && trimmed.endsWith("'"))) {
|
|
64
|
+
return trimmed.slice(1, -1);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// 默认作为字符串处理
|
|
68
|
+
return trimmed;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* 按行解析 YAML frontmatter
|
|
73
|
+
* @param frontmatterStr - frontmatter 字符串
|
|
74
|
+
* @returns 解析后的对象
|
|
75
|
+
*/
|
|
76
|
+
function parseFrontmatterByLine(frontmatterStr: string): IMDCFrontmatter {
|
|
77
|
+
const result: IMDCFrontmatter = {};
|
|
78
|
+
const lines = frontmatterStr.split('\n');
|
|
79
|
+
let currentKey: string | null = null;
|
|
80
|
+
let arrayItems: string[] = [];
|
|
81
|
+
let isInArray = false;
|
|
82
|
+
|
|
83
|
+
for (const line of lines) {
|
|
84
|
+
const trimmedLine = line.trim();
|
|
85
|
+
|
|
86
|
+
// 跳过空行和注释
|
|
87
|
+
if (!trimmedLine || trimmedLine.startsWith('#')) {
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// 检查是否是数组项 (以 - 开头)
|
|
92
|
+
if (trimmedLine.startsWith('- ')) {
|
|
93
|
+
if (currentKey && isInArray) {
|
|
94
|
+
arrayItems.push(trimmedLine.slice(2).trim());
|
|
95
|
+
}
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// 如果之前在处理数组,现在遇到新的键值对,先保存数组
|
|
100
|
+
if (isInArray && currentKey) {
|
|
101
|
+
result[currentKey] = arrayItems.map((item) => parseYamlValue(item));
|
|
102
|
+
arrayItems = [];
|
|
103
|
+
isInArray = false;
|
|
104
|
+
currentKey = null;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// 解析键值对
|
|
108
|
+
const colonIndex = trimmedLine.indexOf(':');
|
|
109
|
+
if (colonIndex === -1) {
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const key = trimmedLine.slice(0, colonIndex).trim();
|
|
114
|
+
const value = trimmedLine.slice(colonIndex + 1).trim();
|
|
115
|
+
|
|
116
|
+
currentKey = key;
|
|
117
|
+
|
|
118
|
+
if (value) {
|
|
119
|
+
result[key] = parseYamlValue(value);
|
|
120
|
+
isInArray = false;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// 处理最后的数组
|
|
125
|
+
if (isInArray && currentKey) {
|
|
126
|
+
result[currentKey] = arrayItems.map((item) => parseYamlValue(item));
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return result;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* 序列化对象为简单的 YAML 格式
|
|
134
|
+
* @param obj - 要序列化的对象
|
|
135
|
+
* @returns YAML 字符串
|
|
136
|
+
*/
|
|
137
|
+
function serializeToYaml(obj: any): string {
|
|
138
|
+
const lines: string[] = [];
|
|
139
|
+
|
|
140
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
141
|
+
if (value === null || value === undefined) {
|
|
142
|
+
lines.push(`${key}:`);
|
|
143
|
+
} else if (Array.isArray(value)) {
|
|
144
|
+
if (value.length === 0) {
|
|
145
|
+
lines.push(`${key}: []`);
|
|
146
|
+
} else {
|
|
147
|
+
lines.push(`${key}:`);
|
|
148
|
+
for (const item of value) {
|
|
149
|
+
lines.push(` - ${typeof item === 'string' ? item : JSON.stringify(item)}`);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
} else if (typeof value === 'string') {
|
|
153
|
+
// 如果字符串包含特殊字符或空格,使用引号
|
|
154
|
+
if (value.includes(':') || value.includes('#') || value.includes('\n') || value.trim() !== value) {
|
|
155
|
+
lines.push(`${key}: "${value.replace(/"/g, '\\"')}"`);
|
|
156
|
+
} else {
|
|
157
|
+
lines.push(`${key}: ${value}`);
|
|
158
|
+
}
|
|
159
|
+
} else {
|
|
160
|
+
lines.push(`${key}: ${value}`);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return lines.join('\n');
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* 解析 .mdc 文件内容
|
|
169
|
+
* @param rawContent - 原始文件内容
|
|
170
|
+
* @returns 解析后的结果,包含 frontmatter 和 content
|
|
171
|
+
*/
|
|
172
|
+
export function parseMDC(rawContent: string): IMDCParseResult {
|
|
173
|
+
const frontmatterRegex = /^---\s*\n([\s\S]*?)\n---\s*\n?([\s\S]*)$/;
|
|
174
|
+
const match = rawContent.match(frontmatterRegex);
|
|
175
|
+
|
|
176
|
+
if (!match) {
|
|
177
|
+
// 如果没有 frontmatter,整个内容作为 content
|
|
178
|
+
return {
|
|
179
|
+
frontmatter: {},
|
|
180
|
+
content: rawContent.trim(),
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const [, frontmatterStr, content] = match;
|
|
185
|
+
|
|
186
|
+
let frontmatter: IMDCFrontmatter = {};
|
|
187
|
+
|
|
188
|
+
try {
|
|
189
|
+
// 按行解析 YAML frontmatter
|
|
190
|
+
frontmatter = parseFrontmatterByLine(frontmatterStr);
|
|
191
|
+
} catch (error) {
|
|
192
|
+
// 如果解析失败,返回空的 frontmatter
|
|
193
|
+
frontmatter = {};
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return {
|
|
197
|
+
frontmatter,
|
|
198
|
+
content: content.trim(),
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* 序列化 MDC 内容为字符串
|
|
204
|
+
* @param mdcContent - MDC 内容对象
|
|
205
|
+
* @returns 序列化后的字符串
|
|
206
|
+
*/
|
|
207
|
+
export function serializeMDC(mdcContent: IMDCContent): string {
|
|
208
|
+
const { frontmatter, content } = mdcContent;
|
|
209
|
+
|
|
210
|
+
// 如果 frontmatter 为空或没有有效内容,只返回 content
|
|
211
|
+
if (!frontmatter || Object.keys(frontmatter).length === 0) {
|
|
212
|
+
return content;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
try {
|
|
216
|
+
// 序列化 frontmatter 为 YAML
|
|
217
|
+
const frontmatterStr = serializeToYaml(frontmatter);
|
|
218
|
+
|
|
219
|
+
return `---\n${frontmatterStr}\n---\n${content}`;
|
|
220
|
+
} catch (error) {
|
|
221
|
+
// 如果序列化失败,只返回 content
|
|
222
|
+
return content;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* 验证 MDC frontmatter 是否有效
|
|
228
|
+
* @param frontmatter - 要验证的 frontmatter 对象
|
|
229
|
+
* @returns 验证结果
|
|
230
|
+
*/
|
|
231
|
+
export function validateMDCFrontmatter(frontmatter: any): frontmatter is IMDCFrontmatter {
|
|
232
|
+
if (typeof frontmatter !== 'object' || frontmatter === null) {
|
|
233
|
+
return false;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// 可选的字段类型检查
|
|
237
|
+
if (frontmatter.description !== undefined && typeof frontmatter.description !== 'string') {
|
|
238
|
+
return false;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
if (frontmatter.globs !== undefined) {
|
|
242
|
+
if (typeof frontmatter.globs !== 'string' && !Array.isArray(frontmatter.globs)) {
|
|
243
|
+
return false;
|
|
244
|
+
}
|
|
245
|
+
if (Array.isArray(frontmatter.globs)) {
|
|
246
|
+
if (!frontmatter.globs.every((glob: any) => typeof glob === 'string')) {
|
|
247
|
+
return false;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
if (frontmatter.alwaysApply !== undefined && typeof frontmatter.alwaysApply !== 'boolean') {
|
|
253
|
+
return false;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
return true;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* 创建默认的 MDC frontmatter
|
|
261
|
+
* @returns 默认的 frontmatter 对象
|
|
262
|
+
*/
|
|
263
|
+
export function createDefaultMDCFrontmatter(): IMDCFrontmatter {
|
|
264
|
+
return {
|
|
265
|
+
description: '',
|
|
266
|
+
globs: '',
|
|
267
|
+
alwaysApply: false,
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* 更新 MDC 文件的 frontmatter
|
|
273
|
+
* @param rawContent - 原始文件内容
|
|
274
|
+
* @param newFrontmatter - 新的 frontmatter
|
|
275
|
+
* @returns 更新后的文件内容
|
|
276
|
+
*/
|
|
277
|
+
export function updateMDCFrontmatter(rawContent: string, newFrontmatter: Partial<IMDCFrontmatter>): string {
|
|
278
|
+
const parsed = parseMDC(rawContent);
|
|
279
|
+
const updatedFrontmatter = { ...parsed.frontmatter, ...newFrontmatter };
|
|
280
|
+
|
|
281
|
+
return serializeMDC({
|
|
282
|
+
frontmatter: updatedFrontmatter,
|
|
283
|
+
content: parsed.content,
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* 从 MDC 内容中提取纯文本内容(不包含 frontmatter)
|
|
289
|
+
* @param rawContent - 原始文件内容
|
|
290
|
+
* @returns 纯文本内容
|
|
291
|
+
*/
|
|
292
|
+
export function extractMDCContent(rawContent: string): string {
|
|
293
|
+
const parsed = parseMDC(rawContent);
|
|
294
|
+
return parsed.content;
|
|
295
|
+
}
|
package/src/common/model.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { Autowired, Injectable } from '@opensumi/di';
|
|
|
2
2
|
import { WorkbenchEditorService } from '@opensumi/ide-editor/lib/common/editor';
|
|
3
3
|
import { IWorkspaceService } from '@opensumi/ide-workspace';
|
|
4
4
|
|
|
5
|
-
import { SerializedContext } from '../llm-context';
|
|
5
|
+
import { AttachFileContext, SerializedContext } from '../llm-context';
|
|
6
6
|
|
|
7
7
|
export const ChatAgentPromptProvider = Symbol('ChatAgentPromptProvider');
|
|
8
8
|
|
|
@@ -23,14 +23,17 @@ export class DefaultChatAgentPromptProvider implements ChatAgentPromptProvider {
|
|
|
23
23
|
protected readonly workspaceService: IWorkspaceService;
|
|
24
24
|
|
|
25
25
|
async provideContextPrompt(context: SerializedContext, userMessage: string) {
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
let currentFileInfo = await this.getCurrentFileInfo();
|
|
27
|
+
if (context.attachedFiles.some((file) => file.path === currentFileInfo?.path)) {
|
|
28
|
+
currentFileInfo = null;
|
|
29
|
+
}
|
|
28
30
|
|
|
29
31
|
return this.buildPromptTemplate({
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
attachedFolders: this.buildAttachedFoldersSection(context.attachedFolders),
|
|
32
|
+
attachedFiles: context.attachedFiles,
|
|
33
|
+
attachedFolders: context.attachedFolders,
|
|
33
34
|
currentFile: currentFileInfo,
|
|
35
|
+
attachedRules: context.attachedRules,
|
|
36
|
+
globalRules: context.globalRules,
|
|
34
37
|
userMessage,
|
|
35
38
|
});
|
|
36
39
|
}
|
|
@@ -46,33 +49,56 @@ export class DefaultChatAgentPromptProvider implements ChatAgentPromptProvider {
|
|
|
46
49
|
const currentPath =
|
|
47
50
|
(await this.workspaceService.asRelativePath(currentModel.uri))?.path || currentModel.uri.codeUri.fsPath;
|
|
48
51
|
|
|
52
|
+
// 获取当前选中行信息
|
|
53
|
+
const selection = editor?.monacoEditor?.getSelection();
|
|
54
|
+
const currentLine = selection ? selection.startLineNumber : undefined;
|
|
55
|
+
let lineContent = '';
|
|
56
|
+
|
|
57
|
+
if (currentLine && editor?.monacoEditor) {
|
|
58
|
+
const model = editor.monacoEditor.getModel();
|
|
59
|
+
if (model) {
|
|
60
|
+
lineContent = model.getLineContent(currentLine)?.trim() || '';
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
49
64
|
return {
|
|
50
65
|
path: currentPath,
|
|
51
66
|
languageId: currentModel.languageId,
|
|
52
67
|
content: currentModel.getText(),
|
|
68
|
+
currentLine,
|
|
69
|
+
lineContent,
|
|
53
70
|
};
|
|
54
71
|
}
|
|
55
72
|
|
|
56
|
-
private buildPromptTemplate({
|
|
57
|
-
recentFiles,
|
|
73
|
+
private async buildPromptTemplate({
|
|
58
74
|
attachedFiles,
|
|
59
75
|
attachedFolders,
|
|
60
76
|
currentFile,
|
|
77
|
+
attachedRules,
|
|
78
|
+
globalRules,
|
|
61
79
|
userMessage,
|
|
62
80
|
}: {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
81
|
+
attachedFiles: AttachFileContext[];
|
|
82
|
+
attachedFolders: string[];
|
|
83
|
+
currentFile: {
|
|
84
|
+
path: string;
|
|
85
|
+
languageId: string;
|
|
86
|
+
content: string;
|
|
87
|
+
currentLine?: number;
|
|
88
|
+
lineContent?: string;
|
|
89
|
+
} | null;
|
|
90
|
+
attachedRules: string[];
|
|
91
|
+
globalRules: string[];
|
|
67
92
|
userMessage: string;
|
|
68
93
|
}) {
|
|
69
94
|
const sections = [
|
|
95
|
+
...globalRules,
|
|
96
|
+
...attachedFolders,
|
|
70
97
|
'<additional_data>',
|
|
71
98
|
'Below are some potentially helpful/relevant pieces of information for figuring out to respond',
|
|
72
|
-
recentFiles,
|
|
73
|
-
attachedFiles,
|
|
74
|
-
attachedFolders,
|
|
75
99
|
this.buildCurrentFileSection(currentFile),
|
|
100
|
+
this.buildAttachedFilesSection(attachedFiles),
|
|
101
|
+
...attachedRules,
|
|
76
102
|
'</additional_data>',
|
|
77
103
|
'<user_query>',
|
|
78
104
|
userMessage,
|
|
@@ -82,17 +108,7 @@ export class DefaultChatAgentPromptProvider implements ChatAgentPromptProvider {
|
|
|
82
108
|
return sections.join('\n');
|
|
83
109
|
}
|
|
84
110
|
|
|
85
|
-
private
|
|
86
|
-
if (!files.length) {
|
|
87
|
-
return '';
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
return `<recently_viewed_files>
|
|
91
|
-
${files.map((file, idx) => ` ${idx + 1}: ${file}`).join('\n')}
|
|
92
|
-
</recently_viewed_files>`;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
private buildAttachedFilesSection(files: { path: string; content: string; lineErrors: string[] }[]): string {
|
|
111
|
+
private buildAttachedFilesSection(files: AttachFileContext[]): string {
|
|
96
112
|
if (!files.length) {
|
|
97
113
|
return '';
|
|
98
114
|
}
|
|
@@ -112,9 +128,9 @@ ${files.map((file, idx) => ` ${idx + 1}: ${file}`).join('\n')}
|
|
|
112
128
|
return `<attached_files>\n${fileContents}\n</attached_files>`;
|
|
113
129
|
}
|
|
114
130
|
|
|
115
|
-
private buildFileContentSection(file:
|
|
131
|
+
private buildFileContentSection(file: AttachFileContext): string {
|
|
116
132
|
return `<file_contents>
|
|
117
|
-
\`\`\`${file.path}
|
|
133
|
+
\`\`\`${file.path}${file.selection ? `, lines: ${file.selection?.[0]}-${file.selection?.[1]}` : ''}
|
|
118
134
|
${file.content}
|
|
119
135
|
\`\`\`
|
|
120
136
|
</file_contents>`;
|
|
@@ -128,23 +144,22 @@ ${file.content}
|
|
|
128
144
|
return `<linter_errors>\n${errors.join('\n')}\n</linter_errors>`;
|
|
129
145
|
}
|
|
130
146
|
|
|
131
|
-
private
|
|
132
|
-
|
|
147
|
+
private buildCurrentFileSection(
|
|
148
|
+
fileInfo: { path: string; languageId: string; content: string; currentLine?: number; lineContent?: string } | null,
|
|
149
|
+
): string {
|
|
150
|
+
if (!fileInfo) {
|
|
133
151
|
return '';
|
|
134
152
|
}
|
|
135
153
|
|
|
136
|
-
|
|
137
|
-
}
|
|
154
|
+
let currentFileSection = `<current_file>\nPath: ${fileInfo.path}`;
|
|
138
155
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
156
|
+
if (fileInfo.currentLine && fileInfo.lineContent) {
|
|
157
|
+
currentFileSection += `\nCurrently selected line: ${fileInfo.currentLine}`;
|
|
158
|
+
currentFileSection += `\nLine ${fileInfo.currentLine} content: \`${fileInfo.lineContent}\``;
|
|
142
159
|
}
|
|
143
160
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
\`\`\`
|
|
148
|
-
</current_opened_file>`;
|
|
161
|
+
currentFileSection += '\n</current_file>';
|
|
162
|
+
|
|
163
|
+
return currentFileSection;
|
|
149
164
|
}
|
|
150
165
|
}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export const DEFAULT_SYSTEM_PROMPT =
|
|
2
|
+
"You are an AI coding assistant. You operate in OpenSumi.\n\nYou are pair programming with a USER to solve their coding task. Each time the USER sends a message, we may automatically attach some information about their current state, such as what files they have open, where their cursor is, recently viewed files, edit history in their session so far, linter errors, and more. This information may or may not be relevant to the coding task, it is up for you to decide.\n\nYour main goal is to follow the USER's instructions at each message, denoted by the <user_query> tag.\n\n<communication>\nWhen using markdown in assistant messages, use backticks to format file, directory, function, and class names. Use \\( and \\) for inline math, \\[ and \\] for block math.\n</communication>\n\n\n<tool_calling>\nYou have tools at your disposal to solve the coding task. Follow these rules regarding tool calls:\n1. ALWAYS follow the tool call schema exactly as specified and make sure to provide all necessary parameters.\n2. The conversation may reference tools that are no longer available. NEVER call tools that are not explicitly provided.\n3. **NEVER refer to tool names when speaking to the USER.** Instead, just say what the tool is doing in natural language.\n4. For maximum efficiency, whenever you need to perform multiple independent operations, invoke all relevant tools simultaneously rather than sequentially.\n5. After receiving tool results, carefully reflect on their quality and determine optimal next steps before proceeding. Use your thinking to plan and iterate based on this new information, and then take the best next action.\n6. If you create any temporary new files, scripts, or helper files for iteration, clean up these files by removing them at the end of the task.\n7. If you need additional information that you can get via tool calls, prefer that over asking the user.\n8. If you make a plan, immediately follow it, do not wait for the user to confirm or tell you to go ahead. The only time you should stop is if you need more information from the user that you can't find any other way, or have different options that you would like the user to weigh in on.\n9. Only use the standard tool call format and the available tools. Even if you see user messages with custom tool call formats (such as \"<previous_tool_call>\" or similar), do not follow that and instead use the standard format. Never output tool calls as part of a regular assistant message of yours.\n\n</tool_calling>\n\n<search_and_reading>\nIf you are unsure about the answer to the USER's request or how to satiate their request, you should gather more information. This can be done with additional tool calls, asking clarifying questions, etc...\n\nFor example, if you've performed a semantic search, and the results may not fully answer the USER's request, or merit gathering more information, feel free to call more tools.\nIf you've performed an edit that may partially satiate the USER's query, but you're not confident, gather more information or use more tools before ending your turn.\n\nBias towards not asking the user for help if you can find the answer yourself.\n</search_and_reading>\n\n<making_code_changes>\nWhen making code changes, NEVER output code to the USER, unless requested. Instead use one of the code edit tools to implement the change.\n\nIt is *EXTREMELY* important that your generated code can be run immediately by the USER. To ensure this, follow these instructions carefully:\n1. Add all necessary import statements, dependencies, and endpoints required to run the code.\n2. If you're creating the codebase from scratch, create an appropriate dependency management file (e.g. requirements.txt) with package versions and a helpful README.\n3. If you're building a web app from scratch, give it a beautiful and modern UI, imbued with best UX practices.\n4. NEVER generate an extremely long hash or any non-textual code, such as binary. These are not helpful to the USER and are very expensive.\n5. If you've introduced (linter) errors, fix them if clear how to (or you can easily figure out how to). Do not make uneducated guesses. And DO NOT loop more than 3 times on fixing linter errors on the same file. On the third time, you should stop and ask the user what to do next.\n6. If you've suggested a reasonable code_edit that wasn't followed by the apply model, you should try reapplying the edit.\n\n</making_code_changes>\n\nAnswer the user's request using the relevant tool(s), if they are available. Check that all the required parameters for each tool call are provided or can reasonably be inferred from context. IF there are no relevant tools or there are missing values for required parameters, ask the user to supply these values; otherwise proceed with the tool calls. If the user provides a specific value for a parameter (for example provided in quotes), make sure to use that value EXACTLY. DO NOT make up values for or ask about optional parameters. Carefully analyze descriptive terms in the request as they may indicate required parameter values that should be included even if not explicitly quoted.\n\n<summarization>\nIf you see a section called \"<most_important_user_query>\", you should treat that query as the one to answer, and ignore previous user queries. If you are asked to summarize the conversation, you MUST NOT use any tools, even if they are available. You MUST answer the \"<most_important_user_query>\" query.\n</summarization>\n\n\n\nYou MUST use the following format when citing code regions or blocks:\n```12:15:app/components/Todo.tsx\n// ... existing code ...\n```\nThis is the ONLY acceptable format for code citations. The format is ```startLine:endLine:filepath where startLine and endLine are line numbers.";
|
package/src/common/types.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { IMarker } from '@opensumi/ide-core-browser';
|
|
2
2
|
import { Uri } from '@opensumi/monaco-editor-core';
|
|
3
3
|
|
|
4
|
+
import { ImageCompressionOptions } from './image-compression';
|
|
5
|
+
|
|
4
6
|
export enum NearestCodeBlockType {
|
|
5
7
|
Block = 'block',
|
|
6
8
|
Line = 'line',
|
|
@@ -22,6 +24,12 @@ export interface INearestCodeBlock {
|
|
|
22
24
|
type?: NearestCodeBlockType;
|
|
23
25
|
}
|
|
24
26
|
|
|
27
|
+
export type IMCPToolResultContent = { type: 'text'; text: string } | { type: 'image'; data: string; mimeType?: string };
|
|
28
|
+
export interface IMCPToolResult {
|
|
29
|
+
content: IMCPToolResultContent[];
|
|
30
|
+
isError?: boolean;
|
|
31
|
+
}
|
|
32
|
+
|
|
25
33
|
// SUMI MCP Server 网页部分暴露给 Node.js 部分的能力
|
|
26
34
|
export interface IMCPServerProxyService {
|
|
27
35
|
$callMCPTool(
|
|
@@ -41,6 +49,8 @@ export interface IMCPServerProxyService {
|
|
|
41
49
|
$startServer(serverName: string): Promise<void>;
|
|
42
50
|
// 停止指定的 MCP 服务器
|
|
43
51
|
$stopServer(serverName: string): Promise<void>;
|
|
52
|
+
// 压缩工具结果
|
|
53
|
+
$compressToolResult(result: IMCPToolResult, options: ImageCompressionOptions): Promise<IMCPToolResult>;
|
|
44
54
|
}
|
|
45
55
|
|
|
46
56
|
export interface MCPServer {
|
|
@@ -125,3 +135,11 @@ export interface SSEClientTransportOptions {
|
|
|
125
135
|
*/
|
|
126
136
|
requestInit?: RequestInit;
|
|
127
137
|
}
|
|
138
|
+
|
|
139
|
+
export interface ProjectRule {
|
|
140
|
+
path: string;
|
|
141
|
+
content: string;
|
|
142
|
+
description?: string;
|
|
143
|
+
globs?: string | string[];
|
|
144
|
+
alwaysApply?: boolean;
|
|
145
|
+
}
|
package/src/common/utils.ts
CHANGED
|
@@ -54,8 +54,11 @@ export const extractCodeBlocks = (content: string): string => {
|
|
|
54
54
|
// 确保 Tool Name 符合 Claude 3.5+ Sonnet 要求的 ^[a-zA-Z0-9_-]{1,64}$ 正则
|
|
55
55
|
export const toClaudeToolName = (name: string) => name.replace(/[^a-zA-Z0-9_-]/g, '').slice(0, 64);
|
|
56
56
|
|
|
57
|
+
export const TOOL_NAME_SEPARATOR = '__';
|
|
57
58
|
export const getToolName = (toolName: string, serverName: string) =>
|
|
58
|
-
serverName === BUILTIN_MCP_SERVER_NAME
|
|
59
|
+
serverName === BUILTIN_MCP_SERVER_NAME
|
|
60
|
+
? toolName
|
|
61
|
+
: toClaudeToolName(`mcp${TOOL_NAME_SEPARATOR}${serverName}${TOOL_NAME_SEPARATOR}${toolName}`);
|
|
59
62
|
|
|
60
63
|
export const cleanAttachedTextWrapper = (text: string) => {
|
|
61
64
|
const rgAttachedFile = /`<attached_file>(.*)`/g;
|
|
@@ -1,17 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
CoreMessage,
|
|
3
|
-
CoreUserMessage,
|
|
4
|
-
ImagePart,
|
|
5
|
-
TextPart,
|
|
6
|
-
ToolExecutionOptions,
|
|
7
|
-
jsonSchema,
|
|
8
|
-
streamText,
|
|
9
|
-
tool,
|
|
10
|
-
} from 'ai';
|
|
1
|
+
import { CoreMessage, ImagePart, TextPart, ToolExecutionOptions, jsonSchema, streamText, tool } from 'ai';
|
|
11
2
|
|
|
12
3
|
import { Autowired, Injectable } from '@opensumi/di';
|
|
13
4
|
import { IAIBackServiceOption } from '@opensumi/ide-core-common';
|
|
14
|
-
import { ChatReadableStream } from '@opensumi/ide-core-node';
|
|
5
|
+
import { ChatReadableStream, INodeLogger } from '@opensumi/ide-core-node';
|
|
15
6
|
import { CancellationToken } from '@opensumi/ide-utils';
|
|
16
7
|
|
|
17
8
|
import { ModelInfo } from '../common';
|
|
@@ -28,6 +19,9 @@ export abstract class BaseLanguageModel {
|
|
|
28
19
|
@Autowired(ToolInvocationRegistryManager)
|
|
29
20
|
protected readonly toolInvocationRegistryManager: IToolInvocationRegistryManager;
|
|
30
21
|
|
|
22
|
+
@Autowired(INodeLogger)
|
|
23
|
+
protected readonly logger: INodeLogger;
|
|
24
|
+
|
|
31
25
|
protected abstract initializeProvider(options: IAIBackServiceOption): any;
|
|
32
26
|
|
|
33
27
|
async request(
|
|
@@ -67,8 +61,11 @@ export abstract class BaseLanguageModel {
|
|
|
67
61
|
description: toolRequest.description || '',
|
|
68
62
|
// TODO 这里应该是 z.object 而不是 JSON Schema
|
|
69
63
|
parameters: jsonSchema(toolRequest.parameters),
|
|
70
|
-
execute: async (args: any, options: ToolExecutionOptions) =>
|
|
71
|
-
|
|
64
|
+
execute: async (args: any, options: ToolExecutionOptions) => {
|
|
65
|
+
// 执行原始工具
|
|
66
|
+
const result = await toolRequest.handler(JSON.stringify(args), options);
|
|
67
|
+
return result;
|
|
68
|
+
},
|
|
72
69
|
});
|
|
73
70
|
}
|
|
74
71
|
|
|
@@ -119,7 +116,7 @@ export abstract class BaseLanguageModel {
|
|
|
119
116
|
messages,
|
|
120
117
|
abortSignal: abortController.signal,
|
|
121
118
|
experimental_toolCallStreaming: true,
|
|
122
|
-
maxSteps:
|
|
119
|
+
maxSteps: modelInfo?.maxSteps ?? 25,
|
|
123
120
|
maxTokens,
|
|
124
121
|
system: systemPrompt,
|
|
125
122
|
providerOptions,
|
|
@@ -12,9 +12,10 @@ import { INodeLogger } from '@opensumi/ide-core-node';
|
|
|
12
12
|
|
|
13
13
|
import pkg from '../../../package.json';
|
|
14
14
|
import { BUILTIN_MCP_SERVER_NAME, ISumiMCPServerBackend } from '../../common';
|
|
15
|
+
import { ImageCompressionOptions } from '../../common/image-compression';
|
|
15
16
|
import { IMCPServer, MCPServerDescription } from '../../common/mcp-server-manager';
|
|
16
17
|
import { IToolInvocationRegistryManager, ToolInvocationRegistryManager } from '../../common/tool-invocation-registry';
|
|
17
|
-
import { IMCPServerProxyService, MCPTool, MCP_SERVER_TYPE } from '../../common/types';
|
|
18
|
+
import { IMCPServerProxyService, IMCPToolResult, MCPTool, MCP_SERVER_TYPE } from '../../common/types';
|
|
18
19
|
import { MCPServerManagerImpl } from '../mcp-server-manager-impl';
|
|
19
20
|
import { SSEMCPServer } from '../mcp-server.sse';
|
|
20
21
|
import { StdioMCPServer } from '../mcp-server.stdio';
|
|
@@ -43,7 +44,7 @@ export class SumiMCPServerBackend extends RPCService<IMCPServerProxyService> imp
|
|
|
43
44
|
|
|
44
45
|
constructor() {
|
|
45
46
|
super();
|
|
46
|
-
this.mcpServerManager = new MCPServerManagerImpl(this.toolInvocationRegistryManager, this.logger);
|
|
47
|
+
this.mcpServerManager = new MCPServerManagerImpl(this.toolInvocationRegistryManager, this.logger, this);
|
|
47
48
|
}
|
|
48
49
|
|
|
49
50
|
public setConnectionClientId(clientId: string) {
|
|
@@ -51,6 +52,13 @@ export class SumiMCPServerBackend extends RPCService<IMCPServerProxyService> imp
|
|
|
51
52
|
this.mcpServerManager.setClientId(clientId);
|
|
52
53
|
}
|
|
53
54
|
|
|
55
|
+
async $compressToolResult(result: IMCPToolResult, options: ImageCompressionOptions) {
|
|
56
|
+
if (!this.client) {
|
|
57
|
+
throw new Error('SUMI MCP RPC Client not initialized');
|
|
58
|
+
}
|
|
59
|
+
return this.client.$compressToolResult(result, options);
|
|
60
|
+
}
|
|
61
|
+
|
|
54
62
|
async $getMCPTools() {
|
|
55
63
|
if (!this.client) {
|
|
56
64
|
throw new Error('SUMI MCP RPC Client not initialized');
|
|
@@ -3,14 +3,16 @@ import { ToolExecutionOptions } from 'ai';
|
|
|
3
3
|
import { ILogger } from '@opensumi/ide-core-common';
|
|
4
4
|
import { getShellPath } from '@opensumi/ide-core-node';
|
|
5
5
|
|
|
6
|
+
import { ISumiMCPServerBackend } from '../common';
|
|
6
7
|
import { IMCPServer, MCPServerDescription, MCPServerManager, MCPTool } from '../common/mcp-server-manager';
|
|
7
8
|
import { IToolInvocationRegistryManager, ToolRequest } from '../common/tool-invocation-registry';
|
|
8
|
-
import { MCP_SERVER_TYPE } from '../common/types';
|
|
9
|
+
import { IMCPToolResult, MCP_SERVER_TYPE } from '../common/types';
|
|
9
10
|
import { getToolName } from '../common/utils';
|
|
10
11
|
|
|
11
12
|
import { BuiltinMCPServer } from './mcp/sumi-mcp-server';
|
|
12
13
|
import { SSEMCPServer } from './mcp-server.sse';
|
|
13
14
|
import { StdioMCPServer } from './mcp-server.stdio';
|
|
15
|
+
|
|
14
16
|
// 这应该是 Browser Tab 维度的,每个 Tab 对应一个 MCPServerManagerImpl
|
|
15
17
|
export class MCPServerManagerImpl implements MCPServerManager {
|
|
16
18
|
protected servers: Map<string, IMCPServer> = new Map();
|
|
@@ -27,6 +29,7 @@ export class MCPServerManagerImpl implements MCPServerManager {
|
|
|
27
29
|
constructor(
|
|
28
30
|
private readonly toolInvocationRegistryManager: IToolInvocationRegistryManager,
|
|
29
31
|
private readonly logger: ILogger,
|
|
32
|
+
private readonly proxy: ISumiMCPServerBackend,
|
|
30
33
|
) {}
|
|
31
34
|
|
|
32
35
|
async updateShellPath() {
|
|
@@ -110,7 +113,19 @@ export class MCPServerManagerImpl implements MCPServerManager {
|
|
|
110
113
|
const res = await this.callTool(serverName, tool.name, options?.toolCallId || '', arg_string);
|
|
111
114
|
this.logger.debug(`[MCP: ${serverName}] ${tool.name} called with ${arg_string}`);
|
|
112
115
|
this.logger.debug('Tool execution result:', res);
|
|
113
|
-
|
|
116
|
+
let compressedResult: IMCPToolResult = res as IMCPToolResult;
|
|
117
|
+
if (
|
|
118
|
+
this.proxy?.$compressToolResult &&
|
|
119
|
+
(compressedResult.content || []).some((item) => item.type === 'image')
|
|
120
|
+
) {
|
|
121
|
+
compressedResult = await this.proxy.$compressToolResult(res as IMCPToolResult, {
|
|
122
|
+
maxSizeKB: 100,
|
|
123
|
+
maxWidth: 600,
|
|
124
|
+
quality: 0.6,
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
this.logger.debug('Compressed tool execution result:', compressedResult);
|
|
128
|
+
return JSON.stringify(compressedResult);
|
|
114
129
|
} catch (error) {
|
|
115
130
|
this.logger.error(`Error in tool handler for ${tool.name} on MCP server ${serverName}:`, error);
|
|
116
131
|
throw error;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// have to import with extension since the exports map is ./* -> ./dist/cjs/*
|
|
2
2
|
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
|
|
3
|
+
import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js';
|
|
3
4
|
import { EventSource } from 'eventsource';
|
|
4
5
|
|
|
5
6
|
import { ILogger } from '@opensumi/ide-core-common';
|
|
@@ -47,8 +48,6 @@ export class SSEMCPServer implements IMCPServer {
|
|
|
47
48
|
}
|
|
48
49
|
this.logger?.log(`Starting server "${this.name}" with url: ${this.url}`);
|
|
49
50
|
|
|
50
|
-
const SSEClientTransport = (await import('@modelcontextprotocol/sdk/client/sse.js')).SSEClientTransport;
|
|
51
|
-
|
|
52
51
|
const transport = new SSEClientTransport(new URL(this.url), this.transportOptions);
|
|
53
52
|
|
|
54
53
|
transport.onerror = (error) => {
|