@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.js
CHANGED
|
@@ -1108,6 +1108,17 @@ function wrapStreamText(originalFn, client, config) {
|
|
|
1108
1108
|
const completionTokens = usage?.completionTokens || 0;
|
|
1109
1109
|
const totalTokens = usage?.totalTokens || promptTokens + completionTokens;
|
|
1110
1110
|
const cost = calculateCostForCall(provider, modelId, promptTokens, completionTokens);
|
|
1111
|
+
await client.trackEvent({
|
|
1112
|
+
sessionId: sid,
|
|
1113
|
+
eventType: "llm_call",
|
|
1114
|
+
eventData: {
|
|
1115
|
+
model: modelId,
|
|
1116
|
+
provider,
|
|
1117
|
+
prompt_tokens: promptTokens,
|
|
1118
|
+
completion_tokens: completionTokens,
|
|
1119
|
+
total_tokens: totalTokens
|
|
1120
|
+
}
|
|
1121
|
+
});
|
|
1111
1122
|
await client.completeSession({
|
|
1112
1123
|
sessionId: sid,
|
|
1113
1124
|
success: true,
|
|
@@ -1119,48 +1130,41 @@ function wrapStreamText(originalFn, client, config) {
|
|
|
1119
1130
|
totalTokens
|
|
1120
1131
|
});
|
|
1121
1132
|
}
|
|
1122
|
-
const originalTextStream = result.textStream;
|
|
1123
|
-
let wrappedTextStream = null;
|
|
1124
|
-
const proxied = new Proxy(result, {
|
|
1125
|
-
get(target, prop, receiver) {
|
|
1126
|
-
if (prop === "textStream") {
|
|
1127
|
-
if (!wrappedTextStream) {
|
|
1128
|
-
let fullText = "";
|
|
1129
|
-
wrappedTextStream = (async function* () {
|
|
1130
|
-
try {
|
|
1131
|
-
for await (const chunk of originalTextStream) {
|
|
1132
|
-
fullText += chunk;
|
|
1133
|
-
yield chunk;
|
|
1134
|
-
}
|
|
1135
|
-
await trackCompletion(fullText);
|
|
1136
|
-
} catch (error) {
|
|
1137
|
-
await trackCompletion(
|
|
1138
|
-
fullText,
|
|
1139
|
-
error instanceof Error ? error : new Error(String(error))
|
|
1140
|
-
);
|
|
1141
|
-
throw error;
|
|
1142
|
-
}
|
|
1143
|
-
})();
|
|
1144
|
-
}
|
|
1145
|
-
return wrappedTextStream;
|
|
1146
|
-
}
|
|
1147
|
-
return Reflect.get(target, prop, receiver);
|
|
1148
|
-
}
|
|
1149
|
-
});
|
|
1150
1133
|
const textProp = result.text;
|
|
1151
1134
|
if (typeof textProp === "string") {
|
|
1152
1135
|
trackCompletion(textProp).catch(() => {
|
|
1153
1136
|
});
|
|
1154
1137
|
} else if (textProp != null && typeof textProp.then === "function") {
|
|
1155
|
-
|
|
1138
|
+
const originalTextPromise = textProp;
|
|
1139
|
+
result.text = originalTextPromise.then((text) => {
|
|
1156
1140
|
trackCompletion(text).catch(() => {
|
|
1157
1141
|
});
|
|
1142
|
+
return text;
|
|
1158
1143
|
}).catch((err) => {
|
|
1159
1144
|
trackCompletion("", err instanceof Error ? err : new Error(String(err))).catch(() => {
|
|
1160
1145
|
});
|
|
1146
|
+
throw err;
|
|
1161
1147
|
});
|
|
1148
|
+
} else {
|
|
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;
|
|
1164
|
+
}
|
|
1165
|
+
})();
|
|
1162
1166
|
}
|
|
1163
|
-
return
|
|
1167
|
+
return result;
|
|
1164
1168
|
};
|
|
1165
1169
|
}
|
|
1166
1170
|
function wrapGenerateObject(originalFn, client, config) {
|
|
@@ -1247,64 +1251,50 @@ function wrapStreamObject(originalFn, client, config) {
|
|
|
1247
1251
|
}
|
|
1248
1252
|
})();
|
|
1249
1253
|
const result = originalFn(params);
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
tracked = true;
|
|
1261
|
-
const durationMs = Date.now() - startTime;
|
|
1262
|
-
const sid = await sessionPromise;
|
|
1263
|
-
if (!sid) return obj;
|
|
1264
|
-
let usage;
|
|
1265
|
-
try {
|
|
1266
|
-
usage = result.usage ? await result.usage : void 0;
|
|
1267
|
-
} catch {
|
|
1268
|
-
}
|
|
1269
|
-
const promptTokens = usage?.promptTokens || 0;
|
|
1270
|
-
const completionTokens = usage?.completionTokens || 0;
|
|
1271
|
-
const totalTokens = usage?.totalTokens || promptTokens + completionTokens;
|
|
1272
|
-
const cost = calculateCostForCall(provider, modelId, promptTokens, completionTokens);
|
|
1273
|
-
await client.completeSession({
|
|
1274
|
-
sessionId: sid,
|
|
1275
|
-
success: true,
|
|
1276
|
-
output: JSON.stringify(obj),
|
|
1277
|
-
durationMs,
|
|
1278
|
-
estimatedCost: cost,
|
|
1279
|
-
promptTokens,
|
|
1280
|
-
completionTokens,
|
|
1281
|
-
totalTokens
|
|
1282
|
-
});
|
|
1283
|
-
return obj;
|
|
1284
|
-
}).catch(async (error) => {
|
|
1285
|
-
if (tracked) throw error;
|
|
1286
|
-
tracked = true;
|
|
1287
|
-
const durationMs = Date.now() - startTime;
|
|
1288
|
-
const sid = await sessionPromise;
|
|
1289
|
-
if (!sid) throw error;
|
|
1290
|
-
await client.trackError({
|
|
1291
|
-
sessionId: sid,
|
|
1292
|
-
errorType: error instanceof Error ? error.name : "Error",
|
|
1293
|
-
errorMessage: error instanceof Error ? error.message : "Unknown error"
|
|
1294
|
-
});
|
|
1295
|
-
await client.completeSession({
|
|
1296
|
-
sessionId: sid,
|
|
1297
|
-
success: false,
|
|
1298
|
-
failureReason: error instanceof Error ? error.message : "Unknown error",
|
|
1299
|
-
durationMs
|
|
1300
|
-
});
|
|
1301
|
-
throw error;
|
|
1302
|
-
});
|
|
1303
|
-
}
|
|
1304
|
-
return wrappedObjectPromise;
|
|
1254
|
+
if (result.object) {
|
|
1255
|
+
const originalObjectPromise = result.object;
|
|
1256
|
+
result.object = originalObjectPromise.then(async (obj) => {
|
|
1257
|
+
const durationMs = Date.now() - startTime;
|
|
1258
|
+
const sid = await sessionPromise;
|
|
1259
|
+
if (sid) {
|
|
1260
|
+
let usage;
|
|
1261
|
+
try {
|
|
1262
|
+
usage = result.usage ? await result.usage : void 0;
|
|
1263
|
+
} catch {
|
|
1305
1264
|
}
|
|
1306
|
-
|
|
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
|
+
});
|
|
1279
|
+
}
|
|
1280
|
+
return obj;
|
|
1281
|
+
}).catch(async (error) => {
|
|
1282
|
+
const durationMs = Date.now() - startTime;
|
|
1283
|
+
const sid = await sessionPromise;
|
|
1284
|
+
if (sid) {
|
|
1285
|
+
await client.trackError({
|
|
1286
|
+
sessionId: sid,
|
|
1287
|
+
errorType: error instanceof Error ? error.name : "Error",
|
|
1288
|
+
errorMessage: error instanceof Error ? error.message : "Unknown error"
|
|
1289
|
+
});
|
|
1290
|
+
await client.completeSession({
|
|
1291
|
+
sessionId: sid,
|
|
1292
|
+
success: false,
|
|
1293
|
+
failureReason: error instanceof Error ? error.message : "Unknown error",
|
|
1294
|
+
durationMs
|
|
1295
|
+
});
|
|
1307
1296
|
}
|
|
1297
|
+
throw error;
|
|
1308
1298
|
});
|
|
1309
1299
|
}
|
|
1310
1300
|
return result;
|