@cloudflare/tanstack-ai 0.0.0 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (127) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +282 -0
  3. package/dist/_tsup-dts-rollup.d.cts +820 -0
  4. package/dist/_tsup-dts-rollup.d.ts +820 -0
  5. package/dist/adapters/anthropic.cjs +13 -0
  6. package/dist/adapters/anthropic.cjs.map +1 -0
  7. package/dist/adapters/anthropic.d.cts +5 -0
  8. package/dist/adapters/anthropic.d.ts +5 -0
  9. package/dist/adapters/anthropic.js +13 -0
  10. package/dist/adapters/anthropic.js.map +1 -0
  11. package/dist/adapters/gemini.cjs +22 -0
  12. package/dist/adapters/gemini.cjs.map +1 -0
  13. package/dist/adapters/gemini.d.cts +14 -0
  14. package/dist/adapters/gemini.d.ts +14 -0
  15. package/dist/adapters/gemini.js +22 -0
  16. package/dist/adapters/gemini.js.map +1 -0
  17. package/dist/adapters/grok.cjs +17 -0
  18. package/dist/adapters/grok.cjs.map +1 -0
  19. package/dist/adapters/grok.d.cts +9 -0
  20. package/dist/adapters/grok.d.ts +9 -0
  21. package/dist/adapters/grok.js +17 -0
  22. package/dist/adapters/grok.js.map +1 -0
  23. package/dist/adapters/openai.cjs +29 -0
  24. package/dist/adapters/openai.cjs.map +1 -0
  25. package/dist/adapters/openai.d.cts +17 -0
  26. package/dist/adapters/openai.d.ts +17 -0
  27. package/dist/adapters/openai.js +29 -0
  28. package/dist/adapters/openai.js.map +1 -0
  29. package/dist/adapters/openrouter.cjs +13 -0
  30. package/dist/adapters/openrouter.cjs.map +1 -0
  31. package/dist/adapters/openrouter.d.cts +7 -0
  32. package/dist/adapters/openrouter.d.ts +7 -0
  33. package/dist/adapters/openrouter.js +13 -0
  34. package/dist/adapters/openrouter.js.map +1 -0
  35. package/dist/adapters/workers-ai-image.cjs +13 -0
  36. package/dist/adapters/workers-ai-image.cjs.map +1 -0
  37. package/dist/adapters/workers-ai-image.d.cts +3 -0
  38. package/dist/adapters/workers-ai-image.d.ts +3 -0
  39. package/dist/adapters/workers-ai-image.js +13 -0
  40. package/dist/adapters/workers-ai-image.js.map +1 -0
  41. package/dist/adapters/workers-ai-summarize.cjs +12 -0
  42. package/dist/adapters/workers-ai-summarize.cjs.map +1 -0
  43. package/dist/adapters/workers-ai-summarize.d.cts +3 -0
  44. package/dist/adapters/workers-ai-summarize.d.ts +3 -0
  45. package/dist/adapters/workers-ai-summarize.js +12 -0
  46. package/dist/adapters/workers-ai-summarize.js.map +1 -0
  47. package/dist/adapters/workers-ai-transcription.cjs +13 -0
  48. package/dist/adapters/workers-ai-transcription.cjs.map +1 -0
  49. package/dist/adapters/workers-ai-transcription.d.cts +3 -0
  50. package/dist/adapters/workers-ai-transcription.d.ts +3 -0
  51. package/dist/adapters/workers-ai-transcription.js +13 -0
  52. package/dist/adapters/workers-ai-transcription.js.map +1 -0
  53. package/dist/adapters/workers-ai-tts.cjs +13 -0
  54. package/dist/adapters/workers-ai-tts.cjs.map +1 -0
  55. package/dist/adapters/workers-ai-tts.d.cts +3 -0
  56. package/dist/adapters/workers-ai-tts.d.ts +3 -0
  57. package/dist/adapters/workers-ai-tts.js +13 -0
  58. package/dist/adapters/workers-ai-tts.js.map +1 -0
  59. package/dist/adapters/workers-ai.cjs +11 -0
  60. package/dist/adapters/workers-ai.cjs.map +1 -0
  61. package/dist/adapters/workers-ai.d.cts +3 -0
  62. package/dist/adapters/workers-ai.d.ts +3 -0
  63. package/dist/adapters/workers-ai.js +11 -0
  64. package/dist/adapters/workers-ai.js.map +1 -0
  65. package/dist/chunk-2AJ6LV2D.js +84 -0
  66. package/dist/chunk-2AJ6LV2D.js.map +1 -0
  67. package/dist/chunk-2VII5BK2.js +42 -0
  68. package/dist/chunk-2VII5BK2.js.map +1 -0
  69. package/dist/chunk-3VQDXJLW.cjs +46 -0
  70. package/dist/chunk-3VQDXJLW.cjs.map +1 -0
  71. package/dist/chunk-4DE2IREA.cjs +8 -0
  72. package/dist/chunk-4DE2IREA.cjs.map +1 -0
  73. package/dist/chunk-7AEFXYJG.js +65 -0
  74. package/dist/chunk-7AEFXYJG.js.map +1 -0
  75. package/dist/chunk-7HSUHP63.cjs +42 -0
  76. package/dist/chunk-7HSUHP63.cjs.map +1 -0
  77. package/dist/chunk-7T4CVHKD.cjs +84 -0
  78. package/dist/chunk-7T4CVHKD.cjs.map +1 -0
  79. package/dist/chunk-BD4CRW3Q.js +389 -0
  80. package/dist/chunk-BD4CRW3Q.js.map +1 -0
  81. package/dist/chunk-BPWGWJJV.cjs +389 -0
  82. package/dist/chunk-BPWGWJJV.cjs.map +1 -0
  83. package/dist/chunk-F5YJMXZR.js +315 -0
  84. package/dist/chunk-F5YJMXZR.js.map +1 -0
  85. package/dist/chunk-GOU66I5T.cjs +315 -0
  86. package/dist/chunk-GOU66I5T.cjs.map +1 -0
  87. package/dist/chunk-IWZJCLOE.cjs +31 -0
  88. package/dist/chunk-IWZJCLOE.cjs.map +1 -0
  89. package/dist/chunk-LBYDBPHY.cjs +109 -0
  90. package/dist/chunk-LBYDBPHY.cjs.map +1 -0
  91. package/dist/chunk-LIUHRGEK.cjs +96 -0
  92. package/dist/chunk-LIUHRGEK.cjs.map +1 -0
  93. package/dist/chunk-M6NETFDR.cjs +48 -0
  94. package/dist/chunk-M6NETFDR.cjs.map +1 -0
  95. package/dist/chunk-O2C4CR57.cjs +54 -0
  96. package/dist/chunk-O2C4CR57.cjs.map +1 -0
  97. package/dist/chunk-PLTFCUMO.js +216 -0
  98. package/dist/chunk-PLTFCUMO.js.map +1 -0
  99. package/dist/chunk-QRHKPL75.js +54 -0
  100. package/dist/chunk-QRHKPL75.js.map +1 -0
  101. package/dist/chunk-RGDUK5KX.cjs +65 -0
  102. package/dist/chunk-RGDUK5KX.cjs.map +1 -0
  103. package/dist/chunk-RQT7M6MU.js +96 -0
  104. package/dist/chunk-RQT7M6MU.js.map +1 -0
  105. package/dist/chunk-U5YJQYLZ.cjs +57 -0
  106. package/dist/chunk-U5YJQYLZ.cjs.map +1 -0
  107. package/dist/chunk-V6TY7KAL.js +8 -0
  108. package/dist/chunk-V6TY7KAL.js.map +1 -0
  109. package/dist/chunk-VTDJEUFS.js +57 -0
  110. package/dist/chunk-VTDJEUFS.js.map +1 -0
  111. package/dist/chunk-XCNU7EEC.js +48 -0
  112. package/dist/chunk-XCNU7EEC.js.map +1 -0
  113. package/dist/chunk-XI2BOYEI.js +109 -0
  114. package/dist/chunk-XI2BOYEI.js.map +1 -0
  115. package/dist/chunk-XKSFDPDY.cjs +216 -0
  116. package/dist/chunk-XKSFDPDY.cjs.map +1 -0
  117. package/dist/chunk-XU7YEPML.js +46 -0
  118. package/dist/chunk-XU7YEPML.js.map +1 -0
  119. package/dist/chunk-YIA5B3QT.js +31 -0
  120. package/dist/chunk-YIA5B3QT.js.map +1 -0
  121. package/dist/index.cjs +97 -0
  122. package/dist/index.cjs.map +1 -0
  123. package/dist/index.d.cts +64 -0
  124. package/dist/index.d.ts +64 -0
  125. package/dist/index.js +97 -0
  126. package/dist/index.js.map +1 -0
  127. package/package.json +119 -10
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Cloudflare, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,282 @@
1
+ # @cloudflare/tanstack-ai
2
+
3
+ Use [TanStack AI](https://tanstack.com/ai) with Cloudflare Workers AI and AI Gateway. Supports chat, image generation, transcription, text-to-speech, and summarization with Workers AI models, plus routing through AI Gateway for OpenAI, Anthropic, Gemini, Grok, and OpenRouter.
4
+
5
+ ## Features
6
+
7
+ - **Workers AI**: Chat, image generation, transcription (Whisper + Deepgram), TTS, and summarization via `env.AI` binding or REST API
8
+ - **AI Gateway**: Route requests to OpenAI, Anthropic, Gemini, Grok, OpenRouter, and Workers AI through Cloudflare's AI Gateway for caching, rate limiting, and unified billing
9
+ - **Flexible Configuration**: Four config modes — plain binding, plain REST, gateway binding, gateway REST
10
+ - **Type-Safe**: Full TypeScript support with model-specific types and type inference
11
+
12
+ ## Installation
13
+
14
+ ```bash
15
+ npm install @cloudflare/tanstack-ai @tanstack/ai
16
+ ```
17
+
18
+ For AI Gateway with third-party providers, install the provider SDKs you need:
19
+
20
+ ```bash
21
+ # For OpenAI
22
+ npm install @tanstack/ai-openai
23
+
24
+ # For Anthropic
25
+ npm install @tanstack/ai-anthropic
26
+
27
+ # For Gemini
28
+ npm install @tanstack/ai-gemini
29
+
30
+ # For Grok
31
+ npm install @tanstack/ai-grok
32
+
33
+ # For OpenRouter
34
+ npm install @tanstack/ai-openrouter @openrouter/sdk
35
+ ```
36
+
37
+ ## Workers AI
38
+
39
+ The simplest way to use AI in a Cloudflare Worker. No API keys needed for Workers AI models when using a binding.
40
+
41
+ ### Chat
42
+
43
+ **Using the binding (recommended):**
44
+
45
+ ```typescript
46
+ import { createWorkersAiChat } from "@cloudflare/tanstack-ai";
47
+ import { chat, toHttpResponse } from "@tanstack/ai";
48
+
49
+ const adapter = createWorkersAiChat("@cf/meta/llama-3.3-70b-instruct-fp8-fast", {
50
+ binding: env.AI,
51
+ });
52
+
53
+ const response = chat({
54
+ adapter,
55
+ stream: true,
56
+ messages: [{ role: "user", content: "Hello!" }],
57
+ });
58
+
59
+ return toHttpResponse(response);
60
+ ```
61
+
62
+ **Using REST credentials:**
63
+
64
+ ```typescript
65
+ const adapter = createWorkersAiChat("@cf/meta/llama-3.3-70b-instruct-fp8-fast", {
66
+ accountId: "your-account-id",
67
+ apiKey: "your-api-key",
68
+ });
69
+ ```
70
+
71
+ ### Image Generation
72
+
73
+ ```typescript
74
+ import { createWorkersAiImage } from "@cloudflare/tanstack-ai";
75
+ import { generateImage } from "@tanstack/ai";
76
+
77
+ const adapter = createWorkersAiImage("@cf/stabilityai/stable-diffusion-xl-base-1.0", {
78
+ binding: env.AI,
79
+ });
80
+
81
+ const result = await generateImage({ adapter, prompt: "a cat in space" });
82
+ // result.images[0].b64Json — base64-encoded image
83
+ ```
84
+
85
+ ### Transcription (Speech-to-Text)
86
+
87
+ ```typescript
88
+ import { createWorkersAiTranscription } from "@cloudflare/tanstack-ai";
89
+ import { generateTranscription } from "@tanstack/ai";
90
+
91
+ const adapter = createWorkersAiTranscription("@cf/openai/whisper-large-v3-turbo", {
92
+ binding: env.AI,
93
+ });
94
+
95
+ const result = await generateTranscription({ adapter, audio: audioArrayBuffer });
96
+ // result.text — the transcribed text
97
+ // result.segments — timed segments (whisper-large-v3-turbo)
98
+ ```
99
+
100
+ Supported models: `@cf/openai/whisper`, `@cf/openai/whisper-tiny-en`, `@cf/openai/whisper-large-v3-turbo`, `@cf/deepgram/nova-3`
101
+
102
+ ### Text-to-Speech
103
+
104
+ ```typescript
105
+ import { createWorkersAiTts } from "@cloudflare/tanstack-ai";
106
+ import { generateSpeech } from "@tanstack/ai";
107
+
108
+ const adapter = createWorkersAiTts("@cf/deepgram/aura-1", {
109
+ binding: env.AI,
110
+ });
111
+
112
+ const result = await generateSpeech({ adapter, text: "Hello world" });
113
+ // result.audio — base64-encoded audio
114
+ ```
115
+
116
+ ### Summarization
117
+
118
+ ```typescript
119
+ import { createWorkersAiSummarize } from "@cloudflare/tanstack-ai";
120
+ import { summarize } from "@tanstack/ai";
121
+
122
+ const adapter = createWorkersAiSummarize("@cf/facebook/bart-large-cnn", {
123
+ binding: env.AI,
124
+ });
125
+
126
+ const result = await summarize({ adapter, text: "Long article here..." });
127
+ // result.summary
128
+ ```
129
+
130
+ > **Coming soon:** Workers AI embedding adapter is implemented internally but waiting on TanStack AI to add `embed()` / `embedMany()` activity functions and a `BaseEmbeddingAdapter` base class.
131
+
132
+ ## AI Gateway
133
+
134
+ Route AI requests through Cloudflare's AI Gateway for caching, rate limiting, and unified billing. Supports Workers AI and third-party providers.
135
+
136
+ ### Configuration
137
+
138
+ **Using AI Binding (Recommended for Cloudflare Workers):**
139
+
140
+ ```typescript
141
+ const adapter = createOpenAiChat("gpt-4o", {
142
+ binding: env.AI.gateway("my-gateway-id"),
143
+ });
144
+ ```
145
+
146
+ **Using Credentials:**
147
+
148
+ ```typescript
149
+ const adapter = createOpenAiChat("gpt-4o", {
150
+ accountId: "your-account-id",
151
+ gatewayId: "your-gateway-id",
152
+ cfApiKey: "your-cf-api-key", // Optional: if gateway is authenticated
153
+ apiKey: "provider-api-key", // Optional: provider API key if not using Unified Billing or BYOK
154
+ });
155
+ ```
156
+
157
+ **Cache options (binding and credentials):**
158
+
159
+ ```typescript
160
+ const adapter = createOpenAiChat("gpt-4o", {
161
+ binding: env.AI.gateway("my-gateway-id"),
162
+ skipCache: false,
163
+ cacheTtl: 3600,
164
+ customCacheKey: "my-key",
165
+ metadata: { user: "test" },
166
+ });
167
+ ```
168
+
169
+ ### Workers AI through Gateway
170
+
171
+ ```typescript
172
+ const adapter = createWorkersAiChat("@cf/meta/llama-3.3-70b-instruct-fp8-fast", {
173
+ binding: env.AI.gateway("my-gateway-id"),
174
+ apiKey: env.WORKERS_AI_TOKEN,
175
+ });
176
+ ```
177
+
178
+ ### Third-Party Providers through Gateway
179
+
180
+ ```typescript
181
+ import {
182
+ createOpenAiChat,
183
+ createAnthropicChat,
184
+ createGeminiChat,
185
+ createGrokChat,
186
+ createOpenRouterChat,
187
+ } from "@cloudflare/tanstack-ai";
188
+
189
+ // OpenAI
190
+ const openai = createOpenAiChat("gpt-4o", config);
191
+
192
+ // Anthropic
193
+ const anthropic = createAnthropicChat("claude-sonnet-4-5", config);
194
+
195
+ // Gemini (credentials only)
196
+ const gemini = createGeminiChat("gemini-2.0-flash", {
197
+ accountId: env.CF_ACCOUNT_ID,
198
+ gatewayId: env.CF_AIG_ID,
199
+ cfApiKey: env.CF_AIG_TOKEN,
200
+ });
201
+
202
+ // Grok
203
+ const grok = createGrokChat("grok-4", config);
204
+
205
+ // OpenRouter (access any model via OpenRouter)
206
+ const openrouter = createOpenRouterChat("openai/gpt-4o", config);
207
+ ```
208
+
209
+ ## Supported Capabilities
210
+
211
+ | Provider | Chat | Summarize | Image Gen | Transcription | TTS | Video |
212
+ | -------------- | ---- | --------- | --------- | ------------- | --- | ----- |
213
+ | **Workers AI** | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ |
214
+ | **OpenAI** | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
215
+ | **Gemini** | ✅ | ✅ | ✅ | ❌ | ✅ | ❌ |
216
+ | **Anthropic** | ✅ | ✅ | ❌ | ❌ | ❌ | ❌ |
217
+ | **Grok** | ✅ | ✅ | ✅ | ❌ | ❌ | ❌ |
218
+ | **OpenRouter** | ✅ | ✅ | ✅ | ❌ | ❌ | ❌ |
219
+
220
+ ### All Functions
221
+
222
+ **Workers AI:**
223
+
224
+ - `createWorkersAiChat(model, config)` -- chat and structured output
225
+ - `createWorkersAiImage(model, config)` -- image generation
226
+ - `createWorkersAiTranscription(model, config)` -- speech-to-text (Whisper, Deepgram nova-3)
227
+ - `createWorkersAiTts(model, config)` -- text-to-speech (Deepgram aura-1)
228
+ - `createWorkersAiSummarize(model, config)` -- summarization (BART-large-CNN)
229
+
230
+ **OpenAI:**
231
+
232
+ - `createOpenAiChat(model, config)`
233
+ - `createOpenAiSummarize(model, config)`
234
+ - `createOpenAiImage(model, config)`
235
+ - `createOpenAiTranscription(model, config)`
236
+ - `createOpenAiTts(model, config)`
237
+ - `createOpenAiVideo(model, config)`
238
+
239
+ **Anthropic:**
240
+
241
+ - `createAnthropicChat(model, config)`
242
+ - `createAnthropicSummarize(model, config)`
243
+
244
+ **Gemini:**
245
+
246
+ - `createGeminiChat(model, config)` -- credentials only, no binding support
247
+ - `createGeminiSummarize(model, config)` -- credentials only, no binding support
248
+ - `createGeminiImage(model, config)` -- credentials only, no binding support
249
+ - `createGeminiTts(model, config)` -- text-to-speech (experimental), credentials only
250
+
251
+ > **Note:** Gemini adapters use the Google GenAI SDK's `httpOptions.baseUrl` and `httpOptions.headers` to route through the gateway, rather than the custom fetch approach used by other providers. Binding config is not supported — only credentials. See [googleapis/js-genai#999](https://github.com/googleapis/js-genai/issues/999) for the upstream issue tracking custom `fetch` support.
252
+
253
+ **Grok:**
254
+
255
+ - `createGrokChat(model, config)`
256
+ - `createGrokSummarize(model, config)`
257
+ - `createGrokImage(model, config)`
258
+
259
+ **OpenRouter:**
260
+
261
+ - `createOpenRouterChat(model, config)` -- access any model via OpenRouter
262
+ - `createOpenRouterSummarize(model, config)`
263
+ - `createOpenRouterImage(model, config)`
264
+
265
+ ## Workers AI Configuration Modes
266
+
267
+ Workers AI supports four configuration modes:
268
+
269
+ | Mode | Config | Description |
270
+ | --------------- | --------------------------------- | ------------------------------ |
271
+ | Plain binding | `{ binding: env.AI }` | Direct access, no gateway |
272
+ | Plain REST | `{ accountId, apiKey }` | REST API, no gateway |
273
+ | Gateway binding | `{ binding: env.AI.gateway(id) }` | Through AI Gateway via binding |
274
+ | Gateway REST | `{ accountId, gatewayId, ... }` | Through AI Gateway via REST |
275
+
276
+ Third-party providers (OpenAI, Anthropic, Gemini, Grok, OpenRouter) only support the gateway modes.
277
+
278
+ ## Links
279
+
280
+ - [TanStack AI Documentation](https://tanstack.com/ai)
281
+ - [Cloudflare Workers AI](https://developers.cloudflare.com/workers-ai/)
282
+ - [Cloudflare AI Gateway](https://developers.cloudflare.com/ai-gateway/)