Skip to content

Commit cb049cb

Browse files
committed
refactor: changes for reset
1 parent 1b50390 commit cb049cb

File tree

3 files changed

+32
-62
lines changed

3 files changed

+32
-62
lines changed

src/core/webview/ClineProvider.ts

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ import { deleteFromContextDirectory } from "../../utils/delete-helper"
3838
import delay from "delay"
3939
import { AutoApprovalSettings, DEFAULT_AUTO_APPROVAL_SETTINGS } from "../../shared/AutoApprovalSettings"
4040
import { HaiBuildDefaults } from "../../shared/haiDefaults"
41-
import chokidar, { FSWatcher } from "chokidar"
4241
import { buildEmbeddingHandler } from "../../embedding"
43-
import { existsSync } from "fs"
4442

4543
/*
4644
https://github.com/microsoft/vscode-webview-ui-toolkit-samples/blob/main/default/weather-webview/src/providers/WeatherViewProvider.ts
@@ -801,13 +799,19 @@ export class ClineProvider implements vscode.WebviewViewProvider {
801799
await fs.writeFile(filePath, fileInstruction.content, "utf8");
802800
}
803801
}
802+
vscode.window.showInformationMessage(`${message.fileInstructions.length} files uploaded successfully`);
804803
}
805804
break;
806805
case "deleteInstruction":
807806
const dir = path.join(this.vsCodeWorkSpaceFolderFsPath, HaiBuildDefaults.defaultInstructionsDirectory);
808807
if(message.text) {
809808
try {
810809
const filePath = path.join(dir, message.text);
810+
let doesFileExist = await fileExistsAtPath(filePath);
811+
if (!doesFileExist) {
812+
vscode.window.showErrorMessage(`${message.text} does not exist.`);
813+
break;
814+
}
811815
await fs.unlink(filePath);
812816
vscode.window.showInformationMessage(message.text + " has been deleted.");
813817
} catch (error) {
@@ -1038,43 +1042,16 @@ export class ClineProvider implements vscode.WebviewViewProvider {
10381042
await this.postStateToWebview()
10391043
}
10401044

1041-
async removeFromFileInstructions(deletedFiles: string[]) {
1042-
const fileInstructions = await this.customGetState("fileInstructions") as HaiInstructionFile[];
1043-
const deletedFileNames = deletedFiles.map(path => path.split('/').pop());
1044-
const updatedFileInstructions = fileInstructions?.filter(
1045-
(instruction) => !deletedFileNames.includes(instruction.name)
1046-
);
1047-
this.updateFileInstructions(updatedFileInstructions);
1048-
}
1049-
1050-
async addFileInstruction(filePaths: string[]) {
1051-
const fileInstructions = await this.customGetState("fileInstructions") as HaiInstructionFile[];
1052-
const newInstructions = await Promise.all(filePaths.map(async (filePath) => ({
1053-
name: filePath.split("/").pop(),
1054-
enabled: false,
1055-
})));
1056-
1057-
newInstructions.forEach((instruction) => {
1058-
if (instruction.name) {
1059-
fileInstructions?.push(instruction as HaiInstructionFile);
1060-
}
1061-
});
1062-
await this.updateFileInstructions(fileInstructions);
1063-
}
1064-
10651045
async checkInstructionFilesFromFileSystem() {
10661046
const workspaceFolder = this.getWorkspacePath();
10671047
if (!workspaceFolder) { return; }
10681048
const instructionsPath = path.join(workspaceFolder, HaiBuildDefaults.defaultInstructionsDirectory);
10691049
try {
10701050
const files = await fs.readdir(instructionsPath);
1071-
const filesInSystemSet = new Set(files.filter(file => file.endsWith('.md')));
1072-
1073-
const currentState = await this.getState();
1074-
const fileInstructions = currentState.fileInstructions || [];
1075-
1051+
const filesInSystemSet = new Set(files.filter(file => file.endsWith('.md')));
1052+
const fileInstructions = await this.customGetState("fileInstructions") as HaiInstructionFile[];
10761053
const existingInstructionsMap = new Map(
1077-
fileInstructions.map(instruction => [instruction.name, instruction.enabled])
1054+
fileInstructions?.map(instruction => [instruction.name, instruction.enabled])
10781055
);
10791056

10801057
const updatedInstructions = Array.from(filesInSystemSet, name => ({
@@ -1798,12 +1775,6 @@ export class ClineProvider implements vscode.WebviewViewProvider {
17981775
await this.context.globalState.update(key, undefined)
17991776
}
18001777

1801-
let workspaceName = this.getWorkspacePath()
1802-
if (workspaceName) {
1803-
let instructionsDir = path.join(workspaceName, HaiBuildDefaults.defaultInstructionsDirectory)
1804-
fs.rmdir(instructionsDir, { recursive: true })
1805-
}
1806-
18071778
const secretKeys: SecretKey[] = [
18081779
"apiKey",
18091780
"openRouterApiKey",
@@ -1829,6 +1800,7 @@ export class ClineProvider implements vscode.WebviewViewProvider {
18291800
this.cline = undefined
18301801
}
18311802
vscode.window.showInformationMessage("State reset")
1803+
await this.checkInstructionFilesFromFileSystem()
18321804
await this.postStateToWebview()
18331805
await this.postMessageToWebview({ type: "action", action: "chatButtonClicked" })
18341806
}

src/integrations/workspace/HaiFileSystemWatcher.ts

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@ class HaiFileSystemWatcher {
1111
private ig: ReturnType<typeof ignore>
1212
private providerRef: WeakRef<ClineProvider>
1313
private watcher: Watcher
14+
private instructionsDir: string
1415

1516
constructor(provider: ClineProvider, sourceFolder: string) {
1617
this.sourceFolder = sourceFolder
1718
this.providerRef = new WeakRef(provider)
1819
this.ig = ignore()
20+
this.instructionsDir = path.join(this.sourceFolder, HaiBuildDefaults.defaultInstructionsDirectory)
21+
1922
this.watcher = new Watcher(this.sourceFolder, {
2023
recursive: true,
21-
debounce: 3000,
24+
debounce: 1000,
2225
ignoreInitial: true,
2326
ignore: (targetPath: string) => {
2427
if (!targetPath || targetPath.trim() === "") {
@@ -48,7 +51,7 @@ class HaiFileSystemWatcher {
4851
const gitignorePath = path.join(this.sourceFolder, ".gitignore")
4952
const content = await fs.readFile(gitignorePath, "utf-8")
5053

51-
this.ig.add(content.split("\n").filter((line) => line.trim() && !line.startsWith("#")))
54+
this.ig.add(content.split("\n").filter((line) => line.trim() && !line.startsWith("#") && !line.includes(".vscode")))
5255
} catch (error) {
5356
console.log("HaiFileSystemWatcher No .gitignore found, using default exclusions.")
5457
}
@@ -61,41 +64,28 @@ class HaiFileSystemWatcher {
6164

6265
this.watcher.on("unlink", (filePath) => {
6366
console.log("HaiFileSystemWatcher File deleted", filePath)
64-
if (filePath.includes("hai-instructions")) {
65-
this.providerRef.deref()?.removeFromFileInstructions([filePath])
67+
if (filePath.includes(this.instructionsDir)) {
68+
this.providerRef.deref()?.checkInstructionFilesFromFileSystem()
6669
} else {
67-
this.providerRef.deref()?.invokeReindex([filePath], FileOperations.Create)
70+
this.providerRef.deref()?.invokeReindex([filePath], FileOperations.Delete)
6871
}
69-
this.providerRef.deref()?.invokeReindex([filePath], FileOperations.Delete)
7072
})
7173

7274
this.watcher.on("add", (filePath) => {
7375
console.log("HaiFileSystemWatcher File added", filePath)
74-
if (filePath.includes("hai-instructions")) {
75-
this.providerRef.deref()?.addFileInstruction([filePath])
76+
if (filePath.includes(this.instructionsDir)) {
77+
this.providerRef.deref()?.checkInstructionFilesFromFileSystem()
7678
} else {
7779
this.providerRef.deref()?.invokeReindex([filePath], FileOperations.Create)
7880
}
7981
})
8082

8183
this.watcher.on("change", (filePath) => {
8284
console.log("HaiFileSystemWatcher File changes", filePath)
83-
this.providerRef.deref()?.invokeReindex([filePath], FileOperations.Change)
84-
})
85-
86-
this.watcher.on("addDir", (filePath) => {
87-
let value = filePath.split("/").pop() === "hai-instructions"
88-
console.log("HaiFileSystemWatcher Folder added", value)
89-
if (value) {
85+
if (filePath.includes(this.instructionsDir)) {
9086
this.providerRef.deref()?.checkInstructionFilesFromFileSystem()
91-
}
92-
})
93-
94-
this.watcher.on("unlinkDir", (filePath) => {
95-
let value = filePath.split("/").pop() === ".vscode" || filePath.split("/").pop() === "hai-instructions"
96-
console.log("HaiFileSystemWatcher Folder deleted", value)
97-
if (value) {
98-
this.providerRef.deref()?.updateFileInstructions([])
87+
} else {
88+
this.providerRef.deref()?.invokeReindex([filePath], FileOperations.Change)
9989
}
10090
})
10191
}

webview-ui/src/components/chat/CustomInstructionsMenu.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState } from "react";
1+
import React, { useEffect, useState } from "react";
22
import { VSCodeCheckbox } from "@vscode/webview-ui-toolkit/react";
33
import styled from "styled-components";
44
import { vscode } from "../../utils/vscode";
@@ -33,6 +33,14 @@ const CustomInstructionsMenu = ({
3333
? true
3434
: false
3535
);
36+
37+
useEffect(() => {
38+
setAllInstructionsEnabled(
39+
(fileInstructions?.every((i) => i.enabled === true) ?? false) &&
40+
customInstructions !== undefined &&
41+
isCustomInstructionsEnabled
42+
);
43+
}, [fileInstructions, customInstructions, isCustomInstructionsEnabled]);
3644

3745
const toggleInstruction = (index: number) => {
3846
const updatedInstructions = fileInstructions ? [...fileInstructions] : [];

0 commit comments

Comments
 (0)