@tanstack/ai 0.43.1 → 0.44.1

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.
Files changed (73) hide show
  1. package/dist/esm/activities/chat/index.js +18 -0
  2. package/dist/esm/activities/chat/index.js.map +1 -1
  3. package/dist/esm/activities/chat/messages.js +21 -8
  4. package/dist/esm/activities/chat/messages.js.map +1 -1
  5. package/dist/esm/activities/embed/adapter.d.ts +69 -0
  6. package/dist/esm/activities/embed/adapter.js +23 -0
  7. package/dist/esm/activities/embed/adapter.js.map +1 -0
  8. package/dist/esm/activities/embed/index.d.ts +117 -0
  9. package/dist/esm/activities/embed/index.js +166 -0
  10. package/dist/esm/activities/embed/index.js.map +1 -0
  11. package/dist/esm/activities/error-payload.d.ts +8 -0
  12. package/dist/esm/activities/error-payload.js +29 -17
  13. package/dist/esm/activities/error-payload.js.map +1 -1
  14. package/dist/esm/activities/generateAudio/index.d.ts +12 -0
  15. package/dist/esm/activities/generateAudio/index.js +19 -6
  16. package/dist/esm/activities/generateAudio/index.js.map +1 -1
  17. package/dist/esm/activities/generateImage/index.d.ts +12 -0
  18. package/dist/esm/activities/generateImage/index.js +21 -7
  19. package/dist/esm/activities/generateImage/index.js.map +1 -1
  20. package/dist/esm/activities/generateSpeech/index.d.ts +17 -1
  21. package/dist/esm/activities/generateSpeech/index.js +19 -6
  22. package/dist/esm/activities/generateSpeech/index.js.map +1 -1
  23. package/dist/esm/activities/generateTranscription/index.d.ts +17 -1
  24. package/dist/esm/activities/generateTranscription/index.js +19 -6
  25. package/dist/esm/activities/generateTranscription/index.js.map +1 -1
  26. package/dist/esm/activities/generateVideo/index.d.ts +18 -0
  27. package/dist/esm/activities/generateVideo/index.js +54 -15
  28. package/dist/esm/activities/generateVideo/index.js.map +1 -1
  29. package/dist/esm/activities/index.d.ts +8 -2
  30. package/dist/esm/activities/index.js +11 -7
  31. package/dist/esm/activities/middleware/types.d.ts +1 -1
  32. package/dist/esm/activities/rerank/adapter.d.ts +63 -0
  33. package/dist/esm/activities/rerank/adapter.js +23 -0
  34. package/dist/esm/activities/rerank/adapter.js.map +1 -0
  35. package/dist/esm/activities/rerank/index.d.ts +92 -0
  36. package/dist/esm/activities/rerank/index.js +163 -0
  37. package/dist/esm/activities/rerank/index.js.map +1 -0
  38. package/dist/esm/activities/summarize/index.d.ts +17 -1
  39. package/dist/esm/activities/summarize/index.js +19 -5
  40. package/dist/esm/activities/summarize/index.js.map +1 -1
  41. package/dist/esm/index.d.ts +7 -2
  42. package/dist/esm/index.js +5 -1
  43. package/dist/esm/middlewares/otel.js +20 -2
  44. package/dist/esm/middlewares/otel.js.map +1 -1
  45. package/dist/esm/types.d.ts +195 -0
  46. package/dist/esm/utilities/activity-abort.d.ts +53 -0
  47. package/dist/esm/utilities/activity-abort.js +150 -0
  48. package/dist/esm/utilities/activity-abort.js.map +1 -0
  49. package/dist/esm/utilities/embedding-input.d.ts +32 -0
  50. package/dist/esm/utilities/embedding-input.js +61 -0
  51. package/dist/esm/utilities/embedding-input.js.map +1 -0
  52. package/package.json +3 -3
  53. package/skills/ai-core/media-generation/SKILL.md +55 -23
  54. package/src/activities/chat/index.ts +27 -2
  55. package/src/activities/chat/messages.ts +30 -1
  56. package/src/activities/embed/adapter.ts +112 -0
  57. package/src/activities/embed/index.ts +318 -0
  58. package/src/activities/error-payload.ts +41 -9
  59. package/src/activities/generateAudio/index.ts +47 -5
  60. package/src/activities/generateImage/index.ts +48 -5
  61. package/src/activities/generateSpeech/index.ts +52 -9
  62. package/src/activities/generateTranscription/index.ts +52 -9
  63. package/src/activities/generateVideo/index.ts +131 -33
  64. package/src/activities/index.ts +44 -0
  65. package/src/activities/middleware/types.ts +2 -0
  66. package/src/activities/rerank/adapter.ts +90 -0
  67. package/src/activities/rerank/index.ts +302 -0
  68. package/src/activities/summarize/index.ts +59 -19
  69. package/src/index.ts +19 -0
  70. package/src/middlewares/otel.ts +38 -3
  71. package/src/types.ts +219 -0
  72. package/src/utilities/activity-abort.ts +197 -0
  73. package/src/utilities/embedding-input.ts +83 -0
@@ -11,11 +11,18 @@ import { resolveDebugOption } from '../../logger/resolve'
11
11
  import {
12
12
  applyGenerationResultTransforms,
13
13
  createGenerationContext,
14
+ runGenerationAbort,
14
15
  runGenerationError,
15
16
  runGenerationFinish,
16
17
  runGenerationStart,
17
18
  runGenerationUsage,
18
19
  } from '../middleware/run'
20
+ import {
21
+ abortReasonMessage,
22
+ createActivityAbortControls,
23
+ isActivityAbortError,
24
+ raceWithAbort,
25
+ } from '../../utilities/activity-abort'
19
26
  import type { InternalLogger } from '../../logger/internal-logger'
20
27
  import type { DebugOption } from '../../logger/types'
21
28
  import type { GenerationMiddleware } from '../middleware/types'
@@ -89,6 +96,18 @@ export interface AudioActivityOptions<
89
96
  threadId?: string
90
97
  /** Stable run id for correlating this run when persisted. */
91
98
  runId?: string
99
+ /**
100
+ * Maximum duration of this activity invocation in milliseconds.
101
+ * No SDK-wide default — choose a value suitable for the provider and job.
102
+ * Composed with {@link abortSignal}; the first abort wins.
103
+ */
104
+ timeout?: number
105
+ /**
106
+ * Caller cancellation signal (request disconnects, job/runtime cancellation).
107
+ * Composed with {@link timeout} into an effective signal forwarded to the
108
+ * adapter. Request-specific — not stored on global provider client config.
109
+ */
110
+ abortSignal?: AbortSignal
92
111
  }
93
112
 
94
113
  // ===========================
@@ -167,12 +186,18 @@ async function runGenerateAudio<
167
186
  middleware,
168
187
  threadId,
169
188
  runId,
189
+ timeout,
190
+ abortSignal: callerAbortSignal,
170
191
  ...rest
171
192
  } = options
172
193
  const model = adapter.model
173
194
  const requestId = createId('audio')
174
195
  const startTime = Date.now()
175
196
  const logger: InternalLogger = resolveDebugOption(options.debug)
197
+ const abortControls = createActivityAbortControls({
198
+ timeout,
199
+ abortSignal: callerAbortSignal,
200
+ })
176
201
  const providerName =
177
202
  (adapter as { name?: string; provider?: string }).provider ??
178
203
  (adapter as { name?: string }).name ??
@@ -208,7 +233,16 @@ async function runGenerateAudio<
208
233
  })
209
234
 
210
235
  try {
211
- const rawResult = await adapter.generateAudio({ ...rest, model, logger })
236
+ const rawResult = await raceWithAbort(
237
+ adapter.generateAudio({
238
+ ...rest,
239
+ model,
240
+ logger,
241
+ ...(abortControls.signal ? { abortSignal: abortControls.signal } : {}),
242
+ }),
243
+ abortControls.signal,
244
+ )
245
+ abortControls.clear()
212
246
  const result = await applyGenerationResultTransforms(mwCtx, rawResult)
213
247
  const elapsedMs = Date.now() - startTime
214
248
 
@@ -245,6 +279,7 @@ async function runGenerateAudio<
245
279
 
246
280
  return result
247
281
  } catch (error) {
282
+ abortControls.clear()
248
283
  const elapsedMs = Date.now() - startTime
249
284
  const err = error as Error
250
285
  aiEventClient.emit('audio:request:error', {
@@ -256,10 +291,17 @@ async function runGenerateAudio<
256
291
  modelOptions: rest.modelOptions as Record<string, unknown> | undefined,
257
292
  timestamp: Date.now(),
258
293
  })
259
- await runGenerationError(middleware, mwCtx, {
260
- error,
261
- duration: elapsedMs,
262
- })
294
+ if (isActivityAbortError(error, abortControls.signal)) {
295
+ await runGenerationAbort(middleware, mwCtx, {
296
+ reason: abortReasonMessage(error, abortControls.signal),
297
+ duration: elapsedMs,
298
+ })
299
+ } else {
300
+ await runGenerationError(middleware, mwCtx, {
301
+ error,
302
+ duration: elapsedMs,
303
+ })
304
+ }
263
305
  logger.errors('generateAudio activity failed', {
264
306
  error,
265
307
  source: 'generateAudio',
@@ -11,11 +11,18 @@ import { resolveDebugOption } from '../../logger/resolve'
11
11
  import {
12
12
  applyGenerationResultTransforms,
13
13
  createGenerationContext,
14
+ runGenerationAbort,
14
15
  runGenerationError,
15
16
  runGenerationFinish,
16
17
  runGenerationStart,
17
18
  runGenerationUsage,
18
19
  } from '../middleware/run'
20
+ import {
21
+ abortReasonMessage,
22
+ createActivityAbortControls,
23
+ isActivityAbortError,
24
+ raceWithAbort,
25
+ } from '../../utilities/activity-abort'
19
26
  import { resolveMediaPrompt } from '../../utilities/media-prompt'
20
27
  import type { InternalLogger } from '../../logger/internal-logger'
21
28
  import type { DebugOption } from '../../logger/types'
@@ -142,6 +149,18 @@ export type ImageActivityOptions<
142
149
  threadId?: string
143
150
  /** Stable run id for correlating this run when persisted. */
144
151
  runId?: string
152
+ /**
153
+ * Maximum duration of this activity invocation in milliseconds.
154
+ * No SDK-wide default — choose a value suitable for the provider and job.
155
+ * Composed with {@link abortSignal}; the first abort wins.
156
+ */
157
+ timeout?: number
158
+ /**
159
+ * Caller cancellation signal (request disconnects, job/runtime cancellation).
160
+ * Composed with {@link timeout} into an effective signal forwarded to the
161
+ * adapter. Request-specific — not stored on global provider client config.
162
+ */
163
+ abortSignal?: AbortSignal
145
164
  } & ({} extends ImageProviderOptionsForModel<TAdapter, TAdapter['model']>
146
165
  ? {
147
166
  /** Provider-specific options for image generation */ modelOptions?: ImageProviderOptionsForModel<
@@ -260,12 +279,18 @@ async function runGenerateImage<
260
279
  middleware,
261
280
  threadId,
262
281
  runId,
282
+ timeout,
283
+ abortSignal: callerAbortSignal,
263
284
  ...rest
264
285
  } = options
265
286
  const model = adapter.model
266
287
  const requestId = createId('image')
267
288
  const startTime = Date.now()
268
289
  const logger: InternalLogger = resolveDebugOption(options.debug)
290
+ const abortControls = createActivityAbortControls({
291
+ timeout,
292
+ abortSignal: callerAbortSignal,
293
+ })
269
294
 
270
295
  const mwCtx = createGenerationContext({
271
296
  requestId,
@@ -311,7 +336,16 @@ async function runGenerateImage<
311
336
  })
312
337
 
313
338
  try {
314
- const rawResult = await adapter.generateImages({ ...rest, model, logger })
339
+ const rawResult = await raceWithAbort(
340
+ adapter.generateImages({
341
+ ...rest,
342
+ model,
343
+ logger,
344
+ ...(abortControls.signal ? { abortSignal: abortControls.signal } : {}),
345
+ }),
346
+ abortControls.signal,
347
+ )
348
+ abortControls.clear()
315
349
  const result = await applyGenerationResultTransforms(mwCtx, rawResult)
316
350
  const duration = Date.now() - startTime
317
351
 
@@ -355,10 +389,19 @@ async function runGenerateImage<
355
389
 
356
390
  return result
357
391
  } catch (error) {
358
- await runGenerationError(middleware, mwCtx, {
359
- error,
360
- duration: Date.now() - startTime,
361
- })
392
+ abortControls.clear()
393
+ const duration = Date.now() - startTime
394
+ if (isActivityAbortError(error, abortControls.signal)) {
395
+ await runGenerationAbort(middleware, mwCtx, {
396
+ reason: abortReasonMessage(error, abortControls.signal),
397
+ duration,
398
+ })
399
+ } else {
400
+ await runGenerationError(middleware, mwCtx, {
401
+ error,
402
+ duration,
403
+ })
404
+ }
362
405
  logger.errors('generateImage activity failed', {
363
406
  error,
364
407
  source: 'generateImage',
@@ -11,11 +11,18 @@ import { resolveDebugOption } from '../../logger/resolve'
11
11
  import {
12
12
  applyGenerationResultTransforms,
13
13
  createGenerationContext,
14
+ runGenerationAbort,
14
15
  runGenerationError,
15
16
  runGenerationFinish,
16
17
  runGenerationStart,
17
18
  runGenerationUsage,
18
19
  } from '../middleware/run'
20
+ import {
21
+ abortReasonMessage,
22
+ createActivityAbortControls,
23
+ isActivityAbortError,
24
+ raceWithAbort,
25
+ } from '../../utilities/activity-abort'
19
26
  import type { InternalLogger } from '../../logger/internal-logger'
20
27
  import type { DebugOption } from '../../logger/types'
21
28
  import type { GenerationMiddleware } from '../middleware/types'
@@ -36,10 +43,11 @@ export const kind = 'tts' as const
36
43
  /**
37
44
  * Extract provider options from a TTSAdapter via ~types.
38
45
  */
39
- export type TTSProviderOptions<TAdapter> =
40
- TAdapter extends TTSAdapter<any, any>
41
- ? TAdapter['~types']['providerOptions']
42
- : object
46
+ export type TTSProviderOptions<TAdapter> = TAdapter extends {
47
+ '~types': { providerOptions: infer P extends object }
48
+ }
49
+ ? P
50
+ : object
43
51
 
44
52
  // ===========================
45
53
  // Activity Options Type
@@ -92,6 +100,18 @@ export interface TTSActivityOptions<
92
100
  threadId?: string
93
101
  /** Stable run id for correlating this run when persisted. */
94
102
  runId?: string
103
+ /**
104
+ * Maximum duration of this activity invocation in milliseconds.
105
+ * No SDK-wide default — choose a value suitable for the provider and job.
106
+ * Composed with {@link abortSignal}; the first abort wins.
107
+ */
108
+ timeout?: number
109
+ /**
110
+ * Caller cancellation signal (request disconnects, job/runtime cancellation).
111
+ * Composed with {@link timeout} into an effective signal forwarded to the
112
+ * adapter. Request-specific — not stored on global provider client config.
113
+ */
114
+ abortSignal?: AbortSignal
95
115
  }
96
116
 
97
117
  // ===========================
@@ -175,12 +195,18 @@ async function runGenerateSpeech<
175
195
  middleware,
176
196
  threadId,
177
197
  runId,
198
+ timeout,
199
+ abortSignal: callerAbortSignal,
178
200
  ...rest
179
201
  } = options
180
202
  const model = adapter.model
181
203
  const requestId = createId('speech')
182
204
  const startTime = Date.now()
183
205
  const logger: InternalLogger = resolveDebugOption(options.debug)
206
+ const abortControls = createActivityAbortControls({
207
+ timeout,
208
+ abortSignal: callerAbortSignal,
209
+ })
184
210
  const providerName =
185
211
  (adapter as { name?: string; provider?: string }).provider ??
186
212
  (adapter as { name?: string }).name ??
@@ -223,7 +249,16 @@ async function runGenerateSpeech<
223
249
  })
224
250
 
225
251
  try {
226
- const rawResult = await adapter.generateSpeech({ ...rest, model, logger })
252
+ const rawResult = await raceWithAbort(
253
+ adapter.generateSpeech({
254
+ ...rest,
255
+ model,
256
+ logger,
257
+ ...(abortControls.signal ? { abortSignal: abortControls.signal } : {}),
258
+ }),
259
+ abortControls.signal,
260
+ )
261
+ abortControls.clear()
227
262
  const result = await applyGenerationResultTransforms(mwCtx, rawResult)
228
263
  const duration = Date.now() - startTime
229
264
 
@@ -263,6 +298,7 @@ async function runGenerateSpeech<
263
298
 
264
299
  return result
265
300
  } catch (error) {
301
+ abortControls.clear()
266
302
  const duration = Date.now() - startTime
267
303
  const err = error as Error
268
304
  aiEventClient.emit('speech:request:error', {
@@ -274,10 +310,17 @@ async function runGenerateSpeech<
274
310
  modelOptions: rest.modelOptions as Record<string, unknown> | undefined,
275
311
  timestamp: Date.now(),
276
312
  })
277
- await runGenerationError(middleware, mwCtx, {
278
- error,
279
- duration,
280
- })
313
+ if (isActivityAbortError(error, abortControls.signal)) {
314
+ await runGenerationAbort(middleware, mwCtx, {
315
+ reason: abortReasonMessage(error, abortControls.signal),
316
+ duration,
317
+ })
318
+ } else {
319
+ await runGenerationError(middleware, mwCtx, {
320
+ error,
321
+ duration,
322
+ })
323
+ }
281
324
  logger.errors('generateSpeech activity failed', {
282
325
  error,
283
326
  source: 'generateSpeech',
@@ -11,11 +11,18 @@ import { resolveDebugOption } from '../../logger/resolve'
11
11
  import {
12
12
  applyGenerationResultTransforms,
13
13
  createGenerationContext,
14
+ runGenerationAbort,
14
15
  runGenerationError,
15
16
  runGenerationFinish,
16
17
  runGenerationStart,
17
18
  runGenerationUsage,
18
19
  } from '../middleware/run'
20
+ import {
21
+ abortReasonMessage,
22
+ createActivityAbortControls,
23
+ isActivityAbortError,
24
+ raceWithAbort,
25
+ } from '../../utilities/activity-abort'
19
26
  import type { InternalLogger } from '../../logger/internal-logger'
20
27
  import type { DebugOption } from '../../logger/types'
21
28
  import type { GenerationMiddleware } from '../middleware/types'
@@ -40,10 +47,11 @@ export const kind = 'transcription' as const
40
47
  /**
41
48
  * Extract provider options from a TranscriptionAdapter via ~types.
42
49
  */
43
- export type TranscriptionProviderOptions<TAdapter> =
44
- TAdapter extends TranscriptionAdapter<any, any>
45
- ? TAdapter['~types']['providerOptions']
46
- : object
50
+ export type TranscriptionProviderOptions<TAdapter> = TAdapter extends {
51
+ '~types': { providerOptions: infer P extends object }
52
+ }
53
+ ? P
54
+ : object
47
55
 
48
56
  // ===========================
49
57
  // Activity Options Type
@@ -99,6 +107,18 @@ export interface TranscriptionActivityOptions<
99
107
  threadId?: string
100
108
  /** Stable run id for correlating this run when persisted. */
101
109
  runId?: string
110
+ /**
111
+ * Maximum duration of this activity invocation in milliseconds.
112
+ * No SDK-wide default — choose a value suitable for the provider and job.
113
+ * Composed with {@link abortSignal}; the first abort wins.
114
+ */
115
+ timeout?: number
116
+ /**
117
+ * Caller cancellation signal (request disconnects, job/runtime cancellation).
118
+ * Composed with {@link timeout} into an effective signal forwarded to the
119
+ * adapter. Request-specific — not stored on global provider client config.
120
+ */
121
+ abortSignal?: AbortSignal
102
122
  }
103
123
 
104
124
  // ===========================
@@ -211,12 +231,18 @@ async function runGenerateTranscription<
211
231
  middleware,
212
232
  threadId,
213
233
  runId,
234
+ timeout,
235
+ abortSignal: callerAbortSignal,
214
236
  ...rest
215
237
  } = options
216
238
  const model = adapter.model
217
239
  const requestId = createId('transcription')
218
240
  const startTime = Date.now()
219
241
  const logger: InternalLogger = resolveDebugOption(options.debug)
242
+ const abortControls = createActivityAbortControls({
243
+ timeout,
244
+ abortSignal: callerAbortSignal,
245
+ })
220
246
  const providerName =
221
247
  (adapter as { name?: string; provider?: string }).provider ??
222
248
  (adapter as { name?: string }).name ??
@@ -258,7 +284,16 @@ async function runGenerateTranscription<
258
284
  })
259
285
 
260
286
  try {
261
- const rawResult = await adapter.transcribe({ ...rest, model, logger })
287
+ const rawResult = await raceWithAbort(
288
+ adapter.transcribe({
289
+ ...rest,
290
+ model,
291
+ logger,
292
+ ...(abortControls.signal ? { abortSignal: abortControls.signal } : {}),
293
+ }),
294
+ abortControls.signal,
295
+ )
296
+ abortControls.clear()
262
297
  const result = await applyGenerationResultTransforms(mwCtx, rawResult)
263
298
  const duration = Date.now() - startTime
264
299
 
@@ -286,6 +321,7 @@ async function runGenerateTranscription<
286
321
 
287
322
  return result
288
323
  } catch (error) {
324
+ abortControls.clear()
289
325
  const duration = Date.now() - startTime
290
326
  const err = error as Error
291
327
  aiEventClient.emit('transcription:request:error', {
@@ -297,10 +333,17 @@ async function runGenerateTranscription<
297
333
  modelOptions: rest.modelOptions as Record<string, unknown> | undefined,
298
334
  timestamp: Date.now(),
299
335
  })
300
- await runGenerationError(middleware, mwCtx, {
301
- error,
302
- duration,
303
- })
336
+ if (isActivityAbortError(error, abortControls.signal)) {
337
+ await runGenerationAbort(middleware, mwCtx, {
338
+ reason: abortReasonMessage(error, abortControls.signal),
339
+ duration,
340
+ })
341
+ } else {
342
+ await runGenerationError(middleware, mwCtx, {
343
+ error,
344
+ duration,
345
+ })
346
+ }
304
347
  logger.errors('generateTranscription activity failed', {
305
348
  error,
306
349
  source: 'generateTranscription',