Files
ai-kit/test/limits.test.js
T
GandClaude Opus 5 0462b74365 feat!: become ai-kit — the AI layer in one install (#5)
The owner of this fleet read the name `ai-ration` and could not tell what the
package did. That is not a cosmetic complaint. An unreadable name is a cost paid
at every install decision, and this package had exactly ONE adopter while five
repos that skipped it — botsmann, evig, kivvi, orangecat, truthseeker — were all
taken down together on 2026-08-26 by a retired model id, which is precisely the
failure `chain` and `catalog` exist to prevent. "Ration" named one of five
modules and hid the other four.

Renamed, and widened to match how an app actually thinks about this. Adding an
AI feature looks like one decision and is really four — which model, is it still
there, what do the 429s mean, who gets the free tier — plus filling forms. Get
any one wrong and it fails identically from outside: the assistant is broken.
AOZ is the proof: it adopted the form half, hand-rolled the rest because that
was a second decision nobody made, and then reported "KI-Assistent nicht
konfiguriert" on a deployment whose key was valid.

So `ai-forms` is re-exported from the root and from ./forms, ./react and
./server. It is NOT absorbed: it works, four apps run it, and it is genuinely
useful outside this fleet. Swallowing it would break four repos to satisfy a
filing system. React stays on its own subpath as an optional peer so importing
ai-kit on a server never pulls in a UI library.

threadkit and limitkit are deliberately NOT merged. Neither has anything to do
with AI — an app throttling its login form should not install a model catalogue
to do it.

Four new tests cover the merge itself, because the merge IS the feature: without
them a broken re-export quietly turns this back into ai-ration under a friendlier
name and nothing else here would notice.

BREAKING CHANGE: the package is now `ai-kit`. Import from 'ai-kit'.
The GitHub repo is renamed; existing `github:maonakamoto/ai-ration` installs
continue to resolve through GitHub's redirect.

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-26 14:33:08 +02:00

87 lines
4.1 KiB
JavaScript

/**
* The three kinds of 429 need OPPOSITE responses, so a misclassification is not
* a cosmetic error — it is the difference between recovering and making things
* worse. These are the real response bodies, not paraphrases.
*/
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { classifyRateLimit, retryAfterSeconds, humanizeWait, rateLimitMessage } from 'ai-kit';
const CAPACITY =
'Rate limit reached for model `llama-3.3-70b-versatile` on tokens per minute (TPM): Limit 12000, Used 11800, Requested 400. Please try again in 3.6s.';
const SIZE =
'Request too large for model `llama-3.1-8b-instant` on tokens per minute (TPM): Limit 6000, Requested 15041, please reduce your message size and try again.';
const DAILY =
'Rate limit reached for model `llama-3.3-70b-versatile` on tokens per day (TPD): Limit 100000, Used 99331, Requested 4589. Please try again in 56m26.88s.';
test('the three kinds are told apart by BODY, not status', () => {
assert.equal(classifyRateLimit(CAPACITY), 'capacity');
assert.equal(classifyRateLimit(SIZE), 'size');
assert.equal(classifyRateLimit(DAILY), 'daily');
});
test('DAILY wins over CAPACITY — it matches both wordings', () => {
// The daily body opens with the same "Rate limit reached" phrase as capacity,
// so a classifier that tests capacity first silently swallows every daily cap
// and then "helpfully" retries against an empty budget for the rest of the day.
assert.match(DAILY, /Rate limit reached/);
assert.equal(classifyRateLimit(DAILY), 'daily');
});
test('requests-per-day is treated as daily too', () => {
assert.equal(classifyRateLimit('Rate limit reached on requests per day (RPD): Limit 50'), 'daily');
});
test('an unrecognisable body degrades to CAPACITY, the safe guess', () => {
// Guessing "size" would shed context that was never the problem; guessing
// "daily" would abandon a turn that might well have succeeded.
assert.equal(classifyRateLimit('some vendor phrasing nobody has seen'), 'capacity');
assert.equal(classifyRateLimit(''), 'capacity');
});
test('the wait the provider named is parsed, in both shapes it emits', () => {
assert.equal(retryAfterSeconds(CAPACITY), 4); // 3.6s → ceil
assert.equal(retryAfterSeconds(DAILY), 3387); // 56m26.88s
assert.equal(retryAfterSeconds(SIZE), null, 'no stated wait must be null, not zero');
});
test('humanizeWait stays readable across the range', () => {
assert.equal(humanizeWait(4), '4s');
assert.equal(humanizeWait(3387), '57 minutes');
assert.equal(humanizeWait(7200), 'about 2 hours');
assert.equal(humanizeWait(null), null);
assert.equal(humanizeWait(0), null, 'a zero wait is no wait');
assert.equal(humanizeWait(-5), null);
});
test('the singular hour is REACHABLE — a dead branch is a lie about the output', () => {
// With the boundary at 90 minutes this was impossible: anything reaching the
// hours branch divided to >= 1.5, which rounds to 2. So "about 1 hour" could
// never print, and an hour-long wait was announced as two.
assert.equal(humanizeWait(3600), 'about 1 hour');
assert.equal(humanizeWait(4800), 'about 1 hour');
});
test('the message tells the user whether waiting can possibly help', () => {
// This is the whole point: "try again shortly" on an exhausted DAY invites
// exactly the retry that is guaranteed to fail for the next hour.
assert.match(rateLimitMessage(DAILY), /daily model quota is used up/);
assert.match(rateLimitMessage(DAILY), /resets in 57 minutes/);
assert.match(rateLimitMessage(CAPACITY), /rate-limited/);
assert.match(rateLimitMessage(CAPACITY), /retry in 4s/);
const size = rateLimitMessage(SIZE);
assert.match(size, /more context than the model allows/);
assert.doesNotMatch(size, /try again/i, 'retrying is not the fix for an oversized request');
});
test('the message is a CLAUSE the caller can embed', () => {
for (const body of [CAPACITY, SIZE, DAILY]) {
const msg = rateLimitMessage(body);
assert.doesNotMatch(msg, /^[A-Z]/, 'a leading capital reads wrong mid-sentence');
assert.doesNotMatch(msg, /\.$/, 'a trailing period double-punctuates the caller');
}
});