@tanstack/ai 0.45.1 → 0.47.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 (82) hide show
  1. package/dist/esm/activities/chat/index.d.ts +36 -11
  2. package/dist/esm/activities/chat/index.js +462 -66
  3. package/dist/esm/activities/chat/index.js.map +1 -1
  4. package/dist/esm/activities/chat/messages.d.ts +1 -0
  5. package/dist/esm/activities/chat/messages.js +12 -7
  6. package/dist/esm/activities/chat/messages.js.map +1 -1
  7. package/dist/esm/activities/chat/middleware/builder.d.ts +7 -2
  8. package/dist/esm/activities/chat/middleware/builder.js.map +1 -1
  9. package/dist/esm/activities/chat/middleware/compose.d.ts +10 -3
  10. package/dist/esm/activities/chat/middleware/compose.js +55 -0
  11. package/dist/esm/activities/chat/middleware/compose.js.map +1 -1
  12. package/dist/esm/activities/chat/middleware/define.d.ts +6 -3
  13. package/dist/esm/activities/chat/middleware/define.js.map +1 -1
  14. package/dist/esm/activities/chat/middleware/generic-interrupts.d.ts +13 -0
  15. package/dist/esm/activities/chat/middleware/generic-interrupts.js +8 -0
  16. package/dist/esm/activities/chat/middleware/generic-interrupts.js.map +1 -0
  17. package/dist/esm/activities/chat/middleware/index.d.ts +4 -1
  18. package/dist/esm/activities/chat/middleware/types.d.ts +54 -3
  19. package/dist/esm/activities/chat/middleware/types.js +16 -0
  20. package/dist/esm/activities/chat/middleware/types.js.map +1 -0
  21. package/dist/esm/activities/chat/stream/processor.js +18 -5
  22. package/dist/esm/activities/chat/stream/processor.js.map +1 -1
  23. package/dist/esm/activities/chat/tools/unique-tool-names.d.ts +20 -0
  24. package/dist/esm/activities/chat/tools/unique-tool-names.js +57 -0
  25. package/dist/esm/activities/chat/tools/unique-tool-names.js.map +1 -0
  26. package/dist/esm/adapter-internals.d.ts +7 -0
  27. package/dist/esm/adapter-internals.js +5 -1
  28. package/dist/esm/client.d.ts +4 -0
  29. package/dist/esm/client.js +3 -1
  30. package/dist/esm/client.js.map +1 -1
  31. package/dist/esm/generic-interrupt-continuation.d.ts +45 -0
  32. package/dist/esm/generic-interrupt-continuation.js +80 -0
  33. package/dist/esm/generic-interrupt-continuation.js.map +1 -0
  34. package/dist/esm/index.d.ts +9 -1
  35. package/dist/esm/index.js +7 -2
  36. package/dist/esm/interrupt-definition.d.ts +113 -0
  37. package/dist/esm/interrupt-definition.js +169 -0
  38. package/dist/esm/interrupt-definition.js.map +1 -0
  39. package/dist/esm/interrupt-resume.d.ts +3 -0
  40. package/dist/esm/interrupt-resume.js +77 -16
  41. package/dist/esm/interrupt-resume.js.map +1 -1
  42. package/dist/esm/interrupts.d.ts +12 -3
  43. package/dist/esm/interrupts.js.map +1 -1
  44. package/dist/esm/middlewares/usage-attributes.d.ts +2 -2
  45. package/dist/esm/middlewares/usage-attributes.js +9 -2
  46. package/dist/esm/middlewares/usage-attributes.js.map +1 -1
  47. package/dist/esm/stream-to-response.d.ts +26 -0
  48. package/dist/esm/stream-to-response.js +1 -1
  49. package/dist/esm/stream-to-response.js.map +1 -1
  50. package/dist/esm/stream-to-websocket.d.ts +123 -0
  51. package/dist/esm/stream-to-websocket.js +249 -0
  52. package/dist/esm/stream-to-websocket.js.map +1 -0
  53. package/dist/esm/types.d.ts +19 -10
  54. package/dist/esm/utilities/chat-params.js +10 -1
  55. package/dist/esm/utilities/chat-params.js.map +1 -1
  56. package/package.json +2 -2
  57. package/skills/ai-core/media-generation/SKILL.md +13 -9
  58. package/skills/ai-core/middleware/SKILL.md +53 -44
  59. package/skills/ai-core/structured-outputs/SKILL.md +59 -55
  60. package/skills/ai-core/tool-calling/SKILL.md +54 -1
  61. package/src/activities/chat/index.ts +1076 -194
  62. package/src/activities/chat/messages.ts +11 -3
  63. package/src/activities/chat/middleware/builder.ts +29 -4
  64. package/src/activities/chat/middleware/compose.ts +95 -5
  65. package/src/activities/chat/middleware/define.ts +13 -3
  66. package/src/activities/chat/middleware/generic-interrupts.ts +26 -0
  67. package/src/activities/chat/middleware/index.ts +15 -0
  68. package/src/activities/chat/middleware/types.ts +127 -2
  69. package/src/activities/chat/stream/processor.ts +21 -0
  70. package/src/activities/chat/tools/unique-tool-names.ts +73 -0
  71. package/src/adapter-internals.ts +24 -0
  72. package/src/client.ts +20 -0
  73. package/src/generic-interrupt-continuation.ts +162 -0
  74. package/src/index.ts +51 -0
  75. package/src/interrupt-definition.ts +581 -0
  76. package/src/interrupt-resume.ts +156 -25
  77. package/src/interrupts.ts +13 -3
  78. package/src/middlewares/usage-attributes.ts +12 -2
  79. package/src/stream-to-response.ts +2 -2
  80. package/src/stream-to-websocket.ts +418 -0
  81. package/src/types.ts +21 -8
  82. package/src/utilities/chat-params.ts +16 -3
package/src/client.ts CHANGED
@@ -343,6 +343,26 @@ export {
343
343
  INTERRUPT_BINDING_VERSION,
344
344
  canonicalizeInterruptResolutions,
345
345
  } from './interrupts'
346
+ export {
347
+ defineInterrupt,
348
+ hashInterruptDefinitionSchema,
349
+ INTERRUPT_PAYLOAD_METADATA_KEY,
350
+ } from './interrupt-definition'
351
+ export {
352
+ INTERRUPT_CONTINUATION_METADATA_KEY,
353
+ INTERRUPT_CONTINUATION_VERSION,
354
+ genericInterruptContinuationFromDescriptor,
355
+ readGenericInterruptContinuation,
356
+ wrapGenericInterruptContinuation,
357
+ } from './generic-interrupt-continuation'
358
+ export type {
359
+ GenericInterruptContinuation,
360
+ GenericInterruptContinuationReadResult,
361
+ } from './generic-interrupt-continuation'
362
+ export type {
363
+ GenericInterruptRequest,
364
+ InterruptDefinition,
365
+ } from './interrupt-definition'
346
366
  export type {
347
367
  BatchInterruptError,
348
368
  BatchInterruptErrorCode,
@@ -0,0 +1,162 @@
1
+ import { INTERRUPT_PAYLOAD_METADATA_KEY } from './interrupt-definition'
2
+ import { readUnopenedInterruptBinding } from './interrupt-resume'
3
+ import type { Interrupt } from './types'
4
+
5
+ /**
6
+ * `ResumeEntry.metadata` key for a first-party generic request.
7
+ *
8
+ * AG-UI `resume` only carries the answer (`interruptId`, `status`, `payload`).
9
+ * The original request rides here so an ephemeral server can rebuild it.
10
+ */
11
+ export const INTERRUPT_CONTINUATION_METADATA_KEY =
12
+ 'tanstack:interruptContinuation' as const
13
+
14
+ export const INTERRUPT_CONTINUATION_VERSION = 1 as const
15
+
16
+ export interface GenericInterruptContinuation {
17
+ v: typeof INTERRUPT_CONTINUATION_VERSION
18
+ definitionId: string
19
+ key: string
20
+ batchIndex: number
21
+ reason: string
22
+ message: string
23
+ expiresAt?: string
24
+ responseSchemaHash?: string
25
+ payloadSchemaHash?: string
26
+ payload?: unknown
27
+ }
28
+
29
+ export type GenericInterruptContinuationReadResult =
30
+ | { status: 'absent' }
31
+ | { status: 'invalid'; message: string }
32
+ | { status: 'ok'; value: GenericInterruptContinuation }
33
+
34
+ function isRecord(value: unknown): value is Record<string, unknown> {
35
+ return value !== null && typeof value === 'object' && !Array.isArray(value)
36
+ }
37
+
38
+ function invalid(
39
+ message: string,
40
+ ): Extract<GenericInterruptContinuationReadResult, { status: 'invalid' }> {
41
+ return { status: 'invalid', message }
42
+ }
43
+
44
+ /**
45
+ * Read one generic request from `resume[].metadata`.
46
+ *
47
+ * Missing key means this resume item is not a first-party generic continuation.
48
+ * A present key that fails the shape is a protocol error.
49
+ */
50
+ export function readGenericInterruptContinuation(
51
+ metadata: unknown,
52
+ ): GenericInterruptContinuationReadResult {
53
+ if (metadata === undefined) return { status: 'absent' }
54
+ if (!isRecord(metadata)) {
55
+ return invalid('Generic interrupt resume metadata must be an object.')
56
+ }
57
+ if (
58
+ !Object.prototype.hasOwnProperty.call(
59
+ metadata,
60
+ INTERRUPT_CONTINUATION_METADATA_KEY,
61
+ )
62
+ ) {
63
+ return { status: 'absent' }
64
+ }
65
+ const raw = metadata[INTERRUPT_CONTINUATION_METADATA_KEY]
66
+ if (!isRecord(raw)) {
67
+ return invalid('Generic interrupt continuation is invalid.')
68
+ }
69
+ if (
70
+ raw.v !== INTERRUPT_CONTINUATION_VERSION ||
71
+ typeof raw.definitionId !== 'string' ||
72
+ typeof raw.key !== 'string' ||
73
+ typeof raw.reason !== 'string' ||
74
+ typeof raw.message !== 'string' ||
75
+ typeof raw.batchIndex !== 'number' ||
76
+ !Number.isInteger(raw.batchIndex) ||
77
+ raw.batchIndex < 0 ||
78
+ (raw.responseSchemaHash !== undefined &&
79
+ typeof raw.responseSchemaHash !== 'string') ||
80
+ (raw.expiresAt !== undefined && typeof raw.expiresAt !== 'string') ||
81
+ (raw.payloadSchemaHash !== undefined &&
82
+ typeof raw.payloadSchemaHash !== 'string')
83
+ ) {
84
+ return invalid('Generic interrupt continuation contains invalid fields.')
85
+ }
86
+ return {
87
+ status: 'ok',
88
+ value: {
89
+ v: INTERRUPT_CONTINUATION_VERSION,
90
+ definitionId: raw.definitionId,
91
+ key: raw.key,
92
+ batchIndex: raw.batchIndex,
93
+ reason: raw.reason,
94
+ message: raw.message,
95
+ ...(typeof raw.expiresAt === 'string'
96
+ ? { expiresAt: raw.expiresAt }
97
+ : {}),
98
+ ...(typeof raw.responseSchemaHash === 'string'
99
+ ? { responseSchemaHash: raw.responseSchemaHash }
100
+ : {}),
101
+ ...(typeof raw.payloadSchemaHash === 'string'
102
+ ? { payloadSchemaHash: raw.payloadSchemaHash }
103
+ : {}),
104
+ ...(Object.prototype.hasOwnProperty.call(raw, 'payload')
105
+ ? { payload: raw.payload }
106
+ : {}),
107
+ },
108
+ }
109
+ }
110
+
111
+ /** Put a parsed continuation on `ResumeEntry.metadata`. */
112
+ export function wrapGenericInterruptContinuation(
113
+ continuation: GenericInterruptContinuation,
114
+ ): Record<string, unknown> {
115
+ return { [INTERRUPT_CONTINUATION_METADATA_KEY]: continuation }
116
+ }
117
+
118
+ /**
119
+ * Build the resume-metadata continuation from an outbound AG-UI interrupt.
120
+ *
121
+ * Returns `undefined` when the descriptor is not a first-party generic item.
122
+ */
123
+ export function genericInterruptContinuationFromDescriptor(
124
+ interrupt: Interrupt,
125
+ ): GenericInterruptContinuation | undefined {
126
+ const binding = readUnopenedInterruptBinding(interrupt)
127
+ if (
128
+ binding?.kind !== 'generic' ||
129
+ binding.definitionId === undefined ||
130
+ binding.key === undefined ||
131
+ binding.batchIndex === undefined
132
+ ) {
133
+ return undefined
134
+ }
135
+ const metadata = isRecord(interrupt.metadata) ? interrupt.metadata : undefined
136
+ const hasPayload =
137
+ metadata !== undefined &&
138
+ Object.prototype.hasOwnProperty.call(
139
+ metadata,
140
+ INTERRUPT_PAYLOAD_METADATA_KEY,
141
+ )
142
+ return {
143
+ v: INTERRUPT_CONTINUATION_VERSION,
144
+ definitionId: binding.definitionId,
145
+ key: binding.key,
146
+ batchIndex: binding.batchIndex,
147
+ reason: interrupt.reason,
148
+ message: interrupt.message ?? '',
149
+ ...(interrupt.expiresAt !== undefined
150
+ ? { expiresAt: interrupt.expiresAt }
151
+ : {}),
152
+ ...(binding.responseSchemaHash !== undefined
153
+ ? { responseSchemaHash: binding.responseSchemaHash }
154
+ : {}),
155
+ ...(binding.payloadSchemaHash !== undefined
156
+ ? { payloadSchemaHash: binding.payloadSchemaHash }
157
+ : {}),
158
+ ...(hasPayload
159
+ ? { payload: metadata[INTERRUPT_PAYLOAD_METADATA_KEY] }
160
+ : {}),
161
+ }
162
+ }
package/src/index.ts CHANGED
@@ -103,6 +103,7 @@ export type {
103
103
 
104
104
  // MCP error classes (value exports — usable with instanceof)
105
105
  export { MCPDuplicateToolNameError } from './activities/chat/mcp/manager'
106
+ export { DuplicateToolNameError } from './activities/chat/tools/unique-tool-names'
106
107
 
107
108
  // Schema conversion (Standard JSON Schema compliant)
108
109
  export {
@@ -138,6 +139,22 @@ export type {
138
139
  UpsertableStreamDurability,
139
140
  } from './stream-durability'
140
141
 
142
+ // WebSocket transport utilities
143
+ export {
144
+ toWebSocketStream,
145
+ toWebSocketResponse,
146
+ resumeWebSocketStream,
147
+ resumeWebSocketResponse,
148
+ encodeWsFrame,
149
+ decodeWsFrame,
150
+ } from './stream-to-websocket'
151
+ export type {
152
+ WebSocketLike,
153
+ WsRunContext,
154
+ WebSocketStreamInit,
155
+ InboundFrame,
156
+ } from './stream-to-websocket'
157
+
141
158
  // Tool call management
142
159
  export { ToolCallManager } from './activities/chat/tools/tool-calls'
143
160
 
@@ -184,6 +201,17 @@ export type {
184
201
  SandboxFileEvent,
185
202
  SandboxFileHookEvent,
186
203
  ChatSandboxHooks,
204
+ InterruptBoundaryPhase,
205
+ InterruptToolResume,
206
+ InterruptResolutionCollection,
207
+ GenericInterruptResolution,
208
+ InterruptBoundaryResult,
209
+ InterruptResolutionResult,
210
+ } from './activities/chat/middleware/index'
211
+
212
+ export {
213
+ INTERRUPT_BOUNDARY_PHASES,
214
+ INTERRUPT_TOOL_RESUMES,
187
215
  } from './activities/chat/middleware/index'
188
216
 
189
217
  // Interrupt protocol surface. Deliberately enumerated rather than
@@ -192,6 +220,29 @@ export type {
192
220
  // a commitment. Only the ephemeral contract this release actually implements
193
221
  // is exported — no durable-recovery or persisted-state types, which would
194
222
  // pre-decide a question the orchestration RFC still owns.
223
+ export {
224
+ defineInterrupt,
225
+ createInterruptBinding,
226
+ INTERRUPT_PAYLOAD_METADATA_KEY,
227
+ } from './interrupt-definition'
228
+ export {
229
+ INTERRUPT_CONTINUATION_METADATA_KEY,
230
+ INTERRUPT_CONTINUATION_VERSION,
231
+ genericInterruptContinuationFromDescriptor,
232
+ readGenericInterruptContinuation,
233
+ wrapGenericInterruptContinuation,
234
+ } from './generic-interrupt-continuation'
235
+ export type {
236
+ GenericInterruptContinuation,
237
+ GenericInterruptContinuationReadResult,
238
+ } from './generic-interrupt-continuation'
239
+ export type {
240
+ InterruptDefinition,
241
+ GenericInterruptRequest,
242
+ InterruptDefinitionOptions,
243
+ InterruptBindingDescriptor,
244
+ } from './interrupt-definition'
245
+
195
246
  export {
196
247
  INTERRUPT_BINDING_VERSION,
197
248
  canonicalizeInterruptResolutions,