@sentrial/sdk 0.3.0 → 0.3.1
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 +55 -58
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +55 -58
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1205,34 +1205,35 @@ function wrapStreamText(originalFn, client, config) {
|
|
|
1205
1205
|
trackCompletion(textProp).catch(() => {
|
|
1206
1206
|
});
|
|
1207
1207
|
} else if (textProp != null && typeof textProp.then === "function") {
|
|
1208
|
-
|
|
1209
|
-
result.text = originalTextPromise.then((text) => {
|
|
1208
|
+
textProp.then((text) => {
|
|
1210
1209
|
trackCompletion(text).catch(() => {
|
|
1211
1210
|
});
|
|
1212
|
-
return text;
|
|
1213
1211
|
}).catch((err) => {
|
|
1214
1212
|
trackCompletion("", err instanceof Error ? err : new Error(String(err))).catch(() => {
|
|
1215
1213
|
});
|
|
1216
|
-
throw err;
|
|
1217
1214
|
});
|
|
1218
1215
|
} else {
|
|
1219
|
-
const
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
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;
|
|
1226
1234
|
}
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
await trackCompletion(
|
|
1230
|
-
"",
|
|
1231
|
-
error instanceof Error ? error : new Error(String(error))
|
|
1232
|
-
);
|
|
1233
|
-
throw error;
|
|
1234
|
-
}
|
|
1235
|
-
})();
|
|
1235
|
+
})();
|
|
1236
|
+
}
|
|
1236
1237
|
}
|
|
1237
1238
|
return result;
|
|
1238
1239
|
};
|
|
@@ -1321,50 +1322,46 @@ function wrapStreamObject(originalFn, client, config) {
|
|
|
1321
1322
|
}
|
|
1322
1323
|
})();
|
|
1323
1324
|
const result = originalFn(params);
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1325
|
+
const objectProp = result.object;
|
|
1326
|
+
if (objectProp && typeof objectProp.then === "function") {
|
|
1327
|
+
objectProp.then(async (obj) => {
|
|
1327
1328
|
const durationMs = Date.now() - startTime;
|
|
1328
1329
|
const sid = await sessionPromise;
|
|
1329
|
-
if (sid)
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
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
|
-
});
|
|
1330
|
+
if (!sid) return;
|
|
1331
|
+
let usage;
|
|
1332
|
+
try {
|
|
1333
|
+
usage = result.usage ? await result.usage : void 0;
|
|
1334
|
+
} catch {
|
|
1349
1335
|
}
|
|
1350
|
-
|
|
1336
|
+
const promptTokens = usage?.promptTokens || 0;
|
|
1337
|
+
const completionTokens = usage?.completionTokens || 0;
|
|
1338
|
+
const totalTokens = usage?.totalTokens || promptTokens + completionTokens;
|
|
1339
|
+
const cost = calculateCostForCall(provider, modelId, promptTokens, completionTokens);
|
|
1340
|
+
await client.completeSession({
|
|
1341
|
+
sessionId: sid,
|
|
1342
|
+
success: true,
|
|
1343
|
+
output: JSON.stringify(obj),
|
|
1344
|
+
durationMs,
|
|
1345
|
+
estimatedCost: cost,
|
|
1346
|
+
promptTokens,
|
|
1347
|
+
completionTokens,
|
|
1348
|
+
totalTokens
|
|
1349
|
+
});
|
|
1351
1350
|
}).catch(async (error) => {
|
|
1352
1351
|
const durationMs = Date.now() - startTime;
|
|
1353
1352
|
const sid = await sessionPromise;
|
|
1354
|
-
if (sid)
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
}
|
|
1367
|
-
throw error;
|
|
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
|
+
});
|
|
1368
1365
|
});
|
|
1369
1366
|
}
|
|
1370
1367
|
return result;
|