@@ -10,6 +10,7 @@ import {
10
10
type UUID ,
11
11
truncateToCompleteSentence ,
12
12
parseJSONObjectFromText ,
13
+ extractAttributes ,
13
14
} from "@elizaos/core" ;
14
15
import { elizaLogger } from "@elizaos/core" ;
15
16
import type { ClientBase } from "./base.ts" ;
@@ -463,6 +464,14 @@ export class TwitterPostClient {
463
464
}
464
465
}
465
466
467
+ /**
468
+ * Cleans a JSON-like response string by removing unnecessary markers, line breaks, and extra whitespace.
469
+ * This is useful for handling improperly formatted JSON responses from external sources.
470
+ *
471
+ * @param response - The raw JSON-like string response to clean.
472
+ * @returns The cleaned string, ready for parsing or further processing.
473
+ */
474
+
466
475
cleanJsonResponse ( response : string ) : string {
467
476
return response
468
477
. replace ( / ` ` ` j s o n \s * / g, "" ) // Remove ```json
@@ -548,6 +557,12 @@ export class TwitterPostClient {
548
557
. trim ( ) ;
549
558
}
550
559
560
+ if ( ! cleanedContent ) {
561
+ cleanedContent = extractAttributes ( newTweetContent , [
562
+ "text" ,
563
+ ] ) . text ;
564
+ }
565
+
551
566
if ( ! cleanedContent ) {
552
567
elizaLogger . error (
553
568
"Failed to extract valid content from response:" ,
@@ -672,11 +687,17 @@ export class TwitterPostClient {
672
687
response ,
673
688
) ;
674
689
}
675
- // If not JSON or no valid content found, clean the raw text
676
- const truncateContent = truncateToCompleteSentence (
677
- cleanedResponse ,
678
- this . client . twitterConfig . MAX_TWEET_LENGTH ,
679
- ) ;
690
+
691
+ let truncateContent = extractAttributes ( cleanedResponse , [ "text" ] ) . text ;
692
+
693
+ if ( ! truncateContent ) {
694
+ // If not JSON or no valid content found, clean the raw text
695
+ truncateContent = truncateToCompleteSentence (
696
+ cleanedResponse ,
697
+ this . client . twitterConfig . MAX_TWEET_LENGTH ,
698
+ ) ;
699
+ }
700
+
680
701
return truncateContent ;
681
702
}
682
703
0 commit comments