raindrop-ai 0.0.88 → 0.0.89
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/LICENSE +21 -0
- package/README.md +25 -0
- package/dist/{chunk-7MR6AAR4.mjs → chunk-R34PD7NX.mjs} +582 -82
- package/dist/chunk-UJCSKKID.mjs +30 -0
- package/dist/index.d.mts +52 -1
- package/dist/index.d.ts +52 -1
- package/dist/index.js +10451 -212
- package/dist/index.mjs +9843 -103
- package/dist/otel/index.mjs +2 -0
- package/dist/tracing/index.js +567 -67
- package/dist/tracing/index.mjs +2 -1
- package/package.json +18 -5
|
@@ -1,11 +1,20 @@
|
|
|
1
1
|
// package.json
|
|
2
2
|
var package_default = {
|
|
3
3
|
name: "raindrop-ai",
|
|
4
|
-
version: "0.0.
|
|
4
|
+
version: "0.0.89",
|
|
5
5
|
main: "dist/index.js",
|
|
6
6
|
module: "dist/index.mjs",
|
|
7
7
|
types: "dist/index.d.ts",
|
|
8
8
|
license: "MIT",
|
|
9
|
+
repository: {
|
|
10
|
+
type: "git",
|
|
11
|
+
url: "git+https://github.com/raindrop-ai/raindrop-js.git",
|
|
12
|
+
directory: "packages/js-sdk"
|
|
13
|
+
},
|
|
14
|
+
homepage: "https://github.com/raindrop-ai/raindrop-js/tree/main/packages/js-sdk#readme",
|
|
15
|
+
bugs: {
|
|
16
|
+
url: "https://github.com/raindrop-ai/raindrop-js/issues"
|
|
17
|
+
},
|
|
9
18
|
exports: {
|
|
10
19
|
".": {
|
|
11
20
|
types: "./dist/index.d.ts",
|
|
@@ -45,15 +54,18 @@ var package_default = {
|
|
|
45
54
|
"smoke:self-diagnostics:all": "pnpm smoke:self-diagnostics:ai-sdk:all && pnpm smoke:self-diagnostics:openai-sdk && pnpm smoke:self-diagnostics:anthropic-sdk"
|
|
46
55
|
},
|
|
47
56
|
devDependencies: {
|
|
57
|
+
"@raindrop-ai/redact-pii": "workspace:*",
|
|
58
|
+
"@raindrop-ai/schemas": "workspace:*",
|
|
59
|
+
ai: "^6.0.0",
|
|
48
60
|
"@types/node": "^20.11.17",
|
|
49
61
|
msw: "^2.12.8",
|
|
50
62
|
tsup: "^8.4.0",
|
|
51
63
|
tsx: "^4.20.3",
|
|
52
64
|
typescript: "^5.3.3",
|
|
65
|
+
vite: "^6.4.1",
|
|
53
66
|
vitest: "^4.0.10"
|
|
54
67
|
},
|
|
55
68
|
dependencies: {
|
|
56
|
-
"@dawn-analytics/redact-pii": "workspace:*",
|
|
57
69
|
"@opentelemetry/api": "^1.9.0",
|
|
58
70
|
"@opentelemetry/resources": "^2.0.1",
|
|
59
71
|
"@traceloop/node-server-sdk": "0.19.0-otel-v1",
|
|
@@ -89,7 +101,8 @@ var package_default = {
|
|
|
89
101
|
"@opentelemetry/sdk-trace-base"
|
|
90
102
|
],
|
|
91
103
|
noExternal: [
|
|
92
|
-
"@
|
|
104
|
+
"@raindrop-ai/redact-pii",
|
|
105
|
+
"@raindrop-ai/schemas"
|
|
93
106
|
],
|
|
94
107
|
dts: {
|
|
95
108
|
resolve: true
|
|
@@ -99,7 +112,7 @@ var package_default = {
|
|
|
99
112
|
};
|
|
100
113
|
|
|
101
114
|
// src/tracing/tracer-core.ts
|
|
102
|
-
import { context as
|
|
115
|
+
import { context as context4, SpanStatusCode as SpanStatusCode4, trace as trace4 } from "@opentelemetry/api";
|
|
103
116
|
import { AnthropicInstrumentation } from "@traceloop/instrumentation-anthropic";
|
|
104
117
|
import { BedrockInstrumentation } from "@traceloop/instrumentation-bedrock";
|
|
105
118
|
import { ChromaDBInstrumentation } from "@traceloop/instrumentation-chromadb";
|
|
@@ -115,7 +128,7 @@ import {
|
|
|
115
128
|
import * as traceloop3 from "@traceloop/node-server-sdk";
|
|
116
129
|
import { WeakValueMap } from "weakref";
|
|
117
130
|
|
|
118
|
-
//
|
|
131
|
+
// ../core/dist/chunk-4UCYIEH4.js
|
|
119
132
|
function wait(ms) {
|
|
120
133
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
121
134
|
}
|
|
@@ -128,7 +141,7 @@ function parseRetryAfter(headers) {
|
|
|
128
141
|
const value = (_a = headers.get("Retry-After")) != null ? _a : headers.get("retry-after");
|
|
129
142
|
if (!value) return void 0;
|
|
130
143
|
const asNumber = Number(value);
|
|
131
|
-
if (!Number.isNaN(asNumber)) return asNumber * 1e3;
|
|
144
|
+
if (value.trim() !== "" && !Number.isNaN(asNumber)) return asNumber * 1e3;
|
|
132
145
|
const asDate = new Date(value).getTime();
|
|
133
146
|
if (!Number.isNaN(asDate)) {
|
|
134
147
|
const delta = asDate - Date.now();
|
|
@@ -147,18 +160,19 @@ function getRetryDelayMs(attemptNumber, previousError) {
|
|
|
147
160
|
return base * factor;
|
|
148
161
|
}
|
|
149
162
|
async function withRetry(operation, opName, opts) {
|
|
163
|
+
const prefix = opts.sdkName ? `[raindrop-ai/${opts.sdkName}]` : "[raindrop-ai/core]";
|
|
150
164
|
let lastError = void 0;
|
|
151
165
|
for (let attemptNumber = 1; attemptNumber <= opts.maxAttempts; attemptNumber++) {
|
|
152
166
|
if (attemptNumber > 1) {
|
|
153
167
|
const delay = getRetryDelayMs(attemptNumber, lastError);
|
|
154
168
|
if (opts.debug) {
|
|
155
169
|
console.warn(
|
|
156
|
-
|
|
170
|
+
`${prefix} ${opName} retry ${attemptNumber}/${opts.maxAttempts} in ${delay}ms`
|
|
157
171
|
);
|
|
158
172
|
}
|
|
159
173
|
if (delay > 0) await wait(delay);
|
|
160
174
|
} else if (opts.debug) {
|
|
161
|
-
console.log(
|
|
175
|
+
console.log(`${prefix} ${opName} attempt ${attemptNumber}/${opts.maxAttempts}`);
|
|
162
176
|
}
|
|
163
177
|
try {
|
|
164
178
|
return await operation();
|
|
@@ -167,9 +181,11 @@ async function withRetry(operation, opName, opts) {
|
|
|
167
181
|
if (opts.debug) {
|
|
168
182
|
const msg = err instanceof Error ? err.message : String(err);
|
|
169
183
|
console.warn(
|
|
170
|
-
|
|
184
|
+
`${prefix} ${opName} attempt ${attemptNumber} failed: ${msg}${attemptNumber === opts.maxAttempts ? " (no more retries)" : ""}`
|
|
171
185
|
);
|
|
172
186
|
}
|
|
187
|
+
if (lastError && typeof lastError === "object" && "retryable" in lastError && !lastError.retryable)
|
|
188
|
+
break;
|
|
173
189
|
if (attemptNumber === opts.maxAttempts) break;
|
|
174
190
|
}
|
|
175
191
|
}
|
|
@@ -194,6 +210,153 @@ async function postJson(url, body, headers, opts) {
|
|
|
194
210
|
);
|
|
195
211
|
const retryAfterMs = parseRetryAfter(resp.headers);
|
|
196
212
|
if (typeof retryAfterMs === "number") err.retryAfterMs = retryAfterMs;
|
|
213
|
+
err.retryable = resp.status === 429 || resp.status >= 500;
|
|
214
|
+
throw err;
|
|
215
|
+
}
|
|
216
|
+
},
|
|
217
|
+
opName,
|
|
218
|
+
opts
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
var LOCAL_DEBUGGER_ENV_VAR = "RAINDROP_LOCAL_DEBUGGER";
|
|
222
|
+
function resolveLocalDebuggerBaseUrl(baseUrl) {
|
|
223
|
+
var _a, _b, _c;
|
|
224
|
+
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;
|
|
225
|
+
return resolved ? (_c = formatEndpoint(resolved)) != null ? _c : null : null;
|
|
226
|
+
}
|
|
227
|
+
function localDebuggerEnabled(baseUrl) {
|
|
228
|
+
return resolveLocalDebuggerBaseUrl(baseUrl) !== null;
|
|
229
|
+
}
|
|
230
|
+
function normalizeLocalDebuggerLiveEventType(type) {
|
|
231
|
+
switch (type) {
|
|
232
|
+
case "text-delta":
|
|
233
|
+
return "text_delta";
|
|
234
|
+
case "reasoning":
|
|
235
|
+
case "reasoning-delta":
|
|
236
|
+
return "reasoning_delta";
|
|
237
|
+
case "tool-call":
|
|
238
|
+
return "tool_start";
|
|
239
|
+
case "tool-result":
|
|
240
|
+
return "tool_result";
|
|
241
|
+
default:
|
|
242
|
+
return type;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
function mirrorTraceExportToLocalDebugger(body, options = {}) {
|
|
246
|
+
var _a;
|
|
247
|
+
const baseUrl = resolveLocalDebuggerBaseUrl(options.baseUrl);
|
|
248
|
+
if (!baseUrl) return;
|
|
249
|
+
void postJson(`${baseUrl}traces`, body, {}, {
|
|
250
|
+
maxAttempts: 1,
|
|
251
|
+
debug: (_a = options.debug) != null ? _a : false,
|
|
252
|
+
sdkName: options.sdkName
|
|
253
|
+
}).catch(() => {
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
function sendLocalDebuggerLiveEvent(event, options = {}) {
|
|
257
|
+
var _a, _b;
|
|
258
|
+
const baseUrl = resolveLocalDebuggerBaseUrl(options.baseUrl);
|
|
259
|
+
if (!baseUrl) return;
|
|
260
|
+
void postJson(
|
|
261
|
+
`${baseUrl}live`,
|
|
262
|
+
{
|
|
263
|
+
...event,
|
|
264
|
+
type: normalizeLocalDebuggerLiveEventType(event.type),
|
|
265
|
+
timestamp: (_a = event.timestamp) != null ? _a : Date.now()
|
|
266
|
+
},
|
|
267
|
+
{},
|
|
268
|
+
{
|
|
269
|
+
maxAttempts: 1,
|
|
270
|
+
debug: (_b = options.debug) != null ? _b : false,
|
|
271
|
+
sdkName: options.sdkName
|
|
272
|
+
}
|
|
273
|
+
).catch(() => {
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// ../core/dist/index.node.js
|
|
278
|
+
import { AsyncLocalStorage } from "async_hooks";
|
|
279
|
+
globalThis.RAINDROP_ASYNC_LOCAL_STORAGE = AsyncLocalStorage;
|
|
280
|
+
|
|
281
|
+
// src/tracing/direct/http.ts
|
|
282
|
+
function wait2(ms) {
|
|
283
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
284
|
+
}
|
|
285
|
+
function formatEndpoint2(endpoint) {
|
|
286
|
+
if (!endpoint) return void 0;
|
|
287
|
+
return endpoint.endsWith("/") ? endpoint : `${endpoint}/`;
|
|
288
|
+
}
|
|
289
|
+
function parseRetryAfter2(headers) {
|
|
290
|
+
var _a;
|
|
291
|
+
const value = (_a = headers.get("Retry-After")) != null ? _a : headers.get("retry-after");
|
|
292
|
+
if (!value) return void 0;
|
|
293
|
+
const asNumber = Number(value);
|
|
294
|
+
if (!Number.isNaN(asNumber)) return asNumber * 1e3;
|
|
295
|
+
const asDate = new Date(value).getTime();
|
|
296
|
+
if (!Number.isNaN(asDate)) {
|
|
297
|
+
const delta = asDate - Date.now();
|
|
298
|
+
return delta > 0 ? delta : 0;
|
|
299
|
+
}
|
|
300
|
+
return void 0;
|
|
301
|
+
}
|
|
302
|
+
function getRetryDelayMs2(attemptNumber, previousError) {
|
|
303
|
+
if (previousError && typeof previousError === "object" && previousError !== null && "retryAfterMs" in previousError) {
|
|
304
|
+
const v = previousError.retryAfterMs;
|
|
305
|
+
if (typeof v === "number") return Math.max(0, v);
|
|
306
|
+
}
|
|
307
|
+
if (attemptNumber <= 1) return 0;
|
|
308
|
+
const base = 500;
|
|
309
|
+
const factor = Math.pow(2, attemptNumber - 2);
|
|
310
|
+
return base * factor;
|
|
311
|
+
}
|
|
312
|
+
async function withRetry2(operation, opName, opts) {
|
|
313
|
+
let lastError = void 0;
|
|
314
|
+
for (let attemptNumber = 1; attemptNumber <= opts.maxAttempts; attemptNumber++) {
|
|
315
|
+
if (attemptNumber > 1) {
|
|
316
|
+
const delay = getRetryDelayMs2(attemptNumber, lastError);
|
|
317
|
+
if (opts.debug) {
|
|
318
|
+
console.warn(
|
|
319
|
+
`[raindrop] ${opName} retry ${attemptNumber}/${opts.maxAttempts} in ${delay}ms`
|
|
320
|
+
);
|
|
321
|
+
}
|
|
322
|
+
if (delay > 0) await wait2(delay);
|
|
323
|
+
} else if (opts.debug) {
|
|
324
|
+
console.log(`[raindrop] ${opName} attempt ${attemptNumber}/${opts.maxAttempts}`);
|
|
325
|
+
}
|
|
326
|
+
try {
|
|
327
|
+
return await operation();
|
|
328
|
+
} catch (err) {
|
|
329
|
+
lastError = err;
|
|
330
|
+
if (opts.debug) {
|
|
331
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
332
|
+
console.warn(
|
|
333
|
+
`[raindrop] ${opName} attempt ${attemptNumber} failed: ${msg}${attemptNumber === opts.maxAttempts ? " (no more retries)" : ""}`
|
|
334
|
+
);
|
|
335
|
+
}
|
|
336
|
+
if (attemptNumber === opts.maxAttempts) break;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
throw lastError instanceof Error ? lastError : new Error(String(lastError));
|
|
340
|
+
}
|
|
341
|
+
async function postJson2(url, body, headers, opts) {
|
|
342
|
+
const opName = `POST ${url}`;
|
|
343
|
+
await withRetry2(
|
|
344
|
+
async () => {
|
|
345
|
+
const resp = await fetch(url, {
|
|
346
|
+
method: "POST",
|
|
347
|
+
headers: {
|
|
348
|
+
"Content-Type": "application/json",
|
|
349
|
+
...headers
|
|
350
|
+
},
|
|
351
|
+
body: JSON.stringify(body)
|
|
352
|
+
});
|
|
353
|
+
if (!resp.ok) {
|
|
354
|
+
const text = await resp.text().catch(() => "");
|
|
355
|
+
const err = new Error(
|
|
356
|
+
`HTTP ${resp.status} ${resp.statusText}${text ? `: ${text}` : ""}`
|
|
357
|
+
);
|
|
358
|
+
const retryAfterMs = parseRetryAfter2(resp.headers);
|
|
359
|
+
if (typeof retryAfterMs === "number") err.retryAfterMs = retryAfterMs;
|
|
197
360
|
throw err;
|
|
198
361
|
}
|
|
199
362
|
},
|
|
@@ -207,7 +370,7 @@ function getCrypto() {
|
|
|
207
370
|
const c = globalThis.crypto;
|
|
208
371
|
return c;
|
|
209
372
|
}
|
|
210
|
-
function
|
|
373
|
+
function randomBytes2(length) {
|
|
211
374
|
const cryptoObj = getCrypto();
|
|
212
375
|
const out = new Uint8Array(length);
|
|
213
376
|
if (cryptoObj && typeof cryptoObj.getRandomValues === "function") {
|
|
@@ -217,7 +380,7 @@ function randomBytes(length) {
|
|
|
217
380
|
for (let i = 0; i < out.length; i++) out[i] = Math.floor(Math.random() * 256);
|
|
218
381
|
return out;
|
|
219
382
|
}
|
|
220
|
-
function
|
|
383
|
+
function base64Encode2(bytes) {
|
|
221
384
|
const maybeBuffer = globalThis.Buffer;
|
|
222
385
|
if (maybeBuffer) {
|
|
223
386
|
return maybeBuffer.from(bytes).toString("base64");
|
|
@@ -246,20 +409,36 @@ function base64Encode(bytes) {
|
|
|
246
409
|
}
|
|
247
410
|
return out;
|
|
248
411
|
}
|
|
412
|
+
function base64ToHex(base64) {
|
|
413
|
+
const maybeBuffer = globalThis.Buffer;
|
|
414
|
+
if (maybeBuffer) {
|
|
415
|
+
return maybeBuffer.from(base64, "base64").toString("hex");
|
|
416
|
+
}
|
|
417
|
+
const atobFn = globalThis.atob;
|
|
418
|
+
if (typeof atobFn === "function") {
|
|
419
|
+
const binary = atobFn(base64);
|
|
420
|
+
let hex = "";
|
|
421
|
+
for (let i = 0; i < binary.length; i++) {
|
|
422
|
+
hex += binary.charCodeAt(i).toString(16).padStart(2, "0");
|
|
423
|
+
}
|
|
424
|
+
return hex;
|
|
425
|
+
}
|
|
426
|
+
throw new Error("No base64 decoder available in this runtime");
|
|
427
|
+
}
|
|
249
428
|
|
|
250
429
|
// src/tracing/direct/otlp.ts
|
|
251
430
|
var libraryVersion = package_default.version;
|
|
252
431
|
var SERVICE_NAME = "raindrop-ai";
|
|
253
|
-
function
|
|
432
|
+
function attrString2(key, value) {
|
|
254
433
|
if (value === void 0) return void 0;
|
|
255
434
|
return { key, value: { stringValue: value } };
|
|
256
435
|
}
|
|
257
|
-
function
|
|
436
|
+
function attrInt2(key, value) {
|
|
258
437
|
if (value === void 0) return void 0;
|
|
259
438
|
if (!Number.isFinite(value)) return void 0;
|
|
260
439
|
return { key, value: { intValue: String(Math.trunc(value)) } };
|
|
261
440
|
}
|
|
262
|
-
function
|
|
441
|
+
function buildOtlpSpan2(args) {
|
|
263
442
|
const attrs = args.attributes.filter((x) => x !== void 0);
|
|
264
443
|
const span = {
|
|
265
444
|
traceId: args.ids.traceIdB64,
|
|
@@ -273,7 +452,7 @@ function buildOtlpSpan(args) {
|
|
|
273
452
|
if (args.status) span.status = args.status;
|
|
274
453
|
return span;
|
|
275
454
|
}
|
|
276
|
-
function
|
|
455
|
+
function buildExportTraceServiceRequest2(spans) {
|
|
277
456
|
return {
|
|
278
457
|
resourceSpans: [
|
|
279
458
|
{
|
|
@@ -300,7 +479,7 @@ function hexToBase64(hex) {
|
|
|
300
479
|
for (let i = 0; i < hex.length; i += 2) {
|
|
301
480
|
bytes[i / 2] = parseInt(hex.substring(i, i + 2), 16);
|
|
302
481
|
}
|
|
303
|
-
return
|
|
482
|
+
return base64Encode2(bytes);
|
|
304
483
|
}
|
|
305
484
|
function getActiveTraceContext() {
|
|
306
485
|
const activeSpan = trace.getSpan(context.active());
|
|
@@ -329,12 +508,18 @@ var DirectTraceShipper = class {
|
|
|
329
508
|
this.inFlight = /* @__PURE__ */ new Set();
|
|
330
509
|
var _a, _b, _c, _d, _e;
|
|
331
510
|
this.writeKey = opts.writeKey;
|
|
332
|
-
this.baseUrl = (_a =
|
|
511
|
+
this.baseUrl = (_a = formatEndpoint2(opts.endpoint)) != null ? _a : "https://api.raindrop.ai/v1/";
|
|
333
512
|
this.enabled = (_b = opts.enabled) != null ? _b : true;
|
|
334
513
|
this.debug = opts.debug;
|
|
335
514
|
this.flushIntervalMs = (_c = opts.flushIntervalMs) != null ? _c : 1e3;
|
|
336
515
|
this.maxBatchSize = (_d = opts.maxBatchSize) != null ? _d : 50;
|
|
337
516
|
this.maxQueueSize = (_e = opts.maxQueueSize) != null ? _e : 5e3;
|
|
517
|
+
if (localDebuggerEnabled()) {
|
|
518
|
+
const resolved = resolveLocalDebuggerBaseUrl();
|
|
519
|
+
if (resolved && resolved !== this.baseUrl) {
|
|
520
|
+
this.localDebuggerUrl = resolved;
|
|
521
|
+
}
|
|
522
|
+
}
|
|
338
523
|
}
|
|
339
524
|
/**
|
|
340
525
|
* Build and enqueue an OTLP span for a trackTool call.
|
|
@@ -342,7 +527,8 @@ var DirectTraceShipper = class {
|
|
|
342
527
|
* Reads the active OTEL context if traceId/parentSpanId are not provided,
|
|
343
528
|
* so spans still appear as children in the trace tree.
|
|
344
529
|
*/
|
|
345
|
-
|
|
530
|
+
sendSpan(params) {
|
|
531
|
+
var _a, _b;
|
|
346
532
|
if (!this.enabled) return;
|
|
347
533
|
let traceIdB64 = params.traceId;
|
|
348
534
|
let parentSpanIdB64 = params.parentSpanId;
|
|
@@ -351,27 +537,27 @@ var DirectTraceShipper = class {
|
|
|
351
537
|
traceIdB64 = activeCtx.traceIdB64;
|
|
352
538
|
parentSpanIdB64 = activeCtx.parentSpanIdB64;
|
|
353
539
|
}
|
|
354
|
-
const spanIdB64 =
|
|
540
|
+
const spanIdB64 = (_a = params.spanId) != null ? _a : base64Encode2(randomBytes2(8));
|
|
355
541
|
if (!traceIdB64) {
|
|
356
|
-
traceIdB64 =
|
|
542
|
+
traceIdB64 = base64Encode2(randomBytes2(16));
|
|
357
543
|
}
|
|
358
544
|
const startTimeUnixNano = String(params.startTimeMs * 1e6);
|
|
359
545
|
const endTimeUnixNano = String(params.endTimeMs * 1e6);
|
|
360
546
|
const attributes = [
|
|
361
547
|
// Required: passes hasAIOperation() filter in backend (parseSpan.ts)
|
|
362
|
-
|
|
548
|
+
attrString2("traceloop.span.kind", (_b = params.spanKind) != null ? _b : "tool"),
|
|
363
549
|
// Parsed by backend for input/output payloads
|
|
364
|
-
|
|
365
|
-
|
|
550
|
+
attrString2("traceloop.entity.input", params.input),
|
|
551
|
+
attrString2("traceloop.entity.output", params.output),
|
|
366
552
|
// Duration tracking
|
|
367
|
-
|
|
553
|
+
attrInt2("traceloop.entity.duration_ms", params.durationMs)
|
|
368
554
|
];
|
|
369
555
|
for (const [key, value] of Object.entries(params.properties)) {
|
|
370
556
|
if (value === void 0) {
|
|
371
557
|
continue;
|
|
372
558
|
}
|
|
373
559
|
attributes.push(
|
|
374
|
-
|
|
560
|
+
attrString2(`traceloop.association.properties.${key}`, serializeAssociationValue(value))
|
|
375
561
|
);
|
|
376
562
|
}
|
|
377
563
|
let status;
|
|
@@ -383,7 +569,7 @@ var DirectTraceShipper = class {
|
|
|
383
569
|
/* STATUS_CODE_OK */
|
|
384
570
|
};
|
|
385
571
|
}
|
|
386
|
-
const span =
|
|
572
|
+
const span = buildOtlpSpan2({
|
|
387
573
|
ids: {
|
|
388
574
|
traceIdB64,
|
|
389
575
|
spanIdB64,
|
|
@@ -395,11 +581,25 @@ var DirectTraceShipper = class {
|
|
|
395
581
|
attributes,
|
|
396
582
|
status
|
|
397
583
|
});
|
|
584
|
+
if (this.localDebuggerUrl) {
|
|
585
|
+
const body = buildExportTraceServiceRequest2([span]);
|
|
586
|
+
mirrorTraceExportToLocalDebugger(body, {
|
|
587
|
+
baseUrl: this.localDebuggerUrl,
|
|
588
|
+
debug: false,
|
|
589
|
+
sdkName: "js-sdk"
|
|
590
|
+
});
|
|
591
|
+
}
|
|
398
592
|
this.enqueue(span);
|
|
399
593
|
if (this.debug) {
|
|
400
594
|
console.log(`[raindrop] direct shipper: enqueued span "${params.name}"`);
|
|
401
595
|
}
|
|
402
596
|
}
|
|
597
|
+
sendToolSpan(params) {
|
|
598
|
+
this.sendSpan({
|
|
599
|
+
...params,
|
|
600
|
+
spanKind: "tool"
|
|
601
|
+
});
|
|
602
|
+
}
|
|
403
603
|
enqueue(span) {
|
|
404
604
|
if (!this.enabled) return;
|
|
405
605
|
if (this.queue.length >= this.maxQueueSize) {
|
|
@@ -427,9 +627,9 @@ var DirectTraceShipper = class {
|
|
|
427
627
|
}
|
|
428
628
|
while (this.queue.length > 0) {
|
|
429
629
|
const batch = this.queue.splice(0, this.maxBatchSize);
|
|
430
|
-
const body =
|
|
630
|
+
const body = buildExportTraceServiceRequest2(batch);
|
|
431
631
|
const url = `${this.baseUrl}traces`;
|
|
432
|
-
const p =
|
|
632
|
+
const p = postJson2(
|
|
433
633
|
url,
|
|
434
634
|
body,
|
|
435
635
|
{ Authorization: `Bearer ${this.writeKey}` },
|
|
@@ -463,17 +663,18 @@ var DirectTraceShipper = class {
|
|
|
463
663
|
};
|
|
464
664
|
|
|
465
665
|
// src/tracing/liveInteraction.ts
|
|
466
|
-
import { SpanStatusCode, trace as trace2 } from "@opentelemetry/api";
|
|
666
|
+
import { context as context2, SpanStatusCode as SpanStatusCode2, TraceFlags, trace as trace2 } from "@opentelemetry/api";
|
|
467
667
|
import * as traceloop from "@traceloop/node-server-sdk";
|
|
468
|
-
function getPropertiesFromContext(
|
|
668
|
+
function getPropertiesFromContext(context5) {
|
|
469
669
|
const properties = {
|
|
470
|
-
...
|
|
471
|
-
...
|
|
472
|
-
...
|
|
473
|
-
...
|
|
670
|
+
...context5.userId && { user_id: context5.userId },
|
|
671
|
+
...context5.convoId && { convo_id: context5.convoId },
|
|
672
|
+
...context5.eventId && { event_id: context5.eventId },
|
|
673
|
+
...context5.event && { event_name: context5.event },
|
|
674
|
+
...context5.properties || {}
|
|
474
675
|
};
|
|
475
|
-
if (
|
|
476
|
-
properties.attachments = JSON.stringify(
|
|
676
|
+
if (context5.attachments && context5.attachments.length > 0) {
|
|
677
|
+
properties.attachments = JSON.stringify(context5.attachments);
|
|
477
678
|
}
|
|
478
679
|
return properties;
|
|
479
680
|
}
|
|
@@ -497,28 +698,48 @@ function serializeSpanValue(value) {
|
|
|
497
698
|
const jsonValue = JSON.stringify(value);
|
|
498
699
|
return jsonValue === void 0 ? String(value) : jsonValue;
|
|
499
700
|
}
|
|
701
|
+
function getLocalDebuggerMetadata(context5) {
|
|
702
|
+
return {
|
|
703
|
+
...context5.eventId ? { eventId: context5.eventId } : {},
|
|
704
|
+
...context5.event ? { eventName: context5.event } : {},
|
|
705
|
+
...context5.userId ? { userId: context5.userId } : {},
|
|
706
|
+
...context5.convoId ? { convoId: context5.convoId } : {}
|
|
707
|
+
};
|
|
708
|
+
}
|
|
500
709
|
var LiveToolSpan = class {
|
|
501
|
-
constructor(
|
|
710
|
+
constructor(params) {
|
|
502
711
|
this.hasError = false;
|
|
503
|
-
this.span = span;
|
|
712
|
+
this.span = params.span;
|
|
713
|
+
this.name = params.name;
|
|
714
|
+
this.emitLiveEvent = params.emitLiveEvent;
|
|
504
715
|
}
|
|
505
716
|
setInput(input) {
|
|
506
717
|
this.span.setAttribute("traceloop.entity.input", serializeSpanValue(input));
|
|
507
718
|
}
|
|
508
719
|
setOutput(output) {
|
|
720
|
+
this.output = output;
|
|
509
721
|
this.span.setAttribute("traceloop.entity.output", serializeSpanValue(output));
|
|
510
722
|
}
|
|
511
723
|
setError(error) {
|
|
512
724
|
this.hasError = true;
|
|
513
|
-
|
|
514
|
-
this.span.setStatus({ code:
|
|
515
|
-
this.span.recordException(error instanceof Error ? error : new Error(errorMessage));
|
|
725
|
+
this.errorMessage = error instanceof Error ? error.message : String(error);
|
|
726
|
+
this.span.setStatus({ code: SpanStatusCode2.ERROR, message: this.errorMessage });
|
|
727
|
+
this.span.recordException(error instanceof Error ? error : new Error(this.errorMessage));
|
|
516
728
|
}
|
|
517
729
|
end() {
|
|
730
|
+
var _a;
|
|
518
731
|
if (!this.hasError) {
|
|
519
|
-
this.span.setStatus({ code:
|
|
732
|
+
this.span.setStatus({ code: SpanStatusCode2.OK });
|
|
520
733
|
}
|
|
521
734
|
this.span.end();
|
|
735
|
+
(_a = this.emitLiveEvent) == null ? void 0 : _a.call(this, {
|
|
736
|
+
type: "tool_result",
|
|
737
|
+
content: this.name,
|
|
738
|
+
metadata: {
|
|
739
|
+
...this.output !== void 0 ? { result: this.output } : {},
|
|
740
|
+
...this.errorMessage ? { error: this.errorMessage } : {}
|
|
741
|
+
}
|
|
742
|
+
});
|
|
522
743
|
}
|
|
523
744
|
};
|
|
524
745
|
var DirectToolSpan = class {
|
|
@@ -531,17 +752,20 @@ var DirectToolSpan = class {
|
|
|
531
752
|
this.parentSpanId = params.parentSpanId;
|
|
532
753
|
this.startTimeMs = Date.now();
|
|
533
754
|
this.input = params.input !== void 0 ? serializeSpanValue(params.input) : void 0;
|
|
755
|
+
this.emitLiveEvent = params.emitLiveEvent;
|
|
534
756
|
}
|
|
535
757
|
setInput(input) {
|
|
536
758
|
this.input = serializeSpanValue(input);
|
|
537
759
|
}
|
|
538
760
|
setOutput(output) {
|
|
761
|
+
this.rawOutput = output;
|
|
539
762
|
this.output = serializeSpanValue(output);
|
|
540
763
|
}
|
|
541
764
|
setError(error) {
|
|
542
765
|
this.error = error instanceof Error ? error.message : String(error);
|
|
543
766
|
}
|
|
544
767
|
end() {
|
|
768
|
+
var _a;
|
|
545
769
|
if (this.ended) {
|
|
546
770
|
return;
|
|
547
771
|
}
|
|
@@ -559,16 +783,65 @@ var DirectToolSpan = class {
|
|
|
559
783
|
traceId: this.traceId,
|
|
560
784
|
parentSpanId: this.parentSpanId
|
|
561
785
|
});
|
|
786
|
+
(_a = this.emitLiveEvent) == null ? void 0 : _a.call(this, {
|
|
787
|
+
traceId: this.traceId,
|
|
788
|
+
type: "tool_result",
|
|
789
|
+
content: this.name,
|
|
790
|
+
timestamp: endTimeMs,
|
|
791
|
+
metadata: {
|
|
792
|
+
...this.rawOutput !== void 0 ? { result: this.rawOutput } : {},
|
|
793
|
+
...this.error ? { error: this.error } : {}
|
|
794
|
+
}
|
|
795
|
+
});
|
|
562
796
|
}
|
|
563
797
|
};
|
|
564
798
|
var LiveInteraction = class {
|
|
565
|
-
constructor(
|
|
566
|
-
this.context =
|
|
799
|
+
constructor(context5, traceId, analytics, directShipper) {
|
|
800
|
+
this.context = context5;
|
|
567
801
|
this.analytics = analytics;
|
|
568
802
|
this.tracer = trace2.getTracer("traceloop.tracer");
|
|
569
803
|
this.traceId = traceId;
|
|
570
804
|
this.directShipper = directShipper;
|
|
571
805
|
}
|
|
806
|
+
ensureTraceId(preferred) {
|
|
807
|
+
if (preferred) {
|
|
808
|
+
this.traceId = preferred;
|
|
809
|
+
return preferred;
|
|
810
|
+
}
|
|
811
|
+
if (this.traceId) {
|
|
812
|
+
return this.traceId;
|
|
813
|
+
}
|
|
814
|
+
this.traceId = base64Encode2(randomBytes2(16));
|
|
815
|
+
return this.traceId;
|
|
816
|
+
}
|
|
817
|
+
resolveLiveTraceId(explicitTraceId) {
|
|
818
|
+
if (explicitTraceId) {
|
|
819
|
+
this.traceId = explicitTraceId;
|
|
820
|
+
return explicitTraceId;
|
|
821
|
+
}
|
|
822
|
+
const activeSpan = trace2.getSpan(context2.active());
|
|
823
|
+
const activeTraceId = activeSpan == null ? void 0 : activeSpan.spanContext().traceId;
|
|
824
|
+
if (activeTraceId) {
|
|
825
|
+
this.traceId = activeTraceId;
|
|
826
|
+
return activeTraceId;
|
|
827
|
+
}
|
|
828
|
+
return this.traceId;
|
|
829
|
+
}
|
|
830
|
+
emitLiveEvent(event) {
|
|
831
|
+
var _a, _b;
|
|
832
|
+
const traceId = (_a = this.resolveLiveTraceId(event.traceId)) != null ? _a : this.ensureTraceId();
|
|
833
|
+
sendLocalDebuggerLiveEvent({
|
|
834
|
+
...event,
|
|
835
|
+
traceId,
|
|
836
|
+
metadata: {
|
|
837
|
+
...getLocalDebuggerMetadata(this.context),
|
|
838
|
+
...(_b = event.metadata) != null ? _b : {}
|
|
839
|
+
}
|
|
840
|
+
});
|
|
841
|
+
}
|
|
842
|
+
getTraceId() {
|
|
843
|
+
return this.traceId;
|
|
844
|
+
}
|
|
572
845
|
async withSpan(params, fn, thisArg, ...args) {
|
|
573
846
|
var _a;
|
|
574
847
|
const taskName = typeof params === "string" ? params : params.name;
|
|
@@ -582,6 +855,64 @@ var LiveInteraction = class {
|
|
|
582
855
|
if ((_a = this.analytics) == null ? void 0 : _a.debugLogs) {
|
|
583
856
|
console.log("[raindrop] using withSpan in liveInteraction");
|
|
584
857
|
}
|
|
858
|
+
if (this.directShipper) {
|
|
859
|
+
const activeContext = getActiveTraceContext();
|
|
860
|
+
const traceIdB64 = this.ensureTraceId(activeContext.traceIdB64);
|
|
861
|
+
const parentSpanIdB64 = activeContext.parentSpanIdB64;
|
|
862
|
+
const spanIdB64 = base64Encode2(randomBytes2(8));
|
|
863
|
+
const startTimeMs = Date.now();
|
|
864
|
+
const spanContext = {
|
|
865
|
+
traceId: base64ToHex(traceIdB64),
|
|
866
|
+
spanId: base64ToHex(spanIdB64),
|
|
867
|
+
traceFlags: TraceFlags.SAMPLED
|
|
868
|
+
};
|
|
869
|
+
const wrappedContext = trace2.setSpan(
|
|
870
|
+
context2.active(),
|
|
871
|
+
trace2.wrapSpanContext(spanContext)
|
|
872
|
+
);
|
|
873
|
+
try {
|
|
874
|
+
const result = await context2.with(wrappedContext, () => fn.apply(thisArg, args));
|
|
875
|
+
const endTimeMs = Date.now();
|
|
876
|
+
this.directShipper.sendSpan({
|
|
877
|
+
name: taskName,
|
|
878
|
+
spanKind: "task",
|
|
879
|
+
spanId: spanIdB64,
|
|
880
|
+
input: inputParameters !== void 0 ? serializeSpanValue(inputParameters) : void 0,
|
|
881
|
+
output: result !== void 0 ? serializeSpanValue(result) : void 0,
|
|
882
|
+
durationMs: endTimeMs - startTimeMs,
|
|
883
|
+
startTimeMs,
|
|
884
|
+
endTimeMs,
|
|
885
|
+
properties,
|
|
886
|
+
traceId: traceIdB64,
|
|
887
|
+
parentSpanId: parentSpanIdB64
|
|
888
|
+
});
|
|
889
|
+
return result;
|
|
890
|
+
} catch (error) {
|
|
891
|
+
const endTimeMs = Date.now();
|
|
892
|
+
this.directShipper.sendSpan({
|
|
893
|
+
name: taskName,
|
|
894
|
+
spanKind: "task",
|
|
895
|
+
spanId: spanIdB64,
|
|
896
|
+
input: inputParameters !== void 0 ? serializeSpanValue(inputParameters) : void 0,
|
|
897
|
+
durationMs: endTimeMs - startTimeMs,
|
|
898
|
+
startTimeMs,
|
|
899
|
+
endTimeMs,
|
|
900
|
+
error: error instanceof Error ? error.message : String(error),
|
|
901
|
+
properties,
|
|
902
|
+
traceId: traceIdB64,
|
|
903
|
+
parentSpanId: parentSpanIdB64
|
|
904
|
+
});
|
|
905
|
+
throw error;
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
const wrappedFn = (...fnArgs) => {
|
|
909
|
+
var _a2;
|
|
910
|
+
const activeTraceId = (_a2 = trace2.getSpan(context2.active())) == null ? void 0 : _a2.spanContext().traceId;
|
|
911
|
+
if (activeTraceId) {
|
|
912
|
+
this.traceId = activeTraceId;
|
|
913
|
+
}
|
|
914
|
+
return fn.apply(thisArg, fnArgs);
|
|
915
|
+
};
|
|
585
916
|
return traceloop.withTask(
|
|
586
917
|
{
|
|
587
918
|
name: taskName,
|
|
@@ -590,8 +921,8 @@ var LiveInteraction = class {
|
|
|
590
921
|
traceContent: params.traceContent,
|
|
591
922
|
suppressTracing: params.suppressTracing
|
|
592
923
|
},
|
|
593
|
-
|
|
594
|
-
|
|
924
|
+
wrappedFn,
|
|
925
|
+
void 0,
|
|
595
926
|
...args
|
|
596
927
|
);
|
|
597
928
|
}
|
|
@@ -606,12 +937,31 @@ var LiveInteraction = class {
|
|
|
606
937
|
console.log("[raindrop] using withTool in liveInteraction");
|
|
607
938
|
}
|
|
608
939
|
if (this.directShipper) {
|
|
609
|
-
const
|
|
940
|
+
const activeContext = getActiveTraceContext();
|
|
941
|
+
this.emitLiveEvent({
|
|
942
|
+
traceId: activeContext.traceIdB64,
|
|
943
|
+
type: "tool_start",
|
|
944
|
+
content: toolName,
|
|
945
|
+
timestamp: Date.now(),
|
|
946
|
+
metadata: params.inputParameters ? { args: params.inputParameters } : void 0
|
|
947
|
+
});
|
|
948
|
+
const traceIdB64 = this.ensureTraceId(activeContext.traceIdB64);
|
|
949
|
+
const { parentSpanIdB64 } = activeContext;
|
|
950
|
+
const spanIdB64 = base64Encode2(randomBytes2(8));
|
|
610
951
|
const startTimeMs = Date.now();
|
|
952
|
+
const spanContext = {
|
|
953
|
+
traceId: base64ToHex(traceIdB64),
|
|
954
|
+
spanId: base64ToHex(spanIdB64),
|
|
955
|
+
traceFlags: TraceFlags.SAMPLED
|
|
956
|
+
};
|
|
957
|
+
const wrappedContext = trace2.setSpan(
|
|
958
|
+
context2.active(),
|
|
959
|
+
trace2.wrapSpanContext(spanContext)
|
|
960
|
+
);
|
|
611
961
|
let result;
|
|
612
962
|
let error;
|
|
613
963
|
try {
|
|
614
|
-
result = await fn.apply(thisArg, args);
|
|
964
|
+
result = await context2.with(wrappedContext, () => fn.apply(thisArg, args));
|
|
615
965
|
} catch (err) {
|
|
616
966
|
error = err instanceof Error ? err.message : String(err);
|
|
617
967
|
const endTimeMs2 = Date.now();
|
|
@@ -619,6 +969,7 @@ var LiveInteraction = class {
|
|
|
619
969
|
const inputStr2 = params.inputParameters ? JSON.stringify(params.inputParameters) : void 0;
|
|
620
970
|
this.directShipper.sendToolSpan({
|
|
621
971
|
name: toolName,
|
|
972
|
+
spanId: spanIdB64,
|
|
622
973
|
input: inputStr2,
|
|
623
974
|
output: void 0,
|
|
624
975
|
durationMs: durationMs2,
|
|
@@ -629,6 +980,16 @@ var LiveInteraction = class {
|
|
|
629
980
|
traceId: traceIdB64,
|
|
630
981
|
parentSpanId: parentSpanIdB64
|
|
631
982
|
});
|
|
983
|
+
this.emitLiveEvent({
|
|
984
|
+
traceId: traceIdB64,
|
|
985
|
+
type: "tool_result",
|
|
986
|
+
content: toolName,
|
|
987
|
+
timestamp: endTimeMs2,
|
|
988
|
+
metadata: {
|
|
989
|
+
...params.inputParameters ? { args: params.inputParameters } : {},
|
|
990
|
+
error
|
|
991
|
+
}
|
|
992
|
+
});
|
|
632
993
|
throw err;
|
|
633
994
|
}
|
|
634
995
|
const endTimeMs = Date.now();
|
|
@@ -637,6 +998,7 @@ var LiveInteraction = class {
|
|
|
637
998
|
const outputStr = result !== void 0 ? typeof result === "string" ? result : JSON.stringify(result) : void 0;
|
|
638
999
|
this.directShipper.sendToolSpan({
|
|
639
1000
|
name: toolName,
|
|
1001
|
+
spanId: spanIdB64,
|
|
640
1002
|
input: inputStr,
|
|
641
1003
|
output: outputStr,
|
|
642
1004
|
durationMs,
|
|
@@ -647,22 +1009,61 @@ var LiveInteraction = class {
|
|
|
647
1009
|
traceId: traceIdB64,
|
|
648
1010
|
parentSpanId: parentSpanIdB64
|
|
649
1011
|
});
|
|
1012
|
+
this.emitLiveEvent({
|
|
1013
|
+
traceId: traceIdB64,
|
|
1014
|
+
type: "tool_result",
|
|
1015
|
+
content: toolName,
|
|
1016
|
+
timestamp: endTimeMs,
|
|
1017
|
+
metadata: {
|
|
1018
|
+
...params.inputParameters ? { args: params.inputParameters } : {},
|
|
1019
|
+
...result !== void 0 ? { result } : {}
|
|
1020
|
+
}
|
|
1021
|
+
});
|
|
650
1022
|
return result;
|
|
651
1023
|
}
|
|
1024
|
+
this.emitLiveEvent({
|
|
1025
|
+
type: "tool_start",
|
|
1026
|
+
content: toolName,
|
|
1027
|
+
timestamp: Date.now(),
|
|
1028
|
+
metadata: params.inputParameters ? { args: params.inputParameters } : void 0
|
|
1029
|
+
});
|
|
652
1030
|
const inputParams = params.inputParameters ? [params.inputParameters] : void 0;
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
1031
|
+
try {
|
|
1032
|
+
const result = await traceloop.withTool(
|
|
1033
|
+
{
|
|
1034
|
+
name: toolName,
|
|
1035
|
+
associationProperties: serializeAssociationProperties(properties),
|
|
1036
|
+
inputParameters: inputParams,
|
|
1037
|
+
version: params.version,
|
|
1038
|
+
traceContent: params.traceContent,
|
|
1039
|
+
suppressTracing: params.suppressTracing
|
|
1040
|
+
},
|
|
1041
|
+
fn,
|
|
1042
|
+
thisArg,
|
|
1043
|
+
...args
|
|
1044
|
+
);
|
|
1045
|
+
this.emitLiveEvent({
|
|
1046
|
+
type: "tool_result",
|
|
1047
|
+
content: toolName,
|
|
1048
|
+
timestamp: Date.now(),
|
|
1049
|
+
metadata: {
|
|
1050
|
+
...params.inputParameters ? { args: params.inputParameters } : {},
|
|
1051
|
+
...result !== void 0 ? { result } : {}
|
|
1052
|
+
}
|
|
1053
|
+
});
|
|
1054
|
+
return result;
|
|
1055
|
+
} catch (error) {
|
|
1056
|
+
this.emitLiveEvent({
|
|
1057
|
+
type: "tool_result",
|
|
1058
|
+
content: toolName,
|
|
1059
|
+
timestamp: Date.now(),
|
|
1060
|
+
metadata: {
|
|
1061
|
+
...params.inputParameters ? { args: params.inputParameters } : {},
|
|
1062
|
+
error: error instanceof Error ? error.message : String(error)
|
|
1063
|
+
}
|
|
1064
|
+
});
|
|
1065
|
+
throw error;
|
|
1066
|
+
}
|
|
666
1067
|
}
|
|
667
1068
|
startSpan(params) {
|
|
668
1069
|
const { name, properties = {} } = params;
|
|
@@ -682,17 +1083,27 @@ var LiveInteraction = class {
|
|
|
682
1083
|
...properties
|
|
683
1084
|
};
|
|
684
1085
|
if (this.directShipper) {
|
|
685
|
-
const
|
|
1086
|
+
const activeTraceContext = getActiveTraceContext();
|
|
1087
|
+
const traceIdB64 = this.ensureTraceId(activeTraceContext.traceIdB64);
|
|
1088
|
+
const { parentSpanIdB64 } = activeTraceContext;
|
|
686
1089
|
if ((_a = this.analytics) == null ? void 0 : _a.debugLogs) {
|
|
687
1090
|
console.log(`[raindrop] startToolSpan (direct): started tool span "${name}"`);
|
|
688
1091
|
}
|
|
1092
|
+
this.emitLiveEvent({
|
|
1093
|
+
traceId: traceIdB64,
|
|
1094
|
+
type: "tool_start",
|
|
1095
|
+
content: name,
|
|
1096
|
+
timestamp: Date.now(),
|
|
1097
|
+
metadata: inputParameters !== void 0 ? { args: inputParameters } : void 0
|
|
1098
|
+
});
|
|
689
1099
|
return new DirectToolSpan({
|
|
690
1100
|
name,
|
|
691
1101
|
properties: mergedProperties,
|
|
692
1102
|
directShipper: this.directShipper,
|
|
693
1103
|
traceId: traceIdB64,
|
|
694
1104
|
parentSpanId: parentSpanIdB64,
|
|
695
|
-
input: inputParameters
|
|
1105
|
+
input: inputParameters,
|
|
1106
|
+
emitLiveEvent: (event) => this.emitLiveEvent(event)
|
|
696
1107
|
});
|
|
697
1108
|
}
|
|
698
1109
|
const span = this.tracer.startSpan(name);
|
|
@@ -705,7 +1116,17 @@ var LiveInteraction = class {
|
|
|
705
1116
|
if ((_b = this.analytics) == null ? void 0 : _b.debugLogs) {
|
|
706
1117
|
console.log(`[raindrop] startToolSpan: started tool span "${name}"`);
|
|
707
1118
|
}
|
|
708
|
-
|
|
1119
|
+
this.emitLiveEvent({
|
|
1120
|
+
type: "tool_start",
|
|
1121
|
+
content: name,
|
|
1122
|
+
timestamp: Date.now(),
|
|
1123
|
+
metadata: inputParameters !== void 0 ? { args: inputParameters } : void 0
|
|
1124
|
+
});
|
|
1125
|
+
return new LiveToolSpan({
|
|
1126
|
+
span,
|
|
1127
|
+
name,
|
|
1128
|
+
emitLiveEvent: (event) => this.emitLiveEvent(event)
|
|
1129
|
+
});
|
|
709
1130
|
}
|
|
710
1131
|
setProperties(properties) {
|
|
711
1132
|
var _a;
|
|
@@ -750,6 +1171,9 @@ var LiveInteraction = class {
|
|
|
750
1171
|
if (this.context.eventId) {
|
|
751
1172
|
metadata["traceloop.association.properties.event_id"] = this.context.eventId;
|
|
752
1173
|
}
|
|
1174
|
+
if (this.context.event) {
|
|
1175
|
+
metadata["traceloop.association.properties.event_name"] = this.context.event;
|
|
1176
|
+
}
|
|
753
1177
|
if (this.context.properties) {
|
|
754
1178
|
for (const [key, value] of Object.entries(this.context.properties)) {
|
|
755
1179
|
if (value === void 0) {
|
|
@@ -780,7 +1204,9 @@ var LiveInteraction = class {
|
|
|
780
1204
|
const startTimeMs = startTime instanceof Date ? startTime.getTime() : typeof startTime === "number" ? startTime : Date.now() - (durationMs != null ? durationMs : 0);
|
|
781
1205
|
const endTimeMs = startTimeMs + (durationMs != null ? durationMs : 0);
|
|
782
1206
|
if (this.directShipper) {
|
|
783
|
-
const
|
|
1207
|
+
const activeContext = getActiveTraceContext();
|
|
1208
|
+
const traceIdB64 = this.ensureTraceId(activeContext.traceIdB64);
|
|
1209
|
+
const { parentSpanIdB64 } = activeContext;
|
|
784
1210
|
const inputStr = input !== void 0 ? typeof input === "string" ? input : JSON.stringify(input) : void 0;
|
|
785
1211
|
const outputStr = output !== void 0 ? typeof output === "string" ? output : JSON.stringify(output) : void 0;
|
|
786
1212
|
this.directShipper.sendToolSpan({
|
|
@@ -806,6 +1232,17 @@ var LiveInteraction = class {
|
|
|
806
1232
|
error: error ? String(error) : void 0
|
|
807
1233
|
});
|
|
808
1234
|
}
|
|
1235
|
+
this.emitLiveEvent({
|
|
1236
|
+
traceId: traceIdB64,
|
|
1237
|
+
type: "tool_result",
|
|
1238
|
+
content: name,
|
|
1239
|
+
timestamp: endTimeMs,
|
|
1240
|
+
metadata: {
|
|
1241
|
+
...input !== void 0 ? { input } : {},
|
|
1242
|
+
...output !== void 0 ? { result: output } : {},
|
|
1243
|
+
...error ? { error: error instanceof Error ? error.message : String(error) } : {}
|
|
1244
|
+
}
|
|
1245
|
+
});
|
|
809
1246
|
return;
|
|
810
1247
|
}
|
|
811
1248
|
const span = this.tracer.startSpan(name, {
|
|
@@ -828,10 +1265,10 @@ var LiveInteraction = class {
|
|
|
828
1265
|
}
|
|
829
1266
|
if (error) {
|
|
830
1267
|
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
831
|
-
span.setStatus({ code:
|
|
1268
|
+
span.setStatus({ code: SpanStatusCode2.ERROR, message: errorMessage });
|
|
832
1269
|
span.recordException(error instanceof Error ? error : new Error(errorMessage));
|
|
833
1270
|
} else {
|
|
834
|
-
span.setStatus({ code:
|
|
1271
|
+
span.setStatus({ code: SpanStatusCode2.OK });
|
|
835
1272
|
}
|
|
836
1273
|
span.end(endTimeMs);
|
|
837
1274
|
if ((_b = this.analytics) == null ? void 0 : _b.debugLogs) {
|
|
@@ -842,11 +1279,21 @@ var LiveInteraction = class {
|
|
|
842
1279
|
error: error ? String(error) : void 0
|
|
843
1280
|
});
|
|
844
1281
|
}
|
|
1282
|
+
this.emitLiveEvent({
|
|
1283
|
+
type: "tool_result",
|
|
1284
|
+
content: name,
|
|
1285
|
+
timestamp: endTimeMs,
|
|
1286
|
+
metadata: {
|
|
1287
|
+
...input !== void 0 ? { input } : {},
|
|
1288
|
+
...output !== void 0 ? { result: output } : {},
|
|
1289
|
+
...error ? { error: error instanceof Error ? error.message : String(error) } : {}
|
|
1290
|
+
}
|
|
1291
|
+
});
|
|
845
1292
|
}
|
|
846
1293
|
};
|
|
847
1294
|
|
|
848
1295
|
// src/tracing/liveTracer.ts
|
|
849
|
-
import { SpanStatusCode as
|
|
1296
|
+
import { context as context3, SpanStatusCode as SpanStatusCode3, trace as trace3 } from "@opentelemetry/api";
|
|
850
1297
|
import * as traceloop2 from "@traceloop/node-server-sdk";
|
|
851
1298
|
var LiveTracer = class {
|
|
852
1299
|
constructor(globalProperties, directShipper) {
|
|
@@ -854,6 +1301,18 @@ var LiveTracer = class {
|
|
|
854
1301
|
this.globalProperties = globalProperties || {};
|
|
855
1302
|
this.directShipper = directShipper;
|
|
856
1303
|
}
|
|
1304
|
+
emitLiveEvent(event) {
|
|
1305
|
+
var _a, _b;
|
|
1306
|
+
const activeSpan = trace3.getSpan(context3.active());
|
|
1307
|
+
const activeTraceId = activeSpan == null ? void 0 : activeSpan.spanContext().traceId;
|
|
1308
|
+
const active = getActiveTraceContext();
|
|
1309
|
+
const traceId = (_b = (_a = event.traceId) != null ? _a : activeTraceId) != null ? _b : active.traceIdB64;
|
|
1310
|
+
if (!traceId) return;
|
|
1311
|
+
sendLocalDebuggerLiveEvent({
|
|
1312
|
+
...event,
|
|
1313
|
+
traceId
|
|
1314
|
+
});
|
|
1315
|
+
}
|
|
857
1316
|
async withSpan(params, fn, thisArg, ...args) {
|
|
858
1317
|
const taskName = typeof params === "string" ? params : params.name;
|
|
859
1318
|
const properties = typeof params === "string" ? {
|
|
@@ -877,6 +1336,7 @@ var LiveTracer = class {
|
|
|
877
1336
|
);
|
|
878
1337
|
}
|
|
879
1338
|
trackTool(params) {
|
|
1339
|
+
var _a;
|
|
880
1340
|
const { name, input, output, durationMs, startTime, error, properties = {} } = params;
|
|
881
1341
|
const startTimeMs = startTime instanceof Date ? startTime.getTime() : typeof startTime === "number" ? startTime : Date.now() - (durationMs != null ? durationMs : 0);
|
|
882
1342
|
const endTimeMs = startTimeMs + (durationMs != null ? durationMs : 0);
|
|
@@ -899,6 +1359,19 @@ var LiveTracer = class {
|
|
|
899
1359
|
traceId: traceIdB64,
|
|
900
1360
|
parentSpanId: parentSpanIdB64
|
|
901
1361
|
});
|
|
1362
|
+
if (traceIdB64) {
|
|
1363
|
+
this.emitLiveEvent({
|
|
1364
|
+
traceId: traceIdB64,
|
|
1365
|
+
type: "tool_result",
|
|
1366
|
+
content: name,
|
|
1367
|
+
timestamp: endTimeMs,
|
|
1368
|
+
metadata: {
|
|
1369
|
+
...input !== void 0 ? { input } : {},
|
|
1370
|
+
...output !== void 0 ? { result: output } : {},
|
|
1371
|
+
...error ? { error: error instanceof Error ? error.message : String(error) } : {}
|
|
1372
|
+
}
|
|
1373
|
+
});
|
|
1374
|
+
}
|
|
902
1375
|
return;
|
|
903
1376
|
}
|
|
904
1377
|
const span = this.tracer.startSpan(name, {
|
|
@@ -924,16 +1397,38 @@ var LiveTracer = class {
|
|
|
924
1397
|
}
|
|
925
1398
|
if (error) {
|
|
926
1399
|
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
927
|
-
span.setStatus({ code:
|
|
1400
|
+
span.setStatus({ code: SpanStatusCode3.ERROR, message: errorMessage });
|
|
928
1401
|
span.recordException(error instanceof Error ? error : new Error(errorMessage));
|
|
929
1402
|
} else {
|
|
930
|
-
span.setStatus({ code:
|
|
1403
|
+
span.setStatus({ code: SpanStatusCode3.OK });
|
|
931
1404
|
}
|
|
932
1405
|
span.end(endTimeMs);
|
|
1406
|
+
const traceId = span.spanContext().traceId || ((_a = trace3.getSpan(context3.active())) == null ? void 0 : _a.spanContext().traceId);
|
|
1407
|
+
if (traceId) {
|
|
1408
|
+
this.emitLiveEvent({
|
|
1409
|
+
traceId,
|
|
1410
|
+
type: "tool_result",
|
|
1411
|
+
content: name,
|
|
1412
|
+
timestamp: endTimeMs,
|
|
1413
|
+
metadata: {
|
|
1414
|
+
...input !== void 0 ? { input } : {},
|
|
1415
|
+
...output !== void 0 ? { result: output } : {},
|
|
1416
|
+
...error ? { error: error instanceof Error ? error.message : String(error) } : {}
|
|
1417
|
+
}
|
|
1418
|
+
});
|
|
1419
|
+
}
|
|
933
1420
|
}
|
|
934
1421
|
};
|
|
935
1422
|
|
|
936
1423
|
// src/tracing/tracer-core.ts
|
|
1424
|
+
if (localDebuggerEnabled()) {
|
|
1425
|
+
if (!process.env.OTEL_EXPORTER_OTLP_PROTOCOL) {
|
|
1426
|
+
process.env.OTEL_EXPORTER_OTLP_PROTOCOL = "http/json";
|
|
1427
|
+
}
|
|
1428
|
+
if (!process.env.OTEL_EXPORTER_OTLP_TRACES_PROTOCOL) {
|
|
1429
|
+
process.env.OTEL_EXPORTER_OTLP_TRACES_PROTOCOL = "http/json";
|
|
1430
|
+
}
|
|
1431
|
+
}
|
|
937
1432
|
try {
|
|
938
1433
|
const _bedrockProto = BedrockInstrumentation.prototype;
|
|
939
1434
|
_bedrockProto._wrapPromise = function(span, promise) {
|
|
@@ -951,7 +1446,7 @@ try {
|
|
|
951
1446
|
e instanceof Error ? e.message : e
|
|
952
1447
|
);
|
|
953
1448
|
span.setStatus({
|
|
954
|
-
code:
|
|
1449
|
+
code: SpanStatusCode4.ERROR,
|
|
955
1450
|
message: e instanceof Error ? e.message : String(e)
|
|
956
1451
|
});
|
|
957
1452
|
span.end();
|
|
@@ -983,7 +1478,7 @@ try {
|
|
|
983
1478
|
);
|
|
984
1479
|
if (!spanEnded) {
|
|
985
1480
|
span.setStatus({
|
|
986
|
-
code:
|
|
1481
|
+
code: SpanStatusCode4.ERROR,
|
|
987
1482
|
message: e instanceof Error ? e.message : String(e)
|
|
988
1483
|
});
|
|
989
1484
|
span.end();
|
|
@@ -997,7 +1492,7 @@ try {
|
|
|
997
1492
|
} catch (error) {
|
|
998
1493
|
if (!spanEnded) {
|
|
999
1494
|
const msg = error instanceof Error ? error.message : String(error);
|
|
1000
|
-
span.setStatus({ code:
|
|
1495
|
+
span.setStatus({ code: SpanStatusCode4.ERROR, message: msg });
|
|
1001
1496
|
span.recordException(error);
|
|
1002
1497
|
span.end();
|
|
1003
1498
|
spanEnded = true;
|
|
@@ -1020,7 +1515,7 @@ try {
|
|
|
1020
1515
|
return result;
|
|
1021
1516
|
}).catch((error) => {
|
|
1022
1517
|
span.setStatus({
|
|
1023
|
-
code:
|
|
1518
|
+
code: SpanStatusCode4.ERROR,
|
|
1024
1519
|
message: error.message
|
|
1025
1520
|
});
|
|
1026
1521
|
span.recordException(error);
|
|
@@ -1083,7 +1578,7 @@ function patchBedrockStreamingBug(inst) {
|
|
|
1083
1578
|
} catch (error) {
|
|
1084
1579
|
if (!spanEnded) {
|
|
1085
1580
|
const msg = error instanceof Error ? error.message : String(error);
|
|
1086
|
-
span.setStatus({ code:
|
|
1581
|
+
span.setStatus({ code: SpanStatusCode4.ERROR, message: msg });
|
|
1087
1582
|
span.recordException(error);
|
|
1088
1583
|
span.end();
|
|
1089
1584
|
spanEnded = true;
|
|
@@ -1106,7 +1601,7 @@ function patchBedrockStreamingBug(inst) {
|
|
|
1106
1601
|
return result;
|
|
1107
1602
|
}).catch((error) => {
|
|
1108
1603
|
span.setStatus({
|
|
1109
|
-
code:
|
|
1604
|
+
code: SpanStatusCode4.ERROR,
|
|
1110
1605
|
message: error.message
|
|
1111
1606
|
});
|
|
1112
1607
|
span.recordException(error);
|
|
@@ -1190,7 +1685,7 @@ var activeInteractions = new WeakValueMap();
|
|
|
1190
1685
|
var activeInteractionsByEventId = new WeakValueMap();
|
|
1191
1686
|
function getCurrentTraceId() {
|
|
1192
1687
|
var _a;
|
|
1193
|
-
return (_a = trace4.getSpan(
|
|
1688
|
+
return (_a = trace4.getSpan(context4.active())) == null ? void 0 : _a.spanContext().traceId;
|
|
1194
1689
|
}
|
|
1195
1690
|
var tracing = (analytics, apiUrl, writeKey, options, isDebug = false) => {
|
|
1196
1691
|
const { logLevel, useExternalOtel, tracingEnabled, disableBatching, bypassOtelForTools, directEndpoint, ...otherOptions } = options;
|
|
@@ -1321,21 +1816,26 @@ var tracing = (analytics, apiUrl, writeKey, options, isDebug = false) => {
|
|
|
1321
1816
|
});
|
|
1322
1817
|
},
|
|
1323
1818
|
begin(traceContext) {
|
|
1819
|
+
var _a;
|
|
1324
1820
|
if (!traceContext.eventId) {
|
|
1325
1821
|
traceContext.eventId = crypto.randomUUID();
|
|
1326
1822
|
}
|
|
1327
1823
|
const traceId = getCurrentTraceId();
|
|
1328
|
-
analytics._trackAiPartial({
|
|
1329
|
-
...traceContext
|
|
1330
|
-
});
|
|
1331
1824
|
const interaction = new LiveInteraction(
|
|
1332
1825
|
traceContext,
|
|
1333
1826
|
traceId,
|
|
1334
1827
|
analytics,
|
|
1335
1828
|
directShipper
|
|
1336
1829
|
);
|
|
1337
|
-
|
|
1338
|
-
|
|
1830
|
+
analytics._trackAiPartial({
|
|
1831
|
+
...traceContext,
|
|
1832
|
+
properties: {
|
|
1833
|
+
...(_a = traceContext.properties) != null ? _a : {},
|
|
1834
|
+
...interaction.getTraceId() ? { trace_id: interaction.getTraceId() } : {}
|
|
1835
|
+
}
|
|
1836
|
+
});
|
|
1837
|
+
if (interaction.getTraceId()) {
|
|
1838
|
+
activeInteractions.set(interaction.getTraceId(), interaction);
|
|
1339
1839
|
}
|
|
1340
1840
|
activeInteractionsByEventId.set(traceContext.eventId, interaction);
|
|
1341
1841
|
return interaction;
|