@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
98 lines • 4.63 kB
JavaScript
/**
* The hand's hook advertisement of hosted model-key custody (row CK21; custody design 4.9, round-10 fold section 15).
*
* A hand tells a hosted brain, per bound session, which providers' calls need the device's hooks: the request hooks
* (`before_provider_request`) and the stream hooks (`provider_stream_event`, `after_provider_response`) of the user's
* extensions, each scoped by its `{ providers }` declaration or, undeclared, every provider (`"*"`). The advertisement
* carries the `connectionId`, the session's `cwd` and its `advertHash`, and it is the first message for its session on
* every (re)connect: the link serves no `hook.model_request` that its session's advertisement on that connection did
* not precede.
*/
import { createHash } from "node:crypto";
import { canonicalJson } from "@earendil-works/pi-agent-core";
const REQUEST_HOOK_EVENTS = ["before_provider_request"];
const STREAM_HOOK_EVENTS = ["provider_stream_event", "after_provider_response"];
function hookProviders(extensions, events, only) {
const declared = new Set();
for (const extension of extensions) {
if (!only(extension.path))
continue;
for (const event of events)
for (const handler of extension.handlers.get(event) ?? []) {
const providers = extension.handlerProviders?.get(handler);
if (providers === undefined)
return "*";
for (const provider of providers)
declared.add(provider);
}
}
return [...declared].sort();
}
/** The request and stream hooks of the extensions `only` accepts (the ones that run on this hand). */
export function handHookAccess(extensions, only) {
return {
requestHooks: hookProviders(extensions, REQUEST_HOOK_EVENTS, only),
streamHooks: hookProviders(extensions, STREAM_HOOK_EVENTS, only),
};
}
/** `H(extension set hash, cwd, project-local hook set)` (4.9): `sha256:` + hex SHA-256 of their canonical JSON. */
export function computeAdvertHash(parts) {
const canonical = canonicalJson({
cwd: parts.cwd,
extensionSetHash: parts.extensionSetHash,
projectHooks: [...parts.projectHooks].sort(),
});
return `sha256:${createHash("sha256").update(canonical, "utf8").digest("hex")}`;
}
/** A hook request for a session not advertised on the connection it names; nothing ran. */
export class HookNotAdvertisedError extends Error {
constructor() {
super(...arguments);
this.name = "HookNotAdvertisedError";
}
}
export function createHandHookLink(options) {
let connectionId;
let advertised = new Set();
// One outgoing order: an advertisement queued before a reply is sent before it.
let outgoing = Promise.resolve();
const enqueue = (work) => {
const run = outgoing.then(work);
outgoing = run.then(() => undefined, () => undefined);
return run;
};
const sendAdvert = (connection, session, into) => enqueue(async () => {
if (connectionId !== connection)
return;
await options.send({ t: "hand.session.advertise", connectionId: connection, ...session });
into.add(session.sessionId);
});
return {
async connect(next, sessions) {
connectionId = next;
const fresh = new Set();
advertised = fresh;
await Promise.all(sessions.map((session) => sendAdvert(next, session, fresh)));
},
async advertise(session) {
if (connectionId === undefined)
throw new HookNotAdvertisedError("no connection to advertise on");
await sendAdvert(connectionId, session, advertised);
},
async receive(connection, request) {
// Only the advertised check and the reply's send take a place in the outgoing order.
await enqueue(async () => {
if (connection !== connectionId || !advertised.has(request.sessionId))
throw new HookNotAdvertisedError(`hook.model_request for session ${JSON.stringify(request.sessionId)} was not preceded by its advertisement on connection ${JSON.stringify(connection)}`);
});
const reply = await options.serve(request);
await enqueue(async () => {
// A reconnect since the request arrived: its connection is gone, so its reply is not sent.
if (connection !== connectionId)
return;
await options.send(reply);
});
},
};
}
//# sourceMappingURL=hook-advertise.js.map