@sentrial/sdk 0.3.3 → 0.4.0
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/index.cjs +88 -54
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +88 -54
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1073,8 +1073,13 @@ function wrapStreamText(originalFn, client, config) {
|
|
|
1073
1073
|
return null;
|
|
1074
1074
|
}
|
|
1075
1075
|
})();
|
|
1076
|
+
const wrappedParams = {
|
|
1077
|
+
...params,
|
|
1078
|
+
tools: params.tools ? wrapToolsAsync(params.tools, sessionPromise, client) : void 0
|
|
1079
|
+
};
|
|
1080
|
+
const result = originalFn(wrappedParams);
|
|
1076
1081
|
let tracked = false;
|
|
1077
|
-
async function trackCompletion(fullText,
|
|
1082
|
+
async function trackCompletion(fullText, error) {
|
|
1078
1083
|
if (tracked) return;
|
|
1079
1084
|
tracked = true;
|
|
1080
1085
|
const durationMs = Date.now() - startTime;
|
|
@@ -1094,10 +1099,26 @@ function wrapStreamText(originalFn, client, config) {
|
|
|
1094
1099
|
});
|
|
1095
1100
|
return;
|
|
1096
1101
|
}
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1102
|
+
let usage;
|
|
1103
|
+
try {
|
|
1104
|
+
usage = result.usage ? await result.usage : void 0;
|
|
1105
|
+
} catch {
|
|
1106
|
+
}
|
|
1107
|
+
const promptTokens = usage?.promptTokens || 0;
|
|
1108
|
+
const completionTokens = usage?.completionTokens || 0;
|
|
1109
|
+
const totalTokens = usage?.totalTokens || promptTokens + completionTokens;
|
|
1100
1110
|
const cost = calculateCostForCall(provider, modelId, promptTokens, completionTokens);
|
|
1111
|
+
await client.trackEvent({
|
|
1112
|
+
sessionId: sid,
|
|
1113
|
+
eventType: "llm_call",
|
|
1114
|
+
eventData: {
|
|
1115
|
+
model: modelId,
|
|
1116
|
+
provider,
|
|
1117
|
+
prompt_tokens: promptTokens,
|
|
1118
|
+
completion_tokens: completionTokens,
|
|
1119
|
+
total_tokens: totalTokens
|
|
1120
|
+
}
|
|
1121
|
+
});
|
|
1101
1122
|
await client.completeSession({
|
|
1102
1123
|
sessionId: sid,
|
|
1103
1124
|
success: true,
|
|
@@ -1109,28 +1130,39 @@ function wrapStreamText(originalFn, client, config) {
|
|
|
1109
1130
|
totalTokens
|
|
1110
1131
|
});
|
|
1111
1132
|
}
|
|
1112
|
-
const userOnFinish = params.onFinish;
|
|
1113
|
-
const wrappedParams = {
|
|
1114
|
-
...params,
|
|
1115
|
-
tools: params.tools ? wrapToolsAsync(params.tools, sessionPromise, client) : void 0,
|
|
1116
|
-
onFinish: async (event) => {
|
|
1117
|
-
try {
|
|
1118
|
-
if (userOnFinish) await userOnFinish(event);
|
|
1119
|
-
} catch {
|
|
1120
|
-
}
|
|
1121
|
-
await trackCompletion(event.text, event.usage);
|
|
1122
|
-
}
|
|
1123
|
-
};
|
|
1124
|
-
const result = originalFn(wrappedParams);
|
|
1125
1133
|
const textProp = result.text;
|
|
1126
|
-
if (
|
|
1127
|
-
textProp.
|
|
1134
|
+
if (typeof textProp === "string") {
|
|
1135
|
+
trackCompletion(textProp).catch(() => {
|
|
1136
|
+
});
|
|
1137
|
+
} else if (textProp != null && typeof textProp.then === "function") {
|
|
1138
|
+
const originalTextPromise = textProp;
|
|
1139
|
+
result.text = originalTextPromise.then((text) => {
|
|
1128
1140
|
trackCompletion(text).catch(() => {
|
|
1129
1141
|
});
|
|
1142
|
+
return text;
|
|
1130
1143
|
}).catch((err) => {
|
|
1131
|
-
trackCompletion("",
|
|
1144
|
+
trackCompletion("", err instanceof Error ? err : new Error(String(err))).catch(() => {
|
|
1132
1145
|
});
|
|
1146
|
+
throw err;
|
|
1133
1147
|
});
|
|
1148
|
+
} else {
|
|
1149
|
+
const originalTextStream = result.textStream;
|
|
1150
|
+
let fullText = "";
|
|
1151
|
+
result.textStream = (async function* () {
|
|
1152
|
+
try {
|
|
1153
|
+
for await (const chunk of originalTextStream) {
|
|
1154
|
+
fullText += chunk;
|
|
1155
|
+
yield chunk;
|
|
1156
|
+
}
|
|
1157
|
+
await trackCompletion(fullText);
|
|
1158
|
+
} catch (error) {
|
|
1159
|
+
await trackCompletion(
|
|
1160
|
+
"",
|
|
1161
|
+
error instanceof Error ? error : new Error(String(error))
|
|
1162
|
+
);
|
|
1163
|
+
throw error;
|
|
1164
|
+
}
|
|
1165
|
+
})();
|
|
1134
1166
|
}
|
|
1135
1167
|
return result;
|
|
1136
1168
|
};
|
|
@@ -1218,52 +1250,54 @@ function wrapStreamObject(originalFn, client, config) {
|
|
|
1218
1250
|
return null;
|
|
1219
1251
|
}
|
|
1220
1252
|
})();
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1253
|
+
const result = originalFn(params);
|
|
1254
|
+
if (result.object) {
|
|
1255
|
+
const originalObjectPromise = result.object;
|
|
1256
|
+
result.object = originalObjectPromise.then(async (obj) => {
|
|
1257
|
+
const durationMs = Date.now() - startTime;
|
|
1258
|
+
const sid = await sessionPromise;
|
|
1259
|
+
if (sid) {
|
|
1260
|
+
let usage;
|
|
1261
|
+
try {
|
|
1262
|
+
usage = result.usage ? await result.usage : void 0;
|
|
1263
|
+
} catch {
|
|
1264
|
+
}
|
|
1265
|
+
const promptTokens = usage?.promptTokens || 0;
|
|
1266
|
+
const completionTokens = usage?.completionTokens || 0;
|
|
1267
|
+
const totalTokens = usage?.totalTokens || promptTokens + completionTokens;
|
|
1268
|
+
const cost = calculateCostForCall(provider, modelId, promptTokens, completionTokens);
|
|
1269
|
+
await client.completeSession({
|
|
1270
|
+
sessionId: sid,
|
|
1271
|
+
success: true,
|
|
1272
|
+
output: JSON.stringify(obj),
|
|
1273
|
+
durationMs,
|
|
1274
|
+
estimatedCost: cost,
|
|
1275
|
+
promptTokens,
|
|
1276
|
+
completionTokens,
|
|
1277
|
+
totalTokens
|
|
1278
|
+
});
|
|
1229
1279
|
}
|
|
1230
|
-
|
|
1231
|
-
|
|
1280
|
+
return obj;
|
|
1281
|
+
}).catch(async (error) => {
|
|
1232
1282
|
const durationMs = Date.now() - startTime;
|
|
1233
1283
|
const sid = await sessionPromise;
|
|
1234
|
-
if (
|
|
1235
|
-
if (event.error) {
|
|
1236
|
-
const err = event.error instanceof Error ? event.error : new Error(String(event.error));
|
|
1284
|
+
if (sid) {
|
|
1237
1285
|
await client.trackError({
|
|
1238
1286
|
sessionId: sid,
|
|
1239
|
-
errorType:
|
|
1240
|
-
errorMessage:
|
|
1287
|
+
errorType: error instanceof Error ? error.name : "Error",
|
|
1288
|
+
errorMessage: error instanceof Error ? error.message : "Unknown error"
|
|
1241
1289
|
});
|
|
1242
1290
|
await client.completeSession({
|
|
1243
1291
|
sessionId: sid,
|
|
1244
1292
|
success: false,
|
|
1245
|
-
failureReason:
|
|
1293
|
+
failureReason: error instanceof Error ? error.message : "Unknown error",
|
|
1246
1294
|
durationMs
|
|
1247
1295
|
});
|
|
1248
|
-
return;
|
|
1249
1296
|
}
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
await client.completeSession({
|
|
1255
|
-
sessionId: sid,
|
|
1256
|
-
success: true,
|
|
1257
|
-
output: event.object != null ? JSON.stringify(event.object) : "",
|
|
1258
|
-
durationMs,
|
|
1259
|
-
estimatedCost: cost,
|
|
1260
|
-
promptTokens,
|
|
1261
|
-
completionTokens,
|
|
1262
|
-
totalTokens
|
|
1263
|
-
});
|
|
1264
|
-
}
|
|
1265
|
-
};
|
|
1266
|
-
return originalFn(wrappedParams);
|
|
1297
|
+
throw error;
|
|
1298
|
+
});
|
|
1299
|
+
}
|
|
1300
|
+
return result;
|
|
1267
1301
|
};
|
|
1268
1302
|
}
|
|
1269
1303
|
function wrapAISDK(ai, options) {
|