@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.cjs
CHANGED
|
@@ -1143,13 +1143,8 @@ function wrapStreamText(originalFn, client, config) {
|
|
|
1143
1143
|
return null;
|
|
1144
1144
|
}
|
|
1145
1145
|
})();
|
|
1146
|
-
const wrappedParams = {
|
|
1147
|
-
...params,
|
|
1148
|
-
tools: params.tools ? wrapToolsAsync(params.tools, sessionPromise, client) : void 0
|
|
1149
|
-
};
|
|
1150
|
-
const result = originalFn(wrappedParams);
|
|
1151
1146
|
let tracked = false;
|
|
1152
|
-
async function trackCompletion(fullText, error) {
|
|
1147
|
+
async function trackCompletion(fullText, usageInfo, error) {
|
|
1153
1148
|
if (tracked) return;
|
|
1154
1149
|
tracked = true;
|
|
1155
1150
|
const durationMs = Date.now() - startTime;
|
|
@@ -1169,26 +1164,10 @@ function wrapStreamText(originalFn, client, config) {
|
|
|
1169
1164
|
});
|
|
1170
1165
|
return;
|
|
1171
1166
|
}
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
} catch {
|
|
1176
|
-
}
|
|
1177
|
-
const promptTokens = usage?.promptTokens || 0;
|
|
1178
|
-
const completionTokens = usage?.completionTokens || 0;
|
|
1179
|
-
const totalTokens = usage?.totalTokens || promptTokens + completionTokens;
|
|
1167
|
+
const promptTokens = usageInfo?.promptTokens || 0;
|
|
1168
|
+
const completionTokens = usageInfo?.completionTokens || 0;
|
|
1169
|
+
const totalTokens = usageInfo?.totalTokens || promptTokens + completionTokens;
|
|
1180
1170
|
const cost = calculateCostForCall(provider, modelId, promptTokens, completionTokens);
|
|
1181
|
-
await client.trackEvent({
|
|
1182
|
-
sessionId: sid,
|
|
1183
|
-
eventType: "llm_call",
|
|
1184
|
-
eventData: {
|
|
1185
|
-
model: modelId,
|
|
1186
|
-
provider,
|
|
1187
|
-
prompt_tokens: promptTokens,
|
|
1188
|
-
completion_tokens: completionTokens,
|
|
1189
|
-
total_tokens: totalTokens
|
|
1190
|
-
}
|
|
1191
|
-
});
|
|
1192
1171
|
await client.completeSession({
|
|
1193
1172
|
sessionId: sid,
|
|
1194
1173
|
success: true,
|
|
@@ -1200,40 +1179,28 @@ function wrapStreamText(originalFn, client, config) {
|
|
|
1200
1179
|
totalTokens
|
|
1201
1180
|
});
|
|
1202
1181
|
}
|
|
1182
|
+
const userOnFinish = params.onFinish;
|
|
1183
|
+
const wrappedParams = {
|
|
1184
|
+
...params,
|
|
1185
|
+
tools: params.tools ? wrapToolsAsync(params.tools, sessionPromise, client) : void 0,
|
|
1186
|
+
onFinish: async (event) => {
|
|
1187
|
+
try {
|
|
1188
|
+
if (userOnFinish) await userOnFinish(event);
|
|
1189
|
+
} catch {
|
|
1190
|
+
}
|
|
1191
|
+
await trackCompletion(event.text, event.usage);
|
|
1192
|
+
}
|
|
1193
|
+
};
|
|
1194
|
+
const result = originalFn(wrappedParams);
|
|
1203
1195
|
const textProp = result.text;
|
|
1204
|
-
if (typeof textProp === "
|
|
1205
|
-
trackCompletion(textProp).catch(() => {
|
|
1206
|
-
});
|
|
1207
|
-
} else if (textProp != null && typeof textProp.then === "function") {
|
|
1196
|
+
if (textProp != null && typeof textProp.then === "function") {
|
|
1208
1197
|
textProp.then((text) => {
|
|
1209
1198
|
trackCompletion(text).catch(() => {
|
|
1210
1199
|
});
|
|
1211
1200
|
}).catch((err) => {
|
|
1212
|
-
trackCompletion("", err instanceof Error ? err : new Error(String(err))).catch(() => {
|
|
1201
|
+
trackCompletion("", void 0, err instanceof Error ? err : new Error(String(err))).catch(() => {
|
|
1213
1202
|
});
|
|
1214
1203
|
});
|
|
1215
|
-
} else {
|
|
1216
|
-
const desc = Object.getOwnPropertyDescriptor(result, "textStream") ?? Object.getOwnPropertyDescriptor(Object.getPrototypeOf(result), "textStream");
|
|
1217
|
-
const isWritable = !desc || desc.writable || desc.set;
|
|
1218
|
-
if (isWritable) {
|
|
1219
|
-
const originalTextStream = result.textStream;
|
|
1220
|
-
let fullText = "";
|
|
1221
|
-
result.textStream = (async function* () {
|
|
1222
|
-
try {
|
|
1223
|
-
for await (const chunk of originalTextStream) {
|
|
1224
|
-
fullText += chunk;
|
|
1225
|
-
yield chunk;
|
|
1226
|
-
}
|
|
1227
|
-
await trackCompletion(fullText);
|
|
1228
|
-
} catch (error) {
|
|
1229
|
-
await trackCompletion(
|
|
1230
|
-
"",
|
|
1231
|
-
error instanceof Error ? error : new Error(String(error))
|
|
1232
|
-
);
|
|
1233
|
-
throw error;
|
|
1234
|
-
}
|
|
1235
|
-
})();
|
|
1236
|
-
}
|
|
1237
1204
|
}
|
|
1238
1205
|
return result;
|
|
1239
1206
|
};
|
|
@@ -1321,50 +1288,52 @@ function wrapStreamObject(originalFn, client, config) {
|
|
|
1321
1288
|
return null;
|
|
1322
1289
|
}
|
|
1323
1290
|
})();
|
|
1324
|
-
|
|
1325
|
-
const
|
|
1326
|
-
|
|
1327
|
-
|
|
1291
|
+
let tracked = false;
|
|
1292
|
+
const userOnFinish = params.onFinish;
|
|
1293
|
+
const wrappedParams = {
|
|
1294
|
+
...params,
|
|
1295
|
+
onFinish: async (event) => {
|
|
1296
|
+
try {
|
|
1297
|
+
if (userOnFinish) await userOnFinish(event);
|
|
1298
|
+
} catch {
|
|
1299
|
+
}
|
|
1300
|
+
if (tracked) return;
|
|
1301
|
+
tracked = true;
|
|
1328
1302
|
const durationMs = Date.now() - startTime;
|
|
1329
1303
|
const sid = await sessionPromise;
|
|
1330
1304
|
if (!sid) return;
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1305
|
+
if (event.error) {
|
|
1306
|
+
const err = event.error instanceof Error ? event.error : new Error(String(event.error));
|
|
1307
|
+
await client.trackError({
|
|
1308
|
+
sessionId: sid,
|
|
1309
|
+
errorType: err.name || "Error",
|
|
1310
|
+
errorMessage: err.message || "Unknown error"
|
|
1311
|
+
});
|
|
1312
|
+
await client.completeSession({
|
|
1313
|
+
sessionId: sid,
|
|
1314
|
+
success: false,
|
|
1315
|
+
failureReason: err.message || "Unknown error",
|
|
1316
|
+
durationMs
|
|
1317
|
+
});
|
|
1318
|
+
return;
|
|
1335
1319
|
}
|
|
1336
|
-
const promptTokens = usage?.promptTokens || 0;
|
|
1337
|
-
const completionTokens = usage?.completionTokens || 0;
|
|
1338
|
-
const totalTokens = usage?.totalTokens || promptTokens + completionTokens;
|
|
1320
|
+
const promptTokens = event.usage?.promptTokens || 0;
|
|
1321
|
+
const completionTokens = event.usage?.completionTokens || 0;
|
|
1322
|
+
const totalTokens = event.usage?.totalTokens || promptTokens + completionTokens;
|
|
1339
1323
|
const cost = calculateCostForCall(provider, modelId, promptTokens, completionTokens);
|
|
1340
1324
|
await client.completeSession({
|
|
1341
1325
|
sessionId: sid,
|
|
1342
1326
|
success: true,
|
|
1343
|
-
output: JSON.stringify(
|
|
1327
|
+
output: event.object != null ? JSON.stringify(event.object) : "",
|
|
1344
1328
|
durationMs,
|
|
1345
1329
|
estimatedCost: cost,
|
|
1346
1330
|
promptTokens,
|
|
1347
1331
|
completionTokens,
|
|
1348
1332
|
totalTokens
|
|
1349
1333
|
});
|
|
1350
|
-
}
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
if (!sid) return;
|
|
1354
|
-
await client.trackError({
|
|
1355
|
-
sessionId: sid,
|
|
1356
|
-
errorType: error instanceof Error ? error.name : "Error",
|
|
1357
|
-
errorMessage: error instanceof Error ? error.message : "Unknown error"
|
|
1358
|
-
});
|
|
1359
|
-
await client.completeSession({
|
|
1360
|
-
sessionId: sid,
|
|
1361
|
-
success: false,
|
|
1362
|
-
failureReason: error instanceof Error ? error.message : "Unknown error",
|
|
1363
|
-
durationMs
|
|
1364
|
-
});
|
|
1365
|
-
});
|
|
1366
|
-
}
|
|
1367
|
-
return result;
|
|
1334
|
+
}
|
|
1335
|
+
};
|
|
1336
|
+
return originalFn(wrappedParams);
|
|
1368
1337
|
};
|
|
1369
1338
|
}
|
|
1370
1339
|
function wrapAISDK(ai, options) {
|