@raindrop-ai/ai-sdk 0.0.15 → 0.0.17
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.
- package/README.md +6 -6
- package/dist/{chunk-HV5PGIWQ.mjs → chunk-IPXCP2EO.mjs} +283 -241
- package/dist/index.d.mts +8 -54
- package/dist/index.d.ts +8 -54
- package/dist/index.js +283 -241
- package/dist/index.mjs +1 -1
- package/dist/index.node.js +283 -241
- package/dist/index.node.mjs +1 -1
- package/dist/index.workers.js +283 -241
- package/dist/index.workers.mjs +1 -1
- package/package.json +7 -6
package/dist/index.node.js
CHANGED
|
@@ -4,7 +4,61 @@ var async_hooks = require('async_hooks');
|
|
|
4
4
|
|
|
5
5
|
// src/index.node.ts
|
|
6
6
|
|
|
7
|
-
//
|
|
7
|
+
// ../core/dist/chunk-FTBZHS25.js
|
|
8
|
+
function getCrypto() {
|
|
9
|
+
const c = globalThis.crypto;
|
|
10
|
+
return c;
|
|
11
|
+
}
|
|
12
|
+
function randomBytes(length) {
|
|
13
|
+
const cryptoObj = getCrypto();
|
|
14
|
+
const out = new Uint8Array(length);
|
|
15
|
+
if (cryptoObj && typeof cryptoObj.getRandomValues === "function") {
|
|
16
|
+
cryptoObj.getRandomValues(out);
|
|
17
|
+
return out;
|
|
18
|
+
}
|
|
19
|
+
for (let i = 0; i < out.length; i++) out[i] = Math.floor(Math.random() * 256);
|
|
20
|
+
return out;
|
|
21
|
+
}
|
|
22
|
+
function randomUUID() {
|
|
23
|
+
const cryptoObj = getCrypto();
|
|
24
|
+
if (cryptoObj && typeof cryptoObj.randomUUID === "function") {
|
|
25
|
+
return cryptoObj.randomUUID();
|
|
26
|
+
}
|
|
27
|
+
const b = randomBytes(16);
|
|
28
|
+
b[6] = b[6] & 15 | 64;
|
|
29
|
+
b[8] = b[8] & 63 | 128;
|
|
30
|
+
const hex = [...b].map((x) => x.toString(16).padStart(2, "0")).join("");
|
|
31
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
32
|
+
}
|
|
33
|
+
function base64Encode(bytes) {
|
|
34
|
+
const maybeBuffer = globalThis.Buffer;
|
|
35
|
+
if (maybeBuffer) {
|
|
36
|
+
return maybeBuffer.from(bytes).toString("base64");
|
|
37
|
+
}
|
|
38
|
+
let binary = "";
|
|
39
|
+
for (let i2 = 0; i2 < bytes.length; i2++) {
|
|
40
|
+
binary += String.fromCharCode(bytes[i2]);
|
|
41
|
+
}
|
|
42
|
+
const btoaFn = globalThis.btoa;
|
|
43
|
+
if (typeof btoaFn === "function") return btoaFn(binary);
|
|
44
|
+
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
45
|
+
let out = "";
|
|
46
|
+
let i = 0;
|
|
47
|
+
while (i < binary.length) {
|
|
48
|
+
const c1 = binary.charCodeAt(i++) & 255;
|
|
49
|
+
const c2 = i < binary.length ? binary.charCodeAt(i++) & 255 : NaN;
|
|
50
|
+
const c3 = i < binary.length ? binary.charCodeAt(i++) & 255 : NaN;
|
|
51
|
+
const e1 = c1 >> 2;
|
|
52
|
+
const e2 = (c1 & 3) << 4 | (Number.isNaN(c2) ? 0 : c2 >> 4);
|
|
53
|
+
const e3 = Number.isNaN(c2) ? 64 : (c2 & 15) << 2 | (Number.isNaN(c3) ? 0 : c3 >> 6);
|
|
54
|
+
const e4 = Number.isNaN(c3) ? 64 : c3 & 63;
|
|
55
|
+
out += alphabet.charAt(e1);
|
|
56
|
+
out += alphabet.charAt(e2);
|
|
57
|
+
out += e3 === 64 ? "=" : alphabet.charAt(e3);
|
|
58
|
+
out += e4 === 64 ? "=" : alphabet.charAt(e4);
|
|
59
|
+
}
|
|
60
|
+
return out;
|
|
61
|
+
}
|
|
8
62
|
function wait(ms) {
|
|
9
63
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
10
64
|
}
|
|
@@ -17,7 +71,7 @@ function parseRetryAfter(headers) {
|
|
|
17
71
|
const value = (_a = headers.get("Retry-After")) != null ? _a : headers.get("retry-after");
|
|
18
72
|
if (!value) return void 0;
|
|
19
73
|
const asNumber = Number(value);
|
|
20
|
-
if (!Number.isNaN(asNumber)) return asNumber * 1e3;
|
|
74
|
+
if (value.trim() !== "" && !Number.isNaN(asNumber)) return asNumber * 1e3;
|
|
21
75
|
const asDate = new Date(value).getTime();
|
|
22
76
|
if (!Number.isNaN(asDate)) {
|
|
23
77
|
const delta = asDate - Date.now();
|
|
@@ -36,16 +90,19 @@ function getRetryDelayMs(attemptNumber, previousError) {
|
|
|
36
90
|
return base * factor;
|
|
37
91
|
}
|
|
38
92
|
async function withRetry(operation, opName2, opts) {
|
|
93
|
+
const prefix = opts.sdkName ? `[raindrop-ai/${opts.sdkName}]` : "[raindrop-ai/core]";
|
|
39
94
|
let lastError = void 0;
|
|
40
95
|
for (let attemptNumber = 1; attemptNumber <= opts.maxAttempts; attemptNumber++) {
|
|
41
96
|
if (attemptNumber > 1) {
|
|
42
97
|
const delay = getRetryDelayMs(attemptNumber, lastError);
|
|
43
98
|
if (opts.debug) {
|
|
44
|
-
console.warn(
|
|
99
|
+
console.warn(
|
|
100
|
+
`${prefix} ${opName2} retry ${attemptNumber}/${opts.maxAttempts} in ${delay}ms`
|
|
101
|
+
);
|
|
45
102
|
}
|
|
46
103
|
if (delay > 0) await wait(delay);
|
|
47
104
|
} else if (opts.debug) {
|
|
48
|
-
console.log(
|
|
105
|
+
console.log(`${prefix} ${opName2} attempt ${attemptNumber}/${opts.maxAttempts}`);
|
|
49
106
|
}
|
|
50
107
|
try {
|
|
51
108
|
return await operation();
|
|
@@ -54,9 +111,11 @@ async function withRetry(operation, opName2, opts) {
|
|
|
54
111
|
if (opts.debug) {
|
|
55
112
|
const msg = err instanceof Error ? err.message : String(err);
|
|
56
113
|
console.warn(
|
|
57
|
-
|
|
114
|
+
`${prefix} ${opName2} attempt ${attemptNumber} failed: ${msg}${attemptNumber === opts.maxAttempts ? " (no more retries)" : ""}`
|
|
58
115
|
);
|
|
59
116
|
}
|
|
117
|
+
if (lastError && typeof lastError === "object" && "retryable" in lastError && !lastError.retryable)
|
|
118
|
+
break;
|
|
60
119
|
if (attemptNumber === opts.maxAttempts) break;
|
|
61
120
|
}
|
|
62
121
|
}
|
|
@@ -76,9 +135,12 @@ async function postJson(url, body, headers, opts) {
|
|
|
76
135
|
});
|
|
77
136
|
if (!resp.ok) {
|
|
78
137
|
const text = await resp.text().catch(() => "");
|
|
79
|
-
const err = new Error(
|
|
138
|
+
const err = new Error(
|
|
139
|
+
`HTTP ${resp.status} ${resp.statusText}${text ? `: ${text}` : ""}`
|
|
140
|
+
);
|
|
80
141
|
const retryAfterMs = parseRetryAfter(resp.headers);
|
|
81
142
|
if (typeof retryAfterMs === "number") err.retryAfterMs = retryAfterMs;
|
|
143
|
+
err.retryable = resp.status === 429 || resp.status >= 500;
|
|
82
144
|
throw err;
|
|
83
145
|
}
|
|
84
146
|
},
|
|
@@ -86,27 +148,81 @@ async function postJson(url, body, headers, opts) {
|
|
|
86
148
|
opts
|
|
87
149
|
);
|
|
88
150
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
// src/internal/version.ts
|
|
96
|
-
var libraryName = package_default.name;
|
|
97
|
-
var libraryVersion = package_default.version;
|
|
98
|
-
|
|
99
|
-
// src/internal/events.ts
|
|
100
|
-
function getRuntimeContext() {
|
|
101
|
-
const isNode = typeof process !== "undefined" && typeof process.version === "string";
|
|
151
|
+
var SpanStatusCode = {
|
|
152
|
+
ERROR: 2
|
|
153
|
+
};
|
|
154
|
+
function createSpanIds(parent) {
|
|
155
|
+
const traceId = parent ? parent.traceIdB64 : base64Encode(randomBytes(16));
|
|
156
|
+
const spanId = base64Encode(randomBytes(8));
|
|
102
157
|
return {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
158
|
+
traceIdB64: traceId,
|
|
159
|
+
spanIdB64: spanId,
|
|
160
|
+
parentSpanIdB64: parent ? parent.spanIdB64 : void 0
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
function nowUnixNanoString() {
|
|
164
|
+
return Date.now().toString() + "000000";
|
|
165
|
+
}
|
|
166
|
+
function attrString(key, value) {
|
|
167
|
+
if (value === void 0) return void 0;
|
|
168
|
+
return { key, value: { stringValue: value } };
|
|
169
|
+
}
|
|
170
|
+
function attrInt(key, value) {
|
|
171
|
+
if (value === void 0) return void 0;
|
|
172
|
+
if (!Number.isFinite(value)) return void 0;
|
|
173
|
+
return { key, value: { intValue: String(Math.trunc(value)) } };
|
|
174
|
+
}
|
|
175
|
+
function attrDouble(key, value) {
|
|
176
|
+
if (value === void 0) return void 0;
|
|
177
|
+
if (!Number.isFinite(value)) return void 0;
|
|
178
|
+
return { key, value: { doubleValue: value } };
|
|
179
|
+
}
|
|
180
|
+
function attrBool(key, value) {
|
|
181
|
+
if (value === void 0) return void 0;
|
|
182
|
+
return { key, value: { boolValue: value } };
|
|
183
|
+
}
|
|
184
|
+
function attrStringArray(key, values) {
|
|
185
|
+
if (!values || values.length === 0) return void 0;
|
|
186
|
+
return {
|
|
187
|
+
key,
|
|
188
|
+
value: {
|
|
189
|
+
arrayValue: {
|
|
190
|
+
values: values.filter((v) => typeof v === "string").map((v) => ({ stringValue: v }))
|
|
191
|
+
}
|
|
107
192
|
}
|
|
108
193
|
};
|
|
109
194
|
}
|
|
195
|
+
function buildOtlpSpan(args) {
|
|
196
|
+
const attrs = args.attributes.filter((x) => x !== void 0);
|
|
197
|
+
const span = {
|
|
198
|
+
traceId: args.ids.traceIdB64,
|
|
199
|
+
spanId: args.ids.spanIdB64,
|
|
200
|
+
name: args.name,
|
|
201
|
+
startTimeUnixNano: args.startTimeUnixNano,
|
|
202
|
+
endTimeUnixNano: args.endTimeUnixNano
|
|
203
|
+
};
|
|
204
|
+
if (args.ids.parentSpanIdB64) span.parentSpanId = args.ids.parentSpanIdB64;
|
|
205
|
+
if (attrs.length) span.attributes = attrs;
|
|
206
|
+
if (args.status) span.status = args.status;
|
|
207
|
+
return span;
|
|
208
|
+
}
|
|
209
|
+
function buildExportTraceServiceRequest(spans, serviceName = "raindrop.core", serviceVersion = "0.0.0") {
|
|
210
|
+
return {
|
|
211
|
+
resourceSpans: [
|
|
212
|
+
{
|
|
213
|
+
resource: {
|
|
214
|
+
attributes: [{ key: "service.name", value: { stringValue: serviceName } }]
|
|
215
|
+
},
|
|
216
|
+
scopeSpans: [
|
|
217
|
+
{
|
|
218
|
+
scope: { name: serviceName, version: serviceVersion },
|
|
219
|
+
spans
|
|
220
|
+
}
|
|
221
|
+
]
|
|
222
|
+
}
|
|
223
|
+
]
|
|
224
|
+
};
|
|
225
|
+
}
|
|
110
226
|
function mergePatches(target, source) {
|
|
111
227
|
var _a, _b, _c, _d;
|
|
112
228
|
const out = { ...target, ...source };
|
|
@@ -124,26 +240,39 @@ var EventShipper = class {
|
|
|
124
240
|
this.sticky = /* @__PURE__ */ new Map();
|
|
125
241
|
this.timers = /* @__PURE__ */ new Map();
|
|
126
242
|
this.inFlight = /* @__PURE__ */ new Set();
|
|
127
|
-
var _a, _b, _c;
|
|
128
|
-
|
|
129
|
-
throw new Error("[raindrop-ai/ai-sdk] writeKey is required when events are enabled");
|
|
130
|
-
}
|
|
131
|
-
this.writeKey = (_a = opts.writeKey) != null ? _a : "";
|
|
243
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
244
|
+
this.writeKey = (_a = opts.writeKey) == null ? void 0 : _a.trim();
|
|
132
245
|
this.baseUrl = (_b = formatEndpoint(opts.endpoint)) != null ? _b : "https://api.raindrop.ai/v1/";
|
|
133
|
-
this.enabled = opts.enabled;
|
|
246
|
+
this.enabled = opts.enabled !== false;
|
|
134
247
|
this.debug = opts.debug;
|
|
135
248
|
this.partialFlushMs = (_c = opts.partialFlushMs) != null ? _c : 1e3;
|
|
136
|
-
this.
|
|
249
|
+
this.sdkName = (_d = opts.sdkName) != null ? _d : "core";
|
|
250
|
+
this.prefix = `[raindrop-ai/${this.sdkName}]`;
|
|
251
|
+
this.defaultEventName = (_e = opts.defaultEventName) != null ? _e : "ai_generation";
|
|
252
|
+
const isNode = typeof process !== "undefined" && typeof process.version === "string";
|
|
253
|
+
this.context = {
|
|
254
|
+
library: {
|
|
255
|
+
name: (_f = opts.libraryName) != null ? _f : "@raindrop-ai/core",
|
|
256
|
+
version: (_g = opts.libraryVersion) != null ? _g : "0.0.0"
|
|
257
|
+
},
|
|
258
|
+
metadata: {
|
|
259
|
+
jsRuntime: isNode ? "node" : "web",
|
|
260
|
+
...isNode ? { nodeVersion: process.version } : {}
|
|
261
|
+
}
|
|
262
|
+
};
|
|
137
263
|
}
|
|
138
264
|
isDebugEnabled() {
|
|
139
265
|
return this.debug;
|
|
140
266
|
}
|
|
267
|
+
authHeaders() {
|
|
268
|
+
return this.writeKey ? { Authorization: `Bearer ${this.writeKey}` } : {};
|
|
269
|
+
}
|
|
141
270
|
async patch(eventId, patch) {
|
|
142
271
|
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
|
|
143
272
|
if (!this.enabled) return;
|
|
144
|
-
if (!eventId) return;
|
|
273
|
+
if (!eventId || !eventId.trim()) return;
|
|
145
274
|
if (this.debug) {
|
|
146
|
-
console.log(
|
|
275
|
+
console.log(`${this.prefix} queue patch`, {
|
|
147
276
|
eventId,
|
|
148
277
|
userId: patch.userId,
|
|
149
278
|
eventName: patch.eventName,
|
|
@@ -211,26 +340,23 @@ var EventShipper = class {
|
|
|
211
340
|
];
|
|
212
341
|
const url = `${this.baseUrl}signals/track`;
|
|
213
342
|
try {
|
|
214
|
-
await postJson(
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
);
|
|
343
|
+
await postJson(url, body, this.authHeaders(), {
|
|
344
|
+
maxAttempts: 3,
|
|
345
|
+
debug: this.debug,
|
|
346
|
+
sdkName: this.sdkName
|
|
347
|
+
});
|
|
220
348
|
} catch (err) {
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
console.warn(`[raindrop-ai/ai-sdk] failed to send signal (dropping): ${msg}`);
|
|
224
|
-
}
|
|
349
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
350
|
+
console.warn(`${this.prefix} failed to send signal (dropping): ${msg}`);
|
|
225
351
|
}
|
|
226
352
|
}
|
|
227
353
|
async identify(users) {
|
|
228
354
|
if (!this.enabled) return;
|
|
229
355
|
const list = Array.isArray(users) ? users : [users];
|
|
230
356
|
const body = list.filter((user) => {
|
|
231
|
-
if (!(user == null ? void 0 : user.userId)) {
|
|
357
|
+
if (!(user == null ? void 0 : user.userId) || !user.userId.trim()) {
|
|
232
358
|
if (this.debug) {
|
|
233
|
-
console.warn(
|
|
359
|
+
console.warn(`${this.prefix} skipping identify: missing userId`);
|
|
234
360
|
}
|
|
235
361
|
return false;
|
|
236
362
|
}
|
|
@@ -245,17 +371,14 @@ var EventShipper = class {
|
|
|
245
371
|
if (body.length === 0) return;
|
|
246
372
|
const url = `${this.baseUrl}users/identify`;
|
|
247
373
|
try {
|
|
248
|
-
await postJson(
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
);
|
|
374
|
+
await postJson(url, body, this.authHeaders(), {
|
|
375
|
+
maxAttempts: 3,
|
|
376
|
+
debug: this.debug,
|
|
377
|
+
sdkName: this.sdkName
|
|
378
|
+
});
|
|
254
379
|
} catch (err) {
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
console.warn(`[raindrop-ai/ai-sdk] failed to send identify (dropping): ${msg}`);
|
|
258
|
-
}
|
|
380
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
381
|
+
console.warn(`${this.prefix} failed to send identify (dropping): ${msg}`);
|
|
259
382
|
}
|
|
260
383
|
}
|
|
261
384
|
async flushOne(eventId) {
|
|
@@ -270,12 +393,13 @@ var EventShipper = class {
|
|
|
270
393
|
this.buffers.delete(eventId);
|
|
271
394
|
if (!accumulated) return;
|
|
272
395
|
const sticky = (_a = this.sticky.get(eventId)) != null ? _a : {};
|
|
273
|
-
const eventName = (_c = (_b = accumulated.eventName) != null ? _b : sticky.eventName) != null ? _c :
|
|
396
|
+
const eventName = (_c = (_b = accumulated.eventName) != null ? _b : sticky.eventName) != null ? _c : this.defaultEventName;
|
|
274
397
|
const userId = (_d = accumulated.userId) != null ? _d : sticky.userId;
|
|
275
398
|
if (!userId) {
|
|
276
399
|
if (this.debug) {
|
|
277
|
-
console.warn(
|
|
400
|
+
console.warn(`${this.prefix} skipping track_partial for ${eventId}: missing userId`);
|
|
278
401
|
}
|
|
402
|
+
this.sticky.delete(eventId);
|
|
279
403
|
return;
|
|
280
404
|
}
|
|
281
405
|
const { wizardSession, ...restProperties } = (_e = accumulated.properties) != null ? _e : {};
|
|
@@ -300,7 +424,7 @@ var EventShipper = class {
|
|
|
300
424
|
};
|
|
301
425
|
const url = `${this.baseUrl}events/track_partial`;
|
|
302
426
|
if (this.debug) {
|
|
303
|
-
console.log(
|
|
427
|
+
console.log(`${this.prefix} sending track_partial`, {
|
|
304
428
|
eventId,
|
|
305
429
|
eventName,
|
|
306
430
|
userId,
|
|
@@ -317,24 +441,21 @@ var EventShipper = class {
|
|
|
317
441
|
endpoint: url
|
|
318
442
|
});
|
|
319
443
|
}
|
|
320
|
-
const p = postJson(
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
);
|
|
444
|
+
const p = postJson(url, payload, this.authHeaders(), {
|
|
445
|
+
maxAttempts: 3,
|
|
446
|
+
debug: this.debug,
|
|
447
|
+
sdkName: this.sdkName
|
|
448
|
+
});
|
|
326
449
|
this.inFlight.add(p);
|
|
327
450
|
try {
|
|
328
451
|
try {
|
|
329
452
|
await p;
|
|
330
453
|
if (this.debug) {
|
|
331
|
-
console.log(
|
|
454
|
+
console.log(`${this.prefix} sent track_partial ${eventId} (${eventName})`);
|
|
332
455
|
}
|
|
333
456
|
} catch (err) {
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
console.warn(`[raindrop-ai/ai-sdk] failed to send track_partial (dropping): ${msg}`);
|
|
337
|
-
}
|
|
457
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
458
|
+
console.warn(`${this.prefix} failed to send track_partial (dropping): ${msg}`);
|
|
338
459
|
}
|
|
339
460
|
} finally {
|
|
340
461
|
this.inFlight.delete(p);
|
|
@@ -345,182 +466,50 @@ var EventShipper = class {
|
|
|
345
466
|
}
|
|
346
467
|
}
|
|
347
468
|
};
|
|
348
|
-
|
|
349
|
-
// src/internal/ids.ts
|
|
350
|
-
function getCrypto() {
|
|
351
|
-
const c = globalThis.crypto;
|
|
352
|
-
return c;
|
|
353
|
-
}
|
|
354
|
-
function randomBytes(length) {
|
|
355
|
-
const cryptoObj = getCrypto();
|
|
356
|
-
const out = new Uint8Array(length);
|
|
357
|
-
if (cryptoObj && typeof cryptoObj.getRandomValues === "function") {
|
|
358
|
-
cryptoObj.getRandomValues(out);
|
|
359
|
-
return out;
|
|
360
|
-
}
|
|
361
|
-
for (let i = 0; i < out.length; i++) out[i] = Math.floor(Math.random() * 256);
|
|
362
|
-
return out;
|
|
363
|
-
}
|
|
364
|
-
function randomUUID() {
|
|
365
|
-
const cryptoObj = getCrypto();
|
|
366
|
-
if (cryptoObj && typeof cryptoObj.randomUUID === "function") {
|
|
367
|
-
return cryptoObj.randomUUID();
|
|
368
|
-
}
|
|
369
|
-
const b = randomBytes(16);
|
|
370
|
-
b[6] = b[6] & 15 | 64;
|
|
371
|
-
b[8] = b[8] & 63 | 128;
|
|
372
|
-
const hex = [...b].map((x) => x.toString(16).padStart(2, "0")).join("");
|
|
373
|
-
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
374
|
-
}
|
|
375
|
-
function base64Encode(bytes) {
|
|
376
|
-
const maybeBuffer = globalThis.Buffer;
|
|
377
|
-
if (maybeBuffer) {
|
|
378
|
-
return maybeBuffer.from(bytes).toString("base64");
|
|
379
|
-
}
|
|
380
|
-
let binary = "";
|
|
381
|
-
for (let i2 = 0; i2 < bytes.length; i2++) {
|
|
382
|
-
binary += String.fromCharCode(bytes[i2]);
|
|
383
|
-
}
|
|
384
|
-
const btoaFn = globalThis.btoa;
|
|
385
|
-
if (typeof btoaFn === "function") return btoaFn(binary);
|
|
386
|
-
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
387
|
-
let out = "";
|
|
388
|
-
let i = 0;
|
|
389
|
-
while (i < binary.length) {
|
|
390
|
-
const c1 = binary.charCodeAt(i++) & 255;
|
|
391
|
-
const c2 = i < binary.length ? binary.charCodeAt(i++) & 255 : NaN;
|
|
392
|
-
const c3 = i < binary.length ? binary.charCodeAt(i++) & 255 : NaN;
|
|
393
|
-
const e1 = c1 >> 2;
|
|
394
|
-
const e2 = (c1 & 3) << 4 | (Number.isNaN(c2) ? 0 : c2 >> 4);
|
|
395
|
-
const e3 = Number.isNaN(c2) ? 64 : (c2 & 15) << 2 | (Number.isNaN(c3) ? 0 : c3 >> 6);
|
|
396
|
-
const e4 = Number.isNaN(c3) ? 64 : c3 & 63;
|
|
397
|
-
out += alphabet.charAt(e1);
|
|
398
|
-
out += alphabet.charAt(e2);
|
|
399
|
-
out += e3 === 64 ? "=" : alphabet.charAt(e3);
|
|
400
|
-
out += e4 === 64 ? "=" : alphabet.charAt(e4);
|
|
401
|
-
}
|
|
402
|
-
return out;
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
// src/internal/otlp.ts
|
|
406
|
-
var SpanStatusCode = {
|
|
407
|
-
ERROR: 2
|
|
408
|
-
};
|
|
409
|
-
function createSpanIds(parent) {
|
|
410
|
-
const traceId = parent ? parent.traceIdB64 : base64Encode(randomBytes(16));
|
|
411
|
-
const spanId = base64Encode(randomBytes(8));
|
|
412
|
-
return {
|
|
413
|
-
traceIdB64: traceId,
|
|
414
|
-
spanIdB64: spanId,
|
|
415
|
-
parentSpanIdB64: parent ? parent.spanIdB64 : void 0
|
|
416
|
-
};
|
|
417
|
-
}
|
|
418
|
-
function nowUnixNanoString() {
|
|
419
|
-
return String(Date.now() * 1e6);
|
|
420
|
-
}
|
|
421
|
-
function attrString(key, value) {
|
|
422
|
-
if (value === void 0) return void 0;
|
|
423
|
-
return { key, value: { stringValue: value } };
|
|
424
|
-
}
|
|
425
|
-
function attrInt(key, value) {
|
|
426
|
-
if (value === void 0) return void 0;
|
|
427
|
-
if (!Number.isFinite(value)) return void 0;
|
|
428
|
-
return { key, value: { intValue: String(Math.trunc(value)) } };
|
|
429
|
-
}
|
|
430
|
-
function attrDouble(key, value) {
|
|
431
|
-
if (value === void 0) return void 0;
|
|
432
|
-
if (!Number.isFinite(value)) return void 0;
|
|
433
|
-
return { key, value: { doubleValue: value } };
|
|
434
|
-
}
|
|
435
|
-
function attrBool(key, value) {
|
|
436
|
-
if (value === void 0) return void 0;
|
|
437
|
-
return { key, value: { boolValue: value } };
|
|
438
|
-
}
|
|
439
|
-
function attrStringArray(key, values) {
|
|
440
|
-
if (!values || values.length === 0) return void 0;
|
|
441
|
-
return {
|
|
442
|
-
key,
|
|
443
|
-
value: {
|
|
444
|
-
arrayValue: {
|
|
445
|
-
values: values.filter((v) => typeof v === "string").map((v) => ({ stringValue: v }))
|
|
446
|
-
}
|
|
447
|
-
}
|
|
448
|
-
};
|
|
449
|
-
}
|
|
450
|
-
function buildOtlpSpan(args) {
|
|
451
|
-
const attrs = args.attributes.filter((x) => x !== void 0);
|
|
452
|
-
const span = {
|
|
453
|
-
traceId: args.ids.traceIdB64,
|
|
454
|
-
spanId: args.ids.spanIdB64,
|
|
455
|
-
name: args.name,
|
|
456
|
-
startTimeUnixNano: args.startTimeUnixNano,
|
|
457
|
-
endTimeUnixNano: args.endTimeUnixNano
|
|
458
|
-
};
|
|
459
|
-
if (args.ids.parentSpanIdB64) span.parentSpanId = args.ids.parentSpanIdB64;
|
|
460
|
-
if (attrs.length) span.attributes = attrs;
|
|
461
|
-
if (args.status) span.status = args.status;
|
|
462
|
-
return span;
|
|
463
|
-
}
|
|
464
|
-
function buildExportTraceServiceRequest(spans) {
|
|
465
|
-
return {
|
|
466
|
-
resourceSpans: [
|
|
467
|
-
{
|
|
468
|
-
resource: {
|
|
469
|
-
attributes: [{ key: "service.name", value: { stringValue: "raindrop.ai-sdk" } }]
|
|
470
|
-
},
|
|
471
|
-
scopeSpans: [
|
|
472
|
-
{
|
|
473
|
-
scope: { name: "raindrop.ai-sdk", version: libraryVersion },
|
|
474
|
-
spans
|
|
475
|
-
}
|
|
476
|
-
]
|
|
477
|
-
}
|
|
478
|
-
]
|
|
479
|
-
};
|
|
480
|
-
}
|
|
481
|
-
|
|
482
|
-
// src/internal/traces.ts
|
|
483
469
|
var TraceShipper = class {
|
|
484
470
|
constructor(opts) {
|
|
485
471
|
this.queue = [];
|
|
486
472
|
this.inFlight = /* @__PURE__ */ new Set();
|
|
487
|
-
var _a, _b, _c, _d, _e;
|
|
488
|
-
|
|
489
|
-
throw new Error("[raindrop-ai/ai-sdk] writeKey is required when traces are enabled");
|
|
490
|
-
}
|
|
491
|
-
this.writeKey = (_a = opts.writeKey) != null ? _a : "";
|
|
473
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
474
|
+
this.writeKey = (_a = opts.writeKey) == null ? void 0 : _a.trim();
|
|
492
475
|
this.baseUrl = (_b = formatEndpoint(opts.endpoint)) != null ? _b : "https://api.raindrop.ai/v1/";
|
|
493
|
-
this.enabled = opts.enabled;
|
|
476
|
+
this.enabled = opts.enabled !== false;
|
|
494
477
|
this.debug = opts.debug;
|
|
495
478
|
this.debugSpans = opts.debugSpans === true;
|
|
496
479
|
this.flushIntervalMs = (_c = opts.flushIntervalMs) != null ? _c : 1e3;
|
|
497
480
|
this.maxBatchSize = (_d = opts.maxBatchSize) != null ? _d : 50;
|
|
498
481
|
this.maxQueueSize = (_e = opts.maxQueueSize) != null ? _e : 5e3;
|
|
482
|
+
this.sdkName = (_f = opts.sdkName) != null ? _f : "core";
|
|
483
|
+
this.prefix = `[raindrop-ai/${this.sdkName}]`;
|
|
484
|
+
this.serviceName = (_g = opts.serviceName) != null ? _g : "raindrop.core";
|
|
485
|
+
this.serviceVersion = (_h = opts.serviceVersion) != null ? _h : "0.0.0";
|
|
499
486
|
}
|
|
500
487
|
isDebugEnabled() {
|
|
501
488
|
return this.debug;
|
|
502
489
|
}
|
|
490
|
+
authHeaders() {
|
|
491
|
+
return this.writeKey ? { Authorization: `Bearer ${this.writeKey}` } : {};
|
|
492
|
+
}
|
|
503
493
|
startSpan(args) {
|
|
504
|
-
var _a;
|
|
494
|
+
var _a, _b;
|
|
505
495
|
const ids = createSpanIds(args.parent);
|
|
506
|
-
const started = nowUnixNanoString();
|
|
496
|
+
const started = (_a = args.startTimeUnixNano) != null ? _a : nowUnixNanoString();
|
|
507
497
|
const attrs = [
|
|
508
498
|
attrString("ai.telemetry.metadata.raindrop.eventId", args.eventId),
|
|
509
|
-
// Important: omit userId to avoid backend spans->events duplication
|
|
510
499
|
attrString("ai.operationId", args.operationId)
|
|
511
500
|
];
|
|
512
|
-
if ((
|
|
501
|
+
if ((_b = args.attributes) == null ? void 0 : _b.length) attrs.push(...args.attributes);
|
|
513
502
|
return { ids, name: args.name, startTimeUnixNano: started, attributes: attrs };
|
|
514
503
|
}
|
|
515
504
|
endSpan(span, extra) {
|
|
516
|
-
var _a;
|
|
505
|
+
var _a, _b;
|
|
517
506
|
if (span.endTimeUnixNano) return;
|
|
518
|
-
span.endTimeUnixNano = nowUnixNanoString();
|
|
519
|
-
if ((
|
|
507
|
+
span.endTimeUnixNano = (_a = extra == null ? void 0 : extra.endTimeUnixNano) != null ? _a : nowUnixNanoString();
|
|
508
|
+
if ((_b = extra == null ? void 0 : extra.attributes) == null ? void 0 : _b.length) {
|
|
520
509
|
span.attributes.push(...extra.attributes);
|
|
521
510
|
}
|
|
522
|
-
let status;
|
|
523
|
-
if ((extra == null ? void 0 : extra.error) !== void 0) {
|
|
511
|
+
let status = extra == null ? void 0 : extra.status;
|
|
512
|
+
if (!status && (extra == null ? void 0 : extra.error) !== void 0) {
|
|
524
513
|
const message = extra.error instanceof Error ? extra.error.message : String(extra.error);
|
|
525
514
|
status = { code: SpanStatusCode.ERROR, message };
|
|
526
515
|
}
|
|
@@ -534,12 +523,29 @@ var TraceShipper = class {
|
|
|
534
523
|
});
|
|
535
524
|
this.enqueue(otlp);
|
|
536
525
|
}
|
|
526
|
+
createSpan(args) {
|
|
527
|
+
var _a;
|
|
528
|
+
const ids = createSpanIds(args.parent);
|
|
529
|
+
const attrs = [
|
|
530
|
+
attrString("ai.telemetry.metadata.raindrop.eventId", args.eventId)
|
|
531
|
+
];
|
|
532
|
+
if ((_a = args.attributes) == null ? void 0 : _a.length) attrs.push(...args.attributes);
|
|
533
|
+
const otlp = buildOtlpSpan({
|
|
534
|
+
ids,
|
|
535
|
+
name: args.name,
|
|
536
|
+
startTimeUnixNano: args.startTimeUnixNano,
|
|
537
|
+
endTimeUnixNano: args.endTimeUnixNano,
|
|
538
|
+
attributes: attrs,
|
|
539
|
+
status: args.status
|
|
540
|
+
});
|
|
541
|
+
this.enqueue(otlp);
|
|
542
|
+
}
|
|
537
543
|
enqueue(span) {
|
|
538
544
|
if (!this.enabled) return;
|
|
539
545
|
if (this.debugSpans) {
|
|
540
546
|
const short = (s) => s ? s.slice(-8) : "none";
|
|
541
547
|
console.log(
|
|
542
|
-
|
|
548
|
+
`${this.prefix}[span] name=${span.name} trace=${short(span.traceId)} span=${short(span.spanId)} parent=${short(
|
|
543
549
|
span.parentSpanId
|
|
544
550
|
)}`
|
|
545
551
|
);
|
|
@@ -569,30 +575,27 @@ var TraceShipper = class {
|
|
|
569
575
|
}
|
|
570
576
|
while (this.queue.length > 0) {
|
|
571
577
|
const batch = this.queue.splice(0, this.maxBatchSize);
|
|
572
|
-
const body = buildExportTraceServiceRequest(batch);
|
|
578
|
+
const body = buildExportTraceServiceRequest(batch, this.serviceName, this.serviceVersion);
|
|
573
579
|
const url = `${this.baseUrl}traces`;
|
|
574
580
|
if (this.debug) {
|
|
575
|
-
console.log(
|
|
581
|
+
console.log(`${this.prefix} sending traces batch`, {
|
|
576
582
|
spans: batch.length,
|
|
577
583
|
endpoint: url
|
|
578
584
|
});
|
|
579
585
|
}
|
|
580
|
-
const p = postJson(
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
);
|
|
586
|
+
const p = postJson(url, body, this.authHeaders(), {
|
|
587
|
+
maxAttempts: 3,
|
|
588
|
+
debug: this.debug,
|
|
589
|
+
sdkName: this.sdkName
|
|
590
|
+
});
|
|
586
591
|
this.inFlight.add(p);
|
|
587
592
|
try {
|
|
588
593
|
try {
|
|
589
594
|
await p;
|
|
590
|
-
if (this.debug) console.log(
|
|
595
|
+
if (this.debug) console.log(`${this.prefix} sent ${batch.length} spans`);
|
|
591
596
|
} catch (err) {
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
console.warn(`[raindrop-ai/ai-sdk] failed to send spans (dropping): ${msg}`);
|
|
595
|
-
}
|
|
597
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
598
|
+
console.warn(`${this.prefix} failed to send ${batch.length} spans: ${msg}`);
|
|
596
599
|
}
|
|
597
600
|
} finally {
|
|
598
601
|
this.inFlight.delete(p);
|
|
@@ -609,8 +612,6 @@ var TraceShipper = class {
|
|
|
609
612
|
})));
|
|
610
613
|
}
|
|
611
614
|
};
|
|
612
|
-
|
|
613
|
-
// src/internal/spanContext.ts
|
|
614
615
|
var NOOP_SPAN = {
|
|
615
616
|
traceIdB64: "",
|
|
616
617
|
spanIdB64: "",
|
|
@@ -784,6 +785,41 @@ async function* asyncGeneratorWithCurrent(span, gen) {
|
|
|
784
785
|
}
|
|
785
786
|
}
|
|
786
787
|
|
|
788
|
+
// package.json
|
|
789
|
+
var package_default = {
|
|
790
|
+
name: "@raindrop-ai/ai-sdk",
|
|
791
|
+
version: "0.0.17"};
|
|
792
|
+
|
|
793
|
+
// src/internal/version.ts
|
|
794
|
+
var libraryName = package_default.name;
|
|
795
|
+
var libraryVersion = package_default.version;
|
|
796
|
+
|
|
797
|
+
// src/internal/events.ts
|
|
798
|
+
var EventShipper2 = class extends EventShipper {
|
|
799
|
+
constructor(opts) {
|
|
800
|
+
var _a, _b, _c;
|
|
801
|
+
super({
|
|
802
|
+
...opts,
|
|
803
|
+
sdkName: (_a = opts.sdkName) != null ? _a : "ai-sdk",
|
|
804
|
+
libraryName: (_b = opts.libraryName) != null ? _b : libraryName,
|
|
805
|
+
libraryVersion: (_c = opts.libraryVersion) != null ? _c : libraryVersion
|
|
806
|
+
});
|
|
807
|
+
}
|
|
808
|
+
};
|
|
809
|
+
|
|
810
|
+
// src/internal/traces.ts
|
|
811
|
+
var TraceShipper2 = class extends TraceShipper {
|
|
812
|
+
constructor(opts) {
|
|
813
|
+
var _a, _b, _c;
|
|
814
|
+
super({
|
|
815
|
+
...opts,
|
|
816
|
+
sdkName: (_a = opts.sdkName) != null ? _a : "ai-sdk",
|
|
817
|
+
serviceName: (_b = opts.serviceName) != null ? _b : "raindrop.ai-sdk",
|
|
818
|
+
serviceVersion: (_c = opts.serviceVersion) != null ? _c : libraryVersion
|
|
819
|
+
});
|
|
820
|
+
}
|
|
821
|
+
};
|
|
822
|
+
|
|
787
823
|
// src/internal/wrap/helpers.ts
|
|
788
824
|
function isRecord(value) {
|
|
789
825
|
return typeof value === "object" && value !== null;
|
|
@@ -1903,7 +1939,7 @@ function wrapAISDK(aiSDK, deps) {
|
|
|
1903
1939
|
"generateObject",
|
|
1904
1940
|
"streamObject"
|
|
1905
1941
|
]);
|
|
1906
|
-
const agentClasses = /* @__PURE__ */ new Set(["ToolLoopAgent"]);
|
|
1942
|
+
const agentClasses = /* @__PURE__ */ new Set(["Agent", "Experimental_Agent", "ToolLoopAgent"]);
|
|
1907
1943
|
const sendEvents = ((_a = deps.options.send) == null ? void 0 : _a.events) !== false;
|
|
1908
1944
|
const sendTraces = ((_b = deps.options.send) == null ? void 0 : _b.traces) !== false;
|
|
1909
1945
|
const autoAttachmentEnabled = deps.options.autoAttachment !== false;
|
|
@@ -2663,10 +2699,16 @@ function wrapModel(args, aiSDK, outerOperationId, ctx) {
|
|
|
2663
2699
|
if (firstChunkMs === void 0) firstChunkMs = Date.now();
|
|
2664
2700
|
if (isRecord(value)) {
|
|
2665
2701
|
const type = value["type"];
|
|
2666
|
-
if (type === "text-delta"
|
|
2667
|
-
|
|
2668
|
-
|
|
2669
|
-
|
|
2702
|
+
if (type === "text-delta") {
|
|
2703
|
+
let textDelta;
|
|
2704
|
+
if (typeof value["delta"] === "string") {
|
|
2705
|
+
textDelta = value["delta"];
|
|
2706
|
+
} else if (typeof value["textDelta"] === "string") {
|
|
2707
|
+
textDelta = value["textDelta"];
|
|
2708
|
+
}
|
|
2709
|
+
if (typeof textDelta === "string") activeText += textDelta;
|
|
2710
|
+
}
|
|
2711
|
+
if (type === "finish") finishReason = extractFinishReason(value);
|
|
2670
2712
|
if (type === "tool-call") toolCallsLocal.push(value);
|
|
2671
2713
|
if ("response" in value && isRecord(value["response"])) {
|
|
2672
2714
|
const response = value["response"];
|
|
@@ -2907,14 +2949,14 @@ function createRaindropAISDK(opts) {
|
|
|
2907
2949
|
"[raindrop-ai/ai-sdk] writeKey not provided; telemetry shipping is disabled"
|
|
2908
2950
|
);
|
|
2909
2951
|
}
|
|
2910
|
-
const eventShipper = new
|
|
2952
|
+
const eventShipper = new EventShipper2({
|
|
2911
2953
|
writeKey,
|
|
2912
2954
|
endpoint: opts.endpoint,
|
|
2913
2955
|
enabled: eventsEnabled,
|
|
2914
2956
|
debug: ((_c = opts.events) == null ? void 0 : _c.debug) === true || envDebug,
|
|
2915
2957
|
partialFlushMs: (_d = opts.events) == null ? void 0 : _d.partialFlushMs
|
|
2916
2958
|
});
|
|
2917
|
-
const traceShipper = new
|
|
2959
|
+
const traceShipper = new TraceShipper2({
|
|
2918
2960
|
writeKey,
|
|
2919
2961
|
endpoint: opts.endpoint,
|
|
2920
2962
|
enabled: tracesEnabled,
|