Skip to content

Commit eb756bb

Browse files
committed
init
1 parent da74488 commit eb756bb

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/llm/io_processing/partial_json_builder.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,21 @@ Document PartialJsonBuilder::add(const std::string& chunk) {
185185
}
186186
} else {
187187
if (c == '"') {
188-
if (it != buffer.begin() && *(it - 1) == '\\') {
189-
continue;
188+
// Count consecutive backslashes before the quote to determine if the quote is escaped
189+
int backslashCount = 0;
190+
auto tmpIt = it;
191+
while (tmpIt != buffer.begin()) {
192+
--tmpIt;
193+
if (*tmpIt == '\\') {
194+
++backslashCount;
195+
} else {
196+
break;
197+
}
198+
}
199+
if (backslashCount % 2 == 1) {
200+
continue; // Quote is escaped, we can move on
190201
} else {
202+
// Quote is not escaped, so we close the string/key
191203
if (state == IteratorState::PROCESSING_KEY) {
192204
// We processed a key, now we expect a colon
193205
state = IteratorState::AWAITING_COLON;

src/test/llm/output_parsers/partial_json_builder_test.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,23 @@ TEST_F(PartialJsonBuilderTest, escapedCharactersSanityCheck) {
262262
ASSERT_EQ(parsedJson["arguments"].GetString(), std::string("{\"impl\": \"TYPE MQTT_Config AS\\n VAR\\n \\\"txt\\\" BrokerIP : STRING := '127.0.0.1';\"}"));
263263
}
264264

265+
TEST_F(PartialJsonBuilderTest, tmptest) {
266+
std::string targetJson = R"({"name": "set_status", "arguments": "{\"status\": \"CONNECTED,\\"})";
267+
PartialJsonBuilder builder;
268+
rapidjson::Document parsedJson;
269+
for (size_t i = 0; i < targetJson.size(); ++i) {
270+
std::string partialInput(1, targetJson[i]);
271+
parsedJson = builder.add(partialInput);
272+
}
273+
ASSERT_TRUE(parsedJson.IsObject());
274+
ASSERT_TRUE(parsedJson.HasMember("name"));
275+
ASSERT_TRUE(parsedJson["name"].IsString());
276+
ASSERT_EQ(parsedJson["name"].GetString(), std::string("set_status"));
277+
ASSERT_TRUE(parsedJson.HasMember("arguments"));
278+
ASSERT_TRUE(parsedJson["arguments"].IsString());
279+
ASSERT_EQ(parsedJson["arguments"].GetString(), std::string("{\"status\": \"CONNECTED,\\"));
280+
}
281+
265282
TEST_F(PartialJsonBuilderTest, complexJsonIncrementalParsingSanityCheck) {
266283
std::string targetJson = R"(
267284

0 commit comments

Comments
 (0)