discourse-ai/lib/ai_bot/tools/create_artifact.rb
Sam 0d7f353284
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

138 lines
4.0 KiB
Ruby

# frozen_string_literal: true
module DiscourseAi
module AiBot
module Tools
class CreateArtifact < Tool
def self.name
"create_artifact"
end
def self.signature
{
name: "create_artifact",
description:
"Creates a web artifact with HTML, CSS, and JavaScript that can be displayed in an iframe",
parameters: [
{
name: "name",
description: "A name for the artifact (max 255 chars)",
type: "string",
required: true,
},
{
name: "html_body",
description: "The HTML content for the BODY tag (do not include the BODY tag)",
type: "string",
required: true,
},
{ name: "css", description: "Optional CSS styles for the artifact", type: "string" },
{
name: "js",
description:
"Optional
JavaScript code for the artifact",
type: "string",
},
],
}
end
def self.allow_partial_tool_calls?
true
end
def partial_invoke
@selected_tab = :html_body
if @prev_parameters
@selected_tab = parameters.keys.find { |k| @prev_parameters[k] != parameters[k] }
end
update_custom_html
@prev_parameters = parameters.dup
end
def invoke
yield parameters[:name] || "Web Artifact"
# Get the current post from context
post = Post.find_by(id: context[:post_id])
return error_response("No post context found") unless post
html = parameters[:html_body].to_s
css = parameters[:css].to_s
js = parameters[:js].to_s
# Create the artifact
artifact =
AiArtifact.new(
user_id: bot_user.id,
post_id: post.id,
name: parameters[:name].to_s[0...255],
html: html,
css: css,
js: js,
metadata: parameters[:metadata],
)
if artifact.save
update_custom_html(artifact)
success_response(artifact)
else
error_response(artifact.errors.full_messages.join(", "))
end
end
def chain_next_response?
@chain_next_response
end
private
def update_custom_html(artifact = nil)
html = parameters[:html_body].to_s
css = parameters[:css].to_s
js = parameters[:js].to_s
artifact_div =
"<div class=\"ai-artifact\" data-ai-artifact-id=#{artifact.id}></div>" if artifact
content = []
content << [:html_body, "### HTML\n\n```html\n#{html}\n```"] if html.present?
content << [:css, "### CSS\n\n```css\n#{css}\n```"] if css.present?
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
self.custom_raw = content.map { |c| c[1] }.join("\n\n")
end
def success_response(artifact)
@chain_next_response = false
{
status: "success",
artifact_id: artifact.id,
message: "Artifact created successfully and rendered to user.",
}
end
def error_response(message)
@chain_next_response = false
{ status: "error", error: message }
end
def help
"Creates a web artifact with HTML, CSS, and JavaScript that can be displayed in an iframe. " \
"Requires a name and HTML content. CSS and JavaScript are optional. " \
"The artifact will be associated with the current post and can be displayed using an iframe."
end
end
end
end
end