Get and delete topics via client
This commit is contained in:
parent
bebe32fa18
commit
b9df28d20d
@ -1,67 +1,62 @@
|
||||
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 * as vscode from "vscode";
|
||||
|
||||
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) {
|
||||
let workspaceDir = UiUtilWrapper.workspaceFoldersFirstPath();
|
||||
if (!workspaceDir) {
|
||||
workspaceDir = os.homedir();
|
||||
}
|
||||
const deletedTopicsPath = path.join(workspaceDir!, '.chat', '.deletedTopics');
|
||||
|
||||
if (!fs.existsSync(deletedTopicsPath)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const deletedTopics = fs.readFileSync(deletedTopicsPath, 'utf-8').split('\n');
|
||||
// check whether topicId in deletedTopics
|
||||
return deletedTopics.includes(topicId);
|
||||
export interface TopicEntry {
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||
root_prompt: LogEntry;
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||
latest_time: number;
|
||||
hidden: boolean;
|
||||
title: string | null;
|
||||
}
|
||||
|
||||
// 注册获取当前topic列表的命令
|
||||
regInMessage({ command: 'getTopics' });
|
||||
regOutMessage({ command: 'getTopics', topics: [] });
|
||||
export async function getTopics(message: any, panel: vscode.WebviewPanel | vscode.WebviewView): Promise<void> {
|
||||
const devChat = new DevChat();
|
||||
const topicEntriesAll: TopicEntry[] = await devChat.topics();
|
||||
regInMessage({ command: "getTopics" });
|
||||
regOutMessage({ command: "getTopics", topics: [] });
|
||||
export async function getTopics(
|
||||
message: any,
|
||||
panel: vscode.WebviewPanel | vscode.WebviewView
|
||||
): Promise<void> {
|
||||
const topics = await dcClient.getTopics(100, 0);
|
||||
const entries: TopicEntry[] = [];
|
||||
|
||||
let topicEntries: TopicEntry[] = [];
|
||||
for (const topicEntry of topicEntriesAll) {
|
||||
if (isDeleteTopic(topicEntry.root_prompt.hash)) {
|
||||
continue;
|
||||
}
|
||||
// append topicEntry to topicEntries
|
||||
topicEntries.push(topicEntry);
|
||||
for (const topic of topics) {
|
||||
const rootLog: LogEntry = {
|
||||
hash: topic.root_prompt_hash,
|
||||
parent: topic.root_prompt_parent,
|
||||
user: topic.root_prompt_user,
|
||||
date: topic.root_prompt_date,
|
||||
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的命令
|
||||
regInMessage({ command: 'deleteTopic', topicId: '' });
|
||||
export async function deleteTopic(message: any, panel: vscode.WebviewPanel | vscode.WebviewView): Promise<void> {
|
||||
regInMessage({ command: "deleteTopic", topicId: "" });
|
||||
export async function deleteTopic(
|
||||
message: any,
|
||||
panel: vscode.WebviewPanel | vscode.WebviewView
|
||||
): Promise<void> {
|
||||
const topicId = message.topicId;
|
||||
let workspaceDir = UiUtilWrapper.workspaceFoldersFirstPath();
|
||||
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'));
|
||||
await dcClient.deleteTopic(topicId);
|
||||
}
|
||||
|
@ -317,6 +317,42 @@ export class DevChatClient {
|
||||
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 {
|
||||
this.cancelMessage();
|
||||
|
Loading…
x
Reference in New Issue
Block a user