Skip to content

Commit 69837e8

Browse files
authored
Merge pull request #167 from covespace/refactor_topic_method
Refactor topic method
2 parents dd2b79d + 80d72ad commit 69837e8

File tree

6 files changed

+59
-161
lines changed

6 files changed

+59
-161
lines changed

src/context/contextManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export interface ChatContext {
6464
async processText(command: string): Promise<string> {
6565
// 处理所有命令
6666
for (const contextObj of this.contexts) {
67-
if (contextObj.name == command) {
67+
if (contextObj.name === command) {
6868
return await contextObj.handler();
6969
}
7070
}

src/handler/historyMessagesBase.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22

33
import { TopicManager } from '../topic/topicManager';
4-
import { LogEntry } from '../toolwrapper/devchat';
4+
import DevChat, { LogEntry, LogOptions } from '../toolwrapper/devchat';
55
import messageHistory from '../util/messageHistory';
66
import { ApiKeyManager } from '../util/apiKey';
77
import { logger } from '../util/logger';
@@ -63,15 +63,19 @@ export async function isWaitForApiKey() {
6363

6464
export async function loadTopicHistoryLogs() : Promise<Array<LogEntry> | undefined> {
6565
const topicId = TopicManager.getInstance().currentTopicId;
66-
let logEntriesFlat: Array<LogEntry> = [];
67-
if (topicId) {
68-
logEntriesFlat = await TopicManager.getInstance().getTopicHistory(topicId);
66+
if (!topicId) {
67+
return [];
6968
}
7069

71-
if (topicId !== TopicManager.getInstance().currentTopicId) {
72-
logger.channel()?.info(`Current topic changed dure load topic hsitory!`)
73-
return undefined;
74-
}
70+
const devChat = new DevChat();
71+
const logOptions: LogOptions = {
72+
skip: 0,
73+
maxCount: 10000,
74+
topic: topicId
75+
};
76+
const logEntries = await devChat.log(logOptions);
77+
const logEntriesFlat = logEntries.flat();
78+
7579
return logEntriesFlat;
7680
}
7781

src/toolwrapper/devchat.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export interface ChatOptions {
2424
export interface LogOptions {
2525
skip?: number;
2626
maxCount?: number;
27+
topic?: string;
2728
}
2829

2930
export interface LogEntry {
@@ -258,6 +259,37 @@ class DevChat {
258259
return JSON.parse(stdout.trim()).reverse();
259260
}
260261

262+
async topics(): Promise<LogEntry[]> {
263+
const args = ["topic", "-l"];
264+
const devChat = this.getDevChatPath();
265+
const workspaceDir = UiUtilWrapper.workspaceFoldersFirstPath();
266+
267+
logger.channel()?.info(`Running devchat with args: ${args.join(" ")}`);
268+
const spawnOptions = {
269+
maxBuffer: 10 * 1024 * 1024, // Set maxBuffer to 10 MB
270+
cwd: workspaceDir,
271+
env: {
272+
...process.env
273+
},
274+
};
275+
const { exitCode: code, stdout, stderr } = await this.commandRun.spawnAsync(devChat, args, spawnOptions, undefined, undefined, undefined, undefined);
276+
277+
logger.channel()?.info(`Finish devchat with args: ${args.join(" ")}`);
278+
if (stderr) {
279+
logger.channel()?.error(`Error getting log: ${stderr}`);
280+
logger.channel()?.show();
281+
return [];
282+
}
283+
284+
try {
285+
return JSON.parse(stdout.trim()).reverse();
286+
} catch (error) {
287+
logger.channel()?.error(`Error parsing log: ${error}`);
288+
logger.channel()?.show();
289+
return [];
290+
}
291+
}
292+
261293
private buildLogArgs(options: LogOptions): string[] {
262294
let args = ["log"];
263295

@@ -271,6 +303,10 @@ class DevChat {
271303
args.push('--max-count', `${maxLogCount}`);
272304
}
273305

306+
if (options.topic) {
307+
args.push('--topic', `${options.topic}`);
308+
}
309+
274310
return args;
275311
}
276312

src/topic/loadTopics.ts

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/topic/topicManager.ts

Lines changed: 10 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import * as fs from 'fs';
44

55
import DevChat, { LogEntry, LogOptions } from '../toolwrapper/devchat';
66

7-
import { loadTopicList } from './loadTopics';
87
import { UiUtilWrapper } from '../util/uiUtil';
98
import { logger } from '../util/logger';
109

@@ -231,39 +230,20 @@ export class TopicManager {
231230
return deletedTopics.includes(topicId);
232231
}
233232

234-
// loadTopics
235-
// 功能:将DevChat日志根据parentHash进行分组,当前条目与该条目parentHash对应条目归属一组,以此类推,直到parentHash为空
236-
// 返回值:多个链表,每个链表中当前元素的hash是下一个元素的parentHash
237-
async loadLogEntries(): Promise<{ [key: string]: LogEntry[] }> {
238-
// 通过DevChat获取日志
239-
const devChat = new DevChat();
240-
const logOptions: LogOptions = {
241-
skip: 0,
242-
maxCount: 10000
243-
};
244-
const logEntries = await devChat.log(logOptions);
245-
const logEntriesFlat = logEntries.flat();
246-
247-
const logTopicLinkedList = loadTopicList(logEntriesFlat);
248-
return logTopicLinkedList;
249-
}
250-
251233
async loadTopics(): Promise<void> {
252-
// 删除已经加载的topic
253-
// 重新构建topic信息
254234
this._topics = {};
255-
const logEntriesMap = await this.loadLogEntries();
256-
for (const logEntriesList of Object.values(logEntriesMap)) {
257-
// 使用logEntriesList第一个元素更新topic的firstMessageHash和name
258-
// 使用logEntriesList最后一个元素更新topic的lastMessageHash和lastUpdated
259-
if (logEntriesList.length === 0) {
260-
continue;
261-
}
262-
const logEntry = logEntriesList[0];
235+
236+
const devChat = new DevChat();
237+
const logEntries: LogEntry[] = await devChat.topics();
238+
239+
// visite logEntries
240+
// for each logEntry
241+
let lastData: number = 0;
242+
for (const logEntry of logEntries.flat()) {
243+
lastData += 1;
263244
const name = this.createTopicName(logEntry.request, logEntry.response);
264-
const lastLogEntry = logEntriesList[logEntriesList.length - 1];
265245
const topic = new Topic(name, logEntry.hash, logEntry.hash, Number(logEntry.date));
266-
topic.updateLastMessageHashAndLastUpdated(lastLogEntry.hash, Number(lastLogEntry.date));
246+
topic.updateLastMessageHashAndLastUpdated(logEntry.hash, lastData);
267247

268248
if (topic.firstMessageHash && this.isDeleteTopic(topic.firstMessageHash)) {
269249
continue;
@@ -272,27 +252,4 @@ export class TopicManager {
272252
}
273253
this._notifyReloadTopicsListeners(Object.values(this._topics));
274254
}
275-
276-
277-
async getTopicHistory(topicId: string): Promise<LogEntry[]> {
278-
/**
279-
* 获取topic历史聊天记录
280-
*/
281-
// TOPIC对象中firstMessageHash可以作为日志查询的起始点
282-
// 在DevChat日志中,找出第一个hash为firstMessageHash的日志,然后向下遍历,直到找不到parentHash为当前日志hash的日志
283-
const topic = this._topics[topicId];
284-
if (!topic || !topic.firstMessageHash) {
285-
logger.channel()?.info(`Topic ${topicId} not found`);
286-
return [];
287-
}
288-
289-
const logEntriesMap = await this.loadLogEntries();
290-
if (!logEntriesMap[topic.firstMessageHash!]) {
291-
logger.channel()?.info(`Topic ${topicId} not found in logEntriesMap`);
292-
return [];
293-
}
294-
295-
const logEntriesFlat = logEntriesMap[topic.firstMessageHash!];
296-
return logEntriesFlat;
297-
}
298255
}

test/topic/loadTopics.test.ts

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
 (0)