fix(credentials): save resident keys without replacing live credentials
This commit is contained in:
@@ -103,6 +103,10 @@ export function ModelCenter({ onConfig, busy, runModel }: { onConfig: (value: Co
|
||||
: '连接设置已保存,原凭据保持。');
|
||||
}
|
||||
}
|
||||
async function saveCurrentCredential(value: Connection) {
|
||||
await api('/model-center/credentials', { id: value.id, revision: value.revision, action: 'save-current' });
|
||||
if (alive.current) setNotice('当前 Key 已加密保存,重启后自动恢复。');
|
||||
}
|
||||
async function selectRoute(value: string) {
|
||||
await act(async () => {
|
||||
const [type, id] = value.split(':'); await api('/model-center/select', { type, id });
|
||||
@@ -174,6 +178,7 @@ export function ModelCenter({ onConfig, busy, runModel }: { onConfig: (value: Co
|
||||
{value.health.reason && <p className="mc-card-warning">{value.health.reason}{!!value.health.cooldownUntil && value.health.cooldownUntil > Date.now() && ' · 冷却至 ' + new Date(value.health.cooldownUntil).toLocaleTimeString()}</p>}
|
||||
<footer><button className="button secondary" disabled={locked || !value.enabled} onClick={() => void act(async () => { const result = await api<{ config: Config }>('/model-center/select', { type: value.id === 'legacy-default' ? 'legacy' : 'connection', id: value.id }); onConfig(result.config, { type: value.id === 'legacy-default' ? 'legacy' : 'connection', id: value.id, name: value.name }); })}>{runtimeConfig?.mode !== 'demo' && selected?.id === value.id ? <Check size={14} /> : <Route size={14} />}{runtimeConfig?.mode !== 'demo' && selected?.id === value.id ? '当前使用' : '用于新任务'}</button>
|
||||
<button className="button secondary" disabled={locked} onClick={() => { setDraft({ ...initial, ...value, modelIdentityAliasesText: (value.modelIdentityAliases ?? []).join(', '), streaming: value.streaming === true, capabilityDeclarations: { ...initial.capabilityDeclarations, ...value.capabilityDeclarations } }); setKey(''); setPersistKey(view?.storage?.defaultPersistKey ?? true); setEditing(true); setCatalogSource(null); setNativeTarget(null); }}>编辑</button>
|
||||
{credentialPersistenceSupported && value.hasKey && !value.credentialSource && value.savedCredential?.status !== 'saved' && <button className="button secondary" disabled={locked} onClick={() => void act(() => saveCurrentCredential(value))}>保存当前 Key</button>}
|
||||
<button className="button secondary" disabled={locked || !value.enabled} onClick={() => { setCatalogSource(value.credentialSource?.connectionId ?? value.id); setEditing(false); setNativeTarget(null); }}>获取同源模型</button>
|
||||
<button className="button secondary" onClick={() => { setNetworkTarget(value.id); setTab('network'); }}>连接诊断</button>
|
||||
<button className="mc-icon-action" aria-label={(value.enabled ? '停用' : '启用') + value.name} disabled={working} onClick={() => void act(async () => { await api('/model-center/toggle', { id: value.id, enabled: !value.enabled }); })}><Power size={15} />{value.enabled ? '停用' : '启用'}</button>
|
||||
|
||||
@@ -50,7 +50,7 @@ export class CredentialVault {
|
||||
}
|
||||
view(hub) { return { supported: process.platform === 'win32', storage: 'windows-user-dpapi', revision: this.state.revision, unavailable: this.blocked,
|
||||
connections: hub.connections().map(connection => ({ id: connection.id, name: connection.name, revision: connection.revision, ...this.status(connection) })),
|
||||
scope: '仅处理用户明确新填或确认导入的凭据;不会提取原内存连接。密文与连接登记在同一SQLite事务提交。' }; }
|
||||
scope: '处理新填、确认导入或明确选择保存的当前连接凭据;当前 Key 在工作台内部加密,不回传给调用方。密文与连接登记在同一SQLite事务提交。' }; }
|
||||
prepare(connection, explicitKey, credentialRevision) {
|
||||
if (this.blocked) throw credentialError('原保存登记无法核对;请保留原数据并修复登记后保存。');
|
||||
if (typeof explicitKey !== 'string' || !explicitKey.trim() || explicitKey.length > 16000) throw credentialError('加密保存需要重新填写API Key。', 'CREDENTIAL_INPUT_REQUIRED', 400);
|
||||
|
||||
@@ -176,7 +176,14 @@ export class ModelHub {
|
||||
if (input.revision !== connection.revision) throw error('模型连接已修改,请刷新凭据保存页面。', 409);
|
||||
return this.editRegistry(() => {
|
||||
const vault = this.vault(), previous = vault.state.entries[connection.id];
|
||||
if (input.action === 'save') {
|
||||
if (input.action === 'save-current') {
|
||||
const resident = this.credential(connection);
|
||||
if (!resident?.key) throw error('当前连接没有可保存的 Key,请先在模型连接中填写。');
|
||||
// This explicit user action stays inside the provider. Preserve the
|
||||
// resident object and revision so frozen requests and cache affinity
|
||||
// do not change; no credential is sent back to the caller.
|
||||
vault.stage(connection.id, vault.prepare(connection, resident.key, resident.revision ?? 1));
|
||||
} else if (input.action === 'save') {
|
||||
const revision = (this.credential(connection)?.revision ?? 0) + 1, record = vault.prepare(connection, input.apiKey, revision);
|
||||
if (connection.id === legacyId) { this.provider.setKey(input.apiKey); this.provider.legacyCredentialRevision = revision; }
|
||||
else this.provider.credentials.set(connection.id, { key: input.apiKey.trim(), revision });
|
||||
|
||||
Reference in New Issue
Block a user