@raindrop-ai/ai-sdk 0.0.2 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{chunk-FKMLMFKH.mjs → chunk-RE7BC2GC.mjs} +473 -75
- package/dist/index.d.mts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +473 -75
- package/dist/index.mjs +1 -1
- package/dist/index.node.js +473 -75
- package/dist/index.node.mjs +1 -1
- package/dist/index.workers.js +473 -75
- 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.4"};
|
|
94
94
|
|
|
95
95
|
// src/internal/version.ts
|
|
96
96
|
var libraryName = package_default.name;
|
|
@@ -137,19 +137,30 @@ var EventShipper = class {
|
|
|
137
137
|
return this.debug;
|
|
138
138
|
}
|
|
139
139
|
async patch(eventId, patch) {
|
|
140
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
140
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
|
|
141
141
|
if (!this.enabled) return;
|
|
142
142
|
if (!eventId) return;
|
|
143
|
-
|
|
144
|
-
|
|
143
|
+
if (this.debug) {
|
|
144
|
+
console.log("[raindrop-ai/ai-sdk] queue patch", {
|
|
145
|
+
eventId,
|
|
146
|
+
userId: patch.userId,
|
|
147
|
+
eventName: patch.eventName,
|
|
148
|
+
hasInput: typeof patch.input === "string" && patch.input.length > 0,
|
|
149
|
+
hasOutput: typeof patch.output === "string" && patch.output.length > 0,
|
|
150
|
+
attachments: (_b = (_a = patch.attachments) == null ? void 0 : _a.length) != null ? _b : 0,
|
|
151
|
+
isPending: patch.isPending
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
const sticky = (_c = this.sticky.get(eventId)) != null ? _c : {};
|
|
155
|
+
const existing = (_d = this.buffers.get(eventId)) != null ? _d : {};
|
|
145
156
|
const merged = mergePatches(existing, patch);
|
|
146
|
-
merged.isPending = (
|
|
157
|
+
merged.isPending = (_g = (_f = (_e = patch.isPending) != null ? _e : existing.isPending) != null ? _f : sticky.isPending) != null ? _g : true;
|
|
147
158
|
this.buffers.set(eventId, merged);
|
|
148
159
|
this.sticky.set(eventId, {
|
|
149
|
-
userId: (
|
|
150
|
-
convoId: (
|
|
151
|
-
eventName: (
|
|
152
|
-
isPending: (
|
|
160
|
+
userId: (_h = merged.userId) != null ? _h : sticky.userId,
|
|
161
|
+
convoId: (_i = merged.convoId) != null ? _i : sticky.convoId,
|
|
162
|
+
eventName: (_j = merged.eventName) != null ? _j : sticky.eventName,
|
|
163
|
+
isPending: (_k = merged.isPending) != null ? _k : sticky.isPending
|
|
153
164
|
});
|
|
154
165
|
const t = this.timers.get(eventId);
|
|
155
166
|
if (t) clearTimeout(t);
|
|
@@ -246,7 +257,7 @@ var EventShipper = class {
|
|
|
246
257
|
}
|
|
247
258
|
}
|
|
248
259
|
async flushOne(eventId) {
|
|
249
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
260
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
|
|
250
261
|
if (!this.enabled) return;
|
|
251
262
|
const timer = this.timers.get(eventId);
|
|
252
263
|
if (timer) {
|
|
@@ -284,6 +295,24 @@ var EventShipper = class {
|
|
|
284
295
|
is_pending: ((_i = (_h = accumulated.isPending) != null ? _h : sticky.isPending) != null ? _i : true) !== false
|
|
285
296
|
};
|
|
286
297
|
const url = `${this.baseUrl}events/track_partial`;
|
|
298
|
+
if (this.debug) {
|
|
299
|
+
console.log("[raindrop-ai/ai-sdk] sending track_partial", {
|
|
300
|
+
eventId,
|
|
301
|
+
eventName,
|
|
302
|
+
userId,
|
|
303
|
+
isPending: payload.is_pending,
|
|
304
|
+
inputPreview: typeof accumulated.input === "string" ? accumulated.input.slice(0, 120) : void 0,
|
|
305
|
+
outputPreview: typeof accumulated.output === "string" ? accumulated.output.slice(0, 120) : void 0,
|
|
306
|
+
attachments: (_k = (_j = accumulated.attachments) == null ? void 0 : _j.length) != null ? _k : 0,
|
|
307
|
+
attachmentKinds: (_m = (_l = accumulated.attachments) == null ? void 0 : _l.map((a) => ({
|
|
308
|
+
type: a.type,
|
|
309
|
+
role: a.role,
|
|
310
|
+
name: a.name,
|
|
311
|
+
valuePreview: a.value.slice(0, 60)
|
|
312
|
+
}))) != null ? _m : [],
|
|
313
|
+
endpoint: url
|
|
314
|
+
});
|
|
315
|
+
}
|
|
287
316
|
const p = postJson(
|
|
288
317
|
url,
|
|
289
318
|
payload,
|
|
@@ -526,6 +555,12 @@ var TraceShipper = class {
|
|
|
526
555
|
const batch = this.queue.splice(0, this.maxBatchSize);
|
|
527
556
|
const body = buildExportTraceServiceRequest(batch);
|
|
528
557
|
const url = `${this.baseUrl}traces`;
|
|
558
|
+
if (this.debug) {
|
|
559
|
+
console.log("[raindrop-ai/ai-sdk] sending traces batch", {
|
|
560
|
+
spans: batch.length,
|
|
561
|
+
endpoint: url
|
|
562
|
+
});
|
|
563
|
+
}
|
|
529
564
|
const p = postJson(
|
|
530
565
|
url,
|
|
531
566
|
body,
|
|
@@ -853,7 +888,13 @@ function isAgentClass(value) {
|
|
|
853
888
|
function extractModel(result) {
|
|
854
889
|
if (!isRecord(result)) return void 0;
|
|
855
890
|
const model = result["model"];
|
|
856
|
-
|
|
891
|
+
if (typeof model === "string" && model.length) return model;
|
|
892
|
+
const response = result["response"];
|
|
893
|
+
if (isRecord(response)) {
|
|
894
|
+
const modelId = response["modelId"];
|
|
895
|
+
if (typeof modelId === "string" && modelId.length) return modelId;
|
|
896
|
+
}
|
|
897
|
+
return void 0;
|
|
857
898
|
}
|
|
858
899
|
function extractFinishReason(result) {
|
|
859
900
|
if (!isRecord(result)) return void 0;
|
|
@@ -863,6 +904,174 @@ function extractFinishReason(result) {
|
|
|
863
904
|
}
|
|
864
905
|
return void 0;
|
|
865
906
|
}
|
|
907
|
+
function bytesToBase64(bytes) {
|
|
908
|
+
if (typeof Buffer !== "undefined") return Buffer.from(bytes).toString("base64");
|
|
909
|
+
let binary = "";
|
|
910
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
911
|
+
binary += String.fromCharCode(bytes[i]);
|
|
912
|
+
}
|
|
913
|
+
if (typeof btoa === "function") return btoa(binary);
|
|
914
|
+
return "";
|
|
915
|
+
}
|
|
916
|
+
function asDataUrl(value, mediaType) {
|
|
917
|
+
if (value instanceof URL) return value.toString();
|
|
918
|
+
if (typeof value === "string") {
|
|
919
|
+
if (value.startsWith("data:")) return value;
|
|
920
|
+
if (value.startsWith("http://") || value.startsWith("https://")) return value;
|
|
921
|
+
return `data:${mediaType};base64,${value}`;
|
|
922
|
+
}
|
|
923
|
+
if (value instanceof Uint8Array) {
|
|
924
|
+
const base64 = bytesToBase64(value);
|
|
925
|
+
if (!base64) return void 0;
|
|
926
|
+
return `data:${mediaType};base64,${base64}`;
|
|
927
|
+
}
|
|
928
|
+
if (value instanceof ArrayBuffer) {
|
|
929
|
+
const base64 = bytesToBase64(new Uint8Array(value));
|
|
930
|
+
if (!base64) return void 0;
|
|
931
|
+
return `data:${mediaType};base64,${base64}`;
|
|
932
|
+
}
|
|
933
|
+
return void 0;
|
|
934
|
+
}
|
|
935
|
+
function dataPartToAttachmentValue(value) {
|
|
936
|
+
if (typeof value === "string") return value;
|
|
937
|
+
if (value instanceof URL) return value.toString();
|
|
938
|
+
if (value instanceof Uint8Array) return `[binary:${value.byteLength} bytes]`;
|
|
939
|
+
if (value instanceof ArrayBuffer) return `[binary:${value.byteLength} bytes]`;
|
|
940
|
+
return void 0;
|
|
941
|
+
}
|
|
942
|
+
function attachmentMediaType(part) {
|
|
943
|
+
if (typeof part["mediaType"] === "string") return part["mediaType"];
|
|
944
|
+
if (typeof part["mimeType"] === "string") return part["mimeType"];
|
|
945
|
+
const file = part["file"];
|
|
946
|
+
if (isRecord(file)) {
|
|
947
|
+
if (typeof file["mediaType"] === "string") return file["mediaType"];
|
|
948
|
+
if (typeof file["mimeType"] === "string") return file["mimeType"];
|
|
949
|
+
}
|
|
950
|
+
return void 0;
|
|
951
|
+
}
|
|
952
|
+
function attachmentName(part) {
|
|
953
|
+
if (typeof part["filename"] === "string") return part["filename"];
|
|
954
|
+
if (typeof part["name"] === "string") return part["name"];
|
|
955
|
+
const file = part["file"];
|
|
956
|
+
if (isRecord(file)) {
|
|
957
|
+
if (typeof file["filename"] === "string") return file["filename"];
|
|
958
|
+
if (typeof file["name"] === "string") return file["name"];
|
|
959
|
+
}
|
|
960
|
+
return void 0;
|
|
961
|
+
}
|
|
962
|
+
function attachmentData(part) {
|
|
963
|
+
if ("data" in part) return part["data"];
|
|
964
|
+
const file = part["file"];
|
|
965
|
+
if (isRecord(file)) {
|
|
966
|
+
if ("file_data" in file) return file["file_data"];
|
|
967
|
+
if ("data" in file) return file["data"];
|
|
968
|
+
}
|
|
969
|
+
return void 0;
|
|
970
|
+
}
|
|
971
|
+
function contentPartToAttachment(part, role) {
|
|
972
|
+
var _a, _b, _c;
|
|
973
|
+
const partType = part["type"];
|
|
974
|
+
if (typeof partType !== "string") return void 0;
|
|
975
|
+
if (partType === "image") {
|
|
976
|
+
const mediaType = (_a = attachmentMediaType(part)) != null ? _a : "image/png";
|
|
977
|
+
const value = asDataUrl(part["image"], mediaType);
|
|
978
|
+
if (!value) return void 0;
|
|
979
|
+
return { type: "image", role, value };
|
|
980
|
+
}
|
|
981
|
+
if (partType === "image_url") {
|
|
982
|
+
const imageUrlPart = part["image_url"];
|
|
983
|
+
const imageUrlValue = isRecord(imageUrlPart) ? imageUrlPart["url"] : imageUrlPart;
|
|
984
|
+
const value = asDataUrl(imageUrlValue, "image/png");
|
|
985
|
+
if (!value) return void 0;
|
|
986
|
+
return { type: "image", role, value };
|
|
987
|
+
}
|
|
988
|
+
if (partType === "file") {
|
|
989
|
+
const mediaType = attachmentMediaType(part);
|
|
990
|
+
const data = attachmentData(part);
|
|
991
|
+
const isImage = (mediaType == null ? void 0 : mediaType.startsWith("image/")) === true;
|
|
992
|
+
const value = isImage && mediaType ? asDataUrl(data, mediaType) : dataPartToAttachmentValue(data);
|
|
993
|
+
if (!value) return void 0;
|
|
994
|
+
const name = (_c = (_b = attachmentName(part)) != null ? _b : mediaType) != null ? _c : "file";
|
|
995
|
+
return { type: isImage ? "image" : "text", role, name, value };
|
|
996
|
+
}
|
|
997
|
+
return void 0;
|
|
998
|
+
}
|
|
999
|
+
function attachmentsFromContent(content, role) {
|
|
1000
|
+
if (!Array.isArray(content)) return void 0;
|
|
1001
|
+
const attachments = [];
|
|
1002
|
+
for (const part of content) {
|
|
1003
|
+
if (!isRecord(part)) continue;
|
|
1004
|
+
const attachment = contentPartToAttachment(part, role);
|
|
1005
|
+
if (attachment) attachments.push(attachment);
|
|
1006
|
+
}
|
|
1007
|
+
return attachments.length ? attachments : void 0;
|
|
1008
|
+
}
|
|
1009
|
+
function generatedFileToAttachment(file) {
|
|
1010
|
+
var _a, _b, _c, _d;
|
|
1011
|
+
const mediaType = typeof file["mediaType"] === "string" ? file["mediaType"] : typeof file["mimeType"] === "string" ? file["mimeType"] : void 0;
|
|
1012
|
+
const data = (_d = (_c = (_b = (_a = file["base64Data"]) != null ? _a : file["base64"]) != null ? _b : file["uint8ArrayData"]) != null ? _c : file["uint8Array"]) != null ? _d : file["data"];
|
|
1013
|
+
const isImage = (mediaType == null ? void 0 : mediaType.startsWith("image/")) === true;
|
|
1014
|
+
const value = isImage && mediaType ? asDataUrl(data, mediaType) : dataPartToAttachmentValue(data);
|
|
1015
|
+
if (!value) return void 0;
|
|
1016
|
+
const name = typeof file["filename"] === "string" ? file["filename"] : typeof file["name"] === "string" ? file["name"] : mediaType != null ? mediaType : "file";
|
|
1017
|
+
return {
|
|
1018
|
+
type: isImage ? "image" : "text",
|
|
1019
|
+
role: "output",
|
|
1020
|
+
name,
|
|
1021
|
+
value
|
|
1022
|
+
};
|
|
1023
|
+
}
|
|
1024
|
+
async function outputAttachmentsFromFiles(files) {
|
|
1025
|
+
let resolvedFiles = files;
|
|
1026
|
+
if (resolvedFiles && (typeof resolvedFiles === "object" || typeof resolvedFiles === "function") && typeof resolvedFiles.then === "function") {
|
|
1027
|
+
try {
|
|
1028
|
+
resolvedFiles = await resolvedFiles;
|
|
1029
|
+
} catch (e) {
|
|
1030
|
+
return void 0;
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
if (!Array.isArray(resolvedFiles)) return void 0;
|
|
1034
|
+
const attachments = [];
|
|
1035
|
+
for (const file of resolvedFiles) {
|
|
1036
|
+
if (!isRecord(file)) continue;
|
|
1037
|
+
const attachment = generatedFileToAttachment(file);
|
|
1038
|
+
if (attachment) attachments.push(attachment);
|
|
1039
|
+
}
|
|
1040
|
+
return attachments.length ? attachments : void 0;
|
|
1041
|
+
}
|
|
1042
|
+
function extractTextFromMessageContent(content) {
|
|
1043
|
+
if (typeof content === "string") return content;
|
|
1044
|
+
if (!Array.isArray(content)) return void 0;
|
|
1045
|
+
let result = "";
|
|
1046
|
+
for (const part of content) {
|
|
1047
|
+
if (!isRecord(part) || part["type"] !== "text" || typeof part["text"] !== "string") continue;
|
|
1048
|
+
result += part["text"];
|
|
1049
|
+
}
|
|
1050
|
+
return result.length ? result : void 0;
|
|
1051
|
+
}
|
|
1052
|
+
function extractInputAttachmentsFromArgs(args) {
|
|
1053
|
+
if (!isRecord(args)) return void 0;
|
|
1054
|
+
const messages = args["messages"];
|
|
1055
|
+
if (!Array.isArray(messages)) return void 0;
|
|
1056
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
1057
|
+
const message = messages[i];
|
|
1058
|
+
if (!isRecord(message) || message["role"] !== "user") continue;
|
|
1059
|
+
return attachmentsFromContent(message["content"], "input");
|
|
1060
|
+
}
|
|
1061
|
+
return void 0;
|
|
1062
|
+
}
|
|
1063
|
+
async function extractOutputAttachmentsFromResult(result) {
|
|
1064
|
+
if (!isRecord(result)) return void 0;
|
|
1065
|
+
const fileAttachments = await outputAttachmentsFromFiles(result["files"]);
|
|
1066
|
+
if (fileAttachments == null ? void 0 : fileAttachments.length) return fileAttachments;
|
|
1067
|
+
const responseMessages = extractResponseMessages(result);
|
|
1068
|
+
for (let i = responseMessages.length - 1; i >= 0; i--) {
|
|
1069
|
+
const message = responseMessages[i];
|
|
1070
|
+
if (!isRecord(message) || message["role"] !== "assistant") continue;
|
|
1071
|
+
return attachmentsFromContent(message["content"], "output");
|
|
1072
|
+
}
|
|
1073
|
+
return attachmentsFromContent(result["content"], "output");
|
|
1074
|
+
}
|
|
866
1075
|
function lastUserMessageTextFromArgs(args) {
|
|
867
1076
|
var _a;
|
|
868
1077
|
if (!isRecord(args)) return void 0;
|
|
@@ -872,7 +1081,8 @@ function lastUserMessageTextFromArgs(args) {
|
|
|
872
1081
|
const message = messages[i];
|
|
873
1082
|
if (!isRecord(message) || message["role"] !== "user") continue;
|
|
874
1083
|
const content = message["content"];
|
|
875
|
-
|
|
1084
|
+
const text = extractTextFromMessageContent(content);
|
|
1085
|
+
if (text !== void 0) return text;
|
|
876
1086
|
return (_a = safeJsonWithUint8(content)) != null ? _a : String(content);
|
|
877
1087
|
}
|
|
878
1088
|
return void 0;
|
|
@@ -887,7 +1097,8 @@ function extractInputFromArgs(args) {
|
|
|
887
1097
|
const last = messages[messages.length - 1];
|
|
888
1098
|
if (isRecord(last)) {
|
|
889
1099
|
const content = last["content"];
|
|
890
|
-
|
|
1100
|
+
const text = extractTextFromMessageContent(content);
|
|
1101
|
+
if (text !== void 0) return text;
|
|
891
1102
|
const asJson = safeJson(content);
|
|
892
1103
|
if (asJson) return asJson;
|
|
893
1104
|
}
|
|
@@ -921,7 +1132,9 @@ function extractResponseMessages(result) {
|
|
|
921
1132
|
if (!isRecord(result)) return [];
|
|
922
1133
|
const response = result["response"];
|
|
923
1134
|
if (isRecord(response) && Array.isArray(response["messages"])) {
|
|
924
|
-
return response["messages"].filter(
|
|
1135
|
+
return response["messages"].filter(
|
|
1136
|
+
(message) => isRecord(message) && typeof message["role"] === "string" && "content" in message
|
|
1137
|
+
).map((message) => message);
|
|
925
1138
|
}
|
|
926
1139
|
const steps = result["steps"];
|
|
927
1140
|
if (Array.isArray(steps) && steps.length > 0) {
|
|
@@ -929,7 +1142,9 @@ function extractResponseMessages(result) {
|
|
|
929
1142
|
if (isRecord(lastStep) && isRecord(lastStep["response"])) {
|
|
930
1143
|
const responseMessages = lastStep["response"]["messages"];
|
|
931
1144
|
if (Array.isArray(responseMessages)) {
|
|
932
|
-
return responseMessages.filter(
|
|
1145
|
+
return responseMessages.filter(
|
|
1146
|
+
(message) => isRecord(message) && typeof message["role"] === "string" && "content" in message
|
|
1147
|
+
).map((message) => message);
|
|
933
1148
|
}
|
|
934
1149
|
}
|
|
935
1150
|
}
|
|
@@ -979,9 +1194,11 @@ function opName(operationId, functionId) {
|
|
|
979
1194
|
function toOtlpAttr(key, value) {
|
|
980
1195
|
if (value === void 0 || value === null) return void 0;
|
|
981
1196
|
if (typeof value === "string") return attrString(key, value);
|
|
982
|
-
if (typeof value === "number")
|
|
1197
|
+
if (typeof value === "number")
|
|
1198
|
+
return Number.isInteger(value) ? attrInt(key, value) : attrDouble(key, value);
|
|
983
1199
|
if (typeof value === "boolean") return attrBool(key, value);
|
|
984
|
-
if (Array.isArray(value) && value.every((v) => typeof v === "string"))
|
|
1200
|
+
if (Array.isArray(value) && value.every((v) => typeof v === "string"))
|
|
1201
|
+
return attrStringArray(key, value);
|
|
985
1202
|
const asJson = safeJsonWithUint8(value);
|
|
986
1203
|
return asJson ? attrString(key, asJson) : void 0;
|
|
987
1204
|
}
|
|
@@ -1027,13 +1244,31 @@ function attrsFromSettings(args) {
|
|
|
1027
1244
|
function attrsFromGenAiRequest(options) {
|
|
1028
1245
|
if (!isRecord(options)) return [];
|
|
1029
1246
|
return [
|
|
1030
|
-
attrDouble(
|
|
1031
|
-
|
|
1032
|
-
|
|
1247
|
+
attrDouble(
|
|
1248
|
+
"gen_ai.request.frequency_penalty",
|
|
1249
|
+
typeof options["frequencyPenalty"] === "number" ? options["frequencyPenalty"] : void 0
|
|
1250
|
+
),
|
|
1251
|
+
attrInt(
|
|
1252
|
+
"gen_ai.request.max_tokens",
|
|
1253
|
+
typeof options["maxOutputTokens"] === "number" ? options["maxOutputTokens"] : void 0
|
|
1254
|
+
),
|
|
1255
|
+
attrDouble(
|
|
1256
|
+
"gen_ai.request.presence_penalty",
|
|
1257
|
+
typeof options["presencePenalty"] === "number" ? options["presencePenalty"] : void 0
|
|
1258
|
+
),
|
|
1033
1259
|
...Array.isArray(options["stopSequences"]) && options["stopSequences"].every((x) => typeof x === "string") ? [attrStringArray("gen_ai.request.stop_sequences", options["stopSequences"])] : [],
|
|
1034
|
-
attrDouble(
|
|
1035
|
-
|
|
1036
|
-
|
|
1260
|
+
attrDouble(
|
|
1261
|
+
"gen_ai.request.temperature",
|
|
1262
|
+
typeof options["temperature"] === "number" ? options["temperature"] : void 0
|
|
1263
|
+
),
|
|
1264
|
+
attrInt(
|
|
1265
|
+
"gen_ai.request.top_k",
|
|
1266
|
+
typeof options["topK"] === "number" ? options["topK"] : void 0
|
|
1267
|
+
),
|
|
1268
|
+
attrDouble(
|
|
1269
|
+
"gen_ai.request.top_p",
|
|
1270
|
+
typeof options["topP"] === "number" ? options["topP"] : void 0
|
|
1271
|
+
)
|
|
1037
1272
|
];
|
|
1038
1273
|
}
|
|
1039
1274
|
|
|
@@ -1081,6 +1316,7 @@ function wrapAISDK(aiSDK, deps) {
|
|
|
1081
1316
|
const agentClasses = /* @__PURE__ */ new Set(["ToolLoopAgent"]);
|
|
1082
1317
|
const sendEvents = ((_a = deps.options.send) == null ? void 0 : _a.events) !== false;
|
|
1083
1318
|
const sendTraces = ((_b = deps.options.send) == null ? void 0 : _b.traces) !== false;
|
|
1319
|
+
const autoAttachmentEnabled = deps.options.autoAttachment !== false;
|
|
1084
1320
|
const proxyTarget = isModuleNamespace(aiSDK) ? Object.setPrototypeOf({}, aiSDK) : aiSDK;
|
|
1085
1321
|
return new Proxy(proxyTarget, {
|
|
1086
1322
|
get(target, prop, receiver) {
|
|
@@ -1134,14 +1370,27 @@ function wrapAISDK(aiSDK, deps) {
|
|
|
1134
1370
|
]
|
|
1135
1371
|
}) : void 0;
|
|
1136
1372
|
const rootParentForChildren = rootSpan ? { traceIdB64: rootSpan.ids.traceIdB64, spanIdB64: rootSpan.ids.spanIdB64 } : inheritedParent;
|
|
1137
|
-
const wrapCtx = {
|
|
1373
|
+
const wrapCtx = {
|
|
1374
|
+
eventId,
|
|
1375
|
+
telemetry,
|
|
1376
|
+
sendTraces,
|
|
1377
|
+
traceShipper: deps.traceShipper,
|
|
1378
|
+
rootParentForChildren
|
|
1379
|
+
};
|
|
1138
1380
|
const toolCalls = [];
|
|
1139
1381
|
const argWithWrappedTools = wrapTools(arg, wrapCtx, toolCalls);
|
|
1140
|
-
const argWithWrappedModel = wrapModel(
|
|
1382
|
+
const argWithWrappedModel = wrapModel(
|
|
1383
|
+
argWithWrappedTools,
|
|
1384
|
+
aiSDK,
|
|
1385
|
+
outerOperationId,
|
|
1386
|
+
wrapCtx
|
|
1387
|
+
);
|
|
1141
1388
|
const finalize = async (result, error) => {
|
|
1142
1389
|
var _a3, _b3, _c2;
|
|
1143
1390
|
const usage = extractUsage(result);
|
|
1144
1391
|
const model = extractModel(result);
|
|
1392
|
+
const inputAttachments = autoAttachmentEnabled ? extractInputAttachmentsFromArgs(arg) : void 0;
|
|
1393
|
+
const outputAttachments = autoAttachmentEnabled ? await extractOutputAttachmentsFromResult(result) : void 0;
|
|
1145
1394
|
const baseMessages = coerceMessagesFromArgs(arg);
|
|
1146
1395
|
const responseMessages = extractResponseMessages(result);
|
|
1147
1396
|
const allMessages = [...baseMessages, ...responseMessages];
|
|
@@ -1154,7 +1403,7 @@ function wrapAISDK(aiSDK, deps) {
|
|
|
1154
1403
|
output: defaultOutput,
|
|
1155
1404
|
model,
|
|
1156
1405
|
properties: ctx.properties,
|
|
1157
|
-
attachments: ctx.attachments
|
|
1406
|
+
attachments: mergeAttachments(ctx.attachments, inputAttachments, outputAttachments)
|
|
1158
1407
|
};
|
|
1159
1408
|
const built = await maybeBuildEvent(deps.options.buildEvent, allMessages);
|
|
1160
1409
|
const patch = mergeBuildEventPatch(defaultPatch, built);
|
|
@@ -1174,7 +1423,10 @@ function wrapAISDK(aiSDK, deps) {
|
|
|
1174
1423
|
attrString("ai.response.finishReason", finishReason),
|
|
1175
1424
|
operation === "generateObject" || operation === "streamObject" ? attrString("ai.response.object", output) : attrString("ai.response.text", output),
|
|
1176
1425
|
attrString("ai.response.toolCalls", resultToolCalls),
|
|
1177
|
-
attrString(
|
|
1426
|
+
attrString(
|
|
1427
|
+
"ai.response.providerMetadata",
|
|
1428
|
+
safeJsonWithUint8(providerMetadata)
|
|
1429
|
+
)
|
|
1178
1430
|
],
|
|
1179
1431
|
attrInt("ai.usage.promptTokens", usage == null ? void 0 : usage.inputTokens),
|
|
1180
1432
|
attrInt("ai.usage.completionTokens", usage == null ? void 0 : usage.outputTokens),
|
|
@@ -1184,7 +1436,12 @@ function wrapAISDK(aiSDK, deps) {
|
|
|
1184
1436
|
attrInt("ai.usage.reasoningTokens", reasoningTokens),
|
|
1185
1437
|
attrInt("ai.usage.cachedInputTokens", cachedInputTokens),
|
|
1186
1438
|
attrInt("ai.toolCall.count", toolCalls.length),
|
|
1187
|
-
...error ? [
|
|
1439
|
+
...error ? [
|
|
1440
|
+
attrString(
|
|
1441
|
+
"error.message",
|
|
1442
|
+
error instanceof Error ? error.message : String(error)
|
|
1443
|
+
)
|
|
1444
|
+
] : []
|
|
1188
1445
|
]
|
|
1189
1446
|
});
|
|
1190
1447
|
}
|
|
@@ -1200,12 +1457,18 @@ function wrapAISDK(aiSDK, deps) {
|
|
|
1200
1457
|
attachments: patch.attachments,
|
|
1201
1458
|
isPending: false
|
|
1202
1459
|
}).catch((err) => {
|
|
1203
|
-
if (debug)
|
|
1460
|
+
if (debug)
|
|
1461
|
+
console.warn(
|
|
1462
|
+
`[raindrop-ai/ai-sdk] event patch failed: ${err instanceof Error ? err.message : err}`
|
|
1463
|
+
);
|
|
1204
1464
|
});
|
|
1205
1465
|
}
|
|
1206
1466
|
};
|
|
1207
1467
|
const callOriginal = async (...args) => {
|
|
1208
|
-
return await original.call(
|
|
1468
|
+
return await original.call(
|
|
1469
|
+
aiSDK,
|
|
1470
|
+
...args
|
|
1471
|
+
);
|
|
1209
1472
|
};
|
|
1210
1473
|
const runWithContext = async (fn) => {
|
|
1211
1474
|
if (!rootSpan) return await fn();
|
|
@@ -1219,7 +1482,10 @@ function wrapAISDK(aiSDK, deps) {
|
|
|
1219
1482
|
try {
|
|
1220
1483
|
await finalize(result);
|
|
1221
1484
|
} catch (err) {
|
|
1222
|
-
if (debug)
|
|
1485
|
+
if (debug)
|
|
1486
|
+
console.warn(
|
|
1487
|
+
`[raindrop-ai/ai-sdk] finalize failed: ${err instanceof Error ? err.message : err}`
|
|
1488
|
+
);
|
|
1223
1489
|
}
|
|
1224
1490
|
});
|
|
1225
1491
|
try {
|
|
@@ -1246,7 +1512,10 @@ function wrapAISDK(aiSDK, deps) {
|
|
|
1246
1512
|
try {
|
|
1247
1513
|
await finalize(void 0, error);
|
|
1248
1514
|
} catch (err) {
|
|
1249
|
-
if (debug)
|
|
1515
|
+
if (debug)
|
|
1516
|
+
console.warn(
|
|
1517
|
+
`[raindrop-ai/ai-sdk] finalize failed: ${err instanceof Error ? err.message : err}`
|
|
1518
|
+
);
|
|
1250
1519
|
}
|
|
1251
1520
|
throw error;
|
|
1252
1521
|
}
|
|
@@ -1260,14 +1529,20 @@ function wrapAISDK(aiSDK, deps) {
|
|
|
1260
1529
|
try {
|
|
1261
1530
|
await finalize(result);
|
|
1262
1531
|
} catch (err) {
|
|
1263
|
-
if (debug)
|
|
1532
|
+
if (debug)
|
|
1533
|
+
console.warn(
|
|
1534
|
+
`[raindrop-ai/ai-sdk] finalize failed: ${err instanceof Error ? err.message : err}`
|
|
1535
|
+
);
|
|
1264
1536
|
}
|
|
1265
1537
|
return result;
|
|
1266
1538
|
} catch (error) {
|
|
1267
1539
|
try {
|
|
1268
1540
|
await finalize(void 0, error);
|
|
1269
1541
|
} catch (err) {
|
|
1270
|
-
if (debug)
|
|
1542
|
+
if (debug)
|
|
1543
|
+
console.warn(
|
|
1544
|
+
`[raindrop-ai/ai-sdk] finalize failed: ${err instanceof Error ? err.message : err}`
|
|
1545
|
+
);
|
|
1271
1546
|
}
|
|
1272
1547
|
throw error;
|
|
1273
1548
|
}
|
|
@@ -1319,6 +1594,7 @@ function wrapAgentGenerate(generate, instance, agentSettings, className, aiSDK,
|
|
|
1319
1594
|
var _a, _b;
|
|
1320
1595
|
const sendEvents = ((_a = deps.options.send) == null ? void 0 : _a.events) !== false;
|
|
1321
1596
|
const sendTraces = ((_b = deps.options.send) == null ? void 0 : _b.traces) !== false;
|
|
1597
|
+
const autoAttachmentEnabled = deps.options.autoAttachment !== false;
|
|
1322
1598
|
return async (...callArgs) => {
|
|
1323
1599
|
var _a2, _b2, _c, _d;
|
|
1324
1600
|
const callParams = callArgs[0];
|
|
@@ -1362,13 +1638,28 @@ function wrapAgentGenerate(generate, instance, agentSettings, className, aiSDK,
|
|
|
1362
1638
|
]
|
|
1363
1639
|
}) : void 0;
|
|
1364
1640
|
const rootParentForChildren = rootSpan ? { traceIdB64: rootSpan.ids.traceIdB64, spanIdB64: rootSpan.ids.spanIdB64 } : inheritedParent;
|
|
1365
|
-
const wrapCtx = {
|
|
1641
|
+
const wrapCtx = {
|
|
1642
|
+
eventId,
|
|
1643
|
+
telemetry,
|
|
1644
|
+
sendTraces,
|
|
1645
|
+
traceShipper: deps.traceShipper,
|
|
1646
|
+
rootParentForChildren
|
|
1647
|
+
};
|
|
1366
1648
|
const toolCalls = [];
|
|
1367
|
-
const
|
|
1649
|
+
const mergedArgsWithWrappedTools = wrapTools(mergedArgs, wrapCtx, toolCalls);
|
|
1650
|
+
const mergedArgsWithWrappedModel = wrapModel(
|
|
1651
|
+
mergedArgsWithWrappedTools,
|
|
1652
|
+
aiSDK,
|
|
1653
|
+
outerOperationId,
|
|
1654
|
+
wrapCtx
|
|
1655
|
+
);
|
|
1656
|
+
const callParamsWithWrappedToolsAndModel = mergedArgsWithWrappedModel != null ? mergedArgsWithWrappedModel : {};
|
|
1368
1657
|
const finalize = async (result, error) => {
|
|
1369
1658
|
var _a3, _b3, _c2;
|
|
1370
1659
|
const usage = extractUsage(result);
|
|
1371
1660
|
const model = extractModel(result);
|
|
1661
|
+
const inputAttachments = autoAttachmentEnabled ? extractInputAttachmentsFromArgs(mergedArgs) : void 0;
|
|
1662
|
+
const outputAttachments = autoAttachmentEnabled ? await extractOutputAttachmentsFromResult(result) : void 0;
|
|
1372
1663
|
const baseMessages = coerceMessagesFromArgs(mergedArgs);
|
|
1373
1664
|
const responseMessages = extractResponseMessages(result);
|
|
1374
1665
|
const allMessages = [...baseMessages, ...responseMessages];
|
|
@@ -1379,7 +1670,7 @@ function wrapAgentGenerate(generate, instance, agentSettings, className, aiSDK,
|
|
|
1379
1670
|
output: outputText,
|
|
1380
1671
|
model,
|
|
1381
1672
|
properties: ctx.properties,
|
|
1382
|
-
attachments: ctx.attachments
|
|
1673
|
+
attachments: mergeAttachments(ctx.attachments, inputAttachments, outputAttachments)
|
|
1383
1674
|
};
|
|
1384
1675
|
const built = await maybeBuildEvent(deps.options.buildEvent, allMessages);
|
|
1385
1676
|
const patch = mergeBuildEventPatch(defaultPatch, built);
|
|
@@ -1409,7 +1700,12 @@ function wrapAgentGenerate(generate, instance, agentSettings, className, aiSDK,
|
|
|
1409
1700
|
attrInt("ai.usage.reasoningTokens", reasoningTokens),
|
|
1410
1701
|
attrInt("ai.usage.cachedInputTokens", cachedInputTokens),
|
|
1411
1702
|
attrInt("ai.toolCall.count", toolCalls.length),
|
|
1412
|
-
...error ? [
|
|
1703
|
+
...error ? [
|
|
1704
|
+
attrString(
|
|
1705
|
+
"error.message",
|
|
1706
|
+
error instanceof Error ? error.message : String(error)
|
|
1707
|
+
)
|
|
1708
|
+
] : []
|
|
1413
1709
|
]
|
|
1414
1710
|
});
|
|
1415
1711
|
}
|
|
@@ -1433,7 +1729,10 @@ function wrapAgentGenerate(generate, instance, agentSettings, className, aiSDK,
|
|
|
1433
1729
|
attachments: patch.attachments,
|
|
1434
1730
|
isPending: false
|
|
1435
1731
|
}).catch((err) => {
|
|
1436
|
-
if (debug)
|
|
1732
|
+
if (debug)
|
|
1733
|
+
console.warn(
|
|
1734
|
+
`[raindrop-ai/ai-sdk] event patch failed: ${err instanceof Error ? err.message : err}`
|
|
1735
|
+
);
|
|
1437
1736
|
});
|
|
1438
1737
|
} else if (debug) {
|
|
1439
1738
|
console.log(`[raindrop-ai/ai-sdk] Agent ${operation} sendEvents=false, skipping event`);
|
|
@@ -1455,7 +1754,7 @@ function wrapAgentGenerate(generate, instance, agentSettings, className, aiSDK,
|
|
|
1455
1754
|
}
|
|
1456
1755
|
try {
|
|
1457
1756
|
const result = await runWithContext(async () => {
|
|
1458
|
-
return await generate.call(instance,
|
|
1757
|
+
return await generate.call(instance, callParamsWithWrappedToolsAndModel);
|
|
1459
1758
|
});
|
|
1460
1759
|
if (debug) {
|
|
1461
1760
|
console.log(`[raindrop-ai/ai-sdk] Agent ${operation} completed, finalizing...`);
|
|
@@ -1463,14 +1762,20 @@ function wrapAgentGenerate(generate, instance, agentSettings, className, aiSDK,
|
|
|
1463
1762
|
try {
|
|
1464
1763
|
await finalize(result);
|
|
1465
1764
|
} catch (err) {
|
|
1466
|
-
if (debug)
|
|
1765
|
+
if (debug)
|
|
1766
|
+
console.warn(
|
|
1767
|
+
`[raindrop-ai/ai-sdk] finalize failed: ${err instanceof Error ? err.message : err}`
|
|
1768
|
+
);
|
|
1467
1769
|
}
|
|
1468
1770
|
return result;
|
|
1469
1771
|
} catch (error) {
|
|
1470
1772
|
try {
|
|
1471
1773
|
await finalize(void 0, error);
|
|
1472
1774
|
} catch (err) {
|
|
1473
|
-
if (debug)
|
|
1775
|
+
if (debug)
|
|
1776
|
+
console.warn(
|
|
1777
|
+
`[raindrop-ai/ai-sdk] finalize failed: ${err instanceof Error ? err.message : err}`
|
|
1778
|
+
);
|
|
1474
1779
|
}
|
|
1475
1780
|
throw error;
|
|
1476
1781
|
}
|
|
@@ -1480,6 +1785,7 @@ function wrapAgentStream(stream, instance, agentSettings, className, aiSDK, deps
|
|
|
1480
1785
|
var _a, _b;
|
|
1481
1786
|
const sendEvents = ((_a = deps.options.send) == null ? void 0 : _a.events) !== false;
|
|
1482
1787
|
const sendTraces = ((_b = deps.options.send) == null ? void 0 : _b.traces) !== false;
|
|
1788
|
+
const autoAttachmentEnabled = deps.options.autoAttachment !== false;
|
|
1483
1789
|
return async (...callArgs) => {
|
|
1484
1790
|
var _a2, _b2, _c, _d;
|
|
1485
1791
|
const callParams = callArgs[0];
|
|
@@ -1523,13 +1829,28 @@ function wrapAgentStream(stream, instance, agentSettings, className, aiSDK, deps
|
|
|
1523
1829
|
]
|
|
1524
1830
|
}) : void 0;
|
|
1525
1831
|
const rootParentForChildren = rootSpan ? { traceIdB64: rootSpan.ids.traceIdB64, spanIdB64: rootSpan.ids.spanIdB64 } : inheritedParent;
|
|
1526
|
-
const wrapCtx = {
|
|
1832
|
+
const wrapCtx = {
|
|
1833
|
+
eventId,
|
|
1834
|
+
telemetry,
|
|
1835
|
+
sendTraces,
|
|
1836
|
+
traceShipper: deps.traceShipper,
|
|
1837
|
+
rootParentForChildren
|
|
1838
|
+
};
|
|
1527
1839
|
const toolCalls = [];
|
|
1528
|
-
const
|
|
1840
|
+
const mergedArgsWithWrappedTools = wrapTools(mergedArgs, wrapCtx, toolCalls);
|
|
1841
|
+
const mergedArgsWithWrappedModel = wrapModel(
|
|
1842
|
+
mergedArgsWithWrappedTools,
|
|
1843
|
+
aiSDK,
|
|
1844
|
+
outerOperationId,
|
|
1845
|
+
wrapCtx
|
|
1846
|
+
);
|
|
1847
|
+
const callParamsWithWrappedToolsAndModel = mergedArgsWithWrappedModel != null ? mergedArgsWithWrappedModel : {};
|
|
1529
1848
|
const finalize = async (result, error) => {
|
|
1530
1849
|
var _a3, _b3, _c2;
|
|
1531
1850
|
const usage = extractUsage(result);
|
|
1532
1851
|
const model = extractModel(result);
|
|
1852
|
+
const inputAttachments = autoAttachmentEnabled ? extractInputAttachmentsFromArgs(mergedArgs) : void 0;
|
|
1853
|
+
const outputAttachments = autoAttachmentEnabled ? await extractOutputAttachmentsFromResult(result) : void 0;
|
|
1533
1854
|
const baseMessages = coerceMessagesFromArgs(mergedArgs);
|
|
1534
1855
|
const responseMessages = extractResponseMessages(result);
|
|
1535
1856
|
const allMessages = [...baseMessages, ...responseMessages];
|
|
@@ -1540,7 +1861,7 @@ function wrapAgentStream(stream, instance, agentSettings, className, aiSDK, deps
|
|
|
1540
1861
|
output: outputText,
|
|
1541
1862
|
model,
|
|
1542
1863
|
properties: ctx.properties,
|
|
1543
|
-
attachments: ctx.attachments
|
|
1864
|
+
attachments: mergeAttachments(ctx.attachments, inputAttachments, outputAttachments)
|
|
1544
1865
|
};
|
|
1545
1866
|
const built = await maybeBuildEvent(deps.options.buildEvent, allMessages);
|
|
1546
1867
|
const patch = mergeBuildEventPatch(defaultPatch, built);
|
|
@@ -1570,7 +1891,12 @@ function wrapAgentStream(stream, instance, agentSettings, className, aiSDK, deps
|
|
|
1570
1891
|
attrInt("ai.usage.reasoningTokens", reasoningTokens),
|
|
1571
1892
|
attrInt("ai.usage.cachedInputTokens", cachedInputTokens),
|
|
1572
1893
|
attrInt("ai.toolCall.count", toolCalls.length),
|
|
1573
|
-
...error ? [
|
|
1894
|
+
...error ? [
|
|
1895
|
+
attrString(
|
|
1896
|
+
"error.message",
|
|
1897
|
+
error instanceof Error ? error.message : String(error)
|
|
1898
|
+
)
|
|
1899
|
+
] : []
|
|
1574
1900
|
]
|
|
1575
1901
|
});
|
|
1576
1902
|
}
|
|
@@ -1594,7 +1920,10 @@ function wrapAgentStream(stream, instance, agentSettings, className, aiSDK, deps
|
|
|
1594
1920
|
attachments: patch.attachments,
|
|
1595
1921
|
isPending: false
|
|
1596
1922
|
}).catch((err) => {
|
|
1597
|
-
if (debug)
|
|
1923
|
+
if (debug)
|
|
1924
|
+
console.warn(
|
|
1925
|
+
`[raindrop-ai/ai-sdk] event patch failed: ${err instanceof Error ? err.message : err}`
|
|
1926
|
+
);
|
|
1598
1927
|
});
|
|
1599
1928
|
} else if (debug) {
|
|
1600
1929
|
console.log(`[raindrop-ai/ai-sdk] Agent ${operation} sendEvents=false, skipping event`);
|
|
@@ -1614,16 +1943,22 @@ function wrapAgentStream(stream, instance, agentSettings, className, aiSDK, deps
|
|
|
1614
1943
|
eventName: ctx.eventName
|
|
1615
1944
|
});
|
|
1616
1945
|
}
|
|
1617
|
-
const callParamsWithOnFinish = wrapOnFinish(
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1946
|
+
const callParamsWithOnFinish = wrapOnFinish(
|
|
1947
|
+
callParamsWithWrappedToolsAndModel != null ? callParamsWithWrappedToolsAndModel : {},
|
|
1948
|
+
async (result) => {
|
|
1949
|
+
if (debug) {
|
|
1950
|
+
console.log(`[raindrop-ai/ai-sdk] Agent ${operation} onFinish callback, finalizing...`);
|
|
1951
|
+
}
|
|
1952
|
+
try {
|
|
1953
|
+
await finalize(result);
|
|
1954
|
+
} catch (err) {
|
|
1955
|
+
if (debug)
|
|
1956
|
+
console.warn(
|
|
1957
|
+
`[raindrop-ai/ai-sdk] finalize failed: ${err instanceof Error ? err.message : err}`
|
|
1958
|
+
);
|
|
1959
|
+
}
|
|
1625
1960
|
}
|
|
1626
|
-
|
|
1961
|
+
);
|
|
1627
1962
|
try {
|
|
1628
1963
|
const result = await runWithContext(async () => {
|
|
1629
1964
|
return await stream.call(instance, callParamsWithOnFinish);
|
|
@@ -1633,7 +1968,10 @@ function wrapAgentStream(stream, instance, agentSettings, className, aiSDK, deps
|
|
|
1633
1968
|
try {
|
|
1634
1969
|
await finalize(void 0, error);
|
|
1635
1970
|
} catch (err) {
|
|
1636
|
-
if (debug)
|
|
1971
|
+
if (debug)
|
|
1972
|
+
console.warn(
|
|
1973
|
+
`[raindrop-ai/ai-sdk] finalize failed: ${err instanceof Error ? err.message : err}`
|
|
1974
|
+
);
|
|
1637
1975
|
}
|
|
1638
1976
|
throw error;
|
|
1639
1977
|
}
|
|
@@ -1676,7 +2014,9 @@ function wrapToolExecute(name, tool, ctx, toolCalls) {
|
|
|
1676
2014
|
if (!span) return;
|
|
1677
2015
|
if (error) {
|
|
1678
2016
|
ctx.traceShipper.endSpan(span, {
|
|
1679
|
-
attributes: [
|
|
2017
|
+
attributes: [
|
|
2018
|
+
attrString("error.message", error instanceof Error ? error.message : String(error))
|
|
2019
|
+
]
|
|
1680
2020
|
});
|
|
1681
2021
|
} else {
|
|
1682
2022
|
ctx.traceShipper.endSpan(span, {
|
|
@@ -1733,7 +2073,11 @@ function wrapToolExecute(name, tool, ctx, toolCalls) {
|
|
|
1733
2073
|
};
|
|
1734
2074
|
if (!toolSpan) return await run();
|
|
1735
2075
|
return await runWithParentSpanContext(
|
|
1736
|
-
{
|
|
2076
|
+
{
|
|
2077
|
+
traceIdB64: toolSpan.ids.traceIdB64,
|
|
2078
|
+
spanIdB64: toolSpan.ids.spanIdB64,
|
|
2079
|
+
eventId: ctx.eventId
|
|
2080
|
+
},
|
|
1737
2081
|
run
|
|
1738
2082
|
);
|
|
1739
2083
|
})();
|
|
@@ -1772,7 +2116,15 @@ function wrapModel(args, aiSDK, outerOperationId, ctx) {
|
|
|
1772
2116
|
if (span) endDoGenerateSpan(span, result, modelInfo, ctx);
|
|
1773
2117
|
return result;
|
|
1774
2118
|
} catch (error) {
|
|
1775
|
-
if (span)
|
|
2119
|
+
if (span)
|
|
2120
|
+
ctx.traceShipper.endSpan(span, {
|
|
2121
|
+
attributes: [
|
|
2122
|
+
attrString(
|
|
2123
|
+
"error.message",
|
|
2124
|
+
error instanceof Error ? error.message : String(error)
|
|
2125
|
+
)
|
|
2126
|
+
]
|
|
2127
|
+
});
|
|
1776
2128
|
throw error;
|
|
1777
2129
|
}
|
|
1778
2130
|
};
|
|
@@ -1800,7 +2152,12 @@ function wrapModel(args, aiSDK, outerOperationId, ctx) {
|
|
|
1800
2152
|
} catch (error) {
|
|
1801
2153
|
if (span)
|
|
1802
2154
|
ctx.traceShipper.endSpan(span, {
|
|
1803
|
-
attributes: [
|
|
2155
|
+
attributes: [
|
|
2156
|
+
attrString(
|
|
2157
|
+
"error.message",
|
|
2158
|
+
error instanceof Error ? error.message : String(error)
|
|
2159
|
+
)
|
|
2160
|
+
]
|
|
1804
2161
|
});
|
|
1805
2162
|
throw error;
|
|
1806
2163
|
}
|
|
@@ -1826,11 +2183,17 @@ function wrapModel(args, aiSDK, outerOperationId, ctx) {
|
|
|
1826
2183
|
...((_a = ctx.telemetry) == null ? void 0 : _a.recordOutputs) === false ? [] : [
|
|
1827
2184
|
attrString("ai.response.finishReason", finishReason),
|
|
1828
2185
|
attrString("ai.response.text", activeText.length ? activeText : void 0),
|
|
1829
|
-
attrString(
|
|
2186
|
+
attrString(
|
|
2187
|
+
"ai.response.toolCalls",
|
|
2188
|
+
safeJsonWithUint8(toolCallsLocal.length ? toolCallsLocal : void 0)
|
|
2189
|
+
),
|
|
1830
2190
|
attrString("ai.response.id", responseId),
|
|
1831
2191
|
attrString("ai.response.model", responseModelId),
|
|
1832
2192
|
attrString("ai.response.timestamp", responseTimestampIso),
|
|
1833
|
-
attrString(
|
|
2193
|
+
attrString(
|
|
2194
|
+
"ai.response.providerMetadata",
|
|
2195
|
+
safeJsonWithUint8(providerMetadata)
|
|
2196
|
+
)
|
|
1834
2197
|
],
|
|
1835
2198
|
attrInt("ai.usage.inputTokens", inputTokens),
|
|
1836
2199
|
attrInt("ai.usage.outputTokens", outputTokens),
|
|
@@ -1842,7 +2205,12 @@ function wrapModel(args, aiSDK, outerOperationId, ctx) {
|
|
|
1842
2205
|
...msToFirstChunk !== void 0 ? [attrInt("ai.stream.msToFirstChunk", msToFirstChunk)] : [],
|
|
1843
2206
|
...msToFinish !== void 0 ? [attrInt("ai.stream.msToFinish", msToFinish)] : [],
|
|
1844
2207
|
...avgOutTokensPerSecond !== void 0 ? [attrDouble("ai.stream.avgOutputTokensPerSecond", avgOutTokensPerSecond)] : [],
|
|
1845
|
-
...error ? [
|
|
2208
|
+
...error ? [
|
|
2209
|
+
attrString(
|
|
2210
|
+
"error.message",
|
|
2211
|
+
error instanceof Error ? error.message : String(error)
|
|
2212
|
+
)
|
|
2213
|
+
] : []
|
|
1846
2214
|
]
|
|
1847
2215
|
});
|
|
1848
2216
|
};
|
|
@@ -1859,15 +2227,20 @@ function wrapModel(args, aiSDK, outerOperationId, ctx) {
|
|
|
1859
2227
|
if (firstChunkMs === void 0) firstChunkMs = Date.now();
|
|
1860
2228
|
if (isRecord(value)) {
|
|
1861
2229
|
const type = value["type"];
|
|
1862
|
-
if (type === "text-delta" && typeof value["textDelta"] === "string")
|
|
1863
|
-
|
|
2230
|
+
if (type === "text-delta" && typeof value["textDelta"] === "string")
|
|
2231
|
+
activeText += value["textDelta"];
|
|
2232
|
+
if (type === "finish" && typeof value["finishReason"] === "string")
|
|
2233
|
+
finishReason = value["finishReason"];
|
|
1864
2234
|
if (type === "tool-call") toolCallsLocal.push(value);
|
|
1865
2235
|
if ("response" in value && isRecord(value["response"])) {
|
|
1866
2236
|
const response = value["response"];
|
|
1867
2237
|
if (typeof response["id"] === "string") responseId = response["id"];
|
|
1868
|
-
if (typeof response["modelId"] === "string")
|
|
1869
|
-
|
|
1870
|
-
|
|
2238
|
+
if (typeof response["modelId"] === "string")
|
|
2239
|
+
responseModelId = response["modelId"];
|
|
2240
|
+
if (response["timestamp"] instanceof Date)
|
|
2241
|
+
responseTimestampIso = response["timestamp"].toISOString();
|
|
2242
|
+
else if (typeof response["timestamp"] === "string")
|
|
2243
|
+
responseTimestampIso = response["timestamp"];
|
|
1871
2244
|
}
|
|
1872
2245
|
if ("usage" in value) usage = value["usage"];
|
|
1873
2246
|
if ("providerMetadata" in value) providerMetadata = value["providerMetadata"];
|
|
@@ -1909,7 +2282,11 @@ function startDoGenerateSpan(operationId, options, modelInfo, parent, ctx) {
|
|
|
1909
2282
|
attrString("ai.telemetry.functionId", (_b = ctx.telemetry) == null ? void 0 : _b.functionId),
|
|
1910
2283
|
attrString("ai.model.provider", modelInfo.provider),
|
|
1911
2284
|
attrString("ai.model.id", modelInfo.modelId),
|
|
1912
|
-
...((_c = ctx.telemetry) == null ? void 0 : _c.recordInputs) === false ? [] : [
|
|
2285
|
+
...((_c = ctx.telemetry) == null ? void 0 : _c.recordInputs) === false ? [] : [
|
|
2286
|
+
attrString("ai.prompt.messages", promptJson),
|
|
2287
|
+
attrStringArray("ai.prompt.tools", toolsJson),
|
|
2288
|
+
attrString("ai.prompt.toolChoice", toolChoiceJson)
|
|
2289
|
+
],
|
|
1913
2290
|
attrString("gen_ai.system", modelInfo.provider),
|
|
1914
2291
|
attrString("gen_ai.request.model", modelInfo.modelId),
|
|
1915
2292
|
...attrsFromGenAiRequest(options)
|
|
@@ -1950,7 +2327,10 @@ function endDoGenerateSpan(span, result, modelInfo, ctx) {
|
|
|
1950
2327
|
...((_a = ctx.telemetry) == null ? void 0 : _a.recordOutputs) === false ? [] : [
|
|
1951
2328
|
attrString("ai.response.finishReason", finishReason),
|
|
1952
2329
|
attrString("ai.response.text", extractTextFromLmContent(content)),
|
|
1953
|
-
attrString(
|
|
2330
|
+
attrString(
|
|
2331
|
+
"ai.response.toolCalls",
|
|
2332
|
+
safeJsonWithUint8(extractToolCallsFromLmContent(content))
|
|
2333
|
+
),
|
|
1954
2334
|
attrString("ai.response.id", responseId),
|
|
1955
2335
|
attrString("ai.response.model", responseModelId),
|
|
1956
2336
|
attrString("ai.response.timestamp", responseTimestampIso),
|
|
@@ -1984,7 +2364,11 @@ function startDoStreamSpan(operationId, options, modelInfo, parent, ctx) {
|
|
|
1984
2364
|
attrString("ai.telemetry.functionId", (_b = ctx.telemetry) == null ? void 0 : _b.functionId),
|
|
1985
2365
|
attrString("ai.model.provider", modelInfo.provider),
|
|
1986
2366
|
attrString("ai.model.id", modelInfo.modelId),
|
|
1987
|
-
...((_c = ctx.telemetry) == null ? void 0 : _c.recordInputs) === false ? [] : [
|
|
2367
|
+
...((_c = ctx.telemetry) == null ? void 0 : _c.recordInputs) === false ? [] : [
|
|
2368
|
+
attrString("ai.prompt.messages", promptJson),
|
|
2369
|
+
attrStringArray("ai.prompt.tools", toolsJson),
|
|
2370
|
+
attrString("ai.prompt.toolChoice", toolChoiceJson)
|
|
2371
|
+
],
|
|
1988
2372
|
attrString("gen_ai.system", modelInfo.provider),
|
|
1989
2373
|
attrString("gen_ai.request.model", modelInfo.modelId),
|
|
1990
2374
|
...attrsFromGenAiRequest(options)
|
|
@@ -2036,6 +2420,13 @@ function mergeBuildEventPatch(defaults, override) {
|
|
|
2036
2420
|
attachments: override.attachments !== void 0 ? [...(_g = defaults.attachments) != null ? _g : [], ...(_h = override.attachments) != null ? _h : []] : defaults.attachments
|
|
2037
2421
|
};
|
|
2038
2422
|
}
|
|
2423
|
+
function mergeAttachments(...groups) {
|
|
2424
|
+
const merged = [];
|
|
2425
|
+
for (const group of groups) {
|
|
2426
|
+
if (group == null ? void 0 : group.length) merged.push(...group);
|
|
2427
|
+
}
|
|
2428
|
+
return merged.length ? merged : void 0;
|
|
2429
|
+
}
|
|
2039
2430
|
function extractNestedTokens(usage, key) {
|
|
2040
2431
|
if (!isRecord(usage)) return void 0;
|
|
2041
2432
|
const val = usage[key];
|
|
@@ -2055,23 +2446,30 @@ function eventMetadata(options) {
|
|
|
2055
2446
|
if (options.properties) result["raindrop.properties"] = JSON.stringify(options.properties);
|
|
2056
2447
|
return result;
|
|
2057
2448
|
}
|
|
2449
|
+
function envDebugEnabled() {
|
|
2450
|
+
var _a;
|
|
2451
|
+
if (typeof process === "undefined") return false;
|
|
2452
|
+
const flag = (_a = process.env) == null ? void 0 : _a.RAINDROP_AI_DEBUG;
|
|
2453
|
+
return flag === "1" || flag === "true";
|
|
2454
|
+
}
|
|
2058
2455
|
function createRaindropAISDK(opts) {
|
|
2059
2456
|
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
2060
2457
|
const eventsEnabled = ((_a = opts.events) == null ? void 0 : _a.enabled) !== false;
|
|
2061
2458
|
const tracesEnabled = ((_b = opts.traces) == null ? void 0 : _b.enabled) !== false;
|
|
2459
|
+
const envDebug = envDebugEnabled();
|
|
2062
2460
|
const eventShipper = new EventShipper({
|
|
2063
2461
|
writeKey: opts.writeKey,
|
|
2064
2462
|
endpoint: opts.endpoint,
|
|
2065
2463
|
enabled: eventsEnabled,
|
|
2066
|
-
debug: ((_c = opts.events) == null ? void 0 : _c.debug) === true,
|
|
2464
|
+
debug: ((_c = opts.events) == null ? void 0 : _c.debug) === true || envDebug,
|
|
2067
2465
|
partialFlushMs: (_d = opts.events) == null ? void 0 : _d.partialFlushMs
|
|
2068
2466
|
});
|
|
2069
2467
|
const traceShipper = new TraceShipper({
|
|
2070
2468
|
writeKey: opts.writeKey,
|
|
2071
2469
|
endpoint: opts.endpoint,
|
|
2072
2470
|
enabled: tracesEnabled,
|
|
2073
|
-
debug: ((_e = opts.traces) == null ? void 0 : _e.debug) === true,
|
|
2074
|
-
debugSpans: ((_f = opts.traces) == null ? void 0 : _f.debugSpans) === true,
|
|
2471
|
+
debug: ((_e = opts.traces) == null ? void 0 : _e.debug) === true || envDebug,
|
|
2472
|
+
debugSpans: ((_f = opts.traces) == null ? void 0 : _f.debugSpans) === true || envDebug,
|
|
2075
2473
|
flushIntervalMs: (_g = opts.traces) == null ? void 0 : _g.flushIntervalMs,
|
|
2076
2474
|
maxBatchSize: (_h = opts.traces) == null ? void 0 : _h.maxBatchSize,
|
|
2077
2475
|
maxQueueSize: (_i = opts.traces) == null ? void 0 : _i.maxQueueSize
|