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

visual-error-examples.ts

254 lines6.6 KB

1/**
2 * VISUAL DEMONSTRATION OF TYPE SAFETY ERRORS
3 *
4 * This file shows exactly what TypeScript errors you'll see
5 * when trying to use incompatible adapters and models.
6 *
7 * Uncomment the examples to see the errors in your IDE!
8 */
9
10import { AI } from "../packages/ai/src";
11import { OpenAIAdapter } from "../packages/ai-openai/src";
12import { AnthropicAdapter } from "../packages/ai-anthropic/src";
14const ai = new AI({
15 adapters: {
16 openai: new OpenAIAdapter({ apiKey: "test" }),
17 anthropic: new AnthropicAdapter({ apiKey: "test" }),
18 },
19});
21// ============================================
22// EXAMPLE 1: Using GPT model with Anthropic
23// ============================================
25/*
26ai.chat({
27 adapter: "anthropic",
28 model: "gpt-4",
29 // ^^^^^^
30 // TypeScript Error:
31 // Type '"gpt-4"' is not assignable to type
32 // '"claude-3-5-sonnet-20241022" | "claude-3-5-sonnet-20240620" |
33 // "claude-3-opus-20240229" | "claude-3-sonnet-20240229" |
34 // "claude-3-haiku-20240307" | "claude-2.1" | "claude-2.0" |
35 // "claude-instant-1.2"'
36 messages: [],
37});
38*/
40// ============================================
41// EXAMPLE 2: Using Claude model with OpenAI
42// ============================================
44/*
45ai.chat({
46 adapter: "openai",
47 model: "claude-3-5-sonnet-20241022",
48 // ^^^^^^^^^^^^^^^^^^^^^^^^^^^
49 // TypeScript Error:
50 // Type '"claude-3-5-sonnet-20241022"' is not assignable to type
51 // '"gpt-4" | "gpt-4-turbo" | "gpt-4-turbo-preview" | "gpt-4o" |
52 // "gpt-4o-mini" | "gpt-3.5-turbo" | "gpt-3.5-turbo-16k" |
53 // "gpt-3.5-turbo-instruct" | "text-embedding-ada-002" |
54 // "text-embedding-3-small" | "text-embedding-3-large"'
55 messages: [],
56});
57*/
59// ============================================
60// EXAMPLE 3: Typo in model name
61// ============================================
63/*
64ai.chat({
65 adapter: "openai",
66 model: "gpt-5", // Typo! Should be gpt-4 or gpt-4o
67 // ^^^^^^
68 // TypeScript Error:
69 // Type '"gpt-5"' is not assignable to type
70 // '"gpt-4" | "gpt-4-turbo" | "gpt-4-turbo-preview" | "gpt-4o" | ...'
71 messages: [],
72});
73*/
75// ============================================
76// EXAMPLE 4: Wrong adapter name
77// ============================================
79/*
80ai.chat({
81 adapter: "openai-gpt4", // Typo! Should be "openai"
82 // ^^^^^^^^^^^^^
83 // TypeScript Error:
84 // Argument of type '"openai-gpt4"' is not assignable to parameter
85 // Expected: "openai" | "anthropic"
86 model: "gpt-4",
87 messages: [],
88});
89*/
91// ============================================
92// EXAMPLE 5: Embedding model with chat
93// ============================================
95/*
96// This is technically valid TypeScript (embedding models are in the list)
97// but semantically wrong. You could add additional validation if needed.
98ai.chat({
99 adapter: "openai",
100 model: "text-embedding-ada-002", // This is an embedding model, not chat!
101 messages: [],
102});
103*/
105// ============================================
106// βœ… VALID EXAMPLES - These work fine
107// ============================================
109// OpenAI with GPT models
110ai.chat({
111 adapter: "openai",
112 model: "gpt-4", // βœ…
113 messages: [],
114});
116ai.chat({
117 adapter: "openai",
118 model: "gpt-4o", // βœ…
119 messages: [],
120});
122ai.chat({
123 adapter: "openai",
124 model: "gpt-3.5-turbo", // βœ…
125 messages: [],
126});
128// Anthropic with Claude models
129ai.chat({
130 adapter: "anthropic",
131 model: "claude-3-5-sonnet-20241022", // βœ…
132 messages: [],
133});
135ai.chat({
136 adapter: "anthropic",
137 model: "claude-3-opus-20240229", // βœ…
138 messages: [],
139});
141ai.chat({
142 adapter: "anthropic",
143 model: "claude-3-haiku-20240307", // βœ…
144 messages: [],
145});
147// ============================================
148// 🎯 IDE AUTOCOMPLETE VISUAL
149// ============================================
151/*
152When you type this in your IDE:
154 ai.chat({
155 adapter: "openai",
156 model: "gpt-β–ˆ
157 ^^^^
158 Your IDE shows a dropdown with:
159 βœ“ gpt-4
160 βœ“ gpt-4-turbo
161 βœ“ gpt-4-turbo-preview
162 βœ“ gpt-4o
163 βœ“ gpt-4o-mini
164 βœ“ gpt-3.5-turbo
165 βœ“ gpt-3.5-turbo-16k
166 βœ“ gpt-3.5-turbo-instruct
167 βœ“ text-embedding-ada-002
168 βœ“ text-embedding-3-small
169 βœ“ text-embedding-3-large
170 })
172When you change to Anthropic:
174 ai.chat({
175 adapter: "anthropic",
176 model: "claude-β–ˆ
177 ^^^^^^^
178 Your IDE shows a dropdown with:
179 βœ“ claude-3-5-sonnet-20241022
180 βœ“ claude-3-5-sonnet-20240620
181 βœ“ claude-3-opus-20240229
182 βœ“ claude-3-sonnet-20240229
183 βœ“ claude-3-haiku-20240307
184 βœ“ claude-2.1
185 βœ“ claude-2.0
186 βœ“ claude-instant-1.2
187 })
188*/
190// ============================================
191// πŸ“ COMPARISON: Before vs After
192// ============================================
194/*
195BEFORE (Runtime Error):
196 await ai.chat({
197 provider: "anthropic",
198 model: "gpt-4"
199 });
200 // ❌ Runtime Error (in production!):
201 // Error: Model 'gpt-4' not found for provider 'anthropic'
203AFTER (Compile Error):
204 await ai.chat({
205 adapter: "anthropic",
206 model: "gpt-4"
207 });
208 // ❌ Compile Error (in your editor!):
209 // Type '"gpt-4"' is not assignable to type 'claude-...'
210 //
211 // You see the error IMMEDIATELY while coding
212 // Your build fails if you try to deploy
213 // Your CI/CD catches it before production
214*/
216// ============================================
217// πŸš€ REAL WORLD SCENARIO
218// ============================================
220/*
221Imagine you're refactoring and switching from OpenAI to Anthropic:
223STEP 1: Change the adapter
224 ai.chat({
225 adapter: "anthropic", // Changed from "openai"
226 model: "gpt-4", // ← TypeScript IMMEDIATELY flags this!
227 messages: []
228 });
230STEP 2: TypeScript tells you to fix it
231 Error: Type '"gpt-4"' is not assignable to type '"claude-3-5-sonnet-20241022" | ...'
233STEP 3: You fix it
234 ai.chat({
235 adapter: "anthropic",
236 model: "claude-3-5-sonnet-20241022", // βœ… Fixed!
237 messages: []
238 });
240Without type safety:
241 - You might miss updating the model
242 - It compiles successfully
243 - It deploys to production
244 - It crashes when a user makes a request
245 - You get paged at 3 AM 😴
247With type safety:
248 - TypeScript catches it immediately
249 - You fix it before committing
250 - It never reaches production
251 - You sleep peacefully 😊
252*/
254export { };