Spaces:
Paused
Paused
File size: 12,847 Bytes
0b9dc2e | 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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 | import { EventType } from '@agentscope-ai/agentscope/event';
import type {
AgentEvent,
CustomEvent,
DataBlockStartEvent,
DataBlockDeltaEvent,
DataBlockEndEvent,
ReplyStartEvent,
UserConfirmResultEvent,
} from '@agentscope-ai/agentscope/event';
import { appendEvent, AssistantMsg, UserMsg } from '@agentscope-ai/agentscope/message';
import type { Msg, ContentBlock } from '@agentscope-ai/agentscope/message';
import type { ToolCallBlock } from '@agentscope-ai/agentscope/message';
import { useState, useCallback, useRef, useEffect } from 'react';
import { sessionApi } from '@/api';
import { chatApi } from '@/api';
import { useAudioManager } from '@/context/AudioContext';
/**
* One pending subagent HITL request, projected from a team *member*
* session onto its *leader* session so the leader UI can render and
* resolve it. Mirrors the Python payload written by
* ``SubagentHitlProjector`` and pushed/replayed as a ``CustomEvent``
* (``name="subagent_require_user_confirm"``).
*/
export type SubagentHitlEntry = {
worker_session_id: string;
worker_agent_id: string;
worker_agent_name: string;
reply_id: string;
event_type: 'require_user_confirm' | 'require_external_execution';
/** The original ``RequireUserConfirmEvent`` payload (serialized). */
event: { tool_calls?: ToolCallBlock[] } & Record<string, unknown>;
created_at: string;
};
const hitlKey = (e: { worker_session_id: string; reply_id: string }) =>
`${e.worker_session_id}:${e.reply_id}`;
/**
* Manages messages for a single ``(agentId, sessionId)`` pair.
*
* Event delivery has two independent channels:
*
* - **History** β ``GET /sessions/{sid}/messages`` fetches persisted
* ``Msg`` objects (each a complete reply).
* - **Live stream** β ``GET /sessions/{sid}/stream`` is a long-lived
* SSE connection that pushes ``AgentEvent`` deltas as they are
* produced by any chat run on this session (user-triggered,
* background retrigger, team member message, β¦).
*
* The hook opens the SSE connection immediately after fetching
* history. User input and human-in-the-loop confirmations are sent
* via ``POST /chat/`` (fire-and-forget); the resulting events arrive
* through the already-open SSE connection.
*
* ``streaming`` is driven by event content, not HTTP lifecycle:
* ``true`` after receiving ``ReplyStartEvent``, ``false`` after
* ``ReplyEndEvent``.
*
* @param agentId - The agent whose session to subscribe. ``null`` to
* skip.
* @param sessionId - The session to subscribe. ``null`` to skip.
* @returns Object with ``msgs``, ``loading``, ``streaming``, ``error``,
* ``send``, ``onUserConfirm``, and ``abort``.
*/
export function useMessages(
agentId: string | null,
sessionId: string | null,
options?: {
/**
* Called when a ``CUSTOM`` event with ``name="team_updated"``
* arrives β the team membership has changed (TeamCreate /
* AgentCreate / TeamDelete ran). The typical response is to
* refetch the session list so the team sidebar updates.
*/
onTeamUpdated?: () => void;
/**
* Called when a ``CUSTOM`` event with ``name="state_updated"``
* arrives β agent state (tasks / permission) changed during a
* tool call. The ``value`` payload contains the latest
* ``tasks_context`` and ``permission_context``.
*/
onStateUpdated?: (value: Record<string, unknown>) => void;
},
) {
const [msgs, setMsgs] = useState<Msg[]>([]);
const [loading, setLoading] = useState(false);
const [streaming, setStreaming] = useState(false);
const [error, setError] = useState<Error | null>(null);
// Pending subagent HITL cards projected onto this (leader) session.
const [subagentHitl, setSubagentHitl] = useState<SubagentHitlEntry[]>([]);
const msgsRef = useRef<Msg[]>([]);
const currentReplyRef = useRef<Msg | null>(null);
const abortRef = useRef<AbortController | null>(null);
const rafRef = useRef<number | null>(null);
const audioManager = useAudioManager();
const optionsRef = useRef(options);
useEffect(() => {
optionsRef.current = options;
}, [options]);
const scheduleUpdate = useCallback(() => {
if (rafRef.current !== null) return;
rafRef.current = requestAnimationFrame(() => {
rafRef.current = null;
setMsgs([...msgsRef.current]);
});
}, []);
/** Apply a single AgentEvent to the in-progress reply. */
const processEvent = useCallback(
(event: AgentEvent) => {
// Custom events are service-layer notifications, not agent
// reply content β route them to callbacks and skip appendEvent.
if (event.type === EventType.CUSTOM) {
const custom = event as CustomEvent;
if (custom.name === 'team_updated') {
optionsRef.current?.onTeamUpdated?.();
} else if (custom.name === 'state_updated' && custom.value) {
optionsRef.current?.onStateUpdated?.(custom.value as Record<string, unknown>);
} else if (custom.name === 'subagent_require_user_confirm') {
// A team member is asking for confirmation; show (or
// refresh) its card on this leader view. Dedup by
// (worker_session_id, reply_id).
const e = custom.value as unknown as SubagentHitlEntry;
setSubagentHitl((prev) => [
...prev.filter((x) => hitlKey(x) !== hitlKey(e)),
e,
]);
} else if (custom.name === 'subagent_user_confirm_result') {
// The member resolved (or its run ended); clear the card.
const v = custom.value as { worker_session_id: string; reply_id: string };
setSubagentHitl((prev) => prev.filter((x) => hitlKey(x) !== hitlKey(v)));
}
return;
}
if (event.type === EventType.REPLY_START) {
audioManager?.stopAllPlayback();
const e = event as ReplyStartEvent;
const msg = AssistantMsg({ id: e.reply_id, name: e.name, content: [] });
msgsRef.current = [...msgsRef.current, msg];
currentReplyRef.current = msg;
setStreaming(true);
} else if (event.type === EventType.REPLY_END) {
if (currentReplyRef.current) {
appendEvent(currentReplyRef.current, event);
}
setStreaming(false);
currentReplyRef.current = null;
} else if (currentReplyRef.current) {
appendEvent(currentReplyRef.current, event);
}
// Route streaming audio DataBlocks to the audio manager. They still
// flow through `appendEvent` above (which builds up `source.data`
// in the Msg), but MessageBubble reads playback state from the
// manager so it can show progress and autoplay on completion.
if (audioManager) {
if (event.type === EventType.DATA_BLOCK_START) {
const e = event as DataBlockStartEvent;
if (e.media_type.startsWith('audio/')) {
audioManager.start(e.block_id, e.media_type);
}
} else if (event.type === EventType.DATA_BLOCK_DELTA) {
const e = event as DataBlockDeltaEvent;
if (e.media_type.startsWith('audio/')) {
audioManager.append(e.block_id, e.data);
}
} else if (event.type === EventType.DATA_BLOCK_END) {
const e = event as DataBlockEndEvent;
// `end` is a no-op when the block isn't being tracked, so
// we can call it unconditionally.
audioManager.end(e.block_id);
}
}
scheduleUpdate();
},
[scheduleUpdate, audioManager],
);
// ββ Lifecycle: fetch history + open SSE stream ββββββββββββββββββ
useEffect(() => {
msgsRef.current = [];
currentReplyRef.current = null;
setMsgs([]);
setError(null);
setStreaming(false);
setSubagentHitl([]);
audioManager?.disposeAll();
if (!agentId || !sessionId) return;
const controller = new AbortController();
abortRef.current = controller;
let cancelled = false;
(async () => {
// 1. Fetch persisted history
setLoading(true);
try {
const { messages } = await sessionApi.messages(sessionId, agentId);
if (cancelled) return;
msgsRef.current = messages;
scheduleUpdate();
} catch (e) {
if (!cancelled) setError(e as Error);
return;
} finally {
if (!cancelled) setLoading(false);
}
// 2. Open SSE long connection for live events
try {
for await (const event of sessionApi.streamEvents(
sessionId,
agentId,
controller.signal,
)) {
if (cancelled) break;
processEvent(event);
}
} catch (e) {
if ((e as Error).name !== 'AbortError' && !cancelled) {
setError(e as Error);
}
}
})();
return () => {
cancelled = true;
controller.abort();
abortRef.current = null;
};
}, [agentId, sessionId, scheduleUpdate, processEvent, audioManager]);
/**
* Send a user message. Appends the message to the local list
* optimistically, then fires a ``POST /chat/`` trigger. Events
* arrive via the already-open SSE connection.
*
* @param content - The message content blocks.
*/
const send = useCallback(
async (content: ContentBlock[]) => {
if (!agentId || !sessionId) return;
const userMsg = UserMsg({ name: 'user', content });
msgsRef.current = [...msgsRef.current, userMsg];
scheduleUpdate();
try {
await chatApi.trigger({
agent_id: agentId,
session_id: sessionId,
input: userMsg,
});
} catch (e) {
setError(e as Error);
}
},
[agentId, sessionId, scheduleUpdate],
);
/**
* Confirm or deny a tool call (human-in-the-loop). Fires a
* ``POST /chat/`` with a ``UserConfirmResultEvent``; events
* arrive via SSE.
*
* @param toolCall - The tool call block to confirm/deny.
* @param confirm - Whether the user confirmed.
* @param replyId - The reply id the tool call belongs to.
* @param rules - Optional permission rules to attach.
*/
const onUserConfirm = useCallback(
async (
toolCall: ToolCallBlock,
confirm: boolean,
replyId: string,
rules?: ToolCallBlock['suggested_rules'],
) => {
if (!agentId || !sessionId) return;
// Restore the ref so continuation events (no REPLY_START)
// have a target.
currentReplyRef.current = msgsRef.current.find((m) => m.id === replyId) ?? null;
const event: UserConfirmResultEvent = {
type: EventType.USER_CONFIRM_RESULT,
id: crypto.randomUUID(),
created_at: new Date().toISOString(),
reply_id: replyId,
confirm_results: [
{ confirmed: confirm, tool_call: toolCall, rules: rules ?? null },
],
};
try {
await chatApi.trigger({
agent_id: agentId,
session_id: sessionId,
input: event,
});
} catch (e) {
setError(e as Error);
}
},
[agentId, sessionId],
);
/** Abort the current SSE connection. */
const abort = useCallback(() => {
abortRef.current?.abort();
}, []);
/**
* Confirm or deny a tool call that a *team member* is awaiting,
* from this leader view (design Β§3.6 β backend routing).
*
* The result is POSTed to the **leader** session (the
* ``(agentId, sessionId)`` this hook is bound to), NOT the worker.
* The backend resolves ``reply_id`` β worker session via the
* leader's pending hash and forwards the event to the worker's
* continuation. The client never addresses the worker directly β
* ``entry.worker_*`` ids are used only for local dedup / clearing.
*
* @param entry - The pending subagent HITL entry being resolved.
* @param toolCall - The tool call block to confirm/deny.
* @param confirm - Whether the user confirmed.
* @param rules - Optional permission rules to attach.
*/
const onSubagentConfirm = useCallback(
async (
entry: SubagentHitlEntry,
toolCall: ToolCallBlock,
confirm: boolean,
rules?: ToolCallBlock['suggested_rules'],
) => {
if (!agentId || !sessionId) return;
const event: UserConfirmResultEvent = {
type: EventType.USER_CONFIRM_RESULT,
id: crypto.randomUUID(),
created_at: new Date().toISOString(),
reply_id: entry.reply_id, // worker's reply_id; backend maps it
confirm_results: [
{ confirmed: confirm, tool_call: toolCall, rules: rules ?? null },
],
};
// Optimistically clear; the backend's clear event re-confirms.
setSubagentHitl((prev) => prev.filter((x) => hitlKey(x) !== hitlKey(entry)));
try {
// Post to the leader front door β backend routes to the
// worker session (Β§3.6). Do NOT address the worker here.
await chatApi.trigger({
agent_id: agentId,
session_id: sessionId,
input: event,
});
} catch (e) {
setError(e as Error);
}
},
[agentId, sessionId],
);
return {
msgs,
loading,
streaming,
error,
send,
onUserConfirm,
onSubagentConfirm,
subagentHitl,
abort,
};
}
|