Skip to content

Commit 216e312

Browse files
authoredDec 7, 2024
Merge pull request #907 from cygaar/fix_evaulation_parsing
fix: evaluation json parsing
2 parents 7bd0892 + af7591b commit 216e312

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed
 

‎packages/client-twitter/src/interactions.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ Response options are RESPOND, IGNORE and STOP .
6363
{{agentName}} should respond to messages that are directed at them, or participate in conversations that are interesting or relevant to their background, IGNORE messages that are irrelevant to them, and should STOP if the conversation is concluded.
6464
6565
{{agentName}} is in a room with other users and wants to be conversational, but not annoying.
66-
{{agentName}} should RESPOND to messages that are directed at them, or participate in conversations that are interesting or relevant to their background.
66+
{{agentName}} must RESPOND to messages that are directed at them, a command towards them, or participate in conversations that are interesting or relevant to their background.
6767
If a message is not interesting or relevant, {{agentName}} should IGNORE.
6868
Unless directly RESPONDing to a user, {{agentName}} should IGNORE messages that are very short or do not contain much information.
6969
If a user asks {{agentName}} to stop talking, {{agentName}} should STOP.

‎packages/core/src/parsing.ts

+14-8
Original file line numberDiff line numberDiff line change
@@ -60,34 +60,40 @@ Your response must include the JSON block.`;
6060
export function parseJsonArrayFromText(text: string) {
6161
let jsonData = null;
6262

63+
// First try to parse with the original JSON format
6364
const jsonBlockMatch = text.match(jsonBlockPattern);
6465

6566
if (jsonBlockMatch) {
6667
try {
67-
jsonData = JSON.parse(jsonBlockMatch[1]);
68+
// Replace single quotes with double quotes before parsing
69+
const normalizedJson = jsonBlockMatch[1].replace(/'/g, '"');
70+
jsonData = JSON.parse(normalizedJson);
6871
} catch (e) {
6972
console.error("Error parsing JSON:", e);
70-
return null;
7173
}
72-
} else {
73-
const arrayPattern = /\[\s*{[\s\S]*?}\s*\]/;
74+
}
75+
76+
// If that fails, try to find an array pattern
77+
if (!jsonData) {
78+
const arrayPattern = /\[\s*['"][^'"]*['"]\s*\]/;
7479
const arrayMatch = text.match(arrayPattern);
7580

7681
if (arrayMatch) {
7782
try {
78-
jsonData = JSON.parse(arrayMatch[0]);
83+
// Replace single quotes with double quotes before parsing
84+
const normalizedJson = arrayMatch[0].replace(/'/g, '"');
85+
jsonData = JSON.parse(normalizedJson);
7986
} catch (e) {
8087
console.error("Error parsing JSON:", e);
81-
return null;
8288
}
8389
}
8490
}
8591

8692
if (Array.isArray(jsonData)) {
8793
return jsonData;
88-
} else {
89-
return null;
9094
}
95+
96+
return null;
9197
}
9298

9399
/**

0 commit comments

Comments
 (0)