@convex-dev/ai-budget 0.0.2-alpha.4 → 0.0.2-alpha.5
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/README.md +67 -6
- package/dist/client/index.d.ts +318 -43
- package/dist/client/index.js +115 -68
- package/dist/component/_generated/component.d.ts +32 -0
- package/dist/component/lib.d.ts +73 -0
- package/dist/component/lib.js +304 -32
- package/dist/component/schema.d.ts +76 -3
- package/dist/component/schema.js +56 -2
- package/package.json +1 -1
- package/src/client/index.ts +199 -131
- package/src/component/_generated/component.ts +54 -3
- package/src/component/lib.test.ts +118 -0
- package/src/component/lib.ts +375 -48
- package/src/component/schema.ts +59 -2
|
@@ -70,6 +70,124 @@ describe("reserve / settle spend caps", () => {
|
|
|
70
70
|
});
|
|
71
71
|
});
|
|
72
72
|
|
|
73
|
+
async function settleWith(t: any, requestId: any, fields: any) {
|
|
74
|
+
await t.mutation(api.lib.finishRequest, { requestId, ...fields });
|
|
75
|
+
vi.useFakeTimers();
|
|
76
|
+
await t.finishAllScheduledFunctions(vi.runAllTimers);
|
|
77
|
+
vi.useRealTimers();
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
describe("monthly budgets", () => {
|
|
81
|
+
test("a tiny monthly cap blocks up front", async () => {
|
|
82
|
+
const t = convexTest(schema, modules);
|
|
83
|
+
await setUserLimits(t, "u", { monthlySpendLimitNanos: 1_000 });
|
|
84
|
+
const r = await start(t, { userId: "u" });
|
|
85
|
+
expect(r.allowed).toBe(false);
|
|
86
|
+
expect(r.code).toBe("user_monthly_spend_limit");
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
describe("cache-aware pricing", () => {
|
|
91
|
+
test("cached prompt tokens are billed at the discount, not full input", async () => {
|
|
92
|
+
const t = convexTest(schema, modules);
|
|
93
|
+
const r = await start(t, { userId: "u" });
|
|
94
|
+
// 1M prompt, ALL cached, 0 completion. gpt-4o-mini input $0.15/Mtok; the
|
|
95
|
+
// cache default is 10% of input → 0.1 * 150_000_000 = 15_000_000 nano.
|
|
96
|
+
await settleWith(t, r.requestId, {
|
|
97
|
+
promptTokens: 1_000_000,
|
|
98
|
+
completionTokens: 0,
|
|
99
|
+
cachedTokens: 1_000_000,
|
|
100
|
+
});
|
|
101
|
+
const req = (await t.query(api.lib.getRequest, { requestId: r.requestId }))!;
|
|
102
|
+
expect(req.costNanos).toBe(15_000_000);
|
|
103
|
+
expect(req.cachedTokens).toBe(1_000_000);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
test("an authoritative gateway cost overrides the token estimate", async () => {
|
|
107
|
+
const t = convexTest(schema, modules);
|
|
108
|
+
const r = await start(t, { userId: "u" });
|
|
109
|
+
await settleWith(t, r.requestId, {
|
|
110
|
+
promptTokens: 1_000_000,
|
|
111
|
+
completionTokens: 1_000_000,
|
|
112
|
+
costNanos: 12_345,
|
|
113
|
+
});
|
|
114
|
+
const req = (await t.query(api.lib.getRequest, { requestId: r.requestId }))!;
|
|
115
|
+
expect(req.costNanos).toBe(12_345);
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
describe("durable usage history", () => {
|
|
120
|
+
test("settled spend lands in a per-day usage row", async () => {
|
|
121
|
+
const t = convexTest(schema, modules);
|
|
122
|
+
const r = await start(t, { userId: "u" });
|
|
123
|
+
await settleWith(t, r.requestId, { promptTokens: 1_000_000, completionTokens: 1_000_000 });
|
|
124
|
+
const hist = await t.query(api.lib.usageHistory, {
|
|
125
|
+
dimension: "user",
|
|
126
|
+
value: "u",
|
|
127
|
+
period: "day",
|
|
128
|
+
});
|
|
129
|
+
expect(hist.length).toBe(1);
|
|
130
|
+
expect(hist[0].spendNanos).toBe(750_000_000); // $0.75
|
|
131
|
+
expect(hist[0].requests).toBe(1);
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
describe("manual adjustments", () => {
|
|
136
|
+
test("a credit reduces spend and is logged", async () => {
|
|
137
|
+
const t = convexTest(schema, modules);
|
|
138
|
+
const r = await start(t, { userId: "u" });
|
|
139
|
+
await settleWith(t, r.requestId, { promptTokens: 1_000_000, completionTokens: 1_000_000 });
|
|
140
|
+
await t.mutation(api.lib.adjustBucket, {
|
|
141
|
+
dimension: "user",
|
|
142
|
+
value: "u",
|
|
143
|
+
deltaNanos: -250_000_000,
|
|
144
|
+
reason: "goodwill credit",
|
|
145
|
+
});
|
|
146
|
+
const u = await userOf(t, "u");
|
|
147
|
+
expect(u.totalSpendNanos).toBe(500_000_000); // 750M - 250M
|
|
148
|
+
const log = await t.query(api.lib.listAdjustments, { dimension: "user", value: "u" });
|
|
149
|
+
expect(log.length).toBe(1);
|
|
150
|
+
expect(log[0].deltaNanos).toBe(-250_000_000);
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
describe("threshold alerts", () => {
|
|
155
|
+
test("crossing warnAtPct returns a notice but still admits", async () => {
|
|
156
|
+
const t = convexTest(schema, modules);
|
|
157
|
+
// One "hi" estimate is ~480_150 nano. Cap 800_000, warn at 50% (400_000).
|
|
158
|
+
await setUserLimits(t, "u", { dailySpendLimitNanos: 800_000, warnAtPct: 0.5 });
|
|
159
|
+
const r = await start(t, { userId: "u" });
|
|
160
|
+
expect(r.allowed).toBe(true);
|
|
161
|
+
expect(r.notices.length).toBeGreaterThan(0);
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
describe("concurrency cap", () => {
|
|
166
|
+
test("maxConcurrent blocks a second in-flight request", async () => {
|
|
167
|
+
const t = convexTest(schema, modules);
|
|
168
|
+
await setUserLimits(t, "u", { maxConcurrent: 1 });
|
|
169
|
+
const first = await start(t, { userId: "u" });
|
|
170
|
+
expect(first.allowed).toBe(true); // reserved, still pending
|
|
171
|
+
const second = await start(t, { userId: "u" });
|
|
172
|
+
expect(second.allowed).toBe(false);
|
|
173
|
+
expect(second.code).toBe("user_max_concurrent");
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
describe("tag-filtered request log", () => {
|
|
178
|
+
test("listRequests filters by a custom tag dimension", async () => {
|
|
179
|
+
const t = convexTest(schema, modules);
|
|
180
|
+
await start(t, { userId: "u", tags: [{ dimension: "customer", value: "acme" }] });
|
|
181
|
+
await start(t, { userId: "u", tags: [{ dimension: "customer", value: "globex" }] });
|
|
182
|
+
const acme = await t.query(api.lib.listRequests, {
|
|
183
|
+
dimension: "customer",
|
|
184
|
+
value: "acme",
|
|
185
|
+
});
|
|
186
|
+
expect(acme.length).toBe(1);
|
|
187
|
+
expect(acme[0].userId).toBe("u");
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
|
|
73
191
|
describe("tagged attribution buckets", () => {
|
|
74
192
|
test("a cap on a custom tag blocks, and settlement accrues to every bucket", async () => {
|
|
75
193
|
const t = convexTest(schema, modules);
|