All files / src/components/session SessionPage.tsx

64.55% Statements 377/584
74.86% Branches 137/183
30.76% Functions 8/26
64.55% Lines 377/584

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 7281x                                                               1x 14x 14x       14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x   14x 2x 12x 12x     14x 49x 7x 7x 42x 42x 42x     14x 14x   14x 49x   14x 14x 14x 14x 14x   14x 3x 14x   14x 14x 14x 14x 14x   14x 14x 14x 14x 14x 14x   14x 14x 14x 14x 14x 14x   14x 14x 14x 14x 14x   14x 14x 14x                                 14x   14x 14x             2x 2x 2x 14x 2x 2x 2x 2x 14x   14x 14x 14x   14x 14x             14x                       14x   14x 14x             14x           14x   14x 14x 14x 14x 14x 14x 14x 14x 3x             3x                           14x 14x 14x 14x 14x 14x 14x   14x 14x 14x   14x       14x 14x 14x 14x 14x 14x 14x 14x   14x 5x 2x 2x 2x 2x   5x             14x 14x 14x 14x 14x 14x 14x 14x   14x 14x   14x 14x   14x 14x   14x 14x 14x 14x 14x 14x 14x 14x 14x   14x 5x   14x   14x 5x 2x 2x 14x   14x 6x 1x 1x 14x   14x 5x 5x     14x   14x   14x 6x 2x 6x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x   14x   14x 14x 14x 14x 14x 14x 14x 14x 14x   8x     6x 14x 14x 11x 8x 8x 8x 8x 14x   14x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 1x   14x                                           14x           14x                           14x 14x 14x 14x 14x 14x 14x   14x   14x 14x 14x 14x 14x 14x 14x 14x 14x 14x       14x       14x 14x       14x 14x 14x 14x 14x 14x 14x 14x 14x 14x         14x 14x 14x 14x   14x 2x 2x 14x 2x 2x 2x 2x 2x             2x 2x 2x 2x 2x     2x       14x 2x 2x 14x     14x     14x     14x     14x       14x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x         1x 14x 14x 14x 14x 14x 14x   14x 14x 14x 14x 14x 14x 14x 14x           14x                                   14x                           14x 14x 14x   14x 10x 4x 14x   14x 14x 14x 14x 14x 14x             14x 14x 14x   14x 3x                       14x   14x 14x 3x 11x   11x 10x 1x   1x   14x 14x 14x 14x 14x 14x 14x 14x 14x 14x   14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x   14x 14x 14x       14x                                                                                               14x  
import { useEffect, useMemo, useRef, useState } from "react";
import { useNavigate, useParams } from "react-router-dom";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import {
  ApiError,
  createLiveSession,
  deleteSession,
  expandSessionInput,
  fetchConfigBootstrap,
  fetchLiveSessionDetail,
  fetchSessionDetail,
  fetchSessions,
  setActiveModelProfile,
  setLiveSessionProfile,
  submitSessionInput,
  uploadSessionImages,
} from "../../api";
import type { HistoryItem, ModelProfileView, SessionRecord, TimelineItem } from "../../types";
import {
  getLiveSessionKey,
  getSavedSessionKey,
  useSessionStore,
} from "../../store";
import { useLiveSessionEvents } from "../../hooks/useLiveSessionEvents";
import { ConnectionBadge } from "./ConnectionBadge";
import { DeleteSessionModal } from "./DeleteSessionModal";
import { RunHistory } from "./RunHistory";
import { SessionSidebar } from "./SessionSidebar";
import { SessionTimeline } from "./SessionTimeline";
import { UsageBar } from "./UsageBar";
import { Composer, type ComposerHandle } from "./Composer";
 
export function SessionPage({
  workspaceRoot,
  supportsImageInputs,
}: {
  workspaceRoot: string | undefined;
  supportsImageInputs: boolean;
}) {
  const client = useQueryClient();
  const navigate = useNavigate();
  const {
    sessionId: routeSessionId,
    liveSessionId: routeLiveSessionId,
  } = useParams<{ sessionId?: string; liveSessionId?: string }>();
  const [sidebarOpen, setSidebarOpen] = useState(false);
  const [inputWarnings, setInputWarnings] = useState<string[]>([]);
  const [pendingDeleteSession, setPendingDeleteSession] = useState<SessionRecord | null>(null);
  const [pendingProfileId, setPendingProfileId] = useState<string | null>(null);
  const composerRef = useRef<ComposerHandle>(null);
  const createRequestKeyRef = useRef<string | null>(null);
 
  const routeSessionKey = routeSessionId
    ? getSavedSessionKey(routeSessionId)
    : routeLiveSessionId
      ? getLiveSessionKey(routeLiveSessionId)
      : null;
 
  const activeSessionKey = useSessionStore((state) => {
    if (routeSessionId) {
      return state.sessionIndex[routeSessionId] ?? getSavedSessionKey(routeSessionId);
    }
    if (routeLiveSessionId) {
      return state.liveSessionIndex[routeLiveSessionId] ?? getLiveSessionKey(routeLiveSessionId);
    }
    return state.activeSessionKey;
  });
  const selectedRouteSessionKey =
    routeSessionId || routeLiveSessionId ? (activeSessionKey ?? routeSessionKey) : null;
 
  const sessionState = useSessionStore((state) =>
    selectedRouteSessionKey ? state.sessionsByKey[selectedRouteSessionKey] ?? null : null,
  );
  const setActiveSession = useSessionStore((state) => state.setActiveSession);
  const hydrateSavedSession = useSessionStore((state) => state.hydrateSavedSession);
  const attachLiveSession = useSessionStore((state) => state.attachLiveSession);
  const hydrateLiveSnapshot = useSessionStore((state) => state.hydrateLiveSnapshot);
  const updateRuntimeFromSession = useSessionStore((state) => state.updateRuntimeFromSession);
 
  useEffect(() => {
    setActiveSession(selectedRouteSessionKey);
  }, [selectedRouteSessionKey, setActiveSession]);
 
  const sessionsQuery = useQuery({
    queryKey: ["sessions"],
    queryFn: fetchSessions,
    refetchInterval: 12_000,
  });
 
  const sessionDetailQuery = useQuery({
    queryKey: ["session", routeSessionId],
    queryFn: () => fetchSessionDetail(routeSessionId!),
    enabled: Boolean(routeSessionId),
    retry: false,
  });
 
  const liveSessionDetailQuery = useQuery({
    queryKey: ["live-session", routeLiveSessionId],
    queryFn: () => fetchLiveSessionDetail(routeLiveSessionId!),
    enabled: Boolean(routeLiveSessionId),
    retry: false,
  });
 
  const configQuery = useQuery({
    queryKey: ["config-bootstrap"],
    queryFn: fetchConfigBootstrap,
    staleTime: 30_000,
  });
 
  const createSessionMutation = useMutation({
    mutationFn: createLiveSession,
    onSuccess: (session, variables) => {
      const requestSessionId = variables?.session_id ?? null;
      const requestedKey = createRequestKeyRef.current
        ?? (requestSessionId
          ? getSavedSessionKey(requestSessionId)
          : getLiveSessionKey(session.live_session_id));
      const resolvedKey = attachLiveSession(requestedKey, session, {
        preserveItems: Boolean(requestSessionId),
      });
      if (!requestSessionId) {
        void navigate(`/sessions/live/${encodeURIComponent(session.live_session_id)}`, {
          replace: true,
        });
      } else if (resolvedKey !== requestedKey && session.session_id) {
        void navigate(`/sessions/${encodeURIComponent(session.session_id)}`, { replace: true });
      }
    },
  });
 
  const sendInputMutation = useMutation({
    mutationFn: (payload: {
      text: string;
      file_paths: string[];
      image_paths: string[];
      image_upload_ids: string[];
      profile_id?: string | null;
    }) => {
      if (!sessionState?.liveSessionId) throw new Error("No live session available.");
      return submitSessionInput(sessionState.liveSessionId, payload);
    },
    onSuccess: (session) => {
      if (selectedRouteSessionKey) {
        updateRuntimeFromSession(selectedRouteSessionKey, session);
      }
    },
  });
 
  const deleteSessionMutation = useMutation({
    mutationFn: deleteSession,
  });
 
  const setSessionProfileMutation = useMutation({
    mutationFn: ({
      liveSessionId,
      profileId,
    }: {
      liveSessionId: string;
      profileId: string | null;
    }) => setLiveSessionProfile(liveSessionId, profileId),
    onSuccess: (session) => {
      const targetKey = session.session_id
        ? getSavedSessionKey(session.session_id)
        : selectedRouteSessionKey;
      if (targetKey) {
        updateRuntimeFromSession(targetKey, session);
      }
      void client.invalidateQueries({ queryKey: ["sessions"] });
      if (session.session_id) {
        void client.invalidateQueries({ queryKey: ["session", session.session_id] });
      }
    },
  });
 
  const setActiveProfileMutation = useMutation({
    mutationFn: ({
      profileId,
      configRevision,
    }: {
      profileId: string | null;
      configRevision: string;
    }) => setActiveModelProfile(profileId, configRevision),
    onSuccess: async () => {
      await Promise.all([
        client.invalidateQueries({ queryKey: ["config-bootstrap"] }),
        client.invalidateQueries({ queryKey: ["bootstrap"] }),
      ]);
    },
  });
 
  // Hydrate saved session items when session detail data arrives.
  // Skip hydration when a live session is already attached and has
  // items — even if the WebSocket is reconnecting.  Re-hydrating
  // during reconnection replaces WS-sourced items (item IDs like
  // "message-N") with API history items ("history-N"), then the WS
  // snapshot replay adds them again with their original IDs,
  // producing duplicates and flickering.
  useEffect(() => {
    if (!routeSessionId || !sessionDetailQuery.isSuccess) return;
    const sessionKey = getSavedSessionKey(routeSessionId);
    const existing = useSessionStore.getState().sessionsByKey[sessionKey];
    const skipHydration =
      existing?.liveSessionId
      && !existing.sessionEnded
      && existing.items.length > 0;
    if (!skipHydration) {
      const serverSeq =
        sessionDetailQuery.data.active_live_session?.last_event_seq;
      hydrateSavedSession(
        routeSessionId,
        sessionDetailQuery.data.history_items.map(mapHistoryItem),
        typeof serverSeq === "number" ? serverSeq : undefined,
      );
    }
    if (sessionDetailQuery.data.active_live_session) {
      attachLiveSession(sessionKey, sessionDetailQuery.data.active_live_session, {
        preserveItems: true,
      });
    }
  }, [
    attachLiveSession,
    hydrateSavedSession,
    routeSessionId,
    sessionDetailQuery.isSuccess,
    sessionDetailQuery.data,
  ]);
 
  // Create a new live session for a saved session when one doesn't exist yet.
  useEffect(() => {
    if (!routeSessionId || !sessionDetailQuery.isSuccess) return;
    if (sessionDetailQuery.data.active_live_session) return;
    if (sessionState?.liveSessionId && !sessionState.sessionEnded) return;
    if (createSessionMutation.isPending) return;
    createRequestKeyRef.current = getSavedSessionKey(routeSessionId);
    createSessionMutation.mutate({ session_id: routeSessionId });
  }, [
    sessionState?.liveSessionId,
    sessionState?.sessionEnded,
    createSessionMutation,
    routeSessionId,
    sessionDetailQuery.isSuccess,
    sessionDetailQuery.data?.active_live_session,
  ]);
 
  useEffect(() => {
    if (!routeLiveSessionId || !liveSessionDetailQuery.isSuccess) return;
    const resolvedKey = hydrateLiveSnapshot(
      getLiveSessionKey(routeLiveSessionId),
      liveSessionDetailQuery.data.live_session,
      liveSessionDetailQuery.data.snapshot,
    );
    if (liveSessionDetailQuery.data.live_session.session_id) {
      void navigate(
        `/sessions/${encodeURIComponent(liveSessionDetailQuery.data.live_session.session_id)}`,
        { replace: true },
      );
      setActiveSession(resolvedKey);
    }
  }, [
    hydrateLiveSnapshot,
    liveSessionDetailQuery.isSuccess,
    liveSessionDetailQuery.data,
    navigate,
    routeLiveSessionId,
    setActiveSession,
  ]);
 
  useEffect(() => {
    if (routeSessionId || routeLiveSessionId) return;
    if (createSessionMutation.isPending) return;
    const profiles = configQuery.data?.model_profiles ?? [];
    if (profiles.length === 0) return;
    const profileId = resolveSavedProfileId(
      pendingProfileId ?? configQuery.data?.active_profile_id ?? profiles[0]?.id ?? null,
      profiles,
    );
    createRequestKeyRef.current = null;
    createSessionMutation.mutate(profileId ? { profile_id: profileId } : {});
  }, [
    configQuery.data,
    createSessionMutation,
    pendingProfileId,
    routeLiveSessionId,
    routeSessionId,
  ]);
 
  useEffect(() => {
    if (!routeLiveSessionId || !sessionState?.sessionId) return;
    void navigate(`/sessions/${encodeURIComponent(sessionState.sessionId)}`, { replace: true });
  }, [sessionState?.sessionId, navigate, routeLiveSessionId]);
 
  useEffect(() => {
    if (sessionState?.inputEnabled && sessionState.liveSessionId && !sessionState.sessionEnded) {
      composerRef.current?.focus();
    }
  }, [sessionState?.inputEnabled, sessionState?.liveSessionId, sessionState?.sessionEnded]);
 
  useEffect(() => {
    if (inputWarnings.length === 0) return undefined;
    const timeoutId = window.setTimeout(() => setInputWarnings([]), 5000);
    return () => window.clearTimeout(timeoutId);
  }, [inputWarnings]);
 
  useEffect(() => {
    const sessionId = sessionState?.sessionId;
    if (!sessionId) return;
    void client.invalidateQueries({ queryKey: ["sessions"] });
    void client.invalidateQueries({ queryKey: ["session", sessionId] });
  }, [sessionState?.sessionId, client]);
 
  useLiveSessionEvents(selectedRouteSessionKey, sessionState?.liveSessionId ?? null);
 
  const activeSessionRecord = useMemo(() => {
    if (!routeSessionId) return null;
    return (
      sessionDetailQuery.data?.session
      ?? sessionsQuery.data?.find((session) => session.session_id === routeSessionId)
      ?? {
        session_id: routeSessionId,
        directory: workspaceRoot ?? "",
        provider: "",
        provider_id: null,
        model: "",
        profile_id: null,
        previous_id: null,
        title: "",
        total_tokens: 0,
        input_tokens: 0,
        output_tokens: 0,
        cost_usd: 0,
        created_at: "",
        updated_at: "",
      }
    );
  }, [routeSessionId, sessionDetailQuery.data?.session, sessionsQuery.data, workspaceRoot]);
 
  const modelProfiles = configQuery.data?.model_profiles ?? [];
  const routeProfileId = routeSessionId ? activeSessionRecord?.profile_id ?? "" : "";
  const runtimeProfileId = sessionState?.runtime?.profile_id ?? "";
  const initialBlankProfileId = configQuery.data?.active_profile_id ?? modelProfiles[0]?.id ?? "";
  const selectedProfileId =
    pendingProfileId ?? (runtimeProfileId || (routeSessionId ? routeProfileId : initialBlankProfileId));
  const selectedSavedProfileId = resolveSavedProfileId(selectedProfileId, modelProfiles);
  const providerSupportsImages =
    sessionState?.runtime?.provider
      ? (
          configQuery.data?.options.provider_metadata[sessionState.runtime.provider]?.supports_image_inputs
          ?? supportsImageInputs
        )
      : supportsImageInputs;
  const profileSelectorDisabled =
    modelProfiles.length === 0
    || !sessionState?.liveSessionId
    || !sessionState.inputEnabled
    || sessionState.sessionEnded
    || createSessionMutation.isPending
    || setSessionProfileMutation.isPending
    || setActiveProfileMutation.isPending;
 
  const handleSubmit = async (payload: { text: string; images: File[] }) => {
    const { text, images } = payload;
    setInputWarnings([]);
    if (text.startsWith("/")) {
      await sendInputMutation.mutateAsync({
        text,
        file_paths: [],
        image_paths: [],
        image_upload_ids: [],
        profile_id: selectedSavedProfileId,
      });
      return;
    }
 
    const expanded = await expandSessionInput(text);
    if (expanded.warnings.length > 0) {
      setInputWarnings(expanded.warnings);
    }
    const uploadedImageIds =
      images.length > 0 && sessionState?.liveSessionId
        ? (await uploadSessionImages(sessionState.liveSessionId, images)).map((image) => image.upload_id)
        : [];
    await sendInputMutation.mutateAsync({
      text: expanded.text,
      file_paths: expanded.file_paths,
      image_paths: Array.from(new Set(expanded.image_paths)),
      image_upload_ids: uploadedImageIds,
      profile_id: selectedSavedProfileId,
    });
  };
 
  const handleProfileChange = async (nextProfileId: string) => {
    setPendingProfileId(nextProfileId);
    try {
      const nextConfigRevision = configQuery.data?.config_revision;
      if (!nextConfigRevision) {
        throw new Error("Settings are not loaded yet.");
      }
      await setActiveProfileMutation.mutateAsync({
        profileId: nextProfileId,
        configRevision: nextConfigRevision,
      });
      if (sessionState?.liveSessionId) {
        await setSessionProfileMutation.mutateAsync({
          liveSessionId: sessionState.liveSessionId,
          profileId: nextProfileId,
        });
      }
    } finally {
      setPendingProfileId(null);
    }
  };
 
  const handleNewSession = () => {
    setSidebarOpen(false);
    createRequestKeyRef.current = null;
    void navigate("/sessions");
  };
 
  const handleDeleteSession = async () => {
    if (!pendingDeleteSession) return;
    const deletingActive = pendingDeleteSession.session_id === routeSessionId;
    await deleteSessionMutation.mutateAsync(pendingDeleteSession.session_id);
    client.setQueryData<SessionRecord[] | undefined>(["sessions"], (sessions) =>
      (sessions ?? []).filter((session) => session.session_id !== pendingDeleteSession.session_id),
    );
    setPendingDeleteSession(null);
    await client.invalidateQueries({ queryKey: ["sessions"] });
    if (deletingActive) {
      void navigate("/sessions", { replace: true });
    }
  };
 
  const canDeleteActiveSession = Boolean(routeSessionId && activeSessionRecord);
  const isDeleteBusy = deleteSessionMutation.isPending || createSessionMutation.isPending;
  const sessionDetailError = routeSessionId ? sessionDetailQuery.error : liveSessionDetailQuery.error;
  const sessionNotFound =
    sessionDetailError instanceof ApiError && sessionDetailError.status === 404;
  const sessionDetailLoadError =
    (routeSessionId || routeLiveSessionId) && sessionDetailError && !sessionNotFound
      ? "Unable to load this session right now."
      : null;
 
  return (
    <section className={`session-layout ${sidebarOpen ? "session-layout--sidebar-open" : ""}`}>
      <div className={`sidebar ${sidebarOpen ? "sidebar--open" : ""}`}>
        <SessionSidebar
          sessions={sessionsQuery.data ?? []}
          isLoading={sessionsQuery.isLoading}
          activeSessionId={routeSessionId ?? sessionState?.sessionId ?? null}
          workspaceRoot={workspaceRoot}
          onNewSession={handleNewSession}
          onResumeSession={(sessionId) => {
            void navigate(`/sessions/${encodeURIComponent(sessionId)}`);
            setSidebarOpen(false);
          }}
          onDeleteSession={(session) => {
            deleteSessionMutation.reset();
            setPendingDeleteSession(session);
          }}
          onToggle={() => setSidebarOpen((prev) => !prev)}
          isOpen={sidebarOpen}
        />
      </div>
 
      <div className="session-panel">
        <div className="session-topbar">
          <div className="session-topbar__leading">
            <ConnectionBadge connection={sessionState?.connection ?? "disconnected"} />
            <ProfileSelector
              selectedProfileId={selectedProfileId}
              modelProfiles={modelProfiles}
              isLoading={configQuery.isPending}
              disabled={profileSelectorDisabled}
              onChange={(id) => {
                void handleProfileChange(id);
              }}
            />
          </div>
          <div className="session-topbar__actions">
            <UsageBar
              sessionUsage={sessionState?.sessionUsage ?? null}
              turnUsage={sessionState?.turnUsage ?? null}
            />
            {routeSessionId ? (
              <RunHistory sessionId={routeSessionId} />
            ) : null}
            {canDeleteActiveSession ? (
              <button
                type="button"
                className="btn btn--ghost btn--icon btn--danger"
                title="Delete session"
                onClick={() => {
                  if (activeSessionRecord) {
                    deleteSessionMutation.reset();
                    setPendingDeleteSession(activeSessionRecord);
                  }
                }}
              >
                <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
                  <polyline points="3 6 5 6 21 6" />
                  <path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2" />
                  <line x1="10" y1="11" x2="10" y2="17" />
                  <line x1="14" y1="11" x2="14" y2="17" />
                </svg>
              </button>
            ) : null}
          </div>
        </div>
 
        {inputWarnings.length > 0 ? (
          <div className="banner banner--notice">{inputWarnings.join(" ")}</div>
        ) : null}
        {sessionState?.fatalError ? (
          <div className="banner banner--error">{sessionState.fatalError}</div>
        ) : null}
        {createSessionMutation.error ? (
          <div className="banner banner--error">{createSessionMutation.error.message}</div>
        ) : null}
        {setSessionProfileMutation.error ? (
          <div className="banner banner--error">{setSessionProfileMutation.error.message}</div>
        ) : null}
        {setActiveProfileMutation.error ? (
          <div className="banner banner--error">{setActiveProfileMutation.error.message}</div>
        ) : null}
        {sessionDetailLoadError ? (
          <div className="banner banner--error">{sessionDetailLoadError}</div>
        ) : null}
 
        {sessionNotFound ? (
          <div className="empty-state">
            <div className="empty-state__title">Session not found</div>
            <div className="empty-state__description">
              {sessionDetailError instanceof Error
                ? sessionDetailError.message
                : "This session does not exist in the current workspace."}
            </div>
            <button
              type="button"
              className="btn btn--primary empty-state__action"
              onClick={handleNewSession}
            >
              Start new session
            </button>
          </div>
        ) : (
          <>
            <SessionTimeline
              items={sessionState?.items ?? []}
              itemsVersion={sessionState?.itemsVersion ?? 0}
              subAgents={sessionState?.subAgents ?? {}}
              connection={sessionState?.connection ?? "disconnected"}
              waitMessage={sessionState?.waitMessage ?? null}
            />
            <Composer
              ref={composerRef}
              inputEnabled={sessionState?.inputEnabled ?? false}
              sessionEnded={sessionState?.sessionEnded ?? false}
              liveSessionId={sessionState?.liveSessionId ?? null}
              supportsImageInputs={providerSupportsImages}
              isSubmitting={sendInputMutation.isPending}
              onSubmit={handleSubmit}
            />
          </>
        )}
      </div>
 
      {pendingDeleteSession ? (
        <DeleteSessionModal
          session={pendingDeleteSession}
          isDeleting={isDeleteBusy}
          error={deleteSessionMutation.error?.message ?? null}
          onConfirm={() => {
            void handleDeleteSession();
          }}
          onClose={() => {
            if (!isDeleteBusy) {
              deleteSessionMutation.reset();
              setPendingDeleteSession(null);
            }
          }}
        />
      ) : null}
    </section>
  );
}
 
function mapHistoryItem(item: HistoryItem): TimelineItem {
  return {
    kind: "message",
    itemId: item.item_id,
    role: item.role,
    content: item.content,
    filePaths: item.file_paths,
    imageAttachments: item.image_attachments,
    markdown: item.markdown,
  };
}
 
function resolveSavedProfileId(
  candidateId: string | null,
  modelProfiles: ModelProfileView[],
): string | null {
  return candidateId && modelProfiles.some((profile) => profile.id === candidateId)
    ? candidateId
    : null;
}
 
function ProfileSelector({
  selectedProfileId,
  modelProfiles,
  isLoading,
  disabled,
  onChange,
}: {
  selectedProfileId: string;
  modelProfiles: ModelProfileView[];
  isLoading: boolean;
  disabled: boolean;
  onChange: (profileId: string) => void;
}) {
  const [isOpen, setIsOpen] = useState(false);
  const containerRef = useRef<HTMLDivElement>(null);
 
  useEffect(() => {
    if (!isOpen) return undefined;
    const handlePointerDown = (event: PointerEvent) => {
      if (
        containerRef.current
        && event.target instanceof Node
        && !containerRef.current.contains(event.target)
      ) {
        setIsOpen(false);
      }
    };
    document.addEventListener("pointerdown", handlePointerDown);
    return () => document.removeEventListener("pointerdown", handlePointerDown);
  }, [isOpen]);
 
  const selectedProfile = modelProfiles.find((profile) => profile.id === selectedProfileId);
  const label = isLoading && modelProfiles.length === 0
    ? "Loading…"
    : modelProfiles.length === 0
      ? "No profiles"
      : selectedProfile
        ? selectedProfile.name
        : selectedProfileId
          ? "Unavailable"
          : "Select profile";
 
  return (
    <div className={`profile-selector${isOpen ? " is-open" : ""}`} ref={containerRef}>
      <button
        type="button"
        className="profile-selector__trigger"
        onClick={() => setIsOpen((value) => !value)}
        disabled={disabled}
        aria-haspopup="listbox"
        aria-expanded={isOpen}
        aria-label={`Model profile: ${label}`}
      >
        <svg className="profile-selector__icon" width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
          <rect x="4" y="4" width="16" height="16" rx="2" />
          <rect x="9" y="9" width="6" height="6" />
          <line x1="9" y1="2" x2="9" y2="4" />
          <line x1="15" y1="2" x2="15" y2="4" />
          <line x1="9" y1="20" x2="9" y2="22" />
          <line x1="15" y1="20" x2="15" y2="22" />
          <line x1="2" y1="9" x2="4" y2="9" />
          <line x1="2" y1="15" x2="4" y2="15" />
          <line x1="20" y1="9" x2="22" y2="9" />
          <line x1="20" y1="15" x2="22" y2="15" />
        </svg>
        <span className="profile-selector__name">{label}</span>
        <svg className="profile-selector__chevron" width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
          <polyline points="6 9 12 15 18 9" />
        </svg>
      </button>
 
      {isOpen && modelProfiles.length > 0 ? (
        <div className="profile-selector__dropdown" role="listbox" aria-label="Model profiles">
          {modelProfiles.map((profile) => {
            const isSelected = profile.id === selectedProfileId;
            return (
              <button
                key={profile.id}
                type="button"
                role="option"
                aria-selected={isSelected}
                className={`profile-selector__option${isSelected ? " is-selected" : ""}`}
                onClick={() => {
                  onChange(profile.id);
                  setIsOpen(false);
                }}
              >
                <span className="profile-selector__option-check">
                  {isSelected ? (
                    <svg width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="3" strokeLinecap="round" strokeLinejoin="round" className="profile-selector__check" aria-hidden="true">
                      <polyline points="20 6 9 17 4 12" />
                    </svg>
                  ) : null}
                </span>
                <span className="profile-selector__option-body">
                  <span className="profile-selector__option-name">{profile.name}</span>
                  <span className="profile-selector__option-meta">
                    <span>{profile.provider.name}</span>
                    {profile.model ? (
                      <>
                        <span className="profile-selector__dot" aria-hidden="true">·</span>
                        <span>{profile.model}</span>
                      </>
                    ) : null}
                    {profile.reasoning_effort ? (
                      <>
                        <span className="profile-selector__dot" aria-hidden="true">·</span>
                        <span>{profile.reasoning_effort}</span>
                      </>
                    ) : null}
                  </span>
                </span>
              </button>
            );
          })}
        </div>
      ) : null}
    </div>
  );
}