@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
98 lines • 3.78 kB
JavaScript
import { randomUUID } from "node:crypto";
import { presentation } from "./mcp-presenter-forms.js";
import { holders, revoke, } from "./mcp-presenter-grants.js";
export function dispose(state) {
for (const binding of [...state.bindings.values()])
binding.dispose();
state.connections.clear();
state.registrations.clear();
}
export function connect(state, id, send) {
const connection = { id, send, epoch: randomUUID(), nonce: randomUUID(), attachments: new Map() };
state.connections.set(connection, connection);
return connection;
}
export function attach(state, sender, handle) {
const connection = state.connections.get(sender);
const binding = [...state.bindings.values()].find((value) => value === handle);
if (connection && binding && !connection.attachments.has(binding))
connection.attachments.set(binding, { id: randomUUID(), epoch: randomUUID() });
}
export function connectionContext(state, sender, sessionId) {
const connection = state.connections.get(sender);
const binding = state.bindings.get(sessionId);
const attachment = binding && connection?.attachments.get(binding);
if (!connection || !binding || !attachment)
return undefined;
return {
hostInstance: state.instance,
connectionNonce: connection.nonce,
bindingEpoch: binding.epoch,
attachmentId: attachment.id,
attachmentEpoch: attachment.epoch,
};
}
export function detach(state, sender, handle) {
const connection = state.connections.get(sender);
if (!connection)
return;
for (const binding of connection.attachments.keys()) {
if (handle !== undefined && handle !== binding)
continue;
connection.attachments.delete(binding);
revokeHeld(state, binding, connection);
}
if (handle === undefined)
state.connections.delete(sender);
}
export function withdraw(state, sender) {
const connection = state.connections.get(sender);
if (!connection)
return;
for (const binding of connection.attachments.keys())
revokeHeld(state, binding, connection);
}
function revokeHeld(state, binding, connection) {
for (const grant of [...binding.grants])
if (grant.registration.connection === connection)
revoke(state, grant.registration);
}
export function bind(state, sessionId, durableId, admit) {
state.bindings.get(sessionId)?.dispose();
const lifetime = new AbortController();
const binding = {
sessionId,
durableId,
admit,
epoch: randomUUID(),
lifetime,
signal: lifetime.signal,
listeners: new Set(),
grants: new Set(),
queue: {},
version: () => (holders(state, binding).length > 0 ? binding.presence?.id : undefined),
subscribe: (listener) => {
binding.listeners.add(listener);
return () => {
binding.listeners.delete(listener);
};
},
capture: () => {
const presence = binding.presence;
return presence && holders(state, binding).length > 0 ? presentation(state, binding, presence) : undefined;
},
dispose: () => {
binding.lifetime.abort();
for (const grant of [...binding.grants])
revoke(state, grant.registration, true);
if (state.bindings.get(sessionId) === binding)
state.bindings.delete(sessionId);
for (const connection of state.connections.values())
connection.attachments.delete(binding);
binding.listeners.clear();
},
};
state.bindings.set(sessionId, binding);
return binding;
}
//# sourceMappingURL=mcp-presenter-bindings.js.map