@@ -34,9 +34,30 @@ export const parseShouldRespondFromText = (
34
34
35
35
export const booleanFooter = `Respond with only a YES or a NO.` ;
36
36
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
+ */
37
46
export const parseBooleanFromText = ( text : string ) => {
38
- const match = text . match ( / ^ ( Y E S | N O ) $ / 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
40
61
} ;
41
62
42
63
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(
150
171
151
172
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.` ;
152
173
153
- export const parseActionResponseFromText = ( text : string ) : { actions : ActionResponse } => {
174
+ export const parseActionResponseFromText = (
175
+ text : string
176
+ ) : { actions : ActionResponse } => {
154
177
const actions : ActionResponse = {
155
178
like : false ,
156
179
retweet : false ,
157
180
quote : false ,
158
- reply : false
181
+ reply : false ,
159
182
} ;
160
183
161
184
// Regex patterns
@@ -171,13 +194,13 @@ export const parseActionResponseFromText = (text: string): { actions: ActionResp
171
194
actions . reply = replyPattern . test ( text ) ;
172
195
173
196
// Also do line by line parsing as backup
174
- const lines = text . split ( '\n' ) ;
197
+ const lines = text . split ( "\n" ) ;
175
198
for ( const line of lines ) {
176
199
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 ;
181
204
}
182
205
183
206
return { actions } ;
0 commit comments