/** * @module cosmos-commands * * High-level orchestration commands for managing Cosmos members. * Unifies galaxy creation and registration across Web and CLI environments. */ import { extractErrorMsg } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs'; import { getIbGibAddr } from '@ibgib/ts-gib/dist/helper.mjs'; import { GLOBAL_LOG_A_LOT } from '../../core-constants.mjs'; import { getSpaceIdFromIb } from '../../witness/space/metaspace/metaspace-innerspace/metaspace-innerspace-helper.mjs'; import { getSpaceQualifiedIbGibAddr } from '../../common/other/ibgib-helper.mjs'; import { SpaceQualifiedIbGibAddr } from '../../common/other/ibgib-types.mjs'; import { MetaspaceService } from '../../witness/space/metaspace/metaspace-types.mjs'; import { IbGibSpaceAny } from '../../witness/space/space-base-v1.mjs'; import { KeystoneIbGib_V1 } from '../../keystone/keystone-types.mjs'; import { GalaxyPresetType } from '../galaxy/galaxy-configs.mjs'; import { GalaxyPolicy } from '../galaxy/galaxy-types.mjs'; import { B2tFSGalaxyIbGib_V1 } from '../branching/b2tfs-galaxy/b2tfs-galaxy-types.mjs'; import { B2TFS_BRANCH_DEFAULT_NAME } from '../branching/branch/branch-constants.mjs'; import { executeVcsInit } from '../branching/commands/core-init-cmd.mjs'; import { VcsInitResult } from '../branching/commands/command-types.mjs'; import { CosmosIbGib_V1, GalaxySliceConfig } from './cosmos-types.mjs'; import { addGalaxyToCosmos } from './cosmos-helpers.mjs'; const lcFile = `[cosmos-commands]`; const logalot = GLOBAL_LOG_A_LOT; export interface CreateGalaxyInCosmosOpts { metaspace: MetaspaceService; space: IbGibSpaceAny; cosmosIbGib: CosmosIbGib_V1; galaxyName: string; discriminator?: GalaxyPresetType; branchName?: string; rootFolderName?: string; policy?: GalaxyPolicy; primaryIdentityTjpAddr?: string; deviceDelegate?: { delegateKeystone: KeystoneIbGib_V1; delegateSecret: string; }; slice?: GalaxySliceConfig; icon?: string; label?: string; } export interface CreateGalaxyInCosmosResult { cosmosIbGib: CosmosIbGib_V1; galaxyIbGib: B2tFSGalaxyIbGib_V1; sqGalaxyAddr: SpaceQualifiedIbGibAddr; initRes: VcsInitResult; } /** * Creates and initializes a new member galaxy inside a containing Cosmos, * bootstrapping its branch space, initial root folder, default branch, genesis snapshot, * and registering its soft-link address and metadata entry in the Cosmos manifest. */ export async function executeCreateGalaxyInCosmos({ metaspace, space, cosmosIbGib, galaxyName, discriminator = GalaxyPresetType.code, branchName = B2TFS_BRANCH_DEFAULT_NAME, rootFolderName = 'root', policy, primaryIdentityTjpAddr, deviceDelegate, slice, icon, label, }: CreateGalaxyInCosmosOpts): Promise { const lc = `${lcFile}[${executeCreateGalaxyInCosmos.name}]`; try { if (logalot) { console.log(`${lc} starting for galaxy "${galaxyName}" in cosmos "${cosmosIbGib?.data?.cosmosName}"...`); } if (!cosmosIbGib || !cosmosIbGib.data?.cosmosName) { throw new Error(`Invalid cosmosIbGib: missing cosmosName. (E: d0ab943f098bf3f7303acdbebadbac33)`); } const cosmosName = cosmosIbGib.data.cosmosName; // 1. Initialize the galaxy repository (branch space, root item, default branch, genesis snapshot) const initRes = await executeVcsInit({ metaspace, localSpace: space, cosmosName, galaxyName, rootFolderName, branchName, discriminator, policy, primaryIdentityTjpAddr, deviceDelegate, }); const galaxyIbGib = initRes.galaxyIbGib; const galaxyAddr = getIbGibAddr({ ibGib: galaxyIbGib }); // 2. Derive space ID and build space-qualified galaxy address const spaceId = (space.data as any)?.spaceId || (space.ib ? getSpaceIdFromIb(space.ib) : undefined); const sqGalaxyAddr = getSpaceQualifiedIbGibAddr({ spaceId, ibGibAddr: galaxyAddr, }); // 3. Register member galaxy into the Cosmos manifest const updatedCosmos = await addGalaxyToCosmos({ cosmosIbGib, sqGalaxyAddr, galaxyIbGib, galaxyEntry: { galaxyName, discriminator, policy: (galaxyIbGib.data as any).policy || policy || {} as GalaxyPolicy, slice, icon: icon || (discriminator === GalaxyPresetType.code ? '' : '📝'), label: label || galaxyName, sqGalaxyAddr, galaxyAddr, }, space, metaspace, }); if (logalot) { console.log(`${lc} Successfully added galaxy "${galaxyName}" to cosmos "${cosmosName}" (sqGalaxyAddr: ${sqGalaxyAddr})`); } return { cosmosIbGib: updatedCosmos, galaxyIbGib, sqGalaxyAddr, initRes, }; } catch (error) { console.error(`${lc} ${extractErrorMsg(error)} (E: 3826abb72f28959658b4ba1dd528a4f1)`); throw error; } }