Skip to content

Commit 3ea9115

Browse files
fix: promote serialized tool calls via repair package
Extracts serialized plaintext tool-call parsing, scrubbing, stream normalization, and standalone promotion into the private internal @openclaw/tool-call-repair package. Provider wrappers and the embedded runner now share one repair path for standalone serialized tool calls, including adjacent text-block splits, while preserving exact argument bytes when already valid. The public plugin SDK payload module remains as the compatibility facade. Verification: - pnpm test src/plugin-sdk/provider-stream-shared.test.ts src/plugin-sdk/tool-payload.test.ts src/agents/embedded-agent-runner/run/attempt.tool-call-normalization.test.ts -- --reporter=verbose - env -u OPENCLAW_TESTBOX pnpm check:changed - PR CI: all reported checks green/skipped/neutral on ff0b3c0 Refs #86924 Co-authored-by: fuller-stack-dev <[email protected]>
1 parent 4432554 commit 3ea9115

20 files changed

Lines changed: 5463 additions & 648 deletions
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "@openclaw/tool-call-repair",
3+
"version": "0.0.0-private",
4+
"private": true,
5+
"type": "module",
6+
"exports": {
7+
".": "./src/index.ts"
8+
}
9+
}
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
export const END_TOOL_REQUEST = "[END_TOOL_REQUEST]";
2+
export const HARMONY_CHANNEL_MARKER = "<|channel|>";
3+
export const HARMONY_MESSAGE_MARKER = "<|message|>";
4+
export const HARMONY_CALL_MARKER = "<|call|>";
5+
6+
export function matchesLiteralPrefix(text: string, literal: string): boolean {
7+
return literal.startsWith(text) || text.startsWith(literal);
8+
}
9+
10+
export function isPlainTextToolNameChar(char: string | undefined): boolean {
11+
return Boolean(char && /[A-Za-z0-9_-]/.test(char));
12+
}
13+
14+
export function isXmlishNameChar(char: string | undefined): boolean {
15+
return Boolean(char && /[A-Za-z0-9_.:-]/.test(char));
16+
}
17+
18+
export function skipHorizontalWhitespace(text: string, start: number): number {
19+
let index = start;
20+
while (index < text.length && (text[index] === " " || text[index] === "\t")) {
21+
index += 1;
22+
}
23+
return index;
24+
}
25+
26+
export function skipWhitespace(text: string, start: number): number {
27+
let index = start;
28+
while (index < text.length && /\s/.test(text[index] ?? "")) {
29+
index += 1;
30+
}
31+
return index;
32+
}
33+
34+
export function consumeLineBreak(text: string, start: number): number | null {
35+
if (text[start] === "\r") {
36+
return text[start + 1] === "\n" ? start + 2 : start + 1;
37+
}
38+
if (text[start] === "\n") {
39+
return start + 1;
40+
}
41+
return null;
42+
}
43+
44+
export function findJsonObjectEnd(
45+
text: string,
46+
start: number,
47+
maxPayloadBytes?: number,
48+
): number | null {
49+
let depth = 0;
50+
let inString = false;
51+
let escaped = false;
52+
for (let index = start; index < text.length; index += 1) {
53+
if (maxPayloadBytes !== undefined && index + 1 - start > maxPayloadBytes) {
54+
return null;
55+
}
56+
const char = text[index];
57+
if (inString) {
58+
if (escaped) {
59+
escaped = false;
60+
} else if (char === "\\") {
61+
escaped = true;
62+
} else if (char === '"') {
63+
inString = false;
64+
}
65+
continue;
66+
}
67+
if (char === '"') {
68+
inString = true;
69+
continue;
70+
}
71+
if (char === "{") {
72+
depth += 1;
73+
continue;
74+
}
75+
if (char === "}") {
76+
depth -= 1;
77+
if (depth === 0) {
78+
return index + 1;
79+
}
80+
}
81+
}
82+
return null;
83+
}
84+
85+
export function skipSerializedToolCallTrailingLineBreak(text: string, cursor: number): number {
86+
const afterLineBreak = consumeLineBreak(text, cursor);
87+
return afterLineBreak ?? cursor;
88+
}
89+
90+
export function consumeJsonToolClosingMarker(text: string, cursor: number): number {
91+
let markerStart = cursor;
92+
while (markerStart < text.length && /\s/.test(text[markerStart] ?? "")) {
93+
markerStart += 1;
94+
}
95+
const rest = text.slice(markerStart);
96+
if (rest.startsWith(END_TOOL_REQUEST)) {
97+
return skipSerializedToolCallTrailingLineBreak(text, markerStart + END_TOOL_REQUEST.length);
98+
}
99+
const bracketClose = /^\[\/[A-Za-z0-9_-]+\]/.exec(rest);
100+
if (bracketClose) {
101+
return skipSerializedToolCallTrailingLineBreak(text, markerStart + bracketClose[0].length);
102+
}
103+
if (rest.startsWith(HARMONY_CALL_MARKER)) {
104+
return skipSerializedToolCallTrailingLineBreak(text, markerStart + HARMONY_CALL_MARKER.length);
105+
}
106+
return skipSerializedToolCallTrailingLineBreak(text, cursor);
107+
}
108+
109+
export function findBracketedJsonPayloadStart(text: string): number | null {
110+
if (!text.startsWith("[")) {
111+
return null;
112+
}
113+
const close = text.indexOf("]");
114+
if (close === -1) {
115+
return null;
116+
}
117+
let cursor = close + 1;
118+
cursor = skipHorizontalWhitespace(text, cursor);
119+
cursor = skipSerializedToolCallTrailingLineBreak(text, cursor);
120+
cursor = skipHorizontalWhitespace(text, cursor);
121+
return text[cursor] === "{" ? cursor : null;
122+
}
123+
124+
export function findHarmonyJsonPayloadStart(text: string): number | null {
125+
let cursor = 0;
126+
if (text.startsWith(HARMONY_CHANNEL_MARKER)) {
127+
cursor = HARMONY_CHANNEL_MARKER.length;
128+
}
129+
const rest = text.slice(cursor);
130+
const channel = ["commentary", "analysis", "final"].find((candidate) =>
131+
rest.startsWith(candidate),
132+
);
133+
if (!channel) {
134+
return null;
135+
}
136+
cursor += channel.length;
137+
cursor = skipHorizontalWhitespace(text, cursor);
138+
if (!text.slice(cursor).startsWith("to=")) {
139+
return null;
140+
}
141+
cursor += "to=".length;
142+
const nameStart = cursor;
143+
while (isPlainTextToolNameChar(text[cursor])) {
144+
cursor += 1;
145+
}
146+
if (cursor === nameStart) {
147+
return null;
148+
}
149+
cursor = skipHorizontalWhitespace(text, cursor);
150+
if (!text.slice(cursor).startsWith("code")) {
151+
return null;
152+
}
153+
cursor += "code".length;
154+
cursor = skipWhitespace(text, cursor);
155+
if (text.slice(cursor).startsWith(HARMONY_MESSAGE_MARKER)) {
156+
cursor = skipWhitespace(text, cursor + HARMONY_MESSAGE_MARKER.length);
157+
}
158+
return text[cursor] === "{" ? cursor : null;
159+
}
160+
161+
export function startsWithAsciiMarkerIgnoreCase(
162+
text: string,
163+
cursor: number,
164+
marker: string,
165+
): boolean {
166+
return text.slice(cursor, cursor + marker.length).toLowerCase() === marker;
167+
}
168+
169+
export function indexOfAsciiMarkerIgnoreCase(text: string, marker: string, start: number): number {
170+
let cursor = start;
171+
while (cursor < text.length) {
172+
const next = text.indexOf(marker[0] ?? "", cursor);
173+
if (next === -1) {
174+
return -1;
175+
}
176+
if (startsWithAsciiMarkerIgnoreCase(text, next, marker)) {
177+
return next;
178+
}
179+
cursor = next + 1;
180+
}
181+
return -1;
182+
}
183+
184+
export function findXmlishToolCallEnd(text: string): number | null {
185+
let cursor: number;
186+
const xmlFunction = /^<function=[A-Za-z0-9_.:-]+>/i.exec(text);
187+
if (xmlFunction) {
188+
cursor = xmlFunction[0].length;
189+
} else {
190+
const bracketed = /^\[(?:tool:)?[A-Za-z0-9_-]+\]/.exec(text);
191+
if (!bracketed) {
192+
return null;
193+
}
194+
cursor = bracketed[0].length;
195+
cursor = skipHorizontalWhitespace(text, cursor);
196+
cursor = skipSerializedToolCallTrailingLineBreak(text, cursor);
197+
}
198+
199+
cursor = skipWhitespace(text, cursor);
200+
if (!startsWithAsciiMarkerIgnoreCase(text, cursor, "<parameter=")) {
201+
return null;
202+
}
203+
204+
while (cursor < text.length) {
205+
const parameterClose = indexOfAsciiMarkerIgnoreCase(text, "</parameter>", cursor);
206+
if (parameterClose === -1) {
207+
return null;
208+
}
209+
cursor = skipWhitespace(text, parameterClose + "</parameter>".length);
210+
if (startsWithAsciiMarkerIgnoreCase(text, cursor, "</function>")) {
211+
return skipSerializedToolCallTrailingLineBreak(text, cursor + "</function>".length);
212+
}
213+
if (!startsWithAsciiMarkerIgnoreCase(text, cursor, "<parameter=")) {
214+
return skipSerializedToolCallTrailingLineBreak(text, cursor);
215+
}
216+
}
217+
return null;
218+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export {
2+
parseStandalonePlainTextToolCallBlocks,
3+
stripPlainTextToolCallBlocks,
4+
type PlainTextToolCallBlock,
5+
type PlainTextToolCallParseOptions,
6+
} from "./payload.js";
7+
export {
8+
normalizePlainTextToolCallStreamEvents,
9+
scrubOverCapPlainTextToolCallMessage,
10+
type PlainTextToolCallMessageNormalization,
11+
type PlainTextToolCallNameMatcher,
12+
type PlainTextToolCallStreamNormalizerOptions,
13+
} from "./stream-normalizer.js";
14+
export {
15+
extractStandalonePlainTextToolCallText,
16+
promoteStandalonePlainTextToolCallMessage,
17+
type PlainTextToolCallPromotionOptions,
18+
type PromotedPlainTextToolCallBlockFactory,
19+
type ToolCallRepairNameResolver,
20+
} from "./promote.js";

0 commit comments

Comments
 (0)