Get and delete topics via client

This commit is contained in:
kagami 2024-07-08 19:12:06 +08:00
parent bebe32fa18
commit b9df28d20d
2 changed files with 84 additions and 53 deletions

View File

@ -1,67 +1,62 @@
import * as vscode from 'vscode'; import * as vscode from "vscode";
import * as os from 'os';
import * as path from 'path';
import * as fs from 'fs';
import { regInMessage, regOutMessage } from '../util/reg_messages';
import { MessageHandler } from './messageHandler';
import DevChat, { TopicEntry } from '../toolwrapper/devchat';
import { UiUtilWrapper } from '../util/uiUtil';
import { regInMessage, regOutMessage } from "../util/reg_messages";
import { MessageHandler } from "./messageHandler";
import { DevChatClient } from "../toolwrapper/devchatClient";
import { LogEntry } from "./historyMessagesBase";
const dcClient = new DevChatClient();
function isDeleteTopic(topicId: string) { export interface TopicEntry {
let workspaceDir = UiUtilWrapper.workspaceFoldersFirstPath(); // eslint-disable-next-line @typescript-eslint/naming-convention
if (!workspaceDir) { root_prompt: LogEntry;
workspaceDir = os.homedir(); // eslint-disable-next-line @typescript-eslint/naming-convention
} latest_time: number;
const deletedTopicsPath = path.join(workspaceDir!, '.chat', '.deletedTopics'); hidden: boolean;
title: string | null;
if (!fs.existsSync(deletedTopicsPath)) {
return false;
}
const deletedTopics = fs.readFileSync(deletedTopicsPath, 'utf-8').split('\n');
// check whether topicId in deletedTopics
return deletedTopics.includes(topicId);
} }
// 注册获取当前topic列表的命令 // 注册获取当前topic列表的命令
regInMessage({ command: 'getTopics' }); regInMessage({ command: "getTopics" });
regOutMessage({ command: 'getTopics', topics: [] }); regOutMessage({ command: "getTopics", topics: [] });
export async function getTopics(message: any, panel: vscode.WebviewPanel | vscode.WebviewView): Promise<void> { export async function getTopics(
const devChat = new DevChat(); message: any,
const topicEntriesAll: TopicEntry[] = await devChat.topics(); panel: vscode.WebviewPanel | vscode.WebviewView
): Promise<void> {
const topics = await dcClient.getTopics(100, 0);
const entries: TopicEntry[] = [];
let topicEntries: TopicEntry[] = []; for (const topic of topics) {
for (const topicEntry of topicEntriesAll) { const rootLog: LogEntry = {
if (isDeleteTopic(topicEntry.root_prompt.hash)) { hash: topic.root_prompt_hash,
continue; parent: topic.root_prompt_parent,
} user: topic.root_prompt_user,
// append topicEntry to topicEntries date: topic.root_prompt_date,
topicEntries.push(topicEntry); request: topic.root_prompt_request,
response: topic.root_prompt_response,
context: [],
};
const e: TopicEntry = {
root_prompt: rootLog,
latest_time: topic.latest_time,
hidden: topic.hidden,
title: topic.title,
};
entries.push(e);
} }
MessageHandler.sendMessage(panel, { command: 'getTopics', topicEntries }); MessageHandler.sendMessage(panel, {
command: "getTopics",
topicEntries: entries,
});
} }
// 注册删除topic的命令 // 注册删除topic的命令
regInMessage({ command: 'deleteTopic', topicId: '' }); regInMessage({ command: "deleteTopic", topicId: "" });
export async function deleteTopic(message: any, panel: vscode.WebviewPanel | vscode.WebviewView): Promise<void> { export async function deleteTopic(
message: any,
panel: vscode.WebviewPanel | vscode.WebviewView
): Promise<void> {
const topicId = message.topicId; const topicId = message.topicId;
let workspaceDir = UiUtilWrapper.workspaceFoldersFirstPath(); await dcClient.deleteTopic(topicId);
if (!workspaceDir) {
workspaceDir = os.homedir();
}
const deletedTopicsPath = path.join(workspaceDir!, '.chat', '.deletedTopics');
// read ${WORKSPACE_ROOT}/.chat/.deletedTopics as String[]
// add topicId to String[]
// write String[] to ${WORKSPACE_ROOT}/.chat/.deletedTopics
let deletedTopics: string[] = [];
if (fs.existsSync(deletedTopicsPath)) {
deletedTopics = fs.readFileSync(deletedTopicsPath, 'utf-8').split('\n');
}
deletedTopics.push(topicId);
fs.writeFileSync(deletedTopicsPath, deletedTopics.join('\n'));
} }

View File

@ -317,6 +317,42 @@ export class DevChatClient {
return logs; return logs;
} }
@timeThis
async getTopics(limit:number, offset:number): Promise<any[]> {
const data = {
limit: limit,
offset: offset,
workspace: UiUtilWrapper.workspaceFoldersFirstPath(),
};
const response = await axios.get(`${this.baseURL}/topics`, {
params: data,
});
const topics: any[] = response.data;
topics.reverse();
logger
.channel()
?.debug(`getTopics response data: ${JSON.stringify(topics)}`);
return topics;
}
@timeThis
async deleteTopic(topicRootHash:string): Promise<void> {
const data = {
topic_hash: topicRootHash,
workspace: UiUtilWrapper.workspaceFoldersFirstPath(),
};
const response = await this._post("/topics/delete", data);
logger
.channel()
?.debug(`deleteTopic response data: ${JSON.stringify(response.data)}`);
return;
}
stopAllRequest(): void { stopAllRequest(): void {
this.cancelMessage(); this.cancelMessage();