import type {Agent} from "@atproto/api"; import type {RepoRecordGroups} from "./diff"; import type {AtprotoDraftRecords, MockStrongRef} from "./record-builder"; export async function createSembleRecords( agent: Agent, repo: string, drafts: AtprotoDraftRecords, existing?: RepoRecordGroups ): Promise<{created: number; failures: Array<{rkey: string; error: string}>}> { const failures: Array<{rkey: string; error: string}> = []; let created = 0; const collectionRefs = new Map(); if (existing) { for (const entry of existing.collections) { const name = extractCollectionName(entry.value); if (!name || !entry.cid) continue; collectionRefs.set(name, {uri: entry.uri, cid: entry.cid}); } } const cardRefs = new Map(); for (const envelope of drafts.collections) { try { const response = await agent.com.atproto.repo.createRecord({ repo, collection: envelope.collection, rkey: envelope.rkey, record: envelope.record }); if (envelope.recordId && response.data.cid) { collectionRefs.set(envelope.recordId, {uri: response.data.uri, cid: response.data.cid}); } created += 1; } catch (error) { failures.push({ rkey: envelope.rkey, error: error instanceof Error ? error.message : String(error) }); } } for (const envelope of drafts.cards) { try { const response = await agent.com.atproto.repo.createRecord({ repo, collection: envelope.collection, rkey: envelope.rkey, record: envelope.record }); if (envelope.recordId && response.data.cid) { cardRefs.set(envelope.recordId, {uri: response.data.uri, cid: response.data.cid}); } created += 1; } catch (error) { failures.push({ rkey: envelope.rkey, error: error instanceof Error ? error.message : String(error) }); } } for (const envelope of drafts.notes) { const cardRef = envelope.cardId ? cardRefs.get(envelope.cardId) : undefined; if (!cardRef) { failures.push({rkey: envelope.rkey, error: "Missing parent card ref for note."}); continue; } try { const record = {...envelope.record, parentCard: cardRef}; await agent.com.atproto.repo.createRecord({ repo, collection: envelope.collection, rkey: envelope.rkey, record }); created += 1; } catch (error) { failures.push({ rkey: envelope.rkey, error: error instanceof Error ? error.message : String(error) }); } } for (const envelope of drafts.collectionLinks) { const collectionRef = envelope.collectionName ? collectionRefs.get(envelope.collectionName) : undefined; const cardRef = envelope.cardId ? cardRefs.get(envelope.cardId) : undefined; if (!collectionRef || !cardRef) { failures.push({rkey: envelope.rkey, error: "Missing collection or card ref for link."}); continue; } try { const record = {...envelope.record, collection: collectionRef, card: cardRef}; await agent.com.atproto.repo.createRecord({ repo, collection: envelope.collection, rkey: envelope.rkey, record }); created += 1; } catch (error) { failures.push({ rkey: envelope.rkey, error: error instanceof Error ? error.message : String(error) }); } } return {created, failures}; } function extractCollectionName(value: unknown): string | undefined { if (!value || typeof value !== "object") return undefined; const record = value as {name?: unknown}; return typeof record.name === "string" ? record.name : undefined; }