Skip to content

Commit bc96abc

Browse files
authored
Merge pull request elizaOS#1501 from elizaOS/enhance/core
feat: extend parseBooleanFromText function with additional boolean values
2 parents 3d656ab + fc3521f commit bc96abc

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

packages/core/src/parsing.ts

+32-9
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,30 @@ export const parseShouldRespondFromText = (
3434

3535
export const booleanFooter = `Respond with only a YES or a NO.`;
3636

37+
/**
38+
* Parses a string to determine its boolean equivalent.
39+
*
40+
* Recognized affirmative values: "YES", "Y", "TRUE", "T", "1", "ON", "ENABLE".
41+
* Recognized negative values: "NO", "N", "FALSE", "F", "0", "OFF", "DISABLE".
42+
*
43+
* @param {string} text - The input text to parse.
44+
* @returns {boolean|null} - Returns `true` for affirmative inputs, `false` for negative inputs, and `null` for unrecognized inputs or null/undefined.
45+
*/
3746
export const parseBooleanFromText = (text: string) => {
38-
const match = text.match(/^(YES|NO)$/i);
39-
return match ? match[0].toUpperCase() === "YES" : null;
47+
if (!text) return null; // Handle null or undefined input
48+
49+
const affirmative = ["YES", "Y", "TRUE", "T", "1", "ON", "ENABLE"];
50+
const negative = ["NO", "N", "FALSE", "F", "0", "OFF", "DISABLE"];
51+
52+
const normalizedText = text.trim().toUpperCase();
53+
54+
if (affirmative.includes(normalizedText)) {
55+
return true;
56+
} else if (negative.includes(normalizedText)) {
57+
return false;
58+
}
59+
60+
return null; // Return null for unrecognized inputs
4061
};
4162

4263
export const stringArrayFooter = `Respond with a JSON array containing the values in a JSON block formatted for markdown with this structure:
@@ -150,12 +171,14 @@ export function parseJSONObjectFromText(
150171

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

153-
export const parseActionResponseFromText = (text: string): { actions: ActionResponse } => {
174+
export const parseActionResponseFromText = (
175+
text: string
176+
): { actions: ActionResponse } => {
154177
const actions: ActionResponse = {
155178
like: false,
156179
retweet: false,
157180
quote: false,
158-
reply: false
181+
reply: false,
159182
};
160183

161184
// Regex patterns
@@ -171,13 +194,13 @@ export const parseActionResponseFromText = (text: string): { actions: ActionResp
171194
actions.reply = replyPattern.test(text);
172195

173196
// Also do line by line parsing as backup
174-
const lines = text.split('\n');
197+
const lines = text.split("\n");
175198
for (const line of lines) {
176199
const trimmed = line.trim();
177-
if (trimmed === '[LIKE]') actions.like = true;
178-
if (trimmed === '[RETWEET]') actions.retweet = true;
179-
if (trimmed === '[QUOTE]') actions.quote = true;
180-
if (trimmed === '[REPLY]') actions.reply = true;
200+
if (trimmed === "[LIKE]") actions.like = true;
201+
if (trimmed === "[RETWEET]") actions.retweet = true;
202+
if (trimmed === "[QUOTE]") actions.quote = true;
203+
if (trimmed === "[REPLY]") actions.reply = true;
181204
}
182205

183206
return { actions };

0 commit comments

Comments
 (0)