@raindrop-ai/ai-sdk 0.0.20 → 0.0.22

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.
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- // ../core/dist/chunk-H6VSZSLN.js
3
+ // ../core/dist/chunk-4UCYIEH4.js
4
4
  function getCrypto() {
5
5
  const c = globalThis.crypto;
6
6
  return c;
@@ -145,6 +145,7 @@ async function postJson(url, body, headers, opts) {
145
145
  );
146
146
  }
147
147
  var SpanStatusCode = {
148
+ UNSET: 0,
148
149
  ERROR: 2
149
150
  };
150
151
  function createSpanIds(parent) {
@@ -159,6 +160,9 @@ function createSpanIds(parent) {
159
160
  function nowUnixNanoString() {
160
161
  return Date.now().toString() + "000000";
161
162
  }
163
+ function unixMsToNanoString(ms) {
164
+ return String(Math.floor(ms)) + "000000";
165
+ }
162
166
  function attrString(key, value) {
163
167
  if (value === void 0) return void 0;
164
168
  return { key, value: { stringValue: value } };
@@ -465,11 +469,66 @@ var EventShipper = class {
465
469
  }
466
470
  }
467
471
  };
472
+ var LOCAL_DEBUGGER_ENV_VAR = "RAINDROP_LOCAL_DEBUGGER";
473
+ function resolveLocalDebuggerBaseUrl(baseUrl) {
474
+ var _a, _b, _c;
475
+ const resolved = (_b = baseUrl != null ? baseUrl : typeof process !== "undefined" ? (_a = process.env) == null ? void 0 : _a[LOCAL_DEBUGGER_ENV_VAR] : void 0) != null ? _b : null;
476
+ return resolved ? (_c = formatEndpoint(resolved)) != null ? _c : null : null;
477
+ }
478
+ function localDebuggerEnabled(baseUrl) {
479
+ return resolveLocalDebuggerBaseUrl(baseUrl) !== null;
480
+ }
481
+ function normalizeLocalDebuggerLiveEventType(type) {
482
+ switch (type) {
483
+ case "text-delta":
484
+ return "text_delta";
485
+ case "reasoning":
486
+ case "reasoning-delta":
487
+ return "reasoning_delta";
488
+ case "tool-call":
489
+ return "tool_start";
490
+ case "tool-result":
491
+ return "tool_result";
492
+ default:
493
+ return type;
494
+ }
495
+ }
496
+ function mirrorTraceExportToLocalDebugger(body, options = {}) {
497
+ var _a;
498
+ const baseUrl = resolveLocalDebuggerBaseUrl(options.baseUrl);
499
+ if (!baseUrl) return;
500
+ void postJson(`${baseUrl}traces`, body, {}, {
501
+ maxAttempts: 1,
502
+ debug: (_a = options.debug) != null ? _a : false,
503
+ sdkName: options.sdkName
504
+ }).catch(() => {
505
+ });
506
+ }
507
+ function sendLocalDebuggerLiveEvent(event, options = {}) {
508
+ var _a, _b;
509
+ const baseUrl = resolveLocalDebuggerBaseUrl(options.baseUrl);
510
+ if (!baseUrl) return;
511
+ void postJson(
512
+ `${baseUrl}live`,
513
+ {
514
+ ...event,
515
+ type: normalizeLocalDebuggerLiveEventType(event.type),
516
+ timestamp: (_a = event.timestamp) != null ? _a : Date.now()
517
+ },
518
+ {},
519
+ {
520
+ maxAttempts: 1,
521
+ debug: (_b = options.debug) != null ? _b : false,
522
+ sdkName: options.sdkName
523
+ }
524
+ ).catch(() => {
525
+ });
526
+ }
468
527
  var TraceShipper = class {
469
528
  constructor(opts) {
470
529
  this.queue = [];
471
530
  this.inFlight = /* @__PURE__ */ new Set();
472
- var _a, _b, _c, _d, _e, _f, _g, _h;
531
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
473
532
  this.writeKey = (_a = opts.writeKey) == null ? void 0 : _a.trim();
474
533
  this.baseUrl = (_b = formatEndpoint(opts.endpoint)) != null ? _b : "https://api.raindrop.ai/v1/";
475
534
  this.enabled = opts.enabled !== false;
@@ -482,6 +541,13 @@ var TraceShipper = class {
482
541
  this.prefix = `[raindrop-ai/${this.sdkName}]`;
483
542
  this.serviceName = (_g = opts.serviceName) != null ? _g : "raindrop.core";
484
543
  this.serviceVersion = (_h = opts.serviceVersion) != null ? _h : "0.0.0";
544
+ const localDebugger = typeof process !== "undefined" ? (_i = process.env) == null ? void 0 : _i.RAINDROP_LOCAL_DEBUGGER : void 0;
545
+ if (localDebugger) {
546
+ this.localDebuggerUrl = (_j = resolveLocalDebuggerBaseUrl(localDebugger)) != null ? _j : void 0;
547
+ if (this.debug) {
548
+ console.log(`${this.prefix} Local debugger mirroring: ${this.localDebuggerUrl}`);
549
+ }
550
+ }
485
551
  }
486
552
  isDebugEnabled() {
487
553
  return this.debug;
@@ -498,7 +564,25 @@ var TraceShipper = class {
498
564
  attrString("ai.operationId", args.operationId)
499
565
  ];
500
566
  if ((_b = args.attributes) == null ? void 0 : _b.length) attrs.push(...args.attributes);
501
- return { ids, name: args.name, startTimeUnixNano: started, attributes: attrs };
567
+ const span = { ids, name: args.name, startTimeUnixNano: started, attributes: attrs };
568
+ if (this.localDebuggerUrl) {
569
+ const openSpan = buildOtlpSpan({
570
+ ids: span.ids,
571
+ name: span.name,
572
+ startTimeUnixNano: span.startTimeUnixNano,
573
+ endTimeUnixNano: span.startTimeUnixNano,
574
+ // placeholder — will be updated on endSpan
575
+ attributes: span.attributes,
576
+ status: { code: SpanStatusCode.UNSET }
577
+ });
578
+ const body = buildExportTraceServiceRequest([openSpan], this.serviceName, this.serviceVersion);
579
+ mirrorTraceExportToLocalDebugger(body, {
580
+ baseUrl: this.localDebuggerUrl,
581
+ debug: false,
582
+ sdkName: this.sdkName
583
+ });
584
+ }
585
+ return span;
502
586
  }
503
587
  endSpan(span, extra) {
504
588
  var _a, _b;
@@ -521,6 +605,14 @@ var TraceShipper = class {
521
605
  status
522
606
  });
523
607
  this.enqueue(otlp);
608
+ if (this.localDebuggerUrl) {
609
+ const body = buildExportTraceServiceRequest([otlp], this.serviceName, this.serviceVersion);
610
+ mirrorTraceExportToLocalDebugger(body, {
611
+ baseUrl: this.localDebuggerUrl,
612
+ debug: false,
613
+ sdkName: this.sdkName
614
+ });
615
+ }
524
616
  }
525
617
  createSpan(args) {
526
618
  var _a;
@@ -538,6 +630,14 @@ var TraceShipper = class {
538
630
  status: args.status
539
631
  });
540
632
  this.enqueue(otlp);
633
+ if (this.localDebuggerUrl) {
634
+ const body = buildExportTraceServiceRequest([otlp], this.serviceName, this.serviceVersion);
635
+ mirrorTraceExportToLocalDebugger(body, {
636
+ baseUrl: this.localDebuggerUrl,
637
+ debug: false,
638
+ sdkName: this.sdkName
639
+ });
640
+ }
541
641
  }
542
642
  enqueue(span) {
543
643
  if (!this.enabled) return;
@@ -732,7 +832,7 @@ async function* asyncGeneratorWithCurrent(span, gen) {
732
832
  // package.json
733
833
  var package_default = {
734
834
  name: "@raindrop-ai/ai-sdk",
735
- version: "0.0.20"};
835
+ version: "0.0.22"};
736
836
 
737
837
  // src/internal/version.ts
738
838
  var libraryName = package_default.name;
@@ -1538,6 +1638,7 @@ var RaindropTelemetryIntegration = class {
1538
1638
  ]
1539
1639
  });
1540
1640
  state.toolSpans.set(toolCall.toolCallId, toolSpan);
1641
+ this.emitLive(state, "tool_start", toolCall.toolName, { args: toolCall.input });
1541
1642
  };
1542
1643
  // ── onToolCallFinish ────────────────────────────────────────────────────
1543
1644
  this.onToolCallFinish = (event) => {
@@ -1552,11 +1653,12 @@ var RaindropTelemetryIntegration = class {
1552
1653
  } else {
1553
1654
  this.traceShipper.endSpan(toolSpan, { error: event.error });
1554
1655
  }
1656
+ this.emitLive(state, "tool_result", event.toolCall.toolName);
1555
1657
  state.toolSpans.delete(event.toolCall.toolCallId);
1556
1658
  };
1557
1659
  // ── onChunk (streaming) ─────────────────────────────────────────────────
1558
1660
  this.onChunk = (event) => {
1559
- var _a, _b, _c;
1661
+ var _a, _b, _c, _d, _e;
1560
1662
  const callId = (_b = event.callId) != null ? _b : (_a = event.chunk) == null ? void 0 : _a.callId;
1561
1663
  if (!callId) return;
1562
1664
  const state = this.getState(callId);
@@ -1567,6 +1669,12 @@ var RaindropTelemetryIntegration = class {
1567
1669
  const delta = (_c = chunk.textDelta) != null ? _c : chunk.delta;
1568
1670
  if (typeof delta === "string") {
1569
1671
  state.accumulatedText += delta;
1672
+ this.emitLive(state, "text_delta", delta);
1673
+ }
1674
+ } else if (chunk.type === "reasoning" || chunk.type === "reasoning-delta") {
1675
+ const text = (_e = (_d = chunk.textDelta) != null ? _d : chunk.text) != null ? _e : chunk.delta;
1676
+ if (typeof text === "string") {
1677
+ this.emitLive(state, "reasoning_delta", text);
1570
1678
  }
1571
1679
  }
1572
1680
  };
@@ -1754,6 +1862,23 @@ var RaindropTelemetryIntegration = class {
1754
1862
  spanParentRef(span) {
1755
1863
  return { traceIdB64: span.ids.traceIdB64, spanIdB64: span.ids.spanIdB64 };
1756
1864
  }
1865
+ emitLive(state, type, content, extra) {
1866
+ var _a, _b, _c, _d, _e, _f;
1867
+ if (!localDebuggerEnabled() || !state.rootSpan) return;
1868
+ const callMeta = this.extractRaindropMetadata(state.metadata);
1869
+ sendLocalDebuggerLiveEvent({
1870
+ traceId: state.rootSpan.ids.traceIdB64,
1871
+ type,
1872
+ content,
1873
+ metadata: {
1874
+ userId: (_b = callMeta.userId) != null ? _b : (_a = this.defaultContext) == null ? void 0 : _a.userId,
1875
+ convoId: (_d = callMeta.convoId) != null ? _d : (_c = this.defaultContext) == null ? void 0 : _c.convoId,
1876
+ eventName: (_f = callMeta.eventName) != null ? _f : (_e = this.defaultContext) == null ? void 0 : _e.eventName,
1877
+ eventId: state.eventId,
1878
+ ...extra
1879
+ }
1880
+ });
1881
+ }
1757
1882
  extractRaindropMetadata(metadata) {
1758
1883
  if (!metadata) return {};
1759
1884
  const result = {};
@@ -2299,6 +2424,36 @@ function shouldKeepEventPending(params) {
2299
2424
  if (params.error != null || !params.canKeepEventPending) return false;
2300
2425
  return params.finishReason === "tool-calls" || params.finishReason === "tool_calls";
2301
2426
  }
2427
+ function normalizePromptAttr(arg) {
2428
+ if (!isRecord(arg)) return arg;
2429
+ const system = arg["system"];
2430
+ const prompt = arg["prompt"];
2431
+ const messages = arg["messages"];
2432
+ if (Array.isArray(messages)) {
2433
+ if (system) {
2434
+ const sysContent = typeof system === "string" ? system : JSON.stringify(system);
2435
+ return [{ role: "system", content: sysContent }, ...messages];
2436
+ }
2437
+ return messages;
2438
+ }
2439
+ if (typeof prompt === "string") {
2440
+ const msgs = [];
2441
+ if (system) {
2442
+ msgs.push({ role: "system", content: typeof system === "string" ? system : JSON.stringify(system) });
2443
+ }
2444
+ msgs.push({ role: "user", content: prompt });
2445
+ return msgs;
2446
+ }
2447
+ return { system, prompt, messages };
2448
+ }
2449
+ function getLocalDebuggerMetadata(ctx) {
2450
+ return {
2451
+ eventId: ctx.eventId,
2452
+ ...ctx.eventName ? { eventName: ctx.eventName } : {},
2453
+ ...ctx.userId ? { userId: ctx.userId } : {},
2454
+ ...ctx.convoId ? { convoId: ctx.convoId } : {}
2455
+ };
2456
+ }
2302
2457
  async function safeFinalize(finalize, debug, result, error) {
2303
2458
  try {
2304
2459
  await finalize(result, error);
@@ -2371,17 +2526,17 @@ function setupOperation(params) {
2371
2526
  attrString("ai.telemetry.functionId", telemetry == null ? void 0 : telemetry.functionId),
2372
2527
  attrString("ai.model.provider", modelInfoFromArgs.provider),
2373
2528
  attrString("ai.model.id", modelInfoFromArgs.modelId),
2529
+ attrString("ai.telemetry.metadata.raindrop.eventId", eventId),
2530
+ attrString("ai.telemetry.metadata.raindrop.eventName", mergedCtx.eventName),
2531
+ attrString("ai.telemetry.metadata.raindrop.userId", mergedCtx.userId),
2532
+ attrString("ai.telemetry.metadata.raindrop.convoId", mergedCtx.convoId),
2374
2533
  ...attrsFromTelemetryMetadata(telemetry == null ? void 0 : telemetry.metadata),
2375
2534
  ...attrsFromHeaders(isRecord(arg) ? arg["headers"] : void 0),
2376
2535
  ...attrsFromSettings(arg),
2377
2536
  ...(telemetry == null ? void 0 : telemetry.recordInputs) === false ? [] : [
2378
2537
  attrString(
2379
2538
  "ai.prompt",
2380
- safeJsonWithUint8({
2381
- system: isRecord(arg) ? arg["system"] : void 0,
2382
- prompt: isRecord(arg) ? arg["prompt"] : void 0,
2383
- messages: isRecord(arg) ? arg["messages"] : void 0
2384
- })
2539
+ safeJsonWithUint8(normalizePromptAttr(arg))
2385
2540
  )
2386
2541
  ]
2387
2542
  ]
@@ -2390,6 +2545,7 @@ function setupOperation(params) {
2390
2545
  const operationSelfDiagnostics = isObjectOperation(operation) ? void 0 : selfDiagnostics;
2391
2546
  const wrapCtx = {
2392
2547
  eventId,
2548
+ context: mergedCtx,
2393
2549
  telemetry,
2394
2550
  sendTraces,
2395
2551
  debug,
@@ -2764,6 +2920,7 @@ function wrapAISDK(aiSDK, deps) {
2764
2920
  const perCallEventIdGenerated = !perCallEventIdExplicit;
2765
2921
  const perCallCtx = {
2766
2922
  eventId: perCallEventId,
2923
+ context: wrapTimeCtx,
2767
2924
  telemetry,
2768
2925
  sendTraces: false,
2769
2926
  debug,
@@ -2918,7 +3075,7 @@ function wrapAgentGenerate(generate, instance, agentSettings, className, aiSDK,
2918
3075
  if (!mergedCtx.userId) warnMissingUserIdOnce();
2919
3076
  const inherited = await getCurrentParentSpanContext();
2920
3077
  const eventId = (_c = (_b2 = (_a2 = callTimeCtx.eventId) != null ? _a2 : mergedCtx.eventId) != null ? _b2 : inherited == null ? void 0 : inherited.eventId) != null ? _c : randomUUID();
2921
- const ctx = { ...mergedCtx};
3078
+ const ctx = { ...mergedCtx, eventId };
2922
3079
  const inheritedParent = inherited && inherited.eventId === eventId ? { traceIdB64: inherited.traceIdB64, spanIdB64: inherited.spanIdB64 } : void 0;
2923
3080
  const outerOperationId = `ai.${operation}`;
2924
3081
  const { operationName, resourceName } = opName(outerOperationId, telemetry == null ? void 0 : telemetry.functionId);
@@ -2934,17 +3091,17 @@ function wrapAgentGenerate(generate, instance, agentSettings, className, aiSDK,
2934
3091
  attrString("ai.telemetry.functionId", telemetry == null ? void 0 : telemetry.functionId),
2935
3092
  attrString("ai.model.provider", modelInfoFromArgs.provider),
2936
3093
  attrString("ai.model.id", modelInfoFromArgs.modelId),
3094
+ attrString("ai.telemetry.metadata.raindrop.eventId", eventId),
3095
+ attrString("ai.telemetry.metadata.raindrop.eventName", ctx.eventName),
3096
+ attrString("ai.telemetry.metadata.raindrop.userId", ctx.userId),
3097
+ attrString("ai.telemetry.metadata.raindrop.convoId", ctx.convoId),
2937
3098
  ...attrsFromTelemetryMetadata(telemetry == null ? void 0 : telemetry.metadata),
2938
3099
  ...attrsFromHeaders(mergedArgs["headers"]),
2939
3100
  ...attrsFromSettings(mergedArgs),
2940
3101
  ...(telemetry == null ? void 0 : telemetry.recordInputs) === false ? [] : [
2941
3102
  attrString(
2942
3103
  "ai.prompt",
2943
- safeJsonWithUint8({
2944
- system: (_d = mergedArgs["system"]) != null ? _d : mergedArgs["instructions"],
2945
- prompt: mergedArgs["prompt"],
2946
- messages: mergedArgs["messages"]
2947
- })
3104
+ safeJsonWithUint8(normalizePromptAttr({ system: (_d = mergedArgs["system"]) != null ? _d : mergedArgs["instructions"], prompt: mergedArgs["prompt"], messages: mergedArgs["messages"] }))
2948
3105
  )
2949
3106
  ]
2950
3107
  ]
@@ -2952,6 +3109,7 @@ function wrapAgentGenerate(generate, instance, agentSettings, className, aiSDK,
2952
3109
  const rootParentForChildren = rootSpan ? { traceIdB64: rootSpan.ids.traceIdB64, spanIdB64: rootSpan.ids.spanIdB64 } : inheritedParent;
2953
3110
  const wrapCtx = {
2954
3111
  eventId,
3112
+ context: ctx,
2955
3113
  telemetry,
2956
3114
  sendTraces,
2957
3115
  debug,
@@ -3125,7 +3283,7 @@ function wrapAgentStream(stream, instance, agentSettings, className, aiSDK, deps
3125
3283
  if (!mergedCtx.userId) warnMissingUserIdOnce();
3126
3284
  const inherited = await getCurrentParentSpanContext();
3127
3285
  const eventId = (_c = (_b2 = (_a2 = callTimeCtx.eventId) != null ? _a2 : mergedCtx.eventId) != null ? _b2 : inherited == null ? void 0 : inherited.eventId) != null ? _c : randomUUID();
3128
- const ctx = { ...mergedCtx};
3286
+ const ctx = { ...mergedCtx, eventId };
3129
3287
  const inheritedParent = inherited && inherited.eventId === eventId ? { traceIdB64: inherited.traceIdB64, spanIdB64: inherited.spanIdB64 } : void 0;
3130
3288
  const outerOperationId = `ai.${operation}`;
3131
3289
  const { operationName, resourceName } = opName(outerOperationId, telemetry == null ? void 0 : telemetry.functionId);
@@ -3141,17 +3299,17 @@ function wrapAgentStream(stream, instance, agentSettings, className, aiSDK, deps
3141
3299
  attrString("ai.telemetry.functionId", telemetry == null ? void 0 : telemetry.functionId),
3142
3300
  attrString("ai.model.provider", modelInfoFromArgs.provider),
3143
3301
  attrString("ai.model.id", modelInfoFromArgs.modelId),
3302
+ attrString("ai.telemetry.metadata.raindrop.eventId", eventId),
3303
+ attrString("ai.telemetry.metadata.raindrop.eventName", ctx.eventName),
3304
+ attrString("ai.telemetry.metadata.raindrop.userId", ctx.userId),
3305
+ attrString("ai.telemetry.metadata.raindrop.convoId", ctx.convoId),
3144
3306
  ...attrsFromTelemetryMetadata(telemetry == null ? void 0 : telemetry.metadata),
3145
3307
  ...attrsFromHeaders(mergedArgs["headers"]),
3146
3308
  ...attrsFromSettings(mergedArgs),
3147
3309
  ...(telemetry == null ? void 0 : telemetry.recordInputs) === false ? [] : [
3148
3310
  attrString(
3149
3311
  "ai.prompt",
3150
- safeJsonWithUint8({
3151
- system: (_d = mergedArgs["system"]) != null ? _d : mergedArgs["instructions"],
3152
- prompt: mergedArgs["prompt"],
3153
- messages: mergedArgs["messages"]
3154
- })
3312
+ safeJsonWithUint8(normalizePromptAttr({ system: (_d = mergedArgs["system"]) != null ? _d : mergedArgs["instructions"], prompt: mergedArgs["prompt"], messages: mergedArgs["messages"] }))
3155
3313
  )
3156
3314
  ]
3157
3315
  ]
@@ -3159,6 +3317,7 @@ function wrapAgentStream(stream, instance, agentSettings, className, aiSDK, deps
3159
3317
  const rootParentForChildren = rootSpan ? { traceIdB64: rootSpan.ids.traceIdB64, spanIdB64: rootSpan.ids.spanIdB64 } : inheritedParent;
3160
3318
  const wrapCtx = {
3161
3319
  eventId,
3320
+ context: ctx,
3162
3321
  telemetry,
3163
3322
  sendTraces,
3164
3323
  debug,
@@ -3382,6 +3541,21 @@ function wrapToolExecute(name, tool, ctx, toolCalls) {
3382
3541
  });
3383
3542
  }
3384
3543
  };
3544
+ const sendToolLiveEvent = (parent, type, metadata) => {
3545
+ if (!localDebuggerEnabled() || !parent) return;
3546
+ sendLocalDebuggerLiveEvent({
3547
+ traceId: parent.traceIdB64,
3548
+ type,
3549
+ content: name,
3550
+ metadata: {
3551
+ ...getLocalDebuggerMetadata({
3552
+ ...ctx.context,
3553
+ eventId: ctx.eventId
3554
+ }),
3555
+ ...metadata
3556
+ }
3557
+ });
3558
+ };
3385
3559
  const createContextSpan = (span) => ({
3386
3560
  traceIdB64: span.ids.traceIdB64,
3387
3561
  spanIdB64: span.ids.spanIdB64,
@@ -3397,6 +3571,7 @@ function wrapToolExecute(name, tool, ctx, toolCalls) {
3397
3571
  const parentCtx = await getCurrentParentSpanContext();
3398
3572
  const parent = parentCtx && parentCtx.eventId === ctx.eventId ? { traceIdB64: parentCtx.traceIdB64, spanIdB64: parentCtx.spanIdB64 } : ctx.rootParentForChildren;
3399
3573
  const toolSpan = createToolSpan(toolCallId, toolArgs, parent);
3574
+ sendToolLiveEvent(parent, "tool_start", { args: toolArgs });
3400
3575
  try {
3401
3576
  let lastValue;
3402
3577
  const iterator = result[Symbol.asyncIterator]();
@@ -3412,9 +3587,13 @@ function wrapToolExecute(name, tool, ctx, toolCalls) {
3412
3587
  }
3413
3588
  toolCalls.push({ id: toolCallId, name, args: toolArgs, result: lastValue, status: "OK" });
3414
3589
  endToolSpan(toolSpan, lastValue);
3590
+ sendToolLiveEvent(parent, "tool_result", { result: lastValue });
3415
3591
  } catch (error) {
3416
3592
  toolCalls.push({ id: toolCallId, name, args: toolArgs, status: "ERROR" });
3417
3593
  endToolSpan(toolSpan, void 0, error);
3594
+ sendToolLiveEvent(parent, "tool_result", {
3595
+ error: error instanceof Error ? error.message : String(error)
3596
+ });
3418
3597
  throw error;
3419
3598
  }
3420
3599
  })();
@@ -3423,6 +3602,7 @@ function wrapToolExecute(name, tool, ctx, toolCalls) {
3423
3602
  const parentCtx = await getCurrentParentSpanContext();
3424
3603
  const parent = parentCtx && parentCtx.eventId === ctx.eventId ? { traceIdB64: parentCtx.traceIdB64, spanIdB64: parentCtx.spanIdB64 } : ctx.rootParentForChildren;
3425
3604
  const toolSpan = createToolSpan(toolCallId, toolArgs, parent);
3605
+ sendToolLiveEvent(parent, "tool_start", { args: toolArgs });
3426
3606
  const run = async () => {
3427
3607
  try {
3428
3608
  const awaitedResult = await result;
@@ -3434,10 +3614,14 @@ function wrapToolExecute(name, tool, ctx, toolCalls) {
3434
3614
  status: "OK"
3435
3615
  });
3436
3616
  endToolSpan(toolSpan, awaitedResult);
3617
+ sendToolLiveEvent(parent, "tool_result", { result: awaitedResult });
3437
3618
  return awaitedResult;
3438
3619
  } catch (error) {
3439
3620
  toolCalls.push({ id: toolCallId, name, args: toolArgs, status: "ERROR" });
3440
3621
  endToolSpan(toolSpan, void 0, error);
3622
+ sendToolLiveEvent(parent, "tool_result", {
3623
+ error: error instanceof Error ? error.message : String(error)
3624
+ });
3441
3625
  throw error;
3442
3626
  }
3443
3627
  };
@@ -3477,6 +3661,7 @@ function wrapModel(args, aiSDK, outerOperationId, ctx) {
3477
3661
  const original = Reflect.get(target, prop, receiver);
3478
3662
  if (prop === "doGenerate" && isFunction(original)) {
3479
3663
  return async (...callArgs) => {
3664
+ var _a, _b;
3480
3665
  const options = callArgs[0];
3481
3666
  const parentCtx = await getCurrentParentSpanContext();
3482
3667
  const parent = parentCtx && parentCtx.eventId === ctx.eventId ? { traceIdB64: parentCtx.traceIdB64, spanIdB64: parentCtx.spanIdB64 } : ctx.rootParentForChildren;
@@ -3484,6 +3669,22 @@ function wrapModel(args, aiSDK, outerOperationId, ctx) {
3484
3669
  try {
3485
3670
  const result = await original.apply(target, callArgs);
3486
3671
  if (span) endDoGenerateSpan(span, result, modelInfo, ctx);
3672
+ if (localDebuggerEnabled() && ctx.rootParentForChildren && isRecord(result)) {
3673
+ const traceId = ctx.rootParentForChildren.traceIdB64;
3674
+ const liveMeta = getLocalDebuggerMetadata({ ...ctx.context, eventId: ctx.eventId });
3675
+ const content = result["content"];
3676
+ if (Array.isArray(content)) {
3677
+ for (const part of content) {
3678
+ if (isRecord(part)) {
3679
+ if ((part["type"] === "reasoning" || part["type"] === "thinking") && typeof ((_a = part["text"]) != null ? _a : part["thinking"]) === "string") {
3680
+ sendLocalDebuggerLiveEvent({ traceId, type: "reasoning_delta", content: (_b = part["text"]) != null ? _b : part["thinking"], metadata: liveMeta });
3681
+ } else if (part["type"] === "text" && typeof part["text"] === "string") {
3682
+ sendLocalDebuggerLiveEvent({ traceId, type: "text_delta", content: part["text"], metadata: liveMeta });
3683
+ }
3684
+ }
3685
+ }
3686
+ }
3687
+ }
3487
3688
  return result;
3488
3689
  } catch (error) {
3489
3690
  if (span)
@@ -3623,6 +3824,23 @@ function wrapModel(args, aiSDK, outerOperationId, ctx) {
3623
3824
  }
3624
3825
  if ("usage" in value) usage = value["usage"];
3625
3826
  if ("providerMetadata" in value) providerMetadata = value["providerMetadata"];
3827
+ if (localDebuggerEnabled() && ctx.rootParentForChildren) {
3828
+ const traceId = ctx.rootParentForChildren.traceIdB64;
3829
+ const liveMeta = getLocalDebuggerMetadata({ ...ctx.context, eventId: ctx.eventId });
3830
+ if (type === "text-delta") {
3831
+ const text = typeof value["delta"] === "string" ? value["delta"] : typeof value["textDelta"] === "string" ? value["textDelta"] : typeof value["text"] === "string" ? value["text"] : void 0;
3832
+ if (typeof text === "string" && text) sendLocalDebuggerLiveEvent({ traceId, type: "text_delta", content: text, metadata: liveMeta });
3833
+ } else if (type === "reasoning" || type === "reasoning-delta") {
3834
+ const text = typeof value["delta"] === "string" ? value["delta"] : typeof value["text"] === "string" ? value["text"] : typeof value["thinking"] === "string" ? value["thinking"] : void 0;
3835
+ if (typeof text === "string" && text) sendLocalDebuggerLiveEvent({ traceId, type: "reasoning_delta", content: text, metadata: liveMeta });
3836
+ } else if (type === "tool-call") {
3837
+ const toolName = typeof value["toolName"] === "string" ? value["toolName"] : "tool";
3838
+ sendLocalDebuggerLiveEvent({ traceId, type: "tool_start", content: toolName, metadata: { ...liveMeta, args: value["args"] } });
3839
+ } else if (type === "tool-result") {
3840
+ const toolName = typeof value["toolName"] === "string" ? value["toolName"] : "tool";
3841
+ sendLocalDebuggerLiveEvent({ traceId, type: "tool_result", content: toolName, metadata: { ...liveMeta, result: value["result"] } });
3842
+ }
3843
+ }
3626
3844
  }
3627
3845
  controller.enqueue(value);
3628
3846
  } catch (error) {
@@ -3860,6 +4078,14 @@ function eventMetadataFromChatRequest(options) {
3860
4078
  ...eventId ? { eventId } : {}
3861
4079
  });
3862
4080
  }
4081
+ function stringify(v) {
4082
+ if (typeof v === "string") return v;
4083
+ return JSON.stringify(v);
4084
+ }
4085
+ function userAttrsToOtlp(attrs) {
4086
+ if (!attrs) return [];
4087
+ return Object.entries(attrs).map(([key, value]) => attrString(key, value));
4088
+ }
3863
4089
  function envDebugEnabled() {
3864
4090
  var _a;
3865
4091
  if (typeof process === "undefined") return false;
@@ -3928,6 +4154,76 @@ function createRaindropAISDK(opts) {
3928
4154
  await eventShipper.finish(eventId, patch);
3929
4155
  }
3930
4156
  },
4157
+ traces: /* @__PURE__ */ (() => {
4158
+ const openSpans = /* @__PURE__ */ new Map();
4159
+ function toTraceSpan(internal) {
4160
+ return { traceId: internal.ids.traceIdB64, spanId: internal.ids.spanIdB64 };
4161
+ }
4162
+ return {
4163
+ startSpan(args) {
4164
+ const parent = args.parent ? { traceIdB64: args.parent.traceId, spanIdB64: args.parent.spanId } : void 0;
4165
+ const attrs = userAttrsToOtlp(args.attributes);
4166
+ if (args.operationId === "ai.toolCall") {
4167
+ attrs.push(attrString("ai.toolCall.name", args.name));
4168
+ }
4169
+ const internal = traceShipper.startSpan({
4170
+ name: args.name,
4171
+ eventId: args.eventId,
4172
+ parent,
4173
+ operationId: args.operationId,
4174
+ attributes: attrs
4175
+ });
4176
+ const handle = toTraceSpan(internal);
4177
+ openSpans.set(handle.spanId, internal);
4178
+ return handle;
4179
+ },
4180
+ endSpan(span, extra) {
4181
+ const internal = openSpans.get(span.spanId);
4182
+ if (!internal) return;
4183
+ openSpans.delete(span.spanId);
4184
+ const errorValue = extra == null ? void 0 : extra.error;
4185
+ const error = errorValue instanceof Error ? errorValue : typeof errorValue === "string" ? new Error(errorValue) : void 0;
4186
+ traceShipper.endSpan(internal, {
4187
+ attributes: userAttrsToOtlp(extra == null ? void 0 : extra.attributes),
4188
+ error
4189
+ });
4190
+ },
4191
+ createSpan(args) {
4192
+ var _a2;
4193
+ const parent = args.parent ? { traceIdB64: args.parent.traceId, spanIdB64: args.parent.spanId } : void 0;
4194
+ const startMs = (_a2 = args.startTime) != null ? _a2 : Date.now() - args.durationMs;
4195
+ const startAttrs = [...userAttrsToOtlp(args.attributes)];
4196
+ if (args.operationId === "ai.toolCall") {
4197
+ startAttrs.push(attrString("ai.toolCall.name", args.name));
4198
+ }
4199
+ if (args.input !== void 0) startAttrs.push(attrString("traceloop.entity.input", stringify(args.input)));
4200
+ if (args.operationId === "ai.toolCall" && args.input !== void 0) {
4201
+ startAttrs.push(attrString("ai.toolCall.args", stringify(args.input)));
4202
+ }
4203
+ const internal = traceShipper.startSpan({
4204
+ name: args.name,
4205
+ eventId: args.eventId,
4206
+ parent,
4207
+ operationId: args.operationId,
4208
+ attributes: startAttrs,
4209
+ startTimeUnixNano: unixMsToNanoString(startMs)
4210
+ });
4211
+ const endAttrs = [];
4212
+ if (args.output !== void 0) endAttrs.push(attrString("traceloop.entity.output", stringify(args.output)));
4213
+ if (args.operationId === "ai.toolCall" && args.output !== void 0) {
4214
+ endAttrs.push(attrString("ai.toolCall.result", stringify(args.output)));
4215
+ }
4216
+ const errorValue = args.error;
4217
+ const error = errorValue instanceof Error ? errorValue : typeof errorValue === "string" ? new Error(errorValue) : void 0;
4218
+ traceShipper.endSpan(internal, {
4219
+ attributes: endAttrs,
4220
+ error,
4221
+ endTimeUnixNano: unixMsToNanoString(startMs + args.durationMs)
4222
+ });
4223
+ return toTraceSpan(internal);
4224
+ }
4225
+ };
4226
+ })(),
3931
4227
  users: {
3932
4228
  async identify(users) {
3933
4229
  await eventShipper.identify(users);