This repository was archived by the owner on Jul 22, 2025. It is now read-only.
generated from discourse/discourse-plugin-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 41
FIX: make AI helper more robust #1484
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,10 @@ def <<(raw) | |
| @partial_json_tracker << raw | ||
| end | ||
|
|
||
| def broken? | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need for this if we move the best effort parser inside this object. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. actually we still need it cause on finish when streaming we need to double check. |
||
| @partial_json_tracker.broken? | ||
| end | ||
|
|
||
| def read_buffered_property(prop_name) | ||
| # Safeguard: If the model is misbehaving and generating something that's not a JSON, | ||
| # treat response as a normal string. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "json" | ||
|
|
||
| module DiscourseAi | ||
| module Utils | ||
| class BestEffortJsonParser | ||
| class << self | ||
| def extract_key(helper_response, schema_type, schema_key) | ||
| return helper_response unless helper_response.is_a?(String) | ||
|
|
||
| schema_type = schema_type.to_sym | ||
| schema_key = schema_key&.to_sym | ||
| cleaned = remove_markdown_fences(helper_response.strip) | ||
|
|
||
| parsed = | ||
| try_parse(cleaned) || try_parse(fix_common_issues(cleaned)) || | ||
| manual_extract(cleaned, schema_key, schema_type) | ||
|
|
||
| value = parsed.is_a?(Hash) ? parsed[schema_key.to_s] : parsed | ||
| parsed = cast_value(value, schema_type) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def remove_markdown_fences(text) | ||
| return text unless text.match?(/^```(?:json)?\s*\n/i) | ||
|
|
||
| text.gsub(/^```(?:json)?\s*\n/i, "").gsub(/\n```\s*$/, "") | ||
| end | ||
|
|
||
| def fix_common_issues(text) | ||
| text.gsub(/(\w+):/, '"\1":').gsub(/'/, "\"") | ||
| end | ||
|
|
||
| def try_parse(text) | ||
| JSON.parse(text) | ||
| rescue JSON::ParserError | ||
| nil | ||
| end | ||
|
|
||
| def manual_extract(text, key, schema_type) | ||
| return default_for(schema_type) unless key | ||
|
|
||
| case schema_type | ||
| when :object | ||
| extract_object(text, key.to_s) | ||
| when :array, :string | ||
| extract_scalar(text, key.to_s, schema_type) | ||
| else | ||
| default_for(schema_type) | ||
| end | ||
| end | ||
|
|
||
| def extract_scalar(text, key, schema_type) | ||
| patterns = | ||
| if schema_type == :array | ||
| [ | ||
| /"#{key}"\s*:\s*\[([^\]]+)\]/, | ||
| /'#{key}'\s*:\s*\[([^\]]+)\]/, | ||
| /#{key}\s*:\s*\[([^\]]+)\]/, | ||
| ] | ||
| else | ||
| [ | ||
| /"#{key}"\s*:\s*"([^"]+)"/, | ||
| /'#{key}'\s*:\s*'([^']+)'/, | ||
| /#{key}\s*:\s*"([^"]+)"/, | ||
| /#{key}\s*:\s*'([^']+)'/, | ||
| ] | ||
| end | ||
|
|
||
| patterns.each do |pattern| | ||
| match = text.match(pattern) | ||
| next unless match | ||
|
|
||
| value = match[1] | ||
| return schema_type == :array ? parse_array(value) : value | ||
| end | ||
|
|
||
| default_for(schema_type) | ||
| end | ||
|
|
||
| def parse_array(value) | ||
| JSON.parse("[#{value}]") | ||
| rescue JSON::ParserError | ||
| value.split(",").map { |item| item.strip.gsub(/^['"]|['"]$/, "") } | ||
| end | ||
|
|
||
| def extract_object(text, key) | ||
| pattern = /("#{key}"|'#{key}'|#{key})\s*:\s*\{/ | ||
| match = text.match(pattern) or return {} | ||
|
|
||
| start = match.end(0) - 1 | ||
| return {} unless text[start] == "{" | ||
|
|
||
| end_pos = find_matching_brace(text, start) | ||
| return {} unless end_pos | ||
|
|
||
| obj_str = text[start..end_pos] | ||
| try_parse(obj_str) || try_parse(fix_common_issues(obj_str)) || {} | ||
| end | ||
|
|
||
| def find_matching_brace(text, start_pos) | ||
| brace_count = 0 | ||
|
|
||
| text[start_pos..-1].each_char.with_index do |char, idx| | ||
| brace_count += 1 if char == "{" | ||
| if char == "}" | ||
| brace_count -= 1 | ||
| return start_pos + idx if brace_count.zero? | ||
| end | ||
| end | ||
| nil | ||
| end | ||
|
|
||
| def cast_value(value, schema_type) | ||
| case schema_type | ||
| when :array | ||
| value.is_a?(Array) ? value : [] | ||
| when :object | ||
| value.is_a?(Hash) ? value : {} | ||
| else | ||
| value.to_s | ||
| end | ||
| end | ||
|
|
||
| def default_for(schema_type) | ||
| schema_type == :array ? [] : schema_type == :object ? {} : "" | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great! I suggest we move it inside
#read_buffered_property.