@blocksdiy/react-common 1.37.0 → 1.38.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -61,6 +61,14 @@ export interface AgentChatContextValue {
|
|
|
61
61
|
renderToolCall: ReturnType<typeof useRenderToolCall>;
|
|
62
62
|
sendMessage: (message: Omit<UserMessage, "id" | "role">) => Promise<void>;
|
|
63
63
|
sendFromInputs: () => Promise<void>;
|
|
64
|
+
/** Resumes a run parked on a server-side interrupt. Rejects if the resume fails. */
|
|
65
|
+
/** Resolves false when a run was already in flight, so nothing was sent. */
|
|
66
|
+
resolveInterrupt: (resume: unknown) => Promise<boolean>;
|
|
67
|
+
/** Answers one parked card; resumes once every card of the turn is answered. */
|
|
68
|
+
answerInterrupt: (answer: {
|
|
69
|
+
toolCallId: string;
|
|
70
|
+
answer: unknown;
|
|
71
|
+
}) => Promise<void>;
|
|
64
72
|
stopGeneration: () => void;
|
|
65
73
|
addAttachments: (attachments: Attachment[]) => void;
|
|
66
74
|
removeAttachment: (url: string) => void;
|
|
@@ -95,6 +103,17 @@ export interface AgentChatRootProps {
|
|
|
95
103
|
* `agent-self-modify` flag) — never in the published app.
|
|
96
104
|
*/
|
|
97
105
|
selfEditEnabled?: boolean;
|
|
106
|
+
/**
|
|
107
|
+
* Set by a chat surface that carries renderers for the server-declared pauses
|
|
108
|
+
* (`ask_user` and `ask-component-…`). The backend offers those tools ONLY when
|
|
109
|
+
* this is true, and then strips their browser-side predecessors.
|
|
110
|
+
*
|
|
111
|
+
* It must originate in the surface's own component, not here: this package ships
|
|
112
|
+
* with the platform and is identical everywhere, while each app carries its own
|
|
113
|
+
* frozen copy of the chat component — and that copy is the only thing that differs
|
|
114
|
+
* between an app that can draw these pauses and one that cannot.
|
|
115
|
+
*/
|
|
116
|
+
supportsServerInterrupt?: boolean;
|
|
98
117
|
}
|
|
99
118
|
export declare const AgentChatRoot: ({ ...props }: AgentChatRootProps) => import("react").JSX.Element | null;
|
|
100
119
|
export interface AgentChatMessageProps {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"new-agent-chat.d.ts","sourceRoot":"","sources":["../../src/components/new-agent-chat.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,OAAO,EAAE,WAAW,EAAE,KAAK,WAAW,EAAE,MAAM,eAAe,CAAC;AAI5E,OAAO,EAAiB,iBAAiB,EAAE,MAAM,2BAA2B,CAAC;AAC7E,OAAO,EAEL,SAAS,EACT,cAAc,EAQf,MAAM,OAAO,CAAC;AAGf,OAAO,EAAiB,KAAK,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAEzE,OAAO,EAAE,KAAK,OAAO,EAAE,KAAK,gBAAgB,EAAE,KAAK,WAAW,EAAE,MAAM,eAAe,CAAC;AACtF,OAAO,EACL,aAAa,EACb,oBAAoB,EACpB,iBAAiB,EACjB,eAAe,EACf,cAAc,GACf,MAAM,2BAA2B,CAAC;AAOnC,MAAM,WAAW,UAAU;IACzB,GAAG,EAAE,MAAM,CAAC;IACZ,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,kBAAkB;IACjC,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAChC,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;;;OAIG;IACH,aAAa,CAAC,EAAE,OAAO,CAAC;CACzB;AAED,eAAO,MAAM,6BAA6B,GAAI,MAAM,MAAM,WAEzD,CAAC;AAEF,MAAM,WAAW,SAAS;IACxB,aAAa,CAAC,EAAE;QAAE,OAAO,CAAC,EAAE,MAAM,CAAC;QAAC,aAAa,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;IAC7D,qCAAqC,CAAC,EAAE,OAAO,CAAC;IAChD,WAAW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AAC,EAAE,OAAO,CAAC;IAClB,MAAM,CAAC,EAAE,UAAU,CAAC;IACpB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB;AAED,wBAAgB,0BAA0B,CAAC,EACzC,OAAe,EACf,MAAM,EACN,OAAO,EACP,QAAQ,EACR,MAAM,EACN,QAAe,EACf,GAAG,KAAK,EACT,EAAE,KAAK,CAAC,cAAc,CAAC,QAAQ,CAAC,GAAG,+BAA+B,+BAmElE;AAED,MAAM,WAAW,wBAAwB;IACvC,UAAU,EAAE,UAAU,CAAC;IACvB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,MAAM,CAAC,EAAE,UAAU,CAAC;CACrB;AAED,wBAAgB,mBAAmB,CAAC,EAClC,UAAU,EACV,OAAe,EACf,MAAM,EACN,GAAG,KAAK,EACT,EAAE,KAAK,CAAC,cAAc,CAAC,KAAK,CAAC,GAAG,wBAAwB,+BAWxD;AAED,MAAM,WAAW,8BAA8B;IAC7C,UAAU,EAAE,UAAU,CAAC;IACvB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,MAAM,CAAC,EAAE,UAAU,CAAC;CACrB;AAED,wBAAgB,yBAAyB,CAAC,EACxC,UAAU,EACV,OAAe,EACf,MAAM,EACN,OAAO,EACP,GAAG,KAAK,EACT,EAAE,KAAK,CAAC,cAAc,CAAC,QAAQ,CAAC,GAAG,8BAA8B,+BAgBjE"}
|
|
@@ -26,9 +26,7 @@ const generateId = (length = 10) => {
|
|
|
26
26
|
.toString(36)
|
|
27
27
|
.substring(2, 2 + length);
|
|
28
28
|
};
|
|
29
|
-
// Assistant tool calls with no matching ToolMessage
|
|
30
|
-
// frontend tools (e.g. user-choice) reach this state; backend tools resolve in
|
|
31
|
-
// the same run. Used to auto-skip dangling answer UIs when the user types past.
|
|
29
|
+
// Assistant tool calls with no matching ToolMessage.
|
|
32
30
|
const _collectPendingToolCalls = (messages) => {
|
|
33
31
|
const toolMessageIds = new Set();
|
|
34
32
|
for (const m of messages) {
|
|
@@ -49,6 +47,35 @@ const _collectPendingToolCalls = (messages) => {
|
|
|
49
47
|
}
|
|
50
48
|
return pending;
|
|
51
49
|
};
|
|
50
|
+
// Message order IS the order of the interrupt's `action_requests`, so decisions
|
|
51
|
+
// pair with them by index and need no ids. Matched by name rather than by "has no
|
|
52
|
+
// local handler", so a backend tool merely mid-flight is never taken for a pause.
|
|
53
|
+
const _SERVER_PAUSE_TOOL_NAME = "ask_user";
|
|
54
|
+
const _SERVER_PAUSE_TOOL_PREFIX = "ask-component-";
|
|
55
|
+
const _isServerPause = (toolCall) => toolCall.function.name === _SERVER_PAUSE_TOOL_NAME || toolCall.function.name.startsWith(_SERVER_PAUSE_TOOL_PREFIX);
|
|
56
|
+
// ONE assistant message's pauses, never the transcript's: the server batches a
|
|
57
|
+
// turn's pauses into a single interrupt requiring exactly one decision each, and a
|
|
58
|
+
// transcript can also hold pauses from a run that died before parking. Counting
|
|
59
|
+
// those raises the count check — permanently, since the checkpoint never advances.
|
|
60
|
+
const _collectServerPauses = (messages) => {
|
|
61
|
+
const answered = new Set();
|
|
62
|
+
for (const m of messages) {
|
|
63
|
+
if (m.role === "tool" && "toolCallId" in m && typeof m.toolCallId === "string") {
|
|
64
|
+
answered.add(m.toolCallId);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
68
|
+
const m = messages[i];
|
|
69
|
+
if (m.role !== "assistant" || !("toolCalls" in m) || !m.toolCalls) {
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
const pauses = m.toolCalls.filter((toolCall) => !answered.has(toolCall.id) && _isServerPause(toolCall));
|
|
73
|
+
if (pauses.length > 0) {
|
|
74
|
+
return pauses;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return [];
|
|
78
|
+
};
|
|
52
79
|
// CopilotKit 1.60.x: `connectAgent()` awaits every replayed HITL tool call's
|
|
53
80
|
// handler before resolving `isAvailable`, and a `useHumanInTheLoop` handler
|
|
54
81
|
// only resolves on `respond()` — so a transcript replayed with an unanswered
|
|
@@ -281,6 +308,7 @@ const ChatInitializer = ({ agentBlock, agentChatBlock, chatId, token, appId, com
|
|
|
281
308
|
});
|
|
282
309
|
return groups;
|
|
283
310
|
}, [copilotMessages]);
|
|
311
|
+
const interruptAnswersRef = useRef(new Map());
|
|
284
312
|
// Mirror `currentThreadId` onto the agent instance — the `<CopilotKit>`
|
|
285
313
|
// prop only feeds React context, but `agent.threadId` is what hits the
|
|
286
314
|
// wire and the LangGraph checkpoint. `<CopilotChat>` does this for you;
|
|
@@ -292,6 +320,9 @@ const ChatInitializer = ({ agentBlock, agentChatBlock, chatId, token, appId, com
|
|
|
292
320
|
if (copilotAgent && currentThreadId) {
|
|
293
321
|
copilotAgent.threadId = currentThreadId;
|
|
294
322
|
}
|
|
323
|
+
// Carrying answers across a thread swap would pair them with another
|
|
324
|
+
// conversation's pause.
|
|
325
|
+
interruptAnswersRef.current.clear();
|
|
295
326
|
}, [copilotAgent, currentThreadId]);
|
|
296
327
|
const [prompt, setPrompt] = useState("");
|
|
297
328
|
const [attachments, setAttachments] = useState([]);
|
|
@@ -302,9 +333,9 @@ const ChatInitializer = ({ agentBlock, agentChatBlock, chatId, token, appId, com
|
|
|
302
333
|
// CopilotKit reports `isLoading` during connect/replay as well as real runs.
|
|
303
334
|
// Only expose thinking once the bootstrap connect phase has completed.
|
|
304
335
|
const isThinking = isAgentRunning && !isFetchingMessages;
|
|
305
|
-
const runAgentWithRetry = useCallback(async (agentToRun, messageCountAtRunStart) => {
|
|
336
|
+
const runAgentWithRetry = useCallback(async (agentToRun, messageCountAtRunStart, forwardedProps) => {
|
|
306
337
|
if (agentToRun.isRunning) {
|
|
307
|
-
return;
|
|
338
|
+
return false;
|
|
308
339
|
}
|
|
309
340
|
const runAgentAndThrowError = async () => {
|
|
310
341
|
let runEventError;
|
|
@@ -317,7 +348,7 @@ const ChatInitializer = ({ agentBlock, agentChatBlock, chatId, token, appId, com
|
|
|
317
348
|
},
|
|
318
349
|
});
|
|
319
350
|
try {
|
|
320
|
-
await copilotkit.runAgent({ agent: agentToRun });
|
|
351
|
+
await copilotkit.runAgent({ agent: agentToRun, forwardedProps });
|
|
321
352
|
if (runEventError) {
|
|
322
353
|
throw runEventError;
|
|
323
354
|
}
|
|
@@ -334,18 +365,75 @@ const ChatInitializer = ({ agentBlock, agentChatBlock, chatId, token, appId, com
|
|
|
334
365
|
const status = getAgentRunErrorStatus(error);
|
|
335
366
|
if (status !== undefined && status >= 500 && status < 600 && !hasAgentStartedResponding) {
|
|
336
367
|
await runAgentAndThrowError();
|
|
337
|
-
return;
|
|
368
|
+
return true;
|
|
338
369
|
}
|
|
339
370
|
throw error;
|
|
340
371
|
}
|
|
372
|
+
return true;
|
|
341
373
|
}, [copilotkit]);
|
|
342
|
-
const sendMessage = useCallback((message) => {
|
|
374
|
+
const sendMessage = useCallback(async (message) => {
|
|
343
375
|
if (!canSendMessage || !copilotAgent || copilotAgent.isRunning) {
|
|
344
|
-
return
|
|
376
|
+
return;
|
|
345
377
|
}
|
|
346
378
|
copilotAgent.addMessage({ ...message, id: generateId(), role: "user" });
|
|
347
|
-
|
|
379
|
+
await runAgentWithRetry(copilotAgent, copilotAgent.messages.length);
|
|
348
380
|
}, [canSendMessage, copilotAgent, runAgentWithRetry]);
|
|
381
|
+
// Not sourced from `useInterrupt`: that hook only hands `resolve` to its own
|
|
382
|
+
// `render`, which it skips whenever nothing is pending — so after a reload there
|
|
383
|
+
// is no way to obtain it. `runAgentWithRetry`'s `isRunning` guard makes a
|
|
384
|
+
// double-click a no-op rather than a duplicate resume.
|
|
385
|
+
const resolveInterrupt = useCallback(async (resume) => {
|
|
386
|
+
if (!copilotAgent) {
|
|
387
|
+
return false;
|
|
388
|
+
}
|
|
389
|
+
return runAgentWithRetry(copilotAgent, copilotAgent.messages.length, { command: { resume } });
|
|
390
|
+
}, [copilotAgent, runAgentWithRetry]);
|
|
391
|
+
// Resumes only once EVERY pause of the turn has an answer: a short decisions list
|
|
392
|
+
// fails `HumanInTheLoopMiddleware`'s count check, and the checkpoint never
|
|
393
|
+
// advances, so every later answer fails identically.
|
|
394
|
+
const answerInterrupt = useCallback(async ({ toolCallId, answer }) => {
|
|
395
|
+
if (!copilotAgent) {
|
|
396
|
+
return;
|
|
397
|
+
}
|
|
398
|
+
const pauses = _collectServerPauses(copilotAgent.messages);
|
|
399
|
+
// Thrown, never returned: a caller resets its submitting state on a rejection,
|
|
400
|
+
// while a silent return leaves the card locked with the answer discarded.
|
|
401
|
+
if (!pauses.some((toolCall) => toolCall.id === toolCallId)) {
|
|
402
|
+
throw new Error(`answerInterrupt: ${toolCallId} is not a pending pause of this turn`);
|
|
403
|
+
}
|
|
404
|
+
const answers = interruptAnswersRef.current;
|
|
405
|
+
answers.set(toolCallId, answer);
|
|
406
|
+
const unanswered = pauses.filter((toolCall) => !answers.has(toolCall.id));
|
|
407
|
+
if (unanswered.length > 0) {
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
const decisions = pauses.map((toolCall) => ({
|
|
411
|
+
type: "respond",
|
|
412
|
+
// `?? null` because a component may call `respond()` with no payload, and
|
|
413
|
+
// `JSON.stringify(undefined)` drops the key, which raises server-side.
|
|
414
|
+
message: JSON.stringify(answers.get(toolCall.id) ?? null),
|
|
415
|
+
}));
|
|
416
|
+
const sent = new Map(answers);
|
|
417
|
+
answers.clear();
|
|
418
|
+
const restore = () => sent.forEach((value, id) => answers.set(id, value));
|
|
419
|
+
let resumed = false;
|
|
420
|
+
try {
|
|
421
|
+
// Every answer goes back, not just this call's, or a resume that never
|
|
422
|
+
// happened discards the rest of the turn's cards with it.
|
|
423
|
+
resumed = await resolveInterrupt({ decisions });
|
|
424
|
+
}
|
|
425
|
+
catch (error) {
|
|
426
|
+
restore();
|
|
427
|
+
throw error;
|
|
428
|
+
}
|
|
429
|
+
if (!resumed) {
|
|
430
|
+
// A run was already in flight, so nothing was sent: `isThinking` lags
|
|
431
|
+
// `agent.isRunning` by a commit, which leaves `respond` live for a moment
|
|
432
|
+
// after a run starts.
|
|
433
|
+
restore();
|
|
434
|
+
throw new Error("answerInterrupt: a run was already in flight; answer kept for retry");
|
|
435
|
+
}
|
|
436
|
+
}, [copilotAgent, resolveInterrupt]);
|
|
349
437
|
const stopGeneration = useCallback(() => {
|
|
350
438
|
copilotStopGeneration();
|
|
351
439
|
copilotAgent?.abortRun?.();
|
|
@@ -526,8 +614,13 @@ const ChatInitializer = ({ agentBlock, agentChatBlock, chatId, token, appId, com
|
|
|
526
614
|
// resolve the parked Promise itself — it lingers in memory until page
|
|
527
615
|
// reload. Acceptable: one closure per skip, cleared on refresh, no
|
|
528
616
|
// functional impact.
|
|
617
|
+
//
|
|
618
|
+
// Only tool calls THIS frontend answers. A `getTool` miss means the tool runs
|
|
619
|
+
// on the backend (`useRenderTool` registers a renderer, not a tool), so an
|
|
620
|
+
// unanswered one is a parked graph — faking its result would tell the server
|
|
621
|
+
// the question was answered and it would never resume.
|
|
529
622
|
if (copilotAgent) {
|
|
530
|
-
const pending = _collectPendingToolCalls(copilotAgent.messages);
|
|
623
|
+
const pending = _collectPendingToolCalls(copilotAgent.messages).filter((toolCall) => Boolean(copilotkit.getTool({ toolName: toolCall.function.name })));
|
|
531
624
|
for (const toolCall of pending) {
|
|
532
625
|
copilotAgent.addMessage({
|
|
533
626
|
id: generateId(),
|
|
@@ -574,6 +667,8 @@ const ChatInitializer = ({ agentBlock, agentChatBlock, chatId, token, appId, com
|
|
|
574
667
|
attachments,
|
|
575
668
|
sendMessage,
|
|
576
669
|
sendFromInputs,
|
|
670
|
+
resolveInterrupt,
|
|
671
|
+
answerInterrupt,
|
|
577
672
|
stopGeneration,
|
|
578
673
|
addAttachments,
|
|
579
674
|
removeAttachment,
|
|
@@ -597,7 +692,7 @@ export const AgentChatRoot = ({ ...props
|
|
|
597
692
|
// memoryEnabled = false,
|
|
598
693
|
// shortTermMemory,
|
|
599
694
|
}) => {
|
|
600
|
-
const { appId, token, children, agentId, agentHarness, useAgentBlockDirectChat, agentChatId, componentIds, chatId, defaultThreadId, noPersistency, chatContext, chatContextFiles, selfEditEnabled, } = props;
|
|
695
|
+
const { appId, token, children, agentId, agentHarness, useAgentBlockDirectChat, agentChatId, componentIds, chatId, defaultThreadId, noPersistency, chatContext, chatContextFiles, selfEditEnabled, supportsServerInterrupt, } = props;
|
|
601
696
|
const socketId = websocketsService.getSocketId();
|
|
602
697
|
const headers = useMemo(() => ({
|
|
603
698
|
...(token ? { Authorization: `Bearer ${token}` } : {}),
|
|
@@ -610,6 +705,12 @@ export const AgentChatRoot = ({ ...props
|
|
|
610
705
|
const [agentChatBlock, setAgentChatBlock] = useState(null);
|
|
611
706
|
const [agentBlock, setAgentBlock] = useState(null);
|
|
612
707
|
const [components, setComponents] = useState([]);
|
|
708
|
+
// Chat components reach the backend nowhere else on the direct-chat path.
|
|
709
|
+
// Withheld unless this surface can render a server-parked one, so an older surface
|
|
710
|
+
// keeps its browser-side pause and pays no extra block fetch.
|
|
711
|
+
const serverInterruptComponentIds = useMemo(() => supportsServerInterrupt
|
|
712
|
+
? components.filter((component) => component.userInterrupt).map((component) => component.id)
|
|
713
|
+
: [], [components, supportsServerInterrupt]);
|
|
613
714
|
const chatContextStr = chatContext == null ? undefined : typeof chatContext === "string" ? chatContext : JSON.stringify(chatContext);
|
|
614
715
|
const getAgentChatBlock = useCallback(async () => {
|
|
615
716
|
if (!agentChatId) {
|
|
@@ -692,6 +793,8 @@ export const AgentChatRoot = ({ ...props
|
|
|
692
793
|
chatContext: chatContextStr,
|
|
693
794
|
chatContextFiles,
|
|
694
795
|
...(selfEditEnabled ? { selfEditEnabled: true } : {}),
|
|
796
|
+
...(supportsServerInterrupt ? { supportsServerInterrupt: true } : {}),
|
|
797
|
+
...(serverInterruptComponentIds.length ? { serverInterruptComponentIds } : {}),
|
|
695
798
|
},
|
|
696
799
|
// Explicit false: when undefined, CopilotKit auto-enables its inspector
|
|
697
800
|
// devtools on localhost hostnames.
|