A platform for community discussion. Free, open, simple.
FIX: Make sure we don't add parts of the same chunk twice when dealing with ParserErrors (#34143)
Roman Rizzi9a8e99c8f8e2
diff --git a/plugins/discourse-ai/lib/completions/json_streaming_tracker.rb b/plugins/discourse-ai/lib/completions/json_streaming_tracker.rbindex 26771e9f410..41378e4e7d3 100644--- a/plugins/discourse-ai/lib/completions/json_streaming_tracker.rb+++ b/plugins/discourse-ai/lib/completions/json_streaming_tracker.rb@@ -80,6 +80,13 @@ module DiscourseAiend# Escape the string as JSON and remove surrounding quotesescaped_json = raw_json.dump[1..-2]++ # 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,+ )+@parser << escaped_jsonrescue DiscourseAi::Completions::ParserError@broken = truediff --git a/plugins/discourse-ai/spec/lib/completions/endpoints/anthropic_spec.rb b/plugins/discourse-ai/spec/lib/completions/endpoints/anthropic_spec.rbindex ca0a01b0af2..1adf6b0cf3a 100644--- a/plugins/discourse-ai/spec/lib/completions/endpoints/anthropic_spec.rb+++ b/plugins/discourse-ai/spec/lib/completions/endpoints/anthropic_spec.rb@@ -903,10 +903,10 @@ RSpec.describe DiscourseAi::Completions::Endpoints::Anthropic dodata: {"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": "\\":\\""}}event: content_block_delta- data: {"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": "Hello!"}}+ data: {"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": "\\n there"}}+ data: {"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": " there"}}event: content_block_deltadata: {"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": "\\"}"}}