@tanstack/ai 0.34.1 → 0.36.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.
Files changed (210) hide show
  1. package/package.json +2 -2
  2. package/skills/ai-core/tool-calling/SKILL.md +26 -1
  3. package/src/activities/chat/index.ts +8 -0
  4. package/src/activities/chat/tools/lazy-tool-manager.ts +12 -4
  5. package/src/activities/chat/tools/lazy-tools.ts +33 -0
  6. package/src/client.ts +2 -0
  7. package/src/index.ts +7 -0
  8. package/src/realtime/types.ts +77 -0
  9. package/src/types.ts +23 -1
  10. package/dist/esm/activities/chat/adapter.d.ts +0 -163
  11. package/dist/esm/activities/chat/adapter.js +0 -17
  12. package/dist/esm/activities/chat/adapter.js.map +0 -1
  13. package/dist/esm/activities/chat/agent-loop-strategies.d.ts +0 -59
  14. package/dist/esm/activities/chat/agent-loop-strategies.js +0 -23
  15. package/dist/esm/activities/chat/agent-loop-strategies.js.map +0 -1
  16. package/dist/esm/activities/chat/index.d.ts +0 -264
  17. package/dist/esm/activities/chat/index.js +0 -1723
  18. package/dist/esm/activities/chat/index.js.map +0 -1
  19. package/dist/esm/activities/chat/mcp/manager.d.ts +0 -25
  20. package/dist/esm/activities/chat/mcp/manager.js +0 -71
  21. package/dist/esm/activities/chat/mcp/manager.js.map +0 -1
  22. package/dist/esm/activities/chat/mcp/types.d.ts +0 -56
  23. package/dist/esm/activities/chat/messages.d.ts +0 -78
  24. package/dist/esm/activities/chat/messages.js +0 -374
  25. package/dist/esm/activities/chat/messages.js.map +0 -1
  26. package/dist/esm/activities/chat/middleware/builder.d.ts +0 -46
  27. package/dist/esm/activities/chat/middleware/builder.js +0 -17
  28. package/dist/esm/activities/chat/middleware/builder.js.map +0 -1
  29. package/dist/esm/activities/chat/middleware/capabilities.d.ts +0 -93
  30. package/dist/esm/activities/chat/middleware/capabilities.js +0 -45
  31. package/dist/esm/activities/chat/middleware/capabilities.js.map +0 -1
  32. package/dist/esm/activities/chat/middleware/compose.d.ts +0 -87
  33. package/dist/esm/activities/chat/middleware/compose.js +0 -510
  34. package/dist/esm/activities/chat/middleware/compose.js.map +0 -1
  35. package/dist/esm/activities/chat/middleware/define.d.ts +0 -20
  36. package/dist/esm/activities/chat/middleware/define.js +0 -7
  37. package/dist/esm/activities/chat/middleware/define.js.map +0 -1
  38. package/dist/esm/activities/chat/middleware/index.d.ts +0 -10
  39. package/dist/esm/activities/chat/middleware/tool-cache-middleware.d.ts +0 -89
  40. package/dist/esm/activities/chat/middleware/tool-cache-middleware.js +0 -76
  41. package/dist/esm/activities/chat/middleware/tool-cache-middleware.js.map +0 -1
  42. package/dist/esm/activities/chat/middleware/types.d.ts +0 -405
  43. package/dist/esm/activities/chat/middleware/validate.d.ts +0 -19
  44. package/dist/esm/activities/chat/middleware/validate.js +0 -30
  45. package/dist/esm/activities/chat/middleware/validate.js.map +0 -1
  46. package/dist/esm/activities/chat/runtime-context-types.d.ts +0 -43
  47. package/dist/esm/activities/chat/stream/index.d.ts +0 -11
  48. package/dist/esm/activities/chat/stream/json-parser.d.ts +0 -38
  49. package/dist/esm/activities/chat/stream/json-parser.js +0 -28
  50. package/dist/esm/activities/chat/stream/json-parser.js.map +0 -1
  51. package/dist/esm/activities/chat/stream/message-updaters.d.ts +0 -81
  52. package/dist/esm/activities/chat/stream/message-updaters.js +0 -253
  53. package/dist/esm/activities/chat/stream/message-updaters.js.map +0 -1
  54. package/dist/esm/activities/chat/stream/processor.d.ts +0 -439
  55. package/dist/esm/activities/chat/stream/processor.js +0 -1426
  56. package/dist/esm/activities/chat/stream/processor.js.map +0 -1
  57. package/dist/esm/activities/chat/stream/strategies.d.ts +0 -43
  58. package/dist/esm/activities/chat/stream/strategies.js +0 -54
  59. package/dist/esm/activities/chat/stream/strategies.js.map +0 -1
  60. package/dist/esm/activities/chat/stream/types.d.ts +0 -90
  61. package/dist/esm/activities/chat/tools/lazy-tool-manager.d.ts +0 -81
  62. package/dist/esm/activities/chat/tools/lazy-tool-manager.js +0 -188
  63. package/dist/esm/activities/chat/tools/lazy-tool-manager.js.map +0 -1
  64. package/dist/esm/activities/chat/tools/schema-converter.d.ts +0 -140
  65. package/dist/esm/activities/chat/tools/schema-converter.js +0 -167
  66. package/dist/esm/activities/chat/tools/schema-converter.js.map +0 -1
  67. package/dist/esm/activities/chat/tools/tool-calls.d.ts +0 -140
  68. package/dist/esm/activities/chat/tools/tool-calls.js +0 -512
  69. package/dist/esm/activities/chat/tools/tool-calls.js.map +0 -1
  70. package/dist/esm/activities/chat/tools/tool-definition.d.ts +0 -135
  71. package/dist/esm/activities/chat/tools/tool-definition.js +0 -25
  72. package/dist/esm/activities/chat/tools/tool-definition.js.map +0 -1
  73. package/dist/esm/activities/error-payload.d.ts +0 -30
  74. package/dist/esm/activities/error-payload.js +0 -54
  75. package/dist/esm/activities/error-payload.js.map +0 -1
  76. package/dist/esm/activities/generateAudio/adapter.d.ts +0 -62
  77. package/dist/esm/activities/generateAudio/adapter.js +0 -16
  78. package/dist/esm/activities/generateAudio/adapter.js.map +0 -1
  79. package/dist/esm/activities/generateAudio/index.d.ts +0 -85
  80. package/dist/esm/activities/generateAudio/index.js +0 -114
  81. package/dist/esm/activities/generateAudio/index.js.map +0 -1
  82. package/dist/esm/activities/generateImage/adapter.d.ts +0 -78
  83. package/dist/esm/activities/generateImage/adapter.js +0 -16
  84. package/dist/esm/activities/generateImage/adapter.js.map +0 -1
  85. package/dist/esm/activities/generateImage/index.d.ts +0 -134
  86. package/dist/esm/activities/generateImage/index.js +0 -121
  87. package/dist/esm/activities/generateImage/index.js.map +0 -1
  88. package/dist/esm/activities/generateSpeech/adapter.d.ts +0 -62
  89. package/dist/esm/activities/generateSpeech/adapter.js +0 -16
  90. package/dist/esm/activities/generateSpeech/adapter.js.map +0 -1
  91. package/dist/esm/activities/generateSpeech/index.d.ts +0 -96
  92. package/dist/esm/activities/generateSpeech/index.js +0 -119
  93. package/dist/esm/activities/generateSpeech/index.js.map +0 -1
  94. package/dist/esm/activities/generateTranscription/adapter.d.ts +0 -62
  95. package/dist/esm/activities/generateTranscription/adapter.js +0 -16
  96. package/dist/esm/activities/generateTranscription/adapter.js.map +0 -1
  97. package/dist/esm/activities/generateTranscription/index.d.ts +0 -109
  98. package/dist/esm/activities/generateTranscription/index.js +0 -109
  99. package/dist/esm/activities/generateTranscription/index.js.map +0 -1
  100. package/dist/esm/activities/generateVideo/adapter.d.ts +0 -145
  101. package/dist/esm/activities/generateVideo/adapter.js +0 -30
  102. package/dist/esm/activities/generateVideo/adapter.js.map +0 -1
  103. package/dist/esm/activities/generateVideo/index.d.ts +0 -223
  104. package/dist/esm/activities/generateVideo/index.js +0 -295
  105. package/dist/esm/activities/generateVideo/index.js.map +0 -1
  106. package/dist/esm/activities/generateVideo/snap.d.ts +0 -14
  107. package/dist/esm/activities/generateVideo/snap.js +0 -54
  108. package/dist/esm/activities/generateVideo/snap.js.map +0 -1
  109. package/dist/esm/activities/index.d.ts +0 -27
  110. package/dist/esm/activities/index.js +0 -43
  111. package/dist/esm/activities/index.js.map +0 -1
  112. package/dist/esm/activities/middleware/index.d.ts +0 -2
  113. package/dist/esm/activities/middleware/run.d.ts +0 -20
  114. package/dist/esm/activities/middleware/run.js +0 -42
  115. package/dist/esm/activities/middleware/run.js.map +0 -1
  116. package/dist/esm/activities/middleware/types.d.ts +0 -118
  117. package/dist/esm/activities/stream-generation-result.d.ts +0 -15
  118. package/dist/esm/activities/stream-generation-result.js +0 -49
  119. package/dist/esm/activities/stream-generation-result.js.map +0 -1
  120. package/dist/esm/activities/summarize/adapter.d.ts +0 -74
  121. package/dist/esm/activities/summarize/adapter.js +0 -16
  122. package/dist/esm/activities/summarize/adapter.js.map +0 -1
  123. package/dist/esm/activities/summarize/chat-stream-summarize.d.ts +0 -45
  124. package/dist/esm/activities/summarize/chat-stream-summarize.js +0 -212
  125. package/dist/esm/activities/summarize/chat-stream-summarize.js.map +0 -1
  126. package/dist/esm/activities/summarize/index.d.ts +0 -108
  127. package/dist/esm/activities/summarize/index.js +0 -113
  128. package/dist/esm/activities/summarize/index.js.map +0 -1
  129. package/dist/esm/adapter-internals.d.ts +0 -5
  130. package/dist/esm/adapter-internals.js +0 -10
  131. package/dist/esm/adapter-internals.js.map +0 -1
  132. package/dist/esm/client.d.ts +0 -44
  133. package/dist/esm/client.js +0 -67
  134. package/dist/esm/client.js.map +0 -1
  135. package/dist/esm/extend-adapter.d.ts +0 -152
  136. package/dist/esm/extend-adapter.js +0 -21
  137. package/dist/esm/extend-adapter.js.map +0 -1
  138. package/dist/esm/index.d.ts +0 -44
  139. package/dist/esm/index.js +0 -106
  140. package/dist/esm/index.js.map +0 -1
  141. package/dist/esm/logger/console-logger.d.ts +0 -29
  142. package/dist/esm/logger/console-logger.js +0 -83
  143. package/dist/esm/logger/console-logger.js.map +0 -1
  144. package/dist/esm/logger/internal-logger.d.ts +0 -41
  145. package/dist/esm/logger/internal-logger.js +0 -86
  146. package/dist/esm/logger/internal-logger.js.map +0 -1
  147. package/dist/esm/logger/resolve.d.ts +0 -14
  148. package/dist/esm/logger/resolve.js +0 -54
  149. package/dist/esm/logger/resolve.js.map +0 -1
  150. package/dist/esm/logger/types.d.ts +0 -75
  151. package/dist/esm/middlewares/content-guard.d.ts +0 -77
  152. package/dist/esm/middlewares/content-guard.js +0 -156
  153. package/dist/esm/middlewares/content-guard.js.map +0 -1
  154. package/dist/esm/middlewares/index.d.ts +0 -2
  155. package/dist/esm/middlewares/index.js +0 -7
  156. package/dist/esm/middlewares/index.js.map +0 -1
  157. package/dist/esm/middlewares/otel.d.ts +0 -81
  158. package/dist/esm/middlewares/otel.js +0 -745
  159. package/dist/esm/middlewares/otel.js.map +0 -1
  160. package/dist/esm/middlewares/tool-cache.d.ts +0 -1
  161. package/dist/esm/middlewares/usage-attributes.d.ts +0 -24
  162. package/dist/esm/middlewares/usage-attributes.js +0 -43
  163. package/dist/esm/middlewares/usage-attributes.js.map +0 -1
  164. package/dist/esm/realtime/index.d.ts +0 -28
  165. package/dist/esm/realtime/index.js +0 -8
  166. package/dist/esm/realtime/index.js.map +0 -1
  167. package/dist/esm/realtime/types.d.ts +0 -234
  168. package/dist/esm/stream-to-response.d.ts +0 -102
  169. package/dist/esm/stream-to-response.js +0 -121
  170. package/dist/esm/stream-to-response.js.map +0 -1
  171. package/dist/esm/strip-to-spec-middleware.d.ts +0 -18
  172. package/dist/esm/strip-to-spec-middleware.js +0 -20
  173. package/dist/esm/strip-to-spec-middleware.js.map +0 -1
  174. package/dist/esm/system-prompts.d.ts +0 -66
  175. package/dist/esm/system-prompts.js +0 -23
  176. package/dist/esm/system-prompts.js.map +0 -1
  177. package/dist/esm/tool-registry.d.ts +0 -81
  178. package/dist/esm/tool-registry.js +0 -49
  179. package/dist/esm/tool-registry.js.map +0 -1
  180. package/dist/esm/tools/provider-tool.d.ts +0 -30
  181. package/dist/esm/tools/provider-tool.js +0 -7
  182. package/dist/esm/tools/provider-tool.js.map +0 -1
  183. package/dist/esm/types.d.ts +0 -1573
  184. package/dist/esm/utilities/ag-ui-wire.d.ts +0 -44
  185. package/dist/esm/utilities/ag-ui-wire.js +0 -107
  186. package/dist/esm/utilities/ag-ui-wire.js.map +0 -1
  187. package/dist/esm/utilities/chat-params.d.ts +0 -85
  188. package/dist/esm/utilities/chat-params.js +0 -100
  189. package/dist/esm/utilities/chat-params.js.map +0 -1
  190. package/dist/esm/utilities/errors.d.ts +0 -13
  191. package/dist/esm/utilities/errors.js +0 -22
  192. package/dist/esm/utilities/errors.js.map +0 -1
  193. package/dist/esm/utilities/media-prompt.d.ts +0 -35
  194. package/dist/esm/utilities/media-prompt.js +0 -43
  195. package/dist/esm/utilities/media-prompt.js.map +0 -1
  196. package/dist/esm/utilities/numbers.d.ts +0 -8
  197. package/dist/esm/utilities/numbers.js +0 -12
  198. package/dist/esm/utilities/numbers.js.map +0 -1
  199. package/dist/esm/utilities/sampling-keys.d.ts +0 -20
  200. package/dist/esm/utilities/sampling-keys.js +0 -20
  201. package/dist/esm/utilities/sampling-keys.js.map +0 -1
  202. package/dist/esm/utilities/tool-result.d.ts +0 -21
  203. package/dist/esm/utilities/tool-result.js +0 -37
  204. package/dist/esm/utilities/tool-result.js.map +0 -1
  205. package/dist/esm/utilities/usage.d.ts +0 -31
  206. package/dist/esm/utilities/usage.js +0 -11
  207. package/dist/esm/utilities/usage.js.map +0 -1
  208. package/dist/esm/utils.d.ts +0 -17
  209. package/dist/esm/utils.js +0 -20
  210. package/dist/esm/utils.js.map +0 -1
@@ -1,264 +0,0 @@
1
- import { AnyTextAdapter } from './adapter.js';
2
- import { AnyTool, ConstrainedModelMessage, InferSchemaType, ModelMessage, SchemaInput, StreamChunk, StructuredOutputStream, TextOptions, UIMessage } from '../../types.js';
3
- import { AnyChatMiddleware, ChatMiddleware } from './middleware/types.js';
4
- import { CheckCoverage } from './middleware/builder.js';
5
- import { SystemPrompt } from '../../system-prompts.js';
6
- import { DebugOption } from '../../logger/types.js';
7
- import { ProviderTool } from '../../tools/provider-tool.js';
8
- import { ContextFromMiddleware, ContextFromTool, DefinedContext, MergeContext, UnionToIntersection } from './runtime-context-types.js';
9
- import { ChatMCPOptions } from './mcp/types.js';
10
- /** The adapter kind this activity handles */
11
- export declare const kind: "text";
12
- type AnyRuntimeTool = AnyTool;
13
- type ContextFromConsumer<T> = ContextFromTool<T> | ContextFromMiddleware<T>;
14
- type RequiredContextFromConsumerUnion<T> = T extends unknown ? undefined extends ContextFromConsumer<T> ? never : ContextFromConsumer<T> : never;
15
- type ContextFromConsumerUnion<T> = [
16
- UnionToIntersection<DefinedContext<ContextFromConsumer<T>>>
17
- ] extends [never] ? never : [RequiredContextFromConsumerUnion<T>] extends [never] ? UnionToIntersection<DefinedContext<ContextFromConsumer<T>>> | undefined : UnionToIntersection<DefinedContext<ContextFromConsumer<T>>>;
18
- type ContextFromArray<T> = T extends readonly [infer THead, ...infer TTail] ? MergeContext<ContextFromConsumer<THead>, ContextFromArray<TTail>> : T extends ReadonlyArray<infer TItem> ? ContextFromConsumerUnion<TItem> : never;
19
- type ContextFromInputs<TTools, TMiddleware> = MergeContext<ContextFromArray<NonNullable<TTools>>, ContextFromArray<NonNullable<TMiddleware>>>;
20
- type InferredContext<TTools, TMiddleware> = [
21
- ContextFromInputs<TTools, TMiddleware>
22
- ] extends [never] ? unknown : ContextFromInputs<TTools, TMiddleware>;
23
- type RequiredContextFromInputs<TTools, TMiddleware> = [
24
- ContextFromInputs<TTools, TMiddleware>
25
- ] extends [never] ? {
26
- context?: unknown;
27
- } : undefined extends ContextFromInputs<TTools, TMiddleware> ? {
28
- context?: ContextFromInputs<TTools, TMiddleware>;
29
- } : {
30
- context: ContextFromInputs<TTools, TMiddleware>;
31
- };
32
- type TextActivityOptionsWithContext<TAdapter extends AnyTextAdapter, TSchema extends SchemaInput | undefined, TStream extends boolean, TTools extends TextActivityOptions<TAdapter, TSchema, TStream, any>['tools'], TMiddleware extends TextActivityOptions<TAdapter, TSchema, TStream, any>['middleware']> = Omit<TextActivityOptions<TAdapter, TSchema, TStream, any>, 'tools' | 'middleware' | 'context'> & {
33
- tools?: TTools;
34
- middleware?: TMiddleware & CheckCoverage<Extract<TMiddleware, ReadonlyArray<AnyChatMiddleware>>>;
35
- } & RequiredContextFromInputs<TTools, TMiddleware>;
36
- /**
37
- * Options for the text activity.
38
- * Types are extracted directly from the adapter (which has pre-resolved generics).
39
- *
40
- * @template TAdapter - The text adapter type (created by a provider function)
41
- * @template TSchema - Optional Standard Schema for structured output
42
- * @template TStream - Whether to stream the output (default: true)
43
- */
44
- export interface TextActivityOptions<TAdapter extends AnyTextAdapter, TSchema extends SchemaInput | undefined, TStream extends boolean, TContext = unknown> {
45
- /** The text adapter to use (created by a provider function like openaiText('gpt-4o')) */
46
- adapter: TAdapter;
47
- /**
48
- * Conversation messages. Accepts:
49
- * - `ConstrainedModelMessage` — content types constrained by the adapter's input modalities.
50
- * - `ModelMessage` — unconstrained model message (e.g., forwarded from an AG-UI wire payload).
51
- * - `UIMessage` — parts-based UI representation; converted internally via `convertMessagesToModelMessages`.
52
- *
53
- * The three shapes can be mixed in a single array (e.g., when forwarding a wire payload that includes both anchor UIMessages and AG-UI fan-out ModelMessages).
54
- */
55
- messages?: Array<UIMessage | ModelMessage | ConstrainedModelMessage<{
56
- inputModalities: TAdapter['~types']['inputModalities'];
57
- messageMetadataByModality: TAdapter['~types']['messageMetadataByModality'];
58
- }>> | undefined;
59
- /**
60
- * System prompts to prepend to the conversation.
61
- *
62
- * Accepts plain strings or `{ content, metadata }` objects. The `metadata`
63
- * field is typed by the adapter — Anthropic narrows it to
64
- * `AnthropicSystemPromptMetadata` (with `cache_control` for prompt
65
- * caching), providers without per-prompt metadata reject the field
66
- * entirely.
67
- */
68
- systemPrompts?: Array<SystemPrompt<TAdapter['~types']['systemPromptMetadata']>> | undefined;
69
- /**
70
- * Tools for function calling (auto-executed when called).
71
- *
72
- * Accepts two shapes:
73
- * - User-defined tools via `toolDefinition()` — plain `Tool`, always assignable.
74
- * - Provider tools from `@tanstack/ai-<provider>/tools` (e.g. `webSearchTool`)
75
- * — branded and type-checked against the selected model's
76
- * `supports.tools` list. Passing an unsupported tool produces a
77
- * compile-time error on the array element.
78
- */
79
- tools?: Array<(AnyRuntimeTool & {
80
- readonly '~toolKind'?: never;
81
- }) | ProviderTool<string, TAdapter['~types']['toolCapabilities'][number]>> | undefined;
82
- /**
83
- * Hand MCP clients/pools to chat(): their tools are discovered at run start
84
- * and merged into the run; `connection` controls whether chat() closes them
85
- * when the run ends. See docs/tools/mcp.md "Managing MCP clients with chat()".
86
- */
87
- mcp?: ChatMCPOptions;
88
- /** Additional metadata to attach to the request. */
89
- metadata?: TextOptions['metadata'];
90
- /** Model-specific provider options (type comes from adapter) */
91
- modelOptions?: TAdapter['~types']['providerOptions'];
92
- /** AbortController for cancellation */
93
- abortController?: TextOptions['abortController'];
94
- /** Strategy for controlling the agent loop */
95
- agentLoopStrategy?: TextOptions['agentLoopStrategy'];
96
- /** Unique conversation identifier for tracking */
97
- conversationId?: TextOptions['conversationId'];
98
- /** Thread/conversation ID for AG-UI protocol. Auto-generated if not provided. */
99
- threadId?: TextOptions['threadId'];
100
- /** Run ID override for AG-UI protocol. Auto-generated by adapter if not provided. */
101
- runId?: TextOptions['runId'];
102
- /** Parent run ID for AG-UI protocol nested run correlation. */
103
- parentRunId?: TextOptions['parentRunId'];
104
- /**
105
- * Optional Standard Schema for structured output.
106
- * When provided, the activity will:
107
- * 1. Run the full agentic loop (executing tools as needed)
108
- * 2. Once complete, return a Promise with the parsed output matching the schema
109
- *
110
- * Supports any Standard Schema compliant library (Zod v4+, ArkType, Valibot, etc.)
111
- *
112
- * @example
113
- * ```ts
114
- * const result = await chat({
115
- * adapter: openaiText('gpt-4o'),
116
- * messages: [{ role: 'user', content: 'Generate a person' }],
117
- * outputSchema: z.object({ name: z.string(), age: z.number() })
118
- * })
119
- * // result is { name: string, age: number }
120
- * ```
121
- */
122
- outputSchema?: TSchema;
123
- /**
124
- * Whether to stream the text result.
125
- * When true (default), returns an AsyncIterable<StreamChunk> for streaming output.
126
- * When false, returns a Promise<string> with the collected text content.
127
- *
128
- * Note: If outputSchema is provided, this option is ignored and the result
129
- * is always a Promise<InferSchemaType<TSchema>>.
130
- *
131
- * @default true
132
- *
133
- * @example Non-streaming text
134
- * ```ts
135
- * const text = await chat({
136
- * adapter: openaiText('gpt-4o'),
137
- * messages: [{ role: 'user', content: 'Hello!' }],
138
- * stream: false
139
- * })
140
- * // text is a string with the full response
141
- * ```
142
- */
143
- stream?: TStream;
144
- /**
145
- * Optional middleware array for observing/transforming chat behavior.
146
- * Middleware hooks are called in array order. See {@link ChatMiddleware} for available hooks.
147
- *
148
- * @example
149
- * ```ts
150
- * const stream = chat({
151
- * adapter: openaiText('gpt-4o'),
152
- * messages: [...],
153
- * middleware: [loggingMiddleware, redactionMiddleware],
154
- * })
155
- * ```
156
- */
157
- middleware?: Array<ChatMiddleware<TContext>>;
158
- /**
159
- * Runtime context value passed to middleware hooks and server tools.
160
- */
161
- context?: TContext;
162
- /**
163
- * Enable debug logging. Pass `true` to enable all categories with the default
164
- * console logger, `false` to silence everything, or a `DebugConfig` object for
165
- * granular control and/or a custom `Logger`. Defaults to `undefined`, which
166
- * means only the `errors` category is active.
167
- */
168
- debug?: DebugOption;
169
- }
170
- /**
171
- * Create typed options for the chat() function without executing.
172
- * This is useful for pre-defining configurations with full type inference.
173
- *
174
- * @example
175
- * ```ts
176
- * const chatOptions = createChatOptions({
177
- * adapter: anthropicText('claude-sonnet-4-5'),
178
- * })
179
- *
180
- * const stream = chat({ ...chatOptions, messages })
181
- * ```
182
- */
183
- export declare function createChatOptions<TAdapter extends AnyTextAdapter, TSchema extends SchemaInput | undefined = undefined, TStream extends boolean = true, const TTools extends TextActivityOptions<TAdapter, TSchema, TStream, any>['tools'] = TextActivityOptions<TAdapter, TSchema, TStream, any>['tools'], const TMiddleware extends TextActivityOptions<TAdapter, TSchema, TStream, any>['middleware'] = TextActivityOptions<TAdapter, TSchema, TStream, any>['middleware']>(options: TextActivityOptionsWithContext<TAdapter, TSchema, TStream, TTools, TMiddleware>): TextActivityOptions<TAdapter, TSchema, TStream, InferredContext<TTools, TMiddleware>>;
184
- /**
185
- * Result type for the text activity.
186
- * - If outputSchema is provided AND stream is explicitly true:
187
- * StructuredOutputStream<InferSchemaType<TSchema>> — yields raw JSON deltas
188
- * via TEXT_MESSAGE_CONTENT plus a terminal StructuredOutputCompleteEvent
189
- * carrying the validated object.
190
- * - If outputSchema is provided without explicit stream:true:
191
- * Promise<InferSchemaType<TSchema>>.
192
- * - If stream is explicitly false (no schema): Promise<string>.
193
- * - Otherwise (default): AsyncIterable<StreamChunk>.
194
- *
195
- * `[TStream] extends [true]` is used (not `TStream extends true`) so that the
196
- * default `boolean` value of `TStream` does *not* match the streaming branch.
197
- * Without this, plain `chat({ outputSchema })` would type as a stream while
198
- * the runtime returns a Promise — see issue #526.
199
- */
200
- export type TextActivityResult<TSchema extends SchemaInput | undefined, TStream extends boolean = boolean> = TSchema extends SchemaInput ? [TStream] extends [true] ? StructuredOutputStream<InferSchemaType<TSchema>> : Promise<InferSchemaType<TSchema>> : [TStream] extends [false] ? Promise<string> : AsyncIterable<StreamChunk>;
201
- /**
202
- * Text activity - handles agentic text generation, one-shot text generation, and agentic structured output.
203
- *
204
- * This activity supports four modes:
205
- * 1. **Streaming agentic text**: Stream responses with automatic tool execution
206
- * 2. **Streaming one-shot text**: Simple streaming request/response without tools
207
- * 3. **Non-streaming text**: Returns collected text as a string (stream: false)
208
- * 4. **Agentic structured output**: Run tools, then return structured data
209
- *
210
- * @example Full agentic text (streaming with tools)
211
- * ```ts
212
- * import { chat } from '@tanstack/ai'
213
- * import { openaiText } from '@tanstack/ai-openai'
214
- *
215
- * for await (const chunk of chat({
216
- * adapter: openaiText('gpt-4o'),
217
- * messages: [{ role: 'user', content: 'What is the weather?' }],
218
- * tools: [weatherTool]
219
- * })) {
220
- * if (chunk.type === 'TEXT_MESSAGE_CONTENT') {
221
- * console.log(chunk.delta)
222
- * }
223
- * }
224
- * ```
225
- *
226
- * @example One-shot text (streaming without tools)
227
- * ```ts
228
- * for await (const chunk of chat({
229
- * adapter: openaiText('gpt-4o'),
230
- * messages: [{ role: 'user', content: 'Hello!' }]
231
- * })) {
232
- * console.log(chunk)
233
- * }
234
- * ```
235
- *
236
- * @example Non-streaming text (stream: false)
237
- * ```ts
238
- * const text = await chat({
239
- * adapter: openaiText('gpt-4o'),
240
- * messages: [{ role: 'user', content: 'Hello!' }],
241
- * stream: false
242
- * })
243
- * // text is a string with the full response
244
- * ```
245
- *
246
- * @example Agentic structured output (tools + structured response)
247
- * ```ts
248
- * import { z } from 'zod'
249
- *
250
- * const result = await chat({
251
- * adapter: openaiText('gpt-4o'),
252
- * messages: [{ role: 'user', content: 'Research and summarize the topic' }],
253
- * tools: [researchTool, analyzeTool],
254
- * outputSchema: z.object({
255
- * summary: z.string(),
256
- * keyPoints: z.array(z.string())
257
- * })
258
- * })
259
- * // result is { summary: string, keyPoints: string[] }
260
- * ```
261
- */
262
- export declare function chat<TAdapter extends AnyTextAdapter, TSchema extends SchemaInput | undefined = undefined, TStream extends boolean = boolean, const TTools extends TextActivityOptions<TAdapter, TSchema, TStream, any>['tools'] = TextActivityOptions<TAdapter, TSchema, TStream, any>['tools'], const TMiddleware extends TextActivityOptions<TAdapter, TSchema, TStream, any>['middleware'] = TextActivityOptions<TAdapter, TSchema, TStream, any>['middleware']>(options: TextActivityOptionsWithContext<TAdapter, TSchema, TStream, TTools, TMiddleware>): TextActivityResult<TSchema, TStream>;
263
- export type { TextAdapter, TextAdapterConfig, StructuredOutputOptions, StructuredOutputResult, } from './adapter.js';
264
- export { BaseTextAdapter } from './adapter.js';