Files
lobehub__lobehub/packages/utils/src/sanitizeToolCallArguments.ts
Arvin Xu ddb5794826 chore: clean up LOBE-XXX code annotations (#15135)
* chore: clean up LOBE-XXX annotations from codebase comments

- Remove 【LOBE-XXX】 bracket markers
- Remove LOBE-XXXX references from inline comments
- Clean up test descriptions containing LOBE identifiers
- Preserve linear.app URLs and code-level regex patterns
- Generated: 2026-05-23 02:30:09

* 🐛 fix(tests): restore () in arrow callbacks broken by annotation cleanup

The LOBE-XXX annotation cleanup script over-matched `(LOBE-XXXX', () =>`
and stripped the callback `()`, leaving invalid syntax like
`describe(..., => {` and `it(..., async => {` across 24 test files.

This caused parse failures in Test Packages, Test Desktop App, Test
Database lint, and Test App shard runs. Restoring `()` / `async ()`
unblocks the suites while keeping the ticket-text cleanup intact.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* 🐛 fix(hintFormat-test): restore label + ellipsis in stripMarkdownLinks fixture

The annotation cleanup stripped `LOBE-8516` from a markdown-link's
*label* (`[LOBE-8516](/task/T-1)` → `[](/task/T-1)`), which then survived
`stripMarkdownLinks` because the pattern requires non-empty link text —
the test expected the link to disappear and asserted equality on a
LOBE-free output. The same line also lost a `.` from the trailing
`...` indicator in both input and expected strings.

Substitute a neutral Chinese label (`发布计划`) so the link continues
to exercise the multi-link substitution path, and restore the full
`...` ellipsis.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Arvin Xu <arvinxx@lobehub.com>
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-23 17:18:18 +08:00

30 lines
1.1 KiB
TypeScript

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 '{}';
};