@inference-gateway/sdk 0.4.0 → 0.4.1

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,12 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file.
4
4
 
5
+ ## [0.4.1](https://github.com/inference-gateway/typescript-sdk/compare/v0.4.0...v0.4.1) (2025-04-25)
6
+
7
+ ### ♻️ Improvements
8
+
9
+ * 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
+
5
11
  ## [0.4.0](https://github.com/inference-gateway/typescript-sdk/compare/v0.3.4...v0.4.0) (2025-03-31)
6
12
 
7
13
  ### ✨ Features
@@ -134,6 +134,7 @@ class InferenceGatewayClient {
134
134
  const reader = response.body.getReader();
135
135
  const decoder = new TextDecoder();
136
136
  let buffer = '';
137
+ const incompleteToolCalls = new Map();
137
138
  while (true) {
138
139
  const { done, value } = await reader.read();
139
140
  if (done)
@@ -145,6 +146,16 @@ class InferenceGatewayClient {
145
146
  if (line.startsWith('data: ')) {
146
147
  const data = line.slice(5).trim();
147
148
  if (data === '[DONE]') {
149
+ for (const [, toolCall] of incompleteToolCalls.entries()) {
150
+ callbacks.onTool?.({
151
+ id: toolCall.id,
152
+ type: toolCall.type,
153
+ function: {
154
+ name: toolCall.function.name,
155
+ arguments: toolCall.function.arguments,
156
+ },
157
+ });
158
+ }
148
159
  callbacks.onFinish?.(null);
149
160
  return;
150
161
  }
@@ -157,15 +168,48 @@ class InferenceGatewayClient {
157
168
  }
158
169
  const toolCalls = chunk.choices[0]?.delta?.tool_calls;
159
170
  if (toolCalls && toolCalls.length > 0) {
160
- const toolCall = {
161
- id: toolCalls[0].id || '',
162
- type: generated_1.ChatCompletionToolType.function,
163
- function: {
164
- name: toolCalls[0].function?.name || '',
165
- arguments: toolCalls[0].function?.arguments || '',
166
- },
167
- };
168
- callbacks.onTool?.(toolCall);
171
+ for (const toolCallChunk of toolCalls) {
172
+ const index = toolCallChunk.index;
173
+ if (!incompleteToolCalls.has(index)) {
174
+ incompleteToolCalls.set(index, {
175
+ id: toolCallChunk.id || '',
176
+ type: generated_1.ChatCompletionToolType.function,
177
+ function: {
178
+ name: toolCallChunk.function?.name || '',
179
+ arguments: toolCallChunk.function?.arguments || '',
180
+ },
181
+ });
182
+ }
183
+ else {
184
+ const existingToolCall = incompleteToolCalls.get(index);
185
+ if (toolCallChunk.id) {
186
+ existingToolCall.id = toolCallChunk.id;
187
+ }
188
+ if (toolCallChunk.function?.name) {
189
+ existingToolCall.function.name =
190
+ toolCallChunk.function.name;
191
+ }
192
+ if (toolCallChunk.function?.arguments) {
193
+ existingToolCall.function.arguments +=
194
+ toolCallChunk.function.arguments;
195
+ }
196
+ }
197
+ }
198
+ }
199
+ const finishReason = chunk.choices[0]?.finish_reason;
200
+ if (finishReason === 'tool_calls' &&
201
+ incompleteToolCalls.size > 0) {
202
+ for (const [, toolCall] of incompleteToolCalls.entries()) {
203
+ callbacks.onTool?.({
204
+ id: toolCall.id,
205
+ type: toolCall.type,
206
+ function: {
207
+ name: toolCall.function.name,
208
+ arguments: toolCall.function.arguments,
209
+ },
210
+ });
211
+ }
212
+ incompleteToolCalls.clear();
169
213
  }
170
214
  }
171
215
  catch (e) {
@@ -250,7 +250,15 @@ describe('InferenceGatewayClient', () => {
250
250
  await streamPromise;
251
251
  expect(callbacks.onOpen).toHaveBeenCalledTimes(1);
252
252
  expect(callbacks.onChunk).toHaveBeenCalledTimes(6);
253
- expect(callbacks.onTool).toHaveBeenCalledTimes(4);
253
+ expect(callbacks.onTool).toHaveBeenCalledTimes(1);
254
+ expect(callbacks.onTool).toHaveBeenCalledWith({
255
+ id: 'call_123',
256
+ type: 'function',
257
+ function: {
258
+ name: 'get_weather',
259
+ arguments: '{"location":"San Francisco, CA"}'
260
+ }
261
+ });
254
262
  expect(callbacks.onFinish).toHaveBeenCalledTimes(1);
255
263
  });
256
264
  it('should handle errors in streaming chat completions', async () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inference-gateway/sdk",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
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",