@raindrop-ai/ai-sdk 0.0.32 → 0.0.33

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.
@@ -365,6 +365,29 @@ declare class TraceShipper extends TraceShipper$1 {
365
365
  }
366
366
 
367
367
  type Listener<T> = (event: T) => PromiseLike<void> | void;
368
+ type ToolExecutionEndEvent = {
369
+ callId: string;
370
+ toolCall: {
371
+ toolCallId: string;
372
+ toolName: string;
373
+ };
374
+ toolOutput: {
375
+ type: "tool-result";
376
+ output: unknown;
377
+ } | {
378
+ type: "tool-error";
379
+ error: unknown;
380
+ };
381
+ } | {
382
+ callId: string;
383
+ toolCall: {
384
+ toolCallId: string;
385
+ toolName: string;
386
+ };
387
+ success: boolean;
388
+ output?: unknown;
389
+ error?: unknown;
390
+ };
368
391
  interface TelemetryIntegration {
369
392
  onStart?: Listener<any>;
370
393
  onStepStart?: Listener<any>;
@@ -381,6 +404,7 @@ interface TelemetryIntegration {
381
404
  onEmbedEnd?: Listener<any>;
382
405
  onFinish?: Listener<any>;
383
406
  onEnd?: Listener<any>;
407
+ onAbort?: Listener<any>;
384
408
  onError?: Listener<any>;
385
409
  executeTool?: <T>(params: {
386
410
  callId: string;
@@ -441,6 +465,10 @@ declare class RaindropTelemetryIntegration implements TelemetryIntegration {
441
465
  */
442
466
  private readonly priorParentContexts;
443
467
  constructor(opts: RaindropTelemetryIntegrationOptions);
468
+ /** Flush any buffered events and trace spans to their destinations. */
469
+ flush: () => Promise<void>;
470
+ /** Flush and stop the background flush timers. */
471
+ shutdown: () => Promise<void>;
444
472
  private getState;
445
473
  private cleanup;
446
474
  private spanParentRef;
@@ -456,9 +484,9 @@ declare class RaindropTelemetryIntegration implements TelemetryIntegration {
456
484
  private toolExecutionStart;
457
485
  private toolExecutionEnd;
458
486
  onToolExecutionStart: (event: any) => void;
459
- onToolExecutionEnd: (event: any) => void;
487
+ onToolExecutionEnd: (event: ToolExecutionEndEvent) => void;
460
488
  onToolCallStart: (event: any) => void;
461
- onToolCallFinish: (event: any) => void;
489
+ onToolCallFinish: (event: ToolExecutionEndEvent) => void;
462
490
  onLanguageModelCallStart: (_event: any) => void;
463
491
  onLanguageModelCallEnd: (_event: any) => void;
464
492
  onChunk: (event: any) => void;
@@ -470,8 +498,15 @@ declare class RaindropTelemetryIntegration implements TelemetryIntegration {
470
498
  onFinish: (event: any) => void;
471
499
  onEnd: (event: any) => void;
472
500
  private finishGenerate;
501
+ /**
502
+ * Patch the Raindrop event for a completed, aborted, or errored text
503
+ * generation so it leaves the `isPending` state. Shared by `onFinish`/`onEnd`,
504
+ * `onAbort`, and `onError`.
505
+ */
506
+ private finalizeGenerateEvent;
473
507
  private finishEmbed;
474
508
  onError: (error: unknown) => void;
509
+ onAbort: (event: any) => void;
475
510
  executeTool: <T>({ callId, toolCallId, execute, }: {
476
511
  callId: string;
477
512
  toolCallId: string;
@@ -1102,5 +1137,53 @@ type RaindropAISDKClient = {
1102
1137
  shutdown(): Promise<void>;
1103
1138
  };
1104
1139
  declare function createRaindropAISDK(opts: RaindropAISDKOptions): RaindropAISDKClient;
1140
+ /**
1141
+ * Options for the {@link raindrop} telemetry-integration factory. A superset of
1142
+ * {@link RaindropAISDKOptions} plus the per-integration `context` defaults and
1143
+ * `subagentWrapping` toggle accepted by `createTelemetryIntegration()`.
1144
+ */
1145
+ type RaindropTelemetryOptions = RaindropAISDKOptions & {
1146
+ /** Default Raindrop context (userId/eventName/convoId/properties) applied to every call. */
1147
+ context?: RaindropTelemetryIntegrationOptions["context"];
1148
+ /**
1149
+ * Wrap sub-agent generations (a `generateText`/`streamText` invoked from
1150
+ * inside a tool's `execute`) in a synthetic tool span so parent and child
1151
+ * spans nest correctly. Enabled by default.
1152
+ */
1153
+ subagentWrapping?: boolean;
1154
+ };
1155
+ /**
1156
+ * One-line factory for the AI SDK v7 `registerTelemetry` DX.
1157
+ *
1158
+ * Returns a {@link RaindropTelemetryIntegration} wired to Raindrop's trace +
1159
+ * event shippers, ready to hand to the AI SDK's `registerTelemetry`. The
1160
+ * `writeKey` defaults to the `RAINDROP_WRITE_KEY` environment variable.
1161
+ *
1162
+ * @example
1163
+ * ```ts
1164
+ * import { generateText, registerTelemetry } from "ai";
1165
+ * import { OpenTelemetry } from "@ai-sdk/otel";
1166
+ * import { raindrop } from "@raindrop-ai/ai-sdk";
1167
+ *
1168
+ * registerTelemetry(new OpenTelemetry(), raindrop());
1169
+ *
1170
+ * await generateText({
1171
+ * model: anthropic("claude-sonnet-4-5"),
1172
+ * prompt: "what is the weather in Tokyo?",
1173
+ * telemetry: { functionId: "weather-agent" },
1174
+ * });
1175
+ * ```
1176
+ *
1177
+ * The returned integration also exposes `flush()` / `shutdown()` for
1178
+ * short-lived scripts that need to drain buffered telemetry before exiting:
1179
+ *
1180
+ * ```ts
1181
+ * const rd = raindrop();
1182
+ * registerTelemetry(rd);
1183
+ * // ...
1184
+ * await rd.flush();
1185
+ * ```
1186
+ */
1187
+ declare function raindrop(options?: RaindropTelemetryOptions): RaindropTelemetryIntegration;
1105
1188
 
1106
- export { type AISDKChatRequestLike, type AISDKChatRequestMessageLike, type AISDKMessage, type AgentCallMetadata, type AgentWithMetadata, type Attachment, type BuildEventPatch, ContextManager, type ContextSpan, type CreateSpanArgs, DEFAULT_REDACT_ATTRIBUTE_KEYS, DEFAULT_SECRET_KEY_NAMES, type EndSpanArgs, type EventBuilder, type EventMetadataOptions, type IdentifyInput, type OtlpAnyValue, type OtlpSpan, type ParentToolContext, REDACTED_PLACEHOLDER, type RaindropAISDKClient, type RaindropAISDKContext, type RaindropAISDKOptions, type RaindropCallMetadata, RaindropTelemetryIntegration, type RaindropTelemetryIntegrationOptions, type SelfDiagnosticsOptions, type SelfDiagnosticsSignalDefinition, type SelfDiagnosticsSignalDefinitions, type SelfDiagnosticsTool, type SelfDiagnosticsToolInput, type SelfDiagnosticsToolOptions, type SelfDiagnosticsToolResult, type StartSpanArgs, type TraceSpan, type TransformSpanHook, type WrapAISDKOptions, type WrappedAI, type WrappedAISDK, _resetParentToolContextStorage, _resetRaindropCallMetadataStorage, _resetWarnedMissingUserId, clearParentToolContext, createRaindropAISDK, currentSpan, defaultTransformSpan, enterParentToolContext, eventMetadata, eventMetadataFromChatRequest, getContextManager, getCurrentParentToolContext, getCurrentRaindropCallMetadata, readRaindropCallMetadataFromArgs, redactJsonAttributeValue, redactSecretsInObject, runWithParentToolContext, runWithRaindropCallMetadata, withCurrent };
1189
+ export { type AISDKChatRequestLike, type AISDKChatRequestMessageLike, type AISDKMessage, type AgentCallMetadata, type AgentWithMetadata, type Attachment, type BuildEventPatch, ContextManager, type ContextSpan, type CreateSpanArgs, DEFAULT_REDACT_ATTRIBUTE_KEYS, DEFAULT_SECRET_KEY_NAMES, type EndSpanArgs, type EventBuilder, type EventMetadataOptions, type IdentifyInput, type OtlpAnyValue, type OtlpSpan, type ParentToolContext, REDACTED_PLACEHOLDER, type RaindropAISDKClient, type RaindropAISDKContext, type RaindropAISDKOptions, type RaindropCallMetadata, RaindropTelemetryIntegration, type RaindropTelemetryIntegrationOptions, type RaindropTelemetryOptions, type SelfDiagnosticsOptions, type SelfDiagnosticsSignalDefinition, type SelfDiagnosticsSignalDefinitions, type SelfDiagnosticsTool, type SelfDiagnosticsToolInput, type SelfDiagnosticsToolOptions, type SelfDiagnosticsToolResult, type StartSpanArgs, type TraceSpan, type TransformSpanHook, type WrapAISDKOptions, type WrappedAI, type WrappedAISDK, _resetParentToolContextStorage, _resetRaindropCallMetadataStorage, _resetWarnedMissingUserId, clearParentToolContext, createRaindropAISDK, currentSpan, defaultTransformSpan, enterParentToolContext, eventMetadata, eventMetadataFromChatRequest, getContextManager, getCurrentParentToolContext, getCurrentRaindropCallMetadata, raindrop, readRaindropCallMetadataFromArgs, redactJsonAttributeValue, redactSecretsInObject, runWithParentToolContext, runWithRaindropCallMetadata, withCurrent };
@@ -365,6 +365,29 @@ declare class TraceShipper extends TraceShipper$1 {
365
365
  }
366
366
 
367
367
  type Listener<T> = (event: T) => PromiseLike<void> | void;
368
+ type ToolExecutionEndEvent = {
369
+ callId: string;
370
+ toolCall: {
371
+ toolCallId: string;
372
+ toolName: string;
373
+ };
374
+ toolOutput: {
375
+ type: "tool-result";
376
+ output: unknown;
377
+ } | {
378
+ type: "tool-error";
379
+ error: unknown;
380
+ };
381
+ } | {
382
+ callId: string;
383
+ toolCall: {
384
+ toolCallId: string;
385
+ toolName: string;
386
+ };
387
+ success: boolean;
388
+ output?: unknown;
389
+ error?: unknown;
390
+ };
368
391
  interface TelemetryIntegration {
369
392
  onStart?: Listener<any>;
370
393
  onStepStart?: Listener<any>;
@@ -381,6 +404,7 @@ interface TelemetryIntegration {
381
404
  onEmbedEnd?: Listener<any>;
382
405
  onFinish?: Listener<any>;
383
406
  onEnd?: Listener<any>;
407
+ onAbort?: Listener<any>;
384
408
  onError?: Listener<any>;
385
409
  executeTool?: <T>(params: {
386
410
  callId: string;
@@ -441,6 +465,10 @@ declare class RaindropTelemetryIntegration implements TelemetryIntegration {
441
465
  */
442
466
  private readonly priorParentContexts;
443
467
  constructor(opts: RaindropTelemetryIntegrationOptions);
468
+ /** Flush any buffered events and trace spans to their destinations. */
469
+ flush: () => Promise<void>;
470
+ /** Flush and stop the background flush timers. */
471
+ shutdown: () => Promise<void>;
444
472
  private getState;
445
473
  private cleanup;
446
474
  private spanParentRef;
@@ -456,9 +484,9 @@ declare class RaindropTelemetryIntegration implements TelemetryIntegration {
456
484
  private toolExecutionStart;
457
485
  private toolExecutionEnd;
458
486
  onToolExecutionStart: (event: any) => void;
459
- onToolExecutionEnd: (event: any) => void;
487
+ onToolExecutionEnd: (event: ToolExecutionEndEvent) => void;
460
488
  onToolCallStart: (event: any) => void;
461
- onToolCallFinish: (event: any) => void;
489
+ onToolCallFinish: (event: ToolExecutionEndEvent) => void;
462
490
  onLanguageModelCallStart: (_event: any) => void;
463
491
  onLanguageModelCallEnd: (_event: any) => void;
464
492
  onChunk: (event: any) => void;
@@ -470,8 +498,15 @@ declare class RaindropTelemetryIntegration implements TelemetryIntegration {
470
498
  onFinish: (event: any) => void;
471
499
  onEnd: (event: any) => void;
472
500
  private finishGenerate;
501
+ /**
502
+ * Patch the Raindrop event for a completed, aborted, or errored text
503
+ * generation so it leaves the `isPending` state. Shared by `onFinish`/`onEnd`,
504
+ * `onAbort`, and `onError`.
505
+ */
506
+ private finalizeGenerateEvent;
473
507
  private finishEmbed;
474
508
  onError: (error: unknown) => void;
509
+ onAbort: (event: any) => void;
475
510
  executeTool: <T>({ callId, toolCallId, execute, }: {
476
511
  callId: string;
477
512
  toolCallId: string;
@@ -1102,5 +1137,53 @@ type RaindropAISDKClient = {
1102
1137
  shutdown(): Promise<void>;
1103
1138
  };
1104
1139
  declare function createRaindropAISDK(opts: RaindropAISDKOptions): RaindropAISDKClient;
1140
+ /**
1141
+ * Options for the {@link raindrop} telemetry-integration factory. A superset of
1142
+ * {@link RaindropAISDKOptions} plus the per-integration `context` defaults and
1143
+ * `subagentWrapping` toggle accepted by `createTelemetryIntegration()`.
1144
+ */
1145
+ type RaindropTelemetryOptions = RaindropAISDKOptions & {
1146
+ /** Default Raindrop context (userId/eventName/convoId/properties) applied to every call. */
1147
+ context?: RaindropTelemetryIntegrationOptions["context"];
1148
+ /**
1149
+ * Wrap sub-agent generations (a `generateText`/`streamText` invoked from
1150
+ * inside a tool's `execute`) in a synthetic tool span so parent and child
1151
+ * spans nest correctly. Enabled by default.
1152
+ */
1153
+ subagentWrapping?: boolean;
1154
+ };
1155
+ /**
1156
+ * One-line factory for the AI SDK v7 `registerTelemetry` DX.
1157
+ *
1158
+ * Returns a {@link RaindropTelemetryIntegration} wired to Raindrop's trace +
1159
+ * event shippers, ready to hand to the AI SDK's `registerTelemetry`. The
1160
+ * `writeKey` defaults to the `RAINDROP_WRITE_KEY` environment variable.
1161
+ *
1162
+ * @example
1163
+ * ```ts
1164
+ * import { generateText, registerTelemetry } from "ai";
1165
+ * import { OpenTelemetry } from "@ai-sdk/otel";
1166
+ * import { raindrop } from "@raindrop-ai/ai-sdk";
1167
+ *
1168
+ * registerTelemetry(new OpenTelemetry(), raindrop());
1169
+ *
1170
+ * await generateText({
1171
+ * model: anthropic("claude-sonnet-4-5"),
1172
+ * prompt: "what is the weather in Tokyo?",
1173
+ * telemetry: { functionId: "weather-agent" },
1174
+ * });
1175
+ * ```
1176
+ *
1177
+ * The returned integration also exposes `flush()` / `shutdown()` for
1178
+ * short-lived scripts that need to drain buffered telemetry before exiting:
1179
+ *
1180
+ * ```ts
1181
+ * const rd = raindrop();
1182
+ * registerTelemetry(rd);
1183
+ * // ...
1184
+ * await rd.flush();
1185
+ * ```
1186
+ */
1187
+ declare function raindrop(options?: RaindropTelemetryOptions): RaindropTelemetryIntegration;
1105
1188
 
1106
- export { type AISDKChatRequestLike, type AISDKChatRequestMessageLike, type AISDKMessage, type AgentCallMetadata, type AgentWithMetadata, type Attachment, type BuildEventPatch, ContextManager, type ContextSpan, type CreateSpanArgs, DEFAULT_REDACT_ATTRIBUTE_KEYS, DEFAULT_SECRET_KEY_NAMES, type EndSpanArgs, type EventBuilder, type EventMetadataOptions, type IdentifyInput, type OtlpAnyValue, type OtlpSpan, type ParentToolContext, REDACTED_PLACEHOLDER, type RaindropAISDKClient, type RaindropAISDKContext, type RaindropAISDKOptions, type RaindropCallMetadata, RaindropTelemetryIntegration, type RaindropTelemetryIntegrationOptions, type SelfDiagnosticsOptions, type SelfDiagnosticsSignalDefinition, type SelfDiagnosticsSignalDefinitions, type SelfDiagnosticsTool, type SelfDiagnosticsToolInput, type SelfDiagnosticsToolOptions, type SelfDiagnosticsToolResult, type StartSpanArgs, type TraceSpan, type TransformSpanHook, type WrapAISDKOptions, type WrappedAI, type WrappedAISDK, _resetParentToolContextStorage, _resetRaindropCallMetadataStorage, _resetWarnedMissingUserId, clearParentToolContext, createRaindropAISDK, currentSpan, defaultTransformSpan, enterParentToolContext, eventMetadata, eventMetadataFromChatRequest, getContextManager, getCurrentParentToolContext, getCurrentRaindropCallMetadata, readRaindropCallMetadataFromArgs, redactJsonAttributeValue, redactSecretsInObject, runWithParentToolContext, runWithRaindropCallMetadata, withCurrent };
1189
+ export { type AISDKChatRequestLike, type AISDKChatRequestMessageLike, type AISDKMessage, type AgentCallMetadata, type AgentWithMetadata, type Attachment, type BuildEventPatch, ContextManager, type ContextSpan, type CreateSpanArgs, DEFAULT_REDACT_ATTRIBUTE_KEYS, DEFAULT_SECRET_KEY_NAMES, type EndSpanArgs, type EventBuilder, type EventMetadataOptions, type IdentifyInput, type OtlpAnyValue, type OtlpSpan, type ParentToolContext, REDACTED_PLACEHOLDER, type RaindropAISDKClient, type RaindropAISDKContext, type RaindropAISDKOptions, type RaindropCallMetadata, RaindropTelemetryIntegration, type RaindropTelemetryIntegrationOptions, type RaindropTelemetryOptions, type SelfDiagnosticsOptions, type SelfDiagnosticsSignalDefinition, type SelfDiagnosticsSignalDefinitions, type SelfDiagnosticsTool, type SelfDiagnosticsToolInput, type SelfDiagnosticsToolOptions, type SelfDiagnosticsToolResult, type StartSpanArgs, type TraceSpan, type TransformSpanHook, type WrapAISDKOptions, type WrappedAI, type WrappedAISDK, _resetParentToolContextStorage, _resetRaindropCallMetadataStorage, _resetWarnedMissingUserId, clearParentToolContext, createRaindropAISDK, currentSpan, defaultTransformSpan, enterParentToolContext, eventMetadata, eventMetadataFromChatRequest, getContextManager, getCurrentParentToolContext, getCurrentRaindropCallMetadata, raindrop, readRaindropCallMetadataFromArgs, redactJsonAttributeValue, redactSecretsInObject, runWithParentToolContext, runWithRaindropCallMetadata, withCurrent };
@@ -1019,7 +1019,7 @@ async function* asyncGeneratorWithCurrent(span, gen) {
1019
1019
  // package.json
1020
1020
  var package_default = {
1021
1021
  name: "@raindrop-ai/ai-sdk",
1022
- version: "0.0.32"};
1022
+ version: "0.0.33"};
1023
1023
 
1024
1024
  // src/internal/version.ts
1025
1025
  var libraryName = package_default.name;
@@ -1802,6 +1802,25 @@ var RaindropTelemetryIntegration = class {
1802
1802
  * (the `event.callId` can be the same for parallel sibling tools).
1803
1803
  */
1804
1804
  this.priorParentContexts = /* @__PURE__ */ new Map();
1805
+ // ── lifecycle ─────────────────────────────────────────────────────────────
1806
+ //
1807
+ // The shippers buffer spans/events and flush on a timer, so a short-lived
1808
+ // script (e.g. a single `generateText` then `process.exit`) can exit before
1809
+ // anything ships. Expose `flush`/`shutdown` on the integration itself so the
1810
+ // value returned by `raindrop()` is self-sufficient — callers can keep the
1811
+ // reference they pass to `registerTelemetry` and `await rd.flush()` before
1812
+ // exiting, without also constructing a separate client.
1813
+ /** Flush any buffered events and trace spans to their destinations. */
1814
+ this.flush = async () => {
1815
+ await Promise.all([this.eventShipper.flush(), this.traceShipper.flush()]);
1816
+ };
1817
+ /** Flush and stop the background flush timers. */
1818
+ this.shutdown = async () => {
1819
+ await Promise.all([
1820
+ this.eventShipper.shutdown(),
1821
+ this.traceShipper.shutdown()
1822
+ ]);
1823
+ };
1805
1824
  // ── onStart ─────────────────────────────────────────────────────────────
1806
1825
  this.onStart = (event) => {
1807
1826
  var _a, _b, _c, _d, _e;
@@ -2195,8 +2214,56 @@ var RaindropTelemetryIntegration = class {
2195
2214
  if (state.subagentToolCallSpan) {
2196
2215
  this.traceShipper.endSpan(state.subagentToolCallSpan, { error: actualError });
2197
2216
  }
2217
+ const isEmbed = state.operationId === "ai.embed" || state.operationId === "ai.embedMany";
2218
+ if (!isEmbed) {
2219
+ this.finalizeGenerateEvent(state, state.accumulatedText || void 0, void 0);
2220
+ }
2198
2221
  this.cleanup(event.callId);
2199
2222
  };
2223
+ // ── onAbort ─────────────────────────────────────────────────────────────
2224
+ //
2225
+ // Dispatched by the v7 canary line (post-beta.116) when a `streamText` call
2226
+ // is cancelled via its `AbortSignal`. On abort the SDK fires neither `onEnd`
2227
+ // nor `onError`, so without this every open span would leak and the event
2228
+ // would hang forever in its `isPending` state. We close every open span with
2229
+ // an `ai.response.aborted` marker (no error status — an abort is not a model
2230
+ // failure) and flush the partial event.
2231
+ this.onAbort = (event) => {
2232
+ const callId = event == null ? void 0 : event.callId;
2233
+ if (!callId) return;
2234
+ const state = this.getState(callId);
2235
+ if (!state) return;
2236
+ const abortedAttr = attrString("ai.response.aborted", "true");
2237
+ if (state.stepSpan) {
2238
+ this.traceShipper.endSpan(state.stepSpan, { attributes: [abortedAttr] });
2239
+ state.stepSpan = void 0;
2240
+ state.stepParent = void 0;
2241
+ }
2242
+ for (const embedSpan of state.embedSpans.values()) {
2243
+ this.traceShipper.endSpan(embedSpan, { attributes: [abortedAttr] });
2244
+ }
2245
+ state.embedSpans.clear();
2246
+ for (const toolCallId of state.parentContextToolCallIds) {
2247
+ this.priorParentContexts.delete(toolCallId);
2248
+ }
2249
+ state.parentContextToolCallIds.clear();
2250
+ for (const toolSpan of state.toolSpans.values()) {
2251
+ this.traceShipper.endSpan(toolSpan, { attributes: [abortedAttr] });
2252
+ }
2253
+ state.toolSpans.clear();
2254
+ if (state.rootSpan) {
2255
+ this.traceShipper.endSpan(state.rootSpan, {
2256
+ attributes: [abortedAttr, attrInt("ai.toolCall.count", state.toolCallCount)]
2257
+ });
2258
+ }
2259
+ if (state.subagentToolCallSpan) {
2260
+ this.traceShipper.endSpan(state.subagentToolCallSpan, {
2261
+ attributes: [abortedAttr]
2262
+ });
2263
+ }
2264
+ this.finalizeGenerateEvent(state, state.accumulatedText || void 0, void 0);
2265
+ this.cleanup(callId);
2266
+ };
2200
2267
  // ── executeTool ─────────────────────────────────────────────────────────
2201
2268
  this.executeTool = async ({
2202
2269
  callId,
@@ -2354,11 +2421,12 @@ var RaindropTelemetryIntegration = class {
2354
2421
  const toolSpan = state.toolSpans.get(event.toolCall.toolCallId);
2355
2422
  if (!toolSpan) return;
2356
2423
  state.toolCallCount += 1;
2357
- if (event.success) {
2358
- const outputAttrs = state.recordOutputs ? [attrString("ai.toolCall.result", safeJsonWithUint8(event.output))] : [];
2424
+ const outcome = "toolOutput" in event ? event.toolOutput.type === "tool-result" ? { success: true, output: event.toolOutput.output } : { success: false, error: event.toolOutput.error } : event.success ? { success: true, output: event.output } : { success: false, error: event.error };
2425
+ if (outcome.success) {
2426
+ const outputAttrs = state.recordOutputs ? [attrString("ai.toolCall.result", safeJsonWithUint8(outcome.output))] : [];
2359
2427
  this.traceShipper.endSpan(toolSpan, { attributes: outputAttrs });
2360
2428
  } else {
2361
- this.traceShipper.endSpan(toolSpan, { error: event.error });
2429
+ this.traceShipper.endSpan(toolSpan, { error: outcome.error });
2362
2430
  }
2363
2431
  this.emitLive(state, "tool_result", event.toolCall.toolName);
2364
2432
  state.toolSpans.delete(event.toolCall.toolCallId);
@@ -2432,7 +2500,7 @@ var RaindropTelemetryIntegration = class {
2432
2500
  }
2433
2501
  }
2434
2502
  finishGenerate(event, state) {
2435
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
2503
+ var _a, _b, _c, _d, _e, _f;
2436
2504
  if (state.rootSpan) {
2437
2505
  const outputAttrs = [];
2438
2506
  if (state.recordOutputs) {
@@ -2480,38 +2548,47 @@ var RaindropTelemetryIntegration = class {
2480
2548
  );
2481
2549
  this.traceShipper.endSpan(state.rootSpan, { attributes: outputAttrs });
2482
2550
  }
2551
+ this.finalizeGenerateEvent(
2552
+ state,
2553
+ (_e = event.text) != null ? _e : state.accumulatedText || void 0,
2554
+ (_f = event.response) == null ? void 0 : _f.modelId
2555
+ );
2556
+ }
2557
+ /**
2558
+ * Patch the Raindrop event for a completed, aborted, or errored text
2559
+ * generation so it leaves the `isPending` state. Shared by `onFinish`/`onEnd`,
2560
+ * `onAbort`, and `onError`.
2561
+ */
2562
+ finalizeGenerateEvent(state, output, model) {
2563
+ var _a, _b, _c, _d, _e, _f, _g, _h;
2483
2564
  const suppressSubagentEvent = state.subagentName !== void 0 && state.subagentToolCallSpan !== void 0;
2484
- if (this.sendEvents && !suppressSubagentEvent) {
2485
- const callMeta = this.extractRaindropMetadata(state.metadata);
2486
- const userId = (_f = callMeta.userId) != null ? _f : (_e = this.defaultContext) == null ? void 0 : _e.userId;
2487
- if (userId) {
2488
- const eventName = (_i = (_h = callMeta.eventName) != null ? _h : (_g = this.defaultContext) == null ? void 0 : _g.eventName) != null ? _i : state.operationId;
2489
- const output = (_j = event.text) != null ? _j : state.accumulatedText || void 0;
2490
- const input = state.inputText;
2491
- const model = (_k = event.response) == null ? void 0 : _k.modelId;
2492
- const properties = {
2493
- ...(_l = this.defaultContext) == null ? void 0 : _l.properties,
2494
- ...callMeta.properties
2495
- };
2496
- const convoId = (_n = callMeta.convoId) != null ? _n : (_m = this.defaultContext) == null ? void 0 : _m.convoId;
2497
- void this.eventShipper.patch(state.eventId, {
2498
- eventName,
2499
- userId,
2500
- convoId,
2501
- input,
2502
- output,
2503
- model,
2504
- properties: Object.keys(properties).length > 0 ? properties : void 0,
2505
- isPending: false
2506
- }).catch((err) => {
2507
- if (this.debug) {
2508
- console.warn(
2509
- `[raindrop-ai/ai-sdk] event patch failed: ${err instanceof Error ? err.message : err}`
2510
- );
2511
- }
2512
- });
2565
+ if (!this.sendEvents || suppressSubagentEvent) return;
2566
+ const callMeta = this.extractRaindropMetadata(state.metadata);
2567
+ const userId = (_b = callMeta.userId) != null ? _b : (_a = this.defaultContext) == null ? void 0 : _a.userId;
2568
+ if (!userId) return;
2569
+ const eventName = (_e = (_d = callMeta.eventName) != null ? _d : (_c = this.defaultContext) == null ? void 0 : _c.eventName) != null ? _e : state.operationId;
2570
+ const input = state.inputText;
2571
+ const properties = {
2572
+ ...(_f = this.defaultContext) == null ? void 0 : _f.properties,
2573
+ ...callMeta.properties
2574
+ };
2575
+ const convoId = (_h = callMeta.convoId) != null ? _h : (_g = this.defaultContext) == null ? void 0 : _g.convoId;
2576
+ void this.eventShipper.patch(state.eventId, {
2577
+ eventName,
2578
+ userId,
2579
+ convoId,
2580
+ input,
2581
+ output,
2582
+ model,
2583
+ properties: Object.keys(properties).length > 0 ? properties : void 0,
2584
+ isPending: false
2585
+ }).catch((err) => {
2586
+ if (this.debug) {
2587
+ console.warn(
2588
+ `[raindrop-ai/ai-sdk] event patch failed: ${err instanceof Error ? err.message : err}`
2589
+ );
2513
2590
  }
2514
- }
2591
+ });
2515
2592
  }
2516
2593
  finishEmbed(event, state) {
2517
2594
  var _a;
@@ -4844,6 +4921,13 @@ function createRaindropAISDK(opts) {
4844
4921
  }
4845
4922
  };
4846
4923
  }
4924
+ function raindrop(options = {}) {
4925
+ var _a;
4926
+ const { context, subagentWrapping, writeKey, ...rest } = options;
4927
+ const resolvedWriteKey = writeKey != null ? writeKey : typeof process !== "undefined" ? (_a = process.env) == null ? void 0 : _a.RAINDROP_WRITE_KEY : void 0;
4928
+ const client = createRaindropAISDK({ ...rest, writeKey: resolvedWriteKey });
4929
+ return client.createTelemetryIntegration({ context, subagentWrapping });
4930
+ }
4847
4931
 
4848
4932
  exports.DEFAULT_REDACT_ATTRIBUTE_KEYS = DEFAULT_REDACT_ATTRIBUTE_KEYS;
4849
4933
  exports.DEFAULT_SECRET_KEY_NAMES = DEFAULT_SECRET_KEY_NAMES;
@@ -4862,6 +4946,7 @@ exports.eventMetadataFromChatRequest = eventMetadataFromChatRequest;
4862
4946
  exports.getContextManager = getContextManager;
4863
4947
  exports.getCurrentParentToolContext = getCurrentParentToolContext;
4864
4948
  exports.getCurrentRaindropCallMetadata = getCurrentRaindropCallMetadata;
4949
+ exports.raindrop = raindrop;
4865
4950
  exports.readRaindropCallMetadataFromArgs = readRaindropCallMetadataFromArgs;
4866
4951
  exports.redactJsonAttributeValue = redactJsonAttributeValue;
4867
4952
  exports.redactSecretsInObject = redactSecretsInObject;