fix(dashboard,core,engine): statically import @fusion/engine to fix createFnAgent undefined in published CLI

The dashboard modules used a variable-specifier dynamic import
(`const m = "@fusion/engine"; await import(m)`) to defeat bundler static
analysis. tsup honored that and left the dynamic import in dist/bin.js,
so the published `@runfusion/fusion` package failed at runtime with
"createFnAgent2 is not a function" — `@fusion/engine` isn't on npm and
the silent catch set the binding to undefined. Replaces the trick with
static imports across planning, chat, subtask-breakdown, mission-interview,
agent-generation, ai-refine, roadmap-suggestions, milestone-slice-interview,
and routes. Core can't statically import engine (cycle), so it now exposes
setCreateFnAgent and engine wires itself in at module load. Documents the
pattern in AGENTS.md.

Fixes Runfusion/Fusion#9.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
gsxdsm
2026-04-26 14:43:03 -07:00
parent 3bdb6a0447
commit 69d9b67ca1
13 changed files with 114 additions and 231 deletions

View File

@@ -25,13 +25,17 @@ import { EventEmitter } from "node:events";
import { join, resolve, relative } from "node:path";
import { SessionEventBuffer } from "./sse-buffer.js";
// Dynamic import for @fusion/engine to avoid resolution issues in test environment
import {
createFnAgent as engineCreateFnAgent,
buildAgentChatPrompt as engineBuildAgentChatPrompt,
} from "@fusion/engine";
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type AgentResult = any;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let createFnAgent: any;
let createFnAgent: any = engineCreateFnAgent;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let buildAgentChatPromptFn: any;
let buildAgentChatPromptFn: any = engineBuildAgentChatPrompt;
/**
* Diagnostics logger for the chat module.
@@ -93,35 +97,8 @@ const diagnostics: DiagnosticsLogger = {
},
};
// Initialize the import (this runs in actual server, mocked in tests)
async function initEngine() {
if (!createFnAgent || !buildAgentChatPromptFn) {
try {
// Use dynamic import with variable to prevent static analysis
const engineModule = "@fusion/engine";
const engine = await import(/* @vite-ignore */ engineModule);
if (!createFnAgent) {
createFnAgent = engine.createFnAgent;
}
if (!buildAgentChatPromptFn) {
buildAgentChatPromptFn = engine.buildAgentChatPrompt;
}
} catch {
// Allow failure in test environments - agent functionality will be stubbed
if (!createFnAgent) {
createFnAgent = undefined;
}
if (!buildAgentChatPromptFn) {
buildAgentChatPromptFn = undefined;
}
}
}
}
let engineReady: Promise<void> | undefined;
function ensureEngineReady() {
engineReady ??= initEngine();
return engineReady;
function ensureEngineReady(): Promise<void> {
return Promise.resolve();
}
// ── Constants ───────────────────────────────────────────────────────────────
@@ -912,8 +889,7 @@ export function __setBuildAgentChatPrompt(mock: typeof buildAgentChatPromptFn):
export function __resetChatState(): void {
chatStreamManager.reset();
rateLimits.clear();
engineReady = undefined;
buildAgentChatPromptFn = undefined;
buildAgentChatPromptFn = engineBuildAgentChatPrompt;
// Reset diagnostics logger to default
__setChatDiagnostics(null);