@sentrial/sdk 0.3.1 → 0.3.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/index.cjs +51 -82
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +51 -82
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1073,13 +1073,8 @@ 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);
|
|
1081
1076
|
let tracked = false;
|
|
1082
|
-
async function trackCompletion(fullText, error) {
|
|
1077
|
+
async function trackCompletion(fullText, usageInfo, error) {
|
|
1083
1078
|
if (tracked) return;
|
|
1084
1079
|
tracked = true;
|
|
1085
1080
|
const durationMs = Date.now() - startTime;
|
|
@@ -1099,26 +1094,10 @@ function wrapStreamText(originalFn, client, config) {
|
|
|
1099
1094
|
});
|
|
1100
1095
|
return;
|
|
1101
1096
|
}
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
} catch {
|
|
1106
|
-
}
|
|
1107
|
-
const promptTokens = usage?.promptTokens || 0;
|
|
1108
|
-
const completionTokens = usage?.completionTokens || 0;
|
|
1109
|
-
const totalTokens = usage?.totalTokens || promptTokens + completionTokens;
|
|
1097
|
+
const promptTokens = usageInfo?.promptTokens || 0;
|
|
1098
|
+
const completionTokens = usageInfo?.completionTokens || 0;
|
|
1099
|
+
const totalTokens = usageInfo?.totalTokens || promptTokens + completionTokens;
|
|
1110
1100
|
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
|
-
});
|
|
1122
1101
|
await client.completeSession({
|
|
1123
1102
|
sessionId: sid,
|
|
1124
1103
|
success: true,
|
|
@@ -1130,40 +1109,28 @@ function wrapStreamText(originalFn, client, config) {
|
|
|
1130
1109
|
totalTokens
|
|
1131
1110
|
});
|
|
1132
1111
|
}
|
|
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);
|
|
1133
1125
|
const textProp = result.text;
|
|
1134
|
-
if (typeof textProp === "
|
|
1135
|
-
trackCompletion(textProp).catch(() => {
|
|
1136
|
-
});
|
|
1137
|
-
} else if (textProp != null && typeof textProp.then === "function") {
|
|
1126
|
+
if (textProp != null && typeof textProp.then === "function") {
|
|
1138
1127
|
textProp.then((text) => {
|
|
1139
1128
|
trackCompletion(text).catch(() => {
|
|
1140
1129
|
});
|
|
1141
1130
|
}).catch((err) => {
|
|
1142
|
-
trackCompletion("", err instanceof Error ? err : new Error(String(err))).catch(() => {
|
|
1131
|
+
trackCompletion("", void 0, err instanceof Error ? err : new Error(String(err))).catch(() => {
|
|
1143
1132
|
});
|
|
1144
1133
|
});
|
|
1145
|
-
} else {
|
|
1146
|
-
const desc = Object.getOwnPropertyDescriptor(result, "textStream") ?? Object.getOwnPropertyDescriptor(Object.getPrototypeOf(result), "textStream");
|
|
1147
|
-
const isWritable = !desc || desc.writable || desc.set;
|
|
1148
|
-
if (isWritable) {
|
|
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
|
-
})();
|
|
1166
|
-
}
|
|
1167
1134
|
}
|
|
1168
1135
|
return result;
|
|
1169
1136
|
};
|
|
@@ -1251,50 +1218,52 @@ function wrapStreamObject(originalFn, client, config) {
|
|
|
1251
1218
|
return null;
|
|
1252
1219
|
}
|
|
1253
1220
|
})();
|
|
1254
|
-
|
|
1255
|
-
const
|
|
1256
|
-
|
|
1257
|
-
|
|
1221
|
+
let tracked = false;
|
|
1222
|
+
const userOnFinish = params.onFinish;
|
|
1223
|
+
const wrappedParams = {
|
|
1224
|
+
...params,
|
|
1225
|
+
onFinish: async (event) => {
|
|
1226
|
+
try {
|
|
1227
|
+
if (userOnFinish) await userOnFinish(event);
|
|
1228
|
+
} catch {
|
|
1229
|
+
}
|
|
1230
|
+
if (tracked) return;
|
|
1231
|
+
tracked = true;
|
|
1258
1232
|
const durationMs = Date.now() - startTime;
|
|
1259
1233
|
const sid = await sessionPromise;
|
|
1260
1234
|
if (!sid) return;
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1235
|
+
if (event.error) {
|
|
1236
|
+
const err = event.error instanceof Error ? event.error : new Error(String(event.error));
|
|
1237
|
+
await client.trackError({
|
|
1238
|
+
sessionId: sid,
|
|
1239
|
+
errorType: err.name || "Error",
|
|
1240
|
+
errorMessage: err.message || "Unknown error"
|
|
1241
|
+
});
|
|
1242
|
+
await client.completeSession({
|
|
1243
|
+
sessionId: sid,
|
|
1244
|
+
success: false,
|
|
1245
|
+
failureReason: err.message || "Unknown error",
|
|
1246
|
+
durationMs
|
|
1247
|
+
});
|
|
1248
|
+
return;
|
|
1265
1249
|
}
|
|
1266
|
-
const promptTokens = usage?.promptTokens || 0;
|
|
1267
|
-
const completionTokens = usage?.completionTokens || 0;
|
|
1268
|
-
const totalTokens = usage?.totalTokens || promptTokens + completionTokens;
|
|
1250
|
+
const promptTokens = event.usage?.promptTokens || 0;
|
|
1251
|
+
const completionTokens = event.usage?.completionTokens || 0;
|
|
1252
|
+
const totalTokens = event.usage?.totalTokens || promptTokens + completionTokens;
|
|
1269
1253
|
const cost = calculateCostForCall(provider, modelId, promptTokens, completionTokens);
|
|
1270
1254
|
await client.completeSession({
|
|
1271
1255
|
sessionId: sid,
|
|
1272
1256
|
success: true,
|
|
1273
|
-
output: JSON.stringify(
|
|
1257
|
+
output: event.object != null ? JSON.stringify(event.object) : "",
|
|
1274
1258
|
durationMs,
|
|
1275
1259
|
estimatedCost: cost,
|
|
1276
1260
|
promptTokens,
|
|
1277
1261
|
completionTokens,
|
|
1278
1262
|
totalTokens
|
|
1279
1263
|
});
|
|
1280
|
-
}
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
if (!sid) return;
|
|
1284
|
-
await client.trackError({
|
|
1285
|
-
sessionId: sid,
|
|
1286
|
-
errorType: error instanceof Error ? error.name : "Error",
|
|
1287
|
-
errorMessage: error instanceof Error ? error.message : "Unknown error"
|
|
1288
|
-
});
|
|
1289
|
-
await client.completeSession({
|
|
1290
|
-
sessionId: sid,
|
|
1291
|
-
success: false,
|
|
1292
|
-
failureReason: error instanceof Error ? error.message : "Unknown error",
|
|
1293
|
-
durationMs
|
|
1294
|
-
});
|
|
1295
|
-
});
|
|
1296
|
-
}
|
|
1297
|
-
return result;
|
|
1264
|
+
}
|
|
1265
|
+
};
|
|
1266
|
+
return originalFn(wrappedParams);
|
|
1298
1267
|
};
|
|
1299
1268
|
}
|
|
1300
1269
|
function wrapAISDK(ai, options) {
|