@inference-gateway/sdk 0.7.2 → 0.7.3

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/CHANGELOG.md CHANGED
@@ -2,6 +2,16 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file.
4
4
 
5
+ ## [0.7.3](https://github.com/inference-gateway/typescript-sdk/compare/v0.7.2...v0.7.3) (2025-06-01)
6
+
7
+ ### ♻️ Improvements
8
+
9
+ * Enhance stream processing with abort signal support and increase default timeout ([#18](https://github.com/inference-gateway/typescript-sdk/issues/18)) ([3778138](https://github.com/inference-gateway/typescript-sdk/commit/377813851b6635ca7aafe2a5c9888b720736c9f5))
10
+
11
+ ### 🔧 Miscellaneous
12
+
13
+ * Update MCP example README and remove unused example file ([99b34e7](https://github.com/inference-gateway/typescript-sdk/commit/99b34e70edf0c8aada1d0e0d0874481ea8381a79))
14
+
5
15
  ## [0.7.2](https://github.com/inference-gateway/typescript-sdk/compare/v0.7.1...v0.7.2) (2025-05-30)
6
16
 
7
17
  ### 📚 Documentation
@@ -54,8 +54,9 @@ export declare class InferenceGatewayClient {
54
54
  * @param request - Chat completion request (must include at least model and messages)
55
55
  * @param callbacks - Callbacks for handling streaming events
56
56
  * @param provider - Optional provider to use for this request
57
+ * @param abortSignal - Optional AbortSignal to cancel the request
57
58
  */
58
- streamChatCompletion(request: Omit<SchemaCreateChatCompletionRequest, 'stream' | 'stream_options'>, callbacks: ChatCompletionStreamCallbacks, provider?: Provider): Promise<void>;
59
+ streamChatCompletion(request: Omit<SchemaCreateChatCompletionRequest, 'stream' | 'stream_options'>, callbacks: ChatCompletionStreamCallbacks, provider?: Provider, abortSignal?: AbortSignal): Promise<void>;
59
60
  /**
60
61
  * Initiates a streaming request to the chat completions endpoint
61
62
  */
@@ -13,12 +13,15 @@ class StreamProcessor {
13
13
  this.callbacks = callbacks;
14
14
  this.clientProvidedTools = clientProvidedTools;
15
15
  }
16
- async processStream(body) {
16
+ async processStream(body, abortSignal) {
17
17
  const reader = body.getReader();
18
18
  const decoder = new TextDecoder();
19
19
  let buffer = '';
20
20
  try {
21
21
  while (true) {
22
+ if (abortSignal?.aborted) {
23
+ throw new Error('Stream processing was aborted');
24
+ }
22
25
  const { done, value } = await reader.read();
23
26
  if (done)
24
27
  break;
@@ -34,6 +37,10 @@ class StreamProcessor {
34
37
  }
35
38
  }
36
39
  catch (error) {
40
+ if (abortSignal?.aborted || error.name === 'AbortError') {
41
+ console.log('Stream processing was cancelled');
42
+ return;
43
+ }
37
44
  const apiError = {
38
45
  error: error.message || 'Unknown error',
39
46
  };
@@ -150,10 +157,10 @@ class StreamProcessor {
150
157
  }
151
158
  }
152
159
  finalizeIncompleteToolCalls() {
153
- for (const [, toolCall] of this.incompleteToolCalls.entries()) {
160
+ this.incompleteToolCalls.forEach((toolCall) => {
154
161
  if (!toolCall.id || !toolCall.function.name) {
155
162
  globalThis.console.warn('Incomplete tool call detected:', toolCall);
156
- continue;
163
+ return;
157
164
  }
158
165
  const completedToolCall = {
159
166
  id: toolCall.id,
@@ -171,13 +178,20 @@ class StreamProcessor {
171
178
  this.callbacks.onMCPTool?.(completedToolCall);
172
179
  }
173
180
  catch (argError) {
174
- globalThis.console.warn(`Invalid MCP tool arguments for ${toolCall.function.name}:`, argError);
181
+ const isIncompleteJSON = toolCall.function.arguments &&
182
+ !toolCall.function.arguments.trim().endsWith('}');
183
+ if (isIncompleteJSON) {
184
+ globalThis.console.warn(`Incomplete MCP tool arguments for ${toolCall.function.name} (stream was likely interrupted):`, toolCall.function.arguments);
185
+ }
186
+ else {
187
+ globalThis.console.warn(`Invalid MCP tool arguments for ${toolCall.function.name}:`, argError);
188
+ }
175
189
  }
176
190
  }
177
191
  else {
178
192
  this.callbacks.onTool?.(completedToolCall);
179
193
  }
180
- }
194
+ });
181
195
  this.incompleteToolCalls.clear();
182
196
  }
183
197
  isMCPTool(toolName) {
@@ -199,7 +213,7 @@ class InferenceGatewayClient {
199
213
  this.apiKey = options.apiKey;
200
214
  this.defaultHeaders = options.defaultHeaders || {};
201
215
  this.defaultQuery = options.defaultQuery || {};
202
- this.timeout = options.timeout || 30000;
216
+ this.timeout = options.timeout || 60000; // Increased default timeout to 60 seconds
203
217
  this.fetchFn = options.fetch || globalThis.fetch;
204
218
  }
205
219
  /**
@@ -291,10 +305,11 @@ class InferenceGatewayClient {
291
305
  * @param request - Chat completion request (must include at least model and messages)
292
306
  * @param callbacks - Callbacks for handling streaming events
293
307
  * @param provider - Optional provider to use for this request
308
+ * @param abortSignal - Optional AbortSignal to cancel the request
294
309
  */
295
- async streamChatCompletion(request, callbacks, provider) {
310
+ async streamChatCompletion(request, callbacks, provider, abortSignal) {
296
311
  try {
297
- const response = await this.initiateStreamingRequest(request, provider);
312
+ const response = await this.initiateStreamingRequest(request, provider, abortSignal);
298
313
  if (!response.body) {
299
314
  const error = {
300
315
  error: 'Response body is not readable',
@@ -313,7 +328,7 @@ class InferenceGatewayClient {
313
328
  }
314
329
  }
315
330
  const streamProcessor = new StreamProcessor(callbacks, clientProvidedTools);
316
- await streamProcessor.processStream(response.body);
331
+ await streamProcessor.processStream(response.body, abortSignal);
317
332
  }
318
333
  catch (error) {
319
334
  const apiError = {
@@ -326,7 +341,7 @@ class InferenceGatewayClient {
326
341
  /**
327
342
  * Initiates a streaming request to the chat completions endpoint
328
343
  */
329
- async initiateStreamingRequest(request, provider) {
344
+ async initiateStreamingRequest(request, provider, abortSignal) {
330
345
  const query = {};
331
346
  if (provider) {
332
347
  query.provider = provider;
@@ -345,6 +360,9 @@ class InferenceGatewayClient {
345
360
  headers.set('Authorization', `Bearer ${this.apiKey}`);
346
361
  }
347
362
  const controller = new AbortController();
363
+ const combinedSignal = abortSignal
364
+ ? AbortSignal.any([abortSignal, controller.signal])
365
+ : controller.signal;
348
366
  const timeoutId = globalThis.setTimeout(() => controller.abort(), this.timeout);
349
367
  try {
350
368
  const response = await this.fetchFn(url, {
@@ -357,7 +375,7 @@ class InferenceGatewayClient {
357
375
  include_usage: true,
358
376
  },
359
377
  }),
360
- signal: controller.signal,
378
+ signal: combinedSignal,
361
379
  });
362
380
  if (!response.ok) {
363
381
  let errorMessage = `HTTP error! status: ${response.status}`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inference-gateway/sdk",
3
- "version": "0.7.2",
3
+ "version": "0.7.3",
4
4
  "description": "An SDK written in Typescript for the [Inference Gateway](https://github.com/inference-gateway/inference-gateway).",
5
5
  "main": "dist/src/index.js",
6
6
  "types": "dist/src/index.d.ts",