@anuma/sdk 6.39.1 → 6.39.2

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.
@@ -24607,6 +24607,7 @@ function useChat(options) {
24607
24607
  apiType: defaultApiType = "auto",
24608
24608
  smoothing,
24609
24609
  preProcessors,
24610
+ hooks,
24610
24611
  piiRedaction,
24611
24612
  onPiiRedacted,
24612
24613
  resumable = false,
@@ -24860,6 +24861,7 @@ function useChat(options) {
24860
24861
  onToolCallArgumentsDelta,
24861
24862
  onStepFinish,
24862
24863
  preProcessors,
24864
+ hooks,
24863
24865
  piiRedaction: requestPiiRedaction ?? resolvedPiiRedaction,
24864
24866
  onPiiRedacted
24865
24867
  });
@@ -24899,6 +24901,7 @@ function useChat(options) {
24899
24901
  defaultApiType,
24900
24902
  smoothing,
24901
24903
  preProcessors,
24904
+ hooks,
24902
24905
  resolvedPiiRedaction,
24903
24906
  onPiiRedacted,
24904
24907
  resumable,
@@ -25869,6 +25872,7 @@ function useChatStorage(options) {
25869
25872
  embeddingModel = DEFAULT_API_EMBEDDING_MODEL,
25870
25873
  minContentLength = DEFAULT_MIN_CONTENT_LENGTH,
25871
25874
  preProcessors,
25875
+ hooks,
25872
25876
  smoothing,
25873
25877
  resumable = false,
25874
25878
  onCancelResult,
@@ -26328,6 +26332,7 @@ function useChatStorage(options) {
26328
26332
  onError,
26329
26333
  apiType,
26330
26334
  preProcessors,
26335
+ hooks,
26331
26336
  smoothing,
26332
26337
  resumable,
26333
26338
  onCancelResult,
@@ -2002,6 +2002,127 @@ declare function createStreamingDeAnonymizer(redactor: PiiRedactor, emit: (chunk
2002
2002
  flush: () => void;
2003
2003
  };
2004
2004
 
2005
+ type LlmTokenUsage = {
2006
+ inputTokens?: number;
2007
+ outputTokens?: number;
2008
+ };
2009
+ type ModelCallStartEvent = {
2010
+ runId: string;
2011
+ stepIndex: number;
2012
+ model: string;
2013
+ messages: Array<LlmapiMessage>;
2014
+ tools: Array<LlmapiChatCompletionTool>;
2015
+ requestBody: Record<string, unknown>;
2016
+ };
2017
+ type ModelCallEndEvent = {
2018
+ runId: string;
2019
+ stepIndex: number;
2020
+ content: string;
2021
+ toolCalls: Array<{
2022
+ id: string;
2023
+ name: string;
2024
+ arguments: string;
2025
+ }>;
2026
+ usage?: LlmTokenUsage;
2027
+ finishReason?: string;
2028
+ error?: string;
2029
+ };
2030
+ type ToolUseStartEvent = {
2031
+ runId: string;
2032
+ stepIndex: number;
2033
+ toolCallId: string;
2034
+ name: string;
2035
+ rawArguments: string;
2036
+ parsedArguments?: Record<string, unknown>;
2037
+ };
2038
+ type ToolUseEndEvent = {
2039
+ runId: string;
2040
+ stepIndex: number;
2041
+ toolCallId: string;
2042
+ name: string;
2043
+ result?: unknown;
2044
+ error?: string;
2045
+ errorType?: "parse" | "timeout" | "execution" | "cancelled";
2046
+ };
2047
+ type RunStartEvent = {
2048
+ runId: string;
2049
+ model: string;
2050
+ messages: Array<LlmapiMessage>;
2051
+ tools: Array<LlmapiChatCompletionTool>;
2052
+ };
2053
+ type RunEndEvent = {
2054
+ runId: string;
2055
+ finalContent: string;
2056
+ /** Number of LLM rounds executed. */
2057
+ totalSteps: number;
2058
+ };
2059
+ type RunErrorEvent = {
2060
+ runId: string;
2061
+ error: string;
2062
+ /**
2063
+ * Only `"model"` today. Tool failures surface on {@link RunHooks.afterToolUse}
2064
+ * with `errorType`, not on `onRunError`, so a tool stage isn't needed yet.
2065
+ * Left as a union to leave room for `"validation"` or `"tool"` later
2066
+ * without a breaking change.
2067
+ */
2068
+ stage: "model";
2069
+ /**
2070
+ * The underlying `Error` when one is available. `RunErrorEvent` predates
2071
+ * this field, so `error` (a string message) is kept for backwards
2072
+ * compatibility; new consumers should prefer `errorObject` when present
2073
+ * for stack traces and `cause`.
2074
+ */
2075
+ errorObject?: Error;
2076
+ };
2077
+ type RunHooks = {
2078
+ /**
2079
+ * Fires once per `runToolLoop` invocation, before any pre-processor or
2080
+ * model call. The `messages` payload is the raw caller-supplied
2081
+ * conversation — pre-processors that enrich it (e.g. web search context)
2082
+ * run afterwards, so `beforeModelCall.messages` reflects the
2083
+ * post-enrichment state.
2084
+ */
2085
+ onRunStart?: (e: RunStartEvent) => Promise<void> | void;
2086
+ /**
2087
+ * Fires exactly once per run on clean completion. Mutually exclusive
2088
+ * with `onRunError` — a run that completes fires `onRunEnd`; a run that
2089
+ * errors or is aborted fires `onRunError`. Consumers can rely on
2090
+ * exactly one terminal hook per `onRunStart`.
2091
+ */
2092
+ onRunEnd?: (e: RunEndEvent) => Promise<void> | void;
2093
+ /**
2094
+ * Fires exactly once per run when the loop errors or is aborted.
2095
+ * `stage` is `"model"` for stream/provider errors and for aborts;
2096
+ * tool execution failures surface as structured `{ error, errorType }`
2097
+ * results on `afterToolUse` rather than firing `onRunError`.
2098
+ * Mutually exclusive with `onRunEnd`.
2099
+ */
2100
+ onRunError?: (e: RunErrorEvent) => Promise<void> | void;
2101
+ beforeModelCall?: (e: ModelCallStartEvent) => Promise<void> | void;
2102
+ afterModelCall?: (e: ModelCallEndEvent) => Promise<void> | void;
2103
+ /**
2104
+ * Fires before each tool the model invoked — including server-side
2105
+ * tools that have no client executor. Today this is observe-only.
2106
+ * TODO(follow-up PR): allow returning { args?, abort?: { reason } } to
2107
+ * mutate arguments or short-circuit the tool call.
2108
+ *
2109
+ * Pairing with {@link afterToolUse} is asymmetric: server-side tools
2110
+ * routed via `onToolCall` get `beforeToolUse` but no `afterToolUse`,
2111
+ * because they don't execute in-process. Tools blocked by a failed
2112
+ * dependency or a dependency cycle DO get `afterToolUse` with
2113
+ * `errorType: "execution"` — consumers tallying matched pairs should
2114
+ * filter on executor-backed tools to avoid spurious imbalances.
2115
+ */
2116
+ beforeToolUse?: (e: ToolUseStartEvent) => Promise<void> | void;
2117
+ /**
2118
+ * Fires after each executor-backed tool finishes (success or failure)
2119
+ * and after each dependency-blocked tool is skipped (`errorType:
2120
+ * "execution"`). Does NOT fire for server-side tools routed via
2121
+ * `onToolCall`. See {@link beforeToolUse} for the pairing contract.
2122
+ */
2123
+ afterToolUse?: (e: ToolUseEndEvent) => Promise<void> | void;
2124
+ };
2125
+
2005
2126
  /**
2006
2127
  * Configuration for stream output smoothing.
2007
2128
  *
@@ -2196,6 +2317,8 @@ type BaseSendMessageArgs = ResponsesApiOptions & {
2196
2317
  type BaseUseChatOptions = {
2197
2318
  getToken?: () => Promise<string | null>;
2198
2319
  baseUrl?: string;
2320
+ /** Run lifecycle hooks for telemetry and tracing, for example from `createMetricsHooks` */
2321
+ hooks?: RunHooks | RunHooks[];
2199
2322
  /**
2200
2323
  * Callback function to be called when a new data chunk is received.
2201
2324
  */
@@ -7135,6 +7258,8 @@ interface CreateConversationOptions {
7135
7258
  * @inline
7136
7259
  */
7137
7260
  interface BaseUseChatStorageOptions {
7261
+ /** Run lifecycle hooks for telemetry and tracing, for example from `createMetricsHooks` */
7262
+ hooks?: RunHooks | RunHooks[];
7138
7263
  /** WatermelonDB database instance for storing conversations and messages */
7139
7264
  database: Database;
7140
7265
  /** Route memory reads, writes, recall and the memory tools through this store instead of the database's vault; folders are unsupported. With `piiRedaction` on, build the store with `embeddingOptions.maskInput` (e.g. `(text) => redactor.maskText(text)` with any `PiiRedactor`) or its memory calls throw. */
@@ -2002,6 +2002,127 @@ declare function createStreamingDeAnonymizer(redactor: PiiRedactor, emit: (chunk
2002
2002
  flush: () => void;
2003
2003
  };
2004
2004
 
2005
+ type LlmTokenUsage = {
2006
+ inputTokens?: number;
2007
+ outputTokens?: number;
2008
+ };
2009
+ type ModelCallStartEvent = {
2010
+ runId: string;
2011
+ stepIndex: number;
2012
+ model: string;
2013
+ messages: Array<LlmapiMessage>;
2014
+ tools: Array<LlmapiChatCompletionTool>;
2015
+ requestBody: Record<string, unknown>;
2016
+ };
2017
+ type ModelCallEndEvent = {
2018
+ runId: string;
2019
+ stepIndex: number;
2020
+ content: string;
2021
+ toolCalls: Array<{
2022
+ id: string;
2023
+ name: string;
2024
+ arguments: string;
2025
+ }>;
2026
+ usage?: LlmTokenUsage;
2027
+ finishReason?: string;
2028
+ error?: string;
2029
+ };
2030
+ type ToolUseStartEvent = {
2031
+ runId: string;
2032
+ stepIndex: number;
2033
+ toolCallId: string;
2034
+ name: string;
2035
+ rawArguments: string;
2036
+ parsedArguments?: Record<string, unknown>;
2037
+ };
2038
+ type ToolUseEndEvent = {
2039
+ runId: string;
2040
+ stepIndex: number;
2041
+ toolCallId: string;
2042
+ name: string;
2043
+ result?: unknown;
2044
+ error?: string;
2045
+ errorType?: "parse" | "timeout" | "execution" | "cancelled";
2046
+ };
2047
+ type RunStartEvent = {
2048
+ runId: string;
2049
+ model: string;
2050
+ messages: Array<LlmapiMessage>;
2051
+ tools: Array<LlmapiChatCompletionTool>;
2052
+ };
2053
+ type RunEndEvent = {
2054
+ runId: string;
2055
+ finalContent: string;
2056
+ /** Number of LLM rounds executed. */
2057
+ totalSteps: number;
2058
+ };
2059
+ type RunErrorEvent = {
2060
+ runId: string;
2061
+ error: string;
2062
+ /**
2063
+ * Only `"model"` today. Tool failures surface on {@link RunHooks.afterToolUse}
2064
+ * with `errorType`, not on `onRunError`, so a tool stage isn't needed yet.
2065
+ * Left as a union to leave room for `"validation"` or `"tool"` later
2066
+ * without a breaking change.
2067
+ */
2068
+ stage: "model";
2069
+ /**
2070
+ * The underlying `Error` when one is available. `RunErrorEvent` predates
2071
+ * this field, so `error` (a string message) is kept for backwards
2072
+ * compatibility; new consumers should prefer `errorObject` when present
2073
+ * for stack traces and `cause`.
2074
+ */
2075
+ errorObject?: Error;
2076
+ };
2077
+ type RunHooks = {
2078
+ /**
2079
+ * Fires once per `runToolLoop` invocation, before any pre-processor or
2080
+ * model call. The `messages` payload is the raw caller-supplied
2081
+ * conversation — pre-processors that enrich it (e.g. web search context)
2082
+ * run afterwards, so `beforeModelCall.messages` reflects the
2083
+ * post-enrichment state.
2084
+ */
2085
+ onRunStart?: (e: RunStartEvent) => Promise<void> | void;
2086
+ /**
2087
+ * Fires exactly once per run on clean completion. Mutually exclusive
2088
+ * with `onRunError` — a run that completes fires `onRunEnd`; a run that
2089
+ * errors or is aborted fires `onRunError`. Consumers can rely on
2090
+ * exactly one terminal hook per `onRunStart`.
2091
+ */
2092
+ onRunEnd?: (e: RunEndEvent) => Promise<void> | void;
2093
+ /**
2094
+ * Fires exactly once per run when the loop errors or is aborted.
2095
+ * `stage` is `"model"` for stream/provider errors and for aborts;
2096
+ * tool execution failures surface as structured `{ error, errorType }`
2097
+ * results on `afterToolUse` rather than firing `onRunError`.
2098
+ * Mutually exclusive with `onRunEnd`.
2099
+ */
2100
+ onRunError?: (e: RunErrorEvent) => Promise<void> | void;
2101
+ beforeModelCall?: (e: ModelCallStartEvent) => Promise<void> | void;
2102
+ afterModelCall?: (e: ModelCallEndEvent) => Promise<void> | void;
2103
+ /**
2104
+ * Fires before each tool the model invoked — including server-side
2105
+ * tools that have no client executor. Today this is observe-only.
2106
+ * TODO(follow-up PR): allow returning { args?, abort?: { reason } } to
2107
+ * mutate arguments or short-circuit the tool call.
2108
+ *
2109
+ * Pairing with {@link afterToolUse} is asymmetric: server-side tools
2110
+ * routed via `onToolCall` get `beforeToolUse` but no `afterToolUse`,
2111
+ * because they don't execute in-process. Tools blocked by a failed
2112
+ * dependency or a dependency cycle DO get `afterToolUse` with
2113
+ * `errorType: "execution"` — consumers tallying matched pairs should
2114
+ * filter on executor-backed tools to avoid spurious imbalances.
2115
+ */
2116
+ beforeToolUse?: (e: ToolUseStartEvent) => Promise<void> | void;
2117
+ /**
2118
+ * Fires after each executor-backed tool finishes (success or failure)
2119
+ * and after each dependency-blocked tool is skipped (`errorType:
2120
+ * "execution"`). Does NOT fire for server-side tools routed via
2121
+ * `onToolCall`. See {@link beforeToolUse} for the pairing contract.
2122
+ */
2123
+ afterToolUse?: (e: ToolUseEndEvent) => Promise<void> | void;
2124
+ };
2125
+
2005
2126
  /**
2006
2127
  * Configuration for stream output smoothing.
2007
2128
  *
@@ -2196,6 +2317,8 @@ type BaseSendMessageArgs = ResponsesApiOptions & {
2196
2317
  type BaseUseChatOptions = {
2197
2318
  getToken?: () => Promise<string | null>;
2198
2319
  baseUrl?: string;
2320
+ /** Run lifecycle hooks for telemetry and tracing, for example from `createMetricsHooks` */
2321
+ hooks?: RunHooks | RunHooks[];
2199
2322
  /**
2200
2323
  * Callback function to be called when a new data chunk is received.
2201
2324
  */
@@ -7135,6 +7258,8 @@ interface CreateConversationOptions {
7135
7258
  * @inline
7136
7259
  */
7137
7260
  interface BaseUseChatStorageOptions {
7261
+ /** Run lifecycle hooks for telemetry and tracing, for example from `createMetricsHooks` */
7262
+ hooks?: RunHooks | RunHooks[];
7138
7263
  /** WatermelonDB database instance for storing conversations and messages */
7139
7264
  database: Database;
7140
7265
  /** Route memory reads, writes, recall and the memory tools through this store instead of the database's vault; folders are unsupported. With `piiRedaction` on, build the store with `embeddingOptions.maskInput` (e.g. `(text) => redactor.maskText(text)` with any `PiiRedactor`) or its memory calls throw. */
@@ -22960,6 +22960,7 @@ function useChat(options) {
22960
22960
  apiType: defaultApiType = "auto",
22961
22961
  smoothing,
22962
22962
  preProcessors,
22963
+ hooks,
22963
22964
  piiRedaction,
22964
22965
  onPiiRedacted,
22965
22966
  resumable = false,
@@ -23213,6 +23214,7 @@ function useChat(options) {
23213
23214
  onToolCallArgumentsDelta,
23214
23215
  onStepFinish,
23215
23216
  preProcessors,
23217
+ hooks,
23216
23218
  piiRedaction: requestPiiRedaction ?? resolvedPiiRedaction,
23217
23219
  onPiiRedacted
23218
23220
  });
@@ -23252,6 +23254,7 @@ function useChat(options) {
23252
23254
  defaultApiType,
23253
23255
  smoothing,
23254
23256
  preProcessors,
23257
+ hooks,
23255
23258
  resolvedPiiRedaction,
23256
23259
  onPiiRedacted,
23257
23260
  resumable,
@@ -24212,6 +24215,7 @@ function useChatStorage(options) {
24212
24215
  embeddingModel = DEFAULT_API_EMBEDDING_MODEL,
24213
24216
  minContentLength = DEFAULT_MIN_CONTENT_LENGTH,
24214
24217
  preProcessors,
24218
+ hooks,
24215
24219
  smoothing,
24216
24220
  resumable = false,
24217
24221
  onCancelResult,
@@ -24671,6 +24675,7 @@ function useChatStorage(options) {
24671
24675
  onError,
24672
24676
  apiType,
24673
24677
  preProcessors,
24678
+ hooks,
24674
24679
  smoothing,
24675
24680
  resumable,
24676
24681
  onCancelResult,
@@ -32345,6 +32345,7 @@ function useChat(options) {
32345
32345
  apiType: defaultApiType = "auto",
32346
32346
  smoothing,
32347
32347
  preProcessors,
32348
+ hooks,
32348
32349
  piiRedaction,
32349
32350
  onPiiRedacted
32350
32351
  } = options || {};
@@ -32516,6 +32517,7 @@ function useChat(options) {
32516
32517
  onToolCallArgumentsDelta,
32517
32518
  onStepFinish,
32518
32519
  preProcessors,
32520
+ hooks,
32519
32521
  piiRedaction: requestPiiRedaction ?? resolvedPiiRedaction,
32520
32522
  onPiiRedacted
32521
32523
  });
@@ -32553,6 +32555,7 @@ function useChat(options) {
32553
32555
  defaultApiType,
32554
32556
  smoothing,
32555
32557
  preProcessors,
32558
+ hooks,
32556
32559
  resolvedPiiRedaction,
32557
32560
  onPiiRedacted
32558
32561
  ]
@@ -33767,6 +33770,7 @@ function useChatStorage(options) {
33767
33770
  minContentLength = DEFAULT_MIN_CONTENT_LENGTH,
33768
33771
  mcpR2Domain = MCP_R2_DOMAIN,
33769
33772
  preProcessors,
33773
+ hooks,
33770
33774
  smoothing,
33771
33775
  piiRedaction,
33772
33776
  onPiiRedacted,
@@ -34341,6 +34345,7 @@ function useChatStorage(options) {
34341
34345
  onStreamMeta,
34342
34346
  apiType,
34343
34347
  preProcessors,
34348
+ hooks,
34344
34349
  smoothing,
34345
34350
  piiRedaction: resolvedPiiRedaction,
34346
34351
  onPiiRedacted
@@ -2706,6 +2706,127 @@ declare class PiiRedactor {
2706
2706
  clear(): void;
2707
2707
  }
2708
2708
 
2709
+ type LlmTokenUsage = {
2710
+ inputTokens?: number;
2711
+ outputTokens?: number;
2712
+ };
2713
+ type ModelCallStartEvent = {
2714
+ runId: string;
2715
+ stepIndex: number;
2716
+ model: string;
2717
+ messages: Array<LlmapiMessage>;
2718
+ tools: Array<LlmapiChatCompletionTool>;
2719
+ requestBody: Record<string, unknown>;
2720
+ };
2721
+ type ModelCallEndEvent = {
2722
+ runId: string;
2723
+ stepIndex: number;
2724
+ content: string;
2725
+ toolCalls: Array<{
2726
+ id: string;
2727
+ name: string;
2728
+ arguments: string;
2729
+ }>;
2730
+ usage?: LlmTokenUsage;
2731
+ finishReason?: string;
2732
+ error?: string;
2733
+ };
2734
+ type ToolUseStartEvent = {
2735
+ runId: string;
2736
+ stepIndex: number;
2737
+ toolCallId: string;
2738
+ name: string;
2739
+ rawArguments: string;
2740
+ parsedArguments?: Record<string, unknown>;
2741
+ };
2742
+ type ToolUseEndEvent = {
2743
+ runId: string;
2744
+ stepIndex: number;
2745
+ toolCallId: string;
2746
+ name: string;
2747
+ result?: unknown;
2748
+ error?: string;
2749
+ errorType?: "parse" | "timeout" | "execution" | "cancelled";
2750
+ };
2751
+ type RunStartEvent = {
2752
+ runId: string;
2753
+ model: string;
2754
+ messages: Array<LlmapiMessage>;
2755
+ tools: Array<LlmapiChatCompletionTool>;
2756
+ };
2757
+ type RunEndEvent = {
2758
+ runId: string;
2759
+ finalContent: string;
2760
+ /** Number of LLM rounds executed. */
2761
+ totalSteps: number;
2762
+ };
2763
+ type RunErrorEvent = {
2764
+ runId: string;
2765
+ error: string;
2766
+ /**
2767
+ * Only `"model"` today. Tool failures surface on {@link RunHooks.afterToolUse}
2768
+ * with `errorType`, not on `onRunError`, so a tool stage isn't needed yet.
2769
+ * Left as a union to leave room for `"validation"` or `"tool"` later
2770
+ * without a breaking change.
2771
+ */
2772
+ stage: "model";
2773
+ /**
2774
+ * The underlying `Error` when one is available. `RunErrorEvent` predates
2775
+ * this field, so `error` (a string message) is kept for backwards
2776
+ * compatibility; new consumers should prefer `errorObject` when present
2777
+ * for stack traces and `cause`.
2778
+ */
2779
+ errorObject?: Error;
2780
+ };
2781
+ type RunHooks = {
2782
+ /**
2783
+ * Fires once per `runToolLoop` invocation, before any pre-processor or
2784
+ * model call. The `messages` payload is the raw caller-supplied
2785
+ * conversation — pre-processors that enrich it (e.g. web search context)
2786
+ * run afterwards, so `beforeModelCall.messages` reflects the
2787
+ * post-enrichment state.
2788
+ */
2789
+ onRunStart?: (e: RunStartEvent) => Promise<void> | void;
2790
+ /**
2791
+ * Fires exactly once per run on clean completion. Mutually exclusive
2792
+ * with `onRunError` — a run that completes fires `onRunEnd`; a run that
2793
+ * errors or is aborted fires `onRunError`. Consumers can rely on
2794
+ * exactly one terminal hook per `onRunStart`.
2795
+ */
2796
+ onRunEnd?: (e: RunEndEvent) => Promise<void> | void;
2797
+ /**
2798
+ * Fires exactly once per run when the loop errors or is aborted.
2799
+ * `stage` is `"model"` for stream/provider errors and for aborts;
2800
+ * tool execution failures surface as structured `{ error, errorType }`
2801
+ * results on `afterToolUse` rather than firing `onRunError`.
2802
+ * Mutually exclusive with `onRunEnd`.
2803
+ */
2804
+ onRunError?: (e: RunErrorEvent) => Promise<void> | void;
2805
+ beforeModelCall?: (e: ModelCallStartEvent) => Promise<void> | void;
2806
+ afterModelCall?: (e: ModelCallEndEvent) => Promise<void> | void;
2807
+ /**
2808
+ * Fires before each tool the model invoked — including server-side
2809
+ * tools that have no client executor. Today this is observe-only.
2810
+ * TODO(follow-up PR): allow returning { args?, abort?: { reason } } to
2811
+ * mutate arguments or short-circuit the tool call.
2812
+ *
2813
+ * Pairing with {@link afterToolUse} is asymmetric: server-side tools
2814
+ * routed via `onToolCall` get `beforeToolUse` but no `afterToolUse`,
2815
+ * because they don't execute in-process. Tools blocked by a failed
2816
+ * dependency or a dependency cycle DO get `afterToolUse` with
2817
+ * `errorType: "execution"` — consumers tallying matched pairs should
2818
+ * filter on executor-backed tools to avoid spurious imbalances.
2819
+ */
2820
+ beforeToolUse?: (e: ToolUseStartEvent) => Promise<void> | void;
2821
+ /**
2822
+ * Fires after each executor-backed tool finishes (success or failure)
2823
+ * and after each dependency-blocked tool is skipped (`errorType:
2824
+ * "execution"`). Does NOT fire for server-side tools routed via
2825
+ * `onToolCall`. See {@link beforeToolUse} for the pairing contract.
2826
+ */
2827
+ afterToolUse?: (e: ToolUseEndEvent) => Promise<void> | void;
2828
+ };
2829
+
2709
2830
  /**
2710
2831
  * Configuration for stream output smoothing.
2711
2832
  *
@@ -2900,6 +3021,8 @@ type BaseSendMessageArgs = ResponsesApiOptions & {
2900
3021
  type BaseUseChatOptions = {
2901
3022
  getToken?: () => Promise<string | null>;
2902
3023
  baseUrl?: string;
3024
+ /** Run lifecycle hooks for telemetry and tracing, for example from `createMetricsHooks` */
3025
+ hooks?: RunHooks | RunHooks[];
2903
3026
  /**
2904
3027
  * Callback function to be called when a new data chunk is received.
2905
3028
  */
@@ -8691,6 +8814,8 @@ interface CreateConversationOptions {
8691
8814
  * @inline
8692
8815
  */
8693
8816
  interface BaseUseChatStorageOptions {
8817
+ /** Run lifecycle hooks for telemetry and tracing, for example from `createMetricsHooks` */
8818
+ hooks?: RunHooks | RunHooks[];
8694
8819
  /** WatermelonDB database instance for storing conversations and messages */
8695
8820
  database: Database;
8696
8821
  /** Route memory reads, writes, recall and the memory tools through this store instead of the database's vault; folders are unsupported. With `piiRedaction` on, build the store with `embeddingOptions.maskInput` (e.g. `(text) => redactor.maskText(text)` with any `PiiRedactor`) or its memory calls throw. */
@@ -2706,6 +2706,127 @@ declare class PiiRedactor {
2706
2706
  clear(): void;
2707
2707
  }
2708
2708
 
2709
+ type LlmTokenUsage = {
2710
+ inputTokens?: number;
2711
+ outputTokens?: number;
2712
+ };
2713
+ type ModelCallStartEvent = {
2714
+ runId: string;
2715
+ stepIndex: number;
2716
+ model: string;
2717
+ messages: Array<LlmapiMessage>;
2718
+ tools: Array<LlmapiChatCompletionTool>;
2719
+ requestBody: Record<string, unknown>;
2720
+ };
2721
+ type ModelCallEndEvent = {
2722
+ runId: string;
2723
+ stepIndex: number;
2724
+ content: string;
2725
+ toolCalls: Array<{
2726
+ id: string;
2727
+ name: string;
2728
+ arguments: string;
2729
+ }>;
2730
+ usage?: LlmTokenUsage;
2731
+ finishReason?: string;
2732
+ error?: string;
2733
+ };
2734
+ type ToolUseStartEvent = {
2735
+ runId: string;
2736
+ stepIndex: number;
2737
+ toolCallId: string;
2738
+ name: string;
2739
+ rawArguments: string;
2740
+ parsedArguments?: Record<string, unknown>;
2741
+ };
2742
+ type ToolUseEndEvent = {
2743
+ runId: string;
2744
+ stepIndex: number;
2745
+ toolCallId: string;
2746
+ name: string;
2747
+ result?: unknown;
2748
+ error?: string;
2749
+ errorType?: "parse" | "timeout" | "execution" | "cancelled";
2750
+ };
2751
+ type RunStartEvent = {
2752
+ runId: string;
2753
+ model: string;
2754
+ messages: Array<LlmapiMessage>;
2755
+ tools: Array<LlmapiChatCompletionTool>;
2756
+ };
2757
+ type RunEndEvent = {
2758
+ runId: string;
2759
+ finalContent: string;
2760
+ /** Number of LLM rounds executed. */
2761
+ totalSteps: number;
2762
+ };
2763
+ type RunErrorEvent = {
2764
+ runId: string;
2765
+ error: string;
2766
+ /**
2767
+ * Only `"model"` today. Tool failures surface on {@link RunHooks.afterToolUse}
2768
+ * with `errorType`, not on `onRunError`, so a tool stage isn't needed yet.
2769
+ * Left as a union to leave room for `"validation"` or `"tool"` later
2770
+ * without a breaking change.
2771
+ */
2772
+ stage: "model";
2773
+ /**
2774
+ * The underlying `Error` when one is available. `RunErrorEvent` predates
2775
+ * this field, so `error` (a string message) is kept for backwards
2776
+ * compatibility; new consumers should prefer `errorObject` when present
2777
+ * for stack traces and `cause`.
2778
+ */
2779
+ errorObject?: Error;
2780
+ };
2781
+ type RunHooks = {
2782
+ /**
2783
+ * Fires once per `runToolLoop` invocation, before any pre-processor or
2784
+ * model call. The `messages` payload is the raw caller-supplied
2785
+ * conversation — pre-processors that enrich it (e.g. web search context)
2786
+ * run afterwards, so `beforeModelCall.messages` reflects the
2787
+ * post-enrichment state.
2788
+ */
2789
+ onRunStart?: (e: RunStartEvent) => Promise<void> | void;
2790
+ /**
2791
+ * Fires exactly once per run on clean completion. Mutually exclusive
2792
+ * with `onRunError` — a run that completes fires `onRunEnd`; a run that
2793
+ * errors or is aborted fires `onRunError`. Consumers can rely on
2794
+ * exactly one terminal hook per `onRunStart`.
2795
+ */
2796
+ onRunEnd?: (e: RunEndEvent) => Promise<void> | void;
2797
+ /**
2798
+ * Fires exactly once per run when the loop errors or is aborted.
2799
+ * `stage` is `"model"` for stream/provider errors and for aborts;
2800
+ * tool execution failures surface as structured `{ error, errorType }`
2801
+ * results on `afterToolUse` rather than firing `onRunError`.
2802
+ * Mutually exclusive with `onRunEnd`.
2803
+ */
2804
+ onRunError?: (e: RunErrorEvent) => Promise<void> | void;
2805
+ beforeModelCall?: (e: ModelCallStartEvent) => Promise<void> | void;
2806
+ afterModelCall?: (e: ModelCallEndEvent) => Promise<void> | void;
2807
+ /**
2808
+ * Fires before each tool the model invoked — including server-side
2809
+ * tools that have no client executor. Today this is observe-only.
2810
+ * TODO(follow-up PR): allow returning { args?, abort?: { reason } } to
2811
+ * mutate arguments or short-circuit the tool call.
2812
+ *
2813
+ * Pairing with {@link afterToolUse} is asymmetric: server-side tools
2814
+ * routed via `onToolCall` get `beforeToolUse` but no `afterToolUse`,
2815
+ * because they don't execute in-process. Tools blocked by a failed
2816
+ * dependency or a dependency cycle DO get `afterToolUse` with
2817
+ * `errorType: "execution"` — consumers tallying matched pairs should
2818
+ * filter on executor-backed tools to avoid spurious imbalances.
2819
+ */
2820
+ beforeToolUse?: (e: ToolUseStartEvent) => Promise<void> | void;
2821
+ /**
2822
+ * Fires after each executor-backed tool finishes (success or failure)
2823
+ * and after each dependency-blocked tool is skipped (`errorType:
2824
+ * "execution"`). Does NOT fire for server-side tools routed via
2825
+ * `onToolCall`. See {@link beforeToolUse} for the pairing contract.
2826
+ */
2827
+ afterToolUse?: (e: ToolUseEndEvent) => Promise<void> | void;
2828
+ };
2829
+
2709
2830
  /**
2710
2831
  * Configuration for stream output smoothing.
2711
2832
  *
@@ -2900,6 +3021,8 @@ type BaseSendMessageArgs = ResponsesApiOptions & {
2900
3021
  type BaseUseChatOptions = {
2901
3022
  getToken?: () => Promise<string | null>;
2902
3023
  baseUrl?: string;
3024
+ /** Run lifecycle hooks for telemetry and tracing, for example from `createMetricsHooks` */
3025
+ hooks?: RunHooks | RunHooks[];
2903
3026
  /**
2904
3027
  * Callback function to be called when a new data chunk is received.
2905
3028
  */
@@ -8691,6 +8814,8 @@ interface CreateConversationOptions {
8691
8814
  * @inline
8692
8815
  */
8693
8816
  interface BaseUseChatStorageOptions {
8817
+ /** Run lifecycle hooks for telemetry and tracing, for example from `createMetricsHooks` */
8818
+ hooks?: RunHooks | RunHooks[];
8694
8819
  /** WatermelonDB database instance for storing conversations and messages */
8695
8820
  database: Database;
8696
8821
  /** Route memory reads, writes, recall and the memory tools through this store instead of the database's vault; folders are unsupported. With `piiRedaction` on, build the store with `embeddingOptions.maskInput` (e.g. `(text) => redactor.maskText(text)` with any `PiiRedactor`) or its memory calls throw. */
@@ -30435,6 +30435,7 @@ function useChat(options) {
30435
30435
  apiType: defaultApiType = "auto",
30436
30436
  smoothing,
30437
30437
  preProcessors,
30438
+ hooks,
30438
30439
  piiRedaction,
30439
30440
  onPiiRedacted
30440
30441
  } = options || {};
@@ -30606,6 +30607,7 @@ function useChat(options) {
30606
30607
  onToolCallArgumentsDelta,
30607
30608
  onStepFinish,
30608
30609
  preProcessors,
30610
+ hooks,
30609
30611
  piiRedaction: requestPiiRedaction ?? resolvedPiiRedaction,
30610
30612
  onPiiRedacted
30611
30613
  });
@@ -30643,6 +30645,7 @@ function useChat(options) {
30643
30645
  defaultApiType,
30644
30646
  smoothing,
30645
30647
  preProcessors,
30648
+ hooks,
30646
30649
  resolvedPiiRedaction,
30647
30650
  onPiiRedacted
30648
30651
  ]
@@ -31847,6 +31850,7 @@ function useChatStorage(options) {
31847
31850
  minContentLength = DEFAULT_MIN_CONTENT_LENGTH,
31848
31851
  mcpR2Domain = MCP_R2_DOMAIN,
31849
31852
  preProcessors,
31853
+ hooks,
31850
31854
  smoothing,
31851
31855
  piiRedaction,
31852
31856
  onPiiRedacted,
@@ -32421,6 +32425,7 @@ function useChatStorage(options) {
32421
32425
  onStreamMeta,
32422
32426
  apiType,
32423
32427
  preProcessors,
32428
+ hooks,
32424
32429
  smoothing,
32425
32430
  piiRedaction: resolvedPiiRedaction,
32426
32431
  onPiiRedacted
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anuma/sdk",
3
- "version": "6.39.1",
3
+ "version": "6.39.2",
4
4
  "description": "",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.mjs",