import { afterCardsCreated } from '../cards/lifecycle.ts' import { schemeTags } from '../cards/tags.ts' import { loadJson, saveJson, storageKeys } from '../lib/storage.ts' import { emptyLedger, newWorkItemId, nowIso, type Ledger, type WorkItem, type WorkItemPriority, } from '../core/work-item.ts' import { rankOf } from '../core/priority-queue.ts' import { loadProjects, primaryRepoRef } from '../projects/config.ts' /** * Create a manual work item / intent intake. * * @param input - Title, projectId, and optional fields * @returns Created work item * * @example * import create from 'kody:@kody/kody-factory/create' * await create({ projectId: 'kody', title: 'Build X', dryRun: true }) */ export default async function create(input: { projectId: string title: string body?: string priority?: WorkItemPriority needsKent?: boolean labels?: string[] repoRef?: string dryRun?: boolean shadow?: boolean /** Client-generated key; a retry with the same key returns the first item instead of a duplicate. */ idempotencyKey?: string /** Import or backfill: emit card.created but skip push and card-route wakes. */ quiet?: boolean /** false: skip the card-route wake (the caller wakes its own role). Push rules still apply. */ routes?: boolean }) { if (!input || typeof input !== 'object') throw new Error('create: params must be an object') if (!input.projectId) throw new Error('create: projectId is required') if (!input.title?.trim()) throw new Error('create: title is required') const projects = await loadProjects() const project = projects.projects[input.projectId] if (!project) { throw new Error( `create: unknown projectId "${input.projectId}". Call seed-projects or upsert-project first.`, ) } if (!project.enabled) { throw new Error(`create: project "${input.projectId}" is disabled`) } const idemKey = typeof input.idempotencyKey === 'string' ? input.idempotencyKey.trim() : '' if (idemKey && !/^[A-Za-z0-9_-]{8,80}$/.test(idemKey)) throw new Error('create: invalid idempotencyKey') if (idemKey) { const fp = `manual:idem:${input.projectId}:${idemKey}` const prior = Object.values((await loadJson(storageKeys.LEDGER_KEY, emptyLedger())).items).find((i) => i.fingerprint === fp) if (prior) return { item: prior, persisted: false, replayed: true, project: { id: project.id, enabled: project.enabled } } } const at = nowIso() const priority = input.priority ?? null const needsKent = input.needsKent === true const item: WorkItem = { id: newWorkItemId(), projectId: input.projectId, title: input.title.trim(), body: input.body ?? '', state: 'intake', priority, needsKent, rank: rankOf(priority, needsKent), source: 'manual', externalId: null, fingerprint: idemKey ? `manual:idem:${input.projectId}:${idemKey}` : `manual:${input.projectId}:${input.title.trim().toLowerCase()}:${at}`, signalIds: [], labels: schemeTags({ source: 'manual', labels: (input.labels ?? []).map(String) }), repoRef: input.repoRef ?? primaryRepoRef(project), planCardId: null, runId: null, createdAt: at, updatedAt: at, events: [ { at, from: null, to: 'intake', reason: 'manual create', actor: 'human', }, ], shadow: input.shadow !== false, } if (input.dryRun) return { item, persisted: false, project: { id: project.id, enabled: project.enabled } } const ledger = await loadJson(storageKeys.LEDGER_KEY, emptyLedger()) ledger.items[item.id] = item await saveJson(storageKeys.LEDGER_KEY, ledger) // card.created always; push and card-route wake under the rules. Never fails the create. const [hook] = await afterCardsCreated([item], { quiet: input.quiet === true, routes: input.routes !== false }) return { item, persisted: true, project: { id: project.id, enabled: project.enabled }, ...(hook ? { push: hook.push, route: hook.route } : {}) } }