import crypto from "node:crypto" ;
import { callGateway } from "../../gateway/call.js" ;
import { INTERNAL_MESSAGE_CHANNEL } from "../../utils/message-channel.js" ;
import { resolveNestedAgentLaneForSession } from "../lanes.js" ;
import { retireSessionMcpRuntimeForSessionKey } from "../pi-bundle-mcp-tools.js" ;
import { waitForAgentRunAndReadUpdatedAssistantReply } from "../run-wait.js" ;
export { readLatestAssistantReply } from "../run-wait.js" ;
type GatewayCaller = typeof callGateway;
const defaultAgentStepDeps = {
callGateway,
};
let agentStepDeps: {
callGateway: GatewayCaller;
} = defaultAgentStepDeps;
export async function runAgentStep(params: {
sessionKey: string;
message: string;
extraSystemPrompt: string;
timeoutMs: number;
channel?: string;
lane?: string;
sourceSessionKey?: string;
sourceChannel?: string;
sourceTool?: string;
}): Promise<string | undefined> {
const stepIdem = crypto.randomUUID();
const response = await agentStepDeps.callGateway({
method: "agent" ,
params: {
message: params.message,
sessionKey: params.sessionKey,
idempotencyKey: stepIdem,
deliver: false ,
channel: params.channel ?? INTERNAL_MESSAGE_CHANNEL,
lane: params.lane ?? resolveNestedAgentLaneForSession(params.sessionKey),
extraSystemPrompt: params.extraSystemPrompt,
inputProvenance: {
kind: "inter_session" ,
sourceSessionKey: params.sourceSessionKey,
sourceChannel: params.sourceChannel,
sourceTool: params.sourceTool ?? "sessions_send" ,
},
},
timeoutMs: 10 _000 ,
});
const stepRunId = typeof response?.runId === "string" && response.runId ? response.runId : "" ;
const resolvedRunId = stepRunId || stepIdem;
const result = await waitForAgentRunAndReadUpdatedAssistantReply({
runId: resolvedRunId,
sessionKey: params.sessionKey,
timeoutMs: Math.min(params.timeoutMs, 60 _000 ),
});
if (result.status === "ok" || result.status === "error" ) {
await retireSessionMcpRuntimeForSessionKey({
sessionKey: params.sessionKey,
reason: "nested-agent-step-complete" ,
});
}
if (result.status !== "ok" ) {
return undefined;
}
return result.replyText;
}
export const __testing = {
setDepsForTest(overrides?: Partial<{ callGateway: GatewayCaller }>) {
agentStepDeps = overrides
? {
...defaultAgentStepDeps,
...overrides,
}
: defaultAgentStepDeps;
},
};
Messung V0.5 in Prozent C=99 H=98 G=98
¤ Dauer der Verarbeitung: 0.11 Sekunden
(vorverarbeitet am 2026-06-10)
¤
*© Formatika GbR, Deutschland