@tanstack/ai-client 0.21.0 → 0.22.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/src/types.ts CHANGED
@@ -144,6 +144,88 @@ export interface MultimodalContent {
144
144
  id?: string
145
145
  }
146
146
 
147
+ /**
148
+ * Action taken when `sendMessage` is called while the client is busy
149
+ * (streaming, claiming a send, or draining the queue).
150
+ * - `queue`: hold the message; it auto-sends when the current run settles
151
+ * **successfully**.
152
+ * - `drop`: ignore the send (promise still resolves; does not throw).
153
+ * - `interrupt`: abort the current stream and send immediately. Unlike
154
+ * `stop()`, does **not** flush already-queued messages — they still drain
155
+ * after the interrupting send settles successfully.
156
+ */
157
+ export type WhenBusy = 'queue' | 'drop' | 'interrupt'
158
+
159
+ /**
160
+ * Why the client is busy when a {@link QueueStrategy} runs.
161
+ * - `streaming` — an LLM stream is active (`isLoading`).
162
+ * - `sendInFlight` — a send has claimed the client but is not yet loading.
163
+ * - `draining` — the queue drain loop is delivering pending messages.
164
+ */
165
+ export type QueueBusyReason = 'streaming' | 'sendInFlight' | 'draining'
166
+
167
+ /**
168
+ * A user message held in the send queue while a stream is active.
169
+ * Rendered separately from `messages`; cancellable via `cancelQueued(id)`
170
+ * until it drains.
171
+ */
172
+ export interface QueuedMessage {
173
+ id: string
174
+ content: string | MultimodalContent
175
+ createdAt: number
176
+ }
177
+
178
+ /**
179
+ * Declarative queue policy.
180
+ */
181
+ export interface QueueConfig {
182
+ /**
183
+ * Action when the client is busy (streaming, claiming a send, or draining).
184
+ * Default `'queue'`.
185
+ */
186
+ whenBusy?: WhenBusy
187
+ /**
188
+ * How queued items leave the queue.
189
+ * - `'fifo'`: one at a time, in order (default).
190
+ * - `'batch'`: merge all queued items into one send when the run settles
191
+ * successfully.
192
+ */
193
+ drain?: 'fifo' | 'batch'
194
+ /** Max queued items. Unlimited when omitted. `0` means never queue. */
195
+ maxSize?: number
196
+ /**
197
+ * Behavior when `maxSize` is reached. Default `'reject'`.
198
+ * `'reject'` silently discards the new send (does not throw);
199
+ * `'drop-oldest'` evicts the oldest queued item to make room.
200
+ * Only meaningful when `maxSize` is set.
201
+ */
202
+ onOverflow?: 'reject' | 'drop-oldest'
203
+ }
204
+
205
+ /**
206
+ * Escape hatch: decide the action for a single send. Drain stays FIFO for the
207
+ * function form (no `batch` via function). Per-call `sendOptions.whenBusy`
208
+ * overrides the strategy for that send.
209
+ *
210
+ * Actions match {@link WhenBusy}: `'queue' | 'drop' | 'interrupt'`. Concurrent
211
+ * streams are not supported. `pending.id` is the id that will be stored if the
212
+ * action is `'queue'` (safe to pass to `cancelQueued`).
213
+ */
214
+ export type QueueStrategy = (ctx: {
215
+ pending: QueuedMessage
216
+ busyReason: QueueBusyReason
217
+ queued: ReadonlyArray<QueuedMessage>
218
+ }) => { action: WhenBusy }
219
+
220
+ /** A `WhenBusy` shorthand, a full config, or a strategy function. */
221
+ export type QueueOption = WhenBusy | QueueConfig | QueueStrategy
222
+
223
+ /** Per-call overrides for `sendMessage`. */
224
+ export interface SendMessageOptions {
225
+ /** Overrides the configured `whenBusy` for this one send. */
226
+ whenBusy?: WhenBusy
227
+ }
228
+
147
229
  /**
148
230
  * Message parts - building blocks of UIMessage
149
231
  */
@@ -492,6 +574,23 @@ export interface ChatClientBaseOptions<
492
574
  */
493
575
  onSessionGeneratingChange?: (isGenerating: boolean) => void
494
576
 
577
+ /**
578
+ * Policy for messages sent while the client is busy (streaming, claiming
579
+ * a send, or draining the queue). Accepts a `WhenBusy` string, a
580
+ * `QueueConfig`, or a `QueueStrategy` function.
581
+ * Default: `{ whenBusy: 'queue', drain: 'fifo' }`.
582
+ * Queued items auto-send only after a **successful** settle; they are
583
+ * discarded on error/abort, `stop()`, `clear()`, `unsubscribe()`, and
584
+ * `reload()`.
585
+ */
586
+ queue?: QueueOption
587
+
588
+ /**
589
+ * Callback when the pending send queue changes (enqueue, cancel, drain,
590
+ * or flush).
591
+ */
592
+ onQueueChange?: (queue: Array<QueuedMessage>) => void
593
+
495
594
  /**
496
595
  * Callback when a custom event is received from a server-side tool.
497
596
  * Custom events are emitted by tools using `context.emitCustomEvent()` during execution.