@tanstack/ai-client 0.12.0 → 0.13.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.
@@ -0,0 +1,276 @@
1
+ import { AnyClientTool, StreamChunk } from '@tanstack/ai';
2
+ import { ChatClientEventContext, ChatClientEventEmitter, ChatClientRunEventContext } from './events.js';
3
+ import { ChatClientState, ConnectionStatus, UIMessage } from './types.js';
4
+ export interface AIDevtoolsDisplayOptions {
5
+ name?: string;
6
+ }
7
+ export interface AIDevtoolsClientMetadata extends AIDevtoolsDisplayOptions {
8
+ framework?: string;
9
+ hookName: string;
10
+ outputKind?: 'chat' | 'text' | 'structured' | 'image' | 'video' | 'audio';
11
+ }
12
+ export interface AIDevtoolsGenerationProgress {
13
+ value: number;
14
+ message?: string;
15
+ }
16
+ export interface AIDevtoolsGenerationMediaItem {
17
+ src: string;
18
+ sourceType: 'url' | 'base64';
19
+ mimeType?: string;
20
+ format?: string;
21
+ duration?: number;
22
+ }
23
+ export interface AIDevtoolsGenerationVideoJob {
24
+ jobId: string;
25
+ status?: string;
26
+ progress?: number;
27
+ error?: string;
28
+ }
29
+ export type AIDevtoolsGenerationPreview = {
30
+ kind: 'image';
31
+ items: Array<AIDevtoolsGenerationMediaItem>;
32
+ } | {
33
+ kind: 'audio';
34
+ items: Array<AIDevtoolsGenerationMediaItem>;
35
+ } | {
36
+ kind: 'video';
37
+ items: Array<AIDevtoolsGenerationMediaItem>;
38
+ job?: AIDevtoolsGenerationVideoJob;
39
+ } | {
40
+ kind: 'text';
41
+ text: string;
42
+ } | {
43
+ kind: 'structured';
44
+ value: unknown;
45
+ } | {
46
+ kind: 'empty';
47
+ };
48
+ export type AIDevtoolsGenerationRunStatus = 'idle' | 'generating' | 'success' | 'error' | 'cancelled';
49
+ export interface AIDevtoolsGenerationRunSnapshot<TOutput = unknown> {
50
+ id: string;
51
+ input: unknown;
52
+ result: TOutput | null;
53
+ preview: AIDevtoolsGenerationPreview;
54
+ progress: AIDevtoolsGenerationProgress | null;
55
+ status: AIDevtoolsGenerationRunStatus;
56
+ isLoading: boolean;
57
+ startedAt: number;
58
+ updatedAt: number;
59
+ completedAt?: number;
60
+ error?: string;
61
+ jobId?: string | null;
62
+ videoStatus?: unknown;
63
+ }
64
+ export interface AIDevtoolsGenerationPreviewInput {
65
+ outputKind?: AIDevtoolsClientMetadata['outputKind'];
66
+ result: unknown;
67
+ videoStatus?: unknown;
68
+ }
69
+ export interface AIDevtoolsChatSnapshot {
70
+ [key: string]: unknown;
71
+ messages: Array<UIMessage>;
72
+ status: ChatClientState;
73
+ isLoading: boolean;
74
+ isSubscribed: boolean;
75
+ connectionStatus: ConnectionStatus;
76
+ sessionGenerating: boolean;
77
+ activeRunIds: Array<string>;
78
+ error?: string;
79
+ }
80
+ export declare function createAIDevtoolsGenerationPreview(input: AIDevtoolsGenerationPreviewInput): AIDevtoolsGenerationPreview;
81
+ export interface AIDevtoolsToolFixture {
82
+ fixtureId?: string;
83
+ hookId?: string;
84
+ threadId?: string;
85
+ runId?: string;
86
+ toolName: string;
87
+ input: unknown;
88
+ output: unknown;
89
+ execute?: boolean;
90
+ message?: {
91
+ id: string;
92
+ role: UIMessage['role'];
93
+ parts: Array<unknown>;
94
+ createdAt?: number | string;
95
+ };
96
+ toolCallId?: string;
97
+ messageId?: string;
98
+ errorText?: string;
99
+ }
100
+ type AIDevtoolsRunEventType = 'run:created' | 'run:started' | 'run:updated' | 'run:completed' | 'run:errored' | 'run:cancelled';
101
+ type AIDevtoolsRunStatus = 'created' | 'started' | 'updated' | 'completed' | 'errored' | 'cancelled';
102
+ export interface AIDevtoolsBridgeOptions<TSnapshot extends object> {
103
+ hookId: string;
104
+ threadId?: string;
105
+ clientId: string;
106
+ metadata: AIDevtoolsClientMetadata;
107
+ getSnapshot: () => TSnapshot;
108
+ getTools?: () => Iterable<AnyClientTool>;
109
+ applyToolFixture?: (fixture: AIDevtoolsToolFixture) => void | Promise<void>;
110
+ }
111
+ export declare class ClientDevtoolsBridge<TSnapshot extends object> {
112
+ protected readonly options: AIDevtoolsBridgeOptions<TSnapshot>;
113
+ private readonly bridgeId;
114
+ private readonly unsubscribers;
115
+ private disposed;
116
+ private superseded;
117
+ private registered;
118
+ constructor(options: AIDevtoolsBridgeOptions<TSnapshot>);
119
+ emitRegistered(): void;
120
+ emitUpdated(): void;
121
+ emitSnapshot(): void;
122
+ emitToolsRegistered(): void;
123
+ emitRunLifecycle(eventType: AIDevtoolsRunEventType, runId: string, status: AIDevtoolsRunStatus, options?: {
124
+ error?: string;
125
+ }): void;
126
+ deactivate(): void;
127
+ supersede(): void;
128
+ dispose(): void;
129
+ private prepareForEmit;
130
+ private prepareForMountEmit;
131
+ private activate;
132
+ private handleRequestState;
133
+ private handleToolFixtureApply;
134
+ private matchesFixtureTarget;
135
+ private createEnvelope;
136
+ private createMetadataPayload;
137
+ }
138
+ export interface ChatDevtoolsBridgeOptions extends AIDevtoolsBridgeOptions<AIDevtoolsChatSnapshot> {
139
+ getMessages: () => Array<UIMessage>;
140
+ setMessages: (messages: Array<UIMessage>) => void;
141
+ addToolResult: (toolCallId: string, output: unknown, errorText?: string) => void;
142
+ generateId: (prefix: string) => string;
143
+ }
144
+ export declare class ChatDevtoolsBridge extends ClientDevtoolsBridge<AIDevtoolsChatSnapshot> {
145
+ readonly events: ChatClientEventEmitter;
146
+ private readonly chatOptions;
147
+ private currentRunId;
148
+ private currentRunThreadId;
149
+ private currentStreamId;
150
+ private lastStreamId;
151
+ private lastRunEventContext;
152
+ constructor(options: ChatDevtoolsBridgeOptions);
153
+ setCurrentStreamId(streamId: string | null): void;
154
+ /**
155
+ * Called by the auto-attaching emitter every time it sees a non-empty
156
+ * streamId pass through. Lets devtools track the latest stream id
157
+ * without the chat client wiring it up explicitly.
158
+ */
159
+ recordStreamId(streamId: string): void;
160
+ mountWithTools(initialMessageCount: number): void;
161
+ notifyToolsChanged(): void;
162
+ getCurrentStreamId(): string | null;
163
+ getLastStreamId(): string | null;
164
+ resolveStreamId(): string;
165
+ beginRun(runId: string, threadId: string): void;
166
+ observeChunk(chunk: StreamChunk): void;
167
+ getCurrentRunEventContext(): ChatClientRunEventContext | undefined;
168
+ getCurrentOrLastRunEventContext(): ChatClientRunEventContext | undefined;
169
+ findToolCallContext(toolCallId: string): ChatClientEventContext;
170
+ /**
171
+ * Entry point invoked when the devtools panel emits
172
+ * `devtools:tool-fixture:apply`. The chat client never calls this
173
+ * directly; it is wired through the base bridge's fixture subscription.
174
+ */
175
+ applyFixture(fixture: AIDevtoolsToolFixture): Promise<void>;
176
+ private executeFixture;
177
+ private addToolResultForFixture;
178
+ private createReplayMessageFromFixture;
179
+ private cloneFixtureSourceMessage;
180
+ private createFixtureToolCallIdMap;
181
+ private resolveFixtureMessageId;
182
+ private resolveFixtureToolCallId;
183
+ private findClientTool;
184
+ }
185
+ export interface AIDevtoolsGenerationSnapshotBase<TOutput> {
186
+ input: unknown;
187
+ result: TOutput | null;
188
+ preview: AIDevtoolsGenerationPreview;
189
+ progress: AIDevtoolsGenerationProgress | null;
190
+ status: AIDevtoolsGenerationRunStatus;
191
+ isLoading: boolean;
192
+ activeRunId: string | null;
193
+ runs: Array<AIDevtoolsGenerationRunSnapshot<TOutput>>;
194
+ error?: string;
195
+ }
196
+ export interface GenerationDevtoolsBridgeOptions<TOutput> extends Omit<AIDevtoolsBridgeOptions<AIDevtoolsGenerationSnapshotBase<TOutput>>, 'getSnapshot'> {
197
+ getCoreState: () => GenerationDevtoolsCoreState<TOutput>;
198
+ maxRuns?: number;
199
+ }
200
+ export interface GenerationDevtoolsCoreState<TOutput> {
201
+ input: unknown;
202
+ result: TOutput | null;
203
+ progress: AIDevtoolsGenerationProgress | null;
204
+ status: AIDevtoolsGenerationRunStatus;
205
+ isLoading: boolean;
206
+ error?: string;
207
+ }
208
+ export interface GenerationRunPatch<TOutput> {
209
+ input?: unknown;
210
+ result?: TOutput | null;
211
+ preview?: AIDevtoolsGenerationPreview;
212
+ progress?: AIDevtoolsGenerationProgress | null;
213
+ status?: AIDevtoolsGenerationRunStatus;
214
+ isLoading?: boolean;
215
+ completedAt?: number;
216
+ error?: string;
217
+ clearError?: boolean;
218
+ }
219
+ export declare class GenerationDevtoolsBridge<TOutput> extends ClientDevtoolsBridge<AIDevtoolsGenerationSnapshotBase<TOutput>> {
220
+ protected activeRunId: string | null;
221
+ protected activeRunStarted: boolean;
222
+ protected devtoolsRuns: Array<AIDevtoolsGenerationRunSnapshot<TOutput>>;
223
+ protected readonly maxRuns: number;
224
+ protected readonly getCoreState: () => GenerationDevtoolsCoreState<TOutput>;
225
+ constructor(options: GenerationDevtoolsBridgeOptions<TOutput>);
226
+ beginRun(input: unknown): string;
227
+ ensureRunStarted(runId: string): void;
228
+ finishRun(runId: string, eventType: 'run:completed' | 'run:errored' | 'run:cancelled', status: 'completed' | 'errored' | 'cancelled', error?: string): void;
229
+ getActiveRunId(): string | null;
230
+ /** Clear all per-run history. Called when the client `reset()`s. */
231
+ resetRuns(): void;
232
+ /** Record state changes from the client and emit the matching snapshot. */
233
+ recordResultChange(): void;
234
+ recordLoadingChange(): void;
235
+ recordErrorChange(error: Error | undefined): void;
236
+ recordStatusChange(status: AIDevtoolsGenerationRunStatus): void;
237
+ recordProgressChange(): void;
238
+ /** Emit the latest snapshot without touching run state. */
239
+ emitState(): void;
240
+ protected buildSnapshot(): AIDevtoolsGenerationSnapshotBase<TOutput>;
241
+ protected updateActiveRun(patch: GenerationRunPatch<TOutput>): void;
242
+ protected upsertRun(runId: string, patch: GenerationRunPatch<TOutput>): void;
243
+ protected renameRun(previousRunId: string, nextRunId: string): void;
244
+ protected getProgress(): AIDevtoolsGenerationProgress | null;
245
+ protected completeProgress(): AIDevtoolsGenerationProgress | null;
246
+ protected createPreview(result: TOutput | null): AIDevtoolsGenerationPreview;
247
+ protected generateRunId(): string;
248
+ }
249
+ export interface AIDevtoolsVideoSnapshotBase<TOutput> extends AIDevtoolsGenerationSnapshotBase<TOutput> {
250
+ jobId: string | null;
251
+ videoStatus: unknown;
252
+ }
253
+ export interface VideoDevtoolsCoreState<TOutput> extends GenerationDevtoolsCoreState<TOutput> {
254
+ jobId: string | null;
255
+ videoStatus: unknown;
256
+ }
257
+ export interface VideoDevtoolsBridgeOptions<TOutput> extends Omit<GenerationDevtoolsBridgeOptions<TOutput>, 'getCoreState'> {
258
+ getCoreState: () => VideoDevtoolsCoreState<TOutput>;
259
+ }
260
+ export interface VideoRunPatch<TOutput> extends GenerationRunPatch<TOutput> {
261
+ jobId?: string | null;
262
+ videoStatus?: unknown;
263
+ }
264
+ export declare class VideoDevtoolsBridge<TOutput> extends GenerationDevtoolsBridge<TOutput> {
265
+ constructor(options: VideoDevtoolsBridgeOptions<TOutput>);
266
+ recordJobIdChange(): void;
267
+ recordVideoStatusChange(): void;
268
+ protected buildSnapshot(): AIDevtoolsVideoSnapshotBase<TOutput>;
269
+ protected upsertRun(runId: string, patch: VideoRunPatch<TOutput>): void;
270
+ protected createPreview(result: TOutput | null): AIDevtoolsGenerationPreview;
271
+ private createVideoPreview;
272
+ }
273
+ export declare function createChatDevtoolsBridge(options: ChatDevtoolsBridgeOptions): ChatDevtoolsBridge;
274
+ export declare function createGenerationDevtoolsBridge<TOutput>(options: GenerationDevtoolsBridgeOptions<TOutput>): GenerationDevtoolsBridge<TOutput>;
275
+ export declare function createVideoDevtoolsBridge<TOutput>(options: VideoDevtoolsBridgeOptions<TOutput>): VideoDevtoolsBridge<TOutput>;
276
+ export {};