@sentrial/sdk 0.3.2 → 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 +76 -86
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +76 -86
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1178,6 +1178,17 @@ function wrapStreamText(originalFn, client, config) {
|
|
|
1178
1178
|
const completionTokens = usage?.completionTokens || 0;
|
|
1179
1179
|
const totalTokens = usage?.totalTokens || promptTokens + completionTokens;
|
|
1180
1180
|
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
|
+
});
|
|
1181
1192
|
await client.completeSession({
|
|
1182
1193
|
sessionId: sid,
|
|
1183
1194
|
success: true,
|
|
@@ -1189,48 +1200,41 @@ function wrapStreamText(originalFn, client, config) {
|
|
|
1189
1200
|
totalTokens
|
|
1190
1201
|
});
|
|
1191
1202
|
}
|
|
1192
|
-
const originalTextStream = result.textStream;
|
|
1193
|
-
let wrappedTextStream = null;
|
|
1194
|
-
const proxied = new Proxy(result, {
|
|
1195
|
-
get(target, prop, receiver) {
|
|
1196
|
-
if (prop === "textStream") {
|
|
1197
|
-
if (!wrappedTextStream) {
|
|
1198
|
-
let fullText = "";
|
|
1199
|
-
wrappedTextStream = (async function* () {
|
|
1200
|
-
try {
|
|
1201
|
-
for await (const chunk of originalTextStream) {
|
|
1202
|
-
fullText += chunk;
|
|
1203
|
-
yield chunk;
|
|
1204
|
-
}
|
|
1205
|
-
await trackCompletion(fullText);
|
|
1206
|
-
} catch (error) {
|
|
1207
|
-
await trackCompletion(
|
|
1208
|
-
fullText,
|
|
1209
|
-
error instanceof Error ? error : new Error(String(error))
|
|
1210
|
-
);
|
|
1211
|
-
throw error;
|
|
1212
|
-
}
|
|
1213
|
-
})();
|
|
1214
|
-
}
|
|
1215
|
-
return wrappedTextStream;
|
|
1216
|
-
}
|
|
1217
|
-
return Reflect.get(target, prop, receiver);
|
|
1218
|
-
}
|
|
1219
|
-
});
|
|
1220
1203
|
const textProp = result.text;
|
|
1221
1204
|
if (typeof textProp === "string") {
|
|
1222
1205
|
trackCompletion(textProp).catch(() => {
|
|
1223
1206
|
});
|
|
1224
1207
|
} else if (textProp != null && typeof textProp.then === "function") {
|
|
1225
|
-
|
|
1208
|
+
const originalTextPromise = textProp;
|
|
1209
|
+
result.text = originalTextPromise.then((text) => {
|
|
1226
1210
|
trackCompletion(text).catch(() => {
|
|
1227
1211
|
});
|
|
1212
|
+
return text;
|
|
1228
1213
|
}).catch((err) => {
|
|
1229
1214
|
trackCompletion("", err instanceof Error ? err : new Error(String(err))).catch(() => {
|
|
1230
1215
|
});
|
|
1216
|
+
throw err;
|
|
1231
1217
|
});
|
|
1218
|
+
} else {
|
|
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
|
+
})();
|
|
1232
1236
|
}
|
|
1233
|
-
return
|
|
1237
|
+
return result;
|
|
1234
1238
|
};
|
|
1235
1239
|
}
|
|
1236
1240
|
function wrapGenerateObject(originalFn, client, config) {
|
|
@@ -1317,64 +1321,50 @@ function wrapStreamObject(originalFn, client, config) {
|
|
|
1317
1321
|
}
|
|
1318
1322
|
})();
|
|
1319
1323
|
const result = originalFn(params);
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
tracked = true;
|
|
1331
|
-
const durationMs = Date.now() - startTime;
|
|
1332
|
-
const sid = await sessionPromise;
|
|
1333
|
-
if (!sid) return obj;
|
|
1334
|
-
let usage;
|
|
1335
|
-
try {
|
|
1336
|
-
usage = result.usage ? await result.usage : void 0;
|
|
1337
|
-
} catch {
|
|
1338
|
-
}
|
|
1339
|
-
const promptTokens = usage?.promptTokens || 0;
|
|
1340
|
-
const completionTokens = usage?.completionTokens || 0;
|
|
1341
|
-
const totalTokens = usage?.totalTokens || promptTokens + completionTokens;
|
|
1342
|
-
const cost = calculateCostForCall(provider, modelId, promptTokens, completionTokens);
|
|
1343
|
-
await client.completeSession({
|
|
1344
|
-
sessionId: sid,
|
|
1345
|
-
success: true,
|
|
1346
|
-
output: JSON.stringify(obj),
|
|
1347
|
-
durationMs,
|
|
1348
|
-
estimatedCost: cost,
|
|
1349
|
-
promptTokens,
|
|
1350
|
-
completionTokens,
|
|
1351
|
-
totalTokens
|
|
1352
|
-
});
|
|
1353
|
-
return obj;
|
|
1354
|
-
}).catch(async (error) => {
|
|
1355
|
-
if (tracked) throw error;
|
|
1356
|
-
tracked = true;
|
|
1357
|
-
const durationMs = Date.now() - startTime;
|
|
1358
|
-
const sid = await sessionPromise;
|
|
1359
|
-
if (!sid) throw error;
|
|
1360
|
-
await client.trackError({
|
|
1361
|
-
sessionId: sid,
|
|
1362
|
-
errorType: error instanceof Error ? error.name : "Error",
|
|
1363
|
-
errorMessage: error instanceof Error ? error.message : "Unknown error"
|
|
1364
|
-
});
|
|
1365
|
-
await client.completeSession({
|
|
1366
|
-
sessionId: sid,
|
|
1367
|
-
success: false,
|
|
1368
|
-
failureReason: error instanceof Error ? error.message : "Unknown error",
|
|
1369
|
-
durationMs
|
|
1370
|
-
});
|
|
1371
|
-
throw error;
|
|
1372
|
-
});
|
|
1373
|
-
}
|
|
1374
|
-
return wrappedObjectPromise;
|
|
1324
|
+
if (result.object) {
|
|
1325
|
+
const originalObjectPromise = result.object;
|
|
1326
|
+
result.object = originalObjectPromise.then(async (obj) => {
|
|
1327
|
+
const durationMs = Date.now() - startTime;
|
|
1328
|
+
const sid = await sessionPromise;
|
|
1329
|
+
if (sid) {
|
|
1330
|
+
let usage;
|
|
1331
|
+
try {
|
|
1332
|
+
usage = result.usage ? await result.usage : void 0;
|
|
1333
|
+
} catch {
|
|
1375
1334
|
}
|
|
1376
|
-
|
|
1335
|
+
const promptTokens = usage?.promptTokens || 0;
|
|
1336
|
+
const completionTokens = usage?.completionTokens || 0;
|
|
1337
|
+
const totalTokens = usage?.totalTokens || promptTokens + completionTokens;
|
|
1338
|
+
const cost = calculateCostForCall(provider, modelId, promptTokens, completionTokens);
|
|
1339
|
+
await client.completeSession({
|
|
1340
|
+
sessionId: sid,
|
|
1341
|
+
success: true,
|
|
1342
|
+
output: JSON.stringify(obj),
|
|
1343
|
+
durationMs,
|
|
1344
|
+
estimatedCost: cost,
|
|
1345
|
+
promptTokens,
|
|
1346
|
+
completionTokens,
|
|
1347
|
+
totalTokens
|
|
1348
|
+
});
|
|
1349
|
+
}
|
|
1350
|
+
return obj;
|
|
1351
|
+
}).catch(async (error) => {
|
|
1352
|
+
const durationMs = Date.now() - startTime;
|
|
1353
|
+
const sid = await sessionPromise;
|
|
1354
|
+
if (sid) {
|
|
1355
|
+
await client.trackError({
|
|
1356
|
+
sessionId: sid,
|
|
1357
|
+
errorType: error instanceof Error ? error.name : "Error",
|
|
1358
|
+
errorMessage: error instanceof Error ? error.message : "Unknown error"
|
|
1359
|
+
});
|
|
1360
|
+
await client.completeSession({
|
|
1361
|
+
sessionId: sid,
|
|
1362
|
+
success: false,
|
|
1363
|
+
failureReason: error instanceof Error ? error.message : "Unknown error",
|
|
1364
|
+
durationMs
|
|
1365
|
+
});
|
|
1377
1366
|
}
|
|
1367
|
+
throw error;
|
|
1378
1368
|
});
|
|
1379
1369
|
}
|
|
1380
1370
|
return result;
|