@tanstack/ai-client 0.13.0 → 0.14.1

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 (54) hide show
  1. package/README.md +1 -1
  2. package/dist/esm/chat-client.d.ts +1 -1
  3. package/dist/esm/chat-client.js +1 -1
  4. package/dist/esm/chat-client.js.map +1 -1
  5. package/dist/esm/connection-adapters.d.ts +21 -1
  6. package/dist/esm/connection-adapters.js +262 -68
  7. package/dist/esm/connection-adapters.js.map +1 -1
  8. package/dist/esm/devtools-noop.d.ts +1 -1
  9. package/dist/esm/devtools-noop.js.map +1 -1
  10. package/dist/esm/devtools.d.ts +1 -1
  11. package/dist/esm/devtools.js +1 -1
  12. package/dist/esm/devtools.js.map +1 -1
  13. package/dist/esm/events.d.ts +1 -1
  14. package/dist/esm/events.js.map +1 -1
  15. package/dist/esm/generation-client.js.map +1 -1
  16. package/dist/esm/generation-types.d.ts +1 -1
  17. package/dist/esm/generation-types.js.map +1 -1
  18. package/dist/esm/index.d.ts +5 -4
  19. package/dist/esm/index.js +7 -3
  20. package/dist/esm/index.js.map +1 -1
  21. package/dist/esm/realtime-client.d.ts +1 -1
  22. package/dist/esm/realtime-client.js +1 -1
  23. package/dist/esm/realtime-client.js.map +1 -1
  24. package/dist/esm/realtime-types.d.ts +1 -1
  25. package/dist/esm/response-stream.d.ts +7 -0
  26. package/dist/esm/response-stream.js +32 -0
  27. package/dist/esm/response-stream.js.map +1 -0
  28. package/dist/esm/sse-parser.d.ts +1 -1
  29. package/dist/esm/sse-parser.js +5 -6
  30. package/dist/esm/sse-parser.js.map +1 -1
  31. package/dist/esm/sse-utils.d.ts +1 -0
  32. package/dist/esm/sse-utils.js +11 -0
  33. package/dist/esm/sse-utils.js.map +1 -0
  34. package/dist/esm/tool-types.d.ts +1 -1
  35. package/dist/esm/types.d.ts +2 -2
  36. package/dist/esm/types.js.map +1 -1
  37. package/dist/esm/video-generation-client.js.map +1 -1
  38. package/package.json +3 -3
  39. package/src/chat-client.ts +2 -2
  40. package/src/connection-adapters.ts +360 -68
  41. package/src/devtools-noop.ts +1 -1
  42. package/src/devtools.ts +2 -2
  43. package/src/events.ts +1 -1
  44. package/src/generation-client.ts +1 -1
  45. package/src/generation-types.ts +1 -1
  46. package/src/index.ts +7 -3
  47. package/src/realtime-client.ts +2 -2
  48. package/src/realtime-types.ts +1 -1
  49. package/src/response-stream.ts +42 -0
  50. package/src/sse-parser.ts +9 -7
  51. package/src/sse-utils.ts +8 -0
  52. package/src/tool-types.ts +1 -1
  53. package/src/types.ts +2 -2
  54. package/src/video-generation-client.ts +1 -1
package/src/events.ts CHANGED
@@ -2,7 +2,7 @@ import {
2
2
  aiEventClient,
3
3
  createAIDevtoolsEventEnvelope,
4
4
  } from '@tanstack/ai-event-client'
5
- import type { ContentPart } from '@tanstack/ai'
5
+ import type { ContentPart } from '@tanstack/ai/client'
6
6
  import type { UIMessage } from './types'
7
7
 
8
8
  export interface ChatClientRunEventContext {
@@ -1,7 +1,7 @@
1
1
  import { GENERATION_EVENTS } from './generation-types'
2
2
  import { createNoOpGenerationDevtoolsBridge } from './devtools-noop'
3
3
  import { parseSSEResponse } from './sse-parser'
4
- import type { StreamChunk } from '@tanstack/ai'
4
+ import type { StreamChunk } from '@tanstack/ai/client'
5
5
  import type {
6
6
  ConnectConnectionAdapter,
7
7
  RunAgentInputContext,
@@ -1,4 +1,4 @@
1
- import type { StreamChunk } from '@tanstack/ai'
1
+ import type { StreamChunk } from '@tanstack/ai/client'
2
2
  import type { ConnectConnectionAdapter } from './connection-adapters'
3
3
  import type { AIDevtoolsClientMetadata } from './devtools'
4
4
  import type {
package/src/index.ts CHANGED
@@ -43,6 +43,7 @@ export type {
43
43
  VideoGenerateInput,
44
44
  } from './generation-types'
45
45
  export { GENERATION_EVENTS } from './generation-types'
46
+ export { UnsupportedResponseStreamError } from './response-stream'
46
47
  export { clientTools, createChatClientOptions } from './types'
47
48
  export {
48
49
  createAIDevtoolsGenerationPreview,
@@ -58,7 +59,7 @@ export type {
58
59
  ExtractToolInput,
59
60
  ExtractToolOutput,
60
61
  } from './tool-types'
61
- export type { AnyClientTool } from '@tanstack/ai'
62
+ export type { AnyClientTool } from '@tanstack/ai/client'
62
63
  export type {
63
64
  RealtimeAdapter,
64
65
  RealtimeConnection,
@@ -69,6 +70,8 @@ export type {
69
70
  export {
70
71
  fetchServerSentEvents,
71
72
  fetchHttpStream,
73
+ xhrServerSentEvents,
74
+ xhrHttpStream,
72
75
  stream,
73
76
  rpcStream,
74
77
  StreamTruncatedError,
@@ -77,6 +80,7 @@ export {
77
80
  type FetchConnectionOptions,
78
81
  type RunAgentInputContext,
79
82
  type SubscribeConnectionAdapter,
83
+ type XhrConnectionOptions,
80
84
  } from './connection-adapters'
81
85
 
82
86
  // Re-export message converters from @tanstack/ai
@@ -87,7 +91,7 @@ export {
87
91
  convertMessagesToModelMessages,
88
92
  normalizeToUIMessage,
89
93
  generateMessageId,
90
- } from '@tanstack/ai'
94
+ } from '@tanstack/ai/client'
91
95
 
92
96
  // Re-export stream processing from @tanstack/ai (shared implementation)
93
97
  export {
@@ -110,4 +114,4 @@ export {
110
114
  type ChunkRecording,
111
115
  type ProcessorResult,
112
116
  type ProcessorState,
113
- } from '@tanstack/ai'
117
+ } from '@tanstack/ai/client'
@@ -1,4 +1,4 @@
1
- import { convertSchemaToJsonSchema } from '@tanstack/ai'
1
+ import { convertSchemaToJsonSchema } from '@tanstack/ai/client'
2
2
  import type {
3
3
  AnyClientTool,
4
4
  AudioVisualization,
@@ -6,7 +6,7 @@ import type {
6
6
  RealtimeMode,
7
7
  RealtimeStatus,
8
8
  RealtimeToken,
9
- } from '@tanstack/ai'
9
+ } from '@tanstack/ai/client'
10
10
  import type {
11
11
  RealtimeClientOptions,
12
12
  RealtimeClientState,
@@ -8,7 +8,7 @@ import type {
8
8
  RealtimeSessionConfig,
9
9
  RealtimeStatus,
10
10
  RealtimeToken,
11
- } from '@tanstack/ai'
11
+ } from '@tanstack/ai/client'
12
12
 
13
13
  // ============================================================================
14
14
  // Adapter Interface
@@ -0,0 +1,42 @@
1
+ export type MissingResponseStreamFeature =
2
+ | 'TextDecoder'
3
+ | 'Response.body'
4
+ | 'Response.body.getReader'
5
+
6
+ export class UnsupportedResponseStreamError extends Error {
7
+ readonly missingFeature: MissingResponseStreamFeature
8
+
9
+ constructor(missingFeature: MissingResponseStreamFeature) {
10
+ super(createUnsupportedResponseStreamMessage(missingFeature))
11
+ this.name = 'UnsupportedResponseStreamError'
12
+ this.missingFeature = missingFeature
13
+ }
14
+ }
15
+
16
+ function createUnsupportedResponseStreamMessage(
17
+ missingFeature: MissingResponseStreamFeature,
18
+ ): string {
19
+ return `Streaming fetch responses are not supported in this runtime because ${missingFeature} is unavailable. React Native users need a compatible fetch/stream/TextDecoder polyfill, or should use a fetch, XHR, or custom transport that can deliver streaming chunks.`
20
+ }
21
+
22
+ export function getResponseStreamReader(
23
+ response: Response,
24
+ ): ReadableStreamDefaultReader<Uint8Array> {
25
+ if (!response.body) {
26
+ throw new UnsupportedResponseStreamError('Response.body')
27
+ }
28
+
29
+ if (typeof response.body.getReader !== 'function') {
30
+ throw new UnsupportedResponseStreamError('Response.body.getReader')
31
+ }
32
+
33
+ return response.body.getReader()
34
+ }
35
+
36
+ export function createResponseStreamTextDecoder(): TextDecoder {
37
+ if (typeof globalThis.TextDecoder !== 'function') {
38
+ throw new UnsupportedResponseStreamError('TextDecoder')
39
+ }
40
+
41
+ return new globalThis.TextDecoder()
42
+ }
package/src/sse-parser.ts CHANGED
@@ -1,4 +1,9 @@
1
- import type { StreamChunk } from '@tanstack/ai'
1
+ import {
2
+ createResponseStreamTextDecoder,
3
+ getResponseStreamReader,
4
+ } from './response-stream'
5
+ import { parseSseDataLine } from './sse-utils'
6
+ import type { StreamChunk } from '@tanstack/ai/client'
2
7
 
3
8
  /**
4
9
  * Read lines from a stream (newline-delimited)
@@ -8,7 +13,7 @@ async function* readStreamLines(
8
13
  abortSignal?: AbortSignal,
9
14
  ): AsyncGenerator<string> {
10
15
  try {
11
- const decoder = new TextDecoder()
16
+ const decoder = createResponseStreamTextDecoder()
12
17
  let buffer = ''
13
18
 
14
19
  while (!abortSignal?.aborted) {
@@ -51,13 +56,10 @@ export async function* parseSSEResponse(
51
56
  )
52
57
  }
53
58
 
54
- const reader = response.body?.getReader()
55
- if (!reader) {
56
- throw new Error('Response body is not readable')
57
- }
59
+ const reader = getResponseStreamReader(response)
58
60
 
59
61
  for await (const line of readStreamLines(reader, abortSignal)) {
60
- const data = line.startsWith('data: ') ? line.slice(6) : line
62
+ const data = parseSseDataLine(line)
61
63
 
62
64
  if (data === '[DONE]') {
63
65
  console.warn(
@@ -0,0 +1,8 @@
1
+ export function parseSseDataLine(line: string): string {
2
+ if (!line.startsWith('data:')) {
3
+ return line
4
+ }
5
+
6
+ const data = line.slice(5)
7
+ return data.startsWith(' ') ? data.slice(1) : data
8
+ }
package/src/tool-types.ts CHANGED
@@ -2,7 +2,7 @@ import type {
2
2
  AnyClientTool,
3
3
  InferToolInput,
4
4
  InferToolOutput,
5
- } from '@tanstack/ai'
5
+ } from '@tanstack/ai/client'
6
6
 
7
7
  /**
8
8
  * Extract all tool names from a tools array as a union type
package/src/types.ts CHANGED
@@ -11,12 +11,12 @@ import type {
11
11
  StreamChunk,
12
12
  StructuredOutputPart,
13
13
  VideoPart,
14
- } from '@tanstack/ai'
14
+ } from '@tanstack/ai/client'
15
15
  import type { ConnectionAdapter } from './connection-adapters'
16
16
  import type { AIDevtoolsClientMetadata } from './devtools'
17
17
  import type { ChatDevtoolsBridgeFactory } from './devtools-noop'
18
18
 
19
- export type { StructuredOutputPart } from '@tanstack/ai'
19
+ export type { StructuredOutputPart } from '@tanstack/ai/client'
20
20
 
21
21
  /**
22
22
  * `messages` is the full UIMessage history (not a delta). `data` is the
@@ -1,7 +1,7 @@
1
1
  import { GENERATION_EVENTS } from './generation-types'
2
2
  import { createNoOpVideoDevtoolsBridge } from './devtools-noop'
3
3
  import { parseSSEResponse } from './sse-parser'
4
- import type { StreamChunk } from '@tanstack/ai'
4
+ import type { StreamChunk } from '@tanstack/ai/client'
5
5
  import type {
6
6
  ConnectConnectionAdapter,
7
7
  RunAgentInputContext,