@tanstack/ai-client 0.18.5 → 0.19.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/audio-recorder.d.ts +76 -0
- package/dist/esm/audio-recorder.js +215 -0
- package/dist/esm/audio-recorder.js.map +1 -0
- package/dist/esm/chat-client.d.ts +264 -0
- package/dist/esm/chat-client.js +1097 -0
- package/dist/esm/chat-client.js.map +1 -0
- package/dist/esm/client-persistor.d.ts +86 -0
- package/dist/esm/client-persistor.js +243 -0
- package/dist/esm/client-persistor.js.map +1 -0
- package/dist/esm/connection-adapters.d.ts +203 -0
- package/dist/esm/connection-adapters.js +611 -0
- package/dist/esm/connection-adapters.js.map +1 -0
- package/dist/esm/devtools-noop.d.ts +63 -0
- package/dist/esm/devtools-noop.js +141 -0
- package/dist/esm/devtools-noop.js.map +1 -0
- package/dist/esm/devtools.d.ts +276 -0
- package/dist/esm/devtools.js +1178 -0
- package/dist/esm/devtools.js.map +1 -0
- package/dist/esm/events.d.ts +129 -0
- package/dist/esm/events.js +227 -0
- package/dist/esm/events.js.map +1 -0
- package/dist/esm/generation-client.d.ts +99 -0
- package/dist/esm/generation-client.js +325 -0
- package/dist/esm/generation-client.js.map +1 -0
- package/dist/esm/generation-types.d.ts +261 -0
- package/dist/esm/generation-types.js +14 -0
- package/dist/esm/generation-types.js.map +1 -0
- package/dist/esm/index.d.ts +20 -0
- package/dist/esm/index.js +48 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/mcp-app-bridge.d.ts +27 -0
- package/dist/esm/mcp-app-bridge.js +70 -0
- package/dist/esm/mcp-app-bridge.js.map +1 -0
- package/dist/esm/realtime-client.d.ts +98 -0
- package/dist/esm/realtime-client.js +403 -0
- package/dist/esm/realtime-client.js.map +1 -0
- package/dist/esm/realtime-types.d.ts +78 -0
- package/dist/esm/response-stream.d.ts +7 -0
- package/dist/esm/response-stream.js +32 -0
- package/dist/esm/response-stream.js.map +1 -0
- package/dist/esm/sse-parser.d.ts +8 -0
- package/dist/esm/sse-parser.js +52 -0
- package/dist/esm/sse-parser.js.map +1 -0
- package/dist/esm/sse-utils.d.ts +1 -0
- package/dist/esm/sse-utils.js +11 -0
- package/dist/esm/sse-utils.js.map +1 -0
- package/dist/esm/tool-types.d.ts +20 -0
- package/dist/esm/types.d.ts +419 -0
- package/dist/esm/types.js +11 -0
- package/dist/esm/types.js.map +1 -0
- package/dist/esm/video-generation-client.d.ts +110 -0
- package/dist/esm/video-generation-client.js +382 -0
- package/dist/esm/video-generation-client.js.map +1 -0
- package/package.json +4 -3
- package/src/audio-recorder.ts +322 -0
- package/src/generation-types.ts +24 -4
- package/src/index.ts +10 -0
- package/src/mcp-app-bridge.ts +120 -0
- package/src/types.ts +2 -0
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { AudioPart } from '@tanstack/ai/client';
|
|
2
|
+
/** Lifecycle state of an {@link AudioRecorder}. */
|
|
3
|
+
export type AudioRecorderState = 'idle' | 'recording' | 'stopping';
|
|
4
|
+
export interface AudioRecorderOptions {
|
|
5
|
+
/** Constraints forwarded to `getUserMedia({ audio })`. Defaults to `true`. */
|
|
6
|
+
audio?: MediaTrackConstraints | boolean;
|
|
7
|
+
/**
|
|
8
|
+
* Preferred recorder mime type. Used only when
|
|
9
|
+
* `MediaRecorder.isTypeSupported` reports it; otherwise the browser default
|
|
10
|
+
* is used.
|
|
11
|
+
*/
|
|
12
|
+
mimeType?: string;
|
|
13
|
+
/** Fired on `getUserMedia` rejection (permission denied) or recorder error. */
|
|
14
|
+
onError?: (error: Error) => void;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Resolves the value `stop()` produces from a recorder transform callback.
|
|
18
|
+
*
|
|
19
|
+
* - If the callback returns a value — sync or async — that value's awaited type
|
|
20
|
+
* is used (a returned `null` is a real value and is preserved).
|
|
21
|
+
* - If the callback returns nothing (`void`/`undefined`), or is absent, falls
|
|
22
|
+
* back to {@link AudioRecording}.
|
|
23
|
+
*
|
|
24
|
+
* @template TFn - The transform callback type (or undefined if not provided)
|
|
25
|
+
*/
|
|
26
|
+
export type InferAudioRecordingOutput<TFn> = TFn extends (recording: AudioRecording) => infer R ? [Exclude<Awaited<R>, void | undefined>] extends [never] ? AudioRecording : Exclude<Awaited<R>, void | undefined> : AudioRecording;
|
|
27
|
+
export interface AudioRecording {
|
|
28
|
+
/** The raw recorded media blob. */
|
|
29
|
+
blob: Blob;
|
|
30
|
+
/** Base64 of the recorded bytes (no `data:` prefix). */
|
|
31
|
+
base64: string;
|
|
32
|
+
/** The recorder's native mime type, e.g. `audio/webm;codecs=opus`. */
|
|
33
|
+
mimeType: string;
|
|
34
|
+
/** Recording length in milliseconds. */
|
|
35
|
+
durationMs: number;
|
|
36
|
+
/**
|
|
37
|
+
* Ready-to-use audio content part for `sendMessage`/generation prompts:
|
|
38
|
+
* `{ type: 'audio', source: { type: 'data', value: base64, mimeType } }`.
|
|
39
|
+
*/
|
|
40
|
+
part: AudioPart;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Framework-agnostic browser audio recorder. Wraps `getUserMedia` +
|
|
44
|
+
* `MediaRecorder`, returns the recorder's native output (no transcode), and
|
|
45
|
+
* builds a plug-and-play {@link AudioRecording.part}.
|
|
46
|
+
*/
|
|
47
|
+
export declare class AudioRecorder {
|
|
48
|
+
private readonly options;
|
|
49
|
+
private recorder;
|
|
50
|
+
private stream;
|
|
51
|
+
private chunks;
|
|
52
|
+
private startedAt;
|
|
53
|
+
private _state;
|
|
54
|
+
private starting;
|
|
55
|
+
private pendingCancel;
|
|
56
|
+
private readonly listeners;
|
|
57
|
+
private stopResolve;
|
|
58
|
+
private stopReject;
|
|
59
|
+
constructor(options?: AudioRecorderOptions);
|
|
60
|
+
/** Feature-detect the browser media APIs. SSR/Worker-safe. */
|
|
61
|
+
static isSupported(): boolean;
|
|
62
|
+
get state(): AudioRecorderState;
|
|
63
|
+
subscribe(cb: (state: AudioRecorderState) => void): () => void;
|
|
64
|
+
private setState;
|
|
65
|
+
start(): Promise<void>;
|
|
66
|
+
stop(): Promise<AudioRecording>;
|
|
67
|
+
cancel(): void;
|
|
68
|
+
private finalize;
|
|
69
|
+
private handleError;
|
|
70
|
+
/** Invoke the user onError callback, isolating a throw so it can't disrupt
|
|
71
|
+
* internal state teardown or strand a pending promise. */
|
|
72
|
+
private notifyError;
|
|
73
|
+
private releaseStream;
|
|
74
|
+
/** Detach all event handlers so a stale recorder can't mutate our state. */
|
|
75
|
+
private detachRecorder;
|
|
76
|
+
}
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { arrayBufferToBase64 } from "@tanstack/ai-utils";
|
|
2
|
+
class AudioRecorder {
|
|
3
|
+
options;
|
|
4
|
+
recorder = null;
|
|
5
|
+
stream = null;
|
|
6
|
+
chunks = [];
|
|
7
|
+
startedAt = 0;
|
|
8
|
+
_state = "idle";
|
|
9
|
+
// True while start() is awaiting getUserMedia (state is still 'idle' then).
|
|
10
|
+
starting = false;
|
|
11
|
+
// Set by cancel()/teardown during that window so start() releases the
|
|
12
|
+
// freshly acquired stream instead of beginning a leaked recording.
|
|
13
|
+
pendingCancel = false;
|
|
14
|
+
listeners = /* @__PURE__ */ new Set();
|
|
15
|
+
stopResolve = null;
|
|
16
|
+
stopReject = null;
|
|
17
|
+
constructor(options = {}) {
|
|
18
|
+
this.options = options;
|
|
19
|
+
}
|
|
20
|
+
/** Feature-detect the browser media APIs. SSR/Worker-safe. */
|
|
21
|
+
static isSupported() {
|
|
22
|
+
return typeof navigator !== "undefined" && typeof navigator.mediaDevices !== "undefined" && typeof navigator.mediaDevices.getUserMedia === "function" && typeof MediaRecorder !== "undefined";
|
|
23
|
+
}
|
|
24
|
+
get state() {
|
|
25
|
+
return this._state;
|
|
26
|
+
}
|
|
27
|
+
subscribe(cb) {
|
|
28
|
+
this.listeners.add(cb);
|
|
29
|
+
return () => {
|
|
30
|
+
this.listeners.delete(cb);
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
setState(state) {
|
|
34
|
+
this._state = state;
|
|
35
|
+
for (const cb of this.listeners) {
|
|
36
|
+
cb(state);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
async start() {
|
|
40
|
+
if (this._state !== "idle" || this.starting) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
this.starting = true;
|
|
44
|
+
try {
|
|
45
|
+
const stream = await navigator.mediaDevices.getUserMedia({
|
|
46
|
+
audio: this.options.audio ?? true
|
|
47
|
+
});
|
|
48
|
+
if (this.pendingCancel) {
|
|
49
|
+
stream.getTracks().forEach((t) => t.stop());
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
this.stream = stream;
|
|
53
|
+
const wanted = this.options.mimeType;
|
|
54
|
+
const useMimeType = wanted && typeof MediaRecorder.isTypeSupported === "function" && MediaRecorder.isTypeSupported(wanted) ? wanted : void 0;
|
|
55
|
+
const recorder = useMimeType ? new MediaRecorder(stream, { mimeType: useMimeType }) : new MediaRecorder(stream);
|
|
56
|
+
this.chunks = [];
|
|
57
|
+
recorder.ondataavailable = (e) => {
|
|
58
|
+
if (e.data.size > 0) {
|
|
59
|
+
this.chunks.push(e.data);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
recorder.onstop = () => {
|
|
63
|
+
void this.finalize();
|
|
64
|
+
};
|
|
65
|
+
recorder.onerror = (event) => {
|
|
66
|
+
const detail = "error" in event && event.error instanceof Error ? event.error : new Error("Audio recording failed");
|
|
67
|
+
this.handleError(detail);
|
|
68
|
+
};
|
|
69
|
+
this.recorder = recorder;
|
|
70
|
+
this.startedAt = Date.now();
|
|
71
|
+
recorder.start();
|
|
72
|
+
this.setState("recording");
|
|
73
|
+
} catch (err) {
|
|
74
|
+
this.releaseStream();
|
|
75
|
+
this.recorder = null;
|
|
76
|
+
const error = err instanceof Error ? err : new Error("Failed to start recording");
|
|
77
|
+
this.setState("idle");
|
|
78
|
+
this.notifyError(error);
|
|
79
|
+
throw error;
|
|
80
|
+
} finally {
|
|
81
|
+
this.starting = false;
|
|
82
|
+
this.pendingCancel = false;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
stop() {
|
|
86
|
+
if (this._state !== "recording" || !this.recorder) {
|
|
87
|
+
return Promise.reject(
|
|
88
|
+
new Error("AudioRecorder.stop() called while not recording")
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
this.setState("stopping");
|
|
92
|
+
const recorder = this.recorder;
|
|
93
|
+
return new Promise((resolve, reject) => {
|
|
94
|
+
const watchdog = setTimeout(() => {
|
|
95
|
+
if (this._state !== "stopping") {
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
this.detachRecorder();
|
|
99
|
+
if (this.chunks.length > 0) {
|
|
100
|
+
void this.finalize();
|
|
101
|
+
} else {
|
|
102
|
+
this.handleError(
|
|
103
|
+
new Error("Recording stop timed out after 10s with no audio")
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
}, 1e4);
|
|
107
|
+
this.stopResolve = (rec) => {
|
|
108
|
+
clearTimeout(watchdog);
|
|
109
|
+
resolve(rec);
|
|
110
|
+
};
|
|
111
|
+
this.stopReject = (err) => {
|
|
112
|
+
clearTimeout(watchdog);
|
|
113
|
+
reject(err);
|
|
114
|
+
};
|
|
115
|
+
recorder.stop();
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
cancel() {
|
|
119
|
+
if (this.starting) {
|
|
120
|
+
this.pendingCancel = true;
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
if (this._state === "idle") {
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
const recorder = this.recorder;
|
|
127
|
+
if (recorder) {
|
|
128
|
+
this.detachRecorder();
|
|
129
|
+
try {
|
|
130
|
+
recorder.stop();
|
|
131
|
+
} catch (err) {
|
|
132
|
+
if (!(err instanceof DOMException && err.name === "InvalidStateError")) {
|
|
133
|
+
this.notifyError(
|
|
134
|
+
err instanceof Error ? err : new Error("Failed to stop recorder")
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
this.releaseStream();
|
|
140
|
+
this.recorder = null;
|
|
141
|
+
this.chunks = [];
|
|
142
|
+
const reject = this.stopReject;
|
|
143
|
+
this.stopResolve = null;
|
|
144
|
+
this.stopReject = null;
|
|
145
|
+
this.setState("idle");
|
|
146
|
+
reject?.(new Error("Recording cancelled"));
|
|
147
|
+
}
|
|
148
|
+
async finalize() {
|
|
149
|
+
const mimeType = this.recorder?.mimeType || "audio/webm";
|
|
150
|
+
const durationMs = Date.now() - this.startedAt;
|
|
151
|
+
try {
|
|
152
|
+
const blob = new Blob(this.chunks, { type: mimeType });
|
|
153
|
+
const base64 = arrayBufferToBase64(await blob.arrayBuffer());
|
|
154
|
+
const recording = {
|
|
155
|
+
blob,
|
|
156
|
+
base64,
|
|
157
|
+
mimeType,
|
|
158
|
+
durationMs,
|
|
159
|
+
part: {
|
|
160
|
+
type: "audio",
|
|
161
|
+
source: { type: "data", value: base64, mimeType }
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
this.releaseStream();
|
|
165
|
+
this.recorder = null;
|
|
166
|
+
this.chunks = [];
|
|
167
|
+
const resolve = this.stopResolve;
|
|
168
|
+
this.stopResolve = null;
|
|
169
|
+
this.stopReject = null;
|
|
170
|
+
this.setState("idle");
|
|
171
|
+
resolve?.(recording);
|
|
172
|
+
} catch (err) {
|
|
173
|
+
this.handleError(
|
|
174
|
+
err instanceof Error ? err : new Error("Failed to finalize recording")
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
handleError(error) {
|
|
179
|
+
this.releaseStream();
|
|
180
|
+
this.recorder = null;
|
|
181
|
+
this.chunks = [];
|
|
182
|
+
const reject = this.stopReject;
|
|
183
|
+
this.stopResolve = null;
|
|
184
|
+
this.stopReject = null;
|
|
185
|
+
this.setState("idle");
|
|
186
|
+
reject?.(error);
|
|
187
|
+
this.notifyError(error);
|
|
188
|
+
}
|
|
189
|
+
/** Invoke the user onError callback, isolating a throw so it can't disrupt
|
|
190
|
+
* internal state teardown or strand a pending promise. */
|
|
191
|
+
notifyError(error) {
|
|
192
|
+
try {
|
|
193
|
+
this.options.onError?.(error);
|
|
194
|
+
} catch {
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
releaseStream() {
|
|
198
|
+
this.stream?.getTracks().forEach((t) => t.stop());
|
|
199
|
+
this.stream = null;
|
|
200
|
+
}
|
|
201
|
+
/** Detach all event handlers so a stale recorder can't mutate our state. */
|
|
202
|
+
detachRecorder() {
|
|
203
|
+
const recorder = this.recorder;
|
|
204
|
+
if (!recorder) {
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
recorder.onstop = null;
|
|
208
|
+
recorder.onerror = null;
|
|
209
|
+
recorder.ondataavailable = null;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
export {
|
|
213
|
+
AudioRecorder
|
|
214
|
+
};
|
|
215
|
+
//# sourceMappingURL=audio-recorder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"audio-recorder.js","sources":["../../src/audio-recorder.ts"],"sourcesContent":["import { arrayBufferToBase64 } from '@tanstack/ai-utils'\nimport type { AudioPart } from '@tanstack/ai/client'\n\n/** Lifecycle state of an {@link AudioRecorder}. */\nexport type AudioRecorderState = 'idle' | 'recording' | 'stopping'\n\nexport interface AudioRecorderOptions {\n /** Constraints forwarded to `getUserMedia({ audio })`. Defaults to `true`. */\n audio?: MediaTrackConstraints | boolean\n /**\n * Preferred recorder mime type. Used only when\n * `MediaRecorder.isTypeSupported` reports it; otherwise the browser default\n * is used.\n */\n mimeType?: string\n /** Fired on `getUserMedia` rejection (permission denied) or recorder error. */\n onError?: (error: Error) => void\n}\n\n/**\n * Resolves the value `stop()` produces from a recorder transform callback.\n *\n * - If the callback returns a value — sync or async — that value's awaited type\n * is used (a returned `null` is a real value and is preserved).\n * - If the callback returns nothing (`void`/`undefined`), or is absent, falls\n * back to {@link AudioRecording}.\n *\n * @template TFn - The transform callback type (or undefined if not provided)\n */\nexport type InferAudioRecordingOutput<TFn> = TFn extends (\n recording: AudioRecording,\n) => infer R\n ? [Exclude<Awaited<R>, void | undefined>] extends [never]\n ? AudioRecording\n : Exclude<Awaited<R>, void | undefined>\n : AudioRecording\n\nexport interface AudioRecording {\n /** The raw recorded media blob. */\n blob: Blob\n /** Base64 of the recorded bytes (no `data:` prefix). */\n base64: string\n /** The recorder's native mime type, e.g. `audio/webm;codecs=opus`. */\n mimeType: string\n /** Recording length in milliseconds. */\n durationMs: number\n /**\n * Ready-to-use audio content part for `sendMessage`/generation prompts:\n * `{ type: 'audio', source: { type: 'data', value: base64, mimeType } }`.\n */\n part: AudioPart\n}\n\n/**\n * Framework-agnostic browser audio recorder. Wraps `getUserMedia` +\n * `MediaRecorder`, returns the recorder's native output (no transcode), and\n * builds a plug-and-play {@link AudioRecording.part}.\n */\nexport class AudioRecorder {\n private readonly options: AudioRecorderOptions\n private recorder: MediaRecorder | null = null\n private stream: MediaStream | null = null\n private chunks: Array<Blob> = []\n private startedAt = 0\n private _state: AudioRecorderState = 'idle'\n // True while start() is awaiting getUserMedia (state is still 'idle' then).\n private starting = false\n // Set by cancel()/teardown during that window so start() releases the\n // freshly acquired stream instead of beginning a leaked recording.\n private pendingCancel = false\n private readonly listeners = new Set<(state: AudioRecorderState) => void>()\n private stopResolve: ((recording: AudioRecording) => void) | null = null\n private stopReject: ((error: Error) => void) | null = null\n\n constructor(options: AudioRecorderOptions = {}) {\n this.options = options\n }\n\n /** Feature-detect the browser media APIs. SSR/Worker-safe. */\n static isSupported(): boolean {\n return (\n typeof navigator !== 'undefined' &&\n typeof navigator.mediaDevices !== 'undefined' &&\n typeof navigator.mediaDevices.getUserMedia === 'function' &&\n typeof MediaRecorder !== 'undefined'\n )\n }\n\n get state(): AudioRecorderState {\n return this._state\n }\n\n subscribe(cb: (state: AudioRecorderState) => void): () => void {\n this.listeners.add(cb)\n return () => {\n this.listeners.delete(cb)\n }\n }\n\n private setState(state: AudioRecorderState): void {\n this._state = state\n for (const cb of this.listeners) {\n cb(state)\n }\n }\n\n async start(): Promise<void> {\n if (this._state !== 'idle' || this.starting) {\n return\n }\n this.starting = true\n try {\n const stream = await navigator.mediaDevices.getUserMedia({\n audio: this.options.audio ?? true,\n })\n // cancel()/teardown ran while we were awaiting the mic: release the\n // freshly acquired stream and bail rather than starting a recording the\n // caller can no longer stop (a leaked live microphone).\n if (this.pendingCancel) {\n stream.getTracks().forEach((t) => t.stop())\n return\n }\n this.stream = stream\n const wanted = this.options.mimeType\n const useMimeType =\n wanted &&\n typeof MediaRecorder.isTypeSupported === 'function' &&\n MediaRecorder.isTypeSupported(wanted)\n ? wanted\n : undefined\n const recorder = useMimeType\n ? new MediaRecorder(stream, { mimeType: useMimeType })\n : new MediaRecorder(stream)\n this.chunks = []\n recorder.ondataavailable = (e) => {\n if (e.data.size > 0) {\n this.chunks.push(e.data)\n }\n }\n recorder.onstop = () => {\n void this.finalize()\n }\n recorder.onerror = (event) => {\n const detail =\n 'error' in event && event.error instanceof Error\n ? event.error\n : new Error('Audio recording failed')\n this.handleError(detail)\n }\n this.recorder = recorder\n this.startedAt = Date.now()\n recorder.start()\n this.setState('recording')\n } catch (err) {\n this.releaseStream()\n this.recorder = null\n const error =\n err instanceof Error ? err : new Error('Failed to start recording')\n this.setState('idle')\n this.notifyError(error)\n throw error\n } finally {\n this.starting = false\n // Reset here (not before the await) so a cancel() that arrives mid-start\n // is observed above; clearing it afterward keeps the next start() clean.\n this.pendingCancel = false\n }\n }\n\n stop(): Promise<AudioRecording> {\n if (this._state !== 'recording' || !this.recorder) {\n return Promise.reject(\n new Error('AudioRecorder.stop() called while not recording'),\n )\n }\n this.setState('stopping')\n const recorder = this.recorder\n return new Promise<AudioRecording>((resolve, reject) => {\n // Some browsers/codecs never fire onstop; this watchdog unwedges the\n // recorder instead of leaking this promise forever.\n const watchdog = setTimeout(() => {\n if (this._state !== 'stopping') {\n return\n }\n // Detach handlers so a late onstop/onerror from the stalled recorder\n // can't reach back in and fire finalize()/onError a second time.\n this.detachRecorder()\n if (this.chunks.length > 0) {\n // onstop never fired, but ondataavailable already delivered the\n // audio — finalize from the buffered chunks rather than discarding a\n // recording the user successfully captured.\n void this.finalize()\n } else {\n this.handleError(\n new Error('Recording stop timed out after 10s with no audio'),\n )\n }\n }, 10_000)\n this.stopResolve = (rec) => {\n clearTimeout(watchdog)\n resolve(rec)\n }\n this.stopReject = (err) => {\n clearTimeout(watchdog)\n reject(err)\n }\n recorder.stop()\n })\n }\n\n cancel(): void {\n if (this.starting) {\n // A start() is awaiting getUserMedia; flag it so the resolved stream is\n // released instead of beginning a recording with no handle to stop it.\n this.pendingCancel = true\n return\n }\n if (this._state === 'idle') {\n return\n }\n const recorder = this.recorder\n if (recorder) {\n // Detach handlers so finalize()/onError never run for a discarded\n // recording.\n this.detachRecorder()\n try {\n recorder.stop()\n } catch (err) {\n // Stopping an already-inactive recorder throws InvalidStateError —\n // that's expected here. Anything else is unexpected; surface it rather\n // than swallowing it silently.\n if (\n !(err instanceof DOMException && err.name === 'InvalidStateError')\n ) {\n this.notifyError(\n err instanceof Error ? err : new Error('Failed to stop recorder'),\n )\n }\n }\n }\n this.releaseStream()\n this.recorder = null\n this.chunks = []\n const reject = this.stopReject\n this.stopResolve = null\n this.stopReject = null\n this.setState('idle')\n reject?.(new Error('Recording cancelled'))\n }\n\n private async finalize(): Promise<void> {\n const mimeType = this.recorder?.mimeType || 'audio/webm'\n const durationMs = Date.now() - this.startedAt\n try {\n const blob = new Blob(this.chunks, { type: mimeType })\n const base64 = arrayBufferToBase64(await blob.arrayBuffer())\n const recording: AudioRecording = {\n blob,\n base64,\n mimeType,\n durationMs,\n part: {\n type: 'audio',\n source: { type: 'data', value: base64, mimeType },\n },\n }\n this.releaseStream()\n this.recorder = null\n this.chunks = []\n const resolve = this.stopResolve\n this.stopResolve = null\n this.stopReject = null\n this.setState('idle')\n resolve?.(recording)\n } catch (err) {\n this.handleError(\n err instanceof Error ? err : new Error('Failed to finalize recording'),\n )\n }\n }\n\n private handleError(error: Error): void {\n this.releaseStream()\n this.recorder = null\n this.chunks = []\n const reject = this.stopReject\n this.stopResolve = null\n this.stopReject = null\n this.setState('idle')\n // Settle the pending stop() promise before invoking the user callback so a\n // throwing onError can't strand the awaiter (the two error channels are\n // independent — see start()/stop() docs).\n reject?.(error)\n this.notifyError(error)\n }\n\n /** Invoke the user onError callback, isolating a throw so it can't disrupt\n * internal state teardown or strand a pending promise. */\n private notifyError(error: Error): void {\n try {\n this.options.onError?.(error)\n } catch {\n // A user onError that throws must not propagate into recorder internals.\n }\n }\n\n private releaseStream(): void {\n this.stream?.getTracks().forEach((t) => t.stop())\n this.stream = null\n }\n\n /** Detach all event handlers so a stale recorder can't mutate our state. */\n private detachRecorder(): void {\n const recorder = this.recorder\n if (!recorder) {\n return\n }\n recorder.onstop = null\n recorder.onerror = null\n recorder.ondataavailable = null\n }\n}\n"],"names":[],"mappings":";AA0DO,MAAM,cAAc;AAAA,EACR;AAAA,EACT,WAAiC;AAAA,EACjC,SAA6B;AAAA,EAC7B,SAAsB,CAAA;AAAA,EACtB,YAAY;AAAA,EACZ,SAA6B;AAAA;AAAA,EAE7B,WAAW;AAAA;AAAA;AAAA,EAGX,gBAAgB;AAAA,EACP,gCAAgB,IAAA;AAAA,EACzB,cAA4D;AAAA,EAC5D,aAA8C;AAAA,EAEtD,YAAY,UAAgC,IAAI;AAC9C,SAAK,UAAU;AAAA,EACjB;AAAA;AAAA,EAGA,OAAO,cAAuB;AAC5B,WACE,OAAO,cAAc,eACrB,OAAO,UAAU,iBAAiB,eAClC,OAAO,UAAU,aAAa,iBAAiB,cAC/C,OAAO,kBAAkB;AAAA,EAE7B;AAAA,EAEA,IAAI,QAA4B;AAC9B,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,UAAU,IAAqD;AAC7D,SAAK,UAAU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AAQ,UAAU,KAAK;AAAA,IAC9B,QAAQ;AAAA,IAER;AAAA,EACF;AAAA,EAEQ,gBAAsB;AAC5B,SAAK,QAAQ,YAAY,QAAQ,CAAC,MAAM,EAAE,MAAM;AAChD,SAAK,SAAS;AAAA,EAChB;AAAA;AAAA,EAGQ,iBAAuB;AAC7B,UAAM,WAAW,KAAK;AACtB,QAAI,CAAC,UAAU;AACb;AAAA,IACF;AACA,aAAS,SAAS;AAClB,aAAS,UAAU;AACnB,aAAS,kBAAkB;AAAA,EAC7B;AACF;"}
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
import { AnyClientTool, ModelMessage, StreamChunk } from '@tanstack/ai/client';
|
|
2
|
+
import { ConnectionAdapter } from './connection-adapters.js';
|
|
3
|
+
import { ChatClientOptions, ChatClientState, ChatFetcher, ConnectionStatus, MultimodalContent, UIMessage } from './types.js';
|
|
4
|
+
type ChatClientUpdateOptionsWithoutContext<TTools extends ReadonlyArray<AnyClientTool>> = {
|
|
5
|
+
connection?: ConnectionAdapter;
|
|
6
|
+
fetcher?: ChatFetcher;
|
|
7
|
+
/** @deprecated Use `forwardedProps` instead. */
|
|
8
|
+
body?: Record<string, any>;
|
|
9
|
+
forwardedProps?: Record<string, any>;
|
|
10
|
+
tools?: TTools;
|
|
11
|
+
onResponse?: (response?: Response) => void | Promise<void>;
|
|
12
|
+
onChunk?: (chunk: StreamChunk) => void;
|
|
13
|
+
onFinish?: (message: UIMessage) => void;
|
|
14
|
+
onError?: (error: Error) => void;
|
|
15
|
+
onSubscriptionChange?: (isSubscribed: boolean) => void;
|
|
16
|
+
onConnectionStatusChange?: (status: ConnectionStatus) => void;
|
|
17
|
+
onSessionGeneratingChange?: (isGenerating: boolean) => void;
|
|
18
|
+
onCustomEvent?: (eventType: string, data: unknown, context: {
|
|
19
|
+
toolCallId?: string;
|
|
20
|
+
}) => void;
|
|
21
|
+
};
|
|
22
|
+
type ClientToolResult = {
|
|
23
|
+
toolCallId: string;
|
|
24
|
+
tool: string;
|
|
25
|
+
output: any;
|
|
26
|
+
state?: 'output-available' | 'output-error';
|
|
27
|
+
errorText?: string;
|
|
28
|
+
};
|
|
29
|
+
export declare class ChatClient<TTools extends ReadonlyArray<AnyClientTool> = any, TContext = unknown> {
|
|
30
|
+
private readonly processor;
|
|
31
|
+
private connection;
|
|
32
|
+
private readonly uniqueId;
|
|
33
|
+
private readonly threadId;
|
|
34
|
+
private readonly persistor?;
|
|
35
|
+
private currentRunId;
|
|
36
|
+
private bodyOption;
|
|
37
|
+
private forwardedPropsOption;
|
|
38
|
+
private context;
|
|
39
|
+
private pendingMessageBody;
|
|
40
|
+
private isLoading;
|
|
41
|
+
private isSubscribed;
|
|
42
|
+
private error;
|
|
43
|
+
private status;
|
|
44
|
+
private connectionStatus;
|
|
45
|
+
private abortController;
|
|
46
|
+
private readonly clientToolsRef;
|
|
47
|
+
private readonly devtoolsBridge;
|
|
48
|
+
/**
|
|
49
|
+
* Alias for `this.events`. The bridge installs an
|
|
50
|
+
* emitter that auto-attaches run/thread context and auto-emits a
|
|
51
|
+
* snapshot after every event, so chat-client only ever calls
|
|
52
|
+
* `this.events.X(...)` exactly like it did before devtools landed.
|
|
53
|
+
*/
|
|
54
|
+
private readonly events;
|
|
55
|
+
private currentStreamId;
|
|
56
|
+
private currentMessageId;
|
|
57
|
+
private readonly postStreamActions;
|
|
58
|
+
private readonly pendingToolExecutions;
|
|
59
|
+
private activeClientTools;
|
|
60
|
+
private activeContext;
|
|
61
|
+
private continuationPending;
|
|
62
|
+
private subscriptionAbortController;
|
|
63
|
+
private processingResolve;
|
|
64
|
+
private errorReportedGeneration;
|
|
65
|
+
private streamGeneration;
|
|
66
|
+
private continuationSkipped;
|
|
67
|
+
private draining;
|
|
68
|
+
private sessionGenerating;
|
|
69
|
+
private readonly activeRunIds;
|
|
70
|
+
private devtoolsMounted;
|
|
71
|
+
private readonly callbacksRef;
|
|
72
|
+
constructor(options: ChatClientOptions<TTools, TContext>);
|
|
73
|
+
mountDevtools(): void;
|
|
74
|
+
/**
|
|
75
|
+
* Drain a runId-less RUN_ERROR that belongs to a cleared run the client is
|
|
76
|
+
* still tracking. The persistor owns the cleared-run bookkeeping; the client
|
|
77
|
+
* owns the active-run / session / processing state.
|
|
78
|
+
*/
|
|
79
|
+
private drainIgnoredRunlessChunk;
|
|
80
|
+
private updateRunLifecycle;
|
|
81
|
+
private generateUniqueId;
|
|
82
|
+
private setIsLoading;
|
|
83
|
+
private setStatus;
|
|
84
|
+
private setIsSubscribed;
|
|
85
|
+
private setConnectionStatus;
|
|
86
|
+
private setSessionGenerating;
|
|
87
|
+
private resetSessionGenerating;
|
|
88
|
+
private setError;
|
|
89
|
+
private buildDevtoolsBridgeOptions;
|
|
90
|
+
private getDevtoolsSnapshot;
|
|
91
|
+
private findMessageIdForToolCall;
|
|
92
|
+
private abortSubscriptionLoop;
|
|
93
|
+
private resolveProcessing;
|
|
94
|
+
private cancelInFlightStream;
|
|
95
|
+
private reportStreamError;
|
|
96
|
+
/**
|
|
97
|
+
* Start the background subscription loop.
|
|
98
|
+
*/
|
|
99
|
+
private startSubscription;
|
|
100
|
+
/**
|
|
101
|
+
* Consume chunks from the connection subscription.
|
|
102
|
+
*/
|
|
103
|
+
private consumeSubscription;
|
|
104
|
+
/**
|
|
105
|
+
* Ensure subscription loop is running, starting it if needed.
|
|
106
|
+
*/
|
|
107
|
+
private ensureSubscription;
|
|
108
|
+
/**
|
|
109
|
+
* Create a promise that resolves when onStreamEnd fires.
|
|
110
|
+
* Used by streamResponse to await processing completion.
|
|
111
|
+
*/
|
|
112
|
+
private waitForProcessing;
|
|
113
|
+
/**
|
|
114
|
+
* Send a message and stream the response.
|
|
115
|
+
* Supports both simple string content and multimodal content (images, audio, video, documents).
|
|
116
|
+
*
|
|
117
|
+
* @param content - The message content. Can be:
|
|
118
|
+
* - A simple string for text-only messages
|
|
119
|
+
* - A MultimodalContent object with content array and optional custom ID
|
|
120
|
+
* @param body - Optional body parameters to merge with the client's base body for this request.
|
|
121
|
+
* Uses shallow merge with per-message body taking priority.
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* ```ts
|
|
125
|
+
* // Simple text message
|
|
126
|
+
* await client.sendMessage('Hello!')
|
|
127
|
+
*
|
|
128
|
+
* // Text message with custom body params
|
|
129
|
+
* await client.sendMessage('Hello!', { temperature: 0.7 })
|
|
130
|
+
*
|
|
131
|
+
* // Multimodal message with image
|
|
132
|
+
* await client.sendMessage({
|
|
133
|
+
* content: [
|
|
134
|
+
* { type: 'text', content: 'What is in this image?' },
|
|
135
|
+
* { type: 'image', source: { type: 'url', value: 'https://example.com/photo.jpg' } }
|
|
136
|
+
* ]
|
|
137
|
+
* })
|
|
138
|
+
*
|
|
139
|
+
* // Multimodal message with custom ID and body params
|
|
140
|
+
* await client.sendMessage(
|
|
141
|
+
* {
|
|
142
|
+
* content: [
|
|
143
|
+
* { type: 'text', content: 'Describe this audio' },
|
|
144
|
+
* { type: 'audio', source: { type: 'data', value: 'base64...' } }
|
|
145
|
+
* ],
|
|
146
|
+
* id: 'custom-message-id'
|
|
147
|
+
* },
|
|
148
|
+
* { model: 'gpt-4-audio' }
|
|
149
|
+
* )
|
|
150
|
+
* ```
|
|
151
|
+
*/
|
|
152
|
+
sendMessage(content: string | MultimodalContent, body?: Record<string, any>): Promise<void>;
|
|
153
|
+
/**
|
|
154
|
+
* Normalize the message input to extract content and optional id.
|
|
155
|
+
* Trims string content automatically.
|
|
156
|
+
*/
|
|
157
|
+
private normalizeMessageInput;
|
|
158
|
+
/**
|
|
159
|
+
* Append a message and stream the response
|
|
160
|
+
*/
|
|
161
|
+
append(message: UIMessage | ModelMessage): Promise<void>;
|
|
162
|
+
/**
|
|
163
|
+
* Stream a response from the LLM.
|
|
164
|
+
* Returns true if the stream completed successfully, false on abort or error.
|
|
165
|
+
*/
|
|
166
|
+
private streamResponse;
|
|
167
|
+
/**
|
|
168
|
+
* Start the client subscription loop.
|
|
169
|
+
* This controls the connection lifecycle independently from request lifecycle.
|
|
170
|
+
*/
|
|
171
|
+
subscribe(options?: {
|
|
172
|
+
restart?: boolean;
|
|
173
|
+
}): void;
|
|
174
|
+
/**
|
|
175
|
+
* Unsubscribe and fully tear down live behavior.
|
|
176
|
+
* This aborts an in-flight request and the subscription loop.
|
|
177
|
+
*/
|
|
178
|
+
unsubscribe(): void;
|
|
179
|
+
/**
|
|
180
|
+
* Reload the last assistant message
|
|
181
|
+
*/
|
|
182
|
+
reload(): Promise<void>;
|
|
183
|
+
/**
|
|
184
|
+
* Stop the current stream
|
|
185
|
+
*/
|
|
186
|
+
stop(): void;
|
|
187
|
+
/**
|
|
188
|
+
* Clear all messages
|
|
189
|
+
*/
|
|
190
|
+
clear(): void;
|
|
191
|
+
/**
|
|
192
|
+
* Add the result of a client-side tool execution
|
|
193
|
+
*/
|
|
194
|
+
addToolResult(result: ClientToolResult): Promise<void>;
|
|
195
|
+
private addToolResultForClientTool;
|
|
196
|
+
private validateClientToolOutput;
|
|
197
|
+
/**
|
|
198
|
+
* Respond to a tool approval request
|
|
199
|
+
*/
|
|
200
|
+
addToolApprovalResponse(response: {
|
|
201
|
+
id: string;
|
|
202
|
+
approved: boolean;
|
|
203
|
+
}): Promise<void>;
|
|
204
|
+
/**
|
|
205
|
+
* Queue an action to be executed after the current stream ends
|
|
206
|
+
*/
|
|
207
|
+
private queuePostStreamAction;
|
|
208
|
+
/**
|
|
209
|
+
* Drain and execute all queued post-stream actions
|
|
210
|
+
*/
|
|
211
|
+
private drainPostStreamActions;
|
|
212
|
+
/**
|
|
213
|
+
* Check if we should continue the flow and do so if needed
|
|
214
|
+
*/
|
|
215
|
+
private checkForContinuation;
|
|
216
|
+
/**
|
|
217
|
+
* Check if all tool calls are complete and we should auto-send.
|
|
218
|
+
* Requires that there is at least one tool call in the last assistant message;
|
|
219
|
+
* a text-only response has nothing to auto-send.
|
|
220
|
+
*/
|
|
221
|
+
private shouldAutoSend;
|
|
222
|
+
/**
|
|
223
|
+
* Get current messages
|
|
224
|
+
*/
|
|
225
|
+
getMessages(): Array<UIMessage<TTools>>;
|
|
226
|
+
/**
|
|
227
|
+
* Get loading state
|
|
228
|
+
*/
|
|
229
|
+
getIsLoading(): boolean;
|
|
230
|
+
/**
|
|
231
|
+
* Get current status
|
|
232
|
+
*/
|
|
233
|
+
getStatus(): ChatClientState;
|
|
234
|
+
/**
|
|
235
|
+
* Get whether the subscription loop is active
|
|
236
|
+
*/
|
|
237
|
+
getIsSubscribed(): boolean;
|
|
238
|
+
/**
|
|
239
|
+
* Get current connection lifecycle status
|
|
240
|
+
*/
|
|
241
|
+
getConnectionStatus(): ConnectionStatus;
|
|
242
|
+
/**
|
|
243
|
+
* Whether the shared session is actively generating.
|
|
244
|
+
* Derived from stream run events (RUN_STARTED / RUN_FINISHED / RUN_ERROR).
|
|
245
|
+
* Unlike `isLoading` (request-local), this reflects shared generation
|
|
246
|
+
* activity visible to all subscribers (e.g. across tabs/devices).
|
|
247
|
+
*/
|
|
248
|
+
getSessionGenerating(): boolean;
|
|
249
|
+
/**
|
|
250
|
+
* Get current error
|
|
251
|
+
*/
|
|
252
|
+
getError(): Error | undefined;
|
|
253
|
+
/**
|
|
254
|
+
* Manually set messages
|
|
255
|
+
*/
|
|
256
|
+
setMessagesManually(messages: Array<UIMessage<TTools>>): void;
|
|
257
|
+
/**
|
|
258
|
+
* Update options refs (for use in React hooks to avoid recreating client)
|
|
259
|
+
*/
|
|
260
|
+
updateOptions(options: ChatClientUpdateOptionsWithoutContext<TTools>): void;
|
|
261
|
+
updateOptions(options: ChatClientUpdateOptionsWithoutContext<TTools> & Pick<ChatClientOptions<TTools, TContext>, 'context'>): void;
|
|
262
|
+
dispose(): void;
|
|
263
|
+
}
|
|
264
|
+
export {};
|