mcp-meilisearch 1.4.13 → 1.4.14
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ai-tools.d.ts","sourceRoot":"","sources":["../../../src/tools/core/ai-tools.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,SAAS,EAAE,MAAM,yCAAyC,CAAC;
|
|
1
|
+
{"version":3,"file":"ai-tools.d.ts","sourceRoot":"","sources":["../../../src/tools/core/ai-tools.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,SAAS,EAAE,MAAM,yCAAyC,CAAC;AAoCpE;;;GAGG;AACH,eAAO,MAAM,eAAe,GAAI,QAAQ,SAAS,SAqEhD,CAAC;AAEF,eAAe,eAAe,CAAC"}
|
|
@@ -17,37 +17,6 @@ const setAvailableTools = (aiService, server) => {
|
|
|
17
17
|
});
|
|
18
18
|
aiService.setAvailableTools(availableTools);
|
|
19
19
|
};
|
|
20
|
-
const splitTextIntoChunks = (text, chunkSize) => {
|
|
21
|
-
if (text.length <= chunkSize) {
|
|
22
|
-
return [text];
|
|
23
|
-
}
|
|
24
|
-
let currentIndex = 0;
|
|
25
|
-
const chunks = [];
|
|
26
|
-
while (currentIndex < text.length) {
|
|
27
|
-
let endIndex = Math.min(currentIndex + chunkSize, text.length);
|
|
28
|
-
if (endIndex < text.length) {
|
|
29
|
-
const sentenceEndMatch = text
|
|
30
|
-
.substring(currentIndex, endIndex)
|
|
31
|
-
.match(/[.!?]\s+/g);
|
|
32
|
-
if (sentenceEndMatch?.length) {
|
|
33
|
-
const lastMatch = sentenceEndMatch[sentenceEndMatch.length - 1];
|
|
34
|
-
const lastMatchIndex = text.lastIndexOf(lastMatch, currentIndex + chunkSize);
|
|
35
|
-
if (lastMatchIndex > currentIndex) {
|
|
36
|
-
endIndex = lastMatchIndex + lastMatch.length;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
else {
|
|
40
|
-
const lastSpace = text.lastIndexOf(" ", endIndex);
|
|
41
|
-
if (lastSpace > currentIndex) {
|
|
42
|
-
endIndex = lastSpace + 1;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
chunks.push(text.substring(currentIndex, endIndex));
|
|
47
|
-
currentIndex = endIndex;
|
|
48
|
-
}
|
|
49
|
-
return chunks;
|
|
50
|
-
};
|
|
51
20
|
/**
|
|
52
21
|
* Register AI tools with the MCP server
|
|
53
22
|
* @param server - The MCP server instance
|
|
@@ -85,66 +54,19 @@ export const registerAITools = (server) => {
|
|
|
85
54
|
return createErrorResponse(error);
|
|
86
55
|
}
|
|
87
56
|
});
|
|
88
|
-
server.tool("process-ai-text", "Process a summary text using AI to describe the data result from a tool", {
|
|
89
|
-
query: z.string().describe("The natural language query to process"),
|
|
90
|
-
chunkSize: z
|
|
91
|
-
.number()
|
|
92
|
-
.positive()
|
|
93
|
-
.default(50000)
|
|
94
|
-
.describe("Optional size of chunks to split the query into (characters)"),
|
|
95
|
-
}, { category: "core" }, async ({ query, chunkSize }) => {
|
|
57
|
+
server.tool("process-ai-text", "Process a summary text using AI to describe the data result from a tool", { query: z.string().describe("The natural language query to process") }, { category: "core" }, async ({ query }) => {
|
|
96
58
|
try {
|
|
97
59
|
const aiService = AIService.getInstance();
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
return response.error
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
type: "text",
|
|
109
|
-
text: JSON.stringify(response.summary, null, 2),
|
|
110
|
-
},
|
|
111
|
-
],
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
const chunks = splitTextIntoChunks(query, chunkSize);
|
|
115
|
-
const chunkPromises = chunks.map((chunk) => aiService.setupAIProcess(chunk, { processType: "text" }));
|
|
116
|
-
const chunkResponses = await Promise.all(chunkPromises);
|
|
117
|
-
const errorResponse = chunkResponses.find((response) => response.error);
|
|
118
|
-
if (errorResponse) {
|
|
119
|
-
return createErrorResponse(errorResponse.error);
|
|
120
|
-
}
|
|
121
|
-
const summaries = chunkResponses
|
|
122
|
-
.map((response) => response.summary)
|
|
123
|
-
.filter(Boolean);
|
|
124
|
-
if (!summaries.length) {
|
|
125
|
-
return createErrorResponse("Failed to process query chunks");
|
|
126
|
-
}
|
|
127
|
-
if (summaries.length === 1) {
|
|
128
|
-
return {
|
|
129
|
-
isError: false,
|
|
130
|
-
content: [
|
|
131
|
-
{ type: "text", text: JSON.stringify(summaries[0], null, 2) },
|
|
132
|
-
],
|
|
133
|
-
};
|
|
134
|
-
}
|
|
135
|
-
const combinedText = summaries.join(" ");
|
|
136
|
-
const finalResponse = await aiService.setupAIProcess(`Synthesize the following text into a coherent summary: ${combinedText}`, { processType: "text" });
|
|
137
|
-
return finalResponse.error
|
|
138
|
-
? createErrorResponse(finalResponse.error)
|
|
139
|
-
: {
|
|
140
|
-
isError: false,
|
|
141
|
-
content: [
|
|
142
|
-
{
|
|
143
|
-
type: "text",
|
|
144
|
-
text: JSON.stringify(finalResponse.summary || combinedText, null, 2),
|
|
145
|
-
},
|
|
146
|
-
],
|
|
147
|
-
};
|
|
60
|
+
const response = await aiService.setupAIProcess(query, {
|
|
61
|
+
processType: "text",
|
|
62
|
+
});
|
|
63
|
+
if (response.error)
|
|
64
|
+
return createErrorResponse(response.error);
|
|
65
|
+
const { summary: result } = response;
|
|
66
|
+
return {
|
|
67
|
+
isError: false,
|
|
68
|
+
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
|
|
69
|
+
};
|
|
148
70
|
}
|
|
149
71
|
catch (error) {
|
|
150
72
|
return createErrorResponse(error);
|
|
@@ -70,8 +70,9 @@ export declare class AIService {
|
|
|
70
70
|
setupAIProcess(query: string, options: AIProcessSetupOptions): Promise<AIProcessResponse>;
|
|
71
71
|
private processOpenAITool;
|
|
72
72
|
private processOpenAIText;
|
|
73
|
-
private processHuggingFaceText;
|
|
74
73
|
private processHuggingFaceTool;
|
|
74
|
+
private processHuggingFaceText;
|
|
75
|
+
private splitTextIntoChunks;
|
|
75
76
|
}
|
|
76
77
|
export {};
|
|
77
78
|
//# sourceMappingURL=ai-handler.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ai-handler.d.ts","sourceRoot":"","sources":["../../src/utils/ai-handler.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,qBAAqB,EAAE,MAAM,qBAAqB,CAAC;AAI5D,UAAU,MAAM;IACd,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACrC;AAED,UAAU,qBAAqB;IAC7B,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;IACzB,WAAW,EAAE,MAAM,GAAG,MAAM,CAAC;CAC9B;AAkBD,UAAU,cAAc;IACtB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACtC;AAED,UAAU,cAAc;IACtB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,UAAU,iBAAkB,SAAQ,cAAc,EAAE,cAAc;IAChE,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB;AAED;;;;;GAKG;AACH,qBAAa,SAAS;IACpB,OAAO,CAAC,cAAc,CAAgB;IACtC,OAAO,CAAC,KAAK,CAA2B;IACxC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAA0B;IACjD,OAAO,CAAC,MAAM,CAAC,iBAAiB,CAAkB;IAClD,OAAO,CAAC,QAAQ,CAAmC;IACnD,OAAO,CAAC,MAAM,CAAgD;IAE9D;;;OAGG;IACH,OAAO;IAEP;;;OAGG;WACW,WAAW,IAAI,SAAS;IAOtC;;;;;;OAMG;IACH,UAAU,CACR,MAAM,EAAE,MAAM,EACd,QAAQ,G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O,CAAC,MAAM,CAAgD;IAE9D;;;OAGG;IACH,OAAO;IAEP;;;OAGG;WACW,WAAW,IAAI,SAAS;IAOtC;;;;;;OAMG;IACH,UAAU,CACR,MAAM,EAAE,MAAM,EACd,QAAQ,GAAE,qBAAgC,EAC1C,KAAK,CAAC,EAAE,MAAM,GACb,IAAI;IA4BP;;;OAGG;IACH,iBAAiB,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,IAAI;IAIxC,iBAAiB,IAAI,OAAO;IAI5B;;;;OAIG;IACH,OAAO,CAAC,kBAAkB;IAe1B;;;;OAIG;IACH,OAAO,CAAC,gBAAgB;IAalB,cAAc,CAClB,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,qBAAqB,GAC7B,OAAO,CAAC,iBAAiB,CAAC;YA6Df,iBAAiB;YA+DjB,iBAAiB;YA+BjB,sBAAsB;YA+DtB,sBAAsB;IAiCpC,OAAO,CAAC,mBAAmB;CAyD5B"}
|
package/dist/utils/ai-handler.js
CHANGED
|
@@ -127,9 +127,21 @@ export class AIService {
|
|
|
127
127
|
{ role: "user", content: query },
|
|
128
128
|
];
|
|
129
129
|
if (processType === "text") {
|
|
130
|
-
|
|
131
|
-
?
|
|
132
|
-
:
|
|
130
|
+
const processTextMethod = this.provider === "huggingface"
|
|
131
|
+
? this.processHuggingFaceText.bind(this)
|
|
132
|
+
: this.processOpenAIText.bind(this);
|
|
133
|
+
const chunks = this.splitTextIntoChunks(query, 50000);
|
|
134
|
+
if (chunks.length === 1) {
|
|
135
|
+
return processTextMethod(messages);
|
|
136
|
+
}
|
|
137
|
+
const chunkPromises = chunks.map((content) => processTextMethod([messages[0], { role: "user", content }]));
|
|
138
|
+
const results = await Promise.all(chunkPromises);
|
|
139
|
+
const error = results.find((result) => result.error);
|
|
140
|
+
if (error) {
|
|
141
|
+
return { error };
|
|
142
|
+
}
|
|
143
|
+
const summary = results.map((result) => result?.summary || "").join(" ");
|
|
144
|
+
return { summary };
|
|
133
145
|
}
|
|
134
146
|
else {
|
|
135
147
|
return this.provider === "huggingface"
|
|
@@ -208,31 +220,6 @@ export class AIService {
|
|
|
208
220
|
return { error };
|
|
209
221
|
}
|
|
210
222
|
}
|
|
211
|
-
async processHuggingFaceText(messages) {
|
|
212
|
-
try {
|
|
213
|
-
const client = this.client;
|
|
214
|
-
const response = await client.chatCompletion({
|
|
215
|
-
messages,
|
|
216
|
-
model: this.model,
|
|
217
|
-
});
|
|
218
|
-
if (!response.choices?.length) {
|
|
219
|
-
return {
|
|
220
|
-
error: "No response returned from OpenAI; processType: 'text'.",
|
|
221
|
-
};
|
|
222
|
-
}
|
|
223
|
-
const message = response.choices[0].message;
|
|
224
|
-
if (message.content) {
|
|
225
|
-
return { summary: message.content };
|
|
226
|
-
}
|
|
227
|
-
return {
|
|
228
|
-
error: "No content in Hugging Face response; processType: 'text'.",
|
|
229
|
-
};
|
|
230
|
-
}
|
|
231
|
-
catch (error) {
|
|
232
|
-
console.error(error);
|
|
233
|
-
return { error };
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
223
|
async processHuggingFaceTool(tools, messages) {
|
|
237
224
|
try {
|
|
238
225
|
const client = this.client;
|
|
@@ -280,4 +267,73 @@ export class AIService {
|
|
|
280
267
|
return { error };
|
|
281
268
|
}
|
|
282
269
|
}
|
|
270
|
+
async processHuggingFaceText(messages) {
|
|
271
|
+
try {
|
|
272
|
+
const client = this.client;
|
|
273
|
+
const response = await client.chatCompletion({
|
|
274
|
+
messages,
|
|
275
|
+
model: this.model,
|
|
276
|
+
});
|
|
277
|
+
if (!response.choices?.length) {
|
|
278
|
+
return {
|
|
279
|
+
error: "No response returned from OpenAI; processType: 'text'.",
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
const message = response.choices[0].message;
|
|
283
|
+
if (message.content) {
|
|
284
|
+
return { summary: message.content };
|
|
285
|
+
}
|
|
286
|
+
return {
|
|
287
|
+
error: "No content in Hugging Face response; processType: 'text'.",
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
catch (error) {
|
|
291
|
+
console.error(error);
|
|
292
|
+
return { error };
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
splitTextIntoChunks(text, chunkSize) {
|
|
296
|
+
if (text.length <= chunkSize) {
|
|
297
|
+
return [text];
|
|
298
|
+
}
|
|
299
|
+
let currentIndex = 0;
|
|
300
|
+
const chunks = [];
|
|
301
|
+
const textLength = text.length;
|
|
302
|
+
const sentenceEndRegex = /[.!?]\s+/g;
|
|
303
|
+
while (currentIndex < textLength) {
|
|
304
|
+
let proposedEndIndex = Math.min(currentIndex + chunkSize, textLength);
|
|
305
|
+
let actualEndIndex = proposedEndIndex;
|
|
306
|
+
if (proposedEndIndex < textLength) {
|
|
307
|
+
let bestBreakPoint = -1;
|
|
308
|
+
const relevantTextSlice = text.substring(currentIndex, proposedEndIndex);
|
|
309
|
+
let match;
|
|
310
|
+
let lastMatch;
|
|
311
|
+
sentenceEndRegex.lastIndex = 0;
|
|
312
|
+
while ((match = sentenceEndRegex.exec(relevantTextSlice)) !== null) {
|
|
313
|
+
lastMatch = match;
|
|
314
|
+
}
|
|
315
|
+
if (lastMatch) {
|
|
316
|
+
bestBreakPoint = currentIndex + lastMatch.index + lastMatch[0].length;
|
|
317
|
+
}
|
|
318
|
+
if (bestBreakPoint > currentIndex) {
|
|
319
|
+
actualEndIndex = bestBreakPoint;
|
|
320
|
+
}
|
|
321
|
+
else {
|
|
322
|
+
const lastSpaceIndex = text.lastIndexOf(" ", proposedEndIndex - 1);
|
|
323
|
+
if (lastSpaceIndex > currentIndex) {
|
|
324
|
+
actualEndIndex = lastSpaceIndex + 1;
|
|
325
|
+
}
|
|
326
|
+
else {
|
|
327
|
+
actualEndIndex = proposedEndIndex;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
else {
|
|
332
|
+
actualEndIndex = textLength;
|
|
333
|
+
}
|
|
334
|
+
chunks.push(text.substring(currentIndex, actualEndIndex));
|
|
335
|
+
currentIndex = actualEndIndex;
|
|
336
|
+
}
|
|
337
|
+
return chunks;
|
|
338
|
+
}
|
|
283
339
|
}
|