2026-05-06 15:54:15 +00:00
|
|
|
import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
|
2026-11-05 17:09:20 +08:00
|
|
|
import type { CSSProperties, PointerEvent as ReactPointerEvent, ReactNode } from "react";
|
2026-08-12 14:45:12 +08:00
|
|
|
import { createPortal } from "react-dom";
|
2026-04-19 06:39:06 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
2026-11-04 16:27:24 +08:00
|
|
|
import { composerUsageSnapshots, type ComposerUsageSnapshot } from "@/lib/model-request";
|
2026-04-18 18:51:53 +00:00
|
|
|
|
2026-07-15 10:45:40 +08:00
|
|
|
import { FilePreviewAvailabilityProvider } from "@/components/FilePreviewAvailabilityContext";
|
2026-06-06 19:49:33 +08:00
|
|
|
import { FilePreviewPanel } from "@/components/FilePreviewPanel";
|
2026-09-24 01:44:23 +08:00
|
|
|
import { PreviewPane } from "@/components/PreviewPane";
|
2026-09-23 14:03:37 +08:00
|
|
|
import { FileActionsProvider } from "@/components/FileActions";
|
2026-09-24 01:44:23 +08:00
|
|
|
import { createFilePreviewResource } from "@/lib/file-preview-resource";
|
2026-08-18 18:28:19 +08:00
|
|
|
import { SessionHandleLabel } from "@/components/SessionHandleLabel";
|
2026-06-06 19:49:33 +08:00
|
|
|
import { PromptNavigator } from "@/components/thread/PromptNavigator";
|
2026-10-18 14:57:23 +08:00
|
|
|
import { usePromptHistory } from "@/hooks/usePromptHistory";
|
2026-09-20 15:51:08 +08:00
|
|
|
import { ModelFallbackNotice } from "@/components/thread/ModelFallbackNotice";
|
2026-08-22 19:33:22 +08:00
|
|
|
import { RecoveryNotice } from "@/components/thread/RecoveryNotice";
|
2026-10-05 05:16:33 +08:00
|
|
|
import { SubagentTasksProvider } from "@/components/thread/SubagentTasks";
|
2026-10-26 13:16:44 +08:00
|
|
|
import { SubagentTaskDock } from "@/components/thread/SubagentTaskDock";
|
|
|
|
|
import { latestSubagentInputKey } from "@/lib/subagent-tasks";
|
2026-06-06 19:49:33 +08:00
|
|
|
import { SessionInfoPopover } from "@/components/thread/SessionInfoPopover";
|
2026-10-22 18:32:18 +08:00
|
|
|
import type { ComposerDraft, ComposerDraftStore } from "@/lib/composer-draft";
|
2026-09-04 15:13:17 +08:00
|
|
|
import { ThreadComposer } from "@/components/thread/ThreadComposer";
|
2026-11-06 11:31:42 +08:00
|
|
|
import {
|
|
|
|
|
ComposerUsagePopover,
|
|
|
|
|
type ComposerContextUsage,
|
|
|
|
|
type ComposerRoundUsage,
|
|
|
|
|
type ComposerUsageControl,
|
2026-09-04 15:13:17 +08:00
|
|
|
} from "@/components/thread/ComposerUsagePopover";
|
2026-09-12 16:32:46 +08:00
|
|
|
import {
|
|
|
|
|
modelPresetOptionsFromSettings,
|
|
|
|
|
toModelBadgeInfo,
|
|
|
|
|
} from "@/components/thread/model-preset";
|
2026-04-18 18:51:53 +00:00
|
|
|
import { ThreadHeader } from "@/components/thread/ThreadHeader";
|
2026-10-20 21:41:30 +08:00
|
|
|
import { ThreadLoadingSkeleton } from "@/components/thread/ThreadLoadingSkeleton";
|
2026-04-21 10:29:47 +00:00
|
|
|
import { StreamErrorNotice } from "@/components/thread/StreamErrorNotice";
|
2026-06-06 19:49:33 +08:00
|
|
|
import { ThreadViewport, type ThreadViewportHandle } from "@/components/thread/ThreadViewport";
|
2026-07-14 14:47:42 +08:00
|
|
|
import { useNanobotStream, type SendAttachment, type SendOptions } from "@/hooks/useNanobotStream";
|
2026-09-24 01:44:23 +08:00
|
|
|
import { useFilePreviewState, type FilePreviewState, type FilePreviewStore } from "@/hooks/useFilePreviewState";
|
2026-06-13 13:26:49 +08:00
|
|
|
import {
|
2026-07-15 10:45:40 +08:00
|
|
|
ApiError,
|
|
|
|
|
fetchFilePreviewAvailability,
|
2026-09-24 01:44:23 +08:00
|
|
|
fetchFilePreview,
|
2026-09-23 14:03:37 +08:00
|
|
|
fetchFileReferenceMetadata,
|
2026-06-13 13:26:49 +08:00
|
|
|
fetchInstalledCliApps,
|
|
|
|
|
fetchMcpPresets,
|
|
|
|
|
fetchSettings,
|
2026-10-07 13:53:48 +08:00
|
|
|
fetchWorkspaces,
|
|
|
|
|
fetchWorkspaceDirectories,
|
2026-06-13 13:26:49 +08:00
|
|
|
listSlashCommands,
|
|
|
|
|
} from "@/lib/api";
|
2026-05-23 00:59:54 +08:00
|
|
|
import {
|
|
|
|
|
CLI_APPS_CHANGED_EVENT,
|
|
|
|
|
installedCliAppsFromPayload,
|
|
|
|
|
isCliAppsPayload,
|
|
|
|
|
} from "@/lib/cli-app-events";
|
2026-05-24 13:38:37 +08:00
|
|
|
import {
|
|
|
|
|
MCP_PRESETS_CHANGED_EVENT,
|
|
|
|
|
installedMcpPresetsFromPayload,
|
|
|
|
|
isMcpPresetsPayload,
|
|
|
|
|
} from "@/lib/mcp-preset-events";
|
2026-10-21 18:33:53 +08:00
|
|
|
import type { StreamError } from "@/lib/nanobot-client";
|
2026-10-22 18:32:18 +08:00
|
|
|
import { isLocalAttachment } from "../../../../packages/client-events/attachments";
|
2026-05-29 03:42:53 +08:00
|
|
|
import type {
|
|
|
|
|
ChatSummary,
|
2026-09-21 17:48:49 +08:00
|
|
|
FilePreviewPayload,
|
2026-09-23 14:03:37 +08:00
|
|
|
FileReferenceMetadata,
|
2026-09-04 15:13:17 +08:00
|
|
|
RoundUsage,
|
2026-05-29 03:42:53 +08:00
|
|
|
SettingsPayload,
|
|
|
|
|
SlashCommand,
|
2026-07-01 15:52:34 +08:00
|
|
|
SkillSummary,
|
2026-05-29 03:42:53 +08:00
|
|
|
UIMessage,
|
|
|
|
|
WorkspaceScopePayload,
|
|
|
|
|
WorkspacesPayload,
|
2026-10-07 13:53:48 +08:00
|
|
|
ProjectDirectory,
|
2026-05-29 03:42:53 +08:00
|
|
|
} from "@/lib/types";
|
2026-09-20 16:48:13 +08:00
|
|
|
import { projectThreadEvents } from "@/lib/thread-event-projection";
|
|
|
|
|
import { projectWebuiThreadMessages } from "@/lib/thread-display-projection";
|
2026-09-09 14:02:26 +08:00
|
|
|
import { ThreadMessageCache } from "@/lib/thread-message-cache";
|
2026-09-20 16:02:09 +08:00
|
|
|
import { providerDisplayLabel } from "@/lib/provider-brand";
|
2026-09-14 15:25:13 +08:00
|
|
|
import { cn } from "@/lib/utils";
|
2026-11-05 17:09:20 +08:00
|
|
|
import { composerContextSnapshots } from "@/lib/context-graph";
|
2026-04-18 18:51:53 +00:00
|
|
|
import { useClient } from "@/providers/ClientProvider";
|
|
|
|
|
|
2026-11-04 16:27:24 +08:00
|
|
|
function latestComposerContextUsage(messages: ComposerUsageSnapshot[]): ComposerContextUsage | null {
|
2026-08-22 19:41:37 +08:00
|
|
|
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
|
|
|
|
const message = messages[index];
|
2026-11-04 16:27:24 +08:00
|
|
|
if (message.compacted) {
|
2026-11-05 17:09:20 +08:00
|
|
|
return message.estimatedContextTokens !== undefined
|
|
|
|
|
? { contextTokens: message.estimatedContextTokens, contextWindowTokens: message.contextWindowTokens, estimated: true }
|
|
|
|
|
: null;
|
2026-09-08 13:16:45 +08:00
|
|
|
}
|
2026-08-22 19:41:37 +08:00
|
|
|
const contextTokens = message.usage?.context_tokens;
|
|
|
|
|
if (
|
2026-11-04 16:27:24 +08:00
|
|
|
typeof contextTokens !== "number"
|
2026-08-22 19:41:37 +08:00
|
|
|
|| !Number.isFinite(contextTokens)
|
|
|
|
|
|| contextTokens < 0
|
|
|
|
|
) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
contextTokens,
|
|
|
|
|
...(typeof message.contextWindowTokens === "number"
|
|
|
|
|
? { contextWindowTokens: message.contextWindowTokens }
|
|
|
|
|
: {}),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-04 16:27:24 +08:00
|
|
|
function recentComposerRoundUsage(messages: ComposerUsageSnapshot[]): ComposerRoundUsage[] {
|
2026-09-04 15:13:17 +08:00
|
|
|
const recent: ComposerRoundUsage[] = [];
|
|
|
|
|
const seenTurns = new Set<string>();
|
|
|
|
|
for (let index = messages.length - 1; index >= 0 && recent.length < 8; index -= 1) {
|
|
|
|
|
const message = messages[index];
|
2026-11-05 17:09:20 +08:00
|
|
|
if (message.compacted) break;
|
2026-09-04 15:13:17 +08:00
|
|
|
const turnKey = message.turnId || message.id;
|
2026-11-04 16:27:24 +08:00
|
|
|
if (seenTurns.has(turnKey)) {
|
2026-09-04 15:13:17 +08:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const rounds: RoundUsage[] = message.roundUsages ?? [];
|
2026-11-04 16:27:24 +08:00
|
|
|
if (!rounds.length) continue;
|
|
|
|
|
seenTurns.add(turnKey);
|
2026-09-04 15:13:17 +08:00
|
|
|
for (let roundIndex = rounds.length - 1; roundIndex >= 0; roundIndex -= 1) {
|
|
|
|
|
const round = rounds[roundIndex];
|
|
|
|
|
const inputTokens = round.prompt_tokens;
|
|
|
|
|
if (
|
|
|
|
|
recent.length >= 8
|
|
|
|
|
|| typeof inputTokens !== "number"
|
|
|
|
|
|| !Number.isFinite(inputTokens)
|
|
|
|
|
|| inputTokens <= 0
|
|
|
|
|
) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
const cachedTokens = round.cached_tokens;
|
|
|
|
|
const generationMs = round.generation_ms;
|
|
|
|
|
recent.push({
|
|
|
|
|
inputTokens,
|
|
|
|
|
...(typeof cachedTokens === "number" && Number.isFinite(cachedTokens)
|
|
|
|
|
? { cachedTokens }
|
|
|
|
|
: {}),
|
|
|
|
|
...(typeof generationMs === "number" && Number.isFinite(generationMs)
|
|
|
|
|
? { generationMs }
|
|
|
|
|
: {}),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return recent.reverse();
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-13 16:25:37 +08:00
|
|
|
function latestActiveTurnId(messages: UIMessage[], runStartedAt: number | null): string | null {
|
2026-07-26 00:18:24 +08:00
|
|
|
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
|
|
|
|
const message = messages[index];
|
|
|
|
|
if (message.isStreaming && message.turnId) return message.turnId;
|
|
|
|
|
}
|
2026-08-13 16:25:37 +08:00
|
|
|
if (runStartedAt === null) return null;
|
2026-07-26 00:18:24 +08:00
|
|
|
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
|
|
|
|
const message = messages[index];
|
2026-07-29 23:15:45 +08:00
|
|
|
if (
|
2026-08-13 16:25:37 +08:00
|
|
|
message.role !== "user"
|
2026-07-29 23:15:45 +08:00
|
|
|
&& message.turnId
|
2026-08-13 16:25:37 +08:00
|
|
|
&& message.createdAt >= runStartedAt * 1000
|
2026-07-29 23:15:45 +08:00
|
|
|
) return message.turnId;
|
2026-07-26 00:18:24 +08:00
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-29 23:15:45 +08:00
|
|
|
function hasInlineDeliveryError(
|
|
|
|
|
messages: UIMessage[],
|
|
|
|
|
error: StreamError | null,
|
|
|
|
|
): boolean {
|
|
|
|
|
if (!error?.turnId) return false;
|
|
|
|
|
return messages.some((message) => (
|
|
|
|
|
message.role === "user"
|
|
|
|
|
&& message.turnId === error.turnId
|
|
|
|
|
&& message.deliveryStatus === "failed"
|
|
|
|
|
&& message.deliveryErrorKind === error.kind
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-06 19:49:33 +08:00
|
|
|
const FILE_PREVIEW_DEFAULT_WIDTH = 544;
|
|
|
|
|
const FILE_PREVIEW_MIN_WIDTH = 360;
|
|
|
|
|
const FILE_PREVIEW_MAX_WIDTH = 860;
|
|
|
|
|
const FILE_PREVIEW_MIN_MAIN_WIDTH = 420;
|
|
|
|
|
const FILE_PREVIEW_CLOSE_ANIMATION_MS = 320;
|
|
|
|
|
|
2026-07-15 10:45:40 +08:00
|
|
|
type FilePreviewAvailabilityCacheEntry = {
|
|
|
|
|
available?: boolean;
|
|
|
|
|
promise: Promise<boolean>;
|
|
|
|
|
revision: number;
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-06 19:49:33 +08:00
|
|
|
function clampFilePreviewWidth(width: number, maxWidth: number): number {
|
|
|
|
|
return Math.min(Math.max(width, FILE_PREVIEW_MIN_WIDTH), maxWidth);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function maxFilePreviewWidth(containerWidth: number): number {
|
2026-09-21 17:48:49 +08:00
|
|
|
return Math.min(
|
|
|
|
|
containerWidth > 0 ? containerWidth : FILE_PREVIEW_MIN_WIDTH,
|
|
|
|
|
Math.max(
|
|
|
|
|
FILE_PREVIEW_MIN_WIDTH,
|
|
|
|
|
Math.min(FILE_PREVIEW_MAX_WIDTH, containerWidth - FILE_PREVIEW_MIN_MAIN_WIDTH),
|
|
|
|
|
),
|
2026-06-06 19:49:33 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-24 03:00:22 +08:00
|
|
|
const EMPTY_TITLE_OVERRIDES: Record<string, string> = {};
|
|
|
|
|
|
2026-04-18 18:51:53 +00:00
|
|
|
interface ThreadShellProps {
|
|
|
|
|
session: ChatSummary | null;
|
2026-08-02 22:04:59 +08:00
|
|
|
sessions?: ChatSummary[];
|
2026-10-24 03:00:22 +08:00
|
|
|
titleOverrides?: Record<string, string>;
|
2026-04-18 18:51:53 +00:00
|
|
|
title: string;
|
2026-08-05 19:24:41 +08:00
|
|
|
temporary?: boolean;
|
2026-08-06 18:29:04 +08:00
|
|
|
temporaryChatIds?: readonly string[];
|
2026-09-21 00:41:00 +08:00
|
|
|
messageCache?: ThreadMessageCache;
|
2026-09-21 17:48:49 +08:00
|
|
|
filePreviewStore?: FilePreviewStore;
|
2026-09-25 23:42:01 +08:00
|
|
|
draftStore?: ComposerDraftStore;
|
2026-08-06 18:29:04 +08:00
|
|
|
temporaryChatEnabled?: boolean;
|
|
|
|
|
onTemporaryChatEnabledChange?: (enabled: boolean) => void;
|
2026-04-18 18:51:53 +00:00
|
|
|
onToggleSidebar: () => void;
|
2026-08-06 18:29:04 +08:00
|
|
|
onCreateChat?: (
|
|
|
|
|
workspaceScope?: WorkspaceScopePayload | null,
|
|
|
|
|
initialMessage?: string,
|
2026-09-04 10:36:24 +08:00
|
|
|
modelPreset?: string | null,
|
2026-08-06 18:29:04 +08:00
|
|
|
) => Promise<string | null>;
|
2026-09-12 16:32:46 +08:00
|
|
|
pendingFirstMessage?: PendingFirstMessage & { id: string; chatId: string } | null;
|
|
|
|
|
onPendingFirstMessageConsumed?: (id: string) => void;
|
2026-06-05 19:49:34 +08:00
|
|
|
onForkChat?: (sourceChatId: string, beforeUserIndex: number) => Promise<string | null>;
|
2026-05-06 14:15:36 +00:00
|
|
|
onTurnEnd?: () => void;
|
|
|
|
|
theme?: "light" | "dark";
|
|
|
|
|
onToggleTheme?: () => void;
|
2026-05-29 03:42:53 +08:00
|
|
|
hideSidebarToggleForHostChrome?: boolean;
|
2026-08-12 14:45:12 +08:00
|
|
|
hideSidebarToggle?: boolean;
|
2026-05-31 17:00:42 +08:00
|
|
|
hideThemeButton?: boolean;
|
2026-08-12 14:45:12 +08:00
|
|
|
hideHeaderTitle?: boolean;
|
2026-08-25 01:55:58 +08:00
|
|
|
inlineHandle?: boolean;
|
2026-05-29 03:42:53 +08:00
|
|
|
hideHeader?: boolean;
|
2026-08-12 14:45:12 +08:00
|
|
|
headerActions?: ReactNode;
|
|
|
|
|
headerPortalTarget?: HTMLElement | null;
|
|
|
|
|
headerActive?: boolean;
|
|
|
|
|
composerPortalTarget?: HTMLElement | null;
|
2026-10-18 16:58:17 +08:00
|
|
|
navigationFrame?: HTMLElement | null;
|
2026-08-12 14:45:12 +08:00
|
|
|
composerActive?: boolean;
|
2026-11-06 11:31:42 +08:00
|
|
|
multiplePanes?: boolean;
|
2026-08-12 14:45:12 +08:00
|
|
|
composerInputAriaLabel?: string;
|
|
|
|
|
emptyComposerVariant?: "hero" | "thread";
|
2026-05-29 03:42:53 +08:00
|
|
|
workspaceScope?: WorkspaceScopePayload | null;
|
|
|
|
|
workspaceDefaultScope?: WorkspaceScopePayload | null;
|
|
|
|
|
workspaceControls?: WorkspacesPayload["controls"] | null;
|
|
|
|
|
workspaceScopeDisabled?: boolean;
|
|
|
|
|
workspaceError?: string | null;
|
|
|
|
|
onWorkspaceScopeChange?: (scope: WorkspaceScopePayload) => void;
|
|
|
|
|
settingsSnapshot?: SettingsPayload | null;
|
2026-09-24 22:05:31 +08:00
|
|
|
settingsLoading?: boolean;
|
2026-09-02 04:19:12 +08:00
|
|
|
onOpenModelSettings?: () => void;
|
2026-07-01 15:52:34 +08:00
|
|
|
skills?: SkillSummary[];
|
2026-04-18 18:51:53 +00:00
|
|
|
}
|
|
|
|
|
|
2026-05-29 03:42:53 +08:00
|
|
|
const HERO_GREETING_KEYS = [
|
|
|
|
|
"thread.empty.greetings.workOn",
|
|
|
|
|
"thread.empty.greetings.start",
|
|
|
|
|
"thread.empty.greetings.build",
|
|
|
|
|
"thread.empty.greetings.tackle",
|
2026-05-06 14:15:36 +00:00
|
|
|
] as const;
|
|
|
|
|
|
2026-07-23 18:18:34 +08:00
|
|
|
function HeroGreeting({ text }: { text: string }) {
|
|
|
|
|
const containerRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
const headingRef = useRef<HTMLHeadingElement>(null);
|
|
|
|
|
|
|
|
|
|
useLayoutEffect(() => {
|
|
|
|
|
const container = containerRef.current;
|
|
|
|
|
const heading = headingRef.current;
|
|
|
|
|
if (!container || !heading) return;
|
|
|
|
|
|
|
|
|
|
const fitToWidth = () => {
|
|
|
|
|
heading.style.removeProperty("font-size");
|
|
|
|
|
const availableWidth = container.clientWidth;
|
|
|
|
|
if (availableWidth <= 0) return;
|
|
|
|
|
|
|
|
|
|
const naturalWidth = heading.scrollWidth;
|
|
|
|
|
const maximumFontSize = Number.parseFloat(window.getComputedStyle(heading).fontSize);
|
|
|
|
|
if (
|
|
|
|
|
naturalWidth <= availableWidth
|
|
|
|
|
|| !Number.isFinite(maximumFontSize)
|
|
|
|
|
|| maximumFontSize <= 0
|
|
|
|
|
) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const fittedFontSize = Math.max(
|
|
|
|
|
12,
|
|
|
|
|
Math.floor(maximumFontSize * ((availableWidth - 2) / naturalWidth) * 100) / 100,
|
|
|
|
|
);
|
|
|
|
|
heading.style.fontSize = `${fittedFontSize}px`;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
fitToWidth();
|
|
|
|
|
|
|
|
|
|
let lastObservedWidth = container.clientWidth;
|
|
|
|
|
const resizeObserver = typeof ResizeObserver === "undefined"
|
|
|
|
|
? null
|
|
|
|
|
: new ResizeObserver(([entry]) => {
|
|
|
|
|
const nextWidth = entry?.contentRect.width ?? container.clientWidth;
|
|
|
|
|
if (nextWidth === lastObservedWidth) return;
|
|
|
|
|
lastObservedWidth = nextWidth;
|
|
|
|
|
fitToWidth();
|
|
|
|
|
});
|
|
|
|
|
resizeObserver?.observe(container);
|
|
|
|
|
window.addEventListener("resize", fitToWidth);
|
|
|
|
|
|
|
|
|
|
let cancelled = false;
|
|
|
|
|
void document.fonts?.ready.then(() => {
|
|
|
|
|
if (!cancelled) fitToWidth();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
cancelled = true;
|
|
|
|
|
resizeObserver?.disconnect();
|
|
|
|
|
window.removeEventListener("resize", fitToWidth);
|
|
|
|
|
};
|
|
|
|
|
}, [text]);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div ref={containerRef} className="min-w-0 w-full max-w-[44rem]">
|
|
|
|
|
<h1
|
|
|
|
|
ref={headingRef}
|
|
|
|
|
data-testid="hero-greeting"
|
2026-10-25 23:54:06 +08:00
|
|
|
data-theme-ink
|
2026-08-06 18:29:04 +08:00
|
|
|
className="select-none whitespace-nowrap text-[34px] font-normal leading-[1.08] tracking-normal text-foreground sm:text-[48px] sm:leading-tight"
|
2026-07-23 18:18:34 +08:00
|
|
|
>
|
|
|
|
|
{text}
|
|
|
|
|
</h1>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-29 03:42:53 +08:00
|
|
|
function randomHeroGreetingKey(): (typeof HERO_GREETING_KEYS)[number] {
|
|
|
|
|
const index = Math.floor(Math.random() * HERO_GREETING_KEYS.length);
|
|
|
|
|
return HERO_GREETING_KEYS[index] ?? HERO_GREETING_KEYS[0];
|
|
|
|
|
}
|
2026-05-08 09:40:15 +00:00
|
|
|
|
|
|
|
|
interface PendingFirstMessage {
|
2026-10-08 11:55:13 +08:00
|
|
|
finish?: (accepted: boolean) => void;
|
2026-05-08 09:40:15 +00:00
|
|
|
content: string;
|
2026-07-14 14:47:42 +08:00
|
|
|
images?: SendAttachment[];
|
2026-05-08 09:40:15 +00:00
|
|
|
options?: SendOptions;
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-06 19:49:33 +08:00
|
|
|
interface InstalledSettingItemsOptions<Payload, Item> {
|
2026-09-24 23:19:06 +08:00
|
|
|
requestCount: number;
|
2026-07-30 10:25:22 +08:00
|
|
|
getToken: () => string;
|
2026-06-06 19:49:33 +08:00
|
|
|
eventName: string;
|
|
|
|
|
fetchPayload: (token: string) => Promise<Payload>;
|
|
|
|
|
isPayload: (value: unknown) => value is Payload;
|
|
|
|
|
selectItems: (payload: Payload) => Item[];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function useInstalledSettingItems<Payload, Item>({
|
2026-09-24 23:19:06 +08:00
|
|
|
requestCount,
|
2026-07-30 10:25:22 +08:00
|
|
|
getToken,
|
2026-06-06 19:49:33 +08:00
|
|
|
eventName,
|
|
|
|
|
fetchPayload,
|
|
|
|
|
isPayload,
|
|
|
|
|
selectItems,
|
|
|
|
|
}: InstalledSettingItemsOptions<Payload, Item>): Item[] {
|
|
|
|
|
const [items, setItems] = useState<Item[]>([]);
|
2026-09-24 23:19:06 +08:00
|
|
|
const loadedRef = useRef(false);
|
|
|
|
|
const pendingRef = useRef<Promise<Payload> | null>(null);
|
2026-06-06 19:49:33 +08:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
let cancelled = false;
|
2026-07-30 10:25:22 +08:00
|
|
|
let refreshQueued = false;
|
|
|
|
|
let refreshAfterFlight = false;
|
|
|
|
|
let refreshing = false;
|
|
|
|
|
let payloadVersion = 0;
|
|
|
|
|
const refresh = async (): Promise<void> => {
|
|
|
|
|
if (refreshing) return;
|
|
|
|
|
refreshing = true;
|
|
|
|
|
const version = payloadVersion;
|
2026-09-24 23:19:06 +08:00
|
|
|
const pending = pendingRef.current ?? fetchPayload(getToken());
|
|
|
|
|
pendingRef.current = pending;
|
2026-07-30 10:25:22 +08:00
|
|
|
try {
|
2026-09-24 23:19:06 +08:00
|
|
|
const payload = await pending;
|
2026-07-30 10:25:22 +08:00
|
|
|
if (!cancelled && version === payloadVersion) {
|
2026-09-24 23:19:06 +08:00
|
|
|
loadedRef.current = true;
|
2026-07-30 10:25:22 +08:00
|
|
|
setItems(selectItems(payload));
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
// Keep the last successful catalog during transient refresh failures.
|
|
|
|
|
} finally {
|
2026-09-24 23:19:06 +08:00
|
|
|
if (pendingRef.current === pending) pendingRef.current = null;
|
2026-07-30 10:25:22 +08:00
|
|
|
refreshing = false;
|
|
|
|
|
if (refreshAfterFlight && !cancelled) {
|
|
|
|
|
refreshAfterFlight = false;
|
|
|
|
|
void refresh();
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-06-06 19:49:33 +08:00
|
|
|
};
|
2026-07-30 10:25:22 +08:00
|
|
|
const queueRefresh = () => {
|
2026-09-24 23:19:06 +08:00
|
|
|
if (!requestCount || document.visibilityState === "hidden" || refreshQueued) return;
|
2026-07-30 10:25:22 +08:00
|
|
|
refreshQueued = true;
|
|
|
|
|
queueMicrotask(() => {
|
|
|
|
|
refreshQueued = false;
|
|
|
|
|
if (!cancelled) void refresh();
|
|
|
|
|
});
|
|
|
|
|
};
|
2026-09-24 23:19:06 +08:00
|
|
|
if (requestCount && !loadedRef.current) void refresh();
|
2026-07-30 10:25:22 +08:00
|
|
|
|
2026-06-06 19:49:33 +08:00
|
|
|
const refreshOnChanged = (event: Event) => {
|
|
|
|
|
const payload = (event as CustomEvent<unknown>).detail;
|
|
|
|
|
if (isPayload(payload)) {
|
2026-07-30 10:25:22 +08:00
|
|
|
payloadVersion += 1;
|
2026-09-24 23:19:06 +08:00
|
|
|
loadedRef.current = true;
|
2026-06-06 19:49:33 +08:00
|
|
|
setItems(selectItems(payload));
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-07-30 10:25:22 +08:00
|
|
|
if (refreshing) {
|
|
|
|
|
refreshAfterFlight = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
queueRefresh();
|
2026-06-06 19:49:33 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
window.addEventListener(eventName, refreshOnChanged);
|
|
|
|
|
return () => {
|
|
|
|
|
cancelled = true;
|
|
|
|
|
window.removeEventListener(eventName, refreshOnChanged);
|
|
|
|
|
};
|
2026-09-24 23:19:06 +08:00
|
|
|
}, [requestCount, eventName, fetchPayload, getToken, isPayload, selectItems]);
|
2026-06-06 19:49:33 +08:00
|
|
|
|
|
|
|
|
return items;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-18 18:51:53 +00:00
|
|
|
export function ThreadShell({
|
|
|
|
|
session,
|
2026-08-02 22:04:59 +08:00
|
|
|
sessions = [],
|
2026-10-24 03:00:22 +08:00
|
|
|
titleOverrides = EMPTY_TITLE_OVERRIDES,
|
2026-04-18 18:51:53 +00:00
|
|
|
title,
|
2026-08-05 19:24:41 +08:00
|
|
|
temporary = false,
|
2026-08-06 18:29:04 +08:00
|
|
|
temporaryChatIds = [],
|
2026-09-21 00:41:00 +08:00
|
|
|
messageCache,
|
2026-09-21 17:48:49 +08:00
|
|
|
filePreviewStore,
|
2026-09-25 23:42:01 +08:00
|
|
|
draftStore,
|
2026-08-06 18:29:04 +08:00
|
|
|
temporaryChatEnabled = false,
|
|
|
|
|
onTemporaryChatEnabledChange,
|
2026-04-18 18:51:53 +00:00
|
|
|
onToggleSidebar,
|
2026-05-06 14:15:36 +00:00
|
|
|
onCreateChat,
|
2026-09-12 16:32:46 +08:00
|
|
|
pendingFirstMessage = null,
|
|
|
|
|
onPendingFirstMessageConsumed,
|
2026-06-05 19:49:34 +08:00
|
|
|
onForkChat,
|
2026-05-06 14:15:36 +00:00
|
|
|
onTurnEnd,
|
|
|
|
|
theme = "light",
|
|
|
|
|
onToggleTheme = () => {},
|
2026-05-29 03:42:53 +08:00
|
|
|
hideSidebarToggleForHostChrome = false,
|
2026-08-12 14:45:12 +08:00
|
|
|
hideSidebarToggle = false,
|
2026-05-31 17:00:42 +08:00
|
|
|
hideThemeButton = false,
|
2026-08-12 14:45:12 +08:00
|
|
|
hideHeaderTitle = false,
|
2026-08-25 01:55:58 +08:00
|
|
|
inlineHandle = false,
|
2026-05-29 03:42:53 +08:00
|
|
|
hideHeader = false,
|
2026-08-12 14:45:12 +08:00
|
|
|
headerActions,
|
|
|
|
|
headerPortalTarget,
|
|
|
|
|
headerActive = true,
|
|
|
|
|
composerPortalTarget,
|
2026-10-18 16:58:17 +08:00
|
|
|
navigationFrame,
|
2026-08-12 14:45:12 +08:00
|
|
|
composerActive = true,
|
2026-11-06 11:31:42 +08:00
|
|
|
multiplePanes = false,
|
2026-08-12 14:45:12 +08:00
|
|
|
composerInputAriaLabel,
|
|
|
|
|
emptyComposerVariant = "hero",
|
2026-05-29 03:42:53 +08:00
|
|
|
workspaceScope = null,
|
|
|
|
|
workspaceDefaultScope = null,
|
|
|
|
|
workspaceControls = null,
|
|
|
|
|
workspaceScopeDisabled = false,
|
|
|
|
|
workspaceError = null,
|
|
|
|
|
onWorkspaceScopeChange,
|
|
|
|
|
settingsSnapshot = null,
|
2026-09-24 22:05:31 +08:00
|
|
|
settingsLoading = false,
|
2026-06-06 19:49:33 +08:00
|
|
|
onOpenModelSettings,
|
2026-07-01 15:52:34 +08:00
|
|
|
skills = [],
|
2026-04-18 18:51:53 +00:00
|
|
|
}: ThreadShellProps) {
|
2026-04-19 06:39:06 +00:00
|
|
|
const { t } = useTranslation();
|
2026-04-18 18:51:53 +00:00
|
|
|
const chatId = session?.chatId ?? null;
|
2026-08-05 19:24:41 +08:00
|
|
|
const historyKey = temporary ? null : session?.key ?? null;
|
2026-09-21 17:48:49 +08:00
|
|
|
const previewSessionKey = session?.key ?? null;
|
2026-08-18 18:28:19 +08:00
|
|
|
const mentionSessions = useMemo(
|
2026-10-24 03:00:22 +08:00
|
|
|
() => sessions.filter((candidate) => candidate.key !== historyKey).map((candidate) => {
|
|
|
|
|
const renamedTitle = titleOverrides[candidate.key]?.trim();
|
|
|
|
|
return renamedTitle ? { ...candidate, title: renamedTitle } : candidate;
|
|
|
|
|
}),
|
|
|
|
|
[historyKey, sessions, titleOverrides],
|
2026-08-02 22:04:59 +08:00
|
|
|
);
|
2026-10-05 05:16:33 +08:00
|
|
|
const { client, getToken, ingressLimits, modelName, token, webuiCapabilities } = useClient();
|
2026-10-07 13:53:48 +08:00
|
|
|
const favoriteWorkspaceProject = useCallback(
|
|
|
|
|
async (path: string, pinned: boolean) => {
|
|
|
|
|
const response = await client.requestMutation<{ favorite_projects: ProjectDirectory[] }>("workspace.favorite", { path, pinned });
|
|
|
|
|
return response.favorite_projects;
|
|
|
|
|
},
|
|
|
|
|
[client],
|
|
|
|
|
);
|
|
|
|
|
const resolveWorkspaceProject = useCallback(
|
|
|
|
|
(path: string) => client.requestMutation<ProjectDirectory>("workspace.resolve_project", { path }),
|
|
|
|
|
[client],
|
|
|
|
|
);
|
|
|
|
|
const loadWorkspaceProjects = useCallback(() => fetchWorkspaces(getToken()), [getToken]);
|
|
|
|
|
const browseWorkspaceDirectories = useCallback(
|
|
|
|
|
(path: string, query: string, showHidden: boolean, allowPartial = false) =>
|
|
|
|
|
fetchWorkspaceDirectories(getToken(), path, query, showHidden, allowPartial),
|
|
|
|
|
[getToken],
|
|
|
|
|
);
|
2026-04-18 18:51:53 +00:00
|
|
|
const [booting, setBooting] = useState(false);
|
2026-05-06 15:54:15 +00:00
|
|
|
const [slashCommands, setSlashCommands] = useState<SlashCommand[]>([]);
|
2026-09-24 23:19:06 +08:00
|
|
|
const [mentionCatalogRequestCount, setMentionCatalogRequestCount] = useState(0);
|
|
|
|
|
const requestMentionCatalogs = useCallback(() => setMentionCatalogRequestCount((count) => count + 1), []);
|
2026-06-06 19:49:33 +08:00
|
|
|
const cliApps = useInstalledSettingItems({
|
2026-09-24 23:19:06 +08:00
|
|
|
requestCount: mentionCatalogRequestCount,
|
2026-07-30 10:25:22 +08:00
|
|
|
getToken,
|
2026-06-06 19:49:33 +08:00
|
|
|
eventName: CLI_APPS_CHANGED_EVENT,
|
2026-06-13 13:26:49 +08:00
|
|
|
fetchPayload: fetchInstalledCliApps,
|
2026-06-06 19:49:33 +08:00
|
|
|
isPayload: isCliAppsPayload,
|
|
|
|
|
selectItems: installedCliAppsFromPayload,
|
|
|
|
|
});
|
|
|
|
|
const mcpPresets = useInstalledSettingItems({
|
2026-09-24 23:19:06 +08:00
|
|
|
requestCount: mentionCatalogRequestCount,
|
2026-07-30 10:25:22 +08:00
|
|
|
getToken,
|
2026-06-06 19:49:33 +08:00
|
|
|
eventName: MCP_PRESETS_CHANGED_EVENT,
|
|
|
|
|
fetchPayload: fetchMcpPresets,
|
|
|
|
|
isPayload: isMcpPresetsPayload,
|
|
|
|
|
selectItems: installedMcpPresetsFromPayload,
|
|
|
|
|
});
|
2026-05-29 03:42:53 +08:00
|
|
|
const [settings, setSettings] = useState<SettingsPayload | null>(settingsSnapshot);
|
2026-09-20 15:51:08 +08:00
|
|
|
const [modelFallback, setModelFallback] = useState<{
|
|
|
|
|
chatId: string;
|
|
|
|
|
model: string;
|
2026-09-20 16:02:09 +08:00
|
|
|
reauthProvider?: string;
|
2026-09-20 15:51:08 +08:00
|
|
|
dismissed: boolean;
|
|
|
|
|
} | null>(null);
|
2026-05-29 03:42:53 +08:00
|
|
|
const [heroGreetingKey, setHeroGreetingKey] = useState(randomHeroGreetingKey);
|
2026-07-26 00:18:24 +08:00
|
|
|
const [submittedViewportTurnId, setSubmittedViewportTurnId] = useState<string | null>(null);
|
2026-10-22 16:34:54 +08:00
|
|
|
const { state: previewState, openFile, resolveFile, selectTab, closeTab, close: closePreview, setWidth: setFilePreviewWidth } =
|
2026-09-21 17:48:49 +08:00
|
|
|
useFilePreviewState(previewSessionKey, filePreviewStore);
|
2026-11-05 17:09:20 +08:00
|
|
|
const [contextSidebarOpen, setContextSidebarOpen] = useState(false);
|
2026-11-06 11:31:42 +08:00
|
|
|
useLayoutEffect(() => {
|
|
|
|
|
if (multiplePanes) setContextSidebarOpen(false);
|
|
|
|
|
}, [multiplePanes, previewSessionKey]);
|
2026-11-05 17:09:20 +08:00
|
|
|
const [sidebarEnterWidth, setSidebarEnterWidth] = useState(0);
|
|
|
|
|
const [contextSidebarHost, setContextSidebarHost] = useState<HTMLElement | null>(null);
|
|
|
|
|
const setSidebarHost = useCallback((node: HTMLElement | null) => { shellRef.current = node; setContextSidebarHost(node); }, []);
|
2026-09-24 01:44:23 +08:00
|
|
|
const [closingPreview, setClosingPreview] = useState<{ key: string; state: FilePreviewState } | null>(null);
|
2026-09-21 17:48:49 +08:00
|
|
|
const filePreviewClosing = closingPreview?.key === previewSessionKey;
|
2026-09-24 01:44:23 +08:00
|
|
|
const visiblePreview = filePreviewClosing ? closingPreview.state : previewState;
|
2026-11-06 11:31:42 +08:00
|
|
|
const activePreview = contextSidebarOpen ? undefined : visiblePreview.tabs.find((tab) => tab.id === visiblePreview.activeId);
|
2026-09-24 01:44:23 +08:00
|
|
|
const previewOpen = Boolean(activePreview);
|
2026-11-05 17:09:20 +08:00
|
|
|
const sidebarOpen = previewOpen || contextSidebarOpen;
|
|
|
|
|
const [localComposerHost, setLocalComposerHost] = useState<HTMLDivElement | null>(null);
|
|
|
|
|
const [composerMount] = useState(() => document.createElement("div"));
|
|
|
|
|
const inlineComposer = composerPortalTarget === undefined || (sidebarOpen && composerActive);
|
|
|
|
|
const composerDestination = inlineComposer ? localComposerHost : composerPortalTarget;
|
|
|
|
|
useLayoutEffect(() => {
|
|
|
|
|
// The inline host ref arrives on the next render. Keep the focused input
|
|
|
|
|
// attached until that destination is ready.
|
|
|
|
|
if (!composerDestination) { if (!inlineComposer) composerMount.remove(); return; }
|
|
|
|
|
if (composerMount.parentElement === composerDestination) return;
|
2026-11-06 11:31:42 +08:00
|
|
|
// Move the host, not the React subtree, to preserve the input and its draft.
|
2026-11-05 17:09:20 +08:00
|
|
|
const focused = document.activeElement instanceof HTMLElement && composerMount.contains(document.activeElement)
|
|
|
|
|
? document.activeElement : null;
|
|
|
|
|
const selection = document.getSelection();
|
|
|
|
|
const range = focused && selection?.rangeCount && composerMount.contains(selection.getRangeAt(0).commonAncestorContainer)
|
|
|
|
|
? selection.getRangeAt(0).cloneRange() : null;
|
|
|
|
|
composerDestination.appendChild(composerMount);
|
|
|
|
|
focused?.focus({ preventScroll: true });
|
|
|
|
|
if (range && selection) { selection.removeAllRanges(); selection.addRange(range); }
|
|
|
|
|
}, [composerDestination, composerMount, inlineComposer]);
|
|
|
|
|
useLayoutEffect(() => () => composerMount.remove(), [composerMount]);
|
2026-09-21 17:48:49 +08:00
|
|
|
const [filePreviewMaxWidth, setFilePreviewMaxWidth] = useState(FILE_PREVIEW_MAX_WIDTH);
|
|
|
|
|
const filePreviewWidth = clampFilePreviewWidth(previewState.width, filePreviewMaxWidth);
|
2026-09-25 23:42:01 +08:00
|
|
|
const draftKey = session?.key ?? (temporaryChatEnabled ? "new:temporary" : "new:chat");
|
|
|
|
|
const persistDraft = session ? !temporary : !temporaryChatEnabled;
|
|
|
|
|
const [quote, setQuote] = useState<{ key: string; text: string | null } | null>(null);
|
|
|
|
|
const quotedContext = quote?.key === draftKey
|
|
|
|
|
? quote.text
|
|
|
|
|
: draftStore?.get(draftKey, persistDraft)?.quotedContext ?? null;
|
|
|
|
|
const setQuotedContext = useCallback((text: string | null) => {
|
|
|
|
|
setQuote({ key: draftKey, text });
|
|
|
|
|
}, [draftKey]);
|
2026-07-22 22:35:39 +08:00
|
|
|
const [composerFocusSignal, setComposerFocusSignal] = useState(0);
|
2026-06-06 19:49:33 +08:00
|
|
|
const shellRef = useRef<HTMLElement | null>(null);
|
2026-10-07 13:53:48 +08:00
|
|
|
const [conversationElement, setConversationElement] = useState<HTMLDivElement | null>(null);
|
2026-11-06 11:31:42 +08:00
|
|
|
const [composerSurface, setComposerSurface] = useState<HTMLDivElement | null>(null);
|
2026-11-06 11:49:08 +08:00
|
|
|
const alignSidebar = useCallback(() => {
|
|
|
|
|
if (!sidebarOpen || !inlineComposer || !contextSidebarHost || !composerSurface) return;
|
|
|
|
|
// Metadata can wrap below the input card. The viewport also reports grid
|
|
|
|
|
// movement, which changes the surface's position without resizing it.
|
|
|
|
|
const inset = contextSidebarHost.getBoundingClientRect().bottom - composerSurface.getBoundingClientRect().bottom;
|
|
|
|
|
contextSidebarHost.style.setProperty("--sidebar-bottom-inset", `${Math.max(0, inset)}px`);
|
|
|
|
|
}, [contextSidebarHost, composerSurface, sidebarOpen, inlineComposer]);
|
2026-11-05 17:09:20 +08:00
|
|
|
useLayoutEffect(() => {
|
2026-11-06 11:49:08 +08:00
|
|
|
if (!sidebarOpen || !inlineComposer || !contextSidebarHost || !composerSurface) return;
|
2026-11-05 17:09:20 +08:00
|
|
|
alignSidebar();
|
|
|
|
|
const observer = new ResizeObserver(alignSidebar);
|
2026-11-06 11:49:08 +08:00
|
|
|
observer.observe(contextSidebarHost);
|
2026-11-05 17:09:20 +08:00
|
|
|
observer.observe(composerMount);
|
2026-11-06 11:49:08 +08:00
|
|
|
observer.observe(composerSurface);
|
2026-11-05 17:09:20 +08:00
|
|
|
return () => observer.disconnect();
|
2026-11-06 11:49:08 +08:00
|
|
|
}, [alignSidebar, contextSidebarHost, sidebarOpen, inlineComposer, composerDestination, composerMount, composerSurface]);
|
2026-06-06 19:49:33 +08:00
|
|
|
const filePreviewWidthRef = useRef(FILE_PREVIEW_DEFAULT_WIDTH);
|
|
|
|
|
const filePreviewCloseTimerRef = useRef<number | null>(null);
|
2026-09-21 17:48:49 +08:00
|
|
|
const filePreviewResizeCleanupRef = useRef<(() => void) | null>(null);
|
2026-10-22 18:32:18 +08:00
|
|
|
const pendingFirstRef = useRef<(PendingFirstMessage & { draft?: ComposerDraft; draftKey: string }) | null>(null);
|
|
|
|
|
const [restoredFirstDraft, setRestoredFirstDraft] = useState<{ chatId: string; revision: number; error: string } | null>(null);
|
2026-07-07 18:20:42 +08:00
|
|
|
const [pendingFirstTargetChatId, setPendingFirstTargetChatId] = useState<string | null>(null);
|
2026-09-12 16:32:46 +08:00
|
|
|
const consumedPendingFirstMessageIdRef = useRef<string | null>(null);
|
2026-06-06 19:49:33 +08:00
|
|
|
const viewportRef = useRef<ThreadViewportHandle | null>(null);
|
2026-07-26 00:18:24 +08:00
|
|
|
const activeViewportTurnByChatIdRef = useRef<Map<string, string>>(new Map());
|
2026-08-07 11:20:08 +08:00
|
|
|
const knownTemporaryChatIdsRef = useRef(new Set<string>());
|
2026-09-21 00:41:00 +08:00
|
|
|
const localMessageCacheRef = useRef(new ThreadMessageCache(
|
2026-09-09 14:02:26 +08:00
|
|
|
(key) => knownTemporaryChatIdsRef.current.has(key),
|
|
|
|
|
));
|
2026-09-21 00:41:00 +08:00
|
|
|
const messageCacheRef = useRef(messageCache ?? localMessageCacheRef.current);
|
|
|
|
|
messageCacheRef.current = messageCache ?? localMessageCacheRef.current;
|
2026-05-16 01:14:11 +08:00
|
|
|
const sessionKeyByChatIdRef = useRef<Map<string, string>>(new Map());
|
2026-05-13 16:39:07 +00:00
|
|
|
const handleTurnEnd = useCallback(() => {
|
2026-07-26 00:18:24 +08:00
|
|
|
if (chatId) activeViewportTurnByChatIdRef.current.delete(chatId);
|
|
|
|
|
setSubmittedViewportTurnId(null);
|
2026-05-13 16:39:07 +00:00
|
|
|
onTurnEnd?.();
|
2026-07-26 00:18:24 +08:00
|
|
|
}, [chatId, onTurnEnd]);
|
2026-04-21 10:29:47 +00:00
|
|
|
const {
|
|
|
|
|
messages,
|
2026-11-04 16:27:24 +08:00
|
|
|
modelRequests,
|
2026-11-02 17:42:16 +08:00
|
|
|
messagesKey,
|
2026-07-26 00:18:24 +08:00
|
|
|
messagesReady,
|
2026-04-21 10:29:47 +00:00
|
|
|
isStreaming,
|
2026-05-16 01:14:11 +08:00
|
|
|
runStartedAt,
|
2026-09-07 15:12:39 +08:00
|
|
|
retryStatus,
|
2026-05-16 01:14:11 +08:00
|
|
|
goalState,
|
2026-08-22 19:33:22 +08:00
|
|
|
recoveryState,
|
|
|
|
|
continueRecovery,
|
|
|
|
|
dismissRecovery,
|
2026-04-21 10:29:47 +00:00
|
|
|
send,
|
2026-06-09 01:08:49 +08:00
|
|
|
transcribeAudio,
|
2026-05-08 09:40:15 +00:00
|
|
|
stop,
|
2026-10-21 18:33:53 +08:00
|
|
|
history: {
|
|
|
|
|
loading,
|
|
|
|
|
error: historyError,
|
|
|
|
|
loadingOlder,
|
|
|
|
|
olderError,
|
|
|
|
|
hasMoreBefore,
|
|
|
|
|
userMessageOffset,
|
|
|
|
|
hasPendingToolCalls,
|
|
|
|
|
completedTurnIds,
|
|
|
|
|
activeTurnId: historyActiveTurnId,
|
|
|
|
|
version: historyVersion,
|
|
|
|
|
forkBoundaryMessageCount,
|
|
|
|
|
},
|
|
|
|
|
refreshHistory,
|
|
|
|
|
loadOlder,
|
|
|
|
|
loadTraceDetails: loadBodyTraceDetails,
|
2026-04-21 10:29:47 +00:00
|
|
|
streamError,
|
|
|
|
|
dismissStreamError,
|
2026-10-16 11:38:05 +08:00
|
|
|
} = useNanobotStream(
|
2026-10-21 18:33:53 +08:00
|
|
|
chatId, undefined, false, handleTurnEnd, undefined, undefined,
|
|
|
|
|
{ key: historyKey, cache: messageCacheRef.current },
|
2026-10-16 11:38:05 +08:00
|
|
|
);
|
2026-10-18 14:57:23 +08:00
|
|
|
const displayMessages = useMemo(() => projectWebuiThreadMessages(messages), [messages]);
|
2026-10-21 18:33:53 +08:00
|
|
|
const initialHistoryLoading = loading;
|
|
|
|
|
const showTemporaryChatControl =
|
|
|
|
|
!hideHeader && !session && !loading && !!onTemporaryChatEnabledChange;
|
2026-10-18 14:57:23 +08:00
|
|
|
const promptNavigation = usePromptHistory(historyKey, historyVersion, displayMessages);
|
|
|
|
|
const updateNavigationMessages = promptNavigation.updateMessages;
|
2026-10-25 20:22:20 +08:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (!chatId || !messagesReady) return;
|
|
|
|
|
messageCacheRef.current.setLoadedMessages(chatId, promptNavigation.window
|
|
|
|
|
? [displayMessages, promptNavigation.window.messages] : [displayMessages]);
|
|
|
|
|
}, [chatId, messagesReady, displayMessages, promptNavigation.window]);
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const cache = messageCacheRef.current;
|
|
|
|
|
return () => { if (chatId) cache.deleteLoadedMessages(chatId); };
|
|
|
|
|
}, [chatId]);
|
2026-05-16 01:14:11 +08:00
|
|
|
|
2026-09-12 04:37:37 +08:00
|
|
|
const loadTraceDetails = useCallback(async (refs: string[]) => {
|
2026-10-21 18:33:53 +08:00
|
|
|
const details = await loadBodyTraceDetails(refs);
|
|
|
|
|
for (const detail of details) {
|
|
|
|
|
const projected = projectThreadEvents(detail.events);
|
|
|
|
|
updateNavigationMessages((current) => current.flatMap((message) => (
|
|
|
|
|
message.traceDetail?.ref === detail.ref
|
|
|
|
|
? projected.map((row) => ({ ...row, activitySegmentId: message.activitySegmentId }))
|
|
|
|
|
: [message]
|
|
|
|
|
)));
|
2026-09-12 04:37:37 +08:00
|
|
|
}
|
2026-10-21 18:33:53 +08:00
|
|
|
}, [loadBodyTraceDetails, updateNavigationMessages]);
|
2026-07-28 11:40:01 +08:00
|
|
|
|
2026-05-16 01:14:11 +08:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (chatId && historyKey) sessionKeyByChatIdRef.current.set(chatId, historyKey);
|
|
|
|
|
}, [chatId, historyKey]);
|
|
|
|
|
|
2026-06-06 19:49:33 +08:00
|
|
|
useEffect(() => {
|
|
|
|
|
filePreviewWidthRef.current = filePreviewWidth;
|
|
|
|
|
}, [filePreviewWidth]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-09-21 17:48:49 +08:00
|
|
|
filePreviewResizeCleanupRef.current?.();
|
2026-06-06 19:49:33 +08:00
|
|
|
if (filePreviewCloseTimerRef.current !== null) {
|
|
|
|
|
window.clearTimeout(filePreviewCloseTimerRef.current);
|
|
|
|
|
filePreviewCloseTimerRef.current = null;
|
|
|
|
|
}
|
2026-09-21 17:48:49 +08:00
|
|
|
setClosingPreview(null);
|
2026-07-26 00:18:24 +08:00
|
|
|
setSubmittedViewportTurnId(null);
|
2026-09-21 17:48:49 +08:00
|
|
|
}, [previewSessionKey]);
|
2026-06-06 19:49:33 +08:00
|
|
|
|
2026-08-05 19:24:41 +08:00
|
|
|
useEffect(() => {
|
2026-08-06 18:29:04 +08:00
|
|
|
const retained = new Set(temporaryChatIds);
|
2026-08-07 11:20:08 +08:00
|
|
|
for (const chatId of retained) knownTemporaryChatIdsRef.current.add(chatId);
|
|
|
|
|
for (const cachedChatId of knownTemporaryChatIdsRef.current) {
|
|
|
|
|
if (!retained.has(cachedChatId)) {
|
2026-09-21 00:41:00 +08:00
|
|
|
// Shared caches are retained/pruned by the app, not by individual panes.
|
|
|
|
|
if (!messageCache) messageCacheRef.current.delete(cachedChatId);
|
2026-08-06 18:29:04 +08:00
|
|
|
activeViewportTurnByChatIdRef.current.delete(cachedChatId);
|
2026-08-07 11:20:08 +08:00
|
|
|
knownTemporaryChatIdsRef.current.delete(cachedChatId);
|
2026-08-06 18:29:04 +08:00
|
|
|
}
|
|
|
|
|
}
|
2026-09-21 00:41:00 +08:00
|
|
|
}, [messageCache, temporaryChatIds]);
|
2026-08-05 19:24:41 +08:00
|
|
|
|
2026-07-22 22:35:39 +08:00
|
|
|
const handleQuoteSelection = useCallback((text: string) => {
|
|
|
|
|
setQuotedContext(text);
|
|
|
|
|
setComposerFocusSignal((value) => value + 1);
|
2026-09-25 23:42:01 +08:00
|
|
|
}, [setQuotedContext]);
|
2026-07-22 22:35:39 +08:00
|
|
|
|
2026-06-06 19:49:33 +08:00
|
|
|
useEffect(() => {
|
|
|
|
|
return () => {
|
2026-09-21 17:48:49 +08:00
|
|
|
filePreviewResizeCleanupRef.current?.();
|
2026-06-06 19:49:33 +08:00
|
|
|
if (filePreviewCloseTimerRef.current !== null) {
|
|
|
|
|
window.clearTimeout(filePreviewCloseTimerRef.current);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}, []);
|
|
|
|
|
|
2026-09-24 23:19:06 +08:00
|
|
|
const hasAppMentions = displayMessages.some((message) => message.cliApps?.length || message.mcpPresets?.length);
|
2026-09-24 23:10:57 +08:00
|
|
|
useEffect(() => {
|
2026-09-24 23:19:06 +08:00
|
|
|
if (hasAppMentions) requestMentionCatalogs();
|
|
|
|
|
}, [hasAppMentions, requestMentionCatalogs]);
|
2026-11-06 11:31:42 +08:00
|
|
|
const usageSnapshots = useMemo(
|
|
|
|
|
() => messagesReady ? composerUsageSnapshots(messages, modelRequests) : [], [messages, modelRequests, messagesReady],
|
|
|
|
|
);
|
2026-08-22 19:41:37 +08:00
|
|
|
const composerContextUsage = useMemo(
|
2026-11-04 16:27:24 +08:00
|
|
|
() => latestComposerContextUsage(usageSnapshots),
|
|
|
|
|
[usageSnapshots],
|
2026-08-22 19:41:37 +08:00
|
|
|
);
|
2026-09-04 15:13:17 +08:00
|
|
|
const composerRoundUsage = useMemo(
|
2026-11-04 16:27:24 +08:00
|
|
|
() => recentComposerRoundUsage(usageSnapshots),
|
|
|
|
|
[usageSnapshots],
|
2026-09-04 15:13:17 +08:00
|
|
|
);
|
2026-11-05 17:09:20 +08:00
|
|
|
const contextGraphSnapshots = useMemo(
|
2026-11-06 11:31:42 +08:00
|
|
|
() => messagesReady ? composerContextSnapshots(messages, modelRequests) : [], [messages, modelRequests, messagesReady],
|
2026-11-05 17:09:20 +08:00
|
|
|
);
|
2026-07-26 00:18:24 +08:00
|
|
|
const currentGoalState = messagesReady ? goalState : undefined;
|
2026-10-24 00:43:34 +08:00
|
|
|
// Recovery decisions gate queued input, not the ledger-owned run state.
|
2026-08-22 19:33:22 +08:00
|
|
|
const recoveryNeedsDecision = recoveryState?.status === "awaiting_user"
|
|
|
|
|
|| recoveryState?.status === "failed";
|
2026-10-24 00:43:34 +08:00
|
|
|
const currentRunStartedAt = messagesReady ? runStartedAt : null;
|
|
|
|
|
const turnActive = messagesReady && isStreaming;
|
2026-07-26 00:18:24 +08:00
|
|
|
const restoredViewportTurnId = useMemo(
|
2026-08-13 16:25:37 +08:00
|
|
|
() => turnActive ? latestActiveTurnId(displayMessages, currentRunStartedAt) : null,
|
|
|
|
|
[currentRunStartedAt, displayMessages, turnActive],
|
2026-07-26 00:18:24 +08:00
|
|
|
);
|
|
|
|
|
const rememberedViewportTurnId = chatId
|
|
|
|
|
? activeViewportTurnByChatIdRef.current.get(chatId) ?? null
|
|
|
|
|
: null;
|
2026-08-15 14:58:35 +08:00
|
|
|
const canonicalRunTurnId = chatId && messagesReady && turnActive
|
|
|
|
|
? client.getRunTurnId(chatId)
|
|
|
|
|
: null;
|
2026-07-26 00:18:24 +08:00
|
|
|
const viewportTurnId = messagesReady && turnActive
|
2026-08-13 16:25:37 +08:00
|
|
|
? canonicalRunTurnId
|
|
|
|
|
?? rememberedViewportTurnId
|
|
|
|
|
?? historyActiveTurnId
|
|
|
|
|
?? restoredViewportTurnId
|
2026-07-26 00:18:24 +08:00
|
|
|
: null;
|
|
|
|
|
const activeTurnStartedHere =
|
|
|
|
|
viewportTurnId !== null && viewportTurnId === submittedViewportTurnId;
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!chatId || !messagesReady || turnActive) return;
|
|
|
|
|
activeViewportTurnByChatIdRef.current.delete(chatId);
|
|
|
|
|
setSubmittedViewportTurnId((current) =>
|
|
|
|
|
current === rememberedViewportTurnId ? null : current,
|
|
|
|
|
);
|
|
|
|
|
}, [chatId, messagesReady, rememberedViewportTurnId, turnActive]);
|
2026-07-15 10:45:40 +08:00
|
|
|
const filePreviewAvailabilityCache = useMemo(
|
|
|
|
|
() => new Map<string, FilePreviewAvailabilityCacheEntry>(),
|
2026-09-21 17:48:49 +08:00
|
|
|
[previewSessionKey],
|
2026-07-15 10:45:40 +08:00
|
|
|
);
|
|
|
|
|
const filePreviewAvailabilityRevision = displayMessages.length;
|
|
|
|
|
const resolveFilePreviewAvailability = useCallback((path: string) => {
|
2026-09-21 17:48:49 +08:00
|
|
|
if (!previewSessionKey) return Promise.resolve(false);
|
2026-07-15 10:45:40 +08:00
|
|
|
const cached = filePreviewAvailabilityCache.get(path);
|
|
|
|
|
if (
|
|
|
|
|
cached
|
|
|
|
|
&& (cached.available !== false || cached.revision === filePreviewAvailabilityRevision)
|
|
|
|
|
) {
|
|
|
|
|
return cached.promise;
|
|
|
|
|
}
|
2026-09-21 17:48:49 +08:00
|
|
|
const request = temporary
|
|
|
|
|
? client.requestMutation<{ available: boolean }>("temporary_chat.file_preview", {
|
|
|
|
|
chat_id: chatId, path, probe: true,
|
|
|
|
|
}).then((result) => result.available)
|
|
|
|
|
: fetchFilePreviewAvailability(getToken(), previewSessionKey, path);
|
|
|
|
|
const pending = request.catch(
|
2026-07-15 10:45:40 +08:00
|
|
|
(error: unknown) => {
|
|
|
|
|
if (error instanceof ApiError) {
|
|
|
|
|
if (error.status === 404 && /API route not found/i.test(error.message)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2026-09-21 17:48:49 +08:00
|
|
|
if ([400, 403, 404, 413, 415].includes(error.status)) return false;
|
2026-07-15 10:45:40 +08:00
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
const entry: FilePreviewAvailabilityCacheEntry = {
|
|
|
|
|
promise: pending,
|
|
|
|
|
revision: filePreviewAvailabilityRevision,
|
|
|
|
|
};
|
|
|
|
|
filePreviewAvailabilityCache.set(path, entry);
|
|
|
|
|
void pending.then((available) => {
|
|
|
|
|
if (filePreviewAvailabilityCache.get(path) === entry) {
|
|
|
|
|
entry.available = available;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return pending;
|
|
|
|
|
}, [
|
|
|
|
|
filePreviewAvailabilityCache,
|
|
|
|
|
filePreviewAvailabilityRevision,
|
2026-07-30 10:25:22 +08:00
|
|
|
getToken,
|
2026-09-21 17:48:49 +08:00
|
|
|
previewSessionKey,
|
|
|
|
|
temporary,
|
|
|
|
|
client,
|
|
|
|
|
chatId,
|
2026-07-15 10:45:40 +08:00
|
|
|
]);
|
2026-05-16 01:14:11 +08:00
|
|
|
|
2026-10-20 21:41:30 +08:00
|
|
|
const showHeroComposer = displayMessages.length === 0 && !initialHistoryLoading;
|
2026-08-12 14:45:12 +08:00
|
|
|
const composerVariant = showHeroComposer ? emptyComposerVariant : "thread";
|
2026-10-28 21:37:23 +08:00
|
|
|
// Only unpersisted drafts can choose a project; history pages do not own this state.
|
|
|
|
|
const projectLocked = !!session && session.projectLocked !== false;
|
2026-05-29 03:42:53 +08:00
|
|
|
const wasShowingHeroComposerRef = useRef(showHeroComposer);
|
2026-07-23 00:38:49 +08:00
|
|
|
const sessionModelPreset = session?.modelPreset?.trim() || null;
|
2026-07-24 17:34:26 +08:00
|
|
|
const [localModelPreset, setLocalModelPreset] = useState<string | null>(null);
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setLocalModelPreset(null);
|
|
|
|
|
}, [session?.key, sessionModelPreset]);
|
2026-08-22 19:41:37 +08:00
|
|
|
const configuredPresetNames = useMemo(
|
|
|
|
|
() => new Set(settings?.model_presets.map((preset) => preset.name) ?? []),
|
|
|
|
|
[settings],
|
|
|
|
|
);
|
2026-07-24 17:34:26 +08:00
|
|
|
const activeModelPreset = (
|
2026-08-22 19:41:37 +08:00
|
|
|
(localModelPreset && (!settings || configuredPresetNames.has(localModelPreset))
|
|
|
|
|
? localModelPreset
|
|
|
|
|
: null)
|
|
|
|
|
|| (sessionModelPreset && (!settings || configuredPresetNames.has(sessionModelPreset))
|
|
|
|
|
? sessionModelPreset
|
|
|
|
|
: null)
|
2026-07-24 17:34:26 +08:00
|
|
|
|| settings?.agent.model_preset
|
|
|
|
|
|| "default"
|
|
|
|
|
);
|
2026-09-20 15:51:08 +08:00
|
|
|
useEffect(() => {
|
|
|
|
|
setModelFallback(null);
|
|
|
|
|
if (!chatId) return;
|
|
|
|
|
return client.onChat(chatId, (event) => {
|
|
|
|
|
if (event.event !== "turn_model_updated") return;
|
|
|
|
|
if (event.fallback !== true) {
|
|
|
|
|
// The next turn starts with its configured model, not the previous fallback.
|
|
|
|
|
setModelFallback(null);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const model = event.model_name.trim();
|
|
|
|
|
if (!model) return;
|
2026-09-20 16:02:09 +08:00
|
|
|
const reauthProvider = typeof event.reauth_provider === "string"
|
|
|
|
|
? event.reauth_provider.trim() || undefined : undefined;
|
2026-09-20 15:51:08 +08:00
|
|
|
// A tool loop may report the same fallback repeatedly. Closing the notice
|
|
|
|
|
// lasts until the next turn/model change, without changing the actual preset.
|
2026-09-20 16:02:09 +08:00
|
|
|
setModelFallback((current) => {
|
|
|
|
|
if (current?.chatId === chatId && current.model === model) {
|
|
|
|
|
// An explicit auth rejection is actionable even after dismissing a
|
|
|
|
|
// generic fallback. A circuit-skipped call must not erase that reason.
|
|
|
|
|
return reauthProvider && reauthProvider !== current.reauthProvider
|
|
|
|
|
? { ...current, reauthProvider, dismissed: false } : current;
|
|
|
|
|
}
|
|
|
|
|
return { chatId, model, reauthProvider, dismissed: false };
|
|
|
|
|
});
|
2026-09-20 15:51:08 +08:00
|
|
|
});
|
|
|
|
|
}, [activeModelPreset, chatId, client]);
|
2026-07-24 17:34:26 +08:00
|
|
|
const handleModelPresetChange = useCallback((name: string) => {
|
|
|
|
|
setLocalModelPreset(name);
|
|
|
|
|
if (chatId) {
|
|
|
|
|
void client.sendSystemCommand(chatId, `/model ${name}`).catch(() => {});
|
|
|
|
|
}
|
|
|
|
|
}, [chatId, client]);
|
|
|
|
|
const modelPresetOptions = useMemo(
|
|
|
|
|
() => modelPresetOptionsFromSettings(settings),
|
|
|
|
|
[settings],
|
|
|
|
|
);
|
2026-08-05 19:24:41 +08:00
|
|
|
const availableSlashCommands = useMemo(
|
|
|
|
|
() => temporary
|
|
|
|
|
? slashCommands.filter(({ command }) => command === "/model" || command === "/stop")
|
|
|
|
|
: slashCommands,
|
|
|
|
|
[slashCommands, temporary],
|
|
|
|
|
);
|
2026-05-24 13:38:37 +08:00
|
|
|
const modelBadge = useMemo(
|
2026-07-24 17:34:26 +08:00
|
|
|
() => toModelBadgeInfo(modelName, settings, activeModelPreset),
|
|
|
|
|
[activeModelPreset, modelName, settings],
|
2026-05-24 13:38:37 +08:00
|
|
|
);
|
2026-06-06 19:49:33 +08:00
|
|
|
const modelBadgeLabel = modelBadge.needsSetup
|
2026-09-01 20:18:58 +08:00
|
|
|
? t("thread.composer.chooseAI", { defaultValue: "Choose your AI" })
|
2026-06-06 19:49:33 +08:00
|
|
|
: modelBadge.label;
|
2026-05-29 03:42:53 +08:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (showHeroComposer && !wasShowingHeroComposerRef.current) {
|
|
|
|
|
setHeroGreetingKey(randomHeroGreetingKey());
|
|
|
|
|
}
|
|
|
|
|
wasShowingHeroComposerRef.current = showHeroComposer;
|
|
|
|
|
}, [showHeroComposer]);
|
|
|
|
|
|
|
|
|
|
const withWorkspaceScope = useCallback(
|
|
|
|
|
(options?: SendOptions): SendOptions | undefined => {
|
|
|
|
|
if (!workspaceScope) return options;
|
|
|
|
|
return {
|
|
|
|
|
...(options ?? {}),
|
|
|
|
|
workspaceScope,
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
[workspaceScope],
|
|
|
|
|
);
|
2026-05-24 13:38:37 +08:00
|
|
|
|
|
|
|
|
const refreshModelSettings = useCallback(async () => {
|
|
|
|
|
try {
|
2026-07-30 10:25:22 +08:00
|
|
|
setSettings(await fetchSettings(getToken()));
|
2026-05-24 13:38:37 +08:00
|
|
|
} catch {
|
2026-05-29 03:42:53 +08:00
|
|
|
if (!settingsSnapshot) setSettings(null);
|
2026-05-24 13:38:37 +08:00
|
|
|
}
|
2026-07-30 10:25:22 +08:00
|
|
|
}, [getToken, settingsSnapshot]);
|
2026-05-24 13:38:37 +08:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-05-29 03:42:53 +08:00
|
|
|
if (settingsSnapshot) {
|
|
|
|
|
setSettings(settingsSnapshot);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-09-24 22:05:31 +08:00
|
|
|
if (!settingsLoading) void refreshModelSettings();
|
|
|
|
|
}, [refreshModelSettings, settingsLoading, settingsSnapshot]);
|
2026-05-24 13:38:37 +08:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
return client.onRuntimeModelUpdate(() => {
|
|
|
|
|
void refreshModelSettings();
|
|
|
|
|
});
|
|
|
|
|
}, [client, refreshModelSettings]);
|
2026-04-18 18:51:53 +00:00
|
|
|
|
2026-10-16 13:33:41 +08:00
|
|
|
const lastRunTurnId = chatId ? client.getRunTurnId(chatId) : null;
|
|
|
|
|
const completedRunTurnId = chatId && lastRunTurnId && client.isRunCompleted(chatId, lastRunTurnId)
|
|
|
|
|
? lastRunTurnId : null;
|
|
|
|
|
const queueCompletedTurnIds = useMemo(() => {
|
2026-10-21 18:33:53 +08:00
|
|
|
const canonical = hasPendingToolCalls ? [] : completedTurnIds;
|
2026-10-16 13:33:41 +08:00
|
|
|
// Live terminal facts can release the queue before the history refresh finishes.
|
|
|
|
|
return completedRunTurnId && !canonical.includes(completedRunTurnId)
|
|
|
|
|
? [...canonical, completedRunTurnId] : canonical;
|
2026-10-21 18:33:53 +08:00
|
|
|
}, [completedTurnIds, completedRunTurnId, hasPendingToolCalls]);
|
2026-05-13 16:39:07 +00:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-08-05 19:24:41 +08:00
|
|
|
if (!historyKey || !chatId) return;
|
2026-05-17 23:52:50 +08:00
|
|
|
return client.onSessionUpdate((updatedChatId, scope) => {
|
2026-05-13 16:39:07 +00:00
|
|
|
if (updatedChatId !== chatId) return;
|
2026-05-17 23:52:50 +08:00
|
|
|
if (scope === "metadata") return;
|
2026-07-26 00:18:24 +08:00
|
|
|
// A turn-end thread refresh can arrive while the viewport is easing the
|
|
|
|
|
// final layout change. User-driven scrolling already disables following,
|
|
|
|
|
// so keep an active programmatic follow alive across canonical hydration.
|
2026-10-21 18:33:53 +08:00
|
|
|
refreshHistory();
|
2026-05-13 16:39:07 +00:00
|
|
|
});
|
2026-10-21 18:33:53 +08:00
|
|
|
}, [chatId, client, historyKey, refreshHistory]);
|
2026-07-28 11:40:01 +08:00
|
|
|
|
2026-07-30 10:25:22 +08:00
|
|
|
const wasPageHiddenRef = useRef(document.visibilityState === "hidden");
|
2026-07-28 11:40:01 +08:00
|
|
|
useEffect(() => {
|
|
|
|
|
const refreshOnReturn = () => {
|
2026-07-30 10:25:22 +08:00
|
|
|
if (document.visibilityState === "hidden") {
|
|
|
|
|
wasPageHiddenRef.current = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!wasPageHiddenRef.current) return;
|
|
|
|
|
wasPageHiddenRef.current = false;
|
2026-08-05 19:24:41 +08:00
|
|
|
if (!historyKey || !chatId || client.status !== "open" || loading) return;
|
2026-07-30 10:25:22 +08:00
|
|
|
if (
|
|
|
|
|
!turnActive
|
|
|
|
|
&& !hasPendingToolCalls
|
|
|
|
|
&& !client.hasUnsettledRun(chatId)
|
|
|
|
|
&& !historyError
|
|
|
|
|
) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-10-21 18:33:53 +08:00
|
|
|
refreshHistory();
|
2026-07-28 11:40:01 +08:00
|
|
|
};
|
|
|
|
|
document.addEventListener("visibilitychange", refreshOnReturn);
|
|
|
|
|
return () => document.removeEventListener("visibilitychange", refreshOnReturn);
|
2026-07-30 10:25:22 +08:00
|
|
|
}, [
|
|
|
|
|
chatId,
|
|
|
|
|
client,
|
|
|
|
|
hasPendingToolCalls,
|
2026-08-05 19:24:41 +08:00
|
|
|
historyKey,
|
2026-07-30 10:25:22 +08:00
|
|
|
historyError,
|
|
|
|
|
loading,
|
2026-10-21 18:33:53 +08:00
|
|
|
refreshHistory,
|
2026-07-30 10:25:22 +08:00
|
|
|
turnActive,
|
|
|
|
|
]);
|
2026-07-28 11:40:01 +08:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
let refreshOnNextOpen = client.status !== "open";
|
|
|
|
|
return client.onStatus((status) => {
|
|
|
|
|
if (status !== "open") {
|
|
|
|
|
refreshOnNextOpen = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-10-21 18:33:53 +08:00
|
|
|
if (refreshOnNextOpen) refreshHistory();
|
2026-07-28 11:40:01 +08:00
|
|
|
refreshOnNextOpen = false;
|
|
|
|
|
});
|
2026-10-21 18:33:53 +08:00
|
|
|
}, [client, refreshHistory]);
|
2026-04-18 18:51:53 +00:00
|
|
|
|
2026-07-07 18:20:42 +08:00
|
|
|
// The landing composer queues the first message while `new_chat` is in flight.
|
|
|
|
|
// Only the chat created for that send may consume it; selecting another chat
|
|
|
|
|
// while creation is pending must not leak the message there.
|
2026-04-18 18:51:53 +00:00
|
|
|
useEffect(() => {
|
2026-07-07 18:20:42 +08:00
|
|
|
if (!chatId || pendingFirstTargetChatId !== chatId) return;
|
2026-04-18 18:51:53 +00:00
|
|
|
const pending = pendingFirstRef.current;
|
2026-07-07 18:20:42 +08:00
|
|
|
if (!pending) {
|
|
|
|
|
setPendingFirstTargetChatId(null);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-04-18 18:51:53 +00:00
|
|
|
pendingFirstRef.current = null;
|
2026-07-07 18:20:42 +08:00
|
|
|
setPendingFirstTargetChatId(null);
|
2026-07-26 00:18:24 +08:00
|
|
|
const submitted = send(pending.content, pending.images, pending.options);
|
|
|
|
|
if (submitted && !submitted.sideChannel) {
|
|
|
|
|
activeViewportTurnByChatIdRef.current.set(chatId, submitted.turnId);
|
|
|
|
|
setSubmittedViewportTurnId(submitted.turnId);
|
|
|
|
|
}
|
2026-10-08 11:55:13 +08:00
|
|
|
if (submitted?.delivery) {
|
2026-10-22 18:32:18 +08:00
|
|
|
void submitted.delivery.then(() => pending.finish?.(true), (error: unknown) => {
|
|
|
|
|
// Topic creation already unmounted the landing composer. A rejected
|
|
|
|
|
// local reference must hand its draft to that topic, not strand it on /new.
|
|
|
|
|
if (pending.draft && pending.images?.some((image) => image.media.path !== undefined)) {
|
|
|
|
|
const targetKey = `websocket:${chatId}`;
|
|
|
|
|
const current = draftStore?.get(targetKey, !temporary);
|
|
|
|
|
if (!current?.text && !current?.attachments.length
|
|
|
|
|
&& !current?.sessionMentions.length && !current?.quotedContext) {
|
|
|
|
|
draftStore?.set(targetKey, pending.draft, !temporary);
|
|
|
|
|
if (draftStore?.get(pending.draftKey) === pending.draft) draftStore.delete(pending.draftKey);
|
|
|
|
|
setRestoredFirstDraft((previous) => ({
|
|
|
|
|
chatId, revision: (previous?.revision ?? 0) + 1,
|
|
|
|
|
error: error instanceof Error ? error.message : String(error),
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
pending.finish?.(false);
|
|
|
|
|
});
|
2026-10-08 11:55:13 +08:00
|
|
|
} else pending.finish?.(submitted !== null);
|
2026-04-18 18:51:53 +00:00
|
|
|
setBooting(false);
|
2026-10-22 18:32:18 +08:00
|
|
|
}, [chatId, draftStore, pendingFirstTargetChatId, send, temporary]);
|
2026-04-18 18:51:53 +00:00
|
|
|
|
2026-09-12 16:32:46 +08:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (
|
|
|
|
|
!chatId
|
|
|
|
|
|| pendingFirstMessage?.chatId !== chatId
|
|
|
|
|
|| consumedPendingFirstMessageIdRef.current === pendingFirstMessage.id
|
|
|
|
|
) return;
|
|
|
|
|
consumedPendingFirstMessageIdRef.current = pendingFirstMessage.id;
|
|
|
|
|
const submitted = send(
|
|
|
|
|
pendingFirstMessage.content,
|
|
|
|
|
pendingFirstMessage.images,
|
|
|
|
|
withWorkspaceScope(pendingFirstMessage.options),
|
|
|
|
|
);
|
|
|
|
|
if (submitted && !submitted.sideChannel) {
|
|
|
|
|
activeViewportTurnByChatIdRef.current.set(chatId, submitted.turnId);
|
|
|
|
|
setSubmittedViewportTurnId(submitted.turnId);
|
|
|
|
|
}
|
|
|
|
|
onPendingFirstMessageConsumed?.(pendingFirstMessage.id);
|
|
|
|
|
}, [
|
|
|
|
|
chatId,
|
|
|
|
|
onPendingFirstMessageConsumed,
|
|
|
|
|
pendingFirstMessage,
|
|
|
|
|
send,
|
|
|
|
|
withWorkspaceScope,
|
|
|
|
|
]);
|
|
|
|
|
|
2026-05-06 15:54:15 +00:00
|
|
|
useEffect(() => {
|
|
|
|
|
let cancelled = false;
|
|
|
|
|
(async () => {
|
|
|
|
|
try {
|
2026-07-30 10:25:22 +08:00
|
|
|
const commands = await listSlashCommands(getToken());
|
2026-05-06 15:54:15 +00:00
|
|
|
if (!cancelled) setSlashCommands(commands);
|
|
|
|
|
} catch {
|
|
|
|
|
if (!cancelled) setSlashCommands([]);
|
|
|
|
|
}
|
|
|
|
|
})();
|
|
|
|
|
return () => {
|
|
|
|
|
cancelled = true;
|
|
|
|
|
};
|
2026-07-30 10:25:22 +08:00
|
|
|
}, [getToken]);
|
2026-05-06 15:54:15 +00:00
|
|
|
|
2026-04-18 18:51:53 +00:00
|
|
|
const handleWelcomeSend = useCallback(
|
2026-07-14 14:47:42 +08:00
|
|
|
async (content: string, images?: SendAttachment[], options?: SendOptions) => {
|
2026-08-10 15:22:25 +08:00
|
|
|
if (booting) return false;
|
2026-04-18 18:51:53 +00:00
|
|
|
setBooting(true);
|
2026-10-08 11:55:13 +08:00
|
|
|
let finish!: (accepted: boolean) => void;
|
|
|
|
|
const delivery = new Promise<boolean>((resolve) => { finish = resolve; });
|
2026-10-22 18:32:18 +08:00
|
|
|
pendingFirstRef.current = {
|
|
|
|
|
content, images, options: withWorkspaceScope(options), finish,
|
|
|
|
|
draftKey, draft: draftStore?.get(draftKey, persistDraft),
|
|
|
|
|
};
|
2026-07-07 18:20:42 +08:00
|
|
|
setPendingFirstTargetChatId(null);
|
2026-09-04 10:36:24 +08:00
|
|
|
const newId = await onCreateChat?.(workspaceScope, content, localModelPreset);
|
2026-04-18 18:51:53 +00:00
|
|
|
if (!newId) {
|
|
|
|
|
pendingFirstRef.current = null;
|
2026-07-07 18:20:42 +08:00
|
|
|
setPendingFirstTargetChatId(null);
|
2026-04-18 18:51:53 +00:00
|
|
|
setBooting(false);
|
2026-08-10 15:22:25 +08:00
|
|
|
return false;
|
2026-04-18 18:51:53 +00:00
|
|
|
}
|
2026-07-24 17:34:26 +08:00
|
|
|
if (localModelPreset) {
|
|
|
|
|
await client.sendSystemCommand(newId, `/model ${localModelPreset}`).catch(() => {});
|
|
|
|
|
}
|
2026-07-07 18:20:42 +08:00
|
|
|
setPendingFirstTargetChatId(newId);
|
2026-10-08 11:55:13 +08:00
|
|
|
return delivery;
|
2026-04-18 18:51:53 +00:00
|
|
|
},
|
2026-10-22 18:32:18 +08:00
|
|
|
[booting, client, draftKey, draftStore, localModelPreset, onCreateChat, persistDraft, withWorkspaceScope, workspaceScope],
|
2026-05-06 14:15:36 +00:00
|
|
|
);
|
|
|
|
|
|
2026-10-22 18:32:18 +08:00
|
|
|
const inspectAttachment = useCallback(async (path: string) => {
|
|
|
|
|
const result = await client.requestMutation("attachment.inspect", {
|
|
|
|
|
chat_id: chatId, path, ...(workspaceScope ? { workspace_scope: workspaceScope } : {}),
|
|
|
|
|
});
|
|
|
|
|
if (!isLocalAttachment(result)) throw new Error("Invalid attachment metadata");
|
|
|
|
|
return result;
|
|
|
|
|
}, [chatId, client, workspaceScope]);
|
|
|
|
|
|
2026-05-13 16:39:07 +00:00
|
|
|
const handleThreadSend = useCallback(
|
2026-07-14 14:47:42 +08:00
|
|
|
(content: string, images?: SendAttachment[], options?: SendOptions) => {
|
2026-10-22 18:32:18 +08:00
|
|
|
setRestoredFirstDraft((current) => current?.chatId === chatId ? { ...current, error: "" } : current);
|
2026-07-26 00:18:24 +08:00
|
|
|
const submitted = send(content, images, withWorkspaceScope(options));
|
2026-08-15 14:58:35 +08:00
|
|
|
if (
|
|
|
|
|
chatId
|
|
|
|
|
&& submitted
|
|
|
|
|
&& !submitted.sideChannel
|
|
|
|
|
&& options?.continueActiveTurn !== true
|
|
|
|
|
) {
|
2026-07-26 00:18:24 +08:00
|
|
|
activeViewportTurnByChatIdRef.current.set(chatId, submitted.turnId);
|
|
|
|
|
setSubmittedViewportTurnId(submitted.turnId);
|
|
|
|
|
}
|
2026-10-08 11:55:13 +08:00
|
|
|
return submitted?.delivery ? submitted.delivery.then(() => true) : submitted !== null;
|
2026-05-13 16:39:07 +00:00
|
|
|
},
|
2026-07-26 00:18:24 +08:00
|
|
|
[chatId, send, withWorkspaceScope],
|
2026-05-06 14:15:36 +00:00
|
|
|
);
|
|
|
|
|
|
2026-09-21 17:48:49 +08:00
|
|
|
const loadTemporaryFilePreview = useCallback((path: string) =>
|
|
|
|
|
client.requestMutation<FilePreviewPayload>("temporary_chat.file_preview", {
|
|
|
|
|
chat_id: chatId, path,
|
|
|
|
|
}).catch((error: unknown) => {
|
|
|
|
|
if (error instanceof Error && "status" in error && typeof error.status === "number") {
|
|
|
|
|
throw new ApiError(error.status, error.message);
|
|
|
|
|
}
|
|
|
|
|
throw error;
|
|
|
|
|
}), [client, chatId]);
|
|
|
|
|
|
|
|
|
|
const handleCloseFilePreview = useCallback(() => {
|
2026-09-24 01:44:23 +08:00
|
|
|
if (!previewSessionKey || !previewState.activeId || filePreviewClosing) return;
|
2026-09-21 17:48:49 +08:00
|
|
|
filePreviewResizeCleanupRef.current?.();
|
2026-09-24 01:44:23 +08:00
|
|
|
setClosingPreview({ key: previewSessionKey, state: previewState });
|
2026-09-21 17:48:49 +08:00
|
|
|
// Record the closed state immediately, even if navigation interrupts the animation.
|
2026-09-24 01:44:23 +08:00
|
|
|
closePreview();
|
2026-09-21 17:48:49 +08:00
|
|
|
filePreviewCloseTimerRef.current = window.setTimeout(() => {
|
|
|
|
|
filePreviewCloseTimerRef.current = null;
|
|
|
|
|
setClosingPreview(null);
|
|
|
|
|
}, FILE_PREVIEW_CLOSE_ANIMATION_MS);
|
2026-09-24 01:44:23 +08:00
|
|
|
}, [previewSessionKey, filePreviewClosing, previewState, closePreview]);
|
2026-09-21 17:48:49 +08:00
|
|
|
|
2026-09-24 01:44:23 +08:00
|
|
|
const cancelPreviewClose = useCallback(() => {
|
|
|
|
|
filePreviewResizeCleanupRef.current?.();
|
2026-06-06 19:49:33 +08:00
|
|
|
if (filePreviewCloseTimerRef.current !== null) {
|
|
|
|
|
window.clearTimeout(filePreviewCloseTimerRef.current);
|
|
|
|
|
filePreviewCloseTimerRef.current = null;
|
|
|
|
|
}
|
2026-09-21 17:48:49 +08:00
|
|
|
setClosingPreview(null);
|
2026-09-24 01:44:23 +08:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const handleOpenFilePreview = useCallback((path: string) => {
|
2026-11-05 17:09:20 +08:00
|
|
|
setSidebarEnterWidth(shellRef.current?.querySelector(".file-preview-pane")?.getBoundingClientRect().width ?? 0);
|
|
|
|
|
setContextSidebarOpen(false);
|
2026-09-24 01:44:23 +08:00
|
|
|
cancelPreviewClose();
|
|
|
|
|
openFile(path);
|
|
|
|
|
}, [cancelPreviewClose, openFile]);
|
|
|
|
|
|
2026-11-05 17:09:20 +08:00
|
|
|
const handleContextSidebarOpenChange = useCallback((open: boolean) => {
|
|
|
|
|
if (open) {
|
|
|
|
|
setSidebarEnterWidth(previewOpen ? filePreviewWidth : 0);
|
|
|
|
|
cancelPreviewClose(); closePreview();
|
|
|
|
|
}
|
|
|
|
|
setContextSidebarOpen(open);
|
|
|
|
|
}, [previewOpen, filePreviewWidth, cancelPreviewClose, closePreview]);
|
|
|
|
|
|
2026-09-24 01:44:23 +08:00
|
|
|
const handleClosePreviewTab = useCallback((id: string) => {
|
|
|
|
|
if (previewState.tabs.length === 1) handleCloseFilePreview();
|
|
|
|
|
else {
|
|
|
|
|
filePreviewResizeCleanupRef.current?.();
|
|
|
|
|
closeTab(id);
|
|
|
|
|
}
|
|
|
|
|
}, [previewState.tabs.length, handleCloseFilePreview, closeTab]);
|
2026-06-06 19:49:33 +08:00
|
|
|
|
2026-09-21 17:48:49 +08:00
|
|
|
// Markdown blocks can retain rendered links while their text is unchanged.
|
|
|
|
|
// Keep their callback stable, but always act on the current pane/session state.
|
|
|
|
|
const openFilePreviewRef = useRef(handleOpenFilePreview);
|
|
|
|
|
openFilePreviewRef.current = handleOpenFilePreview;
|
|
|
|
|
const openFilePreview = useCallback((path: string) => openFilePreviewRef.current(path), []);
|
2026-09-23 14:03:37 +08:00
|
|
|
const resolveFileMetadata = useCallback((path: string) => {
|
|
|
|
|
if (!previewSessionKey) return Promise.reject(new Error("No active session"));
|
|
|
|
|
return temporary
|
|
|
|
|
? client.requestMutation<FileReferenceMetadata>("temporary_chat.file_preview", {
|
|
|
|
|
chat_id: chatId, path, metadata: true,
|
|
|
|
|
})
|
|
|
|
|
: fetchFileReferenceMetadata(getToken(), previewSessionKey, path);
|
|
|
|
|
}, [chatId, client, getToken, previewSessionKey, temporary]);
|
2026-09-24 01:44:23 +08:00
|
|
|
const loadFilePreview = useCallback((path: string) => {
|
|
|
|
|
if (!previewSessionKey) return Promise.reject(new Error("No active session"));
|
|
|
|
|
return temporary ? loadTemporaryFilePreview(path) : fetchFilePreview(getToken(), previewSessionKey, path);
|
|
|
|
|
}, [previewSessionKey, temporary, loadTemporaryFilePreview, getToken]);
|
2026-09-24 01:57:03 +08:00
|
|
|
const filePreviews = useMemo(() => createFilePreviewResource(loadFilePreview), [loadFilePreview]);
|
2026-09-23 14:03:37 +08:00
|
|
|
const fileActions = useMemo(() => ({
|
|
|
|
|
resolveMetadata: resolveFileMetadata,
|
2026-09-24 01:44:23 +08:00
|
|
|
loadPreview: filePreviews.load,
|
|
|
|
|
}), [resolveFileMetadata, filePreviews]);
|
2026-09-21 17:48:49 +08:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-09-24 01:44:23 +08:00
|
|
|
if (!previewOpen || !headerActive) return;
|
2026-09-21 17:48:49 +08:00
|
|
|
const onKeyDown = (event: KeyboardEvent) => {
|
|
|
|
|
if (event.key !== "Escape" || event.defaultPrevented || event.isComposing) return;
|
|
|
|
|
// Let a dialog/menu above the pane consume Escape without closing this preview.
|
|
|
|
|
if (document.querySelector('[role="dialog"], [role="alertdialog"], [role="menu"], [role="listbox"], [data-radix-popper-content-wrapper]')) return;
|
|
|
|
|
event.preventDefault();
|
2026-09-24 01:44:23 +08:00
|
|
|
handleCloseFilePreview();
|
2026-09-21 17:48:49 +08:00
|
|
|
};
|
|
|
|
|
window.addEventListener("keydown", onKeyDown);
|
|
|
|
|
return () => window.removeEventListener("keydown", onKeyDown);
|
2026-09-24 01:44:23 +08:00
|
|
|
}, [previewOpen, headerActive, handleCloseFilePreview]);
|
2026-06-06 19:49:33 +08:00
|
|
|
|
|
|
|
|
const handleFilePreviewResizeStart = useCallback((event: ReactPointerEvent<HTMLButtonElement>) => {
|
2026-09-21 17:48:49 +08:00
|
|
|
filePreviewResizeCleanupRef.current?.();
|
2026-06-06 19:49:33 +08:00
|
|
|
event.preventDefault();
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
const panel = event.currentTarget.closest<HTMLElement>("[data-file-preview-panel]");
|
|
|
|
|
const shellRect = shellRef.current?.getBoundingClientRect();
|
|
|
|
|
const rightEdge = shellRect?.right ?? window.innerWidth;
|
|
|
|
|
const maxWidth = maxFilePreviewWidth(shellRect?.width ?? window.innerWidth);
|
|
|
|
|
const originalBodyCursor = document.body.style.cursor;
|
|
|
|
|
const originalBodyUserSelect = document.body.style.userSelect;
|
|
|
|
|
const originalPanelTransition = panel?.style.transition ?? "";
|
2026-09-23 14:04:30 +08:00
|
|
|
const frameElement = panel?.querySelector("iframe");
|
|
|
|
|
const originalFramePointerEvents = frameElement?.style.pointerEvents ?? "";
|
2026-06-06 19:49:33 +08:00
|
|
|
let nextWidth = filePreviewWidthRef.current;
|
|
|
|
|
let frame: number | null = null;
|
|
|
|
|
|
|
|
|
|
document.body.style.cursor = "col-resize";
|
|
|
|
|
document.body.style.userSelect = "none";
|
|
|
|
|
if (panel) panel.style.transition = "none";
|
2026-09-23 14:04:30 +08:00
|
|
|
if (frameElement) frameElement.style.pointerEvents = "none";
|
2026-06-06 19:49:33 +08:00
|
|
|
|
|
|
|
|
const applyWidth = (clientX: number) => {
|
|
|
|
|
nextWidth = clampFilePreviewWidth(rightEdge - clientX, maxWidth);
|
|
|
|
|
filePreviewWidthRef.current = nextWidth;
|
|
|
|
|
if (frame !== null) return;
|
|
|
|
|
frame = window.requestAnimationFrame(() => {
|
|
|
|
|
frame = null;
|
|
|
|
|
panel?.style.setProperty("--file-preview-width", `${nextWidth}px`);
|
|
|
|
|
panel?.style.setProperty("--file-preview-slot-width", `${nextWidth}px`);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
const handlePointerMove = (moveEvent: PointerEvent) => {
|
|
|
|
|
moveEvent.preventDefault();
|
|
|
|
|
applyWidth(moveEvent.clientX);
|
|
|
|
|
};
|
2026-09-21 17:48:49 +08:00
|
|
|
const stopResize = (commit: boolean) => {
|
2026-06-06 19:49:33 +08:00
|
|
|
if (frame !== null) {
|
|
|
|
|
window.cancelAnimationFrame(frame);
|
|
|
|
|
frame = null;
|
|
|
|
|
}
|
|
|
|
|
panel?.style.setProperty("--file-preview-width", `${nextWidth}px`);
|
|
|
|
|
panel?.style.setProperty("--file-preview-slot-width", `${nextWidth}px`);
|
|
|
|
|
if (panel) panel.style.transition = originalPanelTransition;
|
2026-09-23 14:04:30 +08:00
|
|
|
if (frameElement) frameElement.style.pointerEvents = originalFramePointerEvents;
|
2026-09-21 17:48:49 +08:00
|
|
|
if (commit) setFilePreviewWidth(nextWidth);
|
2026-06-06 19:49:33 +08:00
|
|
|
document.body.style.cursor = originalBodyCursor;
|
|
|
|
|
document.body.style.userSelect = originalBodyUserSelect;
|
|
|
|
|
window.removeEventListener("pointermove", handlePointerMove);
|
|
|
|
|
window.removeEventListener("pointerup", handlePointerUp);
|
|
|
|
|
window.removeEventListener("pointercancel", handlePointerUp);
|
2026-09-21 17:48:49 +08:00
|
|
|
filePreviewResizeCleanupRef.current = null;
|
2026-06-06 19:49:33 +08:00
|
|
|
};
|
2026-09-21 17:48:49 +08:00
|
|
|
const handlePointerUp = () => stopResize(true);
|
|
|
|
|
filePreviewResizeCleanupRef.current = () => stopResize(false);
|
2026-06-06 19:49:33 +08:00
|
|
|
|
|
|
|
|
applyWidth(event.clientX);
|
|
|
|
|
window.addEventListener("pointermove", handlePointerMove);
|
|
|
|
|
window.addEventListener("pointerup", handlePointerUp);
|
|
|
|
|
window.addEventListener("pointercancel", handlePointerUp);
|
2026-09-21 17:48:49 +08:00
|
|
|
}, [setFilePreviewWidth]);
|
2026-06-06 19:49:33 +08:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-09-24 01:44:23 +08:00
|
|
|
if (!previewOpen) return;
|
2026-06-06 19:49:33 +08:00
|
|
|
const clampToShell = () => {
|
|
|
|
|
const shellWidth = shellRef.current?.getBoundingClientRect().width ?? window.innerWidth;
|
|
|
|
|
const maxWidth = maxFilePreviewWidth(shellWidth);
|
2026-09-21 17:48:49 +08:00
|
|
|
setFilePreviewMaxWidth(maxWidth);
|
2026-06-06 19:49:33 +08:00
|
|
|
};
|
|
|
|
|
clampToShell();
|
2026-09-21 17:48:49 +08:00
|
|
|
const observer = typeof ResizeObserver === "undefined" ? null : new ResizeObserver(clampToShell);
|
|
|
|
|
if (shellRef.current) observer?.observe(shellRef.current);
|
2026-06-06 19:49:33 +08:00
|
|
|
window.addEventListener("resize", clampToShell);
|
|
|
|
|
return () => {
|
2026-09-21 17:48:49 +08:00
|
|
|
observer?.disconnect();
|
2026-06-06 19:49:33 +08:00
|
|
|
window.removeEventListener("resize", clampToShell);
|
|
|
|
|
};
|
2026-09-24 01:44:23 +08:00
|
|
|
}, [previewOpen]);
|
2026-06-06 19:49:33 +08:00
|
|
|
|
2026-06-05 19:49:34 +08:00
|
|
|
const handleForkFromMessage = useCallback(
|
|
|
|
|
async (beforeUserIndex: number) => {
|
|
|
|
|
if (!chatId || !onForkChat) return;
|
|
|
|
|
const forkedChatId = await onForkChat(chatId, beforeUserIndex);
|
2026-06-10 02:54:19 +08:00
|
|
|
if (!forkedChatId) return;
|
2026-06-05 19:49:34 +08:00
|
|
|
messageCacheRef.current.delete(forkedChatId);
|
|
|
|
|
},
|
2026-06-10 02:54:19 +08:00
|
|
|
[chatId, onForkChat],
|
2026-06-05 19:49:34 +08:00
|
|
|
);
|
|
|
|
|
|
2026-11-06 11:31:42 +08:00
|
|
|
const composer = (renderContextUsage: ComposerUsageControl | undefined) => (
|
2026-10-20 15:48:32 +08:00
|
|
|
<div className={composerVariant === "hero" ? "thread-composer-stack is-hero" : "thread-composer-stack"}>
|
2026-09-20 15:51:08 +08:00
|
|
|
{modelFallback?.chatId === chatId && !modelFallback.dismissed ? (
|
|
|
|
|
<ModelFallbackNotice
|
|
|
|
|
model={modelFallback.model}
|
2026-09-20 16:05:03 +08:00
|
|
|
reauthProvider={modelFallback.reauthProvider}
|
2026-09-20 16:02:09 +08:00
|
|
|
reauthProviderLabel={modelFallback.reauthProvider
|
|
|
|
|
? providerDisplayLabel(settings?.providers ?? [], modelFallback.reauthProvider)
|
|
|
|
|
: undefined}
|
2026-09-20 15:51:08 +08:00
|
|
|
onOpenSettings={onOpenModelSettings}
|
|
|
|
|
onDismiss={() => setModelFallback((current) => current && { ...current, dismissed: true })}
|
|
|
|
|
/>
|
|
|
|
|
) : null}
|
2026-08-22 19:33:22 +08:00
|
|
|
{recoveryState ? (
|
|
|
|
|
<RecoveryNotice
|
|
|
|
|
state={recoveryState}
|
|
|
|
|
onContinue={continueRecovery}
|
|
|
|
|
onDismiss={dismissRecovery}
|
2026-10-20 15:48:32 +08:00
|
|
|
onOpenSettings={onOpenModelSettings}
|
2026-08-22 19:33:22 +08:00
|
|
|
/>
|
|
|
|
|
) : null}
|
2026-07-29 23:15:45 +08:00
|
|
|
{streamError && !hasInlineDeliveryError(messages, streamError) ? (
|
2026-05-06 14:15:36 +00:00
|
|
|
<StreamErrorNotice
|
|
|
|
|
error={streamError}
|
|
|
|
|
onDismiss={dismissStreamError}
|
2026-10-20 15:48:32 +08:00
|
|
|
onOpenSettings={onOpenModelSettings}
|
2026-05-06 14:15:36 +00:00
|
|
|
/>
|
|
|
|
|
) : null}
|
2026-10-22 18:32:18 +08:00
|
|
|
{restoredFirstDraft?.chatId === chatId && restoredFirstDraft.error ? (
|
|
|
|
|
<p role="alert" className="px-3 text-sm text-destructive">{restoredFirstDraft.error}</p>
|
|
|
|
|
) : null}
|
2026-10-26 13:16:44 +08:00
|
|
|
<SubagentTaskDock inputKey={latestSubagentInputKey(displayMessages)} />
|
2026-05-06 14:15:36 +00:00
|
|
|
{session ? (
|
|
|
|
|
<ThreadComposer
|
2026-10-22 18:32:18 +08:00
|
|
|
key={`${draftKey}:${restoredFirstDraft?.chatId === chatId ? restoredFirstDraft.revision : 0}`}
|
2026-09-25 23:42:01 +08:00
|
|
|
draftKey={draftKey}
|
|
|
|
|
draftStore={draftStore}
|
|
|
|
|
persistDraft={persistDraft}
|
2026-05-13 16:39:07 +00:00
|
|
|
onSend={handleThreadSend}
|
2026-06-10 02:54:19 +08:00
|
|
|
disabled={!chatId}
|
2026-08-12 14:45:12 +08:00
|
|
|
inputAriaLabel={composerInputAriaLabel}
|
2026-07-26 00:18:24 +08:00
|
|
|
isStreaming={turnActive}
|
2026-10-16 13:33:41 +08:00
|
|
|
activeTurnId={turnActive && chatId ? client.getRunTurnId(chatId) ?? historyActiveTurnId : null}
|
|
|
|
|
completedTurnIds={messagesReady && !recoveryNeedsDecision
|
|
|
|
|
&& chatId && !client.hasUnsettledRun(chatId) ? queueCompletedTurnIds : undefined}
|
2026-06-06 19:49:33 +08:00
|
|
|
modelLabel={modelBadgeLabel}
|
2026-08-22 19:41:37 +08:00
|
|
|
modelDetail={modelBadge.model}
|
2026-07-24 17:34:26 +08:00
|
|
|
modelPreset={activeModelPreset}
|
|
|
|
|
modelPresets={modelPresetOptions}
|
|
|
|
|
onModelPresetChange={handleModelPresetChange}
|
2026-05-24 13:38:37 +08:00
|
|
|
modelProvider={modelBadge.provider}
|
|
|
|
|
modelProviderLabel={modelBadge.providerLabel}
|
2026-06-06 19:49:33 +08:00
|
|
|
modelNeedsSetup={modelBadge.needsSetup}
|
|
|
|
|
onModelBadgeClick={modelBadge.needsSetup ? onOpenModelSettings : undefined}
|
2026-08-25 19:32:07 +08:00
|
|
|
onManageModels={onOpenModelSettings}
|
2026-11-06 11:31:42 +08:00
|
|
|
renderContextUsage={renderContextUsage}
|
2026-08-12 14:45:12 +08:00
|
|
|
variant={composerVariant}
|
2026-11-06 11:31:42 +08:00
|
|
|
surfaceRef={setComposerSurface}
|
2026-08-05 19:24:41 +08:00
|
|
|
slashCommands={availableSlashCommands}
|
2026-09-24 23:10:57 +08:00
|
|
|
onMentionSearch={requestMentionCatalogs}
|
2026-05-22 22:25:12 +08:00
|
|
|
cliApps={cliApps}
|
2026-05-24 13:38:37 +08:00
|
|
|
mcpPresets={mcpPresets}
|
2026-08-18 18:28:19 +08:00
|
|
|
sessions={mentionSessions}
|
2026-07-01 15:52:34 +08:00
|
|
|
skills={skills}
|
2026-05-08 09:40:15 +00:00
|
|
|
onStop={stop}
|
2026-10-22 18:32:18 +08:00
|
|
|
onInspectAttachment={inspectAttachment}
|
2026-06-09 01:08:49 +08:00
|
|
|
onTranscribeAudio={transcribeAudio}
|
2026-07-26 00:18:24 +08:00
|
|
|
goalState={currentGoalState}
|
2026-10-07 13:53:48 +08:00
|
|
|
onFavoriteWorkspaceProject={favoriteWorkspaceProject}
|
|
|
|
|
onResolveWorkspaceProject={resolveWorkspaceProject}
|
|
|
|
|
onLoadWorkspaceProjects={loadWorkspaceProjects}
|
|
|
|
|
workspacePickerLayoutAnchor={conversationElement}
|
|
|
|
|
onBrowseWorkspaceDirectories={browseWorkspaceDirectories}
|
2026-05-29 03:42:53 +08:00
|
|
|
workspaceScope={workspaceScope}
|
2026-08-06 18:29:04 +08:00
|
|
|
workspaceControlsHidden={temporary}
|
2026-05-29 03:42:53 +08:00
|
|
|
workspaceDefaultScope={workspaceDefaultScope}
|
|
|
|
|
workspaceControls={workspaceControls}
|
|
|
|
|
workspaceScopeDisabled={workspaceScopeDisabled}
|
2026-10-23 21:09:21 +08:00
|
|
|
workspaceProjectLocked={projectLocked}
|
2026-05-29 03:42:53 +08:00
|
|
|
workspaceError={workspaceError}
|
|
|
|
|
onWorkspaceScopeChange={onWorkspaceScopeChange}
|
2026-08-07 11:20:08 +08:00
|
|
|
pendingQueueKey={temporary ? null : chatId}
|
2026-06-24 19:13:11 +08:00
|
|
|
transcriptionProvider={settingsSnapshot?.transcription?.provider}
|
2026-07-14 14:47:42 +08:00
|
|
|
ingressLimits={ingressLimits}
|
2026-07-22 22:35:39 +08:00
|
|
|
quotedContext={quotedContext}
|
2026-09-02 04:19:12 +08:00
|
|
|
focusRequest={composerFocusSignal}
|
2026-07-22 22:35:39 +08:00
|
|
|
onQuotedContextChange={setQuotedContext}
|
2026-05-06 14:15:36 +00:00
|
|
|
/>
|
|
|
|
|
) : (
|
|
|
|
|
<ThreadComposer
|
2026-09-25 23:42:01 +08:00
|
|
|
key={draftKey}
|
|
|
|
|
draftKey={draftKey}
|
|
|
|
|
draftStore={draftStore}
|
|
|
|
|
persistDraft={persistDraft}
|
2026-05-06 14:15:36 +00:00
|
|
|
onSend={handleWelcomeSend}
|
|
|
|
|
disabled={booting}
|
2026-08-12 14:45:12 +08:00
|
|
|
inputAriaLabel={composerInputAriaLabel}
|
2026-07-26 00:18:24 +08:00
|
|
|
isStreaming={turnActive}
|
2026-11-05 13:32:55 +08:00
|
|
|
placeholder={booting ? t("thread.composer.placeholderOpening") : undefined}
|
2026-06-06 19:49:33 +08:00
|
|
|
modelLabel={modelBadgeLabel}
|
2026-08-22 19:41:37 +08:00
|
|
|
modelDetail={modelBadge.model}
|
2026-07-24 17:34:26 +08:00
|
|
|
modelPreset={activeModelPreset}
|
|
|
|
|
modelPresets={modelPresetOptions}
|
|
|
|
|
onModelPresetChange={handleModelPresetChange}
|
2026-05-24 13:38:37 +08:00
|
|
|
modelProvider={modelBadge.provider}
|
|
|
|
|
modelProviderLabel={modelBadge.providerLabel}
|
2026-06-06 19:49:33 +08:00
|
|
|
modelNeedsSetup={modelBadge.needsSetup}
|
|
|
|
|
onModelBadgeClick={modelBadge.needsSetup ? onOpenModelSettings : undefined}
|
2026-08-25 19:32:07 +08:00
|
|
|
onManageModels={onOpenModelSettings}
|
2026-11-06 11:31:42 +08:00
|
|
|
renderContextUsage={renderContextUsage}
|
2026-05-06 14:15:36 +00:00
|
|
|
variant="hero"
|
2026-08-05 19:24:41 +08:00
|
|
|
slashCommands={availableSlashCommands}
|
2026-09-24 23:10:57 +08:00
|
|
|
onMentionSearch={requestMentionCatalogs}
|
2026-05-22 22:25:12 +08:00
|
|
|
cliApps={cliApps}
|
2026-05-24 13:38:37 +08:00
|
|
|
mcpPresets={mcpPresets}
|
2026-08-18 18:28:19 +08:00
|
|
|
sessions={mentionSessions}
|
2026-07-01 15:52:34 +08:00
|
|
|
skills={skills}
|
2026-11-06 11:31:42 +08:00
|
|
|
surfaceRef={setComposerSurface}
|
2026-10-22 18:32:18 +08:00
|
|
|
onInspectAttachment={inspectAttachment}
|
2026-06-09 01:08:49 +08:00
|
|
|
onTranscribeAudio={transcribeAudio}
|
2026-07-26 00:18:24 +08:00
|
|
|
goalState={currentGoalState}
|
2026-10-07 13:53:48 +08:00
|
|
|
onFavoriteWorkspaceProject={favoriteWorkspaceProject}
|
|
|
|
|
onResolveWorkspaceProject={resolveWorkspaceProject}
|
|
|
|
|
onLoadWorkspaceProjects={loadWorkspaceProjects}
|
|
|
|
|
workspacePickerLayoutAnchor={conversationElement}
|
|
|
|
|
onBrowseWorkspaceDirectories={browseWorkspaceDirectories}
|
2026-05-29 03:42:53 +08:00
|
|
|
workspaceScope={workspaceScope}
|
2026-08-06 18:29:04 +08:00
|
|
|
workspaceControlsHidden={temporary}
|
2026-05-29 03:42:53 +08:00
|
|
|
workspaceDefaultScope={workspaceDefaultScope}
|
|
|
|
|
workspaceControls={workspaceControls}
|
|
|
|
|
workspaceScopeDisabled={workspaceScopeDisabled}
|
2026-10-23 21:09:21 +08:00
|
|
|
workspaceProjectLocked={projectLocked}
|
2026-05-29 03:42:53 +08:00
|
|
|
workspaceError={workspaceError}
|
|
|
|
|
onWorkspaceScopeChange={onWorkspaceScopeChange}
|
2026-06-24 19:13:11 +08:00
|
|
|
transcriptionProvider={settingsSnapshot?.transcription?.provider}
|
2026-07-14 14:47:42 +08:00
|
|
|
ingressLimits={ingressLimits}
|
2026-09-25 23:42:01 +08:00
|
|
|
quotedContext={quotedContext}
|
|
|
|
|
onQuotedContextChange={setQuotedContext}
|
2026-05-06 14:15:36 +00:00
|
|
|
/>
|
|
|
|
|
)}
|
2026-10-20 15:48:32 +08:00
|
|
|
</div>
|
2026-04-18 18:51:53 +00:00
|
|
|
);
|
|
|
|
|
|
2026-10-20 21:41:30 +08:00
|
|
|
const emptyState = initialHistoryLoading ? (
|
|
|
|
|
<ThreadLoadingSkeleton />
|
2026-04-18 18:51:53 +00:00
|
|
|
) : (
|
2026-08-06 18:26:13 +08:00
|
|
|
<div className="flex w-full flex-col items-center text-center animate-in fade-in-0 slide-in-from-bottom-2 [animation-duration:220ms] motion-reduce:animate-none">
|
2026-07-23 18:18:34 +08:00
|
|
|
<HeroGreeting text={t(heroGreetingKey)} />
|
2026-04-18 18:51:53 +00:00
|
|
|
</div>
|
|
|
|
|
);
|
2026-06-06 19:49:33 +08:00
|
|
|
const sessionInfoAction = historyKey ? (
|
2026-11-05 17:09:20 +08:00
|
|
|
<SessionInfoPopover client={client} sessionKey={historyKey} token={token} />
|
2026-06-06 19:49:33 +08:00
|
|
|
) : undefined;
|
|
|
|
|
const promptNavigatorAction = historyKey ? (
|
|
|
|
|
<PromptNavigator
|
2026-11-05 17:09:20 +08:00
|
|
|
shortcutEnabled={composerActive}
|
2026-06-06 19:49:33 +08:00
|
|
|
messages={displayMessages}
|
2026-10-18 14:57:23 +08:00
|
|
|
prompts={promptNavigation.prompts}
|
2026-06-06 19:49:33 +08:00
|
|
|
onJumpToPrompt={(promptId) => viewportRef.current?.jumpToUserPrompt(promptId)}
|
|
|
|
|
/>
|
|
|
|
|
) : undefined;
|
2026-04-18 18:51:53 +00:00
|
|
|
|
2026-08-12 14:45:12 +08:00
|
|
|
const threadHeader = !hideHeader ? (
|
|
|
|
|
<ThreadHeader
|
2026-09-24 01:44:23 +08:00
|
|
|
className={previewOpen ? "h-10" : undefined}
|
2026-08-12 14:45:12 +08:00
|
|
|
title={title}
|
|
|
|
|
onToggleSidebar={onToggleSidebar}
|
|
|
|
|
theme={theme}
|
|
|
|
|
onToggleTheme={onToggleTheme}
|
|
|
|
|
hideSidebarToggleForHostChrome={hideSidebarToggleForHostChrome}
|
|
|
|
|
hideSidebarToggle={hideSidebarToggle}
|
|
|
|
|
hideThemeButton={hideThemeButton}
|
|
|
|
|
hideTitle={hideHeaderTitle}
|
|
|
|
|
actions={headerActions}
|
|
|
|
|
minimal={!session && !loading}
|
|
|
|
|
promptNavigatorAction={promptNavigatorAction}
|
|
|
|
|
sessionInfoAction={sessionInfoAction}
|
|
|
|
|
temporaryChatEnabled={temporaryChatEnabled}
|
|
|
|
|
temporaryChatDisabled={booting || turnActive}
|
|
|
|
|
onTemporaryChatEnabledChange={
|
|
|
|
|
showTemporaryChatControl ? onTemporaryChatEnabledChange : undefined
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
) : null;
|
|
|
|
|
|
2026-04-18 18:51:53 +00:00
|
|
|
return (
|
2026-10-05 05:16:33 +08:00
|
|
|
<SubagentTasksProvider client={client} token={token}
|
|
|
|
|
sessionKey={session?.key ?? null}
|
|
|
|
|
liveEvents={webuiCapabilities.includes("webui.subagents.events.v1")}
|
|
|
|
|
historyEnabled={webuiCapabilities.includes("webui.subagents.history.v1")}
|
|
|
|
|
active={composerActive}
|
|
|
|
|
enabled={!temporary && !!session?.key.startsWith("websocket:") && webuiCapabilities.includes("webui.subagents.v1")}>
|
2026-11-05 17:09:20 +08:00
|
|
|
<section ref={setSidebarHost} style={{ "--sidebar-enter-width": `${sidebarEnterWidth}px`, "--sidebar-enter-mobile-width": sidebarEnterWidth ? "100%" : "0px" } as CSSProperties} data-preview-open={sidebarOpen || undefined} className="thread-preview-layout relative flex min-h-0 flex-1 overflow-hidden">
|
2026-10-07 13:53:48 +08:00
|
|
|
<div ref={setConversationElement} className={cn(
|
2026-09-24 01:44:23 +08:00
|
|
|
"thread-conversation relative flex min-w-0 flex-1 flex-col overflow-hidden",
|
2026-09-14 15:25:13 +08:00
|
|
|
headerPortalTarget === undefined && !hideHeader && "thread-workspace",
|
|
|
|
|
)}>
|
2026-08-25 01:55:58 +08:00
|
|
|
{hideHeaderTitle && inlineHandle && !temporary && session?.handle ? (
|
2026-08-18 09:53:55 +08:00
|
|
|
<div
|
|
|
|
|
aria-label={`Session @${session.handle.name}`}
|
|
|
|
|
className="flex h-8 shrink-0 items-center px-3 text-[12px]"
|
|
|
|
|
>
|
|
|
|
|
<span
|
|
|
|
|
className="shrink-0"
|
|
|
|
|
>
|
2026-08-18 18:28:19 +08:00
|
|
|
<SessionHandleLabel id={session.handle.id}>
|
2026-08-18 09:53:55 +08:00
|
|
|
@{session.handle.name}
|
2026-08-18 18:28:19 +08:00
|
|
|
</SessionHandleLabel>
|
2026-08-18 09:53:55 +08:00
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
) : null}
|
2026-08-12 14:45:12 +08:00
|
|
|
{headerPortalTarget === undefined ? threadHeader : null}
|
2026-07-15 10:45:40 +08:00
|
|
|
<FilePreviewAvailabilityProvider
|
2026-09-21 17:48:49 +08:00
|
|
|
resolve={previewSessionKey ? resolveFilePreviewAvailability : undefined}
|
2026-07-15 10:45:40 +08:00
|
|
|
>
|
2026-09-23 14:03:37 +08:00
|
|
|
<FileActionsProvider value={previewSessionKey ? fileActions : undefined}>
|
2026-07-15 10:45:40 +08:00
|
|
|
<ThreadViewport
|
|
|
|
|
ref={viewportRef}
|
|
|
|
|
messages={displayMessages}
|
2026-11-02 17:42:16 +08:00
|
|
|
messagesKey={messagesKey}
|
2026-10-18 14:57:23 +08:00
|
|
|
navigation={promptNavigation}
|
2026-10-18 16:58:17 +08:00
|
|
|
navigationFrame={navigationFrame}
|
2026-08-06 18:29:04 +08:00
|
|
|
temporary={temporary}
|
2026-07-26 00:18:24 +08:00
|
|
|
isStreaming={turnActive}
|
2026-08-14 17:03:11 +08:00
|
|
|
runStartedAt={currentRunStartedAt}
|
2026-09-07 15:12:39 +08:00
|
|
|
retryStatus={retryStatus}
|
2026-07-15 10:45:40 +08:00
|
|
|
emptyState={emptyState}
|
2026-10-20 21:41:30 +08:00
|
|
|
initialLoading={initialHistoryLoading}
|
2026-11-06 15:55:51 +08:00
|
|
|
initialError={historyError}
|
|
|
|
|
onRetryInitial={refreshHistory}
|
2026-11-05 17:09:20 +08:00
|
|
|
composer={inlineComposer ? <div ref={setLocalComposerHost} /> : null}
|
2026-11-06 11:49:08 +08:00
|
|
|
onComposerLayoutChange={alignSidebar}
|
2026-07-26 00:18:24 +08:00
|
|
|
activeTurnId={viewportTurnId}
|
|
|
|
|
activeTurnStartedHere={activeTurnStartedHere}
|
2026-07-15 10:45:40 +08:00
|
|
|
conversationKey={historyKey}
|
2026-07-26 00:18:24 +08:00
|
|
|
conversationReady={messagesReady}
|
2026-07-15 10:45:40 +08:00
|
|
|
showScrollToBottomButton={!!session}
|
|
|
|
|
cliApps={cliApps}
|
|
|
|
|
mcpPresets={mcpPresets}
|
2026-08-05 19:24:41 +08:00
|
|
|
slashCommands={availableSlashCommands}
|
2026-07-15 10:45:40 +08:00
|
|
|
forkBoundaryMessageCount={forkBoundaryMessageCount}
|
|
|
|
|
hasMoreBefore={hasMoreBefore}
|
2026-10-21 18:33:53 +08:00
|
|
|
loadingOlder={loadingOlder}
|
2026-09-27 21:06:59 +08:00
|
|
|
olderError={olderError}
|
2026-07-15 10:45:40 +08:00
|
|
|
userMessageOffset={userMessageOffset}
|
|
|
|
|
onLoadOlder={loadOlder}
|
2026-09-12 04:37:37 +08:00
|
|
|
traceDetailScope={historyKey}
|
|
|
|
|
onLoadTraceDetails={messagesReady ? loadTraceDetails : undefined}
|
2026-09-21 17:48:49 +08:00
|
|
|
onOpenFilePreview={previewSessionKey ? openFilePreview : undefined}
|
2026-07-15 10:45:40 +08:00
|
|
|
onForkFromMessage={onForkChat ? handleForkFromMessage : undefined}
|
2026-07-22 22:35:39 +08:00
|
|
|
onQuoteSelection={session ? handleQuoteSelection : undefined}
|
2026-07-15 10:45:40 +08:00
|
|
|
/>
|
2026-09-23 14:03:37 +08:00
|
|
|
</FileActionsProvider>
|
2026-07-15 10:45:40 +08:00
|
|
|
</FilePreviewAvailabilityProvider>
|
2026-06-06 19:49:33 +08:00
|
|
|
</div>
|
2026-08-12 14:45:12 +08:00
|
|
|
{headerPortalTarget && headerActive
|
|
|
|
|
? createPortal(threadHeader, headerPortalTarget)
|
|
|
|
|
: null}
|
2026-11-06 11:31:42 +08:00
|
|
|
<ComposerUsagePopover
|
|
|
|
|
sessionKey={previewSessionKey}
|
|
|
|
|
context={composerContextUsage}
|
|
|
|
|
rounds={composerRoundUsage}
|
|
|
|
|
snapshots={contextGraphSnapshots}
|
|
|
|
|
sidebarHost={contextSidebarHost}
|
|
|
|
|
open={contextSidebarOpen}
|
|
|
|
|
onOpenChange={handleContextSidebarOpenChange}
|
|
|
|
|
onOpenFile={handleOpenFilePreview}
|
|
|
|
|
>
|
|
|
|
|
{(renderContextUsage) => createPortal(
|
|
|
|
|
<div
|
|
|
|
|
hidden={!composerActive}
|
|
|
|
|
aria-hidden={!composerActive}
|
|
|
|
|
data-testid={composerActive ? "active-pane-composer" : undefined}
|
|
|
|
|
data-active-composer={composerActive || undefined}
|
|
|
|
|
>
|
|
|
|
|
{composer(renderContextUsage)}
|
|
|
|
|
</div>,
|
|
|
|
|
composerMount,
|
|
|
|
|
)}
|
|
|
|
|
</ComposerUsagePopover>
|
2026-09-24 01:44:23 +08:00
|
|
|
{activePreview && previewSessionKey ? (
|
|
|
|
|
<FileActionsProvider key={previewSessionKey} value={fileActions}>
|
|
|
|
|
<PreviewPane
|
|
|
|
|
key={previewSessionKey}
|
|
|
|
|
tabs={visiblePreview.tabs}
|
|
|
|
|
activeId={activePreview.id}
|
|
|
|
|
width={filePreviewWidth}
|
2026-06-06 19:49:33 +08:00
|
|
|
isClosing={filePreviewClosing}
|
2026-09-24 01:44:23 +08:00
|
|
|
onSelect={selectTab}
|
|
|
|
|
onCloseTab={handleClosePreviewTab}
|
2026-06-06 19:49:33 +08:00
|
|
|
onClose={handleCloseFilePreview}
|
2026-09-23 14:04:30 +08:00
|
|
|
onResizeStart={handleFilePreviewResizeStart}
|
2026-09-24 01:44:23 +08:00
|
|
|
>
|
2026-10-22 16:34:54 +08:00
|
|
|
<FilePreviewPanel
|
|
|
|
|
key={activePreview.id}
|
|
|
|
|
sessionKey={previewSessionKey}
|
|
|
|
|
path={activePreview.value}
|
|
|
|
|
token={token}
|
|
|
|
|
loadPreview={filePreviews.load}
|
|
|
|
|
initialPreview={filePreviews.peek(activePreview.value)}
|
|
|
|
|
onOpenFilePreview={openFilePreview}
|
|
|
|
|
onResolvePath={resolveFile}
|
|
|
|
|
/>
|
2026-09-24 01:44:23 +08:00
|
|
|
</PreviewPane>
|
|
|
|
|
</FileActionsProvider>
|
2026-09-23 14:04:30 +08:00
|
|
|
) : null}
|
2026-04-18 18:51:53 +00:00
|
|
|
</section>
|
2026-10-05 05:16:33 +08:00
|
|
|
</SubagentTasksProvider>
|
2026-04-18 18:51:53 +00:00
|
|
|
);
|
|
|
|
|
}
|