@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
48 lines • 2.44 kB
JavaScript
import { BatchStoreMissingError, } from "@earendil-works/pi-agent-core";
import { createCursorExecBridge } from "./cursor-exec-bridge.js";
/**
* Build the exec handlers for ONE Cursor run.
*
* `runSignal` is the signal of the run that owns this stream, captured when
* the loop opens it. Resolving ownership from the agent's live signal instead
* would let a straggler frame from a stream whose run already ended (a
* provider error or rate-limit fallback restarts the run while its h2 stream
* still holds buffered exec frames) adopt the replacement run's signal, clear
* the ownership guard in `Agent.emitExternalEvent`, and execute a dead run's
* tool inside the new run.
*/
export function createSessionCursorExecBridge(sessionRef, getAgent, runSignal) {
return createCursorExecBridge({
getTool: (name) => sessionRef.current?.getRegisteredTool(name),
preflightToolCall: async (event) => sessionRef.current?.preflightToolCall({
type: "toolCall",
id: event.toolCallId,
name: event.toolName,
// event.input is the validated form of the bridge's JSON exec-frame args; the extension
// ToolCallEvent API types it Record<string, unknown>.
arguments: event.input,
}, event.input, { execBridge: true }),
emitEvent: async (event, runSignal) => await getAgent().emitExternalEvent(event, runSignal),
settleToolCall: (toolCallId) => sessionRef.current?.releaseExecBridgeToolCall(toolCallId),
emitToolResult: async ({ toolName, toolCallId, args, result, isError }) => {
await sessionRef.current?.emitExecBridgeToolResult(toolName, toolCallId, args, result, isError);
},
getAbortSignal: () => {
if (runSignal === undefined || runSignal.aborted)
return undefined;
return runSignal === getAgent().signal ? runSignal : undefined;
},
openBatchStore: () => {
const open = getAgent().batchStoreFactory;
if (open === undefined)
throw new BatchStoreMissingError();
return open();
},
getExecutionScope: () => {
const agent = getAgent();
return agent.getExecutionScope?.() ?? agent.executionScope;
},
recordWitness: (witness) => getAgent().onExecutionPipeline?.(witness),
});
}
//# sourceMappingURL=cursor-exec-bridge-session.js.map