garage44.eu/jeroen/midas

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

.. /web/src/lib/mentions.ts

raw

/**
 * What a question says about other sessions: their midas names (a worktree,
 * `bridge-cse_…`, `session-s3`, a hosted id `s3`) and open ends (`t7`) that
 * exist, and whether it speaks of another session at all ("the other
 * session", "another agent"). The page marks such a question as one about
 * working with others, and links what it names.
 */
export interface Mentions {
    sessions: string[]
    todos: string[]
    /** It speaks of another session or agent, named or not. */
    others: boolean
}

export function mentions(text: string, known: {sessions: string[]; hosted: string[]; todos: string[]; self: string}): Mentions {
    const words = new Set(text.match(/[A-Za-z0-9_-]+/g) ?? [])
    const self = known.self.replace(/^gate-/, '')
    const sessions = new Set<string>()
    for (const s of known.sessions) {
        const base = s.replace(/^gate-/, '')
        // Only a name that cannot be an ordinary word: a worktree called "midas"
        // would otherwise be every question that mentions midas.
        if (!/[-_0-9]/.test(base)) continue
        if (base !== self && (words.has(s) || words.has(base))) sessions.add(base)
    }
    // A hosted id said bare (s3) or as its worktree (session-s3).
    for (const id of known.hosted) {
        const owner = `session-${id}`
        if (owner !== self && (words.has(id) || words.has(owner))) sessions.add(owner)
    }
    const todos = known.todos.filter((t) => words.has(t))
    const others = /\b(other|another)\s+(session|agent)s?\b|\b(sessions|agents)\b.*\b(other|both)\b/i.test(text)
    return {sessions: [...sessions], todos, others: others || sessions.size > 0 || todos.length > 0}
}

/** An option's label without its "(Recommended)", and whether it had one. */
export function recommended(label: string): {label: string; rec: boolean} {
    const m = label.match(/^(.*?)\s*\(recommended\)\s*$/i)
    return m ? {label: m[1], rec: true} : {label, rec: false}
}