@rpcbase/agents-sdk 0.21.0 → 0.22.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/api/chat/handler.d.ts.map +1 -1
- package/dist/api/chat/runtimeConfig.d.ts +7 -0
- package/dist/api/chat/runtimeConfig.d.ts.map +1 -1
- package/dist/api/chat/toolApproval.d.ts.map +1 -1
- package/dist/api/chat/usageAccumulator.d.ts +3 -0
- package/dist/api/chat/usageAccumulator.d.ts.map +1 -0
- package/dist/api/chatDb.d.ts +5 -0
- package/dist/api/chatDb.d.ts.map +1 -1
- package/dist/client-routes.browser.js +35 -33
- package/dist/client-routes.d.ts +3 -1
- package/dist/client-routes.d.ts.map +1 -1
- package/dist/client-routes.js +35 -33
- package/dist/{handler-DS4HXmLA.js → handler-D6etcNwD.js} +567 -519
- package/dist/{handler-akLHP-CG.js → handler-Qw_JrLDo.js} +532 -457
- package/dist/lib/db/schema.d.ts +5 -0
- package/dist/lib/db/schema.d.ts.map +1 -1
- package/dist/models/RBAgentsUsageEvent.d.ts +5 -0
- package/dist/models/RBAgentsUsageEvent.d.ts.map +1 -1
- package/dist/models.browser.js +5 -0
- package/dist/models.js +33 -0
- package/dist/pages/ChatNewPage.d.ts +3 -1
- package/dist/pages/ChatNewPage.d.ts.map +1 -1
- package/dist/pages/ChatPage.d.ts +3 -1
- package/dist/pages/ChatPage.d.ts.map +1 -1
- package/dist/routes.browser.js +1 -1
- package/dist/routes.js +1 -1
- package/dist/runtime.d.ts +2 -2
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +1 -1
- package/dist/tailwind-source.js +1 -1
- package/dist/usage.d.ts +55 -0
- package/dist/usage.d.ts.map +1 -0
- package/dist/usage.js +124 -0
- package/package.json +6 -1
package/dist/usage.js
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { models as e } from "@rpcbase/db";
|
|
2
|
+
//#region src/usage.ts
|
|
3
|
+
var t = 1e6, n = 1e6, r = {
|
|
4
|
+
requestCount: 0,
|
|
5
|
+
inputTokens: 0,
|
|
6
|
+
outputTokens: 0,
|
|
7
|
+
totalTokens: 0,
|
|
8
|
+
billingAmountMicros: 0
|
|
9
|
+
}, i = (e) => typeof e != "number" || !Number.isFinite(e) || e <= 0 ? 0 : Math.trunc(e), a = (e) => {
|
|
10
|
+
if (!e || typeof e != "object" || Array.isArray(e)) return;
|
|
11
|
+
let t = e, n = t.totalUsage ?? t.usage;
|
|
12
|
+
if (n !== void 0) return n;
|
|
13
|
+
if (!t.error || typeof t.error != "object" || Array.isArray(t.error)) return;
|
|
14
|
+
let r = t.error;
|
|
15
|
+
return r.totalUsage ?? r.usage;
|
|
16
|
+
}, o = (e, t) => {
|
|
17
|
+
if (typeof e != "number" || !Number.isSafeInteger(e) || e < 0) throw Error(`${t} must be a non-negative safe integer.`);
|
|
18
|
+
return e;
|
|
19
|
+
}, s = (e) => {
|
|
20
|
+
let t = typeof e == "string" ? e.trim().toUpperCase() : "";
|
|
21
|
+
if (!/^[A-Z]{3}$/.test(t)) throw Error("Agent usage billing currency must be a three-letter ISO code.");
|
|
22
|
+
return t;
|
|
23
|
+
}, c = (e) => {
|
|
24
|
+
let t = typeof e == "string" ? e.trim() : "";
|
|
25
|
+
if (!t) throw Error("Agent usage billing price version is required.");
|
|
26
|
+
return t;
|
|
27
|
+
}, l = (e, t) => {
|
|
28
|
+
let r = i(e.inputTokens), a = i(e.outputTokens), l = o(t.inputPriceMicrosPerMillion, "inputPriceMicrosPerMillion"), u = o(t.outputPriceMicrosPerMillion, "outputPriceMicrosPerMillion"), d = (r * l + a * u) / n, f = Math.round(d);
|
|
29
|
+
if (!Number.isSafeInteger(f)) throw Error("Agent usage billing amount exceeds the supported integer range.");
|
|
30
|
+
return {
|
|
31
|
+
billingCurrency: s(t.currency),
|
|
32
|
+
billingPriceVersion: c(t.version),
|
|
33
|
+
inputPriceMicrosPerMillion: l,
|
|
34
|
+
outputPriceMicrosPerMillion: u,
|
|
35
|
+
billingAmountMicros: f
|
|
36
|
+
};
|
|
37
|
+
}, u = (e) => Number.isFinite(e) ? e / t : 0, d = (e) => {
|
|
38
|
+
let t = typeof e == "string" ? e.trim() : "";
|
|
39
|
+
if (!t) return "UTC";
|
|
40
|
+
try {
|
|
41
|
+
return new Intl.DateTimeFormat("en", { timeZone: t }).format(), t;
|
|
42
|
+
} catch {
|
|
43
|
+
return "UTC";
|
|
44
|
+
}
|
|
45
|
+
}, f = (e) => ({
|
|
46
|
+
requestCount: { $sum: 1 },
|
|
47
|
+
inputTokens: { $sum: { $ifNull: ["$inputTokens", 0] } },
|
|
48
|
+
outputTokens: { $sum: { $ifNull: ["$outputTokens", 0] } },
|
|
49
|
+
totalTokens: { $sum: { $ifNull: ["$totalTokens", { $add: [{ $ifNull: ["$inputTokens", 0] }, { $ifNull: ["$outputTokens", 0] }] }] } },
|
|
50
|
+
billingAmountMicros: { $sum: { $cond: [
|
|
51
|
+
{ $eq: ["$billingCurrency", e] },
|
|
52
|
+
{ $ifNull: ["$billingAmountMicros", 0] },
|
|
53
|
+
0
|
|
54
|
+
] } }
|
|
55
|
+
}), p = (e) => {
|
|
56
|
+
let t = i(e?.inputTokens), n = i(e?.outputTokens), r = e?.totalTokens === void 0 || e.totalTokens === null ? t + n : i(e.totalTokens);
|
|
57
|
+
return {
|
|
58
|
+
requestCount: i(e?.requestCount),
|
|
59
|
+
inputTokens: t,
|
|
60
|
+
outputTokens: n,
|
|
61
|
+
totalTokens: r,
|
|
62
|
+
billingAmountMicros: i(e?.billingAmountMicros)
|
|
63
|
+
};
|
|
64
|
+
}, m = async (t) => {
|
|
65
|
+
let n = t.tenantId.trim(), i = t.workspaceId.trim();
|
|
66
|
+
if (!n || !i) throw Error("Agent usage summary requires tenantId and workspaceId.");
|
|
67
|
+
if (!(t.startAt instanceof Date) || !Number.isFinite(t.startAt.getTime())) throw Error("Agent usage summary startAt must be a valid date.");
|
|
68
|
+
if (!(t.endAt instanceof Date) || !Number.isFinite(t.endAt.getTime())) throw Error("Agent usage summary endAt must be a valid date.");
|
|
69
|
+
if (t.startAt >= t.endAt) throw Error("Agent usage summary endAt must be after startAt.");
|
|
70
|
+
let a = s(t.currency ?? "EUR"), o = f(a), [c] = await (await e.get("RBAgentsUsageEvent", t.ctx)).aggregate([{ $match: {
|
|
71
|
+
tenantId: n,
|
|
72
|
+
workspaceId: i,
|
|
73
|
+
createdAt: {
|
|
74
|
+
$gte: t.startAt,
|
|
75
|
+
$lt: t.endAt
|
|
76
|
+
}
|
|
77
|
+
} }, { $facet: {
|
|
78
|
+
summary: [{ $group: {
|
|
79
|
+
_id: null,
|
|
80
|
+
...o
|
|
81
|
+
} }],
|
|
82
|
+
dailyUsage: [{ $group: {
|
|
83
|
+
_id: { $dateToString: {
|
|
84
|
+
date: "$createdAt",
|
|
85
|
+
format: "%Y-%m-%d",
|
|
86
|
+
timezone: d(t.timeZone)
|
|
87
|
+
} },
|
|
88
|
+
...o
|
|
89
|
+
} }, { $sort: { _id: 1 } }],
|
|
90
|
+
byModel: [{ $group: {
|
|
91
|
+
_id: {
|
|
92
|
+
provider: "$provider",
|
|
93
|
+
model: "$model"
|
|
94
|
+
},
|
|
95
|
+
...o
|
|
96
|
+
} }, { $sort: {
|
|
97
|
+
billingAmountMicros: -1,
|
|
98
|
+
inputTokens: -1,
|
|
99
|
+
outputTokens: -1,
|
|
100
|
+
"_id.model": 1
|
|
101
|
+
} }]
|
|
102
|
+
} }]);
|
|
103
|
+
return {
|
|
104
|
+
currency: a,
|
|
105
|
+
summary: c?.summary?.[0] ? p(c.summary[0]) : { ...r },
|
|
106
|
+
dailyUsage: (c?.dailyUsage ?? []).flatMap((e) => {
|
|
107
|
+
let t = typeof e._id == "string" ? e._id : "";
|
|
108
|
+
return t ? [{
|
|
109
|
+
date: t,
|
|
110
|
+
...p(e)
|
|
111
|
+
}] : [];
|
|
112
|
+
}),
|
|
113
|
+
byModel: (c?.byModel ?? []).flatMap((e) => {
|
|
114
|
+
let t = typeof e._id?.provider == "string" ? e._id.provider : "", n = typeof e._id?.model == "string" ? e._id.model : "";
|
|
115
|
+
return n ? [{
|
|
116
|
+
provider: t,
|
|
117
|
+
model: n,
|
|
118
|
+
...p(e)
|
|
119
|
+
}] : [];
|
|
120
|
+
})
|
|
121
|
+
};
|
|
122
|
+
};
|
|
123
|
+
//#endregion
|
|
124
|
+
export { u as agentUsageMicrosToMajor, l as calculateAgentUsageBilling, m as getAgentUsageSummary, a as getReportedAgentUsage };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rpcbase/agents-sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.22.0",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public",
|
|
6
6
|
"registry": "https://registry.npmjs.org/"
|
|
@@ -65,6 +65,11 @@
|
|
|
65
65
|
"types": "./dist/runtime.d.ts",
|
|
66
66
|
"default": "./dist/runtime.js"
|
|
67
67
|
},
|
|
68
|
+
"./usage": {
|
|
69
|
+
"import": "./dist/usage.js",
|
|
70
|
+
"types": "./dist/usage.d.ts",
|
|
71
|
+
"default": "./dist/usage.js"
|
|
72
|
+
},
|
|
68
73
|
"./api-catalog-search": {
|
|
69
74
|
"import": "./dist/api-catalog-search.js",
|
|
70
75
|
"types": "./dist/api-catalog-search.d.ts",
|