Something went wrong. Try again.
opencode v2 mirror
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140import { getFilename } from "@opencode/util/path"import type { SessionInfo } from "@opencode/client/promise"import { pathKey } from "@/workspaces/path-key"import { isProjectDirectory } from "@/workspaces/paths"import type { ServerConnection } from "@/runtime/server/registry"import type { HomeProjectSelection } from "@/shell/state/layout"
type SessionStore = { session?: SessionInfo[] path: { directory: string }}
export function compareSessionTime(a: SessionInfo, b: SessionInfo) { const updated = (b.time.updated ?? b.time.created) - (a.time.updated ?? a.time.created) if (updated !== 0) return updated return a.id < b.id ? -1 : a.id > b.id ? 1 : 0}
const isRootVisibleSession = (session: SessionInfo, directory: string) => pathKey(session.location.directory) === pathKey(directory) && !session.parentID && !session.time.archived
export const roots = (store: SessionStore) => (store.session ?? []).filter((session) => isRootVisibleSession(session, store.path.directory))
export const sortedRootSessions = (store: SessionStore, _now: number) => roots(store).sort(compareSessionTime)
export const latestRootSession = (stores: SessionStore[], _now: number) => stores.flatMap(roots).sort(compareSessionTime)[0]
export function hasProjectPermissions<T>( request: Record<string, T[] | undefined> | undefined, include: (item: T) => boolean = () => true,) { return Object.values(request ?? {}).some((list) => list?.some(include))}
export const childSessionOnPath = (sessions: SessionInfo[] | undefined, rootID: string, activeID?: string) => { if (!activeID || activeID === rootID) return const map = new Map((sessions ?? []).map((session) => [session.id, session])) let id = activeID
while (id) { const session = map.get(id) if (!session?.parentID) return if (session.parentID === rootID) return session id = session.parentID }}
export const displayName = (project: { name?: string; worktree: string }) => project.name || getFilename(project.worktree) || project.worktree
export function toggleHomeProjectSelection( current: HomeProjectSelection | undefined, server: ServerConnection.Key, directory: string,): HomeProjectSelection { if (current?.server === server && current.directory === directory) return { server } return { server, directory }}
export function closeHomeProject( selected: HomeProjectSelection | undefined, server: ServerConnection.Key, projects: { close: (directory: string) => void }, directory: string,) { projects.close(directory) if (selected?.server === server && selected.directory === directory) return { server } return selected}
export function homeProjectNavigation(active: ServerConnection.Key, server: ServerConnection.Key, href: string) { if (active === server) return { href } return { server, href }}
export function homeProjectDirectories(result: string | string[] | null) { if (!result) return [] return Array.isArray(result) ? result : [result]}
export function homeSessionServerStatus(active: boolean, status: () => { working: boolean; tint?: string }) { if (!active) return { working: false, tint: undefined } return status()}
const OPENCODE_PROJECT_ID = "4b0ea68d7af9a6031a7ffda7ad66e0cb83315750"
export function getProjectAvatarSource(id?: string, icon?: { color?: string; url?: string; override?: string }) { if (id === OPENCODE_PROJECT_ID) return "https://opencode.ai/favicon.svg" if (icon?.override) return icon.override if (icon?.color) return undefined return icon?.url}
export function projectForSession<T extends { id?: string; worktree: string; sandboxes?: string[] }>( session: SessionInfo, projects: T[], byID: Map<string, T> = new Map(projects.flatMap((project) => (project.id ? [[project.id, project] as const] : []))),) { const direct = byID.get(session.projectID) if (direct) return direct return projects.find((project) => isProjectDirectory(project, session.location.directory))}
export const errorMessage = (err: unknown, fallback: string) => { if (err && typeof err === "object" && "data" in err) { const data = (err as { data?: { message?: string } }).data if (data?.message) return data.message } if (err instanceof Error) return err.message return fallback}
export const effectiveWorkspaceOrder = (local: string, dirs: string[], persisted?: string[]) => { const root = pathKey(local) const live = new Map<string, string>()
for (const dir of dirs) { const key = pathKey(dir) if (key === root) continue if (!live.has(key)) live.set(key, dir) }
if (!persisted?.length) return [local, ...live.values()]
const result = [local] for (const dir of persisted) { const key = pathKey(dir) if (key === root) continue const match = live.get(key) if (!match) continue result.push(match) live.delete(key) }
return [...result, ...live.values()]}