π€― LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
import { safeParseJSON, safeParsePartialJSON } from './safeParseJSON';
/**
* Ensure a tool_call `arguments` string is always valid JSON before it
* enters history or gets replayed to providers.
*
* Strict providers (e.g. NVIDIA NIM) validate the full message history on
* every request. A single malformed `arguments` string β even one produced
* many turns ago β causes a 400 on the entire request, terminating the op
* and wasting all accumulated tokens. See .
*
* Behavior:
* - Valid JSON β returned as-is (preserves prompt-cache keys).
* - Recoverable via partial-json β re-stringified.
* - Unrecoverable β "{}" so the tool_call structure survives and the
* model can replan on the next turn.
*/
export const sanitizeToolCallArguments = (argsStr: string | undefined): string => {
if (typeof argsStr !== 'string' || argsStr.length === 0) return '{}';
if (safeParseJSON(argsStr) !== undefined) return argsStr;
const recovered = safeParsePartialJSON(argsStr);
if (recovered !== undefined && typeof recovered === 'object' && recovered !== null) {
return JSON.stringify(recovered);
}
return '{}';
};