ai 7.0.152 → 7.0.154

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 (44) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/dist/index.d.ts +224 -9
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +440 -120
  5. package/dist/index.js.map +1 -1
  6. package/dist/internal/index.d.ts +52 -4
  7. package/dist/internal/index.d.ts.map +1 -1
  8. package/dist/internal/index.js +26 -20
  9. package/dist/internal/index.js.map +1 -1
  10. package/docs/02-getting-started/02-nextjs-app-router.mdx +2 -2
  11. package/docs/02-getting-started/08-angular.mdx +327 -0
  12. package/docs/02-getting-started/{03-nextjs-pages-router.mdx → 10-nextjs-pages-router.mdx} +12 -20
  13. package/docs/02-getting-started/index.mdx +2 -0
  14. package/docs/03-agents/01-overview.mdx +1 -0
  15. package/docs/03-agents/04-loop-control.mdx +117 -0
  16. package/docs/03-agents/06-tool-approvals.mdx +1 -0
  17. package/docs/03-agents/09-chat-platforms.mdx +164 -0
  18. package/docs/03-agents/index.mdx +6 -0
  19. package/docs/03-ai-sdk-core/60-telemetry.mdx +16 -3
  20. package/docs/03-ai-sdk-core/65-lifecycle-callbacks.mdx +10 -0
  21. package/docs/04-ai-sdk-ui/21-transport.mdx +86 -0
  22. package/docs/07-reference/01-ai-sdk-core/01-generate-text.mdx +46 -0
  23. package/docs/07-reference/01-ai-sdk-core/02-stream-text.mdx +46 -0
  24. package/docs/07-reference/01-ai-sdk-core/16-tool-loop-agent.mdx +35 -0
  25. package/package.json +12 -12
  26. package/src/agent/create-agent-ui-stream-response.ts +3 -0
  27. package/src/agent/create-agent-ui-stream.ts +16 -0
  28. package/src/agent/pipe-agent-ui-stream-to-response.ts +3 -0
  29. package/src/agent/tool-loop-agent-settings.ts +37 -1
  30. package/src/agent/tool-loop-agent.ts +43 -7
  31. package/src/generate-object/generate-object.ts +1 -1
  32. package/src/generate-text/generate-text-events.ts +37 -0
  33. package/src/generate-text/generate-text.ts +528 -374
  34. package/src/generate-text/index.ts +8 -0
  35. package/src/generate-text/prepare-step.ts +12 -0
  36. package/src/generate-text/restricted-telemetry-dispatcher.ts +31 -18
  37. package/src/generate-text/steering.ts +293 -0
  38. package/src/generate-text/step-result.ts +18 -0
  39. package/src/generate-text/stream-text.ts +664 -473
  40. package/src/middleware/extract-json-middleware.ts +8 -2
  41. package/src/middleware/extract-reasoning-middleware.ts +42 -30
  42. package/src/telemetry/create-telemetry-dispatcher.ts +3 -0
  43. package/src/telemetry/telemetry.ts +15 -1
  44. package/src/util/is-caused-by-abort.ts +20 -0
@@ -17,6 +17,7 @@ import {
17
17
  createAgentUIStream,
18
18
  type AgentUIMessageStreamOptions,
19
19
  } from './create-agent-ui-stream';
20
+ import type { Steering } from '../generate-text/steering';
20
21
 
21
22
  /**
22
23
  * Pipes the agent UI message stream to a Node.js ServerResponse object.
@@ -28,6 +29,7 @@ import {
28
29
  * @param abortSignal - Abort signal. Optional.
29
30
  * @param timeout - Timeout in milliseconds. Optional.
30
31
  * @param experimental_sandbox - The sandbox environment that is passed through to tool execution. Optional.
32
+ * @param experimental_steering - A steering object that adds user messages to the run at the next step boundary. Only supported by `ToolLoopAgent`. Optional.
31
33
  * @param options - The options for the agent. Optional.
32
34
  * @param experimental_transform - Stream transformations. Optional.
33
35
  * @param onStepEnd - Callback that is called when each step ends. Optional.
@@ -72,6 +74,7 @@ export async function pipeAgentUIStreamToResponse<
72
74
  abortSignal?: AbortSignal;
73
75
  timeout?: TimeoutConfiguration<TOOLS>;
74
76
  experimental_sandbox?: SandboxSession;
77
+ experimental_steering?: Steering;
75
78
  options?: CALL_OPTIONS;
76
79
  experimental_transform?: Arrayable<StreamTextTransform<TOOLS>>;
77
80
  } & UIMessageStreamResponseInit &
@@ -13,6 +13,7 @@ import type {
13
13
  GenerateTextOnEndCallback,
14
14
  GenerateTextOnStartCallback,
15
15
  GenerateTextOnStepEndCallback,
16
+ GenerateTextOnStepErrorCallback,
16
17
  GenerateTextOnStepFinishCallback,
17
18
  GenerateTextOnStepStartCallback,
18
19
  } from '../generate-text/generate-text-events';
@@ -24,7 +25,11 @@ import type {
24
25
  import type { Output } from '../generate-text/output';
25
26
  import type { PrepareStepFunction } from '../generate-text/prepare-step';
26
27
  import type { StopCondition } from '../generate-text/stop-condition';
27
- import type { StreamTextInclude } from '../generate-text/stream-text';
28
+ import type {
29
+ StreamTextInclude,
30
+ StreamTextOnAbortCallback,
31
+ StreamTextOnErrorCallback,
32
+ } from '../generate-text/stream-text';
28
33
  import type { ToolApprovalConfiguration } from '../generate-text/tool-approval-configuration';
29
34
  import type { Experimental_ToolCallers } from '../generate-text/tool-caller-configuration';
30
35
  import type { ToolCallRepairFunction } from '../generate-text/tool-call-repair-function';
@@ -253,6 +258,37 @@ export type ToolLoopAgentSettings<
253
258
  NoInfer<RUNTIME_CONTEXT>
254
259
  >;
255
260
 
261
+ /**
262
+ * Callback that is called when a step that started fails before it
263
+ * produces a step result. `onStepEnd` is not called for such a step.
264
+ *
265
+ * Unlike `onStepStart` and `onStepEnd`, it can only be set here and not
266
+ * per call, because the per-call callbacks are shared with other `Agent`
267
+ * implementations that do not report step errors.
268
+ */
269
+ onStepError?: GenerateTextOnStepErrorCallback<
270
+ NoInfer<TOOLS>,
271
+ NoInfer<RUNTIME_CONTEXT>,
272
+ NoInfer<OUTPUT>
273
+ >;
274
+
275
+ /**
276
+ * Callback that is called when an error occurs. For `stream()` it receives
277
+ * the errors of the stream, for `generate()` it receives the error before
278
+ * it is thrown. Aborts and timeouts are not reported.
279
+ */
280
+ onError?: StreamTextOnErrorCallback;
281
+
282
+ /**
283
+ * Callback that is called when `stream()` is aborted or times out.
284
+ * `generate()` does not call it: an aborted or timed out `generate()` call
285
+ * rejects, and its step ends with `onStepError`.
286
+ */
287
+ onAbort?: StreamTextOnAbortCallback<
288
+ NoInfer<TOOLS>,
289
+ NoInfer<RUNTIME_CONTEXT>
290
+ >;
291
+
256
292
  /**
257
293
  * Callback that is called when each step (LLM call) ends, including intermediate steps.
258
294
  *
@@ -15,8 +15,11 @@ import type { GenerateTextResult } from '../generate-text/generate-text-result';
15
15
  import type { Output } from '../generate-text/output';
16
16
  import { streamText } from '../generate-text/stream-text';
17
17
  import type { StreamTextResult } from '../generate-text/stream-text-result';
18
+ import type { Steering } from '../generate-text/steering';
18
19
  import type { Prompt } from '../prompt';
20
+ import { isCausedByAbort } from '../util/is-caused-by-abort';
19
21
  import { mergeCallbacks } from '../util/merge-callbacks';
22
+ import { notify } from '../util/notify';
20
23
  import type {
21
24
  Agent,
22
25
  AgentCallParameters,
@@ -25,6 +28,17 @@ import type {
25
28
  import type { ToolLoopAgentSettings } from './tool-loop-agent-settings';
26
29
  import { createDefaultStopCondition } from './default-stop-condition';
27
30
 
31
+ /**
32
+ * Call parameters that only `ToolLoopAgent` supports.
33
+ */
34
+ export interface ToolLoopAgentCallExtensions {
35
+ /**
36
+ * A steering object created with `experimental_createSteering()`.
37
+ * Messages sent with it are added to the run at the next step boundary.
38
+ */
39
+ experimental_steering?: Steering;
40
+ }
41
+
28
42
  /**
29
43
  * A tool loop agent is an agent that runs tools in a loop. In each step,
30
44
  * it calls the LLM, and if there are tool calls, it executes the tools
@@ -96,6 +110,8 @@ export class ToolLoopAgent<
96
110
  | 'onToolExecutionEnd'
97
111
  | 'onStepEnd'
98
112
  | 'onStepFinish'
113
+ | 'onError'
114
+ | 'onAbort'
99
115
  | 'onEnd'
100
116
  | 'onFinish'
101
117
  > &
@@ -122,6 +138,8 @@ export class ToolLoopAgent<
122
138
  onToolExecutionEnd: _settingsOnToolExecutionEnd,
123
139
  onStepEnd: _settingsOnStepEnd,
124
140
  onStepFinish: _settingsOnStepFinish,
141
+ onError: _settingsOnError,
142
+ onAbort: _settingsOnAbort,
125
143
  onFinish: _settingsOnFinish,
126
144
  onEnd: _settingsOnEnd,
127
145
  ...settingsWithoutCallbacks
@@ -198,6 +216,7 @@ export class ToolLoopAgent<
198
216
  abortSignal,
199
217
  timeout,
200
218
  experimental_sandbox: sandbox,
219
+ experimental_steering: steering,
201
220
  onStart,
202
221
  experimental_onStart,
203
222
  onStepStart,
@@ -209,7 +228,8 @@ export class ToolLoopAgent<
209
228
  onFinish,
210
229
  onEnd = onFinish,
211
230
  ...options
212
- }: AgentCallParameters<CALL_OPTIONS, TOOLS, RUNTIME_CONTEXT>): Promise<
231
+ }: AgentCallParameters<CALL_OPTIONS, TOOLS, RUNTIME_CONTEXT> &
232
+ ToolLoopAgentCallExtensions): Promise<
213
233
  GenerateTextResult<TOOLS, RUNTIME_CONTEXT, OUTPUT>
214
234
  > {
215
235
  const generate = generateText<TOOLS, RUNTIME_CONTEXT, OUTPUT>;
@@ -221,6 +241,7 @@ export class ToolLoopAgent<
221
241
  abortSignal,
222
242
  timeout: timeout ?? preparedCall.timeout,
223
243
  experimental_sandbox: sandbox,
244
+ experimental_steering: steering,
224
245
  onStart: mergeCallbacks(
225
246
  this.settings.onStart ?? this.settings.experimental_onStart,
226
247
  (onStart ?? experimental_onStart) as
@@ -248,11 +269,21 @@ export class ToolLoopAgent<
248
269
  onEnd: mergeCallbacks(this.settings.onEnd, onEnd),
249
270
  };
250
271
 
251
- return await generate({
252
- ...preparedCall,
253
- ...callbackArgs,
254
- headers: this.agentHeaders(preparedCall),
255
- } as unknown as Parameters<typeof generate>[0]);
272
+ try {
273
+ return await generate({
274
+ ...preparedCall,
275
+ ...callbackArgs,
276
+ headers: this.agentHeaders(preparedCall),
277
+ } as unknown as Parameters<typeof generate>[0]);
278
+ } catch (error) {
279
+ // generateText has no onError option, so report the error before it is
280
+ // thrown. Aborts (with any reason) and timeouts are not reported, like in
281
+ // streamText:
282
+ if (!isCausedByAbort(error) && !abortSignal?.aborted) {
283
+ await notify({ event: { error }, callbacks: this.settings.onError });
284
+ }
285
+ throw error;
286
+ }
256
287
  }
257
288
 
258
289
  /**
@@ -262,6 +293,7 @@ export class ToolLoopAgent<
262
293
  abortSignal,
263
294
  timeout,
264
295
  experimental_sandbox: sandbox,
296
+ experimental_steering: steering,
265
297
  experimental_transform,
266
298
  onStart,
267
299
  experimental_onStart,
@@ -274,7 +306,8 @@ export class ToolLoopAgent<
274
306
  onFinish,
275
307
  onEnd = onFinish,
276
308
  ...options
277
- }: AgentStreamParameters<CALL_OPTIONS, TOOLS, RUNTIME_CONTEXT>): Promise<
309
+ }: AgentStreamParameters<CALL_OPTIONS, TOOLS, RUNTIME_CONTEXT> &
310
+ ToolLoopAgentCallExtensions): Promise<
278
311
  StreamTextResult<TOOLS, RUNTIME_CONTEXT, OUTPUT>
279
312
  > {
280
313
  const stream = streamText<TOOLS, RUNTIME_CONTEXT, OUTPUT>;
@@ -286,6 +319,7 @@ export class ToolLoopAgent<
286
319
  abortSignal,
287
320
  timeout: timeout ?? preparedCall.timeout,
288
321
  experimental_sandbox: sandbox,
322
+ experimental_steering: steering,
289
323
  experimental_transform,
290
324
  onStart: mergeCallbacks(
291
325
  this.settings.onStart ?? this.settings.experimental_onStart,
@@ -312,6 +346,8 @@ export class ToolLoopAgent<
312
346
  onStepEnd ?? onStepFinish,
313
347
  ),
314
348
  onEnd: mergeCallbacks(this.settings.onEnd, onEnd),
349
+ onError: this.settings.onError,
350
+ onAbort: this.settings.onAbort,
315
351
  };
316
352
 
317
353
  return await stream({
@@ -479,7 +479,7 @@ export async function generateObject<
479
479
  });
480
480
 
481
481
  const object = await parseAndValidateObjectResultWithRepair(
482
- text,
482
+ generateResult.structuredOutputText ?? text,
483
483
  outputStrategy,
484
484
  repairText,
485
485
  {
@@ -392,6 +392,43 @@ export type GenerateTextOnStepFinishCallback<
392
392
  RUNTIME_CONTEXT extends Context = Context,
393
393
  > = GenerateTextOnStepEndCallback<TOOLS, RUNTIME_CONTEXT>;
394
394
 
395
+ /**
396
+ * Event passed to the `onStepError` callback.
397
+ *
398
+ * Contains the step start event of the step that failed before it produced
399
+ * a step result, along with the error.
400
+ */
401
+ export type GenerateTextStepErrorEvent<
402
+ TOOLS extends ToolSet = ToolSet,
403
+ RUNTIME_CONTEXT extends Context = Context,
404
+ OUTPUT extends Output = Output,
405
+ > = GenerateTextStepStartEvent<TOOLS, RUNTIME_CONTEXT, OUTPUT> & {
406
+ /** The error that ended the step. */
407
+ readonly error: unknown;
408
+ };
409
+
410
+ /**
411
+ * Callback that is set using the `onStepError` option.
412
+ *
413
+ * Called when a step that started fails before it produces a step result:
414
+ * when the provider call throws or rejects, or when the model stream ends
415
+ * without output. `onStepEnd` is not called for such a step. A provider
416
+ * `error` chunk does not fail the step, it still ends with `onStepEnd` and
417
+ * `finishReason: 'error'`.
418
+ *
419
+ * In `generateText`, an abort or timeout also ends the step here. The error
420
+ * is the abort or timeout error, or a provider error that has it as its
421
+ * `cause`. In `streamText`, an abort or timeout ends the step
422
+ * with `onAbort` instead.
423
+ *
424
+ * @param event - The step that failed and the error.
425
+ */
426
+ export type GenerateTextOnStepErrorCallback<
427
+ TOOLS extends ToolSet = ToolSet,
428
+ RUNTIME_CONTEXT extends Context = Context,
429
+ OUTPUT extends Output = Output,
430
+ > = Callback<GenerateTextStepErrorEvent<TOOLS, RUNTIME_CONTEXT, OUTPUT>>;
431
+
395
432
  /**
396
433
  * Callback that is set using the `onEnd` option.
397
434
  *