File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ // Discord tests cover error body summary behavior.
2+ import { describe , expect , it } from "vitest" ;
3+ import { summarizeDiscordResponseBody } from "./error-body.js" ;
4+
5+ function hasLoneSurrogate ( value : string ) : boolean {
6+ for ( let index = 0 ; index < value . length ; index += 1 ) {
7+ const code = value . charCodeAt ( index ) ;
8+ if ( code >= 0xd800 && code <= 0xdbff ) {
9+ const next = value . charCodeAt ( index + 1 ) ;
10+ if ( ! ( next >= 0xdc00 && next <= 0xdfff ) ) {
11+ return true ;
12+ }
13+ index += 1 ;
14+ continue ;
15+ }
16+ if ( code >= 0xdc00 && code <= 0xdfff ) {
17+ return true ;
18+ }
19+ }
20+ return false ;
21+ }
22+
23+ describe ( "summarizeDiscordResponseBody" , ( ) => {
24+ it ( "keeps truncated summaries on a UTF-16 boundary" , ( ) => {
25+ const summary = summarizeDiscordResponseBody ( `${ "a" . repeat ( 239 ) } 😀tail` ) ;
26+
27+ expect ( summary ) . toHaveLength ( 239 ) ;
28+ expect ( hasLoneSurrogate ( summary ?? "" ) ) . toBe ( false ) ;
29+ } ) ;
30+ } ) ;
Original file line number Diff line number Diff line change 11// Discord plugin module implements error body behavior.
2+ import { truncateUtf16Safe } from "openclaw/plugin-sdk/text-utility-runtime" ;
3+
24const DISCORD_RESPONSE_BODY_SUMMARY_MAX_CHARS = 240 ;
35
46export function summarizeDiscordResponseBody (
@@ -18,7 +20,7 @@ export function summarizeDiscordResponseBody(
1820 if ( ! summary ) {
1921 return opts . emptyText ;
2022 }
21- return summary . slice ( 0 , DISCORD_RESPONSE_BODY_SUMMARY_MAX_CHARS ) ;
23+ return truncateUtf16Safe ( summary , DISCORD_RESPONSE_BODY_SUMMARY_MAX_CHARS ) ;
2224}
2325
2426export function isDiscordHtmlResponseBody ( body : string , contentType ?: string | null ) : boolean {
You can’t perform that action at this time.
0 commit comments