← Public packages
@kody/kody-factory
Forkable multi-project software factory: signals, priority queue, project UI, sweep job. Agent runners and Sentry reach it by events.
Files
/**
* Factory card references in pull request text, GitHub style.
*
* A reference is a card link (`https://<factory host>/c/<id>`, or the older
* `/cards/<id>` and `/work?card=<id>` forms) or the short form
* `factory:<id>`. A closing keyword right before it (close, closes, closed,
* fix, fixes, fixed, resolve, resolves, resolved, with an optional colon)
* closes the card when the PR merges; any other reference only links the PR.
* Text inside HTML comments, fenced code blocks, and inline code is ignored,
* so PR template examples never match.
*/
export type CardRef = { cardId: string; closes: boolean; keyword: string | null; text: string }
export const CLOSING_KEYWORDS = ['close', 'closes', 'closed', 'fix', 'fixes', 'fixed', 'resolve', 'resolves', 'resolved'] as const
const ID = '(wi_[A-Za-z0-9_-]{3,60})'
const URL_REF = `https?://[A-Za-z0-9.-]+(?::\\d+)?(?:/[A-Za-z0-9._~-]+)*?/(?:c|cards)/${ID}(?![A-Za-z0-9_-])`
const WORK_REF = `https?://[A-Za-z0-9.-]+(?::\\d+)?(?:/[A-Za-z0-9._~-]+)*?/work\\?(?:[^\\s#]*&)?card=${ID}(?![A-Za-z0-9_-])`
const SHORT_REF = `(?<![A-Za-z0-9_/:.-])factory:${ID}(?![A-Za-z0-9_-])`
const KEYWORD = `(?:\\b(${CLOSING_KEYWORDS.join('|')})\\b:?[ \\t]+)?`
const REF_RE = new RegExp(`${KEYWORD}(?:${URL_REF}|${WORK_REF}|${SHORT_REF})`, 'gi')
/** PR text with comments and code removed (same length is not needed). */
export function stripNonProse(body: string): string {
return body
.replace(/<!--[\s\S]*?(?:-->|$)/g, ' ')
.replace(/(^|\n)[ \t]*(```|~~~)[\s\S]*?(?:\n[ \t]*\2[^\n]*|$)/g, '$1 ')
.replace(/`[^`\n]*`/g, ' ')
}
/**
* Every distinct card referenced in `body`. A card referenced both with and
* without a keyword counts as closing.
*/
export function parseCardRefs(body: unknown): CardRef[] {
if (typeof body !== 'string' || !body) return []
const text = stripNonProse(body.slice(0, 65536))
const byId = new Map<string, CardRef>()
for (const m of text.matchAll(REF_RE)) {
const keyword = m[1] ? m[1].toLowerCase() : null
const cardId = m[2] ?? m[3] ?? m[4]
if (!cardId) continue
const prev = byId.get(cardId)
if (prev && (prev.closes || !keyword)) continue
byId.set(cardId, { cardId, closes: Boolean(keyword), keyword, text: m[0].trim().slice(0, 200) })
if (byId.size >= 50) break
}
return [...byId.values()]
}