From 08be9a52dcc3dcc044c2e9d2c065ac754e7e87f2 Mon Sep 17 00:00:00 2001 From: "bobo.yang" Date: Tue, 9 May 2023 20:46:57 +0800 Subject: [PATCH] commitmsg Add configuration options for log count and skip - Added new configuration options in package.json for maxLogCount and logSkip - Updated historyMessages.ts to handle the new configuration options - Modified devchat.ts to use the new configuration options when fetching logs --- package.json | 10 ++++++++ src/handler/historyMessages.ts | 46 ++++++++++++++++++++++++++++++++-- src/toolwrapper/devchat.ts | 14 +++++++++-- 3 files changed, 66 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index a8e9cf0..4278b52 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,16 @@ ], "description": "Select whose llm to use." }, + "DevChat.maxLogCount": { + "type": "number", + "default": 20, + "description": "Limit the number of prompts to output" + }, + "DevChat.logSkip": { + "type": "number", + "default": 0, + "description": "Skip number prompts before showing the prompt history" + }, "DevChat.OpenAI.model": { "type": "string", "default": "gpt-4", diff --git a/src/handler/historyMessages.ts b/src/handler/historyMessages.ts index b191ab8..8562ba2 100644 --- a/src/handler/historyMessages.ts +++ b/src/handler/historyMessages.ts @@ -1,13 +1,55 @@ import * as vscode from 'vscode'; -import DevChat, { LogOptions } from '../toolwrapper/devchat'; +import DevChat, { LogOptions, LogEntry } from '../toolwrapper/devchat'; import { MessageHandler } from './messageHandler'; +import messageHistory from '../util/messageHistory'; + +interface LoadHistoryMessages { + command: string; + entries: Array; +} export async function historyMessages(message: any, panel: vscode.WebviewPanel): Promise { const devChat = new DevChat(); const logOptions: LogOptions = message.options || {}; const logEntries = await devChat.log(logOptions); - MessageHandler.sendMessage(panel, { command: 'loadHistoryMessages', entries: logEntries }); + + const logEntriesFlat = logEntries.flat(); + // TODO handle context + + const logEntriesFlatFiltered = logEntriesFlat.map((entry) => { + return { + date: entry.date, + hash: entry.hash, + request: entry.request, + text: entry.response, + user: entry.user, + parent_hash: '', + }; + }); + + for (let i = 0; i < logEntriesFlat.length; i++) { + let entryOld = logEntriesFlat[i]; + let entryNew = { + date: entryOld.date, + hash: entryOld.hash, + request: entryOld.request, + text: entryOld.response, + user: entryOld.user, + parent_hash: '', + }; + if (i > 0) { + entryNew.parent_hash = logEntriesFlat[i - 1].hash; + } + messageHistory.add(panel, entryNew); + } + + const loadHistoryMessages: LoadHistoryMessages = { + command: 'loadHistoryMessages', + entries: logEntriesFlat, + }; + + MessageHandler.sendMessage(panel, loadHistoryMessages); return; } diff --git a/src/toolwrapper/devchat.ts b/src/toolwrapper/devchat.ts index a371693..d9c373e 100644 --- a/src/toolwrapper/devchat.ts +++ b/src/toolwrapper/devchat.ts @@ -31,7 +31,10 @@ export interface LogEntry { date: string; request: string; response: string; - context: string[]; + context: Array<{ + content: string; + role: string; + }>; } export interface ChatResponse { @@ -231,9 +234,15 @@ class DevChat { if (options.skip) { args.push('--skip', `${options.skip}`); + } else { + const skipLogCount = vscode.workspace.getConfiguration('DevChat').get('logSkip'); + args.push('--skip', `${skipLogCount}`); } if (options.maxCount) { args.push('--max-count', `${options.maxCount}`); + } else { + const maxLogCount = vscode.workspace.getConfiguration('DevChat').get('maxLogCount'); + args.push('--max-count', `${maxLogCount}`); } const workspaceDir = vscode.workspace.workspaceFolders?.[0].uri.fsPath; @@ -256,7 +265,8 @@ class DevChat { return []; } - return JSON.parse(stdout.trim()); + const stdoutNew = "[" + stdout.replace(/\}\n\]\n\[\n \{\n/g, "}\n],\n[\n {\n") + "]"; + return JSON.parse(stdoutNew.trim()); } catch (error) { logger.channel()?.error(`Error getting log: ${error}`); logger.channel()?.show();