From 45e4af7efd86d6fc1f34cf43a7fd6d4269e6b93a Mon Sep 17 00:00:00 2001 From: "bobo.yang" Date: Mon, 24 Jul 2023 08:20:26 +0800 Subject: [PATCH] Update DevChat version and refactor log handling - Updated expected DevChat version to 0.2.0. - Refactored log handling to accommodate changes in DevChat's log structure. - Added TopicEntry interface to handle new topic entries from DevChat. - Updated topics function to return TopicEntry array. - Adjusted topic creation to use new TopicEntry structure. --- src/contributes/commandsBase.ts | 2 +- src/handler/historyMessagesBase.ts | 3 +-- src/toolwrapper/devchat.ts | 38 +++++++++++++++++++++++++++--- src/topic/topicManager.ts | 16 ++++++------- 4 files changed, 44 insertions(+), 15 deletions(-) diff --git a/src/contributes/commandsBase.ts b/src/contributes/commandsBase.ts index 6500daf..cea36c7 100644 --- a/src/contributes/commandsBase.ts +++ b/src/contributes/commandsBase.ts @@ -31,7 +31,7 @@ export function checkDevChatDependency(pythonCommand: string, showError: boolean try { // Check if DevChat is installed - const expectVersion = 'DevChat 0.1.15'; + const expectVersion = 'DevChat 0.2.0'; const devchatVersion = runCommand(`"${devChat}" --version`).toString().trim(); if (devchatVersion < expectVersion) { logger.channel()?.info("devchat version: ${devchatVersion}, but expect version: ${expectVersion}"); diff --git a/src/handler/historyMessagesBase.ts b/src/handler/historyMessagesBase.ts index 6306cc0..9972efc 100644 --- a/src/handler/historyMessagesBase.ts +++ b/src/handler/historyMessagesBase.ts @@ -77,9 +77,8 @@ export async function loadTopicHistoryLogs(topicId: string | undefined) : Promis topic: topic.firstMessageHash }; const logEntries = await devChat.log(logOptions); - const logEntriesFlat = logEntries.flat(); - return logEntriesFlat; + return logEntries; } export function updateCurrentMessageHistory(topicId: string, logEntries: Array): void { diff --git a/src/toolwrapper/devchat.ts b/src/toolwrapper/devchat.ts index a86a06a..43d4795 100644 --- a/src/toolwrapper/devchat.ts +++ b/src/toolwrapper/devchat.ts @@ -44,6 +44,24 @@ export interface LogEntry { }>; } +// define TopicEntry interface +/* +[ + { + root_prompt: LogEntry, + latest_time: 1689849274, + hidden: false, + title: null + } +] +*/ +export interface TopicEntry { + root_prompt: LogEntry; + latest_time: number; + hidden: boolean; + title: string | null; +} + export interface ChatResponse { "prompt-hash": string; user: string; @@ -323,10 +341,15 @@ class DevChat { return []; } - return JSON.parse(stdout.trim()).reverse(); + const logs = JSON.parse(stdout.trim()).reverse(); + for (const log of logs) { + log.response = log.responses[0]; + delete log.responses; + } + return logs; } - async topics(): Promise { + async topics(): Promise { const args = ["topic", "-l"]; const devChat = this.getDevChatPath(); const workspaceDir = UiUtilWrapper.workspaceFoldersFirstPath(); @@ -349,7 +372,16 @@ class DevChat { } try { - return JSON.parse(stdout.trim()).reverse(); + const topics = JSON.parse(stdout.trim()).reverse(); + // convert responses to respose, and remove responses field + // responses is in TopicEntry.root_prompt.responses + for (const topic of topics) { + if (topic.root_prompt.responses) { + topic.root_prompt.response = topic.root_prompt.responses[0]; + delete topic.root_prompt.responses; + } + } + return topics; } catch (error) { logger.channel()?.error(`Error parsing JSON: ${error}`); logger.channel()?.show(); diff --git a/src/topic/topicManager.ts b/src/topic/topicManager.ts index 4789316..307cc21 100644 --- a/src/topic/topicManager.ts +++ b/src/topic/topicManager.ts @@ -2,7 +2,7 @@ import { v4 as uuidv4 } from 'uuid'; import * as path from 'path'; import * as fs from 'fs'; -import DevChat, { LogEntry, LogOptions } from '../toolwrapper/devchat'; +import DevChat, { LogEntry, LogOptions, TopicEntry } from '../toolwrapper/devchat'; import { UiUtilWrapper } from '../util/uiUtil'; import { logger } from '../util/logger'; @@ -118,7 +118,7 @@ export class TopicManager { deleteMessage(topicId: string, messageHash: string): void { const topic = this._topics[topicId]; if (topic) { - topic.updateFirstMessageHashAndName(undefined, "Empty topic"); + topic.updateFirstMessageHashAndName(undefined, undefined); topic.lastMessageHash = undefined; this._notifyUpdateTopicListeners(topicId); } @@ -247,16 +247,14 @@ export class TopicManager { this._topics = {}; const devChat = new DevChat(); - const logEntries: LogEntry[] = await devChat.topics(); + const topicEntries: TopicEntry[] = await devChat.topics(); // visite logEntries // for each logEntry - let lastData: number = 0; - for (const logEntry of logEntries.flat()) { - lastData += 1; - const name = this.createTopicName(logEntry.request, logEntry.response); - const topic = new Topic(name, logEntry.hash, logEntry.hash, Number(logEntry.date)); - topic.updateLastMessageHashAndLastUpdated(logEntry.hash, lastData); + for (const topicEntry of topicEntries) { + const name = topicEntry.title ? topicEntry.title : this.createTopicName(topicEntry.root_prompt.request, topicEntry.root_prompt.response); + const topic = new Topic(name!, topicEntry.root_prompt.hash, topicEntry.root_prompt.hash, Number(topicEntry.latest_time)); + topic.updateLastMessageHashAndLastUpdated(topicEntry.root_prompt.hash, topicEntry.latest_time); if (topic.firstMessageHash && this.isDeleteTopic(topic.firstMessageHash)) { continue;