Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

refactor: multimodalEmbeddingModel lives beside the gateway (module-size ceilings)

time-attack0483984487a6
diff --git a/src/core/ai/gateway.ts b/src/core/ai/gateway.ts
index 47a868a5ee..42a99ed115 100644
--- a/src/core/ai/gateway.ts
+++ b/src/core/ai/gateway.ts
@@ -2169,27 +2169,6 @@ async function embedMultimodalOpenAICompat(
* model (typically OpenAI text-embedding-3-large at 1536d or 2560d, NOT
* compatible with the 1024d multimodal column).
*/
-/**
- * The model `embedMultimodal` would embed with (`embedding_multimodal_model`,
- * else `embedding_model`) when it can embed multimodal input, otherwise null.
- * Hybrid search routes a query to the image column only when this is set: on
- * a text-only install (the default `voyage:voyage-4`) the image embed always
- * throws, and routing there only dropped the keyword arm and expansion.
- */
-export function multimodalEmbeddingModel(): string | null {
- if (!_config) return null;
- const modelStr = _config.embedding_multimodal_model ?? getEmbeddingModel();
- try {
- const { parsed, recipe } = resolveRecipe(modelStr);
- const touchpoint = recipe.touchpoints.embedding;
- if (!touchpoint?.supports_multimodal) return null;
- if (touchpoint.multimodal_models && !touchpoint.multimodal_models.includes(parsed.modelId)) return null;
- return modelStr;
- } catch {
- return null;
- }
-}
-
export async function embedQueryMultimodal(text: string): Promise<Float32Array> {
const [vec] = await embedMultimodal([{ kind: 'text', text }], { inputType: 'query' });
if (!vec) {
diff --git a/src/core/ai/multimodal-model.ts b/src/core/ai/multimodal-model.ts
new file mode 100644
index 0000000000..9c8fec3ff3
--- /dev/null
+++ b/src/core/ai/multimodal-model.ts
@@ -0,0 +1,25 @@
+/**
+ * The model `embedMultimodal` (gateway.ts) would embed with
+ * (`embedding_multimodal_model`, else `embedding_model`) when it can embed
+ * multimodal input, otherwise null. It applies the same recipe and model
+ * allow-list checks `embedMultimodal` raises on, without the call. Hybrid
+ * search routes a query to the image column only when this is set: on a
+ * text-only install (the default `voyage:voyage-4`) the image embed always
+ * throws, and routing there only dropped the keyword arm and expansion.
+ * An unconfigured gateway answers null.
+ */
+import { getEmbeddingModel, getMultimodalModel } from './gateway.ts';
+import { resolveRecipe } from './model-resolver.ts';
+
+export function multimodalEmbeddingModel(): string | null {
+ try {
+ const modelStr = getMultimodalModel() ?? getEmbeddingModel();
+ const { parsed, recipe } = resolveRecipe(modelStr);
+ const touchpoint = recipe.touchpoints.embedding;
+ if (!touchpoint?.supports_multimodal) return null;
+ if (touchpoint.multimodal_models && !touchpoint.multimodal_models.includes(parsed.modelId)) return null;
+ return modelStr;
+ } catch {
+ return null;
+ }
+}
diff --git a/src/core/search/hybrid.ts b/src/core/search/hybrid.ts
index f4764741b1..b1d441549b 100644
--- a/src/core/search/hybrid.ts
+++ b/src/core/search/hybrid.ts
@@ -1091,7 +1091,7 @@ export async function hybridSearch(
const postFusionOpts = buildPostFusionOpts(req);
const relationalList = await buildRelationalList(req);
- const { isAvailable, multimodalEmbeddingModel } = await import('../ai/gateway.ts');
+ const { isAvailable } = await import('../ai/gateway.ts');
const providerProbe = resolvedCol.embeddingModel || undefined;
// Image/both/unified routing embeds via the MULTIMODAL provider, not the
// text provider — so a multimodal-only install (text provider absent) must
@@ -1099,8 +1099,8 @@ export async function hybridSearch(
// explicitly and only short-circuit when neither the text provider nor (for
// multimodal-routed queries) the multimodal provider is reachable. Without
// this guard a multimodal-only install would fall to keyword-only here and
- // never run the image/unified vector path.
- const multimodalProviderProbe = multimodalEmbeddingModel();
+ // never run the image/unified vector path. Null when no configured model can embed images.
+ const multimodalProviderProbe = (await import('../ai/multimodal-model.ts')).multimodalEmbeddingModel();
// The LLM intent tie-break (below) can escalate a regex-'text' query to
// 'image'/'both'; account for that possibility so an ambiguous query on a
// multimodal-only install still reaches the multimodal branch.
@@ -1125,8 +1125,7 @@ export async function hybridSearch(
const { effectiveModality, unifiedRouting, queries } = await resolveModalityAndQueries(req);
const { vectorArms, queryEmbedding, imageQueryEmbedding, unifiedDone } =
- await runVectorArms(req, { effectiveModality, unifiedRouting, queries,
- multimodalProviderProbe: multimodalProviderProbe ?? cfgForColumn?.embedding_multimodal_model ?? 'voyage:voyage-multimodal-3' });
+ await runVectorArms(req, { effectiveModality, unifiedRouting, queries, multimodalProviderProbe: multimodalProviderProbe ?? cfgForColumn?.embedding_multimodal_model ?? 'voyage:voyage-multimodal-3' });
if (vectorArms.length === 0) {
return searchVectorFallback(req, lexical, relationalList, postFusionOpts);
}
diff --git a/src/core/search/hybrid/arms.ts b/src/core/search/hybrid/arms.ts
index 4a69457951..fef2eb2aac 100644
--- a/src/core/search/hybrid/arms.ts
+++ b/src/core/search/hybrid/arms.ts
@@ -198,7 +198,7 @@ export async function resolveModalityAndQueries(req: HybridRequest) {
regexModality === 'text' &&
resolvedMode.cross_modal_llm_intent &&
isAmbiguousModalityQuery(query) &&
- (await import('../../ai/gateway.ts')).multimodalEmbeddingModel() !== null
+ (await import('../../ai/multimodal-model.ts')).multimodalEmbeddingModel() !== null
) {
try {
const { classifyModalityWithLLM } = await import('../llm-intent.ts');
diff --git a/src/core/search/hybrid/request.ts b/src/core/search/hybrid/request.ts
index f21bea25b5..8f69c0c73b 100644
--- a/src/core/search/hybrid/request.ts
+++ b/src/core/search/hybrid/request.ts
@@ -180,7 +180,7 @@ export async function resolveHybridRequest(
// An inferred image intent needs a multimodal embedding model; without one
// the image arm cannot run, so the query stays a text query (keyword arm and
// expansion included). An explicit `crossModal` still routes as asked.
- const suggestions = intended.suggestedModality !== 'text' && (await import('../../ai/gateway.ts')).multimodalEmbeddingModel() === null
+ const suggestions = intended.suggestedModality !== 'text' && (await import('../../ai/multimodal-model.ts')).multimodalEmbeddingModel() === null
? { ...intended, suggestedModality: 'text' as const } : intended;
const intentWeightingOn = resolvedMode.intentWeighting;
const intentWeights = intentWeightingOn