Skip to content

Commit 6490485

Browse files
authored
Merge pull request #2732 from elizaOS/tcm-improve-twitter-post
feat: move cleanJsonResponse to parsing
2 parents 9faad38 + f6e0d3d commit 6490485

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

packages/client-twitter/src/post.ts

+3-18
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
truncateToCompleteSentence,
1212
parseJSONObjectFromText,
1313
extractAttributes,
14+
cleanJsonResponse,
1415
} from "@elizaos/core";
1516
import { elizaLogger } from "@elizaos/core";
1617
import type { ClientBase } from "./base.ts";
@@ -464,22 +465,6 @@ export class TwitterPostClient {
464465
}
465466
}
466467

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-
475-
cleanJsonResponse(response: string): string {
476-
return response
477-
.replace(/```json\s*/g, "") // Remove ```json
478-
.replace(/```\s*/g, "") // Remove any remaining ```
479-
.replace(/(\r\n|\n|\r)/g, "") // Remove line breaks
480-
.trim();
481-
}
482-
483468
/**
484469
* Generates and posts a new tweet. If isDryRun is true, only logs what would have been posted.
485470
*/
@@ -529,7 +514,7 @@ export class TwitterPostClient {
529514
modelClass: ModelClass.SMALL,
530515
});
531516

532-
const newTweetContent = this.cleanJsonResponse(response);
517+
const newTweetContent = cleanJsonResponse(response);
533518

534519
// First attempt to clean content
535520
let cleanedContent = "";
@@ -654,7 +639,7 @@ export class TwitterPostClient {
654639
elizaLogger.log("generate tweet content response:\n" + response);
655640

656641
// First clean up any markdown and newlines
657-
const cleanedResponse = this.cleanJsonResponse(response);
642+
const cleanedResponse = cleanJsonResponse(response);
658643

659644
// Try to parse as JSON first
660645
try {

packages/core/src/parsing.ts

+16
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,22 @@ export function extractAttributes(
197197
return attributes;
198198
}
199199

200+
/**
201+
* Cleans a JSON-like response string by removing unnecessary markers, line breaks, and extra whitespace.
202+
* This is useful for handling improperly formatted JSON responses from external sources.
203+
*
204+
* @param response - The raw JSON-like string response to clean.
205+
* @returns The cleaned string, ready for parsing or further processing.
206+
*/
207+
208+
export function cleanJsonResponse(response: string): string {
209+
return response
210+
.replace(/```json\s*/g, "") // Remove ```json
211+
.replace(/```\s*/g, "") // Remove any remaining ```
212+
.replace(/(\r\n|\n|\r)/g, "") // Remove line breaks
213+
.trim();
214+
}
215+
200216
export const postActionResponseFooter = `Choose any combination of [LIKE], [RETWEET], [QUOTE], and [REPLY] that are appropriate. Each action must be on its own line. Your response must only include the chosen actions.`;
201217

202218
export const parseActionResponseFromText = (

0 commit comments

Comments
 (0)