|
| 1 | +/** |
| 2 | + * Optional Interchange-driven core loop (CL-8267). |
| 3 | + * |
| 4 | + * The reactor (`@intx/agent` + `ChatDirector`) remains the only loop driver. |
| 5 | + * This module is the opt-in seam for driving it through Interchange's |
| 6 | + * `runWorkflow`/`runLocal` instead, without waiting on the full 0.4.0 hub. |
| 7 | + * |
| 8 | + * Gating: enabled only when `CORBITS_INTERCHANGE_LOOP` is `1` or `true`. |
| 9 | + * Resolution is lazy and total — `@intx/workflow` is not a dependency of this |
| 10 | + * package yet, so when it cannot be loaded the caller keeps the reactor path. |
| 11 | + * Nothing here changes behavior unless the flag is set. |
| 12 | + */ |
| 13 | + |
| 14 | +export const INTERCHANGE_LOOP_ENV_VAR = "CORBITS_INTERCHANGE_LOOP"; |
| 15 | + |
| 16 | +const INTERCHANGE_WORKFLOW_SPECIFIER = "@intx/workflow"; |
| 17 | + |
| 18 | +export type InterchangeLoopStatus = |
| 19 | + | { status: "driven"; via: "runWorkflow" | "runLocal" } |
| 20 | + | { status: "unavailable"; reason: string }; |
| 21 | + |
| 22 | +/** True only when the operator explicitly opts into the Interchange loop. */ |
| 23 | +export function isInterchangeLoopEnabled( |
| 24 | + env: NodeJS.ProcessEnv = process.env, |
| 25 | +): boolean { |
| 26 | + const raw = env[INTERCHANGE_LOOP_ENV_VAR]?.trim().toLowerCase(); |
| 27 | + return raw === "1" || raw === "true"; |
| 28 | +} |
| 29 | + |
| 30 | +type WorkflowModule = Record<string, unknown>; |
| 31 | + |
| 32 | +function pickDriver(loaded: WorkflowModule): InterchangeLoopStatus { |
| 33 | + // Prefer the hub-backed entry point; the in-memory runner is the local |
| 34 | + // fallback. Both names come from upstream `@intx/workflow`. |
| 35 | + if (typeof loaded["runWorkflow"] === "function") |
| 36 | + return { status: "driven", via: "runWorkflow" }; |
| 37 | + if (typeof loaded["runLocal"] === "function") |
| 38 | + return { status: "driven", via: "runLocal" }; |
| 39 | + return { |
| 40 | + status: "unavailable", |
| 41 | + reason: `${INTERCHANGE_WORKFLOW_SPECIFIER} loaded but exports neither runWorkflow nor runLocal`, |
| 42 | + }; |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Resolve the Interchange loop driver. Never throws and never imports |
| 47 | + * statically — the package is an optional peer, absent until the workflow |
| 48 | + * runtime lands. Callers fall through to the reactor on `unavailable`. |
| 49 | + */ |
| 50 | +export async function resolveInterchangeLoopRunner(): Promise<InterchangeLoopStatus> { |
| 51 | + let loaded: unknown; |
| 52 | + try { |
| 53 | + loaded = (await import( |
| 54 | + INTERCHANGE_WORKFLOW_SPECIFIER |
| 55 | + )) as unknown as WorkflowModule; |
| 56 | + } catch (err: unknown) { |
| 57 | + const detail = err instanceof Error ? err.message : String(err); |
| 58 | + return { |
| 59 | + status: "unavailable", |
| 60 | + reason: `${INTERCHANGE_WORKFLOW_SPECIFIER} is not installed (${detail})`, |
| 61 | + }; |
| 62 | + } |
| 63 | + if (typeof loaded !== "object" || loaded === null) |
| 64 | + return { |
| 65 | + status: "unavailable", |
| 66 | + reason: `${INTERCHANGE_WORKFLOW_SPECIFIER} did not load as a module`, |
| 67 | + }; |
| 68 | + return pickDriver(loaded as unknown as WorkflowModule); |
| 69 | +} |
0 commit comments