@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.js
CHANGED
|
@@ -1135,34 +1135,35 @@ function wrapStreamText(originalFn, client, config) {
|
|
|
1135
1135
|
trackCompletion(textProp).catch(() => {
|
|
1136
1136
|
});
|
|
1137
1137
|
} else if (textProp != null && typeof textProp.then === "function") {
|
|
1138
|
-
|
|
1139
|
-
result.text = originalTextPromise.then((text) => {
|
|
1138
|
+
textProp.then((text) => {
|
|
1140
1139
|
trackCompletion(text).catch(() => {
|
|
1141
1140
|
});
|
|
1142
|
-
return text;
|
|
1143
1141
|
}).catch((err) => {
|
|
1144
1142
|
trackCompletion("", err instanceof Error ? err : new Error(String(err))).catch(() => {
|
|
1145
1143
|
});
|
|
1146
|
-
throw err;
|
|
1147
1144
|
});
|
|
1148
1145
|
} else {
|
|
1149
|
-
const
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
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;
|
|
1156
1164
|
}
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
await trackCompletion(
|
|
1160
|
-
"",
|
|
1161
|
-
error instanceof Error ? error : new Error(String(error))
|
|
1162
|
-
);
|
|
1163
|
-
throw error;
|
|
1164
|
-
}
|
|
1165
|
-
})();
|
|
1165
|
+
})();
|
|
1166
|
+
}
|
|
1166
1167
|
}
|
|
1167
1168
|
return result;
|
|
1168
1169
|
};
|
|
@@ -1251,50 +1252,46 @@ function wrapStreamObject(originalFn, client, config) {
|
|
|
1251
1252
|
}
|
|
1252
1253
|
})();
|
|
1253
1254
|
const result = originalFn(params);
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1255
|
+
const objectProp = result.object;
|
|
1256
|
+
if (objectProp && typeof objectProp.then === "function") {
|
|
1257
|
+
objectProp.then(async (obj) => {
|
|
1257
1258
|
const durationMs = Date.now() - startTime;
|
|
1258
1259
|
const sid = await sessionPromise;
|
|
1259
|
-
if (sid)
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
}
|
|
1265
|
-
const promptTokens = usage?.promptTokens || 0;
|
|
1266
|
-
const completionTokens = usage?.completionTokens || 0;
|
|
1267
|
-
const totalTokens = usage?.totalTokens || promptTokens + completionTokens;
|
|
1268
|
-
const cost = calculateCostForCall(provider, modelId, promptTokens, completionTokens);
|
|
1269
|
-
await client.completeSession({
|
|
1270
|
-
sessionId: sid,
|
|
1271
|
-
success: true,
|
|
1272
|
-
output: JSON.stringify(obj),
|
|
1273
|
-
durationMs,
|
|
1274
|
-
estimatedCost: cost,
|
|
1275
|
-
promptTokens,
|
|
1276
|
-
completionTokens,
|
|
1277
|
-
totalTokens
|
|
1278
|
-
});
|
|
1260
|
+
if (!sid) return;
|
|
1261
|
+
let usage;
|
|
1262
|
+
try {
|
|
1263
|
+
usage = result.usage ? await result.usage : void 0;
|
|
1264
|
+
} catch {
|
|
1279
1265
|
}
|
|
1280
|
-
|
|
1266
|
+
const promptTokens = usage?.promptTokens || 0;
|
|
1267
|
+
const completionTokens = usage?.completionTokens || 0;
|
|
1268
|
+
const totalTokens = usage?.totalTokens || promptTokens + completionTokens;
|
|
1269
|
+
const cost = calculateCostForCall(provider, modelId, promptTokens, completionTokens);
|
|
1270
|
+
await client.completeSession({
|
|
1271
|
+
sessionId: sid,
|
|
1272
|
+
success: true,
|
|
1273
|
+
output: JSON.stringify(obj),
|
|
1274
|
+
durationMs,
|
|
1275
|
+
estimatedCost: cost,
|
|
1276
|
+
promptTokens,
|
|
1277
|
+
completionTokens,
|
|
1278
|
+
totalTokens
|
|
1279
|
+
});
|
|
1281
1280
|
}).catch(async (error) => {
|
|
1282
1281
|
const durationMs = Date.now() - startTime;
|
|
1283
1282
|
const sid = await sessionPromise;
|
|
1284
|
-
if (sid)
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
}
|
|
1297
|
-
throw error;
|
|
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
|
+
});
|
|
1298
1295
|
});
|
|
1299
1296
|
}
|
|
1300
1297
|
return result;
|