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

files-source.test.ts

186 lines4.9 KB

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 }
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
35​
36async function drain(iterable: AsyncIterable<unknown>) {
37 for await (const _ of iterable) {
38 // consume
39 }
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 threadId: 'thread-1',
50 adapter,
51 messages: [
52 {
53 role: 'user',
54 content: [
55 { type: 'text', content: 'look' },
56 {
57 type: 'image',
58 source: {
59 type: 'file',
60 value: 'file-openai-abc',
61 provider: 'openai',
62 },
63 },
64 ],
65 },
66 ],
67 }),
68 )
69​
70 const [payload] = create.mock.calls[0]!
71 const userItem = payload.input.find(
72 (item: any) => item.type === 'message' && item.role === 'user',
73 )
74 const imageContent = userItem.content.find(
75 (c: any) => c.type === 'input_image',
76 )
77 expect(imageContent.file_id).toBe('file-openai-abc')
78 expect(imageContent.image_url).toBeUndefined()
79 })
80​
81 it('maps a document file handle to input_file.file_id', async () => {
82 const create = vi.fn().mockResolvedValueOnce(mockResponsesStream())
83 const adapter = withMockClient(create)
84​
85 await drain(
86 chat({
87 threadId: 'thread-1',
88 adapter,
89 messages: [
90 {
91 role: 'user',
92 content: [
93 { type: 'text', content: 'summarize' },
94 {
95 type: 'document',
96 source: {
97 type: 'file',
98 value: 'file-openai-pdf',
99 provider: 'openai',
100 },
101 },
102 ],
103 },
104 ],
105 }),
106 )
107​
108 const [payload] = create.mock.calls[0]!
109 const userItem = payload.input.find(
110 (item: any) => item.type === 'message' && item.role === 'user',
111 )
112 const fileContent = userItem.content.find(
113 (c: any) => c.type === 'input_file',
114 )
115 expect(fileContent.file_id).toBe('file-openai-pdf')
116 })
117​
118 it('rejects inline document data that is not a PDF', async () => {
119 const create = vi.fn().mockResolvedValueOnce(mockResponsesStream())
120 const adapter = withMockClient(create)
121​
122 const chunks: Array<StreamChunk> = []
123 for await (const chunk of chat({
124 threadId: 'thread-1',
125 adapter,
126 messages: [
127 {
128 role: 'user',
129 content: [
130 {
131 type: 'document',
132 source: {
133 type: 'data',
134 value: 'AAAA',
135 mimeType: 'application/pdf',
136 },
137 },
138 ],
139 },
140 ],
141 })) {
142 chunks.push(chunk)
143 }
144​
145 const runError = chunks.find((c) => c.type === 'RUN_ERROR')
146 expect(runError).toBeDefined()
147 if (runError?.type === 'RUN_ERROR') {
148 expect(runError.message).toMatch(/application\/pdf/)
149 }
150 expect(create).not.toHaveBeenCalled()
151 })
152​
153 it('errors when a foreign provider file handle reaches the openai adapter', async () => {
154 const create = vi.fn().mockResolvedValueOnce(mockResponsesStream())
155 const adapter = withMockClient(create)
156​
157 const chunks: Array<StreamChunk> = []
158 for await (const chunk of chat({
159 threadId: 'thread-1',
160 adapter,
161 messages: [
162 {
163 role: 'user',
164 content: [
165 {
166 type: 'image',
167 source: {
168 type: 'file',
169 value: 'files/gemini-xyz',
170 provider: 'gemini',
171 },
172 },
173 ],
174 },
175 ],
176 })) {
177 chunks.push(chunk)
178 }
179​
180 const runError = chunks.find((c) => c.type === 'RUN_ERROR')
181 expect(runError).toBeDefined()
182 if (runError?.type === 'RUN_ERROR') {
183 expect(runError.message).toMatch(/openai/)
184 }
185 })
186})