Fix state management when starting new task with menu button

This commit is contained in:
Saoud Rizwan
2024-07-08 13:20:34 -04:00
parent 4da785b822
commit 6cace99030
3 changed files with 32 additions and 21 deletions

View File

@@ -168,10 +168,6 @@ export class ClaudeDev {
this.client = new Anthropic({ apiKey }) this.client = new Anthropic({ apiKey })
this.maxRequestsPerTask = maxRequestsPerTask ?? DEFAULT_MAX_REQUESTS_PER_TASK this.maxRequestsPerTask = maxRequestsPerTask ?? DEFAULT_MAX_REQUESTS_PER_TASK
// conversationHistory (for API) and claudeMessages (for webview) need to be in sync
// if the extension process were killed, then on restart the claudeMessages might not be empty, so we need to set it to [] when we create a new ClaudeDev client (otherwise webview would show stale messages from previous session)
this.providerRef.deref()?.setClaudeMessages([])
this.startTask(task) this.startTask(task)
} }
@@ -206,6 +202,11 @@ export class ClaudeDev {
} }
private async startTask(task: string): Promise<void> { private async startTask(task: string): Promise<void> {
// conversationHistory (for API) and claudeMessages (for webview) need to be in sync
// if the extension process were killed, then on restart the claudeMessages might not be empty, so we need to set it to [] when we create a new ClaudeDev client (otherwise webview would show stale messages from previous session)
await this.providerRef.deref()?.setClaudeMessages([])
await this.providerRef.deref()?.postStateToWebview()
// Get all relevant context for the task // Get all relevant context for the task
const filesInCurrentDir = await this.listFiles() const filesInCurrentDir = await this.listFiles()

View File

@@ -34,10 +34,12 @@ export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(vscode.window.registerWebviewViewProvider(SidebarProvider.viewType, provider)) context.subscriptions.push(vscode.window.registerWebviewViewProvider(SidebarProvider.viewType, provider))
context.subscriptions.push( context.subscriptions.push(
vscode.commands.registerCommand("claude-dev.plusButtonTapped", () => { vscode.commands.registerCommand("claude-dev.plusButtonTapped", async () => {
const message = "claude-dev.plusButtonTapped!" const message = "claude-dev.plusButtonTapped!"
vscode.window.showInformationMessage(message) //vscode.window.showInformationMessage(message)
provider.postMessageToWebview({ type: "action", action: "plusButtonTapped"}) await provider.resetTask()
await provider.postStateToWebview()
await provider.postMessageToWebview({ type: "action", action: "plusButtonTapped"})
}) })
) )

View File

@@ -13,7 +13,7 @@ https://github.com/KumarVariable/vscode-extension-sidebar-html/blob/master/src/c
type ExtensionSecretKey = "apiKey" type ExtensionSecretKey = "apiKey"
type ExtensionGlobalStateKey = "didOpenOnce" | "maxRequestsPerTask" type ExtensionGlobalStateKey = "didOpenOnce" | "maxRequestsPerTask"
type ExtensionWorkspaceStateKey = "claudeMessages" | "apiConversationHistory" type ExtensionWorkspaceStateKey = "claudeMessages"
export class SidebarProvider implements vscode.WebviewViewProvider { export class SidebarProvider implements vscode.WebviewViewProvider {
public static readonly viewType = "claude-dev.SidebarProvider" public static readonly viewType = "claude-dev.SidebarProvider"
@@ -40,6 +40,9 @@ export class SidebarProvider implements vscode.WebviewViewProvider {
// Sets up an event listener to listen for messages passed from the webview view context // Sets up an event listener to listen for messages passed from the webview view context
// and executes code based on the message that is recieved // and executes code based on the message that is recieved
this.setWebviewMessageListener(webviewView.webview) this.setWebviewMessageListener(webviewView.webview)
// if the extension is starting a new session, clear previous task state
this.resetTask()
} }
async tryToInitClaudeDevWithTask(task: string) { async tryToInitClaudeDevWithTask(task: string) {
@@ -200,6 +203,11 @@ export class SidebarProvider implements vscode.WebviewViewProvider {
}) })
} }
async resetTask() {
this.claudeDev = undefined
await this.setClaudeMessages([])
}
// client messages // client messages
async getClaudeMessages(): Promise<ClaudeMessage[]> { async getClaudeMessages(): Promise<ClaudeMessage[]> {
@@ -220,21 +228,21 @@ export class SidebarProvider implements vscode.WebviewViewProvider {
// api conversation history // api conversation history
async getApiConversationHistory(): Promise<ClaudeMessage[]> { // async getApiConversationHistory(): Promise<ClaudeMessage[]> {
const messages = (await this.getWorkspaceState("apiConversationHistory")) as ClaudeMessage[] // const messages = (await this.getWorkspaceState("apiConversationHistory")) as ClaudeMessage[]
return messages || [] // return messages || []
} // }
async setApiConversationHistory(messages: ClaudeMessage[] | undefined) { // async setApiConversationHistory(messages: ClaudeMessage[] | undefined) {
await this.updateWorkspaceState("apiConversationHistory", messages) // await this.updateWorkspaceState("apiConversationHistory", messages)
} // }
async addMessageToApiConversationHistory(message: ClaudeMessage): Promise<ClaudeMessage[]> { // async addMessageToApiConversationHistory(message: ClaudeMessage): Promise<ClaudeMessage[]> {
const messages = await this.getClaudeMessages() // const messages = await this.getClaudeMessages()
messages.push(message) // messages.push(message)
await this.setClaudeMessages(messages) // await this.setClaudeMessages(messages)
return messages // return messages
} // }
/* /*
Storage Storage