fix openclaw webhook payload for /v1/responses
This commit is contained in:
@@ -306,6 +306,41 @@ export function isTextRequiredResponse(responseText: string): boolean {
|
||||
return responseText.toLowerCase().includes("text required");
|
||||
}
|
||||
|
||||
function extractResponseErrorMessage(responseText: string): string {
|
||||
const parsed = parseOpenClawResponse(responseText);
|
||||
if (!parsed) return responseText;
|
||||
|
||||
const directError = parsed.error;
|
||||
if (typeof directError === "string") return directError;
|
||||
if (directError && typeof directError === "object") {
|
||||
const nestedMessage = (directError as Record<string, unknown>).message;
|
||||
if (typeof nestedMessage === "string") return nestedMessage;
|
||||
}
|
||||
|
||||
const directMessage = parsed.message;
|
||||
if (typeof directMessage === "string") return directMessage;
|
||||
|
||||
return responseText;
|
||||
}
|
||||
|
||||
export function isWakeCompatibilityRetryableResponse(responseText: string): boolean {
|
||||
if (isTextRequiredResponse(responseText)) return true;
|
||||
|
||||
const normalized = extractResponseErrorMessage(responseText).toLowerCase();
|
||||
const expectsStringInput =
|
||||
normalized.includes("invalid input") &&
|
||||
normalized.includes("expected string") &&
|
||||
normalized.includes("undefined");
|
||||
if (expectsStringInput) return true;
|
||||
|
||||
const missingInputField =
|
||||
normalized.includes("input") &&
|
||||
(normalized.includes("required") || normalized.includes("missing"));
|
||||
if (missingInputField) return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
export async function sendJsonRequest(params: {
|
||||
url: string;
|
||||
method: string;
|
||||
|
||||
@@ -1,14 +1,18 @@
|
||||
import type { AdapterExecutionContext, AdapterExecutionResult } from "@paperclipai/adapter-utils";
|
||||
import {
|
||||
appendWakeText,
|
||||
appendWakeTextToOpenResponsesInput,
|
||||
buildExecutionState,
|
||||
buildWakeCompatibilityPayload,
|
||||
isOpenResponsesEndpoint,
|
||||
isTextRequiredResponse,
|
||||
isWakeCompatibilityRetryableResponse,
|
||||
isWakeCompatibilityEndpoint,
|
||||
readAndLogResponseText,
|
||||
redactForLog,
|
||||
sendJsonRequest,
|
||||
stringifyForLog,
|
||||
toStringRecord,
|
||||
type OpenClawExecutionState,
|
||||
} from "./execute-common.js";
|
||||
import { parseOpenClawResponse } from "./parse.js";
|
||||
@@ -18,12 +22,37 @@ function nonEmpty(value: unknown): string | null {
|
||||
}
|
||||
|
||||
function buildWebhookBody(input: {
|
||||
url: string;
|
||||
state: OpenClawExecutionState;
|
||||
context: AdapterExecutionContext["context"];
|
||||
configModel: unknown;
|
||||
}): Record<string, unknown> {
|
||||
const { state, context } = input;
|
||||
const { url, state, context, configModel } = input;
|
||||
const templateText = nonEmpty(state.payloadTemplate.text);
|
||||
const payloadText = templateText ? appendWakeText(templateText, state.wakeText) : state.wakeText;
|
||||
const isOpenResponses = isOpenResponsesEndpoint(url);
|
||||
|
||||
if (isOpenResponses) {
|
||||
const openResponsesInput = Object.prototype.hasOwnProperty.call(state.payloadTemplate, "input")
|
||||
? appendWakeTextToOpenResponsesInput(state.payloadTemplate.input, state.wakeText)
|
||||
: payloadText;
|
||||
|
||||
return {
|
||||
...state.payloadTemplate,
|
||||
stream: false,
|
||||
model:
|
||||
nonEmpty(state.payloadTemplate.model) ??
|
||||
nonEmpty(configModel) ??
|
||||
"openclaw",
|
||||
input: openResponsesInput,
|
||||
metadata: {
|
||||
...toStringRecord(state.payloadTemplate.metadata),
|
||||
...state.paperclipEnv,
|
||||
paperclip_session_key: state.sessionKey,
|
||||
paperclip_stream_transport: "webhook",
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
...state.payloadTemplate,
|
||||
@@ -74,7 +103,16 @@ export async function executeWebhook(ctx: AdapterExecutionContext, url: string):
|
||||
}
|
||||
|
||||
const headers = { ...state.headers };
|
||||
const webhookBody = buildWebhookBody({ state, context });
|
||||
if (isOpenResponsesEndpoint(url) && !headers["x-openclaw-session-key"] && !headers["X-OpenClaw-Session-Key"]) {
|
||||
headers["x-openclaw-session-key"] = state.sessionKey;
|
||||
}
|
||||
|
||||
const webhookBody = buildWebhookBody({
|
||||
url,
|
||||
state,
|
||||
context,
|
||||
configModel: ctx.config.model,
|
||||
});
|
||||
const wakeCompatibilityBody = buildWakeCompatibilityPayload(state.wakeText);
|
||||
const preferWakeCompatibilityBody = isWakeCompatibilityEndpoint(url);
|
||||
const initialBody = preferWakeCompatibilityBody ? wakeCompatibilityBody : webhookBody;
|
||||
@@ -110,7 +148,7 @@ export async function executeWebhook(ctx: AdapterExecutionContext, url: string):
|
||||
|
||||
if (!initialResponse.response.ok) {
|
||||
const canRetryWithWakeCompatibility =
|
||||
!preferWakeCompatibilityBody && isTextRequiredResponse(initialResponse.responseText);
|
||||
!preferWakeCompatibilityBody && isWakeCompatibilityRetryableResponse(initialResponse.responseText);
|
||||
|
||||
if (canRetryWithWakeCompatibility) {
|
||||
await onLog(
|
||||
|
||||
Reference in New Issue
Block a user