garage44.eu/jeroen/midas

logclone: git clone https://garage44.eu/jeroen/midas.git

.. /web/src/lib/names.ts

raw

import {$s} from '@/lib/store'

/**
 * What to call a session on the page: the start of its first prompt (its
 * label), else its branch, else its worktree's name — with a link to it on
 * claude.ai when it is a Remote Control session. A landing gate's lease
 * (`gate-<worktree>`) is its session's.
 */
export interface Name {
    title: string
    /** The next most telling name, under the title. */
    sub: string | null
    link: string | null
    owner: string
}

export function nameOf(owner: string): Name {
    const gate = owner.startsWith('gate-')
    const base = gate ? owner.slice(5) : owner
    const n = $s.names[base]
    const branch = n?.branch?.replace(/^worktree-/, '') ?? null
    const id = base.startsWith('bridge-cse_') ? base.slice('bridge-cse_'.length) : null
    // A session the hub runs (`session-s3`) is followed here, on its Sessions page; a Remote Control one on claude.ai.
    const hosted = base.match(/^session-(s\d+)$/)?.[1]
    const link = hosted
        ? `/sessions/${hosted}`
        : (n?.link ?? (id && /^[A-Za-z0-9]+$/.test(id) ? `https://claude.ai/code/session_${id}` : null))
    // A worktree named for its session is no name at all once there is a better one.
    const branchName = branch && branch !== base ? branch : null
    const title = n?.label ?? branchName ?? base
    const sub = n?.label ? (branchName ?? base) : branchName ? base : null
    return {title: gate ? `${title} (landing gate)` : title, sub, link, owner}
}

/**
 * Who said something, for a person to read: `hub` is midas itself, `web:<account>`
 * a person on this page, `<caller>:<session>` (how the hub records a change) the
 * session, and anything else a session by its name.
 */
export function speaker(actor: string): Name {
    if (actor === 'hub') return {title: 'midas', sub: null, link: null, owner: actor}
    if (actor === 'you') return {title: 'you', sub: null, link: null, owner: actor}
    const i = actor.indexOf(':')
    const [caller, rest] = i >= 0 ? [actor.slice(0, i), actor.slice(i + 1)] : ['', actor]
    // A person, or a caller with no session behind it (`workstation:midas`, a checkout's name).
    if (caller === 'web' || (caller && !/[-_0-9]/.test(rest))) return {title: rest || caller, sub: null, link: null, owner: actor}
    return nameOf(rest)
}