import { trimString } from './lib/client.ts' import { type ClassifyOptions, classifySession, sessionPrs } from './lib/attention.ts' import { DEFAULT_GITHUB_ACCOUNT, fetchAllMessages, fetchGithubPr, mapConcurrent, withRetry, } from './lib/attention-runtime.ts' import { archiveSession, getSession } from './sessions.ts' export type ArchiveStaleParams = Pick< ClassifyOptions, 'decisionKeywords' | 'ownerNames' > & { /** Session ids (`session_id`) to re-check and archive. Max 100. */ devinIds: string[] /** Default true: only report what would be archived. Pass `false` to archive. */ dryRun?: boolean orgId?: string /** GitHub account alias for `@kentcdodds/github/request` (default `kent`). */ githubAccount?: string } export type ArchiveStaleItem = { devinId: string title: string | null url: string | null prs: Array<{ url: string; state: string }> } /** * Re-check that each session is still `stale` (every PR merged or closed, * session idle, nothing waiting on a human) and archive it. * * Defaults to `dryRun: true`, which only returns `wouldArchive`. Archiving * calls `POST /v3/organizations/{org}/sessions/{devinId}/archive` and only * happens with `dryRun: false`. Sessions that are no longer stale (for * example a new question arrived) are returned in `skipped` with the reason. * * @example * import archiveStale from 'kody:@kentcdodds/devin/sessions/archive-stale' * const preview = await archiveStale({ devinIds: ['abc123'] }) * // => { dryRun: true, wouldArchive: [...], archived: [], skipped: [...] } */ export async function archiveStale(params: ArchiveStaleParams) { const ids = [ ...new Set((params?.devinIds ?? []).map((id) => trimString(id)).filter(Boolean)), ] if (!ids.length) throw new Error('archiveStale requires devinIds: string[]') if (ids.length > 100) throw new Error('archiveStale takes at most 100 devinIds per call') const dryRun = params.dryRun !== false const account = trimString(params.githubAccount) || DEFAULT_GITHUB_ACCOUNT const wouldArchive: ArchiveStaleItem[] = [] const archived: ArchiveStaleItem[] = [] const skipped: Array<{ devinId: string; reason: string }> = [] await mapConcurrent(ids, 8, async (devinId) => { try { const session = await withRetry(() => getSession({ devinId, orgId: params.orgId })) if (session.is_archived) { skipped.push({ devinId, reason: 'already_archived' }) return } const messages = await fetchAllMessages(devinId, params.orgId) const prs = await Promise.all( sessionPrs(session).map((pr) => pr.state === 'unknown' ? fetchGithubPr(pr, account).catch(() => pr) : pr, ), ) const c = classifySession(session, messages, { decisionKeywords: params.decisionKeywords, ownerNames: params.ownerNames, includeStale: true, prs, }) if (c.reason !== 'stale') { skipped.push({ devinId, reason: c.reason ? `now_${c.reason}` : c.activelyWorking ? 'actively_working' : 'not_stale', }) return } const item: ArchiveStaleItem = { devinId, title: session.title ?? null, url: session.url ?? null, prs: c.prs.map((pr) => ({ url: pr.url, state: pr.state })), } if (dryRun) { wouldArchive.push(item) return } await withRetry(() => archiveSession({ devinId, orgId: params.orgId })) archived.push(item) } catch (error) { skipped.push({ devinId, reason: `error: ${(error as Error).message}` }) } }) return { dryRun, wouldArchive, archived, skipped } } export default archiveStale