|
1 | | -import { describe, it, expect } from 'vitest'; |
| 1 | +import { describe, it, expect, vi } from 'vitest'; |
2 | 2 | import { ChunkFrom } from '../../types'; |
3 | | -import { convertFullStreamChunkToMastra } from './transform'; |
| 3 | +import { convertFullStreamChunkToMastra, sanitizeToolCallInput } from './transform'; |
4 | 4 | import type { StreamPart } from './transform'; |
5 | 5 |
|
6 | 6 | describe('convertFullStreamChunkToMastra', () => { |
@@ -180,6 +180,160 @@ describe('convertFullStreamChunkToMastra', () => { |
180 | 180 | throw new Error('Result is not a tool-call'); |
181 | 181 | } |
182 | 182 | }); |
| 183 | + |
| 184 | + it('should recover valid JSON with trailing <|call|> token', () => { |
| 185 | + const chunk: StreamPart = { |
| 186 | + type: 'tool-call', |
| 187 | + toolCallId: 'call-1', |
| 188 | + toolName: 'get_weather', |
| 189 | + input: '{}<|call|>', |
| 190 | + providerExecuted: false, |
| 191 | + }; |
| 192 | + |
| 193 | + const result = convertFullStreamChunkToMastra(chunk, { runId: 'test-run-123' }); |
| 194 | + |
| 195 | + expect(result).toBeDefined(); |
| 196 | + expect(result?.type).toBe('tool-call'); |
| 197 | + if (result?.type === 'tool-call') { |
| 198 | + expect(result.payload.args).toEqual({}); |
| 199 | + } |
| 200 | + }); |
| 201 | + |
| 202 | + it('should recover valid JSON with tab + <|call|> token (issue #13185)', () => { |
| 203 | + const chunk: StreamPart = { |
| 204 | + type: 'tool-call', |
| 205 | + toolCallId: 'call-2', |
| 206 | + toolName: 'checkpoint', |
| 207 | + input: '{\n"checkpointNumber": 1,\n"vehicleType": "leopard"\n}\t<|call|>', |
| 208 | + providerExecuted: false, |
| 209 | + }; |
| 210 | + |
| 211 | + const result = convertFullStreamChunkToMastra(chunk, { runId: 'test-run-123' }); |
| 212 | + |
| 213 | + expect(result).toBeDefined(); |
| 214 | + expect(result?.type).toBe('tool-call'); |
| 215 | + if (result?.type === 'tool-call') { |
| 216 | + expect(result.payload.args).toEqual({ checkpointNumber: 1, vehicleType: 'leopard' }); |
| 217 | + } |
| 218 | + }); |
| 219 | + |
| 220 | + it('should recover valid JSON with <|endoftext|> token', () => { |
| 221 | + const chunk: StreamPart = { |
| 222 | + type: 'tool-call', |
| 223 | + toolCallId: 'call-3', |
| 224 | + toolName: 'search', |
| 225 | + input: '{"query": "hello world"}<|endoftext|>', |
| 226 | + providerExecuted: false, |
| 227 | + }; |
| 228 | + |
| 229 | + const result = convertFullStreamChunkToMastra(chunk, { runId: 'test-run-123' }); |
| 230 | + |
| 231 | + expect(result).toBeDefined(); |
| 232 | + expect(result?.type).toBe('tool-call'); |
| 233 | + if (result?.type === 'tool-call') { |
| 234 | + expect(result.payload.args).toEqual({ query: 'hello world' }); |
| 235 | + } |
| 236 | + }); |
| 237 | + |
| 238 | + it('should gracefully return undefined for truly malformed JSON (issue #13261)', () => { |
| 239 | + const chunk: StreamPart = { |
| 240 | + type: 'tool-call', |
| 241 | + toolCallId: 'call-4', |
| 242 | + toolName: 'checkpoint', |
| 243 | + input: '{"vehicleType":"leopard","checkpointNumber":?}', |
| 244 | + providerExecuted: false, |
| 245 | + }; |
| 246 | + |
| 247 | + const result = convertFullStreamChunkToMastra(chunk, { runId: 'test-run-123' }); |
| 248 | + |
| 249 | + expect(result).toBeDefined(); |
| 250 | + expect(result?.type).toBe('tool-call'); |
| 251 | + if (result?.type === 'tool-call') { |
| 252 | + expect(result.payload.args).toBeUndefined(); |
| 253 | + } |
| 254 | + }); |
| 255 | + |
| 256 | + it('should preserve <|...|> patterns inside JSON string values in tool-call args', () => { |
| 257 | + const chunk: StreamPart = { |
| 258 | + type: 'tool-call', |
| 259 | + toolCallId: 'call-5', |
| 260 | + toolName: 'process_text', |
| 261 | + input: '{"text": "The <|endoftext|> token marks boundaries"}', |
| 262 | + providerExecuted: false, |
| 263 | + }; |
| 264 | + |
| 265 | + const result = convertFullStreamChunkToMastra(chunk, { runId: 'test-run-123' }); |
| 266 | + |
| 267 | + expect(result).toBeDefined(); |
| 268 | + expect(result?.type).toBe('tool-call'); |
| 269 | + if (result?.type === 'tool-call') { |
| 270 | + expect(result.payload.args).toEqual({ text: 'The <|endoftext|> token marks boundaries' }); |
| 271 | + } |
| 272 | + }); |
| 273 | + |
| 274 | + it('should return undefined args without console.error noise when input is purely LLM tokens', () => { |
| 275 | + const errorSpy = vi.spyOn(console, 'error'); |
| 276 | + const chunk: StreamPart = { |
| 277 | + type: 'tool-call', |
| 278 | + toolCallId: 'call-6', |
| 279 | + toolName: 'noop', |
| 280 | + input: '<|call|>', |
| 281 | + providerExecuted: false, |
| 282 | + }; |
| 283 | + |
| 284 | + const result = convertFullStreamChunkToMastra(chunk, { runId: 'test-run-123' }); |
| 285 | + expect(result).toBeDefined(); |
| 286 | + expect(result?.type).toBe('tool-call'); |
| 287 | + if (result?.type === 'tool-call') { |
| 288 | + expect(result.payload.args).toBeUndefined(); |
| 289 | + } |
| 290 | + expect(errorSpy).not.toHaveBeenCalled(); |
| 291 | + errorSpy.mockRestore(); |
| 292 | + }); |
| 293 | + }); |
| 294 | + |
| 295 | + describe('sanitizeToolCallInput', () => { |
| 296 | + it('should strip <|call|> token from valid JSON', () => { |
| 297 | + expect(sanitizeToolCallInput('{}<|call|>')).toBe('{}'); |
| 298 | + }); |
| 299 | + |
| 300 | + it('should strip <|endoftext|> token', () => { |
| 301 | + expect(sanitizeToolCallInput('{"a":1}<|endoftext|>')).toBe('{"a":1}'); |
| 302 | + }); |
| 303 | + |
| 304 | + it('should strip multiple tokens', () => { |
| 305 | + expect(sanitizeToolCallInput('{}<|call|><|endoftext|>')).toBe('{}'); |
| 306 | + }); |
| 307 | + |
| 308 | + it('should strip tab + token combinations', () => { |
| 309 | + expect(sanitizeToolCallInput('{}\t<|call|>')).toBe('{}'); |
| 310 | + }); |
| 311 | + |
| 312 | + it('should be a no-op on clean JSON', () => { |
| 313 | + expect(sanitizeToolCallInput('{"key": "value"}')).toBe('{"key": "value"}'); |
| 314 | + }); |
| 315 | + |
| 316 | + it('should handle empty string', () => { |
| 317 | + expect(sanitizeToolCallInput('')).toBe(''); |
| 318 | + }); |
| 319 | + |
| 320 | + it('should strip <|end|> token', () => { |
| 321 | + expect(sanitizeToolCallInput('{"x":1}<|end|>')).toBe('{"x":1}'); |
| 322 | + }); |
| 323 | + |
| 324 | + it('should strip tokens with surrounding whitespace', () => { |
| 325 | + expect(sanitizeToolCallInput('{"x":1} <|call|> ')).toBe('{"x":1}'); |
| 326 | + }); |
| 327 | + |
| 328 | + it('should preserve <|...|> patterns inside JSON string values', () => { |
| 329 | + const input = '{"text": "use <|call|> token"}'; |
| 330 | + expect(sanitizeToolCallInput(input)).toBe('{"text": "use <|call|> token"}'); |
| 331 | + }); |
| 332 | + |
| 333 | + it('should preserve multiple <|...|> patterns inside JSON string values', () => { |
| 334 | + const input = '{"prompt": "tokens: <|endoftext|> and <|call|> are special"}'; |
| 335 | + expect(sanitizeToolCallInput(input)).toBe('{"prompt": "tokens: <|endoftext|> and <|call|> are special"}'); |
| 336 | + }); |
183 | 337 | }); |
184 | 338 |
|
185 | 339 | describe('other chunk types', () => { |
|
0 commit comments