@tanstack/ai 0.6.2 → 0.8.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.
- package/dist/esm/activities/chat/index.d.ts +20 -0
- package/dist/esm/activities/chat/index.js +248 -213
- package/dist/esm/activities/chat/index.js.map +1 -1
- package/dist/esm/activities/chat/middleware/compose.d.ts +66 -0
- package/dist/esm/activities/chat/middleware/compose.js +327 -0
- package/dist/esm/activities/chat/middleware/compose.js.map +1 -0
- package/dist/esm/activities/chat/middleware/index.d.ts +2 -0
- package/dist/esm/activities/chat/middleware/tool-cache-middleware.d.ts +89 -0
- package/dist/esm/activities/chat/middleware/tool-cache-middleware.js +76 -0
- package/dist/esm/activities/chat/middleware/tool-cache-middleware.js.map +1 -0
- package/dist/esm/activities/chat/middleware/types.d.ts +307 -0
- package/dist/esm/activities/chat/tools/tool-calls.d.ts +16 -1
- package/dist/esm/activities/chat/tools/tool-calls.js +148 -64
- package/dist/esm/activities/chat/tools/tool-calls.js.map +1 -1
- package/dist/esm/activities/generateImage/index.d.ts +19 -6
- package/dist/esm/activities/generateImage/index.js +12 -3
- package/dist/esm/activities/generateImage/index.js.map +1 -1
- package/dist/esm/activities/generateSpeech/index.d.ts +19 -6
- package/dist/esm/activities/generateSpeech/index.js +12 -3
- package/dist/esm/activities/generateSpeech/index.js.map +1 -1
- package/dist/esm/activities/generateTranscription/index.d.ts +30 -6
- package/dist/esm/activities/generateTranscription/index.js +14 -3
- package/dist/esm/activities/generateTranscription/index.js.map +1 -1
- package/dist/esm/activities/generateVideo/index.d.ts +45 -7
- package/dist/esm/activities/generateVideo/index.js +91 -2
- package/dist/esm/activities/generateVideo/index.js.map +1 -1
- package/dist/esm/activities/stream-generation-result.d.ts +14 -0
- package/dist/esm/activities/stream-generation-result.js +40 -0
- package/dist/esm/activities/stream-generation-result.js.map +1 -0
- package/dist/esm/activities/summarize/index.js +3 -18
- package/dist/esm/activities/summarize/index.js.map +1 -1
- package/dist/esm/index.d.ts +3 -1
- package/dist/esm/index.js +2 -2
- package/dist/esm/middlewares/content-guard.d.ts +77 -0
- package/dist/esm/middlewares/content-guard.js +155 -0
- package/dist/esm/middlewares/content-guard.js.map +1 -0
- package/dist/esm/middlewares/index.d.ts +2 -0
- package/dist/esm/middlewares/index.js +7 -0
- package/dist/esm/middlewares/index.js.map +1 -0
- package/dist/esm/middlewares/tool-cache.d.ts +1 -0
- package/dist/esm/realtime/index.d.ts +30 -0
- package/dist/esm/realtime/index.js +8 -0
- package/dist/esm/realtime/index.js.map +1 -0
- package/dist/esm/realtime/types.d.ts +234 -0
- package/package.json +6 -6
- package/src/activities/chat/index.ts +322 -256
- package/src/activities/chat/middleware/compose.ts +392 -0
- package/src/activities/chat/middleware/index.ts +17 -0
- package/src/activities/chat/middleware/tool-cache-middleware.ts +189 -0
- package/src/activities/chat/middleware/types.ts +419 -0
- package/src/activities/chat/tools/tool-calls.ts +225 -87
- package/src/activities/generateImage/index.ts +50 -8
- package/src/activities/generateSpeech/index.ts +42 -8
- package/src/activities/generateTranscription/index.ts +60 -10
- package/src/activities/generateVideo/index.ts +174 -7
- package/src/activities/stream-generation-result.ts +62 -0
- package/src/activities/summarize/index.ts +4 -23
- package/src/index.ts +41 -2
- package/src/middlewares/content-guard.ts +285 -0
- package/src/middlewares/index.ts +13 -0
- package/src/middlewares/tool-cache.ts +6 -0
- package/src/realtime/index.ts +38 -0
- package/src/realtime/types.ts +294 -0
- package/dist/esm/event-client.d.ts +0 -394
- package/dist/esm/event-client.js +0 -13
- package/dist/esm/event-client.js.map +0 -1
- package/src/event-client.ts +0 -497
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export {
|
|
2
|
+
toolCacheMiddleware,
|
|
3
|
+
type ToolCacheMiddlewareOptions,
|
|
4
|
+
type ToolCacheStorage,
|
|
5
|
+
type ToolCacheEntry,
|
|
6
|
+
} from './tool-cache'
|
|
7
|
+
|
|
8
|
+
export {
|
|
9
|
+
contentGuardMiddleware,
|
|
10
|
+
type ContentGuardMiddlewareOptions,
|
|
11
|
+
type ContentGuardRule,
|
|
12
|
+
type ContentFilteredInfo,
|
|
13
|
+
} from './content-guard'
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { RealtimeToken, RealtimeTokenOptions } from './types'
|
|
2
|
+
|
|
3
|
+
// Re-export all types
|
|
4
|
+
export * from './types'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Generate a realtime token using the provided adapter.
|
|
8
|
+
*
|
|
9
|
+
* This function is used on the server to generate ephemeral tokens
|
|
10
|
+
* that clients can use to establish realtime connections.
|
|
11
|
+
*
|
|
12
|
+
* @param options - Token generation options including the adapter
|
|
13
|
+
* @returns Promise resolving to a RealtimeToken
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* import { realtimeToken } from '@tanstack/ai'
|
|
18
|
+
* import { openaiRealtimeToken } from '@tanstack/ai-openai'
|
|
19
|
+
*
|
|
20
|
+
* // Server function (TanStack Start example)
|
|
21
|
+
* export const getRealtimeToken = createServerFn()
|
|
22
|
+
* .handler(async () => {
|
|
23
|
+
* return realtimeToken({
|
|
24
|
+
* adapter: openaiRealtimeToken({
|
|
25
|
+
* model: 'gpt-4o-realtime-preview',
|
|
26
|
+
* voice: 'alloy',
|
|
27
|
+
* instructions: 'You are a helpful assistant...',
|
|
28
|
+
* }),
|
|
29
|
+
* })
|
|
30
|
+
* })
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export async function realtimeToken(
|
|
34
|
+
options: RealtimeTokenOptions,
|
|
35
|
+
): Promise<RealtimeToken> {
|
|
36
|
+
const { adapter } = options
|
|
37
|
+
return adapter.generateToken()
|
|
38
|
+
}
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
// ============================================================================
|
|
2
|
+
// Token Types
|
|
3
|
+
// ============================================================================
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Voice activity detection configuration
|
|
7
|
+
*/
|
|
8
|
+
export interface VADConfig {
|
|
9
|
+
/** Sensitivity threshold (0.0-1.0) */
|
|
10
|
+
threshold?: number
|
|
11
|
+
/** Audio to include before speech detection (ms) */
|
|
12
|
+
prefixPaddingMs?: number
|
|
13
|
+
/** Silence duration to end turn (ms) */
|
|
14
|
+
silenceDurationMs?: number
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Serializable tool descriptor for realtime session configuration.
|
|
19
|
+
* Contains only the metadata needed by providers, not Zod schemas or execute functions.
|
|
20
|
+
*/
|
|
21
|
+
export interface RealtimeToolConfig {
|
|
22
|
+
name: string
|
|
23
|
+
description: string
|
|
24
|
+
inputSchema?: Record<string, any>
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Configuration for a realtime session
|
|
29
|
+
*/
|
|
30
|
+
export interface RealtimeSessionConfig {
|
|
31
|
+
/** Model to use for the session */
|
|
32
|
+
model?: string
|
|
33
|
+
/** Voice to use for audio output */
|
|
34
|
+
voice?: string
|
|
35
|
+
/** System instructions for the assistant */
|
|
36
|
+
instructions?: string
|
|
37
|
+
/** Tools available in the session */
|
|
38
|
+
tools?: Array<RealtimeToolConfig>
|
|
39
|
+
/** VAD mode */
|
|
40
|
+
vadMode?: 'server' | 'semantic' | 'manual'
|
|
41
|
+
/** VAD configuration */
|
|
42
|
+
vadConfig?: VADConfig
|
|
43
|
+
/** Output modalities for responses (e.g., ['audio', 'text'], ['text']) */
|
|
44
|
+
outputModalities?: Array<'audio' | 'text'>
|
|
45
|
+
/** Temperature for generation (provider-specific range, e.g., 0.6-1.2 for OpenAI) */
|
|
46
|
+
temperature?: number
|
|
47
|
+
/** Maximum number of tokens in a response */
|
|
48
|
+
maxOutputTokens?: number | 'inf'
|
|
49
|
+
/** Eagerness level for semantic VAD ('low', 'medium', 'high') */
|
|
50
|
+
semanticEagerness?: 'low' | 'medium' | 'high'
|
|
51
|
+
/** Provider-specific options */
|
|
52
|
+
providerOptions?: Record<string, any>
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Token returned by the server for client authentication
|
|
57
|
+
*/
|
|
58
|
+
export interface RealtimeToken {
|
|
59
|
+
/** Provider identifier */
|
|
60
|
+
provider: string
|
|
61
|
+
/** The ephemeral token value */
|
|
62
|
+
token: string
|
|
63
|
+
/** Token expiration timestamp (ms since epoch) */
|
|
64
|
+
expiresAt: number
|
|
65
|
+
/** Session configuration embedded in the token */
|
|
66
|
+
config: RealtimeSessionConfig
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Adapter interface for generating provider-specific tokens
|
|
71
|
+
*/
|
|
72
|
+
export interface RealtimeTokenAdapter {
|
|
73
|
+
/** Provider identifier */
|
|
74
|
+
provider: string
|
|
75
|
+
/** Generate an ephemeral token for client use */
|
|
76
|
+
generateToken: () => Promise<RealtimeToken>
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Options for the realtimeToken function
|
|
81
|
+
*/
|
|
82
|
+
export interface RealtimeTokenOptions {
|
|
83
|
+
/** The token adapter to use */
|
|
84
|
+
adapter: RealtimeTokenAdapter
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// ============================================================================
|
|
88
|
+
// Message Types
|
|
89
|
+
// ============================================================================
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Text content part in a realtime message
|
|
93
|
+
*/
|
|
94
|
+
export interface RealtimeTextPart {
|
|
95
|
+
type: 'text'
|
|
96
|
+
content: string
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Audio content part in a realtime message
|
|
101
|
+
*/
|
|
102
|
+
export interface RealtimeAudioPart {
|
|
103
|
+
type: 'audio'
|
|
104
|
+
/** Transcription of the audio */
|
|
105
|
+
transcript: string
|
|
106
|
+
/** Raw audio data (optional, if stored) */
|
|
107
|
+
audioData?: ArrayBuffer
|
|
108
|
+
/** Duration of the audio in milliseconds */
|
|
109
|
+
durationMs?: number
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Tool call part in a realtime message
|
|
114
|
+
*/
|
|
115
|
+
export interface RealtimeToolCallPart {
|
|
116
|
+
type: 'tool-call'
|
|
117
|
+
id: string
|
|
118
|
+
name: string
|
|
119
|
+
arguments: string
|
|
120
|
+
input?: unknown
|
|
121
|
+
output?: unknown
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Tool result part in a realtime message
|
|
126
|
+
*/
|
|
127
|
+
export interface RealtimeToolResultPart {
|
|
128
|
+
type: 'tool-result'
|
|
129
|
+
toolCallId: string
|
|
130
|
+
content: string
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Image content part in a realtime message
|
|
135
|
+
*/
|
|
136
|
+
export interface RealtimeImagePart {
|
|
137
|
+
type: 'image'
|
|
138
|
+
/** Base64-encoded image data or a URL */
|
|
139
|
+
data: string
|
|
140
|
+
/** MIME type of the image (e.g., 'image/png', 'image/jpeg') */
|
|
141
|
+
mimeType: string
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Union of all realtime message parts
|
|
146
|
+
*/
|
|
147
|
+
export type RealtimeMessagePart =
|
|
148
|
+
| RealtimeTextPart
|
|
149
|
+
| RealtimeAudioPart
|
|
150
|
+
| RealtimeToolCallPart
|
|
151
|
+
| RealtimeToolResultPart
|
|
152
|
+
| RealtimeImagePart
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* A message in a realtime conversation
|
|
156
|
+
*/
|
|
157
|
+
export interface RealtimeMessage {
|
|
158
|
+
/** Unique message identifier */
|
|
159
|
+
id: string
|
|
160
|
+
/** Message role */
|
|
161
|
+
role: 'user' | 'assistant'
|
|
162
|
+
/** Timestamp when the message was created */
|
|
163
|
+
timestamp: number
|
|
164
|
+
/** Content parts of the message */
|
|
165
|
+
parts: Array<RealtimeMessagePart>
|
|
166
|
+
/** Whether this message was interrupted */
|
|
167
|
+
interrupted?: boolean
|
|
168
|
+
/** Reference to audio buffer if stored */
|
|
169
|
+
audioId?: string
|
|
170
|
+
/** Duration of the audio in milliseconds */
|
|
171
|
+
durationMs?: number
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// ============================================================================
|
|
175
|
+
// Status Types
|
|
176
|
+
// ============================================================================
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Connection status of the realtime client
|
|
180
|
+
*/
|
|
181
|
+
export type RealtimeStatus =
|
|
182
|
+
| 'idle'
|
|
183
|
+
| 'connecting'
|
|
184
|
+
| 'connected'
|
|
185
|
+
| 'reconnecting'
|
|
186
|
+
| 'error'
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Current mode of the realtime session
|
|
190
|
+
*/
|
|
191
|
+
export type RealtimeMode = 'idle' | 'listening' | 'thinking' | 'speaking'
|
|
192
|
+
|
|
193
|
+
// ============================================================================
|
|
194
|
+
// Audio Visualization Types
|
|
195
|
+
// ============================================================================
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Interface for accessing audio visualization data
|
|
199
|
+
*/
|
|
200
|
+
export interface AudioVisualization {
|
|
201
|
+
/** Input volume level (0-1 normalized) */
|
|
202
|
+
readonly inputLevel: number
|
|
203
|
+
/** Output volume level (0-1 normalized) */
|
|
204
|
+
readonly outputLevel: number
|
|
205
|
+
|
|
206
|
+
/** Get frequency data for input audio visualization */
|
|
207
|
+
getInputFrequencyData: () => Uint8Array
|
|
208
|
+
/** Get frequency data for output audio visualization */
|
|
209
|
+
getOutputFrequencyData: () => Uint8Array
|
|
210
|
+
|
|
211
|
+
/** Get time domain data for input waveform */
|
|
212
|
+
getInputTimeDomainData: () => Uint8Array
|
|
213
|
+
/** Get time domain data for output waveform */
|
|
214
|
+
getOutputTimeDomainData: () => Uint8Array
|
|
215
|
+
|
|
216
|
+
/** Input sample rate */
|
|
217
|
+
readonly inputSampleRate: number
|
|
218
|
+
/** Output sample rate */
|
|
219
|
+
readonly outputSampleRate: number
|
|
220
|
+
|
|
221
|
+
/** Subscribe to raw input audio samples */
|
|
222
|
+
onInputAudio?: (
|
|
223
|
+
callback: (samples: Float32Array, sampleRate: number) => void,
|
|
224
|
+
) => () => void
|
|
225
|
+
/** Subscribe to raw output audio samples */
|
|
226
|
+
onOutputAudio?: (
|
|
227
|
+
callback: (samples: Float32Array, sampleRate: number) => void,
|
|
228
|
+
) => () => void
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// ============================================================================
|
|
232
|
+
// Event Types
|
|
233
|
+
// ============================================================================
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Events emitted by the realtime connection
|
|
237
|
+
*/
|
|
238
|
+
export type RealtimeEvent =
|
|
239
|
+
| 'status_change'
|
|
240
|
+
| 'mode_change'
|
|
241
|
+
| 'transcript'
|
|
242
|
+
| 'audio_chunk'
|
|
243
|
+
| 'tool_call'
|
|
244
|
+
| 'message_complete'
|
|
245
|
+
| 'interrupted'
|
|
246
|
+
| 'error'
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Event payloads for realtime events
|
|
250
|
+
*/
|
|
251
|
+
export interface RealtimeEventPayloads {
|
|
252
|
+
status_change: { status: RealtimeStatus }
|
|
253
|
+
mode_change: { mode: RealtimeMode }
|
|
254
|
+
transcript: {
|
|
255
|
+
role: 'user' | 'assistant'
|
|
256
|
+
transcript: string
|
|
257
|
+
isFinal: boolean
|
|
258
|
+
}
|
|
259
|
+
audio_chunk: { data: ArrayBuffer; sampleRate: number }
|
|
260
|
+
tool_call: { toolCallId: string; toolName: string; input: unknown }
|
|
261
|
+
message_complete: { message: RealtimeMessage }
|
|
262
|
+
interrupted: { messageId?: string }
|
|
263
|
+
error: { error: Error }
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Handler type for realtime events
|
|
268
|
+
*/
|
|
269
|
+
export type RealtimeEventHandler<TEvent extends RealtimeEvent> = (
|
|
270
|
+
payload: RealtimeEventPayloads[TEvent],
|
|
271
|
+
) => void
|
|
272
|
+
|
|
273
|
+
// ============================================================================
|
|
274
|
+
// Error Types
|
|
275
|
+
// ============================================================================
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Error codes for realtime errors
|
|
279
|
+
*/
|
|
280
|
+
export type RealtimeErrorCode =
|
|
281
|
+
| 'TOKEN_EXPIRED'
|
|
282
|
+
| 'CONNECTION_FAILED'
|
|
283
|
+
| 'PERMISSION_DENIED'
|
|
284
|
+
| 'PROVIDER_ERROR'
|
|
285
|
+
| 'UNKNOWN'
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Extended error with realtime-specific information
|
|
289
|
+
*/
|
|
290
|
+
export interface RealtimeError extends Error {
|
|
291
|
+
code: RealtimeErrorCode
|
|
292
|
+
provider?: string
|
|
293
|
+
details?: unknown
|
|
294
|
+
}
|