Open History to see the last commit
1/**
2 * Type Safety Test
3 *
4 * This file demonstrates that the discriminated union types work correctly.
5 * When you select an adapter, TypeScript should only show models for that adapter.
6 */
8import { AI } from "../packages/ai/src";
9import { OpenAIAdapter } from "../packages/ai-openai/src";
10import { AnthropicAdapter } from "../packages/ai-anthropic/src";
11import { GeminiAdapter } from "../packages/ai-gemini/src";
13const ai = new AI({
14 adapters: {
15 openai: new OpenAIAdapter({
16 apiKey: process.env.OPENAI_API_KEY!,
17 }),
18 anthropic: new AnthropicAdapter({
19 apiKey: process.env.ANTHROPIC_API_KEY!,
20 }),
21 gemini: new GeminiAdapter({
22 apiKey: process.env.GEMINI_API_KEY!,
23 }),
24 },
25});
27async function testTypeSafety() {
28 // ✓ Valid: OpenAI adapter with OpenAI model
29 await ai.chat({
30 adapter: "openai",
31 model: "gpt-4", // TypeScript should autocomplete only OpenAI models here
32 messages: [{ role: "user", content: "Hello" }],
33 });
35 // ✓ Valid: Anthropic adapter with Anthropic model
36 await ai.chat({
37 adapter: "anthropic",
38 model: "claude-3-5-sonnet-20241022", // TypeScript should autocomplete only Anthropic models here
39 messages: [{ role: "user", content: "Hello" }],
40 });
42 // ✓ Valid: Gemini adapter with Gemini model
43 await ai.chat({
44 adapter: "gemini",
45 model: "gemini-1.5-pro", // TypeScript should autocomplete only Gemini models here
46 messages: [{ role: "user", content: "Hello" }],
47 });
49 // ✗ INVALID: This should show a TypeScript error
50 // Uncommenting this should cause a type error because claude-3 is not an OpenAI model
51 /*
52 await ai.chat({
53 adapter: "openai",
54 model: "claude-3-5-sonnet-20241022", // Type error!
55 messages: [{ role: "user", content: "Hello" }],
56 });
57 */
59 // ✓ Valid: Fallbacks with correct adapter-model pairs
60 await ai.chat({
61 adapter: "openai",
62 model: "gpt-4",
63 fallbacks: [
64 { adapter: "anthropic", model: "claude-3-5-sonnet-20241022" }, // ✓
65 { adapter: "gemini", model: "gemini-1.5-pro" }, // ✓
66 ],
67 messages: [{ role: "user", content: "Hello" }],
68 });
70 // ✗ INVALID: This should show a TypeScript error in fallbacks
71 // Uncommenting this should cause a type error
72 /*
73 await ai.chat({
74 adapter: "openai",
75 model: "gpt-4",
76 fallbacks: [
77 { adapter: "anthropic", model: "gpt-3.5-turbo" }, // Type error! gpt-3.5-turbo is not an Anthropic model
78 ],
79 messages: [{ role: "user", content: "Hello" }],
80 });
81 */
83 // ✓ Valid: Fallback-only mode with correct adapter-model pairs
84 await ai.chat({
85 fallbacks: [
86 { adapter: "openai", model: "gpt-4" },
87 { adapter: "anthropic", model: "claude-3-5-sonnet-20241022" },
88 { adapter: "gemini", model: "gemini-1.5-pro" },
89 ],
90 messages: [{ role: "user", content: "Hello" }],
91 });
93 console.log("✓ All type-safe examples compiled successfully!");
94}
96// Test discriminated union - TypeScript should narrow the type based on adapter
97function testDiscriminatedUnion(
98 options:
99 | { adapter: "openai"; model: "gpt-4" | "gpt-3.5-turbo" }
100 | { adapter: "anthropic"; model: "claude-3-5-sonnet-20241022" | "claude-3-opus-20240229" }
101) {
102 if (options.adapter === "openai") {
103 // TypeScript knows model is "gpt-4" | "gpt-3.5-turbo" here
104 const model: "gpt-4" | "gpt-3.5-turbo" = options.model;
105 console.log("OpenAI model:", model);
106 } else {
107 // TypeScript knows model is Anthropic models here
108 const model: "claude-3-5-sonnet-20241022" | "claude-3-opus-20240229" = options.model;
109 console.log("Anthropic model:", model);
110 }
111}
113testTypeSafety().catch(console.error);