@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
76 lines • 2.38 kB
JavaScript
const HOOK_STATE_VERSION = 1;
export function emptyHookTrustState() {
return { version: HOOK_STATE_VERSION, hooks: {} };
}
export function parseHookTrustStateJson(input) {
if (input === undefined || input.trim() === "") {
return undefined;
}
try {
return parseHookTrustState(JSON.parse(input));
}
catch (error) {
if (error instanceof SyntaxError) {
return undefined;
}
throw error;
}
}
export function readHookTrustStateJson(input) {
return parseHookTrustStateJson(input) ?? emptyHookTrustState();
}
function parseHookTrustState(input) {
if (!isRecord(input) || input.version !== HOOK_STATE_VERSION || !isRecord(input.hooks)) {
return undefined;
}
const hooks = {};
for (const [id, entry] of Object.entries(input.hooks)) {
const parsed = parseHookTrustEntry(entry);
if (parsed !== undefined) {
hooks[id] = parsed;
}
}
return { version: HOOK_STATE_VERSION, hooks };
}
function parseHookTrustEntry(input) {
if (!isRecord(input)) {
return undefined;
}
const enabled = input.enabled;
const trustedHash = input.trustedHash;
const scope = input.scope;
const sourcePath = input.sourcePath;
const matcher = input.matcher;
const commandPreview = input.commandPreview;
const updatedAt = input.updatedAt;
if (typeof enabled !== "boolean" ||
(trustedHash !== undefined && typeof trustedHash !== "string") ||
!isHookSourceScope(scope) ||
typeof sourcePath !== "string" ||
(matcher !== undefined && typeof matcher !== "string") ||
typeof commandPreview !== "string" ||
typeof updatedAt !== "string") {
return undefined;
}
return {
enabled,
...(trustedHash === undefined ? {} : { trustedHash }),
scope,
sourcePath,
...(matcher === undefined ? {} : { matcher }),
commandPreview,
updatedAt,
};
}
function isRecord(value) {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
function isHookSourceScope(value) {
return (value === "global" ||
value === "project" ||
value === "plugin" ||
value === "runtime" ||
value === "cli" ||
value === "managed");
}
//# sourceMappingURL=trust-state-json.js.map