openclaw / src /gateway /server-methods /agent-wait.ts
SaylorTwift's picture
SaylorTwift HF Staff
Add files using upload-large-folder tool
78d0c37 verified
Raw History Blame Contribute Delete
1.78 kB
import {
ErrorCodes,
errorShape,
validateAgentWaitParams,
type AgentWaitParams,
} from "../../../packages/gateway-protocol/src/index.js";
import { getAgentRunContext } from "../../infra/agent-run-registry.js";
import { createAgentTurnService } from "../agent-turn/agent-turn-service.js";
import { operatorSessionCap } from "../operator-role-policy.js";
import {
createSessionListEntryFilter,
isGatewayAdmin,
resolveSessionSharingTarget,
} from "../session-sharing.js";
import type { GatewayRequestHandlers } from "./types.js";
import { assertValidParams } from "./validation.js";
export const agentWaitHandler: GatewayRequestHandlers["agent.wait"] = async ({
params,
respond,
context,
client,
isWebchatConnect,
}) => {
if (!assertValidParams(params, validateAgentWaitParams, "agent.wait", respond)) {
return;
}
const gatewayClient = client ?? null;
if (gatewayClient?.authenticatedUserProfile && !isGatewayAdmin(gatewayClient)) {
const cfg = context.getRuntimeConfig();
if (operatorSessionCap(gatewayClient, cfg) === "none") {
const run = getAgentRunContext(params.runId);
const target = run?.sessionKey
? resolveSessionSharingTarget({ cfg, sessionKey: run.sessionKey, agentId: run.agentId })
: null;
const visibilityFilter = createSessionListEntryFilter({ client: gatewayClient, cfg });
if (!target || visibilityFilter?.(target.storeKey, target.entry) === false) {
respond(
false,
undefined,
errorShape(ErrorCodes.INVALID_REQUEST, "agent run was not found"),
);
return;
}
}
}
const result = await createAgentTurnService({ context, isWebchatConnect }).waitForTurn(
params as AgentWaitParams,
);
respond(true, result);
};