Spaces:
Running
Running
| import test from 'node:test'; | |
| import assert from 'node:assert/strict'; | |
| import { | |
| convertAnthropicEventToOpenAIChunks, | |
| createOpenAIStreamState | |
| } from '../../src/format/openai-stream.js'; | |
| test('Anthropic events become OpenAI chunks and a final usage chunk', () => { | |
| const state = createOpenAIStreamState('gemini-3.5-flash-low', true); | |
| const events = [ | |
| { | |
| type: 'message_start', | |
| message: { id: 'msg_abc', usage: { input_tokens: 4 } } | |
| }, | |
| { | |
| type: 'content_block_delta', | |
| index: 0, | |
| delta: { type: 'text_delta', text: 'Hi' } | |
| }, | |
| { | |
| type: 'message_delta', | |
| delta: { stop_reason: 'end_turn' }, | |
| usage: { output_tokens: 1 } | |
| }, | |
| { type: 'message_stop' } | |
| ]; | |
| const chunks = events.flatMap(event => convertAnthropicEventToOpenAIChunks(event, state)); | |
| assert.equal(chunks[0].choices[0].delta.role, 'assistant'); | |
| assert.equal(chunks[1].choices[0].delta.content, 'Hi'); | |
| assert.equal(chunks[2].choices[0].finish_reason, 'stop'); | |
| assert.equal(chunks[3].usage.total_tokens, 5); | |
| }); | |
| test('tool deltas keep a stable OpenAI tool call index', () => { | |
| const state = createOpenAIStreamState('gemini-3.5-flash-low', false); | |
| convertAnthropicEventToOpenAIChunks({ | |
| type: 'message_start', | |
| message: { id: 'msg_tool', usage: { input_tokens: 2 } } | |
| }, state); | |
| const start = convertAnthropicEventToOpenAIChunks({ | |
| type: 'content_block_start', | |
| index: 3, | |
| content_block: { type: 'tool_use', id: 'call_1', name: 'lookup' } | |
| }, state); | |
| const delta = convertAnthropicEventToOpenAIChunks({ | |
| type: 'content_block_delta', | |
| index: 3, | |
| delta: { type: 'input_json_delta', partial_json: '{"id":"1"}' } | |
| }, state); | |
| assert.equal(start[0].choices[0].delta.tool_calls[0].index, 0); | |
| assert.equal(delta[0].choices[0].delta.tool_calls[0].index, 0); | |
| }); | |