@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
122 lines • 4.39 kB
JavaScript
import { randomBytes, timingSafeEqual } from "node:crypto";
import { validMcpFormContent } from "../../core/extensions/builtin/mcp/modern/form-schema.js";
export const mcpFormClock = {
now: () => performance.now(),
schedule: (callback, delay) => setTimeout(callback, delay),
cancel: (timer) => {
if ("close" in timer && typeof timer.close === "function")
timer.close();
},
};
/** One holder's private view of a group. Lifecycle closures retain it even after its grant is removed. */
export class McpFormDialog {
constructor(group, holder) {
this.delivered = false;
this.presentedHere = false;
this.token = randomBytes(32);
this.group = group;
this.holder = holder;
this.request = {
...holder.envelope,
type: "mcp_form_request",
dialogToken: this.token.toString("hex"),
server: structuredClone(group.input.server),
message: group.input.message,
schema: structuredClone(group.input.schema),
deadlineAtMs: Date.now() + Math.max(0, group.input.deadlineMs),
};
}
deliver() {
const request = this.request;
if (!request)
return;
// Marked first: a presenter may answer synchronously inside `send` and must still see its resolution.
this.delivered = true;
if (!this.holder.current() || !this.holder.send(request))
this.revoked();
}
sync() {
if (!this.live())
return undefined;
return this.request === undefined ? undefined : structuredClone(this.request);
}
matches(token, revision) {
return (this.request?.formRevision === revision &&
/^[a-f0-9]{64}$/.test(token) &&
timingSafeEqual(this.token, Buffer.from(token, "hex")));
}
presented() {
if (!this.live())
return { error: "form_expired" };
this.presentedHere = true;
this.group.markPresented();
return { ok: true };
}
respond(action, content) {
if (!this.live())
return { error: "form_expired" };
if (!this.presentedHere || !this.request)
return { error: "invalid_form_response" };
let result;
switch (action) {
case "accept":
if (!validMcpFormContent(this.request.schema, content) ||
Buffer.byteLength(JSON.stringify(content)) > 1024 * 1024)
return { error: "invalid_form_response" };
result = { action, content: structuredClone(content) };
break;
case "decline":
case "cancel":
if (content !== undefined)
return { error: "invalid_form_response" };
result = { action };
break;
default:
return { error: "invalid_form_response" };
}
if (!this.live())
return { error: "form_expired" };
this.group.settle({ kind: "result", result, source: "presenter_action", humanAdmission: true }, this);
return { ok: true };
}
/** Internal-only: holder loss ends this dialog alone; a binding teardown aborts the whole group. */
revoked(operation = false) {
if (operation) {
this.group.terminate("operation_aborted");
return;
}
this.close();
this.group.leave(this);
}
close() {
this.request = undefined;
this.token.fill(0);
this.holder.remove();
}
resolved(reason, elsewhere) {
if (!this.delivered || !this.holder.current())
return;
const { sessionId, grantId, dialogId } = this.holder.envelope;
this.holder.send({
type: "mcp_form_resolved",
sessionId,
grantId,
dialogId,
reason,
...(elsewhere && this.holder.negotiated ? { elsewhere: true } : {}),
});
}
settled(record) {
if (this.delivered && this.holder.negotiated && this.holder.current())
this.holder.send(record);
}
live() {
if (!this.group.live())
return false;
if (this.holder.current())
return true;
this.revoked();
return false;
}
}
//# sourceMappingURL=mcp-form-dialog.js.map