feat: improve LLM context service enhance logic#4527
Conversation
|
/next |
## Walkthrough
本次更改主要涉及 AI 聊天界面模型选择器的禁用提示优化、文件上下文管理逻辑调整、上下文提示生成逻辑的简化,以及中英文本地化字符串的新增。具体包括:为模型选择器添加禁用时的提示气泡,优化文件手动/自动引用的判断与去重,以及调整上下文提示中附件文件的处理逻辑。同时对进度条相关组件增加空值判断,防止运行时错误。服务器配置保存逻辑也做了调用顺序调整。
## Changes
| 文件路径/分组 | 变更摘要 |
|---|---|
| packages/ai-native/src/browser/components/mention-input/mention-input.tsx | 新增 `renderModelSelectorTip` 回调,模型选择器被禁用时包裹 Popover 提示,提示内容本地化。 |
| packages/ai-native/src/browser/context/llm-context.service.ts | 新增 `hasUserManualReference` 标志,手动添加文件时设置,清理上下文时重置。自动收集文件受此标志控制。文件去重逻辑增加 selection 范围判断,序列化附件时文本提取范围优化。 |
| packages/ai-native/src/common/prompts/context-prompt-provider.ts | `provideContextPrompt` 方法优化:如有附件则跳过当前文件信息获取。`buildAttachedFilesSection` 方法签名变更,移除 recentlyViewFiles 参数及相关过滤逻辑。 |
| packages/i18n/src/common/en-US.lang.ts<br>packages/i18n/src/common/zh-CN.lang.ts | 新增本地化字符串 `aiNative.chat.modelSelector.disableTip`,分别为英文和中文提示。 |
| packages/core-browser/src/progress/progress-bar.tsx<br>packages/main-layout/src/browser/accordion/section.view.tsx<br>packages/main-layout/src/browser/tabbar/bar.view.tsx<br>packages/quick-open/src/browser/quick-open.view.tsx | ProgressBar 及相关组件中对 `progressModel` 和 `indicator` 的访问增加了可选链或空值判断,防止未定义时的运行时错误。 |
| packages/ai-native/src/browser/mcp/config/mcp-config.service.ts | 调整 `saveServer` 方法中服务器配置保存与调用顺序,取消部分 await,先更新偏好设置再异步调用服务端接口。 |
| packages/ai-native/src/browser/mcp/mcp-preferences-contribution.ts | 移除未使用的导入 `CodeSchemaId` 和 `IJSONSchema`,简化导入语句。 |
| packages/components/src/style/variable.less | 提升 `@zindex-modal` 和 `@zindex-modal-mask` 的 z-index 值,从 1000 增加到 10000,调整模态框及遮罩层的层级关系。 |
| packages/ai-native/src/node/mcp-server.stdio.ts | 构造函数中针对 `command` 参数为 `'node'` 时,改为使用环境变量 `NODE_BINARY_PATH` 指定的路径或默认 `'node'`,否则直接赋值。更新方法中同样处理。 |
## Sequence Diagram(s)
```mermaid
sequenceDiagram
participant User as 用户
participant MentionInput as MentionInput组件
participant Popover as Popover提示
participant Select as 模型选择器
User->>MentionInput: 打开模型选择器
MentionInput->>MentionInput: 判断是否禁用
alt 被禁用
MentionInput->>Popover: 显示禁用提示
Popover->>Select: 包裹模型选择器
else 可用
MentionInput->>Select: 直接渲染模型选择器
endsequenceDiagram
participant User as 用户
participant LLMContextService as 上下文服务
participant Editor as 编辑器
User->>LLMContextService: 手动添加文件引用
LLMContextService->>LLMContextService: 设置 hasUserManualReference = true
Editor->>LLMContextService: 触发自动收集文件
LLMContextService->>LLMContextService: 检查 hasUserManualReference
alt 已手动引用
LLMContextService-->>Editor: 跳过自动添加
else 未手动引用
LLMContextService->>LLMContextService: 自动添加文件
end
User->>LLMContextService: 清理上下文
LLMContextService->>LLMContextService: 重置 hasUserManualReference = false
Suggested reviewers
|
There was a problem hiding this comment.
Actionable comments posted: 4
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (5)
packages/ai-native/src/browser/components/mention-input/mention-input.tsx(2 hunks)packages/ai-native/src/browser/context/llm-context.service.ts(5 hunks)packages/ai-native/src/common/prompts/context-prompt-provider.ts(2 hunks)packages/i18n/src/common/en-US.lang.ts(1 hunks)packages/i18n/src/common/zh-CN.lang.ts(1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
packages/ai-native/src/browser/context/llm-context.service.ts (1)
packages/ai-native/src/common/llm-context.ts (1)
FileContext(49-52)
⏰ Context from checks skipped due to timeout of 90000ms (5)
- GitHub Check: 🚀🚀🚀 Next Version for pull request
- GitHub Check: build-windows
- GitHub Check: build (ubuntu-latest, 20.x)
- GitHub Check: build (macos-latest, 20.x)
- GitHub Check: ubuntu-latest, Node.js 20.x
🔇 Additional comments (7)
packages/ai-native/src/browser/context/llm-context.service.ts (1)
55-60: 去重逻辑细化 👍比较条件加入了
selection起止行,能有效避免“同文件不同选区”产生的重复条目,值得肯定。packages/ai-native/src/common/prompts/context-prompt-provider.ts (2)
26-28: 当存在附加文件/文件夹时忽略当前文件信息——请确认符合预期如果用户手动附加了文件,但仍希望模型同时获得当前文件上下文,新的逻辑将不再满足需求。
建议评估业务场景:是否需要通过设置项或参数保留旧行为。
95-112:buildAttachedFilesSection精简入参 👍移除了对
recentlyViewFiles的过滤,逻辑更直观、实现更简洁。packages/i18n/src/common/zh-CN.lang.ts (1)
1237-1237: 新增模型选择器禁用提示已就绪文案贴切,键名也保持了命名一致性,无其他问题。
packages/i18n/src/common/en-US.lang.ts (1)
1469-1470: 本地化字符串已添加,确保中文版本也同步更新新增的本地化字符串为模型选择器被禁用时提供了清晰的提示信息,告诉用户需要清除或创建新会话才能更改模型。
packages/ai-native/src/browser/components/mention-input/mention-input.tsx (2)
1022-1038: 实现良好的气泡提示功能
renderModelSelectorTip函数实现了条件性地为禁用状态的模型选择器添加气泡提示,代码结构清晰。使用React.useCallback进行了适当的性能优化,依赖项设置正确,确保只在footerConfig.disableModelSelector变化时重新创建函数。
1069-1079: 模型选择器渲染逻辑优化渲染代码已适当更新,使用新的
renderModelSelectorTip函数来条件性地添加提示气泡。这种实现提升了用户体验,当模型选择器被禁用时,用户能够看到明确的提示信息。
| file.selection && new Range(file.selection[0], 0, file.selection[1] + 1, Infinity), | ||
| ), |
There was a problem hiding this comment.
Range 起始列应从 1 开始,且不宜多加一行
Monaco 的列号从 1 开始,传入 0 可能触发异常;
同时把 endLineNumber 设为 selection[1] + 1 会越界,进而导致取文本失败。
- file.selection && new Range(file.selection[0], 0, file.selection[1] + 1, Infinity),
+ file.selection && new Range(file.selection[0], 1, file.selection[1], Infinity),📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| file.selection && new Range(file.selection[0], 0, file.selection[1] + 1, Infinity), | |
| ), | |
| file.selection && new Range(file.selection[0], 1, file.selection[1], Infinity), | |
| ), |
🤖 Prompt for AI Agents (early access)
In packages/ai-native/src/browser/context/llm-context.service.ts at lines
293-294, the Range constructor uses a start column of 0 and an end line number
of selection[1] + 1, which is incorrect. Change the start column to 1 since
Monaco columns start at 1, and set the end line number to selection[1] without
adding 1 to avoid out-of-bounds errors.
|
🎉 PR Next publish successful! 3.8.3-next-1746609719.0 |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4527 +/- ##
==========================================
- Coverage 53.02% 53.02% -0.01%
==========================================
Files 1669 1669
Lines 102941 102951 +10
Branches 22299 22308 +9
==========================================
+ Hits 54583 54588 +5
Misses 40228 40228
- Partials 8130 8135 +5
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
/next |
|
🎉 PR Next publish successful! 3.8.3-next-1746611266.0 |
|
/next |
|
🎉 PR Next publish successful! 3.8.3-next-1746617857.0 |
|
/next |
There was a problem hiding this comment.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
packages/ai-native/src/browser/mcp/config/mcp-config.service.ts(1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
packages/ai-native/src/browser/mcp/config/mcp-config.service.ts (1)
packages/ai-native/src/common/mcp-server-manager.ts (1)
MCPServerDescription(95-98)
⏰ Context from checks skipped due to timeout of 90000ms (9)
- GitHub Check: 🚀🚀🚀 Next Version for pull request
- GitHub Check: build (ubuntu-latest, 20.x)
- GitHub Check: build (macos-latest, 20.x)
- GitHub Check: build-windows
- GitHub Check: unittest (ubuntu-latest, 18.x, node)
- GitHub Check: unittest (ubuntu-latest, 18.x, jsdom)
- GitHub Check: unittest (macos-latest, 18.x, jsdom)
- GitHub Check: unittest (macos-latest, 18.x, node)
- GitHub Check: ubuntu-latest, Node.js 20.x
| await this.preferenceService.set('mcp', { mcpServers: servers }); | ||
| if (prev?.enabled) { | ||
| await this.sumiMCPServerBackendProxy.$removeServer(prev.name); | ||
| this.sumiMCPServerBackendProxy.$removeServer(prev.name); | ||
| } | ||
| await this.preferenceService.set('mcp', { mcpServers: servers }); | ||
| this.sumiMCPServerBackendProxy.$addOrUpdateServer(data as MCPServerDescription); |
There was a problem hiding this comment.
🛠️ Refactor suggestion
调整了保存服务器配置的操作顺序和异步行为
代码改动将首先更新偏好设置,然后异步执行后端服务器操作而不等待完成。这种变更有以下影响:
- 用户界面会立即反映新配置,提升了感知响应速度
- 由于不再等待后端操作完成,方法会更快返回
- 后端操作失败时用户不会立即收到通知
建议添加错误处理机制以捕获后端操作可能出现的错误:
await this.preferenceService.set('mcp', { mcpServers: servers });
if (prev?.enabled) {
- this.sumiMCPServerBackendProxy.$removeServer(prev.name);
+ this.sumiMCPServerBackendProxy.$removeServer(prev.name).catch(error => {
+ this.logger.error(`无法移除服务器 ${prev.name}:`, error);
+ this.messageService.error(localize('ai.native.mcp.error.remove.server', '无法移除服务器: {0}', error.message || error));
+ });
}
-this.sumiMCPServerBackendProxy.$addOrUpdateServer(data as MCPServerDescription);
+this.sumiMCPServerBackendProxy.$addOrUpdateServer(data as MCPServerDescription).catch(error => {
+ this.logger.error(`无法添加或更新服务器 ${data.name}:`, error);
+ this.messageService.error(localize('ai.native.mcp.error.add.server', '无法添加或更新服务器: {0}', error.message || error));
+});📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| await this.preferenceService.set('mcp', { mcpServers: servers }); | |
| if (prev?.enabled) { | |
| await this.sumiMCPServerBackendProxy.$removeServer(prev.name); | |
| this.sumiMCPServerBackendProxy.$removeServer(prev.name); | |
| } | |
| await this.preferenceService.set('mcp', { mcpServers: servers }); | |
| this.sumiMCPServerBackendProxy.$addOrUpdateServer(data as MCPServerDescription); | |
| await this.preferenceService.set('mcp', { mcpServers: servers }); | |
| if (prev?.enabled) { | |
| this.sumiMCPServerBackendProxy.$removeServer(prev.name).catch(error => { | |
| this.logger.error(`无法移除服务器 ${prev.name}:`, error); | |
| this.messageService.error( | |
| localize('ai.native.mcp.error.remove.server', '无法移除服务器: {0}', error.message || error) | |
| ); | |
| }); | |
| } | |
| this.sumiMCPServerBackendProxy.$addOrUpdateServer(data as MCPServerDescription).catch(error => { | |
| this.logger.error(`无法添加或更新服务器 ${data.name}:`, error); | |
| this.messageService.error( | |
| localize('ai.native.mcp.error.add.server', '无法添加或更新服务器: {0}', error.message || error) | |
| ); | |
| }); |
🤖 Prompt for AI Agents (early access)
In packages/ai-native/src/browser/mcp/config/mcp-config.service.ts around lines
207 to 211, the code updates preferences first and then calls backend server
operations asynchronously without awaiting them, which improves UI
responsiveness but loses error handling for backend failures. Modify the code to
add try-catch blocks around the backend calls to $removeServer and
$addOrUpdateServer, and handle or log any errors to ensure backend operation
failures are captured and can be addressed.
|
🎉 PR Next publish successful! 3.8.3-next-1746621593.0 |
|
/next |
|
🎉 PR Next publish successful! 3.8.3-next-1746625865.0 |
|
/next |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
packages/ai-native/src/node/mcp-server.stdio.ts (1)
27-27: 建议增加环境变量路径验证当使用
NODE_BINARY_PATH环境变量时,目前没有验证该路径是否指向有效的可执行文件。如果环境变量设置了无效路径,可能会导致服务器启动失败,而错误信息可能不够明确。建议添加一个辅助方法来验证可执行文件路径的有效性:
+ import { existsSync, accessSync, constants } from 'fs'; + import { resolve } from 'path'; constructor( name: string, command: string, args?: string[], env?: Record<string, string>, private readonly logger?: ILogger, ) { this.name = name; - this.command = command === 'node' ? process.env.NODE_BINARY_PATH || 'node' : command; + if (command === 'node' && process.env.NODE_BINARY_PATH) { + const nodePath = process.env.NODE_BINARY_PATH; + try { + // 验证路径是否存在且可执行 + const fullPath = resolve(nodePath); + if (existsSync(fullPath)) { + accessSync(fullPath, constants.X_OK); + this.command = nodePath; + } else { + this.logger?.warn(`环境变量 NODE_BINARY_PATH 设置的路径 "${nodePath}" 不存在,将使用默认的 "node" 命令`); + this.command = 'node'; + } + } catch (error) { + this.logger?.warn(`环境变量 NODE_BINARY_PATH 设置的路径 "${nodePath}" 不可执行,将使用默认的 "node" 命令`, error); + this.command = 'node'; + } + } else { + this.command = command; + } this.args = args; this.env = env; }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
packages/ai-native/src/node/mcp-server.stdio.ts(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (9)
- GitHub Check: unittest (macos-latest, 18.x, jsdom)
- GitHub Check: unittest (ubuntu-latest, 18.x, jsdom)
- GitHub Check: unittest (ubuntu-latest, 18.x, node)
- GitHub Check: unittest (macos-latest, 18.x, node)
- GitHub Check: 🚀🚀🚀 Next Version for pull request
- GitHub Check: ubuntu-latest, Node.js 20.x
- GitHub Check: build-windows
- GitHub Check: build (ubuntu-latest, 20.x)
- GitHub Check: build (macos-latest, 20.x)
|
🎉 PR Next publish successful! 3.8.3-next-1746626195.0 |
|
/next |
|
🎉 PR Next publish successful! 3.8.3-next-1746629393.0 |
Types
Background or solution
优化 LLM 上下文管理,当用户指定文件时,不再引入当前打开的文档内容
Changelog
improve LLM context service enhance logic
Summary by CodeRabbit
新功能
优化