Add reusable multi-session skill and five-page project workbench

This commit is contained in:
aiagent
2026-10-19 03:03:29 -07:00
parent 939c14de77
commit 579dece104
75 changed files with 13096 additions and 0 deletions
@@ -0,0 +1,83 @@
import { useEffect, useRef, useState } from 'react'
import type { FormEvent } from 'react'
import { useSearchParams } from 'react-router-dom'
import { Send, RotateCcw, MessageSquare } from 'lucide-react'
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { Label } from '@/components/ui/label'
import { Textarea } from '@/components/ui/textarea'
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'
import { Alert, AlertDescription } from '@/components/ui/alert'
import { PageHeading, Panel, Empty, Status } from '@/components/shared'
import { useWorkbench } from '@/lib/context'
import { request, errorText, messageKey } from '@/lib/api'
import type { Message } from '@/lib/types'
import { date } from '@/lib/types'
export default function Communication() {
const { state, token, refresh } = useWorkbench()
const primary = state.primary_session_id || 'wp4-progress'
const [params, setParams] = useSearchParams()
const selected = params.get('session') || primary
const session = state.sessions.find(s => s.id === selected)
const [messages, setMessages] = useState<{ session: string; items: Message[] }>({ session: '', items: [] })
const [drafts, setDrafts] = useState<Record<string, string>>({})
const [error, setError] = useState('')
const [sending, setSending] = useState(false)
const [requesting, setRequesting] = useState(false)
const [refreshVersion, setRefreshVersion] = useState(0)
const pending = useRef<{ session_id: string; text: string; idempotency_key: string } | null>(null)
const scroll = useRef<HTMLDivElement>(null)
const known = Boolean(session)
const originalUrl = session?.thread_url && /^codex:\/\/threads\/[a-f0-9-]{36}$/i.test(session.thread_url) ? session.thread_url : undefined
useEffect(() => {
if (!known) return
const controller = new AbortController()
let inFlight = false
const poll = async () => {
if (inFlight) return
inFlight = true
try {
const result = await request<{ messages: Message[] }>(token, `/api/messages?session_id=${encodeURIComponent(selected)}`, undefined, controller.signal)
if (!controller.signal.aborted) { setMessages({ session: selected, items: result.messages }); setError('') }
} catch (e) { if (!controller.signal.aborted) setError(errorText(e)) }
finally { inFlight = false }
}
void poll()
const timer = setInterval(() => void poll(), 3000)
return () => { controller.abort(); clearInterval(timer) }
}, [selected, token, known, refreshVersion])
const items = messages.session === selected ? messages.items : []
const lastId = items.at(-1)?.id
useEffect(() => { const box = scroll.current; if (box && box.scrollHeight - box.scrollTop - box.clientHeight < 300) box.scrollTop = box.scrollHeight }, [lastId])
const text = drafts[selected] || ''
async function send(event: FormEvent) {
event.preventDefault()
if (!session || !text.trim() || sending) return
const sid = selected, body = text
if (!pending.current || pending.current.session_id !== sid || pending.current.text !== body) pending.current = { session_id: sid, text: body, idempotency_key: messageKey() }
setSending(true); setError('')
try {
await request(token, '/api/messages', { ...pending.current, source: 'human' })
pending.current = null
setDrafts(old => old[sid] === body ? { ...old, [sid]: '' } : old)
setRefreshVersion(n => n + 1)
await refresh()
} catch (e) { setError(errorText(e)) }
finally { setSending(false) }
}
async function newSession(event: FormEvent<HTMLFormElement>) {
event.preventDefault(); const form = event.currentTarget, data = new FormData(form)
setRequesting(true)
try { await request(token, '/api/session-requests', { name: data.get('name'), wp: data.get('wp'), prompt: data.get('prompt') }); form.reset(); await refresh() }
catch (e) { setError(errorText(e)) }
finally { setRequesting(false) }
}
return <div className="space-y-6"><PageHeading title="进度与方案沟通" description="当前绑定会话用于进度询问、汇报和方案讨论。" action={<Button variant="outline" onClick={() => setParams({})}><RotateCcw />返回默认会话</Button>} />
{error && <Alert variant="destructive"><AlertDescription>{error}</AlertDescription></Alert>}
<div className="grid items-start gap-6 xl:grid-cols-[minmax(0,1fr)_300px]"><Panel title="项目沟通" description="消息始终发送给当前选中的会话。"><div className="space-y-5">{originalUrl && <Button asChild variant="outline" size="sm"><a href={originalUrl}>在本机打开原会话</a></Button>}<div className="space-y-2"><Label htmlFor="session">当前会话</Label><Select value={selected} onValueChange={value => setParams({ session: value })}><SelectTrigger id="session" className="w-full"><SelectValue /></SelectTrigger><SelectContent>{!session && <SelectItem value={selected}>默认会话尚未登记</SelectItem>}{state.sessions.map(s => <SelectItem key={s.id} value={s.id}>{s.id === primary ? '默认会话 · ' : ''}{s.wp || s.role} · {s.name}</SelectItem>)}</SelectContent></Select></div>
<div className="flex items-start gap-3 rounded-lg bg-muted/60 p-4 text-sm"><MessageSquare className="mt-0.5 size-4 shrink-0 text-primary" /><div><p className="font-medium">{selected === primary ? '同一绑定会话的消息同步' : '当前工作会话'}</p><p className="mt-1 text-muted-foreground">{!session ? '等待登记。不会改投其他会话。' : session.connection === 'connected' ? '已接入真实 AI。' : '等待 AI 执行桥接接入;新消息将持久排队。'}</p>{session?.history_synced_at && <p className="mt-1 text-xs text-muted-foreground">原会话同步于 {date(session.history_synced_at)}</p>}</div></div>
<div ref={scroll} role="log" aria-label="会话消息" aria-live="polite" className="h-[min(48vh,480px)] min-h-64 space-y-4 overflow-y-auto rounded-lg border bg-muted/20 p-4">{items.length ? items.map(m => <article key={m.id} className={`rounded-lg border bg-card p-4 ${m.source === 'agent' ? 'border-l-2 border-l-primary' : ''}`}><div className="mb-2 flex flex-wrap items-center gap-2 text-xs text-muted-foreground"><span className="font-medium text-foreground">{m.source === 'agent' ? 'Agent' : '人工'}</span>{m.origin === 'session_history' && <span>原会话同步</span>}<Status value={m.status} /><time>{date(m.created)}</time></div><p className="whitespace-pre-wrap wrap-anywhere text-sm leading-7">{m.text}</p></article>) : <Empty>{known ? '可以询问项目进度,或提出需要比较的方案。' : '默认会话接入后,在这里继续讨论。'}</Empty>}</div>
<form onSubmit={send} className="space-y-3"><Label htmlFor="message">发送消息</Label><Textarea id="message" value={text} onChange={e => setDrafts(old => ({ ...old, [selected]: e.target.value }))} placeholder="目前进展如何?有哪些方案需要我决定?" maxLength={32000} rows={3} required /><div className="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between"><p className="text-xs text-muted-foreground">消息来源字段不证明人类批准。</p><Button type="submit" disabled={!known || sending || !text.trim()}><Send />{sending ? '发送中…' : '发送到当前会话'}</Button></div></form>
</div></Panel><Panel title="沟通与协作" description="进度、汇报、方案,集中在一个入口。"><p className="text-sm leading-7 text-muted-foreground">历史正文会保留原会话来源。历史人工消息只展示,不会重新进入执行队列。</p><details className="mt-6 border-t pt-5"><summary className="cursor-pointer text-sm font-medium">请求其他工作会话</summary><form onSubmit={newSession} className="mt-4 space-y-4"><div className="space-y-2"><Label htmlFor="name">会话名称</Label><Input id="name" name="name" required maxLength={200} /></div><div className="space-y-2"><Label htmlFor="wp">工作包</Label><Input id="wp" name="wp" maxLength={100} /></div><div className="space-y-2"><Label htmlFor="prompt">任务说明</Label><Textarea id="prompt" name="prompt" required maxLength={16000} /></div><Button variant="outline" disabled={requesting}>{requesting ? '登记中…' : '登记新建请求'}</Button></form><p className="mt-4 text-xs leading-6 text-muted-foreground">登记请求不会自动创建真实 AI 会话。</p>{state.requests.map(r => <p key={r.id} className="mt-3 text-xs text-muted-foreground">{r.name} · 等待接入</p>)}</details></Panel></div>
</div>
}