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