discourse-ai/lib/completions/json_streaming_tracker.rb

90 lines
2.6 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
module DiscourseAi
module Completions
class JsonStreamingTracker
attr_reader :current_key, :current_value, :stream_consumer
def initialize(stream_consumer)
@stream_consumer = stream_consumer
@current_key = nil
@current_value = nil
@tracking_array = false
@parser = DiscourseAi::Completions::JsonStreamingParser.new
@parser.key do |k|
@current_key = k
@current_value = nil
end
@parser.value do |value|
FEATURE: AI artifacts (#898) This is a significant PR that introduces AI Artifacts functionality to the discourse-ai plugin along with several other improvements. Here are the key changes: 1. AI Artifacts System: - Adds a new `AiArtifact` model and database migration - Allows creation of web artifacts with HTML, CSS, and JavaScript content - Introduces security settings (`strict`, `lax`, `disabled`) for controlling artifact execution - Implements artifact rendering in iframes with sandbox protection - New `CreateArtifact` tool for AI to generate interactive content 2. Tool System Improvements: - Adds support for partial tool calls, allowing incremental updates during generation - Better handling of tool call states and progress tracking - Improved XML tool processing with CDATA support - Fixes for tool parameter handling and duplicate invocations 3. LLM Provider Updates: - Updates for Anthropic Claude models with correct token limits - Adds support for native/XML tool modes in Gemini integration - Adds new model configurations including Llama 3.1 models - Improvements to streaming response handling 4. UI Enhancements: - New artifact viewer component with expand/collapse functionality - Security controls for artifact execution (click-to-run in strict mode) - Improved dialog and response handling - Better error management for tool execution 5. Security Improvements: - Sandbox controls for artifact execution - Public/private artifact sharing controls - Security settings to control artifact behavior - CSP and frame-options handling for artifacts 6. Technical Improvements: - Better post streaming implementation - Improved error handling in completions - Better memory management for partial tool calls - Enhanced testing coverage 7. Configuration: - New site settings for artifact security - Extended LLM model configurations - Additional tool configuration options This PR significantly enhances the plugin's capabilities for generating and displaying interactive content while maintaining security and providing flexible configuration options for administrators.
2024-11-19 09:22:39 +11:00
if @current_key
if @tracking_array
@current_value << value
stream_consumer.notify_progress(@current_key, @current_value)
else
stream_consumer.notify_progress(@current_key, value)
@current_key = nil
end
FEATURE: AI artifacts (#898) This is a significant PR that introduces AI Artifacts functionality to the discourse-ai plugin along with several other improvements. Here are the key changes: 1. AI Artifacts System: - Adds a new `AiArtifact` model and database migration - Allows creation of web artifacts with HTML, CSS, and JavaScript content - Introduces security settings (`strict`, `lax`, `disabled`) for controlling artifact execution - Implements artifact rendering in iframes with sandbox protection - New `CreateArtifact` tool for AI to generate interactive content 2. Tool System Improvements: - Adds support for partial tool calls, allowing incremental updates during generation - Better handling of tool call states and progress tracking - Improved XML tool processing with CDATA support - Fixes for tool parameter handling and duplicate invocations 3. LLM Provider Updates: - Updates for Anthropic Claude models with correct token limits - Adds support for native/XML tool modes in Gemini integration - Adds new model configurations including Llama 3.1 models - Improvements to streaming response handling 4. UI Enhancements: - New artifact viewer component with expand/collapse functionality - Security controls for artifact execution (click-to-run in strict mode) - Improved dialog and response handling - Better error management for tool execution 5. Security Improvements: - Sandbox controls for artifact execution - Public/private artifact sharing controls - Security settings to control artifact behavior - CSP and frame-options handling for artifacts 6. Technical Improvements: - Better post streaming implementation - Improved error handling in completions - Better memory management for partial tool calls - Enhanced testing coverage 7. Configuration: - New site settings for artifact security - Extended LLM model configurations - Additional tool configuration options This PR significantly enhances the plugin's capabilities for generating and displaying interactive content while maintaining security and providing flexible configuration options for administrators.
2024-11-19 09:22:39 +11:00
end
end
@parser.start_array do
@tracking_array = true
@current_value = []
end
@parser.end_array do
@tracking_array = false
@current_key = nil
@current_value = nil
end
end
def broken?
@broken
end
def <<(raw_json)
# llm could send broken json
# in that case just deal with it later
# don't stream
return if @broken
begin
@parser << raw_json
rescue 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)
return if @broken
end
if @parser.state == :start_string && @current_key
buffered = @tracking_array ? [@parser.buf] : @parser.buf
# this is is worth notifying
stream_consumer.notify_progress(@current_key, buffered)
end
@current_key = nil if @parser.state == :end_value
end
private
def try_escape_and_parse(raw_json)
if !raw_json.is_a?(String)
@broken = true
return
end
# Escape the string as JSON and remove surrounding quotes
escaped_json = raw_json.dump[1..-2]
@parser << escaped_json
rescue DiscourseAi::Completions::ParserError
@broken = true
end
end
end
end