cclawd 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/dist/build-info.json +3 -3
  2. package/dist/plugin-sdk/active-listener-CN-tMEvN.js +35 -0
  3. package/dist/plugin-sdk/api-key-rotation-CimGYMBc.js +176 -0
  4. package/dist/plugin-sdk/audio-preflight-C-xXBoE2.js +51 -0
  5. package/dist/plugin-sdk/audio-transcription-runner-CTIHpebA.js +2173 -0
  6. package/dist/plugin-sdk/audit-membership-runtime-BFatB2LJ.js +58 -0
  7. package/dist/plugin-sdk/channel-activity-DO0FEzyj.js +95 -0
  8. package/dist/plugin-sdk/channel-web-Da-__nUF.js +2238 -0
  9. package/dist/plugin-sdk/commands-registry-6no2NNrY.js +1118 -0
  10. package/dist/plugin-sdk/compact.runtime-CCoclu5e.js +35 -0
  11. package/dist/plugin-sdk/config-B9ODwgpz.js +37426 -0
  12. package/dist/plugin-sdk/deliver-B1fFpKjV.js +1757 -0
  13. package/dist/plugin-sdk/deliver-runtime-DB-VRMe1.js +15 -0
  14. package/dist/plugin-sdk/deps-send-discord.runtime-DklqycYG.js +15 -0
  15. package/dist/plugin-sdk/deps-send-imessage.runtime-Chs8zeon.js +14 -0
  16. package/dist/plugin-sdk/deps-send-signal.runtime-clW9aSJP.js +13 -0
  17. package/dist/plugin-sdk/deps-send-slack.runtime-BUx0LYY1.js +13 -0
  18. package/dist/plugin-sdk/deps-send-telegram.runtime-LECSHgMG.js +16 -0
  19. package/dist/plugin-sdk/deps-send-whatsapp.runtime-D2d65fw0.js +40 -0
  20. package/dist/plugin-sdk/diagnostic-CxIvS-C2.js +315 -0
  21. package/dist/plugin-sdk/dispatch-BqlR4dPx.js +105863 -0
  22. package/dist/plugin-sdk/env-b9k1PHMI.js +34 -0
  23. package/dist/plugin-sdk/fetch-PoxzAANT.js +326 -0
  24. package/dist/plugin-sdk/fetch-guard-4UVSZ0uS.js +164 -0
  25. package/dist/plugin-sdk/image-Ch6M4tnJ.js +2420 -0
  26. package/dist/plugin-sdk/image-runtime-CSh2o5wY.js +8 -0
  27. package/dist/plugin-sdk/index.js +35 -35
  28. package/dist/plugin-sdk/ir-CugsqGH8.js +1312 -0
  29. package/dist/plugin-sdk/local-roots-adnEg9zb.js +217 -0
  30. package/dist/plugin-sdk/logger-D6zRubj0.js +1164 -0
  31. package/dist/plugin-sdk/login-CYvkQ0At.js +54 -0
  32. package/dist/plugin-sdk/login-qr-ll4NtaT5.js +316 -0
  33. package/dist/plugin-sdk/manager-CHy8IclH.js +3959 -0
  34. package/dist/plugin-sdk/manager-runtime-C70EkEr7.js +11 -0
  35. package/dist/plugin-sdk/outbound-Wzs2iN7X.js +216 -0
  36. package/dist/plugin-sdk/outbound-attachment-khXJwucX.js +17 -0
  37. package/dist/plugin-sdk/paths-BtVqCdw4.js +3063 -0
  38. package/dist/plugin-sdk/pi-model-discovery-Dh4ziodY.js +131 -0
  39. package/dist/plugin-sdk/pi-model-discovery-runtime-b83Xe-HT.js +8 -0
  40. package/dist/plugin-sdk/pi-tools.before-tool-call.runtime-C1z5CDBF.js +349 -0
  41. package/dist/plugin-sdk/proxy-fetch-CJEmoBxi.js +54 -0
  42. package/dist/plugin-sdk/pw-ai-Dj3Cvlzl.js +1990 -0
  43. package/dist/plugin-sdk/qmd-manager-egHUAseQ.js +1581 -0
  44. package/dist/plugin-sdk/resolve-outbound-target-BiICvIKs.js +38 -0
  45. package/dist/plugin-sdk/runtime-whatsapp-login.runtime-DNApufzW.js +9 -0
  46. package/dist/plugin-sdk/runtime-whatsapp-outbound.runtime-CBmtfIQ8.js +13 -0
  47. package/dist/plugin-sdk/send-CScblaI4.js +532 -0
  48. package/dist/plugin-sdk/send-CeHhnld6.js +407 -0
  49. package/dist/plugin-sdk/send-DP_c8JfR.js +3277 -0
  50. package/dist/plugin-sdk/send-Dc5fI6e8.js +495 -0
  51. package/dist/plugin-sdk/send-l-77_s1_.js +2507 -0
  52. package/dist/plugin-sdk/session-CkOKZaqa.js +166 -0
  53. package/dist/plugin-sdk/signal.js +2 -2
  54. package/dist/plugin-sdk/skill-commands-BohYCgkq.js +336 -0
  55. package/dist/plugin-sdk/slash-commands.runtime-DpLfVTM6.js +8 -0
  56. package/dist/plugin-sdk/slash-dispatch.runtime-CASMHwpm.js +35 -0
  57. package/dist/plugin-sdk/slash-skill-commands.runtime-D7rrJEci.js +9 -0
  58. package/dist/plugin-sdk/sqlite-CJE3X7Mv.js +1005 -0
  59. package/dist/plugin-sdk/subagent-registry-runtime-B1oo5bih.js +35 -0
  60. package/dist/plugin-sdk/tables-D5VgpTmm.js +53 -0
  61. package/dist/plugin-sdk/target-errors-C6zZ_OpA.js +191 -0
  62. package/dist/plugin-sdk/tokens-DUnJnpMS.js +50 -0
  63. package/dist/plugin-sdk/web-TfUM1nSi.js +39 -0
  64. package/dist/plugin-sdk/whatsapp-actions-DuWJ0j1r.js +71 -0
  65. package/extensions/mfa-auth/index.ts +36 -17
  66. package/extensions/mfa-auth/src/auth-manager.ts +4 -0
  67. package/extensions/mfa-auth/src/notification-service.ts +5 -1
  68. package/package.json +1 -1
@@ -0,0 +1,3959 @@
1
+ import { L as resolveAgentWorkspaceDir, N as resolveAgentConfig, P as resolveAgentDir, o as resolveSessionTranscriptsDirForAgent } from "./paths-BtVqCdw4.js";
2
+ import { $l as normalizeResolvedSecretInputString, $t as requireApiKey, Hs as formatErrorMessage, Kl as normalizeOptionalSecretInput, Or as onSessionTranscriptUpdate, en as resolveApiKeyForProvider, in as retryAsync, tn as resolveEnvApiKey } from "./config-B9ODwgpz.js";
3
+ import { c as resolveStateDir } from "./paths-eFexkPEh.js";
4
+ import { D as resolveUserPath, P as truncateUtf16Safe, a as createSubsystemLogger, g as clampNumber, h as clampInt } from "./logger-D6zRubj0.js";
5
+ import { t as isTruthyEnvValue } from "./env-b9k1PHMI.js";
6
+ import { t as fetchWithSsrFGuard } from "./fetch-guard-4UVSZ0uS.js";
7
+ import { n as executeWithApiKeyRotation, r as parseGeminiAuth, t as collectProviderApiKeysForExecution } from "./api-key-rotation-CimGYMBc.js";
8
+ import { _ as statRegularFile, a as buildFileEntry, c as ensureDir, d as listMemoryFiles, f as normalizeExtraMemoryPaths, g as isFileMissingError, h as runWithConcurrency, i as sessionPathForFile, l as hashText, m as remapChunkLines, n as buildSessionEntry, o as chunkMarkdown, p as parseEmbedding, r as listSessionFilesForAgent, s as cosineSimilarity, t as requireNodeSqlite, u as isMemoryPath, v as extractKeywords } from "./sqlite-CJE3X7Mv.js";
9
+ import fs from "node:fs";
10
+ import path from "node:path";
11
+ import os from "node:os";
12
+ import fs$1 from "node:fs/promises";
13
+ import { randomUUID } from "node:crypto";
14
+ import chokidar from "chokidar";
15
+ import { createInterface } from "node:readline";
16
+ import { Readable } from "node:stream";
17
+ //#region src/agents/memory-search.ts
18
+ const DEFAULT_OPENAI_MODEL = "text-embedding-3-small";
19
+ const DEFAULT_GEMINI_MODEL = "gemini-embedding-001";
20
+ const DEFAULT_VOYAGE_MODEL = "voyage-4-large";
21
+ const DEFAULT_MISTRAL_MODEL = "mistral-embed";
22
+ const DEFAULT_OLLAMA_MODEL = "nomic-embed-text";
23
+ const DEFAULT_CHUNK_TOKENS = 400;
24
+ const DEFAULT_CHUNK_OVERLAP = 80;
25
+ const DEFAULT_WATCH_DEBOUNCE_MS = 1500;
26
+ const DEFAULT_SESSION_DELTA_BYTES = 1e5;
27
+ const DEFAULT_SESSION_DELTA_MESSAGES = 50;
28
+ const DEFAULT_MAX_RESULTS = 6;
29
+ const DEFAULT_MIN_SCORE = .35;
30
+ const DEFAULT_HYBRID_ENABLED = true;
31
+ const DEFAULT_HYBRID_VECTOR_WEIGHT = .7;
32
+ const DEFAULT_HYBRID_TEXT_WEIGHT = .3;
33
+ const DEFAULT_HYBRID_CANDIDATE_MULTIPLIER = 4;
34
+ const DEFAULT_MMR_ENABLED = false;
35
+ const DEFAULT_MMR_LAMBDA = .7;
36
+ const DEFAULT_TEMPORAL_DECAY_ENABLED = false;
37
+ const DEFAULT_TEMPORAL_DECAY_HALF_LIFE_DAYS = 30;
38
+ const DEFAULT_CACHE_ENABLED = true;
39
+ const DEFAULT_SOURCES = ["memory"];
40
+ function normalizeSources(sources, sessionMemoryEnabled) {
41
+ const normalized = /* @__PURE__ */ new Set();
42
+ const input = sources?.length ? sources : DEFAULT_SOURCES;
43
+ for (const source of input) {
44
+ if (source === "memory") normalized.add("memory");
45
+ if (source === "sessions" && sessionMemoryEnabled) normalized.add("sessions");
46
+ }
47
+ if (normalized.size === 0) normalized.add("memory");
48
+ return Array.from(normalized);
49
+ }
50
+ function resolveStorePath(agentId, raw) {
51
+ const stateDir = resolveStateDir(process.env, os.homedir);
52
+ const fallback = path.join(stateDir, "memory", `${agentId}.sqlite`);
53
+ if (!raw) return fallback;
54
+ return resolveUserPath(raw.includes("{agentId}") ? raw.replaceAll("{agentId}", agentId) : raw);
55
+ }
56
+ function mergeConfig(defaults, overrides, agentId) {
57
+ const enabled = overrides?.enabled ?? defaults?.enabled ?? true;
58
+ const sessionMemory = overrides?.experimental?.sessionMemory ?? defaults?.experimental?.sessionMemory ?? false;
59
+ const provider = overrides?.provider ?? defaults?.provider ?? "auto";
60
+ const defaultRemote = defaults?.remote;
61
+ const overrideRemote = overrides?.remote;
62
+ const includeRemote = Boolean(overrideRemote?.baseUrl || overrideRemote?.apiKey || overrideRemote?.headers || defaultRemote?.baseUrl || defaultRemote?.apiKey || defaultRemote?.headers) || provider === "openai" || provider === "gemini" || provider === "voyage" || provider === "mistral" || provider === "ollama" || provider === "auto";
63
+ const batch = {
64
+ enabled: overrideRemote?.batch?.enabled ?? defaultRemote?.batch?.enabled ?? false,
65
+ wait: overrideRemote?.batch?.wait ?? defaultRemote?.batch?.wait ?? true,
66
+ concurrency: Math.max(1, overrideRemote?.batch?.concurrency ?? defaultRemote?.batch?.concurrency ?? 2),
67
+ pollIntervalMs: overrideRemote?.batch?.pollIntervalMs ?? defaultRemote?.batch?.pollIntervalMs ?? 2e3,
68
+ timeoutMinutes: overrideRemote?.batch?.timeoutMinutes ?? defaultRemote?.batch?.timeoutMinutes ?? 60
69
+ };
70
+ const remote = includeRemote ? {
71
+ baseUrl: overrideRemote?.baseUrl ?? defaultRemote?.baseUrl,
72
+ apiKey: overrideRemote?.apiKey ?? defaultRemote?.apiKey,
73
+ headers: overrideRemote?.headers ?? defaultRemote?.headers,
74
+ batch
75
+ } : void 0;
76
+ const fallback = overrides?.fallback ?? defaults?.fallback ?? "none";
77
+ const modelDefault = provider === "gemini" ? DEFAULT_GEMINI_MODEL : provider === "openai" ? DEFAULT_OPENAI_MODEL : provider === "voyage" ? DEFAULT_VOYAGE_MODEL : provider === "mistral" ? DEFAULT_MISTRAL_MODEL : provider === "ollama" ? DEFAULT_OLLAMA_MODEL : void 0;
78
+ const model = overrides?.model ?? defaults?.model ?? modelDefault ?? "";
79
+ const local = {
80
+ modelPath: overrides?.local?.modelPath ?? defaults?.local?.modelPath,
81
+ modelCacheDir: overrides?.local?.modelCacheDir ?? defaults?.local?.modelCacheDir
82
+ };
83
+ const sources = normalizeSources(overrides?.sources ?? defaults?.sources, sessionMemory);
84
+ const rawPaths = [...defaults?.extraPaths ?? [], ...overrides?.extraPaths ?? []].map((value) => value.trim()).filter(Boolean);
85
+ const extraPaths = Array.from(new Set(rawPaths));
86
+ const vector = {
87
+ enabled: overrides?.store?.vector?.enabled ?? defaults?.store?.vector?.enabled ?? true,
88
+ extensionPath: overrides?.store?.vector?.extensionPath ?? defaults?.store?.vector?.extensionPath
89
+ };
90
+ const store = {
91
+ driver: overrides?.store?.driver ?? defaults?.store?.driver ?? "sqlite",
92
+ path: resolveStorePath(agentId, overrides?.store?.path ?? defaults?.store?.path),
93
+ vector
94
+ };
95
+ const chunking = {
96
+ tokens: overrides?.chunking?.tokens ?? defaults?.chunking?.tokens ?? DEFAULT_CHUNK_TOKENS,
97
+ overlap: overrides?.chunking?.overlap ?? defaults?.chunking?.overlap ?? DEFAULT_CHUNK_OVERLAP
98
+ };
99
+ const sync = {
100
+ onSessionStart: overrides?.sync?.onSessionStart ?? defaults?.sync?.onSessionStart ?? true,
101
+ onSearch: overrides?.sync?.onSearch ?? defaults?.sync?.onSearch ?? true,
102
+ watch: overrides?.sync?.watch ?? defaults?.sync?.watch ?? true,
103
+ watchDebounceMs: overrides?.sync?.watchDebounceMs ?? defaults?.sync?.watchDebounceMs ?? DEFAULT_WATCH_DEBOUNCE_MS,
104
+ intervalMinutes: overrides?.sync?.intervalMinutes ?? defaults?.sync?.intervalMinutes ?? 0,
105
+ sessions: {
106
+ deltaBytes: overrides?.sync?.sessions?.deltaBytes ?? defaults?.sync?.sessions?.deltaBytes ?? DEFAULT_SESSION_DELTA_BYTES,
107
+ deltaMessages: overrides?.sync?.sessions?.deltaMessages ?? defaults?.sync?.sessions?.deltaMessages ?? DEFAULT_SESSION_DELTA_MESSAGES
108
+ }
109
+ };
110
+ const query = {
111
+ maxResults: overrides?.query?.maxResults ?? defaults?.query?.maxResults ?? DEFAULT_MAX_RESULTS,
112
+ minScore: overrides?.query?.minScore ?? defaults?.query?.minScore ?? DEFAULT_MIN_SCORE
113
+ };
114
+ const hybrid = {
115
+ enabled: overrides?.query?.hybrid?.enabled ?? defaults?.query?.hybrid?.enabled ?? DEFAULT_HYBRID_ENABLED,
116
+ vectorWeight: overrides?.query?.hybrid?.vectorWeight ?? defaults?.query?.hybrid?.vectorWeight ?? DEFAULT_HYBRID_VECTOR_WEIGHT,
117
+ textWeight: overrides?.query?.hybrid?.textWeight ?? defaults?.query?.hybrid?.textWeight ?? DEFAULT_HYBRID_TEXT_WEIGHT,
118
+ candidateMultiplier: overrides?.query?.hybrid?.candidateMultiplier ?? defaults?.query?.hybrid?.candidateMultiplier ?? DEFAULT_HYBRID_CANDIDATE_MULTIPLIER,
119
+ mmr: {
120
+ enabled: overrides?.query?.hybrid?.mmr?.enabled ?? defaults?.query?.hybrid?.mmr?.enabled ?? DEFAULT_MMR_ENABLED,
121
+ lambda: overrides?.query?.hybrid?.mmr?.lambda ?? defaults?.query?.hybrid?.mmr?.lambda ?? DEFAULT_MMR_LAMBDA
122
+ },
123
+ temporalDecay: {
124
+ enabled: overrides?.query?.hybrid?.temporalDecay?.enabled ?? defaults?.query?.hybrid?.temporalDecay?.enabled ?? DEFAULT_TEMPORAL_DECAY_ENABLED,
125
+ halfLifeDays: overrides?.query?.hybrid?.temporalDecay?.halfLifeDays ?? defaults?.query?.hybrid?.temporalDecay?.halfLifeDays ?? DEFAULT_TEMPORAL_DECAY_HALF_LIFE_DAYS
126
+ }
127
+ };
128
+ const cache = {
129
+ enabled: overrides?.cache?.enabled ?? defaults?.cache?.enabled ?? DEFAULT_CACHE_ENABLED,
130
+ maxEntries: overrides?.cache?.maxEntries ?? defaults?.cache?.maxEntries
131
+ };
132
+ const overlap = clampNumber(chunking.overlap, 0, Math.max(0, chunking.tokens - 1));
133
+ const minScore = clampNumber(query.minScore, 0, 1);
134
+ const vectorWeight = clampNumber(hybrid.vectorWeight, 0, 1);
135
+ const textWeight = clampNumber(hybrid.textWeight, 0, 1);
136
+ const sum = vectorWeight + textWeight;
137
+ const normalizedVectorWeight = sum > 0 ? vectorWeight / sum : DEFAULT_HYBRID_VECTOR_WEIGHT;
138
+ const normalizedTextWeight = sum > 0 ? textWeight / sum : DEFAULT_HYBRID_TEXT_WEIGHT;
139
+ const candidateMultiplier = clampInt(hybrid.candidateMultiplier, 1, 20);
140
+ const temporalDecayHalfLifeDays = Math.max(1, Math.floor(Number.isFinite(hybrid.temporalDecay.halfLifeDays) ? hybrid.temporalDecay.halfLifeDays : DEFAULT_TEMPORAL_DECAY_HALF_LIFE_DAYS));
141
+ const deltaBytes = clampInt(sync.sessions.deltaBytes, 0, Number.MAX_SAFE_INTEGER);
142
+ const deltaMessages = clampInt(sync.sessions.deltaMessages, 0, Number.MAX_SAFE_INTEGER);
143
+ return {
144
+ enabled,
145
+ sources,
146
+ extraPaths,
147
+ provider,
148
+ remote,
149
+ experimental: { sessionMemory },
150
+ fallback,
151
+ model,
152
+ local,
153
+ store,
154
+ chunking: {
155
+ tokens: Math.max(1, chunking.tokens),
156
+ overlap
157
+ },
158
+ sync: {
159
+ ...sync,
160
+ sessions: {
161
+ deltaBytes,
162
+ deltaMessages
163
+ }
164
+ },
165
+ query: {
166
+ ...query,
167
+ minScore,
168
+ hybrid: {
169
+ enabled: Boolean(hybrid.enabled),
170
+ vectorWeight: normalizedVectorWeight,
171
+ textWeight: normalizedTextWeight,
172
+ candidateMultiplier,
173
+ mmr: {
174
+ enabled: Boolean(hybrid.mmr.enabled),
175
+ lambda: Number.isFinite(hybrid.mmr.lambda) ? Math.max(0, Math.min(1, hybrid.mmr.lambda)) : DEFAULT_MMR_LAMBDA
176
+ },
177
+ temporalDecay: {
178
+ enabled: Boolean(hybrid.temporalDecay.enabled),
179
+ halfLifeDays: temporalDecayHalfLifeDays
180
+ }
181
+ }
182
+ },
183
+ cache: {
184
+ enabled: Boolean(cache.enabled),
185
+ maxEntries: typeof cache.maxEntries === "number" && Number.isFinite(cache.maxEntries) ? Math.max(1, Math.floor(cache.maxEntries)) : void 0
186
+ }
187
+ };
188
+ }
189
+ function resolveMemorySearchConfig(cfg, agentId) {
190
+ const defaults = cfg.agents?.defaults?.memorySearch;
191
+ const overrides = resolveAgentConfig(cfg, agentId)?.memorySearch;
192
+ const resolved = mergeConfig(defaults, overrides, agentId);
193
+ if (!resolved.enabled) return null;
194
+ return resolved;
195
+ }
196
+ //#endregion
197
+ //#region src/memory/embeddings-debug.ts
198
+ const debugEmbeddings = isTruthyEnvValue(process.env.OPENCLAW_DEBUG_MEMORY_EMBEDDINGS);
199
+ const log$3 = createSubsystemLogger("memory/embeddings");
200
+ function debugEmbeddingsLog(message, meta) {
201
+ if (!debugEmbeddings) return;
202
+ const suffix = meta ? ` ${JSON.stringify(meta)}` : "";
203
+ log$3.raw(`${message}${suffix}`);
204
+ }
205
+ //#endregion
206
+ //#region src/memory/remote-http.ts
207
+ function buildRemoteBaseUrlPolicy(baseUrl) {
208
+ const trimmed = baseUrl.trim();
209
+ if (!trimmed) return;
210
+ try {
211
+ const parsed = new URL(trimmed);
212
+ if (parsed.protocol !== "http:" && parsed.protocol !== "https:") return;
213
+ return { allowedHostnames: [parsed.hostname] };
214
+ } catch {
215
+ return;
216
+ }
217
+ }
218
+ async function withRemoteHttpResponse(params) {
219
+ const { response, release } = await fetchWithSsrFGuard({
220
+ url: params.url,
221
+ init: params.init,
222
+ policy: params.ssrfPolicy,
223
+ auditContext: params.auditContext ?? "memory-remote"
224
+ });
225
+ try {
226
+ return await params.onResponse(response);
227
+ } finally {
228
+ await release();
229
+ }
230
+ }
231
+ //#endregion
232
+ //#region src/memory/secret-input.ts
233
+ function resolveMemorySecretInputString(params) {
234
+ return normalizeResolvedSecretInputString({
235
+ value: params.value,
236
+ path: params.path
237
+ });
238
+ }
239
+ //#endregion
240
+ //#region src/memory/embeddings-gemini.ts
241
+ const DEFAULT_GEMINI_BASE_URL = "https://generativelanguage.googleapis.com/v1beta";
242
+ const DEFAULT_GEMINI_EMBEDDING_MODEL = "gemini-embedding-001";
243
+ const GEMINI_MAX_INPUT_TOKENS = { "text-embedding-004": 2048 };
244
+ function resolveRemoteApiKey(remoteApiKey) {
245
+ const trimmed = resolveMemorySecretInputString({
246
+ value: remoteApiKey,
247
+ path: "agents.*.memorySearch.remote.apiKey"
248
+ });
249
+ if (!trimmed) return;
250
+ if (trimmed === "GOOGLE_API_KEY" || trimmed === "GEMINI_API_KEY") return process.env[trimmed]?.trim();
251
+ return trimmed;
252
+ }
253
+ function normalizeGeminiModel(model) {
254
+ const trimmed = model.trim();
255
+ if (!trimmed) return DEFAULT_GEMINI_EMBEDDING_MODEL;
256
+ const withoutPrefix = trimmed.replace(/^models\//, "");
257
+ if (withoutPrefix.startsWith("gemini/")) return withoutPrefix.slice(7);
258
+ if (withoutPrefix.startsWith("google/")) return withoutPrefix.slice(7);
259
+ return withoutPrefix;
260
+ }
261
+ function normalizeGeminiBaseUrl(raw) {
262
+ const trimmed = raw.replace(/\/+$/, "");
263
+ const openAiIndex = trimmed.indexOf("/openai");
264
+ if (openAiIndex > -1) return trimmed.slice(0, openAiIndex);
265
+ return trimmed;
266
+ }
267
+ function buildGeminiModelPath(model) {
268
+ return model.startsWith("models/") ? model : `models/${model}`;
269
+ }
270
+ async function createGeminiEmbeddingProvider(options) {
271
+ const client = await resolveGeminiEmbeddingClient(options);
272
+ const baseUrl = client.baseUrl.replace(/\/$/, "");
273
+ const embedUrl = `${baseUrl}/${client.modelPath}:embedContent`;
274
+ const batchUrl = `${baseUrl}/${client.modelPath}:batchEmbedContents`;
275
+ const fetchWithGeminiAuth = async (apiKey, endpoint, body) => {
276
+ const headers = {
277
+ ...parseGeminiAuth(apiKey).headers,
278
+ ...client.headers
279
+ };
280
+ return await withRemoteHttpResponse({
281
+ url: endpoint,
282
+ ssrfPolicy: client.ssrfPolicy,
283
+ init: {
284
+ method: "POST",
285
+ headers,
286
+ body: JSON.stringify(body)
287
+ },
288
+ onResponse: async (res) => {
289
+ if (!res.ok) {
290
+ const text = await res.text();
291
+ throw new Error(`gemini embeddings failed: ${res.status} ${text}`);
292
+ }
293
+ return await res.json();
294
+ }
295
+ });
296
+ };
297
+ const embedQuery = async (text) => {
298
+ if (!text.trim()) return [];
299
+ return (await executeWithApiKeyRotation({
300
+ provider: "google",
301
+ apiKeys: client.apiKeys,
302
+ execute: (apiKey) => fetchWithGeminiAuth(apiKey, embedUrl, {
303
+ content: { parts: [{ text }] },
304
+ taskType: "RETRIEVAL_QUERY"
305
+ })
306
+ })).embedding?.values ?? [];
307
+ };
308
+ const embedBatch = async (texts) => {
309
+ if (texts.length === 0) return [];
310
+ const requests = texts.map((text) => ({
311
+ model: client.modelPath,
312
+ content: { parts: [{ text }] },
313
+ taskType: "RETRIEVAL_DOCUMENT"
314
+ }));
315
+ const payload = await executeWithApiKeyRotation({
316
+ provider: "google",
317
+ apiKeys: client.apiKeys,
318
+ execute: (apiKey) => fetchWithGeminiAuth(apiKey, batchUrl, { requests })
319
+ });
320
+ const embeddings = Array.isArray(payload.embeddings) ? payload.embeddings : [];
321
+ return texts.map((_, index) => embeddings[index]?.values ?? []);
322
+ };
323
+ return {
324
+ provider: {
325
+ id: "gemini",
326
+ model: client.model,
327
+ maxInputTokens: GEMINI_MAX_INPUT_TOKENS[client.model],
328
+ embedQuery,
329
+ embedBatch
330
+ },
331
+ client
332
+ };
333
+ }
334
+ async function resolveGeminiEmbeddingClient(options) {
335
+ const remote = options.remote;
336
+ const remoteApiKey = resolveRemoteApiKey(remote?.apiKey);
337
+ const remoteBaseUrl = remote?.baseUrl?.trim();
338
+ const apiKey = remoteApiKey ? remoteApiKey : requireApiKey(await resolveApiKeyForProvider({
339
+ provider: "google",
340
+ cfg: options.config,
341
+ agentDir: options.agentDir
342
+ }), "google");
343
+ const providerConfig = options.config.models?.providers?.google;
344
+ const rawBaseUrl = remoteBaseUrl || providerConfig?.baseUrl?.trim() || DEFAULT_GEMINI_BASE_URL;
345
+ const baseUrl = normalizeGeminiBaseUrl(rawBaseUrl);
346
+ const ssrfPolicy = buildRemoteBaseUrlPolicy(baseUrl);
347
+ const headers = { ...Object.assign({}, providerConfig?.headers, remote?.headers) };
348
+ const apiKeys = collectProviderApiKeysForExecution({
349
+ provider: "google",
350
+ primaryApiKey: apiKey
351
+ });
352
+ const model = normalizeGeminiModel(options.model);
353
+ const modelPath = buildGeminiModelPath(model);
354
+ debugEmbeddingsLog("memory embeddings: gemini client", {
355
+ rawBaseUrl,
356
+ baseUrl,
357
+ model,
358
+ modelPath,
359
+ embedEndpoint: `${baseUrl}/${modelPath}:embedContent`,
360
+ batchEndpoint: `${baseUrl}/${modelPath}:batchEmbedContents`
361
+ });
362
+ return {
363
+ baseUrl,
364
+ headers,
365
+ ssrfPolicy,
366
+ model,
367
+ modelPath,
368
+ apiKeys
369
+ };
370
+ }
371
+ //#endregion
372
+ //#region src/memory/embeddings-model-normalize.ts
373
+ function normalizeEmbeddingModelWithPrefixes(params) {
374
+ const trimmed = params.model.trim();
375
+ if (!trimmed) return params.defaultModel;
376
+ for (const prefix of params.prefixes) if (trimmed.startsWith(prefix)) return trimmed.slice(prefix.length);
377
+ return trimmed;
378
+ }
379
+ //#endregion
380
+ //#region src/memory/embeddings-remote-client.ts
381
+ async function resolveRemoteEmbeddingBearerClient(params) {
382
+ const remote = params.options.remote;
383
+ const remoteApiKey = resolveMemorySecretInputString({
384
+ value: remote?.apiKey,
385
+ path: "agents.*.memorySearch.remote.apiKey"
386
+ });
387
+ const remoteBaseUrl = remote?.baseUrl?.trim();
388
+ const providerConfig = params.options.config.models?.providers?.[params.provider];
389
+ const apiKey = remoteApiKey ? remoteApiKey : requireApiKey(await resolveApiKeyForProvider({
390
+ provider: params.provider,
391
+ cfg: params.options.config,
392
+ agentDir: params.options.agentDir
393
+ }), params.provider);
394
+ const baseUrl = remoteBaseUrl || providerConfig?.baseUrl?.trim() || params.defaultBaseUrl;
395
+ const headerOverrides = Object.assign({}, providerConfig?.headers, remote?.headers);
396
+ return {
397
+ baseUrl,
398
+ headers: {
399
+ "Content-Type": "application/json",
400
+ Authorization: `Bearer ${apiKey}`,
401
+ ...headerOverrides
402
+ },
403
+ ssrfPolicy: buildRemoteBaseUrlPolicy(baseUrl)
404
+ };
405
+ }
406
+ //#endregion
407
+ //#region src/memory/post-json.ts
408
+ async function postJson(params) {
409
+ return await withRemoteHttpResponse({
410
+ url: params.url,
411
+ ssrfPolicy: params.ssrfPolicy,
412
+ init: {
413
+ method: "POST",
414
+ headers: params.headers,
415
+ body: JSON.stringify(params.body)
416
+ },
417
+ onResponse: async (res) => {
418
+ if (!res.ok) {
419
+ const text = await res.text();
420
+ const err = /* @__PURE__ */ new Error(`${params.errorPrefix}: ${res.status} ${text}`);
421
+ if (params.attachStatus) err.status = res.status;
422
+ throw err;
423
+ }
424
+ return await params.parse(await res.json());
425
+ }
426
+ });
427
+ }
428
+ //#endregion
429
+ //#region src/memory/embeddings-remote-fetch.ts
430
+ async function fetchRemoteEmbeddingVectors(params) {
431
+ return await postJson({
432
+ url: params.url,
433
+ headers: params.headers,
434
+ ssrfPolicy: params.ssrfPolicy,
435
+ body: params.body,
436
+ errorPrefix: params.errorPrefix,
437
+ parse: (payload) => {
438
+ return (payload.data ?? []).map((entry) => entry.embedding ?? []);
439
+ }
440
+ });
441
+ }
442
+ //#endregion
443
+ //#region src/memory/embeddings-remote-provider.ts
444
+ function createRemoteEmbeddingProvider(params) {
445
+ const { client } = params;
446
+ const url = `${client.baseUrl.replace(/\/$/, "")}/embeddings`;
447
+ const embed = async (input) => {
448
+ if (input.length === 0) return [];
449
+ return await fetchRemoteEmbeddingVectors({
450
+ url,
451
+ headers: client.headers,
452
+ ssrfPolicy: client.ssrfPolicy,
453
+ body: {
454
+ model: client.model,
455
+ input
456
+ },
457
+ errorPrefix: params.errorPrefix
458
+ });
459
+ };
460
+ return {
461
+ id: params.id,
462
+ model: client.model,
463
+ ...typeof params.maxInputTokens === "number" ? { maxInputTokens: params.maxInputTokens } : {},
464
+ embedQuery: async (text) => {
465
+ const [vec] = await embed([text]);
466
+ return vec ?? [];
467
+ },
468
+ embedBatch: embed
469
+ };
470
+ }
471
+ async function resolveRemoteEmbeddingClient(params) {
472
+ const { baseUrl, headers, ssrfPolicy } = await resolveRemoteEmbeddingBearerClient({
473
+ provider: params.provider,
474
+ options: params.options,
475
+ defaultBaseUrl: params.defaultBaseUrl
476
+ });
477
+ return {
478
+ baseUrl,
479
+ headers,
480
+ ssrfPolicy,
481
+ model: params.normalizeModel(params.options.model)
482
+ };
483
+ }
484
+ //#endregion
485
+ //#region src/memory/embeddings-mistral.ts
486
+ const DEFAULT_MISTRAL_EMBEDDING_MODEL = "mistral-embed";
487
+ const DEFAULT_MISTRAL_BASE_URL = "https://api.mistral.ai/v1";
488
+ function normalizeMistralModel(model) {
489
+ return normalizeEmbeddingModelWithPrefixes({
490
+ model,
491
+ defaultModel: DEFAULT_MISTRAL_EMBEDDING_MODEL,
492
+ prefixes: ["mistral/"]
493
+ });
494
+ }
495
+ async function createMistralEmbeddingProvider(options) {
496
+ const client = await resolveMistralEmbeddingClient(options);
497
+ return {
498
+ provider: createRemoteEmbeddingProvider({
499
+ id: "mistral",
500
+ client,
501
+ errorPrefix: "mistral embeddings failed"
502
+ }),
503
+ client
504
+ };
505
+ }
506
+ async function resolveMistralEmbeddingClient(options) {
507
+ return await resolveRemoteEmbeddingClient({
508
+ provider: "mistral",
509
+ options,
510
+ defaultBaseUrl: DEFAULT_MISTRAL_BASE_URL,
511
+ normalizeModel: normalizeMistralModel
512
+ });
513
+ }
514
+ //#endregion
515
+ //#region src/memory/embeddings-ollama.ts
516
+ const DEFAULT_OLLAMA_EMBEDDING_MODEL = "nomic-embed-text";
517
+ const DEFAULT_OLLAMA_BASE_URL = "http://127.0.0.1:11434";
518
+ function sanitizeAndNormalizeEmbedding$1(vec) {
519
+ const sanitized = vec.map((value) => Number.isFinite(value) ? value : 0);
520
+ const magnitude = Math.sqrt(sanitized.reduce((sum, value) => sum + value * value, 0));
521
+ if (magnitude < 1e-10) return sanitized;
522
+ return sanitized.map((value) => value / magnitude);
523
+ }
524
+ function normalizeOllamaModel(model) {
525
+ return normalizeEmbeddingModelWithPrefixes({
526
+ model,
527
+ defaultModel: DEFAULT_OLLAMA_EMBEDDING_MODEL,
528
+ prefixes: ["ollama/"]
529
+ });
530
+ }
531
+ function resolveOllamaApiBase(configuredBaseUrl) {
532
+ if (!configuredBaseUrl) return DEFAULT_OLLAMA_BASE_URL;
533
+ return configuredBaseUrl.replace(/\/+$/, "").replace(/\/v1$/i, "");
534
+ }
535
+ function resolveOllamaApiKey(options) {
536
+ const remoteApiKey = resolveMemorySecretInputString({
537
+ value: options.remote?.apiKey,
538
+ path: "agents.*.memorySearch.remote.apiKey"
539
+ });
540
+ if (remoteApiKey) return remoteApiKey;
541
+ const providerApiKey = normalizeOptionalSecretInput(options.config.models?.providers?.ollama?.apiKey);
542
+ if (providerApiKey) return providerApiKey;
543
+ return resolveEnvApiKey("ollama")?.apiKey;
544
+ }
545
+ function resolveOllamaEmbeddingClient(options) {
546
+ const providerConfig = options.config.models?.providers?.ollama;
547
+ const baseUrl = resolveOllamaApiBase(options.remote?.baseUrl?.trim() || providerConfig?.baseUrl?.trim());
548
+ const model = normalizeOllamaModel(options.model);
549
+ const headerOverrides = Object.assign({}, providerConfig?.headers, options.remote?.headers);
550
+ const headers = {
551
+ "Content-Type": "application/json",
552
+ ...headerOverrides
553
+ };
554
+ const apiKey = resolveOllamaApiKey(options);
555
+ if (apiKey) headers.Authorization = `Bearer ${apiKey}`;
556
+ return {
557
+ baseUrl,
558
+ headers,
559
+ ssrfPolicy: buildRemoteBaseUrlPolicy(baseUrl),
560
+ model
561
+ };
562
+ }
563
+ async function createOllamaEmbeddingProvider(options) {
564
+ const client = resolveOllamaEmbeddingClient(options);
565
+ const embedUrl = `${client.baseUrl.replace(/\/$/, "")}/api/embeddings`;
566
+ const embedOne = async (text) => {
567
+ const json = await withRemoteHttpResponse({
568
+ url: embedUrl,
569
+ ssrfPolicy: client.ssrfPolicy,
570
+ init: {
571
+ method: "POST",
572
+ headers: client.headers,
573
+ body: JSON.stringify({
574
+ model: client.model,
575
+ prompt: text
576
+ })
577
+ },
578
+ onResponse: async (res) => {
579
+ if (!res.ok) throw new Error(`Ollama embeddings HTTP ${res.status}: ${await res.text()}`);
580
+ return await res.json();
581
+ }
582
+ });
583
+ if (!Array.isArray(json.embedding)) throw new Error(`Ollama embeddings response missing embedding[]`);
584
+ return sanitizeAndNormalizeEmbedding$1(json.embedding);
585
+ };
586
+ const provider = {
587
+ id: "ollama",
588
+ model: client.model,
589
+ embedQuery: embedOne,
590
+ embedBatch: async (texts) => {
591
+ return await Promise.all(texts.map(embedOne));
592
+ }
593
+ };
594
+ return {
595
+ provider,
596
+ client: {
597
+ ...client,
598
+ embedBatch: async (texts) => {
599
+ try {
600
+ return await provider.embedBatch(texts);
601
+ } catch (err) {
602
+ throw new Error(formatErrorMessage(err), { cause: err });
603
+ }
604
+ }
605
+ }
606
+ };
607
+ }
608
+ //#endregion
609
+ //#region src/memory/embeddings-openai.ts
610
+ const DEFAULT_OPENAI_EMBEDDING_MODEL = "text-embedding-3-small";
611
+ const DEFAULT_OPENAI_BASE_URL = "https://api.openai.com/v1";
612
+ const OPENAI_MAX_INPUT_TOKENS = {
613
+ "text-embedding-3-small": 8192,
614
+ "text-embedding-3-large": 8192,
615
+ "text-embedding-ada-002": 8191
616
+ };
617
+ function normalizeOpenAiModel(model) {
618
+ return normalizeEmbeddingModelWithPrefixes({
619
+ model,
620
+ defaultModel: DEFAULT_OPENAI_EMBEDDING_MODEL,
621
+ prefixes: ["openai/"]
622
+ });
623
+ }
624
+ async function createOpenAiEmbeddingProvider(options) {
625
+ const client = await resolveOpenAiEmbeddingClient(options);
626
+ return {
627
+ provider: createRemoteEmbeddingProvider({
628
+ id: "openai",
629
+ client,
630
+ errorPrefix: "openai embeddings failed",
631
+ maxInputTokens: OPENAI_MAX_INPUT_TOKENS[client.model]
632
+ }),
633
+ client
634
+ };
635
+ }
636
+ async function resolveOpenAiEmbeddingClient(options) {
637
+ return await resolveRemoteEmbeddingClient({
638
+ provider: "openai",
639
+ options,
640
+ defaultBaseUrl: DEFAULT_OPENAI_BASE_URL,
641
+ normalizeModel: normalizeOpenAiModel
642
+ });
643
+ }
644
+ //#endregion
645
+ //#region src/memory/embeddings-voyage.ts
646
+ const DEFAULT_VOYAGE_EMBEDDING_MODEL = "voyage-4-large";
647
+ const DEFAULT_VOYAGE_BASE_URL = "https://api.voyageai.com/v1";
648
+ const VOYAGE_MAX_INPUT_TOKENS = {
649
+ "voyage-3": 32e3,
650
+ "voyage-3-lite": 16e3,
651
+ "voyage-code-3": 32e3
652
+ };
653
+ function normalizeVoyageModel(model) {
654
+ return normalizeEmbeddingModelWithPrefixes({
655
+ model,
656
+ defaultModel: DEFAULT_VOYAGE_EMBEDDING_MODEL,
657
+ prefixes: ["voyage/"]
658
+ });
659
+ }
660
+ async function createVoyageEmbeddingProvider(options) {
661
+ const client = await resolveVoyageEmbeddingClient(options);
662
+ const url = `${client.baseUrl.replace(/\/$/, "")}/embeddings`;
663
+ const embed = async (input, input_type) => {
664
+ if (input.length === 0) return [];
665
+ const body = {
666
+ model: client.model,
667
+ input
668
+ };
669
+ if (input_type) body.input_type = input_type;
670
+ return await fetchRemoteEmbeddingVectors({
671
+ url,
672
+ headers: client.headers,
673
+ ssrfPolicy: client.ssrfPolicy,
674
+ body,
675
+ errorPrefix: "voyage embeddings failed"
676
+ });
677
+ };
678
+ return {
679
+ provider: {
680
+ id: "voyage",
681
+ model: client.model,
682
+ maxInputTokens: VOYAGE_MAX_INPUT_TOKENS[client.model],
683
+ embedQuery: async (text) => {
684
+ const [vec] = await embed([text], "query");
685
+ return vec ?? [];
686
+ },
687
+ embedBatch: async (texts) => embed(texts, "document")
688
+ },
689
+ client
690
+ };
691
+ }
692
+ async function resolveVoyageEmbeddingClient(options) {
693
+ const { baseUrl, headers, ssrfPolicy } = await resolveRemoteEmbeddingBearerClient({
694
+ provider: "voyage",
695
+ options,
696
+ defaultBaseUrl: DEFAULT_VOYAGE_BASE_URL
697
+ });
698
+ return {
699
+ baseUrl,
700
+ headers,
701
+ ssrfPolicy,
702
+ model: normalizeVoyageModel(options.model)
703
+ };
704
+ }
705
+ //#endregion
706
+ //#region src/memory/node-llama.ts
707
+ async function importNodeLlamaCpp() {
708
+ return import("node-llama-cpp");
709
+ }
710
+ //#endregion
711
+ //#region src/memory/embeddings.ts
712
+ function sanitizeAndNormalizeEmbedding(vec) {
713
+ const sanitized = vec.map((value) => Number.isFinite(value) ? value : 0);
714
+ const magnitude = Math.sqrt(sanitized.reduce((sum, value) => sum + value * value, 0));
715
+ if (magnitude < 1e-10) return sanitized;
716
+ return sanitized.map((value) => value / magnitude);
717
+ }
718
+ const REMOTE_EMBEDDING_PROVIDER_IDS = [
719
+ "openai",
720
+ "gemini",
721
+ "voyage",
722
+ "mistral"
723
+ ];
724
+ function canAutoSelectLocal(options) {
725
+ const modelPath = options.local?.modelPath?.trim();
726
+ if (!modelPath) return false;
727
+ if (/^(hf:|https?:)/i.test(modelPath)) return false;
728
+ const resolved = resolveUserPath(modelPath);
729
+ try {
730
+ return fs.statSync(resolved).isFile();
731
+ } catch {
732
+ return false;
733
+ }
734
+ }
735
+ function isMissingApiKeyError(err) {
736
+ return formatErrorMessage(err).includes("No API key found for provider");
737
+ }
738
+ async function createLocalEmbeddingProvider(options) {
739
+ const modelPath = options.local?.modelPath?.trim() || "hf:ggml-org/embeddinggemma-300m-qat-q8_0-GGUF/embeddinggemma-300m-qat-Q8_0.gguf";
740
+ const modelCacheDir = options.local?.modelCacheDir?.trim();
741
+ const { getLlama, resolveModelFile, LlamaLogLevel } = await importNodeLlamaCpp();
742
+ let llama = null;
743
+ let embeddingModel = null;
744
+ let embeddingContext = null;
745
+ let initPromise = null;
746
+ const ensureContext = async () => {
747
+ if (embeddingContext) return embeddingContext;
748
+ if (initPromise) return initPromise;
749
+ initPromise = (async () => {
750
+ try {
751
+ if (!llama) llama = await getLlama({ logLevel: LlamaLogLevel.error });
752
+ if (!embeddingModel) {
753
+ const resolved = await resolveModelFile(modelPath, modelCacheDir || void 0);
754
+ embeddingModel = await llama.loadModel({ modelPath: resolved });
755
+ }
756
+ if (!embeddingContext) embeddingContext = await embeddingModel.createEmbeddingContext();
757
+ return embeddingContext;
758
+ } catch (err) {
759
+ initPromise = null;
760
+ throw err;
761
+ }
762
+ })();
763
+ return initPromise;
764
+ };
765
+ return {
766
+ id: "local",
767
+ model: modelPath,
768
+ embedQuery: async (text) => {
769
+ const embedding = await (await ensureContext()).getEmbeddingFor(text);
770
+ return sanitizeAndNormalizeEmbedding(Array.from(embedding.vector));
771
+ },
772
+ embedBatch: async (texts) => {
773
+ const ctx = await ensureContext();
774
+ return await Promise.all(texts.map(async (text) => {
775
+ const embedding = await ctx.getEmbeddingFor(text);
776
+ return sanitizeAndNormalizeEmbedding(Array.from(embedding.vector));
777
+ }));
778
+ }
779
+ };
780
+ }
781
+ async function createEmbeddingProvider(options) {
782
+ const requestedProvider = options.provider;
783
+ const fallback = options.fallback;
784
+ const createProvider = async (id) => {
785
+ if (id === "local") return { provider: await createLocalEmbeddingProvider(options) };
786
+ if (id === "ollama") {
787
+ const { provider, client } = await createOllamaEmbeddingProvider(options);
788
+ return {
789
+ provider,
790
+ ollama: client
791
+ };
792
+ }
793
+ if (id === "gemini") {
794
+ const { provider, client } = await createGeminiEmbeddingProvider(options);
795
+ return {
796
+ provider,
797
+ gemini: client
798
+ };
799
+ }
800
+ if (id === "voyage") {
801
+ const { provider, client } = await createVoyageEmbeddingProvider(options);
802
+ return {
803
+ provider,
804
+ voyage: client
805
+ };
806
+ }
807
+ if (id === "mistral") {
808
+ const { provider, client } = await createMistralEmbeddingProvider(options);
809
+ return {
810
+ provider,
811
+ mistral: client
812
+ };
813
+ }
814
+ const { provider, client } = await createOpenAiEmbeddingProvider(options);
815
+ return {
816
+ provider,
817
+ openAi: client
818
+ };
819
+ };
820
+ const formatPrimaryError = (err, provider) => provider === "local" ? formatLocalSetupError(err) : formatErrorMessage(err);
821
+ if (requestedProvider === "auto") {
822
+ const missingKeyErrors = [];
823
+ let localError = null;
824
+ if (canAutoSelectLocal(options)) try {
825
+ return {
826
+ ...await createProvider("local"),
827
+ requestedProvider
828
+ };
829
+ } catch (err) {
830
+ localError = formatLocalSetupError(err);
831
+ }
832
+ for (const provider of REMOTE_EMBEDDING_PROVIDER_IDS) try {
833
+ return {
834
+ ...await createProvider(provider),
835
+ requestedProvider
836
+ };
837
+ } catch (err) {
838
+ const message = formatPrimaryError(err, provider);
839
+ if (isMissingApiKeyError(err)) {
840
+ missingKeyErrors.push(message);
841
+ continue;
842
+ }
843
+ const wrapped = new Error(message);
844
+ wrapped.cause = err;
845
+ throw wrapped;
846
+ }
847
+ const details = [...missingKeyErrors, localError].filter(Boolean);
848
+ return {
849
+ provider: null,
850
+ requestedProvider,
851
+ providerUnavailableReason: details.length > 0 ? details.join("\n\n") : "No embeddings provider available."
852
+ };
853
+ }
854
+ try {
855
+ return {
856
+ ...await createProvider(requestedProvider),
857
+ requestedProvider
858
+ };
859
+ } catch (primaryErr) {
860
+ const reason = formatPrimaryError(primaryErr, requestedProvider);
861
+ if (fallback && fallback !== "none" && fallback !== requestedProvider) try {
862
+ return {
863
+ ...await createProvider(fallback),
864
+ requestedProvider,
865
+ fallbackFrom: requestedProvider,
866
+ fallbackReason: reason
867
+ };
868
+ } catch (fallbackErr) {
869
+ const combinedReason = `${reason}\n\nFallback to ${fallback} failed: ${formatErrorMessage(fallbackErr)}`;
870
+ if (isMissingApiKeyError(primaryErr) && isMissingApiKeyError(fallbackErr)) return {
871
+ provider: null,
872
+ requestedProvider,
873
+ fallbackFrom: requestedProvider,
874
+ fallbackReason: reason,
875
+ providerUnavailableReason: combinedReason
876
+ };
877
+ const wrapped = new Error(combinedReason);
878
+ wrapped.cause = fallbackErr;
879
+ throw wrapped;
880
+ }
881
+ if (isMissingApiKeyError(primaryErr)) return {
882
+ provider: null,
883
+ requestedProvider,
884
+ providerUnavailableReason: reason
885
+ };
886
+ const wrapped = new Error(reason);
887
+ wrapped.cause = primaryErr;
888
+ throw wrapped;
889
+ }
890
+ }
891
+ function isNodeLlamaCppMissing(err) {
892
+ if (!(err instanceof Error)) return false;
893
+ if (err.code === "ERR_MODULE_NOT_FOUND") return err.message.includes("node-llama-cpp");
894
+ return false;
895
+ }
896
+ function formatLocalSetupError(err) {
897
+ const detail = formatErrorMessage(err);
898
+ const missing = isNodeLlamaCppMissing(err);
899
+ return [
900
+ "Local embeddings unavailable.",
901
+ missing ? "Reason: optional dependency node-llama-cpp is missing (or failed to install)." : detail ? `Reason: ${detail}` : void 0,
902
+ missing && detail ? `Detail: ${detail}` : null,
903
+ "To enable local embeddings:",
904
+ "1) Use Node 22 LTS (recommended for installs/updates)",
905
+ missing ? "2) Reinstall OpenClaw (this should install node-llama-cpp): npm i -g openclaw@latest" : null,
906
+ "3) If you use pnpm: pnpm approve-builds (select node-llama-cpp), then pnpm rebuild node-llama-cpp",
907
+ ...REMOTE_EMBEDDING_PROVIDER_IDS.map((provider) => `Or set agents.defaults.memorySearch.provider = "${provider}" (remote).`)
908
+ ].filter(Boolean).join("\n");
909
+ }
910
+ //#endregion
911
+ //#region src/memory/mmr.ts
912
+ const DEFAULT_MMR_CONFIG = {
913
+ enabled: false,
914
+ lambda: .7
915
+ };
916
+ /**
917
+ * Tokenize text for Jaccard similarity computation.
918
+ * Extracts alphanumeric tokens and normalizes to lowercase.
919
+ */
920
+ function tokenize(text) {
921
+ const tokens = text.toLowerCase().match(/[a-z0-9_]+/g) ?? [];
922
+ return new Set(tokens);
923
+ }
924
+ /**
925
+ * Compute Jaccard similarity between two token sets.
926
+ * Returns a value in [0, 1] where 1 means identical sets.
927
+ */
928
+ function jaccardSimilarity(setA, setB) {
929
+ if (setA.size === 0 && setB.size === 0) return 1;
930
+ if (setA.size === 0 || setB.size === 0) return 0;
931
+ let intersectionSize = 0;
932
+ const smaller = setA.size <= setB.size ? setA : setB;
933
+ const larger = setA.size <= setB.size ? setB : setA;
934
+ for (const token of smaller) if (larger.has(token)) intersectionSize++;
935
+ const unionSize = setA.size + setB.size - intersectionSize;
936
+ return unionSize === 0 ? 0 : intersectionSize / unionSize;
937
+ }
938
+ /**
939
+ * Compute the maximum similarity between an item and all selected items.
940
+ */
941
+ function maxSimilarityToSelected(item, selectedItems, tokenCache) {
942
+ if (selectedItems.length === 0) return 0;
943
+ let maxSim = 0;
944
+ const itemTokens = tokenCache.get(item.id) ?? tokenize(item.content);
945
+ for (const selected of selectedItems) {
946
+ const sim = jaccardSimilarity(itemTokens, tokenCache.get(selected.id) ?? tokenize(selected.content));
947
+ if (sim > maxSim) maxSim = sim;
948
+ }
949
+ return maxSim;
950
+ }
951
+ /**
952
+ * Compute MMR score for a candidate item.
953
+ * MMR = λ * relevance - (1-λ) * max_similarity_to_selected
954
+ */
955
+ function computeMMRScore(relevance, maxSimilarity, lambda) {
956
+ return lambda * relevance - (1 - lambda) * maxSimilarity;
957
+ }
958
+ /**
959
+ * Re-rank items using Maximal Marginal Relevance (MMR).
960
+ *
961
+ * The algorithm iteratively selects items that balance relevance with diversity:
962
+ * 1. Start with the highest-scoring item
963
+ * 2. For each remaining slot, select the item that maximizes the MMR score
964
+ * 3. MMR score = λ * relevance - (1-λ) * max_similarity_to_already_selected
965
+ *
966
+ * @param items - Items to re-rank, must have score and content
967
+ * @param config - MMR configuration (lambda, enabled)
968
+ * @returns Re-ranked items in MMR order
969
+ */
970
+ function mmrRerank(items, config = {}) {
971
+ const { enabled = DEFAULT_MMR_CONFIG.enabled, lambda = DEFAULT_MMR_CONFIG.lambda } = config;
972
+ if (!enabled || items.length <= 1) return [...items];
973
+ const clampedLambda = Math.max(0, Math.min(1, lambda));
974
+ if (clampedLambda === 1) return [...items].toSorted((a, b) => b.score - a.score);
975
+ const tokenCache = /* @__PURE__ */ new Map();
976
+ for (const item of items) tokenCache.set(item.id, tokenize(item.content));
977
+ const maxScore = Math.max(...items.map((i) => i.score));
978
+ const minScore = Math.min(...items.map((i) => i.score));
979
+ const scoreRange = maxScore - minScore;
980
+ const normalizeScore = (score) => {
981
+ if (scoreRange === 0) return 1;
982
+ return (score - minScore) / scoreRange;
983
+ };
984
+ const selected = [];
985
+ const remaining = new Set(items);
986
+ while (remaining.size > 0) {
987
+ let bestItem = null;
988
+ let bestMMRScore = -Infinity;
989
+ for (const candidate of remaining) {
990
+ const mmrScore = computeMMRScore(normalizeScore(candidate.score), maxSimilarityToSelected(candidate, selected, tokenCache), clampedLambda);
991
+ if (mmrScore > bestMMRScore || mmrScore === bestMMRScore && candidate.score > (bestItem?.score ?? -Infinity)) {
992
+ bestMMRScore = mmrScore;
993
+ bestItem = candidate;
994
+ }
995
+ }
996
+ if (bestItem) {
997
+ selected.push(bestItem);
998
+ remaining.delete(bestItem);
999
+ } else break;
1000
+ }
1001
+ return selected;
1002
+ }
1003
+ /**
1004
+ * Apply MMR re-ranking to hybrid search results.
1005
+ * Adapts the generic MMR function to work with the hybrid search result format.
1006
+ */
1007
+ function applyMMRToHybridResults(results, config = {}) {
1008
+ if (results.length === 0) return results;
1009
+ const itemById = /* @__PURE__ */ new Map();
1010
+ return mmrRerank(results.map((r, index) => {
1011
+ const id = `${r.path}:${r.startLine}:${index}`;
1012
+ itemById.set(id, r);
1013
+ return {
1014
+ id,
1015
+ score: r.score,
1016
+ content: r.snippet
1017
+ };
1018
+ }), config).map((item) => itemById.get(item.id));
1019
+ }
1020
+ //#endregion
1021
+ //#region src/memory/temporal-decay.ts
1022
+ const DEFAULT_TEMPORAL_DECAY_CONFIG = {
1023
+ enabled: false,
1024
+ halfLifeDays: 30
1025
+ };
1026
+ const DAY_MS = 1440 * 60 * 1e3;
1027
+ const DATED_MEMORY_PATH_RE = /(?:^|\/)memory\/(\d{4})-(\d{2})-(\d{2})\.md$/;
1028
+ function toDecayLambda(halfLifeDays) {
1029
+ if (!Number.isFinite(halfLifeDays) || halfLifeDays <= 0) return 0;
1030
+ return Math.LN2 / halfLifeDays;
1031
+ }
1032
+ function calculateTemporalDecayMultiplier(params) {
1033
+ const lambda = toDecayLambda(params.halfLifeDays);
1034
+ const clampedAge = Math.max(0, params.ageInDays);
1035
+ if (lambda <= 0 || !Number.isFinite(clampedAge)) return 1;
1036
+ return Math.exp(-lambda * clampedAge);
1037
+ }
1038
+ function applyTemporalDecayToScore(params) {
1039
+ return params.score * calculateTemporalDecayMultiplier(params);
1040
+ }
1041
+ function parseMemoryDateFromPath(filePath) {
1042
+ const normalized = filePath.replaceAll("\\", "/").replace(/^\.\//, "");
1043
+ const match = DATED_MEMORY_PATH_RE.exec(normalized);
1044
+ if (!match) return null;
1045
+ const year = Number(match[1]);
1046
+ const month = Number(match[2]);
1047
+ const day = Number(match[3]);
1048
+ if (!Number.isInteger(year) || !Number.isInteger(month) || !Number.isInteger(day)) return null;
1049
+ const timestamp = Date.UTC(year, month - 1, day);
1050
+ const parsed = new Date(timestamp);
1051
+ if (parsed.getUTCFullYear() !== year || parsed.getUTCMonth() !== month - 1 || parsed.getUTCDate() !== day) return null;
1052
+ return parsed;
1053
+ }
1054
+ function isEvergreenMemoryPath(filePath) {
1055
+ const normalized = filePath.replaceAll("\\", "/").replace(/^\.\//, "");
1056
+ if (normalized === "MEMORY.md" || normalized === "memory.md") return true;
1057
+ if (!normalized.startsWith("memory/")) return false;
1058
+ return !DATED_MEMORY_PATH_RE.test(normalized);
1059
+ }
1060
+ async function extractTimestamp(params) {
1061
+ const fromPath = parseMemoryDateFromPath(params.filePath);
1062
+ if (fromPath) return fromPath;
1063
+ if (params.source === "memory" && isEvergreenMemoryPath(params.filePath)) return null;
1064
+ if (!params.workspaceDir) return null;
1065
+ const absolutePath = path.isAbsolute(params.filePath) ? params.filePath : path.resolve(params.workspaceDir, params.filePath);
1066
+ try {
1067
+ const stat = await fs$1.stat(absolutePath);
1068
+ if (!Number.isFinite(stat.mtimeMs)) return null;
1069
+ return new Date(stat.mtimeMs);
1070
+ } catch {
1071
+ return null;
1072
+ }
1073
+ }
1074
+ function ageInDaysFromTimestamp(timestamp, nowMs) {
1075
+ return Math.max(0, nowMs - timestamp.getTime()) / DAY_MS;
1076
+ }
1077
+ async function applyTemporalDecayToHybridResults(params) {
1078
+ const config = {
1079
+ ...DEFAULT_TEMPORAL_DECAY_CONFIG,
1080
+ ...params.temporalDecay
1081
+ };
1082
+ if (!config.enabled) return [...params.results];
1083
+ const nowMs = params.nowMs ?? Date.now();
1084
+ const timestampPromiseCache = /* @__PURE__ */ new Map();
1085
+ return Promise.all(params.results.map(async (entry) => {
1086
+ const cacheKey = `${entry.source}:${entry.path}`;
1087
+ let timestampPromise = timestampPromiseCache.get(cacheKey);
1088
+ if (!timestampPromise) {
1089
+ timestampPromise = extractTimestamp({
1090
+ filePath: entry.path,
1091
+ source: entry.source,
1092
+ workspaceDir: params.workspaceDir
1093
+ });
1094
+ timestampPromiseCache.set(cacheKey, timestampPromise);
1095
+ }
1096
+ const timestamp = await timestampPromise;
1097
+ if (!timestamp) return entry;
1098
+ const decayedScore = applyTemporalDecayToScore({
1099
+ score: entry.score,
1100
+ ageInDays: ageInDaysFromTimestamp(timestamp, nowMs),
1101
+ halfLifeDays: config.halfLifeDays
1102
+ });
1103
+ return {
1104
+ ...entry,
1105
+ score: decayedScore
1106
+ };
1107
+ }));
1108
+ }
1109
+ //#endregion
1110
+ //#region src/memory/hybrid.ts
1111
+ function buildFtsQuery(raw) {
1112
+ const tokens = raw.match(/[\p{L}\p{N}_]+/gu)?.map((t) => t.trim()).filter(Boolean) ?? [];
1113
+ if (tokens.length === 0) return null;
1114
+ return tokens.map((t) => `"${t.replaceAll("\"", "")}"`).join(" AND ");
1115
+ }
1116
+ function bm25RankToScore(rank) {
1117
+ if (!Number.isFinite(rank)) return 1 / 1e3;
1118
+ if (rank < 0) {
1119
+ const relevance = -rank;
1120
+ return relevance / (1 + relevance);
1121
+ }
1122
+ return 1 / (1 + rank);
1123
+ }
1124
+ async function mergeHybridResults(params) {
1125
+ const byId = /* @__PURE__ */ new Map();
1126
+ for (const r of params.vector) byId.set(r.id, {
1127
+ id: r.id,
1128
+ path: r.path,
1129
+ startLine: r.startLine,
1130
+ endLine: r.endLine,
1131
+ source: r.source,
1132
+ snippet: r.snippet,
1133
+ vectorScore: r.vectorScore,
1134
+ textScore: 0
1135
+ });
1136
+ for (const r of params.keyword) {
1137
+ const existing = byId.get(r.id);
1138
+ if (existing) {
1139
+ existing.textScore = r.textScore;
1140
+ if (r.snippet && r.snippet.length > 0) existing.snippet = r.snippet;
1141
+ } else byId.set(r.id, {
1142
+ id: r.id,
1143
+ path: r.path,
1144
+ startLine: r.startLine,
1145
+ endLine: r.endLine,
1146
+ source: r.source,
1147
+ snippet: r.snippet,
1148
+ vectorScore: 0,
1149
+ textScore: r.textScore
1150
+ });
1151
+ }
1152
+ const sorted = (await applyTemporalDecayToHybridResults({
1153
+ results: Array.from(byId.values()).map((entry) => {
1154
+ const score = params.vectorWeight * entry.vectorScore + params.textWeight * entry.textScore;
1155
+ return {
1156
+ path: entry.path,
1157
+ startLine: entry.startLine,
1158
+ endLine: entry.endLine,
1159
+ score,
1160
+ snippet: entry.snippet,
1161
+ source: entry.source
1162
+ };
1163
+ }),
1164
+ temporalDecay: {
1165
+ ...DEFAULT_TEMPORAL_DECAY_CONFIG,
1166
+ ...params.temporalDecay
1167
+ },
1168
+ workspaceDir: params.workspaceDir,
1169
+ nowMs: params.nowMs
1170
+ })).toSorted((a, b) => b.score - a.score);
1171
+ const mmrConfig = {
1172
+ ...DEFAULT_MMR_CONFIG,
1173
+ ...params.mmr
1174
+ };
1175
+ if (mmrConfig.enabled) return applyMMRToHybridResults(sorted, mmrConfig);
1176
+ return sorted;
1177
+ }
1178
+ //#endregion
1179
+ //#region src/memory/batch-utils.ts
1180
+ function normalizeBatchBaseUrl(client) {
1181
+ return client.baseUrl?.replace(/\/$/, "") ?? "";
1182
+ }
1183
+ function buildBatchHeaders(client, params) {
1184
+ const headers = client.headers ? { ...client.headers } : {};
1185
+ if (params.json) {
1186
+ if (!headers["Content-Type"] && !headers["content-type"]) headers["Content-Type"] = "application/json";
1187
+ } else {
1188
+ delete headers["Content-Type"];
1189
+ delete headers["content-type"];
1190
+ }
1191
+ return headers;
1192
+ }
1193
+ function splitBatchRequests(requests, maxRequests) {
1194
+ if (requests.length <= maxRequests) return [requests];
1195
+ const groups = [];
1196
+ for (let i = 0; i < requests.length; i += maxRequests) groups.push(requests.slice(i, i + maxRequests));
1197
+ return groups;
1198
+ }
1199
+ //#endregion
1200
+ //#region src/memory/batch-runner.ts
1201
+ async function runEmbeddingBatchGroups(params) {
1202
+ if (params.requests.length === 0) return /* @__PURE__ */ new Map();
1203
+ const groups = splitBatchRequests(params.requests, params.maxRequests);
1204
+ const byCustomId = /* @__PURE__ */ new Map();
1205
+ const tasks = groups.map((group, groupIndex) => async () => {
1206
+ await params.runGroup({
1207
+ group,
1208
+ groupIndex,
1209
+ groups: groups.length,
1210
+ byCustomId
1211
+ });
1212
+ });
1213
+ params.debug?.(params.debugLabel, {
1214
+ requests: params.requests.length,
1215
+ groups: groups.length,
1216
+ wait: params.wait,
1217
+ concurrency: params.concurrency,
1218
+ pollIntervalMs: params.pollIntervalMs,
1219
+ timeoutMs: params.timeoutMs
1220
+ });
1221
+ await runWithConcurrency(tasks, params.concurrency);
1222
+ return byCustomId;
1223
+ }
1224
+ function buildEmbeddingBatchGroupOptions(params, options) {
1225
+ return {
1226
+ requests: params.requests,
1227
+ maxRequests: options.maxRequests,
1228
+ wait: params.wait,
1229
+ pollIntervalMs: params.pollIntervalMs,
1230
+ timeoutMs: params.timeoutMs,
1231
+ concurrency: params.concurrency,
1232
+ debug: params.debug,
1233
+ debugLabel: options.debugLabel
1234
+ };
1235
+ }
1236
+ //#endregion
1237
+ //#region src/memory/batch-gemini.ts
1238
+ const GEMINI_BATCH_MAX_REQUESTS = 5e4;
1239
+ function getGeminiUploadUrl(baseUrl) {
1240
+ if (baseUrl.includes("/v1beta")) return baseUrl.replace(/\/v1beta\/?$/, "/upload/v1beta");
1241
+ return `${baseUrl.replace(/\/$/, "")}/upload`;
1242
+ }
1243
+ function buildGeminiUploadBody(params) {
1244
+ const boundary = `openclaw-${hashText(params.displayName)}`;
1245
+ const jsonPart = JSON.stringify({ file: {
1246
+ displayName: params.displayName,
1247
+ mimeType: "application/jsonl"
1248
+ } });
1249
+ const delimiter = `--${boundary}\r\n`;
1250
+ const closeDelimiter = `--${boundary}--\r\n`;
1251
+ const parts = [
1252
+ `${delimiter}Content-Type: application/json; charset=UTF-8\r\n\r\n${jsonPart}\r\n`,
1253
+ `${delimiter}Content-Type: application/jsonl; charset=UTF-8\r\n\r\n${params.jsonl}\r\n`,
1254
+ closeDelimiter
1255
+ ];
1256
+ return {
1257
+ body: new Blob([parts.join("")], { type: "multipart/related" }),
1258
+ contentType: `multipart/related; boundary=${boundary}`
1259
+ };
1260
+ }
1261
+ async function submitGeminiBatch(params) {
1262
+ const baseUrl = normalizeBatchBaseUrl(params.gemini);
1263
+ const uploadPayload = buildGeminiUploadBody({
1264
+ jsonl: params.requests.map((request) => JSON.stringify({
1265
+ key: request.custom_id,
1266
+ request: {
1267
+ content: request.content,
1268
+ task_type: request.taskType
1269
+ }
1270
+ })).join("\n"),
1271
+ displayName: `memory-embeddings-${hashText(String(Date.now()))}`
1272
+ });
1273
+ const uploadUrl = `${getGeminiUploadUrl(baseUrl)}/files?uploadType=multipart`;
1274
+ debugEmbeddingsLog("memory embeddings: gemini batch upload", {
1275
+ uploadUrl,
1276
+ baseUrl,
1277
+ requests: params.requests.length
1278
+ });
1279
+ const filePayload = await withRemoteHttpResponse({
1280
+ url: uploadUrl,
1281
+ ssrfPolicy: params.gemini.ssrfPolicy,
1282
+ init: {
1283
+ method: "POST",
1284
+ headers: {
1285
+ ...buildBatchHeaders(params.gemini, { json: false }),
1286
+ "Content-Type": uploadPayload.contentType
1287
+ },
1288
+ body: uploadPayload.body
1289
+ },
1290
+ onResponse: async (fileRes) => {
1291
+ if (!fileRes.ok) {
1292
+ const text = await fileRes.text();
1293
+ throw new Error(`gemini batch file upload failed: ${fileRes.status} ${text}`);
1294
+ }
1295
+ return await fileRes.json();
1296
+ }
1297
+ });
1298
+ const fileId = filePayload.name ?? filePayload.file?.name;
1299
+ if (!fileId) throw new Error("gemini batch file upload failed: missing file id");
1300
+ const batchBody = { batch: {
1301
+ displayName: `memory-embeddings-${params.agentId}`,
1302
+ inputConfig: { file_name: fileId }
1303
+ } };
1304
+ const batchEndpoint = `${baseUrl}/${params.gemini.modelPath}:asyncBatchEmbedContent`;
1305
+ debugEmbeddingsLog("memory embeddings: gemini batch create", {
1306
+ batchEndpoint,
1307
+ fileId
1308
+ });
1309
+ return await withRemoteHttpResponse({
1310
+ url: batchEndpoint,
1311
+ ssrfPolicy: params.gemini.ssrfPolicy,
1312
+ init: {
1313
+ method: "POST",
1314
+ headers: buildBatchHeaders(params.gemini, { json: true }),
1315
+ body: JSON.stringify(batchBody)
1316
+ },
1317
+ onResponse: async (batchRes) => {
1318
+ if (batchRes.ok) return await batchRes.json();
1319
+ const text = await batchRes.text();
1320
+ if (batchRes.status === 404) throw new Error("gemini batch create failed: 404 (asyncBatchEmbedContent not available for this model/baseUrl). Disable remote.batch.enabled or switch providers.");
1321
+ throw new Error(`gemini batch create failed: ${batchRes.status} ${text}`);
1322
+ }
1323
+ });
1324
+ }
1325
+ async function fetchGeminiBatchStatus(params) {
1326
+ const statusUrl = `${normalizeBatchBaseUrl(params.gemini)}/${params.batchName.startsWith("batches/") ? params.batchName : `batches/${params.batchName}`}`;
1327
+ debugEmbeddingsLog("memory embeddings: gemini batch status", { statusUrl });
1328
+ return await withRemoteHttpResponse({
1329
+ url: statusUrl,
1330
+ ssrfPolicy: params.gemini.ssrfPolicy,
1331
+ init: { headers: buildBatchHeaders(params.gemini, { json: true }) },
1332
+ onResponse: async (res) => {
1333
+ if (!res.ok) {
1334
+ const text = await res.text();
1335
+ throw new Error(`gemini batch status failed: ${res.status} ${text}`);
1336
+ }
1337
+ return await res.json();
1338
+ }
1339
+ });
1340
+ }
1341
+ async function fetchGeminiFileContent(params) {
1342
+ const downloadUrl = `${normalizeBatchBaseUrl(params.gemini)}/${params.fileId.startsWith("files/") ? params.fileId : `files/${params.fileId}`}:download`;
1343
+ debugEmbeddingsLog("memory embeddings: gemini batch download", { downloadUrl });
1344
+ return await withRemoteHttpResponse({
1345
+ url: downloadUrl,
1346
+ ssrfPolicy: params.gemini.ssrfPolicy,
1347
+ init: { headers: buildBatchHeaders(params.gemini, { json: true }) },
1348
+ onResponse: async (res) => {
1349
+ if (!res.ok) {
1350
+ const text = await res.text();
1351
+ throw new Error(`gemini batch file content failed: ${res.status} ${text}`);
1352
+ }
1353
+ return await res.text();
1354
+ }
1355
+ });
1356
+ }
1357
+ function parseGeminiBatchOutput(text) {
1358
+ if (!text.trim()) return [];
1359
+ return text.split("\n").map((line) => line.trim()).filter(Boolean).map((line) => JSON.parse(line));
1360
+ }
1361
+ async function waitForGeminiBatch(params) {
1362
+ const start = Date.now();
1363
+ let current = params.initial;
1364
+ while (true) {
1365
+ const status = current ?? await fetchGeminiBatchStatus({
1366
+ gemini: params.gemini,
1367
+ batchName: params.batchName
1368
+ });
1369
+ const state = status.state ?? "UNKNOWN";
1370
+ if ([
1371
+ "SUCCEEDED",
1372
+ "COMPLETED",
1373
+ "DONE"
1374
+ ].includes(state)) {
1375
+ const outputFileId = status.outputConfig?.file ?? status.outputConfig?.fileId ?? status.metadata?.output?.responsesFile;
1376
+ if (!outputFileId) throw new Error(`gemini batch ${params.batchName} completed without output file`);
1377
+ return { outputFileId };
1378
+ }
1379
+ if ([
1380
+ "FAILED",
1381
+ "CANCELLED",
1382
+ "CANCELED",
1383
+ "EXPIRED"
1384
+ ].includes(state)) {
1385
+ const message = status.error?.message ?? "unknown error";
1386
+ throw new Error(`gemini batch ${params.batchName} ${state}: ${message}`);
1387
+ }
1388
+ if (!params.wait) throw new Error(`gemini batch ${params.batchName} still ${state}; wait disabled`);
1389
+ if (Date.now() - start > params.timeoutMs) throw new Error(`gemini batch ${params.batchName} timed out after ${params.timeoutMs}ms`);
1390
+ params.debug?.(`gemini batch ${params.batchName} ${state}; waiting ${params.pollIntervalMs}ms`);
1391
+ await new Promise((resolve) => setTimeout(resolve, params.pollIntervalMs));
1392
+ current = void 0;
1393
+ }
1394
+ }
1395
+ async function runGeminiEmbeddingBatches(params) {
1396
+ return await runEmbeddingBatchGroups({
1397
+ ...buildEmbeddingBatchGroupOptions(params, {
1398
+ maxRequests: GEMINI_BATCH_MAX_REQUESTS,
1399
+ debugLabel: "memory embeddings: gemini batch submit"
1400
+ }),
1401
+ runGroup: async ({ group, groupIndex, groups, byCustomId }) => {
1402
+ const batchInfo = await submitGeminiBatch({
1403
+ gemini: params.gemini,
1404
+ requests: group,
1405
+ agentId: params.agentId
1406
+ });
1407
+ const batchName = batchInfo.name ?? "";
1408
+ if (!batchName) throw new Error("gemini batch create failed: missing batch name");
1409
+ params.debug?.("memory embeddings: gemini batch created", {
1410
+ batchName,
1411
+ state: batchInfo.state,
1412
+ group: groupIndex + 1,
1413
+ groups,
1414
+ requests: group.length
1415
+ });
1416
+ if (!params.wait && batchInfo.state && ![
1417
+ "SUCCEEDED",
1418
+ "COMPLETED",
1419
+ "DONE"
1420
+ ].includes(batchInfo.state)) throw new Error(`gemini batch ${batchName} submitted; enable remote.batch.wait to await completion`);
1421
+ const completed = batchInfo.state && [
1422
+ "SUCCEEDED",
1423
+ "COMPLETED",
1424
+ "DONE"
1425
+ ].includes(batchInfo.state) ? { outputFileId: batchInfo.outputConfig?.file ?? batchInfo.outputConfig?.fileId ?? batchInfo.metadata?.output?.responsesFile ?? "" } : await waitForGeminiBatch({
1426
+ gemini: params.gemini,
1427
+ batchName,
1428
+ wait: params.wait,
1429
+ pollIntervalMs: params.pollIntervalMs,
1430
+ timeoutMs: params.timeoutMs,
1431
+ debug: params.debug,
1432
+ initial: batchInfo
1433
+ });
1434
+ if (!completed.outputFileId) throw new Error(`gemini batch ${batchName} completed without output file`);
1435
+ const outputLines = parseGeminiBatchOutput(await fetchGeminiFileContent({
1436
+ gemini: params.gemini,
1437
+ fileId: completed.outputFileId
1438
+ }));
1439
+ const errors = [];
1440
+ const remaining = new Set(group.map((request) => request.custom_id));
1441
+ for (const line of outputLines) {
1442
+ const customId = line.key ?? line.custom_id ?? line.request_id;
1443
+ if (!customId) continue;
1444
+ remaining.delete(customId);
1445
+ if (line.error?.message) {
1446
+ errors.push(`${customId}: ${line.error.message}`);
1447
+ continue;
1448
+ }
1449
+ if (line.response?.error?.message) {
1450
+ errors.push(`${customId}: ${line.response.error.message}`);
1451
+ continue;
1452
+ }
1453
+ const embedding = line.embedding?.values ?? line.response?.embedding?.values ?? [];
1454
+ if (embedding.length === 0) {
1455
+ errors.push(`${customId}: empty embedding`);
1456
+ continue;
1457
+ }
1458
+ byCustomId.set(customId, embedding);
1459
+ }
1460
+ if (errors.length > 0) throw new Error(`gemini batch ${batchName} failed: ${errors.join("; ")}`);
1461
+ if (remaining.size > 0) throw new Error(`gemini batch ${batchName} missing ${remaining.size} embedding responses`);
1462
+ }
1463
+ });
1464
+ }
1465
+ //#endregion
1466
+ //#region src/memory/batch-error-utils.ts
1467
+ function getResponseErrorMessage(line) {
1468
+ const body = line?.response?.body;
1469
+ if (typeof body === "string") return body || void 0;
1470
+ if (!body || typeof body !== "object") return;
1471
+ return typeof body.error?.message === "string" ? body.error.message : void 0;
1472
+ }
1473
+ function extractBatchErrorMessage(lines) {
1474
+ const first = lines.find((line) => line.error?.message || getResponseErrorMessage(line));
1475
+ return first?.error?.message ?? getResponseErrorMessage(first);
1476
+ }
1477
+ function formatUnavailableBatchError(err) {
1478
+ const message = err instanceof Error ? err.message : String(err);
1479
+ return message ? `error file unavailable: ${message}` : void 0;
1480
+ }
1481
+ //#endregion
1482
+ //#region src/memory/batch-http.ts
1483
+ async function postJsonWithRetry(params) {
1484
+ return await retryAsync(async () => {
1485
+ return await postJson({
1486
+ url: params.url,
1487
+ headers: params.headers,
1488
+ ssrfPolicy: params.ssrfPolicy,
1489
+ body: params.body,
1490
+ errorPrefix: params.errorPrefix,
1491
+ attachStatus: true,
1492
+ parse: async (payload) => payload
1493
+ });
1494
+ }, {
1495
+ attempts: 3,
1496
+ minDelayMs: 300,
1497
+ maxDelayMs: 2e3,
1498
+ jitter: .2,
1499
+ shouldRetry: (err) => {
1500
+ const status = err.status;
1501
+ return status === 429 || typeof status === "number" && status >= 500;
1502
+ }
1503
+ });
1504
+ }
1505
+ //#endregion
1506
+ //#region src/memory/batch-output.ts
1507
+ function applyEmbeddingBatchOutputLine(params) {
1508
+ const customId = params.line.custom_id;
1509
+ if (!customId) return;
1510
+ params.remaining.delete(customId);
1511
+ const errorMessage = params.line.error?.message;
1512
+ if (errorMessage) {
1513
+ params.errors.push(`${customId}: ${errorMessage}`);
1514
+ return;
1515
+ }
1516
+ const response = params.line.response;
1517
+ if ((response?.status_code ?? 0) >= 400) {
1518
+ const messageFromObject = response?.body && typeof response.body === "object" ? response.body.error?.message : void 0;
1519
+ const messageFromString = typeof response?.body === "string" ? response.body : void 0;
1520
+ params.errors.push(`${customId}: ${messageFromObject ?? messageFromString ?? "unknown error"}`);
1521
+ return;
1522
+ }
1523
+ const embedding = (response?.body && typeof response.body === "object" ? response.body.data ?? [] : [])[0]?.embedding ?? [];
1524
+ if (embedding.length === 0) {
1525
+ params.errors.push(`${customId}: empty embedding`);
1526
+ return;
1527
+ }
1528
+ params.byCustomId.set(customId, embedding);
1529
+ }
1530
+ //#endregion
1531
+ //#region src/memory/batch-status.ts
1532
+ const TERMINAL_FAILURE_STATES = new Set([
1533
+ "failed",
1534
+ "expired",
1535
+ "cancelled",
1536
+ "canceled"
1537
+ ]);
1538
+ function resolveBatchCompletionFromStatus(params) {
1539
+ if (!params.status.output_file_id) throw new Error(`${params.provider} batch ${params.batchId} completed without output file`);
1540
+ return {
1541
+ outputFileId: params.status.output_file_id,
1542
+ errorFileId: params.status.error_file_id ?? void 0
1543
+ };
1544
+ }
1545
+ async function throwIfBatchTerminalFailure(params) {
1546
+ const state = params.status.status ?? "unknown";
1547
+ if (!TERMINAL_FAILURE_STATES.has(state)) return;
1548
+ const detail = params.status.error_file_id ? await params.readError(params.status.error_file_id) : void 0;
1549
+ const suffix = detail ? `: ${detail}` : "";
1550
+ throw new Error(`${params.provider} batch ${params.status.id ?? "<unknown>"} ${state}${suffix}`);
1551
+ }
1552
+ async function resolveCompletedBatchResult(params) {
1553
+ const batchId = params.status.id ?? "<unknown>";
1554
+ if (!params.wait && params.status.status !== "completed") throw new Error(`${params.provider} batch ${batchId} submitted; enable remote.batch.wait to await completion`);
1555
+ const completed = params.status.status === "completed" ? resolveBatchCompletionFromStatus({
1556
+ provider: params.provider,
1557
+ batchId,
1558
+ status: params.status
1559
+ }) : await params.waitForBatch();
1560
+ if (!completed.outputFileId) throw new Error(`${params.provider} batch ${batchId} completed without output file`);
1561
+ return completed;
1562
+ }
1563
+ //#endregion
1564
+ //#region src/memory/batch-provider-common.ts
1565
+ const EMBEDDING_BATCH_ENDPOINT = "/v1/embeddings";
1566
+ //#endregion
1567
+ //#region src/memory/batch-upload.ts
1568
+ async function uploadBatchJsonlFile(params) {
1569
+ const baseUrl = normalizeBatchBaseUrl(params.client);
1570
+ const jsonl = params.requests.map((request) => JSON.stringify(request)).join("\n");
1571
+ const form = new FormData();
1572
+ form.append("purpose", "batch");
1573
+ form.append("file", new Blob([jsonl], { type: "application/jsonl" }), `memory-embeddings.${hashText(String(Date.now()))}.jsonl`);
1574
+ const filePayload = await withRemoteHttpResponse({
1575
+ url: `${baseUrl}/files`,
1576
+ ssrfPolicy: params.client.ssrfPolicy,
1577
+ init: {
1578
+ method: "POST",
1579
+ headers: buildBatchHeaders(params.client, { json: false }),
1580
+ body: form
1581
+ },
1582
+ onResponse: async (fileRes) => {
1583
+ if (!fileRes.ok) {
1584
+ const text = await fileRes.text();
1585
+ throw new Error(`${params.errorPrefix}: ${fileRes.status} ${text}`);
1586
+ }
1587
+ return await fileRes.json();
1588
+ }
1589
+ });
1590
+ if (!filePayload.id) throw new Error(`${params.errorPrefix}: missing file id`);
1591
+ return filePayload.id;
1592
+ }
1593
+ //#endregion
1594
+ //#region src/memory/batch-openai.ts
1595
+ const OPENAI_BATCH_ENDPOINT = EMBEDDING_BATCH_ENDPOINT;
1596
+ const OPENAI_BATCH_COMPLETION_WINDOW = "24h";
1597
+ const OPENAI_BATCH_MAX_REQUESTS = 5e4;
1598
+ async function submitOpenAiBatch(params) {
1599
+ const baseUrl = normalizeBatchBaseUrl(params.openAi);
1600
+ const inputFileId = await uploadBatchJsonlFile({
1601
+ client: params.openAi,
1602
+ requests: params.requests,
1603
+ errorPrefix: "openai batch file upload failed"
1604
+ });
1605
+ return await postJsonWithRetry({
1606
+ url: `${baseUrl}/batches`,
1607
+ headers: buildBatchHeaders(params.openAi, { json: true }),
1608
+ ssrfPolicy: params.openAi.ssrfPolicy,
1609
+ body: {
1610
+ input_file_id: inputFileId,
1611
+ endpoint: OPENAI_BATCH_ENDPOINT,
1612
+ completion_window: OPENAI_BATCH_COMPLETION_WINDOW,
1613
+ metadata: {
1614
+ source: "openclaw-memory",
1615
+ agent: params.agentId
1616
+ }
1617
+ },
1618
+ errorPrefix: "openai batch create failed"
1619
+ });
1620
+ }
1621
+ async function fetchOpenAiBatchStatus(params) {
1622
+ return await fetchOpenAiBatchResource({
1623
+ openAi: params.openAi,
1624
+ path: `/batches/${params.batchId}`,
1625
+ errorPrefix: "openai batch status",
1626
+ parse: async (res) => await res.json()
1627
+ });
1628
+ }
1629
+ async function fetchOpenAiFileContent(params) {
1630
+ return await fetchOpenAiBatchResource({
1631
+ openAi: params.openAi,
1632
+ path: `/files/${params.fileId}/content`,
1633
+ errorPrefix: "openai batch file content",
1634
+ parse: async (res) => await res.text()
1635
+ });
1636
+ }
1637
+ async function fetchOpenAiBatchResource(params) {
1638
+ return await withRemoteHttpResponse({
1639
+ url: `${normalizeBatchBaseUrl(params.openAi)}${params.path}`,
1640
+ ssrfPolicy: params.openAi.ssrfPolicy,
1641
+ init: { headers: buildBatchHeaders(params.openAi, { json: true }) },
1642
+ onResponse: async (res) => {
1643
+ if (!res.ok) {
1644
+ const text = await res.text();
1645
+ throw new Error(`${params.errorPrefix} failed: ${res.status} ${text}`);
1646
+ }
1647
+ return await params.parse(res);
1648
+ }
1649
+ });
1650
+ }
1651
+ function parseOpenAiBatchOutput(text) {
1652
+ if (!text.trim()) return [];
1653
+ return text.split("\n").map((line) => line.trim()).filter(Boolean).map((line) => JSON.parse(line));
1654
+ }
1655
+ async function readOpenAiBatchError(params) {
1656
+ try {
1657
+ return extractBatchErrorMessage(parseOpenAiBatchOutput(await fetchOpenAiFileContent({
1658
+ openAi: params.openAi,
1659
+ fileId: params.errorFileId
1660
+ })));
1661
+ } catch (err) {
1662
+ return formatUnavailableBatchError(err);
1663
+ }
1664
+ }
1665
+ async function waitForOpenAiBatch(params) {
1666
+ const start = Date.now();
1667
+ let current = params.initial;
1668
+ while (true) {
1669
+ const status = current ?? await fetchOpenAiBatchStatus({
1670
+ openAi: params.openAi,
1671
+ batchId: params.batchId
1672
+ });
1673
+ const state = status.status ?? "unknown";
1674
+ if (state === "completed") return resolveBatchCompletionFromStatus({
1675
+ provider: "openai",
1676
+ batchId: params.batchId,
1677
+ status
1678
+ });
1679
+ await throwIfBatchTerminalFailure({
1680
+ provider: "openai",
1681
+ status: {
1682
+ ...status,
1683
+ id: params.batchId
1684
+ },
1685
+ readError: async (errorFileId) => await readOpenAiBatchError({
1686
+ openAi: params.openAi,
1687
+ errorFileId
1688
+ })
1689
+ });
1690
+ if (!params.wait) throw new Error(`openai batch ${params.batchId} still ${state}; wait disabled`);
1691
+ if (Date.now() - start > params.timeoutMs) throw new Error(`openai batch ${params.batchId} timed out after ${params.timeoutMs}ms`);
1692
+ params.debug?.(`openai batch ${params.batchId} ${state}; waiting ${params.pollIntervalMs}ms`);
1693
+ await new Promise((resolve) => setTimeout(resolve, params.pollIntervalMs));
1694
+ current = void 0;
1695
+ }
1696
+ }
1697
+ async function runOpenAiEmbeddingBatches(params) {
1698
+ return await runEmbeddingBatchGroups({
1699
+ ...buildEmbeddingBatchGroupOptions(params, {
1700
+ maxRequests: OPENAI_BATCH_MAX_REQUESTS,
1701
+ debugLabel: "memory embeddings: openai batch submit"
1702
+ }),
1703
+ runGroup: async ({ group, groupIndex, groups, byCustomId }) => {
1704
+ const batchInfo = await submitOpenAiBatch({
1705
+ openAi: params.openAi,
1706
+ requests: group,
1707
+ agentId: params.agentId
1708
+ });
1709
+ if (!batchInfo.id) throw new Error("openai batch create failed: missing batch id");
1710
+ const batchId = batchInfo.id;
1711
+ params.debug?.("memory embeddings: openai batch created", {
1712
+ batchId: batchInfo.id,
1713
+ status: batchInfo.status,
1714
+ group: groupIndex + 1,
1715
+ groups,
1716
+ requests: group.length
1717
+ });
1718
+ const completed = await resolveCompletedBatchResult({
1719
+ provider: "openai",
1720
+ status: batchInfo,
1721
+ wait: params.wait,
1722
+ waitForBatch: async () => await waitForOpenAiBatch({
1723
+ openAi: params.openAi,
1724
+ batchId,
1725
+ wait: params.wait,
1726
+ pollIntervalMs: params.pollIntervalMs,
1727
+ timeoutMs: params.timeoutMs,
1728
+ debug: params.debug,
1729
+ initial: batchInfo
1730
+ })
1731
+ });
1732
+ const outputLines = parseOpenAiBatchOutput(await fetchOpenAiFileContent({
1733
+ openAi: params.openAi,
1734
+ fileId: completed.outputFileId
1735
+ }));
1736
+ const errors = [];
1737
+ const remaining = new Set(group.map((request) => request.custom_id));
1738
+ for (const line of outputLines) applyEmbeddingBatchOutputLine({
1739
+ line,
1740
+ remaining,
1741
+ errors,
1742
+ byCustomId
1743
+ });
1744
+ if (errors.length > 0) throw new Error(`openai batch ${batchInfo.id} failed: ${errors.join("; ")}`);
1745
+ if (remaining.size > 0) throw new Error(`openai batch ${batchInfo.id} missing ${remaining.size} embedding responses`);
1746
+ }
1747
+ });
1748
+ }
1749
+ //#endregion
1750
+ //#region src/memory/batch-voyage.ts
1751
+ const VOYAGE_BATCH_ENDPOINT = EMBEDDING_BATCH_ENDPOINT;
1752
+ const VOYAGE_BATCH_COMPLETION_WINDOW = "12h";
1753
+ const VOYAGE_BATCH_MAX_REQUESTS = 5e4;
1754
+ async function assertVoyageResponseOk(res, context) {
1755
+ if (!res.ok) {
1756
+ const text = await res.text();
1757
+ throw new Error(`${context}: ${res.status} ${text}`);
1758
+ }
1759
+ }
1760
+ function buildVoyageBatchRequest(params) {
1761
+ return {
1762
+ url: `${normalizeBatchBaseUrl(params.client)}/${params.path}`,
1763
+ ssrfPolicy: params.client.ssrfPolicy,
1764
+ init: { headers: buildBatchHeaders(params.client, { json: true }) },
1765
+ onResponse: params.onResponse
1766
+ };
1767
+ }
1768
+ async function submitVoyageBatch(params) {
1769
+ const baseUrl = normalizeBatchBaseUrl(params.client);
1770
+ const inputFileId = await uploadBatchJsonlFile({
1771
+ client: params.client,
1772
+ requests: params.requests,
1773
+ errorPrefix: "voyage batch file upload failed"
1774
+ });
1775
+ return await postJsonWithRetry({
1776
+ url: `${baseUrl}/batches`,
1777
+ headers: buildBatchHeaders(params.client, { json: true }),
1778
+ ssrfPolicy: params.client.ssrfPolicy,
1779
+ body: {
1780
+ input_file_id: inputFileId,
1781
+ endpoint: VOYAGE_BATCH_ENDPOINT,
1782
+ completion_window: VOYAGE_BATCH_COMPLETION_WINDOW,
1783
+ request_params: {
1784
+ model: params.client.model,
1785
+ input_type: "document"
1786
+ },
1787
+ metadata: {
1788
+ source: "clawdbot-memory",
1789
+ agent: params.agentId
1790
+ }
1791
+ },
1792
+ errorPrefix: "voyage batch create failed"
1793
+ });
1794
+ }
1795
+ async function fetchVoyageBatchStatus(params) {
1796
+ return await withRemoteHttpResponse(buildVoyageBatchRequest({
1797
+ client: params.client,
1798
+ path: `batches/${params.batchId}`,
1799
+ onResponse: async (res) => {
1800
+ await assertVoyageResponseOk(res, "voyage batch status failed");
1801
+ return await res.json();
1802
+ }
1803
+ }));
1804
+ }
1805
+ async function readVoyageBatchError(params) {
1806
+ try {
1807
+ return await withRemoteHttpResponse(buildVoyageBatchRequest({
1808
+ client: params.client,
1809
+ path: `files/${params.errorFileId}/content`,
1810
+ onResponse: async (res) => {
1811
+ await assertVoyageResponseOk(res, "voyage batch error file content failed");
1812
+ const text = await res.text();
1813
+ if (!text.trim()) return;
1814
+ return extractBatchErrorMessage(text.split("\n").map((line) => line.trim()).filter(Boolean).map((line) => JSON.parse(line)));
1815
+ }
1816
+ }));
1817
+ } catch (err) {
1818
+ return formatUnavailableBatchError(err);
1819
+ }
1820
+ }
1821
+ async function waitForVoyageBatch(params) {
1822
+ const start = Date.now();
1823
+ let current = params.initial;
1824
+ while (true) {
1825
+ const status = current ?? await fetchVoyageBatchStatus({
1826
+ client: params.client,
1827
+ batchId: params.batchId
1828
+ });
1829
+ const state = status.status ?? "unknown";
1830
+ if (state === "completed") return resolveBatchCompletionFromStatus({
1831
+ provider: "voyage",
1832
+ batchId: params.batchId,
1833
+ status
1834
+ });
1835
+ await throwIfBatchTerminalFailure({
1836
+ provider: "voyage",
1837
+ status: {
1838
+ ...status,
1839
+ id: params.batchId
1840
+ },
1841
+ readError: async (errorFileId) => await readVoyageBatchError({
1842
+ client: params.client,
1843
+ errorFileId
1844
+ })
1845
+ });
1846
+ if (!params.wait) throw new Error(`voyage batch ${params.batchId} still ${state}; wait disabled`);
1847
+ if (Date.now() - start > params.timeoutMs) throw new Error(`voyage batch ${params.batchId} timed out after ${params.timeoutMs}ms`);
1848
+ params.debug?.(`voyage batch ${params.batchId} ${state}; waiting ${params.pollIntervalMs}ms`);
1849
+ await new Promise((resolve) => setTimeout(resolve, params.pollIntervalMs));
1850
+ current = void 0;
1851
+ }
1852
+ }
1853
+ async function runVoyageEmbeddingBatches(params) {
1854
+ return await runEmbeddingBatchGroups({
1855
+ ...buildEmbeddingBatchGroupOptions(params, {
1856
+ maxRequests: VOYAGE_BATCH_MAX_REQUESTS,
1857
+ debugLabel: "memory embeddings: voyage batch submit"
1858
+ }),
1859
+ runGroup: async ({ group, groupIndex, groups, byCustomId }) => {
1860
+ const batchInfo = await submitVoyageBatch({
1861
+ client: params.client,
1862
+ requests: group,
1863
+ agentId: params.agentId
1864
+ });
1865
+ if (!batchInfo.id) throw new Error("voyage batch create failed: missing batch id");
1866
+ const batchId = batchInfo.id;
1867
+ params.debug?.("memory embeddings: voyage batch created", {
1868
+ batchId: batchInfo.id,
1869
+ status: batchInfo.status,
1870
+ group: groupIndex + 1,
1871
+ groups,
1872
+ requests: group.length
1873
+ });
1874
+ const completed = await resolveCompletedBatchResult({
1875
+ provider: "voyage",
1876
+ status: batchInfo,
1877
+ wait: params.wait,
1878
+ waitForBatch: async () => await waitForVoyageBatch({
1879
+ client: params.client,
1880
+ batchId,
1881
+ wait: params.wait,
1882
+ pollIntervalMs: params.pollIntervalMs,
1883
+ timeoutMs: params.timeoutMs,
1884
+ debug: params.debug,
1885
+ initial: batchInfo
1886
+ })
1887
+ });
1888
+ const baseUrl = normalizeBatchBaseUrl(params.client);
1889
+ const errors = [];
1890
+ const remaining = new Set(group.map((request) => request.custom_id));
1891
+ await withRemoteHttpResponse({
1892
+ url: `${baseUrl}/files/${completed.outputFileId}/content`,
1893
+ ssrfPolicy: params.client.ssrfPolicy,
1894
+ init: { headers: buildBatchHeaders(params.client, { json: true }) },
1895
+ onResponse: async (contentRes) => {
1896
+ if (!contentRes.ok) {
1897
+ const text = await contentRes.text();
1898
+ throw new Error(`voyage batch file content failed: ${contentRes.status} ${text}`);
1899
+ }
1900
+ if (!contentRes.body) return;
1901
+ const reader = createInterface({
1902
+ input: Readable.fromWeb(contentRes.body),
1903
+ terminal: false
1904
+ });
1905
+ for await (const rawLine of reader) {
1906
+ if (!rawLine.trim()) continue;
1907
+ applyEmbeddingBatchOutputLine({
1908
+ line: JSON.parse(rawLine),
1909
+ remaining,
1910
+ errors,
1911
+ byCustomId
1912
+ });
1913
+ }
1914
+ }
1915
+ });
1916
+ if (errors.length > 0) throw new Error(`voyage batch ${batchInfo.id} failed: ${errors.join("; ")}`);
1917
+ if (remaining.size > 0) throw new Error(`voyage batch ${batchInfo.id} missing ${remaining.size} embedding responses`);
1918
+ }
1919
+ });
1920
+ }
1921
+ //#endregion
1922
+ //#region src/memory/embedding-input-limits.ts
1923
+ function estimateUtf8Bytes(text) {
1924
+ if (!text) return 0;
1925
+ return Buffer.byteLength(text, "utf8");
1926
+ }
1927
+ function splitTextToUtf8ByteLimit(text, maxUtf8Bytes) {
1928
+ if (maxUtf8Bytes <= 0) return [text];
1929
+ if (estimateUtf8Bytes(text) <= maxUtf8Bytes) return [text];
1930
+ const parts = [];
1931
+ let cursor = 0;
1932
+ while (cursor < text.length) {
1933
+ let low = cursor + 1;
1934
+ let high = Math.min(text.length, cursor + maxUtf8Bytes);
1935
+ let best = cursor;
1936
+ while (low <= high) {
1937
+ const mid = Math.floor((low + high) / 2);
1938
+ if (estimateUtf8Bytes(text.slice(cursor, mid)) <= maxUtf8Bytes) {
1939
+ best = mid;
1940
+ low = mid + 1;
1941
+ } else high = mid - 1;
1942
+ }
1943
+ if (best <= cursor) best = Math.min(text.length, cursor + 1);
1944
+ if (best < text.length && best > cursor && text.charCodeAt(best - 1) >= 55296 && text.charCodeAt(best - 1) <= 56319 && text.charCodeAt(best) >= 56320 && text.charCodeAt(best) <= 57343) best -= 1;
1945
+ const part = text.slice(cursor, best);
1946
+ if (!part) break;
1947
+ parts.push(part);
1948
+ cursor = best;
1949
+ }
1950
+ return parts;
1951
+ }
1952
+ //#endregion
1953
+ //#region src/memory/embedding-model-limits.ts
1954
+ const DEFAULT_EMBEDDING_MAX_INPUT_TOKENS = 8192;
1955
+ const DEFAULT_LOCAL_EMBEDDING_MAX_INPUT_TOKENS = 2048;
1956
+ const KNOWN_EMBEDDING_MAX_INPUT_TOKENS = {
1957
+ "openai:text-embedding-3-small": 8192,
1958
+ "openai:text-embedding-3-large": 8192,
1959
+ "openai:text-embedding-ada-002": 8191,
1960
+ "gemini:text-embedding-004": 2048,
1961
+ "voyage:voyage-3": 32e3,
1962
+ "voyage:voyage-3-lite": 16e3,
1963
+ "voyage:voyage-code-3": 32e3
1964
+ };
1965
+ function resolveEmbeddingMaxInputTokens(provider) {
1966
+ if (typeof provider.maxInputTokens === "number") return provider.maxInputTokens;
1967
+ const known = KNOWN_EMBEDDING_MAX_INPUT_TOKENS[`${provider.id}:${provider.model}`.toLowerCase()];
1968
+ if (typeof known === "number") return known;
1969
+ if (provider.id.toLowerCase() === "gemini") return 2048;
1970
+ if (provider.id.toLowerCase() === "local") return DEFAULT_LOCAL_EMBEDDING_MAX_INPUT_TOKENS;
1971
+ return DEFAULT_EMBEDDING_MAX_INPUT_TOKENS;
1972
+ }
1973
+ //#endregion
1974
+ //#region src/memory/embedding-chunk-limits.ts
1975
+ function enforceEmbeddingMaxInputTokens(provider, chunks, hardMaxInputTokens) {
1976
+ const providerMaxInputTokens = resolveEmbeddingMaxInputTokens(provider);
1977
+ const maxInputTokens = typeof hardMaxInputTokens === "number" && hardMaxInputTokens > 0 ? Math.min(providerMaxInputTokens, hardMaxInputTokens) : providerMaxInputTokens;
1978
+ const out = [];
1979
+ for (const chunk of chunks) {
1980
+ if (estimateUtf8Bytes(chunk.text) <= maxInputTokens) {
1981
+ out.push(chunk);
1982
+ continue;
1983
+ }
1984
+ for (const text of splitTextToUtf8ByteLimit(chunk.text, maxInputTokens)) out.push({
1985
+ startLine: chunk.startLine,
1986
+ endLine: chunk.endLine,
1987
+ text,
1988
+ hash: hashText(text)
1989
+ });
1990
+ }
1991
+ return out;
1992
+ }
1993
+ //#endregion
1994
+ //#region src/memory/memory-schema.ts
1995
+ function ensureMemoryIndexSchema(params) {
1996
+ params.db.exec(`
1997
+ CREATE TABLE IF NOT EXISTS meta (
1998
+ key TEXT PRIMARY KEY,
1999
+ value TEXT NOT NULL
2000
+ );
2001
+ `);
2002
+ params.db.exec(`
2003
+ CREATE TABLE IF NOT EXISTS files (
2004
+ path TEXT PRIMARY KEY,
2005
+ source TEXT NOT NULL DEFAULT 'memory',
2006
+ hash TEXT NOT NULL,
2007
+ mtime INTEGER NOT NULL,
2008
+ size INTEGER NOT NULL
2009
+ );
2010
+ `);
2011
+ params.db.exec(`
2012
+ CREATE TABLE IF NOT EXISTS chunks (
2013
+ id TEXT PRIMARY KEY,
2014
+ path TEXT NOT NULL,
2015
+ source TEXT NOT NULL DEFAULT 'memory',
2016
+ start_line INTEGER NOT NULL,
2017
+ end_line INTEGER NOT NULL,
2018
+ hash TEXT NOT NULL,
2019
+ model TEXT NOT NULL,
2020
+ text TEXT NOT NULL,
2021
+ embedding TEXT NOT NULL,
2022
+ updated_at INTEGER NOT NULL
2023
+ );
2024
+ `);
2025
+ params.db.exec(`
2026
+ CREATE TABLE IF NOT EXISTS ${params.embeddingCacheTable} (
2027
+ provider TEXT NOT NULL,
2028
+ model TEXT NOT NULL,
2029
+ provider_key TEXT NOT NULL,
2030
+ hash TEXT NOT NULL,
2031
+ embedding TEXT NOT NULL,
2032
+ dims INTEGER,
2033
+ updated_at INTEGER NOT NULL,
2034
+ PRIMARY KEY (provider, model, provider_key, hash)
2035
+ );
2036
+ `);
2037
+ params.db.exec(`CREATE INDEX IF NOT EXISTS idx_embedding_cache_updated_at ON ${params.embeddingCacheTable}(updated_at);`);
2038
+ let ftsAvailable = false;
2039
+ let ftsError;
2040
+ if (params.ftsEnabled) try {
2041
+ params.db.exec(`CREATE VIRTUAL TABLE IF NOT EXISTS ${params.ftsTable} USING fts5(\n text,\n id UNINDEXED,\n path UNINDEXED,\n source UNINDEXED,\n model UNINDEXED,\n start_line UNINDEXED,\n end_line UNINDEXED\n);`);
2042
+ ftsAvailable = true;
2043
+ } catch (err) {
2044
+ const message = err instanceof Error ? err.message : String(err);
2045
+ ftsAvailable = false;
2046
+ ftsError = message;
2047
+ }
2048
+ ensureColumn(params.db, "files", "source", "TEXT NOT NULL DEFAULT 'memory'");
2049
+ ensureColumn(params.db, "chunks", "source", "TEXT NOT NULL DEFAULT 'memory'");
2050
+ params.db.exec(`CREATE INDEX IF NOT EXISTS idx_chunks_path ON chunks(path);`);
2051
+ params.db.exec(`CREATE INDEX IF NOT EXISTS idx_chunks_source ON chunks(source);`);
2052
+ return {
2053
+ ftsAvailable,
2054
+ ...ftsError ? { ftsError } : {}
2055
+ };
2056
+ }
2057
+ function ensureColumn(db, table, column, definition) {
2058
+ if (db.prepare(`PRAGMA table_info(${table})`).all().some((row) => row.name === column)) return;
2059
+ db.exec(`ALTER TABLE ${table} ADD COLUMN ${column} ${definition}`);
2060
+ }
2061
+ //#endregion
2062
+ //#region src/memory/sqlite-vec.ts
2063
+ async function loadSqliteVecExtension(params) {
2064
+ try {
2065
+ const sqliteVec = await import("sqlite-vec");
2066
+ const resolvedPath = params.extensionPath?.trim() ? params.extensionPath.trim() : void 0;
2067
+ const extensionPath = resolvedPath ?? sqliteVec.getLoadablePath();
2068
+ params.db.enableLoadExtension(true);
2069
+ if (resolvedPath) params.db.loadExtension(extensionPath);
2070
+ else sqliteVec.load(params.db);
2071
+ return {
2072
+ ok: true,
2073
+ extensionPath
2074
+ };
2075
+ } catch (err) {
2076
+ return {
2077
+ ok: false,
2078
+ error: err instanceof Error ? err.message : String(err)
2079
+ };
2080
+ }
2081
+ }
2082
+ //#endregion
2083
+ //#region src/memory/manager-sync-ops.ts
2084
+ const META_KEY = "memory_index_meta_v1";
2085
+ const VECTOR_TABLE$2 = "chunks_vec";
2086
+ const FTS_TABLE$2 = "chunks_fts";
2087
+ const EMBEDDING_CACHE_TABLE$2 = "embedding_cache";
2088
+ const SESSION_DIRTY_DEBOUNCE_MS = 5e3;
2089
+ const SESSION_DELTA_READ_CHUNK_BYTES = 64 * 1024;
2090
+ const VECTOR_LOAD_TIMEOUT_MS = 3e4;
2091
+ const IGNORED_MEMORY_WATCH_DIR_NAMES = new Set([
2092
+ ".git",
2093
+ "node_modules",
2094
+ ".pnpm-store",
2095
+ ".venv",
2096
+ "venv",
2097
+ ".tox",
2098
+ "__pycache__"
2099
+ ]);
2100
+ const log$2 = createSubsystemLogger("memory");
2101
+ function shouldIgnoreMemoryWatchPath(watchPath) {
2102
+ return path.normalize(watchPath).split(path.sep).map((segment) => segment.trim().toLowerCase()).some((segment) => IGNORED_MEMORY_WATCH_DIR_NAMES.has(segment));
2103
+ }
2104
+ var MemoryManagerSyncOps = class {
2105
+ constructor() {
2106
+ this.provider = null;
2107
+ this.sources = /* @__PURE__ */ new Set();
2108
+ this.providerKey = null;
2109
+ this.fts = {
2110
+ enabled: false,
2111
+ available: false
2112
+ };
2113
+ this.vectorReady = null;
2114
+ this.watcher = null;
2115
+ this.watchTimer = null;
2116
+ this.sessionWatchTimer = null;
2117
+ this.sessionUnsubscribe = null;
2118
+ this.intervalTimer = null;
2119
+ this.closed = false;
2120
+ this.dirty = false;
2121
+ this.sessionsDirty = false;
2122
+ this.sessionsDirtyFiles = /* @__PURE__ */ new Set();
2123
+ this.sessionPendingFiles = /* @__PURE__ */ new Set();
2124
+ this.sessionDeltas = /* @__PURE__ */ new Map();
2125
+ this.lastMetaSerialized = null;
2126
+ }
2127
+ async ensureVectorReady(dimensions) {
2128
+ if (!this.vector.enabled) return false;
2129
+ if (!this.vectorReady) this.vectorReady = this.withTimeout(this.loadVectorExtension(), VECTOR_LOAD_TIMEOUT_MS, `sqlite-vec load timed out after ${Math.round(VECTOR_LOAD_TIMEOUT_MS / 1e3)}s`);
2130
+ let ready = false;
2131
+ try {
2132
+ ready = await this.vectorReady || false;
2133
+ } catch (err) {
2134
+ const message = err instanceof Error ? err.message : String(err);
2135
+ this.vector.available = false;
2136
+ this.vector.loadError = message;
2137
+ this.vectorReady = null;
2138
+ log$2.warn(`sqlite-vec unavailable: ${message}`);
2139
+ return false;
2140
+ }
2141
+ if (ready && typeof dimensions === "number" && dimensions > 0) this.ensureVectorTable(dimensions);
2142
+ return ready;
2143
+ }
2144
+ async loadVectorExtension() {
2145
+ if (this.vector.available !== null) return this.vector.available;
2146
+ if (!this.vector.enabled) {
2147
+ this.vector.available = false;
2148
+ return false;
2149
+ }
2150
+ try {
2151
+ const resolvedPath = this.vector.extensionPath?.trim() ? resolveUserPath(this.vector.extensionPath) : void 0;
2152
+ const loaded = await loadSqliteVecExtension({
2153
+ db: this.db,
2154
+ extensionPath: resolvedPath
2155
+ });
2156
+ if (!loaded.ok) throw new Error(loaded.error ?? "unknown sqlite-vec load error");
2157
+ this.vector.extensionPath = loaded.extensionPath;
2158
+ this.vector.available = true;
2159
+ return true;
2160
+ } catch (err) {
2161
+ const message = err instanceof Error ? err.message : String(err);
2162
+ this.vector.available = false;
2163
+ this.vector.loadError = message;
2164
+ log$2.warn(`sqlite-vec unavailable: ${message}`);
2165
+ return false;
2166
+ }
2167
+ }
2168
+ ensureVectorTable(dimensions) {
2169
+ if (this.vector.dims === dimensions) return;
2170
+ if (this.vector.dims && this.vector.dims !== dimensions) this.dropVectorTable();
2171
+ this.db.exec(`CREATE VIRTUAL TABLE IF NOT EXISTS ${VECTOR_TABLE$2} USING vec0(\n id TEXT PRIMARY KEY,\n embedding FLOAT[${dimensions}]\n)`);
2172
+ this.vector.dims = dimensions;
2173
+ }
2174
+ dropVectorTable() {
2175
+ try {
2176
+ this.db.exec(`DROP TABLE IF EXISTS ${VECTOR_TABLE$2}`);
2177
+ } catch (err) {
2178
+ const message = err instanceof Error ? err.message : String(err);
2179
+ log$2.debug(`Failed to drop ${VECTOR_TABLE$2}: ${message}`);
2180
+ }
2181
+ }
2182
+ buildSourceFilter(alias) {
2183
+ const sources = Array.from(this.sources);
2184
+ if (sources.length === 0) return {
2185
+ sql: "",
2186
+ params: []
2187
+ };
2188
+ return {
2189
+ sql: ` AND ${alias ? `${alias}.source` : "source"} IN (${sources.map(() => "?").join(", ")})`,
2190
+ params: sources
2191
+ };
2192
+ }
2193
+ openDatabase() {
2194
+ const dbPath = resolveUserPath(this.settings.store.path);
2195
+ return this.openDatabaseAtPath(dbPath);
2196
+ }
2197
+ openDatabaseAtPath(dbPath) {
2198
+ ensureDir(path.dirname(dbPath));
2199
+ const { DatabaseSync } = requireNodeSqlite();
2200
+ const db = new DatabaseSync(dbPath, { allowExtension: this.settings.store.vector.enabled });
2201
+ db.exec("PRAGMA busy_timeout = 5000");
2202
+ return db;
2203
+ }
2204
+ seedEmbeddingCache(sourceDb) {
2205
+ if (!this.cache.enabled) return;
2206
+ try {
2207
+ const rows = sourceDb.prepare(`SELECT provider, model, provider_key, hash, embedding, dims, updated_at FROM ${EMBEDDING_CACHE_TABLE$2}`).all();
2208
+ if (!rows.length) return;
2209
+ const insert = this.db.prepare(`INSERT INTO ${EMBEDDING_CACHE_TABLE$2} (provider, model, provider_key, hash, embedding, dims, updated_at)
2210
+ VALUES (?, ?, ?, ?, ?, ?, ?)
2211
+ ON CONFLICT(provider, model, provider_key, hash) DO UPDATE SET
2212
+ embedding=excluded.embedding,
2213
+ dims=excluded.dims,
2214
+ updated_at=excluded.updated_at`);
2215
+ this.db.exec("BEGIN");
2216
+ for (const row of rows) insert.run(row.provider, row.model, row.provider_key, row.hash, row.embedding, row.dims, row.updated_at);
2217
+ this.db.exec("COMMIT");
2218
+ } catch (err) {
2219
+ try {
2220
+ this.db.exec("ROLLBACK");
2221
+ } catch {}
2222
+ throw err;
2223
+ }
2224
+ }
2225
+ async swapIndexFiles(targetPath, tempPath) {
2226
+ const backupPath = `${targetPath}.backup-${randomUUID()}`;
2227
+ await this.moveIndexFiles(targetPath, backupPath);
2228
+ try {
2229
+ await this.moveIndexFiles(tempPath, targetPath);
2230
+ } catch (err) {
2231
+ await this.moveIndexFiles(backupPath, targetPath);
2232
+ throw err;
2233
+ }
2234
+ await this.removeIndexFiles(backupPath);
2235
+ }
2236
+ async moveIndexFiles(sourceBase, targetBase) {
2237
+ for (const suffix of [
2238
+ "",
2239
+ "-wal",
2240
+ "-shm"
2241
+ ]) {
2242
+ const source = `${sourceBase}${suffix}`;
2243
+ const target = `${targetBase}${suffix}`;
2244
+ try {
2245
+ await fs$1.rename(source, target);
2246
+ } catch (err) {
2247
+ if (err.code !== "ENOENT") throw err;
2248
+ }
2249
+ }
2250
+ }
2251
+ async removeIndexFiles(basePath) {
2252
+ await Promise.all([
2253
+ "",
2254
+ "-wal",
2255
+ "-shm"
2256
+ ].map((suffix) => fs$1.rm(`${basePath}${suffix}`, { force: true })));
2257
+ }
2258
+ ensureSchema() {
2259
+ const result = ensureMemoryIndexSchema({
2260
+ db: this.db,
2261
+ embeddingCacheTable: EMBEDDING_CACHE_TABLE$2,
2262
+ ftsTable: FTS_TABLE$2,
2263
+ ftsEnabled: this.fts.enabled
2264
+ });
2265
+ this.fts.available = result.ftsAvailable;
2266
+ if (result.ftsError) {
2267
+ this.fts.loadError = result.ftsError;
2268
+ if (this.fts.enabled) log$2.warn(`fts unavailable: ${result.ftsError}`);
2269
+ }
2270
+ }
2271
+ ensureWatcher() {
2272
+ if (!this.sources.has("memory") || !this.settings.sync.watch || this.watcher) return;
2273
+ const watchPaths = new Set([
2274
+ path.join(this.workspaceDir, "MEMORY.md"),
2275
+ path.join(this.workspaceDir, "memory.md"),
2276
+ path.join(this.workspaceDir, "memory", "**", "*.md")
2277
+ ]);
2278
+ const additionalPaths = normalizeExtraMemoryPaths(this.workspaceDir, this.settings.extraPaths);
2279
+ for (const entry of additionalPaths) try {
2280
+ const stat = fs.lstatSync(entry);
2281
+ if (stat.isSymbolicLink()) continue;
2282
+ if (stat.isDirectory()) {
2283
+ watchPaths.add(path.join(entry, "**", "*.md"));
2284
+ continue;
2285
+ }
2286
+ if (stat.isFile() && entry.toLowerCase().endsWith(".md")) watchPaths.add(entry);
2287
+ } catch {}
2288
+ this.watcher = chokidar.watch(Array.from(watchPaths), {
2289
+ ignoreInitial: true,
2290
+ ignored: (watchPath) => shouldIgnoreMemoryWatchPath(String(watchPath)),
2291
+ awaitWriteFinish: {
2292
+ stabilityThreshold: this.settings.sync.watchDebounceMs,
2293
+ pollInterval: 100
2294
+ }
2295
+ });
2296
+ const markDirty = () => {
2297
+ this.dirty = true;
2298
+ this.scheduleWatchSync();
2299
+ };
2300
+ this.watcher.on("add", markDirty);
2301
+ this.watcher.on("change", markDirty);
2302
+ this.watcher.on("unlink", markDirty);
2303
+ }
2304
+ ensureSessionListener() {
2305
+ if (!this.sources.has("sessions") || this.sessionUnsubscribe) return;
2306
+ this.sessionUnsubscribe = onSessionTranscriptUpdate((update) => {
2307
+ if (this.closed) return;
2308
+ const sessionFile = update.sessionFile;
2309
+ if (!this.isSessionFileForAgent(sessionFile)) return;
2310
+ this.scheduleSessionDirty(sessionFile);
2311
+ });
2312
+ }
2313
+ scheduleSessionDirty(sessionFile) {
2314
+ this.sessionPendingFiles.add(sessionFile);
2315
+ if (this.sessionWatchTimer) return;
2316
+ this.sessionWatchTimer = setTimeout(() => {
2317
+ this.sessionWatchTimer = null;
2318
+ this.processSessionDeltaBatch().catch((err) => {
2319
+ log$2.warn(`memory session delta failed: ${String(err)}`);
2320
+ });
2321
+ }, SESSION_DIRTY_DEBOUNCE_MS);
2322
+ }
2323
+ async processSessionDeltaBatch() {
2324
+ if (this.sessionPendingFiles.size === 0) return;
2325
+ const pending = Array.from(this.sessionPendingFiles);
2326
+ this.sessionPendingFiles.clear();
2327
+ let shouldSync = false;
2328
+ for (const sessionFile of pending) {
2329
+ const delta = await this.updateSessionDelta(sessionFile);
2330
+ if (!delta) continue;
2331
+ const bytesThreshold = delta.deltaBytes;
2332
+ const messagesThreshold = delta.deltaMessages;
2333
+ const bytesHit = bytesThreshold <= 0 ? delta.pendingBytes > 0 : delta.pendingBytes >= bytesThreshold;
2334
+ const messagesHit = messagesThreshold <= 0 ? delta.pendingMessages > 0 : delta.pendingMessages >= messagesThreshold;
2335
+ if (!bytesHit && !messagesHit) continue;
2336
+ this.sessionsDirtyFiles.add(sessionFile);
2337
+ this.sessionsDirty = true;
2338
+ delta.pendingBytes = bytesThreshold > 0 ? Math.max(0, delta.pendingBytes - bytesThreshold) : 0;
2339
+ delta.pendingMessages = messagesThreshold > 0 ? Math.max(0, delta.pendingMessages - messagesThreshold) : 0;
2340
+ shouldSync = true;
2341
+ }
2342
+ if (shouldSync) this.sync({ reason: "session-delta" }).catch((err) => {
2343
+ log$2.warn(`memory sync failed (session-delta): ${String(err)}`);
2344
+ });
2345
+ }
2346
+ async updateSessionDelta(sessionFile) {
2347
+ const thresholds = this.settings.sync.sessions;
2348
+ if (!thresholds) return null;
2349
+ let stat;
2350
+ try {
2351
+ stat = await fs$1.stat(sessionFile);
2352
+ } catch {
2353
+ return null;
2354
+ }
2355
+ const size = stat.size;
2356
+ let state = this.sessionDeltas.get(sessionFile);
2357
+ if (!state) {
2358
+ state = {
2359
+ lastSize: 0,
2360
+ pendingBytes: 0,
2361
+ pendingMessages: 0
2362
+ };
2363
+ this.sessionDeltas.set(sessionFile, state);
2364
+ }
2365
+ const deltaBytes = Math.max(0, size - state.lastSize);
2366
+ if (deltaBytes === 0 && size === state.lastSize) return {
2367
+ deltaBytes: thresholds.deltaBytes,
2368
+ deltaMessages: thresholds.deltaMessages,
2369
+ pendingBytes: state.pendingBytes,
2370
+ pendingMessages: state.pendingMessages
2371
+ };
2372
+ if (size < state.lastSize) {
2373
+ state.lastSize = size;
2374
+ state.pendingBytes += size;
2375
+ if (thresholds.deltaMessages > 0 && (thresholds.deltaBytes <= 0 || state.pendingBytes < thresholds.deltaBytes)) state.pendingMessages += await this.countNewlines(sessionFile, 0, size);
2376
+ } else {
2377
+ state.pendingBytes += deltaBytes;
2378
+ if (thresholds.deltaMessages > 0 && (thresholds.deltaBytes <= 0 || state.pendingBytes < thresholds.deltaBytes)) state.pendingMessages += await this.countNewlines(sessionFile, state.lastSize, size);
2379
+ state.lastSize = size;
2380
+ }
2381
+ this.sessionDeltas.set(sessionFile, state);
2382
+ return {
2383
+ deltaBytes: thresholds.deltaBytes,
2384
+ deltaMessages: thresholds.deltaMessages,
2385
+ pendingBytes: state.pendingBytes,
2386
+ pendingMessages: state.pendingMessages
2387
+ };
2388
+ }
2389
+ async countNewlines(absPath, start, end) {
2390
+ if (end <= start) return 0;
2391
+ let handle;
2392
+ try {
2393
+ handle = await fs$1.open(absPath, "r");
2394
+ } catch (err) {
2395
+ if (isFileMissingError(err)) return 0;
2396
+ throw err;
2397
+ }
2398
+ try {
2399
+ let offset = start;
2400
+ let count = 0;
2401
+ const buffer = Buffer.alloc(SESSION_DELTA_READ_CHUNK_BYTES);
2402
+ while (offset < end) {
2403
+ const toRead = Math.min(buffer.length, end - offset);
2404
+ const { bytesRead } = await handle.read(buffer, 0, toRead, offset);
2405
+ if (bytesRead <= 0) break;
2406
+ for (let i = 0; i < bytesRead; i += 1) if (buffer[i] === 10) count += 1;
2407
+ offset += bytesRead;
2408
+ }
2409
+ return count;
2410
+ } finally {
2411
+ await handle.close();
2412
+ }
2413
+ }
2414
+ resetSessionDelta(absPath, size) {
2415
+ const state = this.sessionDeltas.get(absPath);
2416
+ if (!state) return;
2417
+ state.lastSize = size;
2418
+ state.pendingBytes = 0;
2419
+ state.pendingMessages = 0;
2420
+ }
2421
+ isSessionFileForAgent(sessionFile) {
2422
+ if (!sessionFile) return false;
2423
+ const sessionsDir = resolveSessionTranscriptsDirForAgent(this.agentId);
2424
+ const resolvedFile = path.resolve(sessionFile);
2425
+ const resolvedDir = path.resolve(sessionsDir);
2426
+ return resolvedFile.startsWith(`${resolvedDir}${path.sep}`);
2427
+ }
2428
+ ensureIntervalSync() {
2429
+ const minutes = this.settings.sync.intervalMinutes;
2430
+ if (!minutes || minutes <= 0 || this.intervalTimer) return;
2431
+ const ms = minutes * 60 * 1e3;
2432
+ this.intervalTimer = setInterval(() => {
2433
+ this.sync({ reason: "interval" }).catch((err) => {
2434
+ log$2.warn(`memory sync failed (interval): ${String(err)}`);
2435
+ });
2436
+ }, ms);
2437
+ }
2438
+ scheduleWatchSync() {
2439
+ if (!this.sources.has("memory") || !this.settings.sync.watch) return;
2440
+ if (this.watchTimer) clearTimeout(this.watchTimer);
2441
+ this.watchTimer = setTimeout(() => {
2442
+ this.watchTimer = null;
2443
+ this.sync({ reason: "watch" }).catch((err) => {
2444
+ log$2.warn(`memory sync failed (watch): ${String(err)}`);
2445
+ });
2446
+ }, this.settings.sync.watchDebounceMs);
2447
+ }
2448
+ shouldSyncSessions(params, needsFullReindex = false) {
2449
+ if (!this.sources.has("sessions")) return false;
2450
+ if (params?.force) return true;
2451
+ const reason = params?.reason;
2452
+ if (reason === "session-start" || reason === "watch") return false;
2453
+ if (needsFullReindex) return true;
2454
+ return this.sessionsDirty && this.sessionsDirtyFiles.size > 0;
2455
+ }
2456
+ async syncMemoryFiles(params) {
2457
+ if (!this.provider) {
2458
+ log$2.debug("Skipping memory file sync in FTS-only mode (no embedding provider)");
2459
+ return;
2460
+ }
2461
+ const files = await listMemoryFiles(this.workspaceDir, this.settings.extraPaths);
2462
+ const fileEntries = (await Promise.all(files.map(async (file) => buildFileEntry(file, this.workspaceDir)))).filter((entry) => entry !== null);
2463
+ log$2.debug("memory sync: indexing memory files", {
2464
+ files: fileEntries.length,
2465
+ needsFullReindex: params.needsFullReindex,
2466
+ batch: this.batch.enabled,
2467
+ concurrency: this.getIndexConcurrency()
2468
+ });
2469
+ const activePaths = new Set(fileEntries.map((entry) => entry.path));
2470
+ if (params.progress) {
2471
+ params.progress.total += fileEntries.length;
2472
+ params.progress.report({
2473
+ completed: params.progress.completed,
2474
+ total: params.progress.total,
2475
+ label: this.batch.enabled ? "Indexing memory files (batch)..." : "Indexing memory files…"
2476
+ });
2477
+ }
2478
+ await runWithConcurrency(fileEntries.map((entry) => async () => {
2479
+ const record = this.db.prepare(`SELECT hash FROM files WHERE path = ? AND source = ?`).get(entry.path, "memory");
2480
+ if (!params.needsFullReindex && record?.hash === entry.hash) {
2481
+ if (params.progress) {
2482
+ params.progress.completed += 1;
2483
+ params.progress.report({
2484
+ completed: params.progress.completed,
2485
+ total: params.progress.total
2486
+ });
2487
+ }
2488
+ return;
2489
+ }
2490
+ await this.indexFile(entry, { source: "memory" });
2491
+ if (params.progress) {
2492
+ params.progress.completed += 1;
2493
+ params.progress.report({
2494
+ completed: params.progress.completed,
2495
+ total: params.progress.total
2496
+ });
2497
+ }
2498
+ }), this.getIndexConcurrency());
2499
+ const staleRows = this.db.prepare(`SELECT path FROM files WHERE source = ?`).all("memory");
2500
+ for (const stale of staleRows) {
2501
+ if (activePaths.has(stale.path)) continue;
2502
+ this.db.prepare(`DELETE FROM files WHERE path = ? AND source = ?`).run(stale.path, "memory");
2503
+ try {
2504
+ this.db.prepare(`DELETE FROM ${VECTOR_TABLE$2} WHERE id IN (SELECT id FROM chunks WHERE path = ? AND source = ?)`).run(stale.path, "memory");
2505
+ } catch {}
2506
+ this.db.prepare(`DELETE FROM chunks WHERE path = ? AND source = ?`).run(stale.path, "memory");
2507
+ if (this.fts.enabled && this.fts.available) try {
2508
+ this.db.prepare(`DELETE FROM ${FTS_TABLE$2} WHERE path = ? AND source = ? AND model = ?`).run(stale.path, "memory", this.provider.model);
2509
+ } catch {}
2510
+ }
2511
+ }
2512
+ async syncSessionFiles(params) {
2513
+ if (!this.provider) {
2514
+ log$2.debug("Skipping session file sync in FTS-only mode (no embedding provider)");
2515
+ return;
2516
+ }
2517
+ const files = await listSessionFilesForAgent(this.agentId);
2518
+ const activePaths = new Set(files.map((file) => sessionPathForFile(file)));
2519
+ const indexAll = params.needsFullReindex || this.sessionsDirtyFiles.size === 0;
2520
+ log$2.debug("memory sync: indexing session files", {
2521
+ files: files.length,
2522
+ indexAll,
2523
+ dirtyFiles: this.sessionsDirtyFiles.size,
2524
+ batch: this.batch.enabled,
2525
+ concurrency: this.getIndexConcurrency()
2526
+ });
2527
+ if (params.progress) {
2528
+ params.progress.total += files.length;
2529
+ params.progress.report({
2530
+ completed: params.progress.completed,
2531
+ total: params.progress.total,
2532
+ label: this.batch.enabled ? "Indexing session files (batch)..." : "Indexing session files…"
2533
+ });
2534
+ }
2535
+ await runWithConcurrency(files.map((absPath) => async () => {
2536
+ if (!indexAll && !this.sessionsDirtyFiles.has(absPath)) {
2537
+ if (params.progress) {
2538
+ params.progress.completed += 1;
2539
+ params.progress.report({
2540
+ completed: params.progress.completed,
2541
+ total: params.progress.total
2542
+ });
2543
+ }
2544
+ return;
2545
+ }
2546
+ const entry = await buildSessionEntry(absPath);
2547
+ if (!entry) {
2548
+ if (params.progress) {
2549
+ params.progress.completed += 1;
2550
+ params.progress.report({
2551
+ completed: params.progress.completed,
2552
+ total: params.progress.total
2553
+ });
2554
+ }
2555
+ return;
2556
+ }
2557
+ const record = this.db.prepare(`SELECT hash FROM files WHERE path = ? AND source = ?`).get(entry.path, "sessions");
2558
+ if (!params.needsFullReindex && record?.hash === entry.hash) {
2559
+ if (params.progress) {
2560
+ params.progress.completed += 1;
2561
+ params.progress.report({
2562
+ completed: params.progress.completed,
2563
+ total: params.progress.total
2564
+ });
2565
+ }
2566
+ this.resetSessionDelta(absPath, entry.size);
2567
+ return;
2568
+ }
2569
+ await this.indexFile(entry, {
2570
+ source: "sessions",
2571
+ content: entry.content
2572
+ });
2573
+ this.resetSessionDelta(absPath, entry.size);
2574
+ if (params.progress) {
2575
+ params.progress.completed += 1;
2576
+ params.progress.report({
2577
+ completed: params.progress.completed,
2578
+ total: params.progress.total
2579
+ });
2580
+ }
2581
+ }), this.getIndexConcurrency());
2582
+ const staleRows = this.db.prepare(`SELECT path FROM files WHERE source = ?`).all("sessions");
2583
+ for (const stale of staleRows) {
2584
+ if (activePaths.has(stale.path)) continue;
2585
+ this.db.prepare(`DELETE FROM files WHERE path = ? AND source = ?`).run(stale.path, "sessions");
2586
+ try {
2587
+ this.db.prepare(`DELETE FROM ${VECTOR_TABLE$2} WHERE id IN (SELECT id FROM chunks WHERE path = ? AND source = ?)`).run(stale.path, "sessions");
2588
+ } catch {}
2589
+ this.db.prepare(`DELETE FROM chunks WHERE path = ? AND source = ?`).run(stale.path, "sessions");
2590
+ if (this.fts.enabled && this.fts.available) try {
2591
+ this.db.prepare(`DELETE FROM ${FTS_TABLE$2} WHERE path = ? AND source = ? AND model = ?`).run(stale.path, "sessions", this.provider.model);
2592
+ } catch {}
2593
+ }
2594
+ }
2595
+ createSyncProgress(onProgress) {
2596
+ const state = {
2597
+ completed: 0,
2598
+ total: 0,
2599
+ label: void 0,
2600
+ report: (update) => {
2601
+ if (update.label) state.label = update.label;
2602
+ const label = update.total > 0 && state.label ? `${state.label} ${update.completed}/${update.total}` : state.label;
2603
+ onProgress({
2604
+ completed: update.completed,
2605
+ total: update.total,
2606
+ label
2607
+ });
2608
+ }
2609
+ };
2610
+ return state;
2611
+ }
2612
+ async runSync(params) {
2613
+ const progress = params?.progress ? this.createSyncProgress(params.progress) : void 0;
2614
+ if (progress) progress.report({
2615
+ completed: progress.completed,
2616
+ total: progress.total,
2617
+ label: "Loading vector extension…"
2618
+ });
2619
+ const vectorReady = await this.ensureVectorReady();
2620
+ const meta = this.readMeta();
2621
+ const configuredSources = this.resolveConfiguredSourcesForMeta();
2622
+ const needsFullReindex = params?.force || !meta || this.provider && meta.model !== this.provider.model || this.provider && meta.provider !== this.provider.id || meta.providerKey !== this.providerKey || this.metaSourcesDiffer(meta, configuredSources) || meta.chunkTokens !== this.settings.chunking.tokens || meta.chunkOverlap !== this.settings.chunking.overlap || vectorReady && !meta?.vectorDims;
2623
+ try {
2624
+ if (needsFullReindex) {
2625
+ if (process.env.OPENCLAW_TEST_FAST === "1" && process.env.OPENCLAW_TEST_MEMORY_UNSAFE_REINDEX === "1") await this.runUnsafeReindex({
2626
+ reason: params?.reason,
2627
+ force: params?.force,
2628
+ progress: progress ?? void 0
2629
+ });
2630
+ else await this.runSafeReindex({
2631
+ reason: params?.reason,
2632
+ force: params?.force,
2633
+ progress: progress ?? void 0
2634
+ });
2635
+ return;
2636
+ }
2637
+ const shouldSyncMemory = this.sources.has("memory") && (params?.force || needsFullReindex || this.dirty);
2638
+ const shouldSyncSessions = this.shouldSyncSessions(params, needsFullReindex);
2639
+ if (shouldSyncMemory) {
2640
+ await this.syncMemoryFiles({
2641
+ needsFullReindex,
2642
+ progress: progress ?? void 0
2643
+ });
2644
+ this.dirty = false;
2645
+ }
2646
+ if (shouldSyncSessions) {
2647
+ await this.syncSessionFiles({
2648
+ needsFullReindex,
2649
+ progress: progress ?? void 0
2650
+ });
2651
+ this.sessionsDirty = false;
2652
+ this.sessionsDirtyFiles.clear();
2653
+ } else if (this.sessionsDirtyFiles.size > 0) this.sessionsDirty = true;
2654
+ else this.sessionsDirty = false;
2655
+ } catch (err) {
2656
+ const reason = err instanceof Error ? err.message : String(err);
2657
+ if (this.shouldFallbackOnError(reason) && await this.activateFallbackProvider(reason)) {
2658
+ await this.runSafeReindex({
2659
+ reason: params?.reason ?? "fallback",
2660
+ force: true,
2661
+ progress: progress ?? void 0
2662
+ });
2663
+ return;
2664
+ }
2665
+ throw err;
2666
+ }
2667
+ }
2668
+ shouldFallbackOnError(message) {
2669
+ return /embedding|embeddings|batch/i.test(message);
2670
+ }
2671
+ resolveBatchConfig() {
2672
+ const batch = this.settings.remote?.batch;
2673
+ return {
2674
+ enabled: Boolean(batch?.enabled && this.provider && (this.openAi && this.provider.id === "openai" || this.gemini && this.provider.id === "gemini" || this.voyage && this.provider.id === "voyage")),
2675
+ wait: batch?.wait ?? true,
2676
+ concurrency: Math.max(1, batch?.concurrency ?? 2),
2677
+ pollIntervalMs: batch?.pollIntervalMs ?? 2e3,
2678
+ timeoutMs: (batch?.timeoutMinutes ?? 60) * 60 * 1e3
2679
+ };
2680
+ }
2681
+ async activateFallbackProvider(reason) {
2682
+ const fallback = this.settings.fallback;
2683
+ if (!fallback || fallback === "none" || !this.provider || fallback === this.provider.id) return false;
2684
+ if (this.fallbackFrom) return false;
2685
+ const fallbackFrom = this.provider.id;
2686
+ const fallbackModel = fallback === "gemini" ? DEFAULT_GEMINI_EMBEDDING_MODEL : fallback === "openai" ? DEFAULT_OPENAI_EMBEDDING_MODEL : fallback === "voyage" ? DEFAULT_VOYAGE_EMBEDDING_MODEL : fallback === "mistral" ? DEFAULT_MISTRAL_EMBEDDING_MODEL : fallback === "ollama" ? DEFAULT_OLLAMA_EMBEDDING_MODEL : this.settings.model;
2687
+ const fallbackResult = await createEmbeddingProvider({
2688
+ config: this.cfg,
2689
+ agentDir: resolveAgentDir(this.cfg, this.agentId),
2690
+ provider: fallback,
2691
+ remote: this.settings.remote,
2692
+ model: fallbackModel,
2693
+ fallback: "none",
2694
+ local: this.settings.local
2695
+ });
2696
+ this.fallbackFrom = fallbackFrom;
2697
+ this.fallbackReason = reason;
2698
+ this.provider = fallbackResult.provider;
2699
+ this.openAi = fallbackResult.openAi;
2700
+ this.gemini = fallbackResult.gemini;
2701
+ this.voyage = fallbackResult.voyage;
2702
+ this.mistral = fallbackResult.mistral;
2703
+ this.ollama = fallbackResult.ollama;
2704
+ this.providerKey = this.computeProviderKey();
2705
+ this.batch = this.resolveBatchConfig();
2706
+ log$2.warn(`memory embeddings: switched to fallback provider (${fallback})`, { reason });
2707
+ return true;
2708
+ }
2709
+ async runSafeReindex(params) {
2710
+ const dbPath = resolveUserPath(this.settings.store.path);
2711
+ const tempDbPath = `${dbPath}.tmp-${randomUUID()}`;
2712
+ const tempDb = this.openDatabaseAtPath(tempDbPath);
2713
+ const originalDb = this.db;
2714
+ let originalDbClosed = false;
2715
+ const originalState = {
2716
+ ftsAvailable: this.fts.available,
2717
+ ftsError: this.fts.loadError,
2718
+ vectorAvailable: this.vector.available,
2719
+ vectorLoadError: this.vector.loadError,
2720
+ vectorDims: this.vector.dims,
2721
+ vectorReady: this.vectorReady
2722
+ };
2723
+ const restoreOriginalState = () => {
2724
+ if (originalDbClosed) this.db = this.openDatabaseAtPath(dbPath);
2725
+ else this.db = originalDb;
2726
+ this.fts.available = originalState.ftsAvailable;
2727
+ this.fts.loadError = originalState.ftsError;
2728
+ this.vector.available = originalDbClosed ? null : originalState.vectorAvailable;
2729
+ this.vector.loadError = originalState.vectorLoadError;
2730
+ this.vector.dims = originalState.vectorDims;
2731
+ this.vectorReady = originalDbClosed ? null : originalState.vectorReady;
2732
+ };
2733
+ this.db = tempDb;
2734
+ this.vectorReady = null;
2735
+ this.vector.available = null;
2736
+ this.vector.loadError = void 0;
2737
+ this.vector.dims = void 0;
2738
+ this.fts.available = false;
2739
+ this.fts.loadError = void 0;
2740
+ this.ensureSchema();
2741
+ let nextMeta = null;
2742
+ try {
2743
+ this.seedEmbeddingCache(originalDb);
2744
+ const shouldSyncMemory = this.sources.has("memory");
2745
+ const shouldSyncSessions = this.shouldSyncSessions({
2746
+ reason: params.reason,
2747
+ force: params.force
2748
+ }, true);
2749
+ if (shouldSyncMemory) {
2750
+ await this.syncMemoryFiles({
2751
+ needsFullReindex: true,
2752
+ progress: params.progress
2753
+ });
2754
+ this.dirty = false;
2755
+ }
2756
+ if (shouldSyncSessions) {
2757
+ await this.syncSessionFiles({
2758
+ needsFullReindex: true,
2759
+ progress: params.progress
2760
+ });
2761
+ this.sessionsDirty = false;
2762
+ this.sessionsDirtyFiles.clear();
2763
+ } else if (this.sessionsDirtyFiles.size > 0) this.sessionsDirty = true;
2764
+ else this.sessionsDirty = false;
2765
+ nextMeta = {
2766
+ model: this.provider?.model ?? "fts-only",
2767
+ provider: this.provider?.id ?? "none",
2768
+ providerKey: this.providerKey,
2769
+ sources: this.resolveConfiguredSourcesForMeta(),
2770
+ chunkTokens: this.settings.chunking.tokens,
2771
+ chunkOverlap: this.settings.chunking.overlap
2772
+ };
2773
+ if (!nextMeta) throw new Error("Failed to compute memory index metadata for reindexing.");
2774
+ if (this.vector.available && this.vector.dims) nextMeta.vectorDims = this.vector.dims;
2775
+ this.writeMeta(nextMeta);
2776
+ this.pruneEmbeddingCacheIfNeeded?.();
2777
+ this.db.close();
2778
+ originalDb.close();
2779
+ originalDbClosed = true;
2780
+ await this.swapIndexFiles(dbPath, tempDbPath);
2781
+ this.db = this.openDatabaseAtPath(dbPath);
2782
+ this.vectorReady = null;
2783
+ this.vector.available = null;
2784
+ this.vector.loadError = void 0;
2785
+ this.ensureSchema();
2786
+ this.vector.dims = nextMeta?.vectorDims;
2787
+ } catch (err) {
2788
+ try {
2789
+ this.db.close();
2790
+ } catch {}
2791
+ await this.removeIndexFiles(tempDbPath);
2792
+ restoreOriginalState();
2793
+ throw err;
2794
+ }
2795
+ }
2796
+ async runUnsafeReindex(params) {
2797
+ this.resetIndex();
2798
+ const shouldSyncMemory = this.sources.has("memory");
2799
+ const shouldSyncSessions = this.shouldSyncSessions({
2800
+ reason: params.reason,
2801
+ force: params.force
2802
+ }, true);
2803
+ if (shouldSyncMemory) {
2804
+ await this.syncMemoryFiles({
2805
+ needsFullReindex: true,
2806
+ progress: params.progress
2807
+ });
2808
+ this.dirty = false;
2809
+ }
2810
+ if (shouldSyncSessions) {
2811
+ await this.syncSessionFiles({
2812
+ needsFullReindex: true,
2813
+ progress: params.progress
2814
+ });
2815
+ this.sessionsDirty = false;
2816
+ this.sessionsDirtyFiles.clear();
2817
+ } else if (this.sessionsDirtyFiles.size > 0) this.sessionsDirty = true;
2818
+ else this.sessionsDirty = false;
2819
+ const nextMeta = {
2820
+ model: this.provider?.model ?? "fts-only",
2821
+ provider: this.provider?.id ?? "none",
2822
+ providerKey: this.providerKey,
2823
+ sources: this.resolveConfiguredSourcesForMeta(),
2824
+ chunkTokens: this.settings.chunking.tokens,
2825
+ chunkOverlap: this.settings.chunking.overlap
2826
+ };
2827
+ if (this.vector.available && this.vector.dims) nextMeta.vectorDims = this.vector.dims;
2828
+ this.writeMeta(nextMeta);
2829
+ this.pruneEmbeddingCacheIfNeeded?.();
2830
+ }
2831
+ resetIndex() {
2832
+ this.db.exec(`DELETE FROM files`);
2833
+ this.db.exec(`DELETE FROM chunks`);
2834
+ if (this.fts.enabled && this.fts.available) try {
2835
+ this.db.exec(`DELETE FROM ${FTS_TABLE$2}`);
2836
+ } catch {}
2837
+ this.dropVectorTable();
2838
+ this.vector.dims = void 0;
2839
+ this.sessionsDirtyFiles.clear();
2840
+ }
2841
+ readMeta() {
2842
+ const row = this.db.prepare(`SELECT value FROM meta WHERE key = ?`).get(META_KEY);
2843
+ if (!row?.value) {
2844
+ this.lastMetaSerialized = null;
2845
+ return null;
2846
+ }
2847
+ try {
2848
+ const parsed = JSON.parse(row.value);
2849
+ this.lastMetaSerialized = row.value;
2850
+ return parsed;
2851
+ } catch {
2852
+ this.lastMetaSerialized = null;
2853
+ return null;
2854
+ }
2855
+ }
2856
+ writeMeta(meta) {
2857
+ const value = JSON.stringify(meta);
2858
+ if (this.lastMetaSerialized === value) return;
2859
+ this.db.prepare(`INSERT INTO meta (key, value) VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value=excluded.value`).run(META_KEY, value);
2860
+ this.lastMetaSerialized = value;
2861
+ }
2862
+ resolveConfiguredSourcesForMeta() {
2863
+ const normalized = Array.from(this.sources).filter((source) => source === "memory" || source === "sessions").toSorted();
2864
+ return normalized.length > 0 ? normalized : ["memory"];
2865
+ }
2866
+ normalizeMetaSources(meta) {
2867
+ if (!Array.isArray(meta.sources)) return ["memory"];
2868
+ const normalized = Array.from(new Set(meta.sources.filter((source) => source === "memory" || source === "sessions"))).toSorted();
2869
+ return normalized.length > 0 ? normalized : ["memory"];
2870
+ }
2871
+ metaSourcesDiffer(meta, configuredSources) {
2872
+ const metaSources = this.normalizeMetaSources(meta);
2873
+ if (metaSources.length !== configuredSources.length) return true;
2874
+ return metaSources.some((source, index) => source !== configuredSources[index]);
2875
+ }
2876
+ };
2877
+ //#endregion
2878
+ //#region src/memory/manager-embedding-ops.ts
2879
+ const VECTOR_TABLE$1 = "chunks_vec";
2880
+ const FTS_TABLE$1 = "chunks_fts";
2881
+ const EMBEDDING_CACHE_TABLE$1 = "embedding_cache";
2882
+ const EMBEDDING_BATCH_MAX_TOKENS = 8e3;
2883
+ const EMBEDDING_INDEX_CONCURRENCY = 4;
2884
+ const EMBEDDING_RETRY_MAX_ATTEMPTS = 3;
2885
+ const EMBEDDING_RETRY_BASE_DELAY_MS = 500;
2886
+ const EMBEDDING_RETRY_MAX_DELAY_MS = 8e3;
2887
+ const BATCH_FAILURE_LIMIT$1 = 2;
2888
+ const EMBEDDING_QUERY_TIMEOUT_REMOTE_MS = 6e4;
2889
+ const EMBEDDING_QUERY_TIMEOUT_LOCAL_MS = 5 * 6e4;
2890
+ const EMBEDDING_BATCH_TIMEOUT_REMOTE_MS = 2 * 6e4;
2891
+ const EMBEDDING_BATCH_TIMEOUT_LOCAL_MS = 10 * 6e4;
2892
+ const vectorToBlob$1 = (embedding) => Buffer.from(new Float32Array(embedding).buffer);
2893
+ const log$1 = createSubsystemLogger("memory");
2894
+ var MemoryManagerEmbeddingOps = class extends MemoryManagerSyncOps {
2895
+ buildEmbeddingBatches(chunks) {
2896
+ const batches = [];
2897
+ let current = [];
2898
+ let currentTokens = 0;
2899
+ for (const chunk of chunks) {
2900
+ const estimate = estimateUtf8Bytes(chunk.text);
2901
+ if (current.length > 0 && currentTokens + estimate > EMBEDDING_BATCH_MAX_TOKENS) {
2902
+ batches.push(current);
2903
+ current = [];
2904
+ currentTokens = 0;
2905
+ }
2906
+ if (current.length === 0 && estimate > EMBEDDING_BATCH_MAX_TOKENS) {
2907
+ batches.push([chunk]);
2908
+ continue;
2909
+ }
2910
+ current.push(chunk);
2911
+ currentTokens += estimate;
2912
+ }
2913
+ if (current.length > 0) batches.push(current);
2914
+ return batches;
2915
+ }
2916
+ loadEmbeddingCache(hashes) {
2917
+ if (!this.cache.enabled || !this.provider) return /* @__PURE__ */ new Map();
2918
+ if (hashes.length === 0) return /* @__PURE__ */ new Map();
2919
+ const unique = [];
2920
+ const seen = /* @__PURE__ */ new Set();
2921
+ for (const hash of hashes) {
2922
+ if (!hash) continue;
2923
+ if (seen.has(hash)) continue;
2924
+ seen.add(hash);
2925
+ unique.push(hash);
2926
+ }
2927
+ if (unique.length === 0) return /* @__PURE__ */ new Map();
2928
+ const out = /* @__PURE__ */ new Map();
2929
+ const baseParams = [
2930
+ this.provider.id,
2931
+ this.provider.model,
2932
+ this.providerKey
2933
+ ];
2934
+ const batchSize = 400;
2935
+ for (let start = 0; start < unique.length; start += batchSize) {
2936
+ const batch = unique.slice(start, start + batchSize);
2937
+ const placeholders = batch.map(() => "?").join(", ");
2938
+ const rows = this.db.prepare(`SELECT hash, embedding FROM ${EMBEDDING_CACHE_TABLE$1}\n WHERE provider = ? AND model = ? AND provider_key = ? AND hash IN (${placeholders})`).all(...baseParams, ...batch);
2939
+ for (const row of rows) out.set(row.hash, parseEmbedding(row.embedding));
2940
+ }
2941
+ return out;
2942
+ }
2943
+ upsertEmbeddingCache(entries) {
2944
+ if (!this.cache.enabled || !this.provider) return;
2945
+ if (entries.length === 0) return;
2946
+ const now = Date.now();
2947
+ const stmt = this.db.prepare(`INSERT INTO ${EMBEDDING_CACHE_TABLE$1} (provider, model, provider_key, hash, embedding, dims, updated_at)\n VALUES (?, ?, ?, ?, ?, ?, ?)\n ON CONFLICT(provider, model, provider_key, hash) DO UPDATE SET\n embedding=excluded.embedding,\n dims=excluded.dims,\n updated_at=excluded.updated_at`);
2948
+ for (const entry of entries) {
2949
+ const embedding = entry.embedding ?? [];
2950
+ stmt.run(this.provider.id, this.provider.model, this.providerKey, entry.hash, JSON.stringify(embedding), embedding.length, now);
2951
+ }
2952
+ }
2953
+ pruneEmbeddingCacheIfNeeded() {
2954
+ if (!this.cache.enabled) return;
2955
+ const max = this.cache.maxEntries;
2956
+ if (!max || max <= 0) return;
2957
+ const count = this.db.prepare(`SELECT COUNT(*) as c FROM ${EMBEDDING_CACHE_TABLE$1}`).get()?.c ?? 0;
2958
+ if (count <= max) return;
2959
+ const excess = count - max;
2960
+ this.db.prepare(`DELETE FROM ${EMBEDDING_CACHE_TABLE$1}\n WHERE rowid IN (\n SELECT rowid FROM ${EMBEDDING_CACHE_TABLE$1}\n ORDER BY updated_at ASC\n LIMIT ?\n )`).run(excess);
2961
+ }
2962
+ async embedChunksInBatches(chunks) {
2963
+ if (chunks.length === 0) return [];
2964
+ const { embeddings, missing } = this.collectCachedEmbeddings(chunks);
2965
+ if (missing.length === 0) return embeddings;
2966
+ const missingChunks = missing.map((m) => m.chunk);
2967
+ const batches = this.buildEmbeddingBatches(missingChunks);
2968
+ const toCache = [];
2969
+ let cursor = 0;
2970
+ for (const batch of batches) {
2971
+ const batchEmbeddings = await this.embedBatchWithRetry(batch.map((chunk) => chunk.text));
2972
+ for (let i = 0; i < batch.length; i += 1) {
2973
+ const item = missing[cursor + i];
2974
+ const embedding = batchEmbeddings[i] ?? [];
2975
+ if (item) {
2976
+ embeddings[item.index] = embedding;
2977
+ toCache.push({
2978
+ hash: item.chunk.hash,
2979
+ embedding
2980
+ });
2981
+ }
2982
+ }
2983
+ cursor += batch.length;
2984
+ }
2985
+ this.upsertEmbeddingCache(toCache);
2986
+ return embeddings;
2987
+ }
2988
+ computeProviderKey() {
2989
+ if (!this.provider) return hashText(JSON.stringify({
2990
+ provider: "none",
2991
+ model: "fts-only"
2992
+ }));
2993
+ if (this.provider.id === "openai" && this.openAi) {
2994
+ const entries = Object.entries(this.openAi.headers).filter(([key]) => key.toLowerCase() !== "authorization").toSorted(([a], [b]) => a.localeCompare(b)).map(([key, value]) => [key, value]);
2995
+ return hashText(JSON.stringify({
2996
+ provider: "openai",
2997
+ baseUrl: this.openAi.baseUrl,
2998
+ model: this.openAi.model,
2999
+ headers: entries
3000
+ }));
3001
+ }
3002
+ if (this.provider.id === "gemini" && this.gemini) {
3003
+ const entries = Object.entries(this.gemini.headers).filter(([key]) => {
3004
+ const lower = key.toLowerCase();
3005
+ return lower !== "authorization" && lower !== "x-goog-api-key";
3006
+ }).toSorted(([a], [b]) => a.localeCompare(b)).map(([key, value]) => [key, value]);
3007
+ return hashText(JSON.stringify({
3008
+ provider: "gemini",
3009
+ baseUrl: this.gemini.baseUrl,
3010
+ model: this.gemini.model,
3011
+ headers: entries
3012
+ }));
3013
+ }
3014
+ return hashText(JSON.stringify({
3015
+ provider: this.provider.id,
3016
+ model: this.provider.model
3017
+ }));
3018
+ }
3019
+ async embedChunksWithBatch(chunks, entry, source) {
3020
+ if (!this.provider) return this.embedChunksInBatches(chunks);
3021
+ if (this.provider.id === "openai" && this.openAi) return this.embedChunksWithOpenAiBatch(chunks, entry, source);
3022
+ if (this.provider.id === "gemini" && this.gemini) return this.embedChunksWithGeminiBatch(chunks, entry, source);
3023
+ if (this.provider.id === "voyage" && this.voyage) return this.embedChunksWithVoyageBatch(chunks, entry, source);
3024
+ return this.embedChunksInBatches(chunks);
3025
+ }
3026
+ collectCachedEmbeddings(chunks) {
3027
+ const cached = this.loadEmbeddingCache(chunks.map((chunk) => chunk.hash));
3028
+ const embeddings = Array.from({ length: chunks.length }, () => []);
3029
+ const missing = [];
3030
+ for (let i = 0; i < chunks.length; i += 1) {
3031
+ const chunk = chunks[i];
3032
+ const hit = chunk?.hash ? cached.get(chunk.hash) : void 0;
3033
+ if (hit && hit.length > 0) embeddings[i] = hit;
3034
+ else if (chunk) missing.push({
3035
+ index: i,
3036
+ chunk
3037
+ });
3038
+ }
3039
+ return {
3040
+ embeddings,
3041
+ missing
3042
+ };
3043
+ }
3044
+ buildBatchCustomId(params) {
3045
+ return hashText(`${params.source}:${params.entry.path}:${params.chunk.startLine}:${params.chunk.endLine}:${params.chunk.hash}:${params.index}`);
3046
+ }
3047
+ buildBatchRequests(params) {
3048
+ const requests = [];
3049
+ const mapping = /* @__PURE__ */ new Map();
3050
+ for (const item of params.missing) {
3051
+ const chunk = item.chunk;
3052
+ const customId = this.buildBatchCustomId({
3053
+ source: params.source,
3054
+ entry: params.entry,
3055
+ chunk,
3056
+ index: item.index
3057
+ });
3058
+ mapping.set(customId, {
3059
+ index: item.index,
3060
+ hash: chunk.hash
3061
+ });
3062
+ const built = params.build(chunk);
3063
+ requests.push({
3064
+ custom_id: customId,
3065
+ ...built
3066
+ });
3067
+ }
3068
+ return {
3069
+ requests,
3070
+ mapping
3071
+ };
3072
+ }
3073
+ applyBatchEmbeddings(params) {
3074
+ const toCache = [];
3075
+ for (const [customId, embedding] of params.byCustomId.entries()) {
3076
+ const mapped = params.mapping.get(customId);
3077
+ if (!mapped) continue;
3078
+ params.embeddings[mapped.index] = embedding;
3079
+ toCache.push({
3080
+ hash: mapped.hash,
3081
+ embedding
3082
+ });
3083
+ }
3084
+ this.upsertEmbeddingCache(toCache);
3085
+ }
3086
+ buildEmbeddingBatchRunnerOptions(params) {
3087
+ const { requests, chunks, source } = params;
3088
+ return {
3089
+ agentId: this.agentId,
3090
+ requests,
3091
+ wait: this.batch.wait,
3092
+ concurrency: this.batch.concurrency,
3093
+ pollIntervalMs: this.batch.pollIntervalMs,
3094
+ timeoutMs: this.batch.timeoutMs,
3095
+ debug: (message, data) => log$1.debug(message, data ? {
3096
+ ...data,
3097
+ source,
3098
+ chunks: chunks.length
3099
+ } : {
3100
+ source,
3101
+ chunks: chunks.length
3102
+ })
3103
+ };
3104
+ }
3105
+ async embedChunksWithProviderBatch(params) {
3106
+ if (!params.enabled) return this.embedChunksInBatches(params.chunks);
3107
+ if (params.chunks.length === 0) return [];
3108
+ const { embeddings, missing } = this.collectCachedEmbeddings(params.chunks);
3109
+ if (missing.length === 0) return embeddings;
3110
+ const { requests, mapping } = this.buildBatchRequests({
3111
+ missing,
3112
+ entry: params.entry,
3113
+ source: params.source,
3114
+ build: params.buildRequest
3115
+ });
3116
+ const runnerOptions = this.buildEmbeddingBatchRunnerOptions({
3117
+ requests,
3118
+ chunks: params.chunks,
3119
+ source: params.source
3120
+ });
3121
+ const batchResult = await this.runBatchWithFallback({
3122
+ provider: params.provider,
3123
+ run: async () => await params.runBatch(runnerOptions),
3124
+ fallback: async () => await this.embedChunksInBatches(params.chunks)
3125
+ });
3126
+ if (Array.isArray(batchResult)) return batchResult;
3127
+ this.applyBatchEmbeddings({
3128
+ byCustomId: batchResult,
3129
+ mapping,
3130
+ embeddings
3131
+ });
3132
+ return embeddings;
3133
+ }
3134
+ async embedChunksWithVoyageBatch(chunks, entry, source) {
3135
+ const voyage = this.voyage;
3136
+ return await this.embedChunksWithProviderBatch({
3137
+ chunks,
3138
+ entry,
3139
+ source,
3140
+ provider: "voyage",
3141
+ enabled: Boolean(voyage),
3142
+ buildRequest: (chunk) => ({ body: { input: chunk.text } }),
3143
+ runBatch: async (runnerOptions) => await runVoyageEmbeddingBatches({
3144
+ client: voyage,
3145
+ ...runnerOptions
3146
+ })
3147
+ });
3148
+ }
3149
+ async embedChunksWithOpenAiBatch(chunks, entry, source) {
3150
+ const openAi = this.openAi;
3151
+ return await this.embedChunksWithProviderBatch({
3152
+ chunks,
3153
+ entry,
3154
+ source,
3155
+ provider: "openai",
3156
+ enabled: Boolean(openAi),
3157
+ buildRequest: (chunk) => ({
3158
+ method: "POST",
3159
+ url: OPENAI_BATCH_ENDPOINT,
3160
+ body: {
3161
+ model: openAi?.model ?? this.provider?.model ?? "text-embedding-3-small",
3162
+ input: chunk.text
3163
+ }
3164
+ }),
3165
+ runBatch: async (runnerOptions) => await runOpenAiEmbeddingBatches({
3166
+ openAi,
3167
+ ...runnerOptions
3168
+ })
3169
+ });
3170
+ }
3171
+ async embedChunksWithGeminiBatch(chunks, entry, source) {
3172
+ const gemini = this.gemini;
3173
+ return await this.embedChunksWithProviderBatch({
3174
+ chunks,
3175
+ entry,
3176
+ source,
3177
+ provider: "gemini",
3178
+ enabled: Boolean(gemini),
3179
+ buildRequest: (chunk) => ({
3180
+ content: { parts: [{ text: chunk.text }] },
3181
+ taskType: "RETRIEVAL_DOCUMENT"
3182
+ }),
3183
+ runBatch: async (runnerOptions) => await runGeminiEmbeddingBatches({
3184
+ gemini,
3185
+ ...runnerOptions
3186
+ })
3187
+ });
3188
+ }
3189
+ async embedBatchWithRetry(texts) {
3190
+ if (texts.length === 0) return [];
3191
+ if (!this.provider) throw new Error("Cannot embed batch in FTS-only mode (no embedding provider)");
3192
+ let attempt = 0;
3193
+ let delayMs = EMBEDDING_RETRY_BASE_DELAY_MS;
3194
+ while (true) try {
3195
+ const timeoutMs = this.resolveEmbeddingTimeout("batch");
3196
+ log$1.debug("memory embeddings: batch start", {
3197
+ provider: this.provider.id,
3198
+ items: texts.length,
3199
+ timeoutMs
3200
+ });
3201
+ return await this.withTimeout(this.provider.embedBatch(texts), timeoutMs, `memory embeddings batch timed out after ${Math.round(timeoutMs / 1e3)}s`);
3202
+ } catch (err) {
3203
+ const message = err instanceof Error ? err.message : String(err);
3204
+ if (!this.isRetryableEmbeddingError(message) || attempt >= EMBEDDING_RETRY_MAX_ATTEMPTS) throw err;
3205
+ const waitMs = Math.min(EMBEDDING_RETRY_MAX_DELAY_MS, Math.round(delayMs * (1 + Math.random() * .2)));
3206
+ log$1.warn(`memory embeddings rate limited; retrying in ${waitMs}ms`);
3207
+ await new Promise((resolve) => setTimeout(resolve, waitMs));
3208
+ delayMs *= 2;
3209
+ attempt += 1;
3210
+ }
3211
+ }
3212
+ isRetryableEmbeddingError(message) {
3213
+ return /(rate[_ ]limit|too many requests|429|resource has been exhausted|5\d\d|cloudflare|tokens per day)/i.test(message);
3214
+ }
3215
+ resolveEmbeddingTimeout(kind) {
3216
+ const isLocal = this.provider?.id === "local";
3217
+ if (kind === "query") return isLocal ? EMBEDDING_QUERY_TIMEOUT_LOCAL_MS : EMBEDDING_QUERY_TIMEOUT_REMOTE_MS;
3218
+ return isLocal ? EMBEDDING_BATCH_TIMEOUT_LOCAL_MS : EMBEDDING_BATCH_TIMEOUT_REMOTE_MS;
3219
+ }
3220
+ async embedQueryWithTimeout(text) {
3221
+ if (!this.provider) throw new Error("Cannot embed query in FTS-only mode (no embedding provider)");
3222
+ const timeoutMs = this.resolveEmbeddingTimeout("query");
3223
+ log$1.debug("memory embeddings: query start", {
3224
+ provider: this.provider.id,
3225
+ timeoutMs
3226
+ });
3227
+ return await this.withTimeout(this.provider.embedQuery(text), timeoutMs, `memory embeddings query timed out after ${Math.round(timeoutMs / 1e3)}s`);
3228
+ }
3229
+ async withTimeout(promise, timeoutMs, message) {
3230
+ if (!Number.isFinite(timeoutMs) || timeoutMs <= 0) return await promise;
3231
+ let timer = null;
3232
+ const timeoutPromise = new Promise((_, reject) => {
3233
+ timer = setTimeout(() => reject(new Error(message)), timeoutMs);
3234
+ });
3235
+ try {
3236
+ return await Promise.race([promise, timeoutPromise]);
3237
+ } finally {
3238
+ if (timer) clearTimeout(timer);
3239
+ }
3240
+ }
3241
+ async withBatchFailureLock(fn) {
3242
+ let release;
3243
+ const wait = this.batchFailureLock;
3244
+ this.batchFailureLock = new Promise((resolve) => {
3245
+ release = resolve;
3246
+ });
3247
+ await wait;
3248
+ try {
3249
+ return await fn();
3250
+ } finally {
3251
+ release();
3252
+ }
3253
+ }
3254
+ async resetBatchFailureCount() {
3255
+ await this.withBatchFailureLock(async () => {
3256
+ if (this.batchFailureCount > 0) log$1.debug("memory embeddings: batch recovered; resetting failure count");
3257
+ this.batchFailureCount = 0;
3258
+ this.batchFailureLastError = void 0;
3259
+ this.batchFailureLastProvider = void 0;
3260
+ });
3261
+ }
3262
+ async recordBatchFailure(params) {
3263
+ return await this.withBatchFailureLock(async () => {
3264
+ if (!this.batch.enabled) return {
3265
+ disabled: true,
3266
+ count: this.batchFailureCount
3267
+ };
3268
+ const increment = params.forceDisable ? BATCH_FAILURE_LIMIT$1 : Math.max(1, params.attempts ?? 1);
3269
+ this.batchFailureCount += increment;
3270
+ this.batchFailureLastError = params.message;
3271
+ this.batchFailureLastProvider = params.provider;
3272
+ const disabled = params.forceDisable || this.batchFailureCount >= BATCH_FAILURE_LIMIT$1;
3273
+ if (disabled) this.batch.enabled = false;
3274
+ return {
3275
+ disabled,
3276
+ count: this.batchFailureCount
3277
+ };
3278
+ });
3279
+ }
3280
+ isBatchTimeoutError(message) {
3281
+ return /timed out|timeout/i.test(message);
3282
+ }
3283
+ async runBatchWithTimeoutRetry(params) {
3284
+ try {
3285
+ return await params.run();
3286
+ } catch (err) {
3287
+ const message = err instanceof Error ? err.message : String(err);
3288
+ if (this.isBatchTimeoutError(message)) {
3289
+ log$1.warn(`memory embeddings: ${params.provider} batch timed out; retrying once`);
3290
+ try {
3291
+ return await params.run();
3292
+ } catch (retryErr) {
3293
+ retryErr.batchAttempts = 2;
3294
+ throw retryErr;
3295
+ }
3296
+ }
3297
+ throw err;
3298
+ }
3299
+ }
3300
+ async runBatchWithFallback(params) {
3301
+ if (!this.batch.enabled) return await params.fallback();
3302
+ try {
3303
+ const result = await this.runBatchWithTimeoutRetry({
3304
+ provider: params.provider,
3305
+ run: params.run
3306
+ });
3307
+ await this.resetBatchFailureCount();
3308
+ return result;
3309
+ } catch (err) {
3310
+ const message = err instanceof Error ? err.message : String(err);
3311
+ const attempts = err.batchAttempts ?? 1;
3312
+ const forceDisable = /asyncBatchEmbedContent not available/i.test(message);
3313
+ const failure = await this.recordBatchFailure({
3314
+ provider: params.provider,
3315
+ message,
3316
+ attempts,
3317
+ forceDisable
3318
+ });
3319
+ const suffix = failure.disabled ? "disabling batch" : "keeping batch enabled";
3320
+ log$1.warn(`memory embeddings: ${params.provider} batch failed (${failure.count}/${BATCH_FAILURE_LIMIT$1}); ${suffix}; falling back to non-batch embeddings: ${message}`);
3321
+ return await params.fallback();
3322
+ }
3323
+ }
3324
+ getIndexConcurrency() {
3325
+ return this.batch.enabled ? this.batch.concurrency : EMBEDDING_INDEX_CONCURRENCY;
3326
+ }
3327
+ async indexFile(entry, options) {
3328
+ if (!this.provider) {
3329
+ log$1.debug("Skipping embedding indexing in FTS-only mode", {
3330
+ path: entry.path,
3331
+ source: options.source
3332
+ });
3333
+ return;
3334
+ }
3335
+ const content = options.content ?? await fs$1.readFile(entry.absPath, "utf-8");
3336
+ const chunks = enforceEmbeddingMaxInputTokens(this.provider, chunkMarkdown(content, this.settings.chunking).filter((chunk) => chunk.text.trim().length > 0), EMBEDDING_BATCH_MAX_TOKENS);
3337
+ if (options.source === "sessions" && "lineMap" in entry) remapChunkLines(chunks, entry.lineMap);
3338
+ const embeddings = this.batch.enabled ? await this.embedChunksWithBatch(chunks, entry, options.source) : await this.embedChunksInBatches(chunks);
3339
+ const sample = embeddings.find((embedding) => embedding.length > 0);
3340
+ const vectorReady = sample ? await this.ensureVectorReady(sample.length) : false;
3341
+ const now = Date.now();
3342
+ if (vectorReady) try {
3343
+ this.db.prepare(`DELETE FROM ${VECTOR_TABLE$1} WHERE id IN (SELECT id FROM chunks WHERE path = ? AND source = ?)`).run(entry.path, options.source);
3344
+ } catch {}
3345
+ if (this.fts.enabled && this.fts.available) try {
3346
+ this.db.prepare(`DELETE FROM ${FTS_TABLE$1} WHERE path = ? AND source = ? AND model = ?`).run(entry.path, options.source, this.provider.model);
3347
+ } catch {}
3348
+ this.db.prepare(`DELETE FROM chunks WHERE path = ? AND source = ?`).run(entry.path, options.source);
3349
+ for (let i = 0; i < chunks.length; i++) {
3350
+ const chunk = chunks[i];
3351
+ const embedding = embeddings[i] ?? [];
3352
+ const id = hashText(`${options.source}:${entry.path}:${chunk.startLine}:${chunk.endLine}:${chunk.hash}:${this.provider.model}`);
3353
+ this.db.prepare(`INSERT INTO chunks (id, path, source, start_line, end_line, hash, model, text, embedding, updated_at)
3354
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
3355
+ ON CONFLICT(id) DO UPDATE SET
3356
+ hash=excluded.hash,
3357
+ model=excluded.model,
3358
+ text=excluded.text,
3359
+ embedding=excluded.embedding,
3360
+ updated_at=excluded.updated_at`).run(id, entry.path, options.source, chunk.startLine, chunk.endLine, chunk.hash, this.provider.model, chunk.text, JSON.stringify(embedding), now);
3361
+ if (vectorReady && embedding.length > 0) {
3362
+ try {
3363
+ this.db.prepare(`DELETE FROM ${VECTOR_TABLE$1} WHERE id = ?`).run(id);
3364
+ } catch {}
3365
+ this.db.prepare(`INSERT INTO ${VECTOR_TABLE$1} (id, embedding) VALUES (?, ?)`).run(id, vectorToBlob$1(embedding));
3366
+ }
3367
+ if (this.fts.enabled && this.fts.available) this.db.prepare(`INSERT INTO ${FTS_TABLE$1} (text, id, path, source, model, start_line, end_line)\n VALUES (?, ?, ?, ?, ?, ?, ?)`).run(chunk.text, id, entry.path, options.source, this.provider.model, chunk.startLine, chunk.endLine);
3368
+ }
3369
+ this.db.prepare(`INSERT INTO files (path, source, hash, mtime, size) VALUES (?, ?, ?, ?, ?)
3370
+ ON CONFLICT(path) DO UPDATE SET
3371
+ source=excluded.source,
3372
+ hash=excluded.hash,
3373
+ mtime=excluded.mtime,
3374
+ size=excluded.size`).run(entry.path, options.source, entry.hash, entry.mtimeMs, entry.size);
3375
+ }
3376
+ };
3377
+ //#endregion
3378
+ //#region src/memory/manager-search.ts
3379
+ const vectorToBlob = (embedding) => Buffer.from(new Float32Array(embedding).buffer);
3380
+ async function searchVector(params) {
3381
+ if (params.queryVec.length === 0 || params.limit <= 0) return [];
3382
+ if (await params.ensureVectorReady(params.queryVec.length)) return params.db.prepare(`SELECT c.id, c.path, c.start_line, c.end_line, c.text,
3383
+ c.source,
3384
+ vec_distance_cosine(v.embedding, ?) AS dist
3385
+ FROM ${params.vectorTable} v\n JOIN chunks c ON c.id = v.id\n WHERE c.model = ?${params.sourceFilterVec.sql}\n ORDER BY dist ASC\n LIMIT ?`).all(vectorToBlob(params.queryVec), params.providerModel, ...params.sourceFilterVec.params, params.limit).map((row) => ({
3386
+ id: row.id,
3387
+ path: row.path,
3388
+ startLine: row.start_line,
3389
+ endLine: row.end_line,
3390
+ score: 1 - row.dist,
3391
+ snippet: truncateUtf16Safe(row.text, params.snippetMaxChars),
3392
+ source: row.source
3393
+ }));
3394
+ return listChunks({
3395
+ db: params.db,
3396
+ providerModel: params.providerModel,
3397
+ sourceFilter: params.sourceFilterChunks
3398
+ }).map((chunk) => ({
3399
+ chunk,
3400
+ score: cosineSimilarity(params.queryVec, chunk.embedding)
3401
+ })).filter((entry) => Number.isFinite(entry.score)).toSorted((a, b) => b.score - a.score).slice(0, params.limit).map((entry) => ({
3402
+ id: entry.chunk.id,
3403
+ path: entry.chunk.path,
3404
+ startLine: entry.chunk.startLine,
3405
+ endLine: entry.chunk.endLine,
3406
+ score: entry.score,
3407
+ snippet: truncateUtf16Safe(entry.chunk.text, params.snippetMaxChars),
3408
+ source: entry.chunk.source
3409
+ }));
3410
+ }
3411
+ function listChunks(params) {
3412
+ return params.db.prepare(`SELECT id, path, start_line, end_line, text, embedding, source
3413
+ FROM chunks
3414
+ WHERE model = ?${params.sourceFilter.sql}`).all(params.providerModel, ...params.sourceFilter.params).map((row) => ({
3415
+ id: row.id,
3416
+ path: row.path,
3417
+ startLine: row.start_line,
3418
+ endLine: row.end_line,
3419
+ text: row.text,
3420
+ embedding: parseEmbedding(row.embedding),
3421
+ source: row.source
3422
+ }));
3423
+ }
3424
+ async function searchKeyword(params) {
3425
+ if (params.limit <= 0) return [];
3426
+ const ftsQuery = params.buildFtsQuery(params.query);
3427
+ if (!ftsQuery) return [];
3428
+ const modelClause = params.providerModel ? " AND model = ?" : "";
3429
+ const modelParams = params.providerModel ? [params.providerModel] : [];
3430
+ return params.db.prepare(`SELECT id, path, source, start_line, end_line, text,\n bm25(${params.ftsTable}) AS rank\n FROM ${params.ftsTable}\n WHERE ${params.ftsTable} MATCH ?${modelClause}${params.sourceFilter.sql}\n ORDER BY rank ASC\n LIMIT ?`).all(ftsQuery, ...modelParams, ...params.sourceFilter.params, params.limit).map((row) => {
3431
+ const textScore = params.bm25RankToScore(row.rank);
3432
+ return {
3433
+ id: row.id,
3434
+ path: row.path,
3435
+ startLine: row.start_line,
3436
+ endLine: row.end_line,
3437
+ score: textScore,
3438
+ textScore,
3439
+ snippet: truncateUtf16Safe(row.text, params.snippetMaxChars),
3440
+ source: row.source
3441
+ };
3442
+ });
3443
+ }
3444
+ //#endregion
3445
+ //#region src/memory/manager.ts
3446
+ const SNIPPET_MAX_CHARS = 700;
3447
+ const VECTOR_TABLE = "chunks_vec";
3448
+ const FTS_TABLE = "chunks_fts";
3449
+ const EMBEDDING_CACHE_TABLE = "embedding_cache";
3450
+ const BATCH_FAILURE_LIMIT = 2;
3451
+ const log = createSubsystemLogger("memory");
3452
+ const INDEX_CACHE = /* @__PURE__ */ new Map();
3453
+ const INDEX_CACHE_PENDING = /* @__PURE__ */ new Map();
3454
+ async function closeAllMemoryIndexManagers() {
3455
+ const pending = Array.from(INDEX_CACHE_PENDING.values());
3456
+ if (pending.length > 0) await Promise.allSettled(pending);
3457
+ const managers = Array.from(INDEX_CACHE.values());
3458
+ INDEX_CACHE.clear();
3459
+ for (const manager of managers) try {
3460
+ await manager.close();
3461
+ } catch (err) {
3462
+ log.warn(`failed to close memory index manager: ${String(err)}`);
3463
+ }
3464
+ }
3465
+ var MemoryIndexManager = class MemoryIndexManager extends MemoryManagerEmbeddingOps {
3466
+ static async get(params) {
3467
+ const { cfg, agentId } = params;
3468
+ const settings = resolveMemorySearchConfig(cfg, agentId);
3469
+ if (!settings) return null;
3470
+ const workspaceDir = resolveAgentWorkspaceDir(cfg, agentId);
3471
+ const key = `${agentId}:${workspaceDir}:${JSON.stringify(settings)}`;
3472
+ const existing = INDEX_CACHE.get(key);
3473
+ if (existing) return existing;
3474
+ const pending = INDEX_CACHE_PENDING.get(key);
3475
+ if (pending) return pending;
3476
+ const createPromise = (async () => {
3477
+ const providerResult = await createEmbeddingProvider({
3478
+ config: cfg,
3479
+ agentDir: resolveAgentDir(cfg, agentId),
3480
+ provider: settings.provider,
3481
+ remote: settings.remote,
3482
+ model: settings.model,
3483
+ fallback: settings.fallback,
3484
+ local: settings.local
3485
+ });
3486
+ const refreshed = INDEX_CACHE.get(key);
3487
+ if (refreshed) return refreshed;
3488
+ const manager = new MemoryIndexManager({
3489
+ cacheKey: key,
3490
+ cfg,
3491
+ agentId,
3492
+ workspaceDir,
3493
+ settings,
3494
+ providerResult,
3495
+ purpose: params.purpose
3496
+ });
3497
+ INDEX_CACHE.set(key, manager);
3498
+ return manager;
3499
+ })();
3500
+ INDEX_CACHE_PENDING.set(key, createPromise);
3501
+ try {
3502
+ return await createPromise;
3503
+ } finally {
3504
+ if (INDEX_CACHE_PENDING.get(key) === createPromise) INDEX_CACHE_PENDING.delete(key);
3505
+ }
3506
+ }
3507
+ constructor(params) {
3508
+ super();
3509
+ this.batchFailureCount = 0;
3510
+ this.batchFailureLock = Promise.resolve();
3511
+ this.vectorReady = null;
3512
+ this.watcher = null;
3513
+ this.watchTimer = null;
3514
+ this.sessionWatchTimer = null;
3515
+ this.sessionUnsubscribe = null;
3516
+ this.intervalTimer = null;
3517
+ this.closed = false;
3518
+ this.dirty = false;
3519
+ this.sessionsDirty = false;
3520
+ this.sessionsDirtyFiles = /* @__PURE__ */ new Set();
3521
+ this.sessionPendingFiles = /* @__PURE__ */ new Set();
3522
+ this.sessionDeltas = /* @__PURE__ */ new Map();
3523
+ this.sessionWarm = /* @__PURE__ */ new Set();
3524
+ this.syncing = null;
3525
+ this.readonlyRecoveryAttempts = 0;
3526
+ this.readonlyRecoverySuccesses = 0;
3527
+ this.readonlyRecoveryFailures = 0;
3528
+ this.cacheKey = params.cacheKey;
3529
+ this.cfg = params.cfg;
3530
+ this.agentId = params.agentId;
3531
+ this.workspaceDir = params.workspaceDir;
3532
+ this.settings = params.settings;
3533
+ this.provider = params.providerResult.provider;
3534
+ this.requestedProvider = params.providerResult.requestedProvider;
3535
+ this.fallbackFrom = params.providerResult.fallbackFrom;
3536
+ this.fallbackReason = params.providerResult.fallbackReason;
3537
+ this.providerUnavailableReason = params.providerResult.providerUnavailableReason;
3538
+ this.openAi = params.providerResult.openAi;
3539
+ this.gemini = params.providerResult.gemini;
3540
+ this.voyage = params.providerResult.voyage;
3541
+ this.mistral = params.providerResult.mistral;
3542
+ this.ollama = params.providerResult.ollama;
3543
+ this.sources = new Set(params.settings.sources);
3544
+ this.db = this.openDatabase();
3545
+ this.providerKey = this.computeProviderKey();
3546
+ this.cache = {
3547
+ enabled: params.settings.cache.enabled,
3548
+ maxEntries: params.settings.cache.maxEntries
3549
+ };
3550
+ this.fts = {
3551
+ enabled: params.settings.query.hybrid.enabled,
3552
+ available: false
3553
+ };
3554
+ this.ensureSchema();
3555
+ this.vector = {
3556
+ enabled: params.settings.store.vector.enabled,
3557
+ available: null,
3558
+ extensionPath: params.settings.store.vector.extensionPath
3559
+ };
3560
+ const meta = this.readMeta();
3561
+ if (meta?.vectorDims) this.vector.dims = meta.vectorDims;
3562
+ this.ensureWatcher();
3563
+ this.ensureSessionListener();
3564
+ this.ensureIntervalSync();
3565
+ const statusOnly = params.purpose === "status";
3566
+ this.dirty = this.sources.has("memory") && (statusOnly ? !meta : true);
3567
+ this.batch = this.resolveBatchConfig();
3568
+ }
3569
+ async warmSession(sessionKey) {
3570
+ if (!this.settings.sync.onSessionStart) return;
3571
+ const key = sessionKey?.trim() || "";
3572
+ if (key && this.sessionWarm.has(key)) return;
3573
+ this.sync({ reason: "session-start" }).catch((err) => {
3574
+ log.warn(`memory sync failed (session-start): ${String(err)}`);
3575
+ });
3576
+ if (key) this.sessionWarm.add(key);
3577
+ }
3578
+ async search(query, opts) {
3579
+ this.warmSession(opts?.sessionKey);
3580
+ if (this.settings.sync.onSearch && (this.dirty || this.sessionsDirty)) this.sync({ reason: "search" }).catch((err) => {
3581
+ log.warn(`memory sync failed (search): ${String(err)}`);
3582
+ });
3583
+ const cleaned = query.trim();
3584
+ if (!cleaned) return [];
3585
+ const minScore = opts?.minScore ?? this.settings.query.minScore;
3586
+ const maxResults = opts?.maxResults ?? this.settings.query.maxResults;
3587
+ const hybrid = this.settings.query.hybrid;
3588
+ const candidates = Math.min(200, Math.max(1, Math.floor(maxResults * hybrid.candidateMultiplier)));
3589
+ if (!this.provider) {
3590
+ if (!this.fts.enabled || !this.fts.available) {
3591
+ log.warn("memory search: no provider and FTS unavailable");
3592
+ return [];
3593
+ }
3594
+ const keywords = extractKeywords(cleaned);
3595
+ const searchTerms = keywords.length > 0 ? keywords : [cleaned];
3596
+ const resultSets = await Promise.all(searchTerms.map((term) => this.searchKeyword(term, candidates).catch(() => [])));
3597
+ const seenIds = /* @__PURE__ */ new Map();
3598
+ for (const results of resultSets) for (const result of results) {
3599
+ const existing = seenIds.get(result.id);
3600
+ if (!existing || result.score > existing.score) seenIds.set(result.id, result);
3601
+ }
3602
+ return [...seenIds.values()].toSorted((a, b) => b.score - a.score).filter((entry) => entry.score >= minScore).slice(0, maxResults);
3603
+ }
3604
+ const keywordResults = hybrid.enabled && this.fts.enabled && this.fts.available ? await this.searchKeyword(cleaned, candidates).catch(() => []) : [];
3605
+ const queryVec = await this.embedQueryWithTimeout(cleaned);
3606
+ const vectorResults = queryVec.some((v) => v !== 0) ? await this.searchVector(queryVec, candidates).catch(() => []) : [];
3607
+ if (!hybrid.enabled || !this.fts.enabled || !this.fts.available) return vectorResults.filter((entry) => entry.score >= minScore).slice(0, maxResults);
3608
+ const merged = await this.mergeHybridResults({
3609
+ vector: vectorResults,
3610
+ keyword: keywordResults,
3611
+ vectorWeight: hybrid.vectorWeight,
3612
+ textWeight: hybrid.textWeight,
3613
+ mmr: hybrid.mmr,
3614
+ temporalDecay: hybrid.temporalDecay
3615
+ });
3616
+ const strict = merged.filter((entry) => entry.score >= minScore);
3617
+ if (strict.length > 0 || keywordResults.length === 0) return strict.slice(0, maxResults);
3618
+ const relaxedMinScore = Math.min(minScore, hybrid.textWeight);
3619
+ const keywordKeys = new Set(keywordResults.map((entry) => `${entry.source}:${entry.path}:${entry.startLine}:${entry.endLine}`));
3620
+ return merged.filter((entry) => keywordKeys.has(`${entry.source}:${entry.path}:${entry.startLine}:${entry.endLine}`) && entry.score >= relaxedMinScore).slice(0, maxResults);
3621
+ }
3622
+ async searchVector(queryVec, limit) {
3623
+ if (!this.provider) return [];
3624
+ return (await searchVector({
3625
+ db: this.db,
3626
+ vectorTable: VECTOR_TABLE,
3627
+ providerModel: this.provider.model,
3628
+ queryVec,
3629
+ limit,
3630
+ snippetMaxChars: SNIPPET_MAX_CHARS,
3631
+ ensureVectorReady: async (dimensions) => await this.ensureVectorReady(dimensions),
3632
+ sourceFilterVec: this.buildSourceFilter("c"),
3633
+ sourceFilterChunks: this.buildSourceFilter()
3634
+ })).map((entry) => entry);
3635
+ }
3636
+ buildFtsQuery(raw) {
3637
+ return buildFtsQuery(raw);
3638
+ }
3639
+ async searchKeyword(query, limit) {
3640
+ if (!this.fts.enabled || !this.fts.available) return [];
3641
+ const sourceFilter = this.buildSourceFilter();
3642
+ const providerModel = this.provider?.model;
3643
+ return (await searchKeyword({
3644
+ db: this.db,
3645
+ ftsTable: FTS_TABLE,
3646
+ providerModel,
3647
+ query,
3648
+ limit,
3649
+ snippetMaxChars: SNIPPET_MAX_CHARS,
3650
+ sourceFilter,
3651
+ buildFtsQuery: (raw) => this.buildFtsQuery(raw),
3652
+ bm25RankToScore
3653
+ })).map((entry) => entry);
3654
+ }
3655
+ mergeHybridResults(params) {
3656
+ return mergeHybridResults({
3657
+ vector: params.vector.map((r) => ({
3658
+ id: r.id,
3659
+ path: r.path,
3660
+ startLine: r.startLine,
3661
+ endLine: r.endLine,
3662
+ source: r.source,
3663
+ snippet: r.snippet,
3664
+ vectorScore: r.score
3665
+ })),
3666
+ keyword: params.keyword.map((r) => ({
3667
+ id: r.id,
3668
+ path: r.path,
3669
+ startLine: r.startLine,
3670
+ endLine: r.endLine,
3671
+ source: r.source,
3672
+ snippet: r.snippet,
3673
+ textScore: r.textScore
3674
+ })),
3675
+ vectorWeight: params.vectorWeight,
3676
+ textWeight: params.textWeight,
3677
+ mmr: params.mmr,
3678
+ temporalDecay: params.temporalDecay,
3679
+ workspaceDir: this.workspaceDir
3680
+ }).then((entries) => entries.map((entry) => entry));
3681
+ }
3682
+ async sync(params) {
3683
+ if (this.closed) return;
3684
+ if (this.syncing) return this.syncing;
3685
+ this.syncing = this.runSyncWithReadonlyRecovery(params).finally(() => {
3686
+ this.syncing = null;
3687
+ });
3688
+ return this.syncing ?? Promise.resolve();
3689
+ }
3690
+ isReadonlyDbError(err) {
3691
+ const readonlyPattern = /attempt to write a readonly database|database is read-only|SQLITE_READONLY/i;
3692
+ const messages = /* @__PURE__ */ new Set();
3693
+ const pushValue = (value) => {
3694
+ if (typeof value !== "string") return;
3695
+ const normalized = value.trim();
3696
+ if (!normalized) return;
3697
+ messages.add(normalized);
3698
+ };
3699
+ pushValue(err instanceof Error ? err.message : String(err));
3700
+ if (err && typeof err === "object") {
3701
+ const record = err;
3702
+ pushValue(record.message);
3703
+ pushValue(record.code);
3704
+ pushValue(record.name);
3705
+ if (record.cause && typeof record.cause === "object") {
3706
+ const cause = record.cause;
3707
+ pushValue(cause.message);
3708
+ pushValue(cause.code);
3709
+ pushValue(cause.name);
3710
+ }
3711
+ }
3712
+ return [...messages].some((value) => readonlyPattern.test(value));
3713
+ }
3714
+ extractErrorReason(err) {
3715
+ if (err instanceof Error && err.message.trim()) return err.message;
3716
+ if (err && typeof err === "object") {
3717
+ const record = err;
3718
+ if (typeof record.message === "string" && record.message.trim()) return record.message;
3719
+ if (typeof record.code === "string" && record.code.trim()) return record.code;
3720
+ }
3721
+ return String(err);
3722
+ }
3723
+ async runSyncWithReadonlyRecovery(params) {
3724
+ try {
3725
+ await this.runSync(params);
3726
+ return;
3727
+ } catch (err) {
3728
+ if (!this.isReadonlyDbError(err) || this.closed) throw err;
3729
+ const reason = this.extractErrorReason(err);
3730
+ this.readonlyRecoveryAttempts += 1;
3731
+ this.readonlyRecoveryLastError = reason;
3732
+ log.warn(`memory sync readonly handle detected; reopening sqlite connection`, { reason });
3733
+ try {
3734
+ this.db.close();
3735
+ } catch {}
3736
+ this.db = this.openDatabase();
3737
+ this.vectorReady = null;
3738
+ this.vector.available = null;
3739
+ this.vector.loadError = void 0;
3740
+ this.ensureSchema();
3741
+ const meta = this.readMeta();
3742
+ this.vector.dims = meta?.vectorDims;
3743
+ try {
3744
+ await this.runSync(params);
3745
+ this.readonlyRecoverySuccesses += 1;
3746
+ } catch (retryErr) {
3747
+ this.readonlyRecoveryFailures += 1;
3748
+ throw retryErr;
3749
+ }
3750
+ }
3751
+ }
3752
+ async readFile(params) {
3753
+ const rawPath = params.relPath.trim();
3754
+ if (!rawPath) throw new Error("path required");
3755
+ const absPath = path.isAbsolute(rawPath) ? path.resolve(rawPath) : path.resolve(this.workspaceDir, rawPath);
3756
+ const relPath = path.relative(this.workspaceDir, absPath).replace(/\\/g, "/");
3757
+ const allowedWorkspace = relPath.length > 0 && !relPath.startsWith("..") && !path.isAbsolute(relPath) && isMemoryPath(relPath);
3758
+ let allowedAdditional = false;
3759
+ if (!allowedWorkspace && this.settings.extraPaths.length > 0) {
3760
+ const additionalPaths = normalizeExtraMemoryPaths(this.workspaceDir, this.settings.extraPaths);
3761
+ for (const additionalPath of additionalPaths) try {
3762
+ const stat = await fs$1.lstat(additionalPath);
3763
+ if (stat.isSymbolicLink()) continue;
3764
+ if (stat.isDirectory()) {
3765
+ if (absPath === additionalPath || absPath.startsWith(`${additionalPath}${path.sep}`)) {
3766
+ allowedAdditional = true;
3767
+ break;
3768
+ }
3769
+ continue;
3770
+ }
3771
+ if (stat.isFile()) {
3772
+ if (absPath === additionalPath && absPath.endsWith(".md")) {
3773
+ allowedAdditional = true;
3774
+ break;
3775
+ }
3776
+ }
3777
+ } catch {}
3778
+ }
3779
+ if (!allowedWorkspace && !allowedAdditional) throw new Error("path required");
3780
+ if (!absPath.endsWith(".md")) throw new Error("path required");
3781
+ if ((await statRegularFile(absPath)).missing) return {
3782
+ text: "",
3783
+ path: relPath
3784
+ };
3785
+ let content;
3786
+ try {
3787
+ content = await fs$1.readFile(absPath, "utf-8");
3788
+ } catch (err) {
3789
+ if (isFileMissingError(err)) return {
3790
+ text: "",
3791
+ path: relPath
3792
+ };
3793
+ throw err;
3794
+ }
3795
+ if (!params.from && !params.lines) return {
3796
+ text: content,
3797
+ path: relPath
3798
+ };
3799
+ const lines = content.split("\n");
3800
+ const start = Math.max(1, params.from ?? 1);
3801
+ const count = Math.max(1, params.lines ?? lines.length);
3802
+ return {
3803
+ text: lines.slice(start - 1, start - 1 + count).join("\n"),
3804
+ path: relPath
3805
+ };
3806
+ }
3807
+ status() {
3808
+ const sourceFilter = this.buildSourceFilter();
3809
+ const files = this.db.prepare(`SELECT COUNT(*) as c FROM files WHERE 1=1${sourceFilter.sql}`).get(...sourceFilter.params);
3810
+ const chunks = this.db.prepare(`SELECT COUNT(*) as c FROM chunks WHERE 1=1${sourceFilter.sql}`).get(...sourceFilter.params);
3811
+ const sourceCounts = (() => {
3812
+ const sources = Array.from(this.sources);
3813
+ if (sources.length === 0) return [];
3814
+ const bySource = /* @__PURE__ */ new Map();
3815
+ for (const source of sources) bySource.set(source, {
3816
+ files: 0,
3817
+ chunks: 0
3818
+ });
3819
+ const fileRows = this.db.prepare(`SELECT source, COUNT(*) as c FROM files WHERE 1=1${sourceFilter.sql} GROUP BY source`).all(...sourceFilter.params);
3820
+ for (const row of fileRows) {
3821
+ const entry = bySource.get(row.source) ?? {
3822
+ files: 0,
3823
+ chunks: 0
3824
+ };
3825
+ entry.files = row.c ?? 0;
3826
+ bySource.set(row.source, entry);
3827
+ }
3828
+ const chunkRows = this.db.prepare(`SELECT source, COUNT(*) as c FROM chunks WHERE 1=1${sourceFilter.sql} GROUP BY source`).all(...sourceFilter.params);
3829
+ for (const row of chunkRows) {
3830
+ const entry = bySource.get(row.source) ?? {
3831
+ files: 0,
3832
+ chunks: 0
3833
+ };
3834
+ entry.chunks = row.c ?? 0;
3835
+ bySource.set(row.source, entry);
3836
+ }
3837
+ return sources.map((source) => Object.assign({ source }, bySource.get(source)));
3838
+ })();
3839
+ const searchMode = this.provider ? "hybrid" : "fts-only";
3840
+ const providerInfo = this.provider ? {
3841
+ provider: this.provider.id,
3842
+ model: this.provider.model
3843
+ } : {
3844
+ provider: "none",
3845
+ model: void 0
3846
+ };
3847
+ return {
3848
+ backend: "builtin",
3849
+ files: files?.c ?? 0,
3850
+ chunks: chunks?.c ?? 0,
3851
+ dirty: this.dirty || this.sessionsDirty,
3852
+ workspaceDir: this.workspaceDir,
3853
+ dbPath: this.settings.store.path,
3854
+ provider: providerInfo.provider,
3855
+ model: providerInfo.model,
3856
+ requestedProvider: this.requestedProvider,
3857
+ sources: Array.from(this.sources),
3858
+ extraPaths: this.settings.extraPaths,
3859
+ sourceCounts,
3860
+ cache: this.cache.enabled ? {
3861
+ enabled: true,
3862
+ entries: this.db.prepare(`SELECT COUNT(*) as c FROM ${EMBEDDING_CACHE_TABLE}`).get()?.c ?? 0,
3863
+ maxEntries: this.cache.maxEntries
3864
+ } : {
3865
+ enabled: false,
3866
+ maxEntries: this.cache.maxEntries
3867
+ },
3868
+ fts: {
3869
+ enabled: this.fts.enabled,
3870
+ available: this.fts.available,
3871
+ error: this.fts.loadError
3872
+ },
3873
+ fallback: this.fallbackReason ? {
3874
+ from: this.fallbackFrom ?? "local",
3875
+ reason: this.fallbackReason
3876
+ } : void 0,
3877
+ vector: {
3878
+ enabled: this.vector.enabled,
3879
+ available: this.vector.available ?? void 0,
3880
+ extensionPath: this.vector.extensionPath,
3881
+ loadError: this.vector.loadError,
3882
+ dims: this.vector.dims
3883
+ },
3884
+ batch: {
3885
+ enabled: this.batch.enabled,
3886
+ failures: this.batchFailureCount,
3887
+ limit: BATCH_FAILURE_LIMIT,
3888
+ wait: this.batch.wait,
3889
+ concurrency: this.batch.concurrency,
3890
+ pollIntervalMs: this.batch.pollIntervalMs,
3891
+ timeoutMs: this.batch.timeoutMs,
3892
+ lastError: this.batchFailureLastError,
3893
+ lastProvider: this.batchFailureLastProvider
3894
+ },
3895
+ custom: {
3896
+ searchMode,
3897
+ providerUnavailableReason: this.providerUnavailableReason,
3898
+ readonlyRecovery: {
3899
+ attempts: this.readonlyRecoveryAttempts,
3900
+ successes: this.readonlyRecoverySuccesses,
3901
+ failures: this.readonlyRecoveryFailures,
3902
+ lastError: this.readonlyRecoveryLastError
3903
+ }
3904
+ }
3905
+ };
3906
+ }
3907
+ async probeVectorAvailability() {
3908
+ if (!this.provider) return false;
3909
+ if (!this.vector.enabled) return false;
3910
+ return this.ensureVectorReady();
3911
+ }
3912
+ async probeEmbeddingAvailability() {
3913
+ if (!this.provider) return {
3914
+ ok: false,
3915
+ error: this.providerUnavailableReason ?? "No embedding provider available (FTS-only mode)"
3916
+ };
3917
+ try {
3918
+ await this.embedBatchWithRetry(["ping"]);
3919
+ return { ok: true };
3920
+ } catch (err) {
3921
+ return {
3922
+ ok: false,
3923
+ error: err instanceof Error ? err.message : String(err)
3924
+ };
3925
+ }
3926
+ }
3927
+ async close() {
3928
+ if (this.closed) return;
3929
+ this.closed = true;
3930
+ const pendingSync = this.syncing;
3931
+ if (this.watchTimer) {
3932
+ clearTimeout(this.watchTimer);
3933
+ this.watchTimer = null;
3934
+ }
3935
+ if (this.sessionWatchTimer) {
3936
+ clearTimeout(this.sessionWatchTimer);
3937
+ this.sessionWatchTimer = null;
3938
+ }
3939
+ if (this.intervalTimer) {
3940
+ clearInterval(this.intervalTimer);
3941
+ this.intervalTimer = null;
3942
+ }
3943
+ if (this.watcher) {
3944
+ await this.watcher.close();
3945
+ this.watcher = null;
3946
+ }
3947
+ if (this.sessionUnsubscribe) {
3948
+ this.sessionUnsubscribe();
3949
+ this.sessionUnsubscribe = null;
3950
+ }
3951
+ if (pendingSync) try {
3952
+ await pendingSync;
3953
+ } catch {}
3954
+ this.db.close();
3955
+ INDEX_CACHE.delete(this.cacheKey);
3956
+ }
3957
+ };
3958
+ //#endregion
3959
+ export { closeAllMemoryIndexManagers as n, resolveMemorySearchConfig as r, MemoryIndexManager as t };