mcp-meilisearch 1.4.13 → 1.4.15
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 +33 -13
- package/dist/client.d.ts +19 -15
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +50 -41
- package/dist/tools/core/ai-tools.d.ts.map +1 -1
- package/dist/tools/core/ai-tools.js +11 -89
- package/dist/utils/ai-handler.d.ts +2 -1
- package/dist/utils/ai-handler.d.ts.map +1 -1
- package/dist/utils/ai-handler.js +84 -28
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -97,21 +97,42 @@ await client.connectToServer("http://localhost:4995/mcp");
|
|
|
97
97
|
const result = await client.callTool("global-search", {
|
|
98
98
|
q: "search kiosco antonio",
|
|
99
99
|
});
|
|
100
|
+
```
|
|
100
101
|
|
|
101
|
-
|
|
102
|
+
#### Client Methods
|
|
102
103
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
104
|
+
##### `callTool(name, args)`
|
|
105
|
+
|
|
106
|
+
Calls a specific tool on the MCP server with optional arguments.
|
|
107
|
+
|
|
108
|
+
**Parameters:**
|
|
109
|
+
|
|
110
|
+
- `name`: String - The name of the tool to call
|
|
111
|
+
- `args`: Object (Optional) - Arguments to pass to the tool
|
|
112
|
+
|
|
113
|
+
**Returns:**
|
|
114
|
+
|
|
115
|
+
- Promise resolving to a `ToolClientResponse` object with:
|
|
116
|
+
- `success`: Boolean - Whether the tool call was successful
|
|
117
|
+
- `data`: Any (Optional) - The data returned by the tool if successful
|
|
118
|
+
- `error`: String (Optional) - Error message if the tool call failed
|
|
119
|
+
|
|
120
|
+
##### `processSummary(query)`
|
|
121
|
+
|
|
122
|
+
Processes data using AI to generate a human-readable summary.
|
|
123
|
+
|
|
124
|
+
**Parameters:**
|
|
125
|
+
|
|
126
|
+
- `query`: Any - The data to be summarized
|
|
127
|
+
|
|
128
|
+
**Returns:**
|
|
111
129
|
|
|
112
|
-
|
|
130
|
+
- Promise resolving to an `AIToolClientResponse` object with:
|
|
131
|
+
- `success`: Boolean - Whether the summary generation was successful
|
|
132
|
+
- `data`: Any (Optional) - The generated summary if successful
|
|
133
|
+
- `error`: String (Optional) - Error message if summary generation failed
|
|
113
134
|
|
|
114
|
-
|
|
135
|
+
##### `callToolWithAI(query, options)`
|
|
115
136
|
|
|
116
137
|
Processes a user query through AI to determine and execute the most appropriate tool.
|
|
117
138
|
|
|
@@ -120,8 +141,7 @@ Processes a user query through AI to determine and execute the most appropriate
|
|
|
120
141
|
- `query`: String - The user's query or request to be processed
|
|
121
142
|
- `options`: Object (Optional) - Configuration options
|
|
122
143
|
- `specificTools`: String[] (Optional) - Restricts tool selection to this list of tool names
|
|
123
|
-
- `justReasoning`: Boolean (Optional) - When set to `true`, returns only the AI's reasoning without executing the
|
|
124
|
-
selected tool
|
|
144
|
+
- `justReasoning`: Boolean (Optional) - When set to `true`, returns only the AI's reasoning without executing the selected tool
|
|
125
145
|
- `provideSummary`: Boolean (Optional) - When set to `true`, generates a concise summary of the search results along with the regular response
|
|
126
146
|
|
|
127
147
|
### Starting the Server
|
package/dist/client.d.ts
CHANGED
|
@@ -1,16 +1,18 @@
|
|
|
1
|
-
interface
|
|
2
|
-
specificTools?: string[];
|
|
3
|
-
justReasoning?: boolean;
|
|
4
|
-
provideSummary?: boolean;
|
|
5
|
-
}
|
|
6
|
-
interface AIToolClientResponse {
|
|
1
|
+
interface ToolClientResponse {
|
|
7
2
|
data?: any;
|
|
8
|
-
summary?: any;
|
|
9
3
|
error?: string;
|
|
10
4
|
success: boolean;
|
|
5
|
+
}
|
|
6
|
+
interface AIToolClientResponse extends ToolClientResponse {
|
|
7
|
+
summary?: any;
|
|
11
8
|
toolUsed?: string;
|
|
12
9
|
reasoning?: string;
|
|
13
10
|
}
|
|
11
|
+
interface AIToolClientOptions {
|
|
12
|
+
specificTools?: string[];
|
|
13
|
+
justReasoning?: boolean;
|
|
14
|
+
provideSummary?: boolean;
|
|
15
|
+
}
|
|
14
16
|
export declare class MCPClient {
|
|
15
17
|
/**
|
|
16
18
|
* Indicates whether the client is connected to the MCP server
|
|
@@ -52,8 +54,6 @@ export declare class MCPClient {
|
|
|
52
54
|
* @throws Error if connection fails after 5 attempts
|
|
53
55
|
*/
|
|
54
56
|
connectToServer(serverUrl: string): Promise<void>;
|
|
55
|
-
private listTools;
|
|
56
|
-
private setUpNotifications;
|
|
57
57
|
/**
|
|
58
58
|
* Calls a tool on the MCP server with optional arguments
|
|
59
59
|
* Parses and processes the response from the server
|
|
@@ -62,11 +62,7 @@ export declare class MCPClient {
|
|
|
62
62
|
* @throws Error if the tool call fails
|
|
63
63
|
* @returns Object containing success status and either data or error message
|
|
64
64
|
*/
|
|
65
|
-
callTool(name: string, args?: Record<string, any>): Promise<
|
|
66
|
-
success: boolean;
|
|
67
|
-
data?: any;
|
|
68
|
-
error?: string;
|
|
69
|
-
}>;
|
|
65
|
+
callTool(name: string, args?: Record<string, any>): Promise<ToolClientResponse>;
|
|
70
66
|
/**
|
|
71
67
|
* Process a user query through the AI to determine which tool to use
|
|
72
68
|
* @param query The user's query
|
|
@@ -78,12 +74,20 @@ export declare class MCPClient {
|
|
|
78
74
|
* @returns The result of calling the selected tool, or an error
|
|
79
75
|
*/
|
|
80
76
|
callToolWithAI(query: string, options?: AIToolClientOptions): Promise<AIToolClientResponse>;
|
|
81
|
-
|
|
77
|
+
/**
|
|
78
|
+
* Process a summary text using AI
|
|
79
|
+
* @param query The natural language query to process
|
|
80
|
+
* @throws Error if AI inference fails
|
|
81
|
+
*/
|
|
82
|
+
processSummary(query: unknown): Promise<AIToolClientResponse>;
|
|
82
83
|
/**
|
|
83
84
|
* Closes the connection to the server and resets the connection state
|
|
84
85
|
* Called automatically on transport error or can be called manually
|
|
85
86
|
*/
|
|
86
87
|
cleanup(): Promise<void>;
|
|
88
|
+
private listTools;
|
|
89
|
+
private setUpNotifications;
|
|
90
|
+
private setUpTransport;
|
|
87
91
|
}
|
|
88
92
|
export {};
|
|
89
93
|
//# sourceMappingURL=client.d.ts.map
|
package/dist/client.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAQA,UAAU,
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAQA,UAAU,kBAAkB;IAC1B,IAAI,CAAC,EAAE,GAAG,CAAC;IACX,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,OAAO,CAAC;CAClB;AAED,UAAU,oBAAqB,SAAQ,kBAAkB;IACvD,OAAO,CAAC,EAAE,GAAG,CAAC;IACd,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,UAAU,mBAAmB;IAC3B,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;IACzB,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,cAAc,CAAC,EAAE,OAAO,CAAC;CAC1B;AAED,qBAAa,SAAS;IACpB;;;OAGG;IACH,WAAW,EAAE,OAAO,CAAS;IAE7B;;;OAGG;IACH,KAAK,EAAE;QACL,IAAI,EAAE,MAAM,CAAC;QACb,WAAW,EAAE,MAAM,CAAC;QACpB,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;KACjC,EAAE,CAAM;IAET,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,KAAK,CAAa;IAC1B,OAAO,CAAC,SAAS,CAA8C;IAC/D,OAAO,CAAC,oBAAoB,CAEZ;gBAEJ,UAAU,EAAE,MAAM;IAI9B;;;OAGG;IACH,sBAAsB,CACpB,QAAQ,EAAE,CAAC,KAAK,EAAE,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,WAAW,EAAE,MAAM,CAAA;KAAE,CAAC,KAAK,IAAI;IAKzE;;;;;OAKG;IACG,WAAW,CAAC,CAAC,EAAE,QAAQ,EAAE,MAAM,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;IAOhE;;;;OAIG;IACG,eAAe,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAuBvD;;;;;;;OAOG;IACG,QAAQ,CACZ,IAAI,EAAE,MAAM,EACZ,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GACzB,OAAO,CAAC,kBAAkB,CAAC;IA2C9B;;;;;;;;;OASG;IACG,cAAc,CAClB,KAAK,EAAE,MAAM,EACb,OAAO,GAAE,mBAAwB,GAChC,OAAO,CAAC,oBAAoB,CAAC;IAuDhC;;;;OAIG;IACG,cAAc,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC,oBAAoB,CAAC;IAUnE;;;OAGG;IACG,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;YAMhB,SAAS;IA0BvB,OAAO,CAAC,kBAAkB;IAW1B,OAAO,CAAC,cAAc;CAIvB"}
|
package/dist/client.js
CHANGED
|
@@ -63,38 +63,6 @@ export class MCPClient {
|
|
|
63
63
|
await this.connectToServer(serverUrl);
|
|
64
64
|
}
|
|
65
65
|
}
|
|
66
|
-
async listTools() {
|
|
67
|
-
try {
|
|
68
|
-
const toolsResult = await this.client.listTools();
|
|
69
|
-
if (!toolsResult) {
|
|
70
|
-
this.tools = [];
|
|
71
|
-
}
|
|
72
|
-
else if (toolsResult.tools && Array.isArray(toolsResult.tools)) {
|
|
73
|
-
this.tools = toolsResult.tools
|
|
74
|
-
.filter(({ annotations }) => annotations?.category !== "core")
|
|
75
|
-
.map((tool) => ({
|
|
76
|
-
name: tool.name,
|
|
77
|
-
description: tool.description ?? "",
|
|
78
|
-
parameters: tool.parameters || {},
|
|
79
|
-
}));
|
|
80
|
-
}
|
|
81
|
-
else {
|
|
82
|
-
this.tools = [];
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
catch (error) {
|
|
86
|
-
this.tools = [];
|
|
87
|
-
}
|
|
88
|
-
finally {
|
|
89
|
-
if (this.toolsUpdatedCallback) {
|
|
90
|
-
this.toolsUpdatedCallback([...this.tools]);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
setUpNotifications() {
|
|
95
|
-
this.client.setNotificationHandler(LoggingMessageNotificationSchema, console.info);
|
|
96
|
-
this.client.setNotificationHandler(ToolListChangedNotificationSchema, this.listTools);
|
|
97
|
-
}
|
|
98
66
|
/**
|
|
99
67
|
* Calls a tool on the MCP server with optional arguments
|
|
100
68
|
* Parses and processes the response from the server
|
|
@@ -183,11 +151,7 @@ export class MCPClient {
|
|
|
183
151
|
toolUsed: toolName,
|
|
184
152
|
};
|
|
185
153
|
if (provideSummary) {
|
|
186
|
-
const summary = await this.
|
|
187
|
-
query: JSON.stringify(toolResult.data),
|
|
188
|
-
});
|
|
189
|
-
if (!summary.success)
|
|
190
|
-
console.error(summary);
|
|
154
|
+
const summary = await this.processSummary(toolResult.data);
|
|
191
155
|
response["summary"] = summary.data;
|
|
192
156
|
}
|
|
193
157
|
return response;
|
|
@@ -200,10 +164,18 @@ export class MCPClient {
|
|
|
200
164
|
};
|
|
201
165
|
}
|
|
202
166
|
}
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
167
|
+
/**
|
|
168
|
+
* Process a summary text using AI
|
|
169
|
+
* @param query The natural language query to process
|
|
170
|
+
* @throws Error if AI inference fails
|
|
171
|
+
*/
|
|
172
|
+
async processSummary(query) {
|
|
173
|
+
const result = await this.callTool("process-ai-text", {
|
|
174
|
+
query: JSON.stringify(query),
|
|
175
|
+
});
|
|
176
|
+
if (!result.success)
|
|
177
|
+
console.error(result);
|
|
178
|
+
return result;
|
|
207
179
|
}
|
|
208
180
|
/**
|
|
209
181
|
* Closes the connection to the server and resets the connection state
|
|
@@ -215,4 +187,41 @@ export class MCPClient {
|
|
|
215
187
|
await this.client.close();
|
|
216
188
|
this.isConnected = false;
|
|
217
189
|
}
|
|
190
|
+
async listTools() {
|
|
191
|
+
try {
|
|
192
|
+
const toolsResult = await this.client.listTools();
|
|
193
|
+
if (!toolsResult) {
|
|
194
|
+
this.tools = [];
|
|
195
|
+
}
|
|
196
|
+
else if (toolsResult.tools && Array.isArray(toolsResult.tools)) {
|
|
197
|
+
this.tools = toolsResult.tools
|
|
198
|
+
.filter(({ annotations }) => annotations?.category !== "core")
|
|
199
|
+
.map((tool) => ({
|
|
200
|
+
name: tool.name,
|
|
201
|
+
parameters: tool.parameters || {},
|
|
202
|
+
description: tool.description ?? "",
|
|
203
|
+
}));
|
|
204
|
+
}
|
|
205
|
+
else {
|
|
206
|
+
this.tools = [];
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
catch (error) {
|
|
210
|
+
this.tools = [];
|
|
211
|
+
}
|
|
212
|
+
finally {
|
|
213
|
+
if (this.toolsUpdatedCallback) {
|
|
214
|
+
this.toolsUpdatedCallback([...this.tools]);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
setUpNotifications() {
|
|
219
|
+
this.client.setNotificationHandler(LoggingMessageNotificationSchema, console.info);
|
|
220
|
+
this.client.setNotificationHandler(ToolListChangedNotificationSchema, this.listTools);
|
|
221
|
+
}
|
|
222
|
+
setUpTransport() {
|
|
223
|
+
if (this.transport == null)
|
|
224
|
+
return;
|
|
225
|
+
this.transport.onerror = this.cleanup;
|
|
226
|
+
}
|
|
218
227
|
}
|
|
@@ -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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;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
|
}
|