Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,24 @@ export const MentionInput: React.FC<MentionInputProps> = ({
[attachedFiles],
);

const renderModelSelectorTip = React.useCallback(
(children: React.ReactNode) => {
if (footerConfig.disableModelSelector) {
return (
<Popover
id='ai-chat-model-selector'
title={localize('aiNative.chat.modelSelector.disableTip')}
position={PopoverPosition.top}
>
{children}
</Popover>
);
}
return children;
},
[footerConfig.disableModelSelector],
);

return (
<div className={styles.input_container}>
{mentionState.active && (
Expand Down Expand Up @@ -1048,16 +1066,17 @@ export const MentionInput: React.FC<MentionInputProps> = ({
</div>
<div className={styles.footer}>
<div className={styles.left_control}>
{footerConfig.showModelSelector && (
<Select
options={footerConfig.modelOptions || []}
value={selectedModel}
onChange={handleModelChange}
className={styles.model_selector}
size='small'
disabled={footerConfig.disableModelSelector}
/>
)}
{footerConfig.showModelSelector &&
renderModelSelectorTip(
<Select
options={footerConfig.modelOptions || []}
value={selectedModel}
onChange={handleModelChange}
className={styles.model_selector}
size='small'
disabled={footerConfig.disableModelSelector}
/>,
)}
{renderButtons(FooterButtonPosition.LEFT)}
</div>
<div className={styles.right_control}>
Expand Down
34 changes: 24 additions & 10 deletions packages/ai-native/src/browser/context/llm-context.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,16 @@ export class LLMContextServiceImpl extends WithEventBus implements LLMContextSer
attachedFolders: FileContext[];
version: number;
}>();
private hasUserManualReference = false;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
onDidContextFilesChangeEvent = this.onDidContextFilesChangeEmitter.event;

private addFileToList(file: FileContext, list: FileContext[], maxLimit: number) {
const existingIndex = list.findIndex((f) => f.uri.toString() === file.uri.toString());
const existingIndex = list.findIndex(
(f) =>
f.uri.toString() === file.uri.toString() &&
f.selection?.[0] === file.selection?.[0] &&
f.selection?.[1] === file.selection?.[1],
);
if (existingIndex > -1) {
list.splice(existingIndex, 1);
}
Expand Down Expand Up @@ -79,6 +85,10 @@ export class LLMContextServiceImpl extends WithEventBus implements LLMContextSer
return;
}

if (isManual) {
this.hasUserManualReference = true;
}

Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
const file = { uri, selection };
const targetList = isManual ? this.attachedFiles : this.recentlyViewFiles;
const maxLimit = isManual ? this.maxAttachFilesLimit : this.maxViewFilesLimit;
Expand Down Expand Up @@ -109,6 +119,7 @@ export class LLMContextServiceImpl extends WithEventBus implements LLMContextSer
cleanFileContext() {
this.attachedFiles = [];
this.attachedFolders = [];
this.hasUserManualReference = false;
this.notifyContextChange();
}

Expand Down Expand Up @@ -176,14 +187,17 @@ export class LLMContextServiceImpl extends WithEventBus implements LLMContextSer
event.payload.selections[0].positionLineNumber,
].sort() as [number, number];

if (selection[0] === selection[1]) {
this.addFileToContext(event.payload.editorUri, undefined, false);
} else {
this.addFileToContext(
event.payload.editorUri,
selection.sort((a, b) => a - b),
false,
);
if (!this.hasUserManualReference) {
// 当没有用户手动引用时,才自动收集
if (selection[0] === selection[1]) {
this.addFileToContext(event.payload.editorUri, undefined, false);
} else {
this.addFileToContext(
event.payload.editorUri,
selection.sort((a, b) => a - b),
false,
);
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}
}),
Expand Down Expand Up @@ -276,7 +290,7 @@ export class LLMContextServiceImpl extends WithEventBus implements LLMContextSer

return {
content: ref.instance.getText(
file.selection && new Range(file.selection[0], Infinity, file.selection[1], Infinity),
file.selection && new Range(file.selection[0], 0, file.selection[1] + 1, Infinity),
),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

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.

Suggested change
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.

lineErrors: this.getFileErrors(file.uri),
path: workspaceRoot.relative(file.uri)!.toString(),
Expand Down
11 changes: 4 additions & 7 deletions packages/ai-native/src/common/prompts/context-prompt-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ export class DefaultChatAgentPromptProvider implements ChatAgentPromptProvider {
protected readonly workspaceService: IWorkspaceService;

async provideContextPrompt(context: SerializedContext, userMessage: string) {
const currentFileInfo = await this.getCurrentFileInfo();
const currentFileInfo =
context.attachedFiles.length > 0 || context.attachedFolders.length > 0 ? null : await this.getCurrentFileInfo();

return this.buildPromptTemplate({
recentFiles: this.buildRecentFilesSection(context.recentlyViewFiles),
attachedFiles: this.buildAttachedFilesSection(context.attachedFiles, context.recentlyViewFiles),
attachedFiles: this.buildAttachedFilesSection(context.attachedFiles),
attachedFolders: this.buildAttachedFoldersSection(context.attachedFolders),
currentFile: currentFileInfo,
userMessage,
Expand Down Expand Up @@ -91,11 +92,7 @@ ${files.map((file, idx) => ` ${idx + 1}: ${file}`).join('\n')}
</recently_viewed_files>`;
}

private buildAttachedFilesSection(
files: { path: string; content: string; lineErrors: string[] }[],
recentlyViewFiles: string[],
): string {
files = files.filter((file) => !recentlyViewFiles.includes(file.path));
private buildAttachedFilesSection(files: { path: string; content: string; lineErrors: string[] }[]): string {
if (!files.length) {
return '';
}
Expand Down
2 changes: 2 additions & 0 deletions packages/i18n/src/common/en-US.lang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1466,6 +1466,8 @@ export const localizationBundle = {
'aiNative.chat.clearContext': 'Clear Context',
'aiNative.chat.context.description': 'Total {0} References',
'aiNative.chat.context.clear': 'Clear References',
'aiNative.chat.modelSelector.disableTip': 'Clear or create session to change model',

'aiNative.inline.chat.operate.chat.title': 'Chat({0})',
'aiNative.inline.chat.operate.check.title': 'Check',
'aiNative.inline.chat.operate.thumbsup.title': 'Thumbs up',
Expand Down
1 change: 1 addition & 0 deletions packages/i18n/src/common/zh-CN.lang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1234,6 +1234,7 @@ export const localizationBundle = {
'aiNative.chat.clearContext': '清空上下文',
'aiNative.chat.context.description': '共 {0} 个引用',
'aiNative.chat.context.clear': '点击清空引用',
'aiNative.chat.modelSelector.disableTip': '如需切换模型,请新建会话或清空当前会话',

'aiNative.inline.chat.operate.chat.title': 'Chat({0})',
'aiNative.inline.chat.operate.check.title': '采纳',
Expand Down
Loading