Files
zn-ai/electron/gateway/session-store.ts
duanshuwen b1dea9a5c2 feat: implement task management store with IPC integration
- Added a new task store in `src-react/stores/task.ts` to manage tasks and their statuses.
- Implemented functions for creating, executing, and retrying tasks, along with handling task progress and completion.
- Introduced persistence for tasks using IPC.
- Created utility functions for normalizing room types and building subtasks.
- Added a new CSS file for global styles in `src-react/styles.css`.
- Created runtime types in `src-react/types/runtime.ts` and exported them.
- Updated the main entry points for Vue and React applications to support dynamic framework loading.
- Refactored chat model interfaces and utility functions into `src/shared/chat-model.ts`.
- Updated TypeScript configuration to include paths for React components and types.
- Enhanced Vite configuration to support both Vue and React frameworks.
2026-04-17 07:09:56 +08:00

134 lines
3.4 KiB
TypeScript

import * as fs from 'fs';
import * as path from 'path';
import { app } from 'electron';
import logManager from '@electron/service/logger';
import type { RawMessage } from '@shared/chat-model';
let sessionsFilePath: string | null = null;
function getSessionsFilePath(): string {
if (!sessionsFilePath) {
sessionsFilePath = path.join(app.getPath('userData'), 'chat-sessions.json');
}
return sessionsFilePath;
}
export interface SessionEntry {
key: string;
messages: RawMessage[];
updatedAt: number;
activeRun?: {
runId: string;
abortController: AbortController;
};
}
class SessionStore {
private sessions = new Map<string, SessionEntry>();
private loaded = false;
private ensureLoaded(): void {
if (this.loaded) return;
this.loaded = true;
this.loadFromDisk();
}
private loadFromDisk(): void {
try {
const filePath = getSessionsFilePath();
if (fs.existsSync(filePath)) {
const data = JSON.parse(fs.readFileSync(filePath, 'utf-8')) as Record<
string,
Omit<SessionEntry, 'activeRun'>
>;
for (const [key, entry] of Object.entries(data)) {
this.sessions.set(key, {
...entry,
activeRun: undefined,
});
}
}
} catch (e) {
logManager.error('Failed to load sessions from disk:', e);
}
}
saveToDisk(): void {
try {
const filePath = getSessionsFilePath();
const data: Record<string, Omit<SessionEntry, 'activeRun'>> = {};
for (const [key, entry] of this.sessions) {
data[key] = {
key: entry.key,
messages: entry.messages,
updatedAt: entry.updatedAt,
};
}
fs.mkdirSync(path.dirname(filePath), { recursive: true });
fs.writeFileSync(filePath, JSON.stringify(data, null, 2), 'utf-8');
} catch (e) {
logManager.error('Failed to save sessions to disk:', e);
}
}
getOrCreate(key: string): SessionEntry {
this.ensureLoaded();
let session = this.sessions.get(key);
if (!session) {
session = {
key,
messages: [],
updatedAt: Date.now(),
};
this.sessions.set(key, session);
}
return session;
}
get(key: string): SessionEntry | undefined {
this.ensureLoaded();
return this.sessions.get(key);
}
getAllKeys(): string[] {
this.ensureLoaded();
return Array.from(this.sessions.keys());
}
appendMessage(key: string, message: RawMessage): void {
const session = this.getOrCreate(key);
session.messages.push(message);
session.updatedAt = Date.now();
this.saveToDisk();
}
getMessages(key: string, limit = 50): RawMessage[] {
const session = this.get(key);
if (!session) return [];
return session.messages.slice(-limit);
}
setActiveRun(key: string, runId: string, abortController: AbortController): void {
const session = this.getOrCreate(key);
session.activeRun = { runId, abortController };
}
clearActiveRun(key: string): void {
const session = this.sessions.get(key);
if (session) {
session.activeRun = undefined;
}
}
getActiveRun(key: string): { runId: string; abortController: AbortController } | undefined {
return this.sessions.get(key)?.activeRun;
}
deleteSession(key: string): void {
this.sessions.delete(key);
this.saveToDisk();
}
}
export const sessionStore = new SessionStore();