import type { ServerConnection } from "@/runtime/server/registry" import { TAB_GROUP_COLORS, type TabGroupColor } from "@/shell/titlebar/tab-groups" /** * The shared contract of the New navigation experiment (Settings → Experimental → New navigation). The source * (`source.ts`) turns real sessions into `NavSession`s, the view (`view.ts`) decides where each appears, and the list, * tabs, menus and settings page only render a `ListView` and change `NavigationSettings` or `NavigationGroups`. * Ported from the v4 Sessions prototype. Changes to this file go through the lead, not the workers. */ // Settings export const NAVIGATION_MODES = ["tabs", "sessions"] as const export type NavigationMode = (typeof NAVIGATION_MODES)[number] export const TAB_POSITIONS = ["top", "side"] as const export type TabPosition = (typeof TAB_POSITIONS)[number] /** Sessions list grouping. Tabs always group by hand (Manual) whatever this says. */ export const GROUP_BYS = ["none", "status", "project", "server", "manual"] as const export type GroupBy = (typeof GROUP_BYS)[number] /** Compact is the production one-line `TabNavItem`; Detailed renders `DEFAULT_ROW_LAYOUT`; Custom renders `rowLayout`. */ export const ROW_DETAILS = ["compact", "detailed", "custom"] as const export type RowDetails = (typeof ROW_DETAILS)[number] /** How much of the ungrouped list shows before Show more. Sessions that need you always show. */ export const SHOW_COUNTS = ["10", "30", "week", "all"] as const export type ShowCount = (typeof SHOW_COUNTS)[number] /** How many rows each headed group shows before Show more. Sessions that need you always show. */ export const ROWS_PER_GROUP = ["3", "5", "10", "all"] as const export type RowsPerGroup = (typeof ROWS_PER_GROUP)[number] /** Inline row buttons, always rendered in this order. */ export const ROW_ACTIONS = ["archive", "pin", "markRead", "hideUntilReply"] as const export type RowAction = (typeof ROW_ACTIONS)[number] /** Two actions still leave room for a title in a 280px sidebar. */ export const MAX_ROW_ACTIONS = 2 export const AUTO_ARCHIVES = ["never", "7", "30"] as const export type AutoArchive = (typeof AUTO_ARCHIVES)[number] /** What a Detailed or Custom row can show. Title is required; status is always a mark in the time slot, not an item. */ export const ROW_ITEMS = [ "projectIcon", "title", "time", "projectName", "branch", "worktree", "server", "subagents", ] as const export type RowItem = (typeof ROW_ITEMS)[number] export const LANES = ["line1", "line2", "hidden"] as const export type Lane = (typeof LANES)[number] /** Items that can show as an icon alone or as an icon and a name. Below 200 px they always show as icons. */ export const FORM_ITEMS = ["branch", "server"] as const export type FormItem = (typeof FORM_ITEMS)[number] export const ITEM_FORMS = ["icon", "name"] as const export type ItemForm = (typeof ITEM_FORMS)[number] export type RowLayout = { line1: RowItem[] line2: RowItem[] hidden: RowItem[] forms: Record } /** Detailed rows: the production Inbox card's two lines. */ export const DEFAULT_ROW_LAYOUT: RowLayout = { line1: ["projectIcon", "title", "time"], line2: ["projectName", "branch", "server"], hidden: ["worktree", "subagents"], forms: { branch: "name", server: "name" }, } /** Persisted under `settings.v3` → `navigation`. Read with `settings.navigation.get()`, write with `.set(partial)`. */ export type NavigationSettings = { /** The experiment switch. Read the effective value with `settings.general.newNavigation()`. */ enabled: boolean /** First-enable seeding (`seedNavigation`) has run. It never runs again. */ seeded: boolean mode: NavigationMode tabPosition: TabPosition groupBy: GroupBy rowDetails: RowDetails /** The Custom layout; absent until the layout editor (PR 2) saves one. */ rowLayout?: RowLayout show: ShowCount rowsPerGroup: RowsPerGroup /** At most `MAX_ROW_ACTIONS`, in `ROW_ACTIONS` order. The settings accessor normalizes stored values. */ rowActions: RowAction[] /** On: children nest under their parent. Off: children are hidden and the root carries their requests. */ subagents: boolean /** List archived and hidden sessions at the end. */ archived: boolean /** Running sessions in a bottom shelf instead of in place. */ runningAtBottom: boolean autoArchive: AutoArchive } /** What a never-seeded store reads as. Seeding replaces mode and the rest from the current layout on first enable. */ export const DEFAULT_NAVIGATION: NavigationSettings = { enabled: false, seeded: false, mode: "sessions", tabPosition: "side", groupBy: "none", rowDetails: "compact", show: "10", rowsPerGroup: "5", rowActions: ["archive"], subagents: false, archived: false, runningAtBottom: false, autoArchive: "never", } // Keys /** `${server}\0${id}`, the same shape `pinKey` in `shell/tabs/pins.ts` uses. */ export type NavigationKey = string /** `draft:${draftID}`, the same as `tabKey` for drafts, so draft keys never collide with sessions. */ export type DraftKey = `draft:${string}` export type RowKey = NavigationKey | DraftKey export const navigationKey = (server: ServerConnection.Key | string, id: string): NavigationKey => `${server}\0${id}` export const draftKey = (draftID: string): DraftKey => `draft:${draftID}` // Data the source produces /** * A session's own status: a request outranks a run, a run outranks unread, and mute silences unread. With Subagents * off, the view gives a root the oldest request waiting anywhere below it (`SessionRow.request`). */ export const STATUSES = ["approval", "question", "working", "failed", "unread", "idle"] as const export type Status = (typeof STATUSES)[number] export type RequestKind = Extract /** One real session, flattened by `source.ts` from the session index, requests, busy state and notifications. */ export type NavSession = { key: NavigationKey id: string server: ServerConnection.Key title: string /** Undefined for a session outside any project. Unique per server only: compare with `server`. */ projectID: string | undefined directory: string /** The worktree directory when it differs from the project's root. */ worktree: string | undefined branch?: string /** The session that started this one (a subagent), by id on the same server. */ parentID: string | undefined created: number /** The last agent activity: a reply, a request or a new run. Reading does not move it. Lists sort by it. */ activity: number archived: boolean status: Status /** When the pending request arrived, while `status` is approval or question. */ requestSince?: number /** When the current run started, while `status` is working. */ runSince?: number muted: boolean } /** A draft tab: a new session not yet on a server. */ export type NavDraft = { key: DraftKey draftID: string server: ServerConnection.Key projectID: string | undefined directory: string /** The start of the draft's text. */ preview: string updated: number } export type NavServer = { key: ServerConnection.Key; name: string } export type NavProject = { server: ServerConnection.Key; id: string; name: string } // Manual groups: one per-session store for tabs and the Sessions list (`Persist.global("navigation.groups")`) export const GROUP_COLORS = TAB_GROUP_COLORS export type GroupColor = TabGroupColor export type NavigationGroup = { id: string; name: string; color: GroupColor } export type NavigationGroups = { groups: NavigationGroup[] /** Session key → group id. Membership lives on the session, so closing a tab keeps it. */ membership: Record /** Manual order across every session (Group by Manual and tab order inside groups). Missing keys go first. */ order: NavigationKey[] } export const EMPTY_NAVIGATION_GROUPS: NavigationGroups = { groups: [], membership: {}, order: [] } /** * Pure operations over `NavigationGroups`, exported by name from `groups.ts` (for example * `export const assignGroup: NavigationGroupOps["assignGroup"] = ...`). Each returns the same object when nothing changes. * Ids come from the caller (`uuid()`), so the operations stay pure. */ export type NavigationGroupOps = { /** Adds a group, optionally moving `keys` into it. */ createGroup: (state: NavigationGroups, group: NavigationGroup, keys: NavigationKey[]) => NavigationGroups renameGroup: (state: NavigationGroups, id: string, name: string) => NavigationGroups recolorGroup: (state: NavigationGroups, id: string, color: GroupColor) => NavigationGroups /** Removes the group; its sessions stay, ungrouped. */ ungroup: (state: NavigationGroups, id: string) => NavigationGroups /** Moves a session into a group, or out of every group with `group: undefined`. Removes groups left empty. */ assignGroup: (state: NavigationGroups, key: NavigationKey, group: string | undefined) => NavigationGroups /** Moves a session before `before` in manual order, or to the end. */ moveSession: (state: NavigationGroups, key: NavigationKey, before: NavigationKey | undefined) => NavigationGroups /** Drops membership and order of sessions `keep` rejects (deleted sessions); keeps groups. */ pruneGroups: (state: NavigationGroups, keep: (key: NavigationKey) => boolean) => NavigationGroups } // View /** Temporary, from the Customize menu only; never saved. Project ids are `navigationKey(server, projectID)` or "none". */ export type NavigationFilters = { query: string; status: Status[]; project: string[]; server: ServerConnection.Key[] } export const EMPTY_FILTERS: NavigationFilters = { query: "", status: [], project: [], server: [] } /** Window-local view state the list owns (not persisted settings). */ export type NavigationViewState = { filters: NavigationFilters /** Collapsed section ids. */ collapsed: Record /** Extra rows revealed per section id by Show more. */ expanded: Record } export type ViewInput = { sessions: NavSession[] drafts: NavDraft[] /** Pinned sessions in display order (flattened from `navigation.pins`). A pin is independent of its group. */ pins: NavigationKey[] groups: NavigationGroups selected: RowKey | undefined /** Keys of the sessions and drafts open as tabs in this window, in tab order. Never decides what the list shows. */ open: RowKey[] now: number /** In display order: Group by Server follows it. */ servers: NavServer[] projects: NavProject[] view: NavigationViewState /** Hide until reply (PR 2): session key → hiddenAt. Absent in PR 1. */ hiddenUntilReply?: Record } /** The request a row shows: its own, or with Subagents off, the oldest one waiting anywhere below it. */ export type FamilyRequest = { key: NavigationKey; kind: RequestKind; since: number } export type SessionRow = { kind: "session" key: NavigationKey session: NavSession /** Family-level: the row's request kind when `request` is set, else the session's own status. */ status: Status request: FamilyRequest | undefined /** Indent level: 0 for roots, one more per parent when subagents are nested. */ depth: number pinned: boolean open: boolean selected: boolean group: NavigationGroup | undefined /** Archived or hidden, shown only because it needs you; it leaves again once answered. */ surfaced: boolean /** Dimmed rows in the Archived section. */ archived: boolean } export type DraftRow = { kind: "draft"; key: DraftKey; draft: NavDraft; open: boolean; selected: boolean } export type Row = SessionRow | DraftRow export type SectionKind = "pinned" | "drafts" | "group" | "main" | "working" | "archived" /** Status grouping is the Inbox: Needs you (longest waiting first), Running (by start), then Recent. */ export const STATUS_SECTIONS = ["attention", "running", "recent"] as const export type StatusSection = (typeof STATUS_SECTIONS)[number] /** * What a section header says. The view stays free of copy; the list maps each to an i18n key (`navigation.section.*`) * or to the data's own name. Undefined for headerless sections (drafts, the ungrouped main list). */ export type SectionHeading = | { type: "pinned" } | { type: "status"; status: StatusSection } /** `projectID` undefined is "No project". `name` falls back to the project id. */ | { type: "project"; server: ServerConnection.Key; projectID: string | undefined; name: string } | { type: "server"; server: ServerConnection.Key; name: string } | { type: "group"; group: NavigationGroup } | { type: "running" } | { type: "archived" } export type Section = { /** Stable id, also the key in `NavigationViewState.collapsed` and `.expanded`. */ id: string kind: SectionKind heading: SectionHeading | undefined collapsible: boolean collapsed: boolean /** The rows to render. A collapsed section still renders its selected row. */ rows: Row[] /** Every row the section holds, before collapse and limit. */ total: number /** Rows held back by the limit; show "Show more" when above zero. */ more: number /** What a collapsed section hides: a request outranks unread. */ hiddenMark: "attention" | "unread" | undefined } export type ListView = { sections: Section[] /** Every rendered row key in display order: keyboard navigation and ⌘1–9 follow it. */ order: RowKey[] /** No session at all (before filters). */ empty: boolean /** Sessions exist but filters hide them all. */ filteredOut: boolean } /** * Where every session appears, implemented in `view.ts` as `export const navigationView: NavigationView`. Drafts * first, then Pinned, the grouped list, the Running shelf and Archived; always last activity first, except inside Group * by Manual. Show limits the ungrouped list, Rows per group each headed group; rows that need you, the selected row and * the parents they nest under always show. Pure: no signals, no `Date.now()`. */ export type NavigationView = (input: ViewInput, settings: NavigationSettings) => ListView // Seeding /** The current layout, read once on first enable. Values as the settings accessors return them. */ export type SeedInput = { tabLayout: "horizontal" | "vertical" | "inbox" sidebarOrganize: "list" | "customize" | "recent" | "consolidated" tabGroups: boolean /** The Consolidated prototype's Tabs or List (`settings.appearance.navigation()`). */ prototypeNavigation: "tabs" | "list" /** Its tab grouping (`settings.appearance.tabGrouping()`). */ prototypeGrouping: "none" | "project" | "server" /** Customize's show-subagents choice, when that organization is active. */ customizeSubagents: boolean current: NavigationSettings } /** * Settings for a first enable, implemented in `seed.ts` as `export const seedNavigation: SeedNavigation`. Returns * `current` with the plan's table applied, `enabled` and `seeded` true. Store moves (tab groups → `navigation.groups`, * recent pins and pin folders → `navigation.pins`) are the caller's, from the same table. */ export type SeedNavigation = (input: SeedInput) => NavigationSettings