openclaw: accept webhook json ack in sse mode
This commit is contained in:
@@ -83,6 +83,13 @@ function isTextRequiredResponse(responseText: string): boolean {
|
|||||||
return responseText.toLowerCase().includes("text required");
|
return responseText.toLowerCase().includes("text required");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isWebhookAcceptedResponse(parsed: Record<string, unknown> | null): boolean {
|
||||||
|
if (!parsed) return false;
|
||||||
|
if (parsed.ok === true) return true;
|
||||||
|
const status = nonEmpty(parsed.status)?.toLowerCase();
|
||||||
|
return status === "ok" || status === "accepted";
|
||||||
|
}
|
||||||
|
|
||||||
async function sendJsonRequest(params: {
|
async function sendJsonRequest(params: {
|
||||||
url: string;
|
url: string;
|
||||||
method: string;
|
method: string;
|
||||||
@@ -576,6 +583,7 @@ export async function execute(ctx: AdapterExecutionContext): Promise<AdapterExec
|
|||||||
const contentType = (response.headers.get("content-type") ?? "").toLowerCase();
|
const contentType = (response.headers.get("content-type") ?? "").toLowerCase();
|
||||||
if (!contentType.includes("text/event-stream")) {
|
if (!contentType.includes("text/event-stream")) {
|
||||||
const responseText = await readAndLogResponseText({ response, onLog });
|
const responseText = await readAndLogResponseText({ response, onLog });
|
||||||
|
const parsedResponse = parseOpenClawResponse(responseText);
|
||||||
if (isTextRequiredResponse(responseText)) {
|
if (isTextRequiredResponse(responseText)) {
|
||||||
await onLog(
|
await onLog(
|
||||||
"stdout",
|
"stdout",
|
||||||
@@ -607,6 +615,28 @@ export async function execute(ctx: AdapterExecutionContext): Promise<AdapterExec
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (isWebhookAcceptedResponse(parsedResponse)) {
|
||||||
|
await onLog(
|
||||||
|
"stdout",
|
||||||
|
"[openclaw] non-SSE response acknowledged run; treating as webhook compatibility success\n",
|
||||||
|
);
|
||||||
|
return {
|
||||||
|
exitCode: 0,
|
||||||
|
signal: null,
|
||||||
|
timedOut: false,
|
||||||
|
provider: "openclaw",
|
||||||
|
model: null,
|
||||||
|
summary: `OpenClaw webhook ${method} ${url} (non-stream compatibility)`,
|
||||||
|
resultJson: {
|
||||||
|
status: response.status,
|
||||||
|
statusText: response.statusText,
|
||||||
|
contentType,
|
||||||
|
compatibilityMode: "json_ack",
|
||||||
|
transportFallback: "webhook",
|
||||||
|
response: parsedResponse ?? responseText,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
return {
|
return {
|
||||||
exitCode: 1,
|
exitCode: 1,
|
||||||
signal: null,
|
signal: null,
|
||||||
@@ -617,7 +647,7 @@ export async function execute(ctx: AdapterExecutionContext): Promise<AdapterExec
|
|||||||
status: response.status,
|
status: response.status,
|
||||||
statusText: response.statusText,
|
statusText: response.statusText,
|
||||||
contentType,
|
contentType,
|
||||||
response: parseOpenClawResponse(responseText) ?? responseText,
|
response: parsedResponse ?? responseText,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -117,9 +117,9 @@ describe("openclaw adapter execute", () => {
|
|||||||
expect((body.paperclip as Record<string, unknown>).sessionKey).toBe("paperclip:issue:issue-123");
|
expect((body.paperclip as Record<string, unknown>).sessionKey).toBe("paperclip:issue:issue-123");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("fails when SSE endpoint does not return text/event-stream", async () => {
|
it("fails when SSE endpoint does not return text/event-stream and no compatibility fallback applies", async () => {
|
||||||
const fetchMock = vi.fn().mockResolvedValue(
|
const fetchMock = vi.fn().mockResolvedValue(
|
||||||
new Response(JSON.stringify({ ok: true }), {
|
new Response(JSON.stringify({ ok: false, error: "unexpected payload" }), {
|
||||||
status: 200,
|
status: 200,
|
||||||
statusText: "OK",
|
statusText: "OK",
|
||||||
headers: {
|
headers: {
|
||||||
@@ -140,6 +140,30 @@ describe("openclaw adapter execute", () => {
|
|||||||
expect(result.errorCode).toBe("openclaw_sse_expected_event_stream");
|
expect(result.errorCode).toBe("openclaw_sse_expected_event_stream");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("treats webhook-style JSON ack as compatibility success when SSE endpoint returns JSON", async () => {
|
||||||
|
const fetchMock = vi.fn().mockResolvedValue(
|
||||||
|
new Response(JSON.stringify({ ok: true, runId: "oc-run-1" }), {
|
||||||
|
status: 200,
|
||||||
|
statusText: "OK",
|
||||||
|
headers: {
|
||||||
|
"content-type": "application/json",
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
vi.stubGlobal("fetch", fetchMock);
|
||||||
|
|
||||||
|
const result = await execute(
|
||||||
|
buildContext({
|
||||||
|
url: "https://agent.example/hooks/paperclip",
|
||||||
|
method: "POST",
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(result.exitCode).toBe(0);
|
||||||
|
expect(result.resultJson?.compatibilityMode).toBe("json_ack");
|
||||||
|
expect(result.resultJson?.transportFallback).toBe("webhook");
|
||||||
|
});
|
||||||
|
|
||||||
it("falls back to wake text payload when SSE is configured against /hooks/wake", async () => {
|
it("falls back to wake text payload when SSE is configured against /hooks/wake", async () => {
|
||||||
const fetchMock = vi.fn().mockResolvedValue(
|
const fetchMock = vi.fn().mockResolvedValue(
|
||||||
new Response(JSON.stringify({ ok: true }), { status: 200, statusText: "OK" }),
|
new Response(JSON.stringify({ ok: true }), { status: 200, statusText: "OK" }),
|
||||||
|
|||||||
Reference in New Issue
Block a user