@raindrop-ai/ai-sdk 0.0.4 → 0.0.6
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 +9 -2
- package/dist/{chunk-RE7BC2GC.mjs → chunk-5S6OYWE3.mjs} +448 -267
- package/dist/index.d.mts +4 -2
- package/dist/index.d.ts +4 -2
- package/dist/index.js +448 -266
- package/dist/index.mjs +1 -1
- package/dist/index.node.d.mts +1 -1
- package/dist/index.node.d.ts +1 -1
- package/dist/index.node.js +448 -266
- 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 +448 -266
- package/dist/index.workers.mjs +1 -1
- package/package.json +1 -1
package/dist/index.workers.js
CHANGED
|
@@ -90,7 +90,7 @@ async function postJson(url, body, headers, opts) {
|
|
|
90
90
|
// package.json
|
|
91
91
|
var package_default = {
|
|
92
92
|
name: "@raindrop-ai/ai-sdk",
|
|
93
|
-
version: "0.0.
|
|
93
|
+
version: "0.0.6"};
|
|
94
94
|
|
|
95
95
|
// src/internal/version.ts
|
|
96
96
|
var libraryName = package_default.name;
|
|
@@ -767,9 +767,6 @@ async function* asyncGeneratorWithCurrent(span, gen) {
|
|
|
767
767
|
nextValue = yield result.value;
|
|
768
768
|
}
|
|
769
769
|
}
|
|
770
|
-
function isAsyncGenerator(value) {
|
|
771
|
-
return value !== null && typeof value === "object" && Symbol.asyncIterator in value && typeof value.next === "function";
|
|
772
|
-
}
|
|
773
770
|
|
|
774
771
|
// src/internal/wrap/helpers.ts
|
|
775
772
|
function isRecord(value) {
|
|
@@ -854,31 +851,6 @@ function extractObjectOutput(result) {
|
|
|
854
851
|
if (obj === void 0) return void 0;
|
|
855
852
|
return (_a = safeJson(obj)) != null ? _a : String(obj);
|
|
856
853
|
}
|
|
857
|
-
function extractUsage(result) {
|
|
858
|
-
if (!isRecord(result)) return void 0;
|
|
859
|
-
const usage = isRecord(result["totalUsage"]) ? result["totalUsage"] : result["usage"];
|
|
860
|
-
if (!isRecord(usage)) return void 0;
|
|
861
|
-
let inputTokens;
|
|
862
|
-
const inputVal = usage["inputTokens"];
|
|
863
|
-
if (typeof inputVal === "number") {
|
|
864
|
-
inputTokens = inputVal;
|
|
865
|
-
} else if (isRecord(inputVal) && typeof inputVal["total"] === "number") {
|
|
866
|
-
inputTokens = inputVal["total"];
|
|
867
|
-
} else if (typeof usage["promptTokens"] === "number") {
|
|
868
|
-
inputTokens = usage["promptTokens"];
|
|
869
|
-
}
|
|
870
|
-
let outputTokens;
|
|
871
|
-
const outputVal = usage["outputTokens"];
|
|
872
|
-
if (typeof outputVal === "number") {
|
|
873
|
-
outputTokens = outputVal;
|
|
874
|
-
} else if (isRecord(outputVal) && typeof outputVal["total"] === "number") {
|
|
875
|
-
outputTokens = outputVal["total"];
|
|
876
|
-
} else if (typeof usage["completionTokens"] === "number") {
|
|
877
|
-
outputTokens = usage["completionTokens"];
|
|
878
|
-
}
|
|
879
|
-
if (inputTokens === void 0 && outputTokens === void 0) return void 0;
|
|
880
|
-
return { inputTokens, outputTokens };
|
|
881
|
-
}
|
|
882
854
|
function isAgentClass(value) {
|
|
883
855
|
if (typeof value !== "function") return false;
|
|
884
856
|
const proto = value.prototype;
|
|
@@ -1273,6 +1245,17 @@ function attrsFromGenAiRequest(options) {
|
|
|
1273
1245
|
}
|
|
1274
1246
|
|
|
1275
1247
|
// src/internal/wrap/wrapAISDK.ts
|
|
1248
|
+
var warnedMissingUserId = false;
|
|
1249
|
+
function warnMissingUserIdOnce() {
|
|
1250
|
+
if (warnedMissingUserId) return;
|
|
1251
|
+
warnedMissingUserId = true;
|
|
1252
|
+
console.warn(
|
|
1253
|
+
"[raindrop-ai/ai-sdk] userId was not provided in wrap() context or via eventMetadata(). Events will be skipped unless a userId is provided."
|
|
1254
|
+
);
|
|
1255
|
+
}
|
|
1256
|
+
function _resetWarnedMissingUserId() {
|
|
1257
|
+
warnedMissingUserId = false;
|
|
1258
|
+
}
|
|
1276
1259
|
function extractRaindropMetadata(metadata) {
|
|
1277
1260
|
if (!metadata || typeof metadata !== "object") return {};
|
|
1278
1261
|
const result = {};
|
|
@@ -1309,10 +1292,392 @@ function mergeContexts(wrapTime, callTime) {
|
|
|
1309
1292
|
}
|
|
1310
1293
|
return result;
|
|
1311
1294
|
}
|
|
1295
|
+
function getCurrentParentSpanContextSync() {
|
|
1296
|
+
return getContextManager().getParentSpanIds();
|
|
1297
|
+
}
|
|
1298
|
+
function runWithParentSpanContextSync(ctx, fn) {
|
|
1299
|
+
const cm = getContextManager();
|
|
1300
|
+
const span = {
|
|
1301
|
+
traceIdB64: ctx.traceIdB64,
|
|
1302
|
+
spanIdB64: ctx.spanIdB64,
|
|
1303
|
+
eventId: ctx.eventId
|
|
1304
|
+
};
|
|
1305
|
+
return cm.runInContext(span, fn);
|
|
1306
|
+
}
|
|
1307
|
+
function isAsyncIterable(value) {
|
|
1308
|
+
return value !== null && typeof value === "object" && Symbol.asyncIterator in value;
|
|
1309
|
+
}
|
|
1310
|
+
function firstFiniteNumber(...values) {
|
|
1311
|
+
for (const value of values) {
|
|
1312
|
+
if (typeof value === "number" && Number.isFinite(value)) {
|
|
1313
|
+
return value;
|
|
1314
|
+
}
|
|
1315
|
+
}
|
|
1316
|
+
return void 0;
|
|
1317
|
+
}
|
|
1318
|
+
function resolveUsageRecord(result) {
|
|
1319
|
+
if (!isRecord(result)) return void 0;
|
|
1320
|
+
let usage;
|
|
1321
|
+
try {
|
|
1322
|
+
if (isRecord(result["totalUsage"])) {
|
|
1323
|
+
usage = result["totalUsage"];
|
|
1324
|
+
} else if (isRecord(result["usage"])) {
|
|
1325
|
+
usage = result["usage"];
|
|
1326
|
+
}
|
|
1327
|
+
} catch (e) {
|
|
1328
|
+
return void 0;
|
|
1329
|
+
}
|
|
1330
|
+
return isRecord(usage) ? usage : void 0;
|
|
1331
|
+
}
|
|
1332
|
+
function extractUsageMetrics(result) {
|
|
1333
|
+
const usage = resolveUsageRecord(result);
|
|
1334
|
+
if (!usage) return {};
|
|
1335
|
+
const inputTokenValue = usage["inputTokens"];
|
|
1336
|
+
const outputTokenValue = usage["outputTokens"];
|
|
1337
|
+
const inputTokens = firstFiniteNumber(
|
|
1338
|
+
isRecord(inputTokenValue) ? inputTokenValue["total"] : void 0,
|
|
1339
|
+
inputTokenValue,
|
|
1340
|
+
usage["promptTokens"],
|
|
1341
|
+
usage["prompt_tokens"]
|
|
1342
|
+
);
|
|
1343
|
+
const outputTokens = firstFiniteNumber(
|
|
1344
|
+
isRecord(outputTokenValue) ? outputTokenValue["total"] : void 0,
|
|
1345
|
+
outputTokenValue,
|
|
1346
|
+
usage["completionTokens"],
|
|
1347
|
+
usage["completion_tokens"]
|
|
1348
|
+
);
|
|
1349
|
+
const totalTokens = firstFiniteNumber(
|
|
1350
|
+
usage["totalTokens"],
|
|
1351
|
+
usage["tokens"],
|
|
1352
|
+
usage["total_tokens"],
|
|
1353
|
+
inputTokens !== void 0 && outputTokens !== void 0 ? inputTokens + outputTokens : void 0
|
|
1354
|
+
);
|
|
1355
|
+
const reasoningTokens = firstFiniteNumber(
|
|
1356
|
+
isRecord(outputTokenValue) ? outputTokenValue["reasoning"] : void 0,
|
|
1357
|
+
usage["reasoningTokens"],
|
|
1358
|
+
usage["completionReasoningTokens"],
|
|
1359
|
+
usage["completion_reasoning_tokens"],
|
|
1360
|
+
usage["reasoning_tokens"],
|
|
1361
|
+
usage["thinkingTokens"],
|
|
1362
|
+
usage["thinking_tokens"]
|
|
1363
|
+
);
|
|
1364
|
+
const cachedInputTokens = firstFiniteNumber(
|
|
1365
|
+
isRecord(inputTokenValue) ? inputTokenValue["cacheRead"] : void 0,
|
|
1366
|
+
usage["cachedInputTokens"],
|
|
1367
|
+
usage["promptCachedTokens"],
|
|
1368
|
+
usage["prompt_cached_tokens"]
|
|
1369
|
+
);
|
|
1370
|
+
return {
|
|
1371
|
+
inputTokens,
|
|
1372
|
+
outputTokens,
|
|
1373
|
+
totalTokens,
|
|
1374
|
+
reasoningTokens,
|
|
1375
|
+
cachedInputTokens
|
|
1376
|
+
};
|
|
1377
|
+
}
|
|
1378
|
+
function isObjectOperation(operation) {
|
|
1379
|
+
return operation === "generateObject" || operation === "streamObject";
|
|
1380
|
+
}
|
|
1381
|
+
function logFinalizeError(debug, err) {
|
|
1382
|
+
if (debug) {
|
|
1383
|
+
console.warn(
|
|
1384
|
+
`[raindrop-ai/ai-sdk] finalize failed: ${err instanceof Error ? err.message : err}`
|
|
1385
|
+
);
|
|
1386
|
+
}
|
|
1387
|
+
}
|
|
1388
|
+
async function safeFinalize(finalize, debug, result, error) {
|
|
1389
|
+
try {
|
|
1390
|
+
await finalize(result, error);
|
|
1391
|
+
} catch (finalizeErr) {
|
|
1392
|
+
logFinalizeError(debug, finalizeErr);
|
|
1393
|
+
}
|
|
1394
|
+
}
|
|
1395
|
+
function runWithRootContextSync(rootSpan, eventId, fn) {
|
|
1396
|
+
if (!rootSpan) return fn();
|
|
1397
|
+
return runWithParentSpanContextSync(
|
|
1398
|
+
{ traceIdB64: rootSpan.ids.traceIdB64, spanIdB64: rootSpan.ids.spanIdB64, eventId },
|
|
1399
|
+
fn
|
|
1400
|
+
);
|
|
1401
|
+
}
|
|
1402
|
+
async function runWithRootContextAsync(rootSpan, eventId, fn) {
|
|
1403
|
+
if (!rootSpan) return await fn();
|
|
1404
|
+
return await runWithParentSpanContext(
|
|
1405
|
+
{ traceIdB64: rootSpan.ids.traceIdB64, spanIdB64: rootSpan.ids.spanIdB64, eventId },
|
|
1406
|
+
fn
|
|
1407
|
+
);
|
|
1408
|
+
}
|
|
1409
|
+
function teeStreamObjectBaseStream(result) {
|
|
1410
|
+
if (!result || !isRecord(result)) return;
|
|
1411
|
+
const baseStream = result["baseStream"];
|
|
1412
|
+
if (!(baseStream && typeof baseStream === "object" && "tee" in baseStream)) return;
|
|
1413
|
+
try {
|
|
1414
|
+
const [consumeStream, userStream] = baseStream.tee();
|
|
1415
|
+
result["baseStream"] = userStream;
|
|
1416
|
+
consumeStream.pipeTo(new WritableStream({ write() {
|
|
1417
|
+
} })).catch(() => {
|
|
1418
|
+
});
|
|
1419
|
+
} catch (e) {
|
|
1420
|
+
}
|
|
1421
|
+
}
|
|
1422
|
+
function setupOperation(params) {
|
|
1423
|
+
var _a, _b, _c;
|
|
1424
|
+
const { operation, arg, inherited, aiSDK, options, traceShipper, sendTraces } = params;
|
|
1425
|
+
const wrapTimeCtx = resolveContext(options.context, { operation, args: arg });
|
|
1426
|
+
const telemetry = extractExperimentalTelemetry(arg);
|
|
1427
|
+
const callTimeCtx = extractRaindropMetadata(telemetry == null ? void 0 : telemetry.metadata);
|
|
1428
|
+
const mergedCtx = mergeContexts(wrapTimeCtx, callTimeCtx);
|
|
1429
|
+
if (!mergedCtx.userId) warnMissingUserIdOnce();
|
|
1430
|
+
const eventId = (_c = (_b = (_a = callTimeCtx.eventId) != null ? _a : mergedCtx.eventId) != null ? _b : inherited == null ? void 0 : inherited.eventId) != null ? _c : randomUUID();
|
|
1431
|
+
const ctx = { ...mergedCtx, eventId };
|
|
1432
|
+
const inheritedParent = inherited && inherited.eventId === eventId ? { traceIdB64: inherited.traceIdB64, spanIdB64: inherited.spanIdB64 } : void 0;
|
|
1433
|
+
const outerOperationId = `ai.${operation}`;
|
|
1434
|
+
const { operationName, resourceName } = opName(outerOperationId, telemetry == null ? void 0 : telemetry.functionId);
|
|
1435
|
+
const modelInfoFromArgs = isRecord(arg) ? extractModelInfo(arg["model"]) : {};
|
|
1436
|
+
const rootSpan = sendTraces ? traceShipper.startSpan({
|
|
1437
|
+
name: outerOperationId,
|
|
1438
|
+
parent: inheritedParent,
|
|
1439
|
+
eventId,
|
|
1440
|
+
operationId: outerOperationId,
|
|
1441
|
+
attributes: [
|
|
1442
|
+
attrString("operation.name", operationName),
|
|
1443
|
+
attrString("resource.name", resourceName),
|
|
1444
|
+
attrString("ai.telemetry.functionId", telemetry == null ? void 0 : telemetry.functionId),
|
|
1445
|
+
attrString("ai.model.provider", modelInfoFromArgs.provider),
|
|
1446
|
+
attrString("ai.model.id", modelInfoFromArgs.modelId),
|
|
1447
|
+
...attrsFromTelemetryMetadata(telemetry == null ? void 0 : telemetry.metadata),
|
|
1448
|
+
...attrsFromHeaders(isRecord(arg) ? arg["headers"] : void 0),
|
|
1449
|
+
...attrsFromSettings(arg),
|
|
1450
|
+
...(telemetry == null ? void 0 : telemetry.recordInputs) === false ? [] : [
|
|
1451
|
+
attrString(
|
|
1452
|
+
"ai.prompt",
|
|
1453
|
+
safeJsonWithUint8({
|
|
1454
|
+
system: isRecord(arg) ? arg["system"] : void 0,
|
|
1455
|
+
prompt: isRecord(arg) ? arg["prompt"] : void 0,
|
|
1456
|
+
messages: isRecord(arg) ? arg["messages"] : void 0
|
|
1457
|
+
})
|
|
1458
|
+
)
|
|
1459
|
+
]
|
|
1460
|
+
]
|
|
1461
|
+
}) : void 0;
|
|
1462
|
+
const rootParentForChildren = rootSpan ? { traceIdB64: rootSpan.ids.traceIdB64, spanIdB64: rootSpan.ids.spanIdB64 } : inheritedParent;
|
|
1463
|
+
const wrapCtx = {
|
|
1464
|
+
eventId,
|
|
1465
|
+
telemetry,
|
|
1466
|
+
sendTraces,
|
|
1467
|
+
traceShipper,
|
|
1468
|
+
rootParentForChildren
|
|
1469
|
+
};
|
|
1470
|
+
const toolCalls = [];
|
|
1471
|
+
const argsWithWrappedTools = wrapTools(arg, wrapCtx, toolCalls);
|
|
1472
|
+
const wrappedArgs = wrapModel(argsWithWrappedTools, aiSDK, outerOperationId, wrapCtx);
|
|
1473
|
+
return {
|
|
1474
|
+
eventId,
|
|
1475
|
+
ctx,
|
|
1476
|
+
telemetry,
|
|
1477
|
+
rootSpan,
|
|
1478
|
+
wrappedArgs,
|
|
1479
|
+
toolCalls
|
|
1480
|
+
};
|
|
1481
|
+
}
|
|
1482
|
+
function createFinalize(params) {
|
|
1483
|
+
const {
|
|
1484
|
+
operation,
|
|
1485
|
+
arg,
|
|
1486
|
+
setup,
|
|
1487
|
+
autoAttachmentEnabled,
|
|
1488
|
+
sendEvents,
|
|
1489
|
+
debug,
|
|
1490
|
+
options,
|
|
1491
|
+
eventShipper,
|
|
1492
|
+
traceShipper
|
|
1493
|
+
} = params;
|
|
1494
|
+
return async (result, error) => {
|
|
1495
|
+
var _a, _b, _c, _d;
|
|
1496
|
+
const usage = extractUsageMetrics(result);
|
|
1497
|
+
const model = extractModel(result);
|
|
1498
|
+
const inputAttachments = autoAttachmentEnabled ? extractInputAttachmentsFromArgs(arg) : void 0;
|
|
1499
|
+
const outputAttachments = autoAttachmentEnabled ? await extractOutputAttachmentsFromResult(result) : void 0;
|
|
1500
|
+
const baseMessages = coerceMessagesFromArgs(arg);
|
|
1501
|
+
const responseMessages = extractResponseMessages(result);
|
|
1502
|
+
const allMessages = [...baseMessages, ...responseMessages];
|
|
1503
|
+
const outputText = extractTextOutput(result);
|
|
1504
|
+
const outputObjectJson = extractObjectOutput(result);
|
|
1505
|
+
const defaultOutput = isObjectOperation(operation) ? outputObjectJson : outputText;
|
|
1506
|
+
const defaultPatch = {
|
|
1507
|
+
eventName: (_a = setup.ctx.eventName) != null ? _a : operation,
|
|
1508
|
+
input: (_b = lastUserMessageTextFromArgs(arg)) != null ? _b : extractInputFromArgs(arg),
|
|
1509
|
+
output: defaultOutput,
|
|
1510
|
+
model,
|
|
1511
|
+
properties: setup.ctx.properties,
|
|
1512
|
+
attachments: mergeAttachments(setup.ctx.attachments, inputAttachments, outputAttachments)
|
|
1513
|
+
};
|
|
1514
|
+
const built = await maybeBuildEvent(options.buildEvent, allMessages);
|
|
1515
|
+
const patch = mergeBuildEventPatch(defaultPatch, built);
|
|
1516
|
+
const output = patch.output;
|
|
1517
|
+
const finalModel = (_c = patch.model) != null ? _c : model;
|
|
1518
|
+
if (setup.rootSpan) {
|
|
1519
|
+
const finishReason = extractFinishReason(result);
|
|
1520
|
+
const providerMetadata = isRecord(result) ? result["providerMetadata"] : void 0;
|
|
1521
|
+
const resultToolCalls = isRecord(result) && Array.isArray(result["toolCalls"]) ? safeJsonWithUint8(result["toolCalls"]) : setup.toolCalls.length ? safeJsonWithUint8(setup.toolCalls) : void 0;
|
|
1522
|
+
traceShipper.endSpan(setup.rootSpan, {
|
|
1523
|
+
attributes: [
|
|
1524
|
+
...((_d = setup.telemetry) == null ? void 0 : _d.recordOutputs) === false ? [] : [
|
|
1525
|
+
attrString("ai.response.finishReason", finishReason),
|
|
1526
|
+
isObjectOperation(operation) ? attrString("ai.response.object", output) : attrString("ai.response.text", output),
|
|
1527
|
+
attrString("ai.response.toolCalls", resultToolCalls),
|
|
1528
|
+
attrString("ai.response.providerMetadata", safeJsonWithUint8(providerMetadata))
|
|
1529
|
+
],
|
|
1530
|
+
attrInt("ai.usage.promptTokens", usage == null ? void 0 : usage.inputTokens),
|
|
1531
|
+
attrInt("ai.usage.completionTokens", usage == null ? void 0 : usage.outputTokens),
|
|
1532
|
+
attrInt("ai.usage.inputTokens", usage == null ? void 0 : usage.inputTokens),
|
|
1533
|
+
attrInt("ai.usage.outputTokens", usage == null ? void 0 : usage.outputTokens),
|
|
1534
|
+
attrInt("ai.usage.totalTokens", usage == null ? void 0 : usage.totalTokens),
|
|
1535
|
+
attrInt("ai.usage.reasoningTokens", usage == null ? void 0 : usage.reasoningTokens),
|
|
1536
|
+
attrInt("ai.usage.cachedInputTokens", usage == null ? void 0 : usage.cachedInputTokens),
|
|
1537
|
+
attrInt("ai.toolCall.count", setup.toolCalls.length),
|
|
1538
|
+
...error ? [
|
|
1539
|
+
attrString(
|
|
1540
|
+
"error.message",
|
|
1541
|
+
error instanceof Error ? error.message : String(error)
|
|
1542
|
+
)
|
|
1543
|
+
] : []
|
|
1544
|
+
]
|
|
1545
|
+
});
|
|
1546
|
+
}
|
|
1547
|
+
if (sendEvents) {
|
|
1548
|
+
void eventShipper.patch(setup.eventId, {
|
|
1549
|
+
eventName: patch.eventName,
|
|
1550
|
+
userId: setup.ctx.userId,
|
|
1551
|
+
convoId: setup.ctx.convoId,
|
|
1552
|
+
input: patch.input,
|
|
1553
|
+
output,
|
|
1554
|
+
model: finalModel,
|
|
1555
|
+
properties: patch.properties,
|
|
1556
|
+
attachments: patch.attachments,
|
|
1557
|
+
isPending: false
|
|
1558
|
+
}).catch((err) => {
|
|
1559
|
+
if (debug) {
|
|
1560
|
+
console.warn(
|
|
1561
|
+
`[raindrop-ai/ai-sdk] event patch failed: ${err instanceof Error ? err.message : err}`
|
|
1562
|
+
);
|
|
1563
|
+
}
|
|
1564
|
+
});
|
|
1565
|
+
}
|
|
1566
|
+
};
|
|
1567
|
+
}
|
|
1568
|
+
function executeStreamingOperation(params) {
|
|
1569
|
+
const {
|
|
1570
|
+
operation,
|
|
1571
|
+
arg,
|
|
1572
|
+
callArgs,
|
|
1573
|
+
aiSDK,
|
|
1574
|
+
original,
|
|
1575
|
+
deps,
|
|
1576
|
+
sendEvents,
|
|
1577
|
+
sendTraces,
|
|
1578
|
+
autoAttachmentEnabled,
|
|
1579
|
+
debug
|
|
1580
|
+
} = params;
|
|
1581
|
+
const setup = setupOperation({
|
|
1582
|
+
operation,
|
|
1583
|
+
arg,
|
|
1584
|
+
inherited: getCurrentParentSpanContextSync(),
|
|
1585
|
+
aiSDK,
|
|
1586
|
+
options: deps.options,
|
|
1587
|
+
traceShipper: deps.traceShipper,
|
|
1588
|
+
sendTraces
|
|
1589
|
+
});
|
|
1590
|
+
const finalize = createFinalize({
|
|
1591
|
+
operation,
|
|
1592
|
+
arg,
|
|
1593
|
+
setup,
|
|
1594
|
+
autoAttachmentEnabled,
|
|
1595
|
+
sendEvents,
|
|
1596
|
+
debug,
|
|
1597
|
+
options: deps.options,
|
|
1598
|
+
eventShipper: deps.eventShipper,
|
|
1599
|
+
traceShipper: deps.traceShipper
|
|
1600
|
+
});
|
|
1601
|
+
const argWithOnFinish = wrapOnFinish(setup.wrappedArgs, async (result) => {
|
|
1602
|
+
await safeFinalize(finalize, debug, result);
|
|
1603
|
+
});
|
|
1604
|
+
const callOriginal = (...args) => {
|
|
1605
|
+
return original.call(aiSDK, ...args);
|
|
1606
|
+
};
|
|
1607
|
+
try {
|
|
1608
|
+
const result = runWithRootContextSync(setup.rootSpan, setup.eventId, () => {
|
|
1609
|
+
const nextArgs = [...callArgs];
|
|
1610
|
+
nextArgs[0] = argWithOnFinish;
|
|
1611
|
+
return callOriginal(...nextArgs);
|
|
1612
|
+
});
|
|
1613
|
+
if (operation === "streamObject") {
|
|
1614
|
+
teeStreamObjectBaseStream(result);
|
|
1615
|
+
}
|
|
1616
|
+
return result;
|
|
1617
|
+
} catch (error) {
|
|
1618
|
+
void safeFinalize(finalize, debug, void 0, error);
|
|
1619
|
+
throw error;
|
|
1620
|
+
}
|
|
1621
|
+
}
|
|
1622
|
+
async function executeNonStreamingOperation(params) {
|
|
1623
|
+
const {
|
|
1624
|
+
operation,
|
|
1625
|
+
arg,
|
|
1626
|
+
callArgs,
|
|
1627
|
+
aiSDK,
|
|
1628
|
+
original,
|
|
1629
|
+
deps,
|
|
1630
|
+
sendEvents,
|
|
1631
|
+
sendTraces,
|
|
1632
|
+
autoAttachmentEnabled,
|
|
1633
|
+
debug
|
|
1634
|
+
} = params;
|
|
1635
|
+
const inherited = await getCurrentParentSpanContext();
|
|
1636
|
+
const setup = setupOperation({
|
|
1637
|
+
operation,
|
|
1638
|
+
arg,
|
|
1639
|
+
inherited,
|
|
1640
|
+
aiSDK,
|
|
1641
|
+
options: deps.options,
|
|
1642
|
+
traceShipper: deps.traceShipper,
|
|
1643
|
+
sendTraces
|
|
1644
|
+
});
|
|
1645
|
+
const finalize = createFinalize({
|
|
1646
|
+
operation,
|
|
1647
|
+
arg,
|
|
1648
|
+
setup,
|
|
1649
|
+
autoAttachmentEnabled,
|
|
1650
|
+
sendEvents,
|
|
1651
|
+
debug,
|
|
1652
|
+
options: deps.options,
|
|
1653
|
+
eventShipper: deps.eventShipper,
|
|
1654
|
+
traceShipper: deps.traceShipper
|
|
1655
|
+
});
|
|
1656
|
+
const callOriginal = async (...args) => {
|
|
1657
|
+
return await original.call(aiSDK, ...args);
|
|
1658
|
+
};
|
|
1659
|
+
try {
|
|
1660
|
+
const result = await runWithRootContextAsync(setup.rootSpan, setup.eventId, async () => {
|
|
1661
|
+
const nextArgs = [...callArgs];
|
|
1662
|
+
nextArgs[0] = setup.wrappedArgs;
|
|
1663
|
+
return await callOriginal(...nextArgs);
|
|
1664
|
+
});
|
|
1665
|
+
await safeFinalize(finalize, debug, result);
|
|
1666
|
+
return result;
|
|
1667
|
+
} catch (error) {
|
|
1668
|
+
await safeFinalize(finalize, debug, void 0, error);
|
|
1669
|
+
throw error;
|
|
1670
|
+
}
|
|
1671
|
+
}
|
|
1312
1672
|
function wrapAISDK(aiSDK, deps) {
|
|
1313
1673
|
var _a, _b;
|
|
1314
1674
|
const debug = deps.eventShipper.isDebugEnabled() || deps.traceShipper.isDebugEnabled();
|
|
1315
|
-
const instrumentedOps = /* @__PURE__ */ new Set([
|
|
1675
|
+
const instrumentedOps = /* @__PURE__ */ new Set([
|
|
1676
|
+
"generateText",
|
|
1677
|
+
"streamText",
|
|
1678
|
+
"generateObject",
|
|
1679
|
+
"streamObject"
|
|
1680
|
+
]);
|
|
1316
1681
|
const agentClasses = /* @__PURE__ */ new Set(["ToolLoopAgent"]);
|
|
1317
1682
|
const sendEvents = ((_a = deps.options.send) == null ? void 0 : _a.events) !== false;
|
|
1318
1683
|
const sendTraces = ((_b = deps.options.send) == null ? void 0 : _b.traces) !== false;
|
|
@@ -1328,224 +1693,35 @@ function wrapAISDK(aiSDK, deps) {
|
|
|
1328
1693
|
if (typeof prop !== "string" || !instrumentedOps.has(prop) || !isFunction(original)) {
|
|
1329
1694
|
return original;
|
|
1330
1695
|
}
|
|
1331
|
-
return
|
|
1332
|
-
var _a2, _b2, _c;
|
|
1333
|
-
const arg = callArgs[0];
|
|
1696
|
+
return (...callArgs) => {
|
|
1334
1697
|
const operation = prop;
|
|
1335
|
-
const
|
|
1336
|
-
const telemetry = extractExperimentalTelemetry(arg);
|
|
1337
|
-
const callTimeCtx = extractRaindropMetadata(telemetry == null ? void 0 : telemetry.metadata);
|
|
1338
|
-
const mergedCtx = mergeContexts(wrapTimeCtx, callTimeCtx);
|
|
1339
|
-
const inherited = await getCurrentParentSpanContext();
|
|
1340
|
-
const eventId = (_c = (_b2 = (_a2 = callTimeCtx.eventId) != null ? _a2 : mergedCtx.eventId) != null ? _b2 : inherited == null ? void 0 : inherited.eventId) != null ? _c : randomUUID();
|
|
1341
|
-
const ctx = { ...mergedCtx};
|
|
1342
|
-
const inheritedParent = inherited && inherited.eventId === eventId ? { traceIdB64: inherited.traceIdB64, spanIdB64: inherited.spanIdB64 } : void 0;
|
|
1343
|
-
const outerOperationId = `ai.${operation}`;
|
|
1344
|
-
const { operationName, resourceName } = opName(outerOperationId, telemetry == null ? void 0 : telemetry.functionId);
|
|
1345
|
-
const modelInfoFromArgs = isRecord(arg) ? extractModelInfo(arg["model"]) : {};
|
|
1346
|
-
const rootSpan = sendTraces ? deps.traceShipper.startSpan({
|
|
1347
|
-
name: outerOperationId,
|
|
1348
|
-
parent: inheritedParent,
|
|
1349
|
-
eventId,
|
|
1350
|
-
operationId: outerOperationId,
|
|
1351
|
-
attributes: [
|
|
1352
|
-
attrString("operation.name", operationName),
|
|
1353
|
-
attrString("resource.name", resourceName),
|
|
1354
|
-
attrString("ai.telemetry.functionId", telemetry == null ? void 0 : telemetry.functionId),
|
|
1355
|
-
attrString("ai.model.provider", modelInfoFromArgs.provider),
|
|
1356
|
-
attrString("ai.model.id", modelInfoFromArgs.modelId),
|
|
1357
|
-
...attrsFromTelemetryMetadata(telemetry == null ? void 0 : telemetry.metadata),
|
|
1358
|
-
...attrsFromHeaders(isRecord(arg) ? arg["headers"] : void 0),
|
|
1359
|
-
...attrsFromSettings(arg),
|
|
1360
|
-
...(telemetry == null ? void 0 : telemetry.recordInputs) === false ? [] : [
|
|
1361
|
-
attrString(
|
|
1362
|
-
"ai.prompt",
|
|
1363
|
-
safeJsonWithUint8({
|
|
1364
|
-
system: isRecord(arg) ? arg["system"] : void 0,
|
|
1365
|
-
prompt: isRecord(arg) ? arg["prompt"] : void 0,
|
|
1366
|
-
messages: isRecord(arg) ? arg["messages"] : void 0
|
|
1367
|
-
})
|
|
1368
|
-
)
|
|
1369
|
-
]
|
|
1370
|
-
]
|
|
1371
|
-
}) : void 0;
|
|
1372
|
-
const rootParentForChildren = rootSpan ? { traceIdB64: rootSpan.ids.traceIdB64, spanIdB64: rootSpan.ids.spanIdB64 } : inheritedParent;
|
|
1373
|
-
const wrapCtx = {
|
|
1374
|
-
eventId,
|
|
1375
|
-
telemetry,
|
|
1376
|
-
sendTraces,
|
|
1377
|
-
traceShipper: deps.traceShipper,
|
|
1378
|
-
rootParentForChildren
|
|
1379
|
-
};
|
|
1380
|
-
const toolCalls = [];
|
|
1381
|
-
const argWithWrappedTools = wrapTools(arg, wrapCtx, toolCalls);
|
|
1382
|
-
const argWithWrappedModel = wrapModel(
|
|
1383
|
-
argWithWrappedTools,
|
|
1384
|
-
aiSDK,
|
|
1385
|
-
outerOperationId,
|
|
1386
|
-
wrapCtx
|
|
1387
|
-
);
|
|
1388
|
-
const finalize = async (result, error) => {
|
|
1389
|
-
var _a3, _b3, _c2;
|
|
1390
|
-
const usage = extractUsage(result);
|
|
1391
|
-
const model = extractModel(result);
|
|
1392
|
-
const inputAttachments = autoAttachmentEnabled ? extractInputAttachmentsFromArgs(arg) : void 0;
|
|
1393
|
-
const outputAttachments = autoAttachmentEnabled ? await extractOutputAttachmentsFromResult(result) : void 0;
|
|
1394
|
-
const baseMessages = coerceMessagesFromArgs(arg);
|
|
1395
|
-
const responseMessages = extractResponseMessages(result);
|
|
1396
|
-
const allMessages = [...baseMessages, ...responseMessages];
|
|
1397
|
-
const outputText = extractTextOutput(result);
|
|
1398
|
-
const outputObjectJson = extractObjectOutput(result);
|
|
1399
|
-
const defaultOutput = operation === "generateObject" || operation === "streamObject" ? outputObjectJson : outputText;
|
|
1400
|
-
const defaultPatch = {
|
|
1401
|
-
eventName: (_a3 = ctx.eventName) != null ? _a3 : operation,
|
|
1402
|
-
input: (_b3 = lastUserMessageTextFromArgs(arg)) != null ? _b3 : extractInputFromArgs(arg),
|
|
1403
|
-
output: defaultOutput,
|
|
1404
|
-
model,
|
|
1405
|
-
properties: ctx.properties,
|
|
1406
|
-
attachments: mergeAttachments(ctx.attachments, inputAttachments, outputAttachments)
|
|
1407
|
-
};
|
|
1408
|
-
const built = await maybeBuildEvent(deps.options.buildEvent, allMessages);
|
|
1409
|
-
const patch = mergeBuildEventPatch(defaultPatch, built);
|
|
1410
|
-
const output = patch.output;
|
|
1411
|
-
const finalModel = (_c2 = patch.model) != null ? _c2 : model;
|
|
1412
|
-
if (rootSpan) {
|
|
1413
|
-
const finishReason = extractFinishReason(result);
|
|
1414
|
-
const providerMetadata = isRecord(result) ? result["providerMetadata"] : void 0;
|
|
1415
|
-
const resultToolCalls = isRecord(result) && Array.isArray(result["toolCalls"]) ? safeJsonWithUint8(result["toolCalls"]) : toolCalls.length ? safeJsonWithUint8(toolCalls) : void 0;
|
|
1416
|
-
const usageRec = isRecord(result) && isRecord(result["usage"]) ? result["usage"] : void 0;
|
|
1417
|
-
const totalTokens = typeof (usageRec == null ? void 0 : usageRec["totalTokens"]) === "number" ? usageRec["totalTokens"] : void 0;
|
|
1418
|
-
const reasoningTokens = typeof (usageRec == null ? void 0 : usageRec["reasoningTokens"]) === "number" ? usageRec["reasoningTokens"] : void 0;
|
|
1419
|
-
const cachedInputTokens = typeof (usageRec == null ? void 0 : usageRec["cachedInputTokens"]) === "number" ? usageRec["cachedInputTokens"] : void 0;
|
|
1420
|
-
deps.traceShipper.endSpan(rootSpan, {
|
|
1421
|
-
attributes: [
|
|
1422
|
-
...(telemetry == null ? void 0 : telemetry.recordOutputs) === false ? [] : [
|
|
1423
|
-
attrString("ai.response.finishReason", finishReason),
|
|
1424
|
-
operation === "generateObject" || operation === "streamObject" ? attrString("ai.response.object", output) : attrString("ai.response.text", output),
|
|
1425
|
-
attrString("ai.response.toolCalls", resultToolCalls),
|
|
1426
|
-
attrString(
|
|
1427
|
-
"ai.response.providerMetadata",
|
|
1428
|
-
safeJsonWithUint8(providerMetadata)
|
|
1429
|
-
)
|
|
1430
|
-
],
|
|
1431
|
-
attrInt("ai.usage.promptTokens", usage == null ? void 0 : usage.inputTokens),
|
|
1432
|
-
attrInt("ai.usage.completionTokens", usage == null ? void 0 : usage.outputTokens),
|
|
1433
|
-
attrInt("ai.usage.inputTokens", usage == null ? void 0 : usage.inputTokens),
|
|
1434
|
-
attrInt("ai.usage.outputTokens", usage == null ? void 0 : usage.outputTokens),
|
|
1435
|
-
attrInt("ai.usage.totalTokens", totalTokens),
|
|
1436
|
-
attrInt("ai.usage.reasoningTokens", reasoningTokens),
|
|
1437
|
-
attrInt("ai.usage.cachedInputTokens", cachedInputTokens),
|
|
1438
|
-
attrInt("ai.toolCall.count", toolCalls.length),
|
|
1439
|
-
...error ? [
|
|
1440
|
-
attrString(
|
|
1441
|
-
"error.message",
|
|
1442
|
-
error instanceof Error ? error.message : String(error)
|
|
1443
|
-
)
|
|
1444
|
-
] : []
|
|
1445
|
-
]
|
|
1446
|
-
});
|
|
1447
|
-
}
|
|
1448
|
-
if (sendEvents) {
|
|
1449
|
-
void deps.eventShipper.patch(eventId, {
|
|
1450
|
-
eventName: patch.eventName,
|
|
1451
|
-
userId: ctx.userId,
|
|
1452
|
-
convoId: ctx.convoId,
|
|
1453
|
-
input: patch.input,
|
|
1454
|
-
output,
|
|
1455
|
-
model: finalModel,
|
|
1456
|
-
properties: patch.properties,
|
|
1457
|
-
attachments: patch.attachments,
|
|
1458
|
-
isPending: false
|
|
1459
|
-
}).catch((err) => {
|
|
1460
|
-
if (debug)
|
|
1461
|
-
console.warn(
|
|
1462
|
-
`[raindrop-ai/ai-sdk] event patch failed: ${err instanceof Error ? err.message : err}`
|
|
1463
|
-
);
|
|
1464
|
-
});
|
|
1465
|
-
}
|
|
1466
|
-
};
|
|
1467
|
-
const callOriginal = async (...args) => {
|
|
1468
|
-
return await original.call(
|
|
1469
|
-
aiSDK,
|
|
1470
|
-
...args
|
|
1471
|
-
);
|
|
1472
|
-
};
|
|
1473
|
-
const runWithContext = async (fn) => {
|
|
1474
|
-
if (!rootSpan) return await fn();
|
|
1475
|
-
return await runWithParentSpanContext(
|
|
1476
|
-
{ traceIdB64: rootSpan.ids.traceIdB64, spanIdB64: rootSpan.ids.spanIdB64, eventId },
|
|
1477
|
-
fn
|
|
1478
|
-
);
|
|
1479
|
-
};
|
|
1698
|
+
const arg = callArgs[0];
|
|
1480
1699
|
if (operation === "streamText" || operation === "streamObject") {
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
const result = await runWithContext(async () => {
|
|
1493
|
-
const nextArgs = [...callArgs];
|
|
1494
|
-
nextArgs[0] = argWithOnFinish;
|
|
1495
|
-
return await callOriginal(...nextArgs);
|
|
1496
|
-
});
|
|
1497
|
-
if (operation === "streamObject" && result && isRecord(result)) {
|
|
1498
|
-
const baseStream = result["baseStream"];
|
|
1499
|
-
if (baseStream && typeof baseStream === "object" && "tee" in baseStream) {
|
|
1500
|
-
try {
|
|
1501
|
-
const [consumeStream, userStream] = baseStream.tee();
|
|
1502
|
-
result["baseStream"] = userStream;
|
|
1503
|
-
consumeStream.pipeTo(new WritableStream({ write() {
|
|
1504
|
-
} })).catch(() => {
|
|
1505
|
-
});
|
|
1506
|
-
} catch (e) {
|
|
1507
|
-
}
|
|
1508
|
-
}
|
|
1509
|
-
}
|
|
1510
|
-
return result;
|
|
1511
|
-
} catch (error) {
|
|
1512
|
-
try {
|
|
1513
|
-
await finalize(void 0, error);
|
|
1514
|
-
} catch (err) {
|
|
1515
|
-
if (debug)
|
|
1516
|
-
console.warn(
|
|
1517
|
-
`[raindrop-ai/ai-sdk] finalize failed: ${err instanceof Error ? err.message : err}`
|
|
1518
|
-
);
|
|
1519
|
-
}
|
|
1520
|
-
throw error;
|
|
1521
|
-
}
|
|
1522
|
-
}
|
|
1523
|
-
try {
|
|
1524
|
-
const result = await runWithContext(async () => {
|
|
1525
|
-
const nextArgs = [...callArgs];
|
|
1526
|
-
nextArgs[0] = argWithWrappedModel;
|
|
1527
|
-
return await callOriginal(...nextArgs);
|
|
1700
|
+
return executeStreamingOperation({
|
|
1701
|
+
operation,
|
|
1702
|
+
arg,
|
|
1703
|
+
callArgs,
|
|
1704
|
+
aiSDK,
|
|
1705
|
+
original,
|
|
1706
|
+
deps,
|
|
1707
|
+
sendEvents,
|
|
1708
|
+
sendTraces,
|
|
1709
|
+
autoAttachmentEnabled,
|
|
1710
|
+
debug
|
|
1528
1711
|
});
|
|
1529
|
-
try {
|
|
1530
|
-
await finalize(result);
|
|
1531
|
-
} catch (err) {
|
|
1532
|
-
if (debug)
|
|
1533
|
-
console.warn(
|
|
1534
|
-
`[raindrop-ai/ai-sdk] finalize failed: ${err instanceof Error ? err.message : err}`
|
|
1535
|
-
);
|
|
1536
|
-
}
|
|
1537
|
-
return result;
|
|
1538
|
-
} catch (error) {
|
|
1539
|
-
try {
|
|
1540
|
-
await finalize(void 0, error);
|
|
1541
|
-
} catch (err) {
|
|
1542
|
-
if (debug)
|
|
1543
|
-
console.warn(
|
|
1544
|
-
`[raindrop-ai/ai-sdk] finalize failed: ${err instanceof Error ? err.message : err}`
|
|
1545
|
-
);
|
|
1546
|
-
}
|
|
1547
|
-
throw error;
|
|
1548
1712
|
}
|
|
1713
|
+
return executeNonStreamingOperation({
|
|
1714
|
+
operation,
|
|
1715
|
+
arg,
|
|
1716
|
+
callArgs,
|
|
1717
|
+
aiSDK,
|
|
1718
|
+
original,
|
|
1719
|
+
deps,
|
|
1720
|
+
sendEvents,
|
|
1721
|
+
sendTraces,
|
|
1722
|
+
autoAttachmentEnabled,
|
|
1723
|
+
debug
|
|
1724
|
+
});
|
|
1549
1725
|
};
|
|
1550
1726
|
}
|
|
1551
1727
|
});
|
|
@@ -1604,6 +1780,7 @@ function wrapAgentGenerate(generate, instance, agentSettings, className, aiSDK,
|
|
|
1604
1780
|
const telemetry = extractExperimentalTelemetry(mergedArgs);
|
|
1605
1781
|
const callTimeCtx = extractRaindropMetadata(telemetry == null ? void 0 : telemetry.metadata);
|
|
1606
1782
|
const mergedCtx = mergeContexts(wrapTimeCtx, callTimeCtx);
|
|
1783
|
+
if (!mergedCtx.userId) warnMissingUserIdOnce();
|
|
1607
1784
|
const inherited = await getCurrentParentSpanContext();
|
|
1608
1785
|
const eventId = (_c = (_b2 = (_a2 = callTimeCtx.eventId) != null ? _a2 : mergedCtx.eventId) != null ? _b2 : inherited == null ? void 0 : inherited.eventId) != null ? _c : randomUUID();
|
|
1609
1786
|
const ctx = { ...mergedCtx};
|
|
@@ -1656,7 +1833,7 @@ function wrapAgentGenerate(generate, instance, agentSettings, className, aiSDK,
|
|
|
1656
1833
|
const callParamsWithWrappedToolsAndModel = mergedArgsWithWrappedModel != null ? mergedArgsWithWrappedModel : {};
|
|
1657
1834
|
const finalize = async (result, error) => {
|
|
1658
1835
|
var _a3, _b3, _c2;
|
|
1659
|
-
const usage =
|
|
1836
|
+
const usage = extractUsageMetrics(result);
|
|
1660
1837
|
const model = extractModel(result);
|
|
1661
1838
|
const inputAttachments = autoAttachmentEnabled ? extractInputAttachmentsFromArgs(mergedArgs) : void 0;
|
|
1662
1839
|
const outputAttachments = autoAttachmentEnabled ? await extractOutputAttachmentsFromResult(result) : void 0;
|
|
@@ -1680,10 +1857,6 @@ function wrapAgentGenerate(generate, instance, agentSettings, className, aiSDK,
|
|
|
1680
1857
|
const finishReason = extractFinishReason(result);
|
|
1681
1858
|
const providerMetadata = isRecord(result) ? result["providerMetadata"] : void 0;
|
|
1682
1859
|
const resultToolCalls = isRecord(result) && Array.isArray(result["toolCalls"]) ? safeJsonWithUint8(result["toolCalls"]) : toolCalls.length ? safeJsonWithUint8(toolCalls) : void 0;
|
|
1683
|
-
const usageRec = isRecord(result) && isRecord(result["totalUsage"]) ? result["totalUsage"] : isRecord(result) && isRecord(result["usage"]) ? result["usage"] : void 0;
|
|
1684
|
-
const totalTokens = typeof (usageRec == null ? void 0 : usageRec["totalTokens"]) === "number" ? usageRec["totalTokens"] : void 0;
|
|
1685
|
-
const reasoningTokens = typeof (usageRec == null ? void 0 : usageRec["reasoningTokens"]) === "number" ? usageRec["reasoningTokens"] : void 0;
|
|
1686
|
-
const cachedInputTokens = typeof (usageRec == null ? void 0 : usageRec["cachedInputTokens"]) === "number" ? usageRec["cachedInputTokens"] : void 0;
|
|
1687
1860
|
deps.traceShipper.endSpan(rootSpan, {
|
|
1688
1861
|
attributes: [
|
|
1689
1862
|
...(telemetry == null ? void 0 : telemetry.recordOutputs) === false ? [] : [
|
|
@@ -1696,9 +1869,9 @@ function wrapAgentGenerate(generate, instance, agentSettings, className, aiSDK,
|
|
|
1696
1869
|
attrInt("ai.usage.completionTokens", usage == null ? void 0 : usage.outputTokens),
|
|
1697
1870
|
attrInt("ai.usage.inputTokens", usage == null ? void 0 : usage.inputTokens),
|
|
1698
1871
|
attrInt("ai.usage.outputTokens", usage == null ? void 0 : usage.outputTokens),
|
|
1699
|
-
attrInt("ai.usage.totalTokens", totalTokens),
|
|
1700
|
-
attrInt("ai.usage.reasoningTokens", reasoningTokens),
|
|
1701
|
-
attrInt("ai.usage.cachedInputTokens", cachedInputTokens),
|
|
1872
|
+
attrInt("ai.usage.totalTokens", usage == null ? void 0 : usage.totalTokens),
|
|
1873
|
+
attrInt("ai.usage.reasoningTokens", usage == null ? void 0 : usage.reasoningTokens),
|
|
1874
|
+
attrInt("ai.usage.cachedInputTokens", usage == null ? void 0 : usage.cachedInputTokens),
|
|
1702
1875
|
attrInt("ai.toolCall.count", toolCalls.length),
|
|
1703
1876
|
...error ? [
|
|
1704
1877
|
attrString(
|
|
@@ -1795,6 +1968,7 @@ function wrapAgentStream(stream, instance, agentSettings, className, aiSDK, deps
|
|
|
1795
1968
|
const telemetry = extractExperimentalTelemetry(mergedArgs);
|
|
1796
1969
|
const callTimeCtx = extractRaindropMetadata(telemetry == null ? void 0 : telemetry.metadata);
|
|
1797
1970
|
const mergedCtx = mergeContexts(wrapTimeCtx, callTimeCtx);
|
|
1971
|
+
if (!mergedCtx.userId) warnMissingUserIdOnce();
|
|
1798
1972
|
const inherited = await getCurrentParentSpanContext();
|
|
1799
1973
|
const eventId = (_c = (_b2 = (_a2 = callTimeCtx.eventId) != null ? _a2 : mergedCtx.eventId) != null ? _b2 : inherited == null ? void 0 : inherited.eventId) != null ? _c : randomUUID();
|
|
1800
1974
|
const ctx = { ...mergedCtx};
|
|
@@ -1847,7 +2021,7 @@ function wrapAgentStream(stream, instance, agentSettings, className, aiSDK, deps
|
|
|
1847
2021
|
const callParamsWithWrappedToolsAndModel = mergedArgsWithWrappedModel != null ? mergedArgsWithWrappedModel : {};
|
|
1848
2022
|
const finalize = async (result, error) => {
|
|
1849
2023
|
var _a3, _b3, _c2;
|
|
1850
|
-
const usage =
|
|
2024
|
+
const usage = extractUsageMetrics(result);
|
|
1851
2025
|
const model = extractModel(result);
|
|
1852
2026
|
const inputAttachments = autoAttachmentEnabled ? extractInputAttachmentsFromArgs(mergedArgs) : void 0;
|
|
1853
2027
|
const outputAttachments = autoAttachmentEnabled ? await extractOutputAttachmentsFromResult(result) : void 0;
|
|
@@ -1871,10 +2045,6 @@ function wrapAgentStream(stream, instance, agentSettings, className, aiSDK, deps
|
|
|
1871
2045
|
const finishReason = extractFinishReason(result);
|
|
1872
2046
|
const providerMetadata = isRecord(result) ? result["providerMetadata"] : void 0;
|
|
1873
2047
|
const resultToolCalls = isRecord(result) && Array.isArray(result["toolCalls"]) ? safeJsonWithUint8(result["toolCalls"]) : toolCalls.length ? safeJsonWithUint8(toolCalls) : void 0;
|
|
1874
|
-
const usageRec = isRecord(result) && isRecord(result["totalUsage"]) ? result["totalUsage"] : isRecord(result) && isRecord(result["usage"]) ? result["usage"] : void 0;
|
|
1875
|
-
const totalTokens = typeof (usageRec == null ? void 0 : usageRec["totalTokens"]) === "number" ? usageRec["totalTokens"] : void 0;
|
|
1876
|
-
const reasoningTokens = typeof (usageRec == null ? void 0 : usageRec["reasoningTokens"]) === "number" ? usageRec["reasoningTokens"] : void 0;
|
|
1877
|
-
const cachedInputTokens = typeof (usageRec == null ? void 0 : usageRec["cachedInputTokens"]) === "number" ? usageRec["cachedInputTokens"] : void 0;
|
|
1878
2048
|
deps.traceShipper.endSpan(rootSpan, {
|
|
1879
2049
|
attributes: [
|
|
1880
2050
|
...(telemetry == null ? void 0 : telemetry.recordOutputs) === false ? [] : [
|
|
@@ -1887,9 +2057,9 @@ function wrapAgentStream(stream, instance, agentSettings, className, aiSDK, deps
|
|
|
1887
2057
|
attrInt("ai.usage.completionTokens", usage == null ? void 0 : usage.outputTokens),
|
|
1888
2058
|
attrInt("ai.usage.inputTokens", usage == null ? void 0 : usage.inputTokens),
|
|
1889
2059
|
attrInt("ai.usage.outputTokens", usage == null ? void 0 : usage.outputTokens),
|
|
1890
|
-
attrInt("ai.usage.totalTokens", totalTokens),
|
|
1891
|
-
attrInt("ai.usage.reasoningTokens", reasoningTokens),
|
|
1892
|
-
attrInt("ai.usage.cachedInputTokens", cachedInputTokens),
|
|
2060
|
+
attrInt("ai.usage.totalTokens", usage == null ? void 0 : usage.totalTokens),
|
|
2061
|
+
attrInt("ai.usage.reasoningTokens", usage == null ? void 0 : usage.reasoningTokens),
|
|
2062
|
+
attrInt("ai.usage.cachedInputTokens", usage == null ? void 0 : usage.cachedInputTokens),
|
|
1893
2063
|
attrInt("ai.toolCall.count", toolCalls.length),
|
|
1894
2064
|
...error ? [
|
|
1895
2065
|
attrString(
|
|
@@ -2029,20 +2199,26 @@ function wrapToolExecute(name, tool, ctx, toolCalls) {
|
|
|
2029
2199
|
spanIdB64: span.ids.spanIdB64,
|
|
2030
2200
|
eventId: ctx.eventId
|
|
2031
2201
|
});
|
|
2032
|
-
const wrappedExecute = (...execArgs)
|
|
2202
|
+
const wrappedExecute = function(...execArgs) {
|
|
2033
2203
|
const toolArgs = execArgs[0];
|
|
2034
2204
|
const execOptions = execArgs.length > 1 ? execArgs[1] : void 0;
|
|
2035
2205
|
const toolCallId = isRecord(execOptions) && typeof execOptions["toolCallId"] === "string" ? execOptions["toolCallId"] : randomUUID();
|
|
2036
|
-
const result = originalExecute(
|
|
2037
|
-
if (
|
|
2206
|
+
const result = originalExecute.apply(this, execArgs);
|
|
2207
|
+
if (isAsyncIterable(result)) {
|
|
2038
2208
|
return (async function* () {
|
|
2039
2209
|
const parentCtx = await getCurrentParentSpanContext();
|
|
2040
2210
|
const parent = parentCtx && parentCtx.eventId === ctx.eventId ? { traceIdB64: parentCtx.traceIdB64, spanIdB64: parentCtx.spanIdB64 } : ctx.rootParentForChildren;
|
|
2041
2211
|
const toolSpan = createToolSpan(toolCallId, toolArgs, parent);
|
|
2042
2212
|
try {
|
|
2043
2213
|
let lastValue;
|
|
2044
|
-
const
|
|
2045
|
-
|
|
2214
|
+
const iterator = result[Symbol.asyncIterator]();
|
|
2215
|
+
const wrappedIterable = toolSpan ? asyncGeneratorWithCurrent(
|
|
2216
|
+
createContextSpan(toolSpan),
|
|
2217
|
+
iterator
|
|
2218
|
+
) : {
|
|
2219
|
+
[Symbol.asyncIterator]: () => iterator
|
|
2220
|
+
};
|
|
2221
|
+
for await (const value of wrappedIterable) {
|
|
2046
2222
|
lastValue = value;
|
|
2047
2223
|
yield value;
|
|
2048
2224
|
}
|
|
@@ -2388,14 +2564,19 @@ function wrapOnFinish(args, onFinish) {
|
|
|
2388
2564
|
return {
|
|
2389
2565
|
...args,
|
|
2390
2566
|
onFinish: async (result) => {
|
|
2567
|
+
let userError;
|
|
2391
2568
|
try {
|
|
2392
2569
|
const maybePromise = existing(result);
|
|
2393
2570
|
if (maybePromise && typeof maybePromise.then === "function") {
|
|
2394
2571
|
await maybePromise;
|
|
2395
2572
|
}
|
|
2396
|
-
} catch (
|
|
2573
|
+
} catch (error) {
|
|
2574
|
+
userError = error;
|
|
2397
2575
|
}
|
|
2398
2576
|
await onFinish(result);
|
|
2577
|
+
if (userError !== void 0) {
|
|
2578
|
+
throw userError;
|
|
2579
|
+
}
|
|
2399
2580
|
}
|
|
2400
2581
|
};
|
|
2401
2582
|
}
|
|
@@ -2520,6 +2701,7 @@ if (!globalThis.RAINDROP_ASYNC_LOCAL_STORAGE) {
|
|
|
2520
2701
|
globalThis.RAINDROP_ASYNC_LOCAL_STORAGE = async_hooks.AsyncLocalStorage;
|
|
2521
2702
|
}
|
|
2522
2703
|
|
|
2704
|
+
exports._resetWarnedMissingUserId = _resetWarnedMissingUserId;
|
|
2523
2705
|
exports.createRaindropAISDK = createRaindropAISDK;
|
|
2524
2706
|
exports.currentSpan = currentSpan;
|
|
2525
2707
|
exports.eventMetadata = eventMetadata;
|