Fixed garbage collection of aborted tasks; handle if run from root directory then don’t read/write; fixed scroll to bottom; fix other small bugs

This commit is contained in:
Saoud Rizwan
2024-07-09 23:44:20 -04:00
parent 9867a6a597
commit 7170d2a2e8
8 changed files with 101 additions and 26 deletions

View File

@@ -175,6 +175,7 @@ export class ClaudeDev {
private askResponse?: ClaudeAskResponse
private askResponseText?: string
private providerRef: WeakRef<SidebarProvider>
abort: boolean = false
constructor(provider: SidebarProvider, task: string, apiKey: string, maxRequestsPerTask?: number) {
this.providerRef = new WeakRef(provider)
@@ -198,6 +199,8 @@ export class ClaudeDev {
}
async ask(type: ClaudeAsk, question: string): Promise<{ response: ClaudeAskResponse; text?: string }> {
// If this ClaudeDev instance was aborted by the provider, then the only thing keeping us alive is a promise still running in the background, in which case we don't want to send its result to the webview as it is attached to a new instance of ClaudeDev now. So we can safely ignore the result of any active promises, and this class will be deallocated. (Although we set claudeDev = undefined in provider, that simply removes the reference to this instance, but the instance is still alive until this promise resolves or rejects.)
if (this.abort) { throw new Error("ClaudeDev instance aborted") }
this.askResponse = undefined
this.askResponseText = undefined
await this.providerRef.deref()?.addClaudeMessage({ ts: Date.now(), type: "ask", ask: type, text: question })
@@ -210,6 +213,7 @@ export class ClaudeDev {
}
async say(type: ClaudeSay, text: string): Promise<undefined> {
if (this.abort) { throw new Error("ClaudeDev instance aborted") }
await this.providerRef.deref()?.addClaudeMessage({ ts: Date.now(), type: "say", say: type, text: text })
await this.providerRef.deref()?.postStateToWebview()
}
@@ -385,7 +389,7 @@ ${activeEditorContents}`
if (shouldLog) {
this.say("tool", JSON.stringify({ tool: "listFiles", path: dirPath, content: "/" } as ClaudeSayTool))
}
return "Currently in the root directory. Cannot list all files."
return "WARNING: You are currently in the root directory! You DO NOT have read or write permissions in this directory, so you would need to use a command like \`echo $HOME\` to find a path you can work with (e.g. the user's Desktop directory). If you cannot accomplish your task in the root directory, you need to tell the user to \"open this extension in a workspace or another directory\" (since you are a script being run in a VS Code extension)."
}
try {
@@ -459,6 +463,8 @@ ${activeEditorContents}`
| Anthropic.ToolResultBlockParam
>
): Promise<ClaudeRequestResult> {
if (this.abort) { throw new Error("ClaudeDev instance aborted") }
this.conversationHistory.push({ role: "user", content: userContent })
if (this.requestCount >= this.maxRequestsPerTask) {
const { response } = await this.ask(
@@ -483,7 +489,16 @@ ${activeEditorContents}`
}
try {
await this.say("api_req_started", JSON.stringify({ request: userContent }))
// what the user sees in the webview
await this.say("api_req_started", JSON.stringify({ request: {
model: "claude-3-5-sonnet-20240620",
max_tokens: 4096,
system: "(see SYSTEM_PROMPT in src/ClaudeDev.ts)",
messages: [{ "conversation_history": "..." }, { role: "user", content: userContent }],
tools: "(see tools in src/ClaudeDev.ts)",
tool_choice: { type: "auto" },
}}))
const response = await this.client.messages.create({
model: "claude-3-5-sonnet-20240620", // https://docs.anthropic.com/en/docs/about-claude/models
max_tokens: 4096,