@raindrop-ai/ai-sdk 0.0.2 → 0.0.3
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/dist/{chunk-FKMLMFKH.mjs → chunk-3R547TFD.mjs} +449 -71
- package/dist/index.d.mts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +449 -71
- package/dist/index.mjs +1 -1
- package/dist/index.node.js +449 -71
- package/dist/index.node.mjs +1 -1
- package/dist/index.workers.js +449 -71
- package/dist/index.workers.mjs +1 -1
- package/package.json +1 -1
|
@@ -84,7 +84,7 @@ async function postJson(url, body, headers, opts) {
|
|
|
84
84
|
// package.json
|
|
85
85
|
var package_default = {
|
|
86
86
|
name: "@raindrop-ai/ai-sdk",
|
|
87
|
-
version: "0.0.
|
|
87
|
+
version: "0.0.3"};
|
|
88
88
|
|
|
89
89
|
// src/internal/version.ts
|
|
90
90
|
var libraryName = package_default.name;
|
|
@@ -131,19 +131,30 @@ var EventShipper = class {
|
|
|
131
131
|
return this.debug;
|
|
132
132
|
}
|
|
133
133
|
async patch(eventId, patch) {
|
|
134
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
134
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
|
|
135
135
|
if (!this.enabled) return;
|
|
136
136
|
if (!eventId) return;
|
|
137
|
-
|
|
138
|
-
|
|
137
|
+
if (this.debug) {
|
|
138
|
+
console.log("[raindrop-ai/ai-sdk] queue patch", {
|
|
139
|
+
eventId,
|
|
140
|
+
userId: patch.userId,
|
|
141
|
+
eventName: patch.eventName,
|
|
142
|
+
hasInput: typeof patch.input === "string" && patch.input.length > 0,
|
|
143
|
+
hasOutput: typeof patch.output === "string" && patch.output.length > 0,
|
|
144
|
+
attachments: (_b = (_a = patch.attachments) == null ? void 0 : _a.length) != null ? _b : 0,
|
|
145
|
+
isPending: patch.isPending
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
const sticky = (_c = this.sticky.get(eventId)) != null ? _c : {};
|
|
149
|
+
const existing = (_d = this.buffers.get(eventId)) != null ? _d : {};
|
|
139
150
|
const merged = mergePatches(existing, patch);
|
|
140
|
-
merged.isPending = (
|
|
151
|
+
merged.isPending = (_g = (_f = (_e = patch.isPending) != null ? _e : existing.isPending) != null ? _f : sticky.isPending) != null ? _g : true;
|
|
141
152
|
this.buffers.set(eventId, merged);
|
|
142
153
|
this.sticky.set(eventId, {
|
|
143
|
-
userId: (
|
|
144
|
-
convoId: (
|
|
145
|
-
eventName: (
|
|
146
|
-
isPending: (
|
|
154
|
+
userId: (_h = merged.userId) != null ? _h : sticky.userId,
|
|
155
|
+
convoId: (_i = merged.convoId) != null ? _i : sticky.convoId,
|
|
156
|
+
eventName: (_j = merged.eventName) != null ? _j : sticky.eventName,
|
|
157
|
+
isPending: (_k = merged.isPending) != null ? _k : sticky.isPending
|
|
147
158
|
});
|
|
148
159
|
const t = this.timers.get(eventId);
|
|
149
160
|
if (t) clearTimeout(t);
|
|
@@ -240,7 +251,7 @@ var EventShipper = class {
|
|
|
240
251
|
}
|
|
241
252
|
}
|
|
242
253
|
async flushOne(eventId) {
|
|
243
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
254
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
|
|
244
255
|
if (!this.enabled) return;
|
|
245
256
|
const timer = this.timers.get(eventId);
|
|
246
257
|
if (timer) {
|
|
@@ -278,6 +289,24 @@ var EventShipper = class {
|
|
|
278
289
|
is_pending: ((_i = (_h = accumulated.isPending) != null ? _h : sticky.isPending) != null ? _i : true) !== false
|
|
279
290
|
};
|
|
280
291
|
const url = `${this.baseUrl}events/track_partial`;
|
|
292
|
+
if (this.debug) {
|
|
293
|
+
console.log("[raindrop-ai/ai-sdk] sending track_partial", {
|
|
294
|
+
eventId,
|
|
295
|
+
eventName,
|
|
296
|
+
userId,
|
|
297
|
+
isPending: payload.is_pending,
|
|
298
|
+
inputPreview: typeof accumulated.input === "string" ? accumulated.input.slice(0, 120) : void 0,
|
|
299
|
+
outputPreview: typeof accumulated.output === "string" ? accumulated.output.slice(0, 120) : void 0,
|
|
300
|
+
attachments: (_k = (_j = accumulated.attachments) == null ? void 0 : _j.length) != null ? _k : 0,
|
|
301
|
+
attachmentKinds: (_m = (_l = accumulated.attachments) == null ? void 0 : _l.map((a) => ({
|
|
302
|
+
type: a.type,
|
|
303
|
+
role: a.role,
|
|
304
|
+
name: a.name,
|
|
305
|
+
valuePreview: a.value.slice(0, 60)
|
|
306
|
+
}))) != null ? _m : [],
|
|
307
|
+
endpoint: url
|
|
308
|
+
});
|
|
309
|
+
}
|
|
281
310
|
const p = postJson(
|
|
282
311
|
url,
|
|
283
312
|
payload,
|
|
@@ -520,6 +549,12 @@ var TraceShipper = class {
|
|
|
520
549
|
const batch = this.queue.splice(0, this.maxBatchSize);
|
|
521
550
|
const body = buildExportTraceServiceRequest(batch);
|
|
522
551
|
const url = `${this.baseUrl}traces`;
|
|
552
|
+
if (this.debug) {
|
|
553
|
+
console.log("[raindrop-ai/ai-sdk] sending traces batch", {
|
|
554
|
+
spans: batch.length,
|
|
555
|
+
endpoint: url
|
|
556
|
+
});
|
|
557
|
+
}
|
|
523
558
|
const p = postJson(
|
|
524
559
|
url,
|
|
525
560
|
body,
|
|
@@ -857,6 +892,174 @@ function extractFinishReason(result) {
|
|
|
857
892
|
}
|
|
858
893
|
return void 0;
|
|
859
894
|
}
|
|
895
|
+
function bytesToBase64(bytes) {
|
|
896
|
+
if (typeof Buffer !== "undefined") return Buffer.from(bytes).toString("base64");
|
|
897
|
+
let binary = "";
|
|
898
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
899
|
+
binary += String.fromCharCode(bytes[i]);
|
|
900
|
+
}
|
|
901
|
+
if (typeof btoa === "function") return btoa(binary);
|
|
902
|
+
return "";
|
|
903
|
+
}
|
|
904
|
+
function asDataUrl(value, mediaType) {
|
|
905
|
+
if (value instanceof URL) return value.toString();
|
|
906
|
+
if (typeof value === "string") {
|
|
907
|
+
if (value.startsWith("data:")) return value;
|
|
908
|
+
if (value.startsWith("http://") || value.startsWith("https://")) return value;
|
|
909
|
+
return `data:${mediaType};base64,${value}`;
|
|
910
|
+
}
|
|
911
|
+
if (value instanceof Uint8Array) {
|
|
912
|
+
const base64 = bytesToBase64(value);
|
|
913
|
+
if (!base64) return void 0;
|
|
914
|
+
return `data:${mediaType};base64,${base64}`;
|
|
915
|
+
}
|
|
916
|
+
if (value instanceof ArrayBuffer) {
|
|
917
|
+
const base64 = bytesToBase64(new Uint8Array(value));
|
|
918
|
+
if (!base64) return void 0;
|
|
919
|
+
return `data:${mediaType};base64,${base64}`;
|
|
920
|
+
}
|
|
921
|
+
return void 0;
|
|
922
|
+
}
|
|
923
|
+
function dataPartToAttachmentValue(value) {
|
|
924
|
+
if (typeof value === "string") return value;
|
|
925
|
+
if (value instanceof URL) return value.toString();
|
|
926
|
+
if (value instanceof Uint8Array) return `[binary:${value.byteLength} bytes]`;
|
|
927
|
+
if (value instanceof ArrayBuffer) return `[binary:${value.byteLength} bytes]`;
|
|
928
|
+
return void 0;
|
|
929
|
+
}
|
|
930
|
+
function attachmentMediaType(part) {
|
|
931
|
+
if (typeof part["mediaType"] === "string") return part["mediaType"];
|
|
932
|
+
if (typeof part["mimeType"] === "string") return part["mimeType"];
|
|
933
|
+
const file = part["file"];
|
|
934
|
+
if (isRecord(file)) {
|
|
935
|
+
if (typeof file["mediaType"] === "string") return file["mediaType"];
|
|
936
|
+
if (typeof file["mimeType"] === "string") return file["mimeType"];
|
|
937
|
+
}
|
|
938
|
+
return void 0;
|
|
939
|
+
}
|
|
940
|
+
function attachmentName(part) {
|
|
941
|
+
if (typeof part["filename"] === "string") return part["filename"];
|
|
942
|
+
if (typeof part["name"] === "string") return part["name"];
|
|
943
|
+
const file = part["file"];
|
|
944
|
+
if (isRecord(file)) {
|
|
945
|
+
if (typeof file["filename"] === "string") return file["filename"];
|
|
946
|
+
if (typeof file["name"] === "string") return file["name"];
|
|
947
|
+
}
|
|
948
|
+
return void 0;
|
|
949
|
+
}
|
|
950
|
+
function attachmentData(part) {
|
|
951
|
+
if ("data" in part) return part["data"];
|
|
952
|
+
const file = part["file"];
|
|
953
|
+
if (isRecord(file)) {
|
|
954
|
+
if ("file_data" in file) return file["file_data"];
|
|
955
|
+
if ("data" in file) return file["data"];
|
|
956
|
+
}
|
|
957
|
+
return void 0;
|
|
958
|
+
}
|
|
959
|
+
function contentPartToAttachment(part, role) {
|
|
960
|
+
var _a, _b, _c;
|
|
961
|
+
const partType = part["type"];
|
|
962
|
+
if (typeof partType !== "string") return void 0;
|
|
963
|
+
if (partType === "image") {
|
|
964
|
+
const mediaType = (_a = attachmentMediaType(part)) != null ? _a : "image/png";
|
|
965
|
+
const value = asDataUrl(part["image"], mediaType);
|
|
966
|
+
if (!value) return void 0;
|
|
967
|
+
return { type: "image", role, value };
|
|
968
|
+
}
|
|
969
|
+
if (partType === "image_url") {
|
|
970
|
+
const imageUrlPart = part["image_url"];
|
|
971
|
+
const imageUrlValue = isRecord(imageUrlPart) ? imageUrlPart["url"] : imageUrlPart;
|
|
972
|
+
const value = asDataUrl(imageUrlValue, "image/png");
|
|
973
|
+
if (!value) return void 0;
|
|
974
|
+
return { type: "image", role, value };
|
|
975
|
+
}
|
|
976
|
+
if (partType === "file") {
|
|
977
|
+
const mediaType = attachmentMediaType(part);
|
|
978
|
+
const data = attachmentData(part);
|
|
979
|
+
const isImage = (mediaType == null ? void 0 : mediaType.startsWith("image/")) === true;
|
|
980
|
+
const value = isImage && mediaType ? asDataUrl(data, mediaType) : dataPartToAttachmentValue(data);
|
|
981
|
+
if (!value) return void 0;
|
|
982
|
+
const name = (_c = (_b = attachmentName(part)) != null ? _b : mediaType) != null ? _c : "file";
|
|
983
|
+
return { type: isImage ? "image" : "text", role, name, value };
|
|
984
|
+
}
|
|
985
|
+
return void 0;
|
|
986
|
+
}
|
|
987
|
+
function attachmentsFromContent(content, role) {
|
|
988
|
+
if (!Array.isArray(content)) return void 0;
|
|
989
|
+
const attachments = [];
|
|
990
|
+
for (const part of content) {
|
|
991
|
+
if (!isRecord(part)) continue;
|
|
992
|
+
const attachment = contentPartToAttachment(part, role);
|
|
993
|
+
if (attachment) attachments.push(attachment);
|
|
994
|
+
}
|
|
995
|
+
return attachments.length ? attachments : void 0;
|
|
996
|
+
}
|
|
997
|
+
function generatedFileToAttachment(file) {
|
|
998
|
+
var _a, _b, _c, _d;
|
|
999
|
+
const mediaType = typeof file["mediaType"] === "string" ? file["mediaType"] : typeof file["mimeType"] === "string" ? file["mimeType"] : void 0;
|
|
1000
|
+
const data = (_d = (_c = (_b = (_a = file["base64Data"]) != null ? _a : file["base64"]) != null ? _b : file["uint8ArrayData"]) != null ? _c : file["uint8Array"]) != null ? _d : file["data"];
|
|
1001
|
+
const isImage = (mediaType == null ? void 0 : mediaType.startsWith("image/")) === true;
|
|
1002
|
+
const value = isImage && mediaType ? asDataUrl(data, mediaType) : dataPartToAttachmentValue(data);
|
|
1003
|
+
if (!value) return void 0;
|
|
1004
|
+
const name = typeof file["filename"] === "string" ? file["filename"] : typeof file["name"] === "string" ? file["name"] : mediaType != null ? mediaType : "file";
|
|
1005
|
+
return {
|
|
1006
|
+
type: isImage ? "image" : "text",
|
|
1007
|
+
role: "output",
|
|
1008
|
+
name,
|
|
1009
|
+
value
|
|
1010
|
+
};
|
|
1011
|
+
}
|
|
1012
|
+
async function outputAttachmentsFromFiles(files) {
|
|
1013
|
+
let resolvedFiles = files;
|
|
1014
|
+
if (resolvedFiles && (typeof resolvedFiles === "object" || typeof resolvedFiles === "function") && typeof resolvedFiles.then === "function") {
|
|
1015
|
+
try {
|
|
1016
|
+
resolvedFiles = await resolvedFiles;
|
|
1017
|
+
} catch (e) {
|
|
1018
|
+
return void 0;
|
|
1019
|
+
}
|
|
1020
|
+
}
|
|
1021
|
+
if (!Array.isArray(resolvedFiles)) return void 0;
|
|
1022
|
+
const attachments = [];
|
|
1023
|
+
for (const file of resolvedFiles) {
|
|
1024
|
+
if (!isRecord(file)) continue;
|
|
1025
|
+
const attachment = generatedFileToAttachment(file);
|
|
1026
|
+
if (attachment) attachments.push(attachment);
|
|
1027
|
+
}
|
|
1028
|
+
return attachments.length ? attachments : void 0;
|
|
1029
|
+
}
|
|
1030
|
+
function extractTextFromMessageContent(content) {
|
|
1031
|
+
if (typeof content === "string") return content;
|
|
1032
|
+
if (!Array.isArray(content)) return void 0;
|
|
1033
|
+
let result = "";
|
|
1034
|
+
for (const part of content) {
|
|
1035
|
+
if (!isRecord(part) || part["type"] !== "text" || typeof part["text"] !== "string") continue;
|
|
1036
|
+
result += part["text"];
|
|
1037
|
+
}
|
|
1038
|
+
return result.length ? result : void 0;
|
|
1039
|
+
}
|
|
1040
|
+
function extractInputAttachmentsFromArgs(args) {
|
|
1041
|
+
if (!isRecord(args)) return void 0;
|
|
1042
|
+
const messages = args["messages"];
|
|
1043
|
+
if (!Array.isArray(messages)) return void 0;
|
|
1044
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
1045
|
+
const message = messages[i];
|
|
1046
|
+
if (!isRecord(message) || message["role"] !== "user") continue;
|
|
1047
|
+
return attachmentsFromContent(message["content"], "input");
|
|
1048
|
+
}
|
|
1049
|
+
return void 0;
|
|
1050
|
+
}
|
|
1051
|
+
async function extractOutputAttachmentsFromResult(result) {
|
|
1052
|
+
if (!isRecord(result)) return void 0;
|
|
1053
|
+
const fileAttachments = await outputAttachmentsFromFiles(result["files"]);
|
|
1054
|
+
if (fileAttachments == null ? void 0 : fileAttachments.length) return fileAttachments;
|
|
1055
|
+
const responseMessages = extractResponseMessages(result);
|
|
1056
|
+
for (let i = responseMessages.length - 1; i >= 0; i--) {
|
|
1057
|
+
const message = responseMessages[i];
|
|
1058
|
+
if (!isRecord(message) || message["role"] !== "assistant") continue;
|
|
1059
|
+
return attachmentsFromContent(message["content"], "output");
|
|
1060
|
+
}
|
|
1061
|
+
return attachmentsFromContent(result["content"], "output");
|
|
1062
|
+
}
|
|
860
1063
|
function lastUserMessageTextFromArgs(args) {
|
|
861
1064
|
var _a;
|
|
862
1065
|
if (!isRecord(args)) return void 0;
|
|
@@ -866,7 +1069,8 @@ function lastUserMessageTextFromArgs(args) {
|
|
|
866
1069
|
const message = messages[i];
|
|
867
1070
|
if (!isRecord(message) || message["role"] !== "user") continue;
|
|
868
1071
|
const content = message["content"];
|
|
869
|
-
|
|
1072
|
+
const text = extractTextFromMessageContent(content);
|
|
1073
|
+
if (text !== void 0) return text;
|
|
870
1074
|
return (_a = safeJsonWithUint8(content)) != null ? _a : String(content);
|
|
871
1075
|
}
|
|
872
1076
|
return void 0;
|
|
@@ -881,7 +1085,8 @@ function extractInputFromArgs(args) {
|
|
|
881
1085
|
const last = messages[messages.length - 1];
|
|
882
1086
|
if (isRecord(last)) {
|
|
883
1087
|
const content = last["content"];
|
|
884
|
-
|
|
1088
|
+
const text = extractTextFromMessageContent(content);
|
|
1089
|
+
if (text !== void 0) return text;
|
|
885
1090
|
const asJson = safeJson(content);
|
|
886
1091
|
if (asJson) return asJson;
|
|
887
1092
|
}
|
|
@@ -915,7 +1120,9 @@ function extractResponseMessages(result) {
|
|
|
915
1120
|
if (!isRecord(result)) return [];
|
|
916
1121
|
const response = result["response"];
|
|
917
1122
|
if (isRecord(response) && Array.isArray(response["messages"])) {
|
|
918
|
-
return response["messages"].filter(
|
|
1123
|
+
return response["messages"].filter(
|
|
1124
|
+
(message) => isRecord(message) && typeof message["role"] === "string" && "content" in message
|
|
1125
|
+
).map((message) => message);
|
|
919
1126
|
}
|
|
920
1127
|
const steps = result["steps"];
|
|
921
1128
|
if (Array.isArray(steps) && steps.length > 0) {
|
|
@@ -923,7 +1130,9 @@ function extractResponseMessages(result) {
|
|
|
923
1130
|
if (isRecord(lastStep) && isRecord(lastStep["response"])) {
|
|
924
1131
|
const responseMessages = lastStep["response"]["messages"];
|
|
925
1132
|
if (Array.isArray(responseMessages)) {
|
|
926
|
-
return responseMessages.filter(
|
|
1133
|
+
return responseMessages.filter(
|
|
1134
|
+
(message) => isRecord(message) && typeof message["role"] === "string" && "content" in message
|
|
1135
|
+
).map((message) => message);
|
|
927
1136
|
}
|
|
928
1137
|
}
|
|
929
1138
|
}
|
|
@@ -973,9 +1182,11 @@ function opName(operationId, functionId) {
|
|
|
973
1182
|
function toOtlpAttr(key, value) {
|
|
974
1183
|
if (value === void 0 || value === null) return void 0;
|
|
975
1184
|
if (typeof value === "string") return attrString(key, value);
|
|
976
|
-
if (typeof value === "number")
|
|
1185
|
+
if (typeof value === "number")
|
|
1186
|
+
return Number.isInteger(value) ? attrInt(key, value) : attrDouble(key, value);
|
|
977
1187
|
if (typeof value === "boolean") return attrBool(key, value);
|
|
978
|
-
if (Array.isArray(value) && value.every((v) => typeof v === "string"))
|
|
1188
|
+
if (Array.isArray(value) && value.every((v) => typeof v === "string"))
|
|
1189
|
+
return attrStringArray(key, value);
|
|
979
1190
|
const asJson = safeJsonWithUint8(value);
|
|
980
1191
|
return asJson ? attrString(key, asJson) : void 0;
|
|
981
1192
|
}
|
|
@@ -1021,13 +1232,31 @@ function attrsFromSettings(args) {
|
|
|
1021
1232
|
function attrsFromGenAiRequest(options) {
|
|
1022
1233
|
if (!isRecord(options)) return [];
|
|
1023
1234
|
return [
|
|
1024
|
-
attrDouble(
|
|
1025
|
-
|
|
1026
|
-
|
|
1235
|
+
attrDouble(
|
|
1236
|
+
"gen_ai.request.frequency_penalty",
|
|
1237
|
+
typeof options["frequencyPenalty"] === "number" ? options["frequencyPenalty"] : void 0
|
|
1238
|
+
),
|
|
1239
|
+
attrInt(
|
|
1240
|
+
"gen_ai.request.max_tokens",
|
|
1241
|
+
typeof options["maxOutputTokens"] === "number" ? options["maxOutputTokens"] : void 0
|
|
1242
|
+
),
|
|
1243
|
+
attrDouble(
|
|
1244
|
+
"gen_ai.request.presence_penalty",
|
|
1245
|
+
typeof options["presencePenalty"] === "number" ? options["presencePenalty"] : void 0
|
|
1246
|
+
),
|
|
1027
1247
|
...Array.isArray(options["stopSequences"]) && options["stopSequences"].every((x) => typeof x === "string") ? [attrStringArray("gen_ai.request.stop_sequences", options["stopSequences"])] : [],
|
|
1028
|
-
attrDouble(
|
|
1029
|
-
|
|
1030
|
-
|
|
1248
|
+
attrDouble(
|
|
1249
|
+
"gen_ai.request.temperature",
|
|
1250
|
+
typeof options["temperature"] === "number" ? options["temperature"] : void 0
|
|
1251
|
+
),
|
|
1252
|
+
attrInt(
|
|
1253
|
+
"gen_ai.request.top_k",
|
|
1254
|
+
typeof options["topK"] === "number" ? options["topK"] : void 0
|
|
1255
|
+
),
|
|
1256
|
+
attrDouble(
|
|
1257
|
+
"gen_ai.request.top_p",
|
|
1258
|
+
typeof options["topP"] === "number" ? options["topP"] : void 0
|
|
1259
|
+
)
|
|
1031
1260
|
];
|
|
1032
1261
|
}
|
|
1033
1262
|
|
|
@@ -1075,6 +1304,7 @@ function wrapAISDK(aiSDK, deps) {
|
|
|
1075
1304
|
const agentClasses = /* @__PURE__ */ new Set(["ToolLoopAgent"]);
|
|
1076
1305
|
const sendEvents = ((_a = deps.options.send) == null ? void 0 : _a.events) !== false;
|
|
1077
1306
|
const sendTraces = ((_b = deps.options.send) == null ? void 0 : _b.traces) !== false;
|
|
1307
|
+
const autoAttachmentEnabled = deps.options.autoAttachment !== false;
|
|
1078
1308
|
const proxyTarget = isModuleNamespace(aiSDK) ? Object.setPrototypeOf({}, aiSDK) : aiSDK;
|
|
1079
1309
|
return new Proxy(proxyTarget, {
|
|
1080
1310
|
get(target, prop, receiver) {
|
|
@@ -1128,14 +1358,27 @@ function wrapAISDK(aiSDK, deps) {
|
|
|
1128
1358
|
]
|
|
1129
1359
|
}) : void 0;
|
|
1130
1360
|
const rootParentForChildren = rootSpan ? { traceIdB64: rootSpan.ids.traceIdB64, spanIdB64: rootSpan.ids.spanIdB64 } : inheritedParent;
|
|
1131
|
-
const wrapCtx = {
|
|
1361
|
+
const wrapCtx = {
|
|
1362
|
+
eventId,
|
|
1363
|
+
telemetry,
|
|
1364
|
+
sendTraces,
|
|
1365
|
+
traceShipper: deps.traceShipper,
|
|
1366
|
+
rootParentForChildren
|
|
1367
|
+
};
|
|
1132
1368
|
const toolCalls = [];
|
|
1133
1369
|
const argWithWrappedTools = wrapTools(arg, wrapCtx, toolCalls);
|
|
1134
|
-
const argWithWrappedModel = wrapModel(
|
|
1370
|
+
const argWithWrappedModel = wrapModel(
|
|
1371
|
+
argWithWrappedTools,
|
|
1372
|
+
aiSDK,
|
|
1373
|
+
outerOperationId,
|
|
1374
|
+
wrapCtx
|
|
1375
|
+
);
|
|
1135
1376
|
const finalize = async (result, error) => {
|
|
1136
1377
|
var _a3, _b3, _c2;
|
|
1137
1378
|
const usage = extractUsage(result);
|
|
1138
1379
|
const model = extractModel(result);
|
|
1380
|
+
const inputAttachments = autoAttachmentEnabled ? extractInputAttachmentsFromArgs(arg) : void 0;
|
|
1381
|
+
const outputAttachments = autoAttachmentEnabled ? await extractOutputAttachmentsFromResult(result) : void 0;
|
|
1139
1382
|
const baseMessages = coerceMessagesFromArgs(arg);
|
|
1140
1383
|
const responseMessages = extractResponseMessages(result);
|
|
1141
1384
|
const allMessages = [...baseMessages, ...responseMessages];
|
|
@@ -1148,7 +1391,7 @@ function wrapAISDK(aiSDK, deps) {
|
|
|
1148
1391
|
output: defaultOutput,
|
|
1149
1392
|
model,
|
|
1150
1393
|
properties: ctx.properties,
|
|
1151
|
-
attachments: ctx.attachments
|
|
1394
|
+
attachments: mergeAttachments(ctx.attachments, inputAttachments, outputAttachments)
|
|
1152
1395
|
};
|
|
1153
1396
|
const built = await maybeBuildEvent(deps.options.buildEvent, allMessages);
|
|
1154
1397
|
const patch = mergeBuildEventPatch(defaultPatch, built);
|
|
@@ -1168,7 +1411,10 @@ function wrapAISDK(aiSDK, deps) {
|
|
|
1168
1411
|
attrString("ai.response.finishReason", finishReason),
|
|
1169
1412
|
operation === "generateObject" || operation === "streamObject" ? attrString("ai.response.object", output) : attrString("ai.response.text", output),
|
|
1170
1413
|
attrString("ai.response.toolCalls", resultToolCalls),
|
|
1171
|
-
attrString(
|
|
1414
|
+
attrString(
|
|
1415
|
+
"ai.response.providerMetadata",
|
|
1416
|
+
safeJsonWithUint8(providerMetadata)
|
|
1417
|
+
)
|
|
1172
1418
|
],
|
|
1173
1419
|
attrInt("ai.usage.promptTokens", usage == null ? void 0 : usage.inputTokens),
|
|
1174
1420
|
attrInt("ai.usage.completionTokens", usage == null ? void 0 : usage.outputTokens),
|
|
@@ -1178,7 +1424,12 @@ function wrapAISDK(aiSDK, deps) {
|
|
|
1178
1424
|
attrInt("ai.usage.reasoningTokens", reasoningTokens),
|
|
1179
1425
|
attrInt("ai.usage.cachedInputTokens", cachedInputTokens),
|
|
1180
1426
|
attrInt("ai.toolCall.count", toolCalls.length),
|
|
1181
|
-
...error ? [
|
|
1427
|
+
...error ? [
|
|
1428
|
+
attrString(
|
|
1429
|
+
"error.message",
|
|
1430
|
+
error instanceof Error ? error.message : String(error)
|
|
1431
|
+
)
|
|
1432
|
+
] : []
|
|
1182
1433
|
]
|
|
1183
1434
|
});
|
|
1184
1435
|
}
|
|
@@ -1194,12 +1445,18 @@ function wrapAISDK(aiSDK, deps) {
|
|
|
1194
1445
|
attachments: patch.attachments,
|
|
1195
1446
|
isPending: false
|
|
1196
1447
|
}).catch((err) => {
|
|
1197
|
-
if (debug)
|
|
1448
|
+
if (debug)
|
|
1449
|
+
console.warn(
|
|
1450
|
+
`[raindrop-ai/ai-sdk] event patch failed: ${err instanceof Error ? err.message : err}`
|
|
1451
|
+
);
|
|
1198
1452
|
});
|
|
1199
1453
|
}
|
|
1200
1454
|
};
|
|
1201
1455
|
const callOriginal = async (...args) => {
|
|
1202
|
-
return await original.call(
|
|
1456
|
+
return await original.call(
|
|
1457
|
+
aiSDK,
|
|
1458
|
+
...args
|
|
1459
|
+
);
|
|
1203
1460
|
};
|
|
1204
1461
|
const runWithContext = async (fn) => {
|
|
1205
1462
|
if (!rootSpan) return await fn();
|
|
@@ -1213,7 +1470,10 @@ function wrapAISDK(aiSDK, deps) {
|
|
|
1213
1470
|
try {
|
|
1214
1471
|
await finalize(result);
|
|
1215
1472
|
} catch (err) {
|
|
1216
|
-
if (debug)
|
|
1473
|
+
if (debug)
|
|
1474
|
+
console.warn(
|
|
1475
|
+
`[raindrop-ai/ai-sdk] finalize failed: ${err instanceof Error ? err.message : err}`
|
|
1476
|
+
);
|
|
1217
1477
|
}
|
|
1218
1478
|
});
|
|
1219
1479
|
try {
|
|
@@ -1240,7 +1500,10 @@ function wrapAISDK(aiSDK, deps) {
|
|
|
1240
1500
|
try {
|
|
1241
1501
|
await finalize(void 0, error);
|
|
1242
1502
|
} catch (err) {
|
|
1243
|
-
if (debug)
|
|
1503
|
+
if (debug)
|
|
1504
|
+
console.warn(
|
|
1505
|
+
`[raindrop-ai/ai-sdk] finalize failed: ${err instanceof Error ? err.message : err}`
|
|
1506
|
+
);
|
|
1244
1507
|
}
|
|
1245
1508
|
throw error;
|
|
1246
1509
|
}
|
|
@@ -1254,14 +1517,20 @@ function wrapAISDK(aiSDK, deps) {
|
|
|
1254
1517
|
try {
|
|
1255
1518
|
await finalize(result);
|
|
1256
1519
|
} catch (err) {
|
|
1257
|
-
if (debug)
|
|
1520
|
+
if (debug)
|
|
1521
|
+
console.warn(
|
|
1522
|
+
`[raindrop-ai/ai-sdk] finalize failed: ${err instanceof Error ? err.message : err}`
|
|
1523
|
+
);
|
|
1258
1524
|
}
|
|
1259
1525
|
return result;
|
|
1260
1526
|
} catch (error) {
|
|
1261
1527
|
try {
|
|
1262
1528
|
await finalize(void 0, error);
|
|
1263
1529
|
} catch (err) {
|
|
1264
|
-
if (debug)
|
|
1530
|
+
if (debug)
|
|
1531
|
+
console.warn(
|
|
1532
|
+
`[raindrop-ai/ai-sdk] finalize failed: ${err instanceof Error ? err.message : err}`
|
|
1533
|
+
);
|
|
1265
1534
|
}
|
|
1266
1535
|
throw error;
|
|
1267
1536
|
}
|
|
@@ -1313,6 +1582,7 @@ function wrapAgentGenerate(generate, instance, agentSettings, className, aiSDK,
|
|
|
1313
1582
|
var _a, _b;
|
|
1314
1583
|
const sendEvents = ((_a = deps.options.send) == null ? void 0 : _a.events) !== false;
|
|
1315
1584
|
const sendTraces = ((_b = deps.options.send) == null ? void 0 : _b.traces) !== false;
|
|
1585
|
+
const autoAttachmentEnabled = deps.options.autoAttachment !== false;
|
|
1316
1586
|
return async (...callArgs) => {
|
|
1317
1587
|
var _a2, _b2, _c, _d;
|
|
1318
1588
|
const callParams = callArgs[0];
|
|
@@ -1356,13 +1626,21 @@ function wrapAgentGenerate(generate, instance, agentSettings, className, aiSDK,
|
|
|
1356
1626
|
]
|
|
1357
1627
|
}) : void 0;
|
|
1358
1628
|
const rootParentForChildren = rootSpan ? { traceIdB64: rootSpan.ids.traceIdB64, spanIdB64: rootSpan.ids.spanIdB64 } : inheritedParent;
|
|
1359
|
-
const wrapCtx = {
|
|
1629
|
+
const wrapCtx = {
|
|
1630
|
+
eventId,
|
|
1631
|
+
telemetry,
|
|
1632
|
+
sendTraces,
|
|
1633
|
+
traceShipper: deps.traceShipper,
|
|
1634
|
+
rootParentForChildren
|
|
1635
|
+
};
|
|
1360
1636
|
const toolCalls = [];
|
|
1361
1637
|
const callParamsWithWrappedTools = callParams ? wrapTools(callParams, wrapCtx, toolCalls) : callParams;
|
|
1362
1638
|
const finalize = async (result, error) => {
|
|
1363
1639
|
var _a3, _b3, _c2;
|
|
1364
1640
|
const usage = extractUsage(result);
|
|
1365
1641
|
const model = extractModel(result);
|
|
1642
|
+
const inputAttachments = autoAttachmentEnabled ? extractInputAttachmentsFromArgs(mergedArgs) : void 0;
|
|
1643
|
+
const outputAttachments = autoAttachmentEnabled ? await extractOutputAttachmentsFromResult(result) : void 0;
|
|
1366
1644
|
const baseMessages = coerceMessagesFromArgs(mergedArgs);
|
|
1367
1645
|
const responseMessages = extractResponseMessages(result);
|
|
1368
1646
|
const allMessages = [...baseMessages, ...responseMessages];
|
|
@@ -1373,7 +1651,7 @@ function wrapAgentGenerate(generate, instance, agentSettings, className, aiSDK,
|
|
|
1373
1651
|
output: outputText,
|
|
1374
1652
|
model,
|
|
1375
1653
|
properties: ctx.properties,
|
|
1376
|
-
attachments: ctx.attachments
|
|
1654
|
+
attachments: mergeAttachments(ctx.attachments, inputAttachments, outputAttachments)
|
|
1377
1655
|
};
|
|
1378
1656
|
const built = await maybeBuildEvent(deps.options.buildEvent, allMessages);
|
|
1379
1657
|
const patch = mergeBuildEventPatch(defaultPatch, built);
|
|
@@ -1403,7 +1681,12 @@ function wrapAgentGenerate(generate, instance, agentSettings, className, aiSDK,
|
|
|
1403
1681
|
attrInt("ai.usage.reasoningTokens", reasoningTokens),
|
|
1404
1682
|
attrInt("ai.usage.cachedInputTokens", cachedInputTokens),
|
|
1405
1683
|
attrInt("ai.toolCall.count", toolCalls.length),
|
|
1406
|
-
...error ? [
|
|
1684
|
+
...error ? [
|
|
1685
|
+
attrString(
|
|
1686
|
+
"error.message",
|
|
1687
|
+
error instanceof Error ? error.message : String(error)
|
|
1688
|
+
)
|
|
1689
|
+
] : []
|
|
1407
1690
|
]
|
|
1408
1691
|
});
|
|
1409
1692
|
}
|
|
@@ -1427,7 +1710,10 @@ function wrapAgentGenerate(generate, instance, agentSettings, className, aiSDK,
|
|
|
1427
1710
|
attachments: patch.attachments,
|
|
1428
1711
|
isPending: false
|
|
1429
1712
|
}).catch((err) => {
|
|
1430
|
-
if (debug)
|
|
1713
|
+
if (debug)
|
|
1714
|
+
console.warn(
|
|
1715
|
+
`[raindrop-ai/ai-sdk] event patch failed: ${err instanceof Error ? err.message : err}`
|
|
1716
|
+
);
|
|
1431
1717
|
});
|
|
1432
1718
|
} else if (debug) {
|
|
1433
1719
|
console.log(`[raindrop-ai/ai-sdk] Agent ${operation} sendEvents=false, skipping event`);
|
|
@@ -1457,14 +1743,20 @@ function wrapAgentGenerate(generate, instance, agentSettings, className, aiSDK,
|
|
|
1457
1743
|
try {
|
|
1458
1744
|
await finalize(result);
|
|
1459
1745
|
} catch (err) {
|
|
1460
|
-
if (debug)
|
|
1746
|
+
if (debug)
|
|
1747
|
+
console.warn(
|
|
1748
|
+
`[raindrop-ai/ai-sdk] finalize failed: ${err instanceof Error ? err.message : err}`
|
|
1749
|
+
);
|
|
1461
1750
|
}
|
|
1462
1751
|
return result;
|
|
1463
1752
|
} catch (error) {
|
|
1464
1753
|
try {
|
|
1465
1754
|
await finalize(void 0, error);
|
|
1466
1755
|
} catch (err) {
|
|
1467
|
-
if (debug)
|
|
1756
|
+
if (debug)
|
|
1757
|
+
console.warn(
|
|
1758
|
+
`[raindrop-ai/ai-sdk] finalize failed: ${err instanceof Error ? err.message : err}`
|
|
1759
|
+
);
|
|
1468
1760
|
}
|
|
1469
1761
|
throw error;
|
|
1470
1762
|
}
|
|
@@ -1474,6 +1766,7 @@ function wrapAgentStream(stream, instance, agentSettings, className, aiSDK, deps
|
|
|
1474
1766
|
var _a, _b;
|
|
1475
1767
|
const sendEvents = ((_a = deps.options.send) == null ? void 0 : _a.events) !== false;
|
|
1476
1768
|
const sendTraces = ((_b = deps.options.send) == null ? void 0 : _b.traces) !== false;
|
|
1769
|
+
const autoAttachmentEnabled = deps.options.autoAttachment !== false;
|
|
1477
1770
|
return async (...callArgs) => {
|
|
1478
1771
|
var _a2, _b2, _c, _d;
|
|
1479
1772
|
const callParams = callArgs[0];
|
|
@@ -1517,13 +1810,21 @@ function wrapAgentStream(stream, instance, agentSettings, className, aiSDK, deps
|
|
|
1517
1810
|
]
|
|
1518
1811
|
}) : void 0;
|
|
1519
1812
|
const rootParentForChildren = rootSpan ? { traceIdB64: rootSpan.ids.traceIdB64, spanIdB64: rootSpan.ids.spanIdB64 } : inheritedParent;
|
|
1520
|
-
const wrapCtx = {
|
|
1813
|
+
const wrapCtx = {
|
|
1814
|
+
eventId,
|
|
1815
|
+
telemetry,
|
|
1816
|
+
sendTraces,
|
|
1817
|
+
traceShipper: deps.traceShipper,
|
|
1818
|
+
rootParentForChildren
|
|
1819
|
+
};
|
|
1521
1820
|
const toolCalls = [];
|
|
1522
1821
|
const callParamsWithWrappedTools = callParams ? wrapTools(callParams, wrapCtx, toolCalls) : callParams;
|
|
1523
1822
|
const finalize = async (result, error) => {
|
|
1524
1823
|
var _a3, _b3, _c2;
|
|
1525
1824
|
const usage = extractUsage(result);
|
|
1526
1825
|
const model = extractModel(result);
|
|
1826
|
+
const inputAttachments = autoAttachmentEnabled ? extractInputAttachmentsFromArgs(mergedArgs) : void 0;
|
|
1827
|
+
const outputAttachments = autoAttachmentEnabled ? await extractOutputAttachmentsFromResult(result) : void 0;
|
|
1527
1828
|
const baseMessages = coerceMessagesFromArgs(mergedArgs);
|
|
1528
1829
|
const responseMessages = extractResponseMessages(result);
|
|
1529
1830
|
const allMessages = [...baseMessages, ...responseMessages];
|
|
@@ -1534,7 +1835,7 @@ function wrapAgentStream(stream, instance, agentSettings, className, aiSDK, deps
|
|
|
1534
1835
|
output: outputText,
|
|
1535
1836
|
model,
|
|
1536
1837
|
properties: ctx.properties,
|
|
1537
|
-
attachments: ctx.attachments
|
|
1838
|
+
attachments: mergeAttachments(ctx.attachments, inputAttachments, outputAttachments)
|
|
1538
1839
|
};
|
|
1539
1840
|
const built = await maybeBuildEvent(deps.options.buildEvent, allMessages);
|
|
1540
1841
|
const patch = mergeBuildEventPatch(defaultPatch, built);
|
|
@@ -1564,7 +1865,12 @@ function wrapAgentStream(stream, instance, agentSettings, className, aiSDK, deps
|
|
|
1564
1865
|
attrInt("ai.usage.reasoningTokens", reasoningTokens),
|
|
1565
1866
|
attrInt("ai.usage.cachedInputTokens", cachedInputTokens),
|
|
1566
1867
|
attrInt("ai.toolCall.count", toolCalls.length),
|
|
1567
|
-
...error ? [
|
|
1868
|
+
...error ? [
|
|
1869
|
+
attrString(
|
|
1870
|
+
"error.message",
|
|
1871
|
+
error instanceof Error ? error.message : String(error)
|
|
1872
|
+
)
|
|
1873
|
+
] : []
|
|
1568
1874
|
]
|
|
1569
1875
|
});
|
|
1570
1876
|
}
|
|
@@ -1588,7 +1894,10 @@ function wrapAgentStream(stream, instance, agentSettings, className, aiSDK, deps
|
|
|
1588
1894
|
attachments: patch.attachments,
|
|
1589
1895
|
isPending: false
|
|
1590
1896
|
}).catch((err) => {
|
|
1591
|
-
if (debug)
|
|
1897
|
+
if (debug)
|
|
1898
|
+
console.warn(
|
|
1899
|
+
`[raindrop-ai/ai-sdk] event patch failed: ${err instanceof Error ? err.message : err}`
|
|
1900
|
+
);
|
|
1592
1901
|
});
|
|
1593
1902
|
} else if (debug) {
|
|
1594
1903
|
console.log(`[raindrop-ai/ai-sdk] Agent ${operation} sendEvents=false, skipping event`);
|
|
@@ -1608,16 +1917,22 @@ function wrapAgentStream(stream, instance, agentSettings, className, aiSDK, deps
|
|
|
1608
1917
|
eventName: ctx.eventName
|
|
1609
1918
|
});
|
|
1610
1919
|
}
|
|
1611
|
-
const callParamsWithOnFinish = wrapOnFinish(
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1920
|
+
const callParamsWithOnFinish = wrapOnFinish(
|
|
1921
|
+
callParamsWithWrappedTools != null ? callParamsWithWrappedTools : {},
|
|
1922
|
+
async (result) => {
|
|
1923
|
+
if (debug) {
|
|
1924
|
+
console.log(`[raindrop-ai/ai-sdk] Agent ${operation} onFinish callback, finalizing...`);
|
|
1925
|
+
}
|
|
1926
|
+
try {
|
|
1927
|
+
await finalize(result);
|
|
1928
|
+
} catch (err) {
|
|
1929
|
+
if (debug)
|
|
1930
|
+
console.warn(
|
|
1931
|
+
`[raindrop-ai/ai-sdk] finalize failed: ${err instanceof Error ? err.message : err}`
|
|
1932
|
+
);
|
|
1933
|
+
}
|
|
1619
1934
|
}
|
|
1620
|
-
|
|
1935
|
+
);
|
|
1621
1936
|
try {
|
|
1622
1937
|
const result = await runWithContext(async () => {
|
|
1623
1938
|
return await stream.call(instance, callParamsWithOnFinish);
|
|
@@ -1627,7 +1942,10 @@ function wrapAgentStream(stream, instance, agentSettings, className, aiSDK, deps
|
|
|
1627
1942
|
try {
|
|
1628
1943
|
await finalize(void 0, error);
|
|
1629
1944
|
} catch (err) {
|
|
1630
|
-
if (debug)
|
|
1945
|
+
if (debug)
|
|
1946
|
+
console.warn(
|
|
1947
|
+
`[raindrop-ai/ai-sdk] finalize failed: ${err instanceof Error ? err.message : err}`
|
|
1948
|
+
);
|
|
1631
1949
|
}
|
|
1632
1950
|
throw error;
|
|
1633
1951
|
}
|
|
@@ -1670,7 +1988,9 @@ function wrapToolExecute(name, tool, ctx, toolCalls) {
|
|
|
1670
1988
|
if (!span) return;
|
|
1671
1989
|
if (error) {
|
|
1672
1990
|
ctx.traceShipper.endSpan(span, {
|
|
1673
|
-
attributes: [
|
|
1991
|
+
attributes: [
|
|
1992
|
+
attrString("error.message", error instanceof Error ? error.message : String(error))
|
|
1993
|
+
]
|
|
1674
1994
|
});
|
|
1675
1995
|
} else {
|
|
1676
1996
|
ctx.traceShipper.endSpan(span, {
|
|
@@ -1727,7 +2047,11 @@ function wrapToolExecute(name, tool, ctx, toolCalls) {
|
|
|
1727
2047
|
};
|
|
1728
2048
|
if (!toolSpan) return await run();
|
|
1729
2049
|
return await runWithParentSpanContext(
|
|
1730
|
-
{
|
|
2050
|
+
{
|
|
2051
|
+
traceIdB64: toolSpan.ids.traceIdB64,
|
|
2052
|
+
spanIdB64: toolSpan.ids.spanIdB64,
|
|
2053
|
+
eventId: ctx.eventId
|
|
2054
|
+
},
|
|
1731
2055
|
run
|
|
1732
2056
|
);
|
|
1733
2057
|
})();
|
|
@@ -1766,7 +2090,15 @@ function wrapModel(args, aiSDK, outerOperationId, ctx) {
|
|
|
1766
2090
|
if (span) endDoGenerateSpan(span, result, modelInfo, ctx);
|
|
1767
2091
|
return result;
|
|
1768
2092
|
} catch (error) {
|
|
1769
|
-
if (span)
|
|
2093
|
+
if (span)
|
|
2094
|
+
ctx.traceShipper.endSpan(span, {
|
|
2095
|
+
attributes: [
|
|
2096
|
+
attrString(
|
|
2097
|
+
"error.message",
|
|
2098
|
+
error instanceof Error ? error.message : String(error)
|
|
2099
|
+
)
|
|
2100
|
+
]
|
|
2101
|
+
});
|
|
1770
2102
|
throw error;
|
|
1771
2103
|
}
|
|
1772
2104
|
};
|
|
@@ -1794,7 +2126,12 @@ function wrapModel(args, aiSDK, outerOperationId, ctx) {
|
|
|
1794
2126
|
} catch (error) {
|
|
1795
2127
|
if (span)
|
|
1796
2128
|
ctx.traceShipper.endSpan(span, {
|
|
1797
|
-
attributes: [
|
|
2129
|
+
attributes: [
|
|
2130
|
+
attrString(
|
|
2131
|
+
"error.message",
|
|
2132
|
+
error instanceof Error ? error.message : String(error)
|
|
2133
|
+
)
|
|
2134
|
+
]
|
|
1798
2135
|
});
|
|
1799
2136
|
throw error;
|
|
1800
2137
|
}
|
|
@@ -1820,11 +2157,17 @@ function wrapModel(args, aiSDK, outerOperationId, ctx) {
|
|
|
1820
2157
|
...((_a = ctx.telemetry) == null ? void 0 : _a.recordOutputs) === false ? [] : [
|
|
1821
2158
|
attrString("ai.response.finishReason", finishReason),
|
|
1822
2159
|
attrString("ai.response.text", activeText.length ? activeText : void 0),
|
|
1823
|
-
attrString(
|
|
2160
|
+
attrString(
|
|
2161
|
+
"ai.response.toolCalls",
|
|
2162
|
+
safeJsonWithUint8(toolCallsLocal.length ? toolCallsLocal : void 0)
|
|
2163
|
+
),
|
|
1824
2164
|
attrString("ai.response.id", responseId),
|
|
1825
2165
|
attrString("ai.response.model", responseModelId),
|
|
1826
2166
|
attrString("ai.response.timestamp", responseTimestampIso),
|
|
1827
|
-
attrString(
|
|
2167
|
+
attrString(
|
|
2168
|
+
"ai.response.providerMetadata",
|
|
2169
|
+
safeJsonWithUint8(providerMetadata)
|
|
2170
|
+
)
|
|
1828
2171
|
],
|
|
1829
2172
|
attrInt("ai.usage.inputTokens", inputTokens),
|
|
1830
2173
|
attrInt("ai.usage.outputTokens", outputTokens),
|
|
@@ -1836,7 +2179,12 @@ function wrapModel(args, aiSDK, outerOperationId, ctx) {
|
|
|
1836
2179
|
...msToFirstChunk !== void 0 ? [attrInt("ai.stream.msToFirstChunk", msToFirstChunk)] : [],
|
|
1837
2180
|
...msToFinish !== void 0 ? [attrInt("ai.stream.msToFinish", msToFinish)] : [],
|
|
1838
2181
|
...avgOutTokensPerSecond !== void 0 ? [attrDouble("ai.stream.avgOutputTokensPerSecond", avgOutTokensPerSecond)] : [],
|
|
1839
|
-
...error ? [
|
|
2182
|
+
...error ? [
|
|
2183
|
+
attrString(
|
|
2184
|
+
"error.message",
|
|
2185
|
+
error instanceof Error ? error.message : String(error)
|
|
2186
|
+
)
|
|
2187
|
+
] : []
|
|
1840
2188
|
]
|
|
1841
2189
|
});
|
|
1842
2190
|
};
|
|
@@ -1853,15 +2201,20 @@ function wrapModel(args, aiSDK, outerOperationId, ctx) {
|
|
|
1853
2201
|
if (firstChunkMs === void 0) firstChunkMs = Date.now();
|
|
1854
2202
|
if (isRecord(value)) {
|
|
1855
2203
|
const type = value["type"];
|
|
1856
|
-
if (type === "text-delta" && typeof value["textDelta"] === "string")
|
|
1857
|
-
|
|
2204
|
+
if (type === "text-delta" && typeof value["textDelta"] === "string")
|
|
2205
|
+
activeText += value["textDelta"];
|
|
2206
|
+
if (type === "finish" && typeof value["finishReason"] === "string")
|
|
2207
|
+
finishReason = value["finishReason"];
|
|
1858
2208
|
if (type === "tool-call") toolCallsLocal.push(value);
|
|
1859
2209
|
if ("response" in value && isRecord(value["response"])) {
|
|
1860
2210
|
const response = value["response"];
|
|
1861
2211
|
if (typeof response["id"] === "string") responseId = response["id"];
|
|
1862
|
-
if (typeof response["modelId"] === "string")
|
|
1863
|
-
|
|
1864
|
-
|
|
2212
|
+
if (typeof response["modelId"] === "string")
|
|
2213
|
+
responseModelId = response["modelId"];
|
|
2214
|
+
if (response["timestamp"] instanceof Date)
|
|
2215
|
+
responseTimestampIso = response["timestamp"].toISOString();
|
|
2216
|
+
else if (typeof response["timestamp"] === "string")
|
|
2217
|
+
responseTimestampIso = response["timestamp"];
|
|
1865
2218
|
}
|
|
1866
2219
|
if ("usage" in value) usage = value["usage"];
|
|
1867
2220
|
if ("providerMetadata" in value) providerMetadata = value["providerMetadata"];
|
|
@@ -1903,7 +2256,11 @@ function startDoGenerateSpan(operationId, options, modelInfo, parent, ctx) {
|
|
|
1903
2256
|
attrString("ai.telemetry.functionId", (_b = ctx.telemetry) == null ? void 0 : _b.functionId),
|
|
1904
2257
|
attrString("ai.model.provider", modelInfo.provider),
|
|
1905
2258
|
attrString("ai.model.id", modelInfo.modelId),
|
|
1906
|
-
...((_c = ctx.telemetry) == null ? void 0 : _c.recordInputs) === false ? [] : [
|
|
2259
|
+
...((_c = ctx.telemetry) == null ? void 0 : _c.recordInputs) === false ? [] : [
|
|
2260
|
+
attrString("ai.prompt.messages", promptJson),
|
|
2261
|
+
attrStringArray("ai.prompt.tools", toolsJson),
|
|
2262
|
+
attrString("ai.prompt.toolChoice", toolChoiceJson)
|
|
2263
|
+
],
|
|
1907
2264
|
attrString("gen_ai.system", modelInfo.provider),
|
|
1908
2265
|
attrString("gen_ai.request.model", modelInfo.modelId),
|
|
1909
2266
|
...attrsFromGenAiRequest(options)
|
|
@@ -1944,7 +2301,10 @@ function endDoGenerateSpan(span, result, modelInfo, ctx) {
|
|
|
1944
2301
|
...((_a = ctx.telemetry) == null ? void 0 : _a.recordOutputs) === false ? [] : [
|
|
1945
2302
|
attrString("ai.response.finishReason", finishReason),
|
|
1946
2303
|
attrString("ai.response.text", extractTextFromLmContent(content)),
|
|
1947
|
-
attrString(
|
|
2304
|
+
attrString(
|
|
2305
|
+
"ai.response.toolCalls",
|
|
2306
|
+
safeJsonWithUint8(extractToolCallsFromLmContent(content))
|
|
2307
|
+
),
|
|
1948
2308
|
attrString("ai.response.id", responseId),
|
|
1949
2309
|
attrString("ai.response.model", responseModelId),
|
|
1950
2310
|
attrString("ai.response.timestamp", responseTimestampIso),
|
|
@@ -1978,7 +2338,11 @@ function startDoStreamSpan(operationId, options, modelInfo, parent, ctx) {
|
|
|
1978
2338
|
attrString("ai.telemetry.functionId", (_b = ctx.telemetry) == null ? void 0 : _b.functionId),
|
|
1979
2339
|
attrString("ai.model.provider", modelInfo.provider),
|
|
1980
2340
|
attrString("ai.model.id", modelInfo.modelId),
|
|
1981
|
-
...((_c = ctx.telemetry) == null ? void 0 : _c.recordInputs) === false ? [] : [
|
|
2341
|
+
...((_c = ctx.telemetry) == null ? void 0 : _c.recordInputs) === false ? [] : [
|
|
2342
|
+
attrString("ai.prompt.messages", promptJson),
|
|
2343
|
+
attrStringArray("ai.prompt.tools", toolsJson),
|
|
2344
|
+
attrString("ai.prompt.toolChoice", toolChoiceJson)
|
|
2345
|
+
],
|
|
1982
2346
|
attrString("gen_ai.system", modelInfo.provider),
|
|
1983
2347
|
attrString("gen_ai.request.model", modelInfo.modelId),
|
|
1984
2348
|
...attrsFromGenAiRequest(options)
|
|
@@ -2030,6 +2394,13 @@ function mergeBuildEventPatch(defaults, override) {
|
|
|
2030
2394
|
attachments: override.attachments !== void 0 ? [...(_g = defaults.attachments) != null ? _g : [], ...(_h = override.attachments) != null ? _h : []] : defaults.attachments
|
|
2031
2395
|
};
|
|
2032
2396
|
}
|
|
2397
|
+
function mergeAttachments(...groups) {
|
|
2398
|
+
const merged = [];
|
|
2399
|
+
for (const group of groups) {
|
|
2400
|
+
if (group == null ? void 0 : group.length) merged.push(...group);
|
|
2401
|
+
}
|
|
2402
|
+
return merged.length ? merged : void 0;
|
|
2403
|
+
}
|
|
2033
2404
|
function extractNestedTokens(usage, key) {
|
|
2034
2405
|
if (!isRecord(usage)) return void 0;
|
|
2035
2406
|
const val = usage[key];
|
|
@@ -2049,23 +2420,30 @@ function eventMetadata(options) {
|
|
|
2049
2420
|
if (options.properties) result["raindrop.properties"] = JSON.stringify(options.properties);
|
|
2050
2421
|
return result;
|
|
2051
2422
|
}
|
|
2423
|
+
function envDebugEnabled() {
|
|
2424
|
+
var _a;
|
|
2425
|
+
if (typeof process === "undefined") return false;
|
|
2426
|
+
const flag = (_a = process.env) == null ? void 0 : _a.RAINDROP_AI_DEBUG;
|
|
2427
|
+
return flag === "1" || flag === "true";
|
|
2428
|
+
}
|
|
2052
2429
|
function createRaindropAISDK(opts) {
|
|
2053
2430
|
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
2054
2431
|
const eventsEnabled = ((_a = opts.events) == null ? void 0 : _a.enabled) !== false;
|
|
2055
2432
|
const tracesEnabled = ((_b = opts.traces) == null ? void 0 : _b.enabled) !== false;
|
|
2433
|
+
const envDebug = envDebugEnabled();
|
|
2056
2434
|
const eventShipper = new EventShipper({
|
|
2057
2435
|
writeKey: opts.writeKey,
|
|
2058
2436
|
endpoint: opts.endpoint,
|
|
2059
2437
|
enabled: eventsEnabled,
|
|
2060
|
-
debug: ((_c = opts.events) == null ? void 0 : _c.debug) === true,
|
|
2438
|
+
debug: ((_c = opts.events) == null ? void 0 : _c.debug) === true || envDebug,
|
|
2061
2439
|
partialFlushMs: (_d = opts.events) == null ? void 0 : _d.partialFlushMs
|
|
2062
2440
|
});
|
|
2063
2441
|
const traceShipper = new TraceShipper({
|
|
2064
2442
|
writeKey: opts.writeKey,
|
|
2065
2443
|
endpoint: opts.endpoint,
|
|
2066
2444
|
enabled: tracesEnabled,
|
|
2067
|
-
debug: ((_e = opts.traces) == null ? void 0 : _e.debug) === true,
|
|
2068
|
-
debugSpans: ((_f = opts.traces) == null ? void 0 : _f.debugSpans) === true,
|
|
2445
|
+
debug: ((_e = opts.traces) == null ? void 0 : _e.debug) === true || envDebug,
|
|
2446
|
+
debugSpans: ((_f = opts.traces) == null ? void 0 : _f.debugSpans) === true || envDebug,
|
|
2069
2447
|
flushIntervalMs: (_g = opts.traces) == null ? void 0 : _g.flushIntervalMs,
|
|
2070
2448
|
maxBatchSize: (_h = opts.traces) == null ? void 0 : _h.maxBatchSize,
|
|
2071
2449
|
maxQueueSize: (_i = opts.traces) == null ? void 0 : _i.maxQueueSize
|