mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 15:10:00 +08:00
## Description Preserve provider metadata while canonicalizing assistant tool calls. The existing normalization rebuilt a `toolCall` from four fields and dropped `thoughtSignature`, including on unchanged assistant turns restored after compressing a different tool result. Closes #4036. The original dynamic-notice behavior in #3809 is already addressed on main and is not claimed as a new fix here. ## Type of Change - [x] Bug fix (non-breaking) - [x] Documentation update ## Changes Made - Retain the original tool-call block properties before applying the existing canonical type/id/name/arguments normalization. - Keep existing missing-metadata defaults and legacy `tool_use` normalization; no new options, routes, or dependencies. - Add a failing-before regression for a signed canonical tool call and document metadata retention in the existing plugin README. ## Testing - Plugin: `npm test` — **8 files, 121 tests passed**. - Plugin: `npm run typecheck` and `npm run build` — passed. - Regression before the production change: **1 failed, 4 passed**, with `thoughtSignature` observed as `undefined`. - Required pre-push `make ci-precheck` — **passed**: complete Rust workspace formatting, strict Clippy, workspace tests; native extension build/import verification; Python qualification suite **177 passed, 4 skipped**; Conventional Commit lint. - Complete Ruff formatting check — **1768 files already formatted**. - Whole-tree Ruff lint reported five unchanged executable OAuth2 Python files lacking shebangs; no unrelated chmod or suppression was applied. - Whole-tree `mypy headroom` checked 566 files and reported one unchanged `truthy-function` error in `headroom/integrations/langchain/chat_model.py:394`; no Python implementation was changed by this PR. - Complete Python suite — **15,630 passed, 612 skipped, 7 failed**. The failures are outside the changed plugin: two CLI-import shape errors, one WebSocket lifecycle timeout, and four Kompress readiness expectations. These broad failures are not suppressed or claimed green. - Published GitHub checks — **19 passed, 19 skipped**, with no failed or pending check at the recorded readiness check. ## Real Behavior Proof Environment: Linux x86_64, Bun 1.3.13, Node 22.23.1, Python 3.13.14, repository-pinned Rust 1.95.0; base `3c418f6f0b190ad1060413c0eadd77f20ab3cf46`. Actual converter/restorer command: `bun -e` importing `agentToOpenAIIndexed` and `restoreAgentMessages` from `plugins/openclaw/src/convert.ts`. The input contains a user turn, a Google-provider assistant turn with a signed canonical tool call, and a separate tool result. Only the tool-result text is compressed in the returned wire messages. Before: the identical Google-provider fixture run against untouched main fails its unchanged-assistant deep equality assertion because the tool call loses `thoughtSignature`. After: ```text unchanged_user_byte_exact: true unchanged_assistant_byte_exact: true thinking_signature_preserved: true compressed_tool_text: short tool result tool_metadata_preserved: true ``` The unchanged-assistant equality includes the tool-call signature and arguments. This is an actual converter round trip, not a mocked provider or a forwarding-only test. Not tested: full OpenClaw gateway installation, paid provider calls, provider cache-hit rates/billing, macOS, Windows. ## Runtime Rollout Safety No rollout-managed feature/channel or unsafe override is added. Default plugin normalization now retains opaque provider tool-call metadata; canonical defaults remain intact. Disable the OpenClaw plugin to avoid its context assembly, or revert this three-file commit as rollback. Existing Rust/Python qualification remains passing as recorded above. ## Review Readiness - [x] Existing conversion consumers, normalization defaults, and scope reviewed - [x] This PR is ready for human review; all broad-check outcomes are recorded, including unrelated Python limitations ## Checklist - [x] Corresponding documentation updated - [x] Regression proves the fix is effective - [x] No new warnings observed in the changed plugin - [x] No `CHANGELOG.md` edit (release-please owns it) ## Additional Notes Fresh duplicate check covered all 195 open PRs. #4034 preserves signatures in Python CCR handling and does not modify this OpenClaw normalizer. AI-assisted: omp / GPT-6.1 Sol.
199 lines
6.0 KiB
TypeScript
199 lines
6.0 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import { agentToOpenAI, agentToOpenAIIndexed, normalizeAgentMessages, openAIToAgent, restoreAgentMessages, type OpenAIMessage } from "../src/convert";
|
|
|
|
describe("openAIToAgent", () => {
|
|
it("emits toolResult content as blocks so transports can safely filter", () => {
|
|
const messages: OpenAIMessage[] = [
|
|
{
|
|
role: "tool",
|
|
content: "tool output",
|
|
tool_call_id: "call_123",
|
|
},
|
|
];
|
|
|
|
const result = openAIToAgent(messages);
|
|
const toolResult = result[0] as {
|
|
role: string;
|
|
content: Array<{ type: string; text?: string }>;
|
|
toolCallId: string;
|
|
tool_use_id: string;
|
|
};
|
|
|
|
expect(toolResult.role).toBe("toolResult");
|
|
expect(Array.isArray(toolResult.content)).toBe(true);
|
|
expect(toolResult.content).toEqual([{ type: "text", text: "tool output" }]);
|
|
expect(toolResult.toolCallId).toBe("call_123");
|
|
expect(toolResult.tool_use_id).toBe("call_123");
|
|
});
|
|
});
|
|
|
|
describe("normalizeAgentMessages", () => {
|
|
it("normalizes assistant string content into OpenClaw blocks", () => {
|
|
const result = normalizeAgentMessages([
|
|
{
|
|
role: "assistant",
|
|
content: "hello from headroom",
|
|
},
|
|
]);
|
|
|
|
expect(result[0]).toMatchObject({
|
|
role: "assistant",
|
|
content: [{ type: "text", text: "hello from headroom" }],
|
|
api: "headroom",
|
|
provider: "headroom",
|
|
model: "headroom",
|
|
stopReason: "stop",
|
|
});
|
|
});
|
|
|
|
it("normalizes tool result string content into OpenClaw blocks", () => {
|
|
const result = normalizeAgentMessages([
|
|
{
|
|
role: "toolResult",
|
|
content: "tool output",
|
|
},
|
|
]);
|
|
|
|
expect(result[0]).toMatchObject({
|
|
role: "toolResult",
|
|
content: [{ type: "text", text: "tool output" }],
|
|
toolCallId: "unknown",
|
|
tool_use_id: "unknown",
|
|
toolName: "headroom",
|
|
isError: false,
|
|
});
|
|
});
|
|
|
|
it("preserves provider thought signatures on canonical tool calls", () => {
|
|
const result = normalizeAgentMessages([
|
|
{
|
|
role: "assistant",
|
|
content: [
|
|
{
|
|
type: "toolCall",
|
|
id: "call_signed",
|
|
name: "read",
|
|
arguments: { path: "file.ts" },
|
|
thoughtSignature: "provider-tool-call-signature",
|
|
},
|
|
],
|
|
},
|
|
]);
|
|
|
|
expect(result[0].content[0].thoughtSignature).toBe("provider-tool-call-signature");
|
|
expect(result[0].content[0].arguments).toEqual({ path: "file.ts" });
|
|
});
|
|
|
|
it("normalizes legacy tool inputs without retaining a duplicate alias", () => {
|
|
const block = {
|
|
type: "tool_use",
|
|
name: "read",
|
|
input: { path: "file.ts" },
|
|
thoughtSignature: "legacy-signature",
|
|
providerMetadata: { opaque: true },
|
|
};
|
|
|
|
const result = normalizeAgentMessages([{ role: "assistant", content: [block] }]);
|
|
|
|
expect(result[0].content[0]).toEqual({
|
|
type: "toolCall",
|
|
id: "unknown",
|
|
name: "read",
|
|
arguments: { path: "file.ts" },
|
|
thoughtSignature: "legacy-signature",
|
|
providerMetadata: { opaque: true },
|
|
});
|
|
expect(block.input).toEqual({ path: "file.ts" });
|
|
expect(block.type).toBe("tool_use");
|
|
});
|
|
|
|
it("keeps existing arguments precedence while removing the legacy alias", () => {
|
|
const result = normalizeAgentMessages([
|
|
{
|
|
role: "assistant",
|
|
content: [{ type: "tool_use", name: "read", arguments: null, input: { path: "old.ts" } }],
|
|
},
|
|
]);
|
|
|
|
expect(result[0].content[0].arguments).toBeNull();
|
|
expect(result[0].content[0]).not.toHaveProperty("input");
|
|
});
|
|
|
|
it("retains opaque extension fields on already canonical tool calls", () => {
|
|
const block = {
|
|
type: "toolCall",
|
|
id: "call_canonical",
|
|
name: "read",
|
|
arguments: { path: "file.ts" },
|
|
input: { extension: "opaque canonical metadata" },
|
|
thoughtSignature: "canonical-signature",
|
|
providerMetadata: { opaque: true },
|
|
};
|
|
|
|
const result = normalizeAgentMessages([{ role: "assistant", content: [block] }]);
|
|
|
|
expect(result[0].content[0]).toEqual(block);
|
|
});
|
|
});
|
|
|
|
describe("restoreAgentMessages", () => {
|
|
it("restores compressed legacy arguments without stale input and preserves metadata", () => {
|
|
const block = {
|
|
type: "tool_use",
|
|
id: "call_legacy",
|
|
name: "read",
|
|
input: { path: "file.ts", lines: ["long", "original", "payload"] },
|
|
thoughtSignature: "legacy-signature",
|
|
providerMetadata: { opaque: true },
|
|
};
|
|
const original = [{ role: "assistant", content: [block] }];
|
|
const sent = agentToOpenAIIndexed(original);
|
|
const returned = structuredClone(sent);
|
|
returned[0].tool_calls![0].function.arguments = '{"path":"file.ts","lines":["short"]}';
|
|
|
|
const result = restoreAgentMessages(original, sent, returned);
|
|
|
|
expect(result[0].content[0]).toEqual({
|
|
type: "toolCall",
|
|
id: "call_legacy",
|
|
name: "read",
|
|
arguments: { path: "file.ts", lines: ["short"] },
|
|
thoughtSignature: "legacy-signature",
|
|
providerMetadata: { opaque: true },
|
|
});
|
|
expect(block.input.lines).toEqual(["long", "original", "payload"]);
|
|
expect(agentToOpenAI(result)[0].tool_calls![0].function.arguments)
|
|
.toBe('{"path":"file.ts","lines":["short"]}');
|
|
});
|
|
});
|
|
|
|
describe("agentToOpenAI", () => {
|
|
it("captures assistant metadata needed for OpenClaw round-trips", () => {
|
|
const result = agentToOpenAI([
|
|
{
|
|
role: "assistant",
|
|
content: "hello",
|
|
api: "anthropic-messages",
|
|
provider: "anthropic",
|
|
model: "claude-sonnet-4-5",
|
|
stopReason: "stop",
|
|
usage: {
|
|
input: 1,
|
|
output: 2,
|
|
cacheRead: 0,
|
|
cacheWrite: 0,
|
|
totalTokens: 3,
|
|
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
},
|
|
},
|
|
]);
|
|
|
|
expect(result[0]._headroomMeta).toMatchObject({
|
|
api: "anthropic-messages",
|
|
provider: "anthropic",
|
|
model: "claude-sonnet-4-5",
|
|
stopReason: "stop",
|
|
});
|
|
});
|
|
});
|