File size: 8,278 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { useState } from 'react'
import { Plus, Trash2, MessageSquare, Download, StickyNote, Pencil, Check, X } from 'lucide-react'
import { useStore, useActiveProject } from '../../store/useStore'
import { Btn, Field, Section, inputCls, download, stamp } from '../ui'
import ModelPicker from './ModelPicker'

function NoteItem({ note, projectId }) {
  const { updateNote, removeNote } = useStore.getState()
  const [edit, setEdit] = useState(false)
  const [draft, setDraft] = useState(note)
  if (edit) {
    return (
      <div className="space-y-2 bg-field rounded-lg p-2">
        <input className={inputCls} value={draft.title} onChange={(e) => setDraft({ ...draft, title: e.target.value })} />
        <textarea className={`${inputCls} font-mono text-xs`} rows={5} value={draft.content} onChange={(e) => setDraft({ ...draft, content: e.target.value })} />
        <div className="flex gap-2">
          <Btn variant="primary" className="flex-1" onClick={() => { updateNote(projectId, note.id, { title: draft.title, content: draft.content }); setEdit(false) }}>
            <Check className="w-4 h-4" /> 儲存
          </Btn>
          <Btn onClick={() => setEdit(false)}><X className="w-4 h-4" /></Btn>
        </div>
      </div>
    )
  }
  return (
    <details className="bg-field rounded-lg group">
      <summary className="cursor-pointer px-3 py-2 text-sm flex items-center gap-2">
        <StickyNote className="w-3.5 h-3.5 text-amber-500 shrink-0" />
        <span className="truncate flex-1">{note.title}</span>
        <button type="button" onClick={(e) => { e.preventDefault(); setDraft(note); setEdit(true) }} className="text-muted hover:text-ink"><Pencil className="w-3.5 h-3.5" /></button>
        <button type="button" onClick={(e) => { e.preventDefault(); if (confirm(`刪除筆記「${note.title}」?`)) removeNote(projectId, note.id) }} className="text-muted hover:text-red-500"><Trash2 className="w-3.5 h-3.5" /></button>
      </summary>
      <div className="px-3 pb-3 text-xs whitespace-pre-wrap text-muted">{note.content}</div>
    </details>
  )
}

export default function ProjectPanel({ onDone }) {
  const project = useActiveProject()
  const projects = useStore((s) => s.projects)
  const agents = useStore((s) => s.agents)
  const conversations = useStore((s) => s.conversations)
  const activeConversationId = useStore((s) => s.activeConversationId)
  const running = useStore((s) => s.running)
  const st = useStore.getState()

  const convs = Object.values(conversations)
    .filter((c) => c.projectId === project.id)
    .sort((a, b) => b.updatedAt.localeCompare(a.updatedAt))

  const up = (patch) => st.updateProject(project.id, patch)

  return (
    <div>
      <Section
        title="專案"
        right={
          <button type="button" title="新專案" className="p-1 rounded hover:bg-field" onClick={() => {
            const name = prompt('新專案名稱', '新專案')
            if (name !== null) st.createProject(name.trim() || '新專案')
          }}>
            <Plus className="w-4 h-4" />
          </button>
        }
      >
        <div className="space-y-1">
          {projects.map((p) => {
            const count = Object.values(conversations).filter((c) => c.projectId === p.id).length
            return (
              <button
                key={p.id}
                type="button"
                onClick={() => st.selectProject(p.id)}
                className={`w-full text-left px-3 py-2 rounded-lg text-sm flex items-center gap-2 ${p.id === project.id ? 'bg-purple-600/20 text-ink ring-1 ring-purple-500/50' : 'hover:bg-field text-muted'}`}
              >
                <span className="truncate flex-1">📁 {p.name}</span>
                <span className="text-[11px]">{count}</span>
              </button>
            )
          })}
        </div>
      </Section>

      <Section
        title={`對話(${convs.length})`}
        right={
          <button type="button" title="新對話" className="p-1 rounded hover:bg-field" onClick={() => { st.createConversation(); onDone?.() }}>
            <Plus className="w-4 h-4" />
          </button>
        }
      >
        <Btn variant="primary" className="w-full" onClick={() => { st.createConversation(); onDone?.() }}>
          <Plus className="w-4 h-4" /> 新對話
        </Btn>
        <div className="space-y-1">
          {convs.length === 0 && <p className="text-xs text-muted">這個專案還沒有對話</p>}
          {convs.map((c) => {
            const a = agents.find((x) => x.id === c.agentId)
            return (
              <div
                key={c.id}
                className={`group flex items-center gap-2 px-3 py-2 rounded-lg text-sm cursor-pointer ${c.id === activeConversationId ? 'bg-field text-ink' : 'hover:bg-field text-muted'}`}
                onClick={() => st.selectConversation(c.id)}
              >
                {running[c.id] ? <span className="w-2 h-2 rounded-full bg-emerald-500 animate-pulse shrink-0" /> : <MessageSquare className="w-3.5 h-3.5 shrink-0" />}
                <span className="truncate flex-1">{c.title}</span>
                <span className="text-xs">{a?.icon}</span>
                <button
                  type="button"
                  title="刪除對話"
                  className="opacity-100 md:opacity-0 md:group-hover:opacity-100 text-muted hover:text-red-500"
                  onClick={(e) => { e.stopPropagation(); if (confirm(`刪除對話「${c.title}」?`)) st.removeConversation(c.id) }}
                >
                  <Trash2 className="w-3.5 h-3.5" />
                </button>
              </div>
            )
          })}
        </div>
      </Section>

      <Section title={`專案筆記(${project.notes?.length || 0})`} defaultOpen={false}>
        <p className="text-[11px] text-muted">筆記會自動帶進這個專案所有對話的 system prompt;Agent 也能用 save_note / read_notes 工具讀寫。</p>
        <div className="space-y-1">
          {(project.notes || []).map((n) => <NoteItem key={n.id} note={n} projectId={project.id} />)}
        </div>
        <Btn className="w-full" onClick={() => st.addNote(project.id, { title: '新筆記', content: '' })}>
          <Plus className="w-4 h-4" /> 新增筆記
        </Btn>
      </Section>

      <Section title="專案設定" defaultOpen={false}>
        <Field label="名稱">
          <input className={inputCls} value={project.name} onChange={(e) => up({ name: e.target.value })} />
        </Field>
        <Field label="說明">
          <input className={inputCls} value={project.description} onChange={(e) => up({ description: e.target.value })} placeholder="這個專案在做什麼" />
        </Field>
        <Field label="專案指示" hint="附加在每個對話的 system prompt 後面,例如:讀者是國中生、輸出一律用表格。">
          <textarea className={inputCls} rows={4} value={project.instructions} onChange={(e) => up({ instructions: e.target.value })} />
        </Field>
        <Field label="新對話預設 Agent">
          <select className={inputCls} value={project.defaultAgentId} onChange={(e) => up({ defaultAgentId: e.target.value })}>
            {agents.map((a) => <option key={a.id} value={a.id}>{a.icon} {a.name}</option>)}
          </select>
        </Field>
        <ModelPicker
          label="新對話預設模型"
          providerId={project.defaultProviderId}
          model={project.defaultModel}
          onChange={(providerId, model) => up({ defaultProviderId: providerId, defaultModel: model })}
        />
        <div className="flex gap-2">
          <Btn className="flex-1" onClick={() => {
            const data = st.exportData('project', { id: project.id })
            download(`project-${project.name}-${stamp()}.json`, JSON.stringify(data, null, 2))
          }}>
            <Download className="w-4 h-4" /> 匯出專案
          </Btn>
          <Btn variant="danger" onClick={() => {
            if (confirm(`刪除專案「${project.name}」與其中 ${convs.length} 個對話?此動作無法復原。`)) st.removeProject(project.id)
          }}>
            <Trash2 className="w-4 h-4" />
          </Btn>
        </div>
      </Section>
    </div>
  )
}