UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

49 lines • 2.51 kB
import { permissionRequestsFor } from "./parsers.js"; /** Use the permission extension's live parser, evaluator and approval handler. */ export function createDispatchAuthorizer(pi, stateFor, authorize) { return { policy(request, ctx) { const state = stateFor(ctx); if (state.setupError !== null) throw new Error(`Permission setup failed: ${state.setupError}`); const { service, parsers } = state; if (!service || !parsers) throw new Error("Permission authorizer is not initialized"); const requests = permissionRequestsFor(() => pi.getAllTools(), parsers, request.toolName, request.input, ctx.cwd); // The same session key the tool_call check gives its requests, so only this session's answers count. const sessionID = ctx.sessionManager === undefined ? "" : ctx.sessionManager.getSessionId(); let action = "allow"; const decisions = requests.map((permission) => { const rules = permission.patterns.map((pattern) => service.dispatchDecision(permission.permission, pattern, { presetBound: state.preset === "auto", ruleAliases: permission.ruleAliases, originalPaths: permission.originalPaths, }, sessionID)); for (const decision of rules) { if (decision.action === "deny") action = "deny"; else if (decision.action === "ask" && !permission.autoApproveAsk && action !== "deny") action = "ask"; } return { permission, rules }; }); return { action, fingerprint: JSON.stringify([sessionID, ctx.cwd, state.preset === "auto", decisions]), }; }, async ask(request, ctx, signal) { signal?.throwIfAborted(); const result = await authorize({ type: "tool_call", toolCallId: request.toolCallId, toolName: request.toolName, input: request.input, ...(request.parentToolCallId === undefined ? {} : { parentToolCallId: request.parentToolCallId }), }, ctx, signal); if (result?.block) throw new Error(result.reason ?? "Permission request was rejected."); }, }; } //# sourceMappingURL=dispatch-policy.js.map