From af7591b42b4418444b7e3b8f4c10bcf6a4e201d6 Mon Sep 17 00:00:00 2001 From: cygaar Date: Sat, 7 Dec 2024 16:51:27 -0500 Subject: [PATCH] fix: evaluation json parsing --- packages/client-twitter/src/interactions.ts | 2 +- packages/core/src/parsing.ts | 22 +++++++++++++-------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/packages/client-twitter/src/interactions.ts b/packages/client-twitter/src/interactions.ts index 0f1f1c7bd79..1eb93cf79e3 100644 --- a/packages/client-twitter/src/interactions.ts +++ b/packages/client-twitter/src/interactions.ts @@ -63,7 +63,7 @@ Response options are RESPOND, IGNORE and STOP . {{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. {{agentName}} is in a room with other users and wants to be conversational, but not annoying. -{{agentName}} should RESPOND to messages that are directed at them, or participate in conversations that are interesting or relevant to their background. +{{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. If a message is not interesting or relevant, {{agentName}} should IGNORE. Unless directly RESPONDing to a user, {{agentName}} should IGNORE messages that are very short or do not contain much information. If a user asks {{agentName}} to stop talking, {{agentName}} should STOP. diff --git a/packages/core/src/parsing.ts b/packages/core/src/parsing.ts index 72f59837b8d..3f7313f54f2 100644 --- a/packages/core/src/parsing.ts +++ b/packages/core/src/parsing.ts @@ -60,34 +60,40 @@ Your response must include the JSON block.`; export function parseJsonArrayFromText(text: string) { let jsonData = null; + // First try to parse with the original JSON format const jsonBlockMatch = text.match(jsonBlockPattern); if (jsonBlockMatch) { try { - jsonData = JSON.parse(jsonBlockMatch[1]); + // Replace single quotes with double quotes before parsing + const normalizedJson = jsonBlockMatch[1].replace(/'/g, '"'); + jsonData = JSON.parse(normalizedJson); } catch (e) { console.error("Error parsing JSON:", e); - return null; } - } else { - const arrayPattern = /\[\s*{[\s\S]*?}\s*\]/; + } + + // If that fails, try to find an array pattern + if (!jsonData) { + const arrayPattern = /\[\s*['"][^'"]*['"]\s*\]/; const arrayMatch = text.match(arrayPattern); if (arrayMatch) { try { - jsonData = JSON.parse(arrayMatch[0]); + // Replace single quotes with double quotes before parsing + const normalizedJson = arrayMatch[0].replace(/'/g, '"'); + jsonData = JSON.parse(normalizedJson); } catch (e) { console.error("Error parsing JSON:", e); - return null; } } } if (Array.isArray(jsonData)) { return jsonData; - } else { - return null; } + + return null; } /**