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