Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def show

response.headers.delete("X-Frame-Options")
response.headers["Content-Security-Policy"] = "script-src 'unsafe-inline';"
response.headers["X-Robots-Tag"] = "noindex"

# Render the content
render html: trusted_html.html_safe, layout: false, content_type: "text/html"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<meta name="twitter:title" content="<%= I18n.t("discourse_ai.share_ai.title", title: @shared_conversation.title, site_name: SiteSetting.title) %>">
<meta name="twitter:description" content="<%= @shared_conversation.formatted_excerpt %>">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="<%= ::UrlHelper.absolute("/plugins/discourse-ai/ai-share/share.css") %>">
<link rel="stylesheet" href="<%= ::UrlHelper.absolute("/plugins/discourse-ai/ai-share/share.css?v=1") %>">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh interesting, v=1?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

old skool cache breaking

</head>
<body>
<header class="site-header">
Expand Down
1 change: 1 addition & 0 deletions config/locales/server.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ en:
discourse_ai:
ai_artifact:
link: "Show Artifact in new tab"
view_source: "View Source"
unknown_model: "Unknown AI model"

tools:
Expand Down
10 changes: 7 additions & 3 deletions lib/ai_bot/tools/create_artifact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def update_custom_html(artifact = nil)
js = parameters[:js].to_s

artifact_div =
"<div class=\"ai-artifact\" data-ai-artifact-id=#{artifact.id}></div>" if artifact
"<div class=\"ai-artifact\" data-ai-artifact-id=\"#{artifact.id}\"></div>" if artifact

content = []

Expand All @@ -103,10 +103,14 @@ def update_custom_html(artifact = nil)

content << [:js, "### JavaScript\n\n```javascript\n#{js}\n```"] if js.present?

content << [:preview, "### Preview\n\n#{artifact_div}"] if artifact_div

content.sort_by! { |c| c[0] === @selected_tab ? 1 : 0 } if !artifact

if artifact
content.unshift([nil, "[details='#{I18n.t("discourse_ai.ai_artifact.view_source")}']"])
content << [nil, "[/details]"]
end

content << [:preview, "### Preview\n\n#{artifact_div}"] if artifact_div
self.custom_raw = content.map { |c| c[1] }.join("\n\n")
end

Expand Down
36 changes: 36 additions & 0 deletions spec/lib/modules/ai_bot/tools/create_artifact_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,46 @@
fab!(:llm_model)
let(:bot_user) { DiscourseAi::AiBot::EntryPoint.find_user_from_model(llm_model.name) }
let(:llm) { DiscourseAi::Completions::Llm.proxy("custom:#{llm_model.id}") }
fab!(:post)

before { SiteSetting.ai_bot_enabled = true }

describe "#process" do
it "correctly adds details block on final invoke" do
tool =
described_class.new(
{ html_body: "hello" },
bot_user: Fabricate(:user),
llm: llm,
context: {
post_id: post.id,
},
)

tool.parameters = { html_body: "hello" }

tool.invoke {}

artifact_id = AiArtifact.order("id desc").limit(1).pluck(:id).first

expected = <<~MD
[details='View Source']

### HTML

```html
hello
```

[/details]

### Preview

<div class="ai-artifact" data-ai-artifact-id="#{artifact_id}"></div>
MD
expect(tool.custom_raw.strip).to eq(expected.strip)
end

it "can correctly handle partial updates" do
tool = described_class.new({}, bot_user: bot_user, llm: llm)

Expand Down
3 changes: 2 additions & 1 deletion spec/requests/ai_bot/artifacts_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,12 @@ def parse_srcdoc(html)
end
end

it "removes security headers" do
it "removes security headers and disables crawling" do
sign_in(user)
get "/discourse-ai/ai-bot/artifacts/#{artifact.id}"
expect(response.headers["X-Frame-Options"]).to eq(nil)
expect(response.headers["Content-Security-Policy"]).to eq("script-src 'unsafe-inline';")
expect(response.headers["X-Robots-Tag"]).to eq("noindex")
end
end
end