ai 7.0.0-beta.111 → 7.0.0-beta.113

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 (95) hide show
  1. package/CHANGELOG.md +60 -0
  2. package/dist/index.d.ts +2640 -2457
  3. package/dist/index.js +1136 -640
  4. package/dist/index.js.map +1 -1
  5. package/dist/internal/index.d.ts +324 -262
  6. package/dist/internal/index.js +23 -20
  7. package/dist/internal/index.js.map +1 -1
  8. package/docs/02-foundations/03-prompts.mdx +13 -10
  9. package/docs/03-agents/01-overview.mdx +16 -0
  10. package/docs/03-agents/02-building-agents.mdx +78 -14
  11. package/docs/03-agents/06-subagents.mdx +3 -1
  12. package/docs/03-agents/07-workflow-agent.mdx +130 -3
  13. package/docs/03-agents/index.mdx +4 -2
  14. package/docs/03-ai-sdk-core/05-generating-text.mdx +53 -23
  15. package/docs/03-ai-sdk-core/15-tools-and-tool-calling.mdx +154 -115
  16. package/docs/03-ai-sdk-core/17-runtime-and-tool-context.mdx +215 -0
  17. package/docs/03-ai-sdk-core/60-telemetry.mdx +115 -27
  18. package/docs/03-ai-sdk-core/65-event-listeners.mdx +176 -153
  19. package/docs/03-ai-sdk-core/index.mdx +6 -0
  20. package/docs/04-ai-sdk-ui/03-chatbot-tool-usage.mdx +46 -4
  21. package/docs/04-ai-sdk-ui/50-stream-protocol.mdx +43 -0
  22. package/docs/07-reference/01-ai-sdk-core/01-generate-text.mdx +172 -128
  23. package/docs/07-reference/01-ai-sdk-core/02-stream-text.mdx +172 -110
  24. package/docs/07-reference/01-ai-sdk-core/05-embed.mdx +4 -16
  25. package/docs/07-reference/01-ai-sdk-core/06-embed-many.mdx +4 -16
  26. package/docs/07-reference/01-ai-sdk-core/06-rerank.mdx +2 -2
  27. package/docs/07-reference/01-ai-sdk-core/15-agent.mdx +4 -4
  28. package/docs/07-reference/01-ai-sdk-core/16-tool-loop-agent.mdx +92 -126
  29. package/docs/07-reference/01-ai-sdk-core/17-create-agent-ui-stream.mdx +1 -1
  30. package/docs/07-reference/01-ai-sdk-core/18-create-agent-ui-stream-response.mdx +1 -1
  31. package/docs/07-reference/01-ai-sdk-core/18-pipe-agent-ui-stream-to-response.mdx +1 -1
  32. package/docs/07-reference/01-ai-sdk-core/20-tool.mdx +12 -1
  33. package/docs/07-reference/01-ai-sdk-core/22-dynamic-tool.mdx +1 -1
  34. package/docs/07-reference/01-ai-sdk-core/23-create-mcp-client.mdx +14 -1
  35. package/docs/07-reference/01-ai-sdk-core/29-filter-active-tools.mdx +14 -2
  36. package/docs/07-reference/01-ai-sdk-core/30-model-message.mdx +5 -2
  37. package/docs/07-reference/02-ai-sdk-ui/31-convert-to-model-messages.mdx +9 -0
  38. package/docs/07-reference/03-ai-sdk-rsc/01-stream-ui.mdx +7 -0
  39. package/docs/07-reference/04-ai-sdk-workflow/01-workflow-agent.mdx +48 -29
  40. package/docs/08-migration-guides/23-migration-guide-7-0.mdx +99 -0
  41. package/package.json +9 -7
  42. package/src/agent/agent.ts +10 -10
  43. package/src/agent/create-agent-ui-stream-response.ts +5 -4
  44. package/src/agent/create-agent-ui-stream.ts +5 -4
  45. package/src/agent/index.ts +0 -4
  46. package/src/agent/pipe-agent-ui-stream-to-response.ts +5 -4
  47. package/src/agent/tool-loop-agent-settings.ts +28 -41
  48. package/src/agent/tool-loop-agent.ts +23 -10
  49. package/src/embed/embed-events.ts +8 -44
  50. package/src/embed/embed-many.ts +14 -34
  51. package/src/embed/embed.ts +11 -23
  52. package/src/generate-object/generate-object.ts +20 -24
  53. package/src/generate-object/index.ts +4 -4
  54. package/src/generate-object/stream-object.ts +26 -27
  55. package/src/generate-object/structured-output-events.ts +4 -25
  56. package/src/generate-text/active-tools.ts +12 -0
  57. package/src/generate-text/content-part.ts +5 -3
  58. package/src/generate-text/create-execute-tools-transformation.ts +80 -38
  59. package/src/generate-text/execute-tool-call.ts +31 -37
  60. package/src/generate-text/filter-active-tools.ts +38 -0
  61. package/src/generate-text/{core-events.ts → generate-text-events.ts} +108 -113
  62. package/src/generate-text/generate-text.ts +214 -167
  63. package/src/generate-text/index.ts +29 -14
  64. package/src/generate-text/language-model-events.ts +79 -0
  65. package/src/generate-text/prepare-step.ts +2 -1
  66. package/src/generate-text/resolve-tool-approval.ts +134 -0
  67. package/src/generate-text/restricted-telemetry-dispatcher.ts +247 -0
  68. package/src/generate-text/step-result.ts +0 -9
  69. package/src/generate-text/stream-language-model-call.ts +245 -35
  70. package/src/generate-text/stream-text-result.ts +5 -0
  71. package/src/generate-text/stream-text.ts +168 -215
  72. package/src/generate-text/to-response-messages.ts +34 -2
  73. package/src/generate-text/tool-approval-configuration.ts +128 -0
  74. package/src/generate-text/tool-approval-request-output.ts +7 -0
  75. package/src/generate-text/tool-approval-response-output.ts +35 -0
  76. package/src/generate-text/tool-execution-events.ts +120 -65
  77. package/src/index.ts +1 -0
  78. package/src/prompt/convert-to-language-model-prompt.ts +1 -1
  79. package/src/prompt/prompt.ts +9 -0
  80. package/src/prompt/standardize-prompt.ts +38 -27
  81. package/src/rerank/index.ts +3 -3
  82. package/src/rerank/rerank-events.ts +8 -44
  83. package/src/rerank/rerank.ts +13 -33
  84. package/src/telemetry/{create-unified-telemetry.ts → create-telemetry-dispatcher.ts} +71 -24
  85. package/src/telemetry/index.ts +1 -1
  86. package/src/telemetry/telemetry.ts +92 -36
  87. package/src/ui/convert-to-model-messages.ts +21 -1
  88. package/src/ui/process-ui-message-stream.ts +48 -1
  89. package/src/ui/ui-messages.ts +10 -0
  90. package/src/ui/validate-ui-messages.ts +10 -0
  91. package/src/ui-message-stream/ui-message-chunks.ts +18 -0
  92. package/src/util/merge-objects.ts +5 -0
  93. package/src/generate-text/filter-active-tool.ts +0 -41
  94. package/src/generate-text/is-tool-approval-needed.ts +0 -66
  95. package/src/generate-text/tool-needs-approval-configuration.ts +0 -21
@@ -1,4 +1,4 @@
1
- import { SystemModelMessage, ModelMessage, ToolSet, ProviderOptions, InferToolInput, InferToolOutput, AssistantModelMessage, ToolModelMessage, Context, InferToolSetContext, ReasoningPart, ReasoningFilePart, Arrayable } from '@ai-sdk/provider-utils';
1
+ import { SystemModelMessage, ModelMessage, ToolSet, ProviderOptions, Arrayable, InferToolInput, InferToolOutput, AssistantModelMessage, ToolModelMessage, Context, InferToolSetContext, ReasoningPart, ReasoningFilePart, InferToolContext } from '@ai-sdk/provider-utils';
2
2
  export { convertAsyncIteratorToReadableStream } from '@ai-sdk/provider-utils';
3
3
  import { LanguageModelV4Prompt, LanguageModelV4, LanguageModelV3, LanguageModelV2, SharedV4Warning, LanguageModelV4Source, LanguageModelV4ToolChoice, LanguageModelV4FunctionTool, LanguageModelV4ProviderTool, LanguageModelV4CallOptions, LanguageModelV4Usage, JSONObject, EmbeddingModelV4Embedding, SharedV4ProviderMetadata, ProviderV4, ProviderV3, ProviderV2 } from '@ai-sdk/provider';
4
4
  import { GatewayModelId } from '@ai-sdk/gateway';
@@ -52,6 +52,14 @@ type Prompt = {
52
52
  * System message to include in the prompt. Can be used with `prompt` or `messages`.
53
53
  */
54
54
  system?: string | SystemModelMessage | Array<SystemModelMessage>;
55
+ /**
56
+ * Whether system messages are allowed in the `prompt` or `messages` fields.
57
+ *
58
+ * When disabled, system messages must be provided through the `system` option.
59
+ *
60
+ * @default false
61
+ */
62
+ allowSystemInMessages?: boolean;
55
63
  } & ({
56
64
  /**
57
65
  * A prompt. It can be either a text prompt or a list of messages.
@@ -90,7 +98,18 @@ type StandardizedPrompt = {
90
98
  */
91
99
  messages: ModelMessage[];
92
100
  };
93
- declare function standardizePrompt(prompt: Prompt): Promise<StandardizedPrompt>;
101
+ /**
102
+ * Converts a prompt input into a standardized prompt with validated model
103
+ * messages.
104
+ *
105
+ * @param prompt - The prompt definition to standardize.
106
+ * Set `allowSystemInMessages` to true to allow system messages in the
107
+ * `prompt` or `messages` fields. System messages in the `system` option are
108
+ * always allowed.
109
+ * @returns The standardized prompt.
110
+ * @throws {InvalidPromptError} When the prompt is invalid.
111
+ */
112
+ declare function standardizePrompt({ allowSystemInMessages, system, prompt, messages, }: Prompt): Promise<StandardizedPrompt>;
94
113
 
95
114
  declare function convertToLanguageModelPrompt({ prompt, supportedUrls, download, provider, }: {
96
115
  prompt: StandardizedPrompt;
@@ -452,7 +471,7 @@ type LogWarningsFunction = (options: {
452
471
  *
453
472
  * Called when the operation begins, before the embedding model is called.
454
473
  */
455
- interface EmbedOnStartEvent {
474
+ type EmbedStartEvent = {
456
475
  /** Unique identifier for this embed call, used to correlate events. */
457
476
  readonly callId: string;
458
477
  /** Identifies the operation type (e.g. 'ai.embed' or 'ai.embedMany'). */
@@ -469,21 +488,13 @@ interface EmbedOnStartEvent {
469
488
  readonly headers: Record<string, string | undefined> | undefined;
470
489
  /** Additional provider-specific options. */
471
490
  readonly providerOptions: ProviderOptions | undefined;
472
- /** Whether telemetry is enabled. Defaults to `true`. */
473
- readonly isEnabled: boolean | undefined;
474
- /** Whether to record inputs in telemetry. Enabled by default. */
475
- readonly recordInputs: boolean | undefined;
476
- /** Whether to record outputs in telemetry. Enabled by default. */
477
- readonly recordOutputs: boolean | undefined;
478
- /** Identifier from telemetry settings for grouping related operations. */
479
- readonly functionId: string | undefined;
480
- }
491
+ };
481
492
  /**
482
493
  * Event passed to the `onFinish` callback for embed and embedMany operations.
483
494
  *
484
495
  * Called when the operation completes, after the embedding model returns.
485
496
  */
486
- interface EmbedOnFinishEvent {
497
+ type EmbedEndEvent = {
487
498
  /** Unique identifier for this embed call, used to correlate events. */
488
499
  readonly callId: string;
489
500
  /** Identifies the operation type (e.g. 'ai.embed' or 'ai.embedMany'). */
@@ -510,22 +521,14 @@ interface EmbedOnFinishEvent {
510
521
  headers?: Record<string, string>;
511
522
  body?: unknown;
512
523
  } | undefined> | undefined;
513
- /** Whether telemetry is enabled. Defaults to `true`. */
514
- readonly isEnabled: boolean | undefined;
515
- /** Whether to record inputs in telemetry. Enabled by default. */
516
- readonly recordInputs: boolean | undefined;
517
- /** Whether to record outputs in telemetry. Enabled by default. */
518
- readonly recordOutputs: boolean | undefined;
519
- /** Identifier from telemetry settings for grouping related operations. */
520
- readonly functionId: string | undefined;
521
- }
524
+ };
522
525
  /**
523
526
  * Event fired when an individual embedding model call (inner operation doEmbed) begins.
524
527
  *
525
528
  * For `embed`, there is one call. For `embedMany`, there may be multiple
526
529
  * calls when values are chunked.
527
530
  */
528
- interface EmbedStartEvent {
531
+ type EmbeddingModelCallStartEvent = {
529
532
  /** Unique identifier for this embed call, used to correlate events. */
530
533
  readonly callId: string;
531
534
  /** Unique identifier for this individual doEmbed invocation, used to correlate start/finish within parallel chunks. */
@@ -538,21 +541,13 @@ interface EmbedStartEvent {
538
541
  readonly modelId: string;
539
542
  /** The values being embedded in this particular model call. */
540
543
  readonly values: Array<string>;
541
- /** Whether telemetry is enabled. Defaults to `true`. */
542
- readonly isEnabled: boolean | undefined;
543
- /** Whether to record inputs in telemetry. Enabled by default. */
544
- readonly recordInputs: boolean | undefined;
545
- /** Whether to record outputs in telemetry. Enabled by default. */
546
- readonly recordOutputs: boolean | undefined;
547
- /** Identifier from telemetry settings for grouping related operations. */
548
- readonly functionId: string | undefined;
549
- }
544
+ };
550
545
  /**
551
546
  * Event fired when an individual embedding model call (doEmbed) completes.
552
547
  *
553
548
  * Contains the embeddings, usage, and any warnings from the model response.
554
549
  */
555
- interface EmbedFinishEvent {
550
+ type EmbeddingModelCallEndEvent = {
556
551
  /** Unique identifier for this embed call, used to correlate events. */
557
552
  readonly callId: string;
558
553
  /** Unique identifier for this individual doEmbed invocation, used to correlate start/finish within parallel chunks. */
@@ -569,7 +564,7 @@ interface EmbedFinishEvent {
569
564
  readonly embeddings: Array<Embedding>;
570
565
  /** Token usage for this model call. */
571
566
  readonly usage: EmbeddingModelUsage;
572
- }
567
+ };
573
568
 
574
569
  /**
575
570
  * Event passed to the `experimental_onStart` callback of
@@ -579,7 +574,7 @@ interface EmbedFinishEvent {
579
574
  *
580
575
  * @deprecated
581
576
  */
582
- interface ObjectOnStartEvent {
577
+ interface GenerateObjectStartEvent {
583
578
  /** Unique identifier for this generation call, used to correlate events. */
584
579
  readonly callId: string;
585
580
  /** Identifies the operation type (e.g. `'ai.generateObject'` or `'ai.streamObject'`). */
@@ -622,14 +617,6 @@ interface ObjectOnStartEvent {
622
617
  readonly schemaName: string | undefined;
623
618
  /** Optional description of the schema. */
624
619
  readonly schemaDescription: string | undefined;
625
- /** Whether telemetry is enabled. Defaults to `true`. */
626
- readonly isEnabled: boolean | undefined;
627
- /** Whether to record inputs in telemetry. Enabled by default. */
628
- readonly recordInputs: boolean | undefined;
629
- /** Whether to record outputs in telemetry. Enabled by default. */
630
- readonly recordOutputs: boolean | undefined;
631
- /** Identifier from telemetry settings for grouping related operations. */
632
- readonly functionId: string | undefined;
633
620
  }
634
621
  /**
635
622
  * Event passed to the `experimental_onStepStart` callback of
@@ -640,7 +627,7 @@ interface ObjectOnStartEvent {
640
627
  *
641
628
  * @deprecated
642
629
  */
643
- interface ObjectOnStepStartEvent {
630
+ interface GenerateObjectStepStartEvent {
644
631
  /** Unique identifier for this generation call, used to correlate events. */
645
632
  readonly callId: string;
646
633
  /** Zero-based index of the current step. Always `0` for object generation. */
@@ -653,8 +640,6 @@ interface ObjectOnStepStartEvent {
653
640
  readonly providerOptions: ProviderOptions | undefined;
654
641
  /** Additional HTTP headers sent with the request. */
655
642
  readonly headers: Record<string, string | undefined> | undefined;
656
- /** Identifier from telemetry settings for grouping related operations. */
657
- readonly functionId: string | undefined;
658
643
  /** The prompt messages in provider format (for telemetry). */
659
644
  readonly promptMessages?: LanguageModelV4Prompt;
660
645
  }
@@ -667,7 +652,7 @@ interface ObjectOnStepStartEvent {
667
652
  *
668
653
  * @deprecated
669
654
  */
670
- interface ObjectOnStepFinishEvent {
655
+ interface GenerateObjectStepEndEvent {
671
656
  /** Unique identifier for this generation call, used to correlate events. */
672
657
  readonly callId: string;
673
658
  /** Zero-based index of the current step. Always `0` for object generation. */
@@ -694,8 +679,6 @@ interface ObjectOnStepFinishEvent {
694
679
  };
695
680
  /** Additional provider-specific metadata. */
696
681
  readonly providerMetadata: ProviderMetadata | undefined;
697
- /** Identifier from telemetry settings for grouping related operations. */
698
- readonly functionId: string | undefined;
699
682
  /** Milliseconds from the start of the stream to the first chunk (streaming only). */
700
683
  readonly msToFirstChunk: number | undefined;
701
684
  }
@@ -709,7 +692,7 @@ interface ObjectOnStepFinishEvent {
709
692
  *
710
693
  * @deprecated
711
694
  */
712
- interface ObjectOnFinishEvent<RESULT> {
695
+ interface GenerateObjectEndEvent<RESULT> {
713
696
  /** Unique identifier for this generation call, used to correlate events. */
714
697
  readonly callId: string;
715
698
  /**
@@ -739,8 +722,6 @@ interface ObjectOnFinishEvent<RESULT> {
739
722
  };
740
723
  /** Additional provider-specific metadata. */
741
724
  readonly providerMetadata: ProviderMetadata | undefined;
742
- /** Identifier from telemetry settings for grouping related operations. */
743
- readonly functionId: string | undefined;
744
725
  }
745
726
 
746
727
  /**
@@ -760,6 +741,51 @@ type TimeoutConfiguration<TOOLS extends ToolSet> = number | {
760
741
  tools?: Partial<Record<`${keyof TOOLS & string}Ms`, number>>;
761
742
  };
762
743
 
744
+ /**
745
+ * Tool names that are enabled for a generation step.
746
+ *
747
+ * `undefined` means no tool restriction is applied. Tool names are object keys
748
+ * at runtime, so the type is restricted to the string keys of the configured
749
+ * tool set.
750
+ */
751
+ type ActiveTools<TOOLS extends ToolSet> = ReadonlyArray<keyof TOOLS & string> | undefined;
752
+
753
+ /**
754
+ * Telemetry configuration.
755
+ */
756
+ type TelemetryOptions = {
757
+ /**
758
+ * Enable or disable telemetry. Enabled by default when a telemetry
759
+ * integration is registered. Set to `false` to opt out.
760
+ */
761
+ isEnabled?: boolean;
762
+ /**
763
+ * Enable or disable input recording. Enabled by default.
764
+ *
765
+ * You might want to disable input recording to avoid recording sensitive
766
+ * information, to reduce data transfers, or to increase performance.
767
+ */
768
+ recordInputs?: boolean;
769
+ /**
770
+ * Enable or disable output recording. Enabled by default.
771
+ *
772
+ * You might want to disable output recording to avoid recording sensitive
773
+ * information, to reduce data transfers, or to increase performance.
774
+ */
775
+ recordOutputs?: boolean;
776
+ /**
777
+ * Identifier for this function. Used to group telemetry data by function.
778
+ */
779
+ functionId?: string;
780
+ /**
781
+ * Per-call telemetry integrations that receive lifecycle events during generation.
782
+ *
783
+ * When provided, these integrations will take precedence over the globally registered
784
+ * integrations for this call.
785
+ */
786
+ integrations?: Arrayable<Telemetry>;
787
+ };
788
+
763
789
  /**
764
790
  * A generated file.
765
791
  */
@@ -780,6 +806,40 @@ interface GeneratedFile {
780
806
  readonly mediaType: string;
781
807
  }
782
808
 
809
+ /**
810
+ * Reasoning output of a text generation. It contains a reasoning.
811
+ */
812
+ interface ReasoningOutput {
813
+ type: 'reasoning';
814
+ /**
815
+ * The reasoning text.
816
+ */
817
+ text: string;
818
+ /**
819
+ * Additional provider-specific metadata. They are passed through
820
+ * to the provider from the AI SDK and enable provider-specific
821
+ * functionality that can be fully encapsulated in the provider.
822
+ */
823
+ providerMetadata?: ProviderMetadata;
824
+ }
825
+ /**
826
+ * Reasoning file output of a text generation.
827
+ * It contains a file generated as part of reasoning.
828
+ */
829
+ interface ReasoningFileOutput {
830
+ type: 'reasoning-file';
831
+ /**
832
+ * The generated file.
833
+ */
834
+ file: GeneratedFile;
835
+ /**
836
+ * Additional provider-specific metadata. They are passed through
837
+ * to the provider from the AI SDK and enable provider-specific
838
+ * functionality that can be fully encapsulated in the provider.
839
+ */
840
+ providerMetadata?: ProviderMetadata;
841
+ }
842
+
783
843
  /**
784
844
  * Create a union of the given object's values, and optionally specify which keys to get the values from.
785
845
  *
@@ -882,41 +942,41 @@ type ToolApprovalRequestOutput<TOOLS extends ToolSet> = {
882
942
  * Tool call that the approval request is for.
883
943
  */
884
944
  toolCall: TypedToolCall<TOOLS>;
945
+ /**
946
+ * Flag indicating whether the tool was automatically approved or denied.
947
+ *
948
+ * @default false
949
+ */
950
+ isAutomatic?: boolean;
885
951
  };
886
952
 
887
953
  /**
888
- * Reasoning output of a text generation. It contains a reasoning.
954
+ * Output part that indicates that a tool approval response is available.
889
955
  */
890
- interface ReasoningOutput {
891
- type: 'reasoning';
956
+ type ToolApprovalResponseOutput<TOOLS extends ToolSet> = {
957
+ type: 'tool-approval-response';
892
958
  /**
893
- * The reasoning text.
959
+ * ID of the tool approval.
894
960
  */
895
- text: string;
961
+ approvalId: string;
896
962
  /**
897
- * Additional provider-specific metadata. They are passed through
898
- * to the provider from the AI SDK and enable provider-specific
899
- * functionality that can be fully encapsulated in the provider.
963
+ * Tool call that the approval response is for.
900
964
  */
901
- providerMetadata?: ProviderMetadata;
902
- }
903
- /**
904
- * Reasoning file output of a text generation.
905
- * It contains a file generated as part of reasoning.
906
- */
907
- interface ReasoningFileOutput {
908
- type: 'reasoning-file';
965
+ toolCall: TypedToolCall<TOOLS>;
909
966
  /**
910
- * The generated file.
967
+ * Flag indicating whether the approval was granted or denied.
911
968
  */
912
- file: GeneratedFile;
969
+ approved: boolean;
913
970
  /**
914
- * Additional provider-specific metadata. They are passed through
915
- * to the provider from the AI SDK and enable provider-specific
916
- * functionality that can be fully encapsulated in the provider.
971
+ * Optional reason for the approval or denial.
917
972
  */
918
- providerMetadata?: ProviderMetadata;
919
- }
973
+ reason?: string;
974
+ /**
975
+ * Flag indicating whether the tool call is provider-executed.
976
+ * Only provider-executed tool approval responses should be sent to the model.
977
+ */
978
+ providerExecuted?: boolean;
979
+ };
920
980
 
921
981
  type StaticToolError<TOOLS extends ToolSet> = ValueOf<{
922
982
  [NAME in keyof TOOLS]: {
@@ -998,7 +1058,47 @@ type ContentPart<TOOLS extends ToolSet> = {
998
1058
  type: 'tool-error';
999
1059
  } & TypedToolError<TOOLS> & {
1000
1060
  providerMetadata?: ProviderMetadata;
1001
- }) | ToolApprovalRequestOutput<TOOLS>;
1061
+ }) | ToolApprovalRequestOutput<TOOLS> | ToolApprovalResponseOutput<TOOLS>;
1062
+
1063
+ /**
1064
+ * Common model information used across callback events.
1065
+ */
1066
+ type ModelInfo = {
1067
+ /** The provider identifier (e.g., 'openai', 'anthropic'). */
1068
+ readonly provider: string;
1069
+ /** The specific model identifier (e.g., 'gpt-4o'). */
1070
+ readonly modelId: string;
1071
+ };
1072
+ /**
1073
+ * Event passed to the `onLanguageModelCallStart` callback.
1074
+ *
1075
+ * Called immediately before the provider model call begins.
1076
+ * Unlike `onStepStart`, this only represents model invocation work.
1077
+ */
1078
+ type LanguageModelCallStartEvent = ModelInfo & {
1079
+ /** Unique identifier for this generation call, used to correlate events. */
1080
+ readonly callId: string;
1081
+ /** Prepared tool definitions for the model call, if any. */
1082
+ readonly tools: ReadonlyArray<Record<string, unknown>> | undefined;
1083
+ } & StandardizedPrompt & LanguageModelCallOptions;
1084
+ /**
1085
+ * Event passed to the `onLanguageModelCallEnd` callback.
1086
+ *
1087
+ * Called after the model response has been normalized and parsed, but before
1088
+ * any client-side tool execution begins.
1089
+ */
1090
+ type LanguageModelCallEndEvent<TOOLS extends ToolSet = ToolSet> = ModelInfo & {
1091
+ /** Unique identifier for this generation call, used to correlate events. */
1092
+ readonly callId: string;
1093
+ /** The unified reason why the model call finished. */
1094
+ readonly finishReason: FinishReason;
1095
+ /** The token usage reported by the model call. */
1096
+ readonly usage: LanguageModelUsage;
1097
+ /** The content parts produced by the model call. */
1098
+ readonly content: ReadonlyArray<ContentPart<TOOLS>>;
1099
+ /** The provider-returned response id for this model call. */
1100
+ readonly responseId: string;
1101
+ };
1002
1102
 
1003
1103
  /**
1004
1104
  * A message that was generated during the generation process.
@@ -1027,10 +1127,6 @@ type StepResult<TOOLS extends ToolSet, RUNTIME_CONTEXT extends Context = Context
1027
1127
  /** The ID of the model. */
1028
1128
  readonly modelId: string;
1029
1129
  };
1030
- /**
1031
- * Identifier from telemetry settings for grouping related operations.
1032
- */
1033
- readonly functionId: string | undefined;
1034
1130
  /**
1035
1131
  * Tool context.
1036
1132
  */
@@ -1130,20 +1226,6 @@ type StepResult<TOOLS extends ToolSet, RUNTIME_CONTEXT extends Context = Context
1130
1226
  readonly providerMetadata: ProviderMetadata | undefined;
1131
1227
  };
1132
1228
 
1133
- /**
1134
- * A predicate that decides whether a tool-calling loop should stop after the
1135
- * current step.
1136
- *
1137
- * A tool calling loop continues until one of the following conditions is met:
1138
- * - The model returns a finish reason other than `tool-calls`
1139
- * - A tool without an execute function is called
1140
- * - A tool call needs approval
1141
- * - One of the provided stop conditions returns `true`
1142
- */
1143
- type StopCondition<TOOLS extends ToolSet, RUNTIME_CONTEXT extends Context = Context> = (options: {
1144
- steps: Array<StepResult<TOOLS, RUNTIME_CONTEXT>>;
1145
- }) => PromiseLike<boolean> | boolean;
1146
-
1147
1229
  /**
1148
1230
  * Tool output when the tool execution has been denied (for static tools).
1149
1231
  */
@@ -1240,6 +1322,7 @@ type TextStreamToolOutputDeniedPart<TOOLS extends ToolSet> = {
1240
1322
  type: 'tool-output-denied';
1241
1323
  } & StaticToolOutputDenied<TOOLS>;
1242
1324
  type TextStreamToolApprovalRequestPart<TOOLS extends ToolSet> = ToolApprovalRequestOutput<TOOLS>;
1325
+ type TextStreamToolApprovalResponsePart<TOOLS extends ToolSet> = ToolApprovalResponseOutput<TOOLS>;
1243
1326
  type TextStreamStartStepPart = {
1244
1327
  type: 'start-step';
1245
1328
  request: LanguageModelRequestMetadata;
@@ -1274,69 +1357,119 @@ type TextStreamRawPart = {
1274
1357
  type: 'raw';
1275
1358
  rawValue: unknown;
1276
1359
  };
1277
- type TextStreamPart<TOOLS extends ToolSet> = TextStreamTextStartPart | TextStreamTextEndPart | TextStreamTextDeltaPart | TextStreamReasoningStartPart | TextStreamReasoningEndPart | TextStreamReasoningDeltaPart | TextStreamCustomPart | TextStreamToolInputStartPart | TextStreamToolInputEndPart | TextStreamToolInputDeltaPart | TextStreamSourcePart | TextStreamFilePart | TextStreamReasoningFilePart | TextStreamToolCallPart<TOOLS> | TextStreamToolResultPart<TOOLS> | TextStreamToolErrorPart<TOOLS> | TextStreamToolOutputDeniedPart<TOOLS> | TextStreamToolApprovalRequestPart<TOOLS> | TextStreamStartStepPart | TextStreamFinishStepPart | TextStreamStartPart | TextStreamFinishPart | TextStreamAbortPart | TextStreamErrorPart | TextStreamRawPart;
1360
+ type TextStreamPart<TOOLS extends ToolSet> = TextStreamTextStartPart | TextStreamTextEndPart | TextStreamTextDeltaPart | TextStreamReasoningStartPart | TextStreamReasoningEndPart | TextStreamReasoningDeltaPart | TextStreamCustomPart | TextStreamToolInputStartPart | TextStreamToolInputEndPart | TextStreamToolInputDeltaPart | TextStreamSourcePart | TextStreamFilePart | TextStreamReasoningFilePart | TextStreamToolCallPart<TOOLS> | TextStreamToolResultPart<TOOLS> | TextStreamToolErrorPart<TOOLS> | TextStreamToolOutputDeniedPart<TOOLS> | TextStreamToolApprovalRequestPart<TOOLS> | TextStreamToolApprovalResponsePart<TOOLS> | TextStreamStartStepPart | TextStreamFinishStepPart | TextStreamStartPart | TextStreamFinishPart | TextStreamAbortPart | TextStreamErrorPart | TextStreamRawPart;
1278
1361
 
1362
+ type ToolOutput<TOOLS extends ToolSet> = TypedToolResult<TOOLS> | TypedToolError<TOOLS>;
1363
+
1364
+ /**
1365
+ * Resolves a single tool's context type, falling back to `undefined` when the
1366
+ * tool does not declare a `contextSchema`.
1367
+ */
1368
+ type ToolContextFor<TOOL extends ToolSet[keyof ToolSet]> = [
1369
+ InferToolContext<TOOL>
1370
+ ] extends [never] ? undefined : InferToolContext<TOOL>;
1371
+ type BaseToolExecutionStartFields = {
1372
+ /** Unique identifier for this generation call, used to correlate events. */
1373
+ readonly callId: string;
1374
+ /**
1375
+ * Messages that were sent to the language model to initiate the response that contained the tool call.
1376
+ * The messages **do not** include the system prompt nor the assistant response that contained the tool call.
1377
+ */
1378
+ readonly messages: ModelMessage[];
1379
+ };
1380
+ /**
1381
+ * Precise start event union for statically known tools.
1382
+ *
1383
+ * Each union member ties a specific `toolCall.toolName` to that tool's
1384
+ * validated `toolContext` type.
1385
+ */
1386
+ type StaticToolExecutionStartEvent<TOOLS extends ToolSet> = ValueOf<{
1387
+ [NAME in keyof TOOLS]: BaseToolExecutionStartFields & {
1388
+ readonly toolCall: Extract<StaticToolCall<TOOLS>, {
1389
+ toolName: NAME;
1390
+ }>;
1391
+ readonly toolContext: ToolContextFor<TOOLS[NAME]>;
1392
+ };
1393
+ }>;
1394
+ /**
1395
+ * Start event shape for dynamic or untyped tool calls.
1396
+ */
1397
+ type DynamicToolExecutionStartEvent = BaseToolExecutionStartFields & {
1398
+ readonly toolCall: DynamicToolCall;
1399
+ readonly toolContext: unknown;
1400
+ };
1401
+ /**
1402
+ * Broad start event shape used for the default `ToolSet` specialization.
1403
+ *
1404
+ * This keeps generic collectors ergonomic when the caller is not working with
1405
+ * a concrete tool set and therefore cannot benefit from per-tool narrowing.
1406
+ */
1407
+ type WidenedToolExecutionStartEvent = BaseToolExecutionStartFields & {
1408
+ readonly toolCall: StaticToolCall<ToolSet> | DynamicToolCall;
1409
+ readonly toolContext: unknown;
1410
+ };
1279
1411
  /**
1280
1412
  * Event passed to the `onToolExecutionStart` callback.
1281
1413
  *
1282
1414
  * Called when a tool execution begins, before the tool's `execute` function is invoked.
1283
1415
  */
1284
- interface ToolExecutionStartEvent<TOOLS extends ToolSet = ToolSet> {
1416
+ type ToolExecutionStartEvent<TOOLS extends ToolSet = ToolSet> = [
1417
+ ToolSet
1418
+ ] extends [TOOLS] ? WidenedToolExecutionStartEvent : StaticToolExecutionStartEvent<TOOLS> | DynamicToolExecutionStartEvent;
1419
+ type BaseToolExecutionEndFields = {
1285
1420
  /** Unique identifier for this generation call, used to correlate events. */
1286
1421
  readonly callId: string;
1287
- /** Zero-based index of the current step where this tool call occurs. */
1288
- readonly stepNumber: number | undefined;
1289
- /** The provider identifier (e.g., 'openai', 'anthropic'). */
1290
- readonly provider: string | undefined;
1291
- /** The specific model identifier (e.g., 'gpt-4o'). */
1292
- readonly modelId: string | undefined;
1293
- /** The full tool call object. */
1294
- readonly toolCall: TypedToolCall<TOOLS>;
1295
- /** The conversation messages available at tool execution time. */
1296
- readonly messages: Array<ModelMessage>;
1297
- /** Identifier from telemetry settings for grouping related operations. */
1298
- readonly functionId: string | undefined;
1299
- /** User-defined context object flowing through the generation. */
1300
- readonly context: InferToolSetContext<TOOLS>;
1301
- }
1422
+ /** Execution time of the tool call in milliseconds. */
1423
+ readonly durationMs: number;
1424
+ /**
1425
+ * Messages that were sent to the language model to initiate the response that contained the tool call.
1426
+ * The messages **do not** include the system prompt nor the assistant response that contained the tool call.
1427
+ */
1428
+ readonly messages: ModelMessage[];
1429
+ };
1430
+ /**
1431
+ * Precise end event union for statically known tools.
1432
+ *
1433
+ * Each union member preserves the link between `toolCall.toolName`, the
1434
+ * corresponding validated `toolContext`, and the tool execution result.
1435
+ */
1436
+ type StaticToolExecutionEndEvent<TOOLS extends ToolSet> = ValueOf<{
1437
+ [NAME in keyof TOOLS]: BaseToolExecutionEndFields & {
1438
+ readonly toolCall: Extract<StaticToolCall<TOOLS>, {
1439
+ toolName: NAME;
1440
+ }>;
1441
+ readonly toolContext: ToolContextFor<TOOLS[NAME]>;
1442
+ readonly toolOutput: ToolOutput<TOOLS>;
1443
+ };
1444
+ }>;
1445
+ /**
1446
+ * End event shape for dynamic or untyped tool calls.
1447
+ */
1448
+ type DynamicToolExecutionEndEvent<TOOLS extends ToolSet> = BaseToolExecutionEndFields & {
1449
+ readonly toolCall: DynamicToolCall;
1450
+ readonly toolContext: unknown;
1451
+ readonly toolOutput: ToolOutput<TOOLS>;
1452
+ };
1453
+ /**
1454
+ * Broad end event shape used for the default `ToolSet` specialization.
1455
+ *
1456
+ * This provides an assignable catch-all event type for generic consumers while
1457
+ * the concrete-tool specialization retains full per-tool narrowing.
1458
+ */
1459
+ type WidenedToolExecutionEndEvent = BaseToolExecutionEndFields & {
1460
+ readonly toolCall: StaticToolCall<ToolSet> | DynamicToolCall;
1461
+ readonly toolContext: unknown;
1462
+ readonly toolOutput: ToolOutput<ToolSet>;
1463
+ };
1302
1464
  /**
1303
1465
  * Event passed to the `onToolExecutionEnd` callback.
1304
1466
  *
1305
1467
  * Called when a tool execution completes, either successfully or with an error.
1306
- * Uses a discriminated union on the `success` field.
1468
+ * Uses the `toolOutput.type` discriminator to distinguish success and error.
1307
1469
  */
1308
- type ToolExecutionEndEvent<TOOLS extends ToolSet = ToolSet> = {
1309
- /** Unique identifier for this generation call, used to correlate events. */
1310
- readonly callId: string;
1311
- /** Zero-based index of the current step where this tool call occurred. */
1312
- readonly stepNumber: number | undefined;
1313
- /** The provider identifier (e.g., 'openai', 'anthropic'). */
1314
- readonly provider: string | undefined;
1315
- /** The specific model identifier (e.g., 'gpt-4o'). */
1316
- readonly modelId: string | undefined;
1317
- /** The full tool call object. */
1318
- readonly toolCall: TypedToolCall<TOOLS>;
1319
- /** The conversation messages available at tool execution time. */
1320
- readonly messages: Array<ModelMessage>;
1321
- /** Execution time of the tool call in milliseconds. */
1322
- readonly durationMs: number;
1323
- /** Identifier from telemetry settings for grouping related operations. */
1324
- readonly functionId: string | undefined;
1325
- /** User-defined context object flowing through the generation. */
1326
- readonly context: InferToolSetContext<TOOLS>;
1327
- } & ({
1328
- /** Indicates the tool call succeeded. */
1329
- readonly success: true;
1330
- /** The tool's return value. */
1331
- readonly output: unknown;
1332
- readonly error?: never;
1333
- } | {
1334
- /** Indicates the tool call failed. */
1335
- readonly success: false;
1336
- readonly output?: never;
1337
- /** The error that occurred during tool execution. */
1338
- readonly error: unknown;
1339
- });
1470
+ type ToolExecutionEndEvent<TOOLS extends ToolSet = ToolSet> = [
1471
+ ToolSet
1472
+ ] extends [TOOLS] ? WidenedToolExecutionEndEvent : StaticToolExecutionEndEvent<TOOLS> | DynamicToolExecutionEndEvent<TOOLS>;
1340
1473
 
1341
1474
  type EnrichedStreamPart<TOOLS extends ToolSet, PARTIAL_OUTPUT> = {
1342
1475
  part: TextStreamPart<TOOLS>;
@@ -1381,7 +1514,7 @@ interface Output<OUTPUT = any, PARTIAL = any, ELEMENT = any> {
1381
1514
  *
1382
1515
  * Called when the generation operation begins, before any LLM calls.
1383
1516
  */
1384
- interface OnStartEvent<TOOLS extends ToolSet = ToolSet, RUNTIME_CONTEXT extends Context = Context, OUTPUT extends Output = Output> {
1517
+ type GenerateTextStartEvent<TOOLS extends ToolSet = ToolSet, RUNTIME_CONTEXT extends Context = Context, OUTPUT extends Output = Output> = {
1385
1518
  /** Unique identifier for this generation call, used to correlate events. */
1386
1519
  readonly callId: string;
1387
1520
  /** Identifies the operation type (e.g. 'ai.generateText' or 'ai.streamText'). */
@@ -1390,34 +1523,12 @@ interface OnStartEvent<TOOLS extends ToolSet = ToolSet, RUNTIME_CONTEXT extends
1390
1523
  readonly provider: string;
1391
1524
  /** The specific model identifier (e.g., 'gpt-4o'). */
1392
1525
  readonly modelId: string;
1393
- /** The system message(s) provided to the model. */
1394
- readonly system: string | SystemModelMessage | Array<SystemModelMessage> | undefined;
1395
- /** The prompt string or array of messages if using the prompt option. */
1396
- readonly prompt: string | Array<ModelMessage> | undefined;
1397
- /** The messages array if using the messages option. */
1398
- readonly messages: Array<ModelMessage> | undefined;
1399
1526
  /** The tools available for this generation. */
1400
1527
  readonly tools: TOOLS | undefined;
1401
1528
  /** The tool choice strategy for this generation. */
1402
1529
  readonly toolChoice: ToolChoice<NoInfer<TOOLS>> | undefined;
1403
1530
  /** Limits which tools are available for the model to call. */
1404
- readonly activeTools: Array<keyof TOOLS> | undefined;
1405
- /** Maximum number of tokens to generate. */
1406
- readonly maxOutputTokens: number | undefined;
1407
- /** Sampling temperature for generation. */
1408
- readonly temperature: number | undefined;
1409
- /** Top-p (nucleus) sampling parameter. */
1410
- readonly topP: number | undefined;
1411
- /** Top-k sampling parameter. */
1412
- readonly topK: number | undefined;
1413
- /** Presence penalty for generation. */
1414
- readonly presencePenalty: number | undefined;
1415
- /** Frequency penalty for generation. */
1416
- readonly frequencyPenalty: number | undefined;
1417
- /** Sequences that will stop generation. */
1418
- readonly stopSequences: string[] | undefined;
1419
- /** Random seed for reproducible generation. */
1420
- readonly seed: number | undefined;
1531
+ readonly activeTools: ActiveTools<TOOLS>;
1421
1532
  /** Maximum number of retries for failed requests. */
1422
1533
  readonly maxRetries: number;
1423
1534
  /**
@@ -1429,21 +1540,8 @@ interface OnStartEvent<TOOLS extends ToolSet = ToolSet, RUNTIME_CONTEXT extends
1429
1540
  readonly headers: Record<string, string | undefined> | undefined;
1430
1541
  /** Additional provider-specific options. */
1431
1542
  readonly providerOptions: ProviderOptions | undefined;
1432
- /**
1433
- * Condition(s) for stopping the generation.
1434
- * When the condition is an array, any of the conditions can be met to stop.
1435
- */
1436
- readonly stopWhen: Arrayable<StopCondition<NoInfer<TOOLS>, RUNTIME_CONTEXT>>;
1437
1543
  /** The output specification for structured outputs, if configured. */
1438
1544
  readonly output: OUTPUT | undefined;
1439
- /** Whether telemetry is enabled. Defaults to `true`. */
1440
- readonly isEnabled: boolean | undefined;
1441
- /** Whether to record inputs in telemetry. Enabled by default. */
1442
- readonly recordInputs: boolean | undefined;
1443
- /** Whether to record outputs in telemetry. Enabled by default. */
1444
- readonly recordOutputs: boolean | undefined;
1445
- /** Identifier from telemetry settings for grouping related operations. */
1446
- readonly functionId: string | undefined;
1447
1545
  /**
1448
1546
  * Tool context.
1449
1547
  */
@@ -1452,58 +1550,34 @@ interface OnStartEvent<TOOLS extends ToolSet = ToolSet, RUNTIME_CONTEXT extends
1452
1550
  * User-defined runtime context.
1453
1551
  */
1454
1552
  readonly runtimeContext: RUNTIME_CONTEXT;
1455
- }
1553
+ } & LanguageModelCallOptions & StandardizedPrompt;
1456
1554
  /**
1457
1555
  * Event passed to the `onStepStart` callback.
1458
1556
  *
1459
1557
  * Called when a step (LLM call) begins, before the provider is called.
1460
1558
  * Each step represents a single LLM invocation.
1461
1559
  */
1462
- interface OnStepStartEvent<TOOLS extends ToolSet = ToolSet, RUNTIME_CONTEXT extends Context = Context, OUTPUT extends Output = Output> {
1560
+ type GenerateTextStepStartEvent<TOOLS extends ToolSet = ToolSet, RUNTIME_CONTEXT extends Context = Context, OUTPUT extends Output = Output> = {
1463
1561
  /** Unique identifier for this generation call, used to correlate events. */
1464
1562
  readonly callId: string;
1465
- /** Zero-based index of the current step. */
1466
- readonly stepNumber: number;
1467
1563
  /** The provider identifier (e.g., 'openai', 'anthropic'). */
1468
1564
  readonly provider: string;
1469
1565
  /** The specific model identifier (e.g., 'gpt-4o'). */
1470
1566
  readonly modelId: string;
1471
- /**
1472
- * The system message for this step.
1473
- */
1474
- readonly system: string | SystemModelMessage | Array<SystemModelMessage> | undefined;
1475
- /**
1476
- * The messages that will be sent to the model for this step.
1477
- * Uses the user-facing `ModelMessage` format.
1478
- * May be overridden by prepareStep.
1479
- */
1480
- readonly messages: Array<ModelMessage>;
1567
+ /** Zero-based index of the current step. */
1568
+ readonly stepNumber: number;
1481
1569
  /** The tools available for this generation. */
1482
1570
  readonly tools: TOOLS | undefined;
1483
1571
  /** The tool choice configuration for this step. */
1484
1572
  readonly toolChoice: ToolChoice<NoInfer<TOOLS>> | undefined;
1485
1573
  /** Limits which tools are available for this step. */
1486
- readonly activeTools: Array<keyof TOOLS> | undefined;
1574
+ readonly activeTools: ActiveTools<TOOLS>;
1487
1575
  /** Array of results from previous steps (empty for first step). */
1488
1576
  readonly steps: ReadonlyArray<StepResult<TOOLS, RUNTIME_CONTEXT>>;
1489
1577
  /** Additional provider-specific options for this step. */
1490
1578
  readonly providerOptions: ProviderOptions | undefined;
1491
- /**
1492
- * Timeout configuration for the generation.
1493
- * Can be a number (milliseconds) or an object with totalMs, stepMs, chunkMs, toolMs, and per-tool overrides via tools.
1494
- */
1495
- readonly timeout: TimeoutConfiguration<TOOLS> | undefined;
1496
- /** Additional HTTP headers sent with the request. */
1497
- readonly headers: Record<string, string | undefined> | undefined;
1498
- /**
1499
- * Condition(s) for stopping the generation.
1500
- * When the condition is an array, any of the conditions can be met to stop.
1501
- */
1502
- readonly stopWhen: Arrayable<StopCondition<TOOLS, RUNTIME_CONTEXT>>;
1503
1579
  /** The output specification for structured outputs, if configured. */
1504
1580
  readonly output: OUTPUT | undefined;
1505
- /** Identifier from telemetry settings for grouping related operations. */
1506
- readonly functionId: string | undefined;
1507
1581
  /**
1508
1582
  * Runtime context. May be updated from `prepareStep` between steps.
1509
1583
  */
@@ -1512,7 +1586,7 @@ interface OnStepStartEvent<TOOLS extends ToolSet = ToolSet, RUNTIME_CONTEXT exte
1512
1586
  * Tool context. May be updated from `prepareStep` between steps.
1513
1587
  */
1514
1588
  readonly toolsContext: InferToolSetContext<TOOLS>;
1515
- }
1589
+ } & StandardizedPrompt;
1516
1590
  /**
1517
1591
  * Event passed to the `onChunk` callback.
1518
1592
  *
@@ -1520,34 +1594,32 @@ interface OnStepStartEvent<TOOLS extends ToolSet = ToolSet, RUNTIME_CONTEXT exte
1520
1594
  * The chunk is either a content part (text-delta, tool-call, etc.) or
1521
1595
  * a stream lifecycle marker (`ai.stream.firstChunk` / `ai.stream.finish`).
1522
1596
  */
1523
- interface OnChunkEvent<TOOLS extends ToolSet = ToolSet> {
1597
+ type StreamTextChunkEvent<TOOLS extends ToolSet = ToolSet> = {
1524
1598
  readonly chunk: TextStreamPart<TOOLS> | {
1525
1599
  readonly type: 'ai.stream.firstChunk' | 'ai.stream.finish';
1526
1600
  readonly callId: string;
1527
1601
  readonly stepNumber: number;
1528
1602
  readonly attributes?: Record<string, unknown>;
1529
1603
  };
1530
- }
1604
+ };
1531
1605
  /**
1532
1606
  * Event passed to the `onStepFinish` callback.
1533
1607
  *
1534
1608
  * Called when a step (LLM call) completes.
1535
1609
  * Includes the StepResult for that step along with the call identifier.
1536
1610
  */
1537
- type OnStepFinishEvent<TOOLS extends ToolSet = ToolSet, RUNTIME_CONTEXT extends Context = Context> = StepResult<TOOLS, RUNTIME_CONTEXT>;
1611
+ type GenerateTextStepEndEvent<TOOLS extends ToolSet = ToolSet, RUNTIME_CONTEXT extends Context = Context> = StepResult<TOOLS, RUNTIME_CONTEXT>;
1538
1612
  /**
1539
1613
  * Event passed to the `onFinish` callback.
1540
1614
  *
1541
1615
  * Called when the entire generation completes (all steps finished).
1542
1616
  * Includes the final step's result along with aggregated data from all steps.
1543
1617
  */
1544
- type OnFinishEvent<TOOLS extends ToolSet = ToolSet, RUNTIME_CONTEXT extends Context = Context> = StepResult<TOOLS, RUNTIME_CONTEXT> & {
1618
+ type GenerateTextEndEvent<TOOLS extends ToolSet = ToolSet, RUNTIME_CONTEXT extends Context = Context> = StepResult<TOOLS, RUNTIME_CONTEXT> & {
1545
1619
  /** Array containing results from all steps in the generation. */
1546
1620
  readonly steps: StepResult<TOOLS, RUNTIME_CONTEXT>[];
1547
1621
  /** Aggregated token usage across all steps. */
1548
1622
  readonly totalUsage: LanguageModelUsage;
1549
- /** Identifier from telemetry settings for grouping related operations. */
1550
- readonly functionId: string | undefined;
1551
1623
  };
1552
1624
 
1553
1625
  /**
@@ -1555,7 +1627,7 @@ type OnFinishEvent<TOOLS extends ToolSet = ToolSet, RUNTIME_CONTEXT extends Cont
1555
1627
  *
1556
1628
  * Called when the operation begins, before the reranking model is called.
1557
1629
  */
1558
- interface RerankOnStartEvent {
1630
+ type RerankStartEvent = {
1559
1631
  /** Unique identifier for this rerank call, used to correlate events. */
1560
1632
  readonly callId: string;
1561
1633
  /** Identifies the operation type ('ai.rerank'). */
@@ -1575,21 +1647,13 @@ interface RerankOnStartEvent {
1575
1647
  readonly headers: Record<string, string | undefined> | undefined;
1576
1648
  /** Additional provider-specific options. */
1577
1649
  readonly providerOptions: ProviderOptions | undefined;
1578
- /** Whether telemetry is enabled. Defaults to `true`. */
1579
- readonly isEnabled: boolean | undefined;
1580
- /** Whether to record inputs in telemetry. Enabled by default. */
1581
- readonly recordInputs: boolean | undefined;
1582
- /** Whether to record outputs in telemetry. Enabled by default. */
1583
- readonly recordOutputs: boolean | undefined;
1584
- /** Identifier from telemetry settings for grouping related operations. */
1585
- readonly functionId: string | undefined;
1586
- }
1650
+ };
1587
1651
  /**
1588
1652
  * Event passed to the `onFinish` callback for rerank operations.
1589
1653
  *
1590
1654
  * Called when the operation completes, after the reranking model returns.
1591
1655
  */
1592
- interface RerankOnFinishEvent {
1656
+ type RerankEndEvent = {
1593
1657
  /** Unique identifier for this rerank call, used to correlate events. */
1594
1658
  readonly callId: string;
1595
1659
  /** Identifies the operation type ('ai.rerank'). */
@@ -1619,19 +1683,11 @@ interface RerankOnFinishEvent {
1619
1683
  headers?: Record<string, string>;
1620
1684
  body?: unknown;
1621
1685
  };
1622
- /** Whether telemetry is enabled. Defaults to `true`. */
1623
- readonly isEnabled: boolean | undefined;
1624
- /** Whether to record inputs in telemetry. Enabled by default. */
1625
- readonly recordInputs: boolean | undefined;
1626
- /** Whether to record outputs in telemetry. Enabled by default. */
1627
- readonly recordOutputs: boolean | undefined;
1628
- /** Identifier from telemetry settings for grouping related operations. */
1629
- readonly functionId: string | undefined;
1630
- }
1686
+ };
1631
1687
  /**
1632
1688
  * Event fired when an individual reranking model call (inner doRerank) begins.
1633
1689
  */
1634
- interface RerankStartEvent {
1690
+ type RerankingModelCallStartEvent = {
1635
1691
  /** Unique identifier for this rerank call, used to correlate events. */
1636
1692
  readonly callId: string;
1637
1693
  /** Identifies the inner operation ('ai.rerank.doRerank'). */
@@ -1648,21 +1704,13 @@ interface RerankStartEvent {
1648
1704
  readonly query: string;
1649
1705
  /** Number of top documents to return. */
1650
1706
  readonly topN: number | undefined;
1651
- /** Whether telemetry is enabled. Defaults to `true`. */
1652
- readonly isEnabled: boolean | undefined;
1653
- /** Whether to record inputs in telemetry. Enabled by default. */
1654
- readonly recordInputs: boolean | undefined;
1655
- /** Whether to record outputs in telemetry. Enabled by default. */
1656
- readonly recordOutputs: boolean | undefined;
1657
- /** Identifier from telemetry settings for grouping related operations. */
1658
- readonly functionId: string | undefined;
1659
- }
1707
+ };
1660
1708
  /**
1661
1709
  * Event fired when an individual reranking model call (doRerank) completes.
1662
1710
  *
1663
1711
  * Contains the ranking results from the model response.
1664
1712
  */
1665
- interface RerankFinishEvent {
1713
+ type RerankingModelCallEndEvent = {
1666
1714
  /** Unique identifier for this rerank call, used to correlate events. */
1667
1715
  readonly callId: string;
1668
1716
  /** Identifies the inner operation ('ai.rerank.doRerank'). */
@@ -1678,8 +1726,11 @@ interface RerankFinishEvent {
1678
1726
  index: number;
1679
1727
  relevanceScore: number;
1680
1728
  }>;
1681
- }
1729
+ };
1682
1730
 
1731
+ type InferTelemetryEvent<EVENT> = EVENT & Omit<TelemetryOptions, 'integrations' | 'isEnabled'>;
1732
+ type OperationStartEvent = GenerateTextStartEvent | GenerateObjectStartEvent | EmbedStartEvent | RerankStartEvent;
1733
+ type OperationFinishEvent = GenerateTextEndEvent<ToolSet> | GenerateObjectEndEvent<unknown> | EmbedEndEvent | RerankEndEvent;
1683
1734
  /**
1684
1735
  * Implement this interface to create custom telemetry integrations.
1685
1736
  * Methods can be sync or return a PromiseLike.
@@ -1692,7 +1743,7 @@ interface Telemetry {
1692
1743
  *
1693
1744
  * Use the `operationId` field to distinguish between operation types.
1694
1745
  */
1695
- onStart?: Callback<OnStartEvent | ObjectOnStartEvent | EmbedOnStartEvent | RerankOnStartEvent>;
1746
+ onStart?: Callback<InferTelemetryEvent<OperationStartEvent>>;
1696
1747
  /**
1697
1748
  * Called when an individual step (single LLM invocation) begins.
1698
1749
  * A generation may consist of multiple steps (e.g. when tool calls trigger
@@ -1702,12 +1753,23 @@ interface Telemetry {
1702
1753
  * The event includes the step number, accumulated previous step results,
1703
1754
  * and the messages that will be sent to the model.
1704
1755
  */
1705
- onStepStart?: Callback<OnStepStartEvent>;
1756
+ onStepStart?: Callback<InferTelemetryEvent<GenerateTextStepStartEvent>>;
1757
+ /**
1758
+ * Called immediately before the provider model call begins.
1759
+ * Unlike `onStepStart`, this callback is scoped to model work only and
1760
+ * excludes any later client-side tool execution.
1761
+ */
1762
+ onLanguageModelCallStart?: Callback<InferTelemetryEvent<LanguageModelCallStartEvent>>;
1763
+ /**
1764
+ * Called after the model response has been normalized and parsed, but before
1765
+ * any client-side tool execution begins.
1766
+ */
1767
+ onLanguageModelCallEnd?: Callback<InferTelemetryEvent<LanguageModelCallEndEvent>>;
1706
1768
  /**
1707
1769
  * Called when a tool execution begins, before the tool's `execute` function
1708
1770
  * is invoked. Use this to create tool-level spans or log tool invocations.
1709
1771
  */
1710
- onToolExecutionStart?: Callback<ToolExecutionStartEvent>;
1772
+ onToolExecutionStart?: Callback<InferTelemetryEvent<ToolExecutionStartEvent>>;
1711
1773
  /**
1712
1774
  * Called when a tool execution completes, either successfully or with an error.
1713
1775
  * The event uses a discriminated union on the `success` field — check
@@ -1715,54 +1777,54 @@ interface Telemetry {
1715
1777
  *
1716
1778
  * The event includes execution duration (`durationMs`) for performance tracking.
1717
1779
  */
1718
- onToolExecutionEnd?: Callback<ToolExecutionEndEvent>;
1780
+ onToolExecutionEnd?: Callback<InferTelemetryEvent<ToolExecutionEndEvent>>;
1719
1781
  /**
1720
1782
  * Called for each chunk received during streaming.
1721
1783
  * Only relevant for `streamText` — not called during `generateText`.
1722
1784
  */
1723
- onChunk?: Callback<OnChunkEvent>;
1785
+ onChunk?: Callback<StreamTextChunkEvent>;
1724
1786
  /**
1725
1787
  * Called when an individual step (single LLM invocation) completes.
1726
1788
  * The event is a `StepResult` containing the model's response, tool calls
1727
1789
  * and results, usage statistics, finish reason, and optional request/response
1728
1790
  * bodies.
1729
1791
  */
1730
- onStepFinish?: Callback<OnStepFinishEvent>;
1792
+ onStepFinish?: Callback<InferTelemetryEvent<GenerateTextStepEndEvent>>;
1731
1793
  /**
1732
1794
  * Called when an object generation step (single LLM invocation) begins.
1733
1795
  * For generateObject/streamObject there is always exactly one step.
1734
1796
  *
1735
1797
  * @deprecated
1736
1798
  */
1737
- onObjectStepStart?: Callback<ObjectOnStepStartEvent>;
1799
+ onObjectStepStart?: Callback<InferTelemetryEvent<GenerateObjectStepStartEvent>>;
1738
1800
  /**
1739
1801
  * Called when an object generation step (single LLM invocation) completes,
1740
1802
  * with the raw result before JSON parsing and schema validation.
1741
1803
  *
1742
1804
  * @deprecated
1743
1805
  */
1744
- onObjectStepFinish?: Callback<ObjectOnStepFinishEvent>;
1806
+ onObjectStepFinish?: Callback<InferTelemetryEvent<GenerateObjectStepEndEvent>>;
1745
1807
  /**
1746
1808
  * Called when an individual embedding model call (doEmbed) begins.
1747
1809
  * For `embed`, there is one call. For `embedMany`, there may be multiple
1748
1810
  * calls when values are chunked.
1749
1811
  */
1750
- onEmbedStart?: Callback<EmbedStartEvent>;
1812
+ onEmbedStart?: Callback<InferTelemetryEvent<EmbeddingModelCallStartEvent>>;
1751
1813
  /**
1752
1814
  * Called when an individual embedding model call (doEmbed) completes.
1753
1815
  * Contains the embeddings, usage, and any warnings from the model response.
1754
1816
  */
1755
- onEmbedFinish?: Callback<EmbedFinishEvent>;
1817
+ onEmbedFinish?: Callback<InferTelemetryEvent<EmbeddingModelCallEndEvent>>;
1756
1818
  /**
1757
1819
  * Called when an individual reranking model call (doRerank) begins.
1758
1820
  * There is one call per `rerank` invocation.
1759
1821
  */
1760
- onRerankStart?: Callback<RerankStartEvent>;
1822
+ onRerankStart?: Callback<InferTelemetryEvent<RerankingModelCallStartEvent>>;
1761
1823
  /**
1762
1824
  * Called when an individual reranking model call (doRerank) completes.
1763
1825
  * Contains the ranking results from the model response.
1764
1826
  */
1765
- onRerankFinish?: Callback<RerankFinishEvent>;
1827
+ onRerankFinish?: Callback<InferTelemetryEvent<RerankingModelCallEndEvent>>;
1766
1828
  /**
1767
1829
  * Called when an operation completes. Fired for text generation
1768
1830
  * (generateText/streamText), object generation (generateObject/streamObject),
@@ -1770,7 +1832,7 @@ interface Telemetry {
1770
1832
  *
1771
1833
  * Use the event shape or `operationId` to distinguish between operation types.
1772
1834
  */
1773
- onFinish?: Callback<OnFinishEvent<ToolSet> | ObjectOnFinishEvent<unknown> | EmbedOnFinishEvent | RerankOnFinishEvent>;
1835
+ onFinish?: Callback<InferTelemetryEvent<OperationFinishEvent>>;
1774
1836
  /**
1775
1837
  * Called when an unrecoverable error occurs during the generation lifecycle.
1776
1838
  * The error value is untyped — it may be an `Error` instance, an `AISDKError`,