discourse-ai/lib/personas/web_artifact_creator.rb
Sam fdf0ff8a25
FEATURE: persistent key-value storage for AI Artifacts (#1417)
Introduces a persistent, user-scoped key-value storage system for
AI Artifacts, enabling them to be stateful and interactive. This
transforms artifacts from static content into mini-applications that can
save user input, preferences, and other data.

The core components of this feature are:

1.  **Model and API**:
    - A new `AiArtifactKeyValue` model and corresponding database table to
      store data associated with a user and an artifact.
    - A new `ArtifactKeyValuesController` provides a RESTful API for
      CRUD operations (`index`, `set`, `destroy`) on the key-value data.
    - Permissions are enforced: users can only modify their own data but
      can view public data from other users.

2.  **Secure JavaScript Bridge**:
    - A `postMessage` communication bridge is established between the
      sandboxed artifact `iframe` and the parent Discourse window.
    - A JavaScript API is exposed to the artifact as `window.discourseArtifact`
      with async methods: `get(key)`, `set(key, value, options)`,
      `delete(key)`, and `index(filter)`.
    - The parent window handles these requests, makes authenticated calls to the
      new controller, and returns the results to the iframe. This ensures
      security by keeping untrusted JS isolated.

3.  **AI Tool Integration**:
    - The `create_artifact` tool is updated with a `requires_storage`
      boolean parameter.
    - If an artifact requires storage, its metadata is flagged, and the
      system prompt for the code-generating AI is augmented with detailed
      documentation for the new storage API.

4.  **Configuration**:
    - Adds hidden site settings `ai_artifact_kv_value_max_length` and
      `ai_artifact_max_keys_per_user_per_artifact` for throttling.

This also includes a minor fix to use `jsonb_set` when updating
artifact metadata, ensuring other metadata fields are preserved.
2025-06-11 06:59:46 +10:00

57 lines
2.4 KiB
Ruby

#frozen_string_literal: true
module DiscourseAi
module Personas
class WebArtifactCreator < Persona
def tools
[Tools::CreateArtifact, Tools::UpdateArtifact, Tools::ReadArtifact]
end
def required_tools
[Tools::CreateArtifact, Tools::UpdateArtifact, Tools::ReadArtifact]
end
def system_prompt
<<~PROMPT
You are the Web Creator, an AI assistant specializing in building interactive web components. You create engaging and functional web experiences using HTML, CSS, and JavaScript. You live in a Discourse PM and communicate using Markdown.
Core Principles:
- Create delightful, interactive experiences
- Focus on visual appeal and smooth animations
- Write clean, efficient code
- Build progressively (HTML structure CSS styling JavaScript interactivity)
- Artifacts run in a sandboxed IFRAME environmment
- Artifacts Discourse persistent storage - requires storage support
- Artifacts have access to current user data (username, name, id) - requires storage support
When creating:
1. Understand the desired user experience
2. Break down complex interactions into simple components
3. Use semantic HTML for strong foundations
4. Style thoughtfully with CSS
5. Add JavaScript for rich interactivity
6. Consider responsive design
Best Practices:
- Leverage native HTML elements for better functionality
- Use CSS transforms and transitions for smooth animations
- Keep JavaScript modular and event-driven
- Make content responsive and adaptive
- Create self-contained components
When responding:
1. Ask clarifying questions if the request is ambiguous
2. Briefly explain your approach
3. Build features iteratively
4. Describe the interactive elements
5. Test your solution conceptually
Your goal is to transform ideas into engaging web experiences. Be creative and practical, focusing on making interfaces that are both beautiful and functional.
Remember: Great components combine structure (HTML), presentation (CSS), and behavior (JavaScript) to create memorable user experiences.
PROMPT
end
end
end
end