An open-source, faster Devin alternative
TypeScript
web: board ordering, filters and card pieces for tracked items
Igor Zalutski via Computer074206881175
diff --git a/web/src/managed-agents/work/board.test.ts b/web/src/managed-agents/work/board.test.tsnew file mode 100644index 00000000..32172b06--- /dev/null+++ b/web/src/managed-agents/work/board.test.ts@@ -0,0 +1,152 @@+import { describe, expect, it } from 'vitest'+import {+ attentionLabel,+ closedShown,+ indexIsOld,+ inProgress,+ matches,+ presentBody,+ pullLabel,+ readyToClose,+ resolveAgainst,+ staleDays,+} from './board'+import { makeItem, minutesAgo, NOW } from './fixtures'++const DAY = 24 * 60++describe('board columns', () => {+ it('splits open items by the readiness the API gives', () => {+ const items = [+ makeItem({ number: 1 }),+ makeItem({ number: 2, readyToClose: true }),+ makeItem({ number: 3, stage: 'closed', closedAt: minutesAgo(10) }),+ ]+ expect(inProgress(items).map((i) => i.number)).toEqual([1])+ expect(readyToClose(items).map((i) => i.number)).toEqual([2])+ })++ it('puts the cards that need you first, then the latest change', () => {+ const items = [+ makeItem({ number: 1, updatedAt: minutesAgo(5) }),+ makeItem({+ number: 2,+ updatedAt: minutesAgo(500),+ attention: { state: 'needs_you', name: null, since: minutesAgo(500) },+ }),+ makeItem({+ number: 3,+ updatedAt: minutesAgo(1),+ attention: { state: 'waiting_on', name: 'Maya', since: minutesAgo(1) },+ }),+ ]+ expect(inProgress(items).map((i) => i.number)).toEqual([2, 3, 1])+ })++ it('shows closed items of the last seven days unless older are asked for', () => {+ const items = [+ makeItem({ number: 1, stage: 'closed', closedAt: minutesAgo(2 * DAY) }),+ makeItem({ number: 2, stage: 'closed', closedAt: minutesAgo(9 * DAY) }),+ makeItem({ number: 3, stage: 'closed', closedAt: null }),+ ]+ expect(closedShown(items, false, NOW).map((i) => i.number)).toEqual([1])+ expect(closedShown(items, true, NOW).map((i) => i.number)).toEqual([+ 1, 2, 3,+ ])+ })+})++describe('filters', () => {+ const item = makeItem({+ title: 'Usage billing',+ stateLine: 'Price table left',+ tags: ['billing'],+ mine: true,+ })+ it('keeps Mine to what the API marks mine', () => {+ expect(matches(item, { tab: 'mine', q: '', tag: null })).toBe(true)+ expect(+ matches({ ...item, mine: false }, { tab: 'mine', q: '', tag: null }),+ ).toBe(false)+ expect(+ matches({ ...item, mine: false }, { tab: 'everyone', q: '', tag: null }),+ ).toBe(true)+ })+ it('matches text in the title or state line, and the tag exactly', () => {+ const f = { tab: 'everyone' as const, tag: null }+ expect(matches(item, { ...f, q: 'PRICE' })).toBe(true)+ expect(matches(item, { ...f, q: 'nothing' })).toBe(false)+ expect(matches(item, { ...f, q: '', tag: 'billing' })).toBe(true)+ expect(matches(item, { ...f, q: '', tag: 'bill' })).toBe(false)+ })+})++describe('card wording', () => {+ it('colours only "needs you"', () => {+ const ago = () => '3h'+ expect(+ attentionLabel(+ makeItem({+ attention: { state: 'needs_you', name: null, since: minutesAgo(1) },+ }),+ ago,+ ),+ ).toEqual({ text: 'needs you', needsYou: true })+ expect(+ attentionLabel(+ makeItem({+ attention: {+ state: 'waiting_on',+ name: 'Maya',+ since: minutesAgo(1),+ },+ }),+ ago,+ ),+ ).toEqual({ text: 'waiting on Maya · 3h', needsYou: false })+ expect(attentionLabel(makeItem(), ago)).toBeNull()+ })++ it('marks an open card stale after two weeks without a change', () => {+ expect(staleDays(makeItem({ updatedAt: minutesAgo(20 * DAY) }), NOW)).toBe(+ 20,+ )+ expect(staleDays(makeItem({ updatedAt: minutesAgo(DAY) }), NOW)).toBeNull()+ })++ it('names a pull request by its number', () => {+ expect(+ pullLabel({+ url: 'https://github.com/acme/app/pull/412',+ state: 'open',+ title: null,+ }),+ ).toBe('#412')+ })++ it('says the index is old only after a few minutes', () => {+ expect(indexIsOld(null, NOW)).toBe(false)+ expect(indexIsOld(minutesAgo(1), NOW)).toBe(false)+ expect(indexIsOld(minutesAgo(30), NOW)).toBe(true)+ })+})++describe('the doc as shown', () => {+ it('drops the frontmatter and the leading title', () => {+ expect(+ presentBody('---\nnumber: 1\ntitle: X\n---\n\n# X\n\n## Why\n\nBecause.'),+ ).toBe('## Why\n\nBecause.')+ expect(presentBody('Just text.')).toBe('Just text.')+ })++ it('resolves relative links against the doc on GitHub', () => {+ const base = 'https://github.com/acme/knowledge/blob/main/work/001-x.md'+ expect(resolveAgainst('../design/a.md', base)).toBe(+ 'https://github.com/acme/knowledge/blob/main/design/a.md',+ )+ expect(resolveAgainst('https://example.com', base)).toBe(+ 'https://example.com',+ )+ expect(resolveAgainst('#why', base)).toBe('#why')+ })+})diff --git a/web/src/managed-agents/work/board.ts b/web/src/managed-agents/work/board.tsnew file mode 100644index 00000000..66cd5c08--- /dev/null+++ b/web/src/managed-agents/work/board.ts@@ -0,0 +1,141 @@+import type { Item, PullRequest, PullState, Repository } from './api'++// How the board lays out what the API returns (ux-spec 4.1). Nothing here+// decides a stage, readiness or attention: those are the item's own fields.+// This file only filters, orders and words them.++export type Tab = 'mine' | 'everyone'++// The board and the item page poll this often (tech design 3.11).+export const OPEN_POLL_MS = 15_000+export const STALE_DAYS = 14+export const CLOSED_RECENT_DAYS = 7+const DAY = 86_400_000++export type Filters = { tab: Tab; q: string; tag: string | null }++export function matches(item: Item, f: Filters): boolean {+ if (f.tab === 'mine' && !item.mine) return false+ if (f.tag && !item.tags.includes(f.tag)) return false+ const q = f.q.trim().toLowerCase()+ if (!q) return true+ return (+ item.title.toLowerCase().includes(q) ||+ (item.stateLine ?? '').toLowerCase().includes(q)+ )+}++const needsYouFirst = (a: Item, b: Item) =>+ (a.attention?.state === 'needs_you' ? 0 : 1) -+ (b.attention?.state === 'needs_you' ? 0 : 1) ||+ b.updatedAt.localeCompare(a.updatedAt)++/** In progress: the cards that need the viewer first, then by last change. */+export function inProgress(items: Item[]): Item[] {+ return items+ .filter((i) => i.stage === 'open' && !i.readyToClose)+ .sort(needsYouFirst)+}++/** Ready to close: in the same order. */+export function readyToClose(items: Item[]): Item[] {+ return items+ .filter((i) => i.stage === 'open' && i.readyToClose)+ .sort(needsYouFirst)+}++/** Closed in the last seven days, unless older ones are asked for. */+export function closedShown(+ items: Item[],+ showOlder: boolean,+ now = Date.now(),+): Item[] {+ return items+ .filter((i) => i.stage === 'closed')+ .filter(+ (i) =>+ showOlder ||+ (i.closedAt !== null &&+ now - new Date(i.closedAt).getTime() <= CLOSED_RECENT_DAYS * DAY),+ )+}++/** Days since the last change, when that makes the card stale; else null. */+export function staleDays(item: Item, now = Date.now()): number | null {+ if (item.stage !== 'open') return null+ const days = Math.floor((now - new Date(item.updatedAt).getTime()) / DAY)+ return days >= STALE_DAYS ? days : null+}++/** The one attention line on a card, and whether it is the coloured one. */+export function attentionLabel(+ item: Item,+ ago: (iso: string) => string,+): { text: string; needsYou: boolean } | null {+ const a = item.attention+ if (!a) return null+ switch (a.state) {+ case 'needs_you':+ return { text: 'needs you', needsYou: true }+ case 'waiting_on':+ return {+ text: `waiting on ${a.name ?? 'someone'} · ${ago(a.since)}`,+ needsYou: false,+ }+ case 'working':+ return { text: 'agent working', needsYou: false }+ }+}++/** "#412" for a GitHub pull request link, else the link itself. */+export function pullLabel(pr: PullRequest): string {+ const m = pr.url.match(/\/pull\/(\d+)/)+ return m ? `#${m[1]}` : pr.url+}++export function personName(p: { name: string | null; email: string }) {+ return p.name?.trim() || p.email+}++export function plural(n: number, one: string, many = `${one}s`) {+ return `${n} ${n === 1 ? one : many}`+}++export function pullStateWord(state: PullState) {+ return state ?? 'state unknown'+}++export function repositoryName(r: Repository) {+ return `${r.owner}/${r.name}`+}++// The index is this far behind before the board says how old it is.+const STALE_INDEX_MS = 5 * 60_000++/** True when the index the answer was read from is a while old. */+export function indexIsOld(asOf: string | null, now = Date.now()) {+ return asOf !== null && now - new Date(asOf).getTime() > STALE_INDEX_MS+}++/**+ * The doc as a reader sees it: without its frontmatter (the header and the+ * aside show those fields) and without a leading title (the header has it).+ */+export function presentBody(body: string): string {+ let s = body.replace(/^\uFEFF/, '')+ const fm = s.match(/^---\r?\n[\s\S]*?\r?\n---[ \t]*(\r?\n|$)/)+ if (fm) s = s.slice(fm[0].length)+ s = s.replace(/^\s*#[ \t]+[^\n]*(\n|$)/, '')+ return s.trimStart()+}++/** A link in the doc relative to the doc resolves to its place on GitHub. */+export function resolveAgainst(url: string, base: string): string {+ if (!url || url.startsWith('#') || /^[a-z][a-z0-9+.-]*:/i.test(url))+ return url+ try {+ return new URL(url, base).toString()+ } catch {+ return url+ }+}diff --git a/web/src/managed-agents/work/fixtures.ts b/web/src/managed-agents/work/fixtures.tsnew file mode 100644index 00000000..7f73461f--- /dev/null+++ b/web/src/managed-agents/work/fixtures.ts@@ -0,0 +1,35 @@+import type { Item } from './api'++// Test data for the work screens, in the API's shape (tech design 3.10).++// The real clock, so the screens' relative times read as written.+export const NOW = Date.now()+export const minutesAgo = (min: number) =>+ new Date(NOW - min * 60_000).toISOString()++export function makeItem(over: Partial<Item> = {}): Item {+ return {+ number: 1,+ stage: 'open',+ path: 'work/001-thing.md',+ url: 'https://github.com/acme/knowledge/blob/main/work/001-thing.md',+ title: 'A thing',+ stateLine: null,+ tags: [],+ owner: null,+ author: null,+ mine: false,+ left: 1,+ readyToClose: false,+ warnings: [],+ pullRequests: [],+ attention: null,+ sessions: [],+ unpublished: false,+ updatedAt: minutesAgo(60),+ createdAt: null,+ closedAt: null,+ closedBy: null,+ ...over,+ }+}diff --git a/web/src/managed-agents/work/parts.tsx b/web/src/managed-agents/work/parts.tsxnew file mode 100644index 00000000..7b6e52eb--- /dev/null+++ b/web/src/managed-agents/work/parts.tsx@@ -0,0 +1,167 @@+import type { ReactNode } from 'react'+import { AlertTriangle, CloudOff } from 'lucide-react'+import { cn } from '@/lib/utils'+import { timeAgo } from '../computer-sessions/transcript'+import type { Item, Person, PullRequest, PullState } from './api'+import { attentionLabel, personName, pullLabel, pullStateWord } from './board'++// Small pieces shared by the board and the item page.++export function Avatar({+ person,+ className,+}: {+ person: Person+ className?: string+}) {+ const name = personName(person)+ return (+ <span+ title={name}+ className={cn(+ 'bg-secondary text-muted-foreground inline-flex size-5 shrink-0 items-center justify-center rounded-full text-[10px] font-medium uppercase',+ className,+ )}+ >+ {name.charAt(0)}+ </span>+ )+}++export function Tag({+ tag,+ onClick,+ active,+}: {+ tag: string+ onClick?: () => void+ active?: boolean+}) {+ const className = cn(+ 'bg-muted text-muted-foreground relative z-10 rounded px-1.5 py-0.5 font-mono text-[11px] leading-4',+ onClick && 'hover:text-foreground cursor-pointer transition-colors',+ active && 'bg-foreground text-background hover:text-background',+ )+ if (!onClick) return <span className={className}>{tag}</span>+ return (+ <button+ type="button"+ className={className}+ title={`Show items tagged ${tag}`}+ onClick={onClick}+ >+ {tag}+ </button>+ )+}++const PULL_DOT: Record<NonNullable<PullState>, string> = {+ open: 'bg-status-running',+ draft: 'bg-status-stopped',+ merged: 'bg-status-hibernated',+ closed: 'bg-status-error',+}++export function PullDot({ state }: { state: PullState }) {+ return (+ <span+ aria-hidden+ className={cn(+ 'inline-block size-1.5 shrink-0 rounded-full',+ state ? PULL_DOT[state] : 'border-muted-foreground/60 border',+ )}+ />+ )+}++/** A pull request as a quiet link with its state; always to GitHub. */+export function PullChip({ pr }: { pr: PullRequest }) {+ return (+ <a+ href={pr.url}+ target="_blank"+ rel="noreferrer"+ title={[pr.title, pullStateWord(pr.state)].filter(Boolean).join(' · ')}+ className="text-muted-foreground hover:text-foreground relative z-10 inline-flex items-center gap-1.5 rounded-full border px-2 py-0.5 text-xs tabular-nums transition-colors"+ >+ <PullDot state={pr.state} />+ {pullLabel(pr)}+ {pr.state === 'draft' ? <span>· draft</span> : null}+ </a>+ )+}++/** The one attention line: only "needs you" is coloured. */+export function AttentionMark({+ item,+ className,+}: {+ item: Item+ className?: string+}) {+ const label = attentionLabel(item, (iso) => timeAgo(iso))+ if (!label) return null+ return (+ <span+ className={cn(+ 'inline-flex min-w-0 items-center gap-1.5 text-xs',+ label.needsYou+ ? 'font-medium text-amber-700 dark:text-amber-400'+ : 'text-muted-foreground',+ className,+ )}+ >+ <span+ aria-hidden+ className={cn(+ 'inline-block size-1.5 shrink-0 rounded-full',+ label.needsYou+ ? 'bg-amber-500'+ : item.attention?.state === 'working'+ ? 'bg-status-running/60 animate-pulse'+ : 'bg-muted-foreground/50',+ )}+ />+ <span className="truncate">{label.text}</span>+ </span>+ )+}++/** What the parser could not read in the doc: a small mark, the details on hover. */+export function WarningMark({ warnings }: { warnings: string[] }) {+ if (!warnings.length) return null+ const text = `The doc has ${warnings.length === 1 ? 'a problem' : `${warnings.length} problems`}: ${warnings.join('; ')}`+ return (+ <span+ title={text}+ aria-label={text}+ role="img"+ className="text-status-pending relative z-10 inline-flex"+ >+ <AlertTriangle className="size-3.5" aria-hidden />+ </span>+ )+}++export function UnpublishedMark({ className }: { className?: string }) {+ return (+ <span+ title="A session has changes to this doc that are not on GitHub yet"+ className={cn(+ 'text-status-pending inline-flex items-center gap-1 text-xs',+ className,+ )}+ >+ <CloudOff className="size-3.5" aria-hidden />+ unpublished changes+ </span>+ )+}++export function SectionTitle({ children }: { children: ReactNode }) {+ return (+ <h3 className="text-muted-foreground mb-2 text-[11px] font-medium tracking-wide uppercase">+ {children}+ </h3>+ )+}