Skip to content

Commit ff4d0c0

Browse files
Merge pull request #5670 from uinstinct/current-file
feat: add current file context automatically to chat
2 parents 4f66d94 + 835d4eb commit ff4d0c0

File tree

7 files changed

+86
-5
lines changed

7 files changed

+86
-5
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"extensions/.continue-debug": true
4646
// "sync/**": true
4747
},
48+
"editor.formatOnSave": true,
4849
"eslint.workingDirectories": ["./core"],
4950
"typescript.tsdk": "node_modules/typescript/lib",
5051
"conventionalCommits.showNewVersionNotes": false,

core/config/sharedConfig.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export const sharedConfigSchema = z
1818
useChromiumForDocsCrawling: z.boolean(),
1919
readResponseTTS: z.boolean(),
2020
promptPath: z.string(),
21+
useCurrentFileAsContext: z.boolean(),
2122

2223
// `ui` in `ContinueConfig`
2324
showSessionTabs: z.boolean(),
@@ -166,6 +167,10 @@ export function modifyAnyConfigWithSharedConfig<
166167
if (sharedConfig.readResponseTTS !== undefined) {
167168
configCopy.experimental.readResponseTTS = sharedConfig.readResponseTTS;
168169
}
170+
if (sharedConfig.useCurrentFileAsContext !== undefined) {
171+
configCopy.experimental.useCurrentFileAsContext =
172+
sharedConfig.useCurrentFileAsContext;
173+
}
169174

170175
return configCopy;
171176
}

core/index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,6 +1334,11 @@ export interface ExperimentalConfig {
13341334
*/
13351335
useChromiumForDocsCrawling?: boolean;
13361336
modelContextProtocolServers?: ExperimentalMCPOptions[];
1337+
1338+
/**
1339+
* If enabled, will add the current file as context.
1340+
*/
1341+
useCurrentFileAsContext?: boolean;
13371342
}
13381343

13391344
export interface AnalyticsConfig {

extensions/vscode/src/VsCodeIde.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,10 @@ class VsCodeIde implements IDE {
335335
const pathToLastModified: FileStatsMap = {};
336336
await Promise.all(
337337
files.map(async (file) => {
338-
const stat = await this.ideUtils.stat(vscode.Uri.parse(file), false /* No need to catch ENOPRO exceptions */);
338+
const stat = await this.ideUtils.stat(
339+
vscode.Uri.parse(file),
340+
false /* No need to catch ENOPRO exceptions */,
341+
);
339342
pathToLastModified[file] = {
340343
lastModified: stat!.mtime,
341344
size: stat!.size,
@@ -401,7 +404,8 @@ class VsCodeIde implements IDE {
401404
const configs: ContinueRcJson[] = [];
402405
for (const workspaceDir of workspaceDirs) {
403406
const files = await this.ideUtils.readDirectory(workspaceDir);
404-
if (files === null) {//Unlikely, but just in case...
407+
if (files === null) {
408+
//Unlikely, but just in case...
405409
continue;
406410
}
407411
for (const [filename, type] of files) {
@@ -753,7 +757,7 @@ class VsCodeIde implements IDE {
753757

754758
async listDir(dir: string): Promise<[string, FileType][]> {
755759
const entries = await this.ideUtils.readDirectory(vscode.Uri.parse(dir));
756-
return entries === null? [] : entries as any;
760+
return entries === null ? [] : (entries as any);
757761
}
758762

759763
private getIdeSettingsSync(): IdeSettings {

gui/src/components/mainInput/TipTapEditor/useMainEditorWebviewListeners.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import { InputModifiers } from "core";
33
import { rifWithContentsToContextItem } from "core/commands/util";
44
import { MutableRefObject } from "react";
55
import { useWebviewListener } from "../../../hooks/useWebviewListener";
6+
import { useAppSelector } from "../../../redux/hooks";
67
import { clearCodeToEdit } from "../../../redux/slices/editState";
78
import { setNewestToolbarPreviewForInput } from "../../../redux/slices/sessionSlice";
89
import { AppDispatch } from "../../../redux/store";
910
import { loadSession, saveCurrentSession } from "../../../redux/thunks/session";
1011
import { CodeBlock, PromptBlock } from "./extensions";
12+
import { insertCurrentFileContextMention } from "./utils/insertCurrentFileContextMention";
1113

1214
/**
1315
* Hook for setting up main editor specific webview listeners
@@ -27,6 +29,14 @@ export function useMainEditorWebviewListeners({
2729
inputId: string;
2830
editorFocusedRef: MutableRefObject<boolean | undefined>;
2931
}) {
32+
const activeContextProviders = useAppSelector(
33+
(state) => state.config.config.contextProviders,
34+
);
35+
const useCurrentFileAsContext = useAppSelector(
36+
(state) => state.config.config.experimental?.useCurrentFileAsContext,
37+
);
38+
const isInEdit = useAppSelector((state) => state.session.isInEdit);
39+
3040
useWebviewListener(
3141
"isContinueInputFocused",
3242
async () => {
@@ -178,4 +188,14 @@ export function useMainEditorWebviewListeners({
178188
},
179189
[],
180190
);
191+
192+
useWebviewListener(
193+
"newSession",
194+
async () => {
195+
// do not insert current file context mention if we are in edit mode or if addFileContext is disabled
196+
if (!editor || isInEdit || !useCurrentFileAsContext) return;
197+
insertCurrentFileContextMention(editor, activeContextProviders);
198+
},
199+
[editor, activeContextProviders, isInEdit, useCurrentFileAsContext],
200+
);
181201
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { Editor } from "@tiptap/core";
2+
import { ContextProviderDescription } from "core";
3+
import * as ContinueExtensions from "../extensions";
4+
5+
/**
6+
* inserts the current file context as a removable mention in the editor area
7+
*
8+
* uses tiptap's `create` method to create a new node under the hood
9+
*/
10+
export function insertCurrentFileContextMention(
11+
editor: Editor,
12+
contextProviders: ContextProviderDescription[],
13+
) {
14+
const foundCurrentFileProvider = contextProviders.find(
15+
(provider) => provider.title === "currentFile",
16+
);
17+
18+
if (foundCurrentFileProvider) {
19+
const node = editor.schema.nodes[ContinueExtensions.Mention.name].create({
20+
name: foundCurrentFileProvider.displayTitle,
21+
description: foundCurrentFileProvider.description,
22+
id: foundCurrentFileProvider.title,
23+
label: foundCurrentFileProvider.displayTitle,
24+
renderInlineAs: foundCurrentFileProvider.renderInlineAs,
25+
type: foundCurrentFileProvider.type,
26+
itemType: "contextProvider",
27+
});
28+
29+
editor
30+
.chain()
31+
.insertContent([node.toJSON(), { type: "text", text: " " }]) // add a space after the mention
32+
.run();
33+
}
34+
}

gui/src/pages/config/UserSettingsForm.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ export function UserSettingsForm() {
7676
const autoAcceptEditToolDiffs = config.ui?.autoAcceptEditToolDiffs ?? false;
7777
const displayRawMarkdown = config.ui?.displayRawMarkdown ?? false;
7878
const disableSessionTitles = config.disableSessionTitles ?? false;
79+
const useCurrentFileAsContext =
80+
config.experimental?.useCurrentFileAsContext ?? false;
7981

8082
const allowAnonymousTelemetry = config.allowAnonymousTelemetry ?? true;
8183
const disableIndexing = config.disableIndexing ?? false;
@@ -336,7 +338,7 @@ export function UserSettingsForm() {
336338
</form>
337339
</div>
338340

339-
<div className="flex flex-col gap-2">
341+
<div className="flex flex-col gap-x-2 gap-y-4">
340342
<div
341343
className="flex cursor-pointer items-center gap-2 text-left text-sm font-semibold"
342344
onClick={() => setShowExperimental(!showExperimental)}
@@ -353,7 +355,7 @@ export function UserSettingsForm() {
353355
showExperimental ? "max-h-40" : "max-h-0"
354356
}`}
355357
>
356-
<div className="flex flex-col gap-1 pl-6">
358+
<div className="flex flex-col gap-x-1 gap-y-4 pl-6">
357359
<ToggleSwitch
358360
isToggled={autoAcceptEditToolDiffs}
359361
onToggle={() =>
@@ -374,6 +376,16 @@ export function UserSettingsForm() {
374376
</>
375377
}
376378
/>
379+
380+
<ToggleSwitch
381+
isToggled={useCurrentFileAsContext}
382+
onToggle={() =>
383+
handleUpdate({
384+
useCurrentFileAsContext: !useCurrentFileAsContext,
385+
})
386+
}
387+
text="Add Current File by Default"
388+
/>
377389
</div>
378390
</div>
379391
</div>

0 commit comments

Comments
 (0)