File size: 4,059 Bytes
4e039c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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>
  )
}