Help chat (#1007)
This commit is contained in:
@@ -104,10 +104,19 @@ export class IpcClient {
|
||||
private ipcRenderer: IpcRenderer;
|
||||
private chatStreams: Map<number, ChatStreamCallbacks>;
|
||||
private appStreams: Map<number, AppStreamCallbacks>;
|
||||
private helpStreams: Map<
|
||||
string,
|
||||
{
|
||||
onChunk: (delta: string) => void;
|
||||
onEnd: () => void;
|
||||
onError: (error: string) => void;
|
||||
}
|
||||
>;
|
||||
private constructor() {
|
||||
this.ipcRenderer = (window as any).electron.ipcRenderer as IpcRenderer;
|
||||
this.chatStreams = new Map();
|
||||
this.appStreams = new Map();
|
||||
this.helpStreams = new Map();
|
||||
// Set up listeners for stream events
|
||||
this.ipcRenderer.on("chat:response:chunk", (data) => {
|
||||
if (
|
||||
@@ -180,6 +189,48 @@ export class IpcClient {
|
||||
console.error("[IPC] Invalid error data received:", error);
|
||||
}
|
||||
});
|
||||
|
||||
// Help bot events
|
||||
this.ipcRenderer.on("help:chat:response:chunk", (data) => {
|
||||
if (
|
||||
data &&
|
||||
typeof data === "object" &&
|
||||
"sessionId" in data &&
|
||||
"delta" in data
|
||||
) {
|
||||
const { sessionId, delta } = data as {
|
||||
sessionId: string;
|
||||
delta: string;
|
||||
};
|
||||
const callbacks = this.helpStreams.get(sessionId);
|
||||
if (callbacks) callbacks.onChunk(delta);
|
||||
}
|
||||
});
|
||||
|
||||
this.ipcRenderer.on("help:chat:response:end", (data) => {
|
||||
if (data && typeof data === "object" && "sessionId" in data) {
|
||||
const { sessionId } = data as { sessionId: string };
|
||||
const callbacks = this.helpStreams.get(sessionId);
|
||||
if (callbacks) callbacks.onEnd();
|
||||
this.helpStreams.delete(sessionId);
|
||||
}
|
||||
});
|
||||
this.ipcRenderer.on("help:chat:response:error", (data) => {
|
||||
if (
|
||||
data &&
|
||||
typeof data === "object" &&
|
||||
"sessionId" in data &&
|
||||
"error" in data
|
||||
) {
|
||||
const { sessionId, error } = data as {
|
||||
sessionId: string;
|
||||
error: string;
|
||||
};
|
||||
const callbacks = this.helpStreams.get(sessionId);
|
||||
if (callbacks) callbacks.onError(error);
|
||||
this.helpStreams.delete(sessionId);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static getInstance(): IpcClient {
|
||||
@@ -1089,4 +1140,28 @@ export class IpcClient {
|
||||
public async deletePrompt(id: number): Promise<void> {
|
||||
await this.ipcRenderer.invoke("prompts:delete", id);
|
||||
}
|
||||
|
||||
// --- Help bot ---
|
||||
public startHelpChat(
|
||||
sessionId: string,
|
||||
message: string,
|
||||
options: {
|
||||
onChunk: (delta: string) => void;
|
||||
onEnd: () => void;
|
||||
onError: (error: string) => void;
|
||||
},
|
||||
): void {
|
||||
this.helpStreams.set(sessionId, options);
|
||||
this.ipcRenderer
|
||||
.invoke("help:chat:start", { sessionId, message })
|
||||
.catch((err) => {
|
||||
this.helpStreams.delete(sessionId);
|
||||
showError(err);
|
||||
options.onError(String(err));
|
||||
});
|
||||
}
|
||||
|
||||
public cancelHelpChat(sessionId: string): void {
|
||||
this.ipcRenderer.invoke("help:chat:cancel", sessionId).catch(() => {});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user