@usewhisper/mcp-server 0.2.3 → 0.3.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.
@@ -0,0 +1,307 @@
1
+ import "./chunk-QGM4M3NI.js";
2
+
3
+ // ../src/engine/cost-optimization.ts
4
+ import OpenAI from "openai";
5
+ var MODELS = {
6
+ haiku: {
7
+ model: "claude-haiku-4.5",
8
+ maxTokens: 4096,
9
+ temperature: 0,
10
+ costPerMillion: 0.25
11
+ // $0.25 per million input tokens
12
+ },
13
+ sonnet: {
14
+ model: "claude-sonnet-4.5",
15
+ maxTokens: 8192,
16
+ temperature: 0,
17
+ costPerMillion: 3
18
+ // $3.00 per million input tokens
19
+ },
20
+ opus: {
21
+ model: "claude-opus-4.5",
22
+ maxTokens: 16384,
23
+ temperature: 0,
24
+ costPerMillion: 15
25
+ // $15.00 per million input tokens
26
+ }
27
+ };
28
+ var TASK_MODEL_MAP = {
29
+ temporal_parsing: "haiku",
30
+ // Fast, simple parsing
31
+ simple_classification: "haiku",
32
+ // Fast classification
33
+ memory_extraction: "sonnet",
34
+ // Needs accuracy for disambiguation
35
+ relation_detection: "sonnet",
36
+ // Needs reasoning
37
+ consolidation: "sonnet",
38
+ // Needs to merge intelligently
39
+ summarization: "haiku",
40
+ // Fast summarization
41
+ complex_reasoning: "opus"
42
+ // Deep reasoning tasks
43
+ };
44
+ function getOptimalModel(taskType, options = {}) {
45
+ if (options.forceModel) {
46
+ return MODELS[options.forceModel];
47
+ }
48
+ let tier = TASK_MODEL_MAP[taskType];
49
+ if (options.minQuality && tier === "haiku") {
50
+ tier = "sonnet";
51
+ }
52
+ return MODELS[tier];
53
+ }
54
+ function estimateCost(params) {
55
+ const modelConfig = getOptimalModel(params.taskType, { forceModel: params.model });
56
+ const inputCost = params.inputTokens / 1e6 * modelConfig.costPerMillion;
57
+ const outputCostPerMillion = modelConfig.costPerMillion * 5;
58
+ const outputCost = params.outputTokens / 1e6 * outputCostPerMillion;
59
+ return {
60
+ model: modelConfig.model,
61
+ inputCost,
62
+ outputCost,
63
+ totalCost: inputCost + outputCost
64
+ };
65
+ }
66
+ async function smartLLMCall(params) {
67
+ const { taskType, prompt, systemPrompt, maxTokens, temperature, forceModel } = params;
68
+ const modelConfig = getOptimalModel(taskType, { forceModel });
69
+ const openai = new OpenAI({
70
+ apiKey: process.env.OPENAI_API_KEY || ""
71
+ });
72
+ const messages = [{ role: "user", content: prompt }];
73
+ if (systemPrompt) {
74
+ messages.unshift({ role: "system", content: systemPrompt });
75
+ }
76
+ const modelMap = {
77
+ "claude-haiku-4-5-20251001": "gpt-4o-mini",
78
+ "claude-sonnet-4-5-20250929": "gpt-4o",
79
+ "claude-opus-4-5-20251101": "gpt-4o"
80
+ };
81
+ const openaiModel = modelMap[modelConfig.model] || "gpt-4o";
82
+ const response = await openai.chat.completions.create({
83
+ model: openaiModel,
84
+ max_tokens: maxTokens || modelConfig.maxTokens,
85
+ temperature: temperature !== void 0 ? temperature : modelConfig.temperature,
86
+ messages
87
+ });
88
+ const responseText = response.choices[0]?.message?.content || "";
89
+ const tokensUsed = {
90
+ input: response.usage?.prompt_tokens || 0,
91
+ output: response.usage?.completion_tokens || 0
92
+ };
93
+ const cost = estimateCost({
94
+ taskType,
95
+ inputTokens: tokensUsed.input,
96
+ outputTokens: tokensUsed.output,
97
+ model: forceModel
98
+ });
99
+ return {
100
+ response: responseText,
101
+ model: modelConfig.model,
102
+ tokensUsed,
103
+ cost: cost.totalCost
104
+ };
105
+ }
106
+ async function batchOptimize(params) {
107
+ const { items, processFn, batchSize = 10, delayMs = 100 } = params;
108
+ const results = [];
109
+ for (let i = 0; i < items.length; i += batchSize) {
110
+ const batch = items.slice(i, i + batchSize);
111
+ const batchResults = await Promise.all(batch.map(processFn));
112
+ results.push(...batchResults);
113
+ if (i + batchSize < items.length) {
114
+ await new Promise((resolve) => setTimeout(resolve, delayMs));
115
+ }
116
+ }
117
+ return results;
118
+ }
119
+ var costRecords = [];
120
+ function trackCost(record) {
121
+ costRecords.push({
122
+ ...record,
123
+ timestamp: /* @__PURE__ */ new Date()
124
+ });
125
+ }
126
+ async function getCostSummary(params) {
127
+ const { startDate, endDate } = params;
128
+ let filtered = [...costRecords];
129
+ if (startDate) {
130
+ filtered = filtered.filter((r) => r.timestamp >= startDate);
131
+ }
132
+ if (endDate) {
133
+ filtered = filtered.filter((r) => r.timestamp <= endDate);
134
+ }
135
+ const period = {
136
+ start: filtered.length > 0 ? filtered[0].timestamp : /* @__PURE__ */ new Date(),
137
+ end: filtered.length > 0 ? filtered[filtered.length - 1].timestamp : /* @__PURE__ */ new Date()
138
+ };
139
+ const totalCost = filtered.reduce((sum, r) => sum + r.cost, 0);
140
+ const totalRequests = filtered.length;
141
+ const costByModel = {};
142
+ const costByTask = {};
143
+ for (const record of filtered) {
144
+ costByModel[record.model] = (costByModel[record.model] || 0) + record.cost;
145
+ costByTask[record.taskType] = (costByTask[record.taskType] || 0) + record.cost;
146
+ }
147
+ const avgCostPerRequest = totalRequests > 0 ? totalCost / totalRequests : 0;
148
+ const daysDiff = period.end.getTime() - period.start.getTime();
149
+ const days = daysDiff > 0 ? daysDiff / (1e3 * 60 * 60 * 24) : 1;
150
+ const estimatedMonthlyCost = totalCost / days * 30;
151
+ return {
152
+ period,
153
+ totalCost,
154
+ totalRequests,
155
+ costByModel,
156
+ costByTask,
157
+ avgCostPerRequest,
158
+ estimatedMonthlyCost
159
+ };
160
+ }
161
+ function calculateSavings(params) {
162
+ const { since } = params;
163
+ const filtered = since ? costRecords.filter((r) => r.timestamp >= since) : costRecords;
164
+ const actualCost = filtered.reduce((sum, r) => sum + r.cost, 0);
165
+ const opusCost = filtered.reduce((sum, r) => {
166
+ const cost = estimateCost({
167
+ taskType: r.taskType,
168
+ inputTokens: r.inputTokens,
169
+ outputTokens: r.outputTokens,
170
+ model: "opus"
171
+ });
172
+ return sum + cost.totalCost;
173
+ }, 0);
174
+ const savings = opusCost - actualCost;
175
+ const savingsPercent = opusCost > 0 ? savings / opusCost * 100 : 0;
176
+ return {
177
+ actualCost,
178
+ opusCost,
179
+ savings,
180
+ savingsPercent
181
+ };
182
+ }
183
+ function recommendModelUpgrades(params) {
184
+ const { errorRates, threshold = 0.05 } = params;
185
+ const recommendations = [];
186
+ for (const [taskType, errorRate] of Object.entries(errorRates)) {
187
+ if (errorRate > threshold) {
188
+ const currentModel = TASK_MODEL_MAP[taskType];
189
+ let recommendedModel;
190
+ if (currentModel === "haiku") {
191
+ recommendedModel = "sonnet";
192
+ } else if (currentModel === "sonnet") {
193
+ recommendedModel = "opus";
194
+ } else {
195
+ continue;
196
+ }
197
+ recommendations.push({
198
+ taskType,
199
+ currentModel,
200
+ recommendedModel
201
+ });
202
+ }
203
+ }
204
+ return recommendations;
205
+ }
206
+ async function getCostBreakdown(params) {
207
+ const { groupBy, startDate, endDate } = params;
208
+ let filtered = [...costRecords];
209
+ if (startDate) {
210
+ filtered = filtered.filter((r) => r.timestamp >= startDate);
211
+ }
212
+ if (endDate) {
213
+ filtered = filtered.filter((r) => r.timestamp <= endDate);
214
+ }
215
+ const groups = {};
216
+ for (const record of filtered) {
217
+ let key;
218
+ switch (groupBy) {
219
+ case "model":
220
+ key = record.model;
221
+ break;
222
+ case "task":
223
+ key = record.taskType;
224
+ break;
225
+ case "day":
226
+ key = record.timestamp.toISOString().split("T")[0];
227
+ break;
228
+ case "hour":
229
+ key = record.timestamp.toISOString().slice(0, 13) + ":00";
230
+ break;
231
+ default:
232
+ key = record.taskType;
233
+ }
234
+ if (!groups[key]) {
235
+ groups[key] = { cost: 0, requests: 0 };
236
+ }
237
+ groups[key].cost += record.cost;
238
+ groups[key].requests += 1;
239
+ }
240
+ const totalCost = filtered.reduce((sum, r) => sum + r.cost, 0);
241
+ const totalRequests = filtered.length;
242
+ return { groups, totalCost, totalRequests };
243
+ }
244
+ async function getSavingsReport(params) {
245
+ const { startDate, endDate } = params;
246
+ let filtered = [...costRecords];
247
+ if (startDate) {
248
+ filtered = filtered.filter((r) => r.timestamp >= startDate);
249
+ }
250
+ if (endDate) {
251
+ filtered = filtered.filter((r) => r.timestamp <= endDate);
252
+ }
253
+ const period = {
254
+ start: filtered.length > 0 ? filtered[0].timestamp : /* @__PURE__ */ new Date(),
255
+ end: filtered.length > 0 ? filtered[filtered.length - 1].timestamp : /* @__PURE__ */ new Date()
256
+ };
257
+ const actualCost = filtered.reduce((sum, r) => sum + r.cost, 0);
258
+ let opusOnlyCost = 0;
259
+ const requests = { total: filtered.length, haiku: 0, sonnet: 0, opus: 0 };
260
+ for (const record of filtered) {
261
+ opusOnlyCost += estimateCost({
262
+ taskType: record.taskType,
263
+ inputTokens: record.inputTokens,
264
+ outputTokens: record.outputTokens,
265
+ model: "opus"
266
+ }).totalCost;
267
+ if (record.model.includes("haiku")) {
268
+ requests.haiku++;
269
+ } else if (record.model.includes("sonnet")) {
270
+ requests.sonnet++;
271
+ } else if (record.model.includes("opus")) {
272
+ requests.opus++;
273
+ }
274
+ }
275
+ const savings = opusOnlyCost - actualCost;
276
+ const savingsPercentage = opusOnlyCost > 0 ? savings / opusOnlyCost * 100 : 0;
277
+ let recommendation = "";
278
+ if (savingsPercentage > 50) {
279
+ recommendation = "Excellent! Your model selection is highly optimized.";
280
+ } else if (savingsPercentage > 30) {
281
+ recommendation = "Good savings. Consider using Haiku for simpler tasks.";
282
+ } else {
283
+ recommendation = "Consider reviewing task complexity to better match models.";
284
+ }
285
+ return {
286
+ period,
287
+ actualCost,
288
+ opusOnlyCost,
289
+ savings,
290
+ savingsPercentage,
291
+ requests,
292
+ recommendation
293
+ };
294
+ }
295
+ export {
296
+ MODELS,
297
+ batchOptimize,
298
+ calculateSavings,
299
+ estimateCost,
300
+ getCostBreakdown,
301
+ getCostSummary,
302
+ getOptimalModel,
303
+ getSavingsReport,
304
+ recommendModelUpgrades,
305
+ smartLLMCall,
306
+ trackCost
307
+ };
@@ -0,0 +1,15 @@
1
+ import {
2
+ ingestChunk,
3
+ ingestChunksBatch,
4
+ ingestSession,
5
+ updateMemory
6
+ } from "./chunk-EI5CE3EY.js";
7
+ import "./chunk-5KBZQHDL.js";
8
+ import "./chunk-3WGYBAYR.js";
9
+ import "./chunk-QGM4M3NI.js";
10
+ export {
11
+ ingestChunk,
12
+ ingestChunksBatch,
13
+ ingestSession,
14
+ updateMemory
15
+ };
@@ -0,0 +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
+ };
@@ -0,0 +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
+ };
@@ -0,0 +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
+ };