Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

text.test.ts

1,122 lines34.5 KB

1import {
2 afterEach,
3 beforeEach,
4 describe,
5 expect,
6 it,
7 vi,
8 type Mock,
9} from 'vitest'
10import { EventType, StreamProcessor, chat } from '@tanstack/ai'
11import { resolveDebugOption } from '@tanstack/ai/adapter-internals'
12import {
13 createBytePlusText as _realCreateBytePlusText,
14 byteplusText as _realBytePlusText,
15} from '../src/adapters/text'
16import {
17 BYTEPLUS_CHAT_MODELS,
18 BYTEPLUS_STRUCTURED_OUTPUT_CHAT_MODELS,
19 BYTEPLUS_THINKING_SUMMARY_MODELS,
20} from '../src/model-meta'
21import type { ContentPart, ModelMessage, StreamChunk, Tool } from '@tanstack/ai'
22import type { BytePlusTextProviderOptions } from '../src/index'
23​
24// Silent logger for adapter calls under test.
25const testLogger = resolveDebugOption(false)
26​
27// Stub the OpenAI SDK so constructing an adapter never opens a real network
28// handle. The per-test mock client is injected post-construction through the
29// wrapped factories below (the ai-groq / ai-grok pattern) rather than by
30// stubbing globals — the built openai-base dist resolves `openai`
31// independently, so vi.mock alone would not intercept it.
32vi.mock('openai', () => {
33 return {
34 default: class {
35 chat = {
36 completions: {
37 create: vi.fn(),
38 },
39 }
40 },
41 }
42})
43​
44function createAsyncIterable<T>(chunks: Array<T>): AsyncIterable<T> {
45 return {
46 [Symbol.asyncIterator]() {
47 let index = 0
48 return {
49 async next() {
50 if (index < chunks.length) {
51 return { value: chunks[index++]!, done: false }
52 }
53 return { value: undefined as T, done: true }
54 },
55 }
56 },
57 }
59​
60let pendingMockCreate: Mock<(...args: Array<unknown>) => unknown> | undefined
61​
62function setupMockSdkClient(
63 streamChunks: Array<Record<string, unknown>>,
64 nonStreamResponse?: Record<string, unknown>,
65): Mock<(...args: Array<unknown>) => unknown> {
66 pendingMockCreate = vi.fn().mockImplementation((params) => {
67 if (params.stream) {
68 return Promise.resolve(createAsyncIterable(streamChunks))
69 }
70 return Promise.resolve(nonStreamResponse)
71 })
72 return pendingMockCreate
74​
75function applyPendingMock<T extends object>(adapter: T): T {
76 if (pendingMockCreate) {
77 ;(adapter as any).client = {
78 chat: { completions: { create: pendingMockCreate } },
79 }
80 pendingMockCreate = undefined
81 }
82 return adapter
84​
85const createBytePlusText: typeof _realCreateBytePlusText = (
86 model,
87 apiKey,
88 config,
89) => applyPendingMock(_realCreateBytePlusText(model, apiKey, config))
90const byteplusText: typeof _realBytePlusText = (model, config) =>
91 applyPendingMock(_realBytePlusText(model, config))
92​
93// Models are picked off the exported metadata rather than hard-coded so these
94// tests keep testing the *behaviour* (gating, echoing) if the underlying
95// capability lists are corrected.
96const THINKING_SUMMARY_MODEL = BYTEPLUS_THINKING_SUMMARY_MODELS[0]
97const PLAIN_MODEL = BYTEPLUS_CHAT_MODELS.find(
98 (model) =>
99 !(BYTEPLUS_THINKING_SUMMARY_MODELS as ReadonlyArray<string>).includes(
100 model,
101 ),
102)!
103const STRUCTURED_MODEL = BYTEPLUS_STRUCTURED_OUTPUT_CHAT_MODELS[0]
104const UNSTRUCTURED_MODEL = BYTEPLUS_CHAT_MODELS.find(
105 (model) =>
106 !(BYTEPLUS_STRUCTURED_OUTPUT_CHAT_MODELS as ReadonlyArray<string>).includes(
107 model,
108 ),
109)!
110​
111const ENCRYPTED_BLOB = 'ENC-eyJhbGciOiJzaWduZWQifQ.reasoning-signature'
112​
113const weatherTool: Tool = {
114 name: 'lookup_weather',
115 description: 'Return the forecast for a location',
117​
118/** A minimal plain-text completion: one content delta, then a finish chunk. */
119function textStreamChunks(model: string): Array<Record<string, unknown>> {
120 return [
121 {
122 id: 'chatcmpl-text',
123 model,
124 choices: [{ index: 0, delta: { content: 'Hello!' } }],
125 },
126 {
127 id: 'chatcmpl-text',
128 model,
129 choices: [{ index: 0, delta: {}, finish_reason: 'stop' }],
130 usage: { prompt_tokens: 12, completion_tokens: 7, total_tokens: 19 },
131 },
132 ]
134​
135/**
136 * The chunk sequence a thinking-summary model produces, as captured live:
137 * reasoning deltas → one dedicated encrypted_content chunk → content deltas →
138 * a finish chunk → a usage-only chunk with empty `choices`.
139 */
140function thinkingStreamChunks(model: string): Array<Record<string, unknown>> {
141 return [
142 {
143 id: 'chatcmpl-think',
144 model,
145 choices: [{ index: 0, delta: { reasoning_content: 'The user ' } }],
146 },
147 {
148 id: 'chatcmpl-think',
149 model,
150 choices: [
151 { index: 0, delta: { reasoning_content: 'wants a greeting.' } },
152 ],
153 },
154 {
155 id: 'chatcmpl-think',
156 model,
157 choices: [
158 {
159 index: 0,
160 delta: {
161 content: '',
162 reasoning_content: '',
163 encrypted_content: ENCRYPTED_BLOB,
164 },
165 },
166 ],
167 },
168 {
169 id: 'chatcmpl-think',
170 model,
171 choices: [{ index: 0, delta: { content: 'Hello' } }],
172 },
173 {
174 id: 'chatcmpl-think',
175 model,
176 choices: [{ index: 0, delta: { content: '!' }, finish_reason: 'stop' }],
177 },
178 {
179 id: 'chatcmpl-think',
180 model,
181 choices: [],
182 usage: {
183 prompt_tokens: 12,
184 completion_tokens: 7,
185 total_tokens: 19,
186 completion_tokens_details: { reasoning_tokens: 4 },
187 },
188 },
189 ]
191​
192async function collect(
193 stream: AsyncIterable<StreamChunk>,
194): Promise<Array<StreamChunk>> {
195 const chunks: Array<StreamChunk> = []
196 for await (const chunk of stream) chunks.push(chunk)
197 return chunks
199​
200describe('BytePlus text adapter', () => {
201 beforeEach(() => {
202 pendingMockCreate = undefined
203 })
204​
205 afterEach(() => {
206 vi.unstubAllEnvs()
207 })
208​
209 describe('construction', () => {
210 it('creates an adapter with an explicit API key', () => {
211 const adapter = createBytePlusText('seed-2-0-lite-260428', 'ark-test-key')
212​
213 expect(adapter.kind).toBe('text')
214 expect(adapter.name).toBe('byteplus')
215 expect(adapter.model).toBe('seed-2-0-lite-260428')
216 })
217​
218 it('creates an adapter from ARK_API_KEY', () => {
219 vi.stubEnv('ARK_API_KEY', 'env-ark-key')
220​
221 const adapter = byteplusText('seed-1-6-250915')
222​
223 expect(adapter.model).toBe('seed-1-6-250915')
224 })
225​
226 it('throws when ARK_API_KEY is missing', () => {
227 vi.stubEnv('ARK_API_KEY', '')
228​
229 expect(() => byteplusText('seed-2-0-lite-260428')).toThrow('ARK_API_KEY')
230 })
231​
232 it('accepts a baseURL override (EU endpoint)', () => {
233 const adapter = createBytePlusText(
234 'seed-2-0-lite-260428',
235 'ark-test-key',
236 { baseURL: 'https://ark.eu-west.bytepluses.com/api/v3' },
237 )
238​
239 expect(adapter).toBeDefined()
240 })
241 })
242​
243 describe('streaming', () => {
244 it('emits reasoning, then text, then usage for a thinking stream', async () => {
245 setupMockSdkClient(thinkingStreamChunks(THINKING_SUMMARY_MODEL))
246 const adapter = createBytePlusText(THINKING_SUMMARY_MODEL, 'ark-test-key')
247​
248 const chunks = await collect(
249 adapter.chatStream({
250 model: THINKING_SUMMARY_MODEL,
251 messages: [{ role: 'user', content: 'Say hi' }],
252 logger: testLogger,
253 }),
254 )
255​
256 const types = chunks.map((c) => c.type)
257 expect(types[0]).toBe(EventType.RUN_STARTED)
258 expect(types).toContain(EventType.REASONING_MESSAGE_START)
259 expect(types).toContain(EventType.TEXT_MESSAGE_START)
260 expect(types.indexOf(EventType.REASONING_END)).toBeLessThan(
261 types.indexOf(EventType.TEXT_MESSAGE_START),
262 )
263​
264 const reasoning = chunks
265 .filter((c) => c.type === EventType.REASONING_MESSAGE_CONTENT)
266 .map((c) => c.delta)
267 .join('')
268 expect(reasoning).toBe('The user wants a greeting.')
269​
270 const text = chunks
271 .filter((c) => c.type === EventType.TEXT_MESSAGE_CONTENT)
272 .map((c) => c.delta)
273 .join('')
274 expect(text).toBe('Hello!')
275​
276 const runFinished = chunks.find((c) => c.type === EventType.RUN_FINISHED)
277 expect(
278 runFinished?.type === EventType.RUN_FINISHED && runFinished.usage,
279 ).toMatchObject({
280 promptTokens: 12,
281 completionTokens: 7,
282 totalTokens: 19,
283 })
284 })
285​
286 it('attaches encrypted_content to the reasoning step as its signature', async () => {
287 setupMockSdkClient(thinkingStreamChunks(THINKING_SUMMARY_MODEL))
288 const adapter = createBytePlusText(THINKING_SUMMARY_MODEL, 'ark-test-key')
289​
290 const chunks = await collect(
291 adapter.chatStream({
292 model: THINKING_SUMMARY_MODEL,
293 messages: [{ role: 'user', content: 'Say hi' }],
294 logger: testLogger,
295 }),
296 )
297​
298 const stepFinished = chunks.filter(
299 (c) => c.type === EventType.STEP_FINISHED,
300 )
301 expect(stepFinished).toHaveLength(1)
302 const step = stepFinished[0]
303 if (step?.type !== EventType.STEP_FINISHED) throw new Error('no step')
304 expect(step.signature).toBe(ENCRYPTED_BLOB)
305 // `delta` must carry the reasoning text too. chat()'s agent loop
306 // accumulates thinking only from STEP_FINISHED.delta and discards the
307 // whole step — signature included — when that accumulation is empty, so
308 // a signature without a delta never reaches the continuation message.
309 expect(step.delta).toBe('The user wants a greeting.')
310 })
311​
312 it('does not treat the encrypted chunk as reasoning text', async () => {
313 setupMockSdkClient([
314 {
315 id: 'chatcmpl-enc-only',
316 model: THINKING_SUMMARY_MODEL,
317 choices: [
318 {
319 index: 0,
320 delta: {
321 content: '',
322 reasoning_content: '',
323 encrypted_content: ENCRYPTED_BLOB,
324 },
325 },
326 ],
327 },
328 {
329 id: 'chatcmpl-enc-only',
330 model: THINKING_SUMMARY_MODEL,
331 choices: [
332 { index: 0, delta: { content: 'Hi' }, finish_reason: 'stop' },
333 ],
334 },
335 ])
336 const adapter = createBytePlusText(THINKING_SUMMARY_MODEL, 'ark-test-key')
337​
338 const chunks = await collect(
339 adapter.chatStream({
340 model: THINKING_SUMMARY_MODEL,
341 messages: [{ role: 'user', content: 'Say hi' }],
342 logger: testLogger,
343 }),
344 )
345​
346 expect(
347 chunks.some((c) => c.type === EventType.REASONING_MESSAGE_CONTENT),
348 ).toBe(false)
349​
350 // Pinning the consequence, not endorsing it: with no reasoning deltas
351 // the base never opens a reasoning lifecycle, so there is no
352 // STEP_FINISHED to stamp and the blob is silently dropped. Live Ark
353 // always sends the encrypted chunk *after* reasoning deltas, but that
354 // ordering is observed behaviour rather than a documented contract — if
355 // this assertion ever fails because a STEP_FINISHED appeared, the
356 // capture path needs revisiting, not this expectation.
357 expect(chunks.some((c) => c.type === EventType.STEP_FINISHED)).toBe(false)
358 })
359​
360 it('emits AG-UI tool-call events with the OpenAI tool shape', async () => {
361 const mockCreate = setupMockSdkClient([
362 {
363 id: 'chatcmpl-tool',
364 model: 'seed-2-0-lite-260428',
365 choices: [
366 {
367 index: 0,
368 delta: {
369 tool_calls: [
370 {
371 index: 0,
372 id: 'call_1',
373 type: 'function',
374 function: {
375 name: 'lookup_weather',
376 arguments: '{"location":',
377 },
378 },
379 ],
380 },
381 },
382 ],
383 },
384 {
385 id: 'chatcmpl-tool',
386 model: 'seed-2-0-lite-260428',
387 choices: [
388 {
389 index: 0,
390 delta: {
391 tool_calls: [
392 { index: 0, function: { arguments: '"Berlin"}' } },
393 ],
394 },
395 finish_reason: 'tool_calls',
396 },
397 ],
398 },
399 ])
400 const adapter = createBytePlusText('seed-2-0-lite-260428', 'ark-test-key')
401​
402 const chunks = await collect(
403 adapter.chatStream({
404 model: 'seed-2-0-lite-260428',
405 messages: [{ role: 'user', content: 'Weather in Berlin?' }],
406 tools: [weatherTool],
407 logger: testLogger,
408 }),
409 )
410​
411 // Ark accepts (and requires) `type: 'function'` — the docs' claimed
412 // `'function_call'` value is rejected with 400 InvalidParameter.
413 expect(mockCreate.mock.calls[0]?.[0]).toMatchObject({
414 tools: [{ type: 'function', function: { name: 'lookup_weather' } }],
415 })
416​
417 const toolEnd = chunks.find((c) => c.type === EventType.TOOL_CALL_END)
418 expect(
419 toolEnd?.type === EventType.TOOL_CALL_END && toolEnd.input,
420 ).toEqual({ location: 'Berlin' })
421 })
422 })
423​
424 describe('encrypted_content round-trip', () => {
425 const assistantWithSignature: ModelMessage = {
426 role: 'assistant',
427 content: 'Hello!',
428 thinking: [
429 { content: 'The user wants a greeting.', signature: ENCRYPTED_BLOB },
430 ],
431 }
432​
433 async function requestBodyFor(
434 model: (typeof BYTEPLUS_CHAT_MODELS)[number],
435 messages: Array<ModelMessage>,
436 ): Promise<any> {
437 const mockCreate = setupMockSdkClient([
438 {
439 id: 'chatcmpl-echo',
440 model,
441 choices: [
442 { index: 0, delta: { content: 'ok' }, finish_reason: 'stop' },
443 ],
444 },
445 ])
446 const adapter = createBytePlusText(model, 'ark-test-key')
447 await collect(adapter.chatStream({ model, messages, logger: testLogger }))
448 return mockCreate.mock.calls[0]?.[0]
449 }
450​
451 it('echoes the blob verbatim on the outgoing assistant message', async () => {
452 const body = await requestBodyFor(THINKING_SUMMARY_MODEL, [
453 { role: 'user', content: 'Say hi' },
454 assistantWithSignature,
455 { role: 'user', content: 'Again?' },
456 ])
457​
458 expect(body.messages[1]).toMatchObject({
459 role: 'assistant',
460 content: 'Hello!',
461 encrypted_content: ENCRYPTED_BLOB,
462 })
463 })
464​
465 it('omits the field when the assistant message carries no signature', async () => {
466 const body = await requestBodyFor(THINKING_SUMMARY_MODEL, [
467 { role: 'user', content: 'Say hi' },
468 { role: 'assistant', content: 'Hello!' },
469 { role: 'user', content: 'Again?' },
470 ])
471​
472 expect(body.messages[1]).not.toHaveProperty('encrypted_content')
473 })
474​
475 it('never forwards a signature on a model that does not emit the blob', async () => {
476 const body = await requestBodyFor(PLAIN_MODEL, [
477 { role: 'user', content: 'Say hi' },
478 assistantWithSignature,
479 { role: 'user', content: 'Again?' },
480 ])
481​
482 expect(body.messages[1]).not.toHaveProperty('encrypted_content')
483 })
484​
485 it('echoes the last signature when several thinking steps carry one', async () => {
486 const body = await requestBodyFor(THINKING_SUMMARY_MODEL, [
487 { role: 'user', content: 'Say hi' },
488 {
489 role: 'assistant',
490 content: 'Hello!',
491 thinking: [
492 { content: 'first', signature: 'ENC-first' },
493 { content: 'second', signature: 'ENC-second' },
494 ],
495 },
496 { role: 'user', content: 'Again?' },
497 ])
498​
499 expect(body.messages[1].encrypted_content).toBe('ENC-second')
500 })
501 })
502​
503 describe('provider options', () => {
504 it('forwards Ark-only and sampling options into the request body', async () => {
505 const mockCreate = setupMockSdkClient([
506 {
507 id: 'chatcmpl-opts',
508 model: 'seed-2-0-lite-260428',
509 choices: [{ index: 0, delta: {}, finish_reason: 'stop' }],
510 },
511 ])
512 const adapter = createBytePlusText('seed-2-0-lite-260428', 'ark-test-key')
513​
514 const modelOptions: BytePlusTextProviderOptions = {
515 thinking: { type: 'enabled' },
516 reasoning_effort: 'high',
517 repetition_penalty: 1.05,
518 service_tier: 'flex',
519 temperature: 0.4,
520 max_tokens: 256,
521 }
522​
523 await collect(
524 adapter.chatStream({
525 model: 'seed-2-0-lite-260428',
526 messages: [{ role: 'user', content: 'Hello' }],
527 modelOptions,
528 logger: testLogger,
529 }),
530 )
531​
532 expect(mockCreate.mock.calls[0]?.[0]).toMatchObject({
533 thinking: { type: 'enabled' },
534 reasoning_effort: 'high',
535 repetition_penalty: 1.05,
536 service_tier: 'flex',
537 temperature: 0.4,
538 max_tokens: 256,
539 stream: true,
540 stream_options: { include_usage: true },
541 })
542 })
543 })
544​
545 describe('multimodal content parts', () => {
546 async function contentPartsFor(parts: Array<ContentPart>): Promise<any> {
547 const mockCreate = setupMockSdkClient([
548 {
549 id: 'chatcmpl-mm',
550 model: 'seed-2-0-lite-260428',
551 choices: [
552 { index: 0, delta: { content: 'ok' }, finish_reason: 'stop' },
553 ],
554 },
555 ])
556 const adapter = createBytePlusText('seed-2-0-lite-260428', 'ark-test-key')
557 await collect(
558 adapter.chatStream({
559 model: 'seed-2-0-lite-260428',
560 messages: [{ role: 'user', content: parts }],
561 logger: testLogger,
562 }),
563 )
564 const body: any = mockCreate.mock.calls[0]?.[0]
565 return body.messages[0].content
566 }
567​
568 it('sends image parts with Ark detail and pixel bounds', async () => {
569 const content = await contentPartsFor([
570 { type: 'text', content: 'What is this?' },
571 {
572 type: 'image',
573 source: { type: 'url', value: 'https://example.com/cat.png' },
574 metadata: {
575 detail: 'xhigh',
576 image_pixel_limit: { max_pixels: 1_000_000 },
577 },
578 },
579 ])
580​
581 expect(content).toContainEqual({
582 type: 'image_url',
583 image_url: {
584 url: 'https://example.com/cat.png',
585 detail: 'xhigh',
586 image_pixel_limit: { max_pixels: 1_000_000 },
587 },
588 })
589 })
590​
591 it('wraps inline base64 image data in a data: URI', async () => {
592 const content = await contentPartsFor([
593 { type: 'text', content: 'What is this?' },
594 {
595 type: 'image',
596 source: { type: 'data', value: 'QUJD', mimeType: 'image/png' },
597 },
598 ])
599​
600 expect(content).toContainEqual({
601 type: 'image_url',
602 image_url: { url: 'data:image/png;base64,QUJD', detail: 'auto' },
603 })
604 })
605​
606 it('passes an already-encoded image data URI through untouched', async () => {
607 const content = await contentPartsFor([
608 { type: 'text', content: 'What is this?' },
609 {
610 type: 'image',
611 source: {
612 type: 'data',
613 value: 'data:image/png;base64,QUJD',
614 mimeType: 'image/png',
615 },
616 },
617 ])
618​
619 expect(content).toContainEqual({
620 type: 'image_url',
621 image_url: { url: 'data:image/png;base64,QUJD', detail: 'auto' },
622 })
623 })
624​
625 it('sends video parts as video_url', async () => {
626 const content = await contentPartsFor([
627 { type: 'text', content: 'Describe this' },
628 {
629 type: 'video',
630 source: { type: 'url', value: 'https://example.com/clip.mp4' },
631 metadata: { fps: 2 },
632 },
633 ])
634​
635 expect(content).toContainEqual({
636 type: 'video_url',
637 video_url: { url: 'https://example.com/clip.mp4', fps: 2 },
638 })
639 })
640​
641 it('sends URL audio as input_audio.url and inline audio as data + format', async () => {
642 const urlContent = await contentPartsFor([
643 { type: 'text', content: 'Transcribe' },
644 {
645 type: 'audio',
646 source: { type: 'url', value: 'https://example.com/clip.mp3' },
647 },
648 ])
649 expect(urlContent).toContainEqual({
650 type: 'input_audio',
651 input_audio: { url: 'https://example.com/clip.mp3' },
652 })
653​
654 const dataContent = await contentPartsFor([
655 { type: 'text', content: 'Transcribe' },
656 {
657 type: 'audio',
658 source: { type: 'data', value: 'QUJD', mimeType: 'audio/mpeg' },
659 },
660 ])
661 expect(dataContent).toContainEqual({
662 type: 'input_audio',
663 input_audio: { data: 'QUJD', format: 'mp3' },
664 })
665 })
666​
667 it('strips a data: prefix from inline audio — Ark takes bare base64', async () => {
668 const content = await contentPartsFor([
669 { type: 'text', content: 'Transcribe' },
670 {
671 type: 'audio',
672 source: {
673 type: 'data',
674 value: 'data:audio/wav;base64,QUJD',
675 mimeType: 'audio/wav',
676 },
677 },
678 ])
679​
680 expect(content).toContainEqual({
681 type: 'input_audio',
682 input_audio: { data: 'QUJD', format: 'wav' },
683 })
684 })
685​
686 it('honours an explicit metadata.format over the mimeType', async () => {
687 const content = await contentPartsFor([
688 { type: 'text', content: 'Transcribe' },
689 {
690 type: 'audio',
691 source: {
692 type: 'data',
693 value: 'QUJD',
694 mimeType: 'application/octet-stream',
695 },
696 metadata: { format: 'flac' },
697 },
698 ])
699​
700 expect(content).toContainEqual({
701 type: 'input_audio',
702 input_audio: { data: 'QUJD', format: 'flac' },
703 })
704 })
705​
706 it('fails loud on inline audio with an unrecognised mimeType', async () => {
707 const chunks = await collect(
708 createBytePlusText('seed-2-0-lite-260428', 'ark-test-key').chatStream({
709 model: 'seed-2-0-lite-260428',
710 messages: [
711 {
712 role: 'user',
713 content: [
714 { type: 'text', content: 'Transcribe' },
715 {
716 type: 'audio',
717 source: {
718 type: 'data',
719 value: 'QUJD',
720 mimeType: 'audio/weird-codec',
721 },
722 },
723 ],
724 },
725 ],
726 logger: testLogger,
727 }),
728 )
729​
730 const runError = chunks.find((c) => c.type === EventType.RUN_ERROR)
731 expect(
732 runError?.type === EventType.RUN_ERROR && runError.message,
733 ).toContain('unrecognised mimeType')
734 })
735 })
736​
737 describe('structured-output gating', () => {
738 it('opts into the native combined tools + schema path only on supported models', () => {
739 expect(
740 createBytePlusText(
741 STRUCTURED_MODEL,
742 'ark-test-key',
743 ).supportsCombinedToolsAndSchema(),
744 ).toBe(true)
745 expect(
746 createBytePlusText(
747 UNSTRUCTURED_MODEL,
748 'ark-test-key',
749 ).supportsCombinedToolsAndSchema(),
750 ).toBe(false)
751 })
752​
753 it('throws from structuredOutput on a model Ark rejects json_schema for', async () => {
754 const adapter = createBytePlusText(UNSTRUCTURED_MODEL, 'ark-test-key')
755​
756 await expect(
757 adapter.structuredOutput({
758 chatOptions: {
759 model: UNSTRUCTURED_MODEL,
760 messages: [{ role: 'user', content: 'Hi' }],
761 logger: testLogger,
762 },
763 outputSchema: { type: 'object', properties: {} },
764 }),
765 ).rejects.toThrow('does not support structured output')
766 })
767​
768 // The two tests above assert the hook's return value and the
769 // structuredOutput* entry points in isolation. This one drives the whole
770 // chat() activity, which is where `supportsCombinedToolsAndSchema()` is
771 // actually consumed (`nativeCombined`, twice, in chat()'s activity), and
772 // pins the end-to-end consequence: on a rejecting model the guard fires
773 // *before* any request, so no schema Ark would 400 on ever leaves the
774 // process. Every structured E2E overrides to a supporting model, so this
775 // path ran nowhere.
776 it('fails before issuing a request when the model rejects json_schema', async () => {
777 const mockCreate = setupMockSdkClient(
778 textStreamChunks(UNSTRUCTURED_MODEL),
779 )
780 const adapter = createBytePlusText(UNSTRUCTURED_MODEL, 'ark-test-key')
781​
782 const chunks: Array<StreamChunk> = []
783 for await (const chunk of chat({
784 adapter,
785 messages: [{ role: 'user', content: 'Say hi' }],
786 outputSchema: { type: 'object', properties: {} },
787 stream: true,
788 })) {
789 chunks.push(chunk)
790 }
791​
792 // Ark has no json_object fallback, so there is nothing to downgrade to.
793 // The request is never made rather than being made without the schema.
794 expect(mockCreate).not.toHaveBeenCalled()
795​
796 // And it surfaces as a named RUN_ERROR, not a raw 400 or silent prose.
797 const runError = chunks.find((c) => c.type === EventType.RUN_ERROR)
798 expect(
799 runError?.type === EventType.RUN_ERROR && runError.message,
800 ).toContain('does not support structured output')
801 })
802​
803 it('does forward response_format into the streaming turns on a supported model', async () => {
804 const mockCreate = setupMockSdkClient(textStreamChunks(STRUCTURED_MODEL))
805 const adapter = createBytePlusText(STRUCTURED_MODEL, 'ark-test-key')
806​
807 for await (const _ of chat({
808 adapter,
809 messages: [{ role: 'user', content: 'Say hi' }],
810 outputSchema: { type: 'object', properties: {} },
811 stream: true,
812 })) {
813 // consume
814 }
815​
816 const request = mockCreate.mock.calls[0]?.[0] as Record<string, unknown>
817 expect(request.response_format).toMatchObject({ type: 'json_schema' })
818 })
819​
820 it('emits RUN_ERROR (not a throw) from structuredOutputStream on those models', async () => {
821 const adapter = createBytePlusText(UNSTRUCTURED_MODEL, 'ark-test-key')
822​
823 const chunks = await collect(
824 adapter.structuredOutputStream({
825 chatOptions: {
826 model: UNSTRUCTURED_MODEL,
827 messages: [{ role: 'user', content: 'Hi' }],
828 logger: testLogger,
829 },
830 outputSchema: { type: 'object', properties: {} },
831 }),
832 )
833​
834 expect(chunks[0]?.type).toBe(EventType.RUN_STARTED)
835 const runError = chunks.find((c) => c.type === EventType.RUN_ERROR)
836 expect(runError?.type === EventType.RUN_ERROR && runError.code).toBe(
837 'unsupported-structured-output',
838 )
839 })
840​
841 it('sends response_format json_schema on a supported model', async () => {
842 const mockCreate = setupMockSdkClient([], {
843 choices: [{ message: { content: '{"city":"Paris"}' } }],
844 })
845 const adapter = createBytePlusText(STRUCTURED_MODEL, 'ark-test-key')
846​
847 const result = await adapter.structuredOutput({
848 chatOptions: {
849 model: STRUCTURED_MODEL,
850 messages: [{ role: 'user', content: 'Where is the Eiffel tower?' }],
851 logger: testLogger,
852 },
853 outputSchema: {
854 type: 'object',
855 properties: { city: { type: 'string' } },
856 required: ['city'],
857 },
858 })
859​
860 expect(result.data).toEqual({ city: 'Paris' })
861 expect(mockCreate.mock.calls[0]?.[0]).toMatchObject({
862 stream: false,
863 response_format: { type: 'json_schema' },
864 })
865 })
866​
867 it('streams structured output on a supported model', async () => {
868 setupMockSdkClient([
869 {
870 id: 'chatcmpl-sos',
871 model: STRUCTURED_MODEL,
872 choices: [{ index: 0, delta: { content: '{"city":' } }],
873 },
874 {
875 id: 'chatcmpl-sos',
876 model: STRUCTURED_MODEL,
877 choices: [
878 {
879 index: 0,
880 delta: { content: '"Paris"}' },
881 finish_reason: 'stop',
882 },
883 ],
884 },
885 ])
886 const adapter = createBytePlusText(STRUCTURED_MODEL, 'ark-test-key')
887​
888 const chunks = await collect(
889 adapter.structuredOutputStream({
890 chatOptions: {
891 model: STRUCTURED_MODEL,
892 messages: [{ role: 'user', content: 'Where is the Eiffel tower?' }],
893 logger: testLogger,
894 },
895 outputSchema: {
896 type: 'object',
897 properties: { city: { type: 'string' } },
898 required: ['city'],
899 },
900 }),
901 )
902​
903 expect(chunks.some((c) => c.type === EventType.RUN_ERROR)).toBe(false)
904 const complete = chunks.find(
905 (c) =>
906 c.type === EventType.CUSTOM &&
907 c.name === 'structured-output.complete',
908 )
909 expect(
910 complete?.type === EventType.CUSTOM &&
911 (complete.value as { object: unknown }).object,
912 ).toEqual({ city: 'Paris' })
913 })
914 })
915})
916​
917/**
918 * The blob is only useful if it survives the whole round-trip through the
919 * chat engine, not just the adapter's own event stream: the server agent loop
920 * builds the continuation message itself, and it reads thinking from a
921 * different field than the client StreamProcessor does. These tests drive the
922 * real `chat()` agent loop so a regression in that hand-off is caught here
923 * rather than against live Ark.
924 */
925describe('encrypted_content through the chat() agent loop', () => {
926 beforeEach(() => {
927 pendingMockCreate = undefined
928 })
929​
930 const lookupWeather: Tool = {
931 name: 'lookup_weather',
932 description: 'Look up the weather for a city',
933 inputSchema: {
934 type: 'object',
935 properties: { city: { type: 'string' } },
936 required: ['city'],
937 },
938 execute: async () => ({ conditions: 'sunny' }),
939 }
940​
941 /**
942 * Turn 1: reasoning deltas → the dedicated encrypted chunk → a tool call.
943 * `text` controls whether the assistant also produces content, which selects
944 * between the finish_reason path and the drain path in the base.
945 */
946 function toolCallTurn(
947 model: string,
948 options: { text: boolean },
949 ): Array<Record<string, unknown>> {
950 return [
951 {
952 id: 'chatcmpl-loop',
953 model,
954 choices: [{ index: 0, delta: { reasoning_content: 'Need the tool.' } }],
955 },
956 {
957 id: 'chatcmpl-loop',
958 model,
959 choices: [
960 {
961 index: 0,
962 delta: {
963 content: '',
964 reasoning_content: '',
965 encrypted_content: ENCRYPTED_BLOB,
966 },
967 },
968 ],
969 },
970 ...(options.text
971 ? [
972 {
973 id: 'chatcmpl-loop',
974 model,
975 choices: [{ index: 0, delta: { content: 'Checking.' } }],
976 },
977 ]
978 : []),
979 {
980 id: 'chatcmpl-loop',
981 model,
982 choices: [
983 {
984 index: 0,
985 delta: {
986 tool_calls: [
987 {
988 index: 0,
989 id: 'call_loop_1',
990 type: 'function',
991 function: {
992 name: 'lookup_weather',
993 arguments: '{"city":"Berlin"}',
994 },
995 },
996 ],
997 },
998 finish_reason: 'tool_calls',
999 },
1000 ],
1001 },
1004​
1005 function finalTurn(model: string): Array<Record<string, unknown>> {
1006 return [
1008 id: 'chatcmpl-loop-2',
1009 model,
1010 choices: [
1012 index: 0,
1013 delta: { content: 'It is sunny in Berlin.' },
1014 finish_reason: 'stop',
1015 },
1016 ],
1017 },
1020​
1021 /**
1022 * Runs a two-request agent loop and returns the request body of the
1023 * continuation call.
1024 */
1025 async function continuationRequest(options: { text: boolean }): Promise<any> {
1026 const model = THINKING_SUMMARY_MODEL
1027 const turns = [
1028 toolCallTurn(model, { text: options.text }),
1029 finalTurn(model),
1031 let call = 0
1032 const mockCreate = vi.fn().mockImplementation(() => {
1033 const chunks = turns[Math.min(call++, turns.length - 1)]!
1034 return Promise.resolve(createAsyncIterable(chunks))
1035 })
1036​
1037 const adapter = _realCreateBytePlusText(model, 'ark-test-key')
1038 ;(adapter as any).client = {
1039 chat: { completions: { create: mockCreate } },
1041​
1042 for await (const _ of chat({
1043 adapter,
1044 messages: [{ role: 'user', content: 'Weather in Berlin?' }],
1045 tools: [lookupWeather],
1046 })) {
1047 // consume the stream
1049​
1050 expect(mockCreate).toHaveBeenCalledTimes(2)
1051 return mockCreate.mock.calls[1]?.[0]
1053​
1054 it('echoes encrypted_content on the continuation request', async () => {
1055 const body = await continuationRequest({ text: true })
1056​
1057 const assistant = body.messages.find(
1058 (m: any) => m.role === 'assistant' && m.tool_calls,
1060 expect(assistant).toBeDefined()
1061 expect(assistant.encrypted_content).toBe(ENCRYPTED_BLOB)
1062 expect(assistant.tool_calls[0].function.name).toBe('lookup_weather')
1063 // The tool result must be there too — otherwise the loop didn't actually
1064 // continue and the assertion above proves nothing about a real 2nd turn.
1065 expect(
1066 body.messages.some(
1067 (m: any) => m.role === 'tool' && m.tool_call_id === 'call_loop_1',
1068 ),
1069 ).toBe(true)
1070 })
1071​
1072 it('echoes it on the drain path too (thinking + tool call, no text)', async () => {
1073 const body = await continuationRequest({ text: false })
1074​
1075 const assistant = body.messages.find(
1076 (m: any) => m.role === 'assistant' && m.tool_calls,
1078 expect(assistant.encrypted_content).toBe(ENCRYPTED_BLOB)
1079 // An assistant turn with only tool calls sends `content: null`, per the
1080 // Chat Completions contract the base implements.
1081 expect(assistant.content).toBeNull()
1082 })
1084​
1085// The adapter stamps `delta` onto a STEP_FINISHED that already carries the
1086// same reasoning text via REASONING_MESSAGE_CONTENT, because chat()'s agent
1087// loop reads thinking only from STEP_FINISHED.delta and drops the whole step —
1088// signature included — when that accumulation is empty.
1090// Nothing double-counts today only because StreamProcessor short-circuits
1091// STEP_FINISHED content once it has seen reasoning events. That guarantee
1092// lives in @tanstack/ai, not here, so without this test a change over there
1093// would double every BytePlus reasoning block in the UI while this package
1094// stayed green.
1095describe('reasoning delta round-trip through StreamProcessor', () => {
1096 it('surfaces the thinking text once, with the signature attached', async () => {
1097 setupMockSdkClient(thinkingStreamChunks(THINKING_SUMMARY_MODEL))
1098 const adapter = createBytePlusText(THINKING_SUMMARY_MODEL, 'ark-test-key')
1099​
1100 const processor = new StreamProcessor()
1101 for await (const chunk of adapter.chatStream({
1102 model: THINKING_SUMMARY_MODEL,
1103 messages: [{ role: 'user', content: 'Say hi' }],
1104 logger: testLogger,
1105 })) {
1106 processor.processChunk(chunk)
1108​
1109 const thinkingParts = processor
1110 .getMessages()
1111 .flatMap((message) => message.parts ?? [])
1112 .filter((part) => part.type === 'thinking')
1113​
1114 expect(thinkingParts).toHaveLength(1)
1115 // Not 'The user wants a greeting.The user wants a greeting.' — the exact
1116 // failure the STEP_FINISHED short-circuit prevents.
1117 expect(thinkingParts[0]).toMatchObject({
1118 content: 'The user wants a greeting.',
1119 signature: ENCRYPTED_BLOB,
1120 })
1121 })