mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-01 01:24:30 +01:00
fix(openclaw-gateway): keep whitespace on streamed assistant deltas (#14150)
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - The `openclaw_gateway` adapter streams the agent reply as `assistant` deltas and joins them into the run summary > - Each delta goes through `nonEmpty()`, which trims whitespace > - Deltas carry their word-boundary space (`" blocked"`), so the trim removes it and words run together > - The broken text is stored as the issue comment, so users see corrupted agent replies > - This pull request keeps each delta verbatim and skips only empty strings > - The benefit is that agent comments from the OpenClaw gateway are stored correctly ## Linked Issues or Issue Description Fixes #14149 ## What Changed - Add `readAssistantChunk()` in `execute.ts`. It returns a non-empty string `delta` without a trim. If there is no delta, it falls back to the trimmed `text` (the old behavior). - Use `readAssistantChunk()` in the `assistant` stream handler. - Add unit tests for whitespace-bearing, whitespace-only, empty, and non-string deltas. ## Verification - `npx vitest run src/server/execute.test.ts` in `packages/adapters/openclaw-gateway`: 12 tests pass. - `npx tsc --noEmit` in the same package: no errors. - Manual: the same one-line change is applied to a local 2026.916.1 install that showed the bug. ## Risks Low risk. The change affects only `assistant` deltas. The final `join("").trim()` still removes outer whitespace from the summary. ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, with tool use and code execution. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [ ] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [ ] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: EpaL <358229+EpaL@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Andrew Aymeloglu <aaymeloglu@gmail.com>
This commit is contained in:
3 files changed
+52
-8
No files matched your search
@@ -8,6 +8,7 @@ const websocketState = vi.hoisted(() => ({
|
||||
failAgentRequests: 0,
|
||||
events: [] as string[],
|
||||
messages: [] as string[],
|
||||
assistantChunks: [] as Array<{ delta: string; text?: string }>,
|
||||
}));
|
||||
|
||||
vi.mock("ws", async () => {
|
||||
@@ -51,6 +52,15 @@ vi.mock("ws", async () => {
|
||||
? { protocol: 3 }
|
||||
: { status: "ok", runId: "remote-run-1", summary: "done" };
|
||||
queueMicrotask(() => {
|
||||
if (request.method === "agent") {
|
||||
for (const data of websocketState.assistantChunks) {
|
||||
this.emit("message", JSON.stringify({
|
||||
type: "event",
|
||||
event: "agent",
|
||||
payload: { runId: "run-1", stream: "assistant", data },
|
||||
}));
|
||||
}
|
||||
}
|
||||
this.emit("message", JSON.stringify({
|
||||
type: "res",
|
||||
id: request.id,
|
||||
@@ -109,12 +119,25 @@ describe("openclaw_gateway execute dispatch boundary", () => {
|
||||
websocketState.failAgentRequests = 0;
|
||||
websocketState.events = [];
|
||||
websocketState.messages = [];
|
||||
websocketState.assistantChunks = [];
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it("preserves a space delta when the event also includes accumulated text", async () => {
|
||||
websocketState.assistantChunks = [
|
||||
{ delta: "Hello", text: "Hello" },
|
||||
{ delta: " ", text: "Hello " },
|
||||
{ delta: "world", text: "Hello world" },
|
||||
];
|
||||
|
||||
const result = await execute(createContext());
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.summary).toBe("Hello world");
|
||||
});
|
||||
|
||||
it.each([false, true])("sends conversation policy without the issue-completion workflow (resumed=%s)", async (resumed) => {
|
||||
const ctx = createContext();
|
||||
const directive = "Chat directive: clarify goals and hand plans off to project tasks.";
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { buildAgentParams, resolveClaimedApiKeyPath, resolveSessionKey } from "./execute.js";
|
||||
import {
|
||||
buildAgentParams,
|
||||
readAssistantChunk,
|
||||
resolveClaimedApiKeyPath,
|
||||
resolveSessionKey,
|
||||
} from "./execute.js";
|
||||
|
||||
describe("resolveSessionKey", () => {
|
||||
it("prefixes run-scoped session keys with the configured agent", () => {
|
||||
@@ -123,3 +128,19 @@ describe("resolveClaimedApiKeyPath", () => {
|
||||
expect(resolveClaimedApiKeyPath({})).toBe(DEFAULT_PATH);
|
||||
});
|
||||
});
|
||||
|
||||
describe("readAssistantChunk", () => {
|
||||
it("keeps leading and trailing whitespace on streamed deltas", () => {
|
||||
const chunks = ["Hello", " world", " ", "is", " blocked."].map((delta) =>
|
||||
readAssistantChunk({ delta }),
|
||||
);
|
||||
expect(chunks).toEqual(["Hello", " world", " ", "is", " blocked."]);
|
||||
expect(chunks.join("")).toBe("Hello world is blocked.");
|
||||
});
|
||||
|
||||
it("ignores empty or non-string deltas and falls back to text", () => {
|
||||
expect(readAssistantChunk({ delta: "", text: " full reply " })).toBe("full reply");
|
||||
expect(readAssistantChunk({ delta: 42, text: "full reply" })).toBe("full reply");
|
||||
expect(readAssistantChunk({})).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -491,6 +491,11 @@ function joinWakePayloadSections(structuredWakePrompt: string, structuredWakeJso
|
||||
return sections.join("\n");
|
||||
}
|
||||
|
||||
export function readAssistantChunk(data: Record<string, unknown>): string | null {
|
||||
if (typeof data.delta === "string" && data.delta.length > 0) return data.delta;
|
||||
return nonEmpty(data.text);
|
||||
}
|
||||
|
||||
export function buildAgentParams(input: {
|
||||
payloadTemplate: Record<string, unknown>;
|
||||
message: string;
|
||||
@@ -1230,13 +1235,8 @@ export async function execute(ctx: AdapterExecutionContext): Promise<AdapterExec
|
||||
);
|
||||
|
||||
if (stream === "assistant") {
|
||||
const delta = nonEmpty(data.delta);
|
||||
const text = nonEmpty(data.text);
|
||||
if (delta) {
|
||||
assistantChunks.push(delta);
|
||||
} else if (text) {
|
||||
assistantChunks.push(text);
|
||||
}
|
||||
const chunk = readAssistantChunk(data);
|
||||
if (chunk) assistantChunks.push(chunk);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user