@raindrop-ai/ai-sdk 0.3.2 → 0.3.4
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 +48 -0
- package/dist/{chunk-VRVBVX6K.mjs → chunk-BS7SHOEE.mjs} +1219 -381
- package/dist/{index-BqXHiknB.d.mts → index-DCYSGvxI.d.mts} +182 -39
- package/dist/{index-BqXHiknB.d.ts → index-DCYSGvxI.d.ts} +182 -39
- package/dist/index.browser.d.mts +182 -39
- package/dist/index.browser.d.ts +182 -39
- package/dist/index.browser.js +1043 -349
- package/dist/index.browser.mjs +1043 -350
- package/dist/index.node.d.mts +1 -1
- package/dist/index.node.d.ts +1 -1
- package/dist/index.node.js +1219 -380
- package/dist/index.node.mjs +1 -1
- package/dist/index.workers.d.mts +1 -1
- package/dist/index.workers.d.ts +1 -1
- package/dist/index.workers.js +1219 -380
- package/dist/index.workers.mjs +1 -1
- package/package.json +2 -2
package/dist/index.browser.js
CHANGED
|
@@ -62,7 +62,574 @@ function evalScopeAttributes() {
|
|
|
62
62
|
return { [EVAL_CORRELATION_ID_ATTRIBUTE]: scope.correlationId };
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
// ../core/dist/chunk-
|
|
65
|
+
// ../core/dist/chunk-I6TVJQEC.js
|
|
66
|
+
var STATE_KEY = /* @__PURE__ */ Symbol.for("raindrop.tracing.replayTraceRoutingState");
|
|
67
|
+
function state() {
|
|
68
|
+
const holder = globalThis;
|
|
69
|
+
let current = holder[STATE_KEY];
|
|
70
|
+
if (!current) {
|
|
71
|
+
let storage = null;
|
|
72
|
+
try {
|
|
73
|
+
const Ctor = globalThis.RAINDROP_ASYNC_LOCAL_STORAGE;
|
|
74
|
+
if (Ctor) storage = new Ctor();
|
|
75
|
+
} catch (e) {
|
|
76
|
+
storage = null;
|
|
77
|
+
}
|
|
78
|
+
current = {
|
|
79
|
+
storage,
|
|
80
|
+
syncDestination: void 0,
|
|
81
|
+
spanDestinations: /* @__PURE__ */ new WeakMap()
|
|
82
|
+
};
|
|
83
|
+
holder[STATE_KEY] = current;
|
|
84
|
+
}
|
|
85
|
+
return current;
|
|
86
|
+
}
|
|
87
|
+
function currentReplayTraceDestination() {
|
|
88
|
+
var _a, _b;
|
|
89
|
+
const routing = state();
|
|
90
|
+
return (_b = (_a = routing.storage) == null ? void 0 : _a.getStore()) != null ? _b : routing.syncDestination;
|
|
91
|
+
}
|
|
92
|
+
function rememberReplayTraceDestination(span) {
|
|
93
|
+
const destination = currentReplayTraceDestination();
|
|
94
|
+
if (destination) state().spanDestinations.set(span, destination);
|
|
95
|
+
}
|
|
96
|
+
function copyReplayTraceDestination(source, target) {
|
|
97
|
+
const destination = state().spanDestinations.get(source);
|
|
98
|
+
if (destination) state().spanDestinations.set(target, destination);
|
|
99
|
+
}
|
|
100
|
+
function replayTraceDestinationFor(value) {
|
|
101
|
+
if (typeof value !== "object" && typeof value !== "function" || value === null) {
|
|
102
|
+
return void 0;
|
|
103
|
+
}
|
|
104
|
+
return state().spanDestinations.get(value);
|
|
105
|
+
}
|
|
106
|
+
function recordReplayExportedSpan(destination, spanId, traceId) {
|
|
107
|
+
var _a, _b;
|
|
108
|
+
if (!destination || !spanId || !traceId) return;
|
|
109
|
+
(_a = destination.exportedSpanIds) == null ? void 0 : _a.add(spanId);
|
|
110
|
+
(_b = destination.exportedTraceIds) == null ? void 0 : _b.add(traceId);
|
|
111
|
+
}
|
|
112
|
+
function replayTraceUrl(baseUrl) {
|
|
113
|
+
return `${baseUrl.replace(/\/+$/, "")}/v1/traces`;
|
|
114
|
+
}
|
|
115
|
+
var NOOP_SPAN = {
|
|
116
|
+
traceIdB64: "",
|
|
117
|
+
spanIdB64: "",
|
|
118
|
+
eventId: "",
|
|
119
|
+
log() {
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
function getAsyncLocalStorageCtor() {
|
|
123
|
+
return globalThis.RAINDROP_ASYNC_LOCAL_STORAGE;
|
|
124
|
+
}
|
|
125
|
+
var SynchronousContextStorage = class {
|
|
126
|
+
constructor() {
|
|
127
|
+
this._stack = [];
|
|
128
|
+
}
|
|
129
|
+
isEmpty() {
|
|
130
|
+
return this._stack.length === 0;
|
|
131
|
+
}
|
|
132
|
+
getStore() {
|
|
133
|
+
return this._stack[this._stack.length - 1];
|
|
134
|
+
}
|
|
135
|
+
run(store, callback) {
|
|
136
|
+
this._stack.push(store);
|
|
137
|
+
try {
|
|
138
|
+
return callback();
|
|
139
|
+
} finally {
|
|
140
|
+
this._stack.pop();
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
var ContextManager = class {
|
|
145
|
+
};
|
|
146
|
+
var RaindropContextManager = class extends ContextManager {
|
|
147
|
+
constructor() {
|
|
148
|
+
super();
|
|
149
|
+
this._fallback = null;
|
|
150
|
+
const Ctor = getAsyncLocalStorageCtor();
|
|
151
|
+
if (Ctor) {
|
|
152
|
+
this._storage = new Ctor();
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
this._fallback = new SynchronousContextStorage();
|
|
156
|
+
this._storage = this._fallback;
|
|
157
|
+
}
|
|
158
|
+
maybeAdoptAsyncLocalStorage() {
|
|
159
|
+
if (!this._fallback || !this._fallback.isEmpty()) return;
|
|
160
|
+
const Ctor = getAsyncLocalStorageCtor();
|
|
161
|
+
if (!Ctor) return;
|
|
162
|
+
this._storage = new Ctor();
|
|
163
|
+
this._fallback = null;
|
|
164
|
+
}
|
|
165
|
+
isReady() {
|
|
166
|
+
return true;
|
|
167
|
+
}
|
|
168
|
+
getParentSpanIds() {
|
|
169
|
+
this.maybeAdoptAsyncLocalStorage();
|
|
170
|
+
const span = this._storage.getStore();
|
|
171
|
+
if (!span || span === NOOP_SPAN) return void 0;
|
|
172
|
+
return {
|
|
173
|
+
traceIdB64: span.traceIdB64,
|
|
174
|
+
spanIdB64: span.spanIdB64,
|
|
175
|
+
eventId: span.eventId
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
runInContext(span, callback) {
|
|
179
|
+
this.maybeAdoptAsyncLocalStorage();
|
|
180
|
+
return this._storage.run(span, callback);
|
|
181
|
+
}
|
|
182
|
+
getCurrentSpan() {
|
|
183
|
+
this.maybeAdoptAsyncLocalStorage();
|
|
184
|
+
return this._storage.getStore();
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
var _contextManager = null;
|
|
188
|
+
function getContextManager() {
|
|
189
|
+
if (!_contextManager) {
|
|
190
|
+
_contextManager = globalThis.RAINDROP_CONTEXT_MANAGER ? new globalThis.RAINDROP_CONTEXT_MANAGER() : new RaindropContextManager();
|
|
191
|
+
}
|
|
192
|
+
return _contextManager;
|
|
193
|
+
}
|
|
194
|
+
function currentSpan() {
|
|
195
|
+
var _a;
|
|
196
|
+
return (_a = getContextManager().getCurrentSpan()) != null ? _a : NOOP_SPAN;
|
|
197
|
+
}
|
|
198
|
+
function withCurrent(span, callback) {
|
|
199
|
+
return getContextManager().runInContext(span, callback);
|
|
200
|
+
}
|
|
201
|
+
async function getCurrentParentSpanContext() {
|
|
202
|
+
var _a, _b;
|
|
203
|
+
return (_b = getContextManager().getParentSpanIds()) != null ? _b : (_a = currentReplayTraceDestination()) == null ? void 0 : _a.parentSpanContext;
|
|
204
|
+
}
|
|
205
|
+
async function runWithParentSpanContext(ctx, fn) {
|
|
206
|
+
const cm = getContextManager();
|
|
207
|
+
const span = {
|
|
208
|
+
traceIdB64: ctx.traceIdB64,
|
|
209
|
+
spanIdB64: ctx.spanIdB64,
|
|
210
|
+
eventId: ctx.eventId
|
|
211
|
+
};
|
|
212
|
+
return cm.runInContext(span, fn);
|
|
213
|
+
}
|
|
214
|
+
async function* asyncGeneratorWithCurrent(span, gen) {
|
|
215
|
+
let nextValue;
|
|
216
|
+
while (true) {
|
|
217
|
+
const result = await withCurrent(span, async () => {
|
|
218
|
+
try {
|
|
219
|
+
return await gen.next(nextValue);
|
|
220
|
+
} catch (e) {
|
|
221
|
+
return { value: void 0, done: true, error: e };
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
if ("error" in result && result.error !== void 0) {
|
|
225
|
+
throw result.error;
|
|
226
|
+
}
|
|
227
|
+
if (result.done) {
|
|
228
|
+
return result.value;
|
|
229
|
+
}
|
|
230
|
+
nextValue = yield result.value;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
var MAX_BINDING_STACK = 128;
|
|
234
|
+
var EMPTY_STACK = [];
|
|
235
|
+
var ContextStackStorage = class {
|
|
236
|
+
/** Emitted once when this runtime cannot support an ambient push. */
|
|
237
|
+
constructor(noAmbientBindWarning) {
|
|
238
|
+
this.noAmbientBindWarning = noAmbientBindWarning;
|
|
239
|
+
this._als = null;
|
|
240
|
+
this._enterWithUsable = false;
|
|
241
|
+
this._syncStack = EMPTY_STACK;
|
|
242
|
+
this._adoptionChecked = false;
|
|
243
|
+
this._warnedNoAmbientBind = false;
|
|
244
|
+
}
|
|
245
|
+
maybeAdoptAsyncLocalStorage() {
|
|
246
|
+
if (this._adoptionChecked) return;
|
|
247
|
+
this._adoptionChecked = true;
|
|
248
|
+
const Ctor = getAsyncLocalStorageCtor();
|
|
249
|
+
if (!Ctor) return;
|
|
250
|
+
const als = new Ctor();
|
|
251
|
+
try {
|
|
252
|
+
als.run(EMPTY_STACK, () => {
|
|
253
|
+
});
|
|
254
|
+
} catch (e) {
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
this._als = als;
|
|
258
|
+
if (typeof als.enterWith === "function") {
|
|
259
|
+
try {
|
|
260
|
+
als.enterWith(EMPTY_STACK);
|
|
261
|
+
this._enterWithUsable = true;
|
|
262
|
+
} catch (e) {
|
|
263
|
+
this._enterWithUsable = false;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
getStack() {
|
|
268
|
+
var _a;
|
|
269
|
+
this.maybeAdoptAsyncLocalStorage();
|
|
270
|
+
if (this._als) return (_a = this._als.getStore()) != null ? _a : this._syncStack;
|
|
271
|
+
return this._syncStack;
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Persist an ambient stack mutation. Returns false when an ALS is adopted but
|
|
275
|
+
* `enterWith` is unusable (workerd): callers must NOT fall back to the
|
|
276
|
+
* process-shared sync stack, since concurrent isolates would cross-stamp.
|
|
277
|
+
*/
|
|
278
|
+
setAmbientStack(stack) {
|
|
279
|
+
if (this._als) {
|
|
280
|
+
if (!this._enterWithUsable) return false;
|
|
281
|
+
this._als.enterWith(stack);
|
|
282
|
+
return true;
|
|
283
|
+
}
|
|
284
|
+
this._syncStack = stack;
|
|
285
|
+
return true;
|
|
286
|
+
}
|
|
287
|
+
push(binding) {
|
|
288
|
+
this.maybeAdoptAsyncLocalStorage();
|
|
289
|
+
if (this._als && !this._enterWithUsable) {
|
|
290
|
+
this.warnNoAmbientBindOnce();
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
let stack = this.getStack();
|
|
294
|
+
if (stack.length >= MAX_BINDING_STACK) {
|
|
295
|
+
stack = stack.filter((b) => b.isLive());
|
|
296
|
+
if (stack.length >= MAX_BINDING_STACK) {
|
|
297
|
+
stack = stack.slice(stack.length - (MAX_BINDING_STACK - 1));
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
this.setAmbientStack([...stack, binding]);
|
|
301
|
+
}
|
|
302
|
+
remove(binding) {
|
|
303
|
+
binding.markDead();
|
|
304
|
+
const stack = this.getStack();
|
|
305
|
+
const pruned = stack.filter((b) => b !== binding && b.isLive());
|
|
306
|
+
if (pruned.length !== stack.length) {
|
|
307
|
+
this.setAmbientStack(pruned);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
runWith(binding, callback) {
|
|
311
|
+
this.maybeAdoptAsyncLocalStorage();
|
|
312
|
+
if (this._als) {
|
|
313
|
+
return this._als.run([...this.getStack(), binding], callback);
|
|
314
|
+
}
|
|
315
|
+
const previous = this._syncStack;
|
|
316
|
+
this._syncStack = [...previous, binding];
|
|
317
|
+
try {
|
|
318
|
+
return callback();
|
|
319
|
+
} finally {
|
|
320
|
+
this._syncStack = previous;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
current() {
|
|
324
|
+
const stack = this.getStack();
|
|
325
|
+
if (stack.length === 0) return void 0;
|
|
326
|
+
const top = stack[stack.length - 1];
|
|
327
|
+
if (top.isLive()) return top;
|
|
328
|
+
const pruned = stack.filter((b) => b.isLive());
|
|
329
|
+
this.setAmbientStack(pruned);
|
|
330
|
+
return pruned.length > 0 ? pruned[pruned.length - 1] : void 0;
|
|
331
|
+
}
|
|
332
|
+
warnNoAmbientBindOnce() {
|
|
333
|
+
if (this._warnedNoAmbientBind) return;
|
|
334
|
+
this._warnedNoAmbientBind = true;
|
|
335
|
+
console.warn(this.noAmbientBindWarning);
|
|
336
|
+
}
|
|
337
|
+
};
|
|
338
|
+
var NO_AMBIENT_ROUTING_BIND_WARNING = "[raindrop] This runtime's AsyncLocalStorage does not implement enterWith (e.g. Cloudflare Workers), so begin()/finish() cannot scope auto-instrumented spans to a project without cross-contaminating concurrent requests. Manual events and explicit span routing are unaffected; use asCurrent()/withSpan (run-based) to route auto-instrumented spans on this runtime.";
|
|
339
|
+
var ROUTING_STORAGE_KEY = /* @__PURE__ */ Symbol.for("raindrop.tracing.routingContextStorage");
|
|
340
|
+
function routingStorage() {
|
|
341
|
+
const holder = globalThis;
|
|
342
|
+
let storage = holder[ROUTING_STORAGE_KEY];
|
|
343
|
+
if (!storage) {
|
|
344
|
+
storage = new ContextStackStorage(NO_AMBIENT_ROUTING_BIND_WARNING);
|
|
345
|
+
holder[ROUTING_STORAGE_KEY] = storage;
|
|
346
|
+
}
|
|
347
|
+
return storage;
|
|
348
|
+
}
|
|
349
|
+
function currentRoutingBinding() {
|
|
350
|
+
const bound = routingStorage().current();
|
|
351
|
+
if (!bound) return void 0;
|
|
352
|
+
return {
|
|
353
|
+
projectId: bound.projectId,
|
|
354
|
+
authHint: bound.authHint,
|
|
355
|
+
...bound.appGit ? { appGit: bound.appGit } : {},
|
|
356
|
+
...bound.appGitSource ? { appGitSource: bound.appGitSource } : {}
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
var APP_COMMIT_SHA = "raindrop.app.commit_sha";
|
|
360
|
+
var APP_COMMIT_DIRTY = "raindrop.app.commit_dirty";
|
|
361
|
+
var APP_BRANCH = "raindrop.app.branch";
|
|
362
|
+
var APP_GIT_KEYS = [APP_COMMIT_SHA, APP_COMMIT_DIRTY, APP_BRANCH];
|
|
363
|
+
var own = (value, key) => Object.prototype.hasOwnProperty.call(value, key);
|
|
364
|
+
function appGitExplicitKeys(properties) {
|
|
365
|
+
const keys = /* @__PURE__ */ new Set();
|
|
366
|
+
if (!properties) return keys;
|
|
367
|
+
for (const key of APP_GIT_KEYS) {
|
|
368
|
+
if (own(properties, key)) keys.add(key);
|
|
369
|
+
}
|
|
370
|
+
return keys;
|
|
371
|
+
}
|
|
372
|
+
function mergeAppGitSnapshot(base, properties, explicitKeys = /* @__PURE__ */ new Set()) {
|
|
373
|
+
try {
|
|
374
|
+
const result = { ...base };
|
|
375
|
+
if (properties && own(properties, APP_COMMIT_SHA) && properties[APP_COMMIT_SHA] !== result[APP_COMMIT_SHA]) {
|
|
376
|
+
if (!explicitKeys.has(APP_COMMIT_DIRTY) && !own(properties, APP_COMMIT_DIRTY)) {
|
|
377
|
+
delete result[APP_COMMIT_DIRTY];
|
|
378
|
+
}
|
|
379
|
+
if (!explicitKeys.has(APP_BRANCH) && !own(properties, APP_BRANCH)) {
|
|
380
|
+
delete result[APP_BRANCH];
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
for (const key of APP_GIT_KEYS) {
|
|
384
|
+
if (properties && own(properties, key)) result[key] = properties[key];
|
|
385
|
+
}
|
|
386
|
+
return Object.freeze(result);
|
|
387
|
+
} catch (e) {
|
|
388
|
+
return Object.freeze({});
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
function validAppCommitSha(value) {
|
|
392
|
+
if (typeof value !== "string" || value.length > 128) return void 0;
|
|
393
|
+
const cleaned = value.trim();
|
|
394
|
+
return /^(?:[a-fA-F0-9]{40}|[a-fA-F0-9]{64})$/.test(cleaned) ? cleaned.toLowerCase() : void 0;
|
|
395
|
+
}
|
|
396
|
+
function shortString(value) {
|
|
397
|
+
if (typeof value !== "string" || value.length > 1024) return void 0;
|
|
398
|
+
const cleaned = value.trim();
|
|
399
|
+
return cleaned.length > 0 ? cleaned : void 0;
|
|
400
|
+
}
|
|
401
|
+
function flag(value) {
|
|
402
|
+
if (value === true || value === "true" || value === "1") return true;
|
|
403
|
+
if (value === false || value === "false" || value === "0") return false;
|
|
404
|
+
return void 0;
|
|
405
|
+
}
|
|
406
|
+
function environment() {
|
|
407
|
+
try {
|
|
408
|
+
return typeof process === "undefined" ? {} : process.env;
|
|
409
|
+
} catch (e) {
|
|
410
|
+
return {};
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
var DEPLOYMENTS = [
|
|
414
|
+
["VERCEL", "VERCEL_GIT_COMMIT_SHA", "VERCEL_GIT_COMMIT_REF"],
|
|
415
|
+
["NETLIFY", "COMMIT_REF", "BRANCH"],
|
|
416
|
+
["RAILWAY_ENVIRONMENT_NAME", "RAILWAY_GIT_COMMIT_SHA", "RAILWAY_GIT_BRANCH"],
|
|
417
|
+
["RAILWAY_ENVIRONMENT", "RAILWAY_GIT_COMMIT_SHA", "RAILWAY_GIT_BRANCH"],
|
|
418
|
+
["RENDER", "RENDER_GIT_COMMIT", "RENDER_GIT_BRANCH"],
|
|
419
|
+
["DYNO", "HEROKU_BUILD_COMMIT", ""],
|
|
420
|
+
["DYNO", "HEROKU_SLUG_COMMIT", ""],
|
|
421
|
+
["AWS_APP_ID", "AWS_COMMIT_ID", "AWS_BRANCH"],
|
|
422
|
+
["WORKERS_CI", "WORKERS_CI_COMMIT_SHA", "WORKERS_CI_BRANCH"]
|
|
423
|
+
];
|
|
424
|
+
var CI_SOURCES = [
|
|
425
|
+
["GITHUB_ACTIONS", "GITHUB_SHA", "GITHUB_REF_NAME"],
|
|
426
|
+
["GITLAB_CI", "CI_COMMIT_SHA", "CI_COMMIT_REF_NAME"],
|
|
427
|
+
["CIRCLECI", "CIRCLE_SHA1", "CIRCLE_BRANCH"]
|
|
428
|
+
];
|
|
429
|
+
function detectedSource(env, table, detectBranch) {
|
|
430
|
+
for (const row of table) {
|
|
431
|
+
const [marker, shaKey, branchKey] = row;
|
|
432
|
+
if (!marker || !shaKey || !shortString(env[marker]) || flag(env[marker]) === false) continue;
|
|
433
|
+
const commitSha = validAppCommitSha(env[shaKey]);
|
|
434
|
+
if (commitSha)
|
|
435
|
+
return {
|
|
436
|
+
commitSha,
|
|
437
|
+
...detectBranch && branchKey ? { branch: shortString(env[branchKey]) } : {}
|
|
438
|
+
};
|
|
439
|
+
}
|
|
440
|
+
return {};
|
|
441
|
+
}
|
|
442
|
+
var AppGitContextImpl = class {
|
|
443
|
+
constructor(options, runtime = {}) {
|
|
444
|
+
this.defaults = {};
|
|
445
|
+
this.explicit = {};
|
|
446
|
+
this.explicitKeys = /* @__PURE__ */ new Set();
|
|
447
|
+
this.disposed = false;
|
|
448
|
+
this.operations = /* @__PURE__ */ new Map();
|
|
449
|
+
this.operationOverflow = false;
|
|
450
|
+
this.detectBranch = false;
|
|
451
|
+
this.disabled = false;
|
|
452
|
+
var _a, _b, _c, _d, _e, _f;
|
|
453
|
+
try {
|
|
454
|
+
if (options === false) {
|
|
455
|
+
this.disabled = true;
|
|
456
|
+
return;
|
|
457
|
+
}
|
|
458
|
+
const opts = options != null ? options : {};
|
|
459
|
+
const env = (_a = runtime.env) != null ? _a : environment();
|
|
460
|
+
const detectBranch = (_c = (_b = flag(opts.detectBranch)) != null ? _b : flag(env.RAINDROP_GIT_DETECT_BRANCH)) != null ? _c : false;
|
|
461
|
+
this.detectBranch = detectBranch;
|
|
462
|
+
const collectBranch = detectBranch || runtime.captureLegacyReplayBranch === true;
|
|
463
|
+
const autoDetect = runtime.allowDiscovery === false ? false : (_e = (_d = flag(opts.autoDetect)) != null ? _d : flag(env.RAINDROP_GIT_AUTO_DETECT)) != null ? _e : true;
|
|
464
|
+
const configuredSha = opts.commitSha;
|
|
465
|
+
const explicitSha = configuredSha === void 0 ? env.RAINDROP_COMMIT_SHA : configuredSha;
|
|
466
|
+
if (explicitSha === null) this.explicit[APP_COMMIT_SHA] = null;
|
|
467
|
+
else if (typeof explicitSha === "string" && validAppCommitSha(explicitSha))
|
|
468
|
+
this.explicit[APP_COMMIT_SHA] = explicitSha;
|
|
469
|
+
const configuredDirty = opts.commitDirty;
|
|
470
|
+
const explicitDirty = configuredDirty === void 0 ? flag(env.RAINDROP_COMMIT_DIRTY) : configuredDirty;
|
|
471
|
+
if (explicitDirty === null || typeof explicitDirty === "boolean")
|
|
472
|
+
this.explicit[APP_COMMIT_DIRTY] = explicitDirty;
|
|
473
|
+
const configuredBranch = opts.branch;
|
|
474
|
+
const explicitBranch = configuredBranch === void 0 ? env.RAINDROP_BRANCH : configuredBranch;
|
|
475
|
+
if (explicitBranch === null) this.explicit[APP_BRANCH] = null;
|
|
476
|
+
else if (typeof explicitBranch === "string" && explicitBranch.length <= 1024)
|
|
477
|
+
this.explicit[APP_BRANCH] = explicitBranch;
|
|
478
|
+
this.explicitKeys = appGitExplicitKeys(this.explicit);
|
|
479
|
+
this.defaults = { ...this.explicit };
|
|
480
|
+
if (!autoDetect || explicitSha !== void 0) return;
|
|
481
|
+
const sourceDirectory = (_f = shortString(opts.sourceDirectory)) != null ? _f : shortString(env.RAINDROP_GIT_SOURCE_DIRECTORY);
|
|
482
|
+
if (runtime.allowLocalGit === false && !sourceDirectory) return;
|
|
483
|
+
const deploy = sourceDirectory ? {} : detectedSource(env, DEPLOYMENTS, collectBranch);
|
|
484
|
+
if (deploy.commitSha) {
|
|
485
|
+
this.accept(deploy);
|
|
486
|
+
return;
|
|
487
|
+
}
|
|
488
|
+
const fallback = sourceDirectory ? {} : detectedSource(env, CI_SOURCES, collectBranch);
|
|
489
|
+
if (runtime.resolveLocal && (runtime.allowLocalGit !== false || sourceDirectory)) {
|
|
490
|
+
this.cancel = runtime.resolveLocal(sourceDirectory, collectBranch, (result) => {
|
|
491
|
+
try {
|
|
492
|
+
if (!this.disposed) this.accept(result.commitSha ? result : fallback);
|
|
493
|
+
} catch (e) {
|
|
494
|
+
}
|
|
495
|
+
});
|
|
496
|
+
} else this.accept(fallback);
|
|
497
|
+
} catch (e) {
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
accept(result) {
|
|
501
|
+
const sha = validAppCommitSha(result.commitSha);
|
|
502
|
+
if (!sha) return;
|
|
503
|
+
this.replayBranch = shortString(result.branch);
|
|
504
|
+
this.defaults = {
|
|
505
|
+
[APP_COMMIT_SHA]: sha,
|
|
506
|
+
...typeof result.commitDirty === "boolean" ? { [APP_COMMIT_DIRTY]: result.commitDirty } : {},
|
|
507
|
+
...this.detectBranch && this.replayBranch ? { [APP_BRANCH]: this.replayBranch } : {},
|
|
508
|
+
...this.explicit
|
|
509
|
+
};
|
|
510
|
+
}
|
|
511
|
+
snapshot(properties) {
|
|
512
|
+
var _a;
|
|
513
|
+
try {
|
|
514
|
+
const binding = currentRoutingBinding();
|
|
515
|
+
const base = this.disabled && (binding == null ? void 0 : binding.appGitSource) !== "replay-override" ? {} : (_a = binding == null ? void 0 : binding.appGit) != null ? _a : this.defaults;
|
|
516
|
+
return this.mergeSnapshot(base, properties);
|
|
517
|
+
} catch (e) {
|
|
518
|
+
return Object.freeze({});
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
/** Safe defaults when no operation/span start was observed; never infer identity. */
|
|
522
|
+
snapshotExplicit(properties, eventId) {
|
|
523
|
+
try {
|
|
524
|
+
const base = { ...this.explicit };
|
|
525
|
+
const operation = eventId ? this.operations.get(eventId) : void 0;
|
|
526
|
+
if (operation) {
|
|
527
|
+
for (const key of operation.explicitKeys) base[key] = operation.snapshot[key];
|
|
528
|
+
}
|
|
529
|
+
return this.mergeSnapshot(base, properties, new Set(Object.keys(base)));
|
|
530
|
+
} catch (e) {
|
|
531
|
+
return mergeAppGitSnapshot({}, properties);
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
/** Existing replay branch reporting stays separate from opt-in event properties. */
|
|
535
|
+
snapshotForReplay() {
|
|
536
|
+
const snapshot = this.snapshot();
|
|
537
|
+
if (this.replayBranch && !own(snapshot, APP_BRANCH) && snapshot[APP_COMMIT_SHA] === this.defaults[APP_COMMIT_SHA]) {
|
|
538
|
+
return Object.freeze({ ...snapshot, [APP_BRANCH]: this.replayBranch });
|
|
539
|
+
}
|
|
540
|
+
return snapshot;
|
|
541
|
+
}
|
|
542
|
+
/** Merge a frozen span snapshot without losing client-authored provenance. */
|
|
543
|
+
mergeSnapshot(base, properties, explicitKeys = this.explicitKeys) {
|
|
544
|
+
return mergeAppGitSnapshot(base, properties, /* @__PURE__ */ new Set([...this.explicitKeys, ...explicitKeys]));
|
|
545
|
+
}
|
|
546
|
+
/** Shippers belonging to one client share this bounded operation registry. */
|
|
547
|
+
forOperation(eventId, properties, owner) {
|
|
548
|
+
if (!eventId) return this.snapshot(properties);
|
|
549
|
+
let operation = this.operations.get(eventId);
|
|
550
|
+
if (!operation) {
|
|
551
|
+
if (this.operations.size >= 1e4) this.operationOverflow = true;
|
|
552
|
+
if (this.operationOverflow) return this.mergeSnapshot({}, properties);
|
|
553
|
+
operation = {
|
|
554
|
+
snapshot: this.snapshot(properties),
|
|
555
|
+
owners: /* @__PURE__ */ new Set(),
|
|
556
|
+
unowned: owner === void 0,
|
|
557
|
+
explicitKeys: appGitExplicitKeys(properties)
|
|
558
|
+
};
|
|
559
|
+
this.operations.set(eventId, operation);
|
|
560
|
+
} else if (properties && APP_GIT_KEYS.some((key) => own(properties, key))) {
|
|
561
|
+
const explicitKeys = /* @__PURE__ */ new Set([...this.explicitKeys, ...operation.explicitKeys]);
|
|
562
|
+
operation.snapshot = this.mergeSnapshot(operation.snapshot, properties, explicitKeys);
|
|
563
|
+
for (const key of appGitExplicitKeys(properties)) operation.explicitKeys.add(key);
|
|
564
|
+
}
|
|
565
|
+
if (!owner) operation.unowned = true;
|
|
566
|
+
if (owner) operation.owners.add(owner);
|
|
567
|
+
return operation.snapshot;
|
|
568
|
+
}
|
|
569
|
+
releaseOperation(eventId, owner) {
|
|
570
|
+
const operation = this.operations.get(eventId);
|
|
571
|
+
if (!operation) return;
|
|
572
|
+
operation.owners.delete(owner);
|
|
573
|
+
if (operation.owners.size === 0 && !operation.unowned) this.operations.delete(eventId);
|
|
574
|
+
}
|
|
575
|
+
finishOperation(eventId) {
|
|
576
|
+
this.operations.delete(eventId);
|
|
577
|
+
}
|
|
578
|
+
dispose() {
|
|
579
|
+
var _a;
|
|
580
|
+
this.disposed = true;
|
|
581
|
+
try {
|
|
582
|
+
(_a = this.cancel) == null ? void 0 : _a.call(this);
|
|
583
|
+
} catch (e) {
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
};
|
|
587
|
+
var AppGitContext = AppGitContextImpl;
|
|
588
|
+
function createAppGitContext(options, runtime = {}) {
|
|
589
|
+
return new AppGitContext(options, runtime);
|
|
590
|
+
}
|
|
591
|
+
function withAppGitProperties(properties, snapshot) {
|
|
592
|
+
try {
|
|
593
|
+
const merged = { ...properties };
|
|
594
|
+
for (const key of APP_GIT_KEYS)
|
|
595
|
+
if (!own(merged, key) && own(snapshot, key)) merged[key] = snapshot[key];
|
|
596
|
+
return merged;
|
|
597
|
+
} catch (e) {
|
|
598
|
+
return properties != null ? properties : {};
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
function withAppGitAttributes(attributes, snapshot) {
|
|
602
|
+
try {
|
|
603
|
+
const present = new Set(attributes.flatMap((attr) => attr ? [attr.key] : []));
|
|
604
|
+
const added = [];
|
|
605
|
+
for (const key of APP_GIT_KEYS) {
|
|
606
|
+
if (present.has(key) || present.has(`ai.telemetry.metadata.${key}`) || present.has(`traceloop.association.properties.${key}`))
|
|
607
|
+
continue;
|
|
608
|
+
const value = snapshot[key];
|
|
609
|
+
if (typeof value === "string") added.push({ key, value: { stringValue: value } });
|
|
610
|
+
else if (typeof value === "boolean") added.push({ key, value: { boolValue: value } });
|
|
611
|
+
}
|
|
612
|
+
return added.length ? [...attributes, ...added] : attributes;
|
|
613
|
+
} catch (e) {
|
|
614
|
+
return attributes;
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
function appGitPropertiesFromAttributes(attributes) {
|
|
618
|
+
var _a, _b, _c;
|
|
619
|
+
const properties = {};
|
|
620
|
+
try {
|
|
621
|
+
for (const attr of attributes) {
|
|
622
|
+
if (!attr) continue;
|
|
623
|
+
for (const key of APP_GIT_KEYS) {
|
|
624
|
+
if (attr.key === key || attr.key === `ai.telemetry.metadata.${key}` || attr.key === `traceloop.association.properties.${key}`) {
|
|
625
|
+
properties[key] = (_c = (_b = (_a = attr.value.stringValue) != null ? _a : attr.value.boolValue) != null ? _b : attr.value.intValue) != null ? _c : null;
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
} catch (e) {
|
|
630
|
+
}
|
|
631
|
+
return properties;
|
|
632
|
+
}
|
|
66
633
|
function normalizeFeatureFlags(input) {
|
|
67
634
|
if (Array.isArray(input)) {
|
|
68
635
|
return Object.fromEntries(input.map((name) => [name, "true"]));
|
|
@@ -681,55 +1248,6 @@ function buildExportTraceServiceRequest(spans, serviceName = "raindrop.core", se
|
|
|
681
1248
|
]
|
|
682
1249
|
};
|
|
683
1250
|
}
|
|
684
|
-
var STATE_KEY = /* @__PURE__ */ Symbol.for("raindrop.tracing.replayTraceRoutingState");
|
|
685
|
-
function state() {
|
|
686
|
-
const holder = globalThis;
|
|
687
|
-
let current = holder[STATE_KEY];
|
|
688
|
-
if (!current) {
|
|
689
|
-
let storage = null;
|
|
690
|
-
try {
|
|
691
|
-
const Ctor = globalThis.RAINDROP_ASYNC_LOCAL_STORAGE;
|
|
692
|
-
if (Ctor) storage = new Ctor();
|
|
693
|
-
} catch (e) {
|
|
694
|
-
storage = null;
|
|
695
|
-
}
|
|
696
|
-
current = {
|
|
697
|
-
storage,
|
|
698
|
-
syncDestination: void 0,
|
|
699
|
-
spanDestinations: /* @__PURE__ */ new WeakMap()
|
|
700
|
-
};
|
|
701
|
-
holder[STATE_KEY] = current;
|
|
702
|
-
}
|
|
703
|
-
return current;
|
|
704
|
-
}
|
|
705
|
-
function currentReplayTraceDestination() {
|
|
706
|
-
var _a, _b;
|
|
707
|
-
const routing = state();
|
|
708
|
-
return (_b = (_a = routing.storage) == null ? void 0 : _a.getStore()) != null ? _b : routing.syncDestination;
|
|
709
|
-
}
|
|
710
|
-
function rememberReplayTraceDestination(span) {
|
|
711
|
-
const destination = currentReplayTraceDestination();
|
|
712
|
-
if (destination) state().spanDestinations.set(span, destination);
|
|
713
|
-
}
|
|
714
|
-
function copyReplayTraceDestination(source, target) {
|
|
715
|
-
const destination = state().spanDestinations.get(source);
|
|
716
|
-
if (destination) state().spanDestinations.set(target, destination);
|
|
717
|
-
}
|
|
718
|
-
function replayTraceDestinationFor(value) {
|
|
719
|
-
if (typeof value !== "object" && typeof value !== "function" || value === null) {
|
|
720
|
-
return void 0;
|
|
721
|
-
}
|
|
722
|
-
return state().spanDestinations.get(value);
|
|
723
|
-
}
|
|
724
|
-
function recordReplayExportedSpan(destination, spanId, traceId) {
|
|
725
|
-
var _a, _b;
|
|
726
|
-
if (!destination || !spanId || !traceId) return;
|
|
727
|
-
(_a = destination.exportedSpanIds) == null ? void 0 : _a.add(spanId);
|
|
728
|
-
(_b = destination.exportedTraceIds) == null ? void 0 : _b.add(traceId);
|
|
729
|
-
}
|
|
730
|
-
function replayTraceUrl(baseUrl) {
|
|
731
|
-
return `${baseUrl.replace(/\/+$/, "")}/v1/traces`;
|
|
732
|
-
}
|
|
733
1251
|
var LOCAL_DEBUGGER_ENV_VAR = "RAINDROP_LOCAL_DEBUGGER";
|
|
734
1252
|
var WORKSHOP_ENV_VAR = "RAINDROP_WORKSHOP";
|
|
735
1253
|
var DEFAULT_LOCAL_WORKSHOP_URL = "http://localhost:5899/v1/";
|
|
@@ -864,6 +1382,13 @@ function projectIdHeaders(projectId) {
|
|
|
864
1382
|
}
|
|
865
1383
|
var SHUTDOWN_DEADLINE_MS = 1e4;
|
|
866
1384
|
var POST_SHUTDOWN_TIMEOUT_MS = 5e3;
|
|
1385
|
+
function appGitOwnerId(prefix) {
|
|
1386
|
+
try {
|
|
1387
|
+
return `${prefix}:${randomUUID()}`;
|
|
1388
|
+
} catch (e) {
|
|
1389
|
+
return `${prefix}:${Date.now().toString(36)}:${Math.random().toString(36).slice(2)}`;
|
|
1390
|
+
}
|
|
1391
|
+
}
|
|
867
1392
|
function mergePatches(target, source) {
|
|
868
1393
|
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
869
1394
|
const out = { ...target, ...source };
|
|
@@ -913,22 +1438,25 @@ function eventTelemetryProperties(patch) {
|
|
|
913
1438
|
}
|
|
914
1439
|
var EventShipper = class {
|
|
915
1440
|
constructor(opts) {
|
|
1441
|
+
this.appGitOwner = appGitOwnerId("event");
|
|
916
1442
|
this.buffers = /* @__PURE__ */ new Map();
|
|
917
1443
|
this.sticky = /* @__PURE__ */ new Map();
|
|
918
1444
|
this.timers = /* @__PURE__ */ new Map();
|
|
919
1445
|
this.inFlight = /* @__PURE__ */ new Set();
|
|
920
1446
|
this.hasShutdown = false;
|
|
921
|
-
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
922
|
-
this.
|
|
923
|
-
this.
|
|
1447
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
1448
|
+
this.appGitContext = (_a = opts.appGitContext) != null ? _a : createAppGitContext(opts.appGit);
|
|
1449
|
+
this.ownsAppGitContext = opts.appGitContext === void 0;
|
|
1450
|
+
this.writeKey = (_b = opts.writeKey) == null ? void 0 : _b.trim();
|
|
1451
|
+
this.baseUrl = (_c = formatEndpoint(opts.endpoint)) != null ? _c : "https://api.raindrop.ai/v1/";
|
|
924
1452
|
this.enabled = opts.enabled !== false;
|
|
925
1453
|
this.debug = opts.debug;
|
|
926
|
-
this.partialFlushMs = (
|
|
927
|
-
this.sdkName = (
|
|
1454
|
+
this.partialFlushMs = (_d = opts.partialFlushMs) != null ? _d : 1e3;
|
|
1455
|
+
this.sdkName = (_e = opts.sdkName) != null ? _e : "core";
|
|
928
1456
|
this.prefix = `[raindrop-ai/${this.sdkName}]`;
|
|
929
|
-
this.defaultEventName = (
|
|
1457
|
+
this.defaultEventName = (_f = opts.defaultEventName) != null ? _f : "ai_generation";
|
|
930
1458
|
this.maxTextFieldCharsOpt = opts.maxTextFieldChars;
|
|
931
|
-
this.localDebuggerUrl = (
|
|
1459
|
+
this.localDebuggerUrl = (_g = resolveLocalDebuggerBaseUrl(opts.localDebuggerUrl)) != null ? _g : void 0;
|
|
932
1460
|
if (this.debug && this.localDebuggerUrl) {
|
|
933
1461
|
console.log(`${this.prefix} Local debugger mirroring: ${this.localDebuggerUrl}`);
|
|
934
1462
|
}
|
|
@@ -939,8 +1467,8 @@ var EventShipper = class {
|
|
|
939
1467
|
const isNode = typeof process !== "undefined" && typeof process.version === "string";
|
|
940
1468
|
this.context = {
|
|
941
1469
|
library: {
|
|
942
|
-
name: (
|
|
943
|
-
version: (
|
|
1470
|
+
name: (_h = opts.libraryName) != null ? _h : "@raindrop-ai/core",
|
|
1471
|
+
version: (_i = opts.libraryVersion) != null ? _i : "0.0.0"
|
|
944
1472
|
},
|
|
945
1473
|
metadata: {
|
|
946
1474
|
jsRuntime: isNode ? "node" : "web",
|
|
@@ -948,6 +1476,9 @@ var EventShipper = class {
|
|
|
948
1476
|
}
|
|
949
1477
|
};
|
|
950
1478
|
}
|
|
1479
|
+
ownAppGitContextForShutdown() {
|
|
1480
|
+
this.ownsAppGitContext = true;
|
|
1481
|
+
}
|
|
951
1482
|
isDebugEnabled() {
|
|
952
1483
|
return this.debug;
|
|
953
1484
|
}
|
|
@@ -1026,6 +1557,13 @@ var EventShipper = class {
|
|
|
1026
1557
|
}
|
|
1027
1558
|
const sticky = (_c = this.sticky.get(eventId)) != null ? _c : {};
|
|
1028
1559
|
const existing = (_d = this.buffers.get(eventId)) != null ? _d : {};
|
|
1560
|
+
patch = {
|
|
1561
|
+
...patch,
|
|
1562
|
+
properties: withAppGitProperties(
|
|
1563
|
+
patch.properties,
|
|
1564
|
+
this.appGitContext.forOperation(eventId, patch.properties, this.appGitOwner)
|
|
1565
|
+
)
|
|
1566
|
+
};
|
|
1029
1567
|
const merged = mergePatches(existing, patch);
|
|
1030
1568
|
merged.isPending = (_g = (_f = (_e = patch.isPending) != null ? _e : existing.isPending) != null ? _f : sticky.isPending) != null ? _g : true;
|
|
1031
1569
|
this.buffers.set(eventId, merged);
|
|
@@ -1039,6 +1577,8 @@ var EventShipper = class {
|
|
|
1039
1577
|
if (t) clearTimeout(t);
|
|
1040
1578
|
if (merged.isPending === false) {
|
|
1041
1579
|
await this.flushOne(eventId);
|
|
1580
|
+
if (this.appGitContext.releaseOperation) this.appGitContext.releaseOperation(eventId, this.appGitOwner);
|
|
1581
|
+
else this.appGitContext.finishOperation(eventId);
|
|
1042
1582
|
return;
|
|
1043
1583
|
}
|
|
1044
1584
|
const timeout = setTimeout(() => {
|
|
@@ -1070,6 +1610,7 @@ var EventShipper = class {
|
|
|
1070
1610
|
if (!settled) this.warnShutdownDrop("in-flight request(s) at shutdown");
|
|
1071
1611
|
} finally {
|
|
1072
1612
|
this.shutdownDeadlineAt = void 0;
|
|
1613
|
+
if (this.ownsAppGitContext) this.appGitContext.dispose();
|
|
1073
1614
|
}
|
|
1074
1615
|
}
|
|
1075
1616
|
async trackSignal(signal) {
|
|
@@ -1504,6 +2045,13 @@ function redactJsonAttributeValue(key, value) {
|
|
|
1504
2045
|
if (scrubbedJson === json) return void 0;
|
|
1505
2046
|
return { stringValue: scrubbedJson };
|
|
1506
2047
|
}
|
|
2048
|
+
function appGitOwnerId2(prefix) {
|
|
2049
|
+
try {
|
|
2050
|
+
return `${prefix}:${randomUUID()}`;
|
|
2051
|
+
} catch (e) {
|
|
2052
|
+
return `${prefix}:${Date.now().toString(36)}:${Math.random().toString(36).slice(2)}`;
|
|
2053
|
+
}
|
|
2054
|
+
}
|
|
1507
2055
|
function identityAttrs(args) {
|
|
1508
2056
|
return [
|
|
1509
2057
|
attrString("ai.telemetry.metadata.raindrop.ai.userId", args.userId),
|
|
@@ -1526,23 +2074,30 @@ function applyOtelSpanAttributeLimit(limit) {
|
|
|
1526
2074
|
}
|
|
1527
2075
|
var TraceShipper = class {
|
|
1528
2076
|
constructor(opts) {
|
|
2077
|
+
this.appGitOwner = appGitOwnerId2("trace");
|
|
2078
|
+
this.appGitEventIds = /* @__PURE__ */ new WeakMap();
|
|
2079
|
+
this.appGitActiveSpans = /* @__PURE__ */ new Map();
|
|
2080
|
+
this.appGitCaptured = /* @__PURE__ */ new WeakSet();
|
|
2081
|
+
this.appGitAdded = /* @__PURE__ */ new WeakMap();
|
|
1529
2082
|
this.queue = [];
|
|
1530
2083
|
this.inFlight = /* @__PURE__ */ new Set();
|
|
1531
2084
|
this.hasShutdown = false;
|
|
1532
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
1533
|
-
this.
|
|
1534
|
-
this.
|
|
2085
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
|
|
2086
|
+
this.appGitContext = (_a = opts.appGitContext) != null ? _a : createAppGitContext(opts.appGit);
|
|
2087
|
+
this.ownsAppGitContext = opts.appGitContext === void 0;
|
|
2088
|
+
this.writeKey = (_b = opts.writeKey) == null ? void 0 : _b.trim();
|
|
2089
|
+
this.baseUrl = (_c = formatEndpoint(opts.endpoint)) != null ? _c : "https://api.raindrop.ai/v1/";
|
|
1535
2090
|
this.enabled = opts.enabled !== false;
|
|
1536
2091
|
this.debug = opts.debug;
|
|
1537
2092
|
this.debugSpans = opts.debugSpans === true;
|
|
1538
|
-
this.flushIntervalMs = (
|
|
1539
|
-
this.maxBatchSize = (
|
|
1540
|
-
this.maxQueueSize = (
|
|
1541
|
-
this.sdkName = (
|
|
2093
|
+
this.flushIntervalMs = (_d = opts.flushIntervalMs) != null ? _d : 1e3;
|
|
2094
|
+
this.maxBatchSize = (_e = opts.maxBatchSize) != null ? _e : 50;
|
|
2095
|
+
this.maxQueueSize = (_f = opts.maxQueueSize) != null ? _f : 5e3;
|
|
2096
|
+
this.sdkName = (_g = opts.sdkName) != null ? _g : "core";
|
|
1542
2097
|
this.prefix = `[raindrop-ai/${this.sdkName}]`;
|
|
1543
|
-
this.serviceName = (
|
|
1544
|
-
this.serviceVersion = (
|
|
1545
|
-
this.localDebuggerUrl = (
|
|
2098
|
+
this.serviceName = (_h = opts.serviceName) != null ? _h : "raindrop.core";
|
|
2099
|
+
this.serviceVersion = (_i = opts.serviceVersion) != null ? _i : "0.0.0";
|
|
2100
|
+
this.localDebuggerUrl = (_j = resolveLocalDebuggerBaseUrl(opts.localDebuggerUrl)) != null ? _j : void 0;
|
|
1546
2101
|
if (this.debug && this.localDebuggerUrl) {
|
|
1547
2102
|
console.log(`${this.prefix} Local debugger mirroring: ${this.localDebuggerUrl}`);
|
|
1548
2103
|
}
|
|
@@ -1643,7 +2198,7 @@ var TraceShipper = class {
|
|
|
1643
2198
|
return { ...this.authHeaders(), ...projectIdHeaders(this.projectId) };
|
|
1644
2199
|
}
|
|
1645
2200
|
startSpan(args) {
|
|
1646
|
-
var _a, _b;
|
|
2201
|
+
var _a, _b, _c;
|
|
1647
2202
|
const ids = createSpanIds(args.parent);
|
|
1648
2203
|
const started = (_a = args.startTimeUnixNano) != null ? _a : nowUnixNanoString();
|
|
1649
2204
|
const attrs = [
|
|
@@ -1653,13 +2208,24 @@ var TraceShipper = class {
|
|
|
1653
2208
|
...Object.entries(evalScopeAttributes()).map(([key, value]) => attrString(key, value))
|
|
1654
2209
|
];
|
|
1655
2210
|
if ((_b = args.attributes) == null ? void 0 : _b.length) attrs.push(...args.attributes);
|
|
2211
|
+
const tracksAppGit = this.appGitActiveSpans.has(args.eventId) || this.appGitActiveSpans.size < 1e4;
|
|
2212
|
+
const appGitOverrides = appGitPropertiesFromAttributes(attrs);
|
|
1656
2213
|
const span = {
|
|
1657
2214
|
ids,
|
|
1658
2215
|
name: args.name,
|
|
1659
2216
|
startTimeUnixNano: started,
|
|
1660
|
-
attributes:
|
|
2217
|
+
attributes: withAppGitAttributes(
|
|
2218
|
+
attrs,
|
|
2219
|
+
this.appGitContext.forOperation(args.eventId, appGitOverrides, this.appGitOwner)
|
|
2220
|
+
)
|
|
1661
2221
|
};
|
|
1662
2222
|
rememberReplayTraceDestination(span);
|
|
2223
|
+
this.appGitCaptured.add(span);
|
|
2224
|
+
this.appGitEventIds.set(span, args.eventId);
|
|
2225
|
+
if (tracksAppGit) {
|
|
2226
|
+
this.appGitActiveSpans.set(args.eventId, ((_c = this.appGitActiveSpans.get(args.eventId)) != null ? _c : 0) + 1);
|
|
2227
|
+
}
|
|
2228
|
+
this.appGitAdded.set(span, new Set(span.attributes.filter((attr) => !attrs.includes(attr))));
|
|
1663
2229
|
const openSpan = buildOtlpSpan({
|
|
1664
2230
|
ids: span.ids,
|
|
1665
2231
|
name: span.name,
|
|
@@ -1699,11 +2265,17 @@ var TraceShipper = class {
|
|
|
1699
2265
|
});
|
|
1700
2266
|
}
|
|
1701
2267
|
endSpan(span, extra) {
|
|
1702
|
-
var _a, _b;
|
|
2268
|
+
var _a, _b, _c;
|
|
1703
2269
|
if (span.endTimeUnixNano) return;
|
|
1704
2270
|
span.endTimeUnixNano = (_a = extra == null ? void 0 : extra.endTimeUnixNano) != null ? _a : nowUnixNanoString();
|
|
1705
2271
|
if ((_b = extra == null ? void 0 : extra.attributes) == null ? void 0 : _b.length) {
|
|
2272
|
+
const overrides = appGitPropertiesFromAttributes(extra.attributes);
|
|
2273
|
+
const eventId2 = this.appGitEventIds.get(span);
|
|
2274
|
+
const snapshot = eventId2 ? this.appGitContext.forOperation(eventId2, overrides, this.appGitOwner) : overrides;
|
|
2275
|
+
const added = this.appGitAdded.get(span);
|
|
2276
|
+
span.attributes = span.attributes.filter((attr) => !(added == null ? void 0 : added.has(attr)) || !attr || !Object.prototype.hasOwnProperty.call(overrides, APP_COMMIT_SHA) && !Object.prototype.hasOwnProperty.call(overrides, attr.key));
|
|
1706
2277
|
span.attributes.push(...extra.attributes);
|
|
2278
|
+
span.attributes = withAppGitAttributes(span.attributes, snapshot);
|
|
1707
2279
|
}
|
|
1708
2280
|
let status = extra == null ? void 0 : extra.status;
|
|
1709
2281
|
if (!status && (extra == null ? void 0 : extra.error) !== void 0) {
|
|
@@ -1719,8 +2291,19 @@ var TraceShipper = class {
|
|
|
1719
2291
|
status
|
|
1720
2292
|
});
|
|
1721
2293
|
copyReplayTraceDestination(span, otlp);
|
|
2294
|
+
this.appGitCaptured.add(otlp);
|
|
1722
2295
|
this.enqueue(otlp);
|
|
1723
2296
|
this.mirrorToLocalDebugger(otlp, true);
|
|
2297
|
+
const eventId = this.appGitEventIds.get(span);
|
|
2298
|
+
if (eventId && this.appGitActiveSpans.has(eventId)) {
|
|
2299
|
+
const remaining = ((_c = this.appGitActiveSpans.get(eventId)) != null ? _c : 1) - 1;
|
|
2300
|
+
if (remaining > 0) this.appGitActiveSpans.set(eventId, remaining);
|
|
2301
|
+
else {
|
|
2302
|
+
this.appGitActiveSpans.delete(eventId);
|
|
2303
|
+
if (this.appGitContext.releaseOperation) this.appGitContext.releaseOperation(eventId, this.appGitOwner);
|
|
2304
|
+
else if (this.ownsAppGitContext) this.appGitContext.finishOperation(eventId);
|
|
2305
|
+
}
|
|
2306
|
+
}
|
|
1724
2307
|
}
|
|
1725
2308
|
createSpan(args) {
|
|
1726
2309
|
var _a;
|
|
@@ -1731,20 +2314,41 @@ var TraceShipper = class {
|
|
|
1731
2314
|
...Object.entries(evalScopeAttributes()).map(([key, value]) => attrString(key, value))
|
|
1732
2315
|
];
|
|
1733
2316
|
if ((_a = args.attributes) == null ? void 0 : _a.length) attrs.push(...args.attributes);
|
|
2317
|
+
const tracksAppGit = !this.appGitActiveSpans.has(args.eventId);
|
|
1734
2318
|
const otlp = buildOtlpSpan({
|
|
1735
2319
|
ids,
|
|
1736
2320
|
name: args.name,
|
|
1737
2321
|
startTimeUnixNano: args.startTimeUnixNano,
|
|
1738
2322
|
endTimeUnixNano: args.endTimeUnixNano,
|
|
1739
|
-
attributes:
|
|
2323
|
+
attributes: withAppGitAttributes(
|
|
2324
|
+
attrs,
|
|
2325
|
+
this.appGitContext.forOperation(
|
|
2326
|
+
args.eventId,
|
|
2327
|
+
appGitPropertiesFromAttributes(attrs),
|
|
2328
|
+
this.appGitOwner
|
|
2329
|
+
)
|
|
2330
|
+
),
|
|
1740
2331
|
status: args.status
|
|
1741
2332
|
});
|
|
1742
2333
|
rememberReplayTraceDestination(otlp);
|
|
2334
|
+
this.appGitCaptured.add(otlp);
|
|
2335
|
+
if (tracksAppGit) {
|
|
2336
|
+
if (this.appGitContext.releaseOperation) this.appGitContext.releaseOperation(args.eventId, this.appGitOwner);
|
|
2337
|
+
else if (this.ownsAppGitContext) this.appGitContext.finishOperation(args.eventId);
|
|
2338
|
+
}
|
|
1743
2339
|
this.enqueue(otlp);
|
|
1744
2340
|
this.mirrorToLocalDebugger(otlp, true);
|
|
1745
2341
|
}
|
|
1746
2342
|
enqueue(span) {
|
|
2343
|
+
var _a;
|
|
1747
2344
|
if (!this.enabled) return;
|
|
2345
|
+
const attributes = (_a = span.attributes) != null ? _a : [];
|
|
2346
|
+
const enriched = this.appGitCaptured.has(span) ? attributes : withAppGitAttributes(attributes, this.appGitContext.snapshot(appGitPropertiesFromAttributes(attributes)));
|
|
2347
|
+
if (enriched !== attributes) {
|
|
2348
|
+
const original = span;
|
|
2349
|
+
span = { ...span, attributes: enriched.filter((attr) => attr !== void 0) };
|
|
2350
|
+
copyReplayTraceDestination(original, span);
|
|
2351
|
+
}
|
|
1748
2352
|
if (this.debugSpans) {
|
|
1749
2353
|
const short = (s) => s ? s.slice(-8) : "none";
|
|
1750
2354
|
console.log(
|
|
@@ -1890,132 +2494,167 @@ var TraceShipper = class {
|
|
|
1890
2494
|
}
|
|
1891
2495
|
} finally {
|
|
1892
2496
|
this.shutdownDeadlineAt = void 0;
|
|
2497
|
+
if (this.ownsAppGitContext) this.appGitContext.dispose();
|
|
1893
2498
|
}
|
|
1894
2499
|
}
|
|
1895
2500
|
};
|
|
1896
|
-
var
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
2501
|
+
var PROMPT_TOOLS_ATTRIBUTE = "ai.prompt.tools";
|
|
2502
|
+
function isRecord(value) {
|
|
2503
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
2504
|
+
}
|
|
2505
|
+
var JSON_SCHEMA_KEYS = [
|
|
2506
|
+
"type",
|
|
2507
|
+
"properties",
|
|
2508
|
+
"$schema",
|
|
2509
|
+
"$ref",
|
|
2510
|
+
"anyOf",
|
|
2511
|
+
"oneOf",
|
|
2512
|
+
"allOf",
|
|
2513
|
+
"enum",
|
|
2514
|
+
"const",
|
|
2515
|
+
"items",
|
|
2516
|
+
"additionalProperties",
|
|
2517
|
+
"required"
|
|
2518
|
+
];
|
|
2519
|
+
function isJsonSchema(value) {
|
|
2520
|
+
if (!isRecord(value)) return false;
|
|
2521
|
+
if (typeof value["~standard"] === "object") return false;
|
|
2522
|
+
if (isRecord(value._def) || isRecord(value._zod)) return false;
|
|
2523
|
+
if (Object.keys(value).length === 0) return true;
|
|
2524
|
+
return JSON_SCHEMA_KEYS.some((key) => key in value);
|
|
2525
|
+
}
|
|
2526
|
+
function toJsonSchema(value, options) {
|
|
2527
|
+
if (value === void 0 || value === null) return void 0;
|
|
2528
|
+
if (isJsonSchema(value)) return value;
|
|
2529
|
+
if (isRecord(value) && isJsonSchema(value.jsonSchema)) return value.jsonSchema;
|
|
2530
|
+
if (isRecord(value) && isJsonSchema(value.json)) return value.json;
|
|
2531
|
+
if (options == null ? void 0 : options.schema) {
|
|
1918
2532
|
try {
|
|
1919
|
-
return
|
|
1920
|
-
}
|
|
1921
|
-
|
|
2533
|
+
return options.schema(value);
|
|
2534
|
+
} catch (e) {
|
|
2535
|
+
return void 0;
|
|
1922
2536
|
}
|
|
1923
2537
|
}
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
2538
|
+
return void 0;
|
|
2539
|
+
}
|
|
2540
|
+
function pickString(value) {
|
|
2541
|
+
return typeof value === "string" && value.length > 0 ? value : void 0;
|
|
2542
|
+
}
|
|
2543
|
+
function functionTool(name, description, schema, options) {
|
|
2544
|
+
const tool = { type: "function", name };
|
|
2545
|
+
const desc = pickString(description);
|
|
2546
|
+
if (desc !== void 0) tool.description = desc;
|
|
2547
|
+
const inputSchema = toJsonSchema(schema, options);
|
|
2548
|
+
if (inputSchema !== void 0) tool.inputSchema = inputSchema;
|
|
2549
|
+
return tool;
|
|
2550
|
+
}
|
|
2551
|
+
function normalizeToolDeclaration(tool, options, fallbackName) {
|
|
2552
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
|
|
2553
|
+
if (!isRecord(tool)) return void 0;
|
|
2554
|
+
if (tool.type === "provider-defined" || tool.type === "provider") {
|
|
2555
|
+
const name2 = (_b = (_a = pickString(tool.name)) != null ? _a : pickString(tool.id)) != null ? _b : fallbackName;
|
|
2556
|
+
if (name2 === void 0) return void 0;
|
|
2557
|
+
const declaration = { type: "provider-defined", name: name2 };
|
|
2558
|
+
const id = pickString(tool.id);
|
|
2559
|
+
if (id !== void 0) declaration.id = id;
|
|
2560
|
+
if (isRecord(tool.args)) declaration.args = tool.args;
|
|
2561
|
+
return declaration;
|
|
2562
|
+
}
|
|
2563
|
+
if (isRecord(tool.toolSpec)) {
|
|
2564
|
+
const spec = tool.toolSpec;
|
|
2565
|
+
const name2 = (_c = pickString(spec.name)) != null ? _c : fallbackName;
|
|
2566
|
+
if (name2 === void 0) return void 0;
|
|
2567
|
+
return functionTool(name2, spec.description, spec.inputSchema, options);
|
|
2568
|
+
}
|
|
2569
|
+
if (isRecord(tool.function)) {
|
|
2570
|
+
const fn = tool.function;
|
|
2571
|
+
const name2 = (_d = pickString(fn.name)) != null ? _d : fallbackName;
|
|
2572
|
+
if (name2 === void 0) return void 0;
|
|
2573
|
+
return functionTool(name2, fn.description, (_e = fn.parameters) != null ? _e : fn.input_schema, options);
|
|
2574
|
+
}
|
|
2575
|
+
const name = (_g = (_f = pickString(tool.name)) != null ? _f : pickString(tool.id)) != null ? _g : fallbackName;
|
|
2576
|
+
if (name === void 0) return void 0;
|
|
2577
|
+
const type = pickString(tool.type);
|
|
2578
|
+
if (type !== void 0 && type !== "function" && type !== "dynamic" && type !== "custom") {
|
|
2579
|
+
if (!("input_schema" in tool) && !("inputSchema" in tool) && !("parameters" in tool)) {
|
|
2580
|
+
return { type: "provider-defined", id: type, name };
|
|
2581
|
+
}
|
|
2582
|
+
}
|
|
2583
|
+
return functionTool(
|
|
2584
|
+
name,
|
|
2585
|
+
tool.description,
|
|
2586
|
+
(_j = (_i = (_h = tool.inputSchema) != null ? _h : tool.input_schema) != null ? _i : tool.parameters) != null ? _j : tool.schema,
|
|
2587
|
+
options
|
|
2588
|
+
);
|
|
2589
|
+
}
|
|
2590
|
+
var NOT_A_GEMINI_GROUP_KEYS = ["name", "id", "type", "function", "toolSpec", "cachePoint"];
|
|
2591
|
+
function isGeminiToolGroup(tool) {
|
|
2592
|
+
if (Array.isArray(tool.functionDeclarations)) return true;
|
|
2593
|
+
const keys = Object.keys(tool);
|
|
2594
|
+
if (keys.length === 0 || NOT_A_GEMINI_GROUP_KEYS.some((key) => key in tool)) return false;
|
|
2595
|
+
return keys.every((key) => isRecord(tool[key]));
|
|
2596
|
+
}
|
|
2597
|
+
function expandGeminiTool(tool, options) {
|
|
2598
|
+
const declarations = [];
|
|
2599
|
+
for (const [key, value] of Object.entries(tool)) {
|
|
2600
|
+
if (key === "functionDeclarations" && Array.isArray(value)) {
|
|
2601
|
+
for (const declaration of value) {
|
|
2602
|
+
const normalized = normalizeToolDeclaration(declaration, options);
|
|
2603
|
+
if (normalized) declarations.push(normalized);
|
|
2604
|
+
}
|
|
2605
|
+
} else if (isRecord(value)) {
|
|
2606
|
+
declarations.push({ type: "provider-defined", id: key, name: key, args: value });
|
|
1935
2607
|
}
|
|
1936
|
-
this._fallback = new SynchronousContextStorage();
|
|
1937
|
-
this._storage = this._fallback;
|
|
1938
|
-
}
|
|
1939
|
-
maybeAdoptAsyncLocalStorage() {
|
|
1940
|
-
if (!this._fallback || !this._fallback.isEmpty()) return;
|
|
1941
|
-
const Ctor = getAsyncLocalStorageCtor();
|
|
1942
|
-
if (!Ctor) return;
|
|
1943
|
-
this._storage = new Ctor();
|
|
1944
|
-
this._fallback = null;
|
|
1945
|
-
}
|
|
1946
|
-
isReady() {
|
|
1947
|
-
return true;
|
|
1948
|
-
}
|
|
1949
|
-
getParentSpanIds() {
|
|
1950
|
-
this.maybeAdoptAsyncLocalStorage();
|
|
1951
|
-
const span = this._storage.getStore();
|
|
1952
|
-
if (!span || span === NOOP_SPAN) return void 0;
|
|
1953
|
-
return {
|
|
1954
|
-
traceIdB64: span.traceIdB64,
|
|
1955
|
-
spanIdB64: span.spanIdB64,
|
|
1956
|
-
eventId: span.eventId
|
|
1957
|
-
};
|
|
1958
|
-
}
|
|
1959
|
-
runInContext(span, callback) {
|
|
1960
|
-
this.maybeAdoptAsyncLocalStorage();
|
|
1961
|
-
return this._storage.run(span, callback);
|
|
1962
|
-
}
|
|
1963
|
-
getCurrentSpan() {
|
|
1964
|
-
this.maybeAdoptAsyncLocalStorage();
|
|
1965
|
-
return this._storage.getStore();
|
|
1966
2608
|
}
|
|
1967
|
-
|
|
1968
|
-
|
|
1969
|
-
function
|
|
1970
|
-
if (
|
|
1971
|
-
|
|
2609
|
+
return declarations;
|
|
2610
|
+
}
|
|
2611
|
+
function normalizeToolDeclarations(tools, options) {
|
|
2612
|
+
if (tools === void 0 || tools === null) return void 0;
|
|
2613
|
+
if (Array.isArray(tools)) {
|
|
2614
|
+
return tools.flatMap((tool) => {
|
|
2615
|
+
if (isRecord(tool) && isGeminiToolGroup(tool)) {
|
|
2616
|
+
return expandGeminiTool(tool, options);
|
|
2617
|
+
}
|
|
2618
|
+
const declaration = normalizeToolDeclaration(tool, options);
|
|
2619
|
+
return declaration ? [declaration] : [];
|
|
2620
|
+
});
|
|
1972
2621
|
}
|
|
1973
|
-
return
|
|
2622
|
+
if (!isRecord(tools)) return void 0;
|
|
2623
|
+
return Object.entries(tools).flatMap(([key, tool]) => {
|
|
2624
|
+
const declaration = normalizeToolDeclaration(tool, options, key);
|
|
2625
|
+
return declaration ? [declaration] : [];
|
|
2626
|
+
});
|
|
1974
2627
|
}
|
|
1975
|
-
function
|
|
2628
|
+
function resolvePromptTools(args) {
|
|
1976
2629
|
var _a;
|
|
1977
|
-
|
|
1978
|
-
|
|
1979
|
-
|
|
1980
|
-
return
|
|
1981
|
-
}
|
|
1982
|
-
async function getCurrentParentSpanContext() {
|
|
1983
|
-
var _a, _b;
|
|
1984
|
-
return (_b = getContextManager().getParentSpanIds()) != null ? _b : (_a = currentReplayTraceDestination()) == null ? void 0 : _a.parentSpanContext;
|
|
2630
|
+
if (args.override !== void 0) {
|
|
2631
|
+
return (_a = normalizeToolDeclarations(args.override, args.options)) != null ? _a : [];
|
|
2632
|
+
}
|
|
2633
|
+
return normalizeToolDeclarations(args.inferred, args.options);
|
|
1985
2634
|
}
|
|
1986
|
-
|
|
1987
|
-
const
|
|
1988
|
-
|
|
1989
|
-
|
|
1990
|
-
|
|
1991
|
-
|
|
2635
|
+
function promptToolsAttribute(tools) {
|
|
2636
|
+
const values = promptToolsAttributeValue(tools);
|
|
2637
|
+
if (values === void 0) return void 0;
|
|
2638
|
+
return {
|
|
2639
|
+
key: PROMPT_TOOLS_ATTRIBUTE,
|
|
2640
|
+
value: { arrayValue: { values: values.map((stringValue) => ({ stringValue })) } }
|
|
1992
2641
|
};
|
|
1993
|
-
return cm.runInContext(span, fn);
|
|
1994
2642
|
}
|
|
1995
|
-
|
|
1996
|
-
|
|
1997
|
-
|
|
1998
|
-
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
2002
|
-
return { value: void 0, done: true, error: e };
|
|
2003
|
-
}
|
|
2004
|
-
});
|
|
2005
|
-
if ("error" in result && result.error !== void 0) {
|
|
2006
|
-
throw result.error;
|
|
2007
|
-
}
|
|
2008
|
-
if (result.done) {
|
|
2009
|
-
return result.value;
|
|
2643
|
+
function promptToolsAttributeValue(tools) {
|
|
2644
|
+
if (tools === void 0) return void 0;
|
|
2645
|
+
return tools.map((tool) => {
|
|
2646
|
+
try {
|
|
2647
|
+
return JSON.stringify(tool);
|
|
2648
|
+
} catch (e) {
|
|
2649
|
+
return JSON.stringify({ type: tool.type, name: tool.name });
|
|
2010
2650
|
}
|
|
2011
|
-
|
|
2012
|
-
}
|
|
2651
|
+
});
|
|
2013
2652
|
}
|
|
2014
2653
|
|
|
2015
2654
|
// package.json
|
|
2016
2655
|
var package_default = {
|
|
2017
2656
|
name: "@raindrop-ai/ai-sdk",
|
|
2018
|
-
version: "0.3.
|
|
2657
|
+
version: "0.3.4"};
|
|
2019
2658
|
|
|
2020
2659
|
// src/internal/version.ts
|
|
2021
2660
|
var libraryName = package_default.name;
|
|
@@ -2162,7 +2801,7 @@ function boundedStringify(value, limit) {
|
|
|
2162
2801
|
}
|
|
2163
2802
|
|
|
2164
2803
|
// src/internal/wrap/helpers.ts
|
|
2165
|
-
function
|
|
2804
|
+
function isRecord2(value) {
|
|
2166
2805
|
return typeof value === "object" && value !== null;
|
|
2167
2806
|
}
|
|
2168
2807
|
function isFunction(value) {
|
|
@@ -2201,7 +2840,7 @@ function extractModelInfo(model) {
|
|
|
2201
2840
|
}
|
|
2202
2841
|
return { modelId: model };
|
|
2203
2842
|
}
|
|
2204
|
-
if (!
|
|
2843
|
+
if (!isRecord2(model)) return {};
|
|
2205
2844
|
const provider = typeof model["provider"] === "string" ? model["provider"] : void 0;
|
|
2206
2845
|
let modelIdRaw;
|
|
2207
2846
|
if (typeof model["modelId"] === "string") {
|
|
@@ -2222,13 +2861,13 @@ function extractModelInfo(model) {
|
|
|
2222
2861
|
return { provider, modelId: modelIdRaw };
|
|
2223
2862
|
}
|
|
2224
2863
|
function extractTextOutput(result) {
|
|
2225
|
-
if (!
|
|
2864
|
+
if (!isRecord2(result)) return void 0;
|
|
2226
2865
|
const text = result["text"];
|
|
2227
2866
|
return typeof text === "string" ? text : void 0;
|
|
2228
2867
|
}
|
|
2229
2868
|
function extractObjectOutput(result) {
|
|
2230
2869
|
var _a;
|
|
2231
|
-
if (!
|
|
2870
|
+
if (!isRecord2(result)) return void 0;
|
|
2232
2871
|
const obj = result["object"];
|
|
2233
2872
|
if (obj === void 0) return void 0;
|
|
2234
2873
|
return (_a = safeJson(obj)) != null ? _a : String(obj);
|
|
@@ -2236,24 +2875,24 @@ function extractObjectOutput(result) {
|
|
|
2236
2875
|
function isAgentClass(value) {
|
|
2237
2876
|
if (typeof value !== "function") return false;
|
|
2238
2877
|
const proto = value.prototype;
|
|
2239
|
-
if (!
|
|
2878
|
+
if (!isRecord2(proto)) return false;
|
|
2240
2879
|
return typeof proto["generate"] === "function" && typeof proto["stream"] === "function";
|
|
2241
2880
|
}
|
|
2242
2881
|
function extractModel(result) {
|
|
2243
|
-
if (!
|
|
2882
|
+
if (!isRecord2(result)) return void 0;
|
|
2244
2883
|
const model = result["model"];
|
|
2245
2884
|
if (typeof model === "string" && model.length) return model;
|
|
2246
2885
|
const response = result["response"];
|
|
2247
|
-
if (
|
|
2886
|
+
if (isRecord2(response)) {
|
|
2248
2887
|
const modelId = response["modelId"];
|
|
2249
2888
|
if (typeof modelId === "string" && modelId.length) return modelId;
|
|
2250
2889
|
}
|
|
2251
2890
|
return void 0;
|
|
2252
2891
|
}
|
|
2253
2892
|
function extractFinishReason(result) {
|
|
2254
|
-
if (!
|
|
2893
|
+
if (!isRecord2(result)) return void 0;
|
|
2255
2894
|
if (typeof result["finishReason"] === "string") return result["finishReason"];
|
|
2256
|
-
if (
|
|
2895
|
+
if (isRecord2(result["finishReason"]) && typeof result["finishReason"]["unified"] === "string") {
|
|
2257
2896
|
return result["finishReason"]["unified"];
|
|
2258
2897
|
}
|
|
2259
2898
|
return void 0;
|
|
@@ -2297,7 +2936,7 @@ function attachmentMediaType(part) {
|
|
|
2297
2936
|
if (typeof part["mediaType"] === "string") return part["mediaType"];
|
|
2298
2937
|
if (typeof part["mimeType"] === "string") return part["mimeType"];
|
|
2299
2938
|
const file = part["file"];
|
|
2300
|
-
if (
|
|
2939
|
+
if (isRecord2(file)) {
|
|
2301
2940
|
if (typeof file["mediaType"] === "string") return file["mediaType"];
|
|
2302
2941
|
if (typeof file["mimeType"] === "string") return file["mimeType"];
|
|
2303
2942
|
}
|
|
@@ -2307,7 +2946,7 @@ function attachmentName(part) {
|
|
|
2307
2946
|
if (typeof part["filename"] === "string") return part["filename"];
|
|
2308
2947
|
if (typeof part["name"] === "string") return part["name"];
|
|
2309
2948
|
const file = part["file"];
|
|
2310
|
-
if (
|
|
2949
|
+
if (isRecord2(file)) {
|
|
2311
2950
|
if (typeof file["filename"] === "string") return file["filename"];
|
|
2312
2951
|
if (typeof file["name"] === "string") return file["name"];
|
|
2313
2952
|
}
|
|
@@ -2316,7 +2955,7 @@ function attachmentName(part) {
|
|
|
2316
2955
|
function attachmentData(part) {
|
|
2317
2956
|
if ("data" in part) return part["data"];
|
|
2318
2957
|
const file = part["file"];
|
|
2319
|
-
if (
|
|
2958
|
+
if (isRecord2(file)) {
|
|
2320
2959
|
if ("file_data" in file) return file["file_data"];
|
|
2321
2960
|
if ("data" in file) return file["data"];
|
|
2322
2961
|
}
|
|
@@ -2334,7 +2973,7 @@ function contentPartToAttachment(part, role) {
|
|
|
2334
2973
|
}
|
|
2335
2974
|
if (partType === "image_url") {
|
|
2336
2975
|
const imageUrlPart = part["image_url"];
|
|
2337
|
-
const imageUrlValue =
|
|
2976
|
+
const imageUrlValue = isRecord2(imageUrlPart) ? imageUrlPart["url"] : imageUrlPart;
|
|
2338
2977
|
const value = asDataUrl(imageUrlValue, "image/png");
|
|
2339
2978
|
if (!value) return void 0;
|
|
2340
2979
|
return { type: "image", role, value };
|
|
@@ -2354,7 +2993,7 @@ function attachmentsFromContent(content, role) {
|
|
|
2354
2993
|
if (!Array.isArray(content)) return void 0;
|
|
2355
2994
|
const attachments = [];
|
|
2356
2995
|
for (const part of content) {
|
|
2357
|
-
if (!
|
|
2996
|
+
if (!isRecord2(part)) continue;
|
|
2358
2997
|
const attachment = contentPartToAttachment(part, role);
|
|
2359
2998
|
if (attachment) attachments.push(attachment);
|
|
2360
2999
|
}
|
|
@@ -2387,7 +3026,7 @@ async function outputAttachmentsFromFiles(files) {
|
|
|
2387
3026
|
if (!Array.isArray(resolvedFiles)) return void 0;
|
|
2388
3027
|
const attachments = [];
|
|
2389
3028
|
for (const file of resolvedFiles) {
|
|
2390
|
-
if (!
|
|
3029
|
+
if (!isRecord2(file)) continue;
|
|
2391
3030
|
const attachment = generatedFileToAttachment(file);
|
|
2392
3031
|
if (attachment) attachments.push(attachment);
|
|
2393
3032
|
}
|
|
@@ -2398,7 +3037,7 @@ function extractTextFromMessageContent(content) {
|
|
|
2398
3037
|
if (!Array.isArray(content)) return void 0;
|
|
2399
3038
|
let result = "";
|
|
2400
3039
|
for (const part of content) {
|
|
2401
|
-
if (!
|
|
3040
|
+
if (!isRecord2(part) || part["type"] !== "text" || typeof part["text"] !== "string") continue;
|
|
2402
3041
|
result += part["text"];
|
|
2403
3042
|
}
|
|
2404
3043
|
return result.length ? result : void 0;
|
|
@@ -2415,7 +3054,7 @@ function lastUserMessageFromArgs(args) {
|
|
|
2415
3054
|
if (!messages) return void 0;
|
|
2416
3055
|
for (let i = messages.length - 1; i >= 0; i--) {
|
|
2417
3056
|
const message = messages[i];
|
|
2418
|
-
if (
|
|
3057
|
+
if (isRecord2(message) && message["role"] === "user") {
|
|
2419
3058
|
return message;
|
|
2420
3059
|
}
|
|
2421
3060
|
}
|
|
@@ -2423,24 +3062,24 @@ function lastUserMessageFromArgs(args) {
|
|
|
2423
3062
|
}
|
|
2424
3063
|
function extractInputAttachmentsFromArgs(args) {
|
|
2425
3064
|
var _a;
|
|
2426
|
-
if (!
|
|
3065
|
+
if (!isRecord2(args)) return void 0;
|
|
2427
3066
|
return attachmentsFromContent((_a = lastUserMessageFromArgs(args)) == null ? void 0 : _a["content"], "input");
|
|
2428
3067
|
}
|
|
2429
3068
|
async function extractOutputAttachmentsFromResult(result) {
|
|
2430
|
-
if (!
|
|
3069
|
+
if (!isRecord2(result)) return void 0;
|
|
2431
3070
|
const fileAttachments = await outputAttachmentsFromFiles(result["files"]);
|
|
2432
3071
|
if (fileAttachments == null ? void 0 : fileAttachments.length) return fileAttachments;
|
|
2433
3072
|
const responseMessages = extractResponseMessages(result);
|
|
2434
3073
|
for (let i = responseMessages.length - 1; i >= 0; i--) {
|
|
2435
3074
|
const message = responseMessages[i];
|
|
2436
|
-
if (!
|
|
3075
|
+
if (!isRecord2(message) || message["role"] !== "assistant") continue;
|
|
2437
3076
|
return attachmentsFromContent(message["content"], "output");
|
|
2438
3077
|
}
|
|
2439
3078
|
return attachmentsFromContent(result["content"], "output");
|
|
2440
3079
|
}
|
|
2441
3080
|
function lastUserMessageTextFromArgs(args) {
|
|
2442
3081
|
var _a, _b;
|
|
2443
|
-
if (!
|
|
3082
|
+
if (!isRecord2(args)) return void 0;
|
|
2444
3083
|
const content = (_a = lastUserMessageFromArgs(args)) == null ? void 0 : _a["content"];
|
|
2445
3084
|
if (content === void 0) return void 0;
|
|
2446
3085
|
const text = extractTextFromMessageContent(content);
|
|
@@ -2449,13 +3088,13 @@ function lastUserMessageTextFromArgs(args) {
|
|
|
2449
3088
|
}
|
|
2450
3089
|
function extractInputFromArgs(args) {
|
|
2451
3090
|
var _a;
|
|
2452
|
-
if (!
|
|
3091
|
+
if (!isRecord2(args)) return void 0;
|
|
2453
3092
|
const prompt = args["prompt"];
|
|
2454
3093
|
if (typeof prompt === "string") return prompt;
|
|
2455
3094
|
const messages = messagesFromArgs(args);
|
|
2456
3095
|
if (Array.isArray(messages) && messages.length > 0) {
|
|
2457
3096
|
const last = messages[messages.length - 1];
|
|
2458
|
-
if (
|
|
3097
|
+
if (isRecord2(last)) {
|
|
2459
3098
|
const content = last["content"];
|
|
2460
3099
|
const text = extractTextFromMessageContent(content);
|
|
2461
3100
|
if (text !== void 0) return text;
|
|
@@ -2469,7 +3108,7 @@ function extractInputFromArgs(args) {
|
|
|
2469
3108
|
return safeJson(args);
|
|
2470
3109
|
}
|
|
2471
3110
|
function coerceMessagesFromArgs(args) {
|
|
2472
|
-
if (!
|
|
3111
|
+
if (!isRecord2(args)) return [];
|
|
2473
3112
|
const result = [];
|
|
2474
3113
|
if (typeof args["system"] === "string" && args["system"]) {
|
|
2475
3114
|
result.push({ role: "system", content: args["system"] });
|
|
@@ -2477,7 +3116,7 @@ function coerceMessagesFromArgs(args) {
|
|
|
2477
3116
|
const messages = messagesFromArgs(args);
|
|
2478
3117
|
if (Array.isArray(messages)) {
|
|
2479
3118
|
for (const message of messages) {
|
|
2480
|
-
if (
|
|
3119
|
+
if (isRecord2(message) && typeof message["role"] === "string") {
|
|
2481
3120
|
result.push(message);
|
|
2482
3121
|
}
|
|
2483
3122
|
}
|
|
@@ -2489,21 +3128,21 @@ function coerceMessagesFromArgs(args) {
|
|
|
2489
3128
|
return result;
|
|
2490
3129
|
}
|
|
2491
3130
|
function extractResponseMessages(result) {
|
|
2492
|
-
if (!
|
|
3131
|
+
if (!isRecord2(result)) return [];
|
|
2493
3132
|
const response = result["response"];
|
|
2494
|
-
if (
|
|
3133
|
+
if (isRecord2(response) && Array.isArray(response["messages"])) {
|
|
2495
3134
|
return response["messages"].filter(
|
|
2496
|
-
(message) =>
|
|
3135
|
+
(message) => isRecord2(message) && typeof message["role"] === "string" && "content" in message
|
|
2497
3136
|
).map((message) => message);
|
|
2498
3137
|
}
|
|
2499
3138
|
const steps = result["steps"];
|
|
2500
3139
|
if (Array.isArray(steps) && steps.length > 0) {
|
|
2501
3140
|
const lastStep = steps[steps.length - 1];
|
|
2502
|
-
if (
|
|
3141
|
+
if (isRecord2(lastStep) && isRecord2(lastStep["response"])) {
|
|
2503
3142
|
const responseMessages = lastStep["response"]["messages"];
|
|
2504
3143
|
if (Array.isArray(responseMessages)) {
|
|
2505
3144
|
return responseMessages.filter(
|
|
2506
|
-
(message) =>
|
|
3145
|
+
(message) => isRecord2(message) && typeof message["role"] === "string" && "content" in message
|
|
2507
3146
|
).map((message) => message);
|
|
2508
3147
|
}
|
|
2509
3148
|
}
|
|
@@ -2524,7 +3163,7 @@ function isTranscriptToolResultError(part, result) {
|
|
|
2524
3163
|
if (part["isError"] === true || part["type"] === "tool-error") {
|
|
2525
3164
|
return true;
|
|
2526
3165
|
}
|
|
2527
|
-
if (
|
|
3166
|
+
if (isRecord2(result) && typeof result["type"] === "string") {
|
|
2528
3167
|
return result["type"].startsWith("error") || result["type"] === "execution-denied";
|
|
2529
3168
|
}
|
|
2530
3169
|
return false;
|
|
@@ -2533,7 +3172,7 @@ function getTranscriptToolErrorMessage(result) {
|
|
|
2533
3172
|
if (typeof result === "string" && result.length > 0) {
|
|
2534
3173
|
return result;
|
|
2535
3174
|
}
|
|
2536
|
-
if (
|
|
3175
|
+
if (isRecord2(result)) {
|
|
2537
3176
|
if (typeof result["value"] === "string" && result["value"].length > 0) {
|
|
2538
3177
|
return result["value"];
|
|
2539
3178
|
}
|
|
@@ -2547,12 +3186,12 @@ function getTranscriptToolErrorMessage(result) {
|
|
|
2547
3186
|
return safeJsonWithUint8(result);
|
|
2548
3187
|
}
|
|
2549
3188
|
function getTranscriptMessageParts(message) {
|
|
2550
|
-
if (!
|
|
3189
|
+
if (!isRecord2(message)) return [];
|
|
2551
3190
|
const content = message["content"];
|
|
2552
3191
|
if (Array.isArray(content)) {
|
|
2553
|
-
return content.filter(
|
|
3192
|
+
return content.filter(isRecord2);
|
|
2554
3193
|
}
|
|
2555
|
-
return
|
|
3194
|
+
return isRecord2(content) ? [content] : [];
|
|
2556
3195
|
}
|
|
2557
3196
|
function getTranscriptToolCallInput(part) {
|
|
2558
3197
|
return "input" in part ? part["input"] : "args" in part ? part["args"] : void 0;
|
|
@@ -2653,7 +3292,7 @@ function extractTextFromLmContent(content) {
|
|
|
2653
3292
|
if (!Array.isArray(content)) return void 0;
|
|
2654
3293
|
let result = "";
|
|
2655
3294
|
for (const part of content) {
|
|
2656
|
-
if (
|
|
3295
|
+
if (isRecord2(part) && part["type"] === "text" && typeof part["text"] === "string") {
|
|
2657
3296
|
result += part["text"];
|
|
2658
3297
|
}
|
|
2659
3298
|
}
|
|
@@ -2663,7 +3302,7 @@ function extractToolCallsFromLmContent(content) {
|
|
|
2663
3302
|
if (!Array.isArray(content)) return void 0;
|
|
2664
3303
|
const calls = [];
|
|
2665
3304
|
for (const part of content) {
|
|
2666
|
-
if (!
|
|
3305
|
+
if (!isRecord2(part) || part["type"] !== "tool-call") continue;
|
|
2667
3306
|
const toolCallId = getTranscriptToolCallId(part);
|
|
2668
3307
|
const toolName = typeof part["toolName"] === "string" ? part["toolName"] : void 0;
|
|
2669
3308
|
if (toolCallId || toolName) {
|
|
@@ -2673,15 +3312,15 @@ function extractToolCallsFromLmContent(content) {
|
|
|
2673
3312
|
return calls.length ? calls : void 0;
|
|
2674
3313
|
}
|
|
2675
3314
|
function extractExperimentalTelemetry(args) {
|
|
2676
|
-
if (!
|
|
3315
|
+
if (!isRecord2(args)) return void 0;
|
|
2677
3316
|
const telemetryConfig = args["experimental_telemetry"];
|
|
2678
|
-
if (!
|
|
3317
|
+
if (!isRecord2(telemetryConfig)) return void 0;
|
|
2679
3318
|
return {
|
|
2680
3319
|
functionId: typeof telemetryConfig["functionId"] === "string" ? telemetryConfig["functionId"] : void 0,
|
|
2681
3320
|
isEnabled: typeof telemetryConfig["isEnabled"] === "boolean" ? telemetryConfig["isEnabled"] : void 0,
|
|
2682
3321
|
recordInputs: typeof telemetryConfig["recordInputs"] === "boolean" ? telemetryConfig["recordInputs"] : void 0,
|
|
2683
3322
|
recordOutputs: typeof telemetryConfig["recordOutputs"] === "boolean" ? telemetryConfig["recordOutputs"] : void 0,
|
|
2684
|
-
metadata:
|
|
3323
|
+
metadata: isRecord2(telemetryConfig["metadata"]) ? telemetryConfig["metadata"] : void 0
|
|
2685
3324
|
};
|
|
2686
3325
|
}
|
|
2687
3326
|
function opName(operationId, functionId) {
|
|
@@ -2709,7 +3348,7 @@ function attrsFromTelemetryMetadata(metadata) {
|
|
|
2709
3348
|
});
|
|
2710
3349
|
}
|
|
2711
3350
|
function attrsFromHeaders(headers) {
|
|
2712
|
-
if (!
|
|
3351
|
+
if (!isRecord2(headers)) return [];
|
|
2713
3352
|
return Object.entries(headers).filter(([, v]) => typeof v === "string").map(([k, v]) => attrString(`ai.request.headers.${k}`, v));
|
|
2714
3353
|
}
|
|
2715
3354
|
function attrsFromModelProvider(provider) {
|
|
@@ -2722,7 +3361,7 @@ function attrsFromModelProvider(provider) {
|
|
|
2722
3361
|
];
|
|
2723
3362
|
}
|
|
2724
3363
|
function attrsFromSettings(args) {
|
|
2725
|
-
if (!
|
|
3364
|
+
if (!isRecord2(args)) return [];
|
|
2726
3365
|
const result = [];
|
|
2727
3366
|
const settingKeys = [
|
|
2728
3367
|
"maxRetries",
|
|
@@ -2750,7 +3389,7 @@ function attrsFromSettings(args) {
|
|
|
2750
3389
|
return result;
|
|
2751
3390
|
}
|
|
2752
3391
|
function attrsFromGenAiRequest(options) {
|
|
2753
|
-
if (!
|
|
3392
|
+
if (!isRecord2(options)) return [];
|
|
2754
3393
|
return [
|
|
2755
3394
|
attrDouble(
|
|
2756
3395
|
"gen_ai.request.frequency_penalty",
|
|
@@ -2780,7 +3419,7 @@ function attrsFromGenAiRequest(options) {
|
|
|
2780
3419
|
];
|
|
2781
3420
|
}
|
|
2782
3421
|
function attrProviderOptions(options) {
|
|
2783
|
-
if (!
|
|
3422
|
+
if (!isRecord2(options) || !isRecord2(options["providerOptions"])) return void 0;
|
|
2784
3423
|
const json = safeJsonWithUint8(options["providerOptions"]);
|
|
2785
3424
|
return json ? attrString("ai.request.providerOptions", json) : void 0;
|
|
2786
3425
|
}
|
|
@@ -2822,10 +3461,10 @@ function readRaindropCallMetadataFromArgs(args) {
|
|
|
2822
3461
|
var _a;
|
|
2823
3462
|
if (args.length === 0) return void 0;
|
|
2824
3463
|
const first = args[0];
|
|
2825
|
-
if (!
|
|
2826
|
-
const topLevel =
|
|
2827
|
-
const stable =
|
|
2828
|
-
const stableMetadata =
|
|
3464
|
+
if (!isRecord2(first)) return void 0;
|
|
3465
|
+
const topLevel = isRecord2(first["metadata"]) ? first["metadata"] : void 0;
|
|
3466
|
+
const stable = isRecord2(first["telemetry"]) ? first["telemetry"]["metadata"] : void 0;
|
|
3467
|
+
const stableMetadata = isRecord2(stable) ? stable : void 0;
|
|
2829
3468
|
const exp = extractExperimentalTelemetry(first);
|
|
2830
3469
|
const expMetadata = (exp == null ? void 0 : exp.metadata) && typeof exp.metadata === "object" ? exp.metadata : void 0;
|
|
2831
3470
|
const raw = (_a = topLevel != null ? topLevel : stableMetadata) != null ? _a : expMetadata;
|
|
@@ -3265,38 +3904,38 @@ var CACHE_WRITE_METADATA_KEYS = [
|
|
|
3265
3904
|
["bedrock", "cacheWriteInputTokens"]
|
|
3266
3905
|
];
|
|
3267
3906
|
function cacheWriteTokensFromProviderMetadata(providerMetadata) {
|
|
3268
|
-
if (!
|
|
3907
|
+
if (!isRecord2(providerMetadata)) return void 0;
|
|
3269
3908
|
return firstTokenCount(
|
|
3270
3909
|
...CACHE_WRITE_METADATA_KEYS.flatMap(([namespace, key]) => {
|
|
3271
3910
|
const scope = providerMetadata[namespace];
|
|
3272
|
-
if (!
|
|
3911
|
+
if (!isRecord2(scope)) return [];
|
|
3273
3912
|
const usage = scope["usage"];
|
|
3274
|
-
return [scope[key],
|
|
3913
|
+
return [scope[key], isRecord2(usage) ? usage[key] : void 0];
|
|
3275
3914
|
})
|
|
3276
3915
|
);
|
|
3277
3916
|
}
|
|
3278
3917
|
function extractUsageMetricsFromUsage(usage, _provider, providerMetadata) {
|
|
3279
|
-
if (!
|
|
3918
|
+
if (!isRecord2(usage)) return {};
|
|
3280
3919
|
const inputTokenValue = usage["inputTokens"];
|
|
3281
3920
|
const outputTokenValue = usage["outputTokens"];
|
|
3282
3921
|
const inputTokenDetails = usage["inputTokenDetails"];
|
|
3283
3922
|
const outputTokenDetails = usage["outputTokenDetails"];
|
|
3284
|
-
const hasStructuredUsage =
|
|
3923
|
+
const hasStructuredUsage = isRecord2(inputTokenValue) || isRecord2(outputTokenValue) || isRecord2(inputTokenDetails) || isRecord2(outputTokenDetails);
|
|
3285
3924
|
const inputTokens = firstTokenCount(
|
|
3286
|
-
|
|
3925
|
+
isRecord2(inputTokenValue) ? inputTokenValue["total"] : void 0,
|
|
3287
3926
|
inputTokenValue,
|
|
3288
3927
|
usage["promptTokens"],
|
|
3289
3928
|
usage["prompt_tokens"]
|
|
3290
3929
|
);
|
|
3291
3930
|
const outputTokens = firstTokenCount(
|
|
3292
|
-
|
|
3931
|
+
isRecord2(outputTokenValue) ? outputTokenValue["total"] : void 0,
|
|
3293
3932
|
outputTokenValue,
|
|
3294
3933
|
usage["completionTokens"],
|
|
3295
3934
|
usage["completion_tokens"]
|
|
3296
3935
|
);
|
|
3297
3936
|
const reasoningTokens = firstTokenCount(
|
|
3298
|
-
|
|
3299
|
-
|
|
3937
|
+
isRecord2(outputTokenValue) ? outputTokenValue["reasoning"] : void 0,
|
|
3938
|
+
isRecord2(outputTokenDetails) ? outputTokenDetails["reasoningTokens"] : void 0,
|
|
3300
3939
|
usage["reasoningTokens"],
|
|
3301
3940
|
usage["completionReasoningTokens"],
|
|
3302
3941
|
usage["completion_reasoning_tokens"],
|
|
@@ -3305,27 +3944,27 @@ function extractUsageMetricsFromUsage(usage, _provider, providerMetadata) {
|
|
|
3305
3944
|
usage["thinking_tokens"]
|
|
3306
3945
|
);
|
|
3307
3946
|
const cacheReadInputTokens = firstTokenCount(
|
|
3308
|
-
|
|
3309
|
-
|
|
3947
|
+
isRecord2(inputTokenValue) ? inputTokenValue["cacheRead"] : void 0,
|
|
3948
|
+
isRecord2(inputTokenDetails) ? inputTokenDetails["cacheReadTokens"] : void 0,
|
|
3310
3949
|
usage["cachedInputTokens"],
|
|
3311
3950
|
usage["promptCachedTokens"],
|
|
3312
3951
|
usage["prompt_cached_tokens"]
|
|
3313
3952
|
);
|
|
3314
3953
|
const cacheWriteInputTokens = firstTokenCount(
|
|
3315
|
-
|
|
3316
|
-
|
|
3954
|
+
isRecord2(inputTokenValue) ? inputTokenValue["cacheWrite"] : void 0,
|
|
3955
|
+
isRecord2(inputTokenDetails) ? inputTokenDetails["cacheWriteTokens"] : void 0,
|
|
3317
3956
|
usage["cacheWriteInputTokens"],
|
|
3318
3957
|
usage["cacheCreationInputTokens"],
|
|
3319
3958
|
usage["cache_creation_input_tokens"],
|
|
3320
3959
|
cacheWriteTokensFromProviderMetadata(providerMetadata)
|
|
3321
3960
|
);
|
|
3322
3961
|
const nonCachedInputTokens = firstTokenCount(
|
|
3323
|
-
|
|
3324
|
-
|
|
3962
|
+
isRecord2(inputTokenValue) ? inputTokenValue["noCache"] : void 0,
|
|
3963
|
+
isRecord2(inputTokenDetails) ? inputTokenDetails["noCacheTokens"] : void 0
|
|
3325
3964
|
);
|
|
3326
3965
|
const nonReasoningOutputTokens = firstTokenCount(
|
|
3327
|
-
|
|
3328
|
-
|
|
3966
|
+
isRecord2(outputTokenValue) ? outputTokenValue["text"] : void 0,
|
|
3967
|
+
isRecord2(outputTokenDetails) ? outputTokenDetails["textTokens"] : void 0
|
|
3329
3968
|
);
|
|
3330
3969
|
const totalTokens = firstTokenCount(
|
|
3331
3970
|
usage["totalTokens"],
|
|
@@ -3551,17 +4190,10 @@ var RaindropTelemetryIntegration = class {
|
|
|
3551
4190
|
if (event.promptMessages) {
|
|
3552
4191
|
inputAttrs.push(attrString("ai.prompt.messages", safeJsonWithUint8(event.promptMessages)));
|
|
3553
4192
|
}
|
|
3554
|
-
|
|
3555
|
-
|
|
3556
|
-
|
|
3557
|
-
|
|
3558
|
-
event.stepTools.map((t) => {
|
|
3559
|
-
var _a;
|
|
3560
|
-
return (_a = safeJsonWithUint8(t)) != null ? _a : "";
|
|
3561
|
-
})
|
|
3562
|
-
)
|
|
3563
|
-
);
|
|
3564
|
-
}
|
|
4193
|
+
const promptTools = promptToolsAttribute(
|
|
4194
|
+
resolvePromptTools({ override: this.promptTools, inferred: event.stepTools })
|
|
4195
|
+
);
|
|
4196
|
+
if (promptTools) inputAttrs.push(promptTools);
|
|
3565
4197
|
if (event.stepToolChoice != null) {
|
|
3566
4198
|
inputAttrs.push(
|
|
3567
4199
|
attrString("ai.prompt.toolChoice", safeJsonWithUint8(event.stepToolChoice))
|
|
@@ -3909,6 +4541,7 @@ var RaindropTelemetryIntegration = class {
|
|
|
3909
4541
|
this.subagentWrapping = opts.subagentWrapping !== false;
|
|
3910
4542
|
this.debug = opts.debug === true;
|
|
3911
4543
|
this.defaultContext = opts.context;
|
|
4544
|
+
this.promptTools = opts.tools;
|
|
3912
4545
|
this.subagents = new SubagentApi({
|
|
3913
4546
|
traceShipper: this.traceShipper,
|
|
3914
4547
|
eventShipper: this.eventShipper,
|
|
@@ -4547,6 +5180,18 @@ var TraceShipper2 = class extends TraceShipper {
|
|
|
4547
5180
|
};
|
|
4548
5181
|
|
|
4549
5182
|
// src/internal/wrap/wrapAISDK.ts
|
|
5183
|
+
function promptToolsOptionsFrom(aiSDK) {
|
|
5184
|
+
if (!isRecord2(aiSDK)) return void 0;
|
|
5185
|
+
const asSchema = aiSDK["asSchema"];
|
|
5186
|
+
if (!isFunction(asSchema)) return void 0;
|
|
5187
|
+
return {
|
|
5188
|
+
schema: (schema) => {
|
|
5189
|
+
const converted = asSchema(schema);
|
|
5190
|
+
const json = isRecord2(converted) ? converted["jsonSchema"] : void 0;
|
|
5191
|
+
return isRecord2(json) ? json : void 0;
|
|
5192
|
+
}
|
|
5193
|
+
};
|
|
5194
|
+
}
|
|
4550
5195
|
var pendingStoresByShipper = /* @__PURE__ */ new WeakMap();
|
|
4551
5196
|
var PendingToolSpanStore = class _PendingToolSpanStore {
|
|
4552
5197
|
constructor() {
|
|
@@ -4603,9 +5248,9 @@ function _resetWarnedMissingUserId() {
|
|
|
4603
5248
|
warnedMissingUserId = false;
|
|
4604
5249
|
}
|
|
4605
5250
|
function extractRaindropCallOptions(options) {
|
|
4606
|
-
if (!
|
|
5251
|
+
if (!isRecord2(options)) return {};
|
|
4607
5252
|
const em = options["metadata"];
|
|
4608
|
-
if (
|
|
5253
|
+
if (isRecord2(em)) return extractRaindropMetadata(em);
|
|
4609
5254
|
const telemetry = extractExperimentalTelemetry(options);
|
|
4610
5255
|
if (telemetry == null ? void 0 : telemetry.metadata) return extractRaindropMetadata(telemetry.metadata);
|
|
4611
5256
|
return {};
|
|
@@ -4667,20 +5312,20 @@ function mergeContexts(wrapTime, callTime) {
|
|
|
4667
5312
|
return result;
|
|
4668
5313
|
}
|
|
4669
5314
|
function detectAISDKVersion(aiSDK) {
|
|
4670
|
-
if (!
|
|
5315
|
+
if (!isRecord2(aiSDK)) return "unknown";
|
|
4671
5316
|
if (isFunction(aiSDK["jsonSchema"])) return "6";
|
|
4672
5317
|
if (isFunction(aiSDK["tool"])) return "5";
|
|
4673
5318
|
return "4";
|
|
4674
5319
|
}
|
|
4675
5320
|
function hasStructuredTelemetryEvents(aiSDK) {
|
|
4676
|
-
if (!
|
|
5321
|
+
if (!isRecord2(aiSDK)) return false;
|
|
4677
5322
|
const hasRegister = isFunction(aiSDK["registerTelemetry"]) || isFunction(aiSDK["registerTelemetryIntegration"]);
|
|
4678
5323
|
const hasV7Marker = isFunction(aiSDK["experimental_streamLanguageModelCall"]) || isFunction(aiSDK["experimental_streamModelCall"]);
|
|
4679
5324
|
return hasRegister && hasV7Marker;
|
|
4680
5325
|
}
|
|
4681
5326
|
function resolveRegisterTelemetry(aiSDK) {
|
|
4682
5327
|
var _a;
|
|
4683
|
-
if (!
|
|
5328
|
+
if (!isRecord2(aiSDK)) return void 0;
|
|
4684
5329
|
const fn = (_a = aiSDK["registerTelemetry"]) != null ? _a : aiSDK["registerTelemetryIntegration"];
|
|
4685
5330
|
return isFunction(fn) ? fn : void 0;
|
|
4686
5331
|
}
|
|
@@ -4711,18 +5356,18 @@ function isAsyncIterable(value) {
|
|
|
4711
5356
|
return value !== null && typeof value === "object" && Symbol.asyncIterator in value;
|
|
4712
5357
|
}
|
|
4713
5358
|
function resolveUsageRecord(result) {
|
|
4714
|
-
if (!
|
|
5359
|
+
if (!isRecord2(result)) return void 0;
|
|
4715
5360
|
let usage;
|
|
4716
5361
|
try {
|
|
4717
|
-
if (
|
|
5362
|
+
if (isRecord2(result["totalUsage"])) {
|
|
4718
5363
|
usage = result["totalUsage"];
|
|
4719
|
-
} else if (
|
|
5364
|
+
} else if (isRecord2(result["usage"])) {
|
|
4720
5365
|
usage = result["usage"];
|
|
4721
5366
|
}
|
|
4722
5367
|
} catch (e) {
|
|
4723
5368
|
return void 0;
|
|
4724
5369
|
}
|
|
4725
|
-
return
|
|
5370
|
+
return isRecord2(usage) ? usage : void 0;
|
|
4726
5371
|
}
|
|
4727
5372
|
function extractUsageMetrics(result) {
|
|
4728
5373
|
return extractUsageMetricsFromUsage(resolveUsageRecord(result));
|
|
@@ -4743,7 +5388,7 @@ function shouldKeepEventPending(params) {
|
|
|
4743
5388
|
}
|
|
4744
5389
|
function normalizePromptAttr(arg) {
|
|
4745
5390
|
var _a, _b;
|
|
4746
|
-
if (!
|
|
5391
|
+
if (!isRecord2(arg)) return arg;
|
|
4747
5392
|
const system = arg["system"];
|
|
4748
5393
|
const prompt = arg["prompt"];
|
|
4749
5394
|
const messages = arg["messages"];
|
|
@@ -4797,7 +5442,7 @@ async function runWithRootContextAsync(rootSpan, eventId, fn) {
|
|
|
4797
5442
|
);
|
|
4798
5443
|
}
|
|
4799
5444
|
function teeStreamObjectBaseStream(result) {
|
|
4800
|
-
if (!result || !
|
|
5445
|
+
if (!result || !isRecord2(result)) return;
|
|
4801
5446
|
const baseStream = result["baseStream"];
|
|
4802
5447
|
if (!(baseStream && typeof baseStream === "object" && "tee" in baseStream)) return;
|
|
4803
5448
|
try {
|
|
@@ -4838,7 +5483,7 @@ function setupOperation(params) {
|
|
|
4838
5483
|
const inheritedParent = inherited && inherited.eventId === eventId ? { traceIdB64: inherited.traceIdB64, spanIdB64: inherited.spanIdB64 } : void 0;
|
|
4839
5484
|
const outerOperationId = `ai.${operation}`;
|
|
4840
5485
|
const { operationName, resourceName } = opName(outerOperationId, telemetry == null ? void 0 : telemetry.functionId);
|
|
4841
|
-
const modelInfoFromArgs =
|
|
5486
|
+
const modelInfoFromArgs = isRecord2(arg) ? extractModelInfo(arg["model"]) : {};
|
|
4842
5487
|
const rootSpan = sendTraces ? traceShipper.startSpan({
|
|
4843
5488
|
name: outerOperationId,
|
|
4844
5489
|
parent: inheritedParent,
|
|
@@ -4853,7 +5498,7 @@ function setupOperation(params) {
|
|
|
4853
5498
|
attrString("ai.model.id", modelInfoFromArgs.modelId),
|
|
4854
5499
|
attrString("ai.telemetry.metadata.raindrop.eventId", eventId),
|
|
4855
5500
|
...attrsFromTelemetryMetadata(telemetry == null ? void 0 : telemetry.metadata),
|
|
4856
|
-
...attrsFromHeaders(
|
|
5501
|
+
...attrsFromHeaders(isRecord2(arg) ? arg["headers"] : void 0),
|
|
4857
5502
|
...attrsFromSettings(arg),
|
|
4858
5503
|
...(telemetry == null ? void 0 : telemetry.recordInputs) === false ? [] : [attrString("ai.prompt", safeJsonWithUint8(normalizePromptAttr(arg)))]
|
|
4859
5504
|
]
|
|
@@ -4870,7 +5515,9 @@ function setupOperation(params) {
|
|
|
4870
5515
|
traceShipper,
|
|
4871
5516
|
rootParentForChildren,
|
|
4872
5517
|
selfDiagnostics: operationSelfDiagnostics,
|
|
4873
|
-
aiSDKVersion: detectAISDKVersion(aiSDK)
|
|
5518
|
+
aiSDKVersion: detectAISDKVersion(aiSDK),
|
|
5519
|
+
promptTools: options.tools,
|
|
5520
|
+
promptToolsOptions: promptToolsOptionsFrom(aiSDK)
|
|
4874
5521
|
};
|
|
4875
5522
|
const toolCalls = [];
|
|
4876
5523
|
const argsWithWrappedTools = wrapTools(arg, wrapCtx, toolCalls);
|
|
@@ -4943,8 +5590,8 @@ function createFinalize(params) {
|
|
|
4943
5590
|
endTimeUnixNano: spanEndTimeUnixNano,
|
|
4944
5591
|
keepEventPending
|
|
4945
5592
|
});
|
|
4946
|
-
const providerMetadata =
|
|
4947
|
-
const resultToolCalls =
|
|
5593
|
+
const providerMetadata = isRecord2(result) ? result["providerMetadata"] : void 0;
|
|
5594
|
+
const resultToolCalls = isRecord2(result) && Array.isArray(result["toolCalls"]) ? safeJsonWithUint8(result["toolCalls"]) : setup.toolCalls.length ? safeJsonWithUint8(setup.toolCalls) : void 0;
|
|
4948
5595
|
traceShipper.endSpan(setup.rootSpan, {
|
|
4949
5596
|
attributes: [
|
|
4950
5597
|
...((_d = setup.telemetry) == null ? void 0 : _d.recordOutputs) === false ? [] : [
|
|
@@ -5216,6 +5863,7 @@ function wrapAISDK(aiSDK, deps) {
|
|
|
5216
5863
|
sendEvents: ((_b = deps.options.send) == null ? void 0 : _b.events) !== false,
|
|
5217
5864
|
debug,
|
|
5218
5865
|
settledEventIds: deps.settledEventIds,
|
|
5866
|
+
tools: deps.options.tools,
|
|
5219
5867
|
context: {
|
|
5220
5868
|
userId: wrapTimeCtx.userId,
|
|
5221
5869
|
eventId: wrapTimeCtx.eventId,
|
|
@@ -5258,7 +5906,7 @@ function wrapAISDK(aiSDK, deps) {
|
|
|
5258
5906
|
const arg = callArgs[0];
|
|
5259
5907
|
const callMetadata = readRaindropCallMetadataFromArgs(callArgs);
|
|
5260
5908
|
const callOriginal = () => original.call(aiSDK, ...callArgs);
|
|
5261
|
-
if (selfDiagnostics2 && selfDiagnosticsOps.has(propName) &&
|
|
5909
|
+
if (selfDiagnostics2 && selfDiagnosticsOps.has(propName) && isRecord2(arg)) {
|
|
5262
5910
|
const telemetry = extractExperimentalTelemetry(arg);
|
|
5263
5911
|
const callMeta = (telemetry == null ? void 0 : telemetry.metadata) ? extractRaindropMetadata(telemetry.metadata) : {};
|
|
5264
5912
|
const perCallEventIdExplicit = (_b2 = (_a2 = callMetadata == null ? void 0 : callMetadata.eventId) != null ? _a2 : callMeta.eventId) != null ? _b2 : wrapTimeCtx.eventId;
|
|
@@ -5270,16 +5918,18 @@ function wrapAISDK(aiSDK, deps) {
|
|
|
5270
5918
|
eventShipper: deps.eventShipper,
|
|
5271
5919
|
traceShipper: deps.traceShipper,
|
|
5272
5920
|
selfDiagnostics: selfDiagnostics2,
|
|
5273
|
-
aiSDKVersion: "7"
|
|
5921
|
+
aiSDKVersion: "7",
|
|
5922
|
+
promptTools: deps.options.tools,
|
|
5923
|
+
promptToolsOptions: promptToolsOptionsFrom(aiSDK)
|
|
5274
5924
|
};
|
|
5275
|
-
const tools =
|
|
5925
|
+
const tools = isRecord2(arg["tools"]) ? { ...arg["tools"] } : {};
|
|
5276
5926
|
const toolName = selfDiagnostics2.toolName;
|
|
5277
5927
|
if (!(toolName in tools)) {
|
|
5278
5928
|
const reportTool = createSelfDiagnosticsTool(perCallCtx);
|
|
5279
5929
|
if (reportTool) tools[toolName] = reportTool;
|
|
5280
5930
|
}
|
|
5281
|
-
const existingTelemetry =
|
|
5282
|
-
const existingMetadata =
|
|
5931
|
+
const existingTelemetry = isRecord2(arg["experimental_telemetry"]) ? arg["experimental_telemetry"] : {};
|
|
5932
|
+
const existingMetadata = isRecord2(existingTelemetry["metadata"]) ? existingTelemetry["metadata"] : {};
|
|
5283
5933
|
const mergedMetadata = existingMetadata["raindrop.eventId"] ? existingMetadata : {
|
|
5284
5934
|
...existingMetadata,
|
|
5285
5935
|
"raindrop.eventId": perCallEventId,
|
|
@@ -5475,7 +6125,9 @@ function wrapAgentGenerate(generate, instance, agentSettings, className, aiSDK,
|
|
|
5475
6125
|
traceShipper: deps.traceShipper,
|
|
5476
6126
|
rootParentForChildren,
|
|
5477
6127
|
selfDiagnostics,
|
|
5478
|
-
aiSDKVersion: detectAISDKVersion(aiSDK)
|
|
6128
|
+
aiSDKVersion: detectAISDKVersion(aiSDK),
|
|
6129
|
+
promptTools: deps.options.tools,
|
|
6130
|
+
promptToolsOptions: promptToolsOptionsFrom(aiSDK)
|
|
5479
6131
|
};
|
|
5480
6132
|
const toolCalls = [];
|
|
5481
6133
|
const mergedArgsWithWrappedTools = wrapTools(mergedArgs, wrapCtx, toolCalls);
|
|
@@ -5525,8 +6177,8 @@ function wrapAgentGenerate(generate, instance, agentSettings, className, aiSDK,
|
|
|
5525
6177
|
keepEventPending: false
|
|
5526
6178
|
});
|
|
5527
6179
|
const finishReason = extractFinishReason(result);
|
|
5528
|
-
const providerMetadata =
|
|
5529
|
-
const resultToolCalls =
|
|
6180
|
+
const providerMetadata = isRecord2(result) ? result["providerMetadata"] : void 0;
|
|
6181
|
+
const resultToolCalls = isRecord2(result) && Array.isArray(result["toolCalls"]) ? safeJsonWithUint8(result["toolCalls"]) : toolCalls.length ? safeJsonWithUint8(toolCalls) : void 0;
|
|
5530
6182
|
deps.traceShipper.endSpan(rootSpan, {
|
|
5531
6183
|
attributes: [
|
|
5532
6184
|
...(telemetry == null ? void 0 : telemetry.recordOutputs) === false ? [] : [
|
|
@@ -5695,7 +6347,9 @@ function wrapAgentStream(stream, instance, agentSettings, className, aiSDK, deps
|
|
|
5695
6347
|
traceShipper: deps.traceShipper,
|
|
5696
6348
|
rootParentForChildren,
|
|
5697
6349
|
selfDiagnostics,
|
|
5698
|
-
aiSDKVersion: detectAISDKVersion(aiSDK)
|
|
6350
|
+
aiSDKVersion: detectAISDKVersion(aiSDK),
|
|
6351
|
+
promptTools: deps.options.tools,
|
|
6352
|
+
promptToolsOptions: promptToolsOptionsFrom(aiSDK)
|
|
5699
6353
|
};
|
|
5700
6354
|
const toolCalls = [];
|
|
5701
6355
|
const mergedArgsWithWrappedTools = wrapTools(mergedArgs, wrapCtx, toolCalls);
|
|
@@ -5745,8 +6399,8 @@ function wrapAgentStream(stream, instance, agentSettings, className, aiSDK, deps
|
|
|
5745
6399
|
keepEventPending: false
|
|
5746
6400
|
});
|
|
5747
6401
|
const finishReason = extractFinishReason(result);
|
|
5748
|
-
const providerMetadata =
|
|
5749
|
-
const resultToolCalls =
|
|
6402
|
+
const providerMetadata = isRecord2(result) ? result["providerMetadata"] : void 0;
|
|
6403
|
+
const resultToolCalls = isRecord2(result) && Array.isArray(result["toolCalls"]) ? safeJsonWithUint8(result["toolCalls"]) : toolCalls.length ? safeJsonWithUint8(toolCalls) : void 0;
|
|
5750
6404
|
deps.traceShipper.endSpan(rootSpan, {
|
|
5751
6405
|
attributes: [
|
|
5752
6406
|
...(telemetry == null ? void 0 : telemetry.recordOutputs) === false ? [] : [
|
|
@@ -5858,8 +6512,8 @@ function wrapAgentStream(stream, instance, agentSettings, className, aiSDK, deps
|
|
|
5858
6512
|
};
|
|
5859
6513
|
}
|
|
5860
6514
|
function wrapTools(args, ctx, toolCalls) {
|
|
5861
|
-
if (!
|
|
5862
|
-
const tools =
|
|
6515
|
+
if (!isRecord2(args)) return args;
|
|
6516
|
+
const tools = isRecord2(args["tools"]) ? { ...args["tools"] } : {};
|
|
5863
6517
|
if (ctx.selfDiagnostics) {
|
|
5864
6518
|
const reportToolName = ctx.selfDiagnostics.toolName;
|
|
5865
6519
|
if (!(reportToolName in tools)) {
|
|
@@ -5882,7 +6536,7 @@ function wrapTools(args, ctx, toolCalls) {
|
|
|
5882
6536
|
}
|
|
5883
6537
|
function wrapToolExecute(name, tool, ctx, toolCalls) {
|
|
5884
6538
|
var _a;
|
|
5885
|
-
if (!
|
|
6539
|
+
if (!isRecord2(tool) || !isFunction(tool["execute"])) return tool;
|
|
5886
6540
|
const originalExecute = tool["execute"];
|
|
5887
6541
|
const { operationName, resourceName } = opName("ai.toolCall", (_a = ctx.telemetry) == null ? void 0 : _a.functionId);
|
|
5888
6542
|
const createToolSpan = (toolCallId, toolArgs, parent) => {
|
|
@@ -5943,7 +6597,7 @@ function wrapToolExecute(name, tool, ctx, toolCalls) {
|
|
|
5943
6597
|
const wrappedExecute = function(...execArgs) {
|
|
5944
6598
|
const toolArgs = execArgs[0];
|
|
5945
6599
|
const execOptions = execArgs.length > 1 ? execArgs[1] : void 0;
|
|
5946
|
-
const toolCallId =
|
|
6600
|
+
const toolCallId = isRecord2(execOptions) && typeof execOptions["toolCallId"] === "string" ? execOptions["toolCallId"] : randomUUID();
|
|
5947
6601
|
const result = originalExecute.apply(this, execArgs);
|
|
5948
6602
|
if (isAsyncIterable(result)) {
|
|
5949
6603
|
return (async function* () {
|
|
@@ -6018,20 +6672,20 @@ function wrapToolExecute(name, tool, ctx, toolCalls) {
|
|
|
6018
6672
|
return { ...tool, execute: wrappedExecute };
|
|
6019
6673
|
}
|
|
6020
6674
|
function wrapModel(args, aiSDK, outerOperationId, ctx) {
|
|
6021
|
-
if (!
|
|
6675
|
+
if (!isRecord2(args) || !("model" in args)) return args;
|
|
6022
6676
|
let model = args["model"];
|
|
6023
6677
|
if (typeof model === "string") {
|
|
6024
6678
|
const maybeProvider = globalThis.AI_SDK_DEFAULT_PROVIDER;
|
|
6025
|
-
const gateway =
|
|
6679
|
+
const gateway = isRecord2(aiSDK) ? aiSDK["gateway"] : void 0;
|
|
6026
6680
|
const provider = maybeProvider != null ? maybeProvider : gateway;
|
|
6027
|
-
if (
|
|
6681
|
+
if (isRecord2(provider) && isFunction(provider["languageModel"])) {
|
|
6028
6682
|
try {
|
|
6029
6683
|
model = provider["languageModel"](model);
|
|
6030
6684
|
} catch (e) {
|
|
6031
6685
|
}
|
|
6032
6686
|
}
|
|
6033
6687
|
}
|
|
6034
|
-
if (!
|
|
6688
|
+
if (!isRecord2(model)) return args;
|
|
6035
6689
|
const modelInfo = extractModelInfo(model);
|
|
6036
6690
|
const doGenerateOpId = `${outerOperationId}.doGenerate`;
|
|
6037
6691
|
const doStreamOpId = `${outerOperationId}.doStream`;
|
|
@@ -6048,13 +6702,13 @@ function wrapModel(args, aiSDK, outerOperationId, ctx) {
|
|
|
6048
6702
|
try {
|
|
6049
6703
|
const result = await original.apply(target, callArgs);
|
|
6050
6704
|
if (span) endDoGenerateSpan(span, result, modelInfo, ctx);
|
|
6051
|
-
if (localDebuggerEnabled() && ctx.rootParentForChildren &&
|
|
6705
|
+
if (localDebuggerEnabled() && ctx.rootParentForChildren && isRecord2(result)) {
|
|
6052
6706
|
const traceId = ctx.rootParentForChildren.traceIdB64;
|
|
6053
6707
|
const liveMeta = getLocalDebuggerMetadata({ ...ctx.context, eventId: ctx.eventId });
|
|
6054
6708
|
const content = result["content"];
|
|
6055
6709
|
if (Array.isArray(content)) {
|
|
6056
6710
|
for (const part of content) {
|
|
6057
|
-
if (
|
|
6711
|
+
if (isRecord2(part)) {
|
|
6058
6712
|
if ((part["type"] === "reasoning" || part["type"] === "thinking") && typeof ((_a = part["text"]) != null ? _a : part["thinking"]) === "string") {
|
|
6059
6713
|
sendLocalDebuggerLiveEvent({
|
|
6060
6714
|
traceId,
|
|
@@ -6123,7 +6777,7 @@ function wrapModel(args, aiSDK, outerOperationId, ctx) {
|
|
|
6123
6777
|
});
|
|
6124
6778
|
throw error;
|
|
6125
6779
|
}
|
|
6126
|
-
const stream =
|
|
6780
|
+
const stream = isRecord2(result) ? result["stream"] : void 0;
|
|
6127
6781
|
const RS = globalThis.ReadableStream;
|
|
6128
6782
|
if (!RS || !(stream instanceof RS)) {
|
|
6129
6783
|
if (span) ctx.traceShipper.endSpan(span);
|
|
@@ -6196,7 +6850,7 @@ function wrapModel(args, aiSDK, outerOperationId, ctx) {
|
|
|
6196
6850
|
return;
|
|
6197
6851
|
}
|
|
6198
6852
|
if (firstChunkMs === void 0) firstChunkMs = Date.now();
|
|
6199
|
-
if (
|
|
6853
|
+
if (isRecord2(value)) {
|
|
6200
6854
|
const type = value["type"];
|
|
6201
6855
|
if (type === "text-delta") {
|
|
6202
6856
|
let textDelta;
|
|
@@ -6211,7 +6865,7 @@ function wrapModel(args, aiSDK, outerOperationId, ctx) {
|
|
|
6211
6865
|
}
|
|
6212
6866
|
if (type === "finish") finishReason = extractFinishReason(value);
|
|
6213
6867
|
if (type === "tool-call") toolCallsLocal.push(value);
|
|
6214
|
-
if ("response" in value &&
|
|
6868
|
+
if ("response" in value && isRecord2(value["response"])) {
|
|
6215
6869
|
const response = value["response"];
|
|
6216
6870
|
if (typeof response["id"] === "string") responseId = response["id"];
|
|
6217
6871
|
if (typeof response["modelId"] === "string")
|
|
@@ -6288,10 +6942,15 @@ function wrapModel(args, aiSDK, outerOperationId, ctx) {
|
|
|
6288
6942
|
function startDoGenerateSpan(operationId, options, modelInfo, parent, ctx) {
|
|
6289
6943
|
var _a, _b, _c;
|
|
6290
6944
|
const { operationName, resourceName } = opName(operationId, (_a = ctx.telemetry) == null ? void 0 : _a.functionId);
|
|
6291
|
-
const
|
|
6292
|
-
|
|
6293
|
-
|
|
6294
|
-
|
|
6945
|
+
const promptTools = promptToolsAttribute(
|
|
6946
|
+
resolvePromptTools({
|
|
6947
|
+
override: ctx.promptTools,
|
|
6948
|
+
inferred: isRecord2(options) ? options["tools"] : void 0,
|
|
6949
|
+
options: ctx.promptToolsOptions
|
|
6950
|
+
})
|
|
6951
|
+
);
|
|
6952
|
+
const toolChoiceJson = isRecord2(options) ? safeJsonWithUint8(options["toolChoice"]) : void 0;
|
|
6953
|
+
const promptJson = isRecord2(options) ? safeJsonWithUint8(options["prompt"]) : safeJsonWithUint8(options);
|
|
6295
6954
|
return ctx.traceShipper.startSpan({
|
|
6296
6955
|
name: operationId,
|
|
6297
6956
|
parent,
|
|
@@ -6306,7 +6965,7 @@ function startDoGenerateSpan(operationId, options, modelInfo, parent, ctx) {
|
|
|
6306
6965
|
attrString("ai.model.id", modelInfo.modelId),
|
|
6307
6966
|
...((_c = ctx.telemetry) == null ? void 0 : _c.recordInputs) === false ? [] : [
|
|
6308
6967
|
attrString("ai.prompt.messages", promptJson),
|
|
6309
|
-
|
|
6968
|
+
promptTools,
|
|
6310
6969
|
attrString("ai.prompt.toolChoice", toolChoiceJson)
|
|
6311
6970
|
],
|
|
6312
6971
|
attrString(MODEL_USAGE_ATTRIBUTES.requestModel, modelInfo.modelId),
|
|
@@ -6318,26 +6977,26 @@ function startDoGenerateSpan(operationId, options, modelInfo, parent, ctx) {
|
|
|
6318
6977
|
function endDoGenerateSpan(span, result, modelInfo, ctx) {
|
|
6319
6978
|
var _a;
|
|
6320
6979
|
const finishReason = extractFinishReason(result);
|
|
6321
|
-
const content =
|
|
6322
|
-
const response =
|
|
6323
|
-
const usage =
|
|
6324
|
-
const providerMetadata =
|
|
6980
|
+
const content = isRecord2(result) ? result["content"] : void 0;
|
|
6981
|
+
const response = isRecord2(result) ? result["response"] : void 0;
|
|
6982
|
+
const usage = isRecord2(result) ? result["usage"] : void 0;
|
|
6983
|
+
const providerMetadata = isRecord2(result) ? result["providerMetadata"] : void 0;
|
|
6325
6984
|
let responseId;
|
|
6326
|
-
if (
|
|
6985
|
+
if (isRecord2(response) && typeof response["id"] === "string") {
|
|
6327
6986
|
responseId = response["id"];
|
|
6328
6987
|
} else {
|
|
6329
6988
|
responseId = randomUUID();
|
|
6330
6989
|
}
|
|
6331
6990
|
let responseModelId;
|
|
6332
|
-
if (
|
|
6991
|
+
if (isRecord2(response) && typeof response["modelId"] === "string") {
|
|
6333
6992
|
responseModelId = response["modelId"];
|
|
6334
6993
|
} else {
|
|
6335
6994
|
responseModelId = modelInfo.modelId;
|
|
6336
6995
|
}
|
|
6337
6996
|
let responseTimestampIso;
|
|
6338
|
-
if (
|
|
6997
|
+
if (isRecord2(response) && response["timestamp"] instanceof Date) {
|
|
6339
6998
|
responseTimestampIso = response["timestamp"].toISOString();
|
|
6340
|
-
} else if (
|
|
6999
|
+
} else if (isRecord2(response) && typeof response["timestamp"] === "string") {
|
|
6341
7000
|
responseTimestampIso = response["timestamp"];
|
|
6342
7001
|
} else {
|
|
6343
7002
|
responseTimestampIso = (/* @__PURE__ */ new Date()).toISOString();
|
|
@@ -6371,10 +7030,15 @@ function endDoGenerateSpan(span, result, modelInfo, ctx) {
|
|
|
6371
7030
|
function startDoStreamSpan(operationId, options, modelInfo, parent, ctx) {
|
|
6372
7031
|
var _a, _b, _c;
|
|
6373
7032
|
const { operationName, resourceName } = opName(operationId, (_a = ctx.telemetry) == null ? void 0 : _a.functionId);
|
|
6374
|
-
const
|
|
6375
|
-
|
|
6376
|
-
|
|
6377
|
-
|
|
7033
|
+
const promptTools = promptToolsAttribute(
|
|
7034
|
+
resolvePromptTools({
|
|
7035
|
+
override: ctx.promptTools,
|
|
7036
|
+
inferred: isRecord2(options) ? options["tools"] : void 0,
|
|
7037
|
+
options: ctx.promptToolsOptions
|
|
7038
|
+
})
|
|
7039
|
+
);
|
|
7040
|
+
const toolChoiceJson = isRecord2(options) ? safeJsonWithUint8(options["toolChoice"]) : void 0;
|
|
7041
|
+
const promptJson = isRecord2(options) ? safeJsonWithUint8(options["prompt"]) : safeJsonWithUint8(options);
|
|
6378
7042
|
return ctx.traceShipper.startSpan({
|
|
6379
7043
|
name: operationId,
|
|
6380
7044
|
parent,
|
|
@@ -6389,7 +7053,7 @@ function startDoStreamSpan(operationId, options, modelInfo, parent, ctx) {
|
|
|
6389
7053
|
attrString("ai.model.id", modelInfo.modelId),
|
|
6390
7054
|
...((_c = ctx.telemetry) == null ? void 0 : _c.recordInputs) === false ? [] : [
|
|
6391
7055
|
attrString("ai.prompt.messages", promptJson),
|
|
6392
|
-
|
|
7056
|
+
promptTools,
|
|
6393
7057
|
attrString("ai.prompt.toolChoice", toolChoiceJson)
|
|
6394
7058
|
],
|
|
6395
7059
|
attrString(MODEL_USAGE_ATTRIBUTES.requestModel, modelInfo.modelId),
|
|
@@ -6403,7 +7067,7 @@ function resolveContext(context, info) {
|
|
|
6403
7067
|
return typeof context === "function" ? context(info) : context;
|
|
6404
7068
|
}
|
|
6405
7069
|
function wrapOnFinish(args, onFinish) {
|
|
6406
|
-
if (!
|
|
7070
|
+
if (!isRecord2(args)) return args;
|
|
6407
7071
|
const existing = args["onFinish"];
|
|
6408
7072
|
if (existing === void 0) {
|
|
6409
7073
|
return { ...args, onFinish: async (result) => onFinish(result) };
|
|
@@ -6513,11 +7177,11 @@ function userAttrsToOtlp(attrs) {
|
|
|
6513
7177
|
function envDebugEnabled() {
|
|
6514
7178
|
var _a;
|
|
6515
7179
|
if (typeof process === "undefined") return false;
|
|
6516
|
-
const
|
|
6517
|
-
return
|
|
7180
|
+
const flag2 = (_a = process.env) == null ? void 0 : _a.RAINDROP_AI_DEBUG;
|
|
7181
|
+
return flag2 === "1" || flag2 === "true";
|
|
6518
7182
|
}
|
|
6519
7183
|
function createRaindropAISDK(opts) {
|
|
6520
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
|
|
7184
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
|
|
6521
7185
|
const writeKey = opts.writeKey;
|
|
6522
7186
|
const eventsRequested = ((_a = opts.events) == null ? void 0 : _a.enabled) !== false;
|
|
6523
7187
|
const tracesRequested = ((_b = opts.traces) == null ? void 0 : _b.enabled) !== false;
|
|
@@ -6526,37 +7190,53 @@ function createRaindropAISDK(opts) {
|
|
|
6526
7190
|
const localWorkshopInput = opts.localWorkshopUrl === false ? null : opts.localWorkshopUrl;
|
|
6527
7191
|
const resolvedLocalDebuggerUrl = resolveLocalDebuggerBaseUrl(localWorkshopInput);
|
|
6528
7192
|
const localDebuggerUrl = localWorkshopInput === null ? null : resolvedLocalDebuggerUrl != null ? resolvedLocalDebuggerUrl : void 0;
|
|
6529
|
-
const hasDestination =
|
|
7193
|
+
const hasDestination = Boolean(writeKey == null ? void 0 : writeKey.trim()) || Boolean(resolvedLocalDebuggerUrl);
|
|
6530
7194
|
const eventsEnabled = eventsRequested && hasDestination;
|
|
6531
7195
|
const tracesEnabled = tracesRequested && hasDestination;
|
|
7196
|
+
const appGitContext = (_c = opts.appGitContext) != null ? _c : createAppGitContext(
|
|
7197
|
+
eventsEnabled || tracesEnabled ? opts.appGit : false,
|
|
7198
|
+
{ allowLocalGit: true }
|
|
7199
|
+
);
|
|
6532
7200
|
if (!hasDestination && (eventsRequested || tracesRequested)) {
|
|
6533
7201
|
console.warn(
|
|
6534
7202
|
"[raindrop-ai/ai-sdk] writeKey not provided and no local Workshop reachable; telemetry shipping is disabled"
|
|
6535
7203
|
);
|
|
6536
7204
|
}
|
|
6537
|
-
|
|
6538
|
-
|
|
6539
|
-
|
|
6540
|
-
|
|
6541
|
-
|
|
6542
|
-
|
|
6543
|
-
|
|
6544
|
-
|
|
6545
|
-
|
|
6546
|
-
|
|
6547
|
-
|
|
6548
|
-
|
|
6549
|
-
|
|
6550
|
-
|
|
6551
|
-
|
|
6552
|
-
|
|
6553
|
-
|
|
6554
|
-
|
|
6555
|
-
|
|
6556
|
-
|
|
6557
|
-
|
|
6558
|
-
|
|
6559
|
-
|
|
7205
|
+
let eventShipper;
|
|
7206
|
+
let traceShipper;
|
|
7207
|
+
try {
|
|
7208
|
+
eventShipper = new EventShipper2({
|
|
7209
|
+
writeKey,
|
|
7210
|
+
endpoint: opts.endpoint,
|
|
7211
|
+
enabled: eventsEnabled,
|
|
7212
|
+
debug: ((_d = opts.events) == null ? void 0 : _d.debug) === true || envDebug,
|
|
7213
|
+
partialFlushMs: (_e = opts.events) == null ? void 0 : _e.partialFlushMs,
|
|
7214
|
+
projectId: opts.projectId,
|
|
7215
|
+
localDebuggerUrl,
|
|
7216
|
+
appGitContext
|
|
7217
|
+
});
|
|
7218
|
+
traceShipper = new TraceShipper2({
|
|
7219
|
+
writeKey,
|
|
7220
|
+
endpoint: opts.endpoint,
|
|
7221
|
+
enabled: tracesEnabled,
|
|
7222
|
+
debug: ((_f = opts.traces) == null ? void 0 : _f.debug) === true || envDebug,
|
|
7223
|
+
debugSpans: ((_g = opts.traces) == null ? void 0 : _g.debugSpans) === true || envDebug,
|
|
7224
|
+
flushIntervalMs: (_h = opts.traces) == null ? void 0 : _h.flushIntervalMs,
|
|
7225
|
+
maxBatchSize: (_i = opts.traces) == null ? void 0 : _i.maxBatchSize,
|
|
7226
|
+
maxQueueSize: (_j = opts.traces) == null ? void 0 : _j.maxQueueSize,
|
|
7227
|
+
projectId: opts.projectId,
|
|
7228
|
+
localDebuggerUrl,
|
|
7229
|
+
transformSpan: (_k = opts.traces) == null ? void 0 : _k.transformSpan,
|
|
7230
|
+
disableDefaultRedaction: (_l = opts.traces) == null ? void 0 : _l.disableDefaultRedaction,
|
|
7231
|
+
appGitContext
|
|
7232
|
+
});
|
|
7233
|
+
} catch (error) {
|
|
7234
|
+
try {
|
|
7235
|
+
appGitContext.dispose();
|
|
7236
|
+
} catch (e) {
|
|
7237
|
+
}
|
|
7238
|
+
throw error;
|
|
7239
|
+
}
|
|
6560
7240
|
const settledEventIds = /* @__PURE__ */ new Set();
|
|
6561
7241
|
const subagentApi = new SubagentApi({
|
|
6562
7242
|
traceShipper,
|
|
@@ -6594,11 +7274,16 @@ function createRaindropAISDK(opts) {
|
|
|
6594
7274
|
];
|
|
6595
7275
|
let context;
|
|
6596
7276
|
let subagentWrapping;
|
|
7277
|
+
let tools;
|
|
6597
7278
|
if (contextOrOptions === void 0) ; else if ("context" in contextOrOptions) {
|
|
6598
|
-
|
|
6599
|
-
|
|
6600
|
-
|
|
6601
|
-
|
|
7279
|
+
const shaped = contextOrOptions;
|
|
7280
|
+
context = shaped.context;
|
|
7281
|
+
subagentWrapping = shaped.subagentWrapping;
|
|
7282
|
+
tools = shaped.tools;
|
|
7283
|
+
} else if (("subagentWrapping" in contextOrOptions || "tools" in contextOrOptions) && !FLAT_CONTEXT_KEYS.some((k) => k in contextOrOptions)) {
|
|
7284
|
+
const shaped = contextOrOptions;
|
|
7285
|
+
subagentWrapping = shaped.subagentWrapping;
|
|
7286
|
+
tools = shaped.tools;
|
|
6602
7287
|
} else {
|
|
6603
7288
|
context = contextOrOptions;
|
|
6604
7289
|
}
|
|
@@ -6610,7 +7295,8 @@ function createRaindropAISDK(opts) {
|
|
|
6610
7295
|
subagentWrapping,
|
|
6611
7296
|
debug: envDebug,
|
|
6612
7297
|
settledEventIds,
|
|
6613
|
-
context
|
|
7298
|
+
context,
|
|
7299
|
+
tools
|
|
6614
7300
|
});
|
|
6615
7301
|
},
|
|
6616
7302
|
subagents: subagentApi,
|
|
@@ -6724,16 +7410,23 @@ function createRaindropAISDK(opts) {
|
|
|
6724
7410
|
await Promise.all([eventShipper.flush(), traceShipper.flush()]);
|
|
6725
7411
|
},
|
|
6726
7412
|
async shutdown() {
|
|
6727
|
-
|
|
7413
|
+
try {
|
|
7414
|
+
await Promise.all([eventShipper.shutdown(), traceShipper.shutdown()]);
|
|
7415
|
+
} finally {
|
|
7416
|
+
try {
|
|
7417
|
+
appGitContext.dispose();
|
|
7418
|
+
} catch (e) {
|
|
7419
|
+
}
|
|
7420
|
+
}
|
|
6728
7421
|
}
|
|
6729
7422
|
};
|
|
6730
7423
|
}
|
|
6731
7424
|
function raindrop(options = {}) {
|
|
6732
7425
|
var _a;
|
|
6733
|
-
const { context, subagentWrapping, writeKey, ...rest } = options;
|
|
7426
|
+
const { context, subagentWrapping, tools, writeKey, ...rest } = options;
|
|
6734
7427
|
const resolvedWriteKey = writeKey != null ? writeKey : typeof process !== "undefined" ? (_a = process.env) == null ? void 0 : _a.RAINDROP_WRITE_KEY : void 0;
|
|
6735
7428
|
const client = createRaindropAISDK({ ...rest, writeKey: resolvedWriteKey });
|
|
6736
|
-
return client.createTelemetryIntegration({ context, subagentWrapping });
|
|
7429
|
+
return client.createTelemetryIntegration({ context, subagentWrapping, tools });
|
|
6737
7430
|
}
|
|
6738
7431
|
|
|
6739
7432
|
exports.DEFAULT_MAX_TEXT_FIELD_CHARS = DEFAULT_MAX_TEXT_FIELD_CHARS2;
|
|
@@ -6744,6 +7437,7 @@ exports.HANDOFF_ATTRIBUTE_PREFIXES = HANDOFF_ATTRIBUTE_PREFIXES;
|
|
|
6744
7437
|
exports.HANDOFF_ATTRIBUTE_SUFFIXES = HANDOFF_ATTRIBUTE_SUFFIXES;
|
|
6745
7438
|
exports.HANDOFF_TERMINAL_CANCELLED = HANDOFF_TERMINAL_CANCELLED;
|
|
6746
7439
|
exports.HANDOFF_TERMINAL_PROPERTY_KEY = HANDOFF_TERMINAL_PROPERTY_KEY;
|
|
7440
|
+
exports.PROMPT_TOOLS_ATTRIBUTE = PROMPT_TOOLS_ATTRIBUTE;
|
|
6747
7441
|
exports.REDACTED_PLACEHOLDER = REDACTED_PLACEHOLDER;
|
|
6748
7442
|
exports.RaindropTelemetryIntegration = RaindropTelemetryIntegration;
|
|
6749
7443
|
exports.SUBAGENT_ABORTED_OUTPUT = SUBAGENT_ABORTED_OUTPUT;
|