@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.cjs
CHANGED
|
@@ -1143,8 +1143,13 @@ 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);
|
|
1146
1151
|
let tracked = false;
|
|
1147
|
-
async function trackCompletion(fullText,
|
|
1152
|
+
async function trackCompletion(fullText, error) {
|
|
1148
1153
|
if (tracked) return;
|
|
1149
1154
|
tracked = true;
|
|
1150
1155
|
const durationMs = Date.now() - startTime;
|
|
@@ -1164,10 +1169,26 @@ function wrapStreamText(originalFn, client, config) {
|
|
|
1164
1169
|
});
|
|
1165
1170
|
return;
|
|
1166
1171
|
}
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1172
|
+
let usage;
|
|
1173
|
+
try {
|
|
1174
|
+
usage = result.usage ? await result.usage : void 0;
|
|
1175
|
+
} catch {
|
|
1176
|
+
}
|
|
1177
|
+
const promptTokens = usage?.promptTokens || 0;
|
|
1178
|
+
const completionTokens = usage?.completionTokens || 0;
|
|
1179
|
+
const totalTokens = usage?.totalTokens || promptTokens + completionTokens;
|
|
1170
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
|
+
});
|
|
1171
1192
|
await client.completeSession({
|
|
1172
1193
|
sessionId: sid,
|
|
1173
1194
|
success: true,
|
|
@@ -1179,28 +1200,39 @@ function wrapStreamText(originalFn, client, config) {
|
|
|
1179
1200
|
totalTokens
|
|
1180
1201
|
});
|
|
1181
1202
|
}
|
|
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);
|
|
1195
1203
|
const textProp = result.text;
|
|
1196
|
-
if (
|
|
1197
|
-
textProp.
|
|
1204
|
+
if (typeof textProp === "string") {
|
|
1205
|
+
trackCompletion(textProp).catch(() => {
|
|
1206
|
+
});
|
|
1207
|
+
} else if (textProp != null && typeof textProp.then === "function") {
|
|
1208
|
+
const originalTextPromise = textProp;
|
|
1209
|
+
result.text = originalTextPromise.then((text) => {
|
|
1198
1210
|
trackCompletion(text).catch(() => {
|
|
1199
1211
|
});
|
|
1212
|
+
return text;
|
|
1200
1213
|
}).catch((err) => {
|
|
1201
|
-
trackCompletion("",
|
|
1214
|
+
trackCompletion("", err instanceof Error ? err : new Error(String(err))).catch(() => {
|
|
1202
1215
|
});
|
|
1216
|
+
throw err;
|
|
1203
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
|
+
})();
|
|
1204
1236
|
}
|
|
1205
1237
|
return result;
|
|
1206
1238
|
};
|
|
@@ -1288,52 +1320,54 @@ function wrapStreamObject(originalFn, client, config) {
|
|
|
1288
1320
|
return null;
|
|
1289
1321
|
}
|
|
1290
1322
|
})();
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1323
|
+
const result = originalFn(params);
|
|
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 {
|
|
1334
|
+
}
|
|
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
|
+
});
|
|
1299
1349
|
}
|
|
1300
|
-
|
|
1301
|
-
|
|
1350
|
+
return obj;
|
|
1351
|
+
}).catch(async (error) => {
|
|
1302
1352
|
const durationMs = Date.now() - startTime;
|
|
1303
1353
|
const sid = await sessionPromise;
|
|
1304
|
-
if (
|
|
1305
|
-
if (event.error) {
|
|
1306
|
-
const err = event.error instanceof Error ? event.error : new Error(String(event.error));
|
|
1354
|
+
if (sid) {
|
|
1307
1355
|
await client.trackError({
|
|
1308
1356
|
sessionId: sid,
|
|
1309
|
-
errorType:
|
|
1310
|
-
errorMessage:
|
|
1357
|
+
errorType: error instanceof Error ? error.name : "Error",
|
|
1358
|
+
errorMessage: error instanceof Error ? error.message : "Unknown error"
|
|
1311
1359
|
});
|
|
1312
1360
|
await client.completeSession({
|
|
1313
1361
|
sessionId: sid,
|
|
1314
1362
|
success: false,
|
|
1315
|
-
failureReason:
|
|
1363
|
+
failureReason: error instanceof Error ? error.message : "Unknown error",
|
|
1316
1364
|
durationMs
|
|
1317
1365
|
});
|
|
1318
|
-
return;
|
|
1319
1366
|
}
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
await client.completeSession({
|
|
1325
|
-
sessionId: sid,
|
|
1326
|
-
success: true,
|
|
1327
|
-
output: event.object != null ? JSON.stringify(event.object) : "",
|
|
1328
|
-
durationMs,
|
|
1329
|
-
estimatedCost: cost,
|
|
1330
|
-
promptTokens,
|
|
1331
|
-
completionTokens,
|
|
1332
|
-
totalTokens
|
|
1333
|
-
});
|
|
1334
|
-
}
|
|
1335
|
-
};
|
|
1336
|
-
return originalFn(wrappedParams);
|
|
1367
|
+
throw error;
|
|
1368
|
+
});
|
|
1369
|
+
}
|
|
1370
|
+
return result;
|
|
1337
1371
|
};
|
|
1338
1372
|
}
|
|
1339
1373
|
function wrapAISDK(ai, options) {
|