mirror of
https://github.com/bitbaum/botsmann.git
synced 2026-12-05 05:38:59 +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>
62 lines
1.9 KiB
TypeScript
62 lines
1.9 KiB
TypeScript
// @vitest-environment node
|
|
import { NextRequest } from 'next/server';
|
|
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
|
|
const transcribe = vi.fn();
|
|
vi.mock('@bitbaum/ai-kit', async (importOriginal) => ({
|
|
...(await importOriginal<typeof import('@bitbaum/ai-kit')>()),
|
|
transcribe: (...args: unknown[]) => transcribe(...args),
|
|
}));
|
|
|
|
const { POST } = await import('@/app/api/transcribe/route');
|
|
|
|
function post(form?: FormData) {
|
|
return POST(
|
|
new NextRequest('http://localhost/api/transcribe', {
|
|
method: 'POST',
|
|
body: form ?? new FormData(),
|
|
headers: { 'x-forwarded-for': `10.0.0.${Math.floor(Math.random() * 250)}` },
|
|
}),
|
|
);
|
|
}
|
|
|
|
const take = () => {
|
|
const form = new FormData();
|
|
form.set('audio', new File([new Uint8Array([1, 2, 3])], 'take.webm', { type: 'audio/webm' }));
|
|
form.set('locale', 'de-CH');
|
|
return form;
|
|
};
|
|
|
|
describe('POST /api/transcribe', () => {
|
|
afterEach(() => {
|
|
vi.unstubAllEnvs();
|
|
transcribe.mockReset();
|
|
});
|
|
|
|
it('says dictation is unavailable without a speech key', async () => {
|
|
vi.stubEnv('GROQ_API_KEY', '');
|
|
const response = await post(take());
|
|
expect(response.status).toBe(503);
|
|
});
|
|
|
|
it('needs a recording', async () => {
|
|
vi.stubEnv('GROQ_API_KEY', 'test');
|
|
expect((await post()).status).toBe(400);
|
|
});
|
|
|
|
it('answers { text } in the shape chatkit reads', async () => {
|
|
vi.stubEnv('GROQ_API_KEY', 'test');
|
|
transcribe.mockResolvedValue({ text: ' Grüezi mitenand ' });
|
|
const response = await post(take());
|
|
expect(response.status).toBe(200);
|
|
expect(await response.json()).toEqual({ text: 'Grüezi mitenand' });
|
|
expect(transcribe.mock.calls[0][0]).toMatchObject({ language: 'de' });
|
|
});
|
|
|
|
it('drops the silence artefact instead of typing it', async () => {
|
|
vi.stubEnv('GROQ_API_KEY', 'test');
|
|
transcribe.mockResolvedValue({ text: 'Untertitelung des ZDF, 2020' });
|
|
expect(await (await post(take())).json()).toEqual({ text: '' });
|
|
});
|
|
});
|