Files
openclaw/src/gateway/server-methods/agent-wait.ts
Peter Steinberger 84a4ff30d5 refactor(gateway): centralize parameter validation and shared helpers (#114353)
* refactor(gateway): centralize parameter validation

* refactor(outbound): centralize runtime scaffolding stripping

* refactor(usage): remove session cost forwarding layers

* refactor(gateway): share attachment failure logging

* refactor(gateway): centralize startup tracing helpers

* fix(usage): keep refresh result type internal

* fix(outbound): preserve visible whitespace around runtime context
2026-07-27 05:34:12 -04:00

52 lines
1.8 KiB
TypeScript

import { validateAgentWaitParams } from "../../../packages/gateway-protocol/src/index.js";
import { waitForAgentJob } from "./agent-job.js";
import type { GatewayRequestHandlers } from "./types.js";
import { assertValidParams } from "./validation.js";
export const agentWaitHandler: GatewayRequestHandlers["agent.wait"] = async ({
params,
respond,
context,
}) => {
if (!assertValidParams(params, validateAgentWaitParams, "agent.wait", respond)) {
return;
}
const runId = (params.runId ?? "").trim();
const timeoutMs =
typeof params.timeoutMs === "number" && Number.isFinite(params.timeoutMs)
? Math.max(0, Math.floor(params.timeoutMs))
: 30_000;
// `hasActiveChatRun` must exclude agent-kind abort entries so wait snapshot
// preference continues to distinguish chat.send from agent RPC runs.
const activeChatEntry = context.chatAbortControllers.get(runId);
const hasActiveChatRun = activeChatEntry !== undefined && activeChatEntry.kind !== "agent";
const snapshot = await waitForAgentJob({
runId,
timeoutMs,
...(hasActiveChatRun ? { source: "chat" } : {}),
});
if (!snapshot) {
const activeRunRegistered = activeChatEntry !== undefined;
respond(true, {
runId,
status: "timeout",
timeoutPhase: activeRunRegistered ? "gateway_draining" : "queue",
...(activeRunRegistered ? {} : { providerStarted: false }),
});
return;
}
respond(true, {
runId,
status: snapshot.status,
startedAt: snapshot.startedAt,
endedAt: snapshot.endedAt,
error: snapshot.error,
stopReason: snapshot.stopReason,
livenessState: snapshot.livenessState,
yielded: snapshot.yielded,
pendingError: snapshot.pendingError,
timeoutPhase: snapshot.timeoutPhase,
providerStarted: snapshot.providerStarted,
});
};