Files
joshuaandClaude Opus 5.5 7cd59ed651 AI status: tokens used and an estimated allowance in tokens, for churches (2.0.1)
Churches see their AI use as input/output tokens; rand stays on the Hub.
The status also carries usedInputTokens/usedOutputTokens and
capInputTokens/capOutputTokens (half the cap priced as input, half as
output). Cents unchanged for older installs. Protocol §3b updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 10:58:49 +02:00

233 lines
14 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// The AI proxy (utils/ai.js): pricing, the cap and the hold, with an in-memory database and a
// mocked Claude.
const mockRows = [];
const mockState = { licence: null, installs: [{ id: 'inst_1' }] };
const mockDb = {
licence: { findUnique: jest.fn(async () => mockState.licence) },
// Hub 2.0: the cap and markup are the church's (kept on mockState.licence here for the tests), and
// its installs share the cap.
customer: { findUnique: jest.fn(async () => ({ aiMonthlyCapCents: mockState.licence.aiMonthlyCapCents, aiMarkupPercent: mockState.licence.aiMarkupPercent })) },
install: { findMany: jest.fn(async () => mockState.installs) },
aiUsage: {
aggregate: jest.fn(async ({ where }) => {
const cutoff = where.OR[1].createdAt.gt;
const rows = mockRows
.filter(r => where.installId.in.includes(r.installId) && r.month === where.month)
.filter(r => r.status !== 'reserved' || r.createdAt > cutoff);
const sum = k => rows.reduce((n, r) => n + (r[k] || 0), 0);
return { _sum: { chargedMillicents: sum('chargedMillicents'), inputTokens: sum('inputTokens'), outputTokens: sum('outputTokens'), cacheReadTokens: sum('cacheReadTokens'), cacheWriteTokens: sum('cacheWriteTokens') } };
}),
create: jest.fn(async ({ data }) => { const row = { id: `u${mockRows.length + 1}`, createdAt: new Date(), ...data }; mockRows.push(row); return row; }),
update: jest.fn(async ({ where, data }) => Object.assign(mockRows.find(r => r.id === where.id), data)),
},
$executeRaw: jest.fn(async () => 0),
$transaction: jest.fn(async fn => fn(mockDb)),
};
jest.mock('../src/config/db', () => mockDb);
const mockSettings = {
ai_model: 'claude-haiku-5-5', ai_input_usd_per_mtok: '0.10', ai_output_usd_per_mtok: '0.50', ai_usd_zar: '20', ai_markup_percent: '40',
};
jest.mock('../src/utils/settings', () => ({ getSettings: jest.fn(async () => mockSettings), aiApiKey: jest.fn(async () => 'sk-test') }));
const mockCreate = jest.fn();
jest.mock('@anthropic-ai/sdk', () => {
const actual = jest.requireActual('@anthropic-ai/sdk');
function Anthropic() { return { messages: { create: mockCreate } }; }
for (const k of ['APIError', 'RateLimitError', 'AuthenticationError', 'PermissionDeniedError', 'BadRequestError']) Anthropic[k] = actual[k];
return Anthropic;
});
const ai = require('../src/utils/ai');
const install = { id: 'inst_1', licenceId: 'lic_1', customerId: 'c1' };
const price = { model: 'claude-haiku-5-5', inUsd: 0.1, outUsd: 0.5, cacheReadUsd: 0.01, usdZar: 20, markup: 40 };
const body = { protocolVersion: 1, helper: 'draft_message', system: 'Write a friendly message.', input: 'Bring a sleeping bag.', maxTokens: 1000 };
const reply = (usage, extra = {}) => ({ model: 'claude-haiku-5-5', stop_reason: 'end_turn', content: [{ type: 'text', text: 'Hi all!' }], usage, ...extra });
beforeEach(() => {
mockRows.length = 0;
mockState.licence = { id: 'lic_1', status: 'active', expiresAt: null, graceDays: 14, aiMonthlyCapCents: 5000, aiMarkupPercent: null };
mockState.installs = [{ id: 'inst_1' }];
mockCreate.mockReset();
});
describe('months', () => {
test('the month is South African time', () => {
expect(ai.monthOf(new Date('2026-10-31T21:59:00Z'))).toBe('2026-10');
expect(ai.monthOf(new Date('2026-10-31T22:00:00Z'))).toBe('2026-11');
});
test('renews on the 1st of the next month, across the year end', () => {
expect(ai.renewsOn(new Date('2026-11-15T10:00:00Z'))).toBe('2026-12-01');
expect(ai.renewsOn(new Date('2026-12-31T23:00:00Z'))).toBe('2027-02-01');
});
});
describe('pricing', () => {
test('off without a key or an exchange rate; the licence markup wins', () => {
expect(ai.pricing(mockSettings, mockState.licence, false)).toBeNull();
expect(ai.pricing({ ...mockSettings, ai_usd_zar: '' }, mockState.licence, true)).toBeNull();
expect(ai.pricing(mockSettings, mockState.licence, true)).toEqual(price);
expect(ai.pricing(mockSettings, { aiMarkupPercent: 0 }, true).markup).toBe(0);
});
test('Sonnet 5.5 is the default model, at its list prices', () => {
const { DEFAULTS } = jest.requireActual('../src/utils/settings');
expect(DEFAULTS).toMatchObject({ ai_model: 'claude-sonnet-5-5', ai_input_usd_per_mtok: '2', ai_output_usd_per_mtok: '10' });
});
test('blank prices use the built-in list; cache reads are priced per model', () => {
const sonnet = ai.pricing({ ...mockSettings, ai_model: 'claude-sonnet-5-5', ai_input_usd_per_mtok: '', ai_output_usd_per_mtok: '' }, mockState.licence, true);
expect(sonnet).toMatchObject({ inUsd: 2, outUsd: 10, cacheReadUsd: 0.2 });
// Opus 5.5 reads its cache at 0.05× the input price.
expect(ai.pricing({ ...mockSettings, ai_model: 'claude-opus-5-5', ai_input_usd_per_mtok: '4', ai_output_usd_per_mtok: '20' }, mockState.licence, true).cacheReadUsd).toBe(0.2);
});
test('a model the Hub doesn\'t know needs its prices; cache reads at 0.1× of an edited price', () => {
expect(ai.pricing({ ...mockSettings, ai_model: 'claude-new-model', ai_input_usd_per_mtok: '', ai_output_usd_per_mtok: '' }, mockState.licence, true)).toBeNull();
const other = ai.pricing({ ...mockSettings, ai_model: 'claude-new-model', ai_input_usd_per_mtok: '3', ai_output_usd_per_mtok: '15' }, mockState.licence, true);
expect(other.inUsd).toBe(3);
expect(other.cacheReadUsd).toBeCloseTo(0.3);
// Sonnet with a changed input price: its listed cache price no longer applies.
expect(ai.pricing({ ...mockSettings, ai_model: 'claude-sonnet-5-5', ai_input_usd_per_mtok: '2.5', ai_output_usd_per_mtok: '10' }, mockState.licence, true).cacheReadUsd).toBe(0.25);
});
test('cost in millicents: input, cache writes at 1.25×, reads at 0.1×, output; markup on top', () => {
// 1M input tokens at $0.10 × R20 = R2 = 200 000 millicents.
expect(ai.costOf({ inputTokens: 1e6 }, price)).toEqual({ real: 200000, charged: 280000 });
expect(ai.costOf({ cacheWriteTokens: 1e6 }, price).real).toBe(250000);
expect(ai.costOf({ cacheReadTokens: 1e6 }, price).real).toBe(20000);
expect(ai.costOf({ outputTokens: 1e6 }, price).real).toBe(1000000);
// A typical call is a fraction of a cent, rounded up.
expect(ai.costOf({ inputTokens: 3000, outputTokens: 500 }, price)).toEqual({ real: 1100, charged: 1540 });
});
test('the worst case is never less than any real outcome', () => {
const est = ai.estimateInputTokens(body.system, body.input);
const worst = ai.worstCase(est, 1000, price);
expect(worst).toBeGreaterThanOrEqual(ai.costOf({ inputTokens: est, outputTokens: 1000 }, price).charged);
expect(worst).toBeGreaterThanOrEqual(ai.costOf({ cacheReadTokens: est, outputTokens: 1000 }, price).charged);
});
});
describe('status', () => {
const now = new Date('2026-11-10T10:00:00Z');
test('hidden for cap R0, an inactive licence or no Hub set-up', () => {
expect(ai.statusOf({ licence: { ...mockState.licence, aiMonthlyCapCents: 0 }, price, used: null, now }).enabled).toBe(false);
expect(ai.statusOf({ licence: { ...mockState.licence, status: 'suspended' }, price, used: null, now }).enabled).toBe(false);
expect(ai.statusOf({ licence: mockState.licence, price: null, used: null, now }).enabled).toBe(false);
});
test('the allowance as tokens: half the cap priced as input, half as output (Hub 2.0.1)', () => {
// R50 at Haiku's $0.10 / $0.50, R20 to the dollar, 40% markup: R25 buys 8.93M input or 1.79M output.
expect(ai.allowanceTokens(5000, price)).toEqual({ capInputTokens: 8928000, capOutputTokens: 1785000 });
expect(ai.allowanceTokens(0, price)).toEqual({ capInputTokens: 0, capOutputTokens: 0 });
expect(ai.allowanceTokens(5000, null)).toEqual({ capInputTokens: 0, capOutputTokens: 0 });
const s = ai.statusOf({ licence: mockState.licence, price, used: { chargedMillicents: 0, inputTokens: 1840, outputTokens: 420 }, now });
expect(s).toMatchObject({ usedInputTokens: 1840, usedOutputTokens: 420, capInputTokens: 8928000, capOutputTokens: 1785000 });
});
test('an expired licence still has AI during its grace days', () => {
const expired = { ...mockState.licence, expiresAt: new Date('2026-11-01T00:00:00Z') };
expect(ai.licenceAllows(expired, now)).toBe(true);
expect(ai.licenceAllows(expired, new Date('2026-11-16T00:00:00Z'))).toBe(false);
});
test('cap reached once there is no room for the smallest call', () => {
const s = ai.statusOf({ licence: mockState.licence, price, used: { chargedMillicents: 1000 * 1000 }, now });
expect(s).toMatchObject({ enabled: true, capReached: false, capCents: 5000, usedCents: 1000, usedPercent: 20, renewsOn: '2026-12-01' });
expect(ai.statusOf({ licence: mockState.licence, price, used: { chargedMillicents: 5000 * 1000 - 100 }, now }).capReached).toBe(true);
});
});
describe('requests', () => {
test('checks the helper, sizes and effort', () => {
expect(() => ai.cleanRequest({ ...body, helper: 'Draft Event' })).toThrow(/helper/);
expect(() => ai.cleanRequest({ ...body, maxTokens: 9000 })).toThrow(/maxTokens/);
expect(() => ai.cleanRequest({ ...body, effort: 'max' })).toThrow(/effort/);
expect(() => ai.cleanRequest({ ...body, schema: [] })).toThrow(/schema/);
expect(() => ai.cleanRequest({ ...body, input: 'x'.repeat(240000) })).toThrow(expect.objectContaining({ code: 'AI_INPUT_TOO_LARGE' }));
expect(ai.cleanRequest(body)).toMatchObject({ helper: 'draft_message', effort: 'low', schema: null });
});
});
describe('complete', () => {
test('calls Claude with the key, caches the instructions, meters the real use', async () => {
mockCreate.mockResolvedValue(reply({ input_tokens: 40, output_tokens: 200, cache_read_input_tokens: 0, cache_creation_input_tokens: 0 }));
const r = await ai.complete(install, body);
expect(r.output).toBe('Hi all!');
expect(mockCreate.mock.calls[0][0]).toMatchObject({
model: 'claude-haiku-5-5', max_tokens: 1000,
system: [{ type: 'text', text: body.system, cache_control: { type: 'ephemeral' } }],
output_config: { effort: 'low' },
});
expect(mockRows).toHaveLength(1);
expect(mockRows[0]).toMatchObject({ status: 'done', helper: 'draft_message', inputTokens: 40, outputTokens: 200, markupPercent: 40 });
expect(mockRows[0].chargedMillicents).toBe(ai.costOf({ inputTokens: 40, outputTokens: 200 }, price).charged);
expect(r.status).toMatchObject({ enabled: true, capReached: false });
});
test('a schema asks for JSON and the reply is parsed', async () => {
mockCreate.mockResolvedValue(reply({ input_tokens: 40, output_tokens: 20 }, { content: [{ type: 'text', text: '{"title":"Camp"}' }] }));
const schema = { type: 'object', properties: { title: { type: 'string' } }, required: ['title'], additionalProperties: false };
const r = await ai.complete(install, { ...body, schema });
expect(r.output).toEqual({ title: 'Camp' });
expect(mockCreate.mock.calls[0][0].output_config.format).toEqual({ type: 'json_schema', schema });
});
test('refused before calling Claude when the worst case would pass the cap', async () => {
mockState.licence.aiMonthlyCapCents = 1;
await expect(ai.complete(install, body)).rejects.toMatchObject({ status: 402, code: 'AI_CAP_REACHED', aiStatus: expect.objectContaining({ capReached: true }) });
expect(mockCreate).not.toHaveBeenCalled();
expect(mockRows).toHaveLength(0);
});
test('a running call holds its worst case, so a second one at once is refused', async () => {
const big = { ...body, maxTokens: 8000 };
const worst = ai.worstCase(ai.estimateInputTokens(big.system, big.input, null), 8000, price);
// Room for one worst case, not two.
mockState.licence.aiMonthlyCapCents = Math.floor((worst * 1.5) / 1000);
expect(mockState.licence.aiMonthlyCapCents * 1000).toBeGreaterThan(worst);
let finish;
mockCreate.mockReturnValue(new Promise(r => { finish = r; }));
const first = ai.complete(install, big);
while (!mockCreate.mock.calls.length) await new Promise(r => setImmediate(r));
await expect(ai.complete(install, big)).rejects.toMatchObject({ code: 'AI_CAP_REACHED' });
finish(reply({ input_tokens: 40, output_tokens: 10 }));
await expect(first).resolves.toMatchObject({ output: 'Hi all!' });
});
test('a failed call costs nothing; a refusal still pays for its tokens', async () => {
mockCreate.mockRejectedValueOnce(new Error('network down'));
await expect(ai.complete(install, body)).rejects.toMatchObject({ status: 503, code: 'AI_UNAVAILABLE' });
expect(mockRows[0]).toMatchObject({ status: 'failed', chargedMillicents: 0 });
mockCreate.mockResolvedValueOnce(reply({ input_tokens: 40, output_tokens: 5 }, { stop_reason: 'refusal' }));
await expect(ai.complete(install, body)).rejects.toMatchObject({ status: 422, code: 'AI_REFUSED' });
expect(mockRows[1].status).toBe('failed');
expect(mockRows[1].chargedMillicents).toBeGreaterThan(0);
});
test('AI off on the licence, or a suspended licence', async () => {
mockState.licence.aiMonthlyCapCents = 0;
await expect(ai.complete(install, body)).rejects.toMatchObject({ status: 403, code: 'AI_OFF' });
mockState.licence = { ...mockState.licence, aiMonthlyCapCents: 5000, status: 'suspended' };
await expect(ai.complete(install, body)).rejects.toMatchObject({ status: 403, code: 'LICENCE_INACTIVE' });
expect(mockCreate).not.toHaveBeenCalled();
});
});
describe('the cap is the church\'s (Hub 2.0)', () => {
test('what its other installs used this month counts against it', async () => {
mockState.installs = [{ id: 'inst_1' }, { id: 'inst_website' }];
mockRows.push({ id: 'w1', installId: 'inst_website', month: ai.monthOf(), status: 'done', chargedMillicents: 4999 * 1000, createdAt: new Date() });
const status = await ai.statusFor(install);
expect(status).toMatchObject({ enabled: true, capReached: true, usedCents: 4999 });
await expect(ai.complete(install, body)).rejects.toMatchObject({ code: 'AI_CAP_REACHED' });
expect(mockCreate).not.toHaveBeenCalled();
});
test('another church\'s use doesn\'t count', async () => {
mockRows.push({ id: 'o1', installId: 'inst_other_church', month: ai.monthOf(), status: 'done', chargedMillicents: 4999 * 1000, createdAt: new Date() });
expect((await ai.statusFor(install)).usedCents).toBe(0);
});
});