import { lazy, useEffect, useMemo, useRef, useState, type RefObject } from "react"; import { QueryClient, QueryClientProvider, useInfiniteQuery, useMutation, useQueryClient } from "@tanstack/react-query"; import { Brain, Folder, SlidersHorizontal } from "lucide-react"; import type { APIClient } from "../api/client"; import type { ProviderDefinitionView, TaskBudgetSettings, ThreadDetailView, ThreadSearchResultView, ThreadView, WorkspaceView } from "../api/types"; import { normalizedTaskBudget } from "../api/task-configuration"; import { useConnectionStore } from "../state/connection"; import { V2Composer, v2ComposerNotSubmitted } from "./components/composer"; import { V2ApprovalModeControl } from "./components/approval-mode-control"; import type { ExecutionApprovalMode } from "./components/approval-mode-contract"; import { applyCreationApproval } from "./creation-approval"; import { v2FileReferenceKey, type V2FileReference } from "./components/file-context"; import { V2Conversation } from "./components/conversation"; import { V2ConfirmDialog } from "./components/dialog"; import { V2LazySurface } from "./components/lazy-surface"; import type { V2NetworkMode } from "./components/network-scope-control"; import { V2SettingsSidebar, V2Sidebar, type V2SettingsSection } from "./components/sidebar"; import { V2SidebarLayout } from "./components/sidebar-layout"; import { V2Titlebar } from "./components/titlebar"; import { useV2Client } from "./client-session"; import { v2QueryKeys } from "./query-keys"; import { useV2ThreadUnread } from "./use-thread-unread"; import { registerV2RecoveredTurn, useV2RestoreTurns, useV2ThreadTurn, v2TurnFailed } from "./use-thread-turn"; import { ProjectDialog, V2WorkspaceStart } from "./components/workspace-start"; import { useV2Navigation, type V2RunPane } from "./navigation"; import { readDensity } from "../lib/ui-density"; import { useLocale } from "../lib/locale"; import { V2PersistenceConflicts, V2RecoveryProvider, useV2PersistentState, useV2PersistenceWarning, useV2RecoveryStore } from "./recovery-storage"; import { useV2Drafts, validRecoveryTurn } from "./recovery-session"; import { readV2Route } from "./navigation"; import { useV2CreationRecovery } from "./recovery-creation"; import type { WorkspaceImageAttachment } from "../api/image-attachments"; import { mcpReferenceIdentities, type MCPDraftReference } from "../api/mcp-references"; import { v2MCPReferenceKey } from "./components/mcp-reference-picker"; import { fileAttachmentIdentities, type WorkspaceFileAttachment } from "../api/file-attachments"; import { v2AttachmentImportKey, v2AttachmentReferenceKey } from "./attachment-keys"; import { v2ImageReferenceKey } from "./components/image-input"; import { assertV2DraftVersion, getV2DraftDocument, readV2Draft, requireV2DraftVersion, useV2DraftDocument, v2DraftScope } from "./draft-context"; import type { V2DraftVersion } from "./draft-version"; import type { TaskReviewToolMemory } from "./components/task-review-tools"; import { V2DraftConflict } from "./components/draft-conflict"; import { FileEditDraftProvider } from "../components/file-edit-drafts"; import { V2PhasePicker, type V2WorkPhase } from "./components/phase-picker"; import { useV2DraftTaskConfiguration } from "./draft-task-configuration"; import { submitSkillChoice, type ProjectSkillChoice } from "../api/project-skills"; import { useV2WebAccessDefaults } from "./web-access-defaults"; import { useProjectlessWorkspace } from "./projectless-workspace"; import { threadProjectID } from "./components/thread-project-dialog"; import { useChatShortcuts } from "./chat-preferences"; import { V2DraftBrowserWorkspace } from "./components/draft-browser-workspace"; import { adoptDraftSharedBrowser, bindDraftSharedBrowser } from "./shared-browser-session"; type CreationAttempt = Map; type NewThreadOptions = { networkMode: V2NetworkMode; allowedTargets: string[]; modelRoute: { provider: string; model: string } | null }; import "./styles.css"; import "./shared-shell.css"; const V2Settings = lazy(() => import("./components/settings").then((module) => ({ default: module.V2Settings, }))); const V2HistoryPage = lazy(() => import("./components/history-page").then((module) => ({ default: module.V2HistoryPage }))); const V2RunDetailsPage = lazy(() => import("./components/run-details-page").then((module) => ({ default: module.V2RunDetailsPage }))); const V2SessionDetailsPage = lazy(() => import("./components/session-details-page").then((module) => ({ default: module.V2SessionDetailsPage }))); const V2AutomationsPage = lazy(() => import("./components/automations-page").then((module) => ({ default: module.V2AutomationsPage }))); function NewConversation({ client, workspaces, workspaceID, projectID, workspaceNotice, onWorkspaceChange, onCreated, onTurnSuccess, onManageModels, onOpenTaskConfiguration, onOpenWebSettings, phase, onPhaseChange, budget, configurationValid, webAccessValid, projectSkills, draft: legacyDraft, onDraftChange: legacyDraftChange, creationAttemptRef, options, onOptionsChange, moreProjects, onImported }: { client: APIClient; workspaces: WorkspaceView[]; workspaceID: string; projectID?: string; workspaceNotice?: React.ReactNode; onWorkspaceChange: (workspaceID: string) => void; onImported: (workspace: WorkspaceView, currentDraft?: string) => void; onCreated: (thread: ThreadView, submittedDraft: string, files: V2FileReference[], images?: WorkspaceImageAttachment[], version?: V2DraftVersion, attachments?: WorkspaceFileAttachment[], shouldOpen?: boolean, mcpReferences?: MCPDraftReference[]) => void; onTurnSuccess: (threadID: string, submittedDraft: string) => void; onManageModels: (prepareForDraft?: boolean) => void; onOpenTaskConfiguration: () => void; onOpenWebSettings: () => void; phase: V2WorkPhase; onPhaseChange: (phase: V2WorkPhase) => void; budget?: TaskBudgetSettings; configurationValid: boolean; webAccessValid: boolean; projectSkills?: ProjectSkillChoice; draft: string; onDraftChange: (content: string, expected?: string) => void; creationAttemptRef: RefObject; options: NewThreadOptions; onOptionsChange: (options: NewThreadOptions) => void; moreProjects?: React.ReactNode; }) { const { t } = useLocale(); const queryClient = useQueryClient(); const turn = useV2ThreadTurn(client); const recovery = useV2RecoveryStore(); const activeRef = useRef(true); const workspaceRef = useRef(workspaceID); const creationRequestRef = useRef(0); if (workspaceRef.current !== workspaceID) { workspaceRef.current = workspaceID; creationRequestRef.current += 1; } useEffect(() => { activeRef.current = true; return () => { activeRef.current = false; }; }, []); const managedDraft = useV2DraftDocument(workspaceID); const draft = managedDraft?.state.snapshot.text ?? legacyDraft; const onDraftChange = managedDraft?.changeText ?? legacyDraftChange; const [applyingApproval, setApplyingApproval] = useState(false); const creationRecovery = useV2CreationRecovery(client, workspaceID, (thread, originalDraft, files, input, shouldOpen) => { registerV2RecoveredTurn(queryClient, input); bindDraftSharedBrowser(workspaceID, thread.id); onCreated(thread, originalDraft, files, input.images, input.draftVersion, input.attachments, shouldOpen && activeRef.current && workspaceRef.current === input.workspaceID, input.mcpReferences); }, applyingApproval); const { networkMode, allowedTargets, modelRoute } = options; const [modelCatalogError, setModelCatalogError] = useState(""); const [approvalChoice, setApprovalChoice] = useState<{ workspaceID: string; mode: ExecutionApprovalMode; confirmed: boolean } | null>(null); const approval = approvalChoice?.workspaceID === workspaceID ? approvalChoice : null; const create = async (content: string, files: V2FileReference[] = [], images: WorkspaceImageAttachment[] = [], draftVersion?: V2DraftVersion, attachments: WorkspaceFileAttachment[] = [], mcpReferences: MCPDraftReference[] = []) => { if (!webAccessValid) throw new Error(t("请在设置的“配置”页修正网页限制,草稿已保留。", "Correct web restrictions in Settings > Configuration. Your draft has been kept.")); if (!configurationValid) throw new Error(t("任务配置尚未通过核对,请打开任务配置修正预算或重新读取项目设置。草稿已保留。", "Task configuration needs review. Open task configuration to fix the budget or reload project settings. Your draft has been kept.")); if (projectSkills?.refs.length && !projectSkills.confirmUntrustedContext) throw new Error(t("请在任务配置中确认所选技能上下文。草稿已保留。", "Confirm the selected skill context in task configuration. Your draft has been kept.")); normalizedTaskBudget(budget); if (phase === "plan" && !client.hasPlanDelivery) throw new Error(t("当前连接未启用计划确认。请检查连接,或选择直接执行。", "This connection does not support plan confirmation. Check the connection or choose direct execution.")); if (approval?.mode === "full" && !approval.confirmed) throw new Error(t("请先确认完全访问权限,草稿已保留。", "Confirm Full access first. Your draft has been kept.")); const selectedApproval = approval; const selectedApprovalMode = client.hasExecutionPermissionControl ? selectedApproval?.mode ?? "ask" : undefined; const submittedDraft = draft; setModelCatalogError(""); const creationWorkspaceID = workspaceID; const browserGeneration = useConnectionStore.getState().connectionGeneration; const creationRequest = ++creationRequestRef.current; // Leaving this page, changing projects, or starting another request ends // this request's right to navigate, but not its original input handoff. const requestIsCurrent = () => activeRef.current && workspaceRef.current === creationWorkspaceID && creationRequestRef.current === creationRequest; setApplyingApproval(true); try { try { const catalog = await client.availableModelRoutes(); if (!requestIsCurrent()) return v2ComposerNotSubmitted; const selectable = catalog.routes.filter((route) => route.selectable); const selectedReady = !modelRoute || selectable.some((route) => route.provider_id === modelRoute.provider && route.model === modelRoute.model); if (!selectable.length || !selectedReady) { onManageModels(true); return v2ComposerNotSubmitted; } } catch (error) { if (!requestIsCurrent()) return v2ComposerNotSubmitted; setModelCatalogError(`${t("无法检查可用模型,草稿已保留。", "Could not check available models. Your draft has been kept.")}${error instanceof Error && error.message ? ` ${error.message}` : t(" 请检查连接后重试。", " Check the connection and try again.")}`); return v2ComposerNotSubmitted; } let goal = content || "文件对话"; if (!content && images.length) goal = "图片对话"; else if (!content && mcpReferences.length) goal = "MCP 内容"; const request = { version: "thread_creation.v1", workspace_id: workspaceID, goal, profile: "code", surface: "code", phase, network_mode: "disabled", web_access_mode: networkMode, web_allowed_targets: networkMode === "rules" ? allowedTargets : [], ...(budget ? { budget } : {}), ...(modelRoute ? { provider: modelRoute.provider, model: modelRoute.model } : {}), } as Parameters[0] & { provider?: string; model?: string }; try { // Confirmation belongs to this attempt, never to a restored draft. if (selectedApproval?.mode === "full") setApprovalChoice((current) => current === selectedApproval ? { ...current, confirmed: false } : current); if (recovery) { if (managedDraft) { if (draftVersion) assertV2DraftVersion(managedDraft, draftVersion, { text: submittedDraft, files, images, attachments, mcpReferences }); else draftVersion = requireV2DraftVersion(managedDraft, { text: submittedDraft, files, images, attachments, mcpReferences }); } const intent = creationRecovery.prepare(request, content, files, submittedDraft, images, draftVersion, attachments, projectSkills, mcpReferences, selectedApprovalMode); const thread = await creationRecovery.resolve(intent, selectedApproval?.mode === "full" && selectedApproval.confirmed); await adoptDraftSharedBrowser(creationWorkspaceID, thread.id, thread.last_run_id, browserGeneration); const input = creationRecovery.handoff(intent, thread); const submission = turn.mutateAsync(input); onCreated(thread, submittedDraft, files, images, input.draftVersion, attachments, requestIsCurrent(), mcpReferences); void queryClient.invalidateQueries({ queryKey: v2QueryKeys.threads("active") }); try { await submission; } catch (error) { if (v2TurnFailed(error) && !input.draftVersion) onTurnSuccess(thread.id, submittedDraft); throw error; } if (!input.draftVersion) onTurnSuccess(thread.id, submittedDraft); } else { const fingerprint = JSON.stringify([request, files, images, fileAttachmentIdentities(attachments), projectSkills, mcpReferenceIdentities(mcpReferences)]); const operationID = creationAttemptRef.current.get(fingerprint) ?? globalThis.crypto.randomUUID(); creationAttemptRef.current.set(fingerprint, operationID); const result = await client.createThread(request, `v2-thread-create-${operationID}`); if (projectSkills?.refs.length) { try { await submitSkillChoice(client, result.thread.last_run_id, projectSkills, operationID); } catch (error) { void queryClient.invalidateQueries({ queryKey: v2QueryKeys.threads("active") }); throw new Error(`${t(`对话 ${result.thread.id} 已保存,技能选择失败,尚未发送首条消息。`, `Conversation ${result.thread.id} was saved, but skill selection failed and the first message has not been sent.`)}${error instanceof Error ? error.message : t("请重试。", "Please try again.")}`); } } await applyCreationApproval(client, result.thread.id, selectedApprovalMode, selectedApproval?.mode === "full" && selectedApproval.confirmed); await adoptDraftSharedBrowser(creationWorkspaceID, result.thread.id, result.thread.last_run_id, browserGeneration); const submission = turn.mutateAsync({ threadID: result.thread.id, workspaceID, content, ...(files.length ? { files } : {}), ...(images.length ? { images } : {}), ...(attachments.length ? { attachments } : {}), ...(mcpReferences.length ? { mcpReferences } : {}), operationKey: `v2-thread-create-turn-${operationID}`, createdAt: new Date().toISOString() }); onCreated(result.thread, submittedDraft, files, images, undefined, attachments, requestIsCurrent(), mcpReferences); creationAttemptRef.current.delete(fingerprint); void queryClient.invalidateQueries({ queryKey: v2QueryKeys.threads("active") }); try { await submission; } catch (error) { if (v2TurnFailed(error)) onTurnSuccess(result.thread.id, submittedDraft); throw error; } onTurnSuccess(result.thread.id, submittedDraft); } } catch (error) { // The original key/journal and Thread mutation keep this failure. It // must not replace a newer request's error after returning to a project. if (requestIsCurrent()) throw error; return v2ComposerNotSubmitted; } if (!requestIsCurrent()) return v2ComposerNotSubmitted; } finally { setApplyingApproval(false); } }; return ; } export function V2Workbench({ client }: { client: APIClient }) { const scopeID = useConnectionStore((state) => state.health?.data_store_id); const parentQueries = useQueryClient(); const queries = useMemo(() => scopeID ? new QueryClient({ defaultOptions: parentQueries.getDefaultOptions() }) : parentQueries, [scopeID, client.baseURL, parentQueries]); return ; } function V2WorkbenchContent({ client }: { client: APIClient }) { const { t } = useLocale(); useV2RestoreTurns(); const recovery = useV2RecoveryStore(); const scopeID = useConnectionStore((state) => state.health?.data_store_id); const unread = useV2ThreadUnread(client, scopeID); const [readRequest, setReadRequest] = useState({ threadID: "", revision: 0 }); useEffect(() => { document.documentElement.dataset.prayuDensity = readDensity(); }, []); const queryClient = useQueryClient(); const previousThreadID = useConnectionStore((state) => state.selectedThreadID); const selectThread = useConnectionStore((state) => state.selectThread); const navigation = useV2Navigation(); const { route, navigate } = navigation; const routeRef = useRef(route); routeRef.current = route; const selectedThreadID = route.threadID ?? ""; const reviewMemoryRef = useRef({ generation: 0 }); const [reviewEntry, setReviewEntry] = useState<{ threadID: string; runID: string; requestID: string }>(); useEffect(() => { reviewMemoryRef.current = { generation: reviewMemoryRef.current.generation + 1 }; setReviewEntry(undefined); }, [client, selectedThreadID]); useEffect(() => { if (route.kind === "run" && reviewMemoryRef.current.runID && reviewMemoryRef.current.runID !== route.resourceID) reviewMemoryRef.current = { generation: reviewMemoryRef.current.generation + 1 }; }, [route.kind, route.resourceID]); const surface = route.section ? "settings" : route.kind === "thread" || route.kind === "new" ? "conversation" : "resource"; const settingsSection = route.section ?? "general"; const newConversation = !selectedThreadID; const [narrowLayout, setNarrowLayout] = useState(() => Boolean(window.matchMedia?.("(max-width: 720px)").matches)); const [desktopSidebarVisible, setDesktopSidebarVisible] = useState(true); const [narrowSidebarVisible, setNarrowSidebarVisible] = useState(false); const sidebarVisible = narrowLayout ? narrowSidebarVisible : desktopSidebarVisible; const sidebarSlot = useRef(null); const sidebarToggle = useRef(null); const setSidebarVisible = (visible: boolean) => { if (!visible) sidebarToggle.current?.focus(); if (narrowLayout) setNarrowSidebarVisible(visible); else setDesktopSidebarVisible(visible); }; useEffect(() => { const media = window.matchMedia?.("(max-width: 720px)"); if (!media) return; const onChange = (event: MediaQueryListEvent) => { if (sidebarSlot.current?.contains(document.activeElement)) sidebarToggle.current?.focus(); setNarrowLayout(event.matches); setNarrowSidebarVisible(false); }; if (typeof media.addEventListener === "function") { media.addEventListener("change", onChange); return () => media.removeEventListener("change", onChange); } media.addListener?.(onChange); return () => media.removeListener?.(onChange); }, []); const [searchOpen, setSearchOpen] = useState(false); const visibleChatOrder = useRef([]); const [searchFocusRequest, setSearchFocusRequest] = useState(0); useEffect(() => { if (!searchFocusRequest || !searchOpen || !sidebarVisible) return; // The drawer's visibility transition starts after the DOM commit. let frame = requestAnimationFrame(() => { frame = requestAnimationFrame(() => { const input = sidebarSlot.current?.querySelector(".v2-sidebar-search input"); input?.focus(); input?.select(); }); }); return () => cancelAnimationFrame(frame); }, [searchFocusRequest, searchOpen, sidebarVisible]); const [threadSearch, setThreadSearch] = useState(""); const [appliedThreadSearch, setAppliedThreadSearch] = useState(""); const [includeArchivedSearch, setIncludeArchivedSearch] = useState(false); const [searchCurrentThread, setSearchCurrentThread] = useState(false); const searchThreadID = searchCurrentThread ? selectedThreadID : ""; const [searchComposing, setSearchComposing] = useState(false); const normalizedThreadSearch = threadSearch.trim(); const searchTooLong = Array.from(normalizedThreadSearch).length > 256; const searchPending = searchComposing || normalizedThreadSearch !== appliedThreadSearch; useEffect(() => { if (searchComposing || searchTooLong) return; if (!normalizedThreadSearch) { setAppliedThreadSearch(""); return; } const timer = window.setTimeout(() => setAppliedThreadSearch(normalizedThreadSearch), 250); return () => window.clearTimeout(timer); }, [normalizedThreadSearch, searchComposing, searchTooLong]); const [storedWorkspace, setWorkspaceID] = useV2PersistentState("workspace", ""); const projectID = typeof storedWorkspace === "string" ? storedWorkspace : ""; const localWorkspace = useProjectlessWorkspace(client, route.kind === "new" && !route.section && !projectID); const workspaceID = projectID || (client.hasProjectlessWorkspaces ? localWorkspace.workspaceID : ""); // The new conversation mounts this exact draft's image discard controls. const persistenceWarning = useV2PersistenceWarning(route.kind === "new" && !route.section && workspaceID ? `image-upload:${JSON.stringify([workspaceID, ""])}:` : undefined); const [savedPhase, setSavedPhase] = useV2PersistentState(`new-thread-phase:${workspaceID}`, "deliver"); const phase: V2WorkPhase = savedPhase === "plan" ? "plan" : "deliver"; const draftTaskConfiguration = useV2DraftTaskConfiguration(workspaceID); // An explicitly imported existing project may be outside the first list page. // Retain its pathless receipt so selection survives list refetches. const [importedWorkspaces, setImportedWorkspaces] = useState([]); const [editingProject, setEditingProject] = useState(null); const [backgroundCreations, setBackgroundCreations] = useState([]); const [drafts, setDrafts] = useV2Drafts(); useEffect(() => { if (projectID || !localWorkspace.workspaceID || !drafts["new:"]) return; const text = drafts["new:"]; if (recovery) { const document = getV2DraftDocument(recovery); const scope = v2DraftScope(localWorkspace.workspaceID); const target = readV2Draft(document, recovery, scope); if (target.ref || target.snapshot.text || target.error || target.conflict) return; const copied = document.update(scope, { text }, null); if (!copied.persisted || copied.error) return; } setDrafts((current) => ({ ...current, [`new:${localWorkspace.workspaceID}`]: current[`new:${localWorkspace.workspaceID}`] ?? text, "new:": current["new:"] === text ? "" : current["new:"] })); }, [projectID, localWorkspace.workspaceID, drafts, recovery, setDrafts]); const creationAttemptRef = useRef(new Map()); const webAccessDefaults = useV2WebAccessDefaults(); const [newThreadModelRoute, setNewThreadModelRoute] = useState(null); const newThreadOptions: NewThreadOptions = { networkMode: webAccessDefaults.mode, allowedTargets: webAccessDefaults.targets, modelRoute: newThreadModelRoute }; const setNewThreadOptions = (next: NewThreadOptions) => { if (next.networkMode !== newThreadOptions.networkMode || next.allowedTargets !== newThreadOptions.allowedTargets) { webAccessDefaults.onChange(next.networkMode, next.allowedTargets); } setNewThreadModelRoute(next.modelRoute); }; const [modelSetupToken, setModelSetupToken] = useState(""); const modelSetupContextRef = useRef<{ token: string; workspaceID: string; entered: boolean } | null>(null); const updateModelSetupToken = (value: string, setupWorkspaceID = "") => { modelSetupContextRef.current = value ? { token: value, workspaceID: setupWorkspaceID, entered: false } : null; setModelSetupToken(value); }; const draftKey = newConversation || !selectedThreadID ? `new:${workspaceID}` : `thread:${selectedThreadID}`; const updateDraft = (content: string, expected?: string) => setDrafts((current) => expected !== undefined && (current[draftKey] ?? "") !== expected ? current : { ...current, [draftKey]: content }); const selectWorkspace = (id: string) => { if (!workspaceID) setDrafts((current) => ({ ...current, [`new:${id}`]: current[`new:${id}`] ?? current["new:"] ?? "" })); setWorkspaceID(id); }; const [archiveCandidate, setArchiveCandidate] = useState(null); const [executionFilter, setExecutionFilter] = useState<"" | "running" | "waiting_approval">(""); const workspacesQuery = useInfiniteQuery({ queryKey: v2QueryKeys.workspaces, queryFn: ({ signal, pageParam }) => client.getPage("/workspaces", { limit: 100 }, pageParam, signal), initialPageParam: "", getNextPageParam: (last) => last.page.next_cursor || undefined, staleTime: 30_000, }); const threadsQuery = useInfiniteQuery({ queryKey: executionFilter ? [...v2QueryKeys.threadSearch("active", ""), { execution_state: executionFilter }] : v2QueryKeys.threadSearch("active", ""), queryFn: ({ signal, pageParam }) => client.getPage("/threads", { limit: 100, status: "active", ...(executionFilter ? { execution_state: executionFilter } : {}) }, pageParam, signal), initialPageParam: "", getNextPageParam: (last) => last.page.next_cursor || undefined, enabled: !appliedThreadSearch && !searchPending && !searchTooLong, // Infinite queries refetch every cached page. Poll only a single page; // history still refreshes explicitly and through the existing invalidations. refetchInterval: (query) => !appliedThreadSearch && sidebarVisible && surface === "conversation" && !searchPending && (query.state.data?.pages.length ?? 1) === 1 ? 15_000 : false, }); const messageSearchQuery = useInfiniteQuery({ queryKey: ["v2", "threads", "active", "messages", { q: appliedThreadSearch, include_archived: includeArchivedSearch, thread_id: searchThreadID }], queryFn: ({ signal, pageParam }) => client.getPage("/threads/search", { q: appliedThreadSearch, include_archived: includeArchivedSearch, limit: 50, ...(searchThreadID ? { thread_id: searchThreadID } : {}) }, pageParam, signal), initialPageParam: "", getNextPageParam: (last) => last.page.next_cursor || undefined, enabled: Boolean(appliedThreadSearch) && !searchPending && !searchTooLong, refetchInterval: (query) => sidebarVisible && surface === "conversation" && !searchPending && (query.state.data?.pages.length ?? 1) === 1 ? 15_000 : false, }); const sidebarQuery = appliedThreadSearch ? messageSearchQuery : threadsQuery; const searchResults = useMemo(() => [...new Map(messageSearchQuery.data?.pages.flatMap(({ items }) => items) .map((item) => [item.id, item])).values()], [messageSearchQuery.data]); const workspaces = useMemo(() => [...new Map([...importedWorkspaces, ...(workspacesQuery.data?.pages.flatMap(({ items }) => items) ?? [])] .map((workspace) => [workspace.id, workspace])).values()], [workspacesQuery.data, importedWorkspaces]); const threads = useMemo(() => [...new Map(threadsQuery.data?.pages.flatMap(({ items }) => items).map((thread) => [thread.id, thread])).values()], [threadsQuery.data]); useEffect(() => { if (!client.hasProjectlessWorkspaces && !workspaceID && workspaces[0]) { const id = workspaces[0].id; setDrafts((current) => ({ ...current, [`new:${id}`]: current[`new:${id}`] ?? current["new:"] ?? "" })); setWorkspaceID(id); } }, [client.hasProjectlessWorkspaces, workspaceID, workspaces]); useEffect(() => { if (route.kind !== "initial" || !threadsQuery.isSuccess) return; const saved = recovery?.read("route", ""); if (typeof saved === "string" && saved) { const restored = readV2Route(saved); if (restored.kind !== "initial" && restored.kind !== "invalid") { navigate(restored, true); return; } } const id = previousThreadID || threads[0]?.id; navigate(id ? { kind: "thread", threadID: id } : { kind: "new" }, true); }, [route.kind, previousThreadID, threads, threadsQuery.isSuccess, navigate, recovery]); useEffect(() => { const setup = modelSetupContextRef.current; if (!setup) return; if (route.kind === "new" && route.section === "models" && workspaceID === setup.workspaceID) { setup.entered = true; return; } if (setup.entered) updateModelSetupToken(""); }, [route, workspaceID]); useEffect(() => { if (route.kind === "initial" || route.kind === "invalid") return; try { recovery?.write("route", window.location.hash); } catch { /* Visible save notice. */ } }, [route, recovery]); useEffect(() => { if (route.kind !== "initial") selectThread(selectedThreadID); }, [route.kind, selectedThreadID, selectThread]); const archiveMutation = useMutation({ mutationFn: (thread: ThreadView) => client.transitionThread(thread.id, "archive", { version: "thread_lifecycle.v1", expected_version: thread.version, }, `v2-thread-archive-${globalThis.crypto.randomUUID()}`), onSuccess: (result) => { const fallback = threads.find(({ id }) => id !== result.thread.id); if (selectedThreadID === result.thread.id) { navigate(fallback ? { kind: "thread", threadID: fallback.id } : { kind: "new" }, true); } setArchiveCandidate(null); void queryClient.invalidateQueries({ queryKey: v2QueryKeys.threads("active") }); void queryClient.invalidateQueries({ queryKey: v2QueryKeys.threads("archived") }); }, }); const projectMoveKeys = useRef(new Map()); const moveMutation = useMutation({ mutationFn: async ({ thread, destination }: { thread: ThreadView; destination: string }) => { const changingWorkspace = Boolean(destination && destination !== thread.workspace_id); const sourceScope = v2DraftScope(thread.workspace_id ?? "", thread.id); const targetScope = v2DraftScope(destination || sourceScope.workspaceID, thread.id); const document = recovery ? getV2DraftDocument(recovery) : null; const source = document && recovery ? readV2Draft(document, recovery, sourceScope) : null; const target = document ? document.read(targetScope) : null; if (recovery) { recovery.assertReadable("turn:"); if (recovery.entries("turn:").some(([, input]) => validRecoveryTurn(input) && input.threadID === thread.id)) throw new Error(t("消息还在确认中,请等待发送结果后移动。", "Wait for the pending message to be confirmed before moving.")); } if (changingWorkspace) { if (queryClient.getQueryData<{ pending: () => boolean }>(v2AttachmentImportKey(sourceScope.workspaceID, thread.id))?.pending() || recovery?.entries(`file-upload:${JSON.stringify([sourceScope.workspaceID, thread.id])}:`).length || recovery?.entries(`native-clipboard:${JSON.stringify([sourceScope.workspaceID, thread.id])}:`).length) throw new Error(t("此聊天的附件还在保存或待核对,请完成后再移动。", "Finish saving or checking this chat's attachments before moving.")); if (source?.error || source?.conflict || target?.error || target?.conflict) throw new Error(t("请先处理此对话的草稿冲突,再移动项目。", "Resolve this conversation's draft conflict before moving.")); const references = source ? [source.snapshot.files, source.snapshot.images, source.snapshot.attachments ?? [], source.snapshot.mcpReferences ?? []] : [ queryClient.getQueryData(v2FileReferenceKey(sourceScope.workspaceID, thread.id)) ?? [], queryClient.getQueryData(v2ImageReferenceKey(sourceScope.workspaceID, thread.id)) ?? [], queryClient.getQueryData(v2AttachmentReferenceKey(sourceScope.workspaceID, thread.id)) ?? [], queryClient.getQueryData(v2MCPReferenceKey(sourceScope.workspaceID, thread.id)) ?? [], ]; if (references.some((items) => items.length)) throw new Error(t("草稿中还有原项目的附件或引用,请先发送或移除后移动。", "Send or remove the draft's attachments and references before moving.")); if (target && (target.snapshot.text || target.snapshot.files.length || target.snapshot.images.length || target.snapshot.attachments?.length || target.snapshot.mcpReferences?.length)) throw new Error(t("此对话在目标目录还有未发送草稿,请先处理后再移动。", "This chat has an unsent draft in the destination folder. Resolve it before moving.")); } const fingerprint = JSON.stringify([thread.id, destination, thread.project_assignment_version]); const key = projectMoveKeys.current.get(fingerprint) ?? `v2-project-move-${crypto.randomUUID()}`; projectMoveKeys.current.set(fingerprint, key); const result = await client.moveThreadToProject(thread.id, destination, thread.project_assignment_version!, key); if (changingWorkspace && document && recovery) { const latest = readV2Draft(document, recovery, sourceScope); if (!latest.error) { const copied = document.update(targetScope, { text: latest.snapshot.text }, target?.ref ?? null); if (copied.persisted && !copied.error && !copied.conflict && !latest.conflict && latest.ref) document.update(sourceScope, { text: "" }, latest.ref); } } projectMoveKeys.current.delete(fingerprint); return result; }, onSuccess: ({ thread }) => { queryClient.setQueryData(v2QueryKeys.thread(thread.id), (current) => current ? { ...current, thread } : current); void queryClient.invalidateQueries({ queryKey: v2QueryKeys.thread(thread.id) }); void queryClient.invalidateQueries({ queryKey: v2QueryKeys.threads("active") }); }, onError: () => { void queryClient.invalidateQueries({ queryKey: v2QueryKeys.threads("active") }); }, }); const closeNavigationSidebar = () => { if (window.matchMedia?.("(max-width: 720px)").matches) setSidebarVisible(false); }; const openConversation = (threadID: string, onAccepted?: () => void) => { setBackgroundCreations((current) => current.filter((thread) => thread.id !== threadID)); updateModelSetupToken(""); navigate({ kind: "thread", threadID }, false, onAccepted); closeNavigationSidebar(); }; const openSidebarConversation = (threadID: string) => openConversation(threadID, () => { unread.open(threadID); setReadRequest((current) => ({ threadID, revision: current.revision + 1 })); }); const openAdjacentChat = (direction: -1 | 1) => { if (!selectedThreadID || surface !== "conversation" || searchOpen) return false; const index = visibleChatOrder.current.indexOf(selectedThreadID); const next = index < 0 ? undefined : visibleChatOrder.current[index + direction]; if (next) openSidebarConversation(next); // At an edge, consume the shortcut without wrapping or leaving this chat. }; const startNew = (destination?: string) => { updateModelSetupToken(""); const currentThread = queryClient.getQueryData( v2QueryKeys.thread(selectedThreadID))?.thread ?? threads.find(({ id }) => id === selectedThreadID); selectWorkspace(destination ?? (client.hasProjectlessWorkspaces ? "" : currentThread ? threadProjectID(currentThread) : projectID)); navigate({ kind: "new" }); closeNavigationSidebar(); }; const setSettingsSection = (section: V2SettingsSection) => { if (section !== "models" && section !== "advanced-models") updateModelSetupToken(""); const source = route.kind === "initial" || route.kind === "invalid" ? { kind: "new" as const } : route; navigate({ ...source, section }, Boolean(route.section)); closeNavigationSidebar(); }; const goBack = () => { updateModelSetupToken(""); navigation.back(); closeNavigationSidebar(); }; const returnFromSettings = () => { updateModelSetupToken(""); navigation.back(); closeNavigationSidebar(); }; const openTask = () => { updateModelSetupToken(""); navigate(selectedThreadID ? { kind: "thread", threadID: selectedThreadID } : { kind: "new" }); closeNavigationSidebar(); }; const openSettings = () => setSettingsSection("general"); const openModels = (prepareForDraft = false) => { updateModelSetupToken(prepareForDraft && route.kind === "new" ? globalThis.crypto.randomUUID() : "", workspaceID); setSettingsSection("models"); }; const completeModelSetup = (token: string, definition: Pick) => { const setup = modelSetupContextRef.current; const currentRoute = routeRef.current; if (!token || token !== setup?.token || !setup.entered || workspaceID !== setup.workspaceID || currentRoute.section !== "models" || currentRoute.kind !== "new") return; setNewThreadModelRoute({ provider: definition.id, model: definition.default_model }); updateModelSetupToken(""); navigate({ ...currentRoute, section: undefined }, true); requestAnimationFrame(() => requestAnimationFrame(() => document.querySelector('.v2-new-conversation .v2-composer :is(textarea, [contenteditable="true"][role="textbox"])')?.focus())); }; const openHistory = () => { updateModelSetupToken(""); navigate({ kind: "history" }); closeNavigationSidebar(); }; const openTool = (tool: "run" | "session" | "schedule", resourceID?: string, pane?: V2RunPane) => { if (tool === "run" && reviewMemoryRef.current.runID !== resourceID) reviewMemoryRef.current = { generation: reviewMemoryRef.current.generation + 1 }; navigate({ kind: tool === "schedule" ? "automations" : tool, ...(selectedThreadID ? { threadID: selectedThreadID } : {}), ...(resourceID ? { resourceID } : {}), ...(tool === "run" && pane ? { pane } : {}), }); }; const openCreatedRun = (runID: string) => { // The mutation retains the callback from its source route. A later source // selection must not be replaced by the completion of that older request. if (routeRef.current !== route) return; // Fork/Resume returns a Run, not a Thread association. Open that exact // record without carrying the old Thread's permissions into its scope. navigate({ kind: "run", resourceID: runID }); }; const openGithubReview = (runID: string) => { if (!selectedThreadID || routeRef.current !== route) return; setReviewEntry({ threadID: selectedThreadID, runID, requestID: globalThis.crypto.randomUUID() }); navigate({ kind: "thread", threadID: selectedThreadID }); closeNavigationSidebar(); }; const openSearch = (currentThread: boolean) => { if (currentThread && (!selectedThreadID || surface !== "conversation")) return false; if (surface === "settings") returnFromSettings(); setSearchCurrentThread(currentThread); setSidebarVisible(true); setSearchOpen(true); setSearchFocusRequest((request) => request + 1); }; useChatShortcuts({ newChat: () => startNew(), search: () => openSearch(false), findInChat: () => openSearch(true), previousChat: () => openAdjacentChat(-1), nextChat: () => openAdjacentChat(1), settings: openSettings, sidebar: () => setSidebarVisible(!sidebarVisible), composer: () => { if (surface === "settings") returnFromSettings(); requestAnimationFrame(() => document.querySelector('.v2-composer :is(textarea, [contenteditable="true"][role="textbox"])')?.focus()); }, }); return
setSidebarVisible(!sidebarVisible)} sidebarVisible={sidebarVisible} sidebarToggleRef={sidebarToggle} />
: surface === "settings" ? { if (!selectedThreadID && workspaceID) selectWorkspace(workspaceID); openTask(); }} section={settingsSection} threadID={selectedThreadID} workspaces={workspaces} defaultWebAccessMode={webAccessDefaults.mode} defaultWebAccessTargets={webAccessDefaults.targets} onDefaultWebAccessChange={webAccessDefaults.onChange} sourceRunID={route.kind === "run" ? route.resourceID : reviewMemoryRef.current.client === client && reviewMemoryRef.current.threadID === selectedThreadID ? reviewMemoryRef.current.runID : undefined} onOpenGithubReview={openGithubReview} draftWorkspaceID={workspaceID} draftPhase={phase} draftBudget={draftTaskConfiguration.budget} onDraftBudgetChange={draftTaskConfiguration.onBudgetChange} onDraftValidityChange={draftTaskConfiguration.onValidityChange} draftProjectSkills={draftTaskConfiguration.projectSkills} onDraftProjectSkillsChange={draftTaskConfiguration.onProjectSkillsChange} prepareModelForDraft={Boolean(modelSetupToken)} modelSetupToken={modelSetupToken} onModelReady={completeModelSetup} /> : route.kind === "run" ? : route.kind === "session" ? : route.kind === "automations" ? : route.kind === "history" ? navigate({ kind, resourceID })} /> : newConversation || !selectedThreadID ? {localWorkspace.pending &&

{t("正在准备聊天工作目录…", "Preparing the chat folder…")}

} {localWorkspace.error &&
{localWorkspace.error}
} } phase={phase} onPhaseChange={setSavedPhase} creationAttemptRef={creationAttemptRef} options={newThreadOptions} onOptionsChange={setNewThreadOptions} webAccessValid={webAccessDefaults.valid} budget={draftTaskConfiguration.budget} configurationValid={draftTaskConfiguration.valid} projectSkills={draftTaskConfiguration.projectSkills} onOpenTaskConfiguration={() => setSettingsSection("task-configuration")} onOpenWebSettings={() => setSettingsSection("configuration")} onCreated={(thread, submittedDraft, files, images = [], version, attachments = [], shouldOpen = true, mcpReferences = []) => { const source = `new:${thread.workspace_id}`; const target = `thread:${thread.id}`; if (thread.project_workspace_id === "") { // Preserve edits made while the first message was being created. if (recovery && version) { const document = getV2DraftDocument(recovery); const original = readV2Draft(document, recovery, version.scope); if (original.ref?.branchID !== version.ref.branchID || original.ref?.seq !== version.ref.seq) { const targetScope = v2DraftScope(thread.workspace_id ?? "", thread.id); const targetDraft = readV2Draft(document, recovery, targetScope); const occupied = targetDraft.snapshot.text || targetDraft.snapshot.files.length || targetDraft.snapshot.images.length || targetDraft.snapshot.attachments?.length || targetDraft.snapshot.mcpReferences?.length; document.update(targetScope, original.snapshot, occupied ? null : targetDraft.ref); } } localWorkspace.consume(thread.workspace_id ?? ""); } // Move the submitted version into the durable Thread's draft. // Anything typed or selected during creation stays in its project. if (!version) { queryClient.setQueryData(v2MCPReferenceKey(thread.workspace_id ?? "", thread.id), (current) => [...(current ?? []), ...mcpReferences.filter((item) => !current?.some(({ id }) => id === item.id))]); queryClient.setQueryData(v2MCPReferenceKey(thread.workspace_id ?? "", ""), (current) => current?.filter(({ id }) => !mcpReferences.some((item) => item.id === id))); setDrafts((current) => ({ ...current, [source]: current[source] === submittedDraft ? "" : current[source] ?? "", [target]: current[target] ?? submittedDraft })); queryClient.setQueryData(v2FileReferenceKey(thread.workspace_id ?? "", thread.id), (current) => [...(current ?? []), ...files.filter((file) => !current?.some(({ path }) => path === file.path))]); queryClient.setQueryData(v2FileReferenceKey(thread.workspace_id ?? "", ""), (current) => current?.filter(({ id }) => !files.some((file) => file.id === id))); queryClient.setQueryData(v2ImageReferenceKey(thread.workspace_id ?? "", thread.id), (current) => [...(current ?? []), ...images.filter((image) => !current?.some(({ id }) => id === image.id))]); queryClient.setQueryData(v2ImageReferenceKey(thread.workspace_id ?? "", ""), (current) => current?.filter(({ id }) => !images.some((image) => image.id === id))); queryClient.setQueryData(v2AttachmentReferenceKey(thread.workspace_id ?? "", thread.id), (current) => [...(current ?? []), ...attachments.filter((file) => !current?.some(({ id }) => id === file.id))]); queryClient.setQueryData(v2AttachmentReferenceKey(thread.workspace_id ?? "", ""), (current) => current?.filter(({ id }) => !attachments.some((file) => file.id === id))); } // The request can outlive its original new-task page. Hand off // its input above, but let the user keep their current task. if (shouldOpen) openConversation(thread.id); else setBackgroundCreations((current) => [...current.filter((item) => item.id !== thread.id), thread]); void queryClient.invalidateQueries({ queryKey: v2QueryKeys.threads("active") }); }} onTurnSuccess={(threadID, submittedDraft) => setDrafts((current) => current[`thread:${threadID}`] === submittedDraft ? { ...current, [`thread:${threadID}`]: "" } : current)} onManageModels={openModels} onWorkspaceChange={selectWorkspace} onImported={(workspace, currentDraft) => { setImportedWorkspaces((current) => [...current.filter(({ id }) => id !== workspace.id), workspace]); if (recovery) { const document = getV2DraftDocument(recovery); const scope = v2DraftScope(workspace.id); const target = readV2Draft(document, recovery, scope); if (!target.ref && !target.snapshot.text && !target.snapshot.files.length && !target.snapshot.images.length && !target.snapshot.attachments?.length) document.update(scope, { text: currentDraft ?? "" }, null); } else setDrafts((current) => ({ ...current, [`new:${workspace.id}`]: current[`new:${workspace.id}`] ?? currentDraft ?? current[draftKey] ?? "" })); selectWorkspace(workspace.id); }} moreProjects={workspacesQuery.hasNextPage && } workspaceID={workspaceID} workspaces={workspaces} /> : { if (routeRef.current === route) openConversation(thread.id); else setBackgroundCreations((current) => [...current.filter(({ id }) => id !== thread.id), thread]); }} onOpenWorktree={(workspace) => { setImportedWorkspaces((current) => [...current.filter(({ id }) => id !== workspace.id), workspace]); selectWorkspace(workspace.id); setNewThreadModelRoute(null); navigate({ kind: "new" }); closeNavigationSidebar(); }} recordsRequested={route.panel === "records"} onOpenTool={openTool} reviewMemoryRef={reviewMemoryRef} reviewEntry={reviewEntry} onReviewEntryHandled={(requestID) => setReviewEntry((current) => current?.requestID === requestID ? undefined : current)} draft={drafts[draftKey] ?? ""} onDraftChange={updateDraft} onManageModels={() => openModels(false)} messageID={route.messageID} onExitMessage={() => navigate({ kind: "thread", threadID: selectedThreadID })} threadID={selectedThreadID} workspaces={workspaces} />} {backgroundCreations.length > 0 &&
{backgroundCreations.map((thread) =>
{t("对话已创建:", "Conversation created: ")}{thread.title}
)}
} {(workspacesQuery.isError || threadsQuery.isError) &&
{t("项目或对话列表加载失败,已有内容会保留。", "Projects or conversations could not be loaded. Existing content has been kept.")}
} {persistenceWarning &&
{persistenceWarning}
} {unread.error &&
{t("未读回复暂未同步,已有标记会保留。", "Unread replies could not be refreshed. Existing markers have been kept.")}
}