Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit cc74f23

Browse files
committed
improve error handling
1 parent 65d1557 commit cc74f23

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

lib/ai_bot/tools/read_artifact.rb

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,13 @@ def invoke
4040
end
4141

4242
def chain_next_response?
43-
false
43+
@chain_next_response
4444
end
4545

4646
private
4747

4848
def error_response(message)
49+
@chain_next_response = true
4950
{ status: "error", error: message }
5051
end
5152

@@ -105,17 +106,19 @@ def handle_discourse_artifact(uri)
105106

106107
def extract_discourse_artifact(html)
107108
doc = Nokogiri.HTML(html)
108-
iframe = doc.at_css(".ai-artifact iframe")
109+
iframe = doc.at_css("body > iframe")
109110
return nil unless iframe
110111

111-
response = fetch_page(URI.parse(iframe["src"]))
112-
return nil unless response
112+
# parse srcdoc attribute of iframe
113+
iframe_doc = Nokogiri.HTML(iframe["srcdoc"])
114+
return nil unless iframe_doc
113115

114-
iframe_doc = Nokogiri.HTML(response.body)
115-
116-
content = iframe_doc.at_css("#content")&.inner_html.to_s[0...MAX_HTML_SIZE]
116+
body = iframe_doc.at_css("body")
117+
last_script_tag = body&.at_css("script:last-of-type")
118+
script = last_script_tag&.content.to_s[0...MAX_HTML_SIZE]
119+
last_script_tag.remove if last_script_tag
120+
content = body&.inner_html.to_s[0...MAX_HTML_SIZE]
117121
style = iframe_doc.at_css("style")&.content.to_s[0...MAX_HTML_SIZE]
118-
script = iframe_doc.at_css("script:not([src])")&.content.to_s[0...MAX_HTML_SIZE]
119122

120123
[content, style, script]
121124
end

lib/ai_bot/tools/update_artifact.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ def invoke
148148
)
149149

150150
begin
151+
instructions = parameters[:instructions]
151152
partial_response = +""
152153
new_version =
153154
strategy
@@ -157,11 +158,11 @@ def invoke
157158
user: post.user,
158159
artifact: artifact,
159160
artifact_version: artifact_version,
160-
instructions: parameters[:instructions],
161+
instructions: instructions,
161162
)
162163
.apply do |progress|
163164
partial_response << progress
164-
in_progress(instructions: parameters[:instructions], source: partial_response)
165+
in_progress(instructions: instructions, source: partial_response)
165166
# force in progress to render
166167
yield nil, true
167168
end

0 commit comments

Comments
 (0)