Files
order-notification-automation/src/modules/assistant/store.cjs
T

525 lines
20 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'use strict';
const managementLimits = require('./management-limits.cjs');
const path = require('node:path'),
crypto = require('node:crypto'),
fs = require('node:fs'),
{ DatabaseSync } = require('node:sqlite'),
{ BpmVault } = require('../../platform/security/vault.cjs');
const fail = (message, status = 400) => Object.assign(new Error(message), { status }),
now = () => new Date().toISOString();
const { ConversationHistory } = require('./conversation-history.cjs');
const { ConversationEvents } = require('./conversation-events.cjs');
const day = () =>
new Intl.DateTimeFormat('en-CA', { timeZone: 'Asia/Shanghai' }).format(new Date());
function installedRuntime() {
return require('./dsh-runtime.cjs').installedRuntime();
}
class AssistantStore {
constructor(dir) {
try {
this.dir = path.resolve(dir);
this.defaultRuntime = installedRuntime(this.dir);
this.vault = new BpmVault({ stateDir: this.dir });
this.db = new DatabaseSync(path.join(this.dir, 'assistant.sqlite'));
this.db.exec(`PRAGMA journal_mode=WAL; PRAGMA busy_timeout=5000; PRAGMA secure_delete=ON;
CREATE TABLE IF NOT EXISTS config(id INTEGER PRIMARY KEY,version INTEGER NOT NULL,payload BLOB NOT NULL);
CREATE TABLE IF NOT EXISTS conversations(id TEXT PRIMARY KEY,owner TEXT NOT NULL,payload BLOB NOT NULL,created_at TEXT NOT NULL,updated_at TEXT NOT NULL);
CREATE TABLE IF NOT EXISTS turns(id TEXT PRIMARY KEY,conversation_id TEXT NOT NULL,status TEXT NOT NULL,payload BLOB NOT NULL,day TEXT NOT NULL,created_at TEXT NOT NULL);
CREATE INDEX IF NOT EXISTS conversation_turns ON turns(conversation_id,created_at);`);
this.transactionDepth = 0;
this.pendingEvents = [];
this.eventListeners = new Set();
this.db.exec(
'CREATE TABLE IF NOT EXISTS assistant_requests(conversation_id TEXT NOT NULL,request_key TEXT NOT NULL,turn_id TEXT NOT NULL,payload BLOB NOT NULL,PRIMARY KEY(conversation_id,request_key))',
);
this.db.exec(
'CREATE TABLE IF NOT EXISTS assistant_usage(turn_id TEXT PRIMARY KEY,day TEXT NOT NULL); CREATE INDEX IF NOT EXISTS assistant_usage_day ON assistant_usage(day); INSERT OR IGNORE INTO assistant_usage SELECT id,day FROM turns',
);
this.conversationHistory = new ConversationHistory(this);
this.conversationEvents = new ConversationEvents(this);
this.transaction(() => {
this.providers = new (require('./providers.cjs').ProviderRegistry)(this);
const row = this.db.prepare('SELECT * FROM config WHERE id=1').get();
if (row) {
const raw = this.decode('config', row.payload);
if (raw.apiKey)
this.db
.prepare('UPDATE config SET payload=? WHERE id=1')
.run(this.encode('config', { ...raw, apiKey: '' }));
}
});
require('../../shared/refresh-revisions.cjs').installRevisionTracking(this.db, [
'config',
['conversations', "'owner:' || $row.owner"],
['turns', "'owner:' || (SELECT owner FROM conversations WHERE id=$row.conversation_id)"],
['turns', "'usage:' || $row.day", 'OLD.day IS NOT NEW.day'],
['assistant_usage', "'usage:' || $row.day"],
['assistant_providers', "'config'"],
['assistant_provider_defaults', "'config'"],
]);
} catch (error) {
try {
this.db?.close();
} catch {}
try {
this.vault?.destroy();
} catch {}
throw error;
}
}
transaction(work) {
if (this.transactionDepth) return work();
this.db.exec('BEGIN IMMEDIATE');
this.transactionDepth = 1;
this.pendingEvents = [];
let result, events;
try {
result = work();
this.db.exec('COMMIT');
events = this.pendingEvents;
} catch (e) {
this.db.exec('ROLLBACK');
throw e;
} finally {
this.transactionDepth = 0;
this.pendingEvents = [];
}
for (const event of events)
for (const listener of this.eventListeners)
try {
listener(event);
} catch {}
return result;
}
subscribe(listener) {
this.eventListeners.add(listener);
return () => this.eventListeners.delete(listener);
}
events(id, owner, options) {
return this.conversationEvents.read(id, owner, options);
}
eventCursor(id, owner) {
return this.conversationEvents.cursor(id, owner);
}
stage(turnId, kind, data) {
return this.conversationEvents.append(turnId, kind, data);
}
encode(id, value) {
return this.vault.encode('assistant:' + id, value);
}
decode(id, value) {
return this.vault.decode('assistant:' + id, value);
}
secretSettings() {
const row = this.db.prepare('SELECT * FROM config WHERE id=1').get(),
saved = row ? this.decode('config', row.payload) : null;
const result = row
? {
allowCodeRepair: false,
repairRepository: '',
...saved,
runtimePath:
saved.engine === 'dsh' ? saved.runtimePath || this.defaultRuntime : this.defaultRuntime,
version: row.version,
}
: {
version: 1,
enabled: false,
runtimePath: this.defaultRuntime,
apiBase: 'https://api.deepseek.com/anthropic',
model: 'deepseek-flash',
apiKey: '',
timeoutSeconds: 90,
dailyLimit: 100,
allowActions: false,
allowCodeRepair: false,
repairRepository: '',
};
if (this.providers) {
let legacy;
try {
legacy = this.providers.read('deepseek');
} catch (error) {
if (error.status !== 409) throw error;
}
result.apiKey = legacy?.apiKey || '';
if (legacy) {
result.apiBase = legacy.apiBase;
if (!legacy.models.some((m) => m.id === result.model))
result.model = legacy.models[0]?.id || '';
}
}
return result;
}
settings() {
const { apiKey, ...settings } = this.secretSettings();
return { ...settings, hasApiKey: !!apiKey };
}
saveSettings(input) {
return this.transaction(() => {
const legacyWrite =
!!input && ['apiBase', 'model', 'apiKey'].some((key) => Object.hasOwn(input, key));
const old = this.secretSettings(),
keys = [
'version',
'enabled',
'runtimePath',
'apiBase',
'model',
'apiKey',
'hasApiKey',
'timeoutSeconds',
'dailyLimit',
'allowActions',
'allowCodeRepair',
'repairRepository',
'engine',
];
if (!input || Array.isArray(input) || Object.keys(input).some((k) => !keys.includes(k)))
throw fail('助手配置格式不正确。');
if (input.version !== old.version) throw fail('助手配置已更新,请刷新后重试。', 409);
input = { ...old, ...input, apiKey: input.apiKey ?? '' };
if (input.version !== old.version) throw fail('助手配置已更新,请刷新后重试。', 409);
for (const key of ['enabled', 'allowActions'])
if (typeof input[key] !== 'boolean') throw fail('请选择是否启用助手及恢复操作。');
if (
!Number.isInteger(input.timeoutSeconds) ||
input.timeoutSeconds < 15 ||
input.timeoutSeconds > 180 ||
!Number.isInteger(input.dailyLimit) ||
input.dailyLimit < 1 ||
input.dailyLimit > 1000
)
throw fail('超时须为 15–180 秒,每日调用上限须为 1–1000。');
for (const key of ['runtimePath', 'apiBase', 'model'])
if (
typeof input[key] !== 'string' ||
input[key].length > 1000 ||
/[\x00-\x1f]/.test(input[key])
)
throw fail('运行包、模型地址或名称格式不正确。');
const runtimePath = input.runtimePath.trim(),
apiBase = input.apiBase.trim().replace(/\/+$/, ''),
model = input.model.trim();
if (
runtimePath &&
(!path.isAbsolute(runtimePath) ||
!['.exe', '.cjs', '.mjs', '.js'].includes(path.extname(runtimePath).toLowerCase()))
)
throw fail('请选择本机官方 DSH CLI 的绝对路径(exe/cjs/mjs/js)。');
if (apiBase) {
let u;
try {
u = new URL(apiBase);
} catch {
throw fail('模型 API 地址无效。');
}
if (
u.username ||
u.password ||
u.search ||
u.hash ||
!(
u.protocol === 'https:' ||
(u.protocol === 'http:' && ['127.0.0.1', 'localhost', '[::1]'].includes(u.hostname))
)
)
throw fail('模型 API 请使用 HTTPS,或本机 HTTP 地址。');
}
if (
typeof input.apiKey !== 'string' ||
input.apiKey.length > 4096 ||
/[\r\n]/.test(input.apiKey)
)
throw fail('请输入有效模型密钥。');
if (!input.apiKey && old.apiKey && (apiBase !== old.apiBase || model !== old.model))
throw fail('更换模型地址或名称后请重新填写密钥。');
const allowCodeRepair =
input.allowCodeRepair === undefined ? old.allowCodeRepair : input.allowCodeRepair,
repairRepository =
input.repairRepository === undefined ? old.repairRepository : input.repairRepository;
if (
typeof allowCodeRepair !== 'boolean' ||
typeof repairRepository !== 'string' ||
repairRepository.length > 1000 ||
(repairRepository &&
(!path.isAbsolute(repairRepository) || /[\x00-\x1f]/.test(repairRepository)))
)
throw fail('代码修复权限或仓库路径无效。');
if (allowCodeRepair && !repairRepository) throw fail('开放代码修复前请选择源码仓库。');
const saved = {
engine: 'dsh',
allowCodeRepair,
repairRepository,
enabled: input.enabled,
runtimePath,
apiBase,
model,
apiKey: input.apiKey || old.apiKey,
timeoutSeconds: input.timeoutSeconds,
dailyLimit: input.dailyLimit,
allowActions: input.allowActions,
};
let providerReady = false;
try {
providerReady = !!this.providers?.resolve();
} catch {}
if (
saved.enabled &&
(!runtimePath || !((legacyWrite && apiBase && model && saved.apiKey) || providerReady))
)
throw fail('启用前请填写运行包,并配置可用供应商和默认模型。');
this.db
.prepare(
'INSERT INTO config VALUES(1,?,?) ON CONFLICT(id) DO UPDATE SET version=excluded.version,payload=excluded.payload',
)
.run(old.version + 1, this.encode('config', { ...saved, apiKey: '' }));
if (legacyWrite) this.providers?.syncLegacy(saved);
return this.settings();
});
}
conversations(owner, filters) {
return this.conversationHistory.list(owner, filters).items;
}
listConversations(owner, filters) {
return this.conversationHistory.list(owner, filters);
}
row(id, owner, { includeDeleted = false } = {}) {
const row = this.db
.prepare('SELECT * FROM conversations WHERE id=? AND owner=?')
.get(id, owner);
if (!row || (row.deleted_at && !includeDeleted)) throw fail('没有找到此对话。', 404);
return row;
}
create(owner, options) {
return this.conversationHistory.create(owner, options);
}
conversation(id, owner, options) {
return this.conversationHistory.conversation(id, owner, options);
}
archive(id, owner, input) {
return this.conversationHistory.setArchived(id, owner, true, input);
}
restore(id, owner, input) {
return this.conversationHistory.setArchived(id, owner, false, input);
}
continueConversation(id, owner, input) {
return this.conversationHistory.continue(id, owner, input);
}
trash(id, owner, input) {
return this.conversationHistory.trash(id, owner, input);
}
restoreDeleted(id, owner, input) {
return this.conversationHistory.restoreDeleted(id, owner, input);
}
purge(id, owner, input) {
return this.conversationHistory.purge(id, owner, input);
}
setModel(id, owner, input) {
return this.conversationHistory.setModel(id, owner, input);
}
history(id, owner) {
return this.conversationHistory.history(id, owner);
}
requestTurn(id, owner, requestId, payload) {
this.row(id, owner);
if (requestId === undefined) return null;
if (typeof requestId !== 'string' || !/^[-\w]{1,100}$/.test(requestId))
throw fail('请求编号无效。');
const key = crypto.createHash('sha256').update(requestId).digest('hex'),
row = this.db
.prepare('SELECT * FROM assistant_requests WHERE conversation_id=? AND request_key=?')
.get(id, key);
if (!row) return null;
const digest = crypto.createHash('sha256').update(JSON.stringify(payload)).digest('hex');
if (this.decode('request:' + id + ':' + key, row.payload).digest !== digest)
throw fail('此请求编号已用于不同的消息。', 409);
const turn = this.db.prepare('SELECT * FROM turns WHERE id=?').get(row.turn_id);
if (!turn) throw fail('请求结果不可用,请重新读取会话。', 409);
return {
id: turn.id,
status: turn.status,
createdAt: turn.created_at,
...this.decode(turn.id, turn.payload),
};
}
beginTurn(
id,
owner,
text,
{ requestId, requestPayload, diagnosticSnapshot, modelSelection, attachments } = {},
) {
return this.transaction(() => {
const existing = this.requestTurn(id, owner, requestId, requestPayload);
if (existing) return existing;
const row = this.row(id, owner),
count = this.db
.prepare('SELECT COUNT(*) AS n FROM turns WHERE conversation_id=?')
.get(id).n;
if (row.archived) throw fail('请先恢复归档会话或继续新对话。', 409);
if (count >= 100) throw fail('此对话已达 100 轮,请新建对话。', 409);
if (
this.db.prepare("SELECT 1 FROM turns WHERE conversation_id=? AND status='running'").get(id)
)
throw fail('当前对话正在回复,请等待或停止。', 409);
if (
this.db.prepare('SELECT COUNT(*) AS n FROM assistant_usage WHERE day=?').get(day()).n >=
this.settings().dailyLimit
)
throw fail('已达到今天的模型调用上限。', 429);
const turnId = crypto.randomUUID(),
at = now(),
payload = {
text,
reply: '',
actions: [],
evidence: [],
...(attachments?.length ? { attachments } : {}),
...(diagnosticSnapshot ? { diagnosticSnapshot } : {}),
...(modelSelection ? { modelSelection } : {}),
};
this.db
.prepare('INSERT INTO turns VALUES(?,?,?,?,?,?)')
.run(turnId, id, 'running', this.encode(turnId, payload), day(), at);
this.db.prepare('INSERT INTO assistant_usage VALUES(?,?)').run(turnId, day());
if (requestId !== undefined) {
const key = crypto.createHash('sha256').update(requestId).digest('hex'),
digest = crypto.createHash('sha256').update(JSON.stringify(requestPayload)).digest('hex');
this.db
.prepare('INSERT INTO assistant_requests VALUES(?,?,?,?)')
.run(id, key, turnId, this.encode('request:' + id + ':' + key, { digest }));
}
const metadata = this.decode(id, row.payload);
this.db.prepare('UPDATE conversations SET payload=?,updated_at=? WHERE id=?').run(
this.encode(id, {
...metadata,
title: count ? metadata.title : text.slice(0, 40),
version: (metadata.version || 1) + 1,
}),
at,
id,
);
this.stage(turnId, 'queued');
if (diagnosticSnapshot)
this.stage(turnId, 'snapshot', {
observedAt: diagnosticSnapshot.at,
connectionCount: diagnosticSnapshot.connections?.length,
serverCount: diagnosticSnapshot.servers?.length,
jobCount: diagnosticSnapshot.jobs?.length,
});
return { id: turnId, status: 'running', createdAt: at, ...payload };
});
}
updateTurn(id, status, patch) {
return this.transaction(() => {
const row = this.db.prepare('SELECT * FROM turns WHERE id=?').get(id);
if (!row) throw fail('回复不存在。', 404);
const previous = this.decode(id, row.payload),
value = { ...previous, ...patch };
if (status !== 'running') value.cancelRequested = false;
this.db
.prepare('UPDATE turns SET status=?,payload=? WHERE id=?')
.run(status, this.encode(id, value), id);
if (value.cancelRequested && !previous.cancelRequested) this.stage(id, 'cancel_requested');
for (const action of value.actions || []) {
const before = previous.actions?.find((a) => a.id === action.id);
if (action.status === 'running' && before?.status !== 'running')
this.stage(id, 'action_running', { actionId: action.id, status: 'running' });
if (action.progress && JSON.stringify(action.progress) !== JSON.stringify(before?.progress))
this.stage(id, 'action_progress', {
actionId: action.id,
status: action.status,
...action.progress,
});
if (action.status !== before?.status && !['proposed', 'running'].includes(action.status))
this.stage(id, 'action_result', {
actionId: action.id,
status: action.status,
resultStatus: action.result?.status,
});
}
if (status !== row.status && status === 'success' && value.actions?.length) {
this.stage(id, 'proposal', { count: value.actions.length });
this.stage(id, 'awaiting_confirmation', { count: value.actions.length });
}
if (
status !== row.status &&
['success', 'error', 'cancelled', 'interrupted', 'needs_review'].includes(status)
)
this.stage(id, status === 'success' ? 'completed' : status, {
status,
...(status === 'needs_review' ? { code: 'stop_unconfirmed' } : {}),
});
return { id, status, createdAt: row.created_at, ...value };
});
}
action(conversationId, owner, actionId, work) {
return this.transaction(() => {
if (this.row(conversationId, owner).archived) throw fail('请先恢复归档会话。', 409);
for (const row of this.db
.prepare('SELECT * FROM turns WHERE conversation_id=? ORDER BY rowid DESC')
.all(conversationId)) {
const turn = this.decode(row.id, row.payload),
action = turn.actions.find((a) => a.id === actionId);
if (action) {
const result = work(action);
this.updateTurn(row.id, row.status, { actions: turn.actions });
return result;
}
}
throw fail('操作卡片不存在。', 404);
});
}
recover() {
return this.transaction(() => {
for (const row of this.db.prepare('SELECT * FROM turns').all()) {
const turn = this.decode(row.id, row.payload);
let changed = row.status === 'running';
for (const a of turn.actions || [])
if (a.status === 'running') {
a.status = 'needs_review';
a.error = '服务重启,操作结果需要核实。';
changed = true;
}
if (changed)
this.updateTurn(row.id, row.status === 'running' ? 'interrupted' : row.status, {
...turn,
error: '服务重启,上一轮已中断。',
});
}
});
}
reserveModelCall(turnId, round) {
return this.transaction(() => {
const row = this.db.prepare('SELECT status FROM turns WHERE id=?').get(turnId);
if (
!row ||
row.status !== 'running' ||
!Number.isInteger(round) ||
round < 1 ||
round > managementLimits.maxRounds
)
throw fail('额外模型调用状态无效。');
const key = turnId + ':management:' + round;
if (this.db.prepare('SELECT 1 FROM assistant_usage WHERE turn_id=?').get(key))
throw fail('此管理调用已计入额度。', 409);
if (this.usage().calls >= this.settings().dailyLimit)
throw fail('已达到今天的模型调用上限。', 429);
this.db.prepare('INSERT INTO assistant_usage(turn_id,day) VALUES(?,?)').run(key, day());
});
}
usage() {
return {
today: day(),
calls: this.db.prepare('SELECT COUNT(*) AS n FROM assistant_usage WHERE day=?').get(day()).n,
limit: this.settings().dailyLimit,
};
}
close() {
this.eventListeners.clear();
this.db.close();
this.vault.destroy();
}
}
module.exports = { AssistantStore, fail };