import { extractErrorMsg } from "@ibgib/helper-gib/dist/helpers/utils-helper.mjs"; import { Ib } from "@ibgib/ts-gib/dist/types.mjs"; import { SYNC_SAGA_MSG_ATOM } from "./sync-saga-message-constants.mjs"; import { SyncSagaMessageData_V1, SyncSagaMessageIb_V1 } from "./sync-saga-message-types.mjs"; /** * Constructs the standard 'ib' string for a Sync Saga Message stone. */ export async function getSyncSagaMessageIb({ data, }: { data: SyncSagaMessageData_V1, }): Promise { const lc = `[${getSyncSagaMessageIb.name}]`; try { // sync_saga_msg sagaId stage const ib = [ SYNC_SAGA_MSG_ATOM, data.sagaId, data.stage ].join(' '); // console.log(`${lc} constructed ib: ${ib}`); // temporary debug return ib; } catch (error) { console.error(`${lc} ${extractErrorMsg(error)}`); throw error; } } /** * Parses a standard Sync Saga Message 'ib' string. */ export async function parseSyncSagaMessageIb({ ib, }: { ib: Ib, }): Promise { const lc = `[${parseSyncSagaMessageIb.name}]`; try { const parts = ib.split(' '); if (parts.length !== 3) { throw new Error(`Invalid sync saga message ib. Expected 3 parts [atom sagaId stage]. Got ${parts.length}. (E: 7abec847426bd8f41a475cafcc118826)`); } const [ atom, sagaId, stage ] = parts; if (atom !== SYNC_SAGA_MSG_ATOM) { throw new Error(`Atom mismatch. Expected ${SYNC_SAGA_MSG_ATOM}. Got ${atom}. (E: 1b4ddf426ed87e474816ed48cedb8a26)`); } return { atom, sagaId, stage }; } catch (error) { console.error(`${lc} ${extractErrorMsg(error)}`); throw error; } } /** * Helper to retrieve the Message Stone associated with a Saga Frame. * * Saga Frames link to their Message Stone via the `sync-msg` relationship. */ import { IbGib_V1 } from "@ibgib/ts-gib/dist/V1/types.mjs"; import { IbGibSpaceAny } from "../../witness/space/space-base-v1.mjs"; import { getRel8dIbGibs } from "../../common/other/ibgib-helper.mjs"; import { SYNC_MSG_REL8N_NAME } from "../sync-constants.mjs"; export async function getSyncSagaMessageFromFrame({ frameIbGib, space, }: { frameIbGib: IbGib_V1, space: IbGibSpaceAny, }): Promise> { const lc = `[${getSyncSagaMessageFromFrame.name}]`; try { const rel8d = await getRel8dIbGibs({ ibGib: frameIbGib, rel8nNames: [SYNC_MSG_REL8N_NAME], space }); const msgs = rel8d[SYNC_MSG_REL8N_NAME]; if (!msgs || msgs.length === 0) { throw new Error(`(UNEXPECTED) No sync message stone found found for frame: ${frameIbGib.ib} (E: f9fa2ec64bc84191687609080373c826)`); } if (msgs.length > 1) { throw new Error(`(UNEXPECTED) Multiple sync message stones found (${msgs.length}) for frame: ${frameIbGib.ib}. logic not currently equipped to handle multiple messages per frame. (E: a9876fdd114f0109cdafbb08f79ca826)`); } return msgs[0] as IbGib_V1; } catch (error) { console.error(`${lc} ${extractErrorMsg(error)}`); throw error; } }