Download src/components/panels/ConversationPanel.jsx from cheumk/studio: direct link, hf CLI and curl.
- Browser
- Download file 4.06 kB
-
https://huggingface.co/spaces/cheumk/studio/resolve/main/src/components/panels/ConversationPanel.jsx
- Command line
-
hf download hf://spaces/cheumk/studio/src/components/panels/ConversationPanel.jsx
-
curl -L -o ConversationPanel.jsx https://huggingface.co/spaces/cheumk/studio/resolve/main/src/components/panels/ConversationPanel.jsx
4.06 kB
| import { Download, Eraser, FileText, Plus } from 'lucide-react' | |
| import { useStore, useActiveConversation } from '../../store/useStore' | |
| import { Btn, Field, Section, inputCls, download, stamp } from '../ui' | |
| import ModelPicker from './ModelPicker' | |
| function toMarkdown(conv, agents) { | |
| const lines = [`# ${conv.title}`, '', `> 匯出於 ${new Date().toLocaleString()} · 模型 ${conv.model}`, ''] | |
| for (const m of conv.messages) { | |
| if (m.role === 'user') lines.push('## 🧑 使用者', '', m.content, '') | |
| else if (m.role === 'assistant') { | |
| const a = agents.find((x) => x.id === m.agentId) | |
| if (m.content) lines.push(`## ${a?.icon || '🤖'} ${a?.name || 'Assistant'}`, '', m.content, '') | |
| for (const c of m.tool_calls || []) lines.push(`> 🔧 呼叫 \`${c.function?.name}\` ${c.function?.arguments || ''}`, '') | |
| } else if (m.role === 'tool') { | |
| lines.push(`<details><summary>🔧 ${m.name} 結果</summary>`, '', '```', String(m.content), '```', '</details>', '') | |
| } | |
| } | |
| return lines.join('\n') | |
| } | |
| export default function ConversationPanel({ onDone }) { | |
| const conv = useActiveConversation() | |
| const agents = useStore((s) => s.agents) | |
| const running = useStore((s) => (conv ? s.running[conv.id] : 0)) | |
| const st = useStore.getState() | |
| if (!conv) { | |
| return ( | |
| <div className="p-4 space-y-3 text-sm text-muted"> | |
| <p>目前沒有開啟的對話。</p> | |
| <Btn variant="primary" className="w-full" onClick={() => st.createConversation()}> | |
| <Plus className="w-4 h-4" /> 新對話 | |
| </Btn> | |
| </div> | |
| ) | |
| } | |
| const agent = agents.find((a) => a.id === conv.agentId) | |
| const up = (patch) => st.updateConversation(conv.id, patch) | |
| return ( | |
| <div> | |
| <Section title="目前對話"> | |
| <Field label="標題"> | |
| <input className={inputCls} value={conv.title} onChange={(e) => up({ title: e.target.value })} /> | |
| </Field> | |
| <ModelPicker providerId={conv.providerId} model={conv.model} onChange={(providerId, model) => up({ providerId, model })} /> | |
| <p className="text-[11px] text-muted">對話途中可以隨時換模型或 Agent,下一輪就會生效。</p> | |
| </Section> | |
| <Section title="Agent 角色"> | |
| <div className="grid grid-cols-1 gap-1.5"> | |
| {agents.map((a) => ( | |
| <button | |
| key={a.id} | |
| type="button" | |
| onClick={() => { up({ agentId: a.id }); onDone?.() }} | |
| className={`text-left px-3 py-2 rounded-lg flex items-center gap-3 ${a.id === conv.agentId ? 'bg-purple-600/20 ring-1 ring-purple-500/60' : 'bg-field hover:bg-edge'}`} | |
| > | |
| <span className="text-xl">{a.icon}</span> | |
| <span className="min-w-0"> | |
| <span className="block text-sm font-medium truncate">{a.name}</span> | |
| <span className="block text-[11px] text-muted truncate">{a.description} · {a.tools?.length || 0} 個工具</span> | |
| </span> | |
| </button> | |
| ))} | |
| </div> | |
| {agent && ( | |
| <p className="text-[11px] text-muted"> | |
| 啟用工具:{agent.tools?.length ? agent.tools.join('、') : '無'}(到「Agent」頁可修改) | |
| </p> | |
| )} | |
| </Section> | |
| <Section title="操作"> | |
| <div className="grid grid-cols-2 gap-2"> | |
| <Btn onClick={() => download(`chat-${conv.title}-${stamp()}.json`, JSON.stringify(st.exportData('conversation', { id: conv.id }), null, 2))}> | |
| <Download className="w-4 h-4" /> JSON | |
| </Btn> | |
| <Btn onClick={() => download(`chat-${conv.title}-${stamp()}.md`, toMarkdown(conv, agents), 'text/markdown')}> | |
| <FileText className="w-4 h-4" /> Markdown | |
| </Btn> | |
| <Btn className="col-span-2" disabled={!!running || conv.messages.length === 0} onClick={() => confirm('清空這個對話的所有訊息?') && st.clearConversation(conv.id)}> | |
| <Eraser className="w-4 h-4" /> 清空訊息 | |
| </Btn> | |
| </div> | |
| </Section> | |
| </div> | |
| ) | |
| } | |