@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
93 lines • 3.89 kB
JavaScript
import { McpMrtrError } from "./modern/errors.js";
const originOption = Symbol("mcp-request-origin");
const origins = new WeakSet();
const aliases = new WeakMap();
export const physicalMcpClient = (client) => aliases.get(client) ?? client;
export function registerMcpClientAlias(alias, physical) {
aliases.set(alias, physical);
}
const legacyOperations = new WeakMap();
export function beginLegacyMcpOperation(client, origin, caller) {
const lifetime = new AbortController();
const operation = {
origin,
signal: AbortSignal.any([origin.signal, lifetime.signal, ...(caller ? [caller] : [])]),
};
let operations = legacyOperations.get(client);
if (!operations) {
operations = new Set();
legacyOperations.set(client, operations);
}
operations.add(operation);
return () => {
operations.delete(operation);
lifetime.abort();
if (operations.size === 0)
legacyOperations.delete(client);
};
}
export function legacyMcpOperation(client) {
const operations = legacyOperations.get(client);
if (operations && operations.size > 1)
return "ambiguous";
if (operations?.size !== 1)
return undefined;
const operation = operations.values().next().value;
return operation && !operation.signal.aborted && operation.origin.isCurrent() ? operation : undefined;
}
// Tool calls of a binding that serves several execution scopes, by physical client (brain/hand S16c). Kept apart
// from the legacy operations above: a default legacy client never routes its calls through the request adapter.
const scopedCalls = new WeakMap();
/** Record a scoped tool call in flight on `client` until the returned function runs; an unscoped origin records none. */
export function beginScopedMcpCall(client, origin, signal) {
if (origin.scope === undefined)
return () => undefined;
const physical = physicalMcpClient(client);
const call = { origin, signal };
let calls = scopedCalls.get(physical);
if (!calls) {
calls = new Set();
scopedCalls.set(physical, calls);
}
calls.add(call);
return () => {
calls.delete(call);
if (calls.size === 0)
scopedCalls.delete(physical);
};
}
/**
* Whose UI a legacy form arriving on `client` belongs to: the one scope whose calls are in flight there, or
* `"refuse"` when calls of different scopes share it. Without scoped calls the caller keeps its own provider.
*/
export function legacyMcpCallerUi(client) {
const calls = [...(scopedCalls.get(physicalMcpClient(client)) ?? [])].filter((call) => call.signal?.aborted !== true && !call.origin.signal.aborted && call.origin.isCurrent());
const scopes = new Set(calls.map((call) => call.origin.scope));
if (scopes.size === 0)
return undefined;
if (scopes.size > 1)
return "refuse";
return { ui: calls[0]?.origin.elicitationUi?.() };
}
/** Issued by the binding-specific registration boundary, never from JSON/model arguments. */
export function createMcpRequestOrigin(origin) {
origins.add(origin);
return origin;
}
export function mcpRequestOptions(origin, options = {}) {
return origin === undefined ? options : { ...options, [originOption]: origin };
}
export function takeMcpRequestOrigin(options, client) {
const supplied = options;
const origin = supplied?.[originOption];
const { [originOption]: _origin, ...sdkOptions } = supplied ?? {};
if (origin !== undefined) {
if (!origins.has(origin) || !origin.isCurrent())
throw new McpMrtrError("MCP_MRTR_CONTEXT_CHANGED");
const candidate = origin.connection.client;
if (physicalMcpClient(candidate) !== client)
throw new McpMrtrError("MCP_MRTR_CONTEXT_CHANGED");
}
return { origin, sdkOptions };
}
//# sourceMappingURL=request-origin.js.map