Files
metona-ai-desktop/electron/services/config.service.ts
T
thzxx 1d185db6b3 feat: MetonaAI Desktop 初始项目
- Electron + React + TypeScript 架构
- 三栏布局: Sidebar | ChatPanel | DetailPanel
- 9 个内置工具 (文件系统/网络/记忆/命令)
- SQLite 持久化 (better-sqlite3)
- MUI 暗色/亮色主题系统
- Agent Loop ReAct 状态机引擎
- DeepSeek / Agnes AI / Ollama Provider 适配器
- MCP 协议集成
- 系统托盘 + 全局快捷键
- Tailwind CSS v4 + Tailwind Merge
- 修复: Sidebar 缺失 TextField 导入导致黑屏
2026-06-27 21:33:27 +08:00

101 lines
2.3 KiB
TypeScript

/**
* Config Service — 配置读写
*
* 管理 app_config 表的读写操作。
* 所有配置值以 JSON 格式存储。
*
* @see docs/MetonaAI-Desktop 架构与交互设计.html — 数据库配置
*/
import type Database from 'better-sqlite3';
import log from 'electron-log';
export interface ConfigRow {
key: string;
value: string;
category: string;
updated_at: number;
}
export class ConfigService {
constructor(private getDB: () => Database.Database) {}
/**
* 获取配置值
*/
get<T = unknown>(key: string): T | null {
const db = this.getDB();
const row = db.prepare('SELECT value FROM app_config WHERE key = ?').get(key) as ConfigRow | undefined;
if (!row) return null;
try {
return JSON.parse(row.value) as T;
} catch {
return row.value as T;
}
}
/**
* 设置配置值
*/
set(key: string, value: unknown): void {
const db = this.getDB();
const jsonValue = typeof value === 'string' ? value : JSON.stringify(value);
db.prepare(`
INSERT OR REPLACE INTO app_config (key, value, updated_at)
VALUES (?, ?, ?)
`).run(key, jsonValue, Date.now());
log.debug(`Config set: ${key}`);
}
/**
* 获取所有配置
*/
getAll(): Record<string, unknown> {
const db = this.getDB();
const rows = db.prepare('SELECT key, value FROM app_config').all() as ConfigRow[];
const config: Record<string, unknown> = {};
for (const row of rows) {
try {
config[row.key] = JSON.parse(row.value);
} catch {
config[row.key] = row.value;
}
}
return config;
}
/**
* 获取指定分类的所有配置
*/
getByCategory(category: string): Record<string, unknown> {
const db = this.getDB();
const rows = db.prepare('SELECT key, value FROM app_config WHERE category = ?').all(category) as ConfigRow[];
const config: Record<string, unknown> = {};
for (const row of rows) {
try {
config[row.key] = JSON.parse(row.value);
} catch {
config[row.key] = row.value;
}
}
return config;
}
/**
* 删除配置
*/
delete(key: string): boolean {
const db = this.getDB();
const result = db.prepare('DELETE FROM app_config WHERE key = ?').run(key);
return result.changes > 0;
}
}