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