@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
133 lines • 6.54 kB
JavaScript
import { parsePermissionPresetName } from "./extensions/builtin/permission-system/cli.js";
import { readPermissionPreset } from "./extensions/builtin/permission-system/preset-registry.js";
import { resolvePermissionPreset } from "./extensions/builtin/permission-system/settings.js";
import { bindDecisionPreset } from "./permission-decision.js";
const PERMISSION_PRESET_FLAG = "permission-preset";
const PERMISSION_PRESET_CHANGED_CHANNEL = "permission_preset_changed";
const PERMISSION_MODE_ENTRY = "permission_mode";
const clientPresets = new WeakMap();
// Sessions whose `permission-preset` flag holds the host's own default (an embedder's host option), not an operator flag.
const hostDefaultFlagSessions = new WeakSet();
export function markHostDefaultPresetFlag(session, profile) {
if (profile?.permissionPresetSource === "host_default")
hostDefaultFlagSessions.add(session);
else
hostDefaultFlagSessions.delete(session);
}
function markClientPermissionPreset(session, preset) {
if (preset === undefined)
clientPresets.delete(session);
else
clientPresets.set(session, preset);
}
export function permissionModeOf(session) {
const named = clientPresets.get(session);
const namedPreset = named === undefined ? undefined : parsePermissionPresetName(named);
if (namedPreset !== undefined)
return clientMode(namedPreset, readPermissionPreset(session.sessionManager));
const hostFlag = named === undefined ? session.extensionRunner.getFlagValues().get(PERMISSION_PRESET_FLAG) : named;
const hostPreset = typeof hostFlag === "string" ? parsePermissionPresetName(hostFlag) : undefined;
if (typeof hostFlag === "string" && hostPreset === undefined)
return invalidMode(hostFlag);
let preset;
try {
preset = resolvePermissionPreset(session.settingsManager, hostPreset);
}
catch {
return invalidMode(typeof hostFlag === "string" ? hostFlag : "");
}
return {
permissionPreset: preset,
permissionPresetSource: "host_default",
permissionPresetStatus: "applied",
effectivePermissionPreset: readPermissionPreset(session.sessionManager) ?? preset,
};
}
// The live permission system is the authority on what it enforces; the named preset stands in only while no
// permission system has registered for the session yet (it loads with the session's extensions).
function clientMode(preset, enforced) {
return {
permissionPreset: preset,
permissionPresetSource: "client",
permissionPresetStatus: "applied",
effectivePermissionPreset: enforced ?? preset,
};
}
// An invalid preset never counts as one a client set: rule 1 of the delegated-default design must never select an
// unusable preset, so the source stays the host's while every gated call is refused (#2617).
function invalidMode(preset) {
return {
permissionPreset: preset,
permissionPresetSource: "host_default",
permissionPresetStatus: "invalid",
effectivePermissionPreset: null,
};
}
/** The preset a launch profile names as a client's choice; an embedder's host default is not one. */
export function clientNamedPreset(profile) {
return profile?.permissionPresetSource === "host_default" ? undefined : profile?.permissionPreset;
}
/** A profile whose preset a client just named: the host-default marker no longer applies. */
export function withClientPreset(profile, preset) {
const { permissionPresetSource: _hostDefault, ...rest } = profile;
return Object.freeze({ ...rest, permissionPreset: preset });
}
export function permissionModeReply(mode) {
return {
permissionPreset: mode.permissionPreset,
permissionPresetSource: mode.permissionPresetSource,
permissionPresetStatus: mode.permissionPresetStatus,
};
}
function persistedClientPreset(sessionManager) {
const entries = sessionManager.getEntries();
for (let index = entries.length - 1; index >= 0; index--) {
const entry = entries[index];
if (entry?.type !== "custom" || entry.customType !== PERMISSION_MODE_ENTRY)
continue;
const data = entry.data;
if (typeof data !== "object" || data === null || !("preset" in data))
return undefined;
return typeof data.preset === "string" && parsePermissionPresetName(data.preset) ? data.preset : undefined;
}
return undefined;
}
export function openReplyPermissionMode(state) {
const { permissionPreset, permissionPresetSource, permissionPresetStatus } = state;
if (permissionPreset === undefined || permissionPresetSource === undefined || permissionPresetStatus === undefined)
return {};
return { permissionPreset, permissionPresetSource, permissionPresetStatus };
}
/**
* Binds the preset a client named to the live session: reported as `client`, persisted to its file, and, for a change
* to a running session (`announce`), published on the session's `permission_preset_changed` bus channel.
*/
/** Binds the per-decision approval-mode rule to a session, wherever it was created (runtime, SDK, in-process child). */
export function bindPermissionDecision(session) {
bindDecisionPreset({
sessionManager: session.sessionManager,
namedPreset: () => clientPresets.get(session),
launchFlagNamed: () => !hostDefaultFlagSessions.has(session) &&
typeof session.extensionRunner.getFlagValues().get(PERMISSION_PRESET_FLAG) === "string",
recordedPreset: () => persistedClientPreset(session.sessionManager),
});
}
export function applyClientPermissionMode(session, preset, options) {
markClientPermissionPreset(session, preset);
persistClientPreset(session.sessionManager, preset);
bindPermissionDecision(session);
if (options.announce)
session.extensionRunner.emitBusEvent(PERMISSION_PRESET_CHANGED_CHANNEL, permissionModeOf(session));
}
/**
* Records a valid client-named preset as a `permission_mode` custom entry (bookkeeping only: nothing restores it
* on a later open). Writes only on a change; an invalid preset is never recorded.
*/
function persistClientPreset(sessionManager, preset) {
if (preset === undefined || parsePermissionPresetName(preset) === undefined)
return;
if (persistedClientPreset(sessionManager) === preset)
return;
sessionManager.appendCustomEntry(PERMISSION_MODE_ENTRY, { preset, source: "client" }, { startupBookkeeping: true });
}
//# sourceMappingURL=permission-mode.js.map