@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,205 @@
1
+ // No-op devtools bridge implementations + factories. The chat / generation /
2
+ // video clients import the real bridge classes as types only and accept a
3
+ // factory in options; when no factory is supplied they fall back to the
4
+ // no-op factories here, which never touch `aiEventClient` or any of the
5
+ // heavy preview/fixture machinery in `./devtools`. This keeps `./devtools`
6
+ // outside the main-entry import graph — consumers opt into functional
7
+ // devtools via `@tanstack/ai-client/devtools` (see `package.json#exports`).
8
+ import { ChatClientEventEmitter } from './events'
9
+ import type {
10
+ AIDevtoolsToolFixture,
11
+ ChatDevtoolsBridge,
12
+ ChatDevtoolsBridgeOptions,
13
+ GenerationDevtoolsBridge,
14
+ GenerationDevtoolsBridgeOptions,
15
+ VideoDevtoolsBridge,
16
+ VideoDevtoolsBridgeOptions,
17
+ } from './devtools'
18
+ import type { StreamChunk } from '@tanstack/ai'
19
+ import type {
20
+ ChatClientEventContext,
21
+ ChatClientRunEventContext,
22
+ } from './events'
23
+
24
+ export type ChatDevtoolsBridgeFactory = (
25
+ options: ChatDevtoolsBridgeOptions,
26
+ ) => ChatDevtoolsBridge
27
+
28
+ export type GenerationDevtoolsBridgeFactory = <TOutput>(
29
+ options: GenerationDevtoolsBridgeOptions<TOutput>,
30
+ ) => GenerationDevtoolsBridge<TOutput>
31
+
32
+ export type VideoDevtoolsBridgeFactory = <TOutput>(
33
+ options: VideoDevtoolsBridgeOptions<TOutput>,
34
+ ) => VideoDevtoolsBridge<TOutput>
35
+
36
+ // ===========================================================================
37
+ // No-op event emitter — extends the abstract base so it satisfies the type
38
+ // without dragging in any of the event-bus runtime cost.
39
+ // ===========================================================================
40
+
41
+ class NoOpChatClientEventEmitter extends ChatClientEventEmitter {
42
+ protected emitEvent(): void {
43
+ // intentionally empty
44
+ }
45
+ }
46
+
47
+ // ===========================================================================
48
+ // No-op bridges. Methods exist to satisfy the structural shape of the real
49
+ // classes; every emit/record call short-circuits.
50
+ // ===========================================================================
51
+
52
+ export class NoOpChatDevtoolsBridge {
53
+ readonly events: ChatClientEventEmitter
54
+
55
+ constructor(options: ChatDevtoolsBridgeOptions) {
56
+ this.events = new NoOpChatClientEventEmitter(options.clientId)
57
+ }
58
+
59
+ // base bridge surface
60
+ emitRegistered(): void {}
61
+ emitUpdated(): void {}
62
+ emitSnapshot(): void {}
63
+ emitToolsRegistered(): void {}
64
+ emitRunLifecycle(
65
+ _eventType: unknown,
66
+ _runId: string,
67
+ _status: unknown,
68
+ _options?: { error?: string },
69
+ ): void {}
70
+ deactivate(): void {}
71
+ supersede(): void {}
72
+ dispose(): void {}
73
+
74
+ // chat-specific surface
75
+ setCurrentStreamId(_streamId: string | null): void {}
76
+ getCurrentStreamId(): string | null {
77
+ return null
78
+ }
79
+ getLastStreamId(): string | null {
80
+ return null
81
+ }
82
+ resolveStreamId(): string {
83
+ return ''
84
+ }
85
+ observeChunk(_chunk: StreamChunk): void {}
86
+ beginRun(_runId: string, _threadId: string): void {}
87
+ getCurrentRunEventContext(): ChatClientRunEventContext | undefined {
88
+ return undefined
89
+ }
90
+ getCurrentOrLastRunEventContext(): ChatClientRunEventContext | undefined {
91
+ return undefined
92
+ }
93
+ findToolCallContext(toolCallId: string): ChatClientEventContext {
94
+ return { toolCallId }
95
+ }
96
+ async applyFixture(_fixture: AIDevtoolsToolFixture): Promise<void> {
97
+ // intentionally empty
98
+ }
99
+ }
100
+
101
+ export class NoOpGenerationDevtoolsBridge<TOutput> {
102
+ constructor(_options: GenerationDevtoolsBridgeOptions<TOutput>) {}
103
+
104
+ // base bridge surface
105
+ emitRegistered(): void {}
106
+ emitUpdated(): void {}
107
+ emitSnapshot(): void {}
108
+ emitToolsRegistered(): void {}
109
+ emitRunLifecycle(): void {}
110
+ deactivate(): void {}
111
+ supersede(): void {}
112
+ dispose(): void {}
113
+
114
+ // generation-specific surface
115
+ beginRun(_input: unknown): string {
116
+ // Real factories supply a stable id; the no-op still returns a
117
+ // unique value because the generation client passes this run id to
118
+ // the adapter's RunAgentInputContext.
119
+ return `noop-run-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`
120
+ }
121
+ ensureRunStarted(_runId: string): void {}
122
+ finishRun(
123
+ _runId: string,
124
+ _eventType: 'run:completed' | 'run:errored' | 'run:cancelled',
125
+ _status: 'completed' | 'errored' | 'cancelled',
126
+ _error?: string,
127
+ ): void {}
128
+ getActiveRunId(): string | null {
129
+ return null
130
+ }
131
+ resetRuns(): void {}
132
+ recordResultChange(): void {}
133
+ recordLoadingChange(): void {}
134
+ recordErrorChange(_error: Error | undefined): void {}
135
+ recordStatusChange(): void {}
136
+ recordProgressChange(): void {}
137
+ emitState(): void {}
138
+ }
139
+
140
+ export class NoOpVideoDevtoolsBridge<
141
+ TOutput,
142
+ > extends NoOpGenerationDevtoolsBridge<TOutput> {
143
+ constructor(options: VideoDevtoolsBridgeOptions<TOutput>) {
144
+ super(options)
145
+ }
146
+
147
+ recordJobIdChange(): void {}
148
+ recordVideoStatusChange(): void {}
149
+ }
150
+
151
+ // Compile-time parity checks. If a public method is added to the real
152
+ // bridge class without a matching stub on the no-op, the corresponding
153
+ // `Exclude<...>` will resolve to a non-`never` union and the `as never`
154
+ // assignment below will fail to typecheck — surfacing the drift at build
155
+ // time instead of as a runtime TypeError later.
156
+ type _ChatBridgeMissing = Exclude<
157
+ keyof ChatDevtoolsBridge,
158
+ keyof NoOpChatDevtoolsBridge
159
+ >
160
+ type _GenerationBridgeMissing = Exclude<
161
+ keyof GenerationDevtoolsBridge<unknown>,
162
+ keyof NoOpGenerationDevtoolsBridge<unknown>
163
+ >
164
+ type _VideoBridgeMissing = Exclude<
165
+ keyof VideoDevtoolsBridge<unknown>,
166
+ keyof NoOpVideoDevtoolsBridge<unknown>
167
+ >
168
+ const _chatBridgeParity: _ChatBridgeMissing = undefined as never
169
+ const _generationBridgeParity: _GenerationBridgeMissing = undefined as never
170
+ const _videoBridgeParity: _VideoBridgeMissing = undefined as never
171
+ void _chatBridgeParity
172
+ void _generationBridgeParity
173
+ void _videoBridgeParity
174
+
175
+ // ===========================================================================
176
+ // Factories — these are what the clients call when no real factory was
177
+ // supplied in options.
178
+ // ===========================================================================
179
+
180
+ // Casts use `unknown` because the no-op classes don't `extend` the real bridge
181
+ // (that would pull the real implementation into the main-entry import graph).
182
+ // Structural parity is enforced by the `_*BridgeMissing` checks above.
183
+
184
+ export const createNoOpChatDevtoolsBridge: ChatDevtoolsBridgeFactory = (
185
+ options,
186
+ ) =>
187
+ // eslint-disable-next-line no-restricted-syntax -- see comment above
188
+ new NoOpChatDevtoolsBridge(options) as unknown as ChatDevtoolsBridge
189
+
190
+ export const createNoOpGenerationDevtoolsBridge: GenerationDevtoolsBridgeFactory =
191
+ <TOutput>(options: GenerationDevtoolsBridgeOptions<TOutput>) =>
192
+ // eslint-disable-next-line no-restricted-syntax -- see comment above
193
+ new NoOpGenerationDevtoolsBridge<TOutput>(
194
+ options,
195
+ ) as unknown as GenerationDevtoolsBridge<TOutput>
196
+
197
+ export const createNoOpVideoDevtoolsBridge: VideoDevtoolsBridgeFactory = <
198
+ TOutput,
199
+ >(
200
+ options: VideoDevtoolsBridgeOptions<TOutput>,
201
+ ) =>
202
+ // eslint-disable-next-line no-restricted-syntax -- see comment above
203
+ new NoOpVideoDevtoolsBridge<TOutput>(
204
+ options,
205
+ ) as unknown as VideoDevtoolsBridge<TOutput>