* ui: add a voice engine for capture, peaks and transcription Voice input needs a live waveform, a clip that can be sent and drawn, and a transcript, whether the model streams text while the user speaks or not. This adds the engine for it in src/lib/voice, with no imports of app stores or components, so it can move into a package later. - VoiceCapture: one microphone start gives live levels (RMS on a dB curve, fixed history, newest last), PCM16 mono chunks at 16 kHz from an inline AudioWorklet (ScriptProcessor fallback), and on stop a 16-bit mono WAV clip with its duration and peaks. Tracks, nodes and the context are released on stop, cancel, a failed start and page hide. - computePeaks / computePeaksFromAudio: normalised peaks of samples, a buffer, or a stored base64 or Blob file. - transcribeClip: POST /v1/audio/transcriptions, with the server's own error message on failure. - RealtimeTranscriber: the /v1/realtime?intent=transcription socket, queueing audio until session.created, a final commit, and a typed RealtimeUnsupportedError when the model cannot stream or the socket is refused (a browser websocket cannot send the API key header). - startVoiceSession and VoiceRecorder: try realtime, fall back to one HTTP transcription of the clip, and expose levels and live text. Assisted-by: Claude (Opus 5.5 Medium) * ui: add voice waveform and transcript components to Llama UI Voice input needs three visuals that the app can feed with its own recording and transcription logic: the live waveform while recording, a playable waveform of a recorded clip, and a clip with its transcript that opens on hover, keyboard focus or a tap. VoiceWaveformLive draws input levels as many bars as fit the width, newest on the right, and holds still with reduced motion. VoiceWaveform plays a clip through its own audio element, fills the played part in the primary colour, seeks by press, drag and arrow keys (a slider) and pauses any other clip that was playing. VoiceTranscriptReveal keeps the transcript in the DOM for screen readers while it is closed, and shows transcribing and error states. The waveform math (resampling peaks, bar capacity, seek and time formatting) is in src/lib/voice with unit tests. The components are exported from the package root and from @ggml-org/llama-ui/voice, which Llama Chat resolves to the sources like the other entry points. Assisted-by: Claude (Opus 5.5 Medium) * ui: add stories and tests for the voice components Storybook gets UI/Blocks/Voice: the live waveform fed by a fake microphone meter in a chat form row, the clip waveform in its sizes and states, and the transcript reveal in each state and on touch. The demo audio is a WAV synthesised in the story, so no binary file is added. The play functions check seeking by keys and by press, the reveal on hover, keyboard focus and a tap, and a clip without audio. Pressing play needs a real click before a browser plays audio, so a client test covers play and pause and one clip pausing another. Assisted-by: Claude (Opus 5.5 Medium) * ui: store voice messages with their transcript and send it to models that cannot hear A voice message recorded in the chat form is an audio attachment like an uploaded file, plus what the app needs to draw and read it. The audio attachment gets four optional fields: voice (recorded in the app), transcript, peaks and durationMs. Stored rows, export and import carry them as they are, so no migration is needed and older messages keep working. The transcript is for reading. A model with audio input still gets the audio alone; a model without it (the user switched models later) gets the transcript as a labelled text part in place of the audio. When the model's modalities are not known yet, or there is no transcript, the audio goes as before. A transcript often comes back after the message was sent. The new voiceTranscriptsStore follows each pending transcript by the clip's name, hands it to a send that happens after it arrived, and otherwise writes it into the stored attachment of the message it went out with, in the open chat or, if the user left it, in the database. Copying a message now includes its voice transcript. Assisted-by: Claude (Opus 5.5 Medium) * ui: add Voice settings and resolve what the microphone does per model The user should not have to think about which kind of voice input is in use, so the app decides it from the conversation's model and two settings. resolveVoiceMode is a small pure function: with Voice input on Automatic and a chat model that has audio input, a recording is a voice message, transcribed by that same model so a router swaps nothing. Otherwise it is dictation, with the chat model when it can hear, else, in router mode, with the chosen transcription model. When no model can transcribe, the microphone is unavailable and says why. Settings get a Voice section of their own rather than more rows in General, which is already long: these four belong together and one of them lists the server's models. - Voice input: Automatic (default) or Always dictate. - Transcription model: Same as chat model (default) or, in router mode, any model with audio input; on a single-model server it only says whether that model can transcribe. It notes that dictating with another model loads it beside the chat model. - Transcribe voice messages: on by default. - Transcription language: empty for automatic. The keys live in the settings registry like the others, so defaults, storage, reset and export need nothing new. The model picker is drawn by SettingsChatVoiceTab, the way Extensions and Tools draw their sections. Assisted-by: Claude (Opus 5.5 Medium) Co-authored-by: Nicolas Mowen <14866235+NickM-27@users.noreply.github.com> * ui: record voice messages and dictation from the chat form The microphone now does what the conversation's model allows, without the user choosing a mode: its label says "Send a voice message" or "Dictate", and when no model can take audio it is disabled and says why (shown in router mode only when some model there has audio input, so the reason can be acted on; a single-model server without audio keeps the send button as before). The microphone errors keep their warning state and toast, now also for the voice engine's own capture errors. Recording goes through the voice engine's VoiceRecorder instead of the MediaRecorder flow. While it records, the actions row becomes a recording row: cancel, the live waveform, the elapsed time and stop. Escape or cancel drops the recording. The form keeps its two rows while recording, so the message stays visible on desktop and the row fits the phone layouts and the expanded sheet. - Dictation writes the live transcript into the message at the caret as it streams. DictationSpan replaces only the dictated stretch, moves it when the user types before it, keeps what is typed after it, and hands text the user edits over to them. When nothing streams, the row shows Transcribing... after stop and the text goes in once it is back. Cancel takes the dictated text out again. No audio is attached. - A voice message streams its live text, if any, under the waveform, not into the message. On stop the clip is attached as a voice file with its peaks and length, and its transcript is followed by the voice transcripts store (unless Transcribe voice messages is off). Realtime transcription is skipped for servers reached through a tunnel or with an API key, which a browser websocket cannot carry. The first dictation with a separate router model shows a one-time note that it loads beside the chat model. The form takes an optional voiceRecorder, so tests and stories can run it without a microphone. Assisted-by: Claude (Opus 5.5 Medium) * ui: draw audio attachments as playable waveforms with their transcript A voice message should read as a clip, not as a file chip. Audio attachments now render with Llama UI's VoiceWaveform inside VoiceTranscriptReveal, through a direct branch in the core attachment item (voice is core, so it does not go through the extension renderers): the removable sm chip in the chat form, and the md clip in a sent message, on the user bubble's surface. - The audio plays from an object URL made from the stored base64 (or the uploaded file's data URL), revoked when the clip leaves the screen. - A clip stored without peaks, an older voice message or an uploaded audio file, gets them computed from its audio once per session, in memory only; uploaded files keep their name under the waveform. - The transcript opens under the clip on hover, keyboard focus or a tap, and shows Transcribing... while the voice transcripts store waits for it, Transcription unavailable when it failed, and nothing when none was asked for. - The full-screen preview keeps its player and shows the transcript. Tests run the chat form with a fake recorder: the microphone per mode, the recording row, dictation around typed text, the Transcribing... state, cancel, and a voice message chip whose transcript arrives later and goes with the send. Stories show the form recording each kind, a recorded chip, and sent voice messages with the transcript, pending, and computed peaks. Assisted-by: Claude (Opus 5.5 Medium) * ui: drop the old audio recorder and proxy realtime transcription in dev The voice engine replaced AudioRecorder, so it goes. The dev server now proxies WebSockets on /v1, so realtime transcription works under vite dev. The VoiceCapture test waits for the tone to be recorded instead of a fixed 700 ms, which a loaded test run could miss. Assisted-by: Claude (Opus 5.5 Medium) * ui: dictate with a model that is already loaded when the chat model cannot hear The Transcription model setting's Automatic choice now falls back to a router model with audio input that is already loaded, so dictating with a text-only chat model works without picking a model and loads nothing. A picked model still wins, and the empty value is a named constant. Idea from upstream PR 27567, by NickM-27. Assisted-by: Claude (Opus 5.5 Medium) Co-authored-by: Nicolas Mowen <14866235+NickM-27@users.noreply.github.com> * ui: fill the recording waveform, and open voice message transcripts - The live waveform fills the row from the start: a recording shorter than the row is stretched over all its bars, a longer one scrolls with the newest level on the right. No dotted placeholder. - A voice message's transcript is open under its clip, in the chat form while it comes in and in the sent message, with the toggle beside it to fold it. - Settings > Voice > Collapse transcripts (off by default) keeps them folded instead, opening on hover, keyboard focus or a tap. Assisted-by: Claude (Opus 5.5 Medium) (cherry picked from commit 2a93f2f948ed976e40f693ce5d3acf1168284b05) * ui: record voice without ducking other audio, from the first word - Voice input asks for the raw microphone signal: no echo cancellation, noise suppression or auto gain. Echo cancellation made the system treat a recording as a call, which lowers or interrupts other playback, and auto gain faded the first words in. - One AudioContext serves every recording, with the capture worklet loaded once, suspended in between and, where the browser allows it, without an output device, so recording never touches the speakers. - The chat form prepares it when it gets focus (no microphone, so no recording indicator), and a mouse starts recording on press. - A recording starts counting once audio flows: until then the bar says Starting... instead of running the timer. Development builds log how long each step of a start took. - Settings > Voice > Microphone: Automatic (the system default, or the built-in microphone when the default is a Bluetooth headset, which would switch to its low-quality call mode), System default, or a listed microphone. Assisted-by: Claude (Opus 5.5 Medium) (cherry picked from commit 76643e7210d21617d734fdfde677e84af7273153) * ui: show voice text live in the message box, and follow transcripts as spoken - A model that hears but has no live ASR (e.g. LFM3-Omni) now gives text while you record: the realtime client commits the audio piece by piece, at the pauses the levels show, one piece at a time, and the final commit takes only what is left. - A voice message's live text shows as the empty message box's placeholder. Clicking into the box makes it text to edit there, and what is said next follows it. On stop the text leaves the box and goes with the clip; when it was edited, the edit is the clip's transcript. - Llama UI gains VoiceSpokenText and spokenLength(): spoken words in the full foreground colour, which they keep, the rest muted. A clip's transcript follows its playback (estimated over the parts with speech, as there are no word timings), and live text brightens as it arrives. - The recording bar has a send button that stops and sends in one go. Assisted-by: Claude (Opus 5.5 Medium) (cherry picked from commit ce773017f902f8a5690ab9ecca368cecb6427422) * ui: let the capture test start with silence A capture starts with its first audio block, which can come before a stream's first sound, so the steady tone is checked from where it begins. Assisted-by: Claude (Opus 5.5 Medium) (cherry picked from commit a41ee5337b83fef685a5cbaff176925f1647ce9d) * ui: show live voice text sooner, and keep silence from becoming "yeah" - A model that transcribes per commit (LFM3-Omni) gets shorter pieces: cut at a 0.35 s pause after speech, or at the next quiet moment once a piece reaches 3 s, so the text follows the speaker within a few seconds in dictation, the message box and voice mode. - When the recording stops, the audio after the last piece is committed only if speech was heard in it. LFM3-Omni transcribes a commit of pure silence as "Yeah." (or "はい。"), which ended up at the end of the text. A recording whose only piece is that rest still commits it. Assisted-by: Claude (Opus 5.5 Medium) (cherry picked from commit 43e52cbab0fba9d8e84b9491890a406e3c6c90f3) * ui: transcribe a recording cut at pauses once more as a whole Live text comes piece by piece, cut at the speaker's short pauses, and a model reads each piece as a sentence of its own: "I went. To the store." When the recording ends, one pass over the whole clip gives the final text, punctuated by meaning, in dictation and in voice mode alike. The pieces' text stays the live text until then, and is kept when that pass fails or the recording is longer than two minutes. Assisted-by: Claude (Opus 5.5 Medium) (cherry picked from commit 359dd9632e6c88000a1d23c2575bd1e6fcdd9ec1) * ui: give live text over HTTP for models without a realtime session A model that hears but cannot hold a realtime session, such as Qwen3-ASR, was transcribed only after the recording, so dictation showed no text while speaking. The recording is now cut at the speaker's pauses as with a realtime session, and each piece sent to /v1/audio/transcriptions on its own, one at a time, its text joined live. The audio is kept from the start, so what is said while the socket answers goes with the first piece. The whole clip is still transcribed once more at the end, and the pieces' text is kept if that fails. A server a websocket cannot reach (an API key, a remote server) gets live text the same way. Assisted-by: Claude (Opus 5.5 Medium) * ui: say when the browser processes the microphone despite the raw request The browser keeps one processing setting per microphone: while a call, another tab or voice mode has it open with echo cancellation, a dictation that asked for the raw signal is processed too, sounds distant, and the system may lower other sound. The capture now warns when it gets processing it did not ask for, and its development log names the microphone, its rate and what processing is on. Assisted-by: Claude (Opus 5.5 Medium) * ui: keep the microphone beside the stop and send buttons While a reply streams, or once the message has text, the microphone no longer gives way to the main button: it stays beside it as a quieter, secondary button, so more can be dictated without clearing the message or waiting for the reply. Assisted-by: Claude (Opus 5.5 Medium) * ui: show the voice components' stories with Llama UI VoiceWaveform and VoiceTranscriptReveal are Llama UI components, so their stories move to the library's Storybook, under Llama UI/Voice, with the demo audio they play. Llama Chat's Storybook shows them there too, and its own voice stories take the demo audio from the library. VoiceWaveformLive stays with Llama Chat, as its demo records through the chat form's microphone. Assisted-by: Claude (Opus 5.5 Medium) * ui: add a noise suppression setting for voice input Recordings stay raw by default. Settings > Voice can now switch the browser's noise suppression on; echo cancellation and automatic gain stay off either way, as they interrupt other sound and fade the first words in. Assisted-by: Claude (Opus 5.5 Medium) * ui: keep live voice text coming when the room is never quiet Pieces were cut only at a quiet moment, so with music or talk in the room none was ever cut and no text showed until the recording ended. A piece half as long again as its longest length is now cut where it is. Assisted-by: Claude (Opus 5.5 Medium) * ui: add echo cancellation and automatic gain settings for voice input Settings > Voice now has a switch for each kind of microphone processing the browser offers. All three stay off by default, so recordings are raw; each one says what switching it on costs. Assisted-by: Claude (Opus 5.5 Medium) * ui: put the microphone after the stop button, filled like it While a reply streams, the microphone is the last button of the row and looks like the stop and skip buttons beside it, instead of a bare icon before them. Assisted-by: Claude (Opus 5.5 Medium) * ui: say when dictated text is being refined, and send it refined After a recording with live text, the whole clip is transcribed once more so pauses do not end sentences. The form now says so while that pass is out, and a send asked for meanwhile waits for it, so the message goes out with the refined text rather than the pieces. Assisted-by: Claude (Opus 5.5 Medium) * ui: keep the send button in place and put the microphone beside it The send button no longer gives way to the microphone on an empty message. The microphone stands before it as a quieter, tertiary button, with the same tooltip, warning and recording states as before. Assisted-by: Claude (Opus 5.5 Medium) * ui: let a recording be sent while a reply streams, once it has words While a reply streams, the recording row now shows its send button as soon as the first transcribed words are in the message, so the recording no longer has to be stopped first. The microphone also stays beside the send button once a voice message is attached. Assisted-by: Claude (Opus 5.5 Medium) * ui: raise quiet speech for transcription, and say when it is too quiet A quiet microphone gave the model near silence to transcribe. A new Voice setting, Boost quiet speech (on by default), raises speech to a level speech models hear well, up to eight times louder: the audio streamed for live text as it goes, and the whole recording in one pass when it stops, so its first words get the same gain as the rest. Only speech is measured, never the silence around it, loud speech is left as it is, and a soft limiter keeps raised peaks from clipping. When speech stays quiet even at the highest gain, the recording row says so. Assisted-by: Claude (Opus 5.5 Medium) * ui: keep the voice engine free of app code The engine in lib/voice imported the microphone messages from the app's utils and the WAV type from its enums, the only two things keeping it from moving into a package of its own. The microphone checks and messages move into the engine (voice/support.ts), next to the capture that needs them; the uploaded-file helpers stay in the app's utils. Assisted-by: Claude (Opus 5.5 Medium) * ui: start voice sessions from one voice service How a recording reaches the server (routes, API key, whether the realtime socket can be used) and how the Voice settings become capture and transcribe options were built inside the chat form's voice hook, and copied into every other feature that records. VoiceService builds them in one place: a feature asks it for its session options and adds only its own callbacks. Assisted-by: Claude (Opus 5.5 Medium) * ui: tell speech from the room in one place The pause detector, the speech gain and (in voice mode) the listener for speech over a reply each followed a noise floor with their own numbers. A SpeechDetector now does it for all of them, in dB, with one set of settings in VOICE_SPEECH. Assisted-by: Claude (Opus 5.5 Medium) * ui: let any text field take dictation Writing dictated text into a field (at the caret, around the user's typing, replaced by the whole recording's text when it comes) moves out of the chat form's hook into a plain createDictation(). The hook keeps what is the chat form's own: voice messages, toasts and the hint. Assisted-by: Claude (Opus 5.5 Medium) * ui: move the recording row into Llama UI VoiceRecordingBar draws the row shown while recording (cancel, waveform and time, send, and a caption or warning under it) from data alone, so voice mode or a notes page can show the same row. The chat form passes its labels and its microphone button as the stop button. Assisted-by: Claude (Opus 5.5 Medium) * ui: keep the voice engine's Svelte recorder behind its own entry VoiceRecorder, the one part of lib/voice written with runes, moves to lib/voice/svelte, so the rest of the engine is plain TypeScript any app can use without Svelte. Assisted-by: Claude (Opus 5.5 Medium) * ui: move voice input into its own package, Llama Voice The voice engine (capture, the speech signal, transcription over the realtime socket or HTTP) and dictation into a text field move from the app to ui/lib/voice as @ggml-org/llama-voice, so another app, such as a notes app, can use them. The Svelte recorder is its ./svelte entry. Llama Chat compiles the sources, as it does Llama UI's, and keeps its server and settings wiring. The package has its own type check, tests and build, and a CI job for them. Assisted-by: Claude (Opus 5.5 Medium) * ui: show which model does what in voice input The model picker and the Models manager mark each model that hears: Dictation on the one that transcribes dictation, Speech to text on transcription models such as Qwen3-ASR, and Voice on chat models that take voice messages. A loaded model kept only for dictation sits apart from the chat models, a model can be made the dictation model from its row, and Settings > Voice groups the transcription models apart from the chat models with audio input. A voice store now resolves what the microphone does, for the chat form and the model lists alike. Assisted-by: Claude (Opus 5.5 Medium) * ui: warn of a quiet microphone inside the recording row The too-quiet line under the waveform made the form grow and jump. The warning now stays inside the row: the waveform turns the warning colour, an icon sits by the time, and the advice opens above the row in a tooltip for a few seconds, then on hover or focus. Screen readers still hear it when it appears. Assisted-by: Claude (Opus 5.5 Medium) * ui: open the microphone while the pointer is on its button The first seconds of a recording could come out quiet while the microphone settled, and the too-quiet warning showed for them. With the pointer or the focus on the microphone button, the microphone now opens ahead of the press (only when the page may already use it, so nothing asks), and the recording takes it at once; it is let go shortly after the pointer leaves without a press. The warning also waits until the speech has stayed too quiet for two seconds. Assisted-by: Claude (Opus 5.5 Medium) * ui: tidy voice input after review Fix a microphone left open when a recording is cancelled while it starts or the pointer leaves while it opens, a capture that is still letting go silencing the next one, and live text stopping after a realtime error. Share the dictation model choice, the transcript lookup, base64 decoding, PCM joining and stopping a stream instead of repeating them, and export only Llama Voice's public API. Assisted-by: Claude (Opus 5.5 Medium) * ui: pin Llama Voice's dev dependencies to Llama UI's versions Assisted-by: Claude (Opus 5.5 Medium) * ui: let demos speak into a synthetic microphone Assisted-by: Claude (Opus 5.5 Medium) * ui: keep the microphone for voice input, off the audio files entry The attach menu's Audio Files entry used a microphone, which now sits beside the form's own microphone button. The microphone stands for recording and dictation only; a model's audio modality is headphones, and the audio files entry is a file with headphones. Assisted-by: Claude (Opus 5.5 Medium) * ui: set voice input on the slimmed design tokens Voice input's components use the type roles, the one corner scale and shadcn's utility names for the neutral colours, as the rest of Llama UI and Llama Chat do since the design tokens were slimmed down. Assisted-by: Claude (Opus 5.5 Medium) * ui: name voice states, platform strings, patterns and tunings Addresses the review on the voice PR: - Llama Voice: VoiceCaptureState, VoiceRecorderStatus, RealtimeState and RealtimeUnsupportedReason are enums (src/enums.ts, exported); DOMException names, AudioContext states, transcription form fields, socket protocols, device kinds, worklet messages, error messages, regexes and tuning values are named constants. - Llama UI: VoiceTranscriptStatus, VoiceWaveformSize and VoiceRecordingBarStatus are enums; KeyboardKey, MouseButton and PointerType join its UI enums; the voice components' sizes, labels, timings, selectors and patterns move to constants/voice.constants.ts. The voice unit tests move from src to tests/unit. - App: ChatFormVoiceState is an enum, the recording bar's status comes from a map, and the voice code uses the enums above, BooleanString, HEADERS and new voice constants in place of inline strings, regexes and numbers. Assisted-by: Claude (Opus 5.5 Medium) * ui: keep all of Llama UI's tests under tests The Model UI formatting spec moves to tests/unit and the button spec to tests/client, next to the voice specs; the test projects now read only tests/unit and tests/client. Assisted-by: Claude (Opus 5.5 Medium) * ui: show a transcript's words without the model's own format Qwen3-ASR names the language before its text (language French<asr_text>...) and VibeVoice-ASR answers with timed segments as JSON, which the server passes on as they are. The voice engine now takes both off, over HTTP and over the realtime socket, also while segments still stream, so the message box and the clip show the words alone. Assisted-by: Claude (Opus 5.5 Medium) * ui: dictate when a speech-to-text model is picked for the conversation A speech-to-text model has audio input, so on Automatic the microphone recorded a voice message for it, which such a model has nothing to answer with. It now dictates, as the model's badge already says it is for. Assisted-by: Claude (Opus 5.5 Medium) * ui: say why a live-only model cannot transcribe behind an API key A browser websocket cannot send the API key, so the realtime socket is left out on a server that needs one, and a model that only transcribes over it (Voxtral Realtime) answers the HTTP route with a 501. The clip and the dictation toast now say so instead of only that transcription is unavailable. Assisted-by: Claude (Opus 5.5 Medium) * ui: pick the dictation model with a microphone on its row The model that transcribes dictation was marked with a filled Dictation badge. It is now marked by a red microphone among the row's actions in the model picker, beside the heart, and every other model that hears shows the same microphone on hover: a click makes it the transcription model, and a click on the chosen one hands the choice back to Automatic. The badge keeps saying what kind of hearing model a row is, and the Models manager marks the dictation model with a small microphone beside its name. Assisted-by: Claude (Opus 5.5 Medium) --------- Co-authored-by: Nicolas Mowen <14866235+NickM-27@users.noreply.github.com>
616 lines
20 KiB
TypeScript
616 lines
20 KiB
TypeScript
import { demoMcpServerSettings } from './fixtures/demo-config.ts';
|
|
import { generateTrace } from './fixtures/traces.ts';
|
|
import { ScreencastRecorder } from './recorder.ts';
|
|
import { type CDPSession, type Locator, type Page, test as base } from '@playwright/test';
|
|
import { execFileSync } from 'node:child_process';
|
|
import {
|
|
appendFileSync,
|
|
mkdirSync,
|
|
mkdtempSync,
|
|
readFileSync,
|
|
rmSync,
|
|
writeFileSync
|
|
} from 'node:fs';
|
|
import { tmpdir } from 'node:os';
|
|
import { basename, join, resolve } from 'node:path';
|
|
|
|
const VIDEOS_DIR = resolve(import.meta.dirname, 'videos');
|
|
/** How long a demo waits for the app to be ready before it starts recording. */
|
|
const APP_READY_TIMEOUT_MS = 90000;
|
|
/** How far below the viewport's top a pinch has to start for Chromium to zoom at all. */
|
|
const ZOOM_TOP_INSET_PX = 64;
|
|
|
|
/**
|
|
* Draws a fake cursor and click ripples, since recorded videos don't include
|
|
* the OS pointer, and a fingertip per touch, so a pinch shows as two fingers.
|
|
* Runs in the page via addInitScript.
|
|
*/
|
|
function installCursorOverlay(): void {
|
|
const mount = () => {
|
|
const style = document.createElement('style');
|
|
|
|
style.textContent = `
|
|
#demo-cursor { position: fixed; top: 0; left: 0; z-index: 2147483647; pointer-events: none;
|
|
width: 22px; height: 22px; margin: -3px 0 0 -3px; transition: transform 80ms ease-out; }
|
|
#demo-cursor.down { transform: scale(0.85); }
|
|
.demo-ripple { position: fixed; z-index: 2147483646; pointer-events: none; width: 36px; height: 36px;
|
|
margin: -18px 0 0 -18px; border-radius: 50%; background: rgba(59, 130, 246, 0.35);
|
|
animation: demo-ripple 450ms ease-out forwards; }
|
|
@keyframes demo-ripple { from { transform: scale(0.3); opacity: 1; } to { transform: scale(1.4); opacity: 0; } }
|
|
#demo-caption { position: fixed; left: 50%; top: 72px; z-index: 2147483647; pointer-events: none;
|
|
transform: translate(-50%, -8px); opacity: 0; transition: opacity 200ms ease, transform 200ms ease;
|
|
max-width: 80%; padding: 10px 18px; border-radius: 10px; background: rgba(17, 17, 17, 0.88);
|
|
color: #fff; font: 500 18px/1.4 system-ui, sans-serif; text-align: center; }
|
|
#demo-caption.visible { opacity: 1; transform: translate(-50%, 0); }
|
|
.demo-finger { position: fixed; z-index: 2147483646; pointer-events: none; width: 40px; height: 40px;
|
|
margin: -20px 0 0 -20px; border-radius: 50%; background: rgba(59, 130, 246, 0.3);
|
|
border: 2px solid rgba(59, 130, 246, 0.55); transition: opacity 200ms ease; }
|
|
`;
|
|
document.head.appendChild(style);
|
|
|
|
const cursor = document.createElement('div');
|
|
|
|
cursor.id = 'demo-cursor';
|
|
cursor.innerHTML = `<svg viewBox="0 0 24 24" width="22" height="22"><path d="M4 2l16 10.5-7 1.5-3.5 6.5z"
|
|
fill="#111" stroke="#fff" stroke-width="1.5" stroke-linejoin="round"/></svg>`;
|
|
document.documentElement.appendChild(cursor);
|
|
|
|
const caption = document.createElement('div');
|
|
|
|
caption.id = 'demo-caption';
|
|
document.documentElement.appendChild(caption);
|
|
|
|
document.addEventListener(
|
|
'mousemove',
|
|
(e) => (cursor.style.translate = `${e.clientX}px ${e.clientY}px`),
|
|
true
|
|
);
|
|
document.addEventListener(
|
|
'mousedown',
|
|
(e) => {
|
|
cursor.classList.add('down');
|
|
const ripple = document.createElement('div');
|
|
|
|
ripple.className = 'demo-ripple';
|
|
ripple.style.left = `${e.clientX}px`;
|
|
ripple.style.top = `${e.clientY}px`;
|
|
document.documentElement.appendChild(ripple);
|
|
setTimeout(() => ripple.remove(), 500);
|
|
},
|
|
true
|
|
);
|
|
document.addEventListener('mouseup', () => cursor.classList.remove('down'), true);
|
|
|
|
const fingers: HTMLDivElement[] = [];
|
|
const placeFingers = (e: TouchEvent) => {
|
|
Array.from(e.touches).forEach((touch, i) => {
|
|
const finger = (fingers[i] ??= Object.assign(document.createElement('div'), {
|
|
className: 'demo-finger'
|
|
}));
|
|
|
|
finger.style.left = `${touch.clientX}px`;
|
|
finger.style.top = `${touch.clientY}px`;
|
|
finger.style.opacity = '1';
|
|
document.documentElement.appendChild(finger);
|
|
});
|
|
fingers.slice(e.touches.length).forEach((finger) => (finger.style.opacity = '0'));
|
|
};
|
|
|
|
for (const type of ['touchstart', 'touchmove', 'touchend', 'touchcancel']) {
|
|
document.addEventListener(type, placeFingers as EventListener, {
|
|
capture: true,
|
|
passive: true
|
|
});
|
|
}
|
|
};
|
|
|
|
if (document.readyState === 'loading') {
|
|
document.addEventListener('DOMContentLoaded', mount);
|
|
} else {
|
|
mount();
|
|
}
|
|
}
|
|
|
|
/** Seeds UI settings once per demo, so later reloads keep what the demo changed. */
|
|
function seedSettings(config: Record<string, unknown>): void {
|
|
const key = 'LlamaUi.config';
|
|
|
|
if (localStorage.getItem(key) === null) {
|
|
localStorage.setItem(key, JSON.stringify(config));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Swaps the page's microphone for one that hears only what `window.demoSpeak` plays into it,
|
|
* silence otherwise. Chromium's fake capture device crashes the page, so voice input is
|
|
* recorded with this instead. Runs in the page.
|
|
*/
|
|
function installFakeMicrophone(): void {
|
|
const context = new AudioContext();
|
|
const output = context.createMediaStreamDestination();
|
|
|
|
navigator.mediaDevices.getUserMedia = async (constraints) => {
|
|
if (!constraints?.audio || constraints.video) {
|
|
throw new DOMException('demo: only the microphone is faked', 'NotSupportedError');
|
|
}
|
|
|
|
await context.resume();
|
|
|
|
// each opening gets its own tracks, so a recording that stops them leaves the source on
|
|
return new MediaStream(output.stream.getAudioTracks().map((track) => track.clone()));
|
|
};
|
|
|
|
Object.assign(window, {
|
|
demoSpeak: async (wavBase64: string) => {
|
|
const bytes = Uint8Array.from(atob(wavBase64), (c) => c.charCodeAt(0));
|
|
const source = context.createBufferSource();
|
|
|
|
source.buffer = await context.decodeAudioData(bytes.buffer);
|
|
source.connect(output);
|
|
await context.resume();
|
|
await new Promise((done) => {
|
|
source.onended = done;
|
|
source.start();
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
/** `text` read by a synthetic voice, as a base64 WAV. Uses macOS `say`. */
|
|
function synthesizeSpeech(text: string, voice: string): string {
|
|
const dir = mkdtempSync(join(tmpdir(), 'demo-speech-'));
|
|
const file = join(dir, 'speech.wav');
|
|
|
|
try {
|
|
execFileSync('say', [
|
|
'-v',
|
|
voice,
|
|
'-o',
|
|
file,
|
|
'--file-format=WAVE',
|
|
'--data-format=LEI16@24000',
|
|
text
|
|
]);
|
|
|
|
return readFileSync(file).toString('base64');
|
|
} catch (error) {
|
|
throw new Error(`demo: speak() needs macOS \`say\` with the voice ${voice}`, {
|
|
cause: error
|
|
});
|
|
} finally {
|
|
rmSync(dir, { force: true, recursive: true });
|
|
}
|
|
}
|
|
|
|
/** Seeded randomness, so a demo moves the same way on every recording. */
|
|
function createRandom(seed: string): () => number {
|
|
let state = [...seed].reduce((h, c) => Math.imul(h ^ c.charCodeAt(0), 2654435761), 1);
|
|
|
|
return () => {
|
|
state = (state + 0x6d2b79f5) | 0;
|
|
|
|
let t = Math.imul(state ^ (state >>> 15), 1 | state);
|
|
|
|
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
|
|
|
|
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
};
|
|
}
|
|
|
|
const easeInOut = (t: number) => (t < 0.5 ? 4 * t * t * t : 1 - (-2 * t + 2) ** 3 / 2);
|
|
|
|
/**
|
|
* Drives the page like a person: the cursor glides along a slight arc with
|
|
* Fitts-like durations, settles briefly before pressing, and typing and pauses
|
|
* vary a little.
|
|
*/
|
|
export class Demo {
|
|
private cdp: CDPSession | undefined;
|
|
private readonly page: Page;
|
|
private readonly random: () => number;
|
|
private x = 0;
|
|
private y = 0;
|
|
|
|
/** Shows a caption at the top of the frame for `ms`, clear of the chat form. */
|
|
async caption(text: string, ms = 2200): Promise<void> {
|
|
await this.showCaption(text);
|
|
await this.page.waitForTimeout(ms);
|
|
await this.hideCaption();
|
|
}
|
|
|
|
async click(target: Locator, ms = 400): Promise<void> {
|
|
await this.moveTo(target);
|
|
await this.page.waitForTimeout(this.between(80, 160));
|
|
await this.page.mouse.down();
|
|
await this.page.waitForTimeout(this.between(50, 90));
|
|
await this.page.mouse.up();
|
|
await this.pause(ms);
|
|
}
|
|
|
|
constructor(page: Page, seed: string) {
|
|
this.page = page;
|
|
this.random = createRandom(seed);
|
|
|
|
const viewport = page.viewportSize();
|
|
|
|
this.x = (viewport?.width ?? 0) / 2;
|
|
this.y = (viewport?.height ?? 0) / 2;
|
|
}
|
|
|
|
async hover(target: Locator, ms = 350): Promise<void> {
|
|
await this.moveTo(target);
|
|
await this.pause(ms);
|
|
}
|
|
|
|
/**
|
|
* Gives the page a microphone that hears only what `speak` says, and allows the page to
|
|
* use it. Call it before the microphone is first used.
|
|
*/
|
|
async microphone(): Promise<void> {
|
|
await this.page.context().grantPermissions(['microphone']);
|
|
await this.page.evaluate(installFakeMicrophone);
|
|
}
|
|
|
|
/** Glides the cursor to a point near the center of `target`. */
|
|
async moveTo(target: Locator): Promise<void> {
|
|
await target.scrollIntoViewIfNeeded();
|
|
const box = await target.boundingBox();
|
|
|
|
if (!box) throw new Error(`demo: ${target} has no bounding box`);
|
|
|
|
const x = box.x + box.width * this.between(0.4, 0.6);
|
|
const y = box.y + box.height * this.between(0.4, 0.6);
|
|
const dx = x - this.x;
|
|
const dy = y - this.y;
|
|
const distance = Math.hypot(dx, dy);
|
|
const duration = 110 + 55 * Math.log2(distance / 40 + 1);
|
|
// bow the path a little to one side, like a wrist turning
|
|
const bow = distance * this.between(-0.08, 0.08);
|
|
const fromX = this.x;
|
|
const fromY = this.y;
|
|
const started = Date.now();
|
|
|
|
for (;;) {
|
|
const t = Math.min((Date.now() - started) / duration, 1);
|
|
const p = easeInOut(t);
|
|
const arc = Math.sin(Math.PI * p) * bow;
|
|
|
|
this.x = fromX + dx * p - (dy / (distance || 1)) * arc;
|
|
this.y = fromY + dy * p + (dx / (distance || 1)) * arc;
|
|
await this.page.mouse.move(this.x, this.y);
|
|
|
|
if (t === 1) break;
|
|
|
|
await this.page.waitForTimeout(8);
|
|
}
|
|
}
|
|
|
|
/** Waits about `ms`, varied by up to 15% so pauses don't feel metronomic. */
|
|
async pause(ms = 800): Promise<void> {
|
|
await this.page.waitForTimeout(ms * this.between(0.85, 1.15));
|
|
}
|
|
|
|
async press(key: string, ms = 600): Promise<void> {
|
|
await this.page.keyboard.press(key);
|
|
await this.pause(ms);
|
|
}
|
|
|
|
/** Scrolls the wheel over `target` by `dy` pixels (negative scrolls up), in small steps like a trackpad. */
|
|
async scroll(target: Locator, dy: number, ms = 500): Promise<void> {
|
|
await this.moveTo(target);
|
|
|
|
const step = Math.sign(dy) * 40;
|
|
|
|
for (let done = 0; Math.abs(done) < Math.abs(dy); done += step) {
|
|
await this.page.mouse.wheel(0, step);
|
|
await this.page.waitForTimeout(16);
|
|
}
|
|
|
|
await this.pause(ms);
|
|
}
|
|
|
|
/**
|
|
* Says `text` into the microphone from `microphone` in a synthetic voice (macOS `say`), and
|
|
* returns once it is said. The words show as a caption, since the video has no sound.
|
|
*/
|
|
async speak(text: string, voice = 'Samantha', ms = 400): Promise<void> {
|
|
const wav = synthesizeSpeech(text, voice);
|
|
|
|
await this.showCaption(`“${text}”`);
|
|
await this.page.evaluate(
|
|
(base64) =>
|
|
(window as unknown as { demoSpeak(wav: string): Promise<void> }).demoSpeak(base64),
|
|
wav
|
|
);
|
|
await this.hideCaption();
|
|
await this.pause(ms);
|
|
}
|
|
|
|
/** Types quickly with uneven key timing, a little slower after spaces and punctuation. */
|
|
async type(target: Locator, text: string, ms = 500): Promise<void> {
|
|
await this.click(target, 200);
|
|
|
|
for (const char of text) {
|
|
await this.page.keyboard.type(char);
|
|
await this.page.waitForTimeout(
|
|
this.between(15, 45) + (/[\s.,!?]/.test(char) ? this.between(20, 60) : 0)
|
|
);
|
|
}
|
|
|
|
await this.pause(ms);
|
|
}
|
|
|
|
/**
|
|
* Pinches in on `target`, a control or a point in the viewport, holds about `ms`, and pinches
|
|
* back out to 1x: a two-finger zoom onto a detail too small to read in the frame. On a touch
|
|
* device the fingertips show.
|
|
*/
|
|
async zoom(target: Locator | { x: number; y: number }, scale = 2.5, ms = 1400): Promise<void> {
|
|
const point = 'x' in target ? target : await this.center(target);
|
|
// a pinch anchored in the viewport's top band does not zoom at all
|
|
const at = { x: point.x, y: Math.max(point.y, ZOOM_TOP_INSET_PX) };
|
|
const viewport = this.page.viewportSize() ?? { height: 0, width: 0 };
|
|
const middle = { x: viewport.width / 2, y: viewport.height / 2 };
|
|
|
|
await this.pinch(at, scale, 1400);
|
|
|
|
// One near the bottom edge overshoots its ratio, and a pinch there again misses too. A small
|
|
// pinch from the middle evens it out, drawing the detail toward the middle, so it stays in view
|
|
const reached = await this.pageScale();
|
|
|
|
if (Math.abs(reached / scale - 1) > 0.03) await this.pinch(middle, scale / reached, 1400);
|
|
|
|
await this.liftFingers();
|
|
await this.pause(ms);
|
|
|
|
// back to exactly 1x, which the next gesture needs, in one quick pinch from the middle of
|
|
// the viewport, and another for what is left when one lands short
|
|
for (let tries = 0; ; tries++) {
|
|
const current = await this.pageScale();
|
|
|
|
if (current <= 1.001) break;
|
|
|
|
if (tries === 4) throw new Error(`demo: zoom stuck at ${current.toFixed(3)}x`);
|
|
|
|
await this.pinch(middle, 1 / current, 1800);
|
|
}
|
|
|
|
await this.liftFingers();
|
|
await this.pause(200);
|
|
}
|
|
|
|
private between(min: number, max: number): number {
|
|
return min + (max - min) * this.random();
|
|
}
|
|
|
|
private async center(target: Locator): Promise<{ x: number; y: number }> {
|
|
const box = await target.boundingBox();
|
|
|
|
if (!box) throw new Error(`demo: ${target} has no bounding box`);
|
|
|
|
return { x: box.x + box.width / 2, y: box.y + box.height / 2 };
|
|
}
|
|
|
|
private async hideCaption(): Promise<void> {
|
|
await this.page.evaluate(() =>
|
|
document.getElementById('demo-caption')?.classList.remove('visible')
|
|
);
|
|
await this.page.waitForTimeout(250);
|
|
}
|
|
|
|
/** A pinch that reaches the screen's edge can end without its last touchend reaching the page. */
|
|
private async liftFingers(): Promise<void> {
|
|
await this.page.evaluate(() =>
|
|
document
|
|
.querySelectorAll<HTMLElement>('.demo-finger')
|
|
.forEach((finger) => (finger.style.opacity = '0'))
|
|
);
|
|
}
|
|
|
|
private pageScale(): Promise<number> {
|
|
return this.page.evaluate(() => window.visualViewport?.scale ?? 1);
|
|
}
|
|
|
|
/**
|
|
* A two-finger pinch by `factor` around `at`, in viewport pixels. While zoomed in, the gesture
|
|
* reads its position in page-scale units, so it is divided by the scale the page is at.
|
|
*/
|
|
private async pinch(at: { x: number; y: number }, factor: number, speed: number): Promise<void> {
|
|
const cdp = await this.session();
|
|
const current = await this.pageScale();
|
|
|
|
await cdp.send('Input.synthesizePinchGesture', {
|
|
gestureSourceType: 'touch',
|
|
relativeSpeed: speed,
|
|
scaleFactor: factor,
|
|
x: at.x / current,
|
|
y: at.y / current
|
|
});
|
|
}
|
|
|
|
private async session(): Promise<CDPSession> {
|
|
this.cdp ??= await this.page.context().newCDPSession(this.page);
|
|
|
|
return this.cdp;
|
|
}
|
|
|
|
private async showCaption(text: string): Promise<void> {
|
|
await this.page.evaluate((t) => {
|
|
const el = document.getElementById('demo-caption');
|
|
|
|
if (!el) return;
|
|
|
|
el.textContent = t;
|
|
el.classList.add('visible');
|
|
}, text);
|
|
}
|
|
}
|
|
|
|
interface DemoOptions {
|
|
/** Path the demo starts from. */
|
|
startPath: string;
|
|
/** Extra UI settings merged into the seeded config. */
|
|
settings: Record<string, unknown>;
|
|
/**
|
|
* Conversations imported before the demo starts: built-in traces from
|
|
* fixtures/traces.ts, or `<name>.jsonl` files from DEMO_TRACES_DATASET.
|
|
*/
|
|
traces: string[];
|
|
}
|
|
|
|
/** Reads a trace by name, generating built-in ones and downloading the rest. */
|
|
function loadTrace(name: string, dir: string): string {
|
|
const generated = generateTrace(name);
|
|
|
|
if (generated) return generated;
|
|
|
|
const dataset = process.env.DEMO_TRACES_DATASET;
|
|
|
|
if (!dataset)
|
|
throw new Error(`demo: no built-in trace "${name}" and DEMO_TRACES_DATASET is not set`);
|
|
|
|
execFileSync(
|
|
'hf',
|
|
['download', dataset, `${name}.jsonl`, '--repo-type', 'dataset', '--local-dir', dir],
|
|
{ stdio: 'inherit' }
|
|
);
|
|
|
|
return readFileSync(join(dir, `${name}.jsonl`), 'utf8');
|
|
}
|
|
|
|
/** Imports conversations through Settings > Import/Export, like a user would. */
|
|
async function importTraces(page: Page, file: string): Promise<void> {
|
|
// the expanded sidebar adds the shortcut to the name
|
|
const settings = page.getByRole('button', { name: /^Settings(\s|$)/ }).first();
|
|
const reachable = (target: Locator) =>
|
|
target.click({ timeout: 2000, trial: true }).then(
|
|
() => true,
|
|
() => false
|
|
);
|
|
// a collapsed sidebar can sit under the page: on a phone the page's menu key slides it aside,
|
|
// on a desktop its logo expands it
|
|
const keys = [
|
|
page.locator('xpath=//button[@aria-label="Expand navigation"][not(ancestor::aside)]'),
|
|
page.locator('aside button[aria-label="Expand navigation"]')
|
|
];
|
|
|
|
// a few rounds, as a cold dev server reloads the page once it has optimized its dependencies,
|
|
// which can land right after a key opened the sidebar and close it again
|
|
for (let round = 0; round < 3 && !(await reachable(settings)); round++) {
|
|
for (const key of keys) {
|
|
if (await reachable(settings)) break;
|
|
|
|
if ((await key.count()) > 0 && (await reachable(key.first()))) await key.first().click();
|
|
}
|
|
}
|
|
|
|
await settings.click();
|
|
|
|
// a dialog on a desktop or tablet, a page of its own on a phone
|
|
const settingsView = page
|
|
.getByRole('dialog')
|
|
.or(page.getByRole('heading', { exact: true, level: 1, name: 'Settings' }));
|
|
|
|
await settingsView.first().waitFor();
|
|
|
|
const scope = (await page.getByRole('dialog').count()) > 0 ? page.getByRole('dialog') : page;
|
|
|
|
await scope.getByText('Import/Export', { exact: true }).filter({ visible: true }).click();
|
|
|
|
const chooser = page.waitForEvent('filechooser');
|
|
|
|
await scope.getByRole('button', { name: 'Import conversations' }).click();
|
|
await (await chooser).setFiles(file);
|
|
await page.getByRole('button', { name: /^Import \(\d+\)$/ }).click();
|
|
await page.getByRole('heading', { name: /^Imported \d+ conversation/ }).waitFor();
|
|
|
|
// the app remembers the sidebar the import expanded; a demo starts with it collapsed, as on a
|
|
// first visit
|
|
await page.evaluate(() => {
|
|
for (const key of Object.keys(localStorage)) {
|
|
if (key.endsWith('.sidebarExpanded')) localStorage.removeItem(key);
|
|
}
|
|
});
|
|
}
|
|
|
|
export const test = base.extend<DemoOptions & { demo: Demo }>({
|
|
demo: async ({ page }, use, testInfo) => {
|
|
await use(new Demo(page, `${testInfo.file}:${testInfo.title}`));
|
|
},
|
|
page: async ({ page, settings, startPath, traces }, use, testInfo) => {
|
|
await page.addInitScript(installCursorOverlay);
|
|
await page.addInitScript(seedSettings, {
|
|
mcpServers: JSON.stringify(demoMcpServerSettings()),
|
|
...settings
|
|
});
|
|
|
|
if (traces.length > 0) {
|
|
const dir = testInfo.outputPath('traces');
|
|
const file = join(dir, 'import.jsonl');
|
|
|
|
mkdirSync(dir, { recursive: true });
|
|
writeFileSync(file, traces.map((name) => loadTrace(name, dir)).join('\n'));
|
|
await page.goto('/');
|
|
await page
|
|
.getByRole('textbox', { name: 'Type a message...' })
|
|
.waitFor({ timeout: APP_READY_TIMEOUT_MS });
|
|
await importTraces(page, file);
|
|
}
|
|
|
|
await page.goto(startPath);
|
|
|
|
// a hash route after the import doesn't reload, which the conversations list needs
|
|
if (traces.length > 0) await page.reload();
|
|
|
|
// a fresh checkout's dev server optimizes its dependencies on the first load and reloads the
|
|
// page, which can take well past an action's timeout; the recording has not started yet
|
|
await page
|
|
.getByRole('textbox', { name: 'Type a message...' })
|
|
.waitFor({ timeout: APP_READY_TIMEOUT_MS });
|
|
|
|
const viewport = page.viewportSize();
|
|
|
|
if (viewport) await page.mouse.move(viewport.width / 2, viewport.height / 2);
|
|
|
|
// let MCP connections and fonts settle before the recording starts
|
|
await page.waitForTimeout(1500);
|
|
|
|
const recorder = new ScreencastRecorder(testInfo.outputPath('frames'));
|
|
|
|
await recorder.start(page);
|
|
await use(page);
|
|
await recorder.stop();
|
|
await page.close();
|
|
|
|
if (testInfo.status !== testInfo.expectedStatus) return;
|
|
|
|
const file = basename(testInfo.file).replace(/\.demo\.ts$/, '');
|
|
const name = testInfo.title === 'demo' ? file : `${file}--${slugify(testInfo.title)}`;
|
|
const mp4 = resolve(VIDEOS_DIR, `${name}.mp4`);
|
|
|
|
mkdirSync(VIDEOS_DIR, { recursive: true });
|
|
recorder.encode(mp4);
|
|
// the CLI reads which videos a script made, in order, with their titles
|
|
appendFileSync(
|
|
resolve(VIDEOS_DIR, `${file}.videos.jsonl`),
|
|
`${JSON.stringify({ mp4, title: testInfo.title })}\n`
|
|
);
|
|
console.log(`demo video: ${mp4}`);
|
|
},
|
|
|
|
settings: [{}, { option: true }],
|
|
|
|
startPath: ['/', { option: true }],
|
|
|
|
traces: [[], { option: true }]
|
|
});
|
|
|
|
export { expect } from '@playwright/test';
|
|
|
|
function slugify(text: string): string {
|
|
return text
|
|
.toLowerCase()
|
|
.replace(/[^a-z0-9]+/g, '-')
|
|
.replace(/^-|-$/g, '');
|
|
}
|