Skip to content

Commit 10fa7eb

Browse files
committed
extract the text attribute if json parsing failed
1 parent 3962329 commit 10fa7eb

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

packages/client-twitter/src/post.ts

+26-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
type UUID,
1111
truncateToCompleteSentence,
1212
parseJSONObjectFromText,
13+
extractAttributes,
1314
} from "@elizaos/core";
1415
import { elizaLogger } from "@elizaos/core";
1516
import type { ClientBase } from "./base.ts";
@@ -463,6 +464,14 @@ export class TwitterPostClient {
463464
}
464465
}
465466

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+
466475
cleanJsonResponse(response: string): string {
467476
return response
468477
.replace(/```json\s*/g, "") // Remove ```json
@@ -548,6 +557,12 @@ export class TwitterPostClient {
548557
.trim();
549558
}
550559

560+
if (!cleanedContent) {
561+
cleanedContent = extractAttributes(newTweetContent, [
562+
"text",
563+
]).text;
564+
}
565+
551566
if (!cleanedContent) {
552567
elizaLogger.error(
553568
"Failed to extract valid content from response:",
@@ -672,11 +687,17 @@ export class TwitterPostClient {
672687
response,
673688
);
674689
}
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+
680701
return truncateContent;
681702
}
682703

packages/core/src/parsing.ts

+24
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,30 @@ export function parseJSONObjectFromText(
173173
}
174174
}
175175

176+
/**
177+
* Extracts specific attributes (e.g., user, text, action) from a JSON-like string using regex.
178+
* @param response - The cleaned string response to extract attributes from.
179+
* @param attributesToExtract - An array of attribute names to extract.
180+
* @returns An object containing the extracted attributes.
181+
*/
182+
export function extractAttributes(
183+
response: string,
184+
attributesToExtract: string[],
185+
): { [key: string]: string | undefined } {
186+
const attributes: { [key: string]: string | undefined } = {};
187+
188+
attributesToExtract.forEach((attribute) => {
189+
const match = response.match(
190+
new RegExp(`"${attribute}"\\s*:\\s*"([^"]*)"`, "i"),
191+
);
192+
if (match) {
193+
attributes[attribute] = match[1];
194+
}
195+
});
196+
197+
return attributes;
198+
}
199+
176200
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.`;
177201

178202
export const parseActionResponseFromText = (

0 commit comments

Comments
 (0)