mirror of
https://github.com/YspCoder/clawgo.git
synced 2026-06-19 00:13:11 +08:00
Separate main chat from subagent group stream
This commit is contained in:
@@ -393,6 +393,22 @@ func (al *AgentLoop) HandleSubagentRuntime(ctx context.Context, action string, a
|
|||||||
"thread": thread,
|
"thread": thread,
|
||||||
"items": stream,
|
"items": stream,
|
||||||
}, nil
|
}, nil
|
||||||
|
case "stream_all":
|
||||||
|
tasks := sm.ListTasks()
|
||||||
|
sort.Slice(tasks, func(i, j int) bool {
|
||||||
|
left := maxInt64(tasks[i].Updated, tasks[i].Created)
|
||||||
|
right := maxInt64(tasks[j].Updated, tasks[j].Created)
|
||||||
|
if left != right {
|
||||||
|
return left > right
|
||||||
|
}
|
||||||
|
return tasks[i].ID > tasks[j].ID
|
||||||
|
})
|
||||||
|
taskLimit := runtimeIntArg(args, "task_limit", 16)
|
||||||
|
if taskLimit > 0 && len(tasks) > taskLimit {
|
||||||
|
tasks = tasks[:taskLimit]
|
||||||
|
}
|
||||||
|
items := mergeAllSubagentStreams(sm, tasks, runtimeIntArg(args, "limit", 200))
|
||||||
|
return map[string]interface{}{"found": true, "items": items}, nil
|
||||||
case "inbox":
|
case "inbox":
|
||||||
agentID := runtimeStringArg(args, "agent_id")
|
agentID := runtimeStringArg(args, "agent_id")
|
||||||
if agentID == "" {
|
if agentID == "" {
|
||||||
@@ -460,6 +476,99 @@ func mergeSubagentStream(events []tools.SubagentRunEvent, messages []tools.Agent
|
|||||||
return items
|
return items
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func mergeAllSubagentStreams(sm *tools.SubagentManager, tasks []*tools.SubagentTask, limit int) []map[string]interface{} {
|
||||||
|
if sm == nil || len(tasks) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
items := make([]map[string]interface{}, 0)
|
||||||
|
seenEvents := map[string]struct{}{}
|
||||||
|
seenMessages := map[string]struct{}{}
|
||||||
|
for _, task := range tasks {
|
||||||
|
if task == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if events, err := sm.Events(task.ID, limit); err == nil {
|
||||||
|
for _, evt := range events {
|
||||||
|
key := fmt.Sprintf("%s:%s:%d:%s", evt.RunID, evt.Type, evt.At, evt.Message)
|
||||||
|
if _, ok := seenEvents[key]; ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
seenEvents[key] = struct{}{}
|
||||||
|
items = append(items, map[string]interface{}{
|
||||||
|
"kind": "event",
|
||||||
|
"at": evt.At,
|
||||||
|
"task_id": task.ID,
|
||||||
|
"label": task.Label,
|
||||||
|
"run_id": evt.RunID,
|
||||||
|
"agent_id": firstNonEmptyString(evt.AgentID, task.AgentID),
|
||||||
|
"event_type": evt.Type,
|
||||||
|
"status": evt.Status,
|
||||||
|
"message": evt.Message,
|
||||||
|
"retry_count": evt.RetryCount,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if strings.TrimSpace(task.ThreadID) == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if messages, err := sm.ThreadMessages(task.ThreadID, limit); err == nil {
|
||||||
|
for _, msg := range messages {
|
||||||
|
if _, ok := seenMessages[msg.MessageID]; ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
seenMessages[msg.MessageID] = struct{}{}
|
||||||
|
items = append(items, map[string]interface{}{
|
||||||
|
"kind": "message",
|
||||||
|
"at": msg.CreatedAt,
|
||||||
|
"task_id": task.ID,
|
||||||
|
"label": task.Label,
|
||||||
|
"message_id": msg.MessageID,
|
||||||
|
"thread_id": msg.ThreadID,
|
||||||
|
"from_agent": msg.FromAgent,
|
||||||
|
"to_agent": msg.ToAgent,
|
||||||
|
"reply_to": msg.ReplyTo,
|
||||||
|
"correlation_id": msg.CorrelationID,
|
||||||
|
"message_type": msg.Type,
|
||||||
|
"content": msg.Content,
|
||||||
|
"status": msg.Status,
|
||||||
|
"requires_reply": msg.RequiresReply,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sort.Slice(items, func(i, j int) bool {
|
||||||
|
left, _ := items[i]["at"].(int64)
|
||||||
|
right, _ := items[j]["at"].(int64)
|
||||||
|
if left != right {
|
||||||
|
return left < right
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%v", items[i]["task_id"]) < fmt.Sprintf("%v", items[j]["task_id"])
|
||||||
|
})
|
||||||
|
if limit > 0 && len(items) > limit {
|
||||||
|
items = items[len(items)-limit:]
|
||||||
|
}
|
||||||
|
return items
|
||||||
|
}
|
||||||
|
|
||||||
|
func maxInt64(values ...int64) int64 {
|
||||||
|
var out int64
|
||||||
|
for _, v := range values {
|
||||||
|
if v > out {
|
||||||
|
out = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func firstNonEmptyString(values ...string) string {
|
||||||
|
for _, v := range values {
|
||||||
|
if strings.TrimSpace(v) != "" {
|
||||||
|
return strings.TrimSpace(v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
func cloneSubagentTask(in *tools.SubagentTask) *tools.SubagentTask {
|
func cloneSubagentTask(in *tools.SubagentTask) *tools.SubagentTask {
|
||||||
if in == nil {
|
if in == nil {
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -389,3 +389,48 @@ func TestHandleSubagentRuntimeStream(t *testing.T) {
|
|||||||
t.Fatalf("expected merged event and message items, got %#v", items)
|
t.Fatalf("expected merged event and message items, got %#v", items)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHandleSubagentRuntimeStreamAll(t *testing.T) {
|
||||||
|
workspace := t.TempDir()
|
||||||
|
manager := tools.NewSubagentManager(nil, workspace, nil)
|
||||||
|
manager.SetRunFunc(func(ctx context.Context, task *tools.SubagentTask) (string, error) {
|
||||||
|
return "stream-all-result", nil
|
||||||
|
})
|
||||||
|
loop := &AgentLoop{
|
||||||
|
workspace: workspace,
|
||||||
|
subagentManager: manager,
|
||||||
|
subagentRouter: tools.NewSubagentRouter(manager),
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := loop.HandleSubagentRuntime(context.Background(), "spawn", map[string]interface{}{
|
||||||
|
"task": "prepare grouped stream task",
|
||||||
|
"agent_id": "coder",
|
||||||
|
"channel": "webui",
|
||||||
|
"chat_id": "webui",
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("spawn failed: %v", err)
|
||||||
|
}
|
||||||
|
for i := 0; i < 50; i++ {
|
||||||
|
tasks := manager.ListTasks()
|
||||||
|
if len(tasks) > 0 && tasks[0].Status == "completed" {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
time.Sleep(10 * time.Millisecond)
|
||||||
|
}
|
||||||
|
|
||||||
|
out, err := loop.HandleSubagentRuntime(context.Background(), "stream_all", map[string]interface{}{
|
||||||
|
"limit": 100,
|
||||||
|
"task_limit": 10,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("stream_all failed: %v", err)
|
||||||
|
}
|
||||||
|
payload, ok := out.(map[string]interface{})
|
||||||
|
if !ok || payload["found"] != true {
|
||||||
|
t.Fatalf("unexpected stream_all payload: %#v", out)
|
||||||
|
}
|
||||||
|
items, ok := payload["items"].([]map[string]interface{})
|
||||||
|
if !ok || len(items) == 0 {
|
||||||
|
t.Fatalf("expected grouped stream items, got %#v", payload["items"])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -116,6 +116,10 @@ const resources = {
|
|||||||
noCronJobs: 'No cron jobs found',
|
noCronJobs: 'No cron jobs found',
|
||||||
noNodes: 'No nodes available',
|
noNodes: 'No nodes available',
|
||||||
sessions: 'Sessions',
|
sessions: 'Sessions',
|
||||||
|
mainChat: 'Main Chat',
|
||||||
|
subagentGroup: 'Subagent Group',
|
||||||
|
noSubagentStream: 'No subagent internal stream yet.',
|
||||||
|
subagentGroupReadonly: 'Subagent group is read-only.',
|
||||||
startConversation: 'Start a conversation',
|
startConversation: 'Start a conversation',
|
||||||
typeMessage: 'Type a message...',
|
typeMessage: 'Type a message...',
|
||||||
configuration: 'Configuration',
|
configuration: 'Configuration',
|
||||||
@@ -571,6 +575,10 @@ const resources = {
|
|||||||
noCronJobs: '未找到定时任务',
|
noCronJobs: '未找到定时任务',
|
||||||
noNodes: '无可用节点',
|
noNodes: '无可用节点',
|
||||||
sessions: '会话',
|
sessions: '会话',
|
||||||
|
mainChat: '主对话',
|
||||||
|
subagentGroup: '子代理群组',
|
||||||
|
noSubagentStream: '当前还没有子代理内部流。',
|
||||||
|
subagentGroupReadonly: '子代理群组为只读视图。',
|
||||||
startConversation: '开始对话',
|
startConversation: '开始对话',
|
||||||
typeMessage: '输入消息...',
|
typeMessage: '输入消息...',
|
||||||
configuration: '配置',
|
configuration: '配置',
|
||||||
|
|||||||
@@ -5,12 +5,28 @@ import { useTranslation } from 'react-i18next';
|
|||||||
import { useAppContext } from '../context/AppContext';
|
import { useAppContext } from '../context/AppContext';
|
||||||
import { ChatItem } from '../types';
|
import { ChatItem } from '../types';
|
||||||
|
|
||||||
|
type StreamItem = {
|
||||||
|
kind?: string;
|
||||||
|
at?: number;
|
||||||
|
task_id?: string;
|
||||||
|
label?: string;
|
||||||
|
agent_id?: string;
|
||||||
|
event_type?: string;
|
||||||
|
message?: string;
|
||||||
|
message_type?: string;
|
||||||
|
content?: string;
|
||||||
|
from_agent?: string;
|
||||||
|
to_agent?: string;
|
||||||
|
status?: string;
|
||||||
|
};
|
||||||
|
|
||||||
const Chat: React.FC = () => {
|
const Chat: React.FC = () => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const { q, sessions } = useAppContext();
|
const { q, sessions } = useAppContext();
|
||||||
const [chat, setChat] = useState<ChatItem[]>([]);
|
const [chat, setChat] = useState<ChatItem[]>([]);
|
||||||
const [msg, setMsg] = useState('');
|
const [msg, setMsg] = useState('');
|
||||||
const [fileSelected, setFileSelected] = useState(false);
|
const [fileSelected, setFileSelected] = useState(false);
|
||||||
|
const [chatTab, setChatTab] = useState<'main' | 'subagents'>('main');
|
||||||
const [sessionKey, setSessionKey] = useState('main');
|
const [sessionKey, setSessionKey] = useState('main');
|
||||||
const chatEndRef = useRef<HTMLDivElement>(null);
|
const chatEndRef = useRef<HTMLDivElement>(null);
|
||||||
|
|
||||||
@@ -51,6 +67,34 @@ const Chat: React.FC = () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const loadSubagentGroup = async () => {
|
||||||
|
try {
|
||||||
|
const r = await fetch(`/webui/api/subagents_runtime${q}`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ action: 'stream_all', limit: 200, task_limit: 24 }),
|
||||||
|
});
|
||||||
|
if (!r.ok) return;
|
||||||
|
const j = await r.json();
|
||||||
|
const arr = Array.isArray(j?.result?.items) ? j.result.items : [];
|
||||||
|
const mapped: ChatItem[] = arr.map((item: StreamItem) => {
|
||||||
|
const isEvent = item.kind === 'event';
|
||||||
|
const label = isEvent
|
||||||
|
? `${item.agent_id || 'subagent'} · ${item.event_type || 'event'}`
|
||||||
|
: `${item.from_agent || '-'} -> ${item.to_agent || '-'} · ${item.message_type || 'message'}`;
|
||||||
|
const body = isEvent ? (item.message || '') : (item.content || '');
|
||||||
|
return {
|
||||||
|
role: 'assistant',
|
||||||
|
label,
|
||||||
|
text: `${body}${item.status ? `\n\nstatus: ${item.status}` : ''}`,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
setChat(mapped);
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
async function send() {
|
async function send() {
|
||||||
if (!msg.trim() && !fileSelected) return;
|
if (!msg.trim() && !fileSelected) return;
|
||||||
|
|
||||||
@@ -111,27 +155,55 @@ const Chat: React.FC = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
loadHistory();
|
if (chatTab === 'main') {
|
||||||
}, [q, sessionKey]);
|
loadHistory();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
loadSubagentGroup();
|
||||||
|
}, [q, chatTab, sessionKey]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!sessions || sessions.length === 0) return;
|
if (chatTab !== 'subagents') return;
|
||||||
if (!sessions.some(s => s.key === sessionKey)) {
|
const timer = window.setInterval(() => {
|
||||||
setSessionKey(sessions[0].key);
|
loadSubagentGroup();
|
||||||
|
}, 5000);
|
||||||
|
return () => window.clearInterval(timer);
|
||||||
|
}, [q, chatTab]);
|
||||||
|
|
||||||
|
const userSessions = (sessions || []).filter((s: any) => !String(s?.key || '').startsWith('subagent:'));
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (chatTab !== 'main') return;
|
||||||
|
if (!userSessions.length) return;
|
||||||
|
if (!userSessions.some((s: any) => s.key === sessionKey)) {
|
||||||
|
setSessionKey(userSessions[0].key);
|
||||||
}
|
}
|
||||||
}, [sessions]);
|
}, [chatTab, sessionKey, userSessions]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex h-full">
|
<div className="flex h-full">
|
||||||
<div className="flex-1 flex flex-col bg-zinc-950/50">
|
<div className="flex-1 flex flex-col bg-zinc-950/50">
|
||||||
<div className="px-4 py-3 border-b border-zinc-800 flex items-center justify-between gap-3 flex-wrap">
|
<div className="px-4 py-3 border-b border-zinc-800 flex items-center justify-between gap-3 flex-wrap">
|
||||||
<div className="flex items-center gap-2">
|
<div className="flex items-center gap-2">
|
||||||
<h2 className="text-sm text-zinc-300 font-medium">{t('session')}</h2>
|
<button
|
||||||
<select value={sessionKey} onChange={(e)=>setSessionKey(e.target.value)} className="bg-zinc-900 border border-zinc-700 rounded px-2 py-1 text-xs text-zinc-200">
|
onClick={() => setChatTab('main')}
|
||||||
{(sessions || []).map((s:any)=> <option key={s.key} value={s.key}>{s.key}</option>)}
|
className={`px-3 py-1.5 rounded-lg text-xs ${chatTab === 'main' ? 'bg-indigo-600 text-white' : 'bg-zinc-900 border border-zinc-700 text-zinc-300'}`}
|
||||||
</select>
|
>
|
||||||
|
Main Chat
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
onClick={() => setChatTab('subagents')}
|
||||||
|
className={`px-3 py-1.5 rounded-lg text-xs ${chatTab === 'subagents' ? 'bg-amber-600 text-white' : 'bg-zinc-900 border border-zinc-700 text-zinc-300'}`}
|
||||||
|
>
|
||||||
|
{t('subagentGroup')}
|
||||||
|
</button>
|
||||||
|
{chatTab === 'main' && (
|
||||||
|
<select value={sessionKey} onChange={(e) => setSessionKey(e.target.value)} className="bg-zinc-900 border border-zinc-700 rounded px-2 py-1 text-xs text-zinc-200">
|
||||||
|
{userSessions.map((s: any) => <option key={s.key} value={s.key}>{s.title || s.key}</option>)}
|
||||||
|
</select>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
<button onClick={loadHistory} className="flex items-center gap-1 px-2 py-1 text-xs rounded bg-zinc-800 hover:bg-zinc-700"><RefreshCw className="w-3 h-3"/>{t('reloadHistory')}</button>
|
<button onClick={chatTab === 'main' ? loadHistory : loadSubagentGroup} className="flex items-center gap-1 px-2 py-1 text-xs rounded bg-zinc-800 hover:bg-zinc-700"><RefreshCw className="w-3 h-3"/>{t('reloadHistory')}</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="flex-1 overflow-y-auto p-6 space-y-6">
|
<div className="flex-1 overflow-y-auto p-6 space-y-6">
|
||||||
@@ -140,7 +212,7 @@ const Chat: React.FC = () => {
|
|||||||
<div className="w-16 h-16 rounded-2xl bg-zinc-900 flex items-center justify-center border border-zinc-800">
|
<div className="w-16 h-16 rounded-2xl bg-zinc-900 flex items-center justify-center border border-zinc-800">
|
||||||
<MessageSquare className="w-8 h-8 text-zinc-600" />
|
<MessageSquare className="w-8 h-8 text-zinc-600" />
|
||||||
</div>
|
</div>
|
||||||
<p className="text-sm font-medium">{t('startConversation')}</p>
|
<p className="text-sm font-medium">{chatTab === 'main' ? t('startConversation') : t('noSubagentStream')}</p>
|
||||||
</div>
|
</div>
|
||||||
) : (
|
) : (
|
||||||
chat.map((m, i) => {
|
chat.map((m, i) => {
|
||||||
@@ -203,13 +275,14 @@ const Chat: React.FC = () => {
|
|||||||
<input
|
<input
|
||||||
value={msg}
|
value={msg}
|
||||||
onChange={(e) => setMsg(e.target.value)}
|
onChange={(e) => setMsg(e.target.value)}
|
||||||
onKeyDown={(e) => e.key === 'Enter' && send()}
|
onKeyDown={(e) => chatTab === 'main' && e.key === 'Enter' && send()}
|
||||||
placeholder={t('typeMessage')}
|
placeholder={chatTab === 'main' ? t('typeMessage') : t('subagentGroupReadonly')}
|
||||||
className="w-full bg-zinc-900 border border-zinc-800 rounded-full pl-14 pr-14 py-3.5 text-[15px] focus:outline-none focus:border-indigo-500 focus:ring-1 focus:ring-indigo-500 transition-all placeholder:text-zinc-500 shadow-sm"
|
disabled={chatTab !== 'main'}
|
||||||
|
className="w-full bg-zinc-900 border border-zinc-800 rounded-full pl-14 pr-14 py-3.5 text-[15px] focus:outline-none focus:border-indigo-500 focus:ring-1 focus:ring-indigo-500 transition-all placeholder:text-zinc-500 shadow-sm disabled:opacity-60"
|
||||||
/>
|
/>
|
||||||
<button
|
<button
|
||||||
onClick={send}
|
onClick={send}
|
||||||
disabled={!msg.trim() && !fileSelected}
|
disabled={chatTab !== 'main' || (!msg.trim() && !fileSelected)}
|
||||||
className="absolute right-2 p-2.5 bg-indigo-600 hover:bg-indigo-500 disabled:opacity-50 disabled:hover:bg-indigo-600 text-white rounded-full transition-colors shadow-sm"
|
className="absolute right-2 p-2.5 bg-indigo-600 hover:bg-indigo-500 disabled:opacity-50 disabled:hover:bg-indigo-600 text-white rounded-full transition-colors shadow-sm"
|
||||||
>
|
>
|
||||||
<Send className="w-4 h-4 ml-0.5" />
|
<Send className="w-4 h-4 ml-0.5" />
|
||||||
|
|||||||
Reference in New Issue
Block a user