mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-19 14:33:38 +00:00
* feat: stream detached subagent activity * fix: annotate activity stream limits * fix: isolate subagent activity imports * fix: harden detached subagent activity lifecycle * test: cover synchronous activity transport failure * test: include required subagent activity identity * fix: identify and reconnect subagent activity events * fix: bound subagent activity lifecycles * fix: close subagent activity handoff races * fix: bind and synchronize activity subscriptions * fix: detect fresh activity attachment * fix: complete activity synchronization handoff * fix: bind activity sync and failure circuits * fix: expose subscription-bound synchronization * fix: fence activity reconnect publications * test: make detached timeout settlement deterministic * fix: fence Redis activity attachments * fix: close failed activity streams * perf: reuse fenced activity frontier * style: sort subagent thread imports * fix: preserve queued subagent activity * test: type activity publication counter * fix: disconnect subagent activity subscriber * fix: close background activity lifecycle gaps * fix: preserve streamed activity spacing * fix: preserve bounded live subagent activity * fix: merge durable subagent activity safely * fix: model detached activity coverage * fix: type detached activity inputs * fix: order overlapping subagent activity * chore: sort activity test imports * fix: buffer subagent activity handoff gaps * fix: flush activity after parent close * fix: advance closed activity suffixes * fix: preserve detached activity ordering * fix: close detached activity delivery races * fix: bound shared Redis subscriber readiness * fix: expire shared Redis subscription readiness * fix: clean up late Redis subscriptions * fix: preserve late Redis subscription fallback
171 lines
6.6 KiB
TypeScript
171 lines
6.6 KiB
TypeScript
import { useCallback, useEffect, useMemo, useRef } from 'react';
|
|
import { Bot, X } from 'lucide-react';
|
|
import { Button, useMediaQuery } from '@librechat/client';
|
|
import { useRecoilValue, useResetRecoilState } from 'recoil';
|
|
import type { ActiveSubagentPanel } from '~/store/subagents';
|
|
import {
|
|
activeSubagentPanel,
|
|
subagentProgressByToolCallId,
|
|
subagentProgressKey,
|
|
} from '~/store/subagents';
|
|
import useSubagentActivityStream from '~/data-provider/Subagents/useSubagentActivityStream';
|
|
import { subagentThreadHasTaskEvidence, useSubagentThreadQuery } from '~/data-provider';
|
|
import { adaptDurableThreadActivity, adaptLivePersistedActivity } from './adapters';
|
|
import ApprovalProvider from '~/components/Chat/Messages/Content/ApprovalContext';
|
|
import { useFocusTrap, useLocalize } from '~/hooks';
|
|
import SubagentActivity from './SubagentActivity';
|
|
|
|
export default function SubagentThreadPanel({ selection }: { selection: ActiveSubagentPanel }) {
|
|
const localize = useLocalize();
|
|
const panelRef = useRef<HTMLDivElement>(null);
|
|
const isMobile = useMediaQuery('(max-width: 767px)');
|
|
const resetSelection = useResetRecoilState(activeSubagentPanel);
|
|
const progress = useRecoilValue(
|
|
subagentProgressByToolCallId(
|
|
subagentProgressKey(selection.parentMessageId, selection.toolCallId, selection.partIndex),
|
|
),
|
|
);
|
|
const foregroundTitle =
|
|
selection.subagentType === 'self'
|
|
? localize('com_ui_subagent_dialog_title_self')
|
|
: localize('com_ui_subagent_dialog_title', { 0: selection.subagentType });
|
|
const threadId = selection.durable?.threadId ?? '';
|
|
const taskId = selection.durable?.taskId ?? '';
|
|
const { data, isLoading, isError, isReadinessPending } = useSubagentThreadQuery(
|
|
selection.parentConversationId,
|
|
threadId,
|
|
taskId,
|
|
);
|
|
const durableTerminal =
|
|
subagentThreadHasTaskEvidence(data, taskId) &&
|
|
(data?.status === 'completed' ||
|
|
data?.status === 'failed' ||
|
|
data?.status === 'interrupted' ||
|
|
data?.status === 'cancelled');
|
|
useSubagentActivityStream(selection, !durableTerminal);
|
|
const detachedLiveSubmitting =
|
|
selection.durable != null &&
|
|
progress != null &&
|
|
progress.status !== 'stop' &&
|
|
progress.status !== 'error';
|
|
|
|
const close = useCallback(() => {
|
|
resetSelection();
|
|
requestAnimationFrame(() => {
|
|
const trigger = Array.from(
|
|
document.querySelectorAll<HTMLElement>('[data-subagent-tool-call]'),
|
|
).find(
|
|
(element) =>
|
|
element.dataset.subagentToolCall === selection.toolCallId &&
|
|
element.dataset.subagentParentMessage === selection.parentMessageId &&
|
|
element.dataset.subagentPartIndex === String(selection.partIndex),
|
|
);
|
|
trigger?.focus();
|
|
});
|
|
}, [resetSelection, selection.parentMessageId, selection.partIndex, selection.toolCallId]);
|
|
|
|
useFocusTrap(panelRef, isMobile, close);
|
|
|
|
useEffect(() => {
|
|
const activeElement = document.activeElement;
|
|
if (!isMobile || !(activeElement instanceof HTMLElement)) return;
|
|
return () => {
|
|
if (activeElement.isConnected) activeElement.focus();
|
|
};
|
|
}, [isMobile]);
|
|
|
|
const liveActivity = useMemo(
|
|
() =>
|
|
adaptLivePersistedActivity({
|
|
title: foregroundTitle,
|
|
prompt: selection.prompt,
|
|
progress,
|
|
persistedContent: selection.persistedContent,
|
|
isDetached: selection.durable != null,
|
|
legacyOutput: selection.legacyOutput,
|
|
// A detached parent tool step closes as soon as dispatch succeeds;
|
|
// its terminal status does not describe the still-running child.
|
|
initialProgress: selection.durable == null ? selection.initialProgress : 0,
|
|
isSubmitting: selection.durable == null ? selection.isSubmitting : detachedLiveSubmitting,
|
|
runStepStatus: selection.durable == null ? selection.runStepStatus : undefined,
|
|
reasoningVisibility: selection.durable == null ? 'visible' : 'marker',
|
|
}),
|
|
[detachedLiveSubmitting, foregroundTitle, progress, selection],
|
|
);
|
|
const activity = useMemo(() => {
|
|
if (selection.durable == null) return liveActivity;
|
|
if (data == null) {
|
|
return progress == null ? { ...liveActivity, status: 'dispatched' as const } : liveActivity;
|
|
}
|
|
const durable = adaptDurableThreadActivity(data, selection.durable.taskId);
|
|
if (
|
|
(durable.status === 'running' || durable.status === 'dispatched') &&
|
|
liveActivity.items.length > 0
|
|
) {
|
|
return {
|
|
...durable,
|
|
prompt: durable.prompt ?? liveActivity.prompt,
|
|
items: liveActivity.items,
|
|
};
|
|
}
|
|
return {
|
|
...durable,
|
|
prompt: durable.prompt ?? liveActivity.prompt,
|
|
items: durable.items.length > 0 ? durable.items : liveActivity.items,
|
|
};
|
|
}, [data, liveActivity, progress, selection.durable]);
|
|
let panelState: 'ready' | 'loading' | 'error' = 'ready';
|
|
if (
|
|
selection.durable != null &&
|
|
liveActivity.items.length === 0 &&
|
|
(isLoading || isReadinessPending)
|
|
) {
|
|
panelState = 'loading';
|
|
} else if (selection.durable != null && liveActivity.items.length === 0 && isError) {
|
|
panelState = 'error';
|
|
}
|
|
|
|
return (
|
|
<aside
|
|
ref={panelRef}
|
|
role={isMobile ? 'dialog' : 'region'}
|
|
aria-modal={isMobile || undefined}
|
|
aria-label={localize('com_ui_subagent_thread_panel')}
|
|
className="flex h-full w-full flex-col overflow-hidden bg-surface-primary text-text-primary"
|
|
>
|
|
<header className="flex min-h-14 shrink-0 items-center gap-3 border-b border-border-light px-4 py-3">
|
|
<div className="flex h-8 w-8 shrink-0 items-center justify-center rounded-full bg-surface-tertiary">
|
|
<Bot size={17} aria-hidden="true" />
|
|
</div>
|
|
<div className="min-w-0 flex-1">
|
|
<h2 className="truncate text-sm font-semibold" title={activity.title}>
|
|
{activity.title}
|
|
</h2>
|
|
</div>
|
|
<Button
|
|
type="button"
|
|
variant="ghost"
|
|
size="icon"
|
|
onClick={close}
|
|
aria-label={localize('com_ui_close')}
|
|
className="h-8 w-8 shrink-0"
|
|
>
|
|
<X size={17} aria-hidden="true" />
|
|
</Button>
|
|
</header>
|
|
|
|
{/* Keep the foreground panel's existing nested-tool approval controls
|
|
coordinated within this invocation. Detached activity projections
|
|
never include approval payloads. */}
|
|
<ApprovalProvider
|
|
key={`${selection.parentMessageId}\u0000${selection.toolCallId}\u0000${selection.partIndex}`}
|
|
>
|
|
<SubagentActivity
|
|
key={`${selection.parentMessageId}\u0000${selection.toolCallId}\u0000${selection.partIndex}`}
|
|
activity={activity}
|
|
state={panelState}
|
|
/>
|
|
</ApprovalProvider>
|
|
</aside>
|
|
);
|
|
}
|