@inference-gateway/sdk 0.4.1-rc.1 → 0.5.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/CHANGELOG.md CHANGED
@@ -2,11 +2,21 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file.
4
4
 
5
- ## [0.4.1-rc.1](https://github.com/inference-gateway/typescript-sdk/compare/v0.4.0...v0.4.1-rc.1) (2025-04-25)
5
+ ## [0.5.0](https://github.com/inference-gateway/typescript-sdk/compare/v0.4.1...v0.5.0) (2025-04-26)
6
+
7
+ ### ✨ Features
8
+
9
+ * Add on reasoning event callback function handling in streaming chat completions ([#7](https://github.com/inference-gateway/typescript-sdk/issues/7)) ([200eb12](https://github.com/inference-gateway/typescript-sdk/commit/200eb12c5890f46d00e5c9cdaaf40a3a306b4315))
10
+
11
+ ### 📦 Miscellaneous
12
+
13
+ * Add husky pre-commit hook ([#8](https://github.com/inference-gateway/typescript-sdk/issues/8)) ([5ad49a0](https://github.com/inference-gateway/typescript-sdk/commit/5ad49a083e7767a3b7293328c9cead0769324ff6))
14
+
15
+ ## [0.4.1](https://github.com/inference-gateway/typescript-sdk/compare/v0.4.0...v0.4.1) (2025-04-25)
6
16
 
7
17
  ### ♻️ Improvements
8
18
 
9
- * Enhance tool call handling in streaming chat completions ([f05984a](https://github.com/inference-gateway/typescript-sdk/commit/f05984a3fb8f643ea709e394569a414c7d231b7c))
19
+ * Simplify tool call handling in streaming chat completions ([#6](https://github.com/inference-gateway/typescript-sdk/issues/6)) ([f2d2a5a](https://github.com/inference-gateway/typescript-sdk/commit/f2d2a5a0743b0c1ef4930ef860949f486ee0d5fc))
10
20
 
11
21
  ## [0.4.0](https://github.com/inference-gateway/typescript-sdk/compare/v0.3.4...v0.4.0) (2025-03-31)
12
22
 
package/README.md CHANGED
@@ -155,11 +155,11 @@ const client = new InferenceGatewayClient({
155
155
  try {
156
156
  await client.streamChatCompletion(
157
157
  {
158
- model: 'gpt-4o',
158
+ model: 'openai/gpt-4o',
159
159
  messages: [
160
160
  {
161
161
  role: MessageRole.User,
162
- content: 'What's the weather in San Francisco?',
162
+ content: "What's the weather in San Francisco?",
163
163
  },
164
164
  ],
165
165
  tools: [
@@ -186,10 +186,14 @@ try {
186
186
  console.log('Tool call:', toolCall.function.name);
187
187
  console.log('Arguments:', toolCall.function.arguments);
188
188
  },
189
- onContent: (content) => process.stdout.write(content),
189
+ onReasoning: (reasoning) => {
190
+ console.log('Reasoning:', reasoning);
191
+ },
192
+ onContent: (content) => {
193
+ console.log('Content:', content);
194
+ },
190
195
  onFinish: () => console.log('\nStream completed'),
191
- },
192
- Provider.OpenAI
196
+ }
193
197
  );
194
198
  } catch (error) {
195
199
  console.error('Error:', error);
@@ -2,6 +2,7 @@ import type { Provider, SchemaChatCompletionMessageToolCall, SchemaCreateChatCom
2
2
  interface ChatCompletionStreamCallbacks {
3
3
  onOpen?: () => void;
4
4
  onChunk?: (chunk: SchemaCreateChatCompletionStreamResponse) => void;
5
+ onReasoning?: (reasoningContent: string) => void;
5
6
  onContent?: (content: string) => void;
6
7
  onTool?: (toolCall: SchemaChatCompletionMessageToolCall) => void;
7
8
  onFinish?: (response: SchemaCreateChatCompletionStreamResponse | null) => void;
@@ -162,6 +162,10 @@ class InferenceGatewayClient {
162
162
  try {
163
163
  const chunk = JSON.parse(data);
164
164
  callbacks.onChunk?.(chunk);
165
+ const reasoning_content = chunk.choices[0]?.delta?.reasoning_content;
166
+ if (reasoning_content !== undefined) {
167
+ callbacks.onReasoning?.(reasoning_content);
168
+ }
165
169
  const content = chunk.choices[0]?.delta?.content;
166
170
  if (content) {
167
171
  callbacks.onContent?.(content);
@@ -204,6 +204,58 @@ describe('InferenceGatewayClient', () => {
204
204
  }),
205
205
  }));
206
206
  });
207
+ it('should handle streaming chat completions reasoning and content', async () => {
208
+ const mockRequest = {
209
+ model: 'gpt-4o',
210
+ messages: [{ role: generated_1.MessageRole.user, content: 'Hello' }],
211
+ stream: true,
212
+ };
213
+ const mockStream = new web_1.TransformStream();
214
+ const writer = mockStream.writable.getWriter();
215
+ const encoder = new node_util_1.TextEncoder();
216
+ mockFetch.mockResolvedValueOnce({
217
+ ok: true,
218
+ body: mockStream.readable,
219
+ });
220
+ const callbacks = {
221
+ onOpen: jest.fn(),
222
+ onChunk: jest.fn(),
223
+ onReasoning: jest.fn(),
224
+ onContent: jest.fn(),
225
+ onFinish: jest.fn(),
226
+ };
227
+ const streamPromise = client.streamChatCompletion(mockRequest, callbacks);
228
+ await writer.write(encoder.encode('data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1677652288,"model":"gpt-4o","choices":[{"index":0,"delta":{"role":"assistant"},"finish_reason":null}]}\n\n' +
229
+ 'data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1677652288,"model":"gpt-4o","choices":[{"index":0,"delta":{"content":"","reasoning_content":"This"},"finish_reason":null}]}\n\n' +
230
+ 'data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1677652288,"model":"gpt-4o","choices":[{"index":0,"delta":{"content":"","reasoning_content":" is"},"finish_reason":null}]}\n\n' +
231
+ 'data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1677652288,"model":"gpt-4o","choices":[{"index":0,"delta":{"content":"","reasoning_content":" a"},"finish_reason":"stop"}]}\n\n' +
232
+ 'data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1677652288,"model":"gpt-4o","choices":[{"index":0,"delta":{"content":"","reasoning_content":" reasoning"},"finish_reason":"stop"}]}\n\n' +
233
+ 'data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1677652288,"model":"gpt-4o","choices":[{"index":0,"delta":{"content":"","reasoning_content":" content"},"finish_reason":"stop"}]}\n\n' +
234
+ 'data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1677652288,"model":"gpt-4o","choices":[{"index":0,"delta":{"content":"Hello"},"finish_reason":null}]}\n\n' +
235
+ 'data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1677652288,"model":"gpt-4o","choices":[{"index":0,"delta":{"content":"!"},"finish_reason":null}]}\n\n' +
236
+ 'data: [DONE]\n\n'));
237
+ await writer.close();
238
+ await streamPromise;
239
+ expect(callbacks.onOpen).toHaveBeenCalledTimes(1);
240
+ expect(callbacks.onChunk).toHaveBeenCalledTimes(8);
241
+ expect(callbacks.onReasoning).toHaveBeenCalledTimes(5);
242
+ expect(callbacks.onReasoning).toHaveBeenCalledWith('This');
243
+ expect(callbacks.onReasoning).toHaveBeenCalledWith(' is');
244
+ expect(callbacks.onReasoning).toHaveBeenCalledWith(' a');
245
+ expect(callbacks.onReasoning).toHaveBeenCalledWith(' reasoning');
246
+ expect(callbacks.onReasoning).toHaveBeenCalledWith(' content');
247
+ expect(callbacks.onContent).toHaveBeenCalledTimes(2);
248
+ expect(callbacks.onContent).toHaveBeenCalledWith('Hello');
249
+ expect(callbacks.onContent).toHaveBeenCalledWith('!');
250
+ expect(callbacks.onFinish).toHaveBeenCalledTimes(1);
251
+ expect(mockFetch).toHaveBeenCalledWith('http://localhost:8080/v1/chat/completions', expect.objectContaining({
252
+ method: 'POST',
253
+ body: JSON.stringify({
254
+ ...mockRequest,
255
+ stream: true,
256
+ }),
257
+ }));
258
+ });
207
259
  it('should handle tool calls in streaming chat completions', async () => {
208
260
  const mockRequest = {
209
261
  model: 'gpt-4o',
@@ -256,8 +308,8 @@ describe('InferenceGatewayClient', () => {
256
308
  type: 'function',
257
309
  function: {
258
310
  name: 'get_weather',
259
- arguments: '{"location":"San Francisco, CA"}'
260
- }
311
+ arguments: '{"location":"San Francisco, CA"}',
312
+ },
261
313
  });
262
314
  expect(callbacks.onFinish).toHaveBeenCalledTimes(1);
263
315
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inference-gateway/sdk",
3
- "version": "0.4.1-rc.1",
3
+ "version": "0.5.0",
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",
@@ -49,7 +49,8 @@
49
49
  "build": "tsc",
50
50
  "test": "jest",
51
51
  "lint": "eslint src/**/*.ts",
52
- "prepare": "npm run build"
52
+ "format": "prettier --write \"src/**/*.ts\" \"tests/**/*.ts\"",
53
+ "prepare": "husky"
53
54
  },
54
55
  "devDependencies": {
55
56
  "@eslint/js": "^9.18.0",
@@ -59,7 +60,9 @@
59
60
  "@typescript-eslint/parser": "^8.21.0",
60
61
  "eslint": "^9.18.0",
61
62
  "eslint-plugin-prettier": "^5.2.3",
63
+ "husky": "^9.1.7",
62
64
  "jest": "^29.7.0",
65
+ "prettier": "^3.5.3",
63
66
  "ts-jest": "^29.2.5",
64
67
  "typescript": "^5.7.3"
65
68
  },