WIP: A simple cli for daily tangled use cases and AI integration. This is for my personal use right now, but happy if others get mileage from it! :)
at feature/issue-4-pr-create-list-view 58 lines 1.6 kB view raw
1const CONSTELLATION_BASE = 'https://constellation.microcosm.blue'; 2 3export interface ConstellationRecord { 4 did: string; 5 collection: string; 6 rkey: string; 7} 8 9export interface GetBacklinksResult { 10 total: number; 11 records: ConstellationRecord[]; 12 cursor: string | null; 13} 14 15/** 16 * Query the constellation indexer for records that link to a given AT-URI. 17 * Constellation indexes records across all PDSs, enabling multi-collaborator queries. 18 * 19 * @param targetUri - The AT-URI being referenced by the records 20 * @param collection - Filter results to this collection (e.g. 'sh.tangled.repo.issue') 21 * @param path - The field path in each record that holds the target URI (e.g. '.repo') 22 * @param limit - Max records to return (default 100) 23 * @param cursor - Pagination cursor from a previous call 24 */ 25export async function getBacklinks( 26 targetUri: string, 27 collection: string, 28 path: string, 29 limit = 100, 30 cursor?: string 31): Promise<GetBacklinksResult> { 32 const params = new URLSearchParams({ 33 target: targetUri, 34 collection, 35 path, 36 limit: String(limit), 37 }); 38 if (cursor) { 39 params.set('cursor', cursor); 40 } 41 42 const response = await fetch(`${CONSTELLATION_BASE}/links?${params}`); 43 if (!response.ok) { 44 throw new Error(`Constellation API error: ${response.status} ${response.statusText}`); 45 } 46 47 const data = (await response.json()) as { 48 total: number; 49 linking_records: ConstellationRecord[]; 50 cursor: string | null; 51 }; 52 53 return { 54 total: data.total, 55 records: data.linking_records, 56 cursor: data.cursor, 57 }; 58}