@cloudflare/tanstack-ai 0.1.2 → 0.1.4

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 (70) hide show
  1. package/README.md +42 -0
  2. package/dist/_tsup-dts-rollup.d.cts +13 -4
  3. package/dist/_tsup-dts-rollup.d.ts +13 -4
  4. package/dist/adapters/anthropic.cjs +3 -3
  5. package/dist/adapters/anthropic.js +2 -2
  6. package/dist/adapters/grok.cjs +3 -3
  7. package/dist/adapters/grok.js +2 -2
  8. package/dist/adapters/openai.cjs +3 -3
  9. package/dist/adapters/openai.js +2 -2
  10. package/dist/adapters/openrouter.cjs +3 -3
  11. package/dist/adapters/openrouter.js +2 -2
  12. package/dist/adapters/workers-ai-image.cjs +3 -3
  13. package/dist/adapters/workers-ai-image.js +2 -2
  14. package/dist/adapters/workers-ai-summarize.cjs +3 -3
  15. package/dist/adapters/workers-ai-summarize.js +2 -2
  16. package/dist/adapters/workers-ai-transcription.cjs +3 -3
  17. package/dist/adapters/workers-ai-transcription.js +2 -2
  18. package/dist/adapters/workers-ai-tts.cjs +3 -3
  19. package/dist/adapters/workers-ai-tts.js +2 -2
  20. package/dist/adapters/workers-ai.cjs +3 -3
  21. package/dist/adapters/workers-ai.js +2 -2
  22. package/dist/{chunk-LG2I3ANT.cjs → chunk-32U73O6E.cjs} +49 -33
  23. package/dist/chunk-32U73O6E.cjs.map +1 -0
  24. package/dist/{chunk-ZHTFBZXJ.cjs → chunk-3PJHW45X.cjs} +6 -6
  25. package/dist/{chunk-ZHTFBZXJ.cjs.map → chunk-3PJHW45X.cjs.map} +1 -1
  26. package/dist/{chunk-T5KPTTLK.js → chunk-DG46OSFY.js} +2 -2
  27. package/dist/{chunk-IDPLTBQX.cjs → chunk-H24HFVUJ.cjs} +6 -6
  28. package/dist/{chunk-IDPLTBQX.cjs.map → chunk-H24HFVUJ.cjs.map} +1 -1
  29. package/dist/{chunk-JK5ZFOZZ.js → chunk-I2MCLFJ5.js} +2 -2
  30. package/dist/{chunk-OKU3GB5P.js → chunk-IPEZXCDR.js} +2 -2
  31. package/dist/{chunk-ARLHMZ5B.cjs → chunk-KDVHOUEP.cjs} +6 -6
  32. package/dist/{chunk-ARLHMZ5B.cjs.map → chunk-KDVHOUEP.cjs.map} +1 -1
  33. package/dist/{chunk-UWBO33IG.cjs → chunk-KW27GIQL.cjs} +5 -5
  34. package/dist/{chunk-UWBO33IG.cjs.map → chunk-KW27GIQL.cjs.map} +1 -1
  35. package/dist/{chunk-7KZQWFUE.cjs → chunk-MMTLH53H.cjs} +3 -3
  36. package/dist/chunk-MMTLH53H.cjs.map +1 -0
  37. package/dist/{chunk-DSBHOQQ4.js → chunk-MOAWHNNP.js} +2 -2
  38. package/dist/chunk-MOAWHNNP.js.map +1 -0
  39. package/dist/{chunk-3KUHT3K6.js → chunk-NSWLVANP.js} +46 -30
  40. package/dist/chunk-NSWLVANP.js.map +1 -0
  41. package/dist/{chunk-Y7W7NDVS.js → chunk-OGAH3FYW.js} +2 -2
  42. package/dist/{chunk-2QN5XFI2.js → chunk-QMRONSLN.js} +2 -2
  43. package/dist/{chunk-5A2F2QCL.js → chunk-RWLCEKT6.js} +2 -2
  44. package/dist/{chunk-O7MBRPCW.js → chunk-S5QBHI6O.js} +26 -29
  45. package/dist/chunk-S5QBHI6O.js.map +1 -0
  46. package/dist/{chunk-ICHJ2G63.cjs → chunk-TG4UQPGB.cjs} +3 -3
  47. package/dist/{chunk-ICHJ2G63.cjs.map → chunk-TG4UQPGB.cjs.map} +1 -1
  48. package/dist/{chunk-7SKFZ4SU.cjs → chunk-U2PMR3PT.cjs} +5 -5
  49. package/dist/{chunk-7SKFZ4SU.cjs.map → chunk-U2PMR3PT.cjs.map} +1 -1
  50. package/dist/{chunk-IMURYJRF.cjs → chunk-U5NL4EM4.cjs} +33 -36
  51. package/dist/chunk-U5NL4EM4.cjs.map +1 -0
  52. package/dist/{chunk-AYTKQRE4.cjs → chunk-XFQ3QESX.cjs} +6 -6
  53. package/dist/{chunk-AYTKQRE4.cjs.map → chunk-XFQ3QESX.cjs.map} +1 -1
  54. package/dist/{chunk-DZZVI4W5.js → chunk-ZJMMBD6H.js} +2 -2
  55. package/dist/index.cjs +11 -11
  56. package/dist/index.js +10 -10
  57. package/package.json +11 -10
  58. package/dist/chunk-3KUHT3K6.js.map +0 -1
  59. package/dist/chunk-7KZQWFUE.cjs.map +0 -1
  60. package/dist/chunk-DSBHOQQ4.js.map +0 -1
  61. package/dist/chunk-IMURYJRF.cjs.map +0 -1
  62. package/dist/chunk-LG2I3ANT.cjs.map +0 -1
  63. package/dist/chunk-O7MBRPCW.js.map +0 -1
  64. /package/dist/{chunk-T5KPTTLK.js.map → chunk-DG46OSFY.js.map} +0 -0
  65. /package/dist/{chunk-JK5ZFOZZ.js.map → chunk-I2MCLFJ5.js.map} +0 -0
  66. /package/dist/{chunk-OKU3GB5P.js.map → chunk-IPEZXCDR.js.map} +0 -0
  67. /package/dist/{chunk-Y7W7NDVS.js.map → chunk-OGAH3FYW.js.map} +0 -0
  68. /package/dist/{chunk-2QN5XFI2.js.map → chunk-QMRONSLN.js.map} +0 -0
  69. /package/dist/{chunk-5A2F2QCL.js.map → chunk-RWLCEKT6.js.map} +0 -0
  70. /package/dist/{chunk-DZZVI4W5.js.map → chunk-ZJMMBD6H.js.map} +0 -0
package/README.md CHANGED
@@ -68,6 +68,39 @@ const adapter = createWorkersAiChat("@cf/meta/llama-4-scout-17b-16e-instruct", {
68
68
  });
69
69
  ```
70
70
 
71
+ ### Vision (Image Inputs)
72
+
73
+ Send images to vision-capable chat models:
74
+
75
+ ```typescript
76
+ const adapter = createWorkersAiChat("@cf/meta/llama-4-scout-17b-16e-instruct", {
77
+ accountId: "your-account-id",
78
+ apiKey: "your-api-key",
79
+ });
80
+
81
+ const response = chat({
82
+ adapter,
83
+ stream: true,
84
+ messages: [
85
+ {
86
+ role: "user",
87
+ content: [
88
+ { type: "text", content: "What's in this image?" },
89
+ { type: "image", source: { type: "data", value: base64String, mimeType: "image/png" } },
90
+ ],
91
+ },
92
+ ],
93
+ });
94
+ ```
95
+
96
+ URL sources are also supported:
97
+
98
+ ```typescript
99
+ { type: "image", source: { type: "url", value: "https://example.com/photo.jpg" } }
100
+ ```
101
+
102
+ Works with all configuration modes (binding, REST, and AI Gateway).
103
+
71
104
  ### Image Generation
72
105
 
73
106
  ```typescript
@@ -275,6 +308,15 @@ Workers AI supports four configuration modes:
275
308
 
276
309
  Third-party providers (OpenAI, Anthropic, Gemini, Grok, OpenRouter) only support the gateway modes.
277
310
 
311
+ All Workers AI config modes also accept `sessionAffinity` to route requests with the same key to the same backend replica for prefix-cache optimization:
312
+
313
+ ```typescript
314
+ const adapter = createWorkersAiChat("@cf/meta/llama-3.3-70b-instruct-fp8-fast", {
315
+ binding: env.AI,
316
+ sessionAffinity: "my-unique-session-id",
317
+ });
318
+ ```
319
+
278
320
  ## Links
279
321
 
280
322
  - [TanStack AI Documentation](https://tanstack.com/ai)
@@ -2,6 +2,7 @@ import type { AiModels as AiModels_2 } from '@cloudflare/workers-types';
2
2
  import { ANTHROPIC_MODELS } from '@tanstack/ai-anthropic';
3
3
  import { AnthropicChatModel } from '@tanstack/ai-anthropic';
4
4
  import { AnthropicSummarizeAdapter } from '@tanstack/ai-anthropic';
5
+ import { AnthropicTextAdapter } from '@tanstack/ai-anthropic';
5
6
  import type { AnyTextAdapter } from '@tanstack/ai';
6
7
  import type { BaseAiTextEmbeddings as BaseAiTextEmbeddings_2 } from '@cloudflare/workers-types';
7
8
  import type { BaseAiTextGeneration as BaseAiTextGeneration_2 } from '@cloudflare/workers-types';
@@ -146,7 +147,7 @@ export declare function collectStream(stream: ReadableStream<Uint8Array>): Promi
146
147
  * Since AnthropicTextConfig extends the Anthropic SDK's ClientOptions,
147
148
  * we can inject the gateway fetch directly — no subclassing needed.
148
149
  */
149
- declare function createAnthropicChat(model: AnthropicChatModel, config: AnthropicGatewayConfig): AnyTextAdapter;
150
+ declare function createAnthropicChat(model: AnthropicChatModel, config: AnthropicGatewayConfig): AnthropicTextAdapter<AnthropicChatModel>;
150
151
  export { createAnthropicChat }
151
152
  export { createAnthropicChat as createAnthropicChat_alias_1 }
152
153
 
@@ -330,7 +331,9 @@ export { createOpenRouterSummarize as createOpenRouterSummarize_alias_1 }
330
331
  * request parameters are extracted from the JSON body, matching Workers AI's
331
332
  * `binding.run(model, inputs)` calling convention.
332
333
  */
333
- export declare function createWorkersAiBindingFetch(binding: WorkersAiBinding): typeof fetch;
334
+ export declare function createWorkersAiBindingFetch(binding: WorkersAiBinding, options?: {
335
+ extraHeaders?: Record<string, string>;
336
+ }): typeof fetch;
334
337
 
335
338
  declare function createWorkersAiChat(model: WorkersAiTextModel, config: WorkersAiAdapterConfig): WorkersAiTextAdapter<WorkersAiTextModel>;
336
339
  export { createWorkersAiChat }
@@ -601,9 +604,15 @@ export declare function validateWorkersAiConfig(config: WorkersAiAdapterConfig):
601
604
  * upstream provider), distinct from `cfApiKey` (used in the `cf-aig-authorization`
602
605
  * header for authenticated gateways).
603
606
  */
604
- declare type WorkersAiAdapterConfig = WorkersAiDirectBindingConfig | WorkersAiDirectCredentialsConfig | (AiGatewayAdapterConfig & {
607
+ declare type WorkersAiAdapterConfig = (WorkersAiDirectBindingConfig | WorkersAiDirectCredentialsConfig | (AiGatewayAdapterConfig & {
605
608
  apiKey?: string;
606
- });
609
+ })) & {
610
+ /**
611
+ * Session affinity key for prefix-cache optimization.
612
+ * Routes requests with the same key to the same backend replica.
613
+ */
614
+ sessionAffinity?: string;
615
+ };
607
616
  export { WorkersAiAdapterConfig }
608
617
  export { WorkersAiAdapterConfig as WorkersAiAdapterConfig_alias_1 }
609
618
 
@@ -2,6 +2,7 @@ import type { AiModels as AiModels_2 } from '@cloudflare/workers-types';
2
2
  import { ANTHROPIC_MODELS } from '@tanstack/ai-anthropic';
3
3
  import { AnthropicChatModel } from '@tanstack/ai-anthropic';
4
4
  import { AnthropicSummarizeAdapter } from '@tanstack/ai-anthropic';
5
+ import { AnthropicTextAdapter } from '@tanstack/ai-anthropic';
5
6
  import type { AnyTextAdapter } from '@tanstack/ai';
6
7
  import type { BaseAiTextEmbeddings as BaseAiTextEmbeddings_2 } from '@cloudflare/workers-types';
7
8
  import type { BaseAiTextGeneration as BaseAiTextGeneration_2 } from '@cloudflare/workers-types';
@@ -146,7 +147,7 @@ export declare function collectStream(stream: ReadableStream<Uint8Array>): Promi
146
147
  * Since AnthropicTextConfig extends the Anthropic SDK's ClientOptions,
147
148
  * we can inject the gateway fetch directly — no subclassing needed.
148
149
  */
149
- declare function createAnthropicChat(model: AnthropicChatModel, config: AnthropicGatewayConfig): AnyTextAdapter;
150
+ declare function createAnthropicChat(model: AnthropicChatModel, config: AnthropicGatewayConfig): AnthropicTextAdapter<AnthropicChatModel>;
150
151
  export { createAnthropicChat }
151
152
  export { createAnthropicChat as createAnthropicChat_alias_1 }
152
153
 
@@ -330,7 +331,9 @@ export { createOpenRouterSummarize as createOpenRouterSummarize_alias_1 }
330
331
  * request parameters are extracted from the JSON body, matching Workers AI's
331
332
  * `binding.run(model, inputs)` calling convention.
332
333
  */
333
- export declare function createWorkersAiBindingFetch(binding: WorkersAiBinding): typeof fetch;
334
+ export declare function createWorkersAiBindingFetch(binding: WorkersAiBinding, options?: {
335
+ extraHeaders?: Record<string, string>;
336
+ }): typeof fetch;
334
337
 
335
338
  declare function createWorkersAiChat(model: WorkersAiTextModel, config: WorkersAiAdapterConfig): WorkersAiTextAdapter<WorkersAiTextModel>;
336
339
  export { createWorkersAiChat }
@@ -601,9 +604,15 @@ export declare function validateWorkersAiConfig(config: WorkersAiAdapterConfig):
601
604
  * upstream provider), distinct from `cfApiKey` (used in the `cf-aig-authorization`
602
605
  * header for authenticated gateways).
603
606
  */
604
- declare type WorkersAiAdapterConfig = WorkersAiDirectBindingConfig | WorkersAiDirectCredentialsConfig | (AiGatewayAdapterConfig & {
607
+ declare type WorkersAiAdapterConfig = (WorkersAiDirectBindingConfig | WorkersAiDirectCredentialsConfig | (AiGatewayAdapterConfig & {
605
608
  apiKey?: string;
606
- });
609
+ })) & {
610
+ /**
611
+ * Session affinity key for prefix-cache optimization.
612
+ * Routes requests with the same key to the same backend replica.
613
+ */
614
+ sessionAffinity?: string;
615
+ };
607
616
  export { WorkersAiAdapterConfig }
608
617
  export { WorkersAiAdapterConfig as WorkersAiAdapterConfig_alias_1 }
609
618
 
@@ -2,12 +2,12 @@
2
2
 
3
3
 
4
4
 
5
- var _chunk7KZQWFUEcjs = require('../chunk-7KZQWFUE.cjs');
6
- require('../chunk-IMURYJRF.cjs');
5
+ var _chunkMMTLH53Hcjs = require('../chunk-MMTLH53H.cjs');
6
+ require('../chunk-U5NL4EM4.cjs');
7
7
  require('../chunk-4DE2IREA.cjs');
8
8
 
9
9
 
10
10
 
11
11
 
12
- exports.ANTHROPIC_MODELS = _chunk7KZQWFUEcjs.ANTHROPIC_MODELS; exports.createAnthropicChat = _chunk7KZQWFUEcjs.createAnthropicChat; exports.createAnthropicSummarize = _chunk7KZQWFUEcjs.createAnthropicSummarize;
12
+ exports.ANTHROPIC_MODELS = _chunkMMTLH53Hcjs.ANTHROPIC_MODELS; exports.createAnthropicChat = _chunkMMTLH53Hcjs.createAnthropicChat; exports.createAnthropicSummarize = _chunkMMTLH53Hcjs.createAnthropicSummarize;
13
13
  //# sourceMappingURL=anthropic.cjs.map
@@ -2,8 +2,8 @@ import {
2
2
  ANTHROPIC_MODELS,
3
3
  createAnthropicChat,
4
4
  createAnthropicSummarize
5
- } from "../chunk-DSBHOQQ4.js";
6
- import "../chunk-O7MBRPCW.js";
5
+ } from "../chunk-MOAWHNNP.js";
6
+ import "../chunk-S5QBHI6O.js";
7
7
  import "../chunk-V6TY7KAL.js";
8
8
  export {
9
9
  ANTHROPIC_MODELS,
@@ -4,8 +4,8 @@
4
4
 
5
5
 
6
6
 
7
- var _chunkUWBO33IGcjs = require('../chunk-UWBO33IG.cjs');
8
- require('../chunk-IMURYJRF.cjs');
7
+ var _chunkKW27GIQLcjs = require('../chunk-KW27GIQL.cjs');
8
+ require('../chunk-U5NL4EM4.cjs');
9
9
  require('../chunk-4DE2IREA.cjs');
10
10
 
11
11
 
@@ -13,5 +13,5 @@ require('../chunk-4DE2IREA.cjs');
13
13
 
14
14
 
15
15
 
16
- exports.GROK_CHAT_MODELS = _chunkUWBO33IGcjs.GROK_CHAT_MODELS; exports.GROK_IMAGE_MODELS = _chunkUWBO33IGcjs.GROK_IMAGE_MODELS; exports.createGrokChat = _chunkUWBO33IGcjs.createGrokChat; exports.createGrokImage = _chunkUWBO33IGcjs.createGrokImage; exports.createGrokSummarize = _chunkUWBO33IGcjs.createGrokSummarize;
16
+ exports.GROK_CHAT_MODELS = _chunkKW27GIQLcjs.GROK_CHAT_MODELS; exports.GROK_IMAGE_MODELS = _chunkKW27GIQLcjs.GROK_IMAGE_MODELS; exports.createGrokChat = _chunkKW27GIQLcjs.createGrokChat; exports.createGrokImage = _chunkKW27GIQLcjs.createGrokImage; exports.createGrokSummarize = _chunkKW27GIQLcjs.createGrokSummarize;
17
17
  //# sourceMappingURL=grok.cjs.map
@@ -4,8 +4,8 @@ import {
4
4
  createGrokChat,
5
5
  createGrokImage,
6
6
  createGrokSummarize
7
- } from "../chunk-Y7W7NDVS.js";
8
- import "../chunk-O7MBRPCW.js";
7
+ } from "../chunk-OGAH3FYW.js";
8
+ import "../chunk-S5QBHI6O.js";
9
9
  import "../chunk-V6TY7KAL.js";
10
10
  export {
11
11
  GROK_CHAT_MODELS,
@@ -10,8 +10,8 @@
10
10
 
11
11
 
12
12
 
13
- var _chunkICHJ2G63cjs = require('../chunk-ICHJ2G63.cjs');
14
- require('../chunk-IMURYJRF.cjs');
13
+ var _chunkTG4UQPGBcjs = require('../chunk-TG4UQPGB.cjs');
14
+ require('../chunk-U5NL4EM4.cjs');
15
15
  require('../chunk-4DE2IREA.cjs');
16
16
 
17
17
 
@@ -25,5 +25,5 @@ require('../chunk-4DE2IREA.cjs');
25
25
 
26
26
 
27
27
 
28
- exports.OPENAI_CHAT_MODELS = _chunkICHJ2G63cjs.OPENAI_CHAT_MODELS; exports.OPENAI_IMAGE_MODELS = _chunkICHJ2G63cjs.OPENAI_IMAGE_MODELS; exports.OPENAI_TRANSCRIPTION_MODELS = _chunkICHJ2G63cjs.OPENAI_TRANSCRIPTION_MODELS; exports.OPENAI_TTS_MODELS = _chunkICHJ2G63cjs.OPENAI_TTS_MODELS; exports.OPENAI_VIDEO_MODELS = _chunkICHJ2G63cjs.OPENAI_VIDEO_MODELS; exports.createOpenAiChat = _chunkICHJ2G63cjs.createOpenAiChat; exports.createOpenAiImage = _chunkICHJ2G63cjs.createOpenAiImage; exports.createOpenAiSummarize = _chunkICHJ2G63cjs.createOpenAiSummarize; exports.createOpenAiTranscription = _chunkICHJ2G63cjs.createOpenAiTranscription; exports.createOpenAiTts = _chunkICHJ2G63cjs.createOpenAiTts; exports.createOpenAiVideo = _chunkICHJ2G63cjs.createOpenAiVideo;
28
+ exports.OPENAI_CHAT_MODELS = _chunkTG4UQPGBcjs.OPENAI_CHAT_MODELS; exports.OPENAI_IMAGE_MODELS = _chunkTG4UQPGBcjs.OPENAI_IMAGE_MODELS; exports.OPENAI_TRANSCRIPTION_MODELS = _chunkTG4UQPGBcjs.OPENAI_TRANSCRIPTION_MODELS; exports.OPENAI_TTS_MODELS = _chunkTG4UQPGBcjs.OPENAI_TTS_MODELS; exports.OPENAI_VIDEO_MODELS = _chunkTG4UQPGBcjs.OPENAI_VIDEO_MODELS; exports.createOpenAiChat = _chunkTG4UQPGBcjs.createOpenAiChat; exports.createOpenAiImage = _chunkTG4UQPGBcjs.createOpenAiImage; exports.createOpenAiSummarize = _chunkTG4UQPGBcjs.createOpenAiSummarize; exports.createOpenAiTranscription = _chunkTG4UQPGBcjs.createOpenAiTranscription; exports.createOpenAiTts = _chunkTG4UQPGBcjs.createOpenAiTts; exports.createOpenAiVideo = _chunkTG4UQPGBcjs.createOpenAiVideo;
29
29
  //# sourceMappingURL=openai.cjs.map
@@ -10,8 +10,8 @@ import {
10
10
  createOpenAiTranscription,
11
11
  createOpenAiTts,
12
12
  createOpenAiVideo
13
- } from "../chunk-5A2F2QCL.js";
14
- import "../chunk-O7MBRPCW.js";
13
+ } from "../chunk-RWLCEKT6.js";
14
+ import "../chunk-S5QBHI6O.js";
15
15
  import "../chunk-V6TY7KAL.js";
16
16
  export {
17
17
  OPENAI_CHAT_MODELS,
@@ -2,12 +2,12 @@
2
2
 
3
3
 
4
4
 
5
- var _chunk7SKFZ4SUcjs = require('../chunk-7SKFZ4SU.cjs');
6
- require('../chunk-IMURYJRF.cjs');
5
+ var _chunkU2PMR3PTcjs = require('../chunk-U2PMR3PT.cjs');
6
+ require('../chunk-U5NL4EM4.cjs');
7
7
  require('../chunk-4DE2IREA.cjs');
8
8
 
9
9
 
10
10
 
11
11
 
12
- exports.createOpenRouterChat = _chunk7SKFZ4SUcjs.createOpenRouterChat; exports.createOpenRouterImage = _chunk7SKFZ4SUcjs.createOpenRouterImage; exports.createOpenRouterSummarize = _chunk7SKFZ4SUcjs.createOpenRouterSummarize;
12
+ exports.createOpenRouterChat = _chunkU2PMR3PTcjs.createOpenRouterChat; exports.createOpenRouterImage = _chunkU2PMR3PTcjs.createOpenRouterImage; exports.createOpenRouterSummarize = _chunkU2PMR3PTcjs.createOpenRouterSummarize;
13
13
  //# sourceMappingURL=openrouter.cjs.map
@@ -2,8 +2,8 @@ import {
2
2
  createOpenRouterChat,
3
3
  createOpenRouterImage,
4
4
  createOpenRouterSummarize
5
- } from "../chunk-JK5ZFOZZ.js";
6
- import "../chunk-O7MBRPCW.js";
5
+ } from "../chunk-I2MCLFJ5.js";
6
+ import "../chunk-S5QBHI6O.js";
7
7
  import "../chunk-V6TY7KAL.js";
8
8
  export {
9
9
  createOpenRouterChat,
@@ -1,13 +1,13 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
2
 
3
3
 
4
- var _chunkZHTFBZXJcjs = require('../chunk-ZHTFBZXJ.cjs');
4
+ var _chunk3PJHW45Xcjs = require('../chunk-3PJHW45X.cjs');
5
5
  require('../chunk-3VQDXJLW.cjs');
6
6
  require('../chunk-7HSUHP63.cjs');
7
- require('../chunk-IMURYJRF.cjs');
7
+ require('../chunk-U5NL4EM4.cjs');
8
8
  require('../chunk-4DE2IREA.cjs');
9
9
 
10
10
 
11
11
 
12
- exports.WorkersAiImageAdapter = _chunkZHTFBZXJcjs.WorkersAiImageAdapter; exports.createWorkersAiImage = _chunkZHTFBZXJcjs.createWorkersAiImage;
12
+ exports.WorkersAiImageAdapter = _chunk3PJHW45Xcjs.WorkersAiImageAdapter; exports.createWorkersAiImage = _chunk3PJHW45Xcjs.createWorkersAiImage;
13
13
  //# sourceMappingURL=workers-ai-image.cjs.map
@@ -1,10 +1,10 @@
1
1
  import {
2
2
  WorkersAiImageAdapter,
3
3
  createWorkersAiImage
4
- } from "../chunk-DZZVI4W5.js";
4
+ } from "../chunk-ZJMMBD6H.js";
5
5
  import "../chunk-XU7YEPML.js";
6
6
  import "../chunk-2VII5BK2.js";
7
- import "../chunk-O7MBRPCW.js";
7
+ import "../chunk-S5QBHI6O.js";
8
8
  import "../chunk-V6TY7KAL.js";
9
9
  export {
10
10
  WorkersAiImageAdapter,
@@ -1,12 +1,12 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
2
 
3
3
 
4
- var _chunkIDPLTBQXcjs = require('../chunk-IDPLTBQX.cjs');
4
+ var _chunkH24HFVUJcjs = require('../chunk-H24HFVUJ.cjs');
5
5
  require('../chunk-7HSUHP63.cjs');
6
- require('../chunk-IMURYJRF.cjs');
6
+ require('../chunk-U5NL4EM4.cjs');
7
7
  require('../chunk-4DE2IREA.cjs');
8
8
 
9
9
 
10
10
 
11
- exports.WorkersAiSummarizeAdapter = _chunkIDPLTBQXcjs.WorkersAiSummarizeAdapter; exports.createWorkersAiSummarize = _chunkIDPLTBQXcjs.createWorkersAiSummarize;
11
+ exports.WorkersAiSummarizeAdapter = _chunkH24HFVUJcjs.WorkersAiSummarizeAdapter; exports.createWorkersAiSummarize = _chunkH24HFVUJcjs.createWorkersAiSummarize;
12
12
  //# sourceMappingURL=workers-ai-summarize.cjs.map
@@ -1,9 +1,9 @@
1
1
  import {
2
2
  WorkersAiSummarizeAdapter,
3
3
  createWorkersAiSummarize
4
- } from "../chunk-OKU3GB5P.js";
4
+ } from "../chunk-IPEZXCDR.js";
5
5
  import "../chunk-2VII5BK2.js";
6
- import "../chunk-O7MBRPCW.js";
6
+ import "../chunk-S5QBHI6O.js";
7
7
  import "../chunk-V6TY7KAL.js";
8
8
  export {
9
9
  WorkersAiSummarizeAdapter,
@@ -1,13 +1,13 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
2
 
3
3
 
4
- var _chunkAYTKQRE4cjs = require('../chunk-AYTKQRE4.cjs');
4
+ var _chunkXFQ3QESXcjs = require('../chunk-XFQ3QESX.cjs');
5
5
  require('../chunk-3VQDXJLW.cjs');
6
6
  require('../chunk-7HSUHP63.cjs');
7
- require('../chunk-IMURYJRF.cjs');
7
+ require('../chunk-U5NL4EM4.cjs');
8
8
  require('../chunk-4DE2IREA.cjs');
9
9
 
10
10
 
11
11
 
12
- exports.WorkersAiTranscriptionAdapter = _chunkAYTKQRE4cjs.WorkersAiTranscriptionAdapter; exports.createWorkersAiTranscription = _chunkAYTKQRE4cjs.createWorkersAiTranscription;
12
+ exports.WorkersAiTranscriptionAdapter = _chunkXFQ3QESXcjs.WorkersAiTranscriptionAdapter; exports.createWorkersAiTranscription = _chunkXFQ3QESXcjs.createWorkersAiTranscription;
13
13
  //# sourceMappingURL=workers-ai-transcription.cjs.map
@@ -1,10 +1,10 @@
1
1
  import {
2
2
  WorkersAiTranscriptionAdapter,
3
3
  createWorkersAiTranscription
4
- } from "../chunk-T5KPTTLK.js";
4
+ } from "../chunk-DG46OSFY.js";
5
5
  import "../chunk-XU7YEPML.js";
6
6
  import "../chunk-2VII5BK2.js";
7
- import "../chunk-O7MBRPCW.js";
7
+ import "../chunk-S5QBHI6O.js";
8
8
  import "../chunk-V6TY7KAL.js";
9
9
  export {
10
10
  WorkersAiTranscriptionAdapter,
@@ -1,13 +1,13 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
2
 
3
3
 
4
- var _chunkARLHMZ5Bcjs = require('../chunk-ARLHMZ5B.cjs');
4
+ var _chunkKDVHOUEPcjs = require('../chunk-KDVHOUEP.cjs');
5
5
  require('../chunk-3VQDXJLW.cjs');
6
6
  require('../chunk-7HSUHP63.cjs');
7
- require('../chunk-IMURYJRF.cjs');
7
+ require('../chunk-U5NL4EM4.cjs');
8
8
  require('../chunk-4DE2IREA.cjs');
9
9
 
10
10
 
11
11
 
12
- exports.WorkersAiTTSAdapter = _chunkARLHMZ5Bcjs.WorkersAiTTSAdapter; exports.createWorkersAiTts = _chunkARLHMZ5Bcjs.createWorkersAiTts;
12
+ exports.WorkersAiTTSAdapter = _chunkKDVHOUEPcjs.WorkersAiTTSAdapter; exports.createWorkersAiTts = _chunkKDVHOUEPcjs.createWorkersAiTts;
13
13
  //# sourceMappingURL=workers-ai-tts.cjs.map
@@ -1,10 +1,10 @@
1
1
  import {
2
2
  WorkersAiTTSAdapter,
3
3
  createWorkersAiTts
4
- } from "../chunk-2QN5XFI2.js";
4
+ } from "../chunk-QMRONSLN.js";
5
5
  import "../chunk-XU7YEPML.js";
6
6
  import "../chunk-2VII5BK2.js";
7
- import "../chunk-O7MBRPCW.js";
7
+ import "../chunk-S5QBHI6O.js";
8
8
  import "../chunk-V6TY7KAL.js";
9
9
  export {
10
10
  WorkersAiTTSAdapter,
@@ -1,11 +1,11 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
2
 
3
3
 
4
- var _chunkLG2I3ANTcjs = require('../chunk-LG2I3ANT.cjs');
5
- require('../chunk-IMURYJRF.cjs');
4
+ var _chunk32U73O6Ecjs = require('../chunk-32U73O6E.cjs');
5
+ require('../chunk-U5NL4EM4.cjs');
6
6
  require('../chunk-4DE2IREA.cjs');
7
7
 
8
8
 
9
9
 
10
- exports.WorkersAiTextAdapter = _chunkLG2I3ANTcjs.WorkersAiTextAdapter; exports.createWorkersAiChat = _chunkLG2I3ANTcjs.createWorkersAiChat;
10
+ exports.WorkersAiTextAdapter = _chunk32U73O6Ecjs.WorkersAiTextAdapter; exports.createWorkersAiChat = _chunk32U73O6Ecjs.createWorkersAiChat;
11
11
  //# sourceMappingURL=workers-ai.cjs.map
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  WorkersAiTextAdapter,
3
3
  createWorkersAiChat
4
- } from "../chunk-3KUHT3K6.js";
5
- import "../chunk-O7MBRPCW.js";
4
+ } from "../chunk-NSWLVANP.js";
5
+ import "../chunk-S5QBHI6O.js";
6
6
  import "../chunk-V6TY7KAL.js";
7
7
  export {
8
8
  WorkersAiTextAdapter,
@@ -4,7 +4,7 @@
4
4
 
5
5
 
6
6
 
7
- var _chunkIMURYJRFcjs = require('./chunk-IMURYJRF.cjs');
7
+ var _chunkU5NL4EM4cjs = require('./chunk-U5NL4EM4.cjs');
8
8
 
9
9
 
10
10
  var _chunk4DE2IREAcjs = require('./chunk-4DE2IREA.cjs');
@@ -15,51 +15,70 @@ var _chunk4DE2IREAcjs = require('./chunk-4DE2IREA.cjs');
15
15
  var _adapters = require('@tanstack/ai/adapters');
16
16
  var _openai = require('openai'); var _openai2 = _interopRequireDefault(_openai);
17
17
  function buildWorkersAiClient(config) {
18
- _chunkIMURYJRFcjs.validateWorkersAiConfig.call(void 0, config);
19
- if (_chunkIMURYJRFcjs.isDirectBindingConfig.call(void 0, config)) {
18
+ _chunkU5NL4EM4cjs.validateWorkersAiConfig.call(void 0, config);
19
+ const sessionHeaders = config.sessionAffinity ? { "x-session-affinity": config.sessionAffinity } : void 0;
20
+ if (_chunkU5NL4EM4cjs.isDirectBindingConfig.call(void 0, config)) {
20
21
  return new (0, _openai2.default)({
21
22
  apiKey: "unused",
22
- fetch: _chunkIMURYJRFcjs.createWorkersAiBindingFetch.call(void 0, config.binding)
23
+ fetch: _chunkU5NL4EM4cjs.createWorkersAiBindingFetch.call(void 0,
24
+ config.binding,
25
+ sessionHeaders ? { extraHeaders: sessionHeaders } : void 0
26
+ )
23
27
  });
24
28
  }
25
- if (_chunkIMURYJRFcjs.isDirectCredentialsConfig.call(void 0, config)) {
29
+ if (_chunkU5NL4EM4cjs.isDirectCredentialsConfig.call(void 0, config)) {
26
30
  return new (0, _openai2.default)({
27
31
  baseURL: `https://api.cloudflare.com/client/v4/accounts/${config.accountId}/ai/v1`,
28
- apiKey: config.apiKey
32
+ apiKey: config.apiKey,
33
+ defaultHeaders: sessionHeaders
29
34
  });
30
35
  }
31
36
  const gatewayConfig = config;
32
37
  return new (0, _openai2.default)({
33
- fetch: _chunkIMURYJRFcjs.createGatewayFetch.call(void 0, "workers-ai", gatewayConfig),
38
+ fetch: _chunkU5NL4EM4cjs.createGatewayFetch.call(void 0, "workers-ai", gatewayConfig, sessionHeaders),
34
39
  apiKey: _nullishCoalesce(gatewayConfig.apiKey, () => ( "unused"))
35
40
  });
36
41
  }
37
42
  function extractTextContent(content) {
38
43
  if (content === null) return "";
39
44
  if (typeof content === "string") return content;
40
- return content.filter((p) => p.type === "text").map((p) => p.content || "").join("");
45
+ return content.flatMap((p) => p.type === "text" ? [p.content] : []).join("");
46
+ }
47
+ function convertContentPart(part) {
48
+ switch (part.type) {
49
+ case "text":
50
+ if (part.content) {
51
+ return { type: "text", text: part.content };
52
+ }
53
+ return void 0;
54
+ case "image": {
55
+ let url;
56
+ if (part.source.type === "data") {
57
+ url = part.source.value.startsWith("data:") ? part.source.value : `data:${part.source.mimeType};base64,${part.source.value}`;
58
+ } else {
59
+ url = part.source.value;
60
+ }
61
+ return { type: "image_url", image_url: { url } };
62
+ }
63
+ default:
64
+ console.warn(
65
+ `[@cloudflare/tanstack-ai] Unsupported content part type "${part.type}" \u2014 skipping`
66
+ );
67
+ return void 0;
68
+ }
41
69
  }
42
70
  function buildUserContent(content) {
43
71
  if (content === null) return "";
44
72
  if (typeof content === "string") return content;
45
- const hasImages = content.some((p) => p.type === "image_url" || p.type === "image");
73
+ const hasImages = content.some((p) => p.type === "image");
46
74
  if (!hasImages) {
47
- return content.filter((p) => p.type === "text").map((p) => p.content || "").join("");
75
+ return content.flatMap((p) => p.type === "text" ? [p.content] : []).join("");
48
76
  }
49
77
  const parts = [];
50
78
  for (const part of content) {
51
- if (part.type === "text" && part.content) {
52
- parts.push({ type: "text", text: part.content });
53
- } else if (part.type === "image_url" && part.content) {
54
- parts.push({
55
- type: "image_url",
56
- image_url: { url: part.content }
57
- });
58
- } else if (part.type === "image_url" && part.image_url) {
59
- parts.push({
60
- type: "image_url",
61
- image_url: part.image_url
62
- });
79
+ const converted = convertContentPart(part);
80
+ if (converted) {
81
+ parts.push(converted);
63
82
  }
64
83
  }
65
84
  return parts;
@@ -127,8 +146,8 @@ function buildOpenAITools(tools) {
127
146
  }
128
147
  }));
129
148
  }
130
- function generateId(prefix) {
131
- return `${prefix}-${crypto.randomUUID()}`;
149
+ function generateId(prefix = "chatcmpl") {
150
+ return `${prefix}-${crypto.randomUUID().replace(/-/g, "").slice(0, 16)}`;
132
151
  }
133
152
  var WorkersAiTextAdapter = class extends _adapters.BaseTextAdapter {
134
153
  constructor(model, config) {
@@ -142,14 +161,14 @@ var WorkersAiTextAdapter = class extends _adapters.BaseTextAdapter {
142
161
  const openAIMessages = buildOpenAIMessages(systemPrompts, messages);
143
162
  const openAITools = buildOpenAITools(tools);
144
163
  const timestamp = Date.now();
145
- const runId = generateId("workers-ai");
146
- const messageId = generateId("workers-ai");
164
+ const runId = generateId();
165
+ const messageId = generateId();
147
166
  let hasEmittedRunStarted = false;
148
167
  let hasEmittedTextMessageStart = false;
149
168
  let accumulatedContent = "";
150
169
  let hasEmittedStepStarted = false;
151
170
  let accumulatedReasoning = "";
152
- const stepId = generateId("workers-ai-step");
171
+ const stepId = generateId();
153
172
  let hasReceivedFinishReason = false;
154
173
  const toolCallsInProgress = /* @__PURE__ */ new Map();
155
174
  try {
@@ -261,7 +280,7 @@ var WorkersAiTextAdapter = class extends _adapters.BaseTextAdapter {
261
280
  };
262
281
  }
263
282
  const delta = choice.delta;
264
- const reasoningContent = delta.reasoning_content;
283
+ const reasoningContent = _nullishCoalesce(delta.reasoning_content, () => ( delta.reasoning));
265
284
  if (reasoningContent) {
266
285
  if (!hasEmittedStepStarted) {
267
286
  hasEmittedStepStarted = true;
@@ -309,16 +328,13 @@ var WorkersAiTextAdapter = class extends _adapters.BaseTextAdapter {
309
328
  const index = toolCallDelta.index;
310
329
  if (!toolCallsInProgress.has(index)) {
311
330
  toolCallsInProgress.set(index, {
312
- id: toolCallDelta.id || "",
331
+ id: generateId("chatcmpl-tool"),
313
332
  name: _optionalChain([toolCallDelta, 'access', _10 => _10.function, 'optionalAccess', _11 => _11.name]) || "",
314
333
  arguments: "",
315
334
  started: false
316
335
  });
317
336
  }
318
337
  const toolCall = toolCallsInProgress.get(index);
319
- if (toolCallDelta.id) {
320
- toolCall.id = toolCallDelta.id;
321
- }
322
338
  if (_optionalChain([toolCallDelta, 'access', _12 => _12.function, 'optionalAccess', _13 => _13.name])) {
323
339
  toolCall.name = toolCallDelta.function.name;
324
340
  }
@@ -502,4 +518,4 @@ function createWorkersAiChat(model, config) {
502
518
 
503
519
 
504
520
  exports.WorkersAiTextAdapter = WorkersAiTextAdapter; exports.createWorkersAiChat = createWorkersAiChat;
505
- //# sourceMappingURL=chunk-LG2I3ANT.cjs.map
521
+ //# sourceMappingURL=chunk-32U73O6E.cjs.map