A platform for community discussion. Free, open, simple.
FIX: Assume buffer also carries stuff from previous chunks, not just the last one (#34230)
Roman Rizzif2d6c8a8c822
diff --git a/plugins/discourse-ai/lib/completions/json_streaming_tracker.rb b/plugins/discourse-ai/lib/completions/json_streaming_tracker.rbindex 41378e4e7d3..b4e75c08db9 100644--- a/plugins/discourse-ai/lib/completions/json_streaming_tracker.rb+++ b/plugins/discourse-ai/lib/completions/json_streaming_tracker.rb@@ -52,13 +52,14 @@ module DiscourseAireturn if @brokenbegin+ pre_append_buffer = @parser.buf.dup@parser << raw_jsonrescue DiscourseAi::Completions::ParserError# Note: We're parsing JSON content that was itself embedded as a string inside another JSON object.# During the outer JSON.parse, any escaped control characters (like "\\n") are unescaped to real characters ("\n"),# which corrupts the inner JSON structure when passed to the parser here.# To handle this, we retry parsing with the string JSON-escaped again (`.dump[1..-2]`) if the first attempt fails.- try_escape_and_parse(raw_json)+ try_escape_and_parse(raw_json, pre_append_buffer)return if @brokenend@@ -73,7 +74,7 @@ module DiscourseAiprivate- def try_escape_and_parse(raw_json)+ def try_escape_and_parse(raw_json, pre_append_buffer)if !raw_json.is_a?(String)@broken = truereturn@@ -83,9 +84,9 @@ module DiscourseAi# Assume we could have already processed some of the chunk which was stored in the parser's buffer.already_processed_chunk = @parser.buf- escaped_json = escaped_json[already_processed_chunk.length..] if escaped_json.start_with?(- already_processed_chunk,- )+ last_seen_char_idx = already_processed_chunk.length - pre_append_buffer.length++ escaped_json = escaped_json[last_seen_char_idx..]@parser << escaped_jsonrescue DiscourseAi::Completions::ParserErrordiff --git a/plugins/discourse-ai/spec/lib/completions/endpoints/anthropic_spec.rb b/plugins/discourse-ai/spec/lib/completions/endpoints/anthropic_spec.rbindex 1adf6b0cf3a..7d9339e5815 100644--- a/plugins/discourse-ai/spec/lib/completions/endpoints/anthropic_spec.rb+++ b/plugins/discourse-ai/spec/lib/completions/endpoints/anthropic_spec.rb@@ -906,7 +906,16 @@ RSpec.describe DiscourseAi::Completions::Endpoints::Anthropic dodata: {"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": "Hello!\\n"}}event: content_block_delta- data: {"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": " there"}}+ data: {"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": " there\\nis a text"}}++ event: content_block_delta+ data: {"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": " and\\n\\nmore text"}}++ event: content_block_delta+ data: {"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": "\\n \\n and\\n\\n more much more"}}++ event: content_block_delta+ data: {"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": " text"}}event: content_block_deltadata: {"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": "\\"}"}}@@ -944,7 +953,9 @@ RSpec.describe DiscourseAi::Completions::Endpoints::Anthropic doresponse_format: schema,) { |partial, cancel| structured_output = partial }- expect(structured_output.read_buffered_property(:key)).to eq("Hello!\n there")+ expect(structured_output.read_buffered_property(:key)).to eq(+ "Hello!\n there\nis a text and\n\nmore text\n \n and\n\n more much more text",+ )expected_body = {model: "claude-3-opus-20240229",