Fix tool-call lookup failures when models emit whitespace-padded names by normalizing both transcript history and live streamed embedded-runner tool calls before dispatch. Co-authored-by: wangchunyue <80630709+openperf@users.noreply.github.com> Co-authored-by: Sid <sidqin0410@gmail.com> Co-authored-by: Philipp Spiess <hello@philippspiess.com>
377 lines
11 KiB
TypeScript
377 lines
11 KiB
TypeScript
import type { AgentMessage } from "@mariozechner/pi-agent-core";
|
|
import { extractToolCallsFromAssistant, extractToolResultId } from "./tool-call-id.js";
|
|
|
|
const TOOL_CALL_NAME_MAX_CHARS = 64;
|
|
const TOOL_CALL_NAME_RE = /^[A-Za-z0-9_-]+$/;
|
|
|
|
type ToolCallBlock = {
|
|
type?: unknown;
|
|
id?: unknown;
|
|
name?: unknown;
|
|
input?: unknown;
|
|
arguments?: unknown;
|
|
};
|
|
|
|
function isToolCallBlock(block: unknown): block is ToolCallBlock {
|
|
if (!block || typeof block !== "object") {
|
|
return false;
|
|
}
|
|
const type = (block as { type?: unknown }).type;
|
|
return (
|
|
typeof type === "string" &&
|
|
(type === "toolCall" || type === "toolUse" || type === "functionCall")
|
|
);
|
|
}
|
|
|
|
function hasToolCallInput(block: ToolCallBlock): boolean {
|
|
const hasInput = "input" in block ? block.input !== undefined && block.input !== null : false;
|
|
const hasArguments =
|
|
"arguments" in block ? block.arguments !== undefined && block.arguments !== null : false;
|
|
return hasInput || hasArguments;
|
|
}
|
|
|
|
function hasNonEmptyStringField(value: unknown): boolean {
|
|
return typeof value === "string" && value.trim().length > 0;
|
|
}
|
|
|
|
function hasToolCallId(block: ToolCallBlock): boolean {
|
|
return hasNonEmptyStringField(block.id);
|
|
}
|
|
|
|
function normalizeAllowedToolNames(allowedToolNames?: Iterable<string>): Set<string> | null {
|
|
if (!allowedToolNames) {
|
|
return null;
|
|
}
|
|
const normalized = new Set<string>();
|
|
for (const name of allowedToolNames) {
|
|
if (typeof name !== "string") {
|
|
continue;
|
|
}
|
|
const trimmed = name.trim();
|
|
if (trimmed) {
|
|
normalized.add(trimmed.toLowerCase());
|
|
}
|
|
}
|
|
return normalized.size > 0 ? normalized : null;
|
|
}
|
|
|
|
function hasToolCallName(block: ToolCallBlock, allowedToolNames: Set<string> | null): boolean {
|
|
if (typeof block.name !== "string") {
|
|
return false;
|
|
}
|
|
const trimmed = block.name.trim();
|
|
if (!trimmed) {
|
|
return false;
|
|
}
|
|
if (trimmed.length > TOOL_CALL_NAME_MAX_CHARS || !TOOL_CALL_NAME_RE.test(trimmed)) {
|
|
return false;
|
|
}
|
|
if (!allowedToolNames) {
|
|
return true;
|
|
}
|
|
return allowedToolNames.has(trimmed.toLowerCase());
|
|
}
|
|
|
|
function makeMissingToolResult(params: {
|
|
toolCallId: string;
|
|
toolName?: string;
|
|
}): Extract<AgentMessage, { role: "toolResult" }> {
|
|
return {
|
|
role: "toolResult",
|
|
toolCallId: params.toolCallId,
|
|
toolName: params.toolName ?? "unknown",
|
|
content: [
|
|
{
|
|
type: "text",
|
|
text: "[openclaw] missing tool result in session history; inserted synthetic error result for transcript repair.",
|
|
},
|
|
],
|
|
isError: true,
|
|
timestamp: Date.now(),
|
|
} as Extract<AgentMessage, { role: "toolResult" }>;
|
|
}
|
|
|
|
export { makeMissingToolResult };
|
|
|
|
export type ToolCallInputRepairReport = {
|
|
messages: AgentMessage[];
|
|
droppedToolCalls: number;
|
|
droppedAssistantMessages: number;
|
|
};
|
|
|
|
export type ToolCallInputRepairOptions = {
|
|
allowedToolNames?: Iterable<string>;
|
|
};
|
|
|
|
export function stripToolResultDetails(messages: AgentMessage[]): AgentMessage[] {
|
|
let touched = false;
|
|
const out: AgentMessage[] = [];
|
|
for (const msg of messages) {
|
|
if (!msg || typeof msg !== "object" || (msg as { role?: unknown }).role !== "toolResult") {
|
|
out.push(msg);
|
|
continue;
|
|
}
|
|
if (!("details" in msg)) {
|
|
out.push(msg);
|
|
continue;
|
|
}
|
|
const { details: _details, ...rest } = msg as unknown as Record<string, unknown>;
|
|
touched = true;
|
|
out.push(rest as unknown as AgentMessage);
|
|
}
|
|
return touched ? out : messages;
|
|
}
|
|
|
|
export function repairToolCallInputs(
|
|
messages: AgentMessage[],
|
|
options?: ToolCallInputRepairOptions,
|
|
): ToolCallInputRepairReport {
|
|
let droppedToolCalls = 0;
|
|
let droppedAssistantMessages = 0;
|
|
let changed = false;
|
|
const out: AgentMessage[] = [];
|
|
const allowedToolNames = normalizeAllowedToolNames(options?.allowedToolNames);
|
|
|
|
for (const msg of messages) {
|
|
if (!msg || typeof msg !== "object") {
|
|
out.push(msg);
|
|
continue;
|
|
}
|
|
|
|
if (msg.role !== "assistant" || !Array.isArray(msg.content)) {
|
|
out.push(msg);
|
|
continue;
|
|
}
|
|
|
|
const nextContent: typeof msg.content = [];
|
|
let droppedInMessage = 0;
|
|
let trimmedInMessage = 0;
|
|
|
|
for (const block of msg.content) {
|
|
if (
|
|
isToolCallBlock(block) &&
|
|
(!hasToolCallInput(block) ||
|
|
!hasToolCallId(block) ||
|
|
!hasToolCallName(block, allowedToolNames))
|
|
) {
|
|
droppedToolCalls += 1;
|
|
droppedInMessage += 1;
|
|
changed = true;
|
|
continue;
|
|
}
|
|
// Normalize tool call names by trimming whitespace so that downstream
|
|
// lookup (toolsByName map) matches correctly even when the model emits
|
|
// names with leading/trailing spaces (e.g. " read" → "read").
|
|
if (isToolCallBlock(block) && typeof (block as ToolCallBlock).name === "string") {
|
|
const rawName = (block as ToolCallBlock).name as string;
|
|
if (rawName !== rawName.trim()) {
|
|
const normalized = { ...block, name: rawName.trim() } as typeof block;
|
|
nextContent.push(normalized);
|
|
trimmedInMessage += 1;
|
|
changed = true;
|
|
continue;
|
|
}
|
|
}
|
|
nextContent.push(block);
|
|
}
|
|
|
|
if (droppedInMessage > 0) {
|
|
if (nextContent.length === 0) {
|
|
droppedAssistantMessages += 1;
|
|
changed = true;
|
|
continue;
|
|
}
|
|
out.push({ ...msg, content: nextContent });
|
|
continue;
|
|
}
|
|
|
|
// When tool names were trimmed but nothing was dropped,
|
|
// we still need to emit the message with the normalized content.
|
|
if (trimmedInMessage > 0) {
|
|
out.push({ ...msg, content: nextContent });
|
|
continue;
|
|
}
|
|
|
|
out.push(msg);
|
|
}
|
|
|
|
return {
|
|
messages: changed ? out : messages,
|
|
droppedToolCalls,
|
|
droppedAssistantMessages,
|
|
};
|
|
}
|
|
|
|
export function sanitizeToolCallInputs(
|
|
messages: AgentMessage[],
|
|
options?: ToolCallInputRepairOptions,
|
|
): AgentMessage[] {
|
|
return repairToolCallInputs(messages, options).messages;
|
|
}
|
|
|
|
export function sanitizeToolUseResultPairing(messages: AgentMessage[]): AgentMessage[] {
|
|
return repairToolUseResultPairing(messages).messages;
|
|
}
|
|
|
|
export type ToolUseRepairReport = {
|
|
messages: AgentMessage[];
|
|
added: Array<Extract<AgentMessage, { role: "toolResult" }>>;
|
|
droppedDuplicateCount: number;
|
|
droppedOrphanCount: number;
|
|
moved: boolean;
|
|
};
|
|
|
|
export function repairToolUseResultPairing(messages: AgentMessage[]): ToolUseRepairReport {
|
|
// Anthropic (and Cloud Code Assist) reject transcripts where assistant tool calls are not
|
|
// immediately followed by matching tool results. Session files can end up with results
|
|
// displaced (e.g. after user turns) or duplicated. Repair by:
|
|
// - moving matching toolResult messages directly after their assistant toolCall turn
|
|
// - inserting synthetic error toolResults for missing ids
|
|
// - dropping duplicate toolResults for the same id (anywhere in the transcript)
|
|
const out: AgentMessage[] = [];
|
|
const added: Array<Extract<AgentMessage, { role: "toolResult" }>> = [];
|
|
const seenToolResultIds = new Set<string>();
|
|
let droppedDuplicateCount = 0;
|
|
let droppedOrphanCount = 0;
|
|
let moved = false;
|
|
let changed = false;
|
|
|
|
const pushToolResult = (msg: Extract<AgentMessage, { role: "toolResult" }>) => {
|
|
const id = extractToolResultId(msg);
|
|
if (id && seenToolResultIds.has(id)) {
|
|
droppedDuplicateCount += 1;
|
|
changed = true;
|
|
return;
|
|
}
|
|
if (id) {
|
|
seenToolResultIds.add(id);
|
|
}
|
|
out.push(msg);
|
|
};
|
|
|
|
for (let i = 0; i < messages.length; i += 1) {
|
|
const msg = messages[i];
|
|
if (!msg || typeof msg !== "object") {
|
|
out.push(msg);
|
|
continue;
|
|
}
|
|
|
|
const role = (msg as { role?: unknown }).role;
|
|
if (role !== "assistant") {
|
|
// Tool results must only appear directly after the matching assistant tool call turn.
|
|
// Any "free-floating" toolResult entries in session history can make strict providers
|
|
// (Anthropic-compatible APIs, MiniMax, Cloud Code Assist) reject the entire request.
|
|
if (role !== "toolResult") {
|
|
out.push(msg);
|
|
} else {
|
|
droppedOrphanCount += 1;
|
|
changed = true;
|
|
}
|
|
continue;
|
|
}
|
|
|
|
const assistant = msg as Extract<AgentMessage, { role: "assistant" }>;
|
|
|
|
// Skip tool call extraction for aborted or errored assistant messages.
|
|
// When stopReason is "error" or "aborted", the tool_use blocks may be incomplete
|
|
// (e.g., partialJson: true) and should not have synthetic tool_results created.
|
|
// Creating synthetic results for incomplete tool calls causes API 400 errors:
|
|
// "unexpected tool_use_id found in tool_result blocks"
|
|
// See: https://github.com/openclaw/openclaw/issues/4597
|
|
const stopReason = (assistant as { stopReason?: string }).stopReason;
|
|
if (stopReason === "error" || stopReason === "aborted") {
|
|
out.push(msg);
|
|
continue;
|
|
}
|
|
|
|
const toolCalls = extractToolCallsFromAssistant(assistant);
|
|
if (toolCalls.length === 0) {
|
|
out.push(msg);
|
|
continue;
|
|
}
|
|
|
|
const toolCallIds = new Set(toolCalls.map((t) => t.id));
|
|
|
|
const spanResultsById = new Map<string, Extract<AgentMessage, { role: "toolResult" }>>();
|
|
const remainder: AgentMessage[] = [];
|
|
|
|
let j = i + 1;
|
|
for (; j < messages.length; j += 1) {
|
|
const next = messages[j];
|
|
if (!next || typeof next !== "object") {
|
|
remainder.push(next);
|
|
continue;
|
|
}
|
|
|
|
const nextRole = (next as { role?: unknown }).role;
|
|
if (nextRole === "assistant") {
|
|
break;
|
|
}
|
|
|
|
if (nextRole === "toolResult") {
|
|
const toolResult = next as Extract<AgentMessage, { role: "toolResult" }>;
|
|
const id = extractToolResultId(toolResult);
|
|
if (id && toolCallIds.has(id)) {
|
|
if (seenToolResultIds.has(id)) {
|
|
droppedDuplicateCount += 1;
|
|
changed = true;
|
|
continue;
|
|
}
|
|
if (!spanResultsById.has(id)) {
|
|
spanResultsById.set(id, toolResult);
|
|
}
|
|
continue;
|
|
}
|
|
}
|
|
|
|
// Drop tool results that don't match the current assistant tool calls.
|
|
if (nextRole !== "toolResult") {
|
|
remainder.push(next);
|
|
} else {
|
|
droppedOrphanCount += 1;
|
|
changed = true;
|
|
}
|
|
}
|
|
|
|
out.push(msg);
|
|
|
|
if (spanResultsById.size > 0 && remainder.length > 0) {
|
|
moved = true;
|
|
changed = true;
|
|
}
|
|
|
|
for (const call of toolCalls) {
|
|
const existing = spanResultsById.get(call.id);
|
|
if (existing) {
|
|
pushToolResult(existing);
|
|
} else {
|
|
const missing = makeMissingToolResult({
|
|
toolCallId: call.id,
|
|
toolName: call.name,
|
|
});
|
|
added.push(missing);
|
|
changed = true;
|
|
pushToolResult(missing);
|
|
}
|
|
}
|
|
|
|
for (const rem of remainder) {
|
|
if (!rem || typeof rem !== "object") {
|
|
out.push(rem);
|
|
continue;
|
|
}
|
|
out.push(rem);
|
|
}
|
|
i = j - 1;
|
|
}
|
|
|
|
const changedOrMoved = changed || moved;
|
|
return {
|
|
messages: changedOrMoved ? out : messages,
|
|
added,
|
|
droppedDuplicateCount,
|
|
droppedOrphanCount,
|
|
moved: changedOrMoved,
|
|
};
|
|
}
|