@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
137 lines • 6.63 kB
JavaScript
export function matchingMirrors(subscriptions, sessionId, recordType, entryView, dialogs = false) {
const candidates = [...subscriptions].filter((subscription) => (!dialogs || subscription.options.dialogs === true) &&
(recordType === "snapshot_marker" || dialogs
? subscription.options.mirror.kind !== "none"
: mirrorsRecord(subscription.options.mirror, recordType)));
if (candidates.length === 0)
return [];
const entry = entryView();
return entry ? candidates.filter((s) => subscriptionMatches(s.options, sessionId, entry)) : [];
}
const MAX_SELECTOR_IDS = 1_024;
const MAX_MIRROR_TYPES = 64;
const RECORD_TYPE_PATTERN = /^[a-z][a-z0-9_]{0,63}$/;
function refusal(reason, action) {
return { refusal: { reason, action } };
}
function isRecord(value) {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
function parseSelector(value) {
if (value === undefined || value === "all")
return { selector: { kind: "all" } };
const action = 'send sessions: "all", { ids: [<sessionId | durableSessionId | sessionPath>] } or { childrenOf: <durableSessionId> }';
if (!isRecord(value))
return refusal('sessions must be "all" or an object', action);
if ("ids" in value && !("childrenOf" in value)) {
const ids = value.ids;
if (!Array.isArray(ids) || ids.length === 0 || ids.length > MAX_SELECTOR_IDS)
return refusal(`sessions.ids must be a non-empty array of at most ${MAX_SELECTOR_IDS} strings`, action);
if (!ids.every((id) => typeof id === "string" && id.length > 0))
return refusal("every sessions.ids entry must be a non-empty string", action);
return { selector: { kind: "ids", ids: new Set(ids) } };
}
if ("childrenOf" in value && !("ids" in value)) {
const parent = value.childrenOf;
if (typeof parent !== "string" || parent.length === 0)
return refusal("sessions.childrenOf must be a non-empty durable session id", action);
return { selector: { kind: "childrenOf", parent } };
}
return refusal("sessions must name exactly one of ids or childrenOf", action);
}
function parseMirror(value) {
if (value === undefined || value === "none")
return { mirror: { kind: "none" } };
if (value === "all")
return { mirror: { kind: "all" } };
const action = 'send mirror: "none", "all" or an array of record types such as ["message_update"]';
if (!Array.isArray(value) || value.length === 0 || value.length > MAX_MIRROR_TYPES)
return refusal(`mirror must be "none", "all" or a non-empty array of at most ${MAX_MIRROR_TYPES} record types`, action);
if (!value.every((type) => typeof type === "string" && RECORD_TYPE_PATTERN.test(type)))
return refusal(`every mirror entry must be a record type matching ${RECORD_TYPE_PATTERN.source}`, action);
return { mirror: { kind: "types", types: new Set(value) } };
}
function optionalBoolean(value, field) {
if (value === undefined || typeof value === "boolean")
return value;
return { reason: `${field} must be a boolean`, action: `send ${field}: true or false, or omit it` };
}
export function parseSubscription(request) {
const selector = parseSelector(request.sessions);
if ("refusal" in selector)
return selector;
const mirror = parseMirror(request.mirror);
if ("refusal" in mirror)
return mirror;
const includeWorkers = optionalBoolean(request.include_workers, "include_workers");
if (typeof includeWorkers === "object")
return { refusal: includeWorkers };
const includeContext = optionalBoolean(request.include_context, "include_context");
if (typeof includeContext === "object")
return { refusal: includeContext };
const dialogs = optionalBoolean(request.dialogs, "dialogs");
if (typeof dialogs === "object")
return { refusal: dialogs };
const effects = optionalBoolean(request.effects, "effects");
if (typeof effects === "object")
return { refusal: effects };
return {
options: {
selector: selector.selector,
dialogs: dialogs ?? false,
// A childrenOf subscription exists to watch machine-driven children, which open as workers.
includeWorkers: includeWorkers ?? selector.selector.kind === "childrenOf",
includeContext: includeContext ?? false,
mirror: mirror.mirror,
// A childrenOf subscription watches machine-driven work, whose truth is the receipts.
effects: effects ?? selector.selector.kind === "childrenOf",
},
};
}
export function subscriptionMatches(options, sessionId, entry) {
if (entry.kind === "worker" && !options.includeWorkers)
return false;
const selector = options.selector;
switch (selector.kind) {
case "all":
return true;
case "ids":
return (selector.ids.has(sessionId) ||
(entry.durableSessionId !== undefined && selector.ids.has(entry.durableSessionId)) ||
(entry.sessionPath !== undefined && selector.ids.has(entry.sessionPath)));
case "childrenOf": {
// Lineage matching reads the frozen open-time context: a set_session_context patch
// can neither move a session into a supervisor's children view nor out of it.
const lineage = entry.openContext ?? entry.context;
const parent = lineage.parent_session_id ?? lineage.root_session_id;
return parent === selector.parent;
}
default: {
const exhaustive = selector;
throw new Error(`unexpected selector ${String(exhaustive)}`);
}
}
}
function mirrorsRecord(mirror, recordType) {
switch (mirror.kind) {
case "none":
return false;
case "all":
return true;
case "types":
return mirror.types.has(recordType);
default: {
const exhaustive = mirror;
throw new Error(`unexpected mirror ${String(exhaustive)}`);
}
}
}
export function matchingStatusConnections(subscriptions, sessionId, entry) {
const matches = new Map();
if (entry)
for (const subscription of subscriptions)
if (subscriptionMatches(subscription.options, sessionId, entry))
matches.set(subscription.connectionId, (matches.get(subscription.connectionId) ?? false) || subscription.options.includeContext);
return matches;
}
//# sourceMappingURL=session-status-subscriptions.js.map