@usewhisper/mcp-server 0.4.0 → 1.0.0
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.
- package/README.md +79 -146
- package/dist/autosubscribe-6EDKPBE2.js +4068 -4068
- package/dist/autosubscribe-GHO6YR5A.js +4068 -4068
- package/dist/autosubscribe-ISDETQIB.js +435 -435
- package/dist/chunk-3WGYBAYR.js +8387 -8387
- package/dist/chunk-52VJYCZ7.js +455 -455
- package/dist/chunk-5KBZQHDL.js +189 -189
- package/dist/chunk-5KIJNY6Z.js +370 -370
- package/dist/chunk-7SN3CKDK.js +1076 -1076
- package/dist/chunk-B3VWOHUA.js +271 -271
- package/dist/chunk-C57DHKTL.js +459 -459
- package/dist/chunk-EI5CE3EY.js +616 -616
- package/dist/chunk-FTWUJBAH.js +386 -386
- package/dist/chunk-H3HSKH2P.js +4841 -4841
- package/dist/chunk-JO3ORBZD.js +616 -616
- package/dist/chunk-L6DXSM2U.js +456 -456
- package/dist/chunk-LMEYV4JD.js +368 -368
- package/dist/chunk-MEFLJ4PV.js +8385 -8385
- package/dist/chunk-OBLI4FE4.js +275 -275
- package/dist/chunk-PPGYJJED.js +271 -271
- package/dist/chunk-QGM4M3NI.js +37 -37
- package/dist/chunk-T7KMSTWP.js +399 -399
- package/dist/chunk-TWEIYHI6.js +399 -399
- package/dist/chunk-UYWE7HSU.js +368 -368
- package/dist/chunk-X2DL2GWT.js +32 -32
- package/dist/chunk-X7HNNNJJ.js +1079 -1079
- package/dist/consolidation-2GCKI4RE.js +220 -220
- package/dist/consolidation-4JOPW6BG.js +220 -220
- package/dist/consolidation-FOVQTWNQ.js +222 -222
- package/dist/consolidation-IFQ52E44.js +209 -209
- package/dist/context-sharing-4ITCNKG4.js +307 -307
- package/dist/context-sharing-6CCFIAKL.js +275 -275
- package/dist/context-sharing-GYKLXHZA.js +307 -307
- package/dist/context-sharing-PH64JTXS.js +308 -308
- package/dist/context-sharing-Y6LTZZOF.js +307 -307
- package/dist/cost-optimization-6OIKRSBV.js +195 -195
- package/dist/cost-optimization-7DVSTL6R.js +307 -307
- package/dist/cost-optimization-BH5NAX33.js +286 -286
- package/dist/cost-optimization-F3L5BS5F.js +303 -303
- package/dist/ingest-2LPTWUUM.js +16 -16
- package/dist/ingest-7T5FAZNC.js +15 -15
- package/dist/ingest-EBNIE7XB.js +15 -15
- package/dist/ingest-FSHT5BCS.js +15 -15
- package/dist/ingest-QE2BTV72.js +14 -14
- package/dist/oracle-3RLQF3DP.js +259 -259
- package/dist/oracle-FKRTQUUG.js +282 -282
- package/dist/oracle-J47QCSEW.js +263 -263
- package/dist/oracle-MDP5MZRC.js +256 -256
- package/dist/search-BLVHWLWC.js +14 -14
- package/dist/search-CZ5NYL5B.js +12 -12
- package/dist/search-EG6TYWWW.js +13 -13
- package/dist/search-I22QQA7T.js +13 -13
- package/dist/search-T7H5G6DW.js +13 -13
- package/dist/server.d.ts +2 -2
- package/dist/server.js +2264 -197
- package/dist/server.js.map +1 -1
- package/package.json +51 -51
package/dist/ingest-EBNIE7XB.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ingestChunk,
|
|
3
|
-
ingestChunksBatch,
|
|
4
|
-
ingestSession,
|
|
5
|
-
updateMemory
|
|
6
|
-
} from "./chunk-52VJYCZ7.js";
|
|
7
|
-
import "./chunk-LMEYV4JD.js";
|
|
8
|
-
import "./chunk-3WGYBAYR.js";
|
|
9
|
-
import "./chunk-QGM4M3NI.js";
|
|
10
|
-
export {
|
|
11
|
-
ingestChunk,
|
|
12
|
-
ingestChunksBatch,
|
|
13
|
-
ingestSession,
|
|
14
|
-
updateMemory
|
|
15
|
-
};
|
|
1
|
+
import {
|
|
2
|
+
ingestChunk,
|
|
3
|
+
ingestChunksBatch,
|
|
4
|
+
ingestSession,
|
|
5
|
+
updateMemory
|
|
6
|
+
} from "./chunk-52VJYCZ7.js";
|
|
7
|
+
import "./chunk-LMEYV4JD.js";
|
|
8
|
+
import "./chunk-3WGYBAYR.js";
|
|
9
|
+
import "./chunk-QGM4M3NI.js";
|
|
10
|
+
export {
|
|
11
|
+
ingestChunk,
|
|
12
|
+
ingestChunksBatch,
|
|
13
|
+
ingestSession,
|
|
14
|
+
updateMemory
|
|
15
|
+
};
|
package/dist/ingest-FSHT5BCS.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ingestChunk,
|
|
3
|
-
ingestChunksBatch,
|
|
4
|
-
ingestSession,
|
|
5
|
-
updateMemory
|
|
6
|
-
} from "./chunk-JO3ORBZD.js";
|
|
7
|
-
import "./chunk-5KBZQHDL.js";
|
|
8
|
-
import "./chunk-MEFLJ4PV.js";
|
|
9
|
-
import "./chunk-QGM4M3NI.js";
|
|
10
|
-
export {
|
|
11
|
-
ingestChunk,
|
|
12
|
-
ingestChunksBatch,
|
|
13
|
-
ingestSession,
|
|
14
|
-
updateMemory
|
|
15
|
-
};
|
|
1
|
+
import {
|
|
2
|
+
ingestChunk,
|
|
3
|
+
ingestChunksBatch,
|
|
4
|
+
ingestSession,
|
|
5
|
+
updateMemory
|
|
6
|
+
} from "./chunk-JO3ORBZD.js";
|
|
7
|
+
import "./chunk-5KBZQHDL.js";
|
|
8
|
+
import "./chunk-MEFLJ4PV.js";
|
|
9
|
+
import "./chunk-QGM4M3NI.js";
|
|
10
|
+
export {
|
|
11
|
+
ingestChunk,
|
|
12
|
+
ingestChunksBatch,
|
|
13
|
+
ingestSession,
|
|
14
|
+
updateMemory
|
|
15
|
+
};
|
package/dist/ingest-QE2BTV72.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ingestChunk,
|
|
3
|
-
ingestChunksBatch,
|
|
4
|
-
ingestSession,
|
|
5
|
-
updateMemory
|
|
6
|
-
} from "./chunk-L6DXSM2U.js";
|
|
7
|
-
import "./chunk-UYWE7HSU.js";
|
|
8
|
-
import "./chunk-X2DL2GWT.js";
|
|
9
|
-
export {
|
|
10
|
-
ingestChunk,
|
|
11
|
-
ingestChunksBatch,
|
|
12
|
-
ingestSession,
|
|
13
|
-
updateMemory
|
|
14
|
-
};
|
|
1
|
+
import {
|
|
2
|
+
ingestChunk,
|
|
3
|
+
ingestChunksBatch,
|
|
4
|
+
ingestSession,
|
|
5
|
+
updateMemory
|
|
6
|
+
} from "./chunk-L6DXSM2U.js";
|
|
7
|
+
import "./chunk-UYWE7HSU.js";
|
|
8
|
+
import "./chunk-X2DL2GWT.js";
|
|
9
|
+
export {
|
|
10
|
+
ingestChunk,
|
|
11
|
+
ingestChunksBatch,
|
|
12
|
+
ingestSession,
|
|
13
|
+
updateMemory
|
|
14
|
+
};
|
|
15
15
|
//# sourceMappingURL=ingest-QE2BTV72.js.map
|
package/dist/oracle-3RLQF3DP.js
CHANGED
|
@@ -1,259 +1,259 @@
|
|
|
1
|
-
import {
|
|
2
|
-
db,
|
|
3
|
-
embedSingle
|
|
4
|
-
} from "./chunk-3WGYBAYR.js";
|
|
5
|
-
import "./chunk-QGM4M3NI.js";
|
|
6
|
-
|
|
7
|
-
// ../src/engine/oracle.ts
|
|
8
|
-
import OpenAI from "openai";
|
|
9
|
-
var openai = new OpenAI({
|
|
10
|
-
apiKey: process.env.OPENAI_API_KEY || ""
|
|
11
|
-
});
|
|
12
|
-
var MAX_DOCUMENT_CHUNKS = 500;
|
|
13
|
-
async function buildDocumentTree(documentId) {
|
|
14
|
-
const document = await db.document.findUnique({
|
|
15
|
-
where: { id: documentId },
|
|
16
|
-
include: {
|
|
17
|
-
chunks: {
|
|
18
|
-
orderBy: { chunkOrder: "asc" },
|
|
19
|
-
take: MAX_DOCUMENT_CHUNKS
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
});
|
|
23
|
-
if (!document) {
|
|
24
|
-
throw new Error("Document not found");
|
|
25
|
-
}
|
|
26
|
-
const root = {
|
|
27
|
-
id: document.id,
|
|
28
|
-
content: document.title,
|
|
29
|
-
type: "document",
|
|
30
|
-
children: [],
|
|
31
|
-
metadata: document.metadata
|
|
32
|
-
};
|
|
33
|
-
const sectionMap = /* @__PURE__ */ new Map();
|
|
34
|
-
for (const chunk of document.chunks) {
|
|
35
|
-
const sectionPath = chunk.metadata?.sectionPath || chunk.sectionPath || "root";
|
|
36
|
-
if (!sectionMap.has(sectionPath)) {
|
|
37
|
-
const sectionNode = {
|
|
38
|
-
id: `${documentId}::${sectionPath}`,
|
|
39
|
-
content: sectionPath,
|
|
40
|
-
type: "section",
|
|
41
|
-
children: [],
|
|
42
|
-
metadata: { path: sectionPath }
|
|
43
|
-
};
|
|
44
|
-
sectionMap.set(sectionPath, sectionNode);
|
|
45
|
-
root.children.push(sectionNode);
|
|
46
|
-
}
|
|
47
|
-
const section = sectionMap.get(sectionPath);
|
|
48
|
-
section.children.push({
|
|
49
|
-
id: chunk.id,
|
|
50
|
-
content: chunk.content,
|
|
51
|
-
type: "chunk",
|
|
52
|
-
children: [],
|
|
53
|
-
metadata: chunk.metadata,
|
|
54
|
-
embedding: chunk.embedding
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
return {
|
|
58
|
-
root,
|
|
59
|
-
depth: calculateDepth(root),
|
|
60
|
-
nodeCount: countNodes(root)
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
function calculateDepth(node) {
|
|
64
|
-
if (node.children.length === 0) return 1;
|
|
65
|
-
return 1 + Math.max(...node.children.map(calculateDepth));
|
|
66
|
-
}
|
|
67
|
-
function countNodes(node) {
|
|
68
|
-
return 1 + node.children.reduce((sum, child) => sum + countNodes(child), 0);
|
|
69
|
-
}
|
|
70
|
-
async function oracleSearch(params) {
|
|
71
|
-
const { query, projectId, topK = 5, maxDepth = 3 } = params;
|
|
72
|
-
console.log(`\u{1F52E} Oracle search: "${query}"`);
|
|
73
|
-
const queryEmbedding = await embedSingle(query);
|
|
74
|
-
const documents = await db.document.findMany({
|
|
75
|
-
where: { projectId },
|
|
76
|
-
take: 10
|
|
77
|
-
});
|
|
78
|
-
const results = [];
|
|
79
|
-
for (const doc of documents) {
|
|
80
|
-
const tree = await buildDocumentTree(doc.id);
|
|
81
|
-
const traversalResults = await guidedTraversal({
|
|
82
|
-
tree,
|
|
83
|
-
query,
|
|
84
|
-
queryEmbedding,
|
|
85
|
-
maxDepth,
|
|
86
|
-
topK
|
|
87
|
-
});
|
|
88
|
-
results.push(...traversalResults);
|
|
89
|
-
}
|
|
90
|
-
results.sort((a, b) => b.relevance - a.relevance);
|
|
91
|
-
return results.slice(0, topK);
|
|
92
|
-
}
|
|
93
|
-
async function guidedTraversal(params) {
|
|
94
|
-
const {
|
|
95
|
-
tree,
|
|
96
|
-
query,
|
|
97
|
-
queryEmbedding,
|
|
98
|
-
maxDepth,
|
|
99
|
-
topK,
|
|
100
|
-
currentNode = tree.root,
|
|
101
|
-
currentDepth = 0,
|
|
102
|
-
path = ""
|
|
103
|
-
} = params;
|
|
104
|
-
const results = [];
|
|
105
|
-
if (currentDepth >= maxDepth) {
|
|
106
|
-
return results;
|
|
107
|
-
}
|
|
108
|
-
if (currentNode.type === "chunk") {
|
|
109
|
-
const relevance = currentNode.embedding ? cosineSimilarity(queryEmbedding, currentNode.embedding) : 0;
|
|
110
|
-
if (relevance > 0.3) {
|
|
111
|
-
results.push({
|
|
112
|
-
content: currentNode.content,
|
|
113
|
-
path,
|
|
114
|
-
relevance
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
return results;
|
|
118
|
-
}
|
|
119
|
-
const childScores = await Promise.all(
|
|
120
|
-
currentNode.children.map(async (child) => {
|
|
121
|
-
const score = await scoreNode(child, query, queryEmbedding);
|
|
122
|
-
return { child, score };
|
|
123
|
-
})
|
|
124
|
-
);
|
|
125
|
-
childScores.sort((a, b) => b.score - a.score);
|
|
126
|
-
const topChildren = childScores.slice(0, Math.min(3, childScores.length));
|
|
127
|
-
for (const { child, score } of topChildren) {
|
|
128
|
-
if (score > 0.2) {
|
|
129
|
-
const childPath = path ? `${path} > ${child.content.substring(0, 30)}` : child.content;
|
|
130
|
-
const childResults = await guidedTraversal({
|
|
131
|
-
tree,
|
|
132
|
-
query,
|
|
133
|
-
queryEmbedding,
|
|
134
|
-
maxDepth,
|
|
135
|
-
topK,
|
|
136
|
-
currentNode: child,
|
|
137
|
-
currentDepth: currentDepth + 1,
|
|
138
|
-
path: childPath
|
|
139
|
-
});
|
|
140
|
-
results.push(...childResults);
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
return results;
|
|
144
|
-
}
|
|
145
|
-
async function scoreNode(node, query, queryEmbedding) {
|
|
146
|
-
if (node.embedding) {
|
|
147
|
-
return cosineSimilarity(queryEmbedding, node.embedding);
|
|
148
|
-
}
|
|
149
|
-
const queryWords = query.toLowerCase().split(/\s+/);
|
|
150
|
-
const nodeWords = node.content.toLowerCase().split(/\s+/);
|
|
151
|
-
const overlap = queryWords.filter((w) => nodeWords.includes(w)).length;
|
|
152
|
-
return overlap / queryWords.length;
|
|
153
|
-
}
|
|
154
|
-
function cosineSimilarity(a, b) {
|
|
155
|
-
if (a.length !== b.length) return 0;
|
|
156
|
-
let dotProduct = 0;
|
|
157
|
-
let normA = 0;
|
|
158
|
-
let normB = 0;
|
|
159
|
-
for (let i = 0; i < a.length; i++) {
|
|
160
|
-
dotProduct += a[i] * b[i];
|
|
161
|
-
normA += a[i] * a[i];
|
|
162
|
-
normB += b[i] * b[i];
|
|
163
|
-
}
|
|
164
|
-
return dotProduct / (Math.sqrt(normA) * Math.sqrt(normB));
|
|
165
|
-
}
|
|
166
|
-
async function oracleResearch(params) {
|
|
167
|
-
const { question, projectId, maxSteps = 5 } = params;
|
|
168
|
-
const steps = [];
|
|
169
|
-
let currentQuery = question;
|
|
170
|
-
for (let step = 1; step <= maxSteps; step++) {
|
|
171
|
-
console.log(`\u{1F52E} Oracle step ${step}: ${currentQuery}`);
|
|
172
|
-
const results = await oracleSearch({
|
|
173
|
-
query: currentQuery,
|
|
174
|
-
projectId,
|
|
175
|
-
topK: 5
|
|
176
|
-
});
|
|
177
|
-
const reasoning = await reasonAboutResults(currentQuery, results, question);
|
|
178
|
-
steps.push({
|
|
179
|
-
step,
|
|
180
|
-
query: currentQuery,
|
|
181
|
-
results,
|
|
182
|
-
reasoning: reasoning.thought
|
|
183
|
-
});
|
|
184
|
-
if (reasoning.hasAnswer) {
|
|
185
|
-
return {
|
|
186
|
-
answer: reasoning.answer,
|
|
187
|
-
steps
|
|
188
|
-
};
|
|
189
|
-
}
|
|
190
|
-
currentQuery = reasoning.nextQuery || question;
|
|
191
|
-
}
|
|
192
|
-
const finalAnswer = await synthesizeAnswer(question, steps);
|
|
193
|
-
return {
|
|
194
|
-
answer: finalAnswer,
|
|
195
|
-
steps
|
|
196
|
-
};
|
|
197
|
-
}
|
|
198
|
-
async function reasonAboutResults(query, results, originalQuestion) {
|
|
199
|
-
const prompt = `You are analyzing search results to answer a question.
|
|
200
|
-
|
|
201
|
-
**Original question:** ${originalQuestion}
|
|
202
|
-
**Current query:** ${query}
|
|
203
|
-
|
|
204
|
-
**Search results:**
|
|
205
|
-
${results.map((r, i) => `${i + 1}. ${r.content} (relevance: ${r.relevance.toFixed(2)})`).join("\n")}
|
|
206
|
-
|
|
207
|
-
Analyze these results:
|
|
208
|
-
1. Do they answer the original question?
|
|
209
|
-
2. What information is still missing?
|
|
210
|
-
3. What should be the next search query?
|
|
211
|
-
|
|
212
|
-
Return JSON:
|
|
213
|
-
{
|
|
214
|
-
"thought": "your analysis",
|
|
215
|
-
"hasAnswer": true or false,
|
|
216
|
-
"answer": "the answer if you have it" or null,
|
|
217
|
-
"nextQuery": "next search query" or null
|
|
218
|
-
}`;
|
|
219
|
-
const response = await openai.chat.completions.create({
|
|
220
|
-
model: "gpt-4o",
|
|
221
|
-
max_tokens: 1024,
|
|
222
|
-
temperature: 0,
|
|
223
|
-
messages: [{ role: "user", content: prompt }],
|
|
224
|
-
response_format: { type: "json_object" }
|
|
225
|
-
});
|
|
226
|
-
const text = response.choices[0]?.message?.content?.trim();
|
|
227
|
-
if (!text) {
|
|
228
|
-
return {
|
|
229
|
-
thought: "Analysis failed",
|
|
230
|
-
hasAnswer: false
|
|
231
|
-
};
|
|
232
|
-
}
|
|
233
|
-
const jsonMatch = text.match(/```json\n?([\s\S]*?)\n?```/) || text.match(/\{[\s\S]*\}/);
|
|
234
|
-
const jsonStr = jsonMatch ? jsonMatch[1] || jsonMatch[0] : text;
|
|
235
|
-
return JSON.parse(jsonStr);
|
|
236
|
-
}
|
|
237
|
-
async function synthesizeAnswer(question, steps) {
|
|
238
|
-
const prompt = `Synthesize a final answer from multiple research steps.
|
|
239
|
-
|
|
240
|
-
**Question:** ${question}
|
|
241
|
-
|
|
242
|
-
**Research steps:**
|
|
243
|
-
${steps.map((s) => `Step ${s.step}: ${s.query}
|
|
244
|
-
${s.reasoning}`).join("\n\n")}
|
|
245
|
-
|
|
246
|
-
Provide a comprehensive answer based on all the information gathered.`;
|
|
247
|
-
const response = await openai.chat.completions.create({
|
|
248
|
-
model: "gpt-4o",
|
|
249
|
-
max_tokens: 2048,
|
|
250
|
-
temperature: 0,
|
|
251
|
-
messages: [{ role: "user", content: prompt }]
|
|
252
|
-
});
|
|
253
|
-
return response.choices[0]?.message?.content || "Unable to synthesize answer";
|
|
254
|
-
}
|
|
255
|
-
export {
|
|
256
|
-
buildDocumentTree,
|
|
257
|
-
oracleResearch,
|
|
258
|
-
oracleSearch
|
|
259
|
-
};
|
|
1
|
+
import {
|
|
2
|
+
db,
|
|
3
|
+
embedSingle
|
|
4
|
+
} from "./chunk-3WGYBAYR.js";
|
|
5
|
+
import "./chunk-QGM4M3NI.js";
|
|
6
|
+
|
|
7
|
+
// ../src/engine/oracle.ts
|
|
8
|
+
import OpenAI from "openai";
|
|
9
|
+
var openai = new OpenAI({
|
|
10
|
+
apiKey: process.env.OPENAI_API_KEY || ""
|
|
11
|
+
});
|
|
12
|
+
var MAX_DOCUMENT_CHUNKS = 500;
|
|
13
|
+
async function buildDocumentTree(documentId) {
|
|
14
|
+
const document = await db.document.findUnique({
|
|
15
|
+
where: { id: documentId },
|
|
16
|
+
include: {
|
|
17
|
+
chunks: {
|
|
18
|
+
orderBy: { chunkOrder: "asc" },
|
|
19
|
+
take: MAX_DOCUMENT_CHUNKS
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
if (!document) {
|
|
24
|
+
throw new Error("Document not found");
|
|
25
|
+
}
|
|
26
|
+
const root = {
|
|
27
|
+
id: document.id,
|
|
28
|
+
content: document.title,
|
|
29
|
+
type: "document",
|
|
30
|
+
children: [],
|
|
31
|
+
metadata: document.metadata
|
|
32
|
+
};
|
|
33
|
+
const sectionMap = /* @__PURE__ */ new Map();
|
|
34
|
+
for (const chunk of document.chunks) {
|
|
35
|
+
const sectionPath = chunk.metadata?.sectionPath || chunk.sectionPath || "root";
|
|
36
|
+
if (!sectionMap.has(sectionPath)) {
|
|
37
|
+
const sectionNode = {
|
|
38
|
+
id: `${documentId}::${sectionPath}`,
|
|
39
|
+
content: sectionPath,
|
|
40
|
+
type: "section",
|
|
41
|
+
children: [],
|
|
42
|
+
metadata: { path: sectionPath }
|
|
43
|
+
};
|
|
44
|
+
sectionMap.set(sectionPath, sectionNode);
|
|
45
|
+
root.children.push(sectionNode);
|
|
46
|
+
}
|
|
47
|
+
const section = sectionMap.get(sectionPath);
|
|
48
|
+
section.children.push({
|
|
49
|
+
id: chunk.id,
|
|
50
|
+
content: chunk.content,
|
|
51
|
+
type: "chunk",
|
|
52
|
+
children: [],
|
|
53
|
+
metadata: chunk.metadata,
|
|
54
|
+
embedding: chunk.embedding
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
return {
|
|
58
|
+
root,
|
|
59
|
+
depth: calculateDepth(root),
|
|
60
|
+
nodeCount: countNodes(root)
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
function calculateDepth(node) {
|
|
64
|
+
if (node.children.length === 0) return 1;
|
|
65
|
+
return 1 + Math.max(...node.children.map(calculateDepth));
|
|
66
|
+
}
|
|
67
|
+
function countNodes(node) {
|
|
68
|
+
return 1 + node.children.reduce((sum, child) => sum + countNodes(child), 0);
|
|
69
|
+
}
|
|
70
|
+
async function oracleSearch(params) {
|
|
71
|
+
const { query, projectId, topK = 5, maxDepth = 3 } = params;
|
|
72
|
+
console.log(`\u{1F52E} Oracle search: "${query}"`);
|
|
73
|
+
const queryEmbedding = await embedSingle(query);
|
|
74
|
+
const documents = await db.document.findMany({
|
|
75
|
+
where: { projectId },
|
|
76
|
+
take: 10
|
|
77
|
+
});
|
|
78
|
+
const results = [];
|
|
79
|
+
for (const doc of documents) {
|
|
80
|
+
const tree = await buildDocumentTree(doc.id);
|
|
81
|
+
const traversalResults = await guidedTraversal({
|
|
82
|
+
tree,
|
|
83
|
+
query,
|
|
84
|
+
queryEmbedding,
|
|
85
|
+
maxDepth,
|
|
86
|
+
topK
|
|
87
|
+
});
|
|
88
|
+
results.push(...traversalResults);
|
|
89
|
+
}
|
|
90
|
+
results.sort((a, b) => b.relevance - a.relevance);
|
|
91
|
+
return results.slice(0, topK);
|
|
92
|
+
}
|
|
93
|
+
async function guidedTraversal(params) {
|
|
94
|
+
const {
|
|
95
|
+
tree,
|
|
96
|
+
query,
|
|
97
|
+
queryEmbedding,
|
|
98
|
+
maxDepth,
|
|
99
|
+
topK,
|
|
100
|
+
currentNode = tree.root,
|
|
101
|
+
currentDepth = 0,
|
|
102
|
+
path = ""
|
|
103
|
+
} = params;
|
|
104
|
+
const results = [];
|
|
105
|
+
if (currentDepth >= maxDepth) {
|
|
106
|
+
return results;
|
|
107
|
+
}
|
|
108
|
+
if (currentNode.type === "chunk") {
|
|
109
|
+
const relevance = currentNode.embedding ? cosineSimilarity(queryEmbedding, currentNode.embedding) : 0;
|
|
110
|
+
if (relevance > 0.3) {
|
|
111
|
+
results.push({
|
|
112
|
+
content: currentNode.content,
|
|
113
|
+
path,
|
|
114
|
+
relevance
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
return results;
|
|
118
|
+
}
|
|
119
|
+
const childScores = await Promise.all(
|
|
120
|
+
currentNode.children.map(async (child) => {
|
|
121
|
+
const score = await scoreNode(child, query, queryEmbedding);
|
|
122
|
+
return { child, score };
|
|
123
|
+
})
|
|
124
|
+
);
|
|
125
|
+
childScores.sort((a, b) => b.score - a.score);
|
|
126
|
+
const topChildren = childScores.slice(0, Math.min(3, childScores.length));
|
|
127
|
+
for (const { child, score } of topChildren) {
|
|
128
|
+
if (score > 0.2) {
|
|
129
|
+
const childPath = path ? `${path} > ${child.content.substring(0, 30)}` : child.content;
|
|
130
|
+
const childResults = await guidedTraversal({
|
|
131
|
+
tree,
|
|
132
|
+
query,
|
|
133
|
+
queryEmbedding,
|
|
134
|
+
maxDepth,
|
|
135
|
+
topK,
|
|
136
|
+
currentNode: child,
|
|
137
|
+
currentDepth: currentDepth + 1,
|
|
138
|
+
path: childPath
|
|
139
|
+
});
|
|
140
|
+
results.push(...childResults);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return results;
|
|
144
|
+
}
|
|
145
|
+
async function scoreNode(node, query, queryEmbedding) {
|
|
146
|
+
if (node.embedding) {
|
|
147
|
+
return cosineSimilarity(queryEmbedding, node.embedding);
|
|
148
|
+
}
|
|
149
|
+
const queryWords = query.toLowerCase().split(/\s+/);
|
|
150
|
+
const nodeWords = node.content.toLowerCase().split(/\s+/);
|
|
151
|
+
const overlap = queryWords.filter((w) => nodeWords.includes(w)).length;
|
|
152
|
+
return overlap / queryWords.length;
|
|
153
|
+
}
|
|
154
|
+
function cosineSimilarity(a, b) {
|
|
155
|
+
if (a.length !== b.length) return 0;
|
|
156
|
+
let dotProduct = 0;
|
|
157
|
+
let normA = 0;
|
|
158
|
+
let normB = 0;
|
|
159
|
+
for (let i = 0; i < a.length; i++) {
|
|
160
|
+
dotProduct += a[i] * b[i];
|
|
161
|
+
normA += a[i] * a[i];
|
|
162
|
+
normB += b[i] * b[i];
|
|
163
|
+
}
|
|
164
|
+
return dotProduct / (Math.sqrt(normA) * Math.sqrt(normB));
|
|
165
|
+
}
|
|
166
|
+
async function oracleResearch(params) {
|
|
167
|
+
const { question, projectId, maxSteps = 5 } = params;
|
|
168
|
+
const steps = [];
|
|
169
|
+
let currentQuery = question;
|
|
170
|
+
for (let step = 1; step <= maxSteps; step++) {
|
|
171
|
+
console.log(`\u{1F52E} Oracle step ${step}: ${currentQuery}`);
|
|
172
|
+
const results = await oracleSearch({
|
|
173
|
+
query: currentQuery,
|
|
174
|
+
projectId,
|
|
175
|
+
topK: 5
|
|
176
|
+
});
|
|
177
|
+
const reasoning = await reasonAboutResults(currentQuery, results, question);
|
|
178
|
+
steps.push({
|
|
179
|
+
step,
|
|
180
|
+
query: currentQuery,
|
|
181
|
+
results,
|
|
182
|
+
reasoning: reasoning.thought
|
|
183
|
+
});
|
|
184
|
+
if (reasoning.hasAnswer) {
|
|
185
|
+
return {
|
|
186
|
+
answer: reasoning.answer,
|
|
187
|
+
steps
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
currentQuery = reasoning.nextQuery || question;
|
|
191
|
+
}
|
|
192
|
+
const finalAnswer = await synthesizeAnswer(question, steps);
|
|
193
|
+
return {
|
|
194
|
+
answer: finalAnswer,
|
|
195
|
+
steps
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
async function reasonAboutResults(query, results, originalQuestion) {
|
|
199
|
+
const prompt = `You are analyzing search results to answer a question.
|
|
200
|
+
|
|
201
|
+
**Original question:** ${originalQuestion}
|
|
202
|
+
**Current query:** ${query}
|
|
203
|
+
|
|
204
|
+
**Search results:**
|
|
205
|
+
${results.map((r, i) => `${i + 1}. ${r.content} (relevance: ${r.relevance.toFixed(2)})`).join("\n")}
|
|
206
|
+
|
|
207
|
+
Analyze these results:
|
|
208
|
+
1. Do they answer the original question?
|
|
209
|
+
2. What information is still missing?
|
|
210
|
+
3. What should be the next search query?
|
|
211
|
+
|
|
212
|
+
Return JSON:
|
|
213
|
+
{
|
|
214
|
+
"thought": "your analysis",
|
|
215
|
+
"hasAnswer": true or false,
|
|
216
|
+
"answer": "the answer if you have it" or null,
|
|
217
|
+
"nextQuery": "next search query" or null
|
|
218
|
+
}`;
|
|
219
|
+
const response = await openai.chat.completions.create({
|
|
220
|
+
model: "gpt-4o",
|
|
221
|
+
max_tokens: 1024,
|
|
222
|
+
temperature: 0,
|
|
223
|
+
messages: [{ role: "user", content: prompt }],
|
|
224
|
+
response_format: { type: "json_object" }
|
|
225
|
+
});
|
|
226
|
+
const text = response.choices[0]?.message?.content?.trim();
|
|
227
|
+
if (!text) {
|
|
228
|
+
return {
|
|
229
|
+
thought: "Analysis failed",
|
|
230
|
+
hasAnswer: false
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
const jsonMatch = text.match(/```json\n?([\s\S]*?)\n?```/) || text.match(/\{[\s\S]*\}/);
|
|
234
|
+
const jsonStr = jsonMatch ? jsonMatch[1] || jsonMatch[0] : text;
|
|
235
|
+
return JSON.parse(jsonStr);
|
|
236
|
+
}
|
|
237
|
+
async function synthesizeAnswer(question, steps) {
|
|
238
|
+
const prompt = `Synthesize a final answer from multiple research steps.
|
|
239
|
+
|
|
240
|
+
**Question:** ${question}
|
|
241
|
+
|
|
242
|
+
**Research steps:**
|
|
243
|
+
${steps.map((s) => `Step ${s.step}: ${s.query}
|
|
244
|
+
${s.reasoning}`).join("\n\n")}
|
|
245
|
+
|
|
246
|
+
Provide a comprehensive answer based on all the information gathered.`;
|
|
247
|
+
const response = await openai.chat.completions.create({
|
|
248
|
+
model: "gpt-4o",
|
|
249
|
+
max_tokens: 2048,
|
|
250
|
+
temperature: 0,
|
|
251
|
+
messages: [{ role: "user", content: prompt }]
|
|
252
|
+
});
|
|
253
|
+
return response.choices[0]?.message?.content || "Unable to synthesize answer";
|
|
254
|
+
}
|
|
255
|
+
export {
|
|
256
|
+
buildDocumentTree,
|
|
257
|
+
oracleResearch,
|
|
258
|
+
oracleSearch
|
|
259
|
+
};
|