mirror of
https://github.com/ThearchyHelios/AgentLab.git
synced 2026-11-24 02:31:59 +00:00
对话有上下文了,左边也能选会话
三条 run(d983c4e6 / 14103db5 / 08c3de1c)在用户看来是一次对话,系统里却
毫无关联。查下来不是哪里传漏了,是整层缺失:
- 每轮 base_graph=None 从零建图,Copilot 不知道上一轮问过什么
- 每轮 thread_id = run.id,三条独立的 checkpoint 线程
- 前端 turns 只活在 zustand 里,刷新页面就全没了——那三轮当时在界面上
已经找不回来,只剩 runs 表里三条互不相干的记录
- 后端 13 张表里没有会话这个概念
加两张表(conversations / conversation_turns)。失败的轮次也记:只记成功的,
历史就成了一份美化过的记录,而人回来最想搞清楚的恰恰是上次卡在哪。
Copilot 的上下文带三样:前几轮问答、上一轮那张图(走 _slim 去掉坐标噪音)。
只给问答文字不够——"再按月份拆一下"会让模型重新设计一张图,实测经常接到另
一张表上,答案对不上前一轮。运行时另外注入 {{ input.history }},管的是指代:
用户问"它们平均多少",图里的 agent 得知道"它们"是上一轮那 12 家店。
左侧会话栏只在问数据页出现,可折叠、可改名删除,当前会话存 localStorage。
轮次按会话分桶缓存——切走再切回来要看得到原来的步骤,回源只拿得到问答。
历史轮次的执行过程不预加载,点「看执行过程」才取事件、走现成的 decodeRun。
画布右栏的 Copilot 依附工作流各有一条会话,连说两次"再加一个节点"能接上了
(base_graph 带的是图的结果,带不出"你刚才要我干什么"),但不进左侧列表。
过程中修掉的两个自己引入的坑:
- 第一版在列表为空时自动建一条会话。create 是异步的、StrictMode 把 effect
跑两遍,于是每次进页面都留下两三条"新对话"。改成用户开口那一刻按需建
- 加了"在上一轮的图上改"这条指令之后,模型照改图协议只发 update_node,
而问数据页这条路径的累加器从空图起步,操作全部落空,用户收到的是
"图校验未通过:图是空的,先拖一个节点进来"(会话 f5db9809 的第二轮,
而它的 explanation 证明上下文其实完全读懂了)。指令改成沿用 id 和 config
但完整重发,另加一道兜底:空图且上一轮有图时把操作重放到那张图上
check-ui 多一节「会话」:切换、刷新还在、逛一圈不多出空会话。
This commit is contained in:
@@ -1,12 +1,14 @@
|
||||
import { useEffect, useMemo, useState } from 'react'
|
||||
import { Database, MessageSquare, Send, Square, Trash2 } from 'lucide-react'
|
||||
import { Database, ListTree, MessageSquare, Send, Square } from 'lucide-react'
|
||||
import { api } from '../api/client'
|
||||
import { ApprovalCard } from '../run/RunPanel'
|
||||
import { AssistantStream, StreamEmpty, type StreamTurn } from '../run/AssistantStream'
|
||||
import { decodeCopilot, decodeRun } from '../run/decode'
|
||||
import { useCatalog } from '../store/catalog'
|
||||
import { useChat, type ChatTurn } from '../store/chat'
|
||||
import { useConversations } from '../store/conversations'
|
||||
import { useStudio } from '../store/studio'
|
||||
import { ConversationList } from './ConversationList'
|
||||
import { useToast } from '../components/ui'
|
||||
|
||||
/**
|
||||
@@ -23,41 +25,56 @@ import { useToast } from '../components/ui'
|
||||
* dense 一个开关。两边各写一套的话,同一次运行在两个页面会讲出不同的故事。
|
||||
*/
|
||||
export function ChatPage() {
|
||||
const { turns, busy, ask, stop, clear } = useChat()
|
||||
const { busy, ask, stop, load, loadSteps, turnsOf } = useChat()
|
||||
const currentId = useConversations((s) => s.currentId)
|
||||
const create = useConversations((s) => s.create)
|
||||
const turns = useChat((s) => (currentId ? s.byConversation[currentId] : undefined))
|
||||
const [draft, setDraft] = useState('')
|
||||
const sources = useDataSources()
|
||||
const setGraph = useStudio((s) => s.setGraph)
|
||||
const toast = useToast()
|
||||
|
||||
const send = (text?: string) => {
|
||||
// 切到哪个会话就把哪个会话的内容取回来。内存里已经有的不会回源——
|
||||
// 那一份带着这次跑出来的完整步骤,回源拿到的只有问题和答案
|
||||
useEffect(() => { if (currentId) void load(currentId) }, [currentId, load])
|
||||
|
||||
const send = async (text?: string) => {
|
||||
const q = (text ?? draft).trim()
|
||||
if (!q || busy) return
|
||||
ask(q)
|
||||
// 没有会话就先开一个。第一次进来的人不该先被要求"新建对话"
|
||||
const id = currentId ?? (await create())
|
||||
ask(id, q)
|
||||
setDraft('')
|
||||
}
|
||||
|
||||
const streamTurns = useMemo(() => turns.map(toStreamTurn), [turns])
|
||||
const streamTurns = useMemo(() => (turns ?? []).map(toStreamTurn), [turns])
|
||||
|
||||
return (
|
||||
<div className="flex h-full flex-col">
|
||||
<div className="flex h-full">
|
||||
<ConversationList />
|
||||
<div className="flex min-w-0 flex-1 flex-col">
|
||||
<header className="flex shrink-0 items-center gap-2 border-b px-4 py-2">
|
||||
<MessageSquare size={14} style={{ color: 'var(--accent)' }} />
|
||||
<span className="text-[13px] font-semibold">问数据</span>
|
||||
<span className="text-[11px] text-faint">
|
||||
说需求,它自己接数据源、建流程、跑完给结论
|
||||
</span>
|
||||
<span className="flex-1" />
|
||||
{!!turns.length && (
|
||||
<button className="btn btn-sm btn-ghost" onClick={clear} title="清空对话">
|
||||
<Trash2 size={12} />
|
||||
</button>
|
||||
)}
|
||||
</header>
|
||||
|
||||
<AssistantStream
|
||||
turns={streamTurns}
|
||||
empty={<StarterHints sources={sources} onPick={send} />}
|
||||
approvalsFor={(t) => <Approvals turnId={t.id} runId={t.runId} />}
|
||||
approvalsFor={(t) => (
|
||||
<Approvals
|
||||
turnId={t.id}
|
||||
runId={t.runId}
|
||||
onShowSteps={
|
||||
currentId && turnsOf(currentId).find((x) => x.id === t.id)?.restored
|
||||
? () => void loadSteps(currentId, t.id)
|
||||
: undefined
|
||||
}
|
||||
/>
|
||||
)}
|
||||
onOpenGraph={(graph) => {
|
||||
setGraph(graph)
|
||||
toast('已放到画布,切到「编排」继续改', 'ok')
|
||||
@@ -77,15 +94,16 @@ export function ChatPage() {
|
||||
onChange={(e) => setDraft(e.target.value)}
|
||||
onKeyDown={(e) => {
|
||||
// Enter 发送,Shift+Enter 换行——对话框的通用约定
|
||||
if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); send() }
|
||||
if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); void send() }
|
||||
}}
|
||||
/>
|
||||
{busy ? (
|
||||
<button className="btn btn-danger h-9" onClick={stop}>
|
||||
<button className="btn btn-danger h-9"
|
||||
onClick={() => currentId && stop(currentId)}>
|
||||
<Square size={12} /> 停止
|
||||
</button>
|
||||
) : (
|
||||
<button className="btn btn-primary h-9" onClick={() => send()}
|
||||
<button className="btn btn-primary h-9" onClick={() => void send()}
|
||||
disabled={!draft.trim()}>
|
||||
<Send size={12} /> 发送
|
||||
</button>
|
||||
@@ -94,6 +112,7 @@ export function ChatPage() {
|
||||
</div>
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -126,18 +145,39 @@ function toStreamTurn(turn: ChatTurn): StreamTurn {
|
||||
}
|
||||
}
|
||||
|
||||
function Approvals({ turnId, runId }: { turnId: string; runId?: string }) {
|
||||
/**
|
||||
* 挂在一轮下面的两样东西:待办的审批,和历史轮次的「看执行过程」。
|
||||
*
|
||||
* 从库里恢复的轮次没有事件流——一次会话几十轮,把每轮的事件都预加载回来
|
||||
* 是几十个请求换一堆没人看的步骤。所以默认只显示问题和答案,想看过程再取。
|
||||
*/
|
||||
function Approvals({ turnId, runId, onShowSteps }: {
|
||||
turnId: string
|
||||
runId?: string
|
||||
onShowSteps?: () => void
|
||||
}) {
|
||||
const conversationId = useConversations((s) => s.currentId)
|
||||
const approvals = useCatalog((s) => s.approvals)
|
||||
const reattach = useChat((s) => s.reattach)
|
||||
const pending = approvals.filter((a) => a.run_id === runId && a.status === 'pending')
|
||||
|
||||
if (!runId || !pending.length) return null
|
||||
if (!runId) return null
|
||||
return (
|
||||
<div className="mt-2 overflow-hidden rounded border" style={{ borderColor: 'var(--warn)' }}>
|
||||
{pending.map((a) => (
|
||||
<ApprovalCard key={a.id} approval={a} onResolved={() => reattach(turnId)} />
|
||||
))}
|
||||
</div>
|
||||
<>
|
||||
{onShowSteps && (
|
||||
<button className="btn btn-xs btn-ghost mt-2 gap-1 text-[11px]" onClick={onShowSteps}>
|
||||
<ListTree size={11} /> 看执行过程
|
||||
</button>
|
||||
)}
|
||||
{!!pending.length && (
|
||||
<div className="mt-2 overflow-hidden rounded border" style={{ borderColor: 'var(--warn)' }}>
|
||||
{pending.map((a) => (
|
||||
<ApprovalCard key={a.id} approval={a}
|
||||
onResolved={() => { if (conversationId) void reattach(conversationId, turnId) }} />
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -145,7 +185,7 @@ function Approvals({ turnId, runId }: { turnId: string; runId?: string }) {
|
||||
|
||||
function StarterHints({ sources, onPick }: {
|
||||
sources: any[]
|
||||
onPick: (q: string) => void
|
||||
onPick: (q: string) => void | Promise<void>
|
||||
}) {
|
||||
if (!sources.length) {
|
||||
return (
|
||||
@@ -176,7 +216,7 @@ function StarterHints({ sources, onPick }: {
|
||||
{suggestions.map((q) => (
|
||||
<button key={q}
|
||||
className="w-full rounded-lg border px-3 py-2 text-left text-[12px] hover:bg-hover"
|
||||
onClick={() => onPick(q)}>
|
||||
onClick={() => void onPick(q)}>
|
||||
{q}
|
||||
</button>
|
||||
))}
|
||||
|
||||
@@ -0,0 +1,136 @@
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import { Check, MessageSquarePlus, PanelLeftClose, PanelLeftOpen, Pencil, Trash2, X } from 'lucide-react'
|
||||
import clsx from 'clsx'
|
||||
import { useChat } from '../store/chat'
|
||||
import { useConversations } from '../store/conversations'
|
||||
|
||||
/**
|
||||
* 左侧会话列表。
|
||||
*
|
||||
* 之前对话只活在内存里,刷新页面就没了——用户那三次提问在界面上再也找不回来,
|
||||
* 只剩 runs 表里三条互不相干的记录。这一栏是它们重新变成"一次对话"的入口。
|
||||
*
|
||||
* 只在问数据页出现。画布右栏的 Copilot 也有自己的会话,但那是依附某张图的
|
||||
* "改图指令",和这里的"问题"不是一回事,混进同一个列表只会让两种都更难找。
|
||||
*/
|
||||
export function ConversationList() {
|
||||
const { list, currentId, loading, load, create, select, rename, remove } = useConversations()
|
||||
const forget = useChat((s) => s.forget)
|
||||
const busy = useChat((s) => s.busy)
|
||||
const [collapsed, setCollapsed] = useState(false)
|
||||
const [editing, setEditing] = useState<string | null>(null)
|
||||
|
||||
useEffect(() => { void load() }, [load])
|
||||
|
||||
// 这里**不**自动建空会话。试过,代价是每次进页面都留下一条"新对话":
|
||||
// create 是异步的,StrictMode 把 effect 跑两遍,两次都看到空列表于是各建
|
||||
// 一条,列表很快被空壳淹掉。真正需要会话的时刻是用户开口那一下,
|
||||
// ChatPage 的 send 会在那时按需建——在此之前空列表配空态提示就是对的。
|
||||
|
||||
if (collapsed) {
|
||||
return (
|
||||
<div className="flex w-9 shrink-0 flex-col items-center gap-1 border-r bg-panel py-2">
|
||||
<button className="btn btn-sm btn-ghost" title="展开对话列表"
|
||||
onClick={() => setCollapsed(false)}>
|
||||
<PanelLeftOpen size={13} />
|
||||
</button>
|
||||
<button className="btn btn-sm btn-ghost" title="新对话" onClick={() => void create()}>
|
||||
<MessageSquarePlus size={13} />
|
||||
</button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<aside className="flex w-52 shrink-0 flex-col border-r bg-panel">
|
||||
<div className="flex items-center gap-1 px-2 py-2">
|
||||
<button className="btn btn-sm btn-ghost flex-1 justify-start gap-1.5 text-[12px]"
|
||||
onClick={() => void create()}>
|
||||
<MessageSquarePlus size={13} /> 新对话
|
||||
</button>
|
||||
<button className="btn btn-sm btn-ghost" title="收起" onClick={() => setCollapsed(true)}>
|
||||
<PanelLeftClose size={13} />
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div className="min-h-0 flex-1 overflow-y-auto px-1.5 pb-2">
|
||||
{!list.length && !loading && (
|
||||
<p className="px-2 py-4 text-[11px] text-faint">还没有对话</p>
|
||||
)}
|
||||
{list.map((c) => (
|
||||
<div
|
||||
key={c.id}
|
||||
className={clsx(
|
||||
'group mb-0.5 rounded-lg px-2 py-1.5 text-[12px]',
|
||||
c.id === currentId ? 'bg-hover text-fg' : 'text-dim hover:bg-hover',
|
||||
)}
|
||||
>
|
||||
{editing === c.id ? (
|
||||
<RenameField
|
||||
initial={c.title}
|
||||
onCancel={() => setEditing(null)}
|
||||
onSubmit={(title) => { void rename(c.id, title); setEditing(null) }}
|
||||
/>
|
||||
) : (
|
||||
<div className="flex items-center gap-1">
|
||||
<button
|
||||
className="min-w-0 flex-1 text-left"
|
||||
onClick={() => select(c.id)}
|
||||
title={c.last_question || c.title}
|
||||
>
|
||||
<div className="truncate">{c.title || '新对话'}</div>
|
||||
{c.turn_count > 0 && (
|
||||
<div className="truncate text-[10px] text-faint">{c.turn_count} 轮</div>
|
||||
)}
|
||||
</button>
|
||||
{/* 正在跑的时候不给删:底下还挂着一条 WebSocket 和一个真在跑的 run */}
|
||||
<div className="flex shrink-0 gap-0.5 opacity-0 group-hover:opacity-100">
|
||||
<button className="btn btn-xs btn-ghost" title="重命名"
|
||||
onClick={() => setEditing(c.id)}>
|
||||
<Pencil size={11} />
|
||||
</button>
|
||||
<button
|
||||
className="btn btn-xs btn-ghost"
|
||||
title={busy && c.id === currentId ? '正在运行,先停下来再删' : '删除'}
|
||||
disabled={busy && c.id === currentId}
|
||||
onClick={() => { forget(c.id); void remove(c.id) }}
|
||||
>
|
||||
<Trash2 size={11} />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</aside>
|
||||
)
|
||||
}
|
||||
|
||||
function RenameField({ initial, onSubmit, onCancel }: {
|
||||
initial: string
|
||||
onSubmit: (title: string) => void
|
||||
onCancel: () => void
|
||||
}) {
|
||||
const [text, setText] = useState(initial)
|
||||
const ref = useRef<HTMLInputElement>(null)
|
||||
useEffect(() => { ref.current?.select() }, [])
|
||||
|
||||
return (
|
||||
<div className="flex items-center gap-0.5">
|
||||
<input
|
||||
ref={ref}
|
||||
className="field h-6 min-w-0 flex-1 px-1 text-[12px]"
|
||||
value={text}
|
||||
onChange={(e) => setText(e.target.value)}
|
||||
onKeyDown={(e) => {
|
||||
if (e.key === 'Enter') onSubmit(text)
|
||||
if (e.key === 'Escape') onCancel()
|
||||
}}
|
||||
autoFocus
|
||||
/>
|
||||
<button className="btn btn-xs btn-ghost" onClick={() => onSubmit(text)}><Check size={11} /></button>
|
||||
<button className="btn btn-xs btn-ghost" onClick={onCancel}><X size={11} /></button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user