@wipcomputer/memory-crystal 0.7.34-alpha.3 → 0.7.34-alpha.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 (97) hide show
  1. package/package.json +7 -4
  2. package/dist/chunk-25LXQJ4Z.js +0 -110
  3. package/dist/chunk-2DRXIRQW.js +0 -97
  4. package/dist/chunk-2GBYLMEF.js +0 -1385
  5. package/dist/chunk-2ZNH5F6E.js +0 -1281
  6. package/dist/chunk-3G3SFYYI.js +0 -288
  7. package/dist/chunk-3RG5ZIWI.js +0 -10
  8. package/dist/chunk-3S6TI23B.js +0 -97
  9. package/dist/chunk-3VFIJYS4.js +0 -818
  10. package/dist/chunk-437F27T6.js +0 -97
  11. package/dist/chunk-52QE3YI3.js +0 -1169
  12. package/dist/chunk-57RP3DIN.js +0 -1205
  13. package/dist/chunk-5HSZ4W2P.js +0 -62
  14. package/dist/chunk-5I7GMRDN.js +0 -146
  15. package/dist/chunk-645IPXW3.js +0 -290
  16. package/dist/chunk-7A7ELD4C.js +0 -1205
  17. package/dist/chunk-7FYY4GZM.js +0 -1205
  18. package/dist/chunk-7IUE7ODU.js +0 -254
  19. package/dist/chunk-7RMLKZIS.js +0 -108
  20. package/dist/chunk-AA3OPP4Z.js +0 -432
  21. package/dist/chunk-AEWLSYPH.js +0 -72
  22. package/dist/chunk-ASSZDR6I.js +0 -108
  23. package/dist/chunk-AYRJVWUC.js +0 -1205
  24. package/dist/chunk-CCYI5O3D.js +0 -148
  25. package/dist/chunk-CGIDSAJB.js +0 -288
  26. package/dist/chunk-D3I3ZSE2.js +0 -411
  27. package/dist/chunk-D3MACYZ4.js +0 -108
  28. package/dist/chunk-DACSKLY6.js +0 -219
  29. package/dist/chunk-DFQ72B7M.js +0 -248
  30. package/dist/chunk-DW5B4BL7.js +0 -108
  31. package/dist/chunk-EKSACBTJ.js +0 -1070
  32. package/dist/chunk-EXEZZADG.js +0 -248
  33. package/dist/chunk-F3Y7EL7K.js +0 -83
  34. package/dist/chunk-FBQWSDPC.js +0 -1328
  35. package/dist/chunk-FHRZNOMW.js +0 -1205
  36. package/dist/chunk-IM7N24MT.js +0 -129
  37. package/dist/chunk-IPNYIXFK.js +0 -1178
  38. package/dist/chunk-J7MRSZIO.js +0 -167
  39. package/dist/chunk-JITKI2OI.js +0 -106
  40. package/dist/chunk-JWZXYVET.js +0 -1068
  41. package/dist/chunk-KCQUXVYT.js +0 -108
  42. package/dist/chunk-KOQ43OX6.js +0 -1281
  43. package/dist/chunk-KYVWO6ZM.js +0 -1069
  44. package/dist/chunk-L3VHARQH.js +0 -413
  45. package/dist/chunk-LBWDS6BE.js +0 -288
  46. package/dist/chunk-LOVAHSQV.js +0 -411
  47. package/dist/chunk-LQOYCAGG.js +0 -446
  48. package/dist/chunk-LWAIPJ2W.js +0 -146
  49. package/dist/chunk-M5DHKW7M.js +0 -127
  50. package/dist/chunk-MBKCIJHM.js +0 -1328
  51. package/dist/chunk-MK42FMEG.js +0 -147
  52. package/dist/chunk-MOBMYHKL.js +0 -1205
  53. package/dist/chunk-MPLTNMRG.js +0 -67
  54. package/dist/chunk-NIJCVN3O.js +0 -147
  55. package/dist/chunk-NX647OM3.js +0 -310
  56. package/dist/chunk-NZCFSZQ7.js +0 -1205
  57. package/dist/chunk-O2UITJGH.js +0 -465
  58. package/dist/chunk-OCRA44AZ.js +0 -108
  59. package/dist/chunk-P3KJR66H.js +0 -117
  60. package/dist/chunk-PEK6JH65.js +0 -432
  61. package/dist/chunk-PJ6FFKEX.js +0 -77
  62. package/dist/chunk-PLUBBZYR.js +0 -800
  63. package/dist/chunk-PNKVD2UK.js +0 -26
  64. package/dist/chunk-PSQZURHO.js +0 -229
  65. package/dist/chunk-SGL6ISBJ.js +0 -1061
  66. package/dist/chunk-SJABZZT5.js +0 -97
  67. package/dist/chunk-TD3P3K32.js +0 -1199
  68. package/dist/chunk-TMDZJJKV.js +0 -288
  69. package/dist/chunk-UNHVZB5G.js +0 -411
  70. package/dist/chunk-VAFTWSTE.js +0 -1061
  71. package/dist/chunk-VNFXFQBB.js +0 -217
  72. package/dist/chunk-X3GVFKSJ.js +0 -1205
  73. package/dist/chunk-XZ3S56RQ.js +0 -1061
  74. package/dist/chunk-Y72C7F6O.js +0 -148
  75. package/dist/chunk-YLICP577.js +0 -1205
  76. package/dist/chunk-YX6AXLVK.js +0 -159
  77. package/dist/chunk-ZCQYHTNU.js +0 -146
  78. package/dist/cloud-crystal.js +0 -6
  79. package/dist/dev-update-SZ2Z4WCQ.js +0 -6
  80. package/dist/llm-XXLYPIOF.js +0 -16
  81. package/dist/mlx-setup-XKU67WCT.js +0 -289
  82. package/dist/search-pipeline-4K4OJSSS.js +0 -255
  83. package/dist/search-pipeline-4PRS6LI7.js +0 -280
  84. package/dist/search-pipeline-7UJMXPLO.js +0 -280
  85. package/dist/search-pipeline-CBV25NX7.js +0 -99
  86. package/dist/search-pipeline-DQTRLGBH.js +0 -74
  87. package/dist/search-pipeline-HNG37REH.js +0 -282
  88. package/dist/search-pipeline-IZFPLBUB.js +0 -280
  89. package/dist/search-pipeline-MID6F26Q.js +0 -73
  90. package/dist/search-pipeline-N52JZFNN.js +0 -282
  91. package/dist/search-pipeline-OPB2PRQQ.js +0 -280
  92. package/dist/search-pipeline-VXTE5HAD.js +0 -262
  93. package/dist/search-pipeline-XHFKADRG.js +0 -73
  94. package/dist/worker-demo.js +0 -186
  95. package/dist/worker-mcp.js +0 -404
  96. package/scripts/crystal-capture 2.sh +0 -29
  97. package/scripts/deploy-cloud 2.sh +0 -153
@@ -1,255 +0,0 @@
1
- // src/llm.ts
2
- var expansionCache = /* @__PURE__ */ new Map();
3
- var detectedProvider = null;
4
- var detectionDone = false;
5
- async function detectProvider() {
6
- if (detectionDone && detectedProvider) return detectedProvider;
7
- detectionDone = true;
8
- try {
9
- const resp = await fetch("http://localhost:8080/v1/models", { signal: AbortSignal.timeout(1e3) });
10
- if (resp.ok) {
11
- const data = await resp.json();
12
- const model = data?.data?.[0]?.id || "default";
13
- detectedProvider = { provider: "mlx", baseURL: "http://localhost:8080/v1", apiKey: "not-needed", model };
14
- process.stderr.write(`[memory-crystal] LLM provider: MLX (${model})
15
- `);
16
- return detectedProvider;
17
- }
18
- } catch {
19
- }
20
- try {
21
- const resp = await fetch("http://localhost:11434/api/tags", { signal: AbortSignal.timeout(1e3) });
22
- if (resp.ok) {
23
- detectedProvider = { provider: "ollama", baseURL: "http://localhost:11434/v1", apiKey: "ollama", model: "qwen3:1.7b" };
24
- process.stderr.write("[memory-crystal] LLM provider: Ollama\n");
25
- return detectedProvider;
26
- }
27
- } catch {
28
- }
29
- const anthropicKey = process.env.ANTHROPIC_API_KEY;
30
- if (anthropicKey) {
31
- detectedProvider = { provider: "anthropic", baseURL: "https://api.anthropic.com", apiKey: anthropicKey, model: "claude-haiku-4-5-20251001" };
32
- process.stderr.write("[memory-crystal] LLM provider: Anthropic API\n");
33
- return detectedProvider;
34
- }
35
- const openaiKey = process.env.OPENAI_API_KEY;
36
- if (openaiKey) {
37
- detectedProvider = { provider: "openai", baseURL: "https://api.openai.com/v1", apiKey: openaiKey, model: "gpt-4o-mini" };
38
- process.stderr.write("[memory-crystal] LLM provider: OpenAI API\n");
39
- return detectedProvider;
40
- }
41
- detectedProvider = { provider: "none", baseURL: "", apiKey: "", model: "" };
42
- process.stderr.write("[memory-crystal] LLM provider: none (deep search unavailable)\n");
43
- return detectedProvider;
44
- }
45
- async function chatComplete(config, messages, maxTokens = 300) {
46
- if (config.provider === "anthropic") {
47
- return anthropicComplete(config, messages, maxTokens);
48
- }
49
- const resp = await fetch(`${config.baseURL}/chat/completions`, {
50
- method: "POST",
51
- headers: {
52
- "Content-Type": "application/json",
53
- "Authorization": `Bearer ${config.apiKey}`
54
- },
55
- body: JSON.stringify({
56
- model: config.model,
57
- messages,
58
- max_tokens: maxTokens,
59
- temperature: 0.7
60
- })
61
- });
62
- if (!resp.ok) throw new Error(`LLM request failed: ${resp.status}`);
63
- const data = await resp.json();
64
- return data.choices?.[0]?.message?.content || "";
65
- }
66
- async function anthropicComplete(config, messages, maxTokens) {
67
- const systemMsg = messages.find((m) => m.role === "system");
68
- const userMessages = messages.filter((m) => m.role !== "system");
69
- const body = {
70
- model: config.model,
71
- max_tokens: maxTokens,
72
- messages: userMessages
73
- };
74
- if (systemMsg) body.system = systemMsg.content;
75
- const resp = await fetch("https://api.anthropic.com/v1/messages", {
76
- method: "POST",
77
- headers: {
78
- "Content-Type": "application/json",
79
- "x-api-key": config.apiKey,
80
- "anthropic-version": "2023-06-01"
81
- },
82
- body: JSON.stringify(body)
83
- });
84
- if (!resp.ok) throw new Error(`Anthropic request failed: ${resp.status}`);
85
- const data = await resp.json();
86
- return data.content?.[0]?.text || "";
87
- }
88
- var EXPAND_PROMPT = `You are a search query expander. Given a search query, generate exactly 3 variations to improve search recall.
89
-
90
- Output exactly 3 lines in this format (no other text):
91
- lex: <keyword-focused variation for full-text search>
92
- vec: <semantic variation rephrased for embedding similarity>
93
- hyde: <hypothetical document snippet that would answer this query>
94
-
95
- Rules:
96
- - Each variation must contain at least one term from the original query
97
- - Keep variations concise (under 30 words each)
98
- - lex should use specific keywords and synonyms
99
- - vec should rephrase the intent naturally
100
- - hyde should be a short passage as if answering the query`;
101
- async function expandQuery(query) {
102
- const cached = expansionCache.get(query);
103
- if (cached) return cached;
104
- const config = await detectProvider();
105
- if (config.provider === "none") return [];
106
- try {
107
- const result = await chatComplete(config, [
108
- { role: "system", content: EXPAND_PROMPT },
109
- { role: "user", content: query }
110
- ], 300);
111
- const lines = result.trim().split("\n");
112
- const queryLower = query.toLowerCase();
113
- const queryTerms = queryLower.replace(/[^a-z0-9\s]/g, " ").split(/\s+/).filter(Boolean);
114
- const hasQueryTerm = (text) => {
115
- const lower = text.toLowerCase();
116
- if (queryTerms.length === 0) return true;
117
- return queryTerms.some((term) => lower.includes(term));
118
- };
119
- const variations = lines.map((line) => {
120
- const colonIdx = line.indexOf(":");
121
- if (colonIdx === -1) return null;
122
- const type = line.slice(0, colonIdx).trim();
123
- if (type !== "lex" && type !== "vec" && type !== "hyde") return null;
124
- const text = line.slice(colonIdx + 1).trim();
125
- if (!text || !hasQueryTerm(text)) return null;
126
- return { type, text };
127
- }).filter((v) => v !== null);
128
- if (variations.length > 0) {
129
- expansionCache.set(query, variations);
130
- return variations;
131
- }
132
- } catch (err) {
133
- process.stderr.write(`[memory-crystal] Query expansion failed: ${err.message}
134
- `);
135
- }
136
- const fallback = [
137
- { type: "lex", text: query },
138
- { type: "vec", text: query },
139
- { type: "hyde", text: `Information about ${query}` }
140
- ];
141
- return fallback;
142
- }
143
- var RERANK_PROMPT = `You are a search result re-ranker. Given a query and a list of text passages, rate each passage's relevance to the query.
144
-
145
- Output one line per passage in this exact format:
146
- <index>: <score>
147
-
148
- Where index is the passage number (0-based) and score is a float from 0.0 to 1.0.
149
- - 1.0 = perfectly relevant, directly answers the query
150
- - 0.7 = highly relevant, closely related
151
- - 0.4 = somewhat relevant, tangentially related
152
- - 0.1 = barely relevant
153
- - 0.0 = not relevant at all
154
-
155
- Rate ALL passages. Output nothing else.`;
156
- async function rerankResults(query, passages) {
157
- const config = await detectProvider();
158
- if (config.provider === "none") {
159
- return passages.map((_, i) => ({ index: i, score: 1 - i * 0.01 }));
160
- }
161
- try {
162
- const passageList = passages.map((p, i) => `[${i}] ${p.slice(0, 500)}`).join("\n\n");
163
- const result = await chatComplete(config, [
164
- { role: "system", content: RERANK_PROMPT },
165
- { role: "user", content: `Query: ${query}
166
-
167
- Passages:
168
- ${passageList}` }
169
- ], 200);
170
- const results = [];
171
- for (const line of result.trim().split("\n")) {
172
- const match = line.match(/^(\d+):\s*([\d.]+)/);
173
- if (match) {
174
- results.push({ index: parseInt(match[1]), score: parseFloat(match[2]) });
175
- }
176
- }
177
- const scored = new Set(results.map((r) => r.index));
178
- for (let i = 0; i < passages.length; i++) {
179
- if (!scored.has(i)) results.push({ index: i, score: 0 });
180
- }
181
- return results.sort((a, b) => b.score - a.score);
182
- } catch (err) {
183
- process.stderr.write(`[memory-crystal] Reranking failed: ${err.message}
184
- `);
185
- return passages.map((_, i) => ({ index: i, score: 1 - i * 0.01 }));
186
- }
187
- }
188
-
189
- // src/search-pipeline.ts
190
- var STRONG_SIGNAL_MIN_SCORE = 0.85;
191
- var STRONG_SIGNAL_MIN_GAP = 0.15;
192
- var RERANK_CANDIDATE_LIMIT = 40;
193
- async function deepSearch(crystal, query, options = {}) {
194
- const limit = options.limit || 5;
195
- const filter = options.filter;
196
- const provider = await detectProvider();
197
- if (provider.provider === "none") {
198
- return crystal.search(query, limit, filter);
199
- }
200
- const db = crystal.sqliteDb;
201
- if (!db) return crystal.search(query, limit, filter);
202
- const sinceDate = filter?.since ? crystal.parseSince(filter.since) : void 0;
203
- const internalFilter = { ...filter, sinceDate };
204
- const initialFts = crystal.searchFTS(query, 20, internalFilter);
205
- const topScore = initialFts[0]?.score ?? 0;
206
- const secondScore = initialFts[1]?.score ?? 0;
207
- const hasStrongSignal = initialFts.length > 0 && topScore >= STRONG_SIGNAL_MIN_SCORE && topScore - secondScore >= STRONG_SIGNAL_MIN_GAP;
208
- const expanded = hasStrongSignal ? [] : await expandQuery(query);
209
- const allResultLists = [];
210
- if (initialFts.length > 0) allResultLists.push(initialFts);
211
- const [queryEmbedding] = await crystal.embed([query]);
212
- const originalVec = crystal.searchVec(queryEmbedding, 30, internalFilter);
213
- if (originalVec.length > 0) allResultLists.push(originalVec);
214
- for (const variation of expanded) {
215
- if (variation.type === "lex") {
216
- const ftsResults = crystal.searchFTS(variation.text, 20, internalFilter);
217
- if (ftsResults.length > 0) allResultLists.push(ftsResults);
218
- } else {
219
- const [embedding] = await crystal.embed([variation.text]);
220
- const vecResults = crystal.searchVec(embedding, 20, internalFilter);
221
- if (vecResults.length > 0) allResultLists.push(vecResults);
222
- }
223
- }
224
- const weights = allResultLists.map((_, i) => i < 2 ? 2 : 1);
225
- const fused = crystal.reciprocalRankFusion(allResultLists, weights);
226
- const candidates = fused.slice(0, RERANK_CANDIDATE_LIMIT);
227
- if (candidates.length === 0) return [];
228
- const passages = candidates.map((c) => c.text.slice(0, 500));
229
- const reranked = await rerankResults(query, passages);
230
- const now = Date.now();
231
- const blended = reranked.map((r) => {
232
- const candidate = candidates[r.index];
233
- if (!candidate) return null;
234
- const rrfRank = r.index + 1;
235
- let rrfWeight;
236
- if (rrfRank <= 3) rrfWeight = 0.75;
237
- else if (rrfRank <= 10) rrfWeight = 0.6;
238
- else rrfWeight = 0.4;
239
- const rrfScore = 1 / rrfRank;
240
- const blendedScore = rrfWeight * rrfScore + (1 - rrfWeight) * r.score;
241
- const ageDays = candidate.created_at ? (now - new Date(candidate.created_at).getTime()) / 864e5 : 0;
242
- const recency = candidate.created_at ? crystal.recencyWeight(ageDays) : 1;
243
- const finalScore = Math.min(blendedScore * recency * 8, 1);
244
- const freshness = candidate.created_at ? crystal.freshnessLabel(ageDays) : void 0;
245
- return {
246
- ...candidate,
247
- score: finalScore,
248
- freshness
249
- };
250
- }).filter((r) => r !== null);
251
- return blended.sort((a, b) => b.score - a.score).slice(0, limit);
252
- }
253
- export {
254
- deepSearch
255
- };
@@ -1,280 +0,0 @@
1
- // src/llm.ts
2
- import { existsSync, readFileSync } from "fs";
3
- import { join } from "path";
4
- import { homedir } from "os";
5
- import { execSync } from "child_process";
6
- var expansionCache = /* @__PURE__ */ new Map();
7
- var detectedProvider = null;
8
- var detectionDone = false;
9
- function getOpSecret(itemName, fieldLabel) {
10
- try {
11
- const saTokenPath = join(homedir(), ".openclaw/secrets/op-sa-token");
12
- if (!existsSync(saTokenPath)) return void 0;
13
- const saToken = readFileSync(saTokenPath, "utf-8").trim();
14
- const result = execSync(
15
- `OP_SERVICE_ACCOUNT_TOKEN="${saToken}" op item get "${itemName}" --vault "Agent Secrets" --fields "${fieldLabel}" --reveal`,
16
- { encoding: "utf-8", timeout: 5e3, stdio: ["pipe", "pipe", "pipe"] }
17
- ).trim();
18
- return result || void 0;
19
- } catch {
20
- return void 0;
21
- }
22
- }
23
- async function detectProvider() {
24
- if (detectionDone && detectedProvider) return detectedProvider;
25
- detectionDone = true;
26
- try {
27
- const resp = await fetch("http://localhost:8080/v1/models", { signal: AbortSignal.timeout(1e3) });
28
- if (resp.ok) {
29
- const data = await resp.json();
30
- const model = data?.data?.[0]?.id || "default";
31
- detectedProvider = { provider: "mlx", baseURL: "http://localhost:8080/v1", apiKey: "not-needed", model };
32
- process.stderr.write(`[memory-crystal] LLM provider: MLX (${model})
33
- `);
34
- return detectedProvider;
35
- }
36
- } catch {
37
- }
38
- try {
39
- const resp = await fetch("http://localhost:11434/api/tags", { signal: AbortSignal.timeout(1e3) });
40
- if (resp.ok) {
41
- const data = await resp.json();
42
- const models = data?.models || [];
43
- const embeddingOnly = ["nomic-embed-text", "mxbai-embed", "all-minilm", "snowflake-arctic-embed"];
44
- const chatModel = models.find((m) => !embeddingOnly.some((e) => m.name.startsWith(e)));
45
- if (chatModel) {
46
- detectedProvider = { provider: "ollama", baseURL: "http://localhost:11434/v1", apiKey: "ollama", model: chatModel.name };
47
- process.stderr.write(`[memory-crystal] LLM provider: Ollama (${chatModel.name})
48
- `);
49
- return detectedProvider;
50
- }
51
- }
52
- } catch {
53
- }
54
- const openaiKey = process.env.OPENAI_API_KEY || getOpSecret("OpenAI API", "api key");
55
- if (openaiKey) {
56
- detectedProvider = { provider: "openai", baseURL: "https://api.openai.com/v1", apiKey: openaiKey, model: "gpt-4o-mini" };
57
- process.stderr.write("[memory-crystal] LLM provider: OpenAI API\n");
58
- return detectedProvider;
59
- }
60
- const anthropicKey = process.env.ANTHROPIC_API_KEY || getOpSecret("Anthropic Auth Token - remote bunkers", "Auth Token");
61
- if (anthropicKey && !anthropicKey.startsWith("sk-ant-oat")) {
62
- detectedProvider = { provider: "anthropic", baseURL: "https://api.anthropic.com", apiKey: anthropicKey, model: "claude-haiku-4-5-20251001" };
63
- process.stderr.write("[memory-crystal] LLM provider: Anthropic API\n");
64
- return detectedProvider;
65
- }
66
- detectedProvider = { provider: "none", baseURL: "", apiKey: "", model: "" };
67
- process.stderr.write("[memory-crystal] LLM provider: none (deep search unavailable)\n");
68
- return detectedProvider;
69
- }
70
- async function chatComplete(config, messages, maxTokens = 300) {
71
- if (config.provider === "anthropic") {
72
- return anthropicComplete(config, messages, maxTokens);
73
- }
74
- const resp = await fetch(`${config.baseURL}/chat/completions`, {
75
- method: "POST",
76
- headers: {
77
- "Content-Type": "application/json",
78
- "Authorization": `Bearer ${config.apiKey}`
79
- },
80
- body: JSON.stringify({
81
- model: config.model,
82
- messages,
83
- max_tokens: maxTokens,
84
- temperature: 0.7
85
- })
86
- });
87
- if (!resp.ok) throw new Error(`LLM request failed: ${resp.status}`);
88
- const data = await resp.json();
89
- return data.choices?.[0]?.message?.content || "";
90
- }
91
- async function anthropicComplete(config, messages, maxTokens) {
92
- const systemMsg = messages.find((m) => m.role === "system");
93
- const userMessages = messages.filter((m) => m.role !== "system");
94
- const body = {
95
- model: config.model,
96
- max_tokens: maxTokens,
97
- messages: userMessages
98
- };
99
- if (systemMsg) body.system = systemMsg.content;
100
- const resp = await fetch("https://api.anthropic.com/v1/messages", {
101
- method: "POST",
102
- headers: {
103
- "Content-Type": "application/json",
104
- "x-api-key": config.apiKey,
105
- "anthropic-version": "2023-06-01"
106
- },
107
- body: JSON.stringify(body)
108
- });
109
- if (!resp.ok) throw new Error(`Anthropic request failed: ${resp.status}`);
110
- const data = await resp.json();
111
- return data.content?.[0]?.text || "";
112
- }
113
- var EXPAND_PROMPT = `You are a search query expander. Given a search query, generate exactly 3 variations to improve search recall.
114
-
115
- Output exactly 3 lines in this format (no other text):
116
- lex: <keyword-focused variation for full-text search>
117
- vec: <semantic variation rephrased for embedding similarity>
118
- hyde: <hypothetical document snippet that would answer this query>
119
-
120
- Rules:
121
- - Each variation must contain at least one term from the original query
122
- - Keep variations concise (under 30 words each)
123
- - lex should use specific keywords and synonyms
124
- - vec should rephrase the intent naturally
125
- - hyde should be a short passage as if answering the query`;
126
- async function expandQuery(query) {
127
- const cached = expansionCache.get(query);
128
- if (cached) return cached;
129
- const config = await detectProvider();
130
- if (config.provider === "none") return [];
131
- try {
132
- const result = await chatComplete(config, [
133
- { role: "system", content: EXPAND_PROMPT },
134
- { role: "user", content: query }
135
- ], 300);
136
- const lines = result.trim().split("\n");
137
- const queryLower = query.toLowerCase();
138
- const queryTerms = queryLower.replace(/[^a-z0-9\s]/g, " ").split(/\s+/).filter(Boolean);
139
- const hasQueryTerm = (text) => {
140
- const lower = text.toLowerCase();
141
- if (queryTerms.length === 0) return true;
142
- return queryTerms.some((term) => lower.includes(term));
143
- };
144
- const variations = lines.map((line) => {
145
- const colonIdx = line.indexOf(":");
146
- if (colonIdx === -1) return null;
147
- const type = line.slice(0, colonIdx).trim();
148
- if (type !== "lex" && type !== "vec" && type !== "hyde") return null;
149
- const text = line.slice(colonIdx + 1).trim();
150
- if (!text || !hasQueryTerm(text)) return null;
151
- return { type, text };
152
- }).filter((v) => v !== null);
153
- if (variations.length > 0) {
154
- expansionCache.set(query, variations);
155
- return variations;
156
- }
157
- } catch (err) {
158
- process.stderr.write(`[memory-crystal] Query expansion failed: ${err.message}
159
- `);
160
- }
161
- const fallback = [
162
- { type: "lex", text: query },
163
- { type: "vec", text: query },
164
- { type: "hyde", text: `Information about ${query}` }
165
- ];
166
- return fallback;
167
- }
168
- var RERANK_PROMPT = `You are a search result re-ranker. Given a query and a list of text passages, rate each passage's relevance to the query.
169
-
170
- Output one line per passage in this exact format:
171
- <index>: <score>
172
-
173
- Where index is the passage number (0-based) and score is a float from 0.0 to 1.0.
174
- - 1.0 = perfectly relevant, directly answers the query
175
- - 0.7 = highly relevant, closely related
176
- - 0.4 = somewhat relevant, tangentially related
177
- - 0.1 = barely relevant
178
- - 0.0 = not relevant at all
179
-
180
- Rate ALL passages. Output nothing else.`;
181
- async function rerankResults(query, passages) {
182
- const config = await detectProvider();
183
- if (config.provider === "none") {
184
- return passages.map((_, i) => ({ index: i, score: 1 - i * 0.01 }));
185
- }
186
- try {
187
- const passageList = passages.map((p, i) => `[${i}] ${p.slice(0, 500)}`).join("\n\n");
188
- const result = await chatComplete(config, [
189
- { role: "system", content: RERANK_PROMPT },
190
- { role: "user", content: `Query: ${query}
191
-
192
- Passages:
193
- ${passageList}` }
194
- ], 200);
195
- const results = [];
196
- for (const line of result.trim().split("\n")) {
197
- const match = line.match(/^(\d+):\s*([\d.]+)/);
198
- if (match) {
199
- results.push({ index: parseInt(match[1]), score: parseFloat(match[2]) });
200
- }
201
- }
202
- const scored = new Set(results.map((r) => r.index));
203
- for (let i = 0; i < passages.length; i++) {
204
- if (!scored.has(i)) results.push({ index: i, score: 0 });
205
- }
206
- return results.sort((a, b) => b.score - a.score);
207
- } catch (err) {
208
- process.stderr.write(`[memory-crystal] Reranking failed: ${err.message}
209
- `);
210
- return passages.map((_, i) => ({ index: i, score: 1 - i * 0.01 }));
211
- }
212
- }
213
-
214
- // src/search-pipeline.ts
215
- var STRONG_SIGNAL_MIN_SCORE = 0.85;
216
- var STRONG_SIGNAL_MIN_GAP = 0.15;
217
- var RERANK_CANDIDATE_LIMIT = 40;
218
- async function deepSearch(crystal, query, options = {}) {
219
- const limit = options.limit || 5;
220
- const filter = options.filter;
221
- const provider = await detectProvider();
222
- if (provider.provider === "none") {
223
- return crystal.search(query, limit, filter);
224
- }
225
- const db = crystal.sqliteDb;
226
- if (!db) return crystal.search(query, limit, filter);
227
- const sinceDate = filter?.since ? crystal.parseSince(filter.since) : void 0;
228
- const internalFilter = { ...filter, sinceDate };
229
- const initialFts = crystal.searchFTS(query, 20, internalFilter);
230
- const topScore = initialFts[0]?.score ?? 0;
231
- const secondScore = initialFts[1]?.score ?? 0;
232
- const hasStrongSignal = initialFts.length > 0 && topScore >= STRONG_SIGNAL_MIN_SCORE && topScore - secondScore >= STRONG_SIGNAL_MIN_GAP;
233
- const expanded = hasStrongSignal ? [] : await expandQuery(query);
234
- const allResultLists = [];
235
- if (initialFts.length > 0) allResultLists.push(initialFts);
236
- const [queryEmbedding] = await crystal.embed([query]);
237
- const originalVec = crystal.searchVec(queryEmbedding, 30, internalFilter);
238
- if (originalVec.length > 0) allResultLists.push(originalVec);
239
- for (const variation of expanded) {
240
- if (variation.type === "lex") {
241
- const ftsResults = crystal.searchFTS(variation.text, 20, internalFilter);
242
- if (ftsResults.length > 0) allResultLists.push(ftsResults);
243
- } else {
244
- const [embedding] = await crystal.embed([variation.text]);
245
- const vecResults = crystal.searchVec(embedding, 20, internalFilter);
246
- if (vecResults.length > 0) allResultLists.push(vecResults);
247
- }
248
- }
249
- const weights = allResultLists.map((_, i) => i < 2 ? 2 : 1);
250
- const fused = crystal.reciprocalRankFusion(allResultLists, weights);
251
- const candidates = fused.slice(0, RERANK_CANDIDATE_LIMIT);
252
- if (candidates.length === 0) return [];
253
- const passages = candidates.map((c) => c.text.slice(0, 500));
254
- const reranked = await rerankResults(query, passages);
255
- const now = Date.now();
256
- const blended = reranked.map((r) => {
257
- const candidate = candidates[r.index];
258
- if (!candidate) return null;
259
- const rrfRank = r.index + 1;
260
- let rrfWeight;
261
- if (rrfRank <= 3) rrfWeight = 0.75;
262
- else if (rrfRank <= 10) rrfWeight = 0.6;
263
- else rrfWeight = 0.4;
264
- const rrfScore = 1 / rrfRank;
265
- const blendedScore = rrfWeight * rrfScore + (1 - rrfWeight) * r.score;
266
- const ageDays = candidate.created_at ? (now - new Date(candidate.created_at).getTime()) / 864e5 : 0;
267
- const recency = candidate.created_at ? crystal.recencyWeight(ageDays) : 1;
268
- const finalScore = Math.min(blendedScore * recency * 8, 1);
269
- const freshness = candidate.created_at ? crystal.freshnessLabel(ageDays) : void 0;
270
- return {
271
- ...candidate,
272
- score: finalScore,
273
- freshness
274
- };
275
- }).filter((r) => r !== null);
276
- return blended.sort((a, b) => b.score - a.score).slice(0, limit);
277
- }
278
- export {
279
- deepSearch
280
- };