| 1 | /** |
| 2 | * People on the site: the directory (people only; agents are on their |
| 3 | * teams and under Agents), profiles, and teams of any mix. |
| 4 | * Types only from the contracts, so this file runs under `node --test`. |
| 5 | */ |
| 6 | import type { DirectoryPerson, DirectoryTeam, PeopleDirectory, TeamLead, WorkspaceAgent } from "@g1t/contracts"; |
| 7 | |
| 8 | /** An agent as People's pages need it. */ |
| 9 | export type PeopleAgent = Pick< |
| 10 | WorkspaceAgent, |
| 11 | "id" | "handle" | "display_name" | "avatar" | "avatar_seed" | "look" | "title" | "role" | "status" | "responsibilities" | "builtin" | "spent_month_micros" |
| 12 | >; |
| 13 | |
| 14 | /** Just what People's pages read of an agent, so loaders send no more. */ |
| 15 | export function peopleAgent(agent: PeopleAgent): PeopleAgent { |
| 16 | const { id, handle, display_name, avatar, avatar_seed, look, title, role, status, responsibilities, builtin, spent_month_micros } = agent; |
| 17 | return { id, handle, display_name, avatar, avatar_seed, look, title, role, status, responsibilities, builtin, spent_month_micros }; |
| 18 | } |
| 19 | |
| 20 | /** Where a person's profile is. */ |
| 21 | export function personPath(workspace: string, username: string): string { |
| 22 | return `/${workspace}/-/people/${username}`; |
| 23 | } |
| 24 | |
| 25 | /** Where an agent's profile is: its page in Agents. People has none; its old address there redirects. */ |
| 26 | export function agentPath(workspace: string, handle: string): string { |
| 27 | return `/${workspace}/-/agents/${handle}`; |
| 28 | } |
| 29 | |
| 30 | /** The agents on a team, in the order they were added, as found among `agents`. */ |
| 31 | export function agentsOn<A extends Pick<PeopleAgent, "id">>(team: Pick<DirectoryTeam, "agent_ids">, agents: readonly A[]): A[] { |
| 32 | const byId = new Map(agents.map((agent) => [agent.id, agent])); |
| 33 | return [...new Set(team.agent_ids)].map((id) => byId.get(id)).filter((agent): agent is A => !!agent); |
| 34 | } |
| 35 | |
| 36 | /** The teams a person is on. */ |
| 37 | export function teamsOfPerson<T extends Pick<DirectoryTeam, "people">>(teams: readonly T[], username: string): T[] { |
| 38 | return teams.filter((team) => team.people.some((person) => person.username === username)); |
| 39 | } |
| 40 | |
| 41 | /** The teams an agent is on: its memberships, as a person's are. */ |
| 42 | export function teamsOfAgent<T extends Pick<DirectoryTeam, "agent_ids">>(teams: readonly T[], agent: Pick<PeopleAgent, "id">): T[] { |
| 43 | return teams.filter((team) => team.agent_ids.includes(agent.id)); |
| 44 | } |
| 45 | |
| 46 | /** A team an agent is on, as the Agents sidebar groups by it. */ |
| 47 | export type AgentTeamRef = { slug: string; name: string }; |
| 48 | |
| 49 | /** Each agent's teams, by agent id, from teams with the agents on them; the teams by name. */ |
| 50 | export function teamsByAgent(teams: readonly { slug: string; name: string; agent_ids: readonly string[] }[]): Record<string, AgentTeamRef[]> { |
| 51 | const out: Record<string, AgentTeamRef[]> = {}; |
| 52 | for (const team of [...teams].sort((a, b) => a.name.localeCompare(b.name))) { |
| 53 | for (const id of new Set(team.agent_ids)) (out[id] ??= []).push({ slug: team.slug, name: team.name }); |
| 54 | } |
| 55 | return out; |
| 56 | } |
| 57 | |
| 58 | /** The label for agents on no team, wherever agents are grouped by team. */ |
| 59 | export const NO_TEAM = "Not on a team"; |
| 60 | |
| 61 | /** |
| 62 | * Agents grouped by the teams they are on, as the Agents sidebar shows |
| 63 | * them: each team by name, an agent on two teams under both, and agents on |
| 64 | * none last, under "Not on a team". Each group's agents by name. |
| 65 | */ |
| 66 | export function groupByTeam<A extends { id: string; display_name: string }>(agents: readonly A[], teamsOf: Record<string, readonly AgentTeamRef[]>): { key: string; label: string; agents: A[] }[] { |
| 67 | const groups = new Map<string, { key: string; label: string; agents: A[] }>(); |
| 68 | const none: A[] = []; |
| 69 | for (const agent of agents) { |
| 70 | const teams = teamsOf[agent.id] ?? []; |
| 71 | if (!teams.length) none.push(agent); |
| 72 | for (const team of teams) { |
| 73 | const group = groups.get(team.slug) ?? { key: team.slug, label: team.name, agents: [] }; |
| 74 | if (!group.agents.includes(agent)) group.agents.push(agent); |
| 75 | groups.set(team.slug, group); |
| 76 | } |
| 77 | } |
| 78 | const byName = (a: A, b: A) => a.display_name.localeCompare(b.display_name); |
| 79 | const out = [...groups.values()].sort((a, b) => a.label.localeCompare(b.label)).map((group) => ({ ...group, agents: group.agents.sort(byName) })); |
| 80 | if (none.length) out.push({ key: "", label: NO_TEAM, agents: none.sort(byName) }); |
| 81 | return out; |
| 82 | } |
| 83 | |
| 84 | /** Whether `lead` is this person or this agent. */ |
| 85 | export function leads(lead: TeamLead | null, who: { username: string } | { id: string }): boolean { |
| 86 | if (!lead) return false; |
| 87 | if ("username" in who) return lead.kind === "user" && lead.username === who.username; |
| 88 | return lead.kind === "agent" && lead.agent_id === who.id; |
| 89 | } |
| 90 | |
| 91 | /** A person's name as pages show it. */ |
| 92 | export function personName(person: Pick<DirectoryPerson, "username" | "name" | "display_username">): string { |
| 93 | return person.name?.trim() || person.display_username || person.username; |
| 94 | } |
| 95 | |
| 96 | /** Whether every word of `query` is somewhere in `fields`. */ |
| 97 | export function matches(query: string, fields: readonly (string | null | undefined)[]): boolean { |
| 98 | const words = query.toLowerCase().split(/\s+/).filter(Boolean); |
| 99 | if (!words.length) return true; |
| 100 | const text = fields.filter(Boolean).join(" ").toLowerCase(); |
| 101 | return words.every((word) => text.includes(word)); |
| 102 | } |
| 103 | |
| 104 | /** One card in the directory: a person and their teams. */ |
| 105 | export type DirectoryEntry = { person: DirectoryPerson; teams: DirectoryTeam[] }; |
| 106 | |
| 107 | /** |
| 108 | * The people matching `query`, in the directory's order (by name). A |
| 109 | * person matches on their name, username, title, teams, what they own, |
| 110 | * where they are and their bio. The directory is people only: the |
| 111 | * workspace's agents are listed under Agents and on the teams they are |
| 112 | * on, never here, so a query that would only match an agent finds no one. |
| 113 | */ |
| 114 | export function directoryEntries(directory: PeopleDirectory, query: string): DirectoryEntry[] { |
| 115 | const out: DirectoryEntry[] = []; |
| 116 | for (const person of directory.people) { |
| 117 | const teams = teamsOfPerson(directory.teams, person.username); |
| 118 | const fields = [person.username, person.name, person.title, person.location, person.bio, ...person.owns, ...teams.map((team) => team.name), person.role === "owner" ? "owner" : null]; |
| 119 | if (matches(query, fields)) out.push({ person, teams }); |
| 120 | } |
| 121 | return out; |
| 122 | } |
| 123 | |
| 124 | /** What someone owns, from a form's text: one per line or comma. */ |
| 125 | export function ownsFromText(text: string): string[] { |
| 126 | return text |
| 127 | .split(/[\n,]+/) |
| 128 | .map((phrase) => phrase.trim()) |
| 129 | .filter(Boolean); |
| 130 | } |
| 131 | |
| 132 | /** A monthly budget in dollars from a form, as micro-dollars; 0 for none; null when it isn't a number. */ |
| 133 | export function budgetFromText(text: string): number | null { |
| 134 | const value = text.trim().replace(/^\$/, "").replace(/,/g, ""); |
| 135 | if (!value) return 0; |
| 136 | const dollars = Number(value); |
| 137 | if (!Number.isFinite(dollars) || dollars < 0) return null; |
| 138 | return Math.round(dollars * 1_000_000); |
| 139 | } |
| 140 | |
| 141 | /** The access to Code a member has, in words: their role, the base permission, and the teams that add to it. */ |
| 142 | export function codeAccessWords( |
| 143 | person: Pick<DirectoryPerson, "role">, |
| 144 | base: PeopleDirectory["base_permission"], |
| 145 | teams: readonly Pick<DirectoryTeam, "name" | "repos_count">[], |
| 146 | ): { summary: string; through: string[] } { |
| 147 | const summary = |
| 148 | person.role === "owner" |
| 149 | ? "Admin on every repository, as an owner" |
| 150 | : base === "none" |
| 151 | ? "Only repositories they're given a role on" |
| 152 | : `${base[0]!.toUpperCase()}${base.slice(1)} on every repository, the workspace's base permission`; |
| 153 | const through = teams |
| 154 | .filter((team) => team.repos_count > 0) |
| 155 | .map((team) => `Roles on ${team.repos_count} ${team.repos_count === 1 ? "repository" : "repositories"} through ${team.name}`); |
| 156 | return { summary, through }; |
| 157 | } |
| 158 | |
| 159 | /** The part of what an agent is told that is about one team: its heading and what follows, up to the next. */ |
| 160 | export function teamBlock(text: string | null, teamName: string): string | null { |
| 161 | if (!text) return null; |
| 162 | const blocks = text.split(/\n(?=### )/); |
| 163 | const found = blocks.find((block) => block.startsWith(`### ${teamName}\n`) || block.trim() === `### ${teamName}`); |
| 164 | return found ? found.trim() : null; |
| 165 | } |
| 166 | |
| 167 | /** A team an agent is on, as its profile lists it. */ |
| 168 | export type AgentTeamRow = { |
| 169 | slug: string; |
| 170 | name: string; |
| 171 | people: number; |
| 172 | agents: number; |
| 173 | lead: boolean; |
| 174 | /** Whether the viewer may take it off (owners and the team's maintainers). */ |
| 175 | can_manage: boolean; |
| 176 | }; |
| 177 | |
| 178 | export type AgentTeams = { |
| 179 | on: AgentTeamRow[]; |
| 180 | /** Teams it isn't on that the viewer manages, to add it to. */ |
| 181 | addable: { slug: string; name: string }[]; |
| 182 | }; |
| 183 | |
| 184 | /** What adding an agent to a team, or taking it off, answers. */ |
| 185 | export type TeamChange = { intent: "join-team" | "leave-team"; team: string; error: string | null }; |