File size: 8,857 Bytes
cd8bd0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import {
  EMBEDDING_PROVIDERS,
  buildDynamicEmbeddingProvider,
  type EmbeddingProviderNodeRow,
} from "@omniroute/open-sse/config/embeddingRegistry.ts";
import { getProviderCredentials } from "@/sse/services/auth";
import { getProviderNodes } from "@/lib/localDb";
import type { MemorySettingsExtended } from "@/shared/schemas/memory";
import type {
  EmbeddingResolution,
  EmbeddingResult,
  EmbeddingError,
  EmbeddingProviderListing,
} from "./types";
import { embedRemote } from "./remote";
import { embedStatic } from "./staticPotion";
import { embedTransformers } from "./transformersLocal";
import {
  buildCacheKey,
  get as cacheGet,
  set as cacheSet,
  invalidate as cacheInvalidate,
} from "./cache";

const STATIC_MODEL = process.env.MEMORY_STATIC_MODEL || "minishlab/potion-base-8M";
const TRANSFORMERS_MODEL =
  process.env.MEMORY_TRANSFORMERS_MODEL || "Xenova/all-MiniLM-L6-v2";

/** Build an EmbeddingResolution for "no source available" cases. */
function noSource(reason: string): EmbeddingResolution {
  return {
    source: null,
    model: null,
    dimensions: null,
    signature: "null:null:null",
    reason,
  };
}

/** Build a signature string. */
function makeSignature(
  source: "remote" | "static" | "transformers" | null,
  model: string | null,
  dim: number | null
): string {
  return `${source ?? "null"}:${model ?? "null"}:${dim ?? "null"}`;
}

/**
 * Resolve which embedding source is active for the given settings (D4).
 * Pure: no heavy I/O. Provider key check done via synchronous registry lookup.
 */
export function resolveEmbeddingSource(settings: MemorySettingsExtended): EmbeddingResolution {
  const source = settings.embeddingSource ?? "auto";

  if (source === "remote") {
    // Explicit remote — check if the configured model has a key
    const model = settings.embeddingProviderModel ?? null;
    if (!model) {
      return {
        source: null,
        model: null,
        dimensions: null,
        signature: makeSignature(null, null, null),
        reason: "no_key: embeddingProviderModel não configurado",
      };
    }
    // We can't do async here, so we report it as potentially available
    // and the caller will attempt embed + get no_key error on failure.
    // For resolution purposes, mark as remote (will fail at embed time if no key).
    return {
      source: "remote",
      model,
      dimensions: null,
      signature: makeSignature("remote", model, null),
      reason: `provider remoto configurado: ${model}`,
    };
  }

  if (source === "static") {
    if (settings.staticEnabled !== true) {
      return {
        source: null,
        model: null,
        dimensions: null,
        signature: makeSignature(null, null, null),
        reason: "static desabilitado nas configurações",
      };
    }
    return {
      source: "static",
      model: STATIC_MODEL,
      dimensions: 256,
      signature: makeSignature("static", STATIC_MODEL, 256),
      reason: "static (potion-base-8M) selecionado explicitamente",
    };
  }

  if (source === "transformers") {
    if (settings.transformersEnabled !== true) {
      return {
        source: null,
        model: null,
        dimensions: null,
        signature: makeSignature(null, null, null),
        reason: "transformers desabilitado nas configurações",
      };
    }
    return {
      source: "transformers",
      model: TRANSFORMERS_MODEL,
      dimensions: 384,
      signature: makeSignature("transformers", TRANSFORMERS_MODEL, 384),
      reason: "transformers.js (MiniLM-L6-v2) selecionado explicitamente",
    };
  }

  // auto: (1) remote if model configured and provider has key in registry
  // (2) static if staticEnabled
  // (3) transformers if transformersEnabled
  // (4) null
  if (source === "auto") {
    // Try remote first — check if embeddingProviderModel is set
    const providerModel = settings.embeddingProviderModel ?? null;
    if (providerModel) {
      const slashIdx = providerModel.indexOf("/");
      const providerId = slashIdx > 0 ? providerModel.slice(0, slashIdx) : null;
      if (providerId && EMBEDDING_PROVIDERS[providerId]) {
        // We defer the actual hasKey check to listEmbeddingProviders (async).
        // For resolveEmbeddingSource (sync), we report "possibly remote" when model is set.
        // If no key, embed will return EmbeddingError{reason:"no_key"}.
        return {
          source: "remote",
          model: providerModel,
          dimensions: null,
          signature: makeSignature("remote", providerModel, null),
          reason: `auto: provider ${providerId} configurado`,
        };
      }
    }

    if (settings.staticEnabled === true) {
      return {
        source: "static",
        model: STATIC_MODEL,
        dimensions: 256,
        signature: makeSignature("static", STATIC_MODEL, 256),
        reason: "auto: potion-base-8M (static) disponível",
      };
    }

    if (settings.transformersEnabled === true) {
      return {
        source: "transformers",
        model: TRANSFORMERS_MODEL,
        dimensions: 384,
        signature: makeSignature("transformers", TRANSFORMERS_MODEL, 384),
        reason: "auto: transformers.js (MiniLM-L6-v2) disponível",
      };
    }

    return noSource("auto: nenhuma fonte de embedding disponível");
  }

  return noSource("fonte de embedding desconhecida");
}

/**
 * Generate an embedding for the given text using the active source.
 * Caches results in memory (D6).
 */
export async function embed(
  text: string,
  settings: MemorySettingsExtended
): Promise<EmbeddingResult | EmbeddingError> {
  const resolution = resolveEmbeddingSource(settings);

  if (!resolution.source) {
    return {
      source: "remote",
      model: null,
      reason: "unknown",
      message: resolution.reason,
    };
  }

  const cacheKey = buildCacheKey(
    resolution.source,
    resolution.model,
    resolution.dimensions,
    text
  );

  const cached = cacheGet(cacheKey);
  if (cached) {
    return {
      vector: cached,
      source: resolution.source,
      model: resolution.model ?? "",
      dimensions: cached.length,
      latencyMs: 0,
      cached: true,
    };
  }

  let result: EmbeddingResult | EmbeddingError;

  if (resolution.source === "remote") {
    result = await embedRemote(text, resolution.model ?? "");
  } else if (resolution.source === "static") {
    result = await embedStatic(text);
  } else {
    result = await embedTransformers(text);
  }

  if ("vector" in result) {
    cacheSet(cacheKey, result.vector);
  }

  return result;
}

/**
 * List providers that have embedding models, marking which ones have a configured API key.
 * Aggregates from EMBEDDING_PROVIDERS + local provider_nodes.
 */
export async function listEmbeddingProviders(): Promise<EmbeddingProviderListing[]> {
  // Get dynamic local providers
  let dynamicProviders: ReturnType<typeof buildDynamicEmbeddingProvider>[] = [];
  try {
    const nodes = (await getProviderNodes()) as unknown as EmbeddingProviderNodeRow[];
    dynamicProviders = (Array.isArray(nodes) ? nodes : [])
      .filter((n) => {
        const validTypes = ["chat", "responses", "embeddings"];
        return validTypes.includes(n.apiType || "");
      })
      .map((n) => {
        try {
          return buildDynamicEmbeddingProvider(n);
        } catch {
          return null;
        }
      })
      .filter((p): p is NonNullable<typeof p> => p !== null);
  } catch {
    // Ignore failures — just return static providers
  }

  const result: EmbeddingProviderListing[] = [];

  // Process hardcoded EMBEDDING_PROVIDERS
  for (const [providerId, config] of Object.entries(EMBEDDING_PROVIDERS)) {
    let hasKey = false;
    try {
      const creds = await getProviderCredentials(providerId);
      hasKey = !!(
        creds &&
        !("allRateLimited" in creds && creds.allRateLimited) &&
        (("apiKey" in creds ? !!creds.apiKey : false) ||
          ("accessToken" in creds ? !!creds.accessToken : false))
      );
    } catch {
      hasKey = false;
    }

    result.push({
      provider: providerId,
      hasKey,
      models: config.models.map((m) => ({
        id: `${providerId}/${m.id}`,
        name: m.name,
        dimensions: m.dimensions ?? null,
      })),
    });
  }

  // Process dynamic providers (local nodes)
  for (const dp of dynamicProviders) {
    // Dynamic local providers typically have authType="none"
    result.push({
      provider: dp.id,
      hasKey: true, // local providers don't need keys
      models: dp.models.map((m) => ({
        id: `${dp.id}/${m.id}`,
        name: m.name,
        dimensions: m.dimensions ?? null,
      })),
    });
  }

  return result;
}

/**
 * Drop the in-memory embedding cache.
 * Called when settings (model/source) change.
 */
export function invalidateEmbeddingCache(): void {
  cacheInvalidate();
}