import { createMindSpaceLocalServerAdapter } from './mindspace-local-server-adapter.mjs'; import { createMindSpaceRemoteServerAdapter } from './mindspace-remote-server-adapter.mjs'; import { assertMindSpaceServerAdapterBindings, MINDSPACE_SERVER_ADAPTER_BINDING_KEYS, } from './mindspace-server-adapter-contract.mjs'; const VALID_ADAPTER_KINDS = new Set(['local', 'remote']); export function resolveMindSpaceServerAdapterKind(env = process.env) { const raw = String(env.MINDSPACE_SERVER_ADAPTER ?? 'local').trim().toLowerCase(); if (!VALID_ADAPTER_KINDS.has(raw)) { throw new Error( `Unsupported MINDSPACE_SERVER_ADAPTER "${raw}". Expected one of: ${[...VALID_ADAPTER_KINDS].join(', ')}`, ); } return raw; } export function createMindSpaceServerAdapter(options = {}) { const env = options.env ?? process.env; const kind = resolveMindSpaceServerAdapterKind(env); const adapter = kind === 'remote' ? createMindSpaceRemoteServerAdapter({ logger: options.logger, endpoint: options.remote?.baseUrl ?? env.MINDSPACE_REMOTE_BASE_URL, authToken: options.remote?.authToken, operationBasePath: options.remote?.operationBasePath, timeoutMs: options.remote?.timeoutMs, fetchFn: options.fetchFn, }) : createMindSpaceLocalServerAdapter(options); return { ...adapter, kind, }; } export function assertMindSpaceServerAdapterContract(adapter) { const bindings = {}; for (const key of MINDSPACE_SERVER_ADAPTER_BINDING_KEYS) { if (!(key in (adapter ?? {}))) { throw new Error(`MindSpace server adapter is missing required binding: ${key}`); } bindings[key] = adapter[key]; } assertMindSpaceServerAdapterBindings(bindings); if (typeof adapter?.startBackgroundJobs !== 'function') { throw new Error('MindSpace server adapter must expose startBackgroundJobs()'); } return adapter; }