This document outlines the implementation plan for adding artifact support to the chat application. Artifacts are interactive code blocks, HTML previews, diagrams, and other rich content that can be generated by LLMs and displayed interactively in the chat interface.
The chat_message table already has:
contentfield for main message contentrawJSONB field for raw LLM responsesreasoning_contentfield for thinking/reasoning content
interface Message {
uuid: string,
dateTime: string
text: string
model?: string
inversion?: boolean // true for user messages, false for assistant
error?: boolean
loading?: boolean
isPrompt?: boolean
isPin?: boolean
}- User Input → Frontend
- API Call →
/chat_streamendpoint - LLM Processing → Various providers
- Response Streaming → Server-Sent Events
- Database Storage → Full metadata
- Frontend Update → UI refresh
-- Add artifacts column to chat_message table
ALTER TABLE chat_message ADD COLUMN artifacts JSONB;// Add to message struct
type ChatMessage struct {
// ... existing fields
Artifacts []Artifact `json:"artifacts,omitempty"`
}
type Artifact struct {
UUID string `json:"uuid"`
Type string `json:"type"`
Title string `json:"title"`
Content string `json:"content"`
Language string `json:"language,omitempty"`
}// In message processing pipeline
func extractArtifacts(content string) []Artifact {
// Detect code blocks with artifact markers
// Pattern: ```language <!-- artifact: title -->
// Extract HTML blocks with artifact markers
// Detect SVG content
// Find Mermaid diagrams
}- Extend existing message endpoints to include artifact data
- Add artifact-specific endpoints if needed:
GET /api/artifacts/:uuid- Get artifact contentPUT /api/artifacts/:uuid- Update artifact (if editing supported)DELETE /api/artifacts/:uuid- Delete artifactGET /api/artifacts- List all artifacts for a user
<!-- ArtifactViewer.vue -->
<template>
<div class="artifact-container">
<div class="artifact-header">
<span class="artifact-title">{{ artifact.title }}</span>
<div class="artifact-actions">
<button @click="toggleExpanded">{{ expanded ? 'Collapse' : 'Expand' }}</button>
<button @click="copyContent">Copy</button>
<button @click="downloadContent">Download</button>
</div>
</div>
<div v-if="expanded" class="artifact-content">
<component :is="getArtifactComponent(artifact.type)" :artifact="artifact" />
</div>
</div>
</template>HtmlArtifact.vue- Sandboxed HTML previewSvgArtifact.vue- SVG viewer with zoom/panMermaidArtifact.vue- Interactive diagramsJsonArtifact.vue- Formatted JSON viewer
<!-- In Message/index.vue -->
<template>
<div class="message-content">
<div class="message-text">{{ message.text }}</div>
<div v-if="message.artifacts" class="message-artifacts">
<ArtifactViewer
v-for="artifact in message.artifacts"
:key="artifact.uuid"
:artifact="artifact"
/>
</div>
</div>
</template><div id="demo">
<button onclick="alert('Hello!')">Click me</button>
</div><svg viewBox="0 0 100 100">
<circle cx="50" cy="50" r="40" fill="blue" />
</svg>tection
-
HTML Artifacts
- Live HTML preview
- Sandboxed iframe execution
- CSS/JS support
-
SVG Artifacts
- Vector graphics display
- Zoom/pan functionality
- Export options
-
Mermaid Diagrams
- Flowcharts, sequence diagrams
- Interactive navigation
- Export to image
-
JSON/Data Artifacts
- Formatted JSON viewer
- Collapsible tree structure
- Search functionality
- Advanced Artifact Features
// Interactive code execution
- Code Runners: Execute JavaScript, Python snippets safely
- Live Editing: Edit artifacts inline with syntax highlighting
-
Enhanced Visualization
-
Workflow Integration
// Developer tools integration
- CodePen Integration: Open HTML artifacts in CodePen
- Download Options: Save artifacts as files
- Template Library: Reusable artifact templates
- Advanced UI Features
// Enhanced user experience
- Artifact Gallery: Browse all artifacts in a session
- Security & Performance
// Better safety and speed
- Content Sanitization: Advanced XSS protection
- Lazy Loading: Load artifacts only when needed
- Caching: Cache rendered artifacts for better performance
- Resource Limits: Prevent memory leaks from large artifacts
- Database schema updates
- Backend artifact detection logic
- Basic frontend artifact viewer component
- Code artifact component with syntax highlighting
- Copy/download functionality
- HTML artifact component with sandboxing
- SVG artifact viewer
- [] CSS/JS execution in sandbox
- Mermaid diagram support
- JSON/data viewer
- [] Artifact editing capabilities
- [] Sharing and export options
- Sandbox HTML/JS execution to prevent XSS
- Validate and sanitize artifact content
- Limit artifact size and complexity
- Lazy loading of artifact components
- Virtualization for large artifacts
- Caching of rendered content
- Collapsible artifact containers
- Responsive design for mobile
- Keyboard shortcuts for artifact actions
- Loading states for complex artifacts
- Current codebase structure in
/web/src/views/chat/components/Message/ - Database schema in
/api/sqlc/schema.sql - Message handling in
/api/chat_message_handler.go - Frontend message types in
/web/src/types/chat.d.ts