Skip to content

Commit

Permalink
Merge pull request #2732 from elizaOS/tcm-improve-twitter-post
Browse files Browse the repository at this point in the history
feat: move cleanJsonResponse to parsing
  • Loading branch information
tcm390 authored Jan 24, 2025
2 parents 9faad38 + f6e0d3d commit 6490485
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
21 changes: 3 additions & 18 deletions packages/client-twitter/src/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
truncateToCompleteSentence,
parseJSONObjectFromText,
extractAttributes,
cleanJsonResponse,
} from "@elizaos/core";
import { elizaLogger } from "@elizaos/core";
import type { ClientBase } from "./base.ts";
Expand Down Expand Up @@ -464,22 +465,6 @@ export class TwitterPostClient {
}
}

/**
* Cleans a JSON-like response string by removing unnecessary markers, line breaks, and extra whitespace.
* This is useful for handling improperly formatted JSON responses from external sources.
*
* @param response - The raw JSON-like string response to clean.
* @returns The cleaned string, ready for parsing or further processing.
*/

cleanJsonResponse(response: string): string {
return response
.replace(/```json\s*/g, "") // Remove ```json
.replace(/```\s*/g, "") // Remove any remaining ```
.replace(/(\r\n|\n|\r)/g, "") // Remove line breaks
.trim();
}

/**
* Generates and posts a new tweet. If isDryRun is true, only logs what would have been posted.
*/
Expand Down Expand Up @@ -529,7 +514,7 @@ export class TwitterPostClient {
modelClass: ModelClass.SMALL,
});

const newTweetContent = this.cleanJsonResponse(response);
const newTweetContent = cleanJsonResponse(response);

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

// First clean up any markdown and newlines
const cleanedResponse = this.cleanJsonResponse(response);
const cleanedResponse = cleanJsonResponse(response);

// Try to parse as JSON first
try {
Expand Down
16 changes: 16 additions & 0 deletions packages/core/src/parsing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,22 @@ export function extractAttributes(
return attributes;
}

/**
* Cleans a JSON-like response string by removing unnecessary markers, line breaks, and extra whitespace.
* This is useful for handling improperly formatted JSON responses from external sources.
*
* @param response - The raw JSON-like string response to clean.
* @returns The cleaned string, ready for parsing or further processing.
*/

export function cleanJsonResponse(response: string): string {
return response
.replace(/```json\s*/g, "") // Remove ```json
.replace(/```\s*/g, "") // Remove any remaining ```
.replace(/(\r\n|\n|\r)/g, "") // Remove line breaks
.trim();
}

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.`;

export const parseActionResponseFromText = (
Expand Down

0 comments on commit 6490485

Please sign in to comment.