mirror of
https://github.com/bitbaum/botsmann.git
synced 2026-12-05 15:58:36 +01:00
* wip: prose fields on chatkit DictationField (awaits chatkit 0.45) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmuMRAXYWNkfowAXVfi * feat(forms): prose fields on chatkit DictationField, with a working microphone - Bump @bitbaum/chatkit to ^0.45.0. - One app wrapper, components/forms/TextBox, around DictationField. Used by the bot builder (name, description, knowledge topic and content, system prompt), the custom-bot edit page (name, description, system prompt, greeting), the contact message, the demo intake questions, quick-create (name, relationship, memories) and the consultation form's react-hook-form message field ({...register('message')}). - app/api/transcribe: dictation server leg on ai-kit's speechChain and transcribe(), rate limited, recordings and transcripts neither stored nor logged. Every field and every in-page chat points voice at it, so the microphone works where the browser has no recogniser of its own. - Permissions-Policy allowed no microphone at all (microphone=()), which blocked every mic on the site; it now allows the site itself. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmuMRAXYWNkfowAXVfi --------- Co-authored-by: Claude <noreply@anthropic.com>
76 lines
2.5 KiB
TypeScript
76 lines
2.5 KiB
TypeScript
/**
|
|
* Guard: every route that spends money must be rate limited.
|
|
*
|
|
* Two routes once shipped without a limit — app/api/demo/chat (public and
|
|
* unauthenticated) and app/api/custom-bots/[id]/chat (anonymous callers, billed
|
|
* to the bot OWNER's key). Nothing failed, because nothing was checking. This
|
|
* test is that check: it reads the route files and fails on the next omission
|
|
* rather than after the next bill.
|
|
*/
|
|
import { readFileSync, readdirSync, statSync } from 'fs';
|
|
import { join } from 'path';
|
|
|
|
const API_DIR = join(process.cwd(), 'app', 'api');
|
|
|
|
/** Routes that call an LLM or speech provider and therefore cost money per request. */
|
|
const LLM_IMPORT = /from '@\/lib\/llm-client'|\btranscribe\(/;
|
|
|
|
/** Routes deliberately exempt, each with the reason it is safe. */
|
|
const EXEMPT: Record<string, string> = {
|
|
'consultations/route.ts': 'API-key gated and rate limited on a global key (see lib/rate-limit)',
|
|
};
|
|
|
|
function routeFiles(dir: string): string[] {
|
|
const out: string[] = [];
|
|
for (const entry of readdirSync(dir)) {
|
|
const full = join(dir, entry);
|
|
if (statSync(full).isDirectory()) out.push(...routeFiles(full));
|
|
else if (entry === 'route.ts') out.push(full);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
function stripComments(source: string): string {
|
|
return source.replace(/\/\*[\s\S]*?\*\//g, '').replace(/(^|[^:])\/\/[^\n]*/g, '$1');
|
|
}
|
|
|
|
describe('rate limit coverage', () => {
|
|
const files = routeFiles(API_DIR);
|
|
|
|
it('finds the API routes', () => {
|
|
expect(files.length).toBeGreaterThan(20);
|
|
});
|
|
|
|
it('every LLM-calling route enforces a rate limit', () => {
|
|
const unprotected: string[] = [];
|
|
|
|
for (const file of files) {
|
|
const rel = file.slice(API_DIR.length + 1);
|
|
if (EXEMPT[rel]) continue;
|
|
|
|
const source = stripComments(readFileSync(file, 'utf-8'));
|
|
if (!LLM_IMPORT.test(source)) continue;
|
|
|
|
const enforced = /enforceRateLimit\s*\(/.test(source);
|
|
if (!enforced) unprotected.push(rel);
|
|
}
|
|
|
|
expect(unprotected).toEqual([]);
|
|
});
|
|
|
|
it('rate limits come from the shared SSOT, not inline magic numbers', () => {
|
|
const offenders: string[] = [];
|
|
|
|
for (const file of files) {
|
|
const source = stripComments(readFileSync(file, 'utf-8'));
|
|
// A route must not build its own limiter from limitkit — every budget
|
|
// is a named bucket in lib/rate-limit.ts, or it stops living in one place.
|
|
if (/from 'limitkit'/.test(source)) {
|
|
offenders.push(file.slice(API_DIR.length + 1));
|
|
}
|
|
}
|
|
|
|
expect(offenders).toEqual(Object.keys(EXEMPT).filter((f) => offenders.includes(f)));
|
|
});
|
|
});
|