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.
This commit is contained in:
bobo.yang 2023-07-24 08:20:26 +08:00
parent 0d345bbf00
commit 45e4af7efd
4 changed files with 44 additions and 15 deletions

View File

@ -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}");

View File

@ -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<LogEntry>): void {

View File

@ -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<LogEntry[]> {
async topics(): Promise<TopicEntry[]> {
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();

View File

@ -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;