Files
Netcatty/application/state/useVaultAgentBridge.ts
T
axe006andClaude Sonnet 5 bd4680e4d5 fix(ai): 静默会话开关只对真正的外部 MCP 调用生效
openHostForVaultAgent 这条 host_open 路径同时被外部 MCP 客户端和
应用内 Catty AI 聊天工具共用。此前静默开关全局生效会导致应用内 AI
聊天助手打开会话时也被静默,用户在侧边栏里看不到任何新标签,与该
工具"会创建一个终端标签"的文档描述不符。

policy.cjs 对 write 类能力有一条硬性约束:BUILTIN(应用内聊天)
surface 必须携带 chatSessionId,否则请求在权限层直接被拒绝;外部
MCP 的 PUBLIC surface 则没有这个概念、通常不带 chatSessionId。据此
把 chatSessionId 的有无作为可靠信号:只有真正的外部 MCP 调用(无
chatSessionId)才应用静默设置,应用内 AI 聊天始终按现状打开可见标签。

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-21 11:12:31 +08:00

190 lines
7.2 KiB
TypeScript

import { useEffect, useRef } from 'react';
import { applyGroupDefaults, resolveGroupDefaults } from '../../domain/groupConfig';
import type { GroupConfig, Host, Identity, KnownHost, ManagedSource, PortForwardingRule, ProxyProfile, Snippet, SSHKey, TerminalSettings, VaultNote } from '../../domain/models';
import { materializeHostProxyProfile } from '../../domain/proxyProfiles';
import {
handleVaultAgentOp,
registerVaultAgentHandler,
setupVaultAgentBridge,
type VaultAgentApiDeps,
} from '../../infrastructure/ai/vaultAgentBridgeClient';
import {
clearReferenceKeyPassphrases,
rememberKeyPassphrase,
removeDefaultKeyPassphraseAliases,
} from '../defaultKeyPassphrases';
export interface UseVaultAgentBridgeInput {
hosts: Host[];
snippets: Snippet[];
portForwardingRules: PortForwardingRule[];
keys: SSHKey[];
identities: Identity[];
knownHosts: KnownHost[];
proxyProfiles: ProxyProfile[];
managedSources: ManagedSource[];
terminalSettings?: Pick<TerminalSettings, 'keepaliveInterval' | 'keepaliveCountMax'>;
updateHosts: (hosts: Host[]) => void;
updateKeys: (keys: SSHKey[]) => Promise<unknown> | unknown;
updateSnippets: (snippets: Snippet[]) => void;
customGroups: string[];
updateCustomGroups: (groups: string[]) => void;
groupConfigs: GroupConfig[];
updateGroupConfigs: (configs: GroupConfig[]) => void;
updateManagedSources: (sources: ManagedSource[]) => void;
updatePortForwardingRules: (rules: PortForwardingRule[]) => void;
notes: VaultNote[];
updateNotes: (notes: VaultNote[]) => void;
startTunnel: VaultAgentApiDeps['startTunnel'];
stopTunnel: VaultAgentApiDeps['stopTunnel'];
stopRuleTunnels: VaultAgentApiDeps['stopRuleTunnels'];
openHost?: VaultAgentApiDeps['openHost'];
closeSession?: VaultAgentApiDeps['closeSession'];
}
type VaultAgentSnapshot = {
hosts: Host[];
keys: SSHKey[];
notes: VaultNote[];
snippets: Snippet[];
customGroups: string[];
groupConfigs: GroupConfig[];
portForwardingRules: PortForwardingRule[];
managedSources: ManagedSource[];
};
const selectVaultAgentSnapshot = (input: UseVaultAgentBridgeInput): VaultAgentSnapshot => ({
hosts: input.hosts,
keys: input.keys,
notes: input.notes,
snippets: input.snippets,
customGroups: input.customGroups,
groupConfigs: input.groupConfigs,
portForwardingRules: input.portForwardingRules,
managedSources: input.managedSources,
});
export const haveSameVaultAgentSnapshot = (
left: VaultAgentSnapshot,
right: VaultAgentSnapshot,
): boolean => (Object.keys(left) as Array<keyof VaultAgentSnapshot>)
.every((key) => left[key] === right[key]);
export function resolveVaultAgentEffectiveHost(
host: Host,
groupConfigs: GroupConfig[],
proxyProfiles: ProxyProfile[],
): Host {
const validProxyProfileIds = new Set(proxyProfiles.map((profile) => profile.id));
const withGroupDefaults = host.group
? applyGroupDefaults(
host,
resolveGroupDefaults(host.group, groupConfigs, { validProxyProfileIds }),
{ validProxyProfileIds },
)
: applyGroupDefaults(host, {}, { validProxyProfileIds });
return materializeHostProxyProfile(withGroupDefaults, proxyProfiles);
}
export function useVaultAgentBridge(input: UseVaultAgentBridgeInput): void {
const inputRef = useRef(input);
inputRef.current = input;
const selectedSnapshot = selectVaultAgentSnapshot(input);
const vaultSnapshotRef = useRef<VaultAgentSnapshot>(selectedSnapshot);
const lastSyncedVaultInputRef = useRef<VaultAgentSnapshot>(selectedSnapshot);
if (!haveSameVaultAgentSnapshot(selectedSnapshot, lastSyncedVaultInputRef.current)) {
vaultSnapshotRef.current = selectedSnapshot;
lastSyncedVaultInputRef.current = selectedSnapshot;
}
useEffect(() => {
registerVaultAgentHandler(async (op, params) => {
const current = inputRef.current;
return handleVaultAgentOp(op, params, {
getHosts: () => vaultSnapshotRef.current.hosts,
getNotes: () => vaultSnapshotRef.current.notes,
getCustomGroups: () => vaultSnapshotRef.current.customGroups,
getGroupConfigs: () => vaultSnapshotRef.current.groupConfigs,
getPortForwardingRules: () => vaultSnapshotRef.current.portForwardingRules,
getManagedSources: () => vaultSnapshotRef.current.managedSources,
snippets: vaultSnapshotRef.current.snippets,
keys: vaultSnapshotRef.current.keys,
identities: current.identities,
knownHosts: current.knownHosts,
proxyProfiles: current.proxyProfiles,
terminalSettings: current.terminalSettings,
resolveEffectiveHost: (host) => resolveVaultAgentEffectiveHost(
host,
vaultSnapshotRef.current.groupConfigs,
current.proxyProfiles,
),
updateHostNotes: (hostId, notes) => {
const nextHosts = vaultSnapshotRef.current.hosts.map((host) => (
host.id === hostId ? { ...host, notes } : host
));
vaultSnapshotRef.current.hosts = nextHosts;
current.updateHosts(nextHosts);
},
updateCustomGroups: (groups) => {
vaultSnapshotRef.current.customGroups = groups;
current.updateCustomGroups(groups);
},
updateGroupConfigs: (configs) => {
vaultSnapshotRef.current.groupConfigs = configs;
current.updateGroupConfigs(configs);
},
updatePortForwardingRules: (rules) => {
vaultSnapshotRef.current.portForwardingRules = rules;
current.updatePortForwardingRules(rules);
},
updateManagedSources: (sources) => {
vaultSnapshotRef.current.managedSources = sources;
current.updateManagedSources(sources);
},
updateHosts: (hosts) => {
vaultSnapshotRef.current.hosts = hosts;
current.updateHosts(hosts);
},
saveKeyPassphrase: (keyPath, passphrase) => rememberKeyPassphrase({
keyPath,
passphrase,
keys: vaultSnapshotRef.current.keys,
updateKeys: current.updateKeys,
setCurrentKeys: (keys) => {
vaultSnapshotRef.current.keys = keys;
},
}),
removeKeyPassphrases: async (keyPaths) => {
const aliases = await removeDefaultKeyPassphraseAliases(keyPaths);
const currentKeys = vaultSnapshotRef.current.keys;
const updatedKeys = clearReferenceKeyPassphrases(currentKeys, aliases);
if (updatedKeys !== currentKeys) {
vaultSnapshotRef.current.keys = updatedKeys;
await current.updateKeys(updatedKeys);
}
},
updateNotes: (notes) => {
vaultSnapshotRef.current.notes = notes;
current.updateNotes(notes);
},
updateSnippets: (nextSnippets) => {
vaultSnapshotRef.current.snippets = nextSnippets;
current.updateSnippets(nextSnippets);
},
startTunnel: current.startTunnel,
stopTunnel: current.stopTunnel,
stopRuleTunnels: current.stopRuleTunnels,
openHost: current.openHost
? (host, isExternalMcpCall) => current.openHost!(host, isExternalMcpCall)
: undefined,
closeSession: current.closeSession
? (sessionId) => current.closeSession!(sessionId)
: undefined,
});
});
return setupVaultAgentBridge();
}, []);
}