@raindrop-ai/ai-sdk 0.2.1 → 0.3.0

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
  import { AsyncLocalStorage } from 'async_hooks';
2
2
 
3
- // ../core/dist/chunk-QNWZIOOY.js
3
+ // ../core/dist/chunk-YUIXRQWN.js
4
4
  function normalizeFeatureFlags(input) {
5
5
  if (Array.isArray(input)) {
6
6
  return Object.fromEntries(input.map((name) => [name, "true"]));
@@ -63,6 +63,311 @@ function base64Encode(bytes) {
63
63
  }
64
64
  return out;
65
65
  }
66
+ function base64ToHex(value) {
67
+ if (!value) return "";
68
+ const maybeBuffer = globalThis.Buffer;
69
+ if (maybeBuffer) {
70
+ return maybeBuffer.from(value, "base64").toString("hex");
71
+ }
72
+ const atobFn = globalThis.atob;
73
+ if (typeof atobFn !== "function") return "";
74
+ try {
75
+ const binary = atobFn(value);
76
+ let hex = "";
77
+ for (let i = 0; i < binary.length; i++) {
78
+ hex += (binary.charCodeAt(i) & 255).toString(16).padStart(2, "0");
79
+ }
80
+ return hex;
81
+ } catch (e) {
82
+ return "";
83
+ }
84
+ }
85
+ var HANDOFF_ATTRIBUTE_SUFFIXES = {
86
+ mode: "raindrop.handoff.mode",
87
+ childEventId: "raindrop.handoff.childEventId",
88
+ parentEventId: "raindrop.handoff.parentEventId",
89
+ parentSpanId: "raindrop.handoff.parentSpanId",
90
+ name: "raindrop.handoff.name",
91
+ terminal: "raindrop.handoff.terminal",
92
+ agentRole: "raindrop.agent.role"
93
+ };
94
+ var HANDOFF_ATTRIBUTE_PREFIXES = [
95
+ "ai.telemetry.metadata.",
96
+ "ai.settings.context.",
97
+ ""
98
+ ];
99
+ var AI_SDK_METADATA_PREFIX = "ai.telemetry.metadata.";
100
+ var DETACHED_MODE = "detached";
101
+ var SUBAGENT_AGENT_ROLE = "subagent";
102
+ var HANDOFF_TERMINAL_CANCELLED = "cancelled";
103
+ var TOOL_EVENTS_SUFFIX = "raindrop.toolEvents";
104
+ var TOOL_EVENTS_ALLOW = "allow";
105
+ var SUBAGENT_ABORT_OPERATION_ID = "ai.subagent.failed";
106
+ var SUBAGENT_ABORT_SPAN_NAME = SUBAGENT_ABORT_OPERATION_ID;
107
+ var DEFAULT_DISPATCH_SPAN_NAME = "launch_subagent";
108
+ var HANDOFF_TERMINAL_PROPERTY_KEY = HANDOFF_ATTRIBUTE_SUFFIXES.terminal;
109
+ function defined(entries) {
110
+ const out = {};
111
+ for (const [key, value] of Object.entries(entries)) {
112
+ if (typeof value === "string" && value.trim().length > 0) out[key] = value;
113
+ }
114
+ return out;
115
+ }
116
+ function detachedDispatchMetadata(dispatch) {
117
+ return defined({
118
+ [HANDOFF_ATTRIBUTE_SUFFIXES.mode]: DETACHED_MODE,
119
+ [HANDOFF_ATTRIBUTE_SUFFIXES.childEventId]: dispatch.childEventId,
120
+ [HANDOFF_ATTRIBUTE_SUFFIXES.name]: dispatch.name
121
+ });
122
+ }
123
+ function detachedChildMetadata(link) {
124
+ return defined({
125
+ [HANDOFF_ATTRIBUTE_SUFFIXES.parentEventId]: link.parentEventId,
126
+ [HANDOFF_ATTRIBUTE_SUFFIXES.parentSpanId]: link.parentSpanId,
127
+ [HANDOFF_ATTRIBUTE_SUFFIXES.name]: link.name,
128
+ [HANDOFF_ATTRIBUTE_SUFFIXES.agentRole]: SUBAGENT_AGENT_ROLE
129
+ });
130
+ }
131
+ function cancelledTerminalMetadata() {
132
+ return { [HANDOFF_ATTRIBUTE_SUFFIXES.terminal]: HANDOFF_TERMINAL_CANCELLED };
133
+ }
134
+ function cancelledTerminalAttributes() {
135
+ const bare = cancelledTerminalMetadata();
136
+ return { ...bare, ...withMetadataPrefix(bare) };
137
+ }
138
+ function toolEventsAllowMetadata() {
139
+ return { [TOOL_EVENTS_SUFFIX]: TOOL_EVENTS_ALLOW };
140
+ }
141
+ function withMetadataPrefix(metadata) {
142
+ const out = {};
143
+ for (const [key, value] of Object.entries(metadata)) {
144
+ out[key.startsWith(AI_SDK_METADATA_PREFIX) ? key : `${AI_SDK_METADATA_PREFIX}${key}`] = value;
145
+ }
146
+ return out;
147
+ }
148
+ function readHandoffField(metadata, suffix) {
149
+ for (const prefix of HANDOFF_ATTRIBUTE_PREFIXES) {
150
+ const value = metadata[`${prefix}${suffix}`];
151
+ if (typeof value === "string" && value.trim().length > 0) return value.trim();
152
+ }
153
+ return void 0;
154
+ }
155
+ function readDetachedChildLink(metadata) {
156
+ if (!metadata) return void 0;
157
+ const parentEventId = readHandoffField(metadata, HANDOFF_ATTRIBUTE_SUFFIXES.parentEventId);
158
+ if (!parentEventId) return void 0;
159
+ return {
160
+ parentEventId,
161
+ parentSpanId: readHandoffField(metadata, HANDOFF_ATTRIBUTE_SUFFIXES.parentSpanId),
162
+ name: readHandoffField(metadata, HANDOFF_ATTRIBUTE_SUFFIXES.name)
163
+ };
164
+ }
165
+ var BAGGAGE_KEYS = {
166
+ eventId: "raindrop-event-id",
167
+ childEventId: "raindrop-child-event-id",
168
+ name: "raindrop-handoff-name",
169
+ convoId: "raindrop-convo-id",
170
+ userId: "raindrop-user-id",
171
+ // The dispatch span and its trace, carried as fields as well as in
172
+ // `traceparent`. Not redundant: OTel's HTTP instrumentations rewrite
173
+ // `traceparent` from whatever span is current as the request goes out, so by
174
+ // the time the child reads it the ids name the HTTP client span and its trace
175
+ // instead of the dispatch. The child would then point its reverse reference at
176
+ // a span the launcher's UI does not know as a dispatch. A field states which
177
+ // span it means rather than meaning "whoever wrote this header last".
178
+ dispatchSpanId: "raindrop-dispatch-span-id",
179
+ traceId: "raindrop-trace-id"
180
+ };
181
+ var TRACEPARENT_HEADER = "traceparent";
182
+ var BAGGAGE_HEADER = "baggage";
183
+ var W3C_TRACE_ID = /^[0-9a-f]{32}$/;
184
+ var W3C_SPAN_ID = /^[0-9a-f]{16}$/;
185
+ function isW3CTraceId(value) {
186
+ return value !== void 0 && W3C_TRACE_ID.test(value);
187
+ }
188
+ function isW3CSpanId(value) {
189
+ return value !== void 0 && W3C_SPAN_ID.test(value);
190
+ }
191
+ var warnedNonConformantIds = false;
192
+ function warnNonConformantIdsOnce(traceId, spanId) {
193
+ if (warnedNonConformantIds) return;
194
+ warnedNonConformantIds = true;
195
+ console.warn(
196
+ `[raindrop] hand-off carrier ids are not W3C-shaped (traceId="${traceId}", spanId="${spanId}"). The child still reports as its own event, but its reverse reference names a span id the backend does not store, so the link back to the launcher will not resolve. A LangSmith carrier reads this way \u2014 its run ids are UUIDs; propagate Raindrop's own carrier (toHeaders) to link.`
197
+ );
198
+ }
199
+ var HANDOFF_HEADER = "x-raindrop-handoff";
200
+ var LANGSMITH_TRACE_HEADER = "langsmith-trace";
201
+ var LANGSMITH_METADATA_BAGGAGE_KEY = "langsmith-metadata";
202
+ function encodeBaggage(entries) {
203
+ return Object.entries(entries).filter((entry) => Boolean(entry[1])).map(([key, value]) => `${key}=${encodeURIComponent(value)}`).join(",");
204
+ }
205
+ function decodeBaggage(header) {
206
+ if (!header) return {};
207
+ const parsed = {};
208
+ for (const part of header.split(",")) {
209
+ const [rawKey, ...rest] = part.split("=");
210
+ if (!rawKey || rest.length === 0) continue;
211
+ const key = rawKey.trim();
212
+ let value;
213
+ try {
214
+ value = decodeURIComponent(rest.join("=").trim());
215
+ } catch (e) {
216
+ value = rest.join("=").trim();
217
+ }
218
+ if (key in parsed && parsed[key] !== value) throw new ConflictingCarrierError(key);
219
+ parsed[key] = value;
220
+ }
221
+ return parsed;
222
+ }
223
+ function carrierBaggage(carrier) {
224
+ return {
225
+ [BAGGAGE_KEYS.eventId]: carrier.eventId,
226
+ [BAGGAGE_KEYS.childEventId]: carrier.childEventId,
227
+ [BAGGAGE_KEYS.name]: carrier.name,
228
+ [BAGGAGE_KEYS.convoId]: carrier.convoId,
229
+ [BAGGAGE_KEYS.userId]: carrier.userId,
230
+ [BAGGAGE_KEYS.dispatchSpanId]: carrier.spanId,
231
+ [BAGGAGE_KEYS.traceId]: carrier.traceId
232
+ };
233
+ }
234
+ function toHeaders(carrier) {
235
+ const fields = encodeBaggage(carrierBaggage(carrier));
236
+ const headers = {
237
+ [BAGGAGE_HEADER]: fields,
238
+ [HANDOFF_HEADER]: fields
239
+ };
240
+ if (isW3CTraceId(carrier.traceId) && isW3CSpanId(carrier.spanId)) {
241
+ headers[TRACEPARENT_HEADER] = `00-${carrier.traceId}-${carrier.spanId}-01`;
242
+ }
243
+ return headers;
244
+ }
245
+ function langsmithStamp() {
246
+ return (/* @__PURE__ */ new Date()).toISOString().replace(/[-:]/g, "").replace(/\.\d+Z$/, "000000");
247
+ }
248
+ function toLangSmithHeaders(carrier) {
249
+ const stamp = langsmithStamp();
250
+ const dottedOrder = `${stamp}Z${carrier.traceId}.${stamp}Z${carrier.spanId}`;
251
+ return {
252
+ [LANGSMITH_TRACE_HEADER]: dottedOrder,
253
+ [BAGGAGE_HEADER]: encodeBaggage({
254
+ [LANGSMITH_METADATA_BAGGAGE_KEY]: JSON.stringify(carrierBaggage(carrier))
255
+ }),
256
+ // Carried here too: a LangSmith-shaped carrier is rewritten in transit
257
+ // exactly like the native one, and an injected `traceparent` is preferred
258
+ // over the dotted order on read.
259
+ [HANDOFF_HEADER]: encodeBaggage(carrierBaggage(carrier))
260
+ };
261
+ }
262
+ function isHeaderLookup(headers) {
263
+ return "get" in headers && typeof headers.get === "function";
264
+ }
265
+ var ConflictingCarrierError = class extends Error {
266
+ };
267
+ function readHeader(headers, name) {
268
+ var _a, _b, _c;
269
+ if (isHeaderLookup(headers)) return (_a = headers.get(name)) != null ? _a : void 0;
270
+ const value = (_c = headers[name]) != null ? _c : (_b = Object.entries(headers).find(([key]) => key.toLowerCase() === name)) == null ? void 0 : _b[1];
271
+ if (!Array.isArray(value)) return value;
272
+ const distinct = new Set(value);
273
+ if (distinct.size > 1) throw new ConflictingCarrierError(name);
274
+ return value[0];
275
+ }
276
+ function readTraceHeader(headers, name) {
277
+ const value = readHeader(headers, name);
278
+ if (!(value == null ? void 0 : value.includes(","))) return value;
279
+ const copies = value.split(",").map((copy) => copy.trim());
280
+ if (new Set(copies).size > 1) throw new ConflictingCarrierError(name);
281
+ return copies[0];
282
+ }
283
+ function fromHeaders(headers) {
284
+ var _a, _b;
285
+ let baggage;
286
+ let handoff;
287
+ let traceparent;
288
+ let langsmithTrace;
289
+ try {
290
+ baggage = decodeBaggage(readHeader(headers, BAGGAGE_HEADER));
291
+ handoff = decodeBaggage(readHeader(headers, HANDOFF_HEADER));
292
+ traceparent = readTraceHeader(headers, TRACEPARENT_HEADER);
293
+ langsmithTrace = readTraceHeader(headers, LANGSMITH_TRACE_HEADER);
294
+ } catch (error) {
295
+ if (error instanceof ConflictingCarrierError) return null;
296
+ throw error;
297
+ }
298
+ const langsmithMetadata = baggage[LANGSMITH_METADATA_BAGGAGE_KEY];
299
+ const nested = langsmithMetadata ? safeParseStringRecord(langsmithMetadata) : {};
300
+ for (const [key, value] of Object.entries(nested)) {
301
+ if (key in baggage && baggage[key] !== value) return null;
302
+ }
303
+ const fields = {
304
+ ...baggage,
305
+ ...nested,
306
+ // Last, so it wins: the standard headers may have been rewritten in transit
307
+ // by a propagator, this one is only ever written by us.
308
+ ...handoff
309
+ };
310
+ let traceId;
311
+ let spanId;
312
+ if (traceparent) {
313
+ const [, parsedTraceId, parsedSpanId] = traceparent.split("-");
314
+ if (isW3CTraceId(parsedTraceId) && isW3CSpanId(parsedSpanId)) {
315
+ traceId = parsedTraceId;
316
+ spanId = parsedSpanId;
317
+ }
318
+ }
319
+ if (!traceId && !spanId && langsmithTrace) {
320
+ const segments = langsmithTrace.split(".");
321
+ traceId = (_a = segments[0]) == null ? void 0 : _a.split("Z").pop();
322
+ spanId = (_b = segments[segments.length - 1]) == null ? void 0 : _b.split("Z").pop();
323
+ }
324
+ traceId = fields[BAGGAGE_KEYS.traceId] || traceId;
325
+ spanId = fields[BAGGAGE_KEYS.dispatchSpanId] || spanId;
326
+ const eventId = fields[BAGGAGE_KEYS.eventId];
327
+ if (!traceId || !spanId || !eventId) return null;
328
+ if (!isW3CTraceId(traceId) || !isW3CSpanId(spanId)) {
329
+ warnNonConformantIdsOnce(traceId, spanId);
330
+ }
331
+ const carrier = { traceId, spanId, eventId };
332
+ const childEventId = fields[BAGGAGE_KEYS.childEventId];
333
+ if (childEventId) carrier.childEventId = childEventId;
334
+ const name = fields[BAGGAGE_KEYS.name];
335
+ if (name) carrier.name = name;
336
+ const convoId = fields[BAGGAGE_KEYS.convoId];
337
+ if (convoId) carrier.convoId = convoId;
338
+ const userId = fields[BAGGAGE_KEYS.userId];
339
+ if (userId) carrier.userId = userId;
340
+ return carrier;
341
+ }
342
+ var UNRESOLVED_CARRIER_WARNING = `[raindrop] resume: headers were supplied but no hand-off carrier was found on them (searched ${HANDOFF_HEADER}, ${TRACEPARENT_HEADER}, ${BAGGAGE_HEADER}). This run reports as an UNLINKED event, and the launcher that dispatched it will stay on "queued" forever because nothing ever references its child. Passing headers means a parent handed off, so no carrier on them is almost certainly a defect: a gateway or proxy stripping ${HANDOFF_HEADER}, a middleware overwriting ${BAGGAGE_HEADER}, a request forwarded without its headers, or a hand-built carrier whose field names do not match. Send every header the launcher's dispatch returned. Logged once per process; pass no headers for a directly-invoked sub-agent to silence it.`;
343
+ var UNRESOLVED_CARRIER_WARNED_KEY = /* @__PURE__ */ Symbol.for("raindrop.handoff.warnedUnresolvedCarrier");
344
+ function warnedHolder() {
345
+ return globalThis;
346
+ }
347
+ function resetUnresolvedCarrierWarning() {
348
+ warnedHolder()[UNRESOLVED_CARRIER_WARNED_KEY] = false;
349
+ }
350
+ function warnUnresolvedCarrier(headers, carrier) {
351
+ if (carrier) return;
352
+ if (!headers) return;
353
+ const holder = warnedHolder();
354
+ if (holder[UNRESOLVED_CARRIER_WARNED_KEY]) return;
355
+ holder[UNRESOLVED_CARRIER_WARNED_KEY] = true;
356
+ console.warn(UNRESOLVED_CARRIER_WARNING);
357
+ }
358
+ function safeParseStringRecord(raw) {
359
+ try {
360
+ const parsed = JSON.parse(raw);
361
+ if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) return {};
362
+ const result = {};
363
+ for (const [key, value] of Object.entries(parsed)) {
364
+ if (typeof value === "string") result[key] = value;
365
+ }
366
+ return result;
367
+ } catch (e) {
368
+ return {};
369
+ }
370
+ }
66
371
  function runWithTracingSuppressed(fn) {
67
372
  const hook = globalThis.RAINDROP_SUPPRESS_TRACING;
68
373
  if (typeof hook !== "function") return fn();
@@ -2442,6 +2747,353 @@ function runWithParentToolContext(ctx, fn) {
2442
2747
  return getStorage2().run(ctx, fn);
2443
2748
  }
2444
2749
 
2750
+ // src/internal/subagents.ts
2751
+ var SUBAGENT_CANCELLED_OUTPUT = "Cancelled before producing a result.";
2752
+ var SUBAGENT_ABORTED_OUTPUT = "Aborted before producing a result.";
2753
+ var SUBAGENT_FAILED_OPERATION_ID = SUBAGENT_ABORT_OPERATION_ID;
2754
+ var MAX_SETTLED_EVENT_IDS = 1024;
2755
+ function isTraceCarrier(value) {
2756
+ const candidate = value;
2757
+ return typeof candidate.traceId === "string" && typeof candidate.spanId === "string" && typeof candidate.eventId === "string";
2758
+ }
2759
+ function attrsFrom(metadata) {
2760
+ return Object.entries(withMetadataPrefix(metadata)).map(([key, value]) => attrString(key, value));
2761
+ }
2762
+ function attrsExact(attributes) {
2763
+ return Object.entries(attributes).map(([key, value]) => attrString(key, value));
2764
+ }
2765
+ function handoffSpanAttrs(link) {
2766
+ if (!link) return [];
2767
+ return attrsFrom(detachedChildMetadata(link));
2768
+ }
2769
+ function subagentNameAttrs(name, link) {
2770
+ if (!name || (link == null ? void 0 : link.name)) return [];
2771
+ return attrsFrom({ [HANDOFF_ATTRIBUTE_SUFFIXES.name]: name });
2772
+ }
2773
+ function cancelledSpanAttrs() {
2774
+ return attrsExact(cancelledTerminalAttributes());
2775
+ }
2776
+ function describeAbortReason(reason) {
2777
+ if (reason instanceof Error) return `Aborted: ${reason.message}`;
2778
+ if (typeof reason === "string" && reason.trim().length > 0) return reason;
2779
+ if (reason === void 0 || reason === null) return SUBAGENT_ABORTED_OUTPUT;
2780
+ const serialized = boundedStringify(reason);
2781
+ return serialized ? `Aborted: ${serialized}` : SUBAGENT_ABORTED_OUTPUT;
2782
+ }
2783
+ function stampOpenSpan(span, metadata) {
2784
+ if (span.endTimeUnixNano) return;
2785
+ span.attributes.push(...attrsFrom(metadata));
2786
+ }
2787
+ function stampOpenSpanExact(span, attributes) {
2788
+ if (span.endTimeUnixNano) return;
2789
+ span.attributes.push(...attrsExact(attributes));
2790
+ }
2791
+ var SubagentApi = class {
2792
+ constructor(opts) {
2793
+ var _a;
2794
+ this.opts = opts;
2795
+ this.settledEventIds = (_a = opts.settledEventIds) != null ? _a : /* @__PURE__ */ new Set();
2796
+ }
2797
+ /** True when this child's outcome has already been reported. */
2798
+ wasSettledExplicitly(eventId) {
2799
+ return this.settledEventIds.has(eventId);
2800
+ }
2801
+ /**
2802
+ * Record an outcome reported somewhere other than `cancel()` / `fail()`, so
2803
+ * first-outcome-wins holds across every path that can state one.
2804
+ *
2805
+ * An aborted generation is such a path: an AbortSignal cancellation IS the
2806
+ * run's outcome, and the `fail(err)` that a catch block around the aborted
2807
+ * call naturally reports would otherwise error the child's spans — which is
2808
+ * the only thing separating `failed` from `cancelled`.
2809
+ */
2810
+ noteSettled(eventId) {
2811
+ this.remember(eventId);
2812
+ }
2813
+ remember(eventId) {
2814
+ if (this.settledEventIds.has(eventId)) return;
2815
+ if (this.settledEventIds.size >= MAX_SETTLED_EVENT_IDS) {
2816
+ const oldest = this.settledEventIds.values().next();
2817
+ if (!oldest.done) this.settledEventIds.delete(oldest.value);
2818
+ }
2819
+ this.settledEventIds.add(eventId);
2820
+ }
2821
+ /**
2822
+ * Record a detached sub-agent on this turn: allocate the child's event id,
2823
+ * stamp the dispatch, and return the headers to send with the job.
2824
+ *
2825
+ * This dispatches nothing — you do, moments later, with the returned headers.
2826
+ * Nothing here waits for the child either, or claims to know how it is doing.
2827
+ * Readers derive that from the child's own event.
2828
+ */
2829
+ subagent(options = {}) {
2830
+ var _a;
2831
+ const childEventId = (_a = options.childEventId) != null ? _a : randomUUID();
2832
+ const name = options.name;
2833
+ try {
2834
+ return this.recordLaunch(childEventId, name, options);
2835
+ } catch (err) {
2836
+ if (this.opts.debug) {
2837
+ console.warn(
2838
+ `[raindrop-ai/ai-sdk] sub-agent dispatch not recorded: ${err instanceof Error ? err.message : err}`
2839
+ );
2840
+ }
2841
+ return { childEventId, name, headers: {}, carrier: null };
2842
+ }
2843
+ }
2844
+ recordLaunch(childEventId, name, options) {
2845
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
2846
+ const active = (_a = this.opts.spanSource) == null ? void 0 : _a.activeDispatch();
2847
+ const inherited = getContextManager().getParentSpanIds();
2848
+ const eventId = (_e = (_c = (_b = options.eventId) != null ? _b : active == null ? void 0 : active.eventId) != null ? _c : inherited == null ? void 0 : inherited.eventId) != null ? _e : (_d = this.opts.defaultContext) == null ? void 0 : _d.eventId;
2849
+ const userId = (_h = (_f = options.userId) != null ? _f : active == null ? void 0 : active.userId) != null ? _h : (_g = this.opts.defaultContext) == null ? void 0 : _g.userId;
2850
+ const convoId = (_k = (_i = options.convoId) != null ? _i : active == null ? void 0 : active.convoId) != null ? _k : (_j = this.opts.defaultContext) == null ? void 0 : _j.convoId;
2851
+ if (!eventId) {
2852
+ return { childEventId, name, headers: {}, carrier: null };
2853
+ }
2854
+ const dispatch = detachedDispatchMetadata({ childEventId, name });
2855
+ const toolEvents = toolEventsAllowMetadata();
2856
+ for (const span of (_l = active == null ? void 0 : active.turnSpans) != null ? _l : []) {
2857
+ stampOpenSpan(span, toolEvents);
2858
+ }
2859
+ let dispatchIds;
2860
+ if (active == null ? void 0 : active.dispatchSpan) {
2861
+ stampOpenSpan(active.dispatchSpan, dispatch);
2862
+ dispatchIds = active.dispatchSpan.ids;
2863
+ } else if (this.opts.sendTraces) {
2864
+ dispatchIds = this.synthesizeDispatchSpan({
2865
+ childEventId,
2866
+ name,
2867
+ toolName: options.toolName,
2868
+ eventId,
2869
+ userId,
2870
+ convoId,
2871
+ input: options.input,
2872
+ // No active turn means no telemetry setting to honor: `input` is then
2873
+ // recorded on the explicit say-so of whoever passed it.
2874
+ recordInputs: (_m = active == null ? void 0 : active.recordInputs) != null ? _m : true,
2875
+ dispatch,
2876
+ parent: (inherited == null ? void 0 : inherited.eventId) === eventId ? inherited : void 0
2877
+ }).ids;
2878
+ }
2879
+ const traceId = base64ToHex((_o = (_n = dispatchIds == null ? void 0 : dispatchIds.traceIdB64) != null ? _n : inherited == null ? void 0 : inherited.traceIdB64) != null ? _o : "");
2880
+ const spanId = base64ToHex((_p = dispatchIds == null ? void 0 : dispatchIds.spanIdB64) != null ? _p : "");
2881
+ if (!traceId || !spanId) {
2882
+ if (this.opts.debug) {
2883
+ console.warn(
2884
+ "[raindrop-ai/ai-sdk] sub-agent dispatch recorded without a carrier: no dispatch span to reference (traces disabled?)"
2885
+ );
2886
+ }
2887
+ return { childEventId, name, headers: {}, carrier: null };
2888
+ }
2889
+ const carrier = {
2890
+ traceId,
2891
+ spanId,
2892
+ eventId,
2893
+ childEventId,
2894
+ ...name ? { name } : {},
2895
+ ...convoId ? { convoId } : {},
2896
+ ...userId ? { userId } : {}
2897
+ };
2898
+ return { childEventId, name, headers: toHeaders(carrier), carrier };
2899
+ }
2900
+ /**
2901
+ * Record a dispatch span for a launch with no open tool-call span to decorate:
2902
+ * a launch from outside a tool's `execute`, or from a caller that is not an
2903
+ * LLM turn at all (an HTTP handler that enqueues jobs). Shaped as a tool call
2904
+ * so it reads identically to a model-issued launch.
2905
+ */
2906
+ synthesizeDispatchSpan(args) {
2907
+ var _a;
2908
+ const spanName = (_a = args.toolName) != null ? _a : DEFAULT_DISPATCH_SPAN_NAME;
2909
+ const span = this.opts.traceShipper.startSpan({
2910
+ name: spanName,
2911
+ parent: args.parent,
2912
+ eventId: args.eventId,
2913
+ userId: args.userId,
2914
+ convoId: args.convoId,
2915
+ operationId: "ai.toolCall",
2916
+ attributes: [
2917
+ attrString("operation.name", "ai.toolCall"),
2918
+ attrString("resource.name", spanName),
2919
+ attrString("ai.toolCall.name", spanName),
2920
+ attrString("ai.toolCall.id", `call_${args.childEventId.replace(/-/g, "").slice(0, 12)}`),
2921
+ // Gated like every other input the turn's spans record: a caller that
2922
+ // disabled input capture must not leak the payload through the one
2923
+ // span this API synthesizes on its behalf.
2924
+ ...args.recordInputs ? [attrString("ai.toolCall.args", boundedStringify(args.input))] : [],
2925
+ ...attrsFrom(args.dispatch)
2926
+ ]
2927
+ });
2928
+ this.opts.traceShipper.endSpan(span, {
2929
+ // A dispatch returns a handle, not an answer — the answer is the child's.
2930
+ attributes: [
2931
+ attrString(
2932
+ "ai.toolCall.result",
2933
+ boundedStringify({ jobId: args.childEventId, status: "accepted" })
2934
+ )
2935
+ ]
2936
+ });
2937
+ return span;
2938
+ }
2939
+ /**
2940
+ * Adopt a carrier as the current run's parent reference.
2941
+ *
2942
+ * A missing carrier is a legitimate state — the child was invoked directly
2943
+ * rather than dispatched — so this always returns a usable run and leaves
2944
+ * `parent` null. Call sites stay unconditional.
2945
+ */
2946
+ resume(carrierOrHeaders, options = {}) {
2947
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
2948
+ const carrier = carrierOrHeaders ? isTraceCarrier(carrierOrHeaders) ? carrierOrHeaders : this.readCarrier(carrierOrHeaders) : null;
2949
+ warnUnresolvedCarrier(carrierOrHeaders, carrier);
2950
+ const name = (_a = carrier == null ? void 0 : carrier.name) != null ? _a : options.name;
2951
+ const eventId = (_c = (_b = carrier == null ? void 0 : carrier.childEventId) != null ? _b : options.eventId) != null ? _c : randomUUID();
2952
+ const userId = (_f = (_d = carrier == null ? void 0 : carrier.userId) != null ? _d : options.userId) != null ? _f : (_e = this.opts.defaultContext) == null ? void 0 : _e.userId;
2953
+ const convoId = (_i = (_g = carrier == null ? void 0 : carrier.convoId) != null ? _g : options.convoId) != null ? _i : (_h = this.opts.defaultContext) == null ? void 0 : _h.convoId;
2954
+ const parent = carrier ? {
2955
+ parentEventId: carrier.eventId,
2956
+ ...carrier.spanId ? { parentSpanId: carrier.spanId } : {},
2957
+ ...name ? { name } : {}
2958
+ } : null;
2959
+ return {
2960
+ eventId,
2961
+ name,
2962
+ userId,
2963
+ convoId,
2964
+ parent,
2965
+ metadata: {
2966
+ eventId,
2967
+ ...userId ? { userId } : {},
2968
+ ...convoId ? { convoId } : {},
2969
+ ...parent ? { subagent: parent } : {}
2970
+ },
2971
+ cancel: (reason) => this.settle(eventId, { userId, convoId, name, cancelled: true, output: reason }),
2972
+ fail: (reason) => this.settle(eventId, {
2973
+ userId,
2974
+ convoId,
2975
+ name,
2976
+ handoff: parent != null ? parent : void 0,
2977
+ cancelled: false,
2978
+ output: describeAbortReason(reason),
2979
+ error: reason
2980
+ })
2981
+ };
2982
+ }
2983
+ /**
2984
+ * A carrier arrives from outside this process, so a hostile or merely broken
2985
+ * header bag must cost the job its link, never its run.
2986
+ */
2987
+ readCarrier(headers) {
2988
+ try {
2989
+ return fromHeaders(headers);
2990
+ } catch (err) {
2991
+ if (this.opts.debug) {
2992
+ console.warn(
2993
+ `[raindrop-ai/ai-sdk] sub-agent carrier ignored: ${err instanceof Error ? err.message : err}`
2994
+ );
2995
+ }
2996
+ return null;
2997
+ }
2998
+ }
2999
+ /**
3000
+ * Mark a failed child's own spans as errored.
3001
+ *
3002
+ * Reporting the reason as output is what gets the child an event at all, but
3003
+ * output alone is not enough: status is derived from span shape, and a child
3004
+ * with final output and no error spans derives `finished`. A failed job
3005
+ * reporting success is worse than one reporting nothing, so the failure has
3006
+ * to land on the telemetry too, not just in the text.
3007
+ *
3008
+ * This is the child describing its own run, not a caller writing a status it
3009
+ * cannot observe — the child's own spans are exactly where the contract puts
3010
+ * failure.
3011
+ */
3012
+ recordFailureOnSpans(eventId, args, reason) {
3013
+ var _a, _b, _c;
3014
+ if (!this.opts.sendTraces) return;
3015
+ const failure = (_a = args.error) != null ? _a : reason;
3016
+ let marked = 0;
3017
+ for (const span2 of (_c = (_b = this.opts.spanSource) == null ? void 0 : _b.openSpansForEvent(eventId)) != null ? _c : []) {
3018
+ if (span2.endTimeUnixNano) continue;
3019
+ this.opts.traceShipper.endSpan(span2, { error: failure });
3020
+ marked++;
3021
+ }
3022
+ if (marked > 0) return;
3023
+ const spanName = SUBAGENT_ABORT_SPAN_NAME;
3024
+ const span = this.opts.traceShipper.startSpan({
3025
+ name: spanName,
3026
+ eventId,
3027
+ userId: args.userId,
3028
+ convoId: args.convoId,
3029
+ // Required, not decorative: ingest drops any span carrying no
3030
+ // `ai.operationId` (or traceloop / `gen_ai.*` key) and still answers 200,
3031
+ // so without this the failure disappears silently. The value is
3032
+ // deliberately none of the generate/stream/tool/embed forms, which is
3033
+ // what classifies the span as INTERNAL rather than mislabelling a failed
3034
+ // job as a model call or a tool call.
3035
+ operationId: SUBAGENT_FAILED_OPERATION_ID,
3036
+ // Carries the reverse reference like every other span the child emits:
3037
+ // this is often the child's ONLY span, and a span read on its own is the
3038
+ // whole reason the reference cannot live on the root alone. The name is
3039
+ // stamped even for an unlinked run, which has no caller to reference —
3040
+ // `span_name` no longer says which sub-agent this was, so the attribute
3041
+ // is the only thing that can.
3042
+ attributes: [
3043
+ attrString("resource.name", spanName),
3044
+ ...handoffSpanAttrs(args.handoff),
3045
+ ...subagentNameAttrs(args.name, args.handoff)
3046
+ ]
3047
+ });
3048
+ this.opts.traceShipper.endSpan(span, { error: failure });
3049
+ }
3050
+ async settle(eventId, args) {
3051
+ var _a, _b;
3052
+ if (this.settledEventIds.has(eventId)) {
3053
+ if (this.opts.debug) {
3054
+ console.warn(
3055
+ `[raindrop-ai/ai-sdk] sub-agent ${eventId} already reported an outcome; ignoring the second one`
3056
+ );
3057
+ }
3058
+ return;
3059
+ }
3060
+ this.remember(eventId);
3061
+ const output = args.output && args.output.trim().length > 0 ? args.output : args.cancelled ? SUBAGENT_CANCELLED_OUTPUT : SUBAGENT_ABORTED_OUTPUT;
3062
+ try {
3063
+ if (args.cancelled) {
3064
+ for (const span of (_b = (_a = this.opts.spanSource) == null ? void 0 : _a.openSpansForEvent(eventId)) != null ? _b : []) {
3065
+ stampOpenSpanExact(span, cancelledTerminalAttributes());
3066
+ }
3067
+ } else {
3068
+ this.recordFailureOnSpans(eventId, args, output);
3069
+ }
3070
+ } catch (err) {
3071
+ if (this.opts.debug) {
3072
+ console.warn(
3073
+ `[raindrop-ai/ai-sdk] sub-agent outcome not recorded on spans: ${err instanceof Error ? err.message : err}`
3074
+ );
3075
+ }
3076
+ }
3077
+ if (!this.opts.sendEvents) return;
3078
+ await this.opts.eventShipper.patch(eventId, {
3079
+ userId: args.userId,
3080
+ convoId: args.convoId,
3081
+ // An event is only created from a run that reported something. A child
3082
+ // that dies silently produces no event at all, which pins the caller on
3083
+ // `queued` forever — indistinguishable from a job that never started.
3084
+ output,
3085
+ ...args.cancelled ? { properties: { [HANDOFF_TERMINAL_PROPERTY_KEY]: HANDOFF_TERMINAL_CANCELLED } } : {},
3086
+ isPending: false
3087
+ }).catch((err) => {
3088
+ if (this.opts.debug) {
3089
+ console.warn(
3090
+ `[raindrop-ai/ai-sdk] sub-agent outcome patch failed: ${err instanceof Error ? err.message : err}`
3091
+ );
3092
+ }
3093
+ });
3094
+ }
3095
+ };
3096
+
2445
3097
  // src/internal/usage.ts
2446
3098
  function firstTokenCount(...values) {
2447
3099
  for (const value of values) {
@@ -2634,6 +3286,7 @@ var RaindropTelemetryIntegration = class {
2634
3286
  eventName: (_f = callMeta.eventName) != null ? _f : (_e = this.defaultContext) == null ? void 0 : _e.eventName
2635
3287
  };
2636
3288
  const inherited = getContextManager().getParentSpanIds();
3289
+ const handoff = readDetachedChildLink(metadata);
2637
3290
  const eventIdGenerated = (metadata == null ? void 0 : metadata["raindrop.internal.eventIdGenerated"]) === "true" || (metadata == null ? void 0 : metadata["raindrop.internal.eventIdGenerated"]) === true || (callContextMetadata == null ? void 0 : callContextMetadata.eventIdGenerated) === true;
2638
3291
  const explicitEventId = callMeta.eventId && !eventIdGenerated ? callMeta.eventId : void 0;
2639
3292
  const eventId = (_j = (_i = (_h = explicitEventId != null ? explicitEventId : (_g = this.defaultContext) == null ? void 0 : _g.eventId) != null ? _h : inherited == null ? void 0 : inherited.eventId) != null ? _i : callMeta.eventId) != null ? _j : randomUUID();
@@ -2663,7 +3316,10 @@ var RaindropTelemetryIntegration = class {
2663
3316
  attrString("ai.toolCall.name", subagentName),
2664
3317
  attrString("raindrop.subagent.name", subagentName),
2665
3318
  attrString("raindrop.agent.role", "subagent"),
2666
- ...parentAttrs
3319
+ ...parentAttrs,
3320
+ // Top of this child's tree, so the reverse reference matters as much
3321
+ // here as on the root: these are the spans a reader lands on first.
3322
+ ...handoffSpanAttrs(handoff)
2667
3323
  ]
2668
3324
  });
2669
3325
  const subagentParentRef = this.spanParentRef(subagentToolCallSpan);
@@ -2676,7 +3332,8 @@ var RaindropTelemetryIntegration = class {
2676
3332
  attributes: [
2677
3333
  attrString("operation.name", "agent.subagent"),
2678
3334
  attrString("raindrop.span.kind", "llm_call"),
2679
- attrString("raindrop.subagent.name", subagentName)
3335
+ attrString("raindrop.subagent.name", subagentName),
3336
+ ...handoffSpanAttrs(handoff)
2680
3337
  ]
2681
3338
  });
2682
3339
  this.traceShipper.endSpan(markerSpan);
@@ -2744,7 +3401,9 @@ var RaindropTelemetryIntegration = class {
2744
3401
  inputText: isEmbed ? void 0 : this.extractInputText(event),
2745
3402
  toolCallCount: 0,
2746
3403
  subagentName,
2747
- subagentToolCallSpan
3404
+ subagentToolCallSpan,
3405
+ handoff,
3406
+ nestedInEvent: inheritedParent !== void 0
2748
3407
  });
2749
3408
  };
2750
3409
  // ── onStepStart ─────────────────────────────────────────────────────────
@@ -2789,7 +3448,8 @@ var RaindropTelemetryIntegration = class {
2789
3448
  ...attrsFromModelProvider(event.provider),
2790
3449
  attrString("ai.model.id", event.modelId),
2791
3450
  attrString(MODEL_USAGE_ATTRIBUTES.requestModel, event.modelId),
2792
- ...inputAttrs
3451
+ ...inputAttrs,
3452
+ ...handoffSpanAttrs(state.handoff)
2793
3453
  ]
2794
3454
  });
2795
3455
  state.stepSpan = stepSpan;
@@ -2928,7 +3588,8 @@ var RaindropTelemetryIntegration = class {
2928
3588
  attrString("operation.name", operationName),
2929
3589
  attrString("resource.name", resourceName),
2930
3590
  attrString("ai.telemetry.functionId", state.functionId),
2931
- ...inputAttrs
3591
+ ...inputAttrs,
3592
+ ...handoffSpanAttrs(state.handoff)
2932
3593
  ]
2933
3594
  });
2934
3595
  state.embedSpans.set(event.embedCallId, embedSpan);
@@ -3000,15 +3661,26 @@ var RaindropTelemetryIntegration = class {
3000
3661
  this.traceShipper.endSpan(toolSpan, { error: actualError });
3001
3662
  }
3002
3663
  state.toolSpans.clear();
3664
+ const abortReason = describeAbortReason(actualError);
3003
3665
  if (state.rootSpan) {
3004
- this.traceShipper.endSpan(state.rootSpan, { error: actualError });
3666
+ this.traceShipper.endSpan(state.rootSpan, {
3667
+ error: actualError,
3668
+ attributes: this.abortOutcomeAttrs(state, abortReason, "error")
3669
+ });
3005
3670
  }
3006
3671
  if (state.subagentToolCallSpan) {
3007
3672
  this.traceShipper.endSpan(state.subagentToolCallSpan, { error: actualError });
3008
3673
  }
3009
3674
  const isEmbed = state.operationId === "ai.embed" || state.operationId === "ai.embedMany";
3010
3675
  if (!isEmbed) {
3011
- this.finalizeGenerateEvent(state, state.accumulatedText || void 0, void 0);
3676
+ const errorIsTheRunsOutcome = state.handoff !== void 0 && !state.nestedInEvent;
3677
+ this.finalizeGenerateEvent(
3678
+ state,
3679
+ state.accumulatedText || void 0,
3680
+ void 0,
3681
+ false,
3682
+ errorIsTheRunsOutcome ? { fallbackOutput: abortReason } : void 0
3683
+ );
3012
3684
  }
3013
3685
  this.cleanup(event.callId);
3014
3686
  };
@@ -3045,9 +3717,19 @@ var RaindropTelemetryIntegration = class {
3045
3717
  this.traceShipper.endSpan(toolSpan, { attributes: [abortedAttr] });
3046
3718
  }
3047
3719
  state.toolSpans.clear();
3720
+ const abortIsTheRunsOutcome = state.handoff !== void 0 && !state.nestedInEvent;
3048
3721
  if (state.rootSpan) {
3049
3722
  this.traceShipper.endSpan(state.rootSpan, {
3050
- attributes: [abortedAttr, attrInt("ai.toolCall.count", state.toolCallCount)]
3723
+ attributes: [
3724
+ abortedAttr,
3725
+ attrInt("ai.toolCall.count", state.toolCallCount),
3726
+ // An aborted detached child IS a cancelled sub-agent, and a cancelled
3727
+ // run is span-for-span identical to a finished one — spans close,
3728
+ // nothing errors, there is just no answer. So it is the one outcome
3729
+ // that has to be stated, and the child states it on itself.
3730
+ ...abortIsTheRunsOutcome ? cancelledSpanAttrs() : [],
3731
+ ...abortIsTheRunsOutcome ? this.abortOutcomeAttrs(state, SUBAGENT_CANCELLED_OUTPUT, "stop") : []
3732
+ ]
3051
3733
  });
3052
3734
  }
3053
3735
  if (state.subagentToolCallSpan) {
@@ -3055,7 +3737,17 @@ var RaindropTelemetryIntegration = class {
3055
3737
  attributes: [abortedAttr]
3056
3738
  });
3057
3739
  }
3058
- this.finalizeGenerateEvent(state, state.accumulatedText || void 0, void 0);
3740
+ this.finalizeGenerateEvent(
3741
+ state,
3742
+ state.accumulatedText || void 0,
3743
+ void 0,
3744
+ false,
3745
+ abortIsTheRunsOutcome ? {
3746
+ fallbackOutput: SUBAGENT_CANCELLED_OUTPUT,
3747
+ properties: { [HANDOFF_TERMINAL_PROPERTY_KEY]: HANDOFF_TERMINAL_CANCELLED }
3748
+ } : void 0
3749
+ );
3750
+ if (abortIsTheRunsOutcome) this.subagents.noteSettled(state.eventId);
3059
3751
  this.cleanup(callId);
3060
3752
  };
3061
3753
  // ── executeTool ─────────────────────────────────────────────────────────
@@ -3083,6 +3775,97 @@ var RaindropTelemetryIntegration = class {
3083
3775
  this.subagentWrapping = opts.subagentWrapping !== false;
3084
3776
  this.debug = opts.debug === true;
3085
3777
  this.defaultContext = opts.context;
3778
+ this.subagents = new SubagentApi({
3779
+ traceShipper: this.traceShipper,
3780
+ eventShipper: this.eventShipper,
3781
+ sendTraces: this.sendTraces,
3782
+ sendEvents: this.sendEvents,
3783
+ debug: this.debug,
3784
+ defaultContext: this.defaultContext,
3785
+ settledEventIds: opts.settledEventIds,
3786
+ spanSource: {
3787
+ activeDispatch: () => this.activeDispatch(),
3788
+ openSpansForEvent: (eventId) => this.openSpansForEvent(eventId)
3789
+ }
3790
+ });
3791
+ }
3792
+ /**
3793
+ * Record a detached sub-agent on the turn that is live right now.
3794
+ *
3795
+ * Returns the dispatch — the child's event id and the headers to send with
3796
+ * the job. Dispatching is still yours to do; this only states that you are
3797
+ * about to, so a reader can follow the link before the child exists.
3798
+ */
3799
+ subagent(options = {}) {
3800
+ return this.subagents.subagent(options);
3801
+ }
3802
+ // ── detached sub-agent hand-offs ─────────────────────────────────────────
3803
+ /**
3804
+ * The turn a `subagent()` dispatch is happening inside.
3805
+ *
3806
+ * Resolved from the parent-tool context first, which is the only way to reach
3807
+ * the EXACT span that dispatched: the tool call the model itself issued, still
3808
+ * open while its `execute` runs. Falling back to the event id lets a launch
3809
+ * from elsewhere in the turn (a queue producer called mid-generation) still
3810
+ * find the turn's spans, which is where the tool-events opt-in has to land.
3811
+ */
3812
+ activeDispatch() {
3813
+ var _a;
3814
+ const parentTool = getCurrentParentToolContext();
3815
+ let state = parentTool ? this.getState(parentTool.parentCallId) : void 0;
3816
+ const inheritedEventId = (_a = getContextManager().getParentSpanIds()) == null ? void 0 : _a.eventId;
3817
+ if (!state && inheritedEventId) {
3818
+ for (const candidate of this.callStates.values()) {
3819
+ if (candidate.eventId === inheritedEventId) {
3820
+ state = candidate;
3821
+ break;
3822
+ }
3823
+ }
3824
+ }
3825
+ if (!state) return void 0;
3826
+ return {
3827
+ eventId: state.eventId,
3828
+ userId: state.identity.userId,
3829
+ convoId: state.identity.convoId,
3830
+ dispatchSpan: parentTool ? state.toolSpans.get(parentTool.toolCallId) : void 0,
3831
+ turnSpans: [state.rootSpan, state.stepSpan].filter(
3832
+ (span) => span !== void 0
3833
+ ),
3834
+ recordInputs: state.recordInputs
3835
+ };
3836
+ }
3837
+ /** Spans still open for an event, so a late outcome can still be stated. */
3838
+ openSpansForEvent(eventId) {
3839
+ const spans = [];
3840
+ for (const state of this.callStates.values()) {
3841
+ if (state.eventId !== eventId) continue;
3842
+ if (state.rootSpan) spans.push(state.rootSpan);
3843
+ if (state.stepSpan) spans.push(state.stepSpan);
3844
+ }
3845
+ return spans;
3846
+ }
3847
+ /**
3848
+ * End-attributes that keep a detached child legible when it ends without an
3849
+ * answer — an error, an abort, a cancellation.
3850
+ *
3851
+ * An event is created from an LLM span only when it has a finish reason and
3852
+ * non-empty output, so a child that dies silently produces NO event, and the
3853
+ * caller's hand-off pill stays on `queued` forever — indistinguishable from a
3854
+ * job that never started. Reporting the abort reason as the run's output is
3855
+ * what makes the outcome visible. Only detached children get this: an inline
3856
+ * generation's failure is already legible from its caller's own event.
3857
+ *
3858
+ * And only the run's OUTERMOST generation gets it (see `nestedInEvent`): an
3859
+ * inner sub-call ending badly is not the run ending, and since an event is
3860
+ * created from an LLM span with output, `ai.response.text` here would state
3861
+ * the run's outcome through the span while the turn is still working.
3862
+ */
3863
+ abortOutcomeAttrs(state, reason, finishReason) {
3864
+ if (!state.handoff || state.nestedInEvent) return [];
3865
+ return [
3866
+ attrString("ai.response.text", capText2(state.accumulatedText || reason)),
3867
+ attrString("ai.response.finishReason", finishReason)
3868
+ ];
3086
3869
  }
3087
3870
  // ── helpers ──────────────────────────────────────────────────────────────
3088
3871
  getState(callId) {
@@ -3201,7 +3984,8 @@ var RaindropTelemetryIntegration = class {
3201
3984
  attrString("ai.telemetry.functionId", state.functionId),
3202
3985
  attrString("ai.toolCall.name", toolCall.toolName),
3203
3986
  attrString("ai.toolCall.id", toolCall.toolCallId),
3204
- ...inputAttrs
3987
+ ...inputAttrs,
3988
+ ...handoffSpanAttrs(state.handoff)
3205
3989
  ]
3206
3990
  });
3207
3991
  state.toolSpans.set(toolCall.toolCallId, toolSpan);
@@ -3280,7 +4064,8 @@ var RaindropTelemetryIntegration = class {
3280
4064
  attrString("ai.toolCall.name", call.toolName),
3281
4065
  attrString("ai.toolCall.id", call.toolCallId),
3282
4066
  attrString("ai.toolCall.providerExecuted", "true"),
3283
- ...inputAttrs
4067
+ ...inputAttrs,
4068
+ ...handoffSpanAttrs(state.handoff)
3284
4069
  ]
3285
4070
  });
3286
4071
  state.toolCallCount += 1;
@@ -3361,19 +4146,22 @@ var RaindropTelemetryIntegration = class {
3361
4146
  * is finalized by that resume — otherwise each execution would finalize the
3362
4147
  * same event ID into a separate canonical row.
3363
4148
  */
3364
- finalizeGenerateEvent(state, output, model, keepPending = false) {
4149
+ finalizeGenerateEvent(state, output, model, keepPending = false, detachedOutcome) {
3365
4150
  var _a, _b, _c, _d, _e, _f, _g, _h, _i;
3366
4151
  const suppressSubagentEvent = state.subagentName !== void 0 && state.subagentToolCallSpan !== void 0;
3367
4152
  if (!this.sendEvents || suppressSubagentEvent) return;
4153
+ if (this.subagents.wasSettledExplicitly(state.eventId)) return;
3368
4154
  const callMeta = this.extractRaindropMetadata(state.metadata);
3369
4155
  const userId = (_b = callMeta.userId) != null ? _b : (_a = this.defaultContext) == null ? void 0 : _a.userId;
3370
4156
  if (!userId) return;
3371
4157
  const eventName = (_e = (_d = callMeta.eventName) != null ? _d : (_c = this.defaultContext) == null ? void 0 : _c.eventName) != null ? _e : state.operationId;
3372
4158
  const cappedOutput = capText2(output);
4159
+ const reportedOutput = state.handoff && detachedOutcome && !(cappedOutput == null ? void 0 : cappedOutput.trim()) ? detachedOutcome.fallbackOutput : cappedOutput;
3373
4160
  const input = capText2(state.inputText);
3374
4161
  const properties = {
3375
4162
  ...(_f = this.defaultContext) == null ? void 0 : _f.properties,
3376
- ...callMeta.properties
4163
+ ...callMeta.properties,
4164
+ ...state.handoff ? detachedOutcome == null ? void 0 : detachedOutcome.properties : void 0
3377
4165
  };
3378
4166
  const featureFlags = {
3379
4167
  ...((_g = this.defaultContext) == null ? void 0 : _g.featureFlags) ? normalizeFeatureFlags(this.defaultContext.featureFlags) : {},
@@ -3385,7 +4173,7 @@ var RaindropTelemetryIntegration = class {
3385
4173
  userId,
3386
4174
  convoId,
3387
4175
  input,
3388
- output: cappedOutput,
4176
+ output: reportedOutput,
3389
4177
  model,
3390
4178
  properties: Object.keys(properties).length > 0 ? properties : void 0,
3391
4179
  featureFlags: Object.keys(featureFlags).length > 0 ? featureFlags : void 0,
@@ -4284,6 +5072,7 @@ function wrapAISDK(aiSDK, deps) {
4284
5072
  sendTraces: ((_a = deps.options.send) == null ? void 0 : _a.traces) !== false,
4285
5073
  sendEvents: ((_b = deps.options.send) == null ? void 0 : _b.events) !== false,
4286
5074
  debug,
5075
+ settledEventIds: deps.settledEventIds,
4287
5076
  context: {
4288
5077
  userId: wrapTimeCtx.userId,
4289
5078
  eventId: wrapTimeCtx.eventId,
@@ -5481,7 +6270,7 @@ function mergeAttachments(...groups) {
5481
6270
  // package.json
5482
6271
  var package_default = {
5483
6272
  name: "@raindrop-ai/ai-sdk",
5484
- version: "0.2.1"};
6273
+ version: "0.3.0"};
5485
6274
 
5486
6275
  // src/internal/version.ts
5487
6276
  var libraryName = package_default.name;
@@ -5528,6 +6317,8 @@ function eventMetadata(options) {
5528
6317
  if (options.properties) result["raindrop.properties"] = JSON.stringify(options.properties);
5529
6318
  if (options.featureFlags)
5530
6319
  result["raindrop.featureFlags"] = JSON.stringify(normalizeFeatureFlags(options.featureFlags));
6320
+ if (options.subagent) Object.assign(result, detachedChildMetadata(options.subagent));
6321
+ if (options.toolEvents) result[TOOL_EVENTS_SUFFIX] = options.toolEvents;
5531
6322
  return result;
5532
6323
  }
5533
6324
  function deriveChatTurnMessageId(request) {
@@ -5610,12 +6401,22 @@ function createRaindropAISDK(opts) {
5610
6401
  transformSpan: (_j = opts.traces) == null ? void 0 : _j.transformSpan,
5611
6402
  disableDefaultRedaction: (_k = opts.traces) == null ? void 0 : _k.disableDefaultRedaction
5612
6403
  });
6404
+ const settledEventIds = /* @__PURE__ */ new Set();
6405
+ const subagentApi = new SubagentApi({
6406
+ traceShipper,
6407
+ eventShipper,
6408
+ sendTraces: tracesEnabled,
6409
+ sendEvents: eventsEnabled,
6410
+ debug: envDebug,
6411
+ settledEventIds
6412
+ });
5613
6413
  return {
5614
6414
  wrap(aiSDK, options) {
5615
6415
  return wrapAISDK(aiSDK, {
5616
6416
  options: options != null ? options : {},
5617
6417
  eventShipper,
5618
- traceShipper
6418
+ traceShipper,
6419
+ settledEventIds
5619
6420
  });
5620
6421
  },
5621
6422
  createSelfDiagnosticsTool(options = {}) {
@@ -5652,9 +6453,14 @@ function createRaindropAISDK(opts) {
5652
6453
  sendEvents: eventsEnabled,
5653
6454
  subagentWrapping,
5654
6455
  debug: envDebug,
6456
+ settledEventIds,
5655
6457
  context
5656
6458
  });
5657
6459
  },
6460
+ subagents: subagentApi,
6461
+ subagent(options = {}) {
6462
+ return subagentApi.subagent(options);
6463
+ },
5658
6464
  events: {
5659
6465
  async patch(eventId, patch) {
5660
6466
  await eventShipper.patch(eventId, patch);
@@ -5774,4 +6580,4 @@ function raindrop(options = {}) {
5774
6580
  return client.createTelemetryIntegration({ context, subagentWrapping });
5775
6581
  }
5776
6582
 
5777
- export { DEFAULT_MAX_TEXT_FIELD_CHARS2 as DEFAULT_MAX_TEXT_FIELD_CHARS, DEFAULT_REDACT_ATTRIBUTE_KEYS, DEFAULT_SECRET_KEY_NAMES, REDACTED_PLACEHOLDER, RaindropTelemetryIntegration, TRUNCATION_MARKER2 as TRUNCATION_MARKER, _resetParentToolContextStorage, _resetRaindropCallMetadataStorage, _resetWarnedMissingUserId, boundedStringify, capText2 as capText, clearParentToolContext, createRaindropAISDK, currentSpan, defaultTransformSpan, enterParentToolContext, eventMetadata, eventMetadataFromChatRequest, getContextManager, getCurrentParentToolContext, getCurrentRaindropCallMetadata, normalizeFeatureFlags, raindrop, readRaindropCallMetadataFromArgs, redactJsonAttributeValue, redactSecretsInObject, runWithParentToolContext, runWithRaindropCallMetadata, withCurrent };
6583
+ export { DEFAULT_MAX_TEXT_FIELD_CHARS2 as DEFAULT_MAX_TEXT_FIELD_CHARS, DEFAULT_REDACT_ATTRIBUTE_KEYS, DEFAULT_SECRET_KEY_NAMES, DETACHED_MODE, HANDOFF_ATTRIBUTE_PREFIXES, HANDOFF_ATTRIBUTE_SUFFIXES, HANDOFF_TERMINAL_CANCELLED, HANDOFF_TERMINAL_PROPERTY_KEY, REDACTED_PLACEHOLDER, RaindropTelemetryIntegration, SUBAGENT_ABORTED_OUTPUT, SUBAGENT_AGENT_ROLE, SUBAGENT_CANCELLED_OUTPUT, TOOL_EVENTS_ALLOW, TRUNCATION_MARKER2 as TRUNCATION_MARKER, _resetParentToolContextStorage, _resetRaindropCallMetadataStorage, _resetWarnedMissingUserId, boundedStringify, capText2 as capText, clearParentToolContext, createRaindropAISDK, currentSpan, defaultTransformSpan, enterParentToolContext, eventMetadata, eventMetadataFromChatRequest, fromHeaders, getContextManager, getCurrentParentToolContext, getCurrentRaindropCallMetadata, normalizeFeatureFlags, raindrop, readRaindropCallMetadataFromArgs, redactJsonAttributeValue, redactSecretsInObject, resetUnresolvedCarrierWarning, runWithParentToolContext, runWithRaindropCallMetadata, toHeaders, toLangSmithHeaders, withCurrent };