bingocode 1.0.29 → 1.0.31
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/adapters/common/__tests__/chat-queue.test.ts +61 -0
- package/adapters/common/__tests__/format.test.ts +148 -0
- package/adapters/common/__tests__/http-client.test.ts +105 -0
- package/adapters/common/__tests__/message-buffer.test.ts +84 -0
- package/adapters/common/__tests__/message-dedup.test.ts +57 -0
- package/adapters/common/__tests__/session-store.test.ts +62 -0
- package/adapters/common/__tests__/ws-bridge.test.ts +177 -0
- package/adapters/common/attachment/__tests__/attachment-limits.test.ts +52 -0
- package/adapters/common/attachment/__tests__/attachment-store.test.ts +108 -0
- package/adapters/common/attachment/__tests__/image-block-watcher.test.ts +115 -0
- package/adapters/feishu/__tests__/card-errors.test.ts +194 -0
- package/adapters/feishu/__tests__/cardkit.test.ts +295 -0
- package/adapters/feishu/__tests__/extract-payload.test.ts +77 -0
- package/adapters/feishu/__tests__/feishu.test.ts +907 -0
- package/adapters/feishu/__tests__/flush-controller.test.ts +290 -0
- package/adapters/feishu/__tests__/markdown-style.test.ts +353 -0
- package/adapters/feishu/__tests__/media.test.ts +120 -0
- package/adapters/feishu/__tests__/streaming-card.test.ts +914 -0
- package/adapters/telegram/__tests__/media.test.ts +86 -0
- package/adapters/telegram/__tests__/telegram.test.ts +115 -0
- package/bin/bingo-win.cjs +26 -0
- package/bin/bingocode-win.cjs +55 -3
- package/bin/claude-win.cjs +55 -3
- package/package.json +1 -1
- package/src/entrypoints/cli.tsx +4 -2
- package/src/manager/CliMenuManager.tsx +48 -17
- package/src/server/__tests__/conversation-service.test.ts +173 -0
- package/src/server/__tests__/conversations.test.ts +458 -0
- package/src/server/__tests__/cron-scheduler.test.ts +575 -0
- package/src/server/__tests__/e2e/business-flow.test.ts +841 -0
- package/src/server/__tests__/e2e/full-flow.test.ts +357 -0
- package/src/server/__tests__/fixtures/mock-sdk-cli.ts +123 -0
- package/src/server/__tests__/haha-oauth-api.test.ts +146 -0
- package/src/server/__tests__/haha-oauth-service.test.ts +185 -0
- package/src/server/__tests__/providers-real.test.ts +244 -0
- package/src/server/__tests__/providers.test.ts +579 -0
- package/src/server/__tests__/proxy-streaming.test.ts +317 -0
- package/src/server/__tests__/proxy-transform.test.ts +469 -0
- package/src/server/__tests__/real-llm-test.ts +526 -0
- package/src/server/__tests__/scheduled-tasks.test.ts +371 -0
- package/src/server/__tests__/sessions.test.ts +786 -0
- package/src/server/__tests__/settings.test.ts +376 -0
- package/src/server/__tests__/skills.test.ts +125 -0
- package/src/server/__tests__/tasks.test.ts +171 -0
- package/src/server/__tests__/team-watcher.test.ts +400 -0
- package/src/server/__tests__/teams.test.ts +627 -0
- package/src/server/ensureSingletonLocalServer.ts +1 -1
- package/src/server/middleware/cors.test.ts +27 -0
- package/src/utils/__tests__/cronFrequency.test.ts +153 -0
- package/src/utils/__tests__/cronTasks.test.ts +204 -0
- package/src/utils/computerUse/permissions.test.ts +44 -0
- package/src/utils/config.ts +15 -0
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unit tests for proxy streaming SSE transformation
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { describe, test, expect } from 'bun:test'
|
|
6
|
+
import { openaiChatStreamToAnthropic } from '../proxy/streaming/openaiChatStreamToAnthropic.js'
|
|
7
|
+
import { openaiResponsesStreamToAnthropic } from '../proxy/streaming/openaiResponsesStreamToAnthropic.js'
|
|
8
|
+
|
|
9
|
+
// ─── Helpers ────────────────────────────────────────────────────
|
|
10
|
+
|
|
11
|
+
function makeStream(chunks: string[]): ReadableStream<Uint8Array> {
|
|
12
|
+
const encoder = new TextEncoder()
|
|
13
|
+
return new ReadableStream({
|
|
14
|
+
start(controller) {
|
|
15
|
+
for (const chunk of chunks) {
|
|
16
|
+
controller.enqueue(encoder.encode(chunk))
|
|
17
|
+
}
|
|
18
|
+
controller.close()
|
|
19
|
+
},
|
|
20
|
+
})
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
async function collectSse(stream: ReadableStream<Uint8Array>): Promise<Array<{ event: string; data: Record<string, unknown> }>> {
|
|
24
|
+
const decoder = new TextDecoder()
|
|
25
|
+
const reader = stream.getReader()
|
|
26
|
+
let text = ''
|
|
27
|
+
while (true) {
|
|
28
|
+
const { done, value } = await reader.read()
|
|
29
|
+
if (done) break
|
|
30
|
+
text += decoder.decode(value, { stream: true })
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const events: Array<{ event: string; data: Record<string, unknown> }> = []
|
|
34
|
+
const blocks = text.split('\n\n').filter(Boolean)
|
|
35
|
+
for (const block of blocks) {
|
|
36
|
+
const lines = block.split('\n')
|
|
37
|
+
let event = ''
|
|
38
|
+
let data = ''
|
|
39
|
+
for (const line of lines) {
|
|
40
|
+
if (line.startsWith('event: ')) event = line.slice(7)
|
|
41
|
+
if (line.startsWith('data: ')) data = line.slice(6)
|
|
42
|
+
}
|
|
43
|
+
if (event && data) {
|
|
44
|
+
try {
|
|
45
|
+
events.push({ event, data: JSON.parse(data) })
|
|
46
|
+
} catch {
|
|
47
|
+
// skip unparseable
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return events
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// ─── OpenAI Chat Completions SSE → Anthropic SSE ───────────────
|
|
55
|
+
|
|
56
|
+
describe('openaiChatStreamToAnthropic', () => {
|
|
57
|
+
test('basic text streaming', async () => {
|
|
58
|
+
const sseChunks = [
|
|
59
|
+
'data: {"id":"c1","object":"chat.completion.chunk","created":0,"model":"gpt-4","choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":null}]}\n\n',
|
|
60
|
+
'data: {"id":"c1","object":"chat.completion.chunk","created":0,"model":"gpt-4","choices":[{"index":0,"delta":{"content":"Hello"},"finish_reason":null}]}\n\n',
|
|
61
|
+
'data: {"id":"c1","object":"chat.completion.chunk","created":0,"model":"gpt-4","choices":[{"index":0,"delta":{"content":" world"},"finish_reason":null}]}\n\n',
|
|
62
|
+
'data: {"id":"c1","object":"chat.completion.chunk","created":0,"model":"gpt-4","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}\n\n',
|
|
63
|
+
'data: [DONE]\n\n',
|
|
64
|
+
]
|
|
65
|
+
|
|
66
|
+
const upstream = makeStream(sseChunks)
|
|
67
|
+
const anthropicStream = openaiChatStreamToAnthropic(upstream, 'gpt-4')
|
|
68
|
+
const events = await collectSse(anthropicStream)
|
|
69
|
+
|
|
70
|
+
// Should have: message_start, content_block_start, content_block_delta x2, message_delta, content_block_stop, message_stop
|
|
71
|
+
const eventTypes = events.map((e) => e.event)
|
|
72
|
+
expect(eventTypes[0]).toBe('message_start')
|
|
73
|
+
expect(eventTypes).toContain('content_block_start')
|
|
74
|
+
expect(eventTypes).toContain('content_block_delta')
|
|
75
|
+
expect(eventTypes).toContain('message_delta')
|
|
76
|
+
expect(eventTypes).toContain('message_stop')
|
|
77
|
+
|
|
78
|
+
// Check message_start
|
|
79
|
+
const msgStart = events.find((e) => e.event === 'message_start')!
|
|
80
|
+
expect((msgStart.data.message as Record<string, unknown>).model).toBe('gpt-4')
|
|
81
|
+
expect((msgStart.data.message as Record<string, unknown>).role).toBe('assistant')
|
|
82
|
+
|
|
83
|
+
// Check text deltas
|
|
84
|
+
const textDeltas = events.filter((e) => e.event === 'content_block_delta')
|
|
85
|
+
const texts = textDeltas.map((e) => (e.data.delta as Record<string, unknown>).text)
|
|
86
|
+
expect(texts).toContain('Hello')
|
|
87
|
+
expect(texts).toContain(' world')
|
|
88
|
+
|
|
89
|
+
// Check stop reason
|
|
90
|
+
const msgDelta = events.find((e) => e.event === 'message_delta')!
|
|
91
|
+
expect((msgDelta.data.delta as Record<string, unknown>).stop_reason).toBe('end_turn')
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
test('tool call streaming', async () => {
|
|
95
|
+
const sseChunks = [
|
|
96
|
+
'data: {"id":"c2","object":"chat.completion.chunk","created":0,"model":"gpt-4","choices":[{"index":0,"delta":{"role":"assistant","content":null},"finish_reason":null}]}\n\n',
|
|
97
|
+
'data: {"id":"c2","object":"chat.completion.chunk","created":0,"model":"gpt-4","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"id":"call_1","type":"function","function":{"name":"get_weather","arguments":""}}]},"finish_reason":null}]}\n\n',
|
|
98
|
+
'data: {"id":"c2","object":"chat.completion.chunk","created":0,"model":"gpt-4","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"{\\"city\\""}}]},"finish_reason":null}]}\n\n',
|
|
99
|
+
'data: {"id":"c2","object":"chat.completion.chunk","created":0,"model":"gpt-4","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":":\\"NYC\\"}"}}]},"finish_reason":null}]}\n\n',
|
|
100
|
+
'data: {"id":"c2","object":"chat.completion.chunk","created":0,"model":"gpt-4","choices":[{"index":0,"delta":{},"finish_reason":"tool_calls"}]}\n\n',
|
|
101
|
+
'data: [DONE]\n\n',
|
|
102
|
+
]
|
|
103
|
+
|
|
104
|
+
const upstream = makeStream(sseChunks)
|
|
105
|
+
const anthropicStream = openaiChatStreamToAnthropic(upstream, 'gpt-4')
|
|
106
|
+
const events = await collectSse(anthropicStream)
|
|
107
|
+
|
|
108
|
+
// Should have content_block_start with type tool_use
|
|
109
|
+
const toolStart = events.find(
|
|
110
|
+
(e) => e.event === 'content_block_start' && (e.data.content_block as Record<string, unknown>)?.type === 'tool_use',
|
|
111
|
+
)
|
|
112
|
+
expect(toolStart).toBeDefined()
|
|
113
|
+
expect((toolStart!.data.content_block as Record<string, unknown>).name).toBe('get_weather')
|
|
114
|
+
expect((toolStart!.data.content_block as Record<string, unknown>).id).toBe('call_1')
|
|
115
|
+
|
|
116
|
+
// Should have input_json_delta
|
|
117
|
+
const jsonDeltas = events.filter(
|
|
118
|
+
(e) => e.event === 'content_block_delta' && (e.data.delta as Record<string, unknown>)?.type === 'input_json_delta',
|
|
119
|
+
)
|
|
120
|
+
expect(jsonDeltas.length).toBeGreaterThan(0)
|
|
121
|
+
|
|
122
|
+
// Stop reason should be tool_use
|
|
123
|
+
const msgDelta = events.find((e) => e.event === 'message_delta')!
|
|
124
|
+
expect((msgDelta.data.delta as Record<string, unknown>).stop_reason).toBe('tool_use')
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
test('empty stream (just DONE)', async () => {
|
|
128
|
+
const upstream = makeStream(['data: [DONE]\n\n'])
|
|
129
|
+
const anthropicStream = openaiChatStreamToAnthropic(upstream, 'gpt-4')
|
|
130
|
+
const events = await collectSse(anthropicStream)
|
|
131
|
+
// Should at least have message_stop
|
|
132
|
+
expect(events.some((e) => e.event === 'message_stop')).toBe(true)
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
test('event ordering: content_block_stop before message_delta', async () => {
|
|
136
|
+
const sseChunks = [
|
|
137
|
+
'data: {"id":"c3","object":"chat.completion.chunk","created":0,"model":"gpt-4","choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":null}]}\n\n',
|
|
138
|
+
'data: {"id":"c3","object":"chat.completion.chunk","created":0,"model":"gpt-4","choices":[{"index":0,"delta":{"content":"Hi"},"finish_reason":null}]}\n\n',
|
|
139
|
+
'data: {"id":"c3","object":"chat.completion.chunk","created":0,"model":"gpt-4","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}\n\n',
|
|
140
|
+
'data: [DONE]\n\n',
|
|
141
|
+
]
|
|
142
|
+
|
|
143
|
+
const upstream = makeStream(sseChunks)
|
|
144
|
+
const events = await collectSse(openaiChatStreamToAnthropic(upstream, 'gpt-4'))
|
|
145
|
+
const types = events.map((e) => e.event)
|
|
146
|
+
|
|
147
|
+
// content_block_stop MUST appear before message_delta
|
|
148
|
+
const stopIdx = types.indexOf('content_block_stop')
|
|
149
|
+
const deltaIdx = types.indexOf('message_delta')
|
|
150
|
+
expect(stopIdx).toBeGreaterThan(-1)
|
|
151
|
+
expect(deltaIdx).toBeGreaterThan(-1)
|
|
152
|
+
expect(stopIdx).toBeLessThan(deltaIdx)
|
|
153
|
+
|
|
154
|
+
// message_delta before message_stop
|
|
155
|
+
const msgStopIdx = types.indexOf('message_stop')
|
|
156
|
+
expect(deltaIdx).toBeLessThan(msgStopIdx)
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
test('reasoning_content (DeepSeek, OpenRouter, XAI)', async () => {
|
|
160
|
+
const sseChunks = [
|
|
161
|
+
'data: {"id":"c4","object":"chat.completion.chunk","created":0,"model":"deepseek-chat","choices":[{"index":0,"delta":{"role":"assistant","content":"","reasoning_content":"Let me think"},"finish_reason":null}]}\n\n',
|
|
162
|
+
'data: {"id":"c4","object":"chat.completion.chunk","created":0,"model":"deepseek-chat","choices":[{"index":0,"delta":{"reasoning_content":" about this..."},"finish_reason":null}]}\n\n',
|
|
163
|
+
'data: {"id":"c4","object":"chat.completion.chunk","created":0,"model":"deepseek-chat","choices":[{"index":0,"delta":{"content":"Hello!"},"finish_reason":null}]}\n\n',
|
|
164
|
+
'data: {"id":"c4","object":"chat.completion.chunk","created":0,"model":"deepseek-chat","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}\n\n',
|
|
165
|
+
'data: [DONE]\n\n',
|
|
166
|
+
]
|
|
167
|
+
|
|
168
|
+
const upstream = makeStream(sseChunks)
|
|
169
|
+
const events = await collectSse(openaiChatStreamToAnthropic(upstream, 'deepseek-chat'))
|
|
170
|
+
|
|
171
|
+
// Should have thinking block
|
|
172
|
+
const thinkingStart = events.find(
|
|
173
|
+
(e) => e.event === 'content_block_start' && (e.data.content_block as Record<string, unknown>)?.type === 'thinking',
|
|
174
|
+
)
|
|
175
|
+
expect(thinkingStart).toBeDefined()
|
|
176
|
+
|
|
177
|
+
// Should have thinking deltas
|
|
178
|
+
const thinkingDeltas = events.filter(
|
|
179
|
+
(e) => e.event === 'content_block_delta' && (e.data.delta as Record<string, unknown>)?.type === 'thinking_delta',
|
|
180
|
+
)
|
|
181
|
+
expect(thinkingDeltas.length).toBeGreaterThan(0)
|
|
182
|
+
|
|
183
|
+
// Should have text block after thinking
|
|
184
|
+
const textStart = events.find(
|
|
185
|
+
(e) => e.event === 'content_block_start' && (e.data.content_block as Record<string, unknown>)?.type === 'text',
|
|
186
|
+
)
|
|
187
|
+
expect(textStart).toBeDefined()
|
|
188
|
+
|
|
189
|
+
// Text should come after thinking in index order
|
|
190
|
+
expect((textStart!.data as Record<string, unknown>).index).toBeGreaterThan(
|
|
191
|
+
(thinkingStart!.data as Record<string, unknown>).index as number,
|
|
192
|
+
)
|
|
193
|
+
})
|
|
194
|
+
|
|
195
|
+
test('reasoning field (GLM-5, Cerebras, Groq)', async () => {
|
|
196
|
+
const sseChunks = [
|
|
197
|
+
'data: {"id":"c5","object":"chat.completion.chunk","created":0,"model":"glm-5","choices":[{"index":0,"delta":{"role":"assistant","reasoning":"Thinking here"},"finish_reason":null}]}\n\n',
|
|
198
|
+
'data: {"id":"c5","object":"chat.completion.chunk","created":0,"model":"glm-5","choices":[{"index":0,"delta":{"content":"Result"},"finish_reason":null}]}\n\n',
|
|
199
|
+
'data: {"id":"c5","object":"chat.completion.chunk","created":0,"model":"glm-5","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}\n\n',
|
|
200
|
+
'data: [DONE]\n\n',
|
|
201
|
+
]
|
|
202
|
+
|
|
203
|
+
const upstream = makeStream(sseChunks)
|
|
204
|
+
const events = await collectSse(openaiChatStreamToAnthropic(upstream, 'glm-5'))
|
|
205
|
+
|
|
206
|
+
// Should produce thinking delta from "reasoning" field
|
|
207
|
+
const thinkingDeltas = events.filter(
|
|
208
|
+
(e) => e.event === 'content_block_delta' && (e.data.delta as Record<string, unknown>)?.type === 'thinking_delta',
|
|
209
|
+
)
|
|
210
|
+
expect(thinkingDeltas.length).toBe(1)
|
|
211
|
+
expect((thinkingDeltas[0].data.delta as Record<string, unknown>).thinking).toBe('Thinking here')
|
|
212
|
+
})
|
|
213
|
+
|
|
214
|
+
test('thinking_blocks (OpenAI o-series)', async () => {
|
|
215
|
+
const sseChunks = [
|
|
216
|
+
'data: {"id":"c6","object":"chat.completion.chunk","created":0,"model":"o3","choices":[{"index":0,"delta":{"role":"assistant","thinking_blocks":[{"type":"thinking","thinking":"Deep thought"}]},"finish_reason":null}]}\n\n',
|
|
217
|
+
'data: {"id":"c6","object":"chat.completion.chunk","created":0,"model":"o3","choices":[{"index":0,"delta":{"content":"Answer"},"finish_reason":null}]}\n\n',
|
|
218
|
+
'data: {"id":"c6","object":"chat.completion.chunk","created":0,"model":"o3","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}\n\n',
|
|
219
|
+
'data: [DONE]\n\n',
|
|
220
|
+
]
|
|
221
|
+
|
|
222
|
+
const upstream = makeStream(sseChunks)
|
|
223
|
+
const events = await collectSse(openaiChatStreamToAnthropic(upstream, 'o3'))
|
|
224
|
+
|
|
225
|
+
const thinkingDeltas = events.filter(
|
|
226
|
+
(e) => e.event === 'content_block_delta' && (e.data.delta as Record<string, unknown>)?.type === 'thinking_delta',
|
|
227
|
+
)
|
|
228
|
+
expect(thinkingDeltas.length).toBe(1)
|
|
229
|
+
expect((thinkingDeltas[0].data.delta as Record<string, unknown>).thinking).toBe('Deep thought')
|
|
230
|
+
})
|
|
231
|
+
|
|
232
|
+
test('text + tool transition closes text block first', async () => {
|
|
233
|
+
const sseChunks = [
|
|
234
|
+
'data: {"id":"c7","object":"chat.completion.chunk","created":0,"model":"gpt-4","choices":[{"index":0,"delta":{"content":"Let me search"},"finish_reason":null}]}\n\n',
|
|
235
|
+
'data: {"id":"c7","object":"chat.completion.chunk","created":0,"model":"gpt-4","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"id":"call_x","type":"function","function":{"name":"search","arguments":"{\\"q\\":\\"test\\"}"}}]},"finish_reason":null}]}\n\n',
|
|
236
|
+
'data: {"id":"c7","object":"chat.completion.chunk","created":0,"model":"gpt-4","choices":[{"index":0,"delta":{},"finish_reason":"tool_calls"}]}\n\n',
|
|
237
|
+
'data: [DONE]\n\n',
|
|
238
|
+
]
|
|
239
|
+
|
|
240
|
+
const upstream = makeStream(sseChunks)
|
|
241
|
+
const events = await collectSse(openaiChatStreamToAnthropic(upstream, 'gpt-4'))
|
|
242
|
+
const types = events.map((e) => e.event)
|
|
243
|
+
|
|
244
|
+
// Should see: text block start, text delta, text block stop, tool block start, ...
|
|
245
|
+
const firstBlockStop = types.indexOf('content_block_stop')
|
|
246
|
+
const toolBlockStart = types.findIndex(
|
|
247
|
+
(_, i) => events[i].event === 'content_block_start' && (events[i].data.content_block as Record<string, unknown>)?.type === 'tool_use',
|
|
248
|
+
)
|
|
249
|
+
expect(firstBlockStop).toBeLessThan(toolBlockStart)
|
|
250
|
+
})
|
|
251
|
+
})
|
|
252
|
+
|
|
253
|
+
// ─── OpenAI Responses SSE → Anthropic SSE ──────────────────────
|
|
254
|
+
|
|
255
|
+
describe('openaiResponsesStreamToAnthropic', () => {
|
|
256
|
+
test('basic text streaming', async () => {
|
|
257
|
+
const sseChunks = [
|
|
258
|
+
'event: response.created\ndata: {"id":"r1","model":"gpt-4o","status":"in_progress"}\n\n',
|
|
259
|
+
'event: response.output_item.added\ndata: {"output_index":0,"item":{"type":"message","role":"assistant"}}\n\n',
|
|
260
|
+
'event: response.content_part.added\ndata: {"output_index":0,"content_index":0,"part":{"type":"output_text","text":""}}\n\n',
|
|
261
|
+
'event: response.output_text.delta\ndata: {"output_index":0,"content_index":0,"delta":"Hello"}\n\n',
|
|
262
|
+
'event: response.output_text.delta\ndata: {"output_index":0,"content_index":0,"delta":" world"}\n\n',
|
|
263
|
+
'event: response.output_text.done\ndata: {"output_index":0,"content_index":0,"text":"Hello world"}\n\n',
|
|
264
|
+
'event: response.completed\ndata: {"response":{"id":"r1","model":"gpt-4o","status":"completed","usage":{"input_tokens":10,"output_tokens":5}}}\n\n',
|
|
265
|
+
]
|
|
266
|
+
|
|
267
|
+
const upstream = makeStream(sseChunks)
|
|
268
|
+
const anthropicStream = openaiResponsesStreamToAnthropic(upstream, 'gpt-4o')
|
|
269
|
+
const events = await collectSse(anthropicStream)
|
|
270
|
+
|
|
271
|
+
const eventTypes = events.map((e) => e.event)
|
|
272
|
+
expect(eventTypes[0]).toBe('message_start')
|
|
273
|
+
expect(eventTypes).toContain('content_block_start')
|
|
274
|
+
expect(eventTypes).toContain('content_block_delta')
|
|
275
|
+
expect(eventTypes).toContain('content_block_stop')
|
|
276
|
+
expect(eventTypes).toContain('message_delta')
|
|
277
|
+
expect(eventTypes).toContain('message_stop')
|
|
278
|
+
|
|
279
|
+
// Check text deltas
|
|
280
|
+
const textDeltas = events.filter((e) => e.event === 'content_block_delta')
|
|
281
|
+
const texts = textDeltas.map((e) => (e.data.delta as Record<string, unknown>).text)
|
|
282
|
+
expect(texts).toContain('Hello')
|
|
283
|
+
expect(texts).toContain(' world')
|
|
284
|
+
})
|
|
285
|
+
|
|
286
|
+
test('function call streaming', async () => {
|
|
287
|
+
const sseChunks = [
|
|
288
|
+
'event: response.created\ndata: {"id":"r2","model":"gpt-4o","status":"in_progress"}\n\n',
|
|
289
|
+
'event: response.output_item.added\ndata: {"output_index":0,"item":{"type":"function_call","id":"fc_1","call_id":"call_1","name":"search"}}\n\n',
|
|
290
|
+
'event: response.function_call_arguments.delta\ndata: {"item_id":"fc_1","delta":"{\\"q\\":"}\n\n',
|
|
291
|
+
'event: response.function_call_arguments.delta\ndata: {"item_id":"fc_1","delta":"\\"test\\"}"}\n\n',
|
|
292
|
+
'event: response.function_call_arguments.done\ndata: {"item_id":"fc_1","arguments":"{\\"q\\":\\"test\\"}"}\n\n',
|
|
293
|
+
'event: response.completed\ndata: {"response":{"id":"r2","model":"gpt-4o","status":"completed","usage":{"input_tokens":10,"output_tokens":5}}}\n\n',
|
|
294
|
+
]
|
|
295
|
+
|
|
296
|
+
const upstream = makeStream(sseChunks)
|
|
297
|
+
const anthropicStream = openaiResponsesStreamToAnthropic(upstream, 'gpt-4o')
|
|
298
|
+
const events = await collectSse(anthropicStream)
|
|
299
|
+
|
|
300
|
+
// Should have tool_use content_block_start
|
|
301
|
+
const toolStart = events.find(
|
|
302
|
+
(e) => e.event === 'content_block_start' && (e.data.content_block as Record<string, unknown>)?.type === 'tool_use',
|
|
303
|
+
)
|
|
304
|
+
expect(toolStart).toBeDefined()
|
|
305
|
+
expect((toolStart!.data.content_block as Record<string, unknown>).name).toBe('search')
|
|
306
|
+
|
|
307
|
+
// Should have input_json_delta
|
|
308
|
+
const jsonDeltas = events.filter(
|
|
309
|
+
(e) => e.event === 'content_block_delta' && (e.data.delta as Record<string, unknown>)?.type === 'input_json_delta',
|
|
310
|
+
)
|
|
311
|
+
expect(jsonDeltas.length).toBeGreaterThan(0)
|
|
312
|
+
|
|
313
|
+
// Stop reason should be tool_use
|
|
314
|
+
const msgDelta = events.find((e) => e.event === 'message_delta')!
|
|
315
|
+
expect((msgDelta.data.delta as Record<string, unknown>).stop_reason).toBe('tool_use')
|
|
316
|
+
})
|
|
317
|
+
})
|