Skip to content

Commit 2e292ec

Browse files
committed
fix(agents): truncate assistant error text fallback on UTF-16 boundary
Use truncateUtf16Safe instead of String.prototype.slice(0,600) in formatAssistantErrorText and its companion comparison in isRawAssistantErrorPassthrough, preventing dangling surrogates when non-BMP characters straddle the 600-code-unit truncation boundary.
1 parent c004a62 commit 2e292ec

1 file changed

Lines changed: 3 additions & 2 deletions

File tree

src/agents/embedded-agent-helpers/errors.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
isGenericProviderInternalError,
1515
parseApiErrorInfo,
1616
} from "../../shared/assistant-error-format.js";
17+
import { truncateUtf16Safe } from "../../shared/utf16-slice.js";
1718
export {
1819
extractLeadingHttpStatus,
1920
formatRawAssistantErrorForUi,
@@ -1540,7 +1541,7 @@ export function formatAssistantErrorText(
15401541
if (raw.length > 600) {
15411542
log.warn(`Long error truncated: ${raw.slice(0, 200)}`);
15421543
}
1543-
return raw.length > 600 ? `${raw.slice(0, 600)}…` : raw;
1544+
return raw.length > 600 ? `${truncateUtf16Safe(raw, 600)}…` : raw;
15441545
}
15451546

15461547
export function isRawAssistantErrorPassthrough(params: {
@@ -1560,7 +1561,7 @@ export function isRawAssistantErrorPassthrough(params: {
15601561
friendlyError.startsWith("HTTP ");
15611562
return (
15621563
friendlyError === rawError ||
1563-
(rawError.length > 600 && friendlyError === `${rawError.slice(0, 600)}…`) ||
1564+
(rawError.length > 600 && friendlyError === `${truncateUtf16Safe(rawError, 600)}…`) ||
15641565
Boolean(parsedMessage && hasRawDerivedProviderPrefix) ||
15651566
Boolean(leadingStatusRest && friendlyError.startsWith("HTTP "))
15661567
);

0 commit comments

Comments
 (0)