Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

feat(ui): editable artifact panel for all kinds, save new versions, file viewer for binaries

Sanil58a5ee7732d8
diff --git a/app/src/features/conversations/aui/ArtifactPreviewPanel.tsx b/app/src/features/conversations/aui/ArtifactPreviewPanel.tsx
index 5a56a10bf8..5ae3fe6654 100644
--- a/app/src/features/conversations/aui/ArtifactPreviewPanel.tsx
+++ b/app/src/features/conversations/aui/ArtifactPreviewPanel.tsx
@@ -3,9 +3,12 @@
import {
Check,
Clipboard,
+ Code2,
Download,
+ Eye,
+ FileBox,
FileText,
- GripVertical,
+ Save,
X,
} from 'lucide-react';
import {
@@ -30,11 +33,11 @@ interface ArtifactPreviewPanelProps {
onClose: () => void;
}
-interface VersionEntry {
+interface ArtifactBodyResponse {
+ kind: string;
+ title: string;
version: number;
body: string;
- createdAt: string;
- note?: string;
}
// ─── Constants ──────────────────────────────────────────────────
@@ -146,15 +149,10 @@ export function ArtifactPreviewPanel({ artifactId, onClose }: ArtifactPreviewPan
</Button>
</div>
- {/* Toolbar */}
- {activeArtifact && (
- <ArtifactToolbar artifact={activeArtifact} allArtifacts={allArtifacts} />
- )}
-
{/* Content */}
- <div className="flex-1 overflow-y-auto">
+ <div className="flex-1 overflow-hidden">
{activeArtifact ? (
- <ArtifactContent artifact={activeArtifact} />
+ <ArtifactContentPanel artifact={activeArtifact} />
) : (
<div className="flex items-center justify-center h-full p-4 text-sm text-muted-foreground">
{t('chat.artifact.notFound')}
@@ -165,93 +163,37 @@ export function ArtifactPreviewPanel({ artifactId, onClose }: ArtifactPreviewPan
);
}
-// ─── Toolbar (version, copy, download) ──────────────────────────
-
-function ArtifactToolbar({
- artifact,
- allArtifacts,
-}: {
- artifact: ArtifactSnapshot;
- allArtifacts: ArtifactSnapshot[];
-}) {
- const { t } = useT();
- const [copied, setCopied] = useState(false);
-
- // Find the current index among same-titled artifacts for version hint
- const sameTitle = allArtifacts.filter(a => a.title === artifact.title);
- const versionIndex = sameTitle.indexOf(artifact);
-
- const handleCopy = useCallback(() => {
- // In a full implementation, copy the artifact body
- setCopied(true);
- setTimeout(() => setCopied(false), 2000);
- }, []);
-
- const handleDownload = useCallback(() => {
- // In a full implementation, trigger file download via the file URL
- }, []);
+// ─── Content panel (fetches and renders) ────────────────────────
- return (
- <div className="flex items-center justify-between border-b px-3 py-1.5 shrink-0 bg-muted/10">
- <div className="flex items-center gap-2">
- <span className="text-xs font-medium text-muted-foreground capitalize">{artifact.kind}</span>
- {versionIndex >= 0 && (
- <span className="text-[11px] text-muted-foreground/60 border-l pl-2">
- v{versionIndex + 1}
- </span>
- )}
- </div>
- <div className="flex items-center gap-1">
- <Button variant="ghost" size="xs" onClick={handleCopy} title={t('chat.artifact.copy')}>
- {copied ? <Check className="h-3.5 w-3.5 text-green-500" /> : <Clipboard className="h-3.5 w-3.5" />}
- </Button>
- <Button variant="ghost" size="xs" onClick={handleDownload} title={t('chat.artifact.download')}>
- <Download className="h-3.5 w-3.5" />
- </Button>
- </div>
- </div>
- );
-}
-
-// ─── Content renderer ──────────────────────────────────────────
-
-function ArtifactContent({ artifact }: { artifact: ArtifactSnapshot }) {
- const [content, setContent] = useState<string | null>(null);
+function ArtifactContentPanel({ artifact }: { artifact: ArtifactSnapshot }) {
+ const [bodyResult, setBodyResult] = useState<ArtifactBodyResponse | null>(null);
const [loading, setLoading] = useState(true);
- const [artifactKind, setArtifactKind] = useState<string>(artifact.kind);
- const [version, setVersion] = useState(1);
+ const [error, setError] = useState<string | null>(null);
- // Fetch artifact body from versioned store via Tauri IPC
useEffect(() => {
if (!artifact.artifactId) {
setLoading(false);
return;
}
-
let cancelled = false;
setLoading(true);
+ setError(null);
const fetchBody = async () => {
try {
- const result = await invoke<{ kind: string; title: string; version: number; body: string }>(
- 'read_artifact_body',
- { artifactId: artifact.artifactId },
- );
- if (cancelled) return;
- setContent(result.body);
- setArtifactKind(result.kind);
- setVersion(result.version);
+ const result = await invoke<ArtifactBodyResponse>('read_artifact_body', {
+ artifactId: artifact.artifactId,
+ });
+ if (!cancelled) setBodyResult(result);
} catch (err) {
- // Fallback: show a placeholder
- if (cancelled) return;
- setContent(`# ${artifact.title}\n\n*Kind: ${artifact.kind}\nPath: ${artifact.path || '—'}*`);
+ if (!cancelled) setError(String(err));
}
if (!cancelled) setLoading(false);
};
fetchBody();
return () => { cancelled = true; };
- }, [artifact.artifactId, artifact.title, artifact.kind, artifact.path]);
+ }, [artifact.artifactId]);
if (loading) {
return (
@@ -261,44 +203,213 @@ function ArtifactContent({ artifact }: { artifact: ArtifactSnapshot }) {
);
}
- const kind = artifactKind || artifact.kind;
+ const kind = bodyResult?.kind || artifact.kind;
+ const body = bodyResult?.body || '';
+ const version = bodyResult?.version || 1;
- switch (kind) {
- case 'html':
- return <HtmlViewer content={content || ''} />;
- case 'markdown':
- case 'text':
- case 'document':
- default:
- return <TextViewer content={content || `# ${artifact.title}\n\n*Kind: ${artifact.kind}\nPath: ${artifact.path}*`} language={kind} />;
+ // Binary / file-reference kinds → FileViewer (metadata + download)
+ if (kind === 'file' || kind === 'image') {
+ return <FileViewer artifact={artifact} body={body} version={version} error={error} />;
}
+
+ // Text-based kinds → EditableTextViewer (preview + code editor + save)
+ return (
+ <EditableTextViewer
+ artifact={artifact}
+ body={body}
+ version={version}
+ kind={kind}
+ error={error}
+ />
+ );
}
-// ─── HTML viewer ──────────────────────────────────────────────
+// ─── File viewer (binary / file-reference kinds) ────────────────
+
+function FileViewer({
+ artifact,
+ body,
+ version,
+ error,
+}: {
+ artifact: ArtifactSnapshot;
+ body: string;
+ version: number;
+ error: string | null;
+}) {
+ const filePath = body || artifact.path || '';
+ const fileName = filePath.split('/').pop() || filePath;
-function HtmlViewer({ content }: { content: string }) {
return (
- <iframe
- srcDoc={content}
- className="w-full h-full border-0"
- sandbox="allow-scripts allow-same-origin"
- title="HTML preview"
- />
+ <div className="flex flex-col h-full">
+ <div className="flex items-center justify-between border-b px-3 py-1.5 shrink-0 bg-muted/10">
+ <div className="flex items-center gap-2">
+ <FileBox className="h-4 w-4 text-muted-foreground" />
+ <span className="text-xs font-medium text-muted-foreground capitalize">{artifact.kind}</span>
+ <span className="text-[11px] text-muted-foreground/60 border-l pl-2">v{version}</span>
+ </div>
+ </div>
+
+ <div className="flex-1 overflow-auto p-6">
+ <div className="flex flex-col items-center justify-center gap-4 text-center">
+ <FileBox className="h-12 w-12 text-muted-foreground/40" />
+ <div>
+ <p className="text-sm font-medium break-all">{fileName}</p>
+ <p className="text-xs text-muted-foreground mt-1 break-all">{filePath}</p>
+ <p className="text-xs text-muted-foreground mt-2">
+ Binary {artifact.kind} — preview not available
+ </p>
+ {error && <p className="text-xs text-destructive mt-2">{error}</p>}
+ </div>
+ {filePath && (
+ <a
+ href={`file://${filePath}`}
+ download={fileName}
+ className="inline-flex items-center gap-2 rounded-md bg-primary px-4 py-2 text-xs font-medium text-primary-foreground hover:bg-primary/90"
+ >
+ <Download className="h-3.5 w-3.5" />
+ Download
+ </a>
+ )}
+ </div>
+ </div>
+ </div>
);
}
-// ─── Text / Markdown viewer ───────────────────────────────────
+// ─── Editable text viewer (markdown / text / html) ──────────────
+
+function EditableTextViewer({
+ artifact,
+ body,
+ version,
+ kind,
+ error,
+}: {
+ artifact: ArtifactSnapshot;
+ body: string;
+ version: number;
+ kind: string;
+ error: string | null;
+}) {
+ const [tab, setTab] = useState<'preview' | 'code'>('preview');
+ const [editedBody, setEditedBody] = useState(body);
+ const [saving, setSaving] = useState(false);
+ const [saved, setSaved] = useState(false);
+ const [copied, setCopied] = useState(false);
+
+ // Sync when body changes (switching artifact tabs)
+ useEffect(() => {
+ setEditedBody(body);
+ setSaved(false);
+ }, [body]);
+
+ const handleSave = useCallback(async () => {
+ setSaving(true);
+ try {
+ await invoke('save_artifact_body', {
+ args: {
+ artifact_id: artifact.artifactId,
+ body: editedBody,
+ note: null,
+ },
+ });
+ setSaved(true);
+ setTimeout(() => setSaved(false), 2000);
+ } catch (err) {
+ console.error('[ArtifactPreviewPanel] save failed:', err);
+ }
+ setSaving(false);
+ }, [artifact.artifactId, editedBody]);
+
+ const handleCopy = useCallback(() => {
+ void navigator.clipboard.writeText(editedBody);
+ setCopied(true);
+ setTimeout(() => setCopied(false), 2000);
+ }, [editedBody]);
+
+ const isHtml = kind === 'html';
-function TextViewer({ content, language }: { content: string; language: string }) {
return (
- <div className="h-full flex flex-col">
- <div className="border-b bg-muted/20 px-3 py-1.5 text-[11px] font-medium text-muted-foreground flex items-center gap-2">
- <FileText className="h-3 w-3" />
- <span className="uppercase">{language}</span>
+ <div className="flex flex-col h-full">
+ {/* Toolbar */}
+ <div className="flex items-center justify-between border-b px-3 py-1.5 shrink-0 bg-muted/10">
+ <div className="flex items-center gap-2">
+ <span className="text-xs font-medium text-muted-foreground capitalize">{kind}</span>
+ <span className="text-[11px] text-muted-foreground/60 border-l pl-2">v{version}</span>
+ </div>
+ <div className="flex items-center gap-0.5">
+ <div className="flex items-center rounded border mr-2">
+ <button
+ onClick={() => setTab('preview')}
+ className={`flex items-center gap-1 px-2 py-0.5 text-xs ${
+ tab === 'preview'
+ ? 'bg-primary/10 text-primary'
+ : 'text-muted-foreground hover:text-foreground'
+ }`}
+ >
+ <Eye className="h-3 w-3" />
+ Preview
+ </button>
+ <button
+ onClick={() => setTab('code')}
+ className={`flex items-center gap-1 px-2 py-0.5 text-xs ${
+ tab === 'code'
+ ? 'bg-primary/10 text-primary'
+ : 'text-muted-foreground hover:text-foreground'
+ }`}
+ >
+ <Code2 className="h-3 w-3" />
+ Code
+ </button>
+ </div>
+
+ <Button variant="ghost" size="xs" onClick={handleCopy} title="Copy to clipboard">
+ {copied ? <Check className="h-3.5 w-3.5 text-green-500" /> : <Clipboard className="h-3.5 w-3.5" />}
+ </Button>
+ <Button variant="ghost" size="xs" onClick={handleSave} disabled={saving} title="Save as new version">
+ {saving ? (
+ <div className="h-3.5 w-3.5 animate-spin rounded-full border-2 border-primary border-t-transparent" />
+ ) : saved ? (
+ <Check className="h-3.5 w-3.5 text-green-500" />
+ ) : (
+ <Save className="h-3.5 w-3.5" />
+ )}
+ </Button>
+ </div>
</div>
- <pre className="flex-1 overflow-auto p-4 text-sm font-mono leading-relaxed whitespace-pre-wrap break-words">
- {content}
- </pre>
+
+ {/* Error banner */}
+ {error && (
+ <div className="border-b bg-destructive/10 px-3 py-1.5 text-[11px] text-destructive">
+ {error}
+ </div>
+ )}
+
+ {/* Content area */}
+ {tab === 'preview' ? (
+ isHtml ? (
+ <iframe
+ srcDoc={editedBody}
+ className="w-full flex-1 border-0"
+ sandbox="allow-scripts allow-same-origin"
+ title="HTML preview"
+ />
+ ) : (
+ <div className="flex-1 overflow-auto">
+ <pre className="p-4 text-sm font-mono leading-relaxed whitespace-pre-wrap break-words">
+ {editedBody}
+ </pre>
+ </div>
+ )
+ ) : (
+ <textarea
+ value={editedBody}
+ onChange={e => setEditedBody(e.target.value)}
+ className="flex-1 w-full resize-none border-0 bg-background p-4 text-sm font-mono leading-relaxed outline-none focus:ring-0"
+ spellCheck={false}
+ />
+ )}
</div>
);
}
diff --git a/crates/openhuman-app/src/artifact_commands.rs b/crates/openhuman-app/src/artifact_commands.rs
index 316a31afb6..fff2c0cd84 100644
--- a/crates/openhuman-app/src/artifact_commands.rs
+++ b/crates/openhuman-app/src/artifact_commands.rs
@@ -26,7 +26,7 @@
//! written outside the Downloads directory.
use std::path::{Path, PathBuf};
-use serde::Serialize;
+use serde::{Deserialize, Serialize};
/// Resolve an artifact id to the file the core's artifact store vouches for,
/// in the given workspace. Isolated from config loading for unit testing.
@@ -85,6 +85,57 @@ pub async fn read_artifact_body(artifact_id: String) -> Result<ArtifactBodyResul
Ok(ArtifactBodyResult { kind, title, version: versions, body })
}
+#[derive(Deserialize)]
+pub struct SaveArtifactBodyArgs {
+ pub artifact_id: String,
+ pub body: String,
+ pub note: Option<String>,
+}
+
+#[tauri::command]
+pub async fn save_artifact_body(args: SaveArtifactBodyArgs) -> Result<(), String> {
+ let config = openhuman_rpc::embed::config::load_config_with_timeout().await?;
+ let path = config.workspace_dir
+ .join("artifacts/versioned")
+ .join(&args.artifact_id)
+ .with_extension("json");
+
+ // Read existing record
+ let json = tokio::fs::read_to_string(&path)
+ .await
+ .map_err(|e| format!("artifact {} not found: {e}", args.artifact_id))?;
+ let mut record: serde_json::Value = serde_json::from_str(&json)
+ .map_err(|e| format!("artifact {} parse error: {e}", args.artifact_id))?;
+
+ // Add new version
+ let now = chrono::Utc::now().to_rfc3339();
+ let new_version = serde_json::json!({
+ "version": record["versions"].as_array().map(|v| v.len() as u64 + 1).unwrap_or(1),
+ "body": args.body,
+ "created_at": now,
+ "note": args.note.unwrap_or_default(),
+ });
+
+ record["versions"]
+ .as_array_mut()
+ .ok_or_else(|| "versions is not an array".to_string())?
+ .push(new_version);
+ record["updated_at"] = serde_json::Value::String(now);
+
+ let updated = serde_json::to_string_pretty(&record)
+ .map_err(|e| format!("serialization error: {e}"))?;
+ tokio::fs::write(&path, &updated)
+ .await
+ .map_err(|e| format!("write error: {e}"))?;
+
+ log::info!(
+ "[artifact_commands] save_artifact_body id={} version={}",
+ args.artifact_id,
+ record["versions"].as_array().map(|v| v.len()).unwrap_or(0)
+ );
+ Ok(())
+}
+
/// Maximum number of `(N)` suffixes we'll append when picking a
/// non-colliding filename. After 1000 we give up and append a UUID
/// suffix instead so the download never silently overwrites.
diff --git a/crates/openhuman-app/src/lib.rs b/crates/openhuman-app/src/lib.rs
index c6fbda9525..edf11feaaf 100644
--- a/crates/openhuman-app/src/lib.rs
+++ b/crates/openhuman-app/src/lib.rs
@@ -3304,6 +3304,7 @@ pub fn run() {
// front of this went with the shell's `rfd` dependency.
artifact_commands::download_artifact_to_downloads,
artifact_commands::read_artifact_body,
+ artifact_commands::save_artifact_body,
// Native directory chooser for the folder memory-source (#5831).
// Unlike the Save-As dialog above it, this one has no renderer-side
// substitute: a `webkitdirectory` input cannot report where the