save chat threads to workspace history

This commit is contained in:
Aneta Jastrzębska 2024-09-21 18:05:25 +02:00
parent fe5e02c3a9
commit 22d32aca3a
3 changed files with 50 additions and 2 deletions

View file

@ -1,5 +1,5 @@
import * as vscode from 'vscode';
import { WebviewMessage } from './shared_types';
import { ChatThread, WebviewMessage } from './shared_types';
import { CtrlKCodeLensProvider } from './CtrlKCodeLensProvider';
import { getDiffedLines } from './getDiffedLines';
import { ApprovalCodeLensProvider, SuggestedEdit } from './ApprovalCodeLensProvider';
@ -124,6 +124,20 @@ export function activate(context: vscode.ExtensionContext) {
webview.postMessage({ type: 'apiConfig', apiConfig } satisfies WebviewMessage)
}
else if (m.type === 'getThreadHistory') {
const threads: ChatThread[] = context.workspaceState.get('threadHistory') ?? []
webview.postMessage({ type: 'threadHistory', threads } satisfies WebviewMessage)
}
else if (m.type === 'updateThread') {
const threads: ChatThread[] = context.workspaceState.get('threadHistory') as [] ?? []
const updatedThreads = threads.find((t: ChatThread) => t.id === m.thread.id)
? threads.map((t: ChatThread) => t.id === m.thread.id ? m.thread : t)
: [...threads, m.thread]
context.workspaceState.update('threadHistory', updatedThreads)
webview.postMessage({ type: 'threadHistory', threads: updatedThreads } satisfies WebviewMessage)
}
else {
console.error('unrecognized command', m.type, m)

View file

@ -27,13 +27,44 @@ type WebviewMessage = (
// editor -> sidebar
| { type: 'apiConfig', apiConfig: ApiConfig }
// sidebar -> editor
| { type: 'getThreadHistory' }
// editor -> sidebar
| { type: 'threadHistory', threads: ChatThread[] }
// sidebar -> editor
| { type: 'updateThread', thread: ChatThread }
)
type Command = WebviewMessage['type']
type ChatThread = {
id: string;
createdAt: string;
messages: ChatMessage[];
}
type ChatMessage =
| {
role: "user";
content: string; // content sent to the llm
displayContent: string; // content displayed to user
selection: Selection | null; // the user's selection
files: vscode.Uri[]; // the files sent in the message
}
| {
role: "assistant";
content: string; // content received from LLM
displayContent: string; // content displayed to user (this is the same as content for now)
}
export {
Selection,
File,
WebviewMessage,
Command,
ChatThread,
ChatMessage,
}

View file

@ -9,7 +9,10 @@ const awaiting: { [c in Command]: ((res: any) => void)[] } = {
"requestFiles": [],
"files": [],
"apiConfig": [],
"getApiConfig": []
"getApiConfig": [],
"getThreadHistory": [],
"threadHistory": [],
"updateThread": [],
}
// use this function to await responses