diff --git a/src/ClaudeDev.ts b/src/ClaudeDev.ts index afd77be..c425e72 100644 --- a/src/ClaudeDev.ts +++ b/src/ClaudeDev.ts @@ -168,10 +168,6 @@ export class ClaudeDev { this.client = new Anthropic({ apiKey }) 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) } @@ -206,6 +202,11 @@ export class ClaudeDev { } private async startTask(task: string): Promise { + // 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 const filesInCurrentDir = await this.listFiles() diff --git a/src/extension.ts b/src/extension.ts index 3f079ae..06ad0d0 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -34,10 +34,12 @@ export function activate(context: vscode.ExtensionContext) { context.subscriptions.push(vscode.window.registerWebviewViewProvider(SidebarProvider.viewType, provider)) context.subscriptions.push( - vscode.commands.registerCommand("claude-dev.plusButtonTapped", () => { + vscode.commands.registerCommand("claude-dev.plusButtonTapped", async () => { const message = "claude-dev.plusButtonTapped!" - vscode.window.showInformationMessage(message) - provider.postMessageToWebview({ type: "action", action: "plusButtonTapped"}) + //vscode.window.showInformationMessage(message) + await provider.resetTask() + await provider.postStateToWebview() + await provider.postMessageToWebview({ type: "action", action: "plusButtonTapped"}) }) ) diff --git a/src/providers/SidebarProvider.ts b/src/providers/SidebarProvider.ts index 5e49529..c7f8356 100644 --- a/src/providers/SidebarProvider.ts +++ b/src/providers/SidebarProvider.ts @@ -13,7 +13,7 @@ https://github.com/KumarVariable/vscode-extension-sidebar-html/blob/master/src/c type ExtensionSecretKey = "apiKey" type ExtensionGlobalStateKey = "didOpenOnce" | "maxRequestsPerTask" -type ExtensionWorkspaceStateKey = "claudeMessages" | "apiConversationHistory" +type ExtensionWorkspaceStateKey = "claudeMessages" export class SidebarProvider implements vscode.WebviewViewProvider { 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 // and executes code based on the message that is recieved this.setWebviewMessageListener(webviewView.webview) + + // if the extension is starting a new session, clear previous task state + this.resetTask() } async tryToInitClaudeDevWithTask(task: string) { @@ -199,6 +202,11 @@ export class SidebarProvider implements vscode.WebviewViewProvider { state: { didOpenOnce: !!didOpenOnce, apiKey, maxRequestsPerTask, claudeMessages }, }) } + + async resetTask() { + this.claudeDev = undefined + await this.setClaudeMessages([]) + } // client messages @@ -220,21 +228,21 @@ export class SidebarProvider implements vscode.WebviewViewProvider { // api conversation history - async getApiConversationHistory(): Promise { - const messages = (await this.getWorkspaceState("apiConversationHistory")) as ClaudeMessage[] - return messages || [] - } + // async getApiConversationHistory(): Promise { + // const messages = (await this.getWorkspaceState("apiConversationHistory")) as ClaudeMessage[] + // return messages || [] + // } - async setApiConversationHistory(messages: ClaudeMessage[] | undefined) { - await this.updateWorkspaceState("apiConversationHistory", messages) - } + // async setApiConversationHistory(messages: ClaudeMessage[] | undefined) { + // await this.updateWorkspaceState("apiConversationHistory", messages) + // } - async addMessageToApiConversationHistory(message: ClaudeMessage): Promise { - const messages = await this.getClaudeMessages() - messages.push(message) - await this.setClaudeMessages(messages) - return messages - } + // async addMessageToApiConversationHistory(message: ClaudeMessage): Promise { + // const messages = await this.getClaudeMessages() + // messages.push(message) + // await this.setClaudeMessages(messages) + // return messages + // } /* Storage