@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
71 lines • 3.07 kB
JavaScript
const FORWARDABLE_CLASSES = new Set([
"observe",
"drive",
"answer",
"thread",
"device_admin",
]);
const MAX_PRINCIPAL_FIELD = 256;
const MAX_CONN_FIELD = 64;
/** The connection roles a host-mode declaration may carry (omo-relay-daemon#307). */
const DECLARABLE_ROLES = new Set(["interactive_user", "task_parent", "peer_agent"]);
export function parseDeclaration(record) {
const principal = Reflect.get(record, "principal");
if (typeof principal !== "object" || principal === null || Array.isArray(principal))
return "principal must be an object";
const deviceId = Reflect.get(principal, "deviceId");
const deviceName = Reflect.get(principal, "deviceName");
const conn = Reflect.get(principal, "conn");
if (!boundedString(deviceId, MAX_PRINCIPAL_FIELD))
return "principal.deviceId must be 1-256 characters";
if (deviceName !== undefined && !boundedString(deviceName, MAX_PRINCIPAL_FIELD))
return "principal.deviceName must be 1-256 characters";
if (conn !== undefined && !boundedString(conn, MAX_CONN_FIELD))
return "principal.conn must be 1-64 characters";
const sameAccount = Reflect.get(principal, "sameAccount");
if (sameAccount !== undefined && sameAccount !== true)
return "principal.sameAccount must be true when present";
const granted = Reflect.get(record, "granted");
if (!Array.isArray(granted))
return "granted must be an array of method classes";
const classes = new Set();
for (const entry of granted) {
if (typeof entry !== "string" || !FORWARDABLE_CLASSES.has(entry) || classes.has(entry))
return "granted holds each of observe, drive, answer, thread, device_admin at most once";
classes.add(entry);
}
const roles = parseRoles(Reflect.get(record, "roles"));
if (typeof roles === "string")
return roles;
return {
...(roles === undefined ? {} : { roles }),
principal: {
deviceId: deviceId,
...(deviceName === undefined ? {} : { deviceName: deviceName }),
...(conn === undefined ? {} : { conn: conn }),
...(sameAccount === true ? { sameAccount: true } : {}),
},
granted: classes,
};
}
export function samePrincipal(a, b) {
return (a.deviceId === b.deviceId && a.deviceName === b.deviceName && a.conn === b.conn && a.sameAccount === b.sameAccount);
}
function parseRoles(value) {
if (value === undefined)
return undefined;
const invalid = "roles holds each of interactive_user, task_parent, peer_agent at most once";
if (!Array.isArray(value))
return invalid;
const roles = new Set();
for (const entry of value) {
if (typeof entry !== "string" || !DECLARABLE_ROLES.has(entry) || roles.has(entry))
return invalid;
roles.add(entry);
}
return roles;
}
function boundedString(value, max) {
return typeof value === "string" && value.length >= 1 && value.length <= max;
}
//# sourceMappingURL=delegation-declaration.js.map