Open History to see the last commit
1import { describe, expect, it, vi } from 'vitest'
2import { chat } from '@tanstack/ai'
3import { OpenAITextAdapter } from '../src/adapters/text'
4import type { StreamChunk } from '@tanstack/ai'
5
6function mockResponsesStream(): AsyncIterable<Record<string, unknown>> {
7 return {
8 // eslint-disable-next-line @typescript-eslint/require-await
9 async *[Symbol.asyncIterator]() {
10 yield {
11 type: 'response.created',
12 response: { id: 'r1', model: 'gpt-5.5', status: 'in_progress' },
13 }
14 yield {
15 type: 'response.completed',
16 response: {
17 id: 'r1',
18 model: 'gpt-5.5',
19 status: 'completed',
20 output: [],
21 usage: { input_tokens: 1, output_tokens: 1, total_tokens: 2 },
22 },
23 }
24 },
25 }
26}
27
28function withMockClient(create: ReturnType<typeof vi.fn>) {
29 const adapter = new OpenAITextAdapter({ apiKey: 'k' }, 'gpt-5.5')
30 ;(adapter as unknown as { client: unknown }).client = {
31 responses: { create },
32 }
33 return adapter
34}
35
36async function drain(iterable: AsyncIterable<unknown>) {
37 for await (const _ of iterable) {
38 // consume
39 }
40}
41
42describe('openai file content source', () => {
43 it('maps an openai file handle to input_image.file_id on the Responses API', async () => {
44 const create = vi.fn().mockResolvedValueOnce(mockResponsesStream())
45 const adapter = withMockClient(create)
46
47 await drain(
48 chat({
49 adapter,
50 messages: [
51 {
52 role: 'user',
53 content: [
54 { type: 'text', content: 'look' },
55 {
56 type: 'image',
57 source: {
58 type: 'file',
59 value: 'file-openai-abc',
60 provider: 'openai',
61 },
62 },
63 ],
64 },
65 ],
66 }),
67 )
68
69 const [payload] = create.mock.calls[0]!
70 const userItem = payload.input.find(
71 (item: any) => item.type === 'message' && item.role === 'user',
72 )
73 const imageContent = userItem.content.find(
74 (c: any) => c.type === 'input_image',
75 )
76 expect(imageContent.file_id).toBe('file-openai-abc')
77 expect(imageContent.image_url).toBeUndefined()
78 })
79
80 it('maps a document file handle to input_file.file_id', async () => {
81 const create = vi.fn().mockResolvedValueOnce(mockResponsesStream())
82 const adapter = withMockClient(create)
83
84 await drain(
85 chat({
86 adapter,
87 messages: [
88 {
89 role: 'user',
90 content: [
91 { type: 'text', content: 'summarize' },
92 {
93 type: 'document',
94 source: {
95 type: 'file',
96 value: 'file-openai-pdf',
97 provider: 'openai',
98 },
99 },
100 ],
101 },
102 ],
103 }),
104 )
105
106 const [payload] = create.mock.calls[0]!
107 const userItem = payload.input.find(
108 (item: any) => item.type === 'message' && item.role === 'user',
109 )
110 const fileContent = userItem.content.find(
111 (c: any) => c.type === 'input_file',
112 )
113 expect(fileContent.file_id).toBe('file-openai-pdf')
114 })
115
116 it('rejects inline document data that is not a PDF', async () => {
117 const create = vi.fn().mockResolvedValueOnce(mockResponsesStream())
118 const adapter = withMockClient(create)
119
120 const chunks: Array<StreamChunk> = []
121 for await (const chunk of chat({
122 adapter,
123 messages: [
124 {
125 role: 'user',
126 content: [
127 {
128 type: 'document',
129 source: {
130 type: 'data',
131 value: 'AAAA',
132 mimeType: 'application/pdf',
133 },
134 },
135 ],
136 },
137 ],
138 })) {
139 chunks.push(chunk)
140 }
141
142 const runError = chunks.find((c) => c.type === 'RUN_ERROR')
143 expect(runError).toBeDefined()
144 if (runError?.type === 'RUN_ERROR') {
145 expect(runError.message).toMatch(/application\/pdf/)
146 }
147 expect(create).not.toHaveBeenCalled()
148 })
149
150 it('errors when a foreign provider file handle reaches the openai adapter', async () => {
151 const create = vi.fn().mockResolvedValueOnce(mockResponsesStream())
152 const adapter = withMockClient(create)
153
154 const chunks: Array<StreamChunk> = []
155 for await (const chunk of chat({
156 adapter,
157 messages: [
158 {
159 role: 'user',
160 content: [
161 {
162 type: 'image',
163 source: {
164 type: 'file',
165 value: 'files/gemini-xyz',
166 provider: 'gemini',
167 },
168 },
169 ],
170 },
171 ],
172 })) {
173 chunks.push(chunk)
174 }
175
176 const runError = chunks.find((c) => c.type === 'RUN_ERROR')
177 expect(runError).toBeDefined()
178 if (runError?.type === 'RUN_ERROR') {
179 expect(runError.message).toMatch(/openai/)
180 }
181 })
182})