feat(ui): add JSON export functionality to session viewer for AI fine-tuning

This commit is contained in:
2026-05-16 11:32:40 -07:00
parent a4480f9217
commit dfd799f046

View File

@@ -2,7 +2,15 @@
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { useParams, useRouter } from "next/navigation"; import { useParams, useRouter } from "next/navigation";
import { Loader2, ArrowLeft, Bot, User, Code2, Wrench } from "lucide-react"; import {
Loader2,
ArrowLeft,
Bot,
User,
Code2,
Wrench,
Download,
} from "lucide-react";
import ReactMarkdown from "react-markdown"; import ReactMarkdown from "react-markdown";
interface ToolCall { interface ToolCall {
@@ -33,8 +41,8 @@ export default function SessionViewer() {
useEffect(() => { useEffect(() => {
fetch(`/api/chat/threads/${sessionId}`) fetch(`/api/chat/threads/${sessionId}`)
.then(r => r.json()) .then((r) => r.json())
.then(d => { .then((d) => {
setThread(d.thread); setThread(d.thread);
setMessages(d.messages || []); setMessages(d.messages || []);
}) })
@@ -64,6 +72,37 @@ export default function SessionViewer() {
); );
} }
const exportJSON = () => {
const exportData = {
session_id: sessionId,
title: thread.title,
created_at: thread.createdAt,
messages: messages.map((m) => ({
role: m.role,
content:
m.content || (m.textSegments ? m.textSegments.join("\n\n") : ""),
tool_calls: m.toolCalls
? m.toolCalls.map((tc) => ({
name: tc.name,
arguments: tc.args,
}))
: undefined,
})),
};
const blob = new Blob([JSON.stringify(exportData, null, 2)], {
type: "application/json",
});
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = `vibn-session-${sessionId}.json`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
};
return ( return (
<div className="flex-1 bg-[#faf8f5] overflow-y-auto flex flex-col"> <div className="flex-1 bg-[#faf8f5] overflow-y-auto flex flex-col">
<header className="sticky top-0 bg-white border-b border-zinc-200 px-6 py-4 flex items-center gap-4 z-10"> <header className="sticky top-0 bg-white border-b border-zinc-200 px-6 py-4 flex items-center gap-4 z-10">
@@ -74,10 +113,21 @@ export default function SessionViewer() {
> >
<ArrowLeft className="w-4 h-4" /> <ArrowLeft className="w-4 h-4" />
</button> </button>
<div> <div className="flex-1 min-w-0">
<h1 className="text-lg font-semibold text-zinc-900 leading-none mb-1">{thread.title}</h1> <h1 className="text-lg font-semibold text-zinc-900 leading-none mb-1">
<p className="text-xs text-zinc-500 font-mono">{new Date(thread.createdAt).toLocaleString()}</p> {thread.title}
</h1>
<p className="text-xs text-zinc-500 font-mono">
{new Date(thread.createdAt).toLocaleString()}
</p>
</div> </div>
<button
onClick={exportJSON}
className="flex items-center gap-2 px-3 py-1.5 bg-white border border-zinc-200 rounded-md text-sm font-medium text-zinc-700 hover:bg-zinc-50 hover:text-zinc-900 transition-colors shadow-sm"
>
<Download className="w-4 h-4" />
Export JSON
</button>
</header> </header>
<div className="flex-1 max-w-4xl mx-auto w-full p-6 pb-20 space-y-8"> <div className="flex-1 max-w-4xl mx-auto w-full p-6 pb-20 space-y-8">
@@ -107,7 +157,10 @@ export default function SessionViewer() {
{m.toolCalls && m.toolCalls.length > 0 && ( {m.toolCalls && m.toolCalls.length > 0 && (
<div className="space-y-1.5 mb-4"> <div className="space-y-1.5 mb-4">
{m.toolCalls.map((tc, idx) => ( {m.toolCalls.map((tc, idx) => (
<div key={idx} className="bg-zinc-100 border border-zinc-200 rounded-md p-2.5 text-xs font-mono overflow-x-auto text-zinc-600"> <div
key={idx}
className="bg-zinc-100 border border-zinc-200 rounded-md p-2.5 text-xs font-mono overflow-x-auto text-zinc-600"
>
<div className="flex items-center gap-2 text-zinc-800 font-semibold mb-1"> <div className="flex items-center gap-2 text-zinc-800 font-semibold mb-1">
<Wrench className="w-3.5 h-3.5" /> <Wrench className="w-3.5 h-3.5" />
{tc.name} {tc.name}
@@ -127,18 +180,24 @@ export default function SessionViewer() {
)} )}
{/* Fallback for when textSegments exists but content is empty */} {/* Fallback for when textSegments exists but content is empty */}
{!m.content && m.textSegments && m.textSegments.length > 0 && ( {!m.content &&
<div className="prose prose-sm prose-zinc max-w-none bg-white border border-zinc-200 p-4 rounded-xl shadow-sm"> m.textSegments &&
<ReactMarkdown>{m.textSegments.join("\n\n")}</ReactMarkdown> m.textSegments.length > 0 && (
</div> <div className="prose prose-sm prose-zinc max-w-none bg-white border border-zinc-200 p-4 rounded-xl shadow-sm">
)} <ReactMarkdown>
{m.textSegments.join("\n\n")}
</ReactMarkdown>
</div>
)}
</> </>
)} )}
</div> </div>
</div> </div>
))} ))}
{messages.length === 0 && ( {messages.length === 0 && (
<p className="text-center text-zinc-500 italic mt-10">Empty session.</p> <p className="text-center text-zinc-500 italic mt-10">
Empty session.
</p>
)} )}
</div> </div>
</div> </div>