2025-12-07 19:32:53 +02:00
|
|
|
import type { EditPermissionMode } from "../types/sessionTypes";
|
|
|
|
|
|
|
|
|
|
const EDIT_PERMISSION_TOOL_NAMES = new Set([
|
|
|
|
|
'edit',
|
|
|
|
|
'multiedit',
|
|
|
|
|
'str_replace',
|
|
|
|
|
'str_replace_based_edit_tool',
|
|
|
|
|
'write',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
export const isEditPermissionType = (type?: string | null): boolean => {
|
|
|
|
|
if (!type) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return EDIT_PERMISSION_TOOL_NAMES.has(type.toLowerCase());
|
|
|
|
|
};
|
|
|
|
|
|
2026-01-06 21:31:04 +02:00
|
|
|
type PermissionAction = 'allow' | 'deny' | 'ask';
|
|
|
|
|
|
|
|
|
|
type PermissionRule = {
|
|
|
|
|
permission: string;
|
|
|
|
|
pattern: string;
|
|
|
|
|
action: PermissionAction;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type ConfigStoreAgent = {
|
|
|
|
|
name: string;
|
|
|
|
|
permission?: PermissionRule[];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type ConfigStoreState = {
|
|
|
|
|
agents?: ConfigStoreAgent[];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type ConfigStoreRef = { getState?: () => ConfigStoreState };
|
|
|
|
|
|
|
|
|
|
const resolveConfigStore = (): ConfigStoreRef | undefined => {
|
2025-12-07 19:32:53 +02:00
|
|
|
if (typeof window === 'undefined') {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
2026-01-06 21:31:04 +02:00
|
|
|
return (window as { __zustand_config_store__?: ConfigStoreRef }).__zustand_config_store__;
|
2025-12-07 19:32:53 +02:00
|
|
|
};
|
|
|
|
|
|
2026-01-06 21:31:04 +02:00
|
|
|
const getAgentDefinition = (agentName?: string): ConfigStoreAgent | undefined => {
|
2025-12-07 19:32:53 +02:00
|
|
|
if (!agentName) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const configStore = resolveConfigStore();
|
|
|
|
|
if (configStore?.getState) {
|
|
|
|
|
const state = configStore.getState();
|
2026-01-06 21:31:04 +02:00
|
|
|
return state.agents?.find?.((agent) => agent.name === agentName);
|
2025-12-07 19:32:53 +02:00
|
|
|
}
|
2026-01-06 21:31:04 +02:00
|
|
|
} catch {
|
|
|
|
|
/* ignored */
|
|
|
|
|
}
|
2025-12-07 19:32:53 +02:00
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
|
};
|
|
|
|
|
|
2026-01-06 21:31:04 +02:00
|
|
|
const resolvePermissionAction = (ruleset: PermissionRule[] | undefined, permission: string): PermissionAction => {
|
|
|
|
|
if (!ruleset || ruleset.length === 0) {
|
|
|
|
|
return 'ask';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Prefer explicit rule for the tool at wildcard pattern.
|
|
|
|
|
for (let index = ruleset.length - 1; index >= 0; index -= 1) {
|
|
|
|
|
const rule = ruleset[index];
|
|
|
|
|
if (rule.permission === permission && rule.pattern === '*') {
|
|
|
|
|
return rule.action;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fall back to global wildcard.
|
|
|
|
|
for (let index = ruleset.length - 1; index >= 0; index -= 1) {
|
|
|
|
|
const rule = ruleset[index];
|
|
|
|
|
if (rule.permission === '*' && rule.pattern === '*') {
|
|
|
|
|
return rule.action;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 'ask';
|
|
|
|
|
};
|
|
|
|
|
|
2025-12-07 19:32:53 +02:00
|
|
|
export const getAgentDefaultEditPermission = (agentName?: string): EditPermissionMode => {
|
|
|
|
|
const agent = getAgentDefinition(agentName);
|
|
|
|
|
if (!agent) {
|
|
|
|
|
return 'ask';
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-06 21:31:04 +02:00
|
|
|
const action = resolvePermissionAction(agent.permission, 'edit');
|
|
|
|
|
return action;
|
2025-12-07 19:32:53 +02:00
|
|
|
};
|