fix: preserve model state, isolate commands and harden local HTTP in 1.12.1

This commit is contained in:
Agent Workbench Dev
2026-10-30 23:01:19 +08:00
parent c1e9575259
commit f4bd0ad8dc
26 changed files with 373 additions and 68 deletions
+22 -18
View File
@@ -72,23 +72,27 @@ export class ConfigImporter {
if (!preview || Date.now() - preview.created > 300000) throw new Error('导入预览已过期,请重新预览。');
const current = this.read(preview.source, preview.path);
if (current.fingerprint !== preview.fingerprint) { this.previews.delete(id); throw new Error('来源配置已变化,请重新预览再导入。'); }
const imported = [], conflicts = [];
const sourceId = digest({ source: preview.source, path: preview.path });
for (const item of current.entries) {
const previous = this.hub.state.connections.find(value => value.importOrigin === item.origin);
if (previous && previous.importRevision !== previous.revision) { conflicts.push(previous.name); continue; }
const value = this.hub.upsert({ ...(previous ?? {}), ...item, id: previous?.id, revision: previous?.revision, enabled: previous ? previous.enabled : true,
...(item.apiKey ? { apiKey: item.apiKey } : { apiKey: '' }) });
const stored = this.hub.connection(value.id); stored.source = preview.source; stored.importOrigin = item.origin; stored.importRevision = stored.revision;
imported.push(value.id);
}
const previousSource = this.hub.state.imports[sourceId];
const checkedAt = new Date().toISOString();
this.hub.state.imports[sourceId] = { source: preview.source, path: preview.path, enabled: autoSync === undefined ? previousSource?.enabled === true : autoSync === true,
lastFingerprint: current.fingerprint, lastCheckedAt: checkedAt, lastSyncedAt: imported.length ? checkedAt : previousSource?.lastSyncedAt ?? null,
lastResult: { imported: imported.length, conflicts } };
this.hub.save(); this.previews.delete(id);
return { imported, conflicts, sourceId };
const result = this.hub.editRegistry(() => {
const imported = [], conflicts = [];
const sourceId = digest({ source: preview.source, path: preview.path });
for (const item of current.entries) {
const previous = this.hub.state.connections.find(value => value.importOrigin === item.origin);
if (previous && previous.importRevision !== previous.revision) { conflicts.push(previous.name); continue; }
const value = this.hub.upsert({ ...(previous ?? {}), ...item, id: previous?.id, revision: previous?.revision, enabled: previous ? previous.enabled : true,
...(item.apiKey ? { apiKey: item.apiKey } : { apiKey: '' }) });
const stored = this.hub.connection(value.id); stored.source = preview.source; stored.importOrigin = item.origin; stored.importRevision = stored.revision;
imported.push(value.id);
}
const previousSource = this.hub.state.imports[sourceId];
const checkedAt = new Date().toISOString();
this.hub.state.imports[sourceId] = { source: preview.source, path: preview.path, enabled: autoSync === undefined ? previousSource?.enabled === true : autoSync === true,
lastFingerprint: current.fingerprint, lastCheckedAt: checkedAt, lastSyncedAt: imported.length ? checkedAt : previousSource?.lastSyncedAt ?? null,
lastResult: { imported: imported.length, conflicts } };
this.hub.save();
return { imported, conflicts, sourceId };
});
this.previews.delete(id);
return result;
}
toggle(id, enabled) {
const source = this.hub.state.imports[id]; if (!source) throw new Error('导入来源不存在。');
@@ -104,7 +108,7 @@ export class ConfigImporter {
if (read.fingerprint === source.lastFingerprint) { this.hub.save(); continue; }
const preview = this.preview(source.source, source.path);
reports.push(this.apply(preview.id, true));
} catch (cause) { source.lastError = '来源暂不可读取或格式变化,请重新预览核对。'; source.lastCheckedAt = new Date().toISOString(); this.hub.save(); reports.push({ id, error: source.lastError }); }
} catch (cause) { const current = this.hub.state.imports[id]; current.lastError = '来源暂不可读取或格式变化,请重新预览核对。'; current.lastCheckedAt = new Date().toISOString(); this.hub.save(); reports.push({ id, error: current.lastError }); }
}
return reports;
}
+39 -8
View File
@@ -26,7 +26,40 @@ export class ModelHub {
if (recovered) this.saveAttempts();
provider.credentials ??= new Map();
}
save() { this.state.revision++; this.store.storage.transaction(() => this.store.storage.setMeta('modelRegistry', this.state)); }
save(config = null) {
this.state.revision++;
if (this.editingRegistry) {
if (config) throw error('批量连接导入不能修改当前运行模式。');
return;
}
const persist = () => this.store.storage.setMeta('modelRegistry', this.state);
try {
if (config) this.store.saveConfig(config, { beforeSave: persist });
else this.store.storage.transaction(persist);
} catch (cause) {
// Keep runtime routing consistent with the authoritative committed state.
this.state = this.store.storage.meta('modelRegistry', { schema: 1, revision: 0,
selected: { type: 'legacy', id: legacyId }, connections: [], pools: [], imports: {} });
throw cause;
}
}
editRegistry(action) {
if (this.editingRegistry) return action();
const before = structuredClone(this.state), credentials = new Map(this.provider.credentials);
this.editingRegistry = true;
try {
return this.store.storage.transaction(() => {
const result = action();
this.store.storage.setMeta('modelRegistry', this.state);
return result;
});
} catch (cause) {
this.state = before;
this.provider.credentials.clear();
for (const [id, value] of credentials) this.provider.credentials.set(id, value);
throw cause;
} finally { this.editingRegistry = false; }
}
saveAttempts() { this.attempts = this.attempts.slice(-1000); this.store.storage.transaction(() => this.store.storage.setMeta('modelAttempts', this.attempts)); }
connections() {
return [{ id: legacyId, name: '原模型连接', brand: '兼容服务', protocol: 'chat-completions', baseUrl: this.store.config.baseUrl,
@@ -90,16 +123,14 @@ export class ModelHub {
maxAttempts: bounded(input.maxAttempts, 3, 1, 8), queueTimeoutMs: bounded(input.queueTimeoutMs, 15000, 100, 60000), revision: (previous?.revision ?? 0) + 1 };
this.state.pools = [...this.state.pools.filter(item => item.id !== value.id), value]; this.save(); return value;
}
select(input) {
select(input, config = null) {
if (input.type === 'connection') this.connection(input.id);
else if (input.type === 'pool') { if (!this.state.pools.some(item => item.id === input.id)) throw error('模型池不存在。'); }
else if (input.type !== 'legacy') throw error('模型选择无效。');
this.state.selected = { type: input.type, id: input.type === 'legacy' ? legacyId : input.id }; this.save();
if (input.type !== 'legacy') {
// Keep the original endpoint/key pair intact. Only a Run's immutable
// routing snapshot receives the selected member's endpoint and model.
this.store.saveConfig({ mode: 'compatible' });
}
this.state.selected = { type: input.type, id: input.type === 'legacy' ? legacyId : input.id };
// Selection and mode must commit together; the original endpoint/key pair
// stays intact and a Run alone receives its immutable routing snapshot.
this.save(config ?? (input.type !== 'legacy' ? { mode: 'compatible' } : null));
return this.view();
}
prepare(config) {