diff --git a/AGENTS.md b/AGENTS.md index 48fdf262376..a551eb0d1c7 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -76,6 +76,8 @@ - Language: TypeScript (ESM). Prefer strict typing; avoid `any`. - Formatting/linting via Oxlint and Oxfmt; run `pnpm check` before commits. - Never add `@ts-nocheck` and do not disable `no-explicit-any`; fix root causes and update Oxlint/Oxfmt config only when required. +- Dynamic import guardrail: do not mix `await import("x")` and static `import ... from "x"` for the same module in production code paths. If you need lazy loading, create a dedicated `*.runtime.ts` boundary (that re-exports from `x`) and dynamically import that boundary from lazy callers only. +- Dynamic import verification: after refactors that touch lazy-loading/module boundaries, run `pnpm build` and check for `[INEFFECTIVE_DYNAMIC_IMPORT]` warnings before submitting. - Never share class behavior via prototype mutation (`applyPrototypeMixins`, `Object.defineProperty` on `.prototype`, or exporting `Class.prototype` for merges). Use explicit inheritance/composition (`A extends B extends C`) or helper composition so TypeScript can typecheck. - If this pattern is needed, stop and get explicit approval before shipping; default behavior is to split/refactor into an explicit class hierarchy and keep members strongly typed. - In tests, prefer per-instance stubs over prototype mutation (`SomeClass.prototype.method = ...`) unless a test explicitly documents why prototype-level patching is required. diff --git a/src/agents/pi-model-discovery-runtime.ts b/src/agents/pi-model-discovery-runtime.ts new file mode 100644 index 00000000000..8f57cfab65b --- /dev/null +++ b/src/agents/pi-model-discovery-runtime.ts @@ -0,0 +1 @@ +export { discoverAuthStorage, discoverModels } from "./pi-model-discovery.js"; diff --git a/src/media-understanding/providers/image.ts b/src/media-understanding/providers/image.ts index 8cf08f5d43b..d0dc13c0086 100644 --- a/src/media-understanding/providers/image.ts +++ b/src/media-understanding/providers/image.ts @@ -3,14 +3,23 @@ import { complete } from "@mariozechner/pi-ai"; import { minimaxUnderstandImage } from "../../agents/minimax-vlm.js"; import { getApiKeyForModel, requireApiKey } from "../../agents/model-auth.js"; import { ensureOpenClawModelsJson } from "../../agents/models-config.js"; -import { discoverAuthStorage, discoverModels } from "../../agents/pi-model-discovery.js"; import { coerceImageAssistantText } from "../../agents/tools/image-tool.helpers.js"; import type { ImageDescriptionRequest, ImageDescriptionResult } from "../types.js"; +let piModelDiscoveryRuntimePromise: Promise< + typeof import("../../agents/pi-model-discovery-runtime.js") +> | null = null; + +function loadPiModelDiscoveryRuntime() { + piModelDiscoveryRuntimePromise ??= import("../../agents/pi-model-discovery-runtime.js"); + return piModelDiscoveryRuntimePromise; +} + export async function describeImageWithModel( params: ImageDescriptionRequest, ): Promise { await ensureOpenClawModelsJson(params.cfg, params.agentDir); + const { discoverAuthStorage, discoverModels } = await loadPiModelDiscoveryRuntime(); const authStorage = discoverAuthStorage(params.agentDir); const modelRegistry = discoverModels(authStorage, params.agentDir); const model = modelRegistry.find(params.provider, params.model) as Model | null;