AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
feat(platform): voice brain-dump onboarding step (#13764)
Abhimanyu Yadav6c5c9b412975
diff --git a/autogpt_platform/backend/.env.default b/autogpt_platform/backend/.env.defaultindex b294318f9b..bf5632af17 100644--- a/autogpt_platform/backend/.env.default+++ b/autogpt_platform/backend/.env.default@@ -301,3 +301,30 @@ TALLY_API_KEY=# Other ServicesAUTOMOD_API_KEY=++## ===== ONBOARDING BRAIN DUMP ===== ##+# The voice brain-dump onboarding step is gated on the `onboarding-brain-dump`+# LaunchDarkly flag and is fail-closed: the endpoints 404 unless it is on.+#+# Left commented deliberately — .env.default ships to every install, and an+# active override here would force the feature on for everyone and bypass+# LaunchDarkly. Uncomment in your own .env to test locally, and set the+# matching NEXT_PUBLIC_FORCE_FLAG_ONBOARDING_BRAIN_DUMP in frontend/.env.+# FORCE_FLAG_ONBOARDING_BRAIN_DUMP=true++# Speech-to-text model for the brain dump. Requires a direct OpenAI key+# (OPENAI_INTERNAL_API_KEY, else OPENAI_API_KEY) — OpenRouter has no+# /audio/transcriptions endpoint. Falls back automatically on failure.+# BRAIN_DUMP_TRANSCRIPTION_MODEL=gpt-4o-transcribe+# BRAIN_DUMP_TRANSCRIPTION_FALLBACK_MODEL=whisper-1++# Greeting generation for the copilot home after onboarding. The prompt is+# managed in Langfuse under BRAIN_DUMP_GREETING_PROMPT_NAME when Langfuse+# credentials are configured; otherwise a built-in prompt is used.+# BRAIN_DUMP_GREETING_MODEL=anthropic/claude-sonnet-5+# BRAIN_DUMP_GREETING_PROMPT_NAME=Brain Dump Greeting++# Model that picks which integrations to suggest in the welcome dialog's+# "Connect your tools" panel. Recommendations are best-effort — a failure+# here never blocks onboarding.+# BRAIN_DUMP_RECOMMEND_MODEL=anthropic/claude-sonnet-5diff --git a/autogpt_platform/backend/backend/api/features/onboarding_dump/EVAL.md b/autogpt_platform/backend/backend/api/features/onboarding_dump/EVAL.mdnew file mode 100644index 0000000000..d37a186e3c--- /dev/null+++ b/autogpt_platform/backend/backend/api/features/onboarding_dump/EVAL.md@@ -0,0 +1,146 @@+# Brain-dump transcription eval++WER (word error rate) harness for the onboarding brain-dump transcription+pipeline (`transcription.py`). It runs the **real** pipeline — same models,+same retry/fallback, same ffmpeg splitting and stitching — against a corpus of+recordings with human-written reference transcripts, and reports how far the+machine transcript drifts from the truth.++**Release gate: aggregate (pooled) WER must stay under 5%.** The harness exits+`1` when it is at or above 5%, `0` otherwise, so it can be wired into a release+check as-is.++## Running it++```bash+# from autogpt_platform/backend+poetry run brain-dump-eval --dir /path/to/dumps++# override the primary model (fallback stays whisper-1)+poetry run brain-dump-eval --dir /path/to/dumps --model gpt-4o-mini-transcribe++# duration fallback for chunked files whose container header reports none+poetry run brain-dump-eval --dir /path/to/dumps --duration-secs 1800++# also dump machine-readable results+poetry run brain-dump-eval --dir /path/to/dumps --json /tmp/wer.json+```++Run `poetry install` once after pulling this in — `brain-dump-eval` is a new+console script and won't exist in an older virtualenv. Equivalent without it:++```bash+poetry run python -m backend.api.features.onboarding_dump.brain_dump_eval --dir /path/to/dumps+```++Requires a direct OpenAI key (`OPENAI_INTERNAL_API_KEY` or `OPENAI_API_KEY`) —+OpenRouter does not implement `/audio/transcriptions`. Recordings above the+single-request byte cap also need `ffmpeg` on `PATH`.++**This costs real money and real minutes.** A 25-file, 2–15 minute corpus is+roughly 3 hours of audio per run. Run it before a transcription change ships,+not on every commit.++## The corpus++Put the recordings and their reference transcripts in one flat directory,+matched **by basename**:++```+dumps/+ dump01.webm dump01.txt+ dump02.m4a dump02.txt+ dump03-es.mp3 dump03-es.txt+```++- Audio extensions: `.webm` `.mp4` `.m4a` `.mp3` `.wav` `.ogg`+- Reference: same basename, `.txt`, UTF-8, plain text (no timestamps, no+ speaker labels)+- Unmatched files are skipped and listed at the bottom of the report, so a+ typo'd basename never silently shrinks the corpus.++### What to record — please help fill this out++We need **25+ real rambling dumps**. Not scripts read aloud: the pipeline's+failure mode is messy natural speech, so read-aloud audio will pass at ~1% WER+and tell us nothing. Target mix:++- **25+ recordings**, each **2–15 minutes** long+- **Mixed accents** — at minimum US, UK, Indian, and non-native-English+ speakers+- **Background noise** — a few from a café, a street, a room with a fan, a+ phone on speaker+- **At least 3–4 non-English dumps** (e.g. Spanish, French, Hindi). The+ pipeline must transcribe in the language spoken, never translate — a+ non-English dump that comes back in English is a bug even at low WER+- **At least 2 long ones (>20 MB)** so the chunked/stitched path is exercised+- Real rambling: false starts, "um", topic switches, self-corrections, long+ pauses++### Writing the reference transcript++Type what was actually said, verbatim: keep filler words and false starts, drop+nothing. Don't clean up grammar. Punctuation, casing and hyphenation don't+matter — the harness lowercases, strips punctuation and collapses whitespace+before comparing — but *words* do.++## Reading the report++```+file WER sub ins del ref secs segments model+dump01 3.20% 8 2 1 344 12.4 1 gpt-4o-transcribe+dump02 7.10% 15 4 3 312 41.9 3 gpt-4o-transcribe,whisper-1++Stitch boundaries (chunked files):+ dump02 — 3 segments+ seam 0/1: dedup dropped 7 word(s)+ ...so then I went back to the drawing+ board and I thought maybe the whole thing+ seam 1/2: dedup dropped 0 word(s)+ ...++pooled WER 4.30% (656 reference words)+mean WER 5.15%+median WER 5.15%+wall clock 54.3s++Release gate: aggregate WER must stay under 5% — PASS (exit 0)+```++- **pooled WER** — errors over all reference words in the corpus. This is the+ gated number; long files count more, which is what we want.+- **mean / median WER** — per-file averages. A mean far above the pooled number+ means a small file is failing badly; look at the per-file rows.+- **sub / ins / del** — substitutions dominate for accents and jargon,+ insertions for hallucination (the failure mode that matters most: a+ hallucinated sentence becomes a "fact" about the user), deletions for+ dropped audio and bad seams.+- **segments** — `1` means single-request; more means the chunked path ran.+- **model** — which STT model actually produced the transcript. The pipeline+ falls back silently, so a run meant to gate `gpt-4o-transcribe` that shows+ `whisper-1` here measured the fallback, not the model you asked for. A+ chunked file lists every model that contributed.+- **Stitch boundaries** — for chunked files only, the last ~10 words of each+ segment and the first ~10 of the next, plus how many words the overlap-dedup+ removed. `dropped 0` at a seam usually means the dedup failed to find the+ overlap and the transcript now repeats a phrase; a large drop means it ate+ real speech. Both show up as insertions/deletions in the WER but are only+ diagnosable here.++A file whose transcription raised is shown as `ERROR` and forces a failing exit+regardless of WER.++## Notes / limits++- The harness has no duration metadata for the files, so it takes the+ single-request vs chunked decision on the byte cap+ (`SINGLE_REQUEST_MAX_BYTES`) alone — exactly what+ `transcribe(audio, filename, duration_secs=None)` does. In production the+ frontend supplies a duration, so a long-but-small recording can chunk there+ and not here. When a chunked file's container header carries no duration+ either (the norm for browser `MediaRecorder` webm), pass `--duration-secs`;+ an upper bound is safe, since the split stops at the first empty segment.+- WER is computed with an inline Levenshtein over normalised words+ (`brain_dump_wer.py`); no `jiwer` dependency.+- The corpus is deliberately **not** committed — the recordings are personal+ speech. Keep them in shared storage and point `--dir` at a local copy.diff --git a/autogpt_platform/backend/backend/api/features/onboarding_dump/__init__.py b/autogpt_platform/backend/backend/api/features/onboarding_dump/__init__.pynew file mode 100644index 0000000000..e69de29bb2diff --git a/autogpt_platform/backend/backend/api/features/onboarding_dump/brain_dump_eval.py b/autogpt_platform/backend/backend/api/features/onboarding_dump/brain_dump_eval.pynew file mode 100644index 0000000000..1fb902813b--- /dev/null+++ b/autogpt_platform/backend/backend/api/features/onboarding_dump/brain_dump_eval.py@@ -0,0 +1,341 @@+"""Word-error-rate harness for the onboarding brain-dump transcription.++Runs the real pipeline (``transcription.transcribe``) over a directory of+``<name>.<audio ext>`` + ``<name>.txt`` pairs and reports WER per file and+pooled over the corpus. Files big enough to take the chunked path are+transcribed segment by segment here so the stitch boundaries can be shown+— seam errors are invisible in an aggregate number.++See ``EVAL.md`` for the corpus requirements and the 5% release gate.+"""++import argparse+import asyncio+import json+import statistics+import sys+import time+from pathlib import Path++from pydantic import BaseModel++from backend.api.features.onboarding_dump import transcription+from backend.api.features.onboarding_dump.brain_dump_wer import (+ WordErrors,+ compute_word_errors,+ normalize_words,+)++WER_RELEASE_GATE = 0.05+SEAM_CONTEXT_WORDS = 10+AUDIO_EXTENSIONS = (".webm", ".mp4", ".m4a", ".mp3", ".wav", ".ogg")+++class Seam(BaseModel):+ """One stitch boundary, between segment ``index`` and ``index + 1``."""++ index: int+ left_tail: str+ right_head: str+ dropped_words: int+++class PipelineRun(BaseModel):+ """What one trip through the pipeline produced, before scoring."""++ transcript: str+ language: str | None = None+ model: str+ segment_count: int+ seams: list[Seam] = []+++class FileResult(BaseModel):+ name: str+ audio_path: str+ errors: WordErrors+ latency_secs: float+ chunked: bool+ hypothesis_words: int = 0+ language: str | None = None+ model: str = ""+ """Which STT model actually produced this transcript.++ The pipeline falls back to ``FALLBACK_MODEL`` silently, so without+ this a run meant to gate ``gpt-4o-transcribe`` can pass on+ ``whisper-1``'s numbers with nothing in the report saying so.+ """+ segment_count: int = 0+ seams: list[Seam] = []+ error: str | None = None+++class EvalReport(BaseModel):+ files: list[FileResult]+ pooled: WordErrors+ mean_wer: float+ median_wer: float+ total_secs: float+ gate: float+ passed: bool+ audio_without_reference: list[str]+ reference_without_audio: list[str]+++class DumpPair(BaseModel):+ name: str+ audio_path: Path+ reference_path: Path+++class PairDiscovery(BaseModel):+ pairs: list[DumpPair]+ audio_without_reference: list[str]+ reference_without_audio: list[str]+++def main() -> None:+ parser = argparse.ArgumentParser(+ description="Measure WER of the brain-dump transcription pipeline."+ )+ parser.add_argument(+ "--dir", required=True, type=Path, help="directory of audio + .txt pairs"+ )+ parser.add_argument("--model", default=None, help="override the primary STT model")+ parser.add_argument(+ "--duration-secs",+ dest="duration_secs",+ default=None,+ type=float,+ help=(+ "fallback duration for chunked files whose container header "+ "reports none (an upper bound is safe)"+ ),+ )+ parser.add_argument(+ "--json", dest="json_path", default=None, type=Path, help="write results here"+ )+ args = parser.parse_args()++ if args.model:+ # The pipeline reads this module global per request, so assigning+ # it overrides the model for the run without touching the env.+ transcription.PRIMARY_MODEL = args.model++ report = asyncio.run(run_eval(args.dir, args.duration_secs))+ print(render_report(report))+ if args.json_path:+ args.json_path.write_text(+ json.dumps(report.model_dump(mode="json"), indent=2), encoding="utf-8"+ )+ sys.exit(0 if report.passed else 1)+++async def run_eval(directory: Path, duration_secs: float | None = None) -> EvalReport:+ discovery = discover_pairs(directory)+ started = time.monotonic()+ results = [await evaluate_pair(pair, duration_secs) for pair in discovery.pairs]+ scored = [result for result in results if result.error is None]+ pooled = WordErrors(+ substitutions=sum(r.errors.substitutions for r in scored),+ insertions=sum(r.errors.insertions for r in scored),+ deletions=sum(r.errors.deletions for r in scored),+ reference_words=sum(r.errors.reference_words for r in scored),+ )+ rates = [result.errors.wer for result in scored]+ return EvalReport(+ files=results,+ pooled=pooled,+ mean_wer=statistics.fmean(rates) if rates else 0.0,+ median_wer=statistics.median(rates) if rates else 0.0,+ total_secs=time.monotonic() - started,+ gate=WER_RELEASE_GATE,+ passed=bool(scored)+ and len(scored) == len(results)+ and pooled.wer < WER_RELEASE_GATE,+ audio_without_reference=discovery.audio_without_reference,+ reference_without_audio=discovery.reference_without_audio,+ )+++async def evaluate_pair(+ pair: DumpPair, duration_secs: float | None = None+) -> FileResult:+ audio = pair.audio_path.read_bytes()+ reference = pair.reference_path.read_text(encoding="utf-8")+ # The harness has no duration metadata, so it takes the split decision+ # on the same byte cap `transcribe(audio, name, duration_secs=None)` uses.+ chunked = len(audio) > transcription.SINGLE_REQUEST_MAX_BYTES+ started = time.monotonic()+ try:+ run = await _run_pipeline(audio, pair.audio_path.name, chunked, duration_secs)+ except Exception as e:+ return FileResult(+ name=pair.name,+ audio_path=pair.audio_path.name,+ errors=_empty_errors(reference),+ latency_secs=time.monotonic() - started,+ chunked=chunked,+ error=str(e),+ )+ return FileResult(+ name=pair.name,+ audio_path=pair.audio_path.name,+ errors=compute_word_errors(reference, run.transcript),+ latency_secs=time.monotonic() - started,+ chunked=chunked,+ hypothesis_words=len(normalize_words(run.transcript)),+ language=run.language,+ model=run.model,+ segment_count=run.segment_count,+ seams=run.seams,+ )+++def _empty_errors(reference: str) -> WordErrors:+ return WordErrors(+ substitutions=0,+ insertions=0,+ deletions=0,+ reference_words=len(normalize_words(reference)),+ )+++async def _run_pipeline(+ audio: bytes,+ filename: str,+ chunked: bool,+ duration_secs: float | None = None,+) -> PipelineRun:+ if not chunked:+ result = await transcription.transcribe(audio, filename)+ return PipelineRun(+ transcript=result.text,+ language=result.language,+ model=result.model,+ segment_count=1,+ )++ segments = await transcription.split_audio(audio, filename, duration_secs)+ # ``split_audio`` re-encodes to ogg/opus, and the STT client infers the+ # format from the filename — naming a segment after the source (e.g.+ # ``0-dump01.webm``) gets the whole corpus rejected.+ results = [+ await transcription.transcribe(+ segment, f"segment-{index}{transcription.SEGMENT_SUFFIX}"+ )+ for index, segment in enumerate(segments)+ ]+ parts = [result.text for result in results]+ return PipelineRun(+ transcript=transcription.stitch_transcripts(parts),+ model=",".join(dict.fromkeys(result.model for result in results)),+ segment_count=len(parts),+ seams=describe_seams(parts),+ )+++def describe_seams(parts: list[str]) -> list[Seam]:+ """Report what the overlap-dedup did at each stitch boundary."""+ seams: list[Seam] = []+ for index in range(len(parts) - 1):+ before = len(transcription.stitch_transcripts(parts[: index + 1]).split())+ added = len(parts[index + 1].split())+ after = len(transcription.stitch_transcripts(parts[: index + 2]).split())+ seams.append(+ Seam(+ index=index,+ left_tail=" ".join(parts[index].split()[-SEAM_CONTEXT_WORDS:]),+ right_head=" ".join(parts[index + 1].split()[:SEAM_CONTEXT_WORDS]),+ dropped_words=before + added - after,+ )+ )+ return seams+++def discover_pairs(directory: Path) -> PairDiscovery:+ entries = sorted(directory.iterdir())+ audio_by_name: dict[str, Path] = {}+ for path in entries:+ if path.suffix.lower() in AUDIO_EXTENSIONS and path.stem not in audio_by_name:+ audio_by_name[path.stem] = path+ references = {path.stem: path for path in entries if path.suffix.lower() == ".txt"}+ return PairDiscovery(+ pairs=[+ DumpPair(name=name, audio_path=path, reference_path=references[name])+ for name, path in sorted(audio_by_name.items())+ if name in references+ ],+ audio_without_reference=sorted(set(audio_by_name) - set(references)),+ reference_without_audio=sorted(set(references) - set(audio_by_name)),+ )+++def render_report(report: EvalReport) -> str:+ lines = [+ "",+ f"Brain-dump transcription WER — {len(report.files)} file(s)",+ "",+ f"{'file':<20}{'WER':>9}{'sub':>6}{'ins':>6}{'del':>6}"+ f"{'ref':>8}{'secs':>8} {'segments':<10}model",+ ]+ lines.extend(_render_row(result) for result in report.files)+ lines.extend(_render_seams(report))+ lines.extend(_render_mismatches(report))+ lines.extend(+ [+ "",+ f"pooled WER {report.pooled.wer:>8.2%} "+ f"({report.pooled.reference_words} reference words)",+ f"mean WER {report.mean_wer:>8.2%}",+ f"median WER {report.median_wer:>8.2%}",+ f"wall clock {report.total_secs:>8.1f}s",+ "",+ f"Release gate: aggregate WER must stay under {report.gate:.0%} — "+ + ("PASS (exit 0)" if report.passed else "FAIL (exit 1)"),+ ]+ )+ return "\n".join(lines)+++def _render_row(result: FileResult) -> str:+ if result.error:+ return f"{result.name:<20}{'ERROR':>9} {result.error[:60]}"+ return (+ f"{result.name:<20}{result.errors.wer:>9.2%}"+ f"{result.errors.substitutions:>6}{result.errors.insertions:>6}"+ f"{result.errors.deletions:>6}{result.errors.reference_words:>8}"+ f"{result.latency_secs:>8.1f} {result.segment_count:<10}{result.model}"+ )+++def _render_seams(report: EvalReport) -> list[str]:+ chunked = [result for result in report.files if result.seams]+ if not chunked:+ return []+ lines = ["", "Stitch boundaries (chunked files):"]+ for result in chunked:+ lines.append(f" {result.name} — {result.segment_count} segments")+ for seam in result.seams:+ lines.append(+ f" seam {seam.index}/{seam.index + 1}: "+ f"dedup dropped {seam.dropped_words} word(s)"+ )+ lines.append(f" ...{seam.left_tail}")+ lines.append(f" {seam.right_head}...")+ return lines+++def _render_mismatches(report: EvalReport) -> list[str]:+ lines: list[str] = []+ if report.audio_without_reference:+ joined = ", ".join(report.audio_without_reference)+ lines.append(f"\nAudio with no .txt reference: {joined}")+ if report.reference_without_audio:+ joined = ", ".join(report.reference_without_audio)+ lines.append(f"\n.txt reference with no audio: {joined}")+ return lines+++if __name__ == "__main__":+ main()diff --git a/autogpt_platform/backend/backend/api/features/onboarding_dump/brain_dump_eval_test.py b/autogpt_platform/backend/backend/api/features/onboarding_dump/brain_dump_eval_test.pynew file mode 100644index 0000000000..6751d00f4a--- /dev/null+++ b/autogpt_platform/backend/backend/api/features/onboarding_dump/brain_dump_eval_test.py@@ -0,0 +1,254 @@+from pathlib import Path+from unittest.mock import AsyncMock++import pytest++from backend.api.features.onboarding_dump import brain_dump_eval, transcription+from backend.api.features.onboarding_dump.brain_dump_wer import (+ compute_word_errors,+ normalize_words,+)+from backend.api.features.onboarding_dump.transcription import TranscriptionResult+++def test_identical_text_has_no_errors():+ errors = compute_word_errors("the quick brown fox", "the quick brown fox")++ assert errors.wer == 0.0+ assert (errors.substitutions, errors.insertions, errors.deletions) == (0, 0, 0)+ assert errors.reference_words == 4+++def test_substitution_is_counted_once():+ errors = compute_word_errors("the quick brown fox", "the quick green fox")++ assert (errors.substitutions, errors.insertions, errors.deletions) == (1, 0, 0)+ assert errors.wer == pytest.approx(0.25)+++def test_insertion_is_counted_once():+ errors = compute_word_errors("the quick brown fox", "the very quick brown fox")++ assert (errors.substitutions, errors.insertions, errors.deletions) == (0, 1, 0)+ assert errors.wer == pytest.approx(0.25)+++def test_deletion_is_counted_once():+ errors = compute_word_errors("the quick brown fox", "the quick fox")++ assert (errors.substitutions, errors.insertions, errors.deletions) == (0, 0, 1)+ assert errors.wer == pytest.approx(0.25)+++def test_mixed_operations_are_reported_separately():+ errors = compute_word_errors("a b c d", "a x c d e")++ assert (errors.substitutions, errors.insertions, errors.deletions) == (1, 1, 0)+ assert errors.wer == pytest.approx(0.5)+++def test_empty_hypothesis_deletes_every_reference_word():+ errors = compute_word_errors("one two three", "")++ assert errors.deletions == 3+ assert errors.wer == pytest.approx(1.0)+++def test_empty_reference_has_zero_wer():+ errors = compute_word_errors("", "hallucinated words here")++ assert errors.insertions == 3+ assert errors.wer == 0.0+++def test_normalisation_ignores_case_punctuation_and_whitespace():+ reference = "So, I built AutoGPT — it's great!"+ hypothesis = "so i built autogpt its great"++ assert normalize_words(reference) == normalize_words(hypothesis)+ assert compute_word_errors(reference, hypothesis).wer == 0.0+++def test_normalisation_splits_on_hyphens_and_slashes():+ assert normalize_words("well-known and/or 24_7") == [+ "well",+ "known",+ "and",+ "or",+ "24",+ "7",+ ]+++def test_normalisation_handles_non_english_punctuation():+ assert normalize_words("¿Qué tal? Muy bien.") == ["qué", "tal", "muy", "bien"]+++def _result(text: str, language: str | None = None) -> TranscriptionResult:+ return TranscriptionResult(+ text=text, language=language, model=transcription.PRIMARY_MODEL+ )+++def _write_pair(directory: Path, name: str, extension: str) -> None:+ (directory / f"{name}{extension}").write_bytes(b"audio-bytes")+ (directory / f"{name}.txt").write_text("hello world", encoding="utf-8")+++def test_discover_pairs_matches_by_basename(tmp_path: Path):+ _write_pair(tmp_path, "dump01", ".webm")+ _write_pair(tmp_path, "dump02", ".mp3")++ discovery = brain_dump_eval.discover_pairs(tmp_path)++ assert [pair.name for pair in discovery.pairs] == ["dump01", "dump02"]+ assert discovery.pairs[0].audio_path == tmp_path / "dump01.webm"+ assert discovery.pairs[0].reference_path == tmp_path / "dump01.txt"+++def test_discover_pairs_accepts_every_supported_extension(tmp_path: Path):+ for index, extension in enumerate(brain_dump_eval.AUDIO_EXTENSIONS):+ _write_pair(tmp_path, f"dump{index}", extension)++ discovery = brain_dump_eval.discover_pairs(tmp_path)++ assert len(discovery.pairs) == len(brain_dump_eval.AUDIO_EXTENSIONS)+++def test_discover_pairs_reports_unmatched_files(tmp_path: Path):+ _write_pair(tmp_path, "dump01", ".webm")+ (tmp_path / "lonely_audio.wav").write_bytes(b"audio-bytes")+ (tmp_path / "lonely_reference.txt").write_text("text", encoding="utf-8")+ (tmp_path / "notes.pdf").write_bytes(b"ignored")++ discovery = brain_dump_eval.discover_pairs(tmp_path)++ assert [pair.name for pair in discovery.pairs] == ["dump01"]+ assert discovery.audio_without_reference == ["lonely_audio"]+ assert discovery.reference_without_audio == ["lonely_reference"]+++async def test_run_eval_scores_every_pair(tmp_path: Path, monkeypatch):+ (tmp_path / "dump01.webm").write_bytes(b"audio")+ (tmp_path / "dump01.txt").write_text("the quick brown fox", encoding="utf-8")+ (tmp_path / "dump02.mp3").write_bytes(b"audio")+ (tmp_path / "dump02.txt").write_text("hello there world", encoding="utf-8")+ transcribe = AsyncMock(+ side_effect=[+ _result("The quick brown fox!", "en"),+ _result("hello world", "en"),+ ]+ )+ monkeypatch.setattr(transcription, "transcribe", transcribe)++ report = await brain_dump_eval.run_eval(tmp_path)++ assert transcribe.await_count == 2+ assert [result.errors.wer for result in report.files] == [0.0, pytest.approx(1 / 3)]+ assert report.pooled.deletions == 1+ assert report.pooled.reference_words == 7+ assert report.passed is False+ assert "FAIL (exit 1)" in brain_dump_eval.render_report(report)+++async def test_run_eval_passes_when_under_the_gate(tmp_path: Path, monkeypatch):+ (tmp_path / "dump01.webm").write_bytes(b"audio")+ (tmp_path / "dump01.txt").write_text("the quick brown fox", encoding="utf-8")+ monkeypatch.setattr(+ transcription,+ "transcribe",+ AsyncMock(return_value=_result("the quick brown fox")),+ )++ report = await brain_dump_eval.run_eval(tmp_path)++ assert report.passed is True+ assert report.gate == brain_dump_eval.WER_RELEASE_GATE+ assert "PASS (exit 0)" in brain_dump_eval.render_report(report)+++async def test_run_eval_reports_which_model_produced_the_transcript(+ tmp_path: Path, monkeypatch+):+ """A silent fallback must not be able to pass as the gated model."""+ (tmp_path / "dump01.webm").write_bytes(b"audio")+ (tmp_path / "dump01.txt").write_text("the quick brown fox", encoding="utf-8")+ monkeypatch.setattr(+ transcription,+ "transcribe",+ AsyncMock(+ return_value=TranscriptionResult(+ text="the quick brown fox", model=transcription.FALLBACK_MODEL+ )+ ),+ )++ report = await brain_dump_eval.run_eval(tmp_path)++ assert report.files[0].model == transcription.FALLBACK_MODEL+ assert transcription.FALLBACK_MODEL in brain_dump_eval.render_report(report)+++async def test_run_eval_records_pipeline_failures(tmp_path: Path, monkeypatch):+ (tmp_path / "dump01.webm").write_bytes(b"audio")+ (tmp_path / "dump01.txt").write_text("some words here", encoding="utf-8")+ monkeypatch.setattr(+ transcription,+ "transcribe",+ AsyncMock(side_effect=transcription.TranscriptionFailedError("boom")),+ )++ report = await brain_dump_eval.run_eval(tmp_path)++ assert report.files[0].error == "boom"+ assert report.pooled.reference_words == 0+ assert report.passed is False+++async def test_run_eval_reports_seams_for_chunked_files(tmp_path: Path, monkeypatch):+ (tmp_path / "dump01.webm").write_bytes(b"audio")+ (tmp_path / "dump01.txt").write_text("one two three four", encoding="utf-8")+ monkeypatch.setattr(transcription, "SINGLE_REQUEST_MAX_BYTES", 1)+ split = AsyncMock(return_value=[b"a", b"b"])+ monkeypatch.setattr(transcription, "split_audio", split)+ transcribe = AsyncMock(+ side_effect=[_result("one two three"), _result("two three four")]+ )+ monkeypatch.setattr(transcription, "transcribe", transcribe)++ report = await brain_dump_eval.run_eval(tmp_path, duration_secs=1800)+ result = report.files[0]++ assert result.chunked is True+ assert result.segment_count == 2+ # The split re-encodes to ogg/opus and the STT client infers the format+ # from the filename, so a segment must not keep the source extension.+ assert [call.args[1] for call in transcribe.await_args_list] == [+ "segment-0.ogg",+ "segment-1.ogg",+ ]+ # A `.webm` with no container duration cannot be split without a hint.+ assert split.await_args.args[2] == 1800+ assert result.errors.wer == 0.0+ assert result.seams == [+ brain_dump_eval.Seam(+ index=0,+ left_tail="one two three",+ right_head="two three four",+ dropped_words=2,+ )+ ]+ assert "seam 0/1: dedup dropped 2 word(s)" in brain_dump_eval.render_report(report)+++def test_describe_seams_truncates_context_to_ten_words():+ left = " ".join(f"l{index}" for index in range(30))+ right = " ".join(f"r{index}" for index in range(30))++ seams = brain_dump_eval.describe_seams([left, right])++ assert len(seams[0].left_tail.split()) == brain_dump_eval.SEAM_CONTEXT_WORDS+ assert len(seams[0].right_head.split()) == brain_dump_eval.SEAM_CONTEXT_WORDS+ assert seams[0].left_tail.startswith("l20")+ assert seams[0].right_head.endswith("r9")+ assert seams[0].dropped_words == 0diff --git a/autogpt_platform/backend/backend/api/features/onboarding_dump/brain_dump_wer.py b/autogpt_platform/backend/backend/api/features/onboarding_dump/brain_dump_wer.pynew file mode 100644index 0000000000..b028fa6a13--- /dev/null+++ b/autogpt_platform/backend/backend/api/features/onboarding_dump/brain_dump_wer.py@@ -0,0 +1,89 @@+"""Word error rate for the brain-dump transcription eval.++An inline Levenshtein over words rather than ``jiwer``: the algorithm is+~20 lines and keeping it here means the harness runs anywhere without an+extra dependency. Used by ``brain_dump_eval.py``.+"""++import unicodedata++from pydantic import BaseModel, computed_field++# Word separators inside a token: a reference writing "well known" and a+# transcript writing "well-known" is a formatting difference, not an error.+SPACING_PUNCTUATION = frozenset({"-", "–", "—", "/", "_"})+++class WordErrors(BaseModel):+ substitutions: int+ insertions: int+ deletions: int+ reference_words: int++ @computed_field+ @property+ def wer(self) -> float:+ if self.reference_words == 0:+ return 0.0+ total = self.substitutions + self.insertions + self.deletions+ return total / self.reference_words+++def compute_word_errors(reference: str, hypothesis: str) -> WordErrors:+ """Count substitutions/insertions/deletions of ``hypothesis`` vs ``reference``."""+ ref = normalize_words(reference)+ hyp = normalize_words(hypothesis)+ return _count_operations(ref, hyp, _edit_matrix(ref, hyp))+++def normalize_words(text: str) -> list[str]:+ """Lowercase, drop punctuation and collapse whitespace into words."""+ cleaned = [+ (+ " "+ if char in SPACING_PUNCTUATION+ else "" if unicodedata.category(char).startswith("P") else char+ )+ for char in text.lower()+ ]+ return "".join(cleaned).split()+++def _edit_matrix(ref: list[str], hyp: list[str]) -> list[list[int]]:+ costs = [[0] * (len(hyp) + 1) for _ in range(len(ref) + 1)]+ for i in range(len(ref) + 1):+ costs[i][0] = i+ for j in range(len(hyp) + 1):+ costs[0][j] = j+ for i in range(1, len(ref) + 1):+ for j in range(1, len(hyp) + 1):+ if ref[i - 1] == hyp[j - 1]:+ costs[i][j] = costs[i - 1][j - 1]+ continue+ costs[i][j] = 1 + min(costs[i - 1][j - 1], costs[i][j - 1], costs[i - 1][j])+ return costs+++def _count_operations(+ ref: list[str], hyp: list[str], costs: list[list[int]]+) -> WordErrors:+ substitutions = insertions = deletions = 0+ i, j = len(ref), len(hyp)+ while i > 0 or j > 0:+ if i > 0 and j > 0 and ref[i - 1] == hyp[j - 1]:+ i, j = i - 1, j - 1+ elif i > 0 and j > 0 and costs[i][j] == costs[i - 1][j - 1] + 1:+ substitutions += 1+ i, j = i - 1, j - 1+ elif j > 0 and costs[i][j] == costs[i][j - 1] + 1:+ insertions += 1+ j -= 1+ else:+ deletions += 1+ i -= 1+ return WordErrors(+ substitutions=substitutions,+ insertions=insertions,+ deletions=deletions,+ reference_words=len(ref),+ )diff --git a/autogpt_platform/backend/backend/api/features/onboarding_dump/db.py b/autogpt_platform/backend/backend/api/features/onboarding_dump/db.pynew file mode 100644index 0000000000..ff0cf049ac--- /dev/null+++ b/autogpt_platform/backend/backend/api/features/onboarding_dump/db.py@@ -0,0 +1,188 @@+"""Persistence for the onboarding brain dump.++One row per user (``userId`` is unique): re-recording replaces the take in+place, and ``recordingId`` identifies the current one so ``finalize`` can+be retried safely.+"""++from typing import Any++from prisma import Json+from prisma.enums import BrainDumpInputMode, BrainDumpStatus+from prisma.models import OnboardingBrainDump+++async def get_dump(user_id: str) -> OnboardingBrainDump | None:+ return await OnboardingBrainDump.prisma().find_unique(where={"userId": user_id})+++async def owns_dump(user_id: str, recording_id: str) -> bool:+ """Whether ``recording_id`` is still the take the row belongs to.++ Row writes carry their own ``recordingId`` guard, but the business+ understanding is shared user context with no take on it. A background+ job for a superseded take has to be told to stop before it writes+ there, and this is what tells it.+ """+ dump = await get_dump(user_id)+ return dump is not None and dump.recordingId == recording_id+++# A take that has reached any of these is being processed, or is already+# done. Re-claiming it would knock it back to the start of the pipeline.+# ``failed`` is deliberately absent: retrying a failed take must reset it.+_IN_FLIGHT_STATUSES = frozenset(+ {+ BrainDumpStatus.recording_uploaded,+ BrainDumpStatus.transcribing,+ BrainDumpStatus.transcribed,+ BrainDumpStatus.extracting,+ BrainDumpStatus.completed,+ }+)++# Everything a single take owns. One row per user means a new take+# inherits whatever the last one left here, so unless these are cleared+# ``/recording``, ``/intro`` and ``/recommended-providers`` keep serving+# the *previous* take's audio, transcript, greeting and picks whenever the+# new one fails, is skipped or is abandoned half-recorded.+#+# ``greetingSeen`` belongs with them. It is only ever written True and+# short-circuits the intro endpoint, so a new take that inherited it would+# run the whole pipeline for a greeting that could never render. Clearing+# it is only safe *because* the transcript goes too: a take that never+# produces a greeting leaves nothing to reflect back, so the intro falls+# through to Path B rather than waiting on a greeting that isn't coming.+_TAKE_OWNED_RESET: dict[str, Any] = {+ "audioPath": None,+ "mimeType": None,+ "sizeBytes": None,+ "durationSecs": None,+ "transcript": None,+ "transcriptLang": None,+ "greeting": None,+ "suggestedPrompts": Json([]),+ "recommendedProviders": Json(None),+ "greetingSeen": False,+}+++async def start_dump(+ user_id: str,+ recording_id: str,+ input_mode: BrainDumpInputMode,+) -> OnboardingBrainDump:+ """Claim the row for ``recording_id``, resetting any prior take's state.++ A take already moving through the pipeline is returned untouched when+ recovery replays part 0 or finalize is retried. Typed and skipped+ finalizes also cannot displace an active take with a different id: unlike+ part 0 of a voice upload, they carry no earlier server-side event that can+ establish which tab's request is newer. A different voice recording id is+ a deliberate new take and claims the row, taking every take-owned column+ with it.+ """+ existing = await get_dump(user_id)+ if (+ existing+ and existing.status in _IN_FLIGHT_STATUSES+ and (+ existing.recordingId == recording_id+ or input_mode != BrainDumpInputMode.voice+ )+ ):+ return existing++ is_new_take = existing is None or existing.recordingId != recording_id+ fields: dict[str, Any] = {+ "recordingId": recording_id,+ "status": BrainDumpStatus.recording_uploaded,+ "inputMode": input_mode,+ "errorCode": None,+ **(_TAKE_OWNED_RESET if is_new_take else {}),+ }+ return await OnboardingBrainDump.prisma().upsert(+ where={"userId": user_id},+ data={+ "create": {"userId": user_id, **_TAKE_OWNED_RESET, **fields},+ "update": fields,+ },+ )+++async def update_dump(user_id: str, recording_id: str, **fields: Any) -> bool:+ """Advance one take's row, and only that take's.++ ``recordingId`` is part of the ``WHERE`` on purpose. There is one row+ per user, so a second tab starting a new take moves the row on while+ the old take's transcription is still running — minutes, for a long+ dump. Scoped on ``userId`` alone, that old take's transcript, greeting+ and failure code all land on the *new* take's row, and the new+ recording is never transcribed at all.++ Returns ``False`` when nothing matched: no row (a status update for a+ take that was never started is a no-op, not an error, so a+ late-arriving retry can't resurrect a deleted user's dump) or a take+ that has since been superseded.+ """+ updated = await OnboardingBrainDump.prisma().update_many(+ where={"userId": user_id, "recordingId": recording_id},+ data=fields,+ )+ return updated == 1+++async def claim_transition(+ user_id: str,+ recording_id: str,+ *,+ expected: BrainDumpStatus,+ new: BrainDumpStatus,+ **fields: Any,+) -> bool:+ """Move a take from ``expected`` to ``new``, once.++ Returns ``True`` only for the caller that won. The read-then-write+ idempotency guards in the service can both be passed by two finalize+ requests that arrive together; this cannot, because the status is+ part of the ``WHERE`` clause, so the database decides the winner.+ """+ updated = await OnboardingBrainDump.prisma().update_many(+ where={+ "userId": user_id,+ "recordingId": recording_id,+ "status": expected,+ },+ data={"status": new, **fields},+ )+ return updated == 1+++async def mark_failed(user_id: str, recording_id: str, error_code: str) -> None:+ """Record a terminal failure without ever dropping the stored audio."""+ await update_dump(+ user_id, recording_id, status=BrainDumpStatus.failed, errorCode=error_code+ )+++async def mark_greeting_seen(user_id: str) -> None:+ """Set the greeting-done flag, creating a stub row when none exists.++ Users who never reached the dump (or whose browser lost the local+ flag) still need "seen" to stick server-side, so this upserts a+ minimal skipped-completed row rather than silently no-opping.+ """+ await OnboardingBrainDump.prisma().upsert(+ where={"userId": user_id},+ data={+ "create": {+ "userId": user_id,+ **_TAKE_OWNED_RESET,+ "recordingId": "greeting-only",+ "status": BrainDumpStatus.completed,+ "inputMode": BrainDumpInputMode.skipped,+ "greetingSeen": True,+ },+ "update": {"greetingSeen": True},+ },+ )diff --git a/autogpt_platform/backend/backend/api/features/onboarding_dump/db_test.py b/autogpt_platform/backend/backend/api/features/onboarding_dump/db_test.pynew file mode 100644index 0000000000..4dbe456cdb--- /dev/null+++ b/autogpt_platform/backend/backend/api/features/onboarding_dump/db_test.py@@ -0,0 +1,238 @@+"""Guards on claiming the one-row-per-user brain dump."""++from unittest.mock import AsyncMock, MagicMock++import pytest+from prisma import Json+from prisma.enums import BrainDumpInputMode, BrainDumpStatus+from prisma.models import OnboardingBrainDump+from pytest_mock import MockerFixture++from backend.api.features.onboarding_dump import db++USER_ID = "user-1"+RECORDING_ID = "rec-1"+++def _row(status: BrainDumpStatus, recording_id: str = RECORDING_ID):+ return OnboardingBrainDump.model_construct(+ userId=USER_ID,+ recordingId=recording_id,+ status=status,+ inputMode=BrainDumpInputMode.voice,+ transcript=None,+ errorCode=None,+ )+++def upsert(mocker: MockerFixture) -> AsyncMock:+ mock = AsyncMock(return_value=_row(BrainDumpStatus.recording_uploaded))+ mocker.patch.object(+ OnboardingBrainDump, "prisma", MagicMock(return_value=MagicMock(upsert=mock))+ )+ return mock+++ "status",+ [+ BrainDumpStatus.transcribing,+ BrainDumpStatus.transcribed,+ BrainDumpStatus.extracting,+ BrainDumpStatus.completed,+ ],+)+async def test_the_same_take_is_left_alone_once_it_is_moving(+ mocker: MockerFixture, upsert: AsyncMock, status: BrainDumpStatus+):+ """Recovery replays part 0, and finalize can be retried.++ Both land back here after the dump is already being processed. Left+ unguarded they reset the row to ``recording_uploaded`` underneath the+ running pipeline.+ """+ existing = _row(status)+ mocker.patch.object(db, "get_dump", AsyncMock(return_value=existing))++ result = await db.start_dump(USER_ID, RECORDING_ID, BrainDumpInputMode.voice)++ assert result is existing+ assert result.status == status+ upsert.assert_not_awaited()+++async def test_a_failed_take_is_reclaimed_so_retry_works(+ mocker: MockerFixture, upsert: AsyncMock+):+ mocker.patch.object(+ db, "get_dump", AsyncMock(return_value=_row(BrainDumpStatus.failed))+ )++ await db.start_dump(USER_ID, RECORDING_ID, BrainDumpInputMode.voice)++ upsert.assert_awaited_once()+++async def test_a_new_recording_id_always_claims_the_row(+ mocker: MockerFixture, upsert: AsyncMock+):+ """Re-recording is a new take, even mid-pipeline on the old one."""+ mocker.patch.object(+ db,+ "get_dump",+ AsyncMock(return_value=_row(BrainDumpStatus.extracting, "rec-previous")),+ )++ await db.start_dump(USER_ID, RECORDING_ID, BrainDumpInputMode.voice)++ upsert.assert_awaited_once()+ update = upsert.await_args.kwargs["data"]["update"]+ assert update["recordingId"] == RECORDING_ID+ # ``greetingSeen`` is only ever written True and short-circuits the+ # intro endpoint, so a new take that inherited it would run the whole+ # pipeline for a greeting that can never render.+ assert update["greetingSeen"] is False+++ "status",+ [BrainDumpStatus.recording_uploaded, BrainDumpStatus.completed],+)+async def test_a_delayed_typed_finalize_cannot_replace_an_active_take(+ mocker: MockerFixture, upsert: AsyncMock, status: BrainDumpStatus+):+ existing = _row(status, "rec-newer")+ mocker.patch.object(db, "get_dump", AsyncMock(return_value=existing))++ result = await db.start_dump(USER_ID, RECORDING_ID, BrainDumpInputMode.typed)++ assert result is existing+ upsert.assert_not_awaited()+++async def test_a_new_recording_id_clears_every_column_the_last_take_owned(+ mocker: MockerFixture, upsert: AsyncMock+):+ """A new take must not be able to answer with the old take's work.++ There is one row per user, so anything left behind here is served as+ if it belonged to the new recording: ``/recording`` hands back the+ previous audio, ``/intro`` its greeting and transcript, and+ ``/recommended-providers`` its picks — for a take that may never get+ past its first part.+ """+ mocker.patch.object(+ db,+ "get_dump",+ AsyncMock(return_value=_row(BrainDumpStatus.completed, "rec-previous")),+ )++ await db.start_dump(USER_ID, RECORDING_ID, BrainDumpInputMode.voice)++ update = upsert.await_args.kwargs["data"]["update"]+ assert update["audioPath"] is None+ assert update["mimeType"] is None+ assert update["sizeBytes"] is None+ assert update["durationSecs"] is None+ assert update["transcript"] is None+ assert update["transcriptLang"] is None+ assert update["greeting"] is None+ assert update["recommendedProviders"] == Json(None)+ # Non-nullable with a ``[]`` default, so it is emptied, not nulled.+ assert update["suggestedPrompts"] == Json([])+++async def test_retrying_the_same_take_keeps_what_that_take_produced(+ mocker: MockerFixture, upsert: AsyncMock+):+ """A retry is the same recording, not a replacement for it.++ Re-uploading part 0 after a failed finalize, or finalizing again, has+ to leave the audio and transcript that retry still needs.+ """+ mocker.patch.object(+ db, "get_dump", AsyncMock(return_value=_row(BrainDumpStatus.failed))+ )++ await db.start_dump(USER_ID, RECORDING_ID, BrainDumpInputMode.voice)++ create = upsert.await_args.kwargs["data"]["create"]+ update = upsert.await_args.kwargs["data"]["update"]+ assert create["recommendedProviders"] == Json(None)+ assert set(update) == {"recordingId", "status", "inputMode", "errorCode"}+++async def test_greeting_only_create_sets_recommended_providers(+ upsert: AsyncMock,+):+ await db.mark_greeting_seen(USER_ID)++ create = upsert.await_args.kwargs["data"]["create"]+ assert create["recommendedProviders"] == Json(None)+ assert create["greetingSeen"] is True+++async def test_ownership_is_lost_as_soon_as_another_take_claims_the_row(+ mocker: MockerFixture,+):+ """Guards the one write that is not scoped to a take.++ The business understanding is shared user context, so the background+ job checks this before writing there.+ """+ mocker.patch.object(+ db,+ "get_dump",+ AsyncMock(return_value=_row(BrainDumpStatus.transcribing, "rec-2")),+ )+ assert await db.owns_dump(USER_ID, RECORDING_ID) is False++ mocker.patch.object(db, "get_dump", AsyncMock(return_value=None))+ assert await db.owns_dump(USER_ID, RECORDING_ID) is False++ mocker.patch.object(+ db, "get_dump", AsyncMock(return_value=_row(BrainDumpStatus.transcribing))+ )+ assert await db.owns_dump(USER_ID, RECORDING_ID) is True+++async def test_a_write_for_a_superseded_take_hits_nothing(mocker: MockerFixture):+ """Post-claim writes are scoped to the take that made them.++ One row per user means a second tab's take can own the row by the time+ a long transcription finishes. On ``userId`` alone the old take's+ transcript and greeting land on the new take's row.+ """+ update_many = AsyncMock(return_value=0)+ mocker.patch.object(+ OnboardingBrainDump,+ "prisma",+ MagicMock(return_value=MagicMock(update_many=update_many)),+ )++ updated = await db.update_dump(USER_ID, RECORDING_ID, transcript="anything")++ assert updated is False+ assert update_many.await_args.kwargs["where"] == {+ "userId": USER_ID,+ "recordingId": RECORDING_ID,+ }+++async def test_a_first_take_creates_the_row(mocker: MockerFixture, upsert: AsyncMock):+ mocker.patch.object(db, "get_dump", AsyncMock(return_value=None))++ await db.start_dump(USER_ID, RECORDING_ID, BrainDumpInputMode.voice)++ upsert.assert_awaited_once()diff --git a/autogpt_platform/backend/backend/api/features/onboarding_dump/intro.py b/autogpt_platform/backend/backend/api/features/onboarding_dump/intro.pynew file mode 100644index 0000000000..2c2a718e65--- /dev/null+++ b/autogpt_platform/backend/backend/api/features/onboarding_dump/intro.py@@ -0,0 +1,328 @@+"""The copilot home's onboarding greeting: what AutoPilot says it heard.++Generated while the onboarding loading screen is still up, then stored —+landing on ``/copilot`` must never wait on a model. If generation fails the+greeting falls back to a template, so there is always something to show.++The generation instructions live in Langfuse (``BRAIN_DUMP_GREETING_PROMPT_NAME``)+so they can be tuned without a deploy; ``_LOCAL_PROMPT`` below is the+fallback when Langfuse is unconfigured or unreachable.+"""++import asyncio+import json+import logging+import os+import re++from langfuse import get_client++from backend.api.features.onboarding_dump.models import SuggestedPrompt+from backend.util.clients import get_openai_client+from backend.util.settings import Settings++logger = logging.getLogger(__name__)++settings = Settings()++_MODEL = os.environ.get("BRAIN_DUMP_GREETING_MODEL", "anthropic/claude-sonnet-5")+_TIMEOUT_SECONDS = 30++LANGFUSE_PROMPT_NAME = os.environ.get(+ "BRAIN_DUMP_GREETING_PROMPT_NAME", "Brain Dump Greeting"+)+LANGFUSE_PROMPT_CACHE_TTL_SECONDS = 300++MIN_PROMPTS = 5+MAX_PROMPTS = 6+MAX_GREETING_CHARS = 500+MAX_TITLE_CHARS = 120+MAX_PROMPT_CHARS = 2_000++# Phosphor icon slugs the model may pick from — the frontend maps these to+# real icon components and falls back to "sparkle" for anything else.+PROMPT_ICONS = frozenset(+ {+ "sparkle",+ "chart-bar",+ "envelope",+ "magnifying-glass",+ "calendar-check",+ "bell",+ "rocket-launch",+ "file-text",+ "globe",+ "code",+ "newspaper",+ "users",+ "shopping-cart",+ "chats",+ "lightning",+ "target",+ "robot",+ "clock",+ "megaphone",+ "currency-dollar",+ }+)+DEFAULT_PROMPT_ICON = "sparkle"++# The local fallback for the Langfuse-managed instructions. The greeting+# reflects the dump back so the user can see they were heard; the suggested+# prompts are what turn that into an action. A Langfuse edit must keep the+# same JSON contract — a malformed generation degrades to the template.+_LOCAL_PROMPT = """You are AutoPilot, an AI teammate that can run real \+recurring automations: watch sources, draft content, send digests, build \+agents that work while the user sleeps. A new user just recorded a short \+spoken brain dump about their work. Write the greeting they will see when \+they first open the app.++Return ONLY valid JSON with exactly these keys:+- "greeting": 2-3 sentences, max 450 characters, second person, warm and \+concrete. Show them you listened by naming the specific things they \+actually said. This is their FIRST time in the app — never say "welcome \+back" or imply any prior visit or conversation. The app already renders \+"Hey, <name>" directly above this text, so NEVER use the user's name or \+any other salutation ("good to have you here", "welcome") — open \+directly with what you heard. Do not ask a question, do not promise \+anything you were not told about, and never mention what you cannot do \+or cannot reach — talk only about what you will take on.+- "prompts": an array of 5-6 objects, each with:+ - "title": 8-14 words. Each title must feel like a small product built \+specifically for THIS person — assembled from the exact tools, projects, \+clients and pains they named. The reaction you are engineering is "wait, \+I can do THAT here?". Prefer compound automations that chain steps into \+one flow (watch a source → extract what matters → draft the response → \+deliver it on a schedule) over single actions. The test: if a title \+could be shown to a random stranger and still make sense, it is too \+generic — rewrite it until it could only belong to this user. Banned: \+"automate repetitive tasks", "organize your work", "stay on top of", \+"streamline", "boost productivity" and anything equally hollow.+ - "prompt": the full message to send on their behalf if they pick it — \+2-4 sentences, first person as the user, carrying over the concrete \+details from the title so work can start immediately.+ - "icon": the best-fitting slug from exactly this list: sparkle, \+chart-bar, envelope, magnifying-glass, calendar-check, bell, \+rocket-launch, file-text, globe, code, newspaper, users, shopping-cart, \+chats, lightning, target, robot, clock, megaphone, currency-dollar.++Example of the required jump in specificity (do NOT copy these, derive \+your own from the transcript):+- weak: "Automate your email follow-ups"+- strong: "Chase unpaid Shopify invoices with polite follow-ups that \+escalate every 3 days"+- weak: "Get a summary of your meetings"+- strong: "Turn every sales call into a CRM update and a follow-up \+draft before you're back at your desk"++Order the prompts most-valuable first. If the transcript is thin, fill \+the remaining slots with the most impressive broadly useful automations \+you can offer (a morning digest of their industry's news, an always-on \+competitor monitor, a weekly auto-drafted report) — but never invent \+personal details that are not in the transcript.++Transcript:+"""+++async def generate_intro(transcript: str) -> tuple[str, list[SuggestedPrompt]]:+ """Return ``(greeting, prompts)`` for ``transcript``.++ Never raises: a failed or malformed generation degrades to the+ template below rather than costing the user their greeting.+ """+ text = transcript.strip()+ if not text:+ return fallback_intro(text)++ client = get_openai_client(prefer_openrouter=True)+ if client is None:+ logger.warning("Brain dump greeting: no LLM client configured")+ return fallback_intro(text)++ instructions = await _fetch_langfuse_prompt() or _LOCAL_PROMPT+ data = None+ # Two attempts: at temperature 0.6 an occasional generation comes back+ # truncated or malformed, and one retry is far cheaper than shipping+ # the generic fallback to a brand-new user.+ for attempt in range(2):+ try:+ response = await asyncio.wait_for(+ client.chat.completions.create(+ model=_MODEL,+ messages=[{"role": "user", "content": f"{instructions}{text}"}],+ temperature=0.6,+ max_tokens=3000,+ ),+ timeout=_TIMEOUT_SECONDS,+ )+ data = _parse_response_json(response.choices[0].message.content or "")+ except Exception as e: # degrades to the template below+ logger.warning(+ "Brain dump greeting generation failed (attempt %s): %s",+ attempt + 1,+ e,+ )+ continue+ if data is not None:+ break+ logger.warning("Brain dump greeting: non-JSON output (attempt %s)", attempt + 1)+ if data is None:+ return fallback_intro(text)++ greeting = data.get("greeting")+ if not isinstance(greeting, str) or not greeting.strip():+ return fallback_intro(text)++ prompts = _parse_prompts(data.get("prompts"))+ if len(prompts) < MIN_PROMPTS:+ # A greeting with two suggestions under it looks broken; the+ # generic set is better than a half-empty one.+ return greeting.strip()[:MAX_GREETING_CHARS], fallback_prompts()+ return greeting.strip()[:MAX_GREETING_CHARS], prompts[:MAX_PROMPTS]+++async def _fetch_langfuse_prompt() -> str | None:+ """Fetch the greeting instructions from Langfuse.++ Returns the compiled prompt string, or None when Langfuse is+ unconfigured or the fetch fails — the caller falls back to+ ``_LOCAL_PROMPT`` so a Langfuse outage never blocks onboarding.+ """+ if not (+ settings.secrets.langfuse_public_key and settings.secrets.langfuse_secret_key+ ):+ return None+ try:+ prompt = await asyncio.to_thread(+ get_client().get_prompt,+ LANGFUSE_PROMPT_NAME,+ cache_ttl_seconds=LANGFUSE_PROMPT_CACHE_TTL_SECONDS,+ )+ return prompt.compile()+ except Exception as e: # local prompt is the fallback+ logger.warning("Brain dump greeting: Langfuse prompt fetch failed: %s", e)+ return None+++def _parse_response_json(content: str) -> dict | None:+ """Parse the model's JSON, tolerating markdown fences and preamble.++ Anthropic models have no OpenAI-style JSON mode, so the contract is+ prompt-level ("return ONLY valid JSON") and the parser forgives the+ two ways that commonly bends: a ```json fence around the object, or+ stray prose before/after it.+ """+ text = content.strip()+ if text.startswith("```"):+ text = re.sub(r"^```[a-zA-Z]*\s*", "", text)+ text = re.sub(r"\s*```$", "", text)+ try:+ data = json.loads(text)+ except json.JSONDecodeError:+ start, end = text.find("{"), text.rfind("}")+ if start == -1 or end <= start:+ return None+ try:+ data = json.loads(text[start : end + 1])+ except json.JSONDecodeError:+ return None+ return data if isinstance(data, dict) else None+++def _parse_prompts(raw: object) -> list[SuggestedPrompt]:+ if not isinstance(raw, list):+ return []+ prompts = []+ for item in raw:+ if not isinstance(item, dict):+ continue+ title, prompt = item.get("title"), item.get("prompt")+ if not (isinstance(title, str) and title.strip()):+ continue+ if not (isinstance(prompt, str) and prompt.strip()):+ continue+ icon = item.get("icon")+ prompts.append(+ SuggestedPrompt(+ title=title.strip()[:MAX_TITLE_CHARS],+ prompt=prompt.strip()[:MAX_PROMPT_CHARS],+ icon=icon if icon in PROMPT_ICONS else DEFAULT_PROMPT_ICON,+ )+ )+ return prompts+++def fallback_intro(transcript: str) -> tuple[str, list[SuggestedPrompt]]:+ """A greeting that is true even when the model gave us nothing.++ Deliberately makes no claim about *what* was said — inventing detail+ here would be worse than being generic.+ """+ if not transcript.strip():+ return (+ "I'm ready when you are. Tell me what your week looks like and "+ "I'll find the parts worth handing over. Here are a few places "+ "we could start.",+ fallback_prompts(),+ )+ return (+ "Thanks for talking me through your work — I've got it. "+ "Here are a few places I can start.",+ fallback_prompts(),+ )+++def fallback_prompts() -> list[SuggestedPrompt]:+ """Generic starters used when generation fails or there is no dump.++ Phrased as things the user might genuinely want on day one, not as+ claims about this particular user.+ """+ return [+ SuggestedPrompt(+ title="Build an agent that drafts your Monday status report before you wake up",+ prompt=(+ "I want a recurring weekly summary of my work. Ask me what "+ "sources to pull from, then draft the first report so I can "+ "see the format before it goes on a schedule."+ ),+ icon="calendar-check",+ ),+ SuggestedPrompt(+ title="Get a sourced research brief on any topic in minutes, not afternoons",+ prompt=(+ "I need research support. Let me give you a topic and I want "+ "back a concise brief with sources — start by asking me what "+ "I'm currently trying to learn about."+ ),+ icon="magnifying-glass",+ ),+ SuggestedPrompt(+ title="Put an always-on watch on competitors and get pinged the moment they move",+ prompt=(+ "I want to monitor competitors. Help me pick who to track and "+ "what changes matter — pricing, product launches, messaging — "+ "and set up alerts for them."+ ),+ icon="bell",+ ),+ SuggestedPrompt(+ title="Have your next post or newsletter drafted in your voice, ready to edit",+ prompt=(+ "I regularly need written content. Ask me what I publish and "+ "how often, then draft the next one in my voice so I can see "+ "how close you get."+ ),+ icon="megaphone",+ ),+ SuggestedPrompt(+ title="Find the two tasks eating your week and hand them to an agent today",+ prompt=(+ "Help me find what to automate. Interview me briefly about "+ "what repeats every week, then propose the two tasks you'd "+ "take over first and how."+ ),+ icon="robot",+ ),+ ]diff --git a/autogpt_platform/backend/backend/api/features/onboarding_dump/models.py b/autogpt_platform/backend/backend/api/features/onboarding_dump/models.pynew file mode 100644index 0000000000..7a44ec8c7e--- /dev/null+++ b/autogpt_platform/backend/backend/api/features/onboarding_dump/models.py@@ -0,0 +1,151 @@+"""Request / response models for the onboarding brain dump."""++from typing import Annotated++from prisma.enums import BrainDumpInputMode, BrainDumpStatus+from pydantic import BaseModel, Field, StringConstraints++# ``recording_id`` is concatenated into a cloud-storage object key and a+# Redis key, so anything with a path or namespace separator in it would+# let a caller reach outside their own prefix. The client sends a+# ``crypto.randomUUID()``; this is the server refusing everything else.+RECORDING_ID_PATTERN = r"^[A-Za-z0-9_-]{1,64}$"+RecordingId = Annotated[str, StringConstraints(pattern=RECORDING_ID_PATTERN)]++# A 3s MediaRecorder timeslice of webm/opus voice is ~10 KB; 2 MB per part+# leaves three orders of magnitude of headroom while still rejecting a+# client that tries to stream a movie through the endpoint.+MAX_PART_BYTES = 2 * 1024 * 1024++# ~30 min of webm/opus voice is ~6 MB. 50 MB is the cumulative ceiling for+# one recording — generous enough that no honest dump ever hits it.+MAX_RECORDING_BYTES = 50 * 1024 * 1024++# Typed fallback. Copilot's own message ceiling is 64,000 chars; the typed+# composer is a paragraph or two, so cap well below that.+MAX_TYPED_CHARS = 20_000++# The client stops recording at 30 minutes, and this number is not just+# metadata: an unmuxed MediaRecorder stream carries no container duration,+# so it becomes the loop bound in ``transcription.split_audio`` — one+# ffmpeg run and one billed STT call per 10 minutes of it. Four hours+# leaves room for clock skew and a paused-and-resumed take while keeping+# a hostile value from turning a 10 KB upload into six figures of both.+MAX_DURATION_SECS = 4 * 60 * 60++ALLOWED_AUDIO_MIME_TYPES = frozenset(+ {+ "audio/webm",+ "audio/mp4",+ "audio/mpeg",+ "audio/wav",+ "audio/x-wav",+ "audio/ogg",+ }+)+++class UploadPartResponse(BaseModel):+ recording_id: str+ part_index: int+ received_bytes: int+ total_bytes: int+++class FinalizeRequest(BaseModel):+ """Finalize one take.++ ``text`` is the typed fallback: when set, no audio is expected and the+ dump is stored with ``input_mode=typed``. ``input_mode=skipped``+ records the deliberate skip so Path B can be chosen server-side.+ """++ recording_id: RecordingId+ input_mode: BrainDumpInputMode = BrainDumpInputMode.voice+ duration_secs: float | None = Field(default=None, ge=0, le=MAX_DURATION_SECS)+ mime_type: str | None = None+ text: str | None = Field(default=None, max_length=MAX_TYPED_CHARS)+++class FinalizeResponse(BaseModel):+ status: BrainDumpStatus+ input_mode: BrainDumpInputMode+ transcript_preview: str | None = None+ error_code: str | None = None+++class DumpStatusResponse(BaseModel):+ status: BrainDumpStatus | None = None+ input_mode: BrainDumpInputMode | None = None+ error_code: str | None = None+ has_audio: bool = False+ greeting_ready: bool = False+ """True once the copilot greeting is stored and ready to render — the+ onboarding loading screen holds the user until this flips (or the+ pipeline terminally fails)."""+++class SuggestedPrompt(BaseModel):+ """One pickable prompt under the greeting.++ ``title`` is the short line the user sees; ``prompt`` is the full+ message that gets sent when they pick it. ``icon`` is a Phosphor icon+ slug from ``intro.PROMPT_ICONS`` — the frontend maps it to the actual+ icon component and falls back to a sparkle for anything unknown.+ """++ title: str+ prompt: str+ icon: str = "sparkle"+++class IntroCardResponse(BaseModel):+ """Content for the copilot home's onboarding greeting.++ ``path`` is "A" when we have something to reflect back and "B" when+ the greeting's job is to invite a recording instead. ``greeting_done``+ is the server-side "already seen it" flag — when true the client+ shows nothing and caches the fact locally.+ """++ path: str+ greeting: str+ prompts: list[SuggestedPrompt] = []+ greeting_done: bool = False+ transcript: str | None = None+ """The full transcript of the recorded dump, so the greeting page can+ offer a copy button. Only present on Path A while the greeting is+ still showing."""+++class RecommendedProvider(BaseModel):+ """One provider Claude picked from the transcript.++ ``provider`` is a registry id (validated against the live provider+ list before storage); ``reason`` is the one-liner shown under it in+ the welcome dialog's Recommended section.+ """++ provider: str+ reason: str = ""+++class RecommendedProvidersResponse(BaseModel):+ """Recommendations for the "Connect your tools" panel.++ ``ready`` is false while the background job is still running — the+ client keeps polling. Once true, ``providers`` is the final answer,+ empty included ("nothing worth recommending" is a real result).+ """++ ready: bool+ providers: list[RecommendedProvider] = []+++class GreetingDoneResponse(BaseModel):+ greeting_done: bool+++class RecordingUrlResponse(BaseModel):+ url: str+ mime_type: str | None = Nonediff --git a/autogpt_platform/backend/backend/api/features/onboarding_dump/prompts.py b/autogpt_platform/backend/backend/api/features/onboarding_dump/prompts.pynew file mode 100644index 0000000000..83a2418e34--- /dev/null+++ b/autogpt_platform/backend/backend/api/features/onboarding_dump/prompts.py@@ -0,0 +1,14 @@+"""Copy for the copilot home's onboarding greeting.++Path A (they talked) gets a generated greeting — see ``intro.py``. Path B+is static: there is nothing to reflect back, so the greeting's whole job+is to make recording later feel easy and unpunished. The suggested+prompts under it are the generic set from ``intro.fallback_prompts``.+"""++PATH_B_GREETING = (+ "No rush — you can tell me about your work whenever you like. "+ "Tap the microphone below and talk for a couple of minutes, and I'll "+ "start finding the things worth taking off your plate. Until then, "+ "here are a few places we could start."+)diff --git a/autogpt_platform/backend/backend/api/features/onboarding_dump/recommend.py b/autogpt_platform/backend/backend/api/features/onboarding_dump/recommend.pynew file mode 100644index 0000000000..fb3d87e8e0--- /dev/null+++ b/autogpt_platform/backend/backend/api/features/onboarding_dump/recommend.py@@ -0,0 +1,135 @@+"""Provider recommendations from the brain-dump transcript.++Runs as its own background job beside the greeting pipeline — the two are+deliberately decoupled so a slow or failed recommendation never delays the+greeting, and vice versa. The model sees the transcript plus the live+provider registry and picks the handful worth connecting first; anything+it hallucinates outside the registry is dropped.+"""++import asyncio+import logging+import os++from backend.api.features.integrations.models import (+ get_all_provider_names,+ get_provider_description,+)+from backend.api.features.onboarding_dump.intro import _parse_response_json+from backend.api.features.onboarding_dump.models import RecommendedProvider+from backend.util.clients import get_openai_client++logger = logging.getLogger(__name__)++_MODEL = os.environ.get("BRAIN_DUMP_RECOMMEND_MODEL", "anthropic/claude-sonnet-5")+_TIMEOUT_SECONDS = 30++MAX_RECOMMENDATIONS = 6+MAX_REASON_CHARS = 140++_PROMPT = """A new user just recorded a spoken brain dump about their work. \+Below is their transcript and the list of integration providers this \+platform can connect to.++Pick the providers this specific user should connect first, based only on \+tools and workflows they actually mentioned or clearly implied. Return ONLY \+valid JSON with exactly one key, "providers": an array of at most \+{max_recommendations} objects, most useful first, each with:+- "provider": a provider id copied EXACTLY from the list below+- "reason": one short sentence (max 100 characters), second person, tying \+the provider to something they said++Rules: never invent a provider id that is not in the list; skip generic \+picks the transcript gives no evidence for; an empty array is a valid \+answer for a thin transcript.++Providers:+{providers}++Transcript:+{transcript}+"""+++async def generate_recommendations(transcript: str) -> list[RecommendedProvider]:+ """Return the model's provider picks for ``transcript``.++ Never raises: any failure returns an empty list, which the caller+ still persists — "no recommendations" is a valid, final result.+ """+ text = transcript.strip()+ if not text:+ return []++ client = get_openai_client(prefer_openrouter=True)+ if client is None:+ logger.warning("Brain dump recommendations: no LLM client configured")+ return []++ known = _known_providers()+ prompt = _PROMPT.format(+ max_recommendations=MAX_RECOMMENDATIONS,+ providers="\n".join(+ f"- {name}: {description}" if description else f"- {name}"+ for name, description in known.items()+ ),+ transcript=text,+ )+ try:+ response = await asyncio.wait_for(+ client.chat.completions.create(+ model=_MODEL,+ messages=[{"role": "user", "content": prompt}],+ temperature=0.2,+ max_tokens=1000,+ ),+ timeout=_TIMEOUT_SECONDS,+ )+ except Exception as e: # background job, empty list is fine+ logger.warning("Brain dump recommendation generation failed: %s", e)+ return []++ data = _parse_response_json(response.choices[0].message.content or "")+ return _parse_recommendations(data, set(known))+++def _known_providers() -> dict[str, str | None]:+ """The live provider registry as ``{id: description}``.++ Mirrors the ``/providers`` endpoint: block modules must be imported+ before AutoRegistry knows about SDK-registered providers.+ """+ try:+ from backend.blocks import load_all_blocks++ load_all_blocks()+ except Exception as e: # static providers still work+ logger.warning("Brain dump recommendations: block load failed: %s", e)+ return {name: get_provider_description(name) for name in get_all_provider_names()}+++def _parse_recommendations(data: object, known: set[str]) -> list[RecommendedProvider]:+ items = data.get("providers") if isinstance(data, dict) else None+ if not isinstance(items, list):+ return []+ seen: set[str] = set()+ recommendations = []+ for item in items:+ if not isinstance(item, dict) or not isinstance(item.get("provider"), str):+ continue+ provider = item["provider"].strip()+ if provider not in known or provider in seen:+ continue+ seen.add(provider)+ reason = item.get("reason")+ recommendations.append(+ RecommendedProvider(+ provider=provider,+ reason=(+ reason.strip()[:MAX_REASON_CHARS] if isinstance(reason, str) else ""+ ),+ )+ )+ if len(recommendations) == MAX_RECOMMENDATIONS:+ break+ return recommendationsdiff --git a/autogpt_platform/backend/backend/api/features/onboarding_dump/recommend_test.py b/autogpt_platform/backend/backend/api/features/onboarding_dump/recommend_test.pynew file mode 100644index 0000000000..a88d1ee386--- /dev/null+++ b/autogpt_platform/backend/backend/api/features/onboarding_dump/recommend_test.py@@ -0,0 +1,104 @@+"""Tests for the provider-recommendation job's parsing and read path.++The LLM call itself is not under test — what matters is that hallucinated+provider ids never reach storage, and that ``ready`` reflects the job (a+null column) rather than the greeting pipeline's status.+"""++from unittest.mock import AsyncMock++import pytest+from prisma.enums import BrainDumpInputMode, BrainDumpStatus+from prisma.models import OnboardingBrainDump+from pytest_mock import MockerFixture++from backend.api.features.onboarding_dump import recommend, service++USER_ID = "user-1"++KNOWN = {"slack", "github", "notion", "google"}+++def test_parse_recommendations_drops_unknown_and_duplicate_providers():+ data = {+ "providers": [+ {"provider": "slack", "reason": "You mentioned Slack standups."},+ {"provider": "slack", "reason": "Duplicate."},+ {"provider": "made_up_tool", "reason": "Hallucinated."},+ {"provider": "github", "reason": ""},+ "not-a-dict",+ {"reason": "missing provider key"},+ ]+ }+ result = recommend._parse_recommendations(data, KNOWN)+ assert [r.provider for r in result] == ["slack", "github"]+ assert result[0].reason == "You mentioned Slack standups."+++def test_parse_recommendations_caps_count_and_reason_length():+ known = {f"tool{i}" for i in range(10)}+ data = {+ "providers": [{"provider": f"tool{i}", "reason": "x" * 500} for i in range(10)]+ }+ result = recommend._parse_recommendations(data, known)+ assert len(result) == recommend.MAX_RECOMMENDATIONS+ assert all(len(r.reason) == recommend.MAX_REASON_CHARS for r in result)+++def test_parse_recommendations_rejects_non_object_payloads():+ assert recommend._parse_recommendations(None, KNOWN) == []+ assert recommend._parse_recommendations(["slack"], KNOWN) == []+ assert recommend._parse_recommendations({"providers": "slack"}, KNOWN) == []+++def _dump(**overrides) -> OnboardingBrainDump:+ fields = {+ "userId": USER_ID,+ "recordingId": "rec-1",+ "status": BrainDumpStatus.completed,+ "inputMode": BrainDumpInputMode.voice,+ "transcript": "I live in Slack and ship on GitHub.",+ "recommendedProviders": None,+ **overrides,+ }+ return OnboardingBrainDump.model_construct(**fields)+++async def test_recommended_providers_not_ready_while_column_is_null(+ mocker: MockerFixture,+):+ # Greeting pipeline already completed — status must not flip ready.+ mocker.patch.object(service.db, "get_dump", AsyncMock(return_value=_dump()))+ response = await service.get_recommended_providers(USER_ID)+ assert response.ready is False+ assert response.providers == []+++async def test_recommended_providers_ready_once_job_wrote_result(+ mocker: MockerFixture,+):+ stored = [{"provider": "slack", "reason": "You mentioned Slack."}, {"bad": 1}]+ mocker.patch.object(+ service.db,+ "get_dump",+ AsyncMock(return_value=_dump(recommendedProviders=stored)),+ )+ response = await service.get_recommended_providers(USER_ID)+ assert response.ready is True+ assert [p.provider for p in response.providers] == ["slack"]+++async def test_recommended_providers_ready_without_transcript(+ mocker: MockerFixture,+):+ mocker.patch.object(+ service.db,+ "get_dump",+ AsyncMock(return_value=_dump(transcript=None)),+ )+ response = await service.get_recommended_providers(USER_ID)+ assert response.ready is True+ assert response.providers == []diff --git a/autogpt_platform/backend/backend/api/features/onboarding_dump/routes.py b/autogpt_platform/backend/backend/api/features/onboarding_dump/routes.pynew file mode 100644index 0000000000..3ab5afd5e6--- /dev/null+++ b/autogpt_platform/backend/backend/api/features/onboarding_dump/routes.py@@ -0,0 +1,291 @@+"""Onboarding brain-dump endpoints.++Everything here is user-scoped and gated on ``ONBOARDING_BRAIN_DUMP`` — a+stale frontend must not be able to start writing recordings after the flag+is turned back off.+"""++import logging+from typing import Annotated++import autogpt_libs.auth as autogpt_auth_lib+import fastapi+from autogpt_libs.auth.dependencies import get_user_id+from fastapi import APIRouter, Depends, Form, Security, UploadFile+from fastapi.responses import RedirectResponse+from prisma.enums import BrainDumpInputMode, BrainDumpStatus++from backend.api.features.onboarding_dump import db, service, storage+from backend.api.features.onboarding_dump.models import (+ ALLOWED_AUDIO_MIME_TYPES,+ MAX_PART_BYTES,+ MAX_RECORDING_BYTES,+ RECORDING_ID_PATTERN,+ DumpStatusResponse,+ FinalizeRequest,+ FinalizeResponse,+ GreetingDoneResponse,+ IntroCardResponse,+ RecommendedProvidersResponse,+ UploadPartResponse,+)+from backend.api.features.store.exceptions import VirusDetectedError, VirusScanError+from backend.util.feature_flag import Flag, is_feature_enabled++logger = logging.getLogger(__name__)+++async def require_brain_dump_flag(+ user_id: Annotated[str, Security(get_user_id)],+) -> None:+ """Gate every endpoint on the brain-dump flag, fail-closed.++ Deliberately not ``create_feature_flag_dependency``: that helper+ short-circuits to 404 whenever LaunchDarkly has no SDK key, which is+ every local dev and test environment — and it does so *before*+ consulting the ``FORCE_FLAG_ONBOARDING_BRAIN_DUMP`` override, so the+ documented way to switch the feature on locally could never work.+ ``is_feature_enabled`` checks the env override first and still+ defaults to False when LaunchDarkly is unreachable.+ """+ if not await is_feature_enabled(Flag.ONBOARDING_BRAIN_DUMP, user_id):+ raise fastapi.HTTPException(status_code=404, detail="Feature not available")+++router = APIRouter(+ prefix="/onboarding/brain-dump",+ tags=["onboarding", "brain-dump"],+ dependencies=[+ Security(autogpt_auth_lib.requires_user),+ Depends(require_brain_dump_flag),+ ],+)++[email protected]("/parts", operation_id="upload_brain_dump_part")+async def upload_brain_dump_part(+ user_id: Annotated[str, Security(get_user_id)],+ file: UploadFile,+ recording_id: Annotated[str, Form(pattern=RECORDING_ID_PATTERN)],+ part_index: Annotated[int, Form(ge=0)],+) -> UploadPartResponse:+ """Accept one MediaRecorder timeslice blob.++ Parts arrive while the user is still talking, so this stays cheap:+ size checks, buffer, done. Validation that needs the whole stream+ (virus scan, transcription) happens on finalize.+ """+ if file.content_type and file.content_type.split(";")[0] not in (+ ALLOWED_AUDIO_MIME_TYPES+ ):+ raise fastapi.HTTPException(+ status_code=415, detail=f"Unsupported audio type: {file.content_type}"+ )++ chunks: list[bytes] = []+ total = 0+ while chunk := await file.read(64 * 1024):+ total += len(chunk)+ if total > MAX_PART_BYTES:+ raise fastapi.HTTPException(+ status_code=413,+ detail=f"Recording part exceeds {MAX_PART_BYTES // (1024 * 1024)} MB",+ )+ chunks.append(chunk)++ if await storage.buffered_size(user_id, recording_id) + total > MAX_RECORDING_BYTES:+ raise fastapi.HTTPException(+ status_code=413,+ detail=(+ "Recording exceeds the "+ f"{MAX_RECORDING_BYTES // (1024 * 1024)} MB limit"+ ),+ )++ if part_index == 0:+ await db.start_dump(user_id, recording_id, BrainDumpInputMode.voice)++ cumulative = await storage.append_part(+ user_id, recording_id, part_index, b"".join(chunks)+ )+ # The check above races: two parts in flight can both read a buffer+ # that is under the cap and both be admitted. This one is+ # authoritative because the size comes back from inside the same+ # transaction that wrote the part. A recording past the ceiling is+ # not going to assemble into anything usable, so the buffer goes.+ if cumulative > MAX_RECORDING_BYTES:+ await storage.discard_parts(user_id, recording_id)+ raise fastapi.HTTPException(+ status_code=413,+ detail=(+ "Recording exceeds the "+ f"{MAX_RECORDING_BYTES // (1024 * 1024)} MB limit"+ ),+ )++ return UploadPartResponse(+ recording_id=recording_id,+ part_index=part_index,+ received_bytes=total,+ total_bytes=cumulative,+ )++[email protected]("/finalize", operation_id="finalize_brain_dump")+async def finalize_brain_dump(+ request: FinalizeRequest,+ user_id: Annotated[str, Security(get_user_id)],+ background_tasks: fastapi.BackgroundTasks,+) -> FinalizeResponse:+ """Close out a take: assemble, scan, store, transcribe.++ Extraction and greeting generation continue in the background after+ this responds — the preparing step polls ``/status`` until+ ``greeting_ready``. Idempotent per ``recording_id`` — a client that+ retries after a timeout gets the stored result rather than a second+ transcription.+ """+ try:+ if request.input_mode == BrainDumpInputMode.skipped:+ return await service.finalize_skipped_dump(user_id, request.recording_id)++ if request.input_mode == BrainDumpInputMode.typed:+ text = (request.text or "").strip()+ if not text:+ raise fastapi.HTTPException(+ status_code=422, detail="Typed brain dump cannot be empty"+ )+ return await service.finalize_typed_dump(+ user_id, request.recording_id, text, background_tasks+ )++ return await service.finalize_voice_dump(+ user_id,+ request.recording_id,+ request.duration_secs,+ request.mime_type,+ background_tasks,+ )+ except VirusDetectedError as e:+ await db.mark_failed(user_id, request.recording_id, "virus_detected")+ raise fastapi.HTTPException(status_code=400, detail=str(e)) from e+ except VirusScanError as e:+ await db.mark_failed(user_id, request.recording_id, "virus_scan_failed")+ raise fastapi.HTTPException(status_code=500, detail=str(e)) from e++[email protected]("/status", operation_id="get_brain_dump_status")+async def get_brain_dump_status(+ user_id: Annotated[str, Security(get_user_id)],+) -> DumpStatusResponse:+ """Polled by the loading screen while the pipeline runs."""+ dump = await db.get_dump(user_id)+ if dump is None:+ return DumpStatusResponse()+ return DumpStatusResponse(+ status=dump.status,+ input_mode=dump.inputMode,+ error_code=dump.errorCode,+ has_audio=bool(dump.audioPath),+ # Skipped dumps get the static Path B greeting, so there is+ # nothing to generate or wait for.+ greeting_ready=(+ dump.inputMode == BrainDumpInputMode.skipped+ or bool((dump.greeting or "").strip())+ ),+ )++[email protected]("/intro", operation_id="get_brain_dump_intro")+async def get_brain_dump_intro(+ user_id: Annotated[str, Security(get_user_id)],+) -> IntroCardResponse:+ """Content for the copilot home's onboarding greeting.++ Pre-generated during finalize, so this is a plain row read — the+ copilot home never waits on a model to render its first screen.+ ``greeting_done=true`` means the client should show nothing and cache+ that locally.+ """+ return await service.get_intro_card(user_id)+++ "/recommended-providers", operation_id="get_brain_dump_recommended_providers"+)+async def get_brain_dump_recommended_providers(+ user_id: Annotated[str, Security(get_user_id)],+) -> RecommendedProvidersResponse:+ """Provider picks for the welcome dialog's "Connect your tools" panel.++ Written by a background job that runs beside the greeting one; a plain+ row read here. ``ready=false`` means keep polling.+ """+ return await service.get_recommended_providers(user_id)++[email protected]("/intro/complete", operation_id="complete_brain_dump_greeting")+async def complete_brain_dump_greeting(+ user_id: Annotated[str, Security(get_user_id)],+) -> GreetingDoneResponse:+ """Mark the greeting as done — called when the first message is sent.++ Permanent and content-preserving: the flag flips, the stored greeting+ stays in the row.+ """+ await service.mark_greeting_done(user_id)+ return GreetingDoneResponse(greeting_done=True)++[email protected]("/recording", operation_id="download_brain_dump_recording")+async def download_brain_dump_recording(+ user_id: Annotated[str, Security(get_user_id)],+) -> RedirectResponse:+ """Hand back the user's own recording.++ Kept working even when ``status`` is ``failed`` — "your recording is+ safe" has to be true, and this is what makes it checkable.+ """+ dump = await db.get_dump(user_id)+ if dump is None or not dump.audioPath:+ raise fastapi.HTTPException(status_code=404, detail="No recording found")++ url = await storage.audio_download_url(user_id, dump.audioPath)+ return RedirectResponse(url=url, status_code=307)+++# Path is "" rather than "/": with the router prefix that would register+# ``/api/onboarding/brain-dump/`` and every call to the unslashed URL would+# take a 307 through FastAPI's redirect_slashes. Redirects drop the+# Authorization header, so the retried request arrives unauthenticated and+# 401s. No other route in this codebase uses "/" — this is why.[email protected]("", operation_id="discard_brain_dump")+async def discard_brain_dump(+ user_id: Annotated[str, Security(get_user_id)],+ recording_id: Annotated[+ str | None, fastapi.Query(pattern=RECORDING_ID_PATTERN)+ ] = None,+) -> DumpStatusResponse:+ """Drop a half-uploaded take's server buffer when the user re-records.++ The row and any stored audio are left alone — only the disposable+ part buffer is cleared.++ ``recording_id`` says *which* take to drop. Without it the row's+ current take is assumed, which is wrong as soon as a second tab has+ moved the row on: the caller would clear a buffer that is still being+ filled. Callers know their own id, so they send it.+ """+ dump = await db.get_dump(user_id)+ target = recording_id or (dump.recordingId if dump else None)+ if target and not (+ dump is not None+ and dump.recordingId == target+ and dump.status == BrainDumpStatus.completed+ ):+ await storage.discard_parts(user_id, target)+ return DumpStatusResponse(+ status=dump.status if dump else None,+ input_mode=dump.inputMode if dump else None,+ has_audio=bool(dump and dump.audioPath),+ )diff --git a/autogpt_platform/backend/backend/api/features/onboarding_dump/routes_test.py b/autogpt_platform/backend/backend/api/features/onboarding_dump/routes_test.pynew file mode 100644index 0000000000..5a16820184--- /dev/null+++ b/autogpt_platform/backend/backend/api/features/onboarding_dump/routes_test.py@@ -0,0 +1,598 @@+"""Endpoint tests for the onboarding brain-dump routes.++Everything below the router (Prisma, Redis, cloud storage, OpenAI) is+mocked at the point of use, so these run without a live stack.+"""++from typing import Any+from unittest.mock import AsyncMock, MagicMock++import fastapi+import fastapi.testclient+import pytest+from autogpt_libs.auth.jwt_utils import get_jwt_payload+from prisma.enums import BrainDumpInputMode, BrainDumpStatus+from prisma.models import OnboardingBrainDump+from pytest_mock import MockerFixture++from backend.api.features.onboarding_dump import routes+from backend.api.features.onboarding_dump.models import (+ MAX_DURATION_SECS,+ MAX_PART_BYTES,+ MAX_RECORDING_BYTES,+ SuggestedPrompt,+)+from backend.api.features.store.exceptions import VirusDetectedError+from backend.data.understanding import BusinessUnderstandingInput++app = fastapi.FastAPI()+app.include_router(routes.router)+client = fastapi.testclient.TestClient(app)++PARTS_URL = "/onboarding/brain-dump/parts"+FINALIZE_URL = "/onboarding/brain-dump/finalize"+STATUS_URL = "/onboarding/brain-dump/status"+RECORDING_URL = "/onboarding/brain-dump/recording"+DISCARD_URL = "/onboarding/brain-dump/"+INTRO_URL = "/onboarding/brain-dump/intro"+INTRO_COMPLETE_URL = "/onboarding/brain-dump/intro/complete"+RECOMMENDED_URL = "/onboarding/brain-dump/recommended-providers"++RECORDING_ID = "rec-1"+TRANSCRIPT = "I run a small bakery and I want the weekly order emails handled."+GREETING = "Let's get those weekly order emails off your plate."+++class DumpStore:+ """In-memory stand-in for the one-row-per-user brain dump table."""++ def __init__(self) -> None:+ self.row: OnboardingBrainDump | None = None+ self.statuses: list[BrainDumpStatus] = []++ async def get_dump(self, user_id: str) -> OnboardingBrainDump | None:+ return self.row++ async def start_dump(+ self, user_id: str, recording_id: str, input_mode: BrainDumpInputMode+ ) -> OnboardingBrainDump:+ # Mirrors the real `start_dump`: a take already moving through+ # the pipeline is returned untouched, so a replayed part 0 or a+ # repeated finalize cannot reset it.+ if (+ self.row is not None+ and self.row.status+ in (+ BrainDumpStatus.recording_uploaded,+ BrainDumpStatus.transcribing,+ BrainDumpStatus.transcribed,+ BrainDumpStatus.extracting,+ BrainDumpStatus.completed,+ )+ and (+ self.row.recordingId == recording_id+ or input_mode != BrainDumpInputMode.voice+ )+ ):+ return self.row+ self.row = OnboardingBrainDump.model_construct(+ userId=user_id,+ recordingId=recording_id,+ status=BrainDumpStatus.recording_uploaded,+ inputMode=input_mode,+ transcript=None,+ greeting=None,+ suggestedPrompts=[],+ greetingSeen=False,+ audioPath=None,+ errorCode=None,+ recommendedProviders=None,+ )+ self.statuses.append(BrainDumpStatus.recording_uploaded)+ return self.row++ async def update_dump(self, user_id: str, recording_id: str, **fields: Any) -> bool:+ # Mirrors the scoped UPDATE: a write from a take the row has+ # already moved past hits nothing.+ if self.row is None or self.row.recordingId != recording_id:+ return False+ for name, value in fields.items():+ setattr(self.row, name, value)+ status = fields.get("status")+ if status is not None:+ self.statuses.append(status)+ return True++ async def claim_transition(+ self,+ user_id: str,+ recording_id: str,+ *,+ expected: BrainDumpStatus,+ new: BrainDumpStatus,+ **fields: Any,+ ) -> bool:+ """Mirrors the conditional UPDATE: only matching rows transition."""+ if (+ self.row is None+ or self.row.recordingId != recording_id+ or self.row.status != expected+ ):+ return False+ await self.update_dump(user_id, recording_id, status=new, **fields)+ return True++ async def mark_failed(+ self, user_id: str, recording_id: str, error_code: str+ ) -> None:+ await self.update_dump(+ user_id,+ recording_id,+ status=BrainDumpStatus.failed,+ errorCode=error_code,+ )++ async def mark_greeting_seen(self, user_id: str) -> None:+ if self.row is None:+ self.row = OnboardingBrainDump.model_construct(+ userId=user_id,+ recordingId="greeting-only",+ status=BrainDumpStatus.completed,+ inputMode=BrainDumpInputMode.skipped,+ transcript=None,+ greeting=None,+ suggestedPrompts=[],+ greetingSeen=True,+ audioPath=None,+ errorCode=None,+ )+ else:+ self.row.greetingSeen = True+++# ``require_brain_dump_flag`` runs the real ``is_feature_enabled``, which+# consults this env override before LaunchDarkly — so the gate is driven+# here exactly the way it is driven locally, with no mock in the path.+FLAG_ENV_VAR = "FORCE_FLAG_ONBOARDING_BRAIN_DUMP"++[email protected](autouse=True)+def setup_app_auth(mock_jwt_user):+ app.dependency_overrides[get_jwt_payload] = mock_jwt_user["get_jwt_payload"]+ yield+ app.dependency_overrides.clear()++[email protected](autouse=True)+def flag_on(monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.setenv(FLAG_ENV_VAR, "true")++[email protected](autouse=True)+def dumps(mocker: MockerFixture) -> DumpStore:+ store = DumpStore()+ module = "backend.api.features.onboarding_dump.db"+ mocker.patch(f"{module}.get_dump", new=store.get_dump)+ mocker.patch(f"{module}.start_dump", new=store.start_dump)+ mocker.patch(f"{module}.update_dump", new=store.update_dump)+ mocker.patch(f"{module}.mark_failed", new=store.mark_failed)+ mocker.patch(f"{module}.claim_transition", new=store.claim_transition)+ mocker.patch(f"{module}.mark_greeting_seen", new=store.mark_greeting_seen)+ return store++[email protected](autouse=True)+def storage_mocks(mocker: MockerFixture) -> dict[str, AsyncMock]:+ module = "backend.api.features.onboarding_dump.storage"+ mocks = {+ "buffered_size": AsyncMock(return_value=0),+ "append_part": AsyncMock(return_value=1024),+ "assemble_parts": AsyncMock(return_value=b"opus-audio-bytes"),+ "store_audio": AsyncMock(return_value="brain-dumps/rec-1.webm"),+ "discard_parts": AsyncMock(),+ "audio_download_url": AsyncMock(return_value="https://signed.example/audio"),+ }+ for name, mock in mocks.items():+ mocker.patch(f"{module}.{name}", new=mock)+ return mocks++[email protected](autouse=True)+def stt_create(mocker: MockerFixture) -> AsyncMock:+ create = AsyncMock(return_value=MagicMock(text=TRANSCRIPT, language="en"))+ stt = MagicMock()+ stt.audio.transcriptions.create = create+ mocker.patch(+ "backend.api.features.onboarding_dump.transcription.get_stt_client",+ return_value=stt,+ )+ return create++[email protected](autouse=True)+def extraction(mocker: MockerFixture) -> dict[str, AsyncMock]:+ module = "backend.api.features.onboarding_dump.service"+ mocks = {+ "scan_content_safe": AsyncMock(),+ "get_business_understanding": AsyncMock(return_value=None),+ "extract_business_understanding": AsyncMock(+ return_value=BusinessUnderstandingInput.model_construct()+ ),+ "upsert_business_understanding": AsyncMock(),+ }+ for name, mock in mocks.items():+ mocker.patch(f"{module}.{name}", new=mock)+ return mocks++[email protected](autouse=True)+def generation(mocker: MockerFixture) -> dict[str, AsyncMock]:+ """Stub the two LLM jobs the background half of finalize kicks off.++ Both degrade instead of raising, so without this the tests still pass+ — while quietly building a real client and reaching for the network+ on every finalize. Deterministic stubs keep the suite offline and+ give the intro assertions something fixed to match.+ """+ intro_mock = AsyncMock(+ return_value=(+ GREETING,+ [+ SuggestedPrompt(title=f"Prompt {index}", prompt="Do the thing")+ for index in range(5)+ ],+ )+ )+ mocker.patch(+ "backend.api.features.onboarding_dump.intro.generate_intro", new=intro_mock+ )+ recommend_mock = AsyncMock(return_value=[])+ mocker.patch(+ "backend.api.features.onboarding_dump.recommend.generate_recommendations",+ new=recommend_mock,+ )+ return {"generate_intro": intro_mock, "generate_recommendations": recommend_mock}+++def upload_part(+ part_index: int = 0,+ content: bytes = b"chunk",+ content_type: str = "audio/webm",+):+ return client.post(+ PARTS_URL,+ files={"file": (f"part-{part_index}.webm", content, content_type)},+ data={"recording_id": RECORDING_ID, "part_index": str(part_index)},+ )+++def finalize(**overrides: Any):+ payload: dict[str, Any] = {"recording_id": RECORDING_ID}+ payload.update(overrides)+ return client.post(FINALIZE_URL, json=payload)+++def test_parts_upload_then_finalize_completes_with_full_transcript(+ dumps: DumpStore, stt_create: AsyncMock, extraction: dict[str, AsyncMock]+):+ part = upload_part()+ assert part.status_code == 200+ assert part.json() == {+ "recording_id": RECORDING_ID,+ "part_index": 0,+ "received_bytes": len(b"chunk"),+ "total_bytes": 1024,+ }++ response = finalize()++ assert response.status_code == 200+ body = response.json()+ # Finalize answers as soon as the transcript is stored; extraction and+ # the greeting run as a background task (which the TestClient executes+ # before returning), so the row below still ends up completed.+ assert body["status"] == BrainDumpStatus.transcribed+ assert body["input_mode"] == BrainDumpInputMode.voice+ assert body["transcript_preview"] == TRANSCRIPT+ assert dumps.statuses == [+ BrainDumpStatus.recording_uploaded,+ # Written twice: once by the atomic claim that decides which+ # concurrent finalize proceeds, then again with the audio+ # metadata once the recording is stored.+ BrainDumpStatus.transcribing,+ BrainDumpStatus.transcribing,+ BrainDumpStatus.transcribed,+ BrainDumpStatus.extracting,+ BrainDumpStatus.completed,+ ]+ assert dumps.row is not None+ assert dumps.row.transcript == TRANSCRIPT+ assert dumps.row.audioPath == "brain-dumps/rec-1.webm"+ stt_create.assert_awaited_once()+ extraction["upsert_business_understanding"].assert_awaited_once()+++def test_finalize_retry_is_idempotent_and_does_not_retranscribe(+ dumps: DumpStore, stt_create: AsyncMock+):+ upload_part()+ assert finalize().status_code == 200+ assert dumps.row is not None+ assert dumps.row.status == BrainDumpStatus.completed++ retry = finalize()++ assert retry.status_code == 200+ assert retry.json()["status"] == BrainDumpStatus.completed+ assert retry.json()["transcript_preview"] == TRANSCRIPT+ stt_create.assert_awaited_once()+++def test_virus_detected_rejects_and_marks_the_dump_failed(+ dumps: DumpStore, extraction: dict[str, AsyncMock]+):+ upload_part()+ extraction["scan_content_safe"].side_effect = VirusDetectedError("Eicar-Test")++ response = finalize()++ assert response.status_code == 400+ assert "Eicar-Test" in response.json()["detail"]+ assert dumps.row is not None+ assert dumps.row.status == BrainDumpStatus.failed+ assert dumps.row.errorCode == "virus_detected"+++def test_part_over_the_per_part_limit_is_rejected():+ response = upload_part(content=b"a" * (MAX_PART_BYTES + 1))++ assert response.status_code == 413+ assert "part exceeds" in response.json()["detail"]+++def test_part_pushing_the_recording_over_the_total_limit_is_rejected(+ storage_mocks: dict[str, AsyncMock]+):+ storage_mocks["buffered_size"].return_value = MAX_RECORDING_BYTES++ response = upload_part(part_index=1)++ assert response.status_code == 413+ assert "Recording exceeds" in response.json()["detail"]+ storage_mocks["append_part"].assert_not_awaited()+++def test_a_racing_part_that_blows_the_total_limit_is_rejected_after_the_write(+ storage_mocks: dict[str, AsyncMock]+):+ """The pre-check races; the post-check is the authoritative one.++ Two parts in flight can both read a buffer that is under the cap and+ both be admitted. `append_part` reports the size from inside the same+ transaction that wrote the part, so that figure cannot race.+ """+ storage_mocks["buffered_size"].return_value = 0+ storage_mocks["append_part"].return_value = MAX_RECORDING_BYTES + 1++ response = upload_part(part_index=1)++ assert response.status_code == 413+ assert "Recording exceeds" in response.json()["detail"]+ storage_mocks["discard_parts"].assert_awaited_once()+++def test_discard_targets_the_recording_the_caller_names(+ storage_mocks: dict[str, AsyncMock], dumps: DumpStore+):+ """A second tab must not be able to clear the wrong buffer.++ Without an explicit id the server drops whatever the row points at,+ which after another tab has claimed it is a take still being filled.+ """+ client.post(+ FINALIZE_URL, json={"recording_id": "rec-other", "input_mode": "skipped"}+ )++ response = client.delete(DISCARD_URL.rstrip("/"), params={"recording_id": "rec-1"})++ assert response.status_code == 200+ # The take the caller named, not the one the row happens to hold.+ assert storage_mocks["discard_parts"].await_args.args[1] == "rec-1"+++def test_unsupported_mime_type_is_rejected():+ response = upload_part(content_type="video/mp4")++ assert response.status_code == 415+ assert "Unsupported audio type" in response.json()["detail"]+++def test_typed_finalize_skips_audio_and_extracts_directly(+ dumps: DumpStore, storage_mocks: dict[str, AsyncMock], stt_create: AsyncMock+):+ response = finalize(input_mode="typed", text=" I run a bakery. ")++ assert response.status_code == 200+ assert response.json()["status"] == BrainDumpStatus.transcribed+ assert response.json()["input_mode"] == BrainDumpInputMode.typed+ assert dumps.row is not None+ assert dumps.row.transcript == "I run a bakery."+ assert dumps.row.audioPath is None+ storage_mocks["assemble_parts"].assert_not_awaited()+ stt_create.assert_not_awaited()+++def test_a_duration_past_the_ceiling_is_rejected(stt_create: AsyncMock):+ """``duration_secs`` is not just metadata — it bounds the split loop.++ An unmuxed MediaRecorder stream carries no container duration, so the+ client's number is what ``split_audio`` iterates over: one ffmpeg run+ and one billed transcription per 10 minutes of whatever is claimed.+ """+ upload_part()++ response = finalize(duration_secs=MAX_DURATION_SECS + 1)++ assert response.status_code == 422+ stt_create.assert_not_awaited()+++def test_typed_finalize_with_empty_text_is_rejected():+ response = finalize(input_mode="typed", text=" ")++ assert response.status_code == 422+ assert response.json()["detail"] == "Typed brain dump cannot be empty"+++def test_skipped_finalize_records_the_skip_without_a_transcript(dumps: DumpStore):+ response = finalize(input_mode="skipped")++ assert response.status_code == 200+ assert response.json() == {+ "status": BrainDumpStatus.completed,+ "input_mode": BrainDumpInputMode.skipped,+ "transcript_preview": None,+ "error_code": None,+ }+ assert dumps.row is not None+ assert dumps.row.inputMode == BrainDumpInputMode.skipped+ assert dumps.row.status == BrainDumpStatus.completed+ assert dumps.row.transcript is None+++def test_status_and_recording_reflect_the_stored_dump(dumps: DumpStore):+ upload_part()+ finalize()++ status = client.get(STATUS_URL)+ assert status.status_code == 200+ assert status.json() == {+ "status": BrainDumpStatus.completed,+ "input_mode": BrainDumpInputMode.voice,+ "error_code": None,+ "has_audio": True,+ # The greeting is stored in the same update that completes the+ # dump, so a completed voice dump is always greeting-ready.+ "greeting_ready": True,+ }++ recording = client.get(RECORDING_URL, follow_redirects=False)+ assert recording.status_code == 307+ assert recording.headers["location"] == "https://signed.example/audio"+++def test_discard_clears_the_part_buffer_of_an_unfinished_take(+ dumps: DumpStore, storage_mocks: dict[str, AsyncMock]+):+ upload_part()++ response = client.delete(DISCARD_URL)++ assert response.status_code == 200+ assert response.json()["status"] == BrainDumpStatus.recording_uploaded+ storage_mocks["discard_parts"].assert_awaited_once()+++def test_greeting_complete_hides_the_intro_for_good(dumps: DumpStore):+ upload_part()+ finalize(mime_type="audio/webm")++ complete = client.post(INTRO_COMPLETE_URL)+ assert complete.status_code == 200+ assert complete.json()["greeting_done"] is True++ intro = client.get(INTRO_URL)+ assert intro.status_code == 200+ body = intro.json()+ assert body["greeting_done"] is True+ assert body["greeting"] == ""+ assert body["prompts"] == []+++def test_greeting_complete_sticks_without_a_dump_row(dumps: DumpStore):+ # A user who never recorded still gets a durable "seen" flag.+ complete = client.post(INTRO_COMPLETE_URL)+ assert complete.status_code == 200++ intro = client.get(INTRO_URL)+ assert intro.json()["greeting_done"] is True+++def test_intro_returns_greeting_and_prompts_before_completion(dumps: DumpStore):+ upload_part()+ finalize(mime_type="audio/webm")++ response = client.get(INTRO_URL)++ assert response.status_code == 200+ body = response.json()+ assert body["greeting_done"] is False+ assert body["greeting"]+ assert len(body["prompts"]) >= 5+ assert all(p["title"] and p["prompt"] for p in body["prompts"])+++def test_recommended_providers_are_pending_until_the_job_writes(dumps: DumpStore):+ upload_part()+ finalize()+ assert dumps.row is not None+ # A null column is the job still running, not "nothing to recommend".+ dumps.row.recommendedProviders = None++ response = client.get(RECOMMENDED_URL)++ assert response.status_code == 200+ assert response.json() == {"ready": False, "providers": []}+++def test_recommended_providers_returns_the_stored_picks(dumps: DumpStore):+ upload_part()+ finalize()+ assert dumps.row is not None+ dumps.row.recommendedProviders = [+ {"provider": "github", "reason": "You mentioned chasing issues."}+ ]++ response = client.get(RECOMMENDED_URL)++ assert response.status_code == 200+ assert response.json() == {+ "ready": True,+ "providers": [+ {"provider": "github", "reason": "You mentioned chasing issues."}+ ],+ }+++ "call",+ [+ lambda: upload_part(),+ lambda: finalize(),+ lambda: client.get(STATUS_URL),+ lambda: client.get(RECORDING_URL, follow_redirects=False),+ lambda: client.delete(DISCARD_URL),+ lambda: client.get(INTRO_URL),+ lambda: client.post(INTRO_COMPLETE_URL),+ lambda: client.get(RECOMMENDED_URL),+ ],+ ids=[+ "parts",+ "finalize",+ "status",+ "recording",+ "discard",+ "intro",+ "complete",+ "recommended-providers",+ ],+)+def test_every_endpoint_is_404_when_the_flag_is_off(+ monkeypatch: pytest.MonkeyPatch, call+):+ monkeypatch.setenv(FLAG_ENV_VAR, "false")++ response = call()++ assert response.status_code == 404+ assert response.json()["detail"] == "Feature not available"diff --git a/autogpt_platform/backend/backend/api/features/onboarding_dump/service.py b/autogpt_platform/backend/backend/api/features/onboarding_dump/service.pynew file mode 100644index 0000000000..18bd8af6d9--- /dev/null+++ b/autogpt_platform/backend/backend/api/features/onboarding_dump/service.py@@ -0,0 +1,566 @@+"""The brain-dump pipeline: parts → audio → transcript → understanding.++Every step advances ``OnboardingBrainDump.status`` so the loading screen+can narrate honestly, and no failure ever deletes the audio — a failed+dump stays downloadable and retryable.+"""++import asyncio+import logging+import time++from fastapi import BackgroundTasks+from prisma import Json+from prisma.enums import BrainDumpInputMode, BrainDumpStatus++from backend.api.features.onboarding_dump import (+ db,+ intro,+ prompts,+ recommend,+ storage,+ transcription,+)+from backend.api.features.onboarding_dump.models import (+ FinalizeResponse,+ IntroCardResponse,+ RecommendedProvider,+ RecommendedProvidersResponse,+ SuggestedPrompt,+)+from backend.data.onboarding import format_brain_dump_for_extraction+from backend.data.tally import extract_business_understanding+from backend.data.understanding import (+ BusinessUnderstandingInput,+ get_business_understanding,+ upsert_business_understanding,+)+from backend.util.virus_scanner import scan_content_safe++logger = logging.getLogger(__name__)++TRANSCRIPT_PREVIEW_CHARS = 280+++async def finalize_voice_dump(+ user_id: str,+ recording_id: str,+ duration_secs: float | None,+ mime_type: str | None,+ background_tasks: BackgroundTasks,+) -> FinalizeResponse:+ existing = await db.get_dump(user_id)+ if (+ existing is not None+ and existing.recordingId == recording_id+ and existing.status+ in (+ # Also idempotent while the background half of the pipeline is+ # still running: a client retry must not re-transcribe (the+ # part buffer is already discarded) or double-run extraction.+ #+ # ``transcribing`` matters most of all. The buffer is dropped+ # the moment the audio is stored, so a retry that arrives+ # while a long recording is still being transcribed would+ # assemble nothing and mark a perfectly good take failed.+ BrainDumpStatus.transcribing,+ BrainDumpStatus.transcribed,+ BrainDumpStatus.extracting,+ BrainDumpStatus.completed,+ )+ ):+ return _pipeline_response(+ existing.status, existing.transcript, BrainDumpInputMode.voice+ )++ # The guard above is a read followed by a write, so two finalizes+ # that arrive together can both pass it and both go on to assemble,+ # store and transcribe the same take. This claim is decided by the+ # database, so exactly one of them proceeds.+ claimed = await db.claim_transition(+ user_id,+ recording_id,+ expected=BrainDumpStatus.recording_uploaded,+ new=BrainDumpStatus.transcribing,+ errorCode=None,+ )+ if not claimed:+ current = await db.get_dump(user_id)+ if current is None:+ # No row, so there is nothing to write results to: every+ # ``update_dump`` below would no-op while ``discard_parts``+ # dropped the buffer for real, leaving stored audio nobody+ # can reach and a client told its dump succeeded. Stop here+ # and keep the parts — the browser still holds them, and a+ # re-upload recreates the row on part 0.+ return FinalizeResponse(+ status=BrainDumpStatus.failed,+ input_mode=BrainDumpInputMode.voice,+ error_code="no_audio_received",+ )+ if current.recordingId != recording_id:+ # There is one row per user, so a newer take from a second+ # tab has taken it over. Returning its status would narrate+ # somebody else's recording back to this client, and writing+ # anything would knock the live take over — so report this+ # take's own outcome and leave the row alone.+ return FinalizeResponse(+ status=BrainDumpStatus.failed,+ input_mode=BrainDumpInputMode.voice,+ error_code="superseded",+ )+ # Our take, already moving under another request. Not ours to+ # process, but its status is the honest answer.+ return _pipeline_response(+ current.status, current.transcript, BrainDumpInputMode.voice+ )++ audio = await storage.assemble_parts(user_id, recording_id)+ if not audio:+ # Nothing buffered server-side. The browser still holds every part+ # in IndexedDB, so this is a re-upload prompt, not a lost dump.+ await db.mark_failed(user_id, recording_id, "no_audio_received")+ return FinalizeResponse(+ status=BrainDumpStatus.failed,+ input_mode=BrainDumpInputMode.voice,+ error_code="no_audio_received",+ )++ filename = _audio_filename(recording_id, mime_type)+ await scan_content_safe(audio, filename=filename)+ try:+ audio_path = await storage.store_audio(user_id, audio, filename)+ except Exception as e:+ logger.warning("Brain dump audio storage failed for user %s: %s", user_id, e)+ await db.mark_failed(user_id, recording_id, "storage_failed")+ return FinalizeResponse(+ status=BrainDumpStatus.failed,+ input_mode=BrainDumpInputMode.voice,+ error_code="storage_failed",+ )++ await db.update_dump(+ user_id,+ recording_id,+ status=BrainDumpStatus.transcribing,+ audioPath=audio_path,+ mimeType=mime_type,+ sizeBytes=len(audio),+ durationSecs=duration_secs,+ errorCode=None,+ )+ # The audio is durable now; the buffer has done its job.+ await storage.discard_parts(user_id, recording_id)++ started_at = time.monotonic()+ try:+ result = await transcription.transcribe(audio, filename, duration_secs)+ except Exception as e:+ logger.warning("Brain dump transcription failed for user %s: %s", user_id, e)+ error_code = (+ "transcription_unavailable"+ if isinstance(e, transcription.TranscriptionUnavailableError)+ else "transcription_failed"+ )+ await db.mark_failed(user_id, recording_id, error_code)+ return FinalizeResponse(+ status=BrainDumpStatus.failed,+ input_mode=BrainDumpInputMode.voice,+ error_code=error_code,+ )++ logger.info(+ "Brain dump transcribed for user %s in %.1fs (%s chars)",+ user_id,+ time.monotonic() - started_at,+ len(result.text),+ )+ await db.update_dump(+ user_id,+ recording_id,+ status=BrainDumpStatus.transcribed,+ transcript=result.text,+ transcriptLang=result.language,+ )+ # Finalize only does the audio work. Extraction and the Sonnet+ # greeting are LLM calls that can outlive the frontend proxy's 30s+ # request timeout, so they run after this response is sent — the+ # preparing step polls /status and holds the user until+ # ``greeting_ready`` flips.+ background_tasks.add_task(+ _run_background_jobs,+ user_id,+ recording_id,+ result.text,+ BrainDumpInputMode.voice,+ )+ return _pipeline_response(+ BrainDumpStatus.transcribed, result.text, BrainDumpInputMode.voice+ )+++async def finalize_typed_dump(+ user_id: str,+ recording_id: str,+ text: str,+ background_tasks: BackgroundTasks,+) -> FinalizeResponse:+ # Same idempotency contract as the voice path: a repeated submit must+ # not reset the row or queue a second extraction and greeting on top+ # of the pair already running.+ existing = await db.get_dump(user_id)+ if (+ existing is not None+ and existing.recordingId == recording_id+ and existing.status+ in (+ BrainDumpStatus.transcribing,+ BrainDumpStatus.transcribed,+ BrainDumpStatus.extracting,+ BrainDumpStatus.completed,+ )+ ):+ return _pipeline_response(+ existing.status, existing.transcript, existing.inputMode+ )++ await db.start_dump(user_id, recording_id, BrainDumpInputMode.typed)+ # Same reasoning as the voice path: only the caller that wins this+ # transition queues the extraction and greeting pair.+ claimed = await db.claim_transition(+ user_id,+ recording_id,+ expected=BrainDumpStatus.recording_uploaded,+ new=BrainDumpStatus.transcribed,+ transcript=text.strip(),+ )+ if not claimed:+ # Losing the claim means another finalize owns the row — this+ # take, or a newer one submitted from a second tab. Either way+ # this request must not queue a second pipeline, so there is no+ # falling through to the tasks below.+ current = await db.get_dump(user_id)+ if current is None:+ return FinalizeResponse(+ status=BrainDumpStatus.failed, input_mode=BrainDumpInputMode.typed+ )+ if current.recordingId != recording_id:+ return FinalizeResponse(+ status=BrainDumpStatus.failed,+ input_mode=BrainDumpInputMode.typed,+ error_code="superseded",+ )+ return _pipeline_response(current.status, current.transcript, current.inputMode)++ background_tasks.add_task(+ _run_background_jobs,+ user_id,+ recording_id,+ text.strip(),+ BrainDumpInputMode.typed,+ )+ return _pipeline_response(+ BrainDumpStatus.transcribed, text.strip(), BrainDumpInputMode.typed+ )+++async def finalize_skipped_dump(user_id: str, recording_id: str) -> FinalizeResponse:+ """Record the skip so the copilot intro can take Path B.++ Skipping is a first-class outcome, not an error — the row exists so+ the intro endpoint can tell "skipped" apart from "never got here".+ """+ await db.start_dump(user_id, recording_id, BrainDumpInputMode.skipped)+ await db.update_dump(user_id, recording_id, status=BrainDumpStatus.completed)+ return FinalizeResponse(+ status=BrainDumpStatus.completed, input_mode=BrainDumpInputMode.skipped+ )+++async def _run_background_jobs(+ user_id: str,+ recording_id: str,+ transcript: str,+ input_mode: BrainDumpInputMode,+) -> None:+ """Run the greeting pipeline and the recommendation job side by side.++ One background task, not two: ``BackgroundTasks`` awaits what it is+ given strictly in order, so a second entry would not start until the+ greeting's LLM calls had finished — and would never start at all if+ something escaped the first one. Gathered here they are genuinely+ concurrent, and neither can take the other down.+ """+ await asyncio.gather(+ _run_completion(user_id, recording_id, transcript, input_mode),+ _run_provider_recommendations(user_id, recording_id, transcript),+ return_exceptions=True,+ )+++async def _run_completion(+ user_id: str,+ recording_id: str,+ transcript: str,+ input_mode: BrainDumpInputMode,+) -> None:+ """Background half of the pipeline, run after the response is sent.++ Anything that escapes here has no request to report to, so it marks+ the dump failed — that releases the preparing screen (it polls+ /status) instead of leaving it to hang until its ceiling.+ """+ try:+ await _extract_and_complete(user_id, recording_id, transcript, input_mode)+ except Exception as e: # background task, nowhere to raise+ logger.error("Brain dump completion failed for user %s: %s", user_id, e)+ await db.mark_failed(user_id, recording_id, "understanding_failed")+++async def _run_provider_recommendations(+ user_id: str, recording_id: str, transcript: str+) -> None:+ """The recommendation job, deliberately separate from ``_run_completion``.++ It writes only ``recommendedProviders`` — never the dump status — so a+ failure here can't strand the loading screen or mask a good greeting.+ ``generate_recommendations`` never raises; an empty result is stored+ as the final answer so the client stops polling.+ """+ recommendations = await recommend.generate_recommendations(transcript)+ try:+ await db.update_dump(+ user_id,+ recording_id,+ recommendedProviders=Json([r.model_dump() for r in recommendations]),+ )+ except Exception as e: # background task, nowhere to raise+ logger.warning(+ "Brain dump provider recommendations not stored for user %s: %s",+ user_id,+ e,+ )+++async def get_recommended_providers(user_id: str) -> RecommendedProvidersResponse:+ """Stored recommendations for the welcome dialog's connect panel.++ ``ready`` is true once the job has written its result (a null column+ means it is still running), or immediately when there is no transcript+ to recommend from. The dump ``status`` deliberately plays no part —+ it tracks the greeting pipeline, which can finish before this job.+ The client caps its own polling for rows the job never got to write+ (e.g. a process restart mid-job).+ """+ dump = await db.get_dump(user_id)+ if dump is None or not (dump.transcript or "").strip():+ return RecommendedProvidersResponse(ready=True)+ if dump.recommendedProviders is None:+ return RecommendedProvidersResponse(ready=False)+ return RecommendedProvidersResponse(+ ready=True, providers=_stored_recommendations(dump.recommendedProviders)+ )+++def _stored_recommendations(raw: object) -> list[RecommendedProvider]:+ if not isinstance(raw, list):+ return []+ return [+ RecommendedProvider(provider=item["provider"], reason=item.get("reason", ""))+ for item in raw+ if isinstance(item, dict) and isinstance(item.get("provider"), str)+ ]+++async def _extract_and_complete(+ user_id: str,+ recording_id: str,+ transcript: str,+ input_mode: BrainDumpInputMode,+) -> FinalizeResponse:+ # Losing this write means a newer take owns the row. The row writes+ # below would all no-op on their own, but the business understanding+ # is shared user context with no recording id on it — carrying on+ # would fold an abandoned take's transcript into the context the live+ # take is about to write. So the whole job stops here.+ if not await db.update_dump(+ user_id, recording_id, status=BrainDumpStatus.extracting+ ):+ return _superseded_response(user_id, input_mode)++ extracted = await _extract_understanding(user_id, transcript)+ extracted.additional_notes = _append_note(+ extracted.additional_notes, transcript, input_mode+ )+ # Re-checked rather than inferred from the claim above: extraction is+ # an LLM call, so the row has had seconds — minutes, for a long+ # transcript — to change hands since.+ if not await db.owns_dump(user_id, recording_id):+ return _superseded_response(user_id, input_mode)+ await upsert_business_understanding(user_id, extracted)++ # Generated here, while the onboarding loading screen is still up, so+ # the copilot home can render its greeting without waiting.+ greeting, suggested_prompts = await intro.generate_intro(transcript)++ await db.update_dump(+ user_id,+ recording_id,+ status=BrainDumpStatus.completed,+ errorCode=None,+ greeting=greeting,+ suggestedPrompts=Json([p.model_dump() for p in suggested_prompts]),+ )+ return _completed_response(transcript, input_mode)+++async def _extract_understanding(+ user_id: str, transcript: str+) -> BusinessUnderstandingInput:+ understanding = await get_business_understanding(user_id)+ formatted = format_brain_dump_for_extraction(+ user_name=(understanding.user_name if understanding else None) or "",+ user_role=(understanding.user_role if understanding else None) or "",+ transcript=transcript,+ )+ try:+ return await extract_business_understanding(formatted)+ except Exception as e:+ # A failed extraction must not cost the user their transcript: the+ # raw text still lands in the understanding, so the copilot's+ # <user_context> is personalised even without structured fields.+ logger.warning("Brain dump extraction failed for user %s: %s", user_id, e)+ return BusinessUnderstandingInput.model_construct()+++def _superseded_response(+ user_id: str, input_mode: BrainDumpInputMode+) -> FinalizeResponse:+ logger.info("Brain dump take for user %s superseded; stopping", user_id)+ return FinalizeResponse(+ status=BrainDumpStatus.failed, input_mode=input_mode, error_code="superseded"+ )+++def _append_note(+ existing: str | None, transcript: str, input_mode: BrainDumpInputMode+) -> str:+ label = (+ "Onboarding brain dump (typed)"+ if input_mode == BrainDumpInputMode.typed+ else "Onboarding brain dump (spoken)"+ )+ note = f"{label}: {transcript}"+ return f"{existing}\n\n{note}" if existing else note+++def _completed_response(+ transcript: str | None, input_mode: BrainDumpInputMode+) -> FinalizeResponse:+ return _pipeline_response(BrainDumpStatus.completed, transcript, input_mode)+++def _pipeline_response(+ status: BrainDumpStatus,+ transcript: str | None,+ input_mode: BrainDumpInputMode,+) -> FinalizeResponse:+ return FinalizeResponse(+ status=status,+ input_mode=input_mode,+ transcript_preview=(transcript or "")[:TRANSCRIPT_PREVIEW_CHARS] or None,+ )+++def _audio_filename(recording_id: str, mime_type: str | None) -> str:+ extensions = {+ "audio/webm": "webm",+ "audio/mp4": "m4a",+ "audio/mpeg": "mp3",+ "audio/wav": "wav",+ "audio/x-wav": "wav",+ "audio/ogg": "ogg",+ }+ return f"brain-dump-{recording_id}.{extensions.get(mime_type or '', 'webm')}"+++async def get_intro_card(user_id: str) -> IntroCardResponse:+ """The copilot home's greeting content.++ Path B covers every case where there is nothing to reflect back — the+ user skipped, or the pipeline failed and we have no transcript. A+ greeting that invites them to record is honest in both; one that+ claims to have heard them would not be.+ """+ dump = await db.get_dump(user_id)+ if dump is not None and dump.greetingSeen:+ # Content deliberately withheld: the client caches "done" and the+ # greeting must never reappear once the first message is sent.+ return IntroCardResponse(path="A", greeting="", greeting_done=True)++ if (+ dump is None+ or dump.inputMode == BrainDumpInputMode.skipped+ or not (dump.transcript or "").strip()+ ):+ return IntroCardResponse(+ path="B",+ greeting=prompts.PATH_B_GREETING,+ prompts=intro.fallback_prompts(),+ )++ greeting = (dump.greeting or "").strip()+ if not greeting and dump.status not in (+ BrainDumpStatus.completed,+ BrainDumpStatus.failed,+ ):+ # The background half of the pipeline is still writing the+ # greeting. An empty Path A response tells the client to keep+ # polling — serving the generic fallback to a brand-new user+ # would waste the personalised one that is seconds away.+ return IntroCardResponse(path="A", greeting="")+ if not greeting:+ # Completed before the greeting column existed, or generation+ # terminally failed after the transcript landed.+ greeting, _ = intro.fallback_intro(dump.transcript or "")+ return IntroCardResponse(+ path="A",+ greeting=greeting,+ prompts=_stored_prompts(dump.suggestedPrompts),+ transcript=dump.transcript,+ )+++def _stored_prompts(raw: object) -> list[SuggestedPrompt]:+ """Rehydrate the stored prompt list, dropping anything malformed.++ A row written by an older build (or a hand-edited one) must degrade+ to the generic set rather than 500 the copilot home.+ """+ if not isinstance(raw, list):+ return intro.fallback_prompts()+ parsed = [+ SuggestedPrompt(+ title=item["title"],+ prompt=item["prompt"],+ icon=(+ item["icon"]+ if item.get("icon") in intro.PROMPT_ICONS+ else intro.DEFAULT_PROMPT_ICON+ ),+ )+ for item in raw+ if isinstance(item, dict)+ and isinstance(item.get("title"), str)+ and isinstance(item.get("prompt"), str)+ ]+ return parsed or intro.fallback_prompts()+++async def mark_greeting_done(user_id: str) -> None:+ """Record that the user has completed the greeting (sent a message).++ Upserts so the flag also sticks for users who never produced a dump+ row — the greeting content, when any exists, is kept forever.+ """+ await db.mark_greeting_seen(user_id)diff --git a/autogpt_platform/backend/backend/api/features/onboarding_dump/service_test.py b/autogpt_platform/backend/backend/api/features/onboarding_dump/service_test.pynew file mode 100644index 0000000000..f1ca3d7a74--- /dev/null+++ b/autogpt_platform/backend/backend/api/features/onboarding_dump/service_test.py@@ -0,0 +1,898 @@+"""Pipeline-level tests for the brain-dump service.++These call the service functions directly so the guarantees that matter+(the audio survives a failed transcription, the *complete* transcript is+what reaches the database and the understanding) can be asserted on the+stored row rather than inferred from a response body.+"""++import asyncio+from typing import Any+from unittest.mock import AsyncMock++import pytest+from fastapi import BackgroundTasks+from prisma.enums import BrainDumpInputMode, BrainDumpStatus+from prisma.models import OnboardingBrainDump+from pytest_mock import MockerFixture++from backend.api.features.onboarding_dump import (+ db,+ intro,+ prompts,+ service,+ transcription,+)+from backend.data.understanding import BusinessUnderstandingInput++USER_ID = "user-1"+RECORDING_ID = "rec-1"+TRANSCRIPT = "I run a bakery and want the weekly order emails handled."+AUDIO_PATH = "brain-dumps/rec-1.webm"+LONG_TRANSCRIPT = "the bakery ships pastries every friday morning " * 400++# The columns a new take clears, taken from the module under test so this+# fake cannot drift from it. A real read returns ``suggestedPrompts`` as a+# plain list rather than the ``Json`` wrapper used to write it.+_NEW_TAKE_COLUMNS: dict[str, Any] = {+ **db._TAKE_OWNED_RESET,+ "suggestedPrompts": [],+ "errorCode": None,+}+++class DumpStore:+ """In-memory stand-in for the one-row-per-user brain dump table."""++ def __init__(self) -> None:+ self.row: OnboardingBrainDump | None = None+ self.statuses: list[BrainDumpStatus] = []+ self.transcripts: list[str | None] = []++ async def get_dump(self, user_id: str) -> OnboardingBrainDump | None:+ return self.row++ async def owns_dump(self, user_id: str, recording_id: str) -> bool:+ return self.row is not None and self.row.recordingId == recording_id++ async def start_dump(+ self, user_id: str, recording_id: str, input_mode: BrainDumpInputMode+ ) -> OnboardingBrainDump:+ # Mirrors the real `start_dump`: a take already moving through+ # the pipeline is returned untouched, so a replayed part 0 or a+ # repeated finalize cannot reset it.+ if (+ self.row is not None+ and self.row.status+ in (+ BrainDumpStatus.recording_uploaded,+ BrainDumpStatus.transcribing,+ BrainDumpStatus.transcribed,+ BrainDumpStatus.extracting,+ BrainDumpStatus.completed,+ )+ and (+ self.row.recordingId == recording_id+ or input_mode != BrainDumpInputMode.voice+ )+ ):+ return self.row+ if self.row is not None and self.row.recordingId == recording_id:+ # A retry of the same take: it keeps everything it has already+ # produced, exactly as the real claim does.+ self.row.status = BrainDumpStatus.recording_uploaded+ self.row.inputMode = input_mode+ self.row.errorCode = None+ else:+ # model_construct skips defaults, so every column the code+ # under test reads has to be seeded explicitly — otherwise a+ # plain attribute access raises instead of returning the DB+ # default. A different take starts from the cleared set.+ self.row = OnboardingBrainDump.model_construct(+ userId=user_id,+ recordingId=recording_id,+ status=BrainDumpStatus.recording_uploaded,+ inputMode=input_mode,+ **_NEW_TAKE_COLUMNS,+ )+ self.statuses.append(BrainDumpStatus.recording_uploaded)+ return self.row++ async def update_dump(self, user_id: str, recording_id: str, **fields: Any) -> bool:+ # Mirrors the scoped UPDATE: a write from a take the row has+ # already moved past hits nothing.+ if self.row is None or self.row.recordingId != recording_id:+ return False+ for name, value in fields.items():+ setattr(self.row, name, value)+ status = fields.get("status")+ if status is not None:+ self.statuses.append(status)+ if "transcript" in fields:+ self.transcripts.append(fields["transcript"])+ return True++ async def claim_transition(+ self,+ user_id: str,+ recording_id: str,+ *,+ expected: BrainDumpStatus,+ new: BrainDumpStatus,+ **fields: Any,+ ) -> bool:+ """Mirrors the conditional UPDATE: only matching rows transition."""+ if (+ self.row is None+ or self.row.recordingId != recording_id+ or self.row.status != expected+ ):+ return False+ await self.update_dump(user_id, recording_id, status=new, **fields)+ return True++ async def mark_failed(+ self, user_id: str, recording_id: str, error_code: str+ ) -> None:+ await self.update_dump(+ user_id,+ recording_id,+ status=BrainDumpStatus.failed,+ errorCode=error_code,+ )++[email protected](autouse=True)+def dumps(mocker: MockerFixture) -> DumpStore:+ store = DumpStore()+ module = "backend.api.features.onboarding_dump.db"+ mocker.patch(f"{module}.get_dump", new=store.get_dump)+ mocker.patch(f"{module}.owns_dump", new=store.owns_dump)+ mocker.patch(f"{module}.start_dump", new=store.start_dump)+ mocker.patch(f"{module}.update_dump", new=store.update_dump)+ mocker.patch(f"{module}.mark_failed", new=store.mark_failed)+ mocker.patch(f"{module}.claim_transition", new=store.claim_transition)+ return store++[email protected](autouse=True)+def storage_mocks(mocker: MockerFixture) -> dict[str, AsyncMock]:+ module = "backend.api.features.onboarding_dump.storage"+ mocks = {+ "assemble_parts": AsyncMock(return_value=b"opus-audio-bytes"),+ "store_audio": AsyncMock(return_value=AUDIO_PATH),+ "discard_parts": AsyncMock(),+ }+ for name, mock in mocks.items():+ mocker.patch(f"{module}.{name}", new=mock)+ return mocks++[email protected](autouse=True)+def transcribe(mocker: MockerFixture) -> AsyncMock:+ mock = AsyncMock(+ return_value=transcription.TranscriptionResult(+ text=TRANSCRIPT, language="en", model="gpt-4o-transcribe"+ )+ )+ mocker.patch(+ "backend.api.features.onboarding_dump.transcription.transcribe", new=mock+ )+ return mock++[email protected](autouse=True)+def extraction(mocker: MockerFixture) -> dict[str, AsyncMock]:+ module = "backend.api.features.onboarding_dump.service"+ mocks = {+ "scan_content_safe": AsyncMock(),+ "get_business_understanding": AsyncMock(return_value=None),+ "extract_business_understanding": AsyncMock(+ return_value=BusinessUnderstandingInput.model_construct()+ ),+ "upsert_business_understanding": AsyncMock(),+ }+ for name, mock in mocks.items():+ mocker.patch(f"{module}.{name}", new=mock)+ return mocks+++async def start_voice_take(dumps: DumpStore) -> None:+ await dumps.start_dump(USER_ID, RECORDING_ID, BrainDumpInputMode.voice)+++async def finalize_voice(duration_secs: float = 12.0, mime_type: str | None = None):+ """Run finalize plus its background half (extraction + greeting),+ the way starlette does after the response is sent."""+ background = BackgroundTasks()+ response = await service.finalize_voice_dump(+ USER_ID, RECORDING_ID, duration_secs, mime_type, background+ )+ await background()+ return response+++async def test_transcription_failure_keeps_the_audio_and_records_the_error(+ dumps: DumpStore, transcribe: AsyncMock+):+ await start_voice_take(dumps)+ transcribe.side_effect = RuntimeError("provider blew up")++ response = await finalize_voice()++ assert response.status == BrainDumpStatus.failed+ assert response.error_code == "transcription_failed"+ assert dumps.row is not None+ assert dumps.row.status == BrainDumpStatus.failed+ assert dumps.row.errorCode == "transcription_failed"+ # The zero-lost-recordings guarantee: the audio is still addressable.+ assert dumps.row.audioPath == AUDIO_PATH+++async def test_missing_stt_provider_is_reported_as_unavailable(+ dumps: DumpStore, transcribe: AsyncMock+):+ await start_voice_take(dumps)+ transcribe.side_effect = service.transcription.TranscriptionUnavailableError(+ "no key"+ )++ response = await finalize_voice()++ assert response.error_code == "transcription_unavailable"+ assert dumps.row is not None+ assert dumps.row.errorCode == "transcription_unavailable"+ assert dumps.row.audioPath == AUDIO_PATH+++async def test_audio_storage_failure_is_retryable(+ dumps: DumpStore,+ storage_mocks: dict[str, AsyncMock],+ transcribe: AsyncMock,+):+ await start_voice_take(dumps)+ storage_mocks["store_audio"].side_effect = RuntimeError("storage unavailable")++ response = await finalize_voice()++ assert response.status == BrainDumpStatus.failed+ assert response.error_code == "storage_failed"+ assert dumps.row is not None+ assert dumps.row.status == BrainDumpStatus.failed+ assert dumps.row.errorCode == "storage_failed"+ storage_mocks["discard_parts"].assert_not_awaited()+ transcribe.assert_not_awaited()+++async def test_transcript_lands_in_the_business_understanding(+ dumps: DumpStore, extraction: dict[str, AsyncMock]+):+ await start_voice_take(dumps)++ await finalize_voice(mime_type="audio/webm")++ upsert = extraction["upsert_business_understanding"]+ upsert.assert_awaited_once()+ user_id, understanding = upsert.await_args.args+ assert user_id == USER_ID+ assert isinstance(understanding, BusinessUnderstandingInput)+ assert understanding.additional_notes == (+ f"Onboarding brain dump (spoken): {TRANSCRIPT}"+ )+++async def test_typed_dump_is_labelled_as_typed_in_the_understanding(+ extraction: dict[str, AsyncMock]+):+ background = BackgroundTasks()+ await service.finalize_typed_dump(+ USER_ID, RECORDING_ID, "I run a bakery.", background+ )+ await background()++ _, understanding = extraction["upsert_business_understanding"].await_args.args+ assert understanding.additional_notes == (+ "Onboarding brain dump (typed): I run a bakery."+ )+++async def test_repeating_a_typed_finalize_does_not_restart_the_pipeline(+ dumps: DumpStore, extraction: dict[str, AsyncMock]+):+ """A second submit of the same typed take is a no-op.++ Without the guard the row was re-claimed — dropping an in-flight+ extraction back to ``recording_uploaded`` — and a second pair of+ background jobs was queued on top of the running ones.+ """+ first = BackgroundTasks()+ await service.finalize_typed_dump(USER_ID, RECORDING_ID, "I run a bakery.", first)+ await first()+ statuses_after_first = list(dumps.statuses)++ second = BackgroundTasks()+ response = await service.finalize_typed_dump(+ USER_ID, RECORDING_ID, "I run a bakery.", second+ )++ assert response.status == BrainDumpStatus.completed+ assert dumps.statuses == statuses_after_first+ assert BrainDumpStatus.recording_uploaded not in statuses_after_first[1:]+ # Nothing queued the second time round.+ assert second.tasks == []+ assert extraction["upsert_business_understanding"].await_count == 1+++async def test_a_stale_typed_finalize_cannot_replace_a_completed_take(+ dumps: DumpStore,+):+ newer_tasks = BackgroundTasks()+ await service.finalize_typed_dump(+ USER_ID, "rec-newer", "This is the newer transcript.", newer_tasks+ )+ await newer_tasks()+ assert dumps.row is not None+ newer_transcript = dumps.row.transcript+ stale_tasks = BackgroundTasks()++ response = await service.finalize_typed_dump(+ USER_ID, "rec-stale", "This stale request arrived late.", stale_tasks+ )++ assert response.status == BrainDumpStatus.failed+ assert response.error_code == "superseded"+ assert dumps.row.recordingId == "rec-newer"+ assert dumps.row.transcript == newer_transcript+ assert stale_tasks.tasks == []+++async def test_typed_finalize_cannot_replace_an_uploaded_voice_take(+ dumps: DumpStore,+):+ await start_voice_take(dumps)+ tasks = BackgroundTasks()++ response = await service.finalize_typed_dump(+ USER_ID, "rec-typed", "Typed text arrived from another tab.", tasks+ )++ assert response.status == BrainDumpStatus.failed+ assert response.error_code == "superseded"+ assert dumps.row is not None+ assert dumps.row.recordingId == RECORDING_ID+ assert dumps.row.status == BrainDumpStatus.recording_uploaded+ assert dumps.row.inputMode == BrainDumpInputMode.voice+ assert tasks.tasks == []+++async def test_typed_finalize_does_not_relabel_voice_transcription(+ dumps: DumpStore,+):+ await start_voice_take(dumps)+ await dumps.update_dump(USER_ID, RECORDING_ID, status=BrainDumpStatus.transcribing)+ tasks = BackgroundTasks()++ response = await service.finalize_typed_dump(+ USER_ID, RECORDING_ID, "Typed text arrived late.", tasks+ )++ assert response.status == BrainDumpStatus.transcribing+ assert response.input_mode == BrainDumpInputMode.voice+ assert dumps.row is not None+ assert dumps.row.inputMode == BrainDumpInputMode.voice+ assert tasks.tasks == []+++def release_both_past_the_guard(mocker: MockerFixture, dumps: "DumpStore"):+ """Hold the first two `get_dump` calls until both have happened.++ Without this the coroutines never interleave — none of the fakes+ suspend, so `asyncio.gather` just runs one to completion and then the+ other, and the race under test never occurs.+ """+ barrier = asyncio.Barrier(2)+ inner = dumps.get_dump+ arrivals = 0++ async def gated(user_id: str):+ nonlocal arrivals+ arrivals += 1+ row = await inner(user_id)+ # Snapshot first, *then* wait. The read is what races: both+ # callers must come away holding the status as it was before+ # either of them acted. The store mutates its single row in+ # place, so without the copy the loser would silently observe+ # the winner's later writes and the race would vanish.+ snapshot = row.model_copy() if row is not None else None+ if arrivals <= 2:+ await barrier.wait()+ return snapshot++ mocker.patch("backend.api.features.onboarding_dump.db.get_dump", new=gated)+++async def test_two_concurrent_voice_finalizes_only_process_the_take_once(+ mocker: MockerFixture,+ dumps: DumpStore,+ storage_mocks: dict[str, AsyncMock],+ transcribe: AsyncMock,+):+ """Only the caller that wins the atomic claim does the work.++ Both requests read the guard while the status is still+ `recording_uploaded`, so both pass it. Without the conditional UPDATE+ both then assembled, stored and transcribed the same recording.+ """+ await start_voice_take(dumps)+ release_both_past_the_guard(mocker, dumps)++ await asyncio.gather(+ service.finalize_voice_dump(+ USER_ID, RECORDING_ID, 12.0, None, BackgroundTasks()+ ),+ service.finalize_voice_dump(+ USER_ID, RECORDING_ID, 12.0, None, BackgroundTasks()+ ),+ )++ transcribe.assert_awaited_once()+ storage_mocks["store_audio"].assert_awaited_once()+++async def test_two_concurrent_typed_finalizes_only_queue_one_pipeline(+ mocker: MockerFixture, dumps: DumpStore, extraction: dict[str, AsyncMock]+):+ await dumps.start_dump(USER_ID, RECORDING_ID, BrainDumpInputMode.typed)+ release_both_past_the_guard(mocker, dumps)+ first_tasks = BackgroundTasks()+ second_tasks = BackgroundTasks()++ await asyncio.gather(+ service.finalize_typed_dump(+ USER_ID, RECORDING_ID, "I run a bakery.", first_tasks+ ),+ service.finalize_typed_dump(+ USER_ID, RECORDING_ID, "I run a bakery.", second_tasks+ ),+ )+ await first_tasks()+ await second_tasks()++ # One winner queues the extraction/greeting pair; the loser queues+ # nothing, so the understanding is written exactly once.+ assert extraction["upsert_business_understanding"].await_count == 1+++async def test_a_typed_finalize_that_loses_the_claim_queues_nothing(+ mocker: MockerFixture, dumps: DumpStore, extraction: dict[str, AsyncMock]+):+ """Losing the claim must not fall through to the background jobs.++ The earlier version returned early only when the row still belonged+ to this take; any other losing case carried on and queued a second+ extraction and greeting on top of the winner's.+ """+ await dumps.start_dump(USER_ID, RECORDING_ID, BrainDumpInputMode.typed)+ mocker.patch(+ "backend.api.features.onboarding_dump.db.claim_transition",+ new=AsyncMock(return_value=False),+ )+ tasks = BackgroundTasks()++ response = await service.finalize_typed_dump(+ USER_ID, RECORDING_ID, "I run a bakery.", tasks+ )++ assert tasks.tasks == []+ assert response.input_mode == BrainDumpInputMode.typed+ await tasks()+ extraction["upsert_business_understanding"].assert_not_awaited()+++async def test_a_voice_finalize_that_loses_the_claim_does_no_work(+ mocker: MockerFixture,+ dumps: DumpStore,+ storage_mocks: dict[str, AsyncMock],+ transcribe: AsyncMock,+):+ await start_voice_take(dumps)+ mocker.patch(+ "backend.api.features.onboarding_dump.db.claim_transition",+ new=AsyncMock(return_value=False),+ )++ await service.finalize_voice_dump(+ USER_ID, RECORDING_ID, 12.0, None, BackgroundTasks()+ )++ storage_mocks["assemble_parts"].assert_not_awaited()+ storage_mocks["store_audio"].assert_not_awaited()+ transcribe.assert_not_awaited()+++async def test_a_voice_finalize_for_a_superseded_take_reports_its_own_take(+ dumps: DumpStore, storage_mocks: dict[str, AsyncMock], transcribe: AsyncMock+):+ """A second tab's take owns the row — say so, don't mirror it.++ There is one row per user, so a newer recording overwrites the older+ one. The losing finalize used to return whatever status it found,+ which is the *other* take's progress dressed up as this one: the old+ tab would narrate a recording it never made, and could be walked+ through to a greeting built from audio the user recorded elsewhere.+ """+ await start_voice_take(dumps)+ # Second tab starts its own take; the row is now theirs.+ await dumps.start_dump(USER_ID, "rec-2", BrainDumpInputMode.voice)++ response = await service.finalize_voice_dump(+ USER_ID, RECORDING_ID, 12.0, None, BackgroundTasks()+ )++ assert response.status == BrainDumpStatus.failed+ assert response.error_code == "superseded"+ # The live take must come through untouched — no status write, and+ # none of the pipeline run against the wrong recording.+ assert dumps.row is not None+ assert dumps.row.recordingId == "rec-2"+ assert dumps.row.status == BrainDumpStatus.recording_uploaded+ storage_mocks["assemble_parts"].assert_not_awaited()+ transcribe.assert_not_awaited()+++async def test_a_take_superseded_mid_transcription_never_writes_to_the_new_row(+ dumps: DumpStore, transcribe: AsyncMock+):+ """The old take loses the row the moment a second tab claims it.++ Transcription can run for minutes. Scoped on ``userId`` alone, every+ write after the claim — transcript, status, greeting, failure code —+ landed on whatever take held the row by the time it finished. The new+ recording then looked already-transcribed to the idempotency guard+ and was never transcribed at all.+ """+ await start_voice_take(dumps)++ async def supersede(*args: Any, **kwargs: Any):+ await dumps.start_dump(USER_ID, "rec-2", BrainDumpInputMode.voice)+ return transcription.TranscriptionResult(+ text=TRANSCRIPT, language="en", model="gpt-4o-transcribe"+ )++ transcribe.side_effect = supersede++ await finalize_voice()++ assert dumps.row is not None+ assert dumps.row.recordingId == "rec-2"+ assert dumps.row.transcript is None+ assert dumps.row.greeting is None+ assert dumps.row.status == BrainDumpStatus.recording_uploaded+++async def test_a_superseded_take_never_writes_to_the_shared_understanding(+ dumps: DumpStore, extraction: dict[str, AsyncMock]+):+ """The background half has to stop, not just fail to write its row.++ Row writes carry a ``recordingId`` guard so they no-op once a second+ tab owns the row, but the business understanding is shared user+ context with no take on it. An abandoned take that kept going folded+ its transcript into the context the live take is about to write.+ """+ await start_voice_take(dumps)+ background = BackgroundTasks()+ await service.finalize_voice_dump(USER_ID, RECORDING_ID, 12.0, None, background)+ # A second tab claims the row between the response and the background+ # half that starlette runs after it.+ await dumps.start_dump(USER_ID, "rec-2", BrainDumpInputMode.voice)++ await background()++ extraction["upsert_business_understanding"].assert_not_awaited()+ assert dumps.row is not None+ assert dumps.row.recordingId == "rec-2"+ assert dumps.row.status == BrainDumpStatus.recording_uploaded+++async def test_a_take_superseded_during_extraction_stops_before_the_understanding(+ dumps: DumpStore, extraction: dict[str, AsyncMock]+):+ """Ownership is re-checked next to the understanding write.++ Extraction is an LLM call, so passing the claim before it says+ nothing about who owns the row after it.+ """+ await start_voice_take(dumps)++ async def supersede(*args: Any, **kwargs: Any) -> BusinessUnderstandingInput:+ await dumps.start_dump(USER_ID, "rec-2", BrainDumpInputMode.voice)+ return BusinessUnderstandingInput.model_construct()++ extraction["extract_business_understanding"].side_effect = supersede++ await finalize_voice()++ extraction["upsert_business_understanding"].assert_not_awaited()+++async def test_a_new_take_never_serves_the_previous_takes_answers(dumps: DumpStore):+ """One row per user, so a new take inherits the last one's columns.++ Left there, the user re-records, gets nowhere, and the copilot greets+ them with the greeting and transcript of a take they replaced —+ while ``/recording`` hands back its audio.+ """+ await start_voice_take(dumps)+ await finalize_voice()+ await dumps.update_dump(USER_ID, RECORDING_ID, greetingSeen=True)+ assert dumps.row is not None and dumps.row.greeting++ # They record again and never finish this one.+ await dumps.start_dump(USER_ID, "rec-2", BrainDumpInputMode.voice)++ card = await service.get_intro_card(USER_ID)+ providers = await service.get_recommended_providers(USER_ID)++ assert dumps.row.transcript is None+ assert dumps.row.greeting is None+ assert dumps.row.audioPath is None+ assert card.path == "B"+ assert card.greeting == prompts.PATH_B_GREETING+ assert card.transcript is None+ assert card.greeting_done is False+ assert providers.ready is True+ assert providers.providers == []+++async def test_a_failed_new_take_gets_an_answer_rather_than_an_endless_wait(+ dumps: DumpStore, transcribe: AsyncMock+):+ """Claiming a new take clears ``greetingSeen``, so the intro is live again.++ That is right while a new greeting is on its way, and it must not+ strand a user whose new take dies: an empty Path A tells the client+ to keep polling, so a failed take has to resolve to Path B instead.+ """+ await start_voice_take(dumps)+ await finalize_voice()+ await dumps.update_dump(USER_ID, RECORDING_ID, greetingSeen=True)++ await dumps.start_dump(USER_ID, "rec-2", BrainDumpInputMode.voice)+ transcribe.side_effect = RuntimeError("provider blew up")+ background = BackgroundTasks()+ await service.finalize_voice_dump(USER_ID, "rec-2", 12.0, None, background)+ await background()++ card = await service.get_intro_card(USER_ID)++ assert dumps.row is not None+ assert dumps.row.status == BrainDumpStatus.failed+ assert card.path == "B"+ assert card.greeting == prompts.PATH_B_GREETING+++async def test_finalizing_a_take_that_never_uploaded_writes_no_row(+ dumps: DumpStore, storage_mocks: dict[str, AsyncMock], transcribe: AsyncMock+):+ """Finalize without a single part told the caller, not the database.++ Nothing was ever uploaded, so there is no row to fail — and creating+ one here would let a late finalize resurrect a dump for a user whose+ row was deleted, which ``update_dump`` exists to prevent. The browser+ still holds every part in IndexedDB, so the caller re-uploads on the+ strength of the response alone.+ """+ storage_mocks["assemble_parts"].return_value = b""++ response = await service.finalize_voice_dump(+ USER_ID, RECORDING_ID, 12.0, None, BackgroundTasks()+ )++ assert response.status == BrainDumpStatus.failed+ assert response.error_code == "no_audio_received"+ assert dumps.row is None+ transcribe.assert_not_awaited()+++async def test_a_buffer_with_no_row_is_kept_rather_than_processed_into_nothing(+ dumps: DumpStore, storage_mocks: dict[str, AsyncMock], transcribe: AsyncMock+):+ """Parts but no row is the one shape that could lose a recording.++ Every ``update_dump`` keys on ``userId`` and silently no-ops when+ there is no row, so processing here would store the audio, drop the+ Redis buffer and hand the client a success it can never read back —+ the transcript, the greeting and the status all written nowhere. The+ buffer has to survive so a re-upload (part 0 recreates the row) can+ still recover the take.+ """+ storage_mocks["assemble_parts"].return_value = b"opus-audio-bytes"++ response = await service.finalize_voice_dump(+ USER_ID, RECORDING_ID, 12.0, None, BackgroundTasks()+ )++ assert response.status == BrainDumpStatus.failed+ assert response.error_code == "no_audio_received"+ assert dumps.row is None+ storage_mocks["store_audio"].assert_not_awaited()+ storage_mocks["discard_parts"].assert_not_awaited()+ transcribe.assert_not_awaited()+++async def test_a_failed_extraction_still_preserves_the_transcript(+ dumps: DumpStore, extraction: dict[str, AsyncMock]+):+ await start_voice_take(dumps)+ extraction["extract_business_understanding"].side_effect = RuntimeError("no llm")++ response = await finalize_voice()++ # Finalize answers as soon as the transcript is stored; the failed+ # extraction is absorbed by the background half.+ assert response.status == BrainDumpStatus.transcribed+ assert dumps.row is not None+ assert dumps.row.status == BrainDumpStatus.completed+ _, understanding = extraction["upsert_business_understanding"].await_args.args+ assert TRANSCRIPT in (understanding.additional_notes or "")+++async def test_long_transcript_is_stored_whole(+ dumps: DumpStore, transcribe: AsyncMock, extraction: dict[str, AsyncMock]+):+ await start_voice_take(dumps)+ transcribe.return_value = transcription.TranscriptionResult(+ text=LONG_TRANSCRIPT, language="en", model="gpt-4o-transcribe"+ )++ await finalize_voice(duration_secs=900.0)++ assert dumps.transcripts == [LONG_TRANSCRIPT]+ assert dumps.row is not None+ assert dumps.row.transcript == LONG_TRANSCRIPT+ _, understanding = extraction["upsert_business_understanding"].await_args.args+ assert LONG_TRANSCRIPT in (understanding.additional_notes or "")+++async def test_intro_card_takes_path_a_with_the_stored_greeting(dumps: DumpStore):+ await dumps.start_dump(USER_ID, RECORDING_ID, BrainDumpInputMode.voice)+ await dumps.update_dump(+ USER_ID,+ RECORDING_ID,+ status=BrainDumpStatus.completed,+ transcript=TRANSCRIPT,+ greeting="You mentioned the weekly order emails.",+ suggestedPrompts=[+ {"title": "Handle the weekly order emails", "prompt": "Please handle them."}+ ],+ )++ card = await service.get_intro_card(USER_ID)++ assert card.path == "A"+ assert card.greeting == "You mentioned the weekly order emails."+ assert [p.title for p in card.prompts] == ["Handle the weekly order emails"]+ assert [p.prompt for p in card.prompts] == ["Please handle them."]+ assert card.greeting_done is False+ assert card.transcript == TRANSCRIPT+++async def test_intro_card_takes_path_b_when_the_user_skipped(dumps: DumpStore):+ await dumps.start_dump(USER_ID, RECORDING_ID, BrainDumpInputMode.skipped)+ await dumps.update_dump(USER_ID, RECORDING_ID, status=BrainDumpStatus.completed)++ card = await service.get_intro_card(USER_ID)++ assert card.path == "B"+ assert card.greeting == prompts.PATH_B_GREETING+ assert card.prompts == intro.fallback_prompts()+ assert card.greeting_done is False+ assert card.transcript is None+++async def test_intro_card_takes_path_b_when_the_transcript_is_empty(dumps: DumpStore):+ await dumps.start_dump(USER_ID, RECORDING_ID, BrainDumpInputMode.voice)+ await dumps.update_dump(+ USER_ID,+ RECORDING_ID,+ status=BrainDumpStatus.failed,+ transcript=" ",+ errorCode="whatever",+ )++ card = await service.get_intro_card(USER_ID)++ assert card.path == "B"+ assert card.greeting == prompts.PATH_B_GREETING+++async def test_intro_card_takes_path_b_without_a_dump_row():+ card = await service.get_intro_card(USER_ID)++ assert card.path == "B"+ assert card.greeting == prompts.PATH_B_GREETING+ assert card.prompts == intro.fallback_prompts()+ assert card.greeting_done is False+++async def test_intro_card_is_withheld_once_the_greeting_was_seen(dumps: DumpStore):+ await dumps.start_dump(USER_ID, RECORDING_ID, BrainDumpInputMode.voice)+ await dumps.update_dump(+ USER_ID,+ RECORDING_ID,+ status=BrainDumpStatus.completed,+ transcript=TRANSCRIPT,+ greeting="You mentioned the weekly order emails.",+ greetingSeen=True,+ )++ card = await service.get_intro_card(USER_ID)++ assert card.greeting_done is True+ assert card.greeting == ""+ assert card.prompts == []+ assert card.transcript is None+++async def test_intro_card_falls_back_when_the_greeting_was_never_generated(+ dumps: DumpStore,+):+ # A dump processed before the greeting existed, or one whose generation+ # failed. Still Path A — we did hear them — just without the prose.+ await dumps.start_dump(USER_ID, RECORDING_ID, BrainDumpInputMode.voice)+ await dumps.update_dump(+ USER_ID, RECORDING_ID, status=BrainDumpStatus.completed, transcript=TRANSCRIPT+ )++ card = await service.get_intro_card(USER_ID)++ assert card.path == "A"+ assert card.greeting == intro.fallback_intro(TRANSCRIPT)[0]+ # An empty stored list degrades to the generic starters — a greeting+ # with nothing under it would look broken.+ assert card.prompts == intro.fallback_prompts()+++async def test_intro_generation_degrades_instead_of_raising(mocker: MockerFixture):+ # The greeting must never be the reason onboarding fails, so a broken+ # LLM call resolves to the template rather than propagating.+ mocker.patch(+ "backend.api.features.onboarding_dump.intro.get_openai_client",+ return_value=None,+ )++ greeting, suggested = await intro.generate_intro(TRANSCRIPT)++ assert greeting == intro.fallback_intro(TRANSCRIPT)[0]+ assert suggested == intro.fallback_prompts()diff --git a/autogpt_platform/backend/backend/api/features/onboarding_dump/storage.py b/autogpt_platform/backend/backend/api/features/onboarding_dump/storage.pynew file mode 100644index 0000000000..75ae89c495--- /dev/null+++ b/autogpt_platform/backend/backend/api/features/onboarding_dump/storage.py@@ -0,0 +1,140 @@+"""Part buffering and durable storage for brain-dump audio.++MediaRecorder emits timeslice blobs that, concatenated in order, form a+valid stream — so parts are buffered in Redis (keyed by user + recording,+indexed by part) while the user is still talking, then concatenated once+on finalize and written to cloud storage as a single object.++Redis rather than one blob per part: a 30-minute dump is ~600 parts, and+600 GCS objects per user would need their own lifecycle management. The+buffer is disposable — the browser holds every part in IndexedDB until+finalize reports ``completed``, so losing the buffer costs a re-upload,+never a recording.+"""++import base64+import logging+from typing import Any, Awaitable, cast++from backend.data import redis_client+from backend.util.cloud_storage import get_cloud_storage_handler++logger = logging.getLogger(__name__)++# Long enough to cover a paused-and-resumed session or a client that+# reconnects after a network drop, short enough that abandoned takes don't+# accumulate.+PART_BUFFER_TTL_SECONDS = 6 * 60 * 60++# The audio has to outlive the onboarding session by enough for the user+# to come back and download it. 48h is the ceiling ``store_file`` accepts.+AUDIO_EXPIRATION_HOURS = 48+++def _buffer_key(user_id: str, recording_id: str, suffix: str) -> str:+ return f"onboarding:braindump:{{{user_id}:{recording_id}}}:{suffix}"+++def _parts_key(user_id: str, recording_id: str) -> str:+ return _buffer_key(user_id, recording_id, "parts")+++def _sizes_key(user_id: str, recording_id: str) -> str:+ return _buffer_key(user_id, recording_id, "sizes")+++async def _read_parts(user_id: str, recording_id: str) -> dict[str, str]:+ redis = await redis_client.get_redis_async()+ # The shared client runs with ``decode_responses=True``, so values come+ # back as ``str`` — which is why parts are base64 on the way in.+ return await cast(+ Awaitable[dict[str, str]], redis.hgetall(_parts_key(user_id, recording_id))+ )+++async def append_part(+ user_id: str,+ recording_id: str,+ part_index: int,+ content: bytes,+) -> int:+ """Buffer one part and return the recording's cumulative byte count.++ Re-uploading the same ``part_index`` overwrites it, so the client's+ retry queue can replay a part it isn't sure landed.++ Parts are base64-encoded because the shared Redis client decodes+ responses as UTF-8 text — handing it raw opus bytes would round-trip+ them through a lossy decode and corrupt the recording.++ The running total comes from a companion hash of per-part *sizes*+ rather than from the parts themselves: summing ``hvals`` on the parts+ would pull every buffered byte back over the wire on every single+ upload — ~136 MB of live buffers at the 50 MB cap, and quadratic+ traffic across the ~600 parts of a 30-minute dump. Sizes are keyed by+ part index, so a replayed part overwrites its own entry instead of+ being counted twice, and the sum is read inside the same transaction+ that wrote the part, which is what makes it authoritative.+ """+ redis = await redis_client.get_redis_async()+ parts_key = _parts_key(user_id, recording_id)+ sizes_key = _sizes_key(user_id, recording_id)+ encoded = base64.b64encode(content).decode("ascii")+ async with redis.pipeline(transaction=True) as pipe:+ pipe.hset(parts_key, str(part_index), encoded)+ pipe.hset(sizes_key, str(part_index), str(len(content)))+ pipe.expire(parts_key, PART_BUFFER_TTL_SECONDS)+ pipe.expire(sizes_key, PART_BUFFER_TTL_SECONDS)+ pipe.hvals(sizes_key)+ results = await cast(Awaitable[list[Any]], pipe.execute())+ return _sum_sizes(cast(list[str], results[-1]))+++async def buffered_size(user_id: str, recording_id: str) -> int:+ redis = await redis_client.get_redis_async()+ sizes = await cast(+ Awaitable[list[str]], redis.hvals(_sizes_key(user_id, recording_id))+ )+ return _sum_sizes(sizes)+++def _sum_sizes(sizes: list[str]) -> int:+ return sum(int(size) for size in sizes)+++async def assemble_parts(user_id: str, recording_id: str) -> bytes:+ """Concatenate the buffered parts in ``part_index`` order."""+ entries = await _read_parts(user_id, recording_id)+ if not entries:+ return b""+ ordered = sorted(entries.items(), key=lambda kv: int(kv[0]))+ return b"".join(base64.b64decode(value) for _, value in ordered)+++async def discard_parts(user_id: str, recording_id: str) -> None:+ redis = await redis_client.get_redis_async()+ await redis.delete(+ _parts_key(user_id, recording_id), _sizes_key(user_id, recording_id)+ )+++async def store_audio(user_id: str, content: bytes, filename: str) -> str:+ handler = await get_cloud_storage_handler()+ return await handler.store_file(+ content,+ filename,+ expiration_hours=AUDIO_EXPIRATION_HOURS,+ user_id=user_id,+ )+++async def audio_download_url(user_id: str, audio_path: str) -> str:+ handler = await get_cloud_storage_handler()+ return await handler.generate_signed_url(+ audio_path, expiration_hours=1, user_id=user_id+ )+++async def retrieve_audio(user_id: str, audio_path: str) -> bytes:+ handler = await get_cloud_storage_handler()+ return await handler.retrieve_file(audio_path, user_id=user_id)diff --git a/autogpt_platform/backend/backend/api/features/onboarding_dump/storage_test.py b/autogpt_platform/backend/backend/api/features/onboarding_dump/storage_test.pynew file mode 100644index 0000000000..da9b435bf0--- /dev/null+++ b/autogpt_platform/backend/backend/api/features/onboarding_dump/storage_test.py@@ -0,0 +1,167 @@+"""Unit tests for the Redis part buffer.++Every other suite in this package replaces ``storage`` with ``AsyncMock``s,+so the ordering and accounting here — the two things standing between a+long recording and a silently corrupted or wrongly rejected upload — are+exercised against a fake hash rather than mocked away.+"""++from typing import Any++import pytest+from pytest_mock import MockerFixture+from redis.cluster import key_slot++from backend.api.features.onboarding_dump import storage++USER_ID = "user-1"+RECORDING_ID = "rec-1"+++class FakeRedis:+ """The handful of hash commands the buffer uses."""++ def __init__(self) -> None:+ self.hashes: dict[str, dict[str, str]] = {}+ self.expiries: dict[str, int] = {}++ async def hgetall(self, key: str) -> dict[str, str]:+ return dict(self.hashes.get(key, {}))++ async def hvals(self, key: str) -> list[str]:+ return list(self.hashes.get(key, {}).values())++ async def hset(self, key: str, field: str, value: str) -> int:+ entries = self.hashes.setdefault(key, {})+ added = field not in entries+ entries[field] = value+ return int(added)++ async def expire(self, key: str, seconds: int) -> None:+ self.expiries[key] = seconds++ async def delete(self, *keys: str) -> None:+ for key in keys:+ self.hashes.pop(key, None)+ self.expiries.pop(key, None)++ def pipeline(self, transaction: bool = True) -> "FakePipeline":+ return FakePipeline(self)+++class FakePipeline:+ """Queues commands like redis-py: sync on the way in, awaited on exec."""++ def __init__(self, redis: FakeRedis) -> None:+ self.redis = redis+ self.queued: list[tuple[str, tuple[Any, ...]]] = []++ async def __aenter__(self) -> "FakePipeline":+ return self++ async def __aexit__(self, *exc_info: object) -> None:+ return None++ def hset(self, *args: Any) -> None:+ self.queued.append(("hset", args))++ def expire(self, *args: Any) -> None:+ self.queued.append(("expire", args))++ def hvals(self, *args: Any) -> None:+ self.queued.append(("hvals", args))++ async def execute(self) -> list[Any]:+ return [await getattr(self.redis, name)(*args) for name, args in self.queued]+++def redis(mocker: MockerFixture) -> FakeRedis:+ fake = FakeRedis()++ async def get_redis_async() -> FakeRedis:+ return fake++ mocker.patch.object(storage.redis_client, "get_redis_async", new=get_redis_async)+ return fake+++async def _append(redis: FakeRedis, part_index: int, content: bytes) -> int:+ return await storage.append_part(USER_ID, RECORDING_ID, part_index, content)+++def test_buffer_keys_share_a_redis_cluster_slot():+ for user_id, recording_id in [+ (USER_ID, RECORDING_ID),+ ("c0933014-3c5a-499a-91db-10e020d527b1", "recording_42"),+ ]:+ parts_key = storage._parts_key(user_id, recording_id)+ sizes_key = storage._sizes_key(user_id, recording_id)++ assert key_slot(parts_key.encode()) == key_slot(sizes_key.encode())+++async def test_parts_are_assembled_in_numeric_not_lexicographic_order(+ redis: FakeRedis,+):+ """Redis hash fields are strings, so ``"10" < "2"`` when sorted raw.++ A recording longer than ten timeslices would then concatenate as+ 1, 10, 11, 2, … — a stream that is still valid enough to store and+ transcribe, and completely scrambled.+ """+ for index in range(12):+ await _append(redis, index, f"<{index}>".encode())++ assembled = await storage.assemble_parts(USER_ID, RECORDING_ID)++ assert assembled == b"".join(f"<{index}>".encode() for index in range(12))+++async def test_an_empty_buffer_assembles_to_nothing(redis: FakeRedis):+ assert await storage.assemble_parts(USER_ID, RECORDING_ID) == b""+ assert await storage.buffered_size(USER_ID, RECORDING_ID) == 0++[email protected]("length", [3, 4, 5, 300, 301, 302])+async def test_the_running_total_is_the_exact_decoded_byte_count(+ redis: FakeRedis, length: int+):+ """Base64 padding varies with ``len(payload) % 3``; the cap does not."""+ content = b"x" * length++ total = await _append(redis, 0, content)++ assert total == length+ assert await storage.buffered_size(USER_ID, RECORDING_ID) == length+ assert await storage.assemble_parts(USER_ID, RECORDING_ID) == content+++async def test_replaying_a_part_overwrites_it_instead_of_double_counting(+ redis: FakeRedis,+):+ """The client's retry queue replays parts it isn't sure landed."""+ await _append(redis, 0, b"a" * 10)+ await _append(redis, 1, b"b" * 20)++ total = await _append(redis, 1, b"b" * 20)++ assert total == 30+ assert await storage.buffered_size(USER_ID, RECORDING_ID) == 30+++async def test_the_buffer_and_its_size_index_expire_and_are_dropped_together(+ redis: FakeRedis,+):+ await _append(redis, 0, b"chunk")+ assert set(redis.expiries.values()) == {storage.PART_BUFFER_TTL_SECONDS}++ await storage.discard_parts(USER_ID, RECORDING_ID)++ assert redis.hashes == {}+ assert await storage.buffered_size(USER_ID, RECORDING_ID) == 0diff --git a/autogpt_platform/backend/backend/api/features/onboarding_dump/transcription.py b/autogpt_platform/backend/backend/api/features/onboarding_dump/transcription.pynew file mode 100644index 0000000000..be40703925--- /dev/null+++ b/autogpt_platform/backend/backend/api/features/onboarding_dump/transcription.py@@ -0,0 +1,361 @@+"""Speech-to-text for the onboarding brain dump.++``gpt-4o-transcribe`` is the primary model: on messy, rambling natural+speech it is markedly more accurate and hallucinates far less than+``whisper-1``, which is the failure mode that matters most here (a+hallucinated sentence becomes a *fact* about the user). ``whisper-1`` is+the automatic fallback so a model outage degrades rather than fails.++Both models take a 25 MB request and roughly 25 minutes of audio, so a+long dump is split with ffmpeg into overlapping segments and the+transcripts stitched back together. ffmpeg is present in the backend+image (``backend/Dockerfile``); if it ever isn't, the split path raises+and the caller records a ``failed`` status with the audio still stored.+"""++import asyncio+import logging+import math+import os+import shutil+import string+import tempfile++from openai import AsyncOpenAI+from pydantic import BaseModel++from backend.util.settings import Settings++logger = logging.getLogger(__name__)++settings = Settings()++PRIMARY_MODEL = os.environ.get("BRAIN_DUMP_TRANSCRIPTION_MODEL", "gpt-4o-transcribe")+FALLBACK_MODEL = os.environ.get("BRAIN_DUMP_TRANSCRIPTION_FALLBACK_MODEL", "whisper-1")++# Both caps are the provider's, minus headroom for the multipart envelope.+SINGLE_REQUEST_MAX_BYTES = 20 * 1024 * 1024+SINGLE_REQUEST_MAX_SECONDS = 20 * 60++SEGMENT_SECONDS = 10 * 60+# ``split_audio`` re-encodes to opus in an ogg container, so a segment must+# not inherit the source extension: the STT client infers the format from+# the filename, and an ogg body announced as ``.webm`` is rejected.+SEGMENT_SUFFIX = ".ogg"+# Cutting mid-word loses the word on both sides of the seam; a few seconds+# of overlap means the word survives in at least one segment and the+# stitcher drops the duplicate.+SEGMENT_OVERLAP_SECONDS = 5+# ``-ss`` past the end of the stream is not an error: ffmpeg exits 0 and+# writes a container with nothing in it. Any real segment is orders of+# magnitude bigger than an empty ogg/opus header, so this is how the+# segment loop finds the true end of an audio whose claimed duration is+# longer than what is actually there.+MIN_SEGMENT_BYTES = 1024++TRANSCRIBE_TIMEOUT_SECONDS = 180+MAX_ATTEMPTS = 3+++class TranscriptionUnavailableError(RuntimeError):+ """No STT provider is configured."""+++class TranscriptionFailedError(RuntimeError):+ """Every model and retry was exhausted."""+++class TranscriptionResult(BaseModel):+ """One transcription, plus which model actually produced it.++ ``model`` matters because the fallback is silent: a run that thinks it+ measured ``gpt-4o-transcribe`` may be reading ``whisper-1``'s output.+ For a stitched transcript it lists every model that contributed.+ """++ text: str+ language: str | None = None+ model: str+++def get_stt_client() -> AsyncOpenAI | None:+ """Return a client for the audio-transcriptions endpoint.++ Deliberately not ``backend.util.clients.get_openai_client()``: that+ helper falls back to OpenRouter, which does not implement+ ``/audio/transcriptions``, so a deployment with only an OpenRouter key+ would get a confusing 404 from the provider instead of a clean+ "not configured" error here.++ Built per call rather than ``@cached``: ``AsyncOpenAI`` binds its+ connection pool to the first event loop that uses it, so a process-wide+ cache poisons itself across loops. ``backend/util/architecture_test.py``+ enforces this — the cached helpers in ``util/clients.py`` are a+ grandfathered allowlist that is being burned down, not the pattern to+ copy. Finalize runs once per user, so the pool churn is negligible.+ """+ api_key = (+ settings.secrets.openai_internal_api_key or settings.secrets.openai_api_key+ )+ if not api_key:+ return None+ return AsyncOpenAI(api_key=api_key)+++async def transcribe(+ audio: bytes,+ filename: str,+ duration_secs: float | None = None,+) -> TranscriptionResult:+ """Transcribe ``audio``.++ ``language`` is never passed to the provider — a non-English dump must+ transcribe in the language it was spoken in, never error or silently+ translate. It is also not reported for a stitched transcript, where+ the per-segment answers can disagree.+ """+ client = get_stt_client()+ if client is None:+ raise TranscriptionUnavailableError(+ "Brain-dump transcription needs a direct OpenAI key. Set "+ "OPENAI_INTERNAL_API_KEY or OPENAI_API_KEY."+ )++ needs_split = len(audio) > SINGLE_REQUEST_MAX_BYTES or (+ duration_secs is not None and duration_secs > SINGLE_REQUEST_MAX_SECONDS+ )+ if not needs_split:+ return await _transcribe_one(client, audio, filename)++ segments = await split_audio(audio, filename, duration_secs)+ logger.info("Brain dump split into %s segments for transcription", len(segments))+ results = [+ await _transcribe_one(client, segment, f"segment-{index}{SEGMENT_SUFFIX}")+ for index, segment in enumerate(segments)+ ]+ return TranscriptionResult(+ text=stitch_transcripts([result.text for result in results]),+ model=",".join(dict.fromkeys(result.model for result in results)),+ )+++async def _transcribe_one(+ client: AsyncOpenAI, audio: bytes, filename: str+) -> TranscriptionResult:+ last_error: Exception | None = None+ for model in (PRIMARY_MODEL, FALLBACK_MODEL):+ for attempt in range(MAX_ATTEMPTS):+ try:+ response = await asyncio.wait_for(+ client.audio.transcriptions.create(+ model=model,+ file=(filename, audio),+ ),+ timeout=TRANSCRIBE_TIMEOUT_SECONDS,+ )+ language = getattr(response, "language", None)+ return TranscriptionResult(+ text=response.text,+ language=language if isinstance(language, str) else None,+ model=model,+ )+ except Exception as e: # retried across models below+ last_error = e+ logger.warning(+ "Brain dump transcription failed (model=%s attempt=%s): %s",+ model,+ attempt + 1,+ e,+ )+ if attempt < MAX_ATTEMPTS - 1:+ await asyncio.sleep(2**attempt)+ raise TranscriptionFailedError(str(last_error))+++async def split_audio(+ audio: bytes, filename: str, duration_hint: float | None = None+) -> list[bytes]:+ """Cut ``audio`` into overlapping segments with ffmpeg.++ Byte-slicing an opus stream would produce unplayable fragments, so+ this is a real container-aware re-cut. Segments are re-encoded to+ 16 kHz mono opus, which is what the STT models downsample to anyway+ and keeps every segment far inside the request cap.++ ``duration_hint`` is only ever an upper bound: it usually comes from+ the browser's wall clock, so the loop stops at the first empty segment+ rather than trusting it to the second.+ """+ ffmpeg = shutil.which("ffmpeg")+ if ffmpeg is None:+ raise TranscriptionFailedError(+ "ffmpeg is required to transcribe recordings over "+ f"{SINGLE_REQUEST_MAX_SECONDS // 60} minutes but is not installed"+ )++ suffix = os.path.splitext(filename)[1] or ".webm"+ with tempfile.TemporaryDirectory() as workdir:+ source = os.path.join(workdir, f"source{suffix}")+ # Off the event loop: these are multi-megabyte reads and writes,+ # and this runs as a background task alongside live requests.+ await asyncio.to_thread(_write_file, source, audio)++ # A browser MediaRecorder stream is not muxed, so its header+ # usually carries no duration at all — for precisely the long+ # takes that land here. The browser timed the take with a wall+ # clock, so its figure is the fallback rather than a failure.+ duration = await _probe_duration(ffmpeg, source) or duration_hint+ if duration is None or duration <= 0:+ raise TranscriptionFailedError(+ f"Could not determine the duration of {os.path.basename(filename)}"+ )+ segments: list[bytes] = []+ start = 0.0+ index = 0+ while start < duration:+ target = os.path.join(workdir, f"segment-{index}{SEGMENT_SUFFIX}")+ await _run_ffmpeg(+ ffmpeg,+ "-ss",+ str(start),+ "-t",+ str(SEGMENT_SECONDS + SEGMENT_OVERLAP_SECONDS),+ "-i",+ source,+ "-ac",+ "1",+ "-ar",+ "16000",+ "-c:a",+ "libopus",+ target,+ )+ segment = await asyncio.to_thread(_read_file, target)+ if len(segment) < MIN_SEGMENT_BYTES:+ # Past the real end of the audio. Everything from here on+ # would be another empty container, another ffmpeg run and+ # another billed transcription of silence.+ break+ segments.append(segment)+ start += SEGMENT_SECONDS+ index += 1+ return segments+++async def _probe_duration(ffmpeg: str, path: str) -> float | None:+ """Read the container duration via ffmpeg's stderr banner.++ ffprobe is not guaranteed to be installed alongside ffmpeg, so this+ parses the ``Duration: HH:MM:SS.ms`` line instead of shelling out to a+ second binary.++ Best-effort by design: ``None`` means "ffmpeg would not tell us",+ which is the normal answer for an unmuxed stream, and the caller+ falls back to the duration the browser measured.+ """+ process = await asyncio.create_subprocess_exec(+ ffmpeg,+ "-i",+ path,+ stdout=asyncio.subprocess.DEVNULL,+ stderr=asyncio.subprocess.PIPE,+ )+ _, stderr = await process.communicate()+ for line in stderr.decode(errors="replace").splitlines():+ marker = "Duration:"+ if marker not in line:+ continue+ raw = line.split(marker, 1)[1].split(",", 1)[0].strip()+ # ffmpeg writes the sign on the hours field, and ``int("-00")`` is+ # ``0`` — so a negative duration would otherwise parse as a+ # positive one of the same magnitude.+ if raw.startswith("-"):+ return None+ try:+ hours, minutes, seconds = raw.split(":")+ probed = int(hours) * 3600 + int(minutes) * 60 + float(seconds)+ except ValueError:+ # ``Duration: N/A``, which is what an unmuxed MediaRecorder+ # stream reports.+ return None+ # A damaged header can also yield zero or a non-finite value,+ # which would make the segment loop produce nothing or spin.+ if not math.isfinite(probed) or probed <= 0:+ return None+ return probed+ return None+++def _write_file(path: str, data: bytes) -> None:+ with open(path, "wb") as handle:+ handle.write(data)+++def _read_file(path: str) -> bytes:+ with open(path, "rb") as handle:+ return handle.read()+++async def _run_ffmpeg(ffmpeg: str, *args: str) -> None:+ process = await asyncio.create_subprocess_exec(+ ffmpeg,+ "-hide_banner",+ "-loglevel",+ "error",+ "-y",+ *args,+ stdout=asyncio.subprocess.DEVNULL,+ stderr=asyncio.subprocess.PIPE,+ )+ _, stderr = await process.communicate()+ if process.returncode != 0:+ raise TranscriptionFailedError(+ f"ffmpeg failed: {stderr.decode(errors='replace')[-500:]}"+ )+++def stitch_transcripts(transcripts: list[str]) -> str:+ """Join segment transcripts, dropping the duplicated overlap words.++ The overlap is a few seconds of speech, so the seam is found by+ looking for the longest word-sequence that ends the previous segment+ and starts the next one.+ """+ stitched: list[str] = []+ for transcript in transcripts:+ words = transcript.split()+ if not words:+ continue+ if not stitched:+ stitched = words+ continue+ overlap = _overlap_length(stitched, words)+ stitched.extend(words[overlap:])+ return " ".join(stitched)+++# A 5s overlap is at most ~25 spoken words; searching beyond that risks+# collapsing a genuinely repeated phrase.+MAX_OVERLAP_WORDS = 40+++def _overlap_length(left: list[str], right: list[str]) -> int:+ limit = min(MAX_OVERLAP_WORDS, len(left), len(right))+ normalised_left = [_seam_word(w) for w in left[-limit:]] if limit else []+ normalised_right = [_seam_word(w) for w in right[:limit]] if limit else []+ for size in range(limit, 0, -1):+ if normalised_left[-size:] == normalised_right[:size]:+ return size+ return 0+++def _seam_word(word: str) -> str:+ """Fold a word to what the seam comparison should care about.++ STT models punctuate each segment as if it were a whole utterance, so+ the same spoken word comes back as ``schedule.`` at the end of one+ segment and ``schedule`` at the start of the next. Comparing them raw+ finds no overlap at all and the whole 5s overlap is duplicated into+ the stitched transcript at every seam.+ """+ return word.lower().strip(string.punctuation)diff --git a/autogpt_platform/backend/backend/api/features/onboarding_dump/transcription_test.py b/autogpt_platform/backend/backend/api/features/onboarding_dump/transcription_test.pynew file mode 100644index 0000000000..72c93a286e--- /dev/null+++ b/autogpt_platform/backend/backend/api/features/onboarding_dump/transcription_test.py@@ -0,0 +1,290 @@+"""Unit tests for the speech-to-text helpers. No network, no ffmpeg."""++from unittest.mock import AsyncMock, MagicMock++import pytest+from pytest_mock import MockerFixture++from backend.api.features.onboarding_dump import transcription+++def test_stitch_drops_the_duplicated_overlap_at_the_seam():+ stitched = transcription.stitch_transcripts(+ [+ "so the first thing I want to automate is the weekly order email",+ "the weekly order email goes out every monday morning",+ ]+ )++ assert stitched == (+ "so the first thing I want to automate is the weekly order email "+ "goes out every monday morning"+ )+++def test_stitch_matches_the_seam_case_insensitively():+ stitched = transcription.stitch_transcripts(+ ["we ship on friday", "Friday is when the boxes go out"]+ )++ assert stitched == "we ship on friday is when the boxes go out"+++def test_stitch_matches_the_seam_across_the_terminal_punctuation():+ """STT models end every segment as if it were a whole utterance.++ The same spoken word therefore comes back as ``schedule.`` on the left+ of the seam and ``schedule`` on the right. Comparing raw words finds+ no overlap at all, and the deliberate 5s overlap is duplicated into+ the transcript at every 10-minute seam.+ """+ stitched = transcription.stitch_transcripts(+ [+ "and then I have to go and rebuild the whole schedule.",+ "Rebuild the whole schedule, and that eats my monday.",+ ]+ )++ assert stitched == (+ "and then I have to go and rebuild the whole schedule. "+ "and that eats my monday."+ )+++def test_stitch_joins_segments_that_do_not_overlap():+ stitched = transcription.stitch_transcripts(+ ["alpha beta gamma", "delta epsilon zeta"]+ )++ assert stitched == "alpha beta gamma delta epsilon zeta"+++def test_stitch_keeps_a_phrase_that_legitimately_repeats_outside_the_seam():+ stitched = transcription.stitch_transcripts(+ [+ "we ship on friday we ship on friday every single week",+ "every single week without fail",+ ]+ )++ assert stitched == (+ "we ship on friday we ship on friday every single week without fail"+ )+++def test_stitch_does_not_collapse_a_repeat_longer_than_the_overlap_window():+ phrase = [f"word{index}" for index in range(transcription.MAX_OVERLAP_WORDS + 5)]+ stitched = transcription.stitch_transcripts([" ".join(phrase), " ".join(phrase)])++ # The seam search never looks further back than MAX_OVERLAP_WORDS, so a+ # repetition longer than the window is left intact rather than being+ # mistaken for a duplicated overlap and silently deleted.+ assert stitched.split() == phrase + phrase+++def test_stitch_ignores_empty_segments():+ assert transcription.stitch_transcripts(["", "hello there", " "]) == "hello there"+++async def test_transcribe_falls_back_to_whisper_after_the_primary_exhausts_retries(+ mocker: MockerFixture,+):+ mocker.patch.object(transcription.asyncio, "sleep", new=AsyncMock())+ attempted_models: list[str] = []++ async def create(*, model: str, file: tuple[str, bytes]):+ attempted_models.append(model)+ if model == transcription.PRIMARY_MODEL:+ raise RuntimeError("primary model is down")+ return MagicMock(text="hello there", language="en")++ stt = MagicMock()+ stt.audio.transcriptions.create = AsyncMock(side_effect=create)+ mocker.patch.object(transcription, "get_stt_client", return_value=stt)++ result = await transcription.transcribe(b"opus", "dump.webm")++ assert (result.text, result.language) == ("hello there", "en")+ # The fallback is silent, so the result has to say which model won.+ assert result.model == transcription.FALLBACK_MODEL+ assert attempted_models == (+ [transcription.PRIMARY_MODEL] * transcription.MAX_ATTEMPTS+ + [transcription.FALLBACK_MODEL]+ )+++async def test_transcribe_raises_once_every_model_and_retry_is_exhausted(+ mocker: MockerFixture,+):+ mocker.patch.object(transcription.asyncio, "sleep", new=AsyncMock())+ create = AsyncMock(side_effect=RuntimeError("everything is down"))+ stt = MagicMock()+ stt.audio.transcriptions.create = create+ mocker.patch.object(transcription, "get_stt_client", return_value=stt)++ with pytest.raises(transcription.TranscriptionFailedError):+ await transcription.transcribe(b"opus", "dump.webm")++ assert create.await_count == 2 * transcription.MAX_ATTEMPTS+++async def test_transcribe_without_a_configured_provider_is_unavailable(+ mocker: MockerFixture,+):+ mocker.patch.object(transcription, "get_stt_client", return_value=None)++ with pytest.raises(transcription.TranscriptionUnavailableError):+ await transcription.transcribe(b"opus", "dump.webm")+++async def test_a_long_recording_is_split_and_stitched(mocker: MockerFixture):+ create = AsyncMock(+ side_effect=[+ MagicMock(text="first half of the dump", language="en"),+ MagicMock(text="of the dump and then the rest", language="en"),+ ]+ )+ stt = MagicMock()+ stt.audio.transcriptions.create = create+ mocker.patch.object(transcription, "get_stt_client", return_value=stt)+ split = mocker.patch.object(+ transcription, "split_audio", new=AsyncMock(return_value=[b"one", b"two"])+ )++ result = await transcription.transcribe(+ b"opus", "dump.webm", duration_secs=transcription.SINGLE_REQUEST_MAX_SECONDS + 1+ )++ split.assert_awaited_once()+ assert result.text == "first half of the dump and then the rest"+ # Language is unreliable across segments, so it is not reported.+ assert result.language is None+ assert result.model == transcription.PRIMARY_MODEL+ # Segments come back as ogg regardless of what the browser recorded, and+ # the client infers the format from the filename — announcing an ogg body+ # as ``.webm`` makes the provider reject it.+ sent = [call.kwargs["file"][0] for call in create.await_args_list]+ assert sent == ["segment-0.ogg", "segment-1.ogg"]+++def _ffmpeg_banner(duration_line: str) -> bytes:+ return (+ b"ffmpeg version 6.0\n"+ b" Input #0, matroska,webm, from 'source.webm':\n"+ + duration_line.encode()+ + b"\n Stream #0:0: Audio: opus\n"+ )+++ "duration_line",+ [+ # What an unmuxed MediaRecorder stream actually reports — the+ # normal case for the long takes that reach split_audio.+ " Duration: N/A, start: 0.000000, bitrate: N/A",+ # A damaged header can produce values that would make the segment+ # loop spin forever or emit nothing.+ " Duration: -00:00:01.00, start: 0.000000, bitrate: 32 kb/s",+ " Duration: 00:00:00.00, start: 0.000000, bitrate: 32 kb/s",+ ],+)+async def test_an_unusable_ffmpeg_duration_is_reported_as_unknown(+ mocker: MockerFixture, duration_line: str+):+ process = MagicMock()+ process.communicate = AsyncMock(return_value=(b"", _ffmpeg_banner(duration_line)))+ mocker.patch("asyncio.create_subprocess_exec", new=AsyncMock(return_value=process))++ assert await transcription._probe_duration("ffmpeg", "source.webm") is None+++async def test_a_readable_ffmpeg_duration_is_parsed(mocker: MockerFixture):+ process = MagicMock()+ process.communicate = AsyncMock(+ return_value=(+ b"",+ _ffmpeg_banner(+ " Duration: 00:21:30.50, start: 0.000000, bitrate: 32 kb/s"+ ),+ )+ )+ mocker.patch("asyncio.create_subprocess_exec", new=AsyncMock(return_value=process))++ assert await transcription._probe_duration("ffmpeg", "source.webm") == 1290.5+++async def test_split_falls_back_to_the_browser_duration_when_ffmpeg_cannot_tell(+ mocker: MockerFixture,+):+ """The take must still be split when the container has no duration.++ ffmpeg reporting ``N/A`` is the norm for a browser MediaRecorder+ stream, so treating it as fatal would fail exactly the long+ recordings this code path exists to handle.+ """+ mocker.patch.object(transcription.shutil, "which", return_value="ffmpeg")+ mocker.patch.object(+ transcription, "_probe_duration", new=AsyncMock(return_value=None)+ )+ run = mocker.patch.object(transcription, "_run_ffmpeg", new=AsyncMock())+ mocker.patch.object(transcription, "_write_file", new=MagicMock())+ mocker.patch.object(+ transcription,+ "_read_file",+ new=MagicMock(return_value=b"s" * transcription.MIN_SEGMENT_BYTES),+ )++ hint = transcription.SEGMENT_SECONDS * 2 + 5+ segments = await transcription.split_audio(b"opus", "dump.webm", hint)++ assert len(segments) == 3+ assert run.await_count == 3+++async def test_split_stops_at_the_first_empty_segment(mocker: MockerFixture):+ """The duration hint is the client's number, and it can be a lie.++ ``-ss`` past the end of the stream is not an error — ffmpeg exits 0+ and writes an empty container — so a hostile ``duration_secs`` would+ otherwise buy one ffmpeg run and one billed transcription per 10+ minutes of it, however little audio was actually uploaded.+ """+ mocker.patch.object(transcription.shutil, "which", return_value="ffmpeg")+ mocker.patch.object(+ transcription, "_probe_duration", new=AsyncMock(return_value=None)+ )+ run = mocker.patch.object(transcription, "_run_ffmpeg", new=AsyncMock())+ mocker.patch.object(transcription, "_write_file", new=MagicMock())+ real = b"a" * transcription.MIN_SEGMENT_BYTES+ mocker.patch.object(+ transcription, "_read_file", new=MagicMock(side_effect=[real, b"OggS-header"])+ )++ segments = await transcription.split_audio(+ b"opus", "dump.webm", transcription.SEGMENT_SECONDS * 10_000+ )++ assert segments == [real]+ assert run.await_count == 2+++async def test_split_fails_cleanly_when_no_duration_is_available(+ mocker: MockerFixture,+):+ mocker.patch.object(transcription.shutil, "which", return_value="ffmpeg")+ mocker.patch.object(+ transcription, "_probe_duration", new=AsyncMock(return_value=None)+ )+ mocker.patch.object(transcription, "_write_file", new=MagicMock())++ with pytest.raises(transcription.TranscriptionFailedError):+ await transcription.split_audio(b"opus", "dump.webm", None)diff --git a/autogpt_platform/backend/backend/api/rest_api.py b/autogpt_platform/backend/backend/api/rest_api.pyindex edd9bbb007..97e1f4f4f7 100644--- a/autogpt_platform/backend/backend/api/rest_api.py+++ b/autogpt_platform/backend/backend/api/rest_api.py@@ -37,6 +37,7 @@ import backend.api.features.library.modelimport backend.api.features.library.routesimport backend.api.features.mcp.routes as mcp_routesimport backend.api.features.oauth+import backend.api.features.onboarding_dump.routes as onboarding_dump_routesimport backend.api.features.orgs.invitation_routesimport backend.api.features.orgs.routes as org_routesimport backend.api.features.orgs.team_routes@@ -429,6 +430,11 @@ app.include_router(backend.api.features.otto.routes.router, tags=["v2", "otto"], prefix="/api/otto")+app.include_router(+ onboarding_dump_routes.router,+ tags=["v1", "onboarding"],+ prefix="/api",+)app.include_router(backend.api.features.postmark.postmark.router,tags=["v1", "email"],diff --git a/autogpt_platform/backend/backend/data/onboarding.py b/autogpt_platform/backend/backend/data/onboarding.pyindex fef044d81f..d770b1e818 100644--- a/autogpt_platform/backend/backend/data/onboarding.py+++ b/autogpt_platform/backend/backend/data/onboarding.py@@ -42,6 +42,7 @@ FrontendOnboardingStep = Literal[OnboardingStep.CONGRATS,OnboardingStep.VISIT_COPILOT,OnboardingStep.BUILDER_OPEN,+ OnboardingStep.CAPABILITY_CARDS,]@@ -427,6 +428,44 @@ def format_onboarding_for_extraction(return "\n\n".join(lines)+def format_brain_dump_for_extraction(+ user_name: str,+ user_role: str,+ transcript: str,+) -> str:+ """Format a spoken brain dump for the same LLM extraction pass.++ The transcript is unpunctuated, rambling, first-person speech rather+ than form answers, so it gets its own framing: the extractor is told+ what it is looking at and which fields the "what would you hand off"+ part of a dump maps onto, otherwise it tends to file everything under+ ``additional_notes``.++ The transcript is passed **whole** — extraction runs against the+ complete text even when a shortened version is what gets injected into+ the copilot's first prompt.+ """+ name = " ".join(user_name.strip().split())+ role = " ".join(user_role.strip().split())++ return "\n\n".join(+ [+ f"Q: What is your name?\nA: {name}",+ f"Q: What best describes your role?\nA: {role}",+ (+ "The following is a transcript of the user speaking freely for a "+ "few minutes about their work, in answer to: what keeps stealing "+ "your week? It is unedited speech — expect filler words, "+ "self-corrections and no punctuation. Extract only what the user "+ "actually said; never invent details. Work they describe wanting "+ "to hand off belongs in manual_tasks and automation_goals; tools "+ "they name belong in current_software.\n\n"+ f"Transcript:\n{transcript.strip()}"+ ),+ ]+ )++@cached(maxsize=1, ttl_seconds=300) # Cache for 5 minutes since this rarely changesasync def onboarding_enabled() -> bool:"""diff --git a/autogpt_platform/backend/backend/util/feature_flag.py b/autogpt_platform/backend/backend/util/feature_flag.pyindex 80a192be33..7bcb52d18a 100644--- a/autogpt_platform/backend/backend/util/feature_flag.py+++ b/autogpt_platform/backend/backend/util/feature_flag.py@@ -64,6 +64,12 @@ class Flag(str, Enum):COPILOT_TIER_STRIPE_PRICES = "copilot-tier-stripe-prices"GRAPHITI_MEMORY = "graphiti-memory"+ # Gates the onboarding voice "brain dump" end-to-end. The upload /+ # finalize / status / download endpoints 404 when off so a stale+ # frontend can't start writing recordings, and the wizard keeps+ # rendering the pillbox step. Fail-closed (default False).+ ONBOARDING_BRAIN_DUMP = "onboarding-brain-dump"+# Gates the per-user weekly community rebuild registered by# ``add_community_rebuild_schedule``. Off by default; opt-in canary# so the Leiden + LLM-summarization cost doesn't ramp beforediff --git a/autogpt_platform/backend/migrations/20260801100000_add_onboarding_brain_dump/migration.sql b/autogpt_platform/backend/migrations/20260801100000_add_onboarding_brain_dump/migration.sqlnew file mode 100644index 0000000000..59d9582082--- /dev/null+++ b/autogpt_platform/backend/migrations/20260801100000_add_onboarding_brain_dump/migration.sql@@ -0,0 +1,50 @@+-- Onboarding voice "brain dump": the raw recording plus its full+-- transcript. Postgres enums (rather than TEXT) so an invalid status or+-- input mode is rejected at the DB layer; new states need a cheap+-- ``ALTER TYPE ... ADD VALUE``.+CREATE TYPE "BrainDumpStatus" AS ENUM (+ 'recording_uploaded',+ 'transcribing',+ 'transcribed',+ 'extracting',+ 'completed',+ 'failed'+);++CREATE TYPE "BrainDumpInputMode" AS ENUM ('voice', 'typed', 'skipped');++-- One row per user: re-recording overwrites the take in place, so+-- ``userId`` is unique and ``recordingId`` identifies the current take+-- (it is what makes ``finalize`` idempotent across retries).+--+-- ``transcript`` holds the COMPLETE transcript even when a shortened+-- version is what gets injected into the copilot's first prompt —+-- truncation is never silent. ``audioPath`` is kept on failure so the+-- download endpoint keeps working and the pipeline can be re-run.+CREATE TABLE "OnboardingBrainDump" (+ "id" TEXT NOT NULL,+ "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,+ "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,+ "userId" TEXT NOT NULL,+ "recordingId" TEXT NOT NULL,+ "status" "BrainDumpStatus" NOT NULL,+ "inputMode" "BrainDumpInputMode" NOT NULL,+ "audioPath" TEXT,+ "mimeType" TEXT,+ "sizeBytes" INTEGER,+ "durationSecs" DOUBLE PRECISION,+ "transcript" TEXT,+ "transcriptLang" TEXT,+ "errorCode" TEXT,++ CONSTRAINT "OnboardingBrainDump_pkey" PRIMARY KEY ("id")+);++CREATE UNIQUE INDEX "OnboardingBrainDump_userId_key" ON "OnboardingBrainDump"("userId");++CREATE INDEX "OnboardingBrainDump_userId_idx" ON "OnboardingBrainDump"("userId");++ALTER TABLE "OnboardingBrainDump"+ ADD CONSTRAINT "OnboardingBrainDump_userId_fkey"+ FOREIGN KEY ("userId") REFERENCES "User"("id")+ ON DELETE CASCADE ON UPDATE CASCADE;diff --git a/autogpt_platform/backend/migrations/20260801130000_add_brain_dump_greeting/migration.sql b/autogpt_platform/backend/migrations/20260801130000_add_brain_dump_greeting/migration.sqlnew file mode 100644index 0000000000..70aadebe13--- /dev/null+++ b/autogpt_platform/backend/migrations/20260801130000_add_brain_dump_greeting/migration.sql@@ -0,0 +1,11 @@+-- The copilot home's greeting reads these. Generated when the dump is+-- processed (during the onboarding loading screen) rather than on page+-- load, so landing on /copilot never waits on an LLM.+--+-- "greetingSeen" flips true the first time the user sends a message+-- (their first session). The greeting content is kept forever — only the+-- flag decides whether it is shown again.+ALTER TABLE "OnboardingBrainDump"+ ADD COLUMN "greeting" TEXT,+ ADD COLUMN "suggestedPrompts" JSONB NOT NULL DEFAULT '[]'::jsonb,+ ADD COLUMN "greetingSeen" BOOLEAN NOT NULL DEFAULT false;diff --git a/autogpt_platform/backend/migrations/20260802090000_add_brain_dump_recommended_providers/migration.sql b/autogpt_platform/backend/migrations/20260802090000_add_brain_dump_recommended_providers/migration.sqlnew file mode 100644index 0000000000..d0a448e3d4--- /dev/null+++ b/autogpt_platform/backend/migrations/20260802090000_add_brain_dump_recommended_providers/migration.sql@@ -0,0 +1,7 @@+-- Provider recommendations for the welcome dialog's "Connect your tools"+-- panel. Written by a background job that runs beside (not inside) the+-- greeting pipeline, so a slow or failed recommendation never delays the+-- greeting. NULL means "not generated yet" — an empty list is a real+-- result ("nothing worth recommending").+ALTER TABLE "OnboardingBrainDump"+ ADD COLUMN "recommendedProviders" JSONB;diff --git a/autogpt_platform/backend/migrations/20260802120000_add_capability_cards_step/migration.sql b/autogpt_platform/backend/migrations/20260802120000_add_capability_cards_step/migration.sqlnew file mode 100644index 0000000000..ac3cb4c99f--- /dev/null+++ b/autogpt_platform/backend/migrations/20260802120000_add_capability_cards_step/migration.sql@@ -0,0 +1,3 @@+-- Copilot home first-run: records that the capability-cards modal was+-- completed or skipped, so it shows only once per user.+ALTER TYPE "OnboardingStep" ADD VALUE 'CAPABILITY_CARDS';diff --git a/autogpt_platform/backend/pyproject.toml b/autogpt_platform/backend/pyproject.tomlindex 3e6f0e41f3..0831d097c4 100644--- a/autogpt_platform/backend/pyproject.toml+++ b/autogpt_platform/backend/pyproject.toml@@ -147,6 +147,7 @@ test = "scripts.run_tests:test"load-store-agents = "test.load_store_agents:run"export-api-schema = "backend.cli.generate_openapi_json:main"gen-prisma-stub = "scripts.gen_prisma_types_stub:main"+brain-dump-eval = "backend.api.features.onboarding_dump.brain_dump_eval:main"oauth-tool = "backend.cli.oauth_tool:cli"[tool.isort]diff --git a/autogpt_platform/backend/schema.prisma b/autogpt_platform/backend/schema.prismaindex 2ff3b02d94..16df06720b 100644--- a/autogpt_platform/backend/schema.prisma+++ b/autogpt_platform/backend/schema.prisma@@ -144,6 +144,7 @@ model User {Profile Profile[]UserOnboarding UserOnboarding?CoPilotUnderstanding CoPilotUnderstanding?+ OnboardingBrainDump OnboardingBrainDump?BuilderSearchHistory BuilderSearchHistory[]StoreListings StoreListing[]StoreListingReviews StoreListingReview[]@@ -214,6 +215,9 @@ enum OnboardingStep {// No longer rewarded but exist for analytical purposesBUILDER_OPENBUILDER_RUN_AGENT+ // Copilot home first-run: the capability-cards modal was completed or+ // skipped. Unrewarded; recorded so it shows only once per user.+ CAPABILITY_CARDS}model UserOnboarding {@@ -252,6 +256,67 @@ model CoPilotUnderstanding {@@index([userId])}+enum BrainDumpStatus {+ recording_uploaded+ transcribing+ transcribed+ extracting+ completed+ failed+}++enum BrainDumpInputMode {+ voice+ typed+ skipped+}++/// Onboarding voice "brain dump": the raw recording plus its full+/// transcript. The transcript is stored complete even when a shortened+/// version is what gets injected into the copilot's first prompt —+/// truncation is never silent, and the audio is never deleted on failure+/// so the user can always re-run or download it.+model OnboardingBrainDump {+ id String @id @default(uuid())+ createdAt DateTime @default(now())+ updatedAt DateTime @default(now()) @updatedAt++ userId String @unique+ User User @relation(fields: [userId], references: [id], onDelete: Cascade)++ /// Client-generated identifier that ties uploaded parts to one take and+ /// makes `finalize` idempotent across retries.+ recordingId String++ status BrainDumpStatus+ inputMode BrainDumpInputMode++ audioPath String?+ mimeType String?+ sizeBytes Int?+ durationSecs Float?+ transcript String?+ transcriptLang String?+ errorCode String?++ /// Written once when the dump is processed, read by the copilot home's+ /// greeting. Generated up-front rather than on page load so the greeting+ /// is instant — the user has already waited through the loading screen.+ greeting String?+ /// List of {title, prompt} objects the user can pick from under the+ /// greeting. JSON so the shape can evolve without a migration.+ suggestedPrompts Json @default("[]")+ /// True once the user has sent their first message. The greeting is+ /// never shown again after this, but its content is kept.+ greetingSeen Boolean @default(false)+ /// List of {provider, reason} objects Claude picks from the transcript+ /// and the live provider registry. Null until the recommendation+ /// background job (separate from the greeting one) writes its result.+ recommendedProviders Json?++ @@index([userId])+}+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// USER WORKSPACE TABLES /////////////////diff --git a/autogpt_platform/frontend/.env.default b/autogpt_platform/frontend/.env.defaultindex 42ab15b6d7..a91e5bfb9f 100644--- a/autogpt_platform/frontend/.env.default+++ b/autogpt_platform/frontend/.env.default@@ -81,3 +81,10 @@ NEXT_PUBLIC_VAPID_PUBLIC_KEY=BBg49iVTWthVbRYphwmZNvZyiSJDqtSO4nmLxDzLKe3Oo9jbtu0# OpenAI (for voice transcription)OPENAI_API_KEY=++# Voice brain-dump onboarding step (`onboarding-brain-dump` LaunchDarkly+# flag). Commented deliberately: an active value here would ship the feature+# on to every install. Uncomment in your own .env, pair it with+# FORCE_FLAG_ONBOARDING_BRAIN_DUMP in backend/.env, and restart the dev+# server — NEXT_PUBLIC_* vars are inlined at build time.+# NEXT_PUBLIC_FORCE_FLAG_ONBOARDING_BRAIN_DUMP=truediff --git a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/__tests__/brain-dump.test.tsx b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/__tests__/brain-dump.test.tsxnew file mode 100644index 0000000000..11f6450fb0--- /dev/null+++ b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/__tests__/brain-dump.test.tsx@@ -0,0 +1,689 @@+import {+ getDiscardBrainDumpMockHandler200,+ getFinalizeBrainDumpMockHandler200,+ getGetBrainDumpStatusMockHandler200,+ getUploadBrainDumpPartMockHandler200,+} from "@/app/api/__generated__/endpoints/brain-dump/brain-dump.msw";+import { server } from "@/mocks/mock-server";+import {+ cleanup,+ render,+ screen,+ waitFor,+} from "@/tests/integrations/test-utils";+import userEvent from "@testing-library/user-event";+import { http, HttpResponse } from "msw";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";+import OnboardingPage from "../page";+import { NO_PAYWALL_STEPS, PAYWALL_FIRST_STEPS } from "../store";+import { useOnboardingWizardStore } from "../store";++// IndexedDB does not exist in happy-dom and `fake-indexeddb` is not a+// devDependency here, so the recording store is replaced with an+// in-memory double. Everything above it (the recorder, the upload queue,+// the recovery prompt) runs for real.+const { recordingStoreState } = vi.hoisted(() => ({+ recordingStoreState: {+ // Ids the store was asked to mark finalized, kept separately because+ // `clearRecording` deletes the row moments later.+ finalizedIds: [] as string[],+ meta: null as {+ key: string;+ recordingId: string;+ mimeType: string;+ startedAt: number;+ durationSecs: number;+ finalized: boolean;+ } | null,+ parts: [] as {+ id: string;+ recordingId: string;+ partIndex: number;+ blob: Blob;+ savedAt: number;+ uploaded: boolean;+ }[],+ },+}));++vi.mock("../steps/BrainDumpStep/recordingStore", () => ({+ isIndexedDBAvailable: () => true,+ partId: (recordingId: string, partIndex: number) =>+ `${recordingId}:${partIndex}`,+ savePart: async (part: (typeof recordingStoreState.parts)[number]) => {+ recordingStoreState.parts = [+ ...recordingStoreState.parts.filter((p) => p.id !== part.id),+ part,+ ];+ },+ markPartUploaded: async (id: string) => {+ recordingStoreState.parts = recordingStoreState.parts.map((p) =>+ p.id === id ? { ...p, uploaded: true } : p,+ );+ },+ getParts: async (recordingId: string) =>+ recordingStoreState.parts+ .filter((p) => p.recordingId === recordingId)+ .sort((a, b) => a.partIndex - b.partIndex),+ saveMeta: async (meta: Record<string, unknown>) => {+ if (meta.finalized) {+ recordingStoreState.finalizedIds.push(String(meta.recordingId));+ }+ recordingStoreState.meta = {+ ...meta,+ key: "current",+ } as typeof recordingStoreState.meta;+ },+ getMeta: async () => recordingStoreState.meta,+ getMetaById: async (recordingId: string) =>+ recordingStoreState.meta?.recordingId === recordingId+ ? recordingStoreState.meta+ : null,+ clearRecording: async () => {+ recordingStoreState.parts = [];+ recordingStoreState.meta = null;+ },+}));++vi.mock("posthog-js", () => ({+ default: { capture: vi.fn() },+}));++vi.mock("../steps/WelcomeStep", () => ({+ WelcomeStep: () => <div data-testid="step-welcome" />,+}));+vi.mock("../steps/RoleStep", () => ({+ RoleStep: () => <div data-testid="step-role" />,+}));+vi.mock("../steps/SubscriptionStep/SubscriptionStep", () => ({+ SubscriptionStep: () => <div data-testid="step-subscription" />,+}));+vi.mock("../steps/PreparingStep", () => ({+ PreparingStep: () => <div data-testid="step-preparing" />,+}));++let currentSearchParams = new URLSearchParams();+const routerReplace = vi.fn();+vi.mock("next/navigation", () => ({+ useRouter: () => ({+ replace: routerReplace,+ push: vi.fn(),+ refresh: vi.fn(),+ }),+ useSearchParams: () => currentSearchParams,+ usePathname: () => "/onboarding",+}));++const mockRefreshSession = vi.fn(() => Promise.resolve({ user: null }));+vi.mock("@/lib/auth/hooks/useAuth", () => ({+ useAuth: () => ({+ isLoggedIn: true,+ isUserLoading: false,+ user: null,+ refreshSession: mockRefreshSession,+ }),+}));++vi.mock("@/app/api/__generated__/endpoints/onboarding/onboarding", () => ({+ getV1OnboardingState: () =>+ Promise.resolve({ status: 200, data: { completedSteps: [] } }),+ getV1CheckIfOnboardingIsCompleted: () =>+ Promise.resolve({ status: 200, data: false }),+ patchV1UpdateOnboardingState: () => Promise.resolve({ status: 200 }),+ postV1CompleteOnboardingStep: () => Promise.resolve({ status: 200 }),+ postV1SubmitOnboardingProfile: () => Promise.resolve({ status: 200 }),+}));++vi.mock("@/app/api/__generated__/endpoints/credits/credits", () => ({+ useGetSubscriptionStatus: (opts: {+ query: { select: (res: { status: number; data: unknown }) => unknown };+ }) => ({+ data: opts.query.select({ status: 200, data: { tier: "NO_TIER" } }),+ isLoading: false,+ }),+}));++vi.mock("@/app/api/helpers", () => ({+ resolveResponse: (p: Promise<{ data: unknown }>) => p.then((r) => r.data),+}));++// Flag-aware so the payment flag and the brain-dump flag can be driven+// independently — they gate different step slots.+let mockFlags: Record<string, boolean> = {};+vi.mock("@/services/feature-flags/use-get-flag", () => ({+ Flag: {+ ENABLE_PLATFORM_PAYMENT: "enable-platform-payment",+ ONBOARDING_BRAIN_DUMP: "onboarding-brain-dump",+ },+ useGetFlag: (flag: string) => mockFlags[flag] ?? false,+}));++vi.mock("launchdarkly-react-client-sdk", () => ({+ useLDClient: () => ({+ waitForInitialization: () => Promise.resolve(),+ }),+}));++const STEP_STORAGE_KEY = "autogpt:onboarding-highest-step";+const INTRO_PATH_KEY = "autogpt:onboarding-intro-path";+const PILLBOX_HEADING = "What's eating your time?";+const DUMP_HEADLINE = "What keeps stealing your week?";++class FakeMediaRecorder {+ static isTypeSupported() {+ return true;+ }+ state: "inactive" | "recording" = "inactive";+ mimeType: string;+ ondataavailable: ((event: { data: Blob }) => void) | null = null;+ onstop: (() => void) | null = null;++ constructor(_stream: unknown, options?: { mimeType?: string }) {+ this.mimeType = options?.mimeType ?? "audio/webm";+ }++ start(_timesliceMs?: number) {+ this.state = "recording";+ this.ondataavailable?.({+ data: new Blob(["chunk-one"], { type: this.mimeType }),+ });+ }++ stop() {+ this.state = "inactive";+ this.onstop?.();+ }+}++const getUserMedia = vi.fn();++function installBrowserRecordingAPIs() {+ Object.defineProperty(globalThis, "MediaRecorder", {+ configurable: true,+ writable: true,+ value: FakeMediaRecorder,+ });+ Object.defineProperty(navigator, "mediaDevices", {+ configurable: true,+ writable: true,+ value: { getUserMedia },+ });+}++// Records every brain-dump call so "flag off makes no brain-dump request"+// can be asserted, and so request bodies can be inspected.+function recordBrainDumpTraffic() {+ const calls: { endpoint: string; body?: unknown }[] = [];+ server.use(+ getFinalizeBrainDumpMockHandler200(async (info) => {+ calls.push({ endpoint: "finalize", body: await info.request.json() });+ return { status: "completed" as const, input_mode: "voice" as const };+ }),+ getUploadBrainDumpPartMockHandler200(() => {+ calls.push({ endpoint: "parts" });+ return {+ recording_id: "r",+ part_index: 0,+ received_bytes: 9,+ total_bytes: 9,+ };+ }),+ getGetBrainDumpStatusMockHandler200(() => {+ calls.push({ endpoint: "status" });+ return { status: "completed" as const, greeting_ready: true };+ }),+ getDiscardBrainDumpMockHandler200(() => {+ calls.push({ endpoint: "discard" });+ return { status: null };+ }),+ http.get(+ "http://localhost:3000/api/proxy/api/onboarding/brain-dump/recording",+ () => {+ calls.push({ endpoint: "recording" });+ return HttpResponse.json({});+ },+ ),+ );+ return calls;+}++function finalizeReturns(response: {+ status: "completed" | "failed";+ error_code?: string;+}) {+ const bodies: unknown[] = [];+ server.use(+ getFinalizeBrainDumpMockHandler200(async (info) => {+ bodies.push(await info.request.json());+ return { ...response, input_mode: "voice" as const };+ }),+ );+ return bodies;+}++function stepDots(container: HTMLElement) {+ return container.querySelectorAll("div.h-2.rounded-full").length;+}++function progressWidth(container: HTMLElement) {+ const bar = container.querySelector<HTMLElement>("div.bg-purple-400");+ return bar?.style.width ?? null;+}++// Lands the wizard on the step the pillboxes / the brain dump share.+function landOnPainPointsStep() {+ window.sessionStorage.setItem(+ STEP_STORAGE_KEY,+ String(NO_PAYWALL_STEPS.painPoints),+ );+ currentSearchParams = new URLSearchParams(+ `step=${NO_PAYWALL_STEPS.painPoints}`,+ );+}++beforeEach(() => {+ installBrowserRecordingAPIs();+ getUserMedia.mockReset();+ getUserMedia.mockResolvedValue({ getTracks: () => [] });+ mockFlags = {};+ currentSearchParams = new URLSearchParams();+ routerReplace.mockClear();+ recordingStoreState.meta = null;+ recordingStoreState.parts = [];+ recordingStoreState.finalizedIds = [];+ useOnboardingWizardStore.getState().reset();+ window.sessionStorage.removeItem(STEP_STORAGE_KEY);+ window.sessionStorage.removeItem(INTRO_PATH_KEY);+});++afterEach(() => {+ cleanup();+});++describe("onboarding brain dump — flag gating", () => {+ it("renders the brain dump in the pillbox slot when the flag is on", async () => {+ mockFlags = { "onboarding-brain-dump": true };+ landOnPainPointsStep();++ render(<OnboardingPage />);++ expect(await screen.findByText(DUMP_HEADLINE)).toBeDefined();+ expect(+ screen.getByRole("button", { name: "Start recording" }),+ ).toBeDefined();+ expect(screen.queryByText(PILLBOX_HEADING)).toBeNull();+ });++ it("leaves the pillboxes untouched and makes no brain-dump request when the flag is off", async () => {+ const calls = recordBrainDumpTraffic();+ mockFlags = {};+ landOnPainPointsStep();++ render(<OnboardingPage />);++ expect(await screen.findByText(PILLBOX_HEADING)).toBeDefined();+ expect(+ screen.getByText("Pick the tasks you'd love to hand off to AutoPilot"),+ ).toBeDefined();+ expect(screen.queryByText(DUMP_HEADLINE)).toBeNull();+ expect(+ screen.queryByRole("button", { name: "Start recording" }),+ ).toBeNull();+ expect(screen.queryByText("Skip for now")).toBeNull();++ // Give any stray effect a chance to fire before declaring silence.+ await new Promise((resolve) => setTimeout(resolve, 50));+ expect(calls).toEqual([]);+ });++ // Guards the assertion above: proves the recorder above is actually wired+ // to the brain-dump endpoints, so an empty `calls` means silence rather+ // than a handler that never intercepts anything.+ it("records brain-dump traffic when the flag is on and the user acts", async () => {+ const calls = recordBrainDumpTraffic();+ mockFlags = { "onboarding-brain-dump": true };+ landOnPainPointsStep();++ render(<OnboardingPage />);+ await screen.findByText(DUMP_HEADLINE);+ await userEvent.click(screen.getByRole("button", { name: "Skip for now" }));++ await waitFor(() =>+ expect(calls.map((c) => c.endpoint)).toEqual(["finalize"]),+ );+ });+});++describe("onboarding brain dump — typed fallback", () => {+ it("opens the typed composer under the same headline when mic permission is denied", async () => {+ getUserMedia.mockRejectedValue(+ new DOMException("denied", "NotAllowedError"),+ );+ mockFlags = { "onboarding-brain-dump": true };+ landOnPainPointsStep();++ render(<OnboardingPage />);+ await screen.findByText(DUMP_HEADLINE);++ await userEvent.click(+ screen.getByRole("button", { name: "Start recording" }),+ );++ expect(+ await screen.findByPlaceholderText(+ "What repeats every week? What would you hand off first?",+ ),+ ).toBeDefined();+ // Same headline, not a dead end.+ expect(screen.getByText(DUMP_HEADLINE)).toBeDefined();+ expect(+ screen.queryByRole("button", { name: "Start recording" }),+ ).toBeNull();+ // Offering a way back to the orb would be a dead end here: the browser+ // has already refused the microphone.+ expect(screen.queryByRole("button", { name: "record instead" })).toBeNull();+ });++ it("opens the typed composer from the rest state via 'type instead'", async () => {+ mockFlags = { "onboarding-brain-dump": true };+ landOnPainPointsStep();++ render(<OnboardingPage />);+ await screen.findByText(DUMP_HEADLINE);+ expect(+ screen.queryByPlaceholderText(+ "What repeats every week? What would you hand off first?",+ ),+ ).toBeNull();++ await userEvent.click(screen.getByRole("button", { name: "type instead" }));++ expect(+ await screen.findByPlaceholderText(+ "What repeats every week? What would you hand off first?",+ ),+ ).toBeDefined();+ expect(getUserMedia).not.toHaveBeenCalled();+ });+});++describe("onboarding brain dump — skip", () => {+ it("posts input_mode 'skipped' and advances the wizard", async () => {+ const bodies = finalizeReturns({ status: "completed" });+ mockFlags = { "onboarding-brain-dump": true };+ landOnPainPointsStep();++ render(<OnboardingPage />);+ await screen.findByText(DUMP_HEADLINE);++ await userEvent.click(screen.getByRole("button", { name: "Skip for now" }));++ await waitFor(() => expect(bodies).toHaveLength(1));+ expect(bodies[0]).toEqual({+ recording_id: expect.any(String),+ input_mode: "skipped",+ });++ expect(await screen.findByTestId("step-preparing")).toBeDefined();+ expect(window.sessionStorage.getItem(INTRO_PATH_KEY)).toBe("B");+ });+});++describe("onboarding brain dump — finishing a take", () => {+ // The success path all the way through: without it the finalize+ // bookkeeping could throw and be swallowed by its own try/catch and no+ // test would notice.+ it("marks the take finalized and clears it before advancing", async () => {+ const partUploads: string[] = [];+ server.use(+ getUploadBrainDumpPartMockHandler200(() => {+ partUploads.push("part");+ return {+ recording_id: "r",+ part_index: 0,+ received_bytes: 9,+ total_bytes: 9,+ };+ }),+ );+ const bodies = finalizeReturns({ status: "completed" });+ mockFlags = { "onboarding-brain-dump": true };+ landOnPainPointsStep();++ render(<OnboardingPage />);+ await screen.findByText(DUMP_HEADLINE);++ await userEvent.click(+ screen.getByRole("button", { name: "Start recording" }),+ );+ await waitFor(() => expect(partUploads).toHaveLength(1));+ const doneButtons = await screen.findAllByRole("button", {+ name: "I'm done",+ });+ await userEvent.click(doneButtons[doneButtons.length - 1]);++ expect(await screen.findByTestId("step-preparing")).toBeDefined();+ expect(bodies).toHaveLength(1);+ const recordingId = (bodies[0] as { recording_id: string }).recording_id;+ // Marked finalized by id, then cleared: an unfinalized row left behind+ // makes the next visit offer back a take that is already submitted.+ expect(recordingStoreState.finalizedIds).toEqual([recordingId]);+ expect(recordingStoreState.meta).toBeNull();+ expect(recordingStoreState.parts).toEqual([]);+ expect(window.sessionStorage.getItem(INTRO_PATH_KEY)).toBe("A");+ });++ // Skipping mid-submit ran a second `nextStep()` behind the finalize+ // already in flight, landing past the last step on a blank screen with+ // Back and Log out hidden.+ it("takes 'Skip for now' away while the dump is being submitted", async () => {+ const partUploads: string[] = [];+ server.use(+ getUploadBrainDumpPartMockHandler200(() => {+ partUploads.push("part");+ return {+ recording_id: "r",+ part_index: 0,+ received_bytes: 9,+ total_bytes: 9,+ };+ }),+ getFinalizeBrainDumpMockHandler200(async () => {+ await new Promise(() => undefined);+ return { status: "completed" as const, input_mode: "voice" as const };+ }),+ );+ mockFlags = { "onboarding-brain-dump": true };+ landOnPainPointsStep();++ render(<OnboardingPage />);+ await screen.findByText(DUMP_HEADLINE);+ expect(screen.getByRole("button", { name: "Skip for now" })).toBeDefined();++ await userEvent.click(+ screen.getByRole("button", { name: "Start recording" }),+ );+ await waitFor(() => expect(partUploads).toHaveLength(1));+ const doneButtons = await screen.findAllByRole("button", {+ name: "I'm done",+ });+ await userEvent.click(doneButtons[doneButtons.length - 1]);++ expect(await screen.findByText("Got it. One second…")).toBeDefined();+ expect(screen.queryByRole("button", { name: "Skip for now" })).toBeNull();+ });+});++describe("onboarding brain dump — recovery", () => {+ it("offers to pick up an unfinalized recording that still has parts", async () => {+ recordingStoreState.meta = {+ key: "current",+ recordingId: "rec-1",+ mimeType: "audio/webm",+ startedAt: Date.now(),+ durationSecs: 95,+ finalized: false,+ };+ recordingStoreState.parts = [+ {+ id: "rec-1:0",+ recordingId: "rec-1",+ partIndex: 0,+ blob: new Blob(["chunk"], { type: "audio/webm" }),+ savedAt: Date.now(),+ uploaded: false,+ },+ ];+ mockFlags = { "onboarding-brain-dump": true };+ landOnPainPointsStep();++ render(<OnboardingPage />);++ expect(+ await screen.findByText("Pick up where you left off?"),+ ).toBeDefined();+ // The duration sits in its own <span>, so match on the paragraph's+ // combined text rather than a single text node.+ expect(+ screen.getByText(+ (_content, element) =>+ element?.textContent === "We kept the 1:35 you already recorded.",+ { selector: "p" },+ ),+ ).toBeDefined();+ expect(+ screen.getByRole("button", { name: "Use that recording" }),+ ).toBeDefined();+ expect(screen.getByRole("button", { name: "Start over" })).toBeDefined();+ });++ it("does not offer recovery when the stored recording has no parts", async () => {+ recordingStoreState.meta = {+ key: "current",+ recordingId: "rec-empty",+ mimeType: "audio/webm",+ startedAt: Date.now(),+ durationSecs: 12,+ finalized: false,+ };+ recordingStoreState.parts = [];+ mockFlags = { "onboarding-brain-dump": true };+ landOnPainPointsStep();++ render(<OnboardingPage />);++ expect(await screen.findByText(DUMP_HEADLINE)).toBeDefined();+ await waitFor(() =>+ expect(screen.queryByText("Pick up where you left off?")).toBeNull(),+ );+ expect(+ screen.getByRole("button", { name: "Start recording" }),+ ).toBeDefined();+ });+});++describe("onboarding brain dump — failure", () => {+ it("keeps the copy calm and offers retry plus download when finalize reports failure", async () => {+ const partUploads: string[] = [];+ server.use(+ getUploadBrainDumpPartMockHandler200(() => {+ partUploads.push("part");+ return {+ recording_id: "r",+ part_index: 0,+ received_bytes: 9,+ total_bytes: 9,+ };+ }),+ );+ const finalizeBodies = finalizeReturns({+ status: "failed",+ error_code: "transcription_error",+ });+ mockFlags = { "onboarding-brain-dump": true };+ landOnPainPointsStep();++ render(<OnboardingPage />);+ await screen.findByText(DUMP_HEADLINE);++ await userEvent.click(+ screen.getByRole("button", { name: "Start recording" }),+ );+ // Wait for the first chunk to reach the server so "I'm done" is not+ // racing the upload queue.+ await waitFor(() => expect(partUploads).toHaveLength(1));++ const doneButtons = await screen.findAllByRole("button", {+ name: "I'm done",+ });+ await userEvent.click(doneButtons[doneButtons.length - 1]);++ expect(await screen.findByText("That didn't go through.")).toBeDefined();+ // The failure has to come from finalize reporting `failed`, not from the+ // upload queue giving up before it ever got there.+ expect(finalizeBodies).toHaveLength(1);+ expect(finalizeBodies[0]).toMatchObject({+ input_mode: "voice",+ mime_type: "audio/webm",+ recording_id: expect.any(String),+ });+ expect(+ screen.getByText("Your recording is safe. Try again."),+ ).toBeDefined();+ expect(screen.getByRole("button", { name: "Try again" })).toBeDefined();+ expect(+ screen.getByRole("button", { name: "Download recording" }),+ ).toBeDefined();+ expect(+ screen.getByRole("button", { name: "Continue without it" }),+ ).toBeDefined();+ // The wizard must not advance on failure.+ expect(screen.queryByTestId("step-preparing")).toBeNull();+ });+});++describe("onboarding step map integrity", () => {+ it("keeps the step constants identical regardless of the brain-dump flag", () => {+ expect(PAYWALL_FIRST_STEPS).toEqual({+ subscription: 1,+ welcome: 2,+ role: 3,+ painPoints: 4,+ preparing: 5,+ });+ expect(NO_PAYWALL_STEPS).toEqual({+ welcome: 1,+ role: 2,+ painPoints: 3,+ preparing: 4,+ });+ });++ it("renders the same screen count and progress with the flag on and off", async () => {+ mockFlags = {};+ landOnPainPointsStep();+ const off = render(<OnboardingPage />);+ await screen.findByText(PILLBOX_HEADING);+ const offDots = stepDots(off.container);+ const offWidth = progressWidth(off.container);+ const offUrl = routerReplace.mock.calls.map((c) => c[0]);++ cleanup();+ routerReplace.mockClear();+ useOnboardingWizardStore.getState().reset();++ mockFlags = { "onboarding-brain-dump": true };+ landOnPainPointsStep();+ const on = render(<OnboardingPage />);+ await screen.findByText(DUMP_HEADLINE);+ const onDots = stepDots(on.container);+ const onWidth = progressWidth(on.container);+ const onUrl = routerReplace.mock.calls.map((c) => c[0]);++ expect(offDots).toBe(3);+ expect(offWidth).toBe("75%");+ expect(onDots).toBe(offDots);+ expect(onWidth).toBe(offWidth);+ expect(onUrl).toEqual(offUrl);+ });+});diff --git a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/__tests__/page.test.tsx b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/__tests__/page.test.tsxindex 2e0642fdf9..d60f766341 100644--- a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/__tests__/page.test.tsx+++ b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/__tests__/page.test.tsx@@ -90,10 +90,18 @@ vi.mock("@/app/api/helpers", () => ({resolveResponse: (p: Promise<{ data: unknown }>) => p.then((r) => r.data),}));+// Answers per-flag rather than returning one shared boolean: these tests+// only mean to toggle the paywall, and a blanket `true` would also switch+// on every other gated flag — including the brain dump, which replaces+// the pillbox step this file asserts on.let mockFlagValue = false;vi.mock("@/services/feature-flags/use-get-flag", () => ({- Flag: { ENABLE_PLATFORM_PAYMENT: "ENABLE_PLATFORM_PAYMENT" },- useGetFlag: () => mockFlagValue,+ Flag: {+ ENABLE_PLATFORM_PAYMENT: "ENABLE_PLATFORM_PAYMENT",+ ONBOARDING_BRAIN_DUMP: "ONBOARDING_BRAIN_DUMP",+ },+ useGetFlag: (flag: string) =>+ flag === "ENABLE_PLATFORM_PAYMENT" ? mockFlagValue : false,}));vi.mock("launchdarkly-react-client-sdk", () => ({diff --git a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/page.tsx b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/page.tsxindex 0aac14b9cb..54c79db46e 100644--- a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/page.tsx+++ b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/page.tsx@@ -2,6 +2,7 @@import Link from "next/link";import { ProgressBar } from "./components/ProgressBar";import { StepIndicator } from "./components/StepIndicator";+import { BrainDumpStep } from "./steps/BrainDumpStep/BrainDumpStep";import { PainPointsStep } from "./steps/PainPointsStep";import { PreparingStep } from "./steps/PreparingStep";import { RoleStep } from "./steps/RoleStep";@@ -18,11 +19,13 @@ export default function OnboardingPage() {isLoading,handlePreparingComplete,isPaymentEnabled,+ isBrainDumpEnabled,steps,preparingStep,totalSteps,} = useOnboardingPage();const prevStep = useOnboardingWizardStore((s) => s.prevStep);+ const isStepBusy = useOnboardingWizardStore((s) => s.isStepBusy);if (isLoading) return null;@@ -31,8 +34,11 @@ export default function OnboardingPage() {const showDots = currentStep <= totalSteps;// Back is hidden on Welcome (the first profile step): going back from there// when payments are on would return the user to the paywall they already- // paid through and let them re-trigger checkout.- const showBack = currentStep > steps.welcome && currentStep <= totalSteps;+ // paid through and let them re-trigger checkout. Also hidden while the+ // current step is mid-flight (brain dump processing) — there is nothing+ // coherent to go back to.+ const showBack =+ currentStep > steps.welcome && currentStep <= totalSteps && !isStepBusy;const showProgressBar = currentStep <= totalSteps;const showLogout = currentStep <= totalSteps;@@ -60,9 +66,13 @@ export default function OnboardingPage() {)}{currentStep === steps.welcome && <WelcomeStep />}{currentStep === steps.role && <RoleStep />}- {currentStep === steps.painPoints && <PainPointsStep />}+ {currentStep === steps.painPoints &&+ (isBrainDumpEnabled ? <BrainDumpStep /> : <PainPointsStep />)}{currentStep === preparingStep && (- <PreparingStep onComplete={handlePreparingComplete} />+ <PreparingStep+ onComplete={handlePreparingComplete}+ isBrainDumpEnabled={isBrainDumpEnabled}+ />)}</div>diff --git a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/BrainDumpStep.tsx b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/BrainDumpStep.tsxnew file mode 100644index 0000000000..145967ce2a--- /dev/null+++ b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/BrainDumpStep.tsx@@ -0,0 +1,253 @@+"use client";++import { Button } from "@/components/atoms/Button/Button";+import { Text } from "@/components/atoms/Text/Text";+import { cn } from "@/lib/utils";+import { ElapsedTime } from "./components/ElapsedTime";+import { FailureState } from "./components/FailureState";+import { DEFAULT_GLASS_PARAMS } from "@/components/molecules/GlassOrb/GlassSurface";+import { MicButton, OrbScreen } from "./components/MicButton";+import { PrivacyNote } from "./components/PrivacyNote";+import { RecordingStatus } from "./components/RecordingStatus";+import { RecoveryPrompt } from "./components/RecoveryPrompt";+import { RevealGroup, RevealItem } from "@/components/atoms/Reveal/Reveal";+import { SwapFade } from "@/components/atoms/SwapFade/SwapFade";+import { TapHint } from "./components/TapHint";+import { TypedFallback } from "./components/TypedFallback";+import { ringProgress } from "./helpers";+import { ScreenState, useBrainDumpStep } from "./useBrainDumpStep";++const MIC_CAPTION = "Tap and talk. Most people go 2 to 3 minutes.";+const FAILURE_HEADLINE = "That didn't go through.";+const TIME_LIMIT_CAPTION =+ "That's 30 minutes — the most we record in one go. Saving all of it…";++export function BrainDumpStep() {+ const dump = useBrainDumpStep();+ const isRecording = dump.screen === "recording";+ const isProcessing = dump.screen === "processing";+ const isMicScreen = dump.screen === "rest" || isRecording;+ const isTyping = dump.screen === "typing";+ const showSubline = dump.screen !== "failed" && dump.screen !== "recovery";+ // rest → recording → processing all share one orb, so it is never+ // unmounted between them: only the glyph and the ring change.+ const orbScreen = toOrbScreen(dump.screen);++ function orbClick(screen: OrbScreen) {+ if (screen === "processing") return undefined;+ if (screen === "failed") return dump.handleRetry;+ return screen === "recording" ? dump.handleDone : dump.handleStart;+ }++ return (+ <>+ <RevealGroup+ className={cn(+ "-mt-44 flex w-full flex-col items-center gap-12 px-4",+ // The composer needs more room than the orb screens do.+ isTyping ? "max-w-4xl" : "max-w-2xl",+ )}+ >+ <div className="absolute right-6 top-6 flex items-center gap-5">+ {isRecording && (+ <Button+ variant="secondary"+ size="small"+ onClick={dump.handleRestart}+ >+ Restart+ </Button>+ )}+ {/* Skipping mid-submit would advance the wizard a second time+ behind the finalize that is already in flight, landing past+ the last step on a blank screen. */}+ {!isProcessing && (+ <button+ type="button"+ onClick={dump.handleSkip}+ className="text-sm text-zinc-400 transition-colors hover:text-zinc-700"+ >+ Skip for now+ </button>+ )}+ </div>++ <div className="mx-auto flex w-full max-w-2xl flex-col items-center gap-2 px-4 text-center">+ <RevealItem>+ <Text variant="h3">+ {dump.screen === "failed" ? FAILURE_HEADLINE : dump.headline}+ </Text>+ </RevealItem>+ {showSubline && (+ <RevealItem>+ <Text+ variant="lead"+ className="!text-zinc-500 md:whitespace-nowrap"+ >+ Just talk.{" "}+ <span className="bg-gradient-to-r from-purple-500 to-indigo-500 bg-clip-text text-transparent">+ AutoPilot+ </span>{" "}+ listens, remembers, and starts taking work off your plate.+ </Text>+ </RevealItem>+ )}+ </div>++ {orbScreen && (+ <RevealItem className="flex flex-col items-center gap-4">+ <MicButton+ screen={orbScreen}+ progress={ringProgress(dump.elapsedSeconds)}+ audioStream={dump.audioStream}+ glassParams={DEFAULT_GLASS_PARAMS}+ onClick={orbClick(orbScreen)}+ />+ {/* Both slots keep their height across rest → recording →+ processing, so advancing a screen swaps their contents without+ nudging the orb or the headline. Failure has its own layout+ below the orb and needs neither. */}+ {orbScreen !== "failed" && (+ <>+ <div className="flex h-10 w-full items-center justify-center">+ <SwapFade+ swapKey={orbScreen}+ className="flex w-full justify-center"+ >+ <OrbCaption+ screen={orbScreen}+ reachedTimeLimit={dump.reachedTimeLimit}+ />+ </SwapFade>+ </div>+ <div className="flex h-10 items-center justify-center">+ {isRecording && <ElapsedTime seconds={dump.elapsedSeconds} />}+ </div>+ </>+ )}+ </RevealItem>+ )}++ {dump.screen === "recovery" && dump.recoverable && (+ <RevealItem>+ <RecoveryPrompt+ durationSecs={dump.recoverable.durationSecs}+ onResume={dump.handleResumeRecovered}+ onDiscard={dump.handleDiscardRecovered}+ />+ </RevealItem>+ )}++ {dump.screen === "failed" && (+ <RevealItem>+ <FailureState+ onDownload={dump.handleDownloadRecording}+ onSkip={dump.handleSkip}+ />+ </RevealItem>+ )}++ {dump.screen === "typing" && (+ <RevealItem className="w-full">+ <TypedFallback+ value={dump.typedText}+ onChange={dump.setTypedText}+ onSubmit={dump.handleSubmitTyped}+ />+ </RevealItem>+ )}+ </RevealGroup>++ {/* Viewport-anchored, and kept outside the reveal group: an ancestor+ that animates `filter` or `transform` would turn these into+ absolutely positioned elements. */}+ {(isMicScreen ||+ isTyping ||+ dump.screen === "failed" ||+ dump.screen === "recovery") && (+ <div className="fixed inset-x-0 bottom-32 flex justify-center px-4">+ <SwapFade swapKey={dump.screen}>+ {isRecording && (+ <div className="flex flex-col items-center gap-3">+ <RecordingStatus+ elapsedSeconds={dump.elapsedSeconds}+ showSilenceNudge={dump.showSilenceNudge}+ isOffline={dump.isOffline}+ isSavedLocally={dump.isSavedLocally}+ />+ <Button+ variant="primary"+ size="large"+ onClick={dump.handleDone}+ >+ I'm done+ </Button>+ </div>+ )}+ {(dump.screen === "rest" || dump.screen === "failed") && (+ <Button+ variant="ghost"+ size="small"+ onClick={dump.showTyping}+ className="underline underline-offset-4"+ >+ type instead+ </Button>+ )}+ {isTyping && !dump.isMicBlocked && (+ <Button+ variant="ghost"+ size="small"+ onClick={dump.showRecording}+ className="underline underline-offset-4"+ >+ record instead+ </Button>+ )}+ {dump.screen === "recovery" && (+ <Button+ variant="ghost"+ size="small"+ onClick={dump.handleTypeInsteadOfRecovered}+ className="underline underline-offset-4"+ >+ type instead+ </Button>+ )}+ </SwapFade>+ </div>+ )}++ {showSubline && <PrivacyNote />}+ </>+ );+}++function toOrbScreen(screen: ScreenState): OrbScreen | null {+ if (screen === "typing" || screen === "recovery") return null;+ return screen;+}++function OrbCaption({+ screen,+ reachedTimeLimit,+}: {+ screen: OrbScreen;+ reachedTimeLimit: boolean;+}) {+ if (screen === "processing") {+ return (+ <Text variant="lead" className="!text-base !text-zinc-500">+ {reachedTimeLimit ? TIME_LIMIT_CAPTION : "Got it. One second…"}+ </Text>+ );+ }++ // The failure copy sits with its buttons in FailureState, so it is not+ // held back by the swap's exit animation.+ if (screen === "failed") return null;++ if (screen === "recording") return null;++ return <TapHint caption={MIC_CAPTION} />;+}diff --git a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/__tests__/helpers.test.ts b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/__tests__/helpers.test.tsnew file mode 100644index 0000000000..c715012889--- /dev/null+++ b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/__tests__/helpers.test.ts@@ -0,0 +1,104 @@+import { afterEach, describe, expect, it, vi } from "vitest";+import {+ encouragementAt,+ formatElapsed,+ headline,+ isPermissionDenied,+ pickMimeType,+ RING_TARGET_SECONDS,+ ringProgress,+} from "../helpers";++describe("headline", () => {+ it("greets by name and trims the stored value", () => {+ expect(headline(" Ada ")).toBe("What keeps stealing your week, Ada?");+ });++ it("drops the name when the wizard never collected one", () => {+ expect(headline(" ")).toBe("What keeps stealing your week?");+ });+});++describe("encouragementAt", () => {+ it("shows nothing before the first line is due", () => {+ expect(encouragementAt(0)).toBeNull();+ expect(encouragementAt(9.9)).toBeNull();+ });++ it("shows a line for six seconds and then goes quiet", () => {+ expect(encouragementAt(10)).toBe("Keep going, this is gold");+ expect(encouragementAt(15.9)).toBe("Keep going, this is gold");+ expect(encouragementAt(16)).toBeNull();+ });++ // After the last line the screen stays quiet — a nag every 20s would+ // turn encouragement into pressure.+ it("stops encouraging after the last line", () => {+ expect(encouragementAt(45)).toBe(+ "You're building AutoPilot's memory right now",+ );+ expect(encouragementAt(51)).toBeNull();+ expect(encouragementAt(600)).toBeNull();+ });+});++describe("formatElapsed", () => {+ it("pads the seconds and floors the fraction", () => {+ expect(formatElapsed(0)).toBe("0:00");+ expect(formatElapsed(9.9)).toBe("0:09");+ expect(formatElapsed(95)).toBe("1:35");+ expect(formatElapsed(3600)).toBe("60:00");+ });+});++describe("ringProgress", () => {+ // A depth meter, not a limit: the ring holds at full and recording+ // carries on past it.+ it("fills toward the target and then holds", () => {+ expect(ringProgress(0)).toBe(0);+ expect(ringProgress(RING_TARGET_SECONDS / 2)).toBeCloseTo(0.5, 5);+ expect(ringProgress(RING_TARGET_SECONDS)).toBe(1);+ expect(ringProgress(RING_TARGET_SECONDS * 10)).toBe(1);+ });+});++describe("pickMimeType", () => {+ afterEach(() => {+ vi.unstubAllGlobals();+ });++ it("falls back to webm where MediaRecorder does not exist", () => {+ expect(pickMimeType()).toBe("audio/webm");+ });++ it("uses webm when the browser supports it", () => {+ vi.stubGlobal("MediaRecorder", { isTypeSupported: () => true });+ expect(pickMimeType()).toBe("audio/webm");+ });++ // Safari only offers mp4, and the server allowlist has to match.+ it("falls back to mp4 when webm is unsupported", () => {+ vi.stubGlobal("MediaRecorder", {+ isTypeSupported: (type: string) => type !== "audio/webm",+ });+ expect(pickMimeType()).toBe("audio/mp4");+ });+});++describe("isPermissionDenied", () => {+ it("recognises the browser's refusal", () => {+ expect(isPermissionDenied(new DOMException("no", "NotAllowedError"))).toBe(+ true,+ );+ });++ // A missing microphone or a generic failure is not a denial — treating+ // it as one would send the user to the typed fallback for good.+ it("does not treat other failures as a denial", () => {+ expect(isPermissionDenied(new DOMException("gone", "NotFoundError"))).toBe(+ false,+ );+ expect(isPermissionDenied(new Error("NotAllowedError"))).toBe(false);+ expect(isPermissionDenied(null)).toBe(false);+ });+});diff --git a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/__tests__/recordingStore.test.ts b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/__tests__/recordingStore.test.tsnew file mode 100644index 0000000000..12eab4dbe2--- /dev/null+++ b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/__tests__/recordingStore.test.ts@@ -0,0 +1,481 @@+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";+import {+ clearRecording,+ getMeta,+ getMetaById,+ getParts,+ isIndexedDBAvailable,+ markPartUploaded,+ partId,+ saveMeta,+ savePart,+ type RecordingMeta,+ type RecordingPart,+} from "../recordingStore";++// happy-dom has no IndexedDB and `fake-indexeddb` is not a devDependency+// here, so the whole factory is faked in-memory. It models the parts the+// store actually leans on: events fire asynchronously, a transaction+// commits *after* its requests succeed, an upgrade can be blocked by+// another connection, and connections have to be closed by hand.+const DB_NAME = "autogpt-onboarding-brain-dump";++interface FakeStoreData {+ keyPath: string;+ records: Map<unknown, Record<string, unknown>>;+ indexes: Map<string, string>;+}++interface FakeDatabaseData {+ version: number;+ stores: Map<string, FakeStoreData>;+}++class FakeRequest {+ result: unknown = undefined;+ error: unknown = null;+ onsuccess: (() => void) | null = null;+ onerror: (() => void) | null = null;+}++class FakeOpenRequest extends FakeRequest {+ onupgradeneeded: (() => void) | null = null;+ onblocked: (() => void) | null = null;+}++class FakeStoreHandle {+ constructor(+ private data: FakeStoreData,+ private transaction: FakeTransaction | null,+ private factory: FakeIndexedDB,+ ) {}++ createIndex(name: string, keyPath: string) {+ this.data.indexes.set(name, keyPath);+ }++ put(value: Record<string, unknown>) {+ return this.run(() => {+ if (this.factory.failWrites) throw new Error("QuotaExceededError");+ const key = value[this.data.keyPath];+ this.data.records.set(key, value);+ return key;+ });+ }++ get(key: unknown) {+ return this.run(() => this.data.records.get(key));+ }++ getAll() {+ return this.run(() => [...this.data.records.values()]);+ }++ delete(key: unknown) {+ return this.run(() => {+ this.data.records.delete(key);+ return undefined;+ });+ }++ index(name: string) {+ const keyPath = this.data.indexes.get(name);+ if (!keyPath) throw new Error(`no index ${name}`);+ return {+ getAll: (value: unknown) =>+ this.run(() =>+ [...this.data.records.values()].filter(+ (record) => record[keyPath] === value,+ ),+ ),+ };+ }++ private run(execute: () => unknown) {+ const request = new FakeRequest();+ if (!this.transaction) throw new Error("no transaction");+ this.transaction.push(request, execute);+ return request;+ }+}++class FakeTransaction {+ error: unknown = null;+ oncomplete: (() => void) | null = null;+ onabort: (() => void) | null = null;+ onerror: (() => void) | null = null;+ private operations: (() => void)[] = [];+ private aborted = false;++ constructor(+ private data: FakeDatabaseData,+ private factory: FakeIndexedDB,+ ) {+ queueMicrotask(() => this.settle());+ }++ objectStore(name: string) {+ const store = this.data.stores.get(name);+ if (!store) throw new Error(`no store ${name}`);+ return new FakeStoreHandle(store, this, this.factory);+ }++ push(request: FakeRequest, execute: () => unknown) {+ this.operations.push(() => {+ try {+ request.result = execute();+ request.onsuccess?.();+ } catch (error) {+ request.error = error;+ this.error = error;+ this.aborted = true;+ request.onerror?.();+ }+ });+ }++ private settle() {+ for (const operation of this.operations) {+ if (this.aborted) break;+ operation();+ }+ // A commit can fail after every request reported success — the case+ // the store is careful to resolve on `oncomplete` for.+ if (!this.aborted && this.factory.failCommit) {+ this.error = new Error("commit failed");+ this.aborted = true;+ }+ if (this.aborted) this.onabort?.();+ else this.oncomplete?.();+ }+}++class FakeConnection {+ constructor(+ private data: FakeDatabaseData,+ private factory: FakeIndexedDB,+ ) {}++ get objectStoreNames() {+ const names = [...this.data.stores.keys()];+ return { contains: (name: string) => names.includes(name) };+ }++ createObjectStore(name: string, options: { keyPath: string }) {+ const store: FakeStoreData = {+ keyPath: options.keyPath,+ records: new Map(),+ indexes: new Map(),+ };+ this.data.stores.set(name, store);+ return new FakeStoreHandle(store, null, this.factory);+ }++ deleteObjectStore(name: string) {+ this.data.stores.delete(name);+ }++ transaction(_storeName: string, _mode: string) {+ return new FakeTransaction(this.data, this.factory);+ }++ close() {+ this.factory.openConnections.delete(this);+ }+}++class FakeIndexedDB {+ readonly openConnections = new Set<FakeConnection>();+ failWrites = false;+ failCommit = false;+ // Stands in for another tab holding an older version open.+ blockUpgrades = false;+ private databases = new Map<string, FakeDatabaseData>();+ private parked: (() => void)[] = [];++ open(name: string, version: number) {+ const request = new FakeOpenRequest();+ queueMicrotask(() => {+ const existing = this.databases.get(name);+ if (this.blockUpgrades && (!existing || existing.version < version)) {+ this.parked.push(() => this.finishOpen(request, name, version));+ request.onblocked?.();+ return;+ }+ this.finishOpen(request, name, version);+ });+ return request;+ }++ // The blocking tab went away: parked upgrades now run to completion,+ // handing a connection to a caller that may already have given up.+ releaseBlockedUpgrades() {+ const parked = this.parked;+ this.parked = [];+ this.blockUpgrades = false;+ parked.forEach((resume) => resume());+ }++ private finishOpen(request: FakeOpenRequest, name: string, version: number) {+ let data = this.databases.get(name);+ const needsUpgrade = !data || data.version < version;+ if (!data) {+ data = { version, stores: new Map() };+ this.databases.set(name, data);+ }+ const connection = new FakeConnection(data, this);+ request.result = connection;+ if (needsUpgrade) {+ data.version = version;+ request.onupgradeneeded?.();+ }+ this.openConnections.add(connection);+ request.onsuccess?.();+ }+}++let fakeIndexedDB: FakeIndexedDB;++function part(overrides: Partial<RecordingPart> = {}): RecordingPart {+ const recordingId = overrides.recordingId ?? "rec-1";+ const partIndex = overrides.partIndex ?? 0;+ return {+ id: partId(recordingId, partIndex),+ recordingId,+ partIndex,+ blob: new Blob(["chunk"], { type: "audio/webm" }),+ savedAt: 1_000,+ uploaded: false,+ ...overrides,+ };+}++function meta(overrides: Partial<RecordingMeta> = {}): RecordingMeta {+ return {+ recordingId: "rec-1",+ mimeType: "audio/webm",+ startedAt: 1_000,+ durationSecs: 0,+ finalized: false,+ ...overrides,+ };+}++// Builds the v1 shape: a `meta` store keyed by a single "current" row.+function seedLegacyDatabase(legacyPart: RecordingPart) {+ return new Promise<void>((resolve, reject) => {+ const request = fakeIndexedDB.open(DB_NAME, 1);+ request.onupgradeneeded = () => {+ const db = request.result as FakeConnection;+ const parts = db.createObjectStore("parts", { keyPath: "id" });+ parts.createIndex("recordingId", "recordingId");+ db.createObjectStore("meta", { keyPath: "key" });+ };+ request.onerror = () => reject(request.error);+ request.onsuccess = () => {+ const db = request.result as FakeConnection;+ const transaction = db.transaction("parts", "readwrite");+ transaction+ .objectStore("parts")+ .put(legacyPart as unknown as Record<string, unknown>);+ transaction.objectStore("meta").put({+ key: "current",+ recordingId: "rec-legacy",+ finalized: false,+ startedAt: 1,+ });+ transaction.oncomplete = () => {+ db.close();+ resolve();+ };+ transaction.onabort = () => reject(transaction.error);+ };+ });+}++describe("recordingStore", () => {+ beforeEach(() => {+ fakeIndexedDB = new FakeIndexedDB();+ vi.stubGlobal("indexedDB", fakeIndexedDB);+ });++ afterEach(() => {+ vi.unstubAllGlobals();+ });++ it("returns parts in recording order however they were written", async () => {+ await savePart(part({ partIndex: 2 }));+ await savePart(part({ partIndex: 0 }));+ await savePart(part({ partIndex: 1 }));+ await savePart(part({ recordingId: "rec-2", partIndex: 0 }));++ const parts = await getParts("rec-1");++ expect(parts.map((p) => p.partIndex)).toEqual([0, 1, 2]);+ // Scoped by index, so a second tab's take never bleeds in.+ expect(parts.every((p) => p.recordingId === "rec-1")).toBe(true);+ });++ it("marks a stored part uploaded", async () => {+ await savePart(part({ partIndex: 0 }));++ await markPartUploaded(partId("rec-1", 0));++ const [stored] = await getParts("rec-1");+ expect(stored.uploaded).toBe(true);+ expect(stored.blob).toBeInstanceOf(Blob);+ });++ // The upload queue calls this for parts it may have replayed from a+ // previous session — writing a row back for an id that is gone would+ // resurrect a part with no blob.+ it("ignores an unknown id rather than writing a phantom part", async () => {+ await savePart(part({ partIndex: 0 }));++ await markPartUploaded(partId("rec-1", 7));++ expect(await getParts("rec-1")).toHaveLength(1);+ });++ it("offers the newest unfinalized take for recovery", async () => {+ await saveMeta(meta({ recordingId: "old", startedAt: 100 }));+ await saveMeta(meta({ recordingId: "new", startedAt: 300 }));++ expect((await getMeta())?.recordingId).toBe("new");+ });++ // Two tabs: one finished its dump, the other is still going. The+ // unfinished one is the only one worth offering back, even though the+ // finished one is newer.+ it("prefers an older unfinalized take over a newer finalized one", async () => {+ await saveMeta(meta({ recordingId: "live", startedAt: 100 }));+ await saveMeta(+ meta({ recordingId: "done", startedAt: 300, finalized: true }),+ );++ expect((await getMeta())?.recordingId).toBe("live");+ });++ it("falls back to the newest take when every take is finalized", async () => {+ await saveMeta(+ meta({ recordingId: "first", startedAt: 100, finalized: true }),+ );+ await saveMeta(+ meta({ recordingId: "second", startedAt: 300, finalized: true }),+ );++ expect((await getMeta())?.recordingId).toBe("second");+ });++ it("returns null when nothing was ever recorded", async () => {+ expect(await getMeta()).toBeNull();+ expect(await getMetaById("rec-1")).toBeNull();+ });++ // Finalize marks the take it just submitted, not "the newest" — which+ // in a second tab is somebody else's recording.+ it("looks a take up by id without regard to which is newest", async () => {+ await saveMeta(meta({ recordingId: "mine", startedAt: 100 }));+ await saveMeta(meta({ recordingId: "theirs", startedAt: 300 }));++ const mine = await getMetaById("mine");+ expect(mine?.recordingId).toBe("mine");+ expect(mine?.finalized).toBe(false);++ await saveMeta({ ...mine!, finalized: true });+ expect((await getMetaById("mine"))?.finalized).toBe(true);+ // The other tab's take is untouched, so it can still be recovered.+ expect((await getMetaById("theirs"))?.finalized).toBe(false);+ });++ it("clears only the recording it was asked to clear", async () => {+ await savePart(part({ recordingId: "done", partIndex: 0 }));+ await savePart(part({ recordingId: "done", partIndex: 1 }));+ await savePart(part({ recordingId: "other", partIndex: 0 }));+ await saveMeta(meta({ recordingId: "done" }));+ await saveMeta(meta({ recordingId: "other" }));++ await clearRecording("done");++ expect(await getParts("done")).toEqual([]);+ expect(await getMetaById("done")).toBeNull();+ expect(await getParts("other")).toHaveLength(1);+ expect((await getMetaById("other"))?.recordingId).toBe("other");+ });++ it("closes every connection it opens", async () => {+ await savePart(part({ partIndex: 0 }));+ await saveMeta(meta());+ await getParts("rec-1");+ await getMeta();+ await clearRecording("rec-1");++ expect(fakeIndexedDB.openConnections.size).toBe(0);+ });++ it("rejects instead of hanging when IndexedDB is missing", async () => {+ vi.unstubAllGlobals();+ expect(isIndexedDBAvailable()).toBe(false);++ await expect(savePart(part())).rejects.toBeInstanceOf(Error);+ });++ it("rejects and closes the connection when a write aborts", async () => {+ fakeIndexedDB.failWrites = true;++ await expect(savePart(part())).rejects.toThrow("QuotaExceededError");+ expect(fakeIndexedDB.openConnections.size).toBe(0);+ });++ // The zero-loss guarantee: reporting a chunk as persisted on request+ // success would call a write durable that the commit then threw away.+ it("rejects when the commit fails even though the write succeeded", async () => {+ fakeIndexedDB.failCommit = true;++ await expect(savePart(part())).rejects.toThrow("commit failed");+ expect(fakeIndexedDB.openConnections.size).toBe(0);+ });++ it("rejects rather than hanging when another tab blocks the upgrade", async () => {+ fakeIndexedDB.blockUpgrades = true;++ await expect(savePart(part())).rejects.toThrow(+ "IndexedDB upgrade blocked by another tab",+ );+ });++ // The blocking tab can close after we have given up, handing us a+ // connection nobody holds. Leaving it open would block the next upgrade.+ it("closes a connection that arrives after the blocked open gave up", async () => {+ fakeIndexedDB.blockUpgrades = true;+ await expect(savePart(part())).rejects.toThrow(/blocked/);++ fakeIndexedDB.releaseBlockedUpgrades();+ await Promise.resolve();++ expect(fakeIndexedDB.openConnections.size).toBe(0);+ // And the store is usable again once the blocker is gone.+ await savePart(part({ partIndex: 0 }));+ expect(await getParts("rec-1")).toHaveLength(1);+ });++ // v2 re-keys the meta store by recordingId. The parts are the+ // irreplaceable half and live in a store the upgrade does not touch.+ it("keeps the parts and drops the single-row meta store on upgrade", async () => {+ const legacyPart = part({ recordingId: "rec-legacy", partIndex: 0 });+ await seedLegacyDatabase(legacyPart);++ const parts = await getParts("rec-legacy");+ expect(parts.map((p) => p.id)).toEqual([legacyPart.id]);+ // The v1 "current" row is gone rather than migrated, so recovery does+ // not offer a take keyed by something the new code cannot address.+ expect(await getMeta()).toBeNull();++ await saveMeta(meta({ recordingId: "rec-legacy" }));+ expect((await getMetaById("rec-legacy"))?.recordingId).toBe("rec-legacy");+ });++ it("keys a part by recording and index", () => {+ expect(partId("rec-1", 3)).toBe("rec-1:3");+ });+});diff --git a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/__tests__/useBrainDumpRecorder.test.ts b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/__tests__/useBrainDumpRecorder.test.tsnew file mode 100644index 0000000000..9858f9a08f--- /dev/null+++ b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/__tests__/useBrainDumpRecorder.test.ts@@ -0,0 +1,539 @@+import { act, renderHook } from "@testing-library/react";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";++const savedMeta: Record<string, unknown>[] = [];++const { store, queue } = vi.hoisted(() => ({+ store: {+ savePart: vi.fn(),+ getMeta: vi.fn(),+ getParts: vi.fn(),+ },+ queue: {+ enqueue: vi.fn(),+ flush: vi.fn(),+ reset: vi.fn(),+ },+}));++vi.mock("../recordingStore", () => ({+ savePart: store.savePart,+ saveMeta: vi.fn(async (meta: Record<string, unknown>) => {+ savedMeta.push(meta);+ }),+ getMeta: store.getMeta,+ getParts: store.getParts,+ partId: (recordingId: string, partIndex: number) =>+ `${recordingId}:${partIndex}`,+}));++vi.mock("../useUploadQueue", async () => {+ const actual =+ await vi.importActual<typeof import("../useUploadQueue")>(+ "../useUploadQueue",+ );+ return {+ buildPart: actual.buildPart,+ useUploadQueue: () => ({+ enqueue: queue.enqueue,+ flush: queue.flush,+ reset: queue.reset,+ pendingCount: 0,+ isOffline: false,+ }),+ };+});++vi.mock("@/services/onboarding/brain-dump-analytics", () => ({+ trackBrainDump: vi.fn(),+}));++import { useBrainDumpRecorder } from "../useBrainDumpRecorder";++// Stopping is not instant: `onstop` fires after the encoder drains, and+// the hook then awaits the pending IndexedDB writes. This recorder makes+// that gap explicit so a test can observe what happens across it.+const STOP_GAP_MS = 4000;++class SlowStoppingMediaRecorder {+ static isTypeSupported() {+ return true;+ }+ state: "inactive" | "recording" = "inactive";+ ondataavailable: ((event: { data: Blob }) => void) | null = null;+ onstop: (() => void) | null = null;++ start() {+ this.state = "recording";+ }++ stop() {+ this.state = "inactive";+ // The clock keeps running while the encoder finishes.+ vi.advanceTimersByTime(STOP_GAP_MS);+ this.onstop?.();+ }+}++// Hands a chunk over on demand, so the chunk → IndexedDB → queue+// ordering can be observed a step at a time.+class ChunkingMediaRecorder {+ static isTypeSupported() {+ return true;+ }+ static latest: ChunkingMediaRecorder | null = null;+ state: "inactive" | "recording" = "inactive";+ ondataavailable: ((event: { data: Blob }) => void) | null = null;+ onstop: (() => void) | null = null;++ constructor() {+ ChunkingMediaRecorder.latest = this;+ }++ start() {+ this.state = "recording";+ }++ stop() {+ this.state = "inactive";+ this.onstop?.();+ }++ emit(blob: Blob) {+ this.ondataavailable?.({ data: blob });+ }+}++// A room whose loudness the test can change mid-take. Waveform samples+// sit either side of the 128 midpoint; `peak` is the distance from it.+function stubAudioContext(room: { peak: number }) {+ vi.stubGlobal(+ "AudioContext",+ class {+ close = vi.fn().mockResolvedValue(undefined);+ createAnalyser() {+ return {+ fftSize: 0,+ frequencyBinCount: 8,+ getByteTimeDomainData(samples: Uint8Array) {+ samples.fill(128);+ samples[0] = 128 + room.peak;+ },+ };+ }+ createMediaStreamSource() {+ return { connect: () => undefined };+ }+ },+ );+}++function stubGetUserMedia(getUserMedia: ReturnType<typeof vi.fn>) {+ vi.stubGlobal("navigator", { ...navigator, mediaDevices: { getUserMedia } });+}++describe("useBrainDumpRecorder", () => {+ beforeEach(() => {+ savedMeta.length = 0;+ vi.clearAllMocks();+ store.savePart.mockResolvedValue(undefined);+ store.getMeta.mockResolvedValue(null);+ store.getParts.mockResolvedValue([]);+ queue.flush.mockResolvedValue(true);+ vi.useFakeTimers();+ vi.stubGlobal("MediaRecorder", SlowStoppingMediaRecorder);+ vi.stubGlobal("navigator", {+ ...navigator,+ mediaDevices: {+ getUserMedia: vi.fn().mockResolvedValue({ getTracks: () => [] }),+ },+ });+ });++ afterEach(() => {+ vi.useRealTimers();+ vi.unstubAllGlobals();+ });++ // The regression: the silence nudge keyed off `isSavedLocally`, which+ // flips as soon as the first chunk is persisted. MediaRecorder emits+ // one every timeslice whether or not anybody spoke, so by the nudge's+ // own 5s threshold the flag was always already true and the nudge+ // could never appear.+ it("does not report speech from a silent room", async () => {+ const { result } = renderHook(() => useBrainDumpRecorder());++ await act(async () => {+ await result.current.start();+ });+ await act(async () => {+ vi.advanceTimersByTime(10_000);+ });++ // Well past the 5s nudge threshold, and still silent — which is+ // exactly when the nudge should be showing.+ expect(result.current.elapsedSeconds).toBeGreaterThan(5);+ expect(result.current.hasSpoken).toBe(false);+ });++ // The regression: `stop()` reported `elapsedSeconds` straight off React+ // state, which is the value from the caller's own render. Every second+ // spent stopping was missing from it. The backend splits recordings+ // over 20 minutes on this number, so under-reporting it means a long+ // dump silently skips splitting and fails to transcribe.+ it("reports the duration including the time spent stopping", async () => {+ const { result } = renderHook(() => useBrainDumpRecorder());++ await act(async () => {+ await result.current.start();+ });++ const RECORDED_MS = 60_000;+ await act(async () => {+ vi.advanceTimersByTime(RECORDED_MS);+ });++ let reported = 0;+ await act(async () => {+ reported = await result.current.stop();+ });++ const expected = (RECORDED_MS + STOP_GAP_MS) / 1000;+ expect(reported).toBeCloseTo(expected, 1);+ expect(reported).toBeGreaterThanOrEqual(RECORDED_MS / 1000);++ // The same figure is what crash recovery replays from.+ const lastMeta = savedMeta.at(-1);+ expect(lastMeta?.durationSecs).toBeCloseTo(expected, 1);+ });++ // The regression: `start()` awaits the permission prompt, and the orb+ // stays tappable across it. A second tap reset `partIndexRef` under the+ // first take, so both recorders wrote into one recording id with+ // interleaved indices (0,2,4… and 1,3,5…) — the server's contiguity+ // check rejects that — and the first stream's tracks were never+ // stopped, leaving the mic indicator lit.+ it("ignores a second tap while the permission prompt is still open", async () => {+ vi.stubGlobal("MediaRecorder", ChunkingMediaRecorder);+ let grantPermission!: (stream: unknown) => void;+ const getUserMedia = vi.fn().mockReturnValue(+ new Promise((resolve) => {+ grantPermission = resolve;+ }),+ );+ stubGetUserMedia(getUserMedia);+ const { result } = renderHook(() => useBrainDumpRecorder());++ const first = result.current.start();+ const second = result.current.start();+ let firstStarted: boolean | undefined;+ let secondStarted: boolean | undefined;+ await act(async () => {+ grantPermission({ getTracks: () => [] });+ firstStarted = await first;+ secondStarted = await second;+ });++ expect(firstStarted).toBe(true);+ expect(secondStarted).toBe(false);+ expect(getUserMedia).toHaveBeenCalledTimes(1);++ // One take, one contiguous run of part indices.+ await act(async () => {+ ChunkingMediaRecorder.latest?.emit(new Blob(["a"]));+ ChunkingMediaRecorder.latest?.emit(new Blob(["b"]));+ await Promise.resolve();+ });+ expect(store.savePart.mock.calls.map(([part]) => part.partIndex)).toEqual([+ 0, 1,+ ]);+ const recordingIds = new Set(+ store.savePart.mock.calls.map(([part]) => part.recordingId),+ );+ expect(recordingIds.size).toBe(1);+ });++ it("reports a denial so the step can offer the typed composer", async () => {+ stubGetUserMedia(+ vi.fn().mockRejectedValue(new DOMException("no", "NotAllowedError")),+ );+ const { result } = renderHook(() => useBrainDumpRecorder());++ let started: boolean | undefined;+ await act(async () => {+ started = await result.current.start();+ });++ expect(started).toBe(false);+ expect(result.current.permissionDenied).toBe(true);+ expect(result.current.phase).toBe("idle");+ });++ // A missing microphone is not a refusal, and treating it as one would+ // send the user to the typed fallback with no way back.+ it("does not call a missing microphone a denial", async () => {+ stubGetUserMedia(+ vi.fn().mockRejectedValue(new DOMException("gone", "NotFoundError")),+ );+ const { result } = renderHook(() => useBrainDumpRecorder());++ let started: boolean | undefined;+ await act(async () => {+ started = await result.current.start();+ });++ expect(started).toBe(false);+ expect(result.current.permissionDenied).toBe(false);+ });++ // The ordering is the contract: a chunk is durable before the network+ // is ever offered it.+ it("persists a chunk before handing it to the upload queue", async () => {+ vi.stubGlobal("MediaRecorder", ChunkingMediaRecorder);+ const { result } = renderHook(() => useBrainDumpRecorder());++ await act(async () => {+ await result.current.start();+ });+ await act(async () => {+ ChunkingMediaRecorder.latest?.emit(new Blob(["chunk"]));+ await Promise.resolve();+ });++ const saved = store.savePart.mock.calls[0][0];+ expect(saved).toMatchObject({ partIndex: 0, uploaded: false });+ expect(saved.recordingId).toBe(result.current.recordingId);+ expect(queue.enqueue).toHaveBeenCalledWith(saved);+ expect(store.savePart.mock.invocationCallOrder[0]).toBeLessThan(+ queue.enqueue.mock.invocationCallOrder[0],+ );+ expect(result.current.isSavedLocally).toBe(true);+ });++ it("ignores an empty chunk", async () => {+ vi.stubGlobal("MediaRecorder", ChunkingMediaRecorder);+ const { result } = renderHook(() => useBrainDumpRecorder());++ await act(async () => {+ await result.current.start();+ });+ await act(async () => {+ ChunkingMediaRecorder.latest?.emit(new Blob([]));+ await Promise.resolve();+ });++ expect(store.savePart).not.toHaveBeenCalled();+ expect(queue.enqueue).not.toHaveBeenCalled();+ });++ // Private-mode Safari and hardened profiles have no usable IndexedDB.+ // The chunk still goes out — the reassurance on screen just stays off+ // rather than lying about a backup that does not exist.+ it("still uploads a chunk it could not persist, without claiming it saved", async () => {+ vi.stubGlobal("MediaRecorder", ChunkingMediaRecorder);+ store.savePart.mockRejectedValue(new Error("no indexeddb"));+ const { result } = renderHook(() => useBrainDumpRecorder());++ await act(async () => {+ await result.current.start();+ });+ await act(async () => {+ ChunkingMediaRecorder.latest?.emit(new Blob(["chunk"]));+ await Promise.resolve();+ });++ expect(queue.enqueue).toHaveBeenCalledTimes(1);+ expect(result.current.isSavedLocally).toBe(false);+ });++ it("notices speech and never takes it back during a pause", async () => {+ const room = { peak: 40 };+ stubAudioContext(room);+ const { result } = renderHook(() => useBrainDumpRecorder());++ await act(async () => {+ await result.current.start();+ });+ await act(async () => {+ vi.advanceTimersByTime(1_000);+ });+ expect(result.current.hasSpoken).toBe(true);++ // One-way: the nudge is for someone who has not started, so a quiet+ // moment must not bring it back.+ room.peak = 0;+ await act(async () => {+ vi.advanceTimersByTime(10_000);+ });+ expect(result.current.hasSpoken).toBe(true);+ });++ it("treats room noise below the threshold as silence", async () => {+ stubAudioContext({ peak: 5 });+ const { result } = renderHook(() => useBrainDumpRecorder());++ await act(async () => {+ await result.current.start();+ });+ await act(async () => {+ vi.advanceTimersByTime(10_000);+ });++ expect(result.current.hasSpoken).toBe(false);+ });++ // 30 minutes stops the recorder but keeps every second captured.+ it("stops itself at the hard stop", async () => {+ vi.stubGlobal("MediaRecorder", ChunkingMediaRecorder);+ const { result } = renderHook(() => useBrainDumpRecorder());++ await act(async () => {+ await result.current.start();+ });+ await act(async () => {+ vi.advanceTimersByTime(1_800_000);+ });++ expect(result.current.phase).toBe("stopped");+ expect(result.current.getElapsedSeconds()).toBeGreaterThanOrEqual(1800);+ expect(savedMeta.at(-1)?.durationSecs).toBeGreaterThanOrEqual(1800);+ // The step submits on this, not on the phase: a restart stops the+ // recorder too, and that take is thrown away rather than sent.+ expect(result.current.hitTimeLimit).toBe(true);+ });++ it("does not call an ordinary stop a time limit", async () => {+ const { result } = renderHook(() => useBrainDumpRecorder());++ await act(async () => {+ await result.current.start();+ });+ await act(async () => {+ vi.advanceTimersByTime(60_000);+ await result.current.stop();+ });++ expect(result.current.phase).toBe("stopped");+ expect(result.current.hitTimeLimit).toBe(false);+ });++ // The regression: the meta row was written with `durationSecs: 0` at+ // start and only refreshed by `stop()`. A crash or a refresh — the exact+ // case recovery exists for — never gets there, so the prompt offered+ // back "the 0:00 you already recorded" and finalize submitted 0, which+ // skips the backend's splitting of long takes.+ it("keeps the stored duration current while the take runs", async () => {+ const { result } = renderHook(() => useBrainDumpRecorder());++ await act(async () => {+ await result.current.start();+ });+ expect(savedMeta.at(-1)?.durationSecs).toBe(0);++ await act(async () => {+ vi.advanceTimersByTime(60_000);+ await Promise.resolve();+ });++ const stored = savedMeta.at(-1);+ expect(stored?.durationSecs).toBeGreaterThanOrEqual(55);+ expect(stored?.finalized).toBe(false);+ expect(stored?.recordingId).toBe(result.current.recordingId);+ });++ it("reports the known duration when there is nothing to stop", async () => {+ const { result } = renderHook(() => useBrainDumpRecorder());++ let reported: number | undefined;+ await act(async () => {+ reported = await result.current.stop();+ });++ expect(reported).toBe(0);+ expect(result.current.phase).toBe("idle");+ });++ describe("recovery", () => {+ it("offers an unfinalized take back", async () => {+ const meta = {+ recordingId: "rec-crashed",+ mimeType: "audio/webm",+ startedAt: 1,+ durationSecs: 42,+ finalized: false,+ };+ store.getMeta.mockResolvedValue(meta);+ const { result } = renderHook(() => useBrainDumpRecorder());++ await expect(result.current.findRecoverable()).resolves.toEqual(meta);+ });++ // A finished take is not something to recover from.+ it("ignores a finalized take", async () => {+ store.getMeta.mockResolvedValue({+ recordingId: "rec-done",+ mimeType: "audio/webm",+ startedAt: 1,+ durationSecs: 42,+ finalized: true,+ });+ const { result } = renderHook(() => useBrainDumpRecorder());++ await expect(result.current.findRecoverable()).resolves.toBeNull();+ });++ it("offers nothing when local storage cannot be read", async () => {+ store.getMeta.mockRejectedValue(new Error("no indexeddb"));+ const { result } = renderHook(() => useBrainDumpRecorder());++ await expect(result.current.findRecoverable()).resolves.toBeNull();+ });++ // After a crash the upload queue is gone with the page, so every part+ // is replayed — including ones already marked uploaded, since the+ // server's buffer may have expired and a missing part 0 is fatal.+ it("replays every stored part when a take is adopted", async () => {+ const parts = [+ { id: "rec-crashed:0", partIndex: 0, uploaded: true },+ { id: "rec-crashed:1", partIndex: 1, uploaded: false },+ ];+ store.getParts.mockResolvedValue(parts);+ const { result } = renderHook(() => useBrainDumpRecorder());++ await act(async () => {+ await result.current.adoptRecovered("rec-crashed", "audio/mp4");+ });++ expect(queue.enqueue.mock.calls.map(([p]) => p)).toEqual(parts);+ expect(result.current.recordingId).toBe("rec-crashed");+ expect(result.current.mimeType).toBe("audio/mp4");+ expect(result.current.phase).toBe("stopped");+ });++ it("replays nothing when the stored parts cannot be read", async () => {+ store.getParts.mockRejectedValue(new Error("no indexeddb"));+ const { result } = renderHook(() => useBrainDumpRecorder());++ let replayed: unknown[] | undefined;+ await act(async () => {+ replayed = await result.current.resendAllParts("rec-crashed");+ });++ expect(replayed).toEqual([]);+ expect(queue.enqueue).not.toHaveBeenCalled();+ });+ });++ it("releases the microphone when the step unmounts", async () => {+ const track = { stop: vi.fn() };+ stubGetUserMedia(vi.fn().mockResolvedValue({ getTracks: () => [track] }));+ const { result, unmount } = renderHook(() => useBrainDumpRecorder());++ await act(async () => {+ await result.current.start();+ });+ unmount();++ expect(track.stop).toHaveBeenCalled();+ });+});diff --git a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/__tests__/useBrainDumpStep.test.ts b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/__tests__/useBrainDumpStep.test.tsnew file mode 100644index 0000000000..5cc4cd7ddf--- /dev/null+++ b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/__tests__/useBrainDumpStep.test.ts@@ -0,0 +1,918 @@+import { act, renderHook, waitFor } from "@testing-library/react";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";+import type { RecordingPart } from "../recordingStore";++// A snapshot per render, exactly like the real hook: mutating+// `recorderState` after a render does NOT reach the handlers already+// closed over — which is why the hook passes ids and durations around by+// argument instead of reading them off the recorder.+const { recorderState } = vi.hoisted(() => ({+ recorderState: {+ phase: "idle" as "idle" | "recording" | "stopping" | "stopped",+ hitTimeLimit: false,+ elapsedSeconds: 0,+ isOffline: false,+ isSavedLocally: false,+ audioStream: null as MediaStream | null,+ hasSpoken: false,+ permissionDenied: false,+ recordingId: null as string | null,+ mimeType: "audio/webm",+ start: vi.fn(),+ stop: vi.fn(),+ flushUploads: vi.fn(),+ resetQueue: vi.fn(),+ findRecoverable: vi.fn(),+ adoptRecovered: vi.fn(),+ resendAllParts: vi.fn(),+ getElapsedSeconds: vi.fn(),+ },+}));++vi.mock("../useBrainDumpRecorder", () => ({+ useBrainDumpRecorder: () => ({ ...recorderState }),+}));++const finalizeBrainDump = vi.fn();+const discardBrainDump = vi.fn();+vi.mock("@/app/api/__generated__/endpoints/brain-dump/brain-dump", () => ({+ finalizeBrainDump: (...args: unknown[]) => finalizeBrainDump(...args),+ discardBrainDump: (...args: unknown[]) => discardBrainDump(...args),+}));++const clearRecording = vi.fn();+const getMetaById = vi.fn();+const getParts = vi.fn();+const saveMeta = vi.fn();+vi.mock("../recordingStore", () => ({+ clearRecording: (...args: unknown[]) => clearRecording(...args),+ getMetaById: (...args: unknown[]) => getMetaById(...args),+ getParts: (...args: unknown[]) => getParts(...args),+ saveMeta: (...args: unknown[]) => saveMeta(...args),+}));++const trackBrainDump = vi.fn();+vi.mock("@/services/onboarding/brain-dump-analytics", () => ({+ trackBrainDump: (...args: unknown[]) => trackBrainDump(...args),+}));++import { useOnboardingWizardStore } from "../../../store";+import { useBrainDumpStep } from "../useBrainDumpStep";++const INTRO_PATH_KEY = "autogpt:onboarding-intro-path";++function completed() {+ return { status: 200, data: { status: "completed" } };+}++function part(index: number, recordingId = "rec-1"): RecordingPart {+ return {+ id: `${recordingId}:${index}`,+ recordingId,+ partIndex: index,+ blob: new Blob(["chunk"], { type: "audio/webm" }),+ savedAt: 1,+ uploaded: false,+ };+}++function deferred<T>() {+ let resolve!: (value: T) => void;+ const promise = new Promise<T>((r) => {+ resolve = r;+ });+ return { promise, resolve };+}++async function renderStep() {+ const view = renderHook(() => useBrainDumpStep());+ // Let the mount-time recovery check settle so every test starts from a+ // screen the hook has finished deciding on.+ await act(async () => {+ await Promise.resolve();+ await Promise.resolve();+ });+ return view;+}++function events() {+ return trackBrainDump.mock.calls.map(([name]) => name);+}++function finalizeBody(callIndex = 0) {+ return finalizeBrainDump.mock.calls[callIndex]?.[0];+}++beforeEach(() => {+ vi.clearAllMocks();+ Object.assign(recorderState, {+ phase: "idle",+ hitTimeLimit: false,+ elapsedSeconds: 0,+ isOffline: false,+ isSavedLocally: false,+ audioStream: null,+ hasSpoken: false,+ permissionDenied: false,+ recordingId: null,+ mimeType: "audio/webm",+ });+ recorderState.start.mockResolvedValue(true);+ recorderState.stop.mockResolvedValue(0);+ recorderState.flushUploads.mockResolvedValue(true);+ recorderState.findRecoverable.mockResolvedValue(null);+ recorderState.adoptRecovered.mockResolvedValue(undefined);+ recorderState.resendAllParts.mockResolvedValue([]);+ recorderState.getElapsedSeconds.mockReturnValue(0);+ finalizeBrainDump.mockResolvedValue(completed());+ discardBrainDump.mockResolvedValue({ status: 200 });+ clearRecording.mockResolvedValue(undefined);+ getMetaById.mockResolvedValue(null);+ getParts.mockResolvedValue([]);+ saveMeta.mockResolvedValue(undefined);+ window.sessionStorage.clear();+ useOnboardingWizardStore.getState().reset();+ useOnboardingWizardStore.getState().setStepBusy(false);+});++afterEach(() => {+ vi.unstubAllGlobals();+ vi.restoreAllMocks();+});++describe("useBrainDumpStep — headline", () => {+ it("uses the name the wizard already collected", async () => {+ useOnboardingWizardStore.getState().setName("Ada");++ const { result } = await renderStep();++ expect(result.current.headline).toBe("What keeps stealing your week, Ada?");+ });+});++describe("useBrainDumpStep — recording", () => {+ it("moves to the recording screen only when the mic actually opened", async () => {+ const { result } = await renderStep();+ expect(result.current.screen).toBe("rest");++ recorderState.start.mockResolvedValue(false);+ await act(async () => {+ await result.current.handleStart();+ });+ expect(result.current.screen).toBe("rest");++ recorderState.start.mockResolvedValue(true);+ await act(async () => {+ await result.current.handleStart();+ });+ expect(result.current.screen).toBe("recording");+ });++ // The nudge is for someone who has not started talking yet, so it keys+ // on whether the mic has heard anything — not on elapsed time alone.+ it("nudges only while recording, past the threshold, and still silent", async () => {+ recorderState.elapsedSeconds = 6;+ const { result, rerender } = await renderStep();++ expect(result.current.showSilenceNudge).toBe(false);++ await act(async () => {+ await result.current.handleStart();+ });+ expect(result.current.showSilenceNudge).toBe(true);++ recorderState.hasSpoken = true;+ rerender();+ expect(result.current.showSilenceNudge).toBe(false);+ });++ it("keeps the nudge away before the threshold", async () => {+ recorderState.elapsedSeconds = 4;+ const { result } = await renderStep();++ await act(async () => {+ await result.current.handleStart();+ });++ expect(result.current.showSilenceNudge).toBe(false);+ });++ it("blocks wizard navigation while the dump is being processed", async () => {+ const inFlight = deferred<ReturnType<typeof completed>>();+ finalizeBrainDump.mockReturnValue(inFlight.promise);+ recorderState.recordingId = "rec-1";+ const { result, unmount } = await renderStep();++ await act(async () => {+ void result.current.handleDone();+ await Promise.resolve();+ });++ expect(result.current.screen).toBe("processing");+ expect(useOnboardingWizardStore.getState().isStepBusy).toBe(true);++ await act(async () => {+ inFlight.resolve(completed());+ await Promise.resolve();+ });+ unmount();++ // Leaving the step must never strand the wizard with Back hidden.+ expect(useOnboardingWizardStore.getState().isStepBusy).toBe(false);+ });+});++describe("useBrainDumpStep — the 30-minute cap", () => {+ // The recorder enforces the cap from the inside. Nothing observed+ // `phase`, so the screen stayed on "recording" with a frozen timer and+ // a closed mic while the user kept talking.+ it("submits the take when the recorder stops itself", async () => {+ recorderState.recordingId = "rec-1";+ recorderState.getElapsedSeconds.mockReturnValue(1800.4);+ const { result, rerender } = await renderStep();++ await act(async () => {+ await result.current.handleStart();+ });+ expect(result.current.screen).toBe("recording");++ recorderState.hitTimeLimit = true;+ await act(async () => {+ rerender();+ });++ await waitFor(() => expect(result.current.reachedTimeLimit).toBe(true));+ expect(finalizeBody()).toMatchObject({+ recording_id: "rec-1",+ duration_secs: 1800.4,+ });+ expect(result.current.screen).not.toBe("recording");+ });++ // A restart stops the recorder too, and the screen stays on "recording"+ // right through it — submitting there would upload the take the user+ // just threw away.+ it("stays quiet while a restart stops the recorder", async () => {+ recorderState.recordingId = "rec-old";+ const stopped = deferred<number>();+ recorderState.stop.mockReturnValue(stopped.promise);+ const { result, rerender } = await renderStep();++ await act(async () => {+ await result.current.handleStart();+ });++ let restarting: Promise<void> | undefined;+ await act(async () => {+ restarting = result.current.handleRestart();+ await Promise.resolve();+ });++ // Mid-restart the recorder is stopped and the screen still says+ // "recording" — the same shape as the hard stop, but this take is+ // being thrown away, not submitted.+ recorderState.phase = "stopped";+ await act(async () => {+ rerender();+ stopped.resolve(0);+ await restarting;+ });++ expect(finalizeBrainDump).not.toHaveBeenCalled();+ expect(result.current.screen).toBe("recording");+ });+});++describe("useBrainDumpStep — finishing a take", () => {+ it("finalizes with the duration reported by stop(), not the last render", async () => {+ // The render's value is stale by however long stopping took, and the+ // backend splits long recordings on this number.+ recorderState.elapsedSeconds = 12;+ recorderState.recordingId = "rec-1";+ recorderState.stop.mockResolvedValue(64.5);+ const { result } = await renderStep();++ await act(async () => {+ await result.current.handleDone();+ });++ expect(finalizeBody()).toEqual({+ recording_id: "rec-1",+ input_mode: "voice",+ duration_secs: 64.5,+ mime_type: "audio/webm",+ });+ expect(trackBrainDump).toHaveBeenCalledWith("brain_dump_completed", {+ duration_secs: 65,+ input_mode: "voice",+ });+ });++ it("marks the take finalized by id and clears it before advancing", async () => {+ recorderState.recordingId = "rec-1";+ getMetaById.mockResolvedValue({+ recordingId: "rec-1",+ mimeType: "audio/webm",+ startedAt: 5,+ durationSecs: 30,+ finalized: false,+ });+ const { result } = await renderStep();++ await act(async () => {+ await result.current.handleDone();+ });++ // By id, not "the newest take" — a second tab may have started one.+ expect(getMetaById).toHaveBeenCalledWith("rec-1");+ expect(saveMeta).toHaveBeenCalledWith(+ expect.objectContaining({ recordingId: "rec-1", finalized: true }),+ );+ expect(clearRecording).toHaveBeenCalledWith("rec-1");+ expect(recorderState.resetQueue).toHaveBeenCalled();+ expect(window.sessionStorage.getItem(INTRO_PATH_KEY)).toBe("A");+ expect(useOnboardingWizardStore.getState().currentStep).toBe(2);+ });++ // The dump is already safe on the server by this point, so a storage+ // error must not strand the user on the loading screen.+ it("advances even when local cleanup fails", async () => {+ recorderState.recordingId = "rec-1";+ getMetaById.mockRejectedValue(new Error("no indexeddb"));+ const { result } = await renderStep();++ await act(async () => {+ await result.current.handleDone();+ });++ expect(window.sessionStorage.getItem(INTRO_PATH_KEY)).toBe("A");+ expect(useOnboardingWizardStore.getState().currentStep).toBe(2);+ });++ it("fails without calling finalize when parts are still unsent", async () => {+ recorderState.recordingId = "rec-1";+ recorderState.flushUploads.mockResolvedValue(false);+ const { result } = await renderStep();++ await act(async () => {+ await result.current.handleDone();+ });++ expect(result.current.screen).toBe("failed");+ expect(finalizeBrainDump).not.toHaveBeenCalled();+ expect(useOnboardingWizardStore.getState().currentStep).toBe(1);+ });++ it("fails without touching the network when there is no recording", async () => {+ recorderState.recordingId = null;+ const { result } = await renderStep();++ await act(async () => {+ await result.current.handleDone();+ });++ expect(result.current.screen).toBe("failed");+ expect(recorderState.flushUploads).not.toHaveBeenCalled();+ expect(finalizeBrainDump).not.toHaveBeenCalled();+ });++ // A 2xx envelope can still carry a failed pipeline. Advancing on it+ // would hand the user a copilot home built from nothing.+ it("treats a 200 carrying status 'failed' as a failure", async () => {+ recorderState.recordingId = "rec-1";+ finalizeBrainDump.mockResolvedValue({+ status: 200,+ data: { status: "failed", error_code: "transcription_error" },+ });+ const { result } = await renderStep();++ await act(async () => {+ await result.current.handleDone();+ });++ expect(result.current.screen).toBe("failed");+ expect(trackBrainDump).toHaveBeenCalledWith("transcription_failed", {+ error_code: "transcription_error",+ });+ expect(window.sessionStorage.getItem(INTRO_PATH_KEY)).toBeNull();+ });++ it("reports the HTTP status as the error code on a non-200", async () => {+ recorderState.recordingId = "rec-1";+ finalizeBrainDump.mockResolvedValue({ status: 500, data: {} });+ const { result } = await renderStep();++ await act(async () => {+ await result.current.handleDone();+ });++ expect(result.current.screen).toBe("failed");+ expect(trackBrainDump).toHaveBeenCalledWith("transcription_failed", {+ error_code: 500,+ });+ });++ it("fails rather than throwing when finalize rejects", async () => {+ recorderState.recordingId = "rec-1";+ finalizeBrainDump.mockRejectedValue(new Error("offline"));+ const { result } = await renderStep();++ await act(async () => {+ await result.current.handleDone();+ });++ expect(result.current.screen).toBe("failed");+ expect(useOnboardingWizardStore.getState().currentStep).toBe(1);+ });+});++describe("useBrainDumpStep — restart", () => {+ it("throws away the old take locally and on the server before listening again", async () => {+ recorderState.recordingId = "rec-old";+ const { result } = await renderStep();++ await act(async () => {+ await result.current.handleRestart();+ });++ expect(recorderState.stop).toHaveBeenCalled();+ expect(recorderState.resetQueue).toHaveBeenCalled();+ expect(clearRecording).toHaveBeenCalledWith("rec-old");+ // Named explicitly: without an id the server drops whatever the row+ // points at, which in a second tab is another take still filling.+ expect(discardBrainDump).toHaveBeenCalledWith({+ recording_id: "rec-old",+ });+ expect(recorderState.start).toHaveBeenCalled();+ expect(events()).toContain("brain_dump_restarted");+ });++ it("survives a failed discard and still starts the new take", async () => {+ recorderState.recordingId = "rec-old";+ clearRecording.mockRejectedValue(new Error("no indexeddb"));+ discardBrainDump.mockRejectedValue(new Error("offline"));+ const { result } = await renderStep();++ await act(async () => {+ await result.current.handleRestart();+ });++ expect(recorderState.start).toHaveBeenCalled();+ expect(result.current.screen).not.toBe("failed");+ });++ it("drops back to rest when the mic does not reopen", async () => {+ recorderState.recordingId = "rec-old";+ recorderState.start.mockResolvedValue(false);+ const { result } = await renderStep();++ await act(async () => {+ await result.current.handleRestart();+ });++ expect(result.current.screen).toBe("rest");+ });++ it("discards nothing when there is no take to throw away", async () => {+ recorderState.recordingId = null;+ const { result } = await renderStep();++ await act(async () => {+ await result.current.handleRestart();+ });++ expect(clearRecording).not.toHaveBeenCalled();+ expect(discardBrainDump).not.toHaveBeenCalled();+ });+});++describe("useBrainDumpStep — retry", () => {+ it("replays the parts and finalizes on the live duration", async () => {+ recorderState.recordingId = "rec-1";+ // The state value on the closure is whatever the last render saw; the+ // live figure is the one the backend needs.+ recorderState.elapsedSeconds = 3;+ recorderState.getElapsedSeconds.mockReturnValue(212.4);+ const { result } = await renderStep();++ await act(async () => {+ await result.current.handleRetry();+ });++ expect(recorderState.resendAllParts).toHaveBeenCalledWith("rec-1");+ expect(finalizeBody()).toMatchObject({+ recording_id: "rec-1",+ duration_secs: 212.4,+ });+ expect(trackBrainDump).toHaveBeenCalledWith("brain_dump_retry", {+ attempt: 1,+ });+ });++ it("counts each retry attempt", async () => {+ recorderState.recordingId = "rec-1";+ finalizeBrainDump.mockRejectedValue(new Error("offline"));+ const { result } = await renderStep();++ await act(async () => {+ await result.current.handleRetry();+ });+ await act(async () => {+ await result.current.handleRetry();+ });++ expect(trackBrainDump).toHaveBeenCalledWith("brain_dump_retry", {+ attempt: 2,+ });+ });++ it("does not replay parts when there is no recording to retry", async () => {+ recorderState.recordingId = null;+ const { result } = await renderStep();++ await act(async () => {+ await result.current.handleRetry();+ });++ expect(recorderState.resendAllParts).not.toHaveBeenCalled();+ expect(result.current.screen).toBe("failed");+ });+});++describe("useBrainDumpStep — typing", () => {+ it("opens the composer when the browser refused the microphone", async () => {+ recorderState.permissionDenied = true;+ const { result } = await renderStep();++ expect(result.current.screen).toBe("typing");+ expect(result.current.isMicBlocked).toBe(true);+ expect(trackBrainDump).toHaveBeenCalledWith("brain_dump_typed_fallback", {+ reason: "permission_denied",+ });+ });++ it("opens and closes the composer by choice", async () => {+ const { result } = await renderStep();++ act(() => {+ result.current.showTyping();+ });+ expect(result.current.screen).toBe("typing");+ expect(trackBrainDump).toHaveBeenCalledWith("brain_dump_typed_fallback", {+ reason: "chose_to_type",+ });++ act(() => {+ result.current.showRecording();+ });+ expect(result.current.screen).toBe("rest");+ expect(result.current.isMicBlocked).toBe(false);+ });++ it("ignores a submit with nothing but whitespace", async () => {+ const { result } = await renderStep();++ act(() => {+ result.current.setTypedText(" ");+ });+ await act(async () => {+ await result.current.handleSubmitTyped();+ });++ expect(finalizeBrainDump).not.toHaveBeenCalled();+ expect(result.current.screen).toBe("rest");+ });++ it("submits trimmed text and advances down the same path as a recording", async () => {+ const { result } = await renderStep();++ act(() => {+ result.current.setTypedText(" invoices every friday ");+ });+ await act(async () => {+ await result.current.handleSubmitTyped();+ });++ expect(finalizeBody()).toEqual({+ recording_id: expect.any(String),+ input_mode: "typed",+ text: "invoices every friday",+ });+ expect(trackBrainDump).toHaveBeenCalledWith("brain_dump_completed", {+ input_mode: "typed",+ chars: "invoices every friday".length,+ });+ expect(window.sessionStorage.getItem(INTRO_PATH_KEY)).toBe("A");+ expect(useOnboardingWizardStore.getState().currentStep).toBe(2);+ });++ it("reuses the recording id when the user typed after recording", async () => {+ recorderState.recordingId = "rec-1";+ const { result } = await renderStep();++ act(() => {+ result.current.setTypedText("notes");+ });+ await act(async () => {+ await result.current.handleSubmitTyped();+ });++ expect(finalizeBody()).toMatchObject({ recording_id: "rec-1" });+ });++ it("fails on a typed dump the pipeline could not process", async () => {+ finalizeBrainDump.mockResolvedValue({+ status: 200,+ data: { status: "failed" },+ });+ const { result } = await renderStep();++ act(() => {+ result.current.setTypedText("notes");+ });+ await act(async () => {+ await result.current.handleSubmitTyped();+ });++ expect(result.current.screen).toBe("failed");+ expect(useOnboardingWizardStore.getState().currentStep).toBe(1);+ });++ it("fails rather than throwing when the typed submit rejects", async () => {+ finalizeBrainDump.mockRejectedValue(new Error("offline"));+ const { result } = await renderStep();++ act(() => {+ result.current.setTypedText("notes");+ });+ await act(async () => {+ await result.current.handleSubmitTyped();+ });++ expect(result.current.screen).toBe("failed");+ });+});++describe("useBrainDumpStep — skip", () => {+ it("records the skip and takes path B", async () => {+ const { result } = await renderStep();++ await act(async () => {+ await result.current.handleSkip();+ });++ expect(finalizeBody()).toEqual({+ recording_id: expect.any(String),+ input_mode: "skipped",+ });+ expect(window.sessionStorage.getItem(INTRO_PATH_KEY)).toBe("B");+ expect(useOnboardingWizardStore.getState().currentStep).toBe(2);+ });++ // Recording for minutes and then skipping used to leave the blobs and+ // an unfinalized meta row behind, so the next visit offered back a take+ // the user had explicitly abandoned — and the server's part buffer sat+ // there until its TTL.+ it("throws away a recorded take it is skipping past", async () => {+ recorderState.recordingId = "rec-1";+ getMetaById.mockResolvedValue({+ recordingId: "rec-1",+ mimeType: "audio/webm",+ startedAt: 5,+ durationSecs: 120,+ finalized: false,+ });+ const { result } = await renderStep();++ await act(async () => {+ await result.current.handleSkip();+ });++ expect(saveMeta).toHaveBeenCalledWith(+ expect.objectContaining({ recordingId: "rec-1", finalized: true }),+ );+ expect(clearRecording).toHaveBeenCalledWith("rec-1");+ expect(discardBrainDump).toHaveBeenCalledWith({ recording_id: "rec-1" });+ expect(recorderState.resetQueue).toHaveBeenCalled();+ expect(window.sessionStorage.getItem(INTRO_PATH_KEY)).toBe("B");+ });++ it("has nothing to throw away when no take was ever recorded", async () => {+ recorderState.recordingId = null;+ const { result } = await renderStep();++ await act(async () => {+ await result.current.handleSkip();+ });++ expect(clearRecording).not.toHaveBeenCalled();+ expect(discardBrainDump).not.toHaveBeenCalled();+ });++ // Both `handleSkip` and the finalize in flight call `nextStep()`, and+ // the second one lands past the last step — a blank screen with Back+ // and Log out hidden, escapable only by refreshing.+ it("ignores a skip while a submit is already in flight", async () => {+ const inFlight = deferred<ReturnType<typeof completed>>();+ finalizeBrainDump.mockReturnValue(inFlight.promise);+ recorderState.recordingId = "rec-1";+ const { result } = await renderStep();++ await act(async () => {+ void result.current.handleDone();+ await Promise.resolve();+ });+ expect(result.current.screen).toBe("processing");++ await act(async () => {+ await result.current.handleSkip();+ });+ expect(finalizeBrainDump).toHaveBeenCalledTimes(1);++ await act(async () => {+ inFlight.resolve(completed());+ await Promise.resolve();+ });++ // One advance, down the path the recording earned.+ expect(useOnboardingWizardStore.getState().currentStep).toBe(2);+ expect(window.sessionStorage.getItem(INTRO_PATH_KEY)).toBe("A");+ });++ // Being unable to say "no thanks" because the network is down would be+ // absurd — path B is the safe default anyway.+ it("lets the user through even when the skip call fails", async () => {+ finalizeBrainDump.mockRejectedValue(new Error("offline"));+ const { result } = await renderStep();++ await act(async () => {+ await result.current.handleSkip();+ });++ expect(window.sessionStorage.getItem(INTRO_PATH_KEY)).toBe("B");+ expect(useOnboardingWizardStore.getState().currentStep).toBe(2);+ });+});++describe("useBrainDumpStep — recovery", () => {+ const recovered = {+ recordingId: "rec-crashed",+ mimeType: "audio/webm",+ durationSecs: 95,+ };++ it("offers an unfinalized take that still has parts on disk", async () => {+ recorderState.findRecoverable.mockResolvedValue(recovered);+ getParts.mockResolvedValue([+ part(0, "rec-crashed"),+ part(1, "rec-crashed"),+ ]);++ const { result } = await renderStep();++ await waitFor(() => expect(result.current.screen).toBe("recovery"));+ expect(result.current.recoverable).toEqual(recovered);+ expect(trackBrainDump).toHaveBeenCalledWith("brain_dump_recovery_shown", {+ parts: 2,+ });+ });++ // Metadata without parts is a take with no audio in it — offering it+ // back would promise something that cannot be uploaded.+ it("stays silent when the stored take has no parts", async () => {+ recorderState.findRecoverable.mockResolvedValue(recovered);+ getParts.mockResolvedValue([]);++ const { result } = await renderStep();++ expect(result.current.screen).toBe("rest");+ expect(result.current.recoverable).toBeNull();+ expect(events()).not.toContain("brain_dump_recovery_shown");+ });++ it("stays silent when the parts cannot be read at all", async () => {+ recorderState.findRecoverable.mockResolvedValue(recovered);+ getParts.mockRejectedValue(new Error("no indexeddb"));++ const { result } = await renderStep();++ expect(result.current.screen).toBe("rest");+ expect(result.current.recoverable).toBeNull();+ });++ // The recorder writes the adopted id to a ref, so a handler reading+ // `recorder.recordingId` would still see this render's null.+ it("resumes with the recovered id and its stored duration", async () => {+ recorderState.findRecoverable.mockResolvedValue(recovered);+ getParts.mockResolvedValue([part(0, "rec-crashed")]);+ const { result } = await renderStep();+ await waitFor(() => expect(result.current.screen).toBe("recovery"));++ await act(async () => {+ await result.current.handleResumeRecovered();+ });++ expect(recorderState.adoptRecovered).toHaveBeenCalledWith(+ "rec-crashed",+ "audio/webm",+ );+ expect(finalizeBody()).toMatchObject({+ recording_id: "rec-crashed",+ duration_secs: 95,+ });+ expect(window.sessionStorage.getItem(INTRO_PATH_KEY)).toBe("A");+ expect(events()).toContain("brain_dump_recovery_used");+ });++ it("releases the server buffer when the take is abandoned", async () => {+ recorderState.findRecoverable.mockResolvedValue(recovered);+ getParts.mockResolvedValue([part(0, "rec-crashed")]);+ const { result } = await renderStep();+ await waitFor(() => expect(result.current.screen).toBe("recovery"));++ await act(async () => {+ await result.current.handleDiscardRecovered();+ });++ expect(clearRecording).toHaveBeenCalledWith("rec-crashed");+ // Otherwise those chunks sit in Redis until their TTL for a recording+ // nobody will ever finalize.+ expect(discardBrainDump).toHaveBeenCalledWith({+ recording_id: "rec-crashed",+ });+ expect(result.current.recoverable).toBeNull();+ expect(result.current.screen).toBe("rest");+ });++ it("abandons the take and opens the composer when the user types instead", async () => {+ recorderState.findRecoverable.mockResolvedValue(recovered);+ getParts.mockResolvedValue([part(0, "rec-crashed")]);+ const { result } = await renderStep();+ await waitFor(() => expect(result.current.screen).toBe("recovery"));++ await act(async () => {+ await result.current.handleTypeInsteadOfRecovered();+ });++ expect(discardBrainDump).toHaveBeenCalledWith({+ recording_id: "rec-crashed",+ });+ expect(result.current.screen).toBe("typing");+ expect(result.current.recoverable).toBeNull();+ });+});++describe("useBrainDumpStep — download", () => {+ it("stitches the stored parts into one file named after the recording", async () => {+ const createObjectURL = vi.fn().mockReturnValue("blob:dump");+ const revokeObjectURL = vi.fn();+ vi.stubGlobal("URL", { ...URL, createObjectURL, revokeObjectURL });+ const click = vi+ .spyOn(HTMLAnchorElement.prototype, "click")+ .mockImplementation(() => undefined);+ recorderState.recordingId = "rec-1";+ recorderState.mimeType = "audio/mp4";+ getParts.mockResolvedValue([part(0), part(1)]);+ const { result } = await renderStep();++ await act(async () => {+ await result.current.handleDownloadRecording();+ });++ expect(click).toHaveBeenCalledTimes(1);+ const blob = createObjectURL.mock.calls[0][0] as Blob;+ expect(blob.type).toBe("audio/mp4");+ // Both chunks, in order, rather than just the last one.+ expect(blob.size).toBe(part(0).blob.size + part(1).blob.size);+ // Revoked in the same turn so the object URL does not leak.+ expect(revokeObjectURL).toHaveBeenCalledWith("blob:dump");+ expect(events()).toContain("brain_dump_download");+ });++ it("downloads nothing when there is nothing stored", async () => {+ const click = vi+ .spyOn(HTMLAnchorElement.prototype, "click")+ .mockImplementation(() => undefined);+ recorderState.recordingId = "rec-1";+ getParts.mockResolvedValue([]);+ const { result } = await renderStep();++ await act(async () => {+ await result.current.handleDownloadRecording();+ });++ expect(click).not.toHaveBeenCalled();+ });++ it("downloads nothing when no take was ever started", async () => {+ recorderState.recordingId = null;+ const { result } = await renderStep();++ await act(async () => {+ await result.current.handleDownloadRecording();+ });++ expect(getParts).not.toHaveBeenCalled();+ expect(events()).not.toContain("brain_dump_download");+ });+});diff --git a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/__tests__/useUploadQueue.test.ts b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/__tests__/useUploadQueue.test.tsnew file mode 100644index 0000000000..4bb1c2f9da--- /dev/null+++ b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/__tests__/useUploadQueue.test.ts@@ -0,0 +1,398 @@+import { ApiError } from "@/lib/autogpt-server-api/helpers";+import { act, renderHook } from "@testing-library/react";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";+import type { RecordingPart } from "../recordingStore";++const uploadBrainDumpPart = vi.fn();+const markPartUploaded = vi.fn();++vi.mock("@/app/api/__generated__/endpoints/brain-dump/brain-dump", () => ({+ uploadBrainDumpPart: (...args: unknown[]) => uploadBrainDumpPart(...args),+}));++vi.mock("../recordingStore", () => ({+ markPartUploaded: (...args: unknown[]) => markPartUploaded(...args),+ partId: (recordingId: string, partIndex: number) =>+ `${recordingId}:${partIndex}`,+}));++import { buildPart, useUploadQueue } from "../useUploadQueue";++function part(index: number, recordingId = "rec-1"): RecordingPart {+ return buildPart(recordingId, index, new Blob(["x"]) as Blob);+}++// Resolves only when the test says so, standing in for an upload that is+// still on the wire when the user presses "I'm done".+function deferred() {+ let resolve!: () => void;+ const promise = new Promise<void>((r) => {+ resolve = r;+ });+ return { promise, resolve };+}++describe("useUploadQueue", () => {+ beforeEach(() => {+ uploadBrainDumpPart.mockReset();+ markPartUploaded.mockReset();+ markPartUploaded.mockResolvedValue(undefined);+ });++ afterEach(() => {+ vi.useRealTimers();+ vi.unstubAllGlobals();+ });++ it("waits for an in-flight upload instead of reporting failure", async () => {+ // The regression: pressing "I'm done" while the last chunk is still+ // uploading used to make flush() await a no-op, see a non-empty queue+ // and report failure — so finalize was never called at all.+ const inFlight = deferred();+ uploadBrainDumpPart.mockReturnValueOnce(inFlight.promise);++ const { result } = renderHook(() => useUploadQueue());++ act(() => {+ result.current.enqueue(part(0));+ });++ let flushed: boolean | undefined;+ act(() => {+ void result.current.flush().then((value) => {+ flushed = value;+ });+ });++ // Let every pending microtask settle while the upload is still in+ // flight. This is the assertion that fails against the old code: a+ // no-op drain let all of flush()'s passes complete here and resolve+ // to false, so "I'm done" reported failure without calling finalize.+ await act(async () => {+ await new Promise((resolve) => setTimeout(resolve, 0));+ });+ expect(flushed).toBeUndefined();++ await act(async () => {+ inFlight.resolve();+ await Promise.resolve();+ });++ expect(flushed).toBe(true);+ expect(uploadBrainDumpPart).toHaveBeenCalledTimes(1);+ });++ it("uploads every queued part in order", async () => {+ const seen: number[] = [];+ uploadBrainDumpPart.mockImplementation(+ async ({ part_index }: { part_index: number }) => {+ seen.push(part_index);+ },+ );++ const { result } = renderHook(() => useUploadQueue());++ await act(async () => {+ result.current.enqueue(part(0));+ result.current.enqueue(part(1));+ result.current.enqueue(part(2));+ await result.current.flush();+ });++ expect(seen).toEqual([0, 1, 2]);+ });++ it("reports failure when a part cannot be uploaded", async () => {+ uploadBrainDumpPart.mockRejectedValue(new Error("offline"));+ vi.useFakeTimers();++ const { result } = renderHook(() => useUploadQueue());++ let flushed: boolean | undefined;+ await act(async () => {+ result.current.enqueue(part(0));+ const pending = result.current.flush().then((value) => {+ flushed = value;+ });+ await vi.runAllTimersAsync();+ await pending;+ });++ expect(flushed).toBe(false);+ vi.useRealTimers();+ });++ it("keeps the failed part at the head so nothing overtakes it", async () => {+ const attempted: number[] = [];+ uploadBrainDumpPart.mockImplementation(+ async ({ part_index }: { part_index: number }) => {+ attempted.push(part_index);+ throw new Error("offline");+ },+ );+ vi.useFakeTimers();++ const { result } = renderHook(() => useUploadQueue());++ await act(async () => {+ result.current.enqueue(part(0));+ result.current.enqueue(part(1));+ const pending = result.current.flush();+ await vi.runAllTimersAsync();+ await pending;+ });++ expect(new Set(attempted)).toEqual(new Set([0]));+ vi.useRealTimers();+ });++ // A part the server rejects on its merits fails identically every time.+ // Retrying it burned 12s per pass and then again on every 3-second+ // chunk that followed, holding the queue — and its blobs — at the head+ // for the rest of the take.+ it.each([413, 422, 401])(+ "gives up immediately on a %s rather than working the retry ladder",+ async (status) => {+ uploadBrainDumpPart.mockRejectedValue(+ new ApiError("nope", status, undefined),+ );++ const { result } = renderHook(() => useUploadQueue());++ let flushed: boolean | undefined;+ await act(async () => {+ result.current.enqueue(part(0));+ flushed = await result.current.flush();+ });++ expect(flushed).toBe(false);+ // One attempt per flush pass, no back-off waits in between.+ expect(uploadBrainDumpPart).toHaveBeenCalledTimes(3);+ },+ );++ it.each([408, 429, 500])(+ "still retries a %s, which the server may honour next time",+ async (status) => {+ uploadBrainDumpPart.mockRejectedValue(+ new ApiError("later", status, undefined),+ );+ vi.useFakeTimers();++ const { result } = renderHook(() => useUploadQueue());++ await act(async () => {+ result.current.enqueue(part(0));+ const pending = result.current.flush();+ await vi.runAllTimersAsync();+ await pending;+ });++ // Four attempts (one plus the three delays) on the first pass alone.+ expect(uploadBrainDumpPart.mock.calls.length).toBeGreaterThan(3);+ vi.useRealTimers();+ },+ );++ it("counts the parts still waiting to go out", async () => {+ const inFlight = deferred();+ uploadBrainDumpPart.mockReturnValueOnce(inFlight.promise);+ uploadBrainDumpPart.mockResolvedValue(undefined);++ const { result } = renderHook(() => useUploadQueue());++ act(() => {+ result.current.enqueue(part(0));+ result.current.enqueue(part(1));+ });+ expect(result.current.pendingCount).toBe(2);++ await act(async () => {+ inFlight.resolve();+ await result.current.flush();+ });++ expect(result.current.pendingCount).toBe(0);+ });++ it("marks a part uploaded once the server has it", async () => {+ uploadBrainDumpPart.mockResolvedValue(undefined);++ const { result } = renderHook(() => useUploadQueue());++ await act(async () => {+ result.current.enqueue(part(3));+ await result.current.flush();+ });++ expect(markPartUploaded).toHaveBeenCalledWith("rec-1:3");+ });++ // Bookkeeping only, and the part is already on the server. Where+ // IndexedDB is unavailable this rejects — reporting failure for a dump+ // that fully uploaded would send the user to the error screen.+ it("still reports success when the uploaded flag cannot be written", async () => {+ uploadBrainDumpPart.mockResolvedValue(undefined);+ markPartUploaded.mockRejectedValue(new Error("no indexeddb"));++ const { result } = renderHook(() => useUploadQueue());++ let flushed: boolean | undefined;+ await act(async () => {+ result.current.enqueue(part(0));+ flushed = await result.current.flush();+ });++ expect(flushed).toBe(true);+ expect(result.current.pendingCount).toBe(0);+ });++ it("starts out parked when the browser is already offline", () => {+ vi.stubGlobal("navigator", { onLine: true });+ const online = renderHook(() => useUploadQueue());+ expect(online.result.current.isOffline).toBe(false);++ vi.stubGlobal("navigator", { onLine: false });+ const offline = renderHook(() => useUploadQueue());+ expect(offline.result.current.isOffline).toBe(true);+ });++ it("parks the queue on a dropped connection and replays it on reconnect", async () => {+ uploadBrainDumpPart.mockRejectedValue(new Error("offline"));+ vi.useFakeTimers();++ const { result } = renderHook(() => useUploadQueue());++ let flushed: boolean | undefined;+ await act(async () => {+ result.current.enqueue(part(0));+ const pending = result.current.flush().then((value) => {+ flushed = value;+ });+ await vi.runAllTimersAsync();+ await pending;+ });+ expect(flushed).toBe(false);+ expect(result.current.pendingCount).toBe(1);++ act(() => {+ window.dispatchEvent(new Event("offline"));+ });+ expect(result.current.isOffline).toBe(true);++ // The part is still at the head, so coming back online replays it+ // rather than leaving a hole in the audio.+ const attemptsWhileOffline = uploadBrainDumpPart.mock.calls.length;+ uploadBrainDumpPart.mockResolvedValue(undefined);+ await act(async () => {+ window.dispatchEvent(new Event("online"));+ await vi.runAllTimersAsync();+ });++ expect(result.current.isOffline).toBe(false);+ expect(result.current.pendingCount).toBe(0);+ // Exactly one more attempt: the reconnect resumes the queue rather+ // than restarting the retry ladder from scratch.+ expect(uploadBrainDumpPart).toHaveBeenCalledTimes(attemptsWhileOffline + 1);+ expect(uploadBrainDumpPart).toHaveBeenLastCalledWith(+ expect.objectContaining({ part_index: 0, recording_id: "rec-1" }),+ );+ });++ it("drops the queue on reset so a restarted take sends nothing old", async () => {+ uploadBrainDumpPart.mockRejectedValue(new Error("offline"));+ vi.useFakeTimers();++ const { result } = renderHook(() => useUploadQueue());++ await act(async () => {+ result.current.enqueue(part(0));+ const pending = result.current.flush();+ await vi.runAllTimersAsync();+ await pending;+ });+ expect(result.current.pendingCount).toBe(1);++ act(() => {+ result.current.reset();+ });+ expect(result.current.pendingCount).toBe(0);++ uploadBrainDumpPart.mockClear();+ let flushed: boolean | undefined;+ await act(async () => {+ flushed = await result.current.flush();+ });++ expect(flushed).toBe(true);+ expect(uploadBrainDumpPart).not.toHaveBeenCalled();+ });++ // The upload of the old take's last chunk is usually still on the wire+ // when restart swaps the queue. Removing "the head" once it lands used to+ // take the new take's part 0 with it — a recording with a hole at its+ // start, which cannot be decoded at all.+ it("keeps the restarted take's first part when the discarded take's upload lands late", async () => {+ const inFlight = deferred();+ uploadBrainDumpPart.mockReturnValueOnce(inFlight.promise);+ uploadBrainDumpPart.mockResolvedValue(undefined);++ const { result } = renderHook(() => useUploadQueue());++ act(() => {+ result.current.enqueue(part(0));+ });++ act(() => {+ result.current.reset();+ });++ act(() => {+ result.current.enqueue(part(0, "rec-2"));+ });++ await act(async () => {+ inFlight.resolve();+ await new Promise((resolve) => setTimeout(resolve, 0));+ });++ let flushed: boolean | undefined;+ await act(async () => {+ flushed = await result.current.flush();+ });++ expect(flushed).toBe(true);+ expect(uploadBrainDumpPart).toHaveBeenCalledWith(+ expect.objectContaining({ recording_id: "rec-2", part_index: 0 }),+ );+ expect(markPartUploaded).not.toHaveBeenCalledWith("rec-1:0");+ expect(result.current.pendingCount).toBe(0);+ });++ it("reports failure when the take it was flushing gets discarded", async () => {+ const inFlight = deferred();+ uploadBrainDumpPart.mockReturnValueOnce(inFlight.promise);+ uploadBrainDumpPart.mockResolvedValue(undefined);++ const { result } = renderHook(() => useUploadQueue());++ let flushed: boolean | undefined;+ act(() => {+ result.current.enqueue(part(0));+ void result.current.flush().then((value) => {+ flushed = value;+ });+ });++ act(() => {+ result.current.reset();+ });++ await act(async () => {+ inFlight.resolve();+ await new Promise((resolve) => setTimeout(resolve, 0));+ });++ expect(flushed).toBe(false);+ });+});diff --git a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/components/ElapsedTime.tsx b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/components/ElapsedTime.tsxnew file mode 100644index 0000000000..212ac7391f--- /dev/null+++ b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/components/ElapsedTime.tsx@@ -0,0 +1,57 @@+"use client";++import { AnimatePresence, motion, useReducedMotion } from "framer-motion";+import { formatElapsed } from "../helpers";++// Each digit is its own slot, so only the digits that actually changed pop:+// the seconds tick every second while the minutes sit still.+export function ElapsedTime({ seconds }: { seconds: number }) {+ const prefersReducedMotion = useReducedMotion();+ const characters = formatElapsed(seconds).split("");++ return (+ <span+ className="flex items-center font-poppins text-[2rem] font-medium tabular-nums leading-[2.5rem] text-zinc-900"+ aria-label={`${seconds} seconds recorded`}+ role="timer"+ >+ {characters.map((character, index) => (+ <span+ key={index}+ className="relative inline-flex justify-center"+ style={{ width: character === ":" ? "0.3em" : "0.62em" }}+ aria-hidden+ >+ <AnimatePresence initial={false} mode="popLayout">+ <motion.span+ key={character}+ initial={+ prefersReducedMotion+ ? { opacity: 0 }+ : { opacity: 0, scale: 0.6, y: 8 }+ }+ animate={+ prefersReducedMotion+ ? { opacity: 1 }+ : { opacity: 1, scale: 1, y: 0 }+ }+ exit={+ prefersReducedMotion+ ? { opacity: 0 }+ : { opacity: 0, scale: 0.6, y: -8 }+ }+ transition={{+ type: "spring",+ stiffness: 520,+ damping: 28,+ mass: 0.6,+ }}+ >+ {character}+ </motion.span>+ </AnimatePresence>+ </span>+ ))}+ </span>+ );+}diff --git a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/components/FailureState.tsx b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/components/FailureState.tsxnew file mode 100644index 0000000000..1aa689041f--- /dev/null+++ b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/components/FailureState.tsx@@ -0,0 +1,31 @@+"use client";++import { Button } from "@/components/atoms/Button/Button";+import { Text } from "@/components/atoms/Text/Text";++interface Props {+ onDownload: () => void;+ onSkip: () => void;+}++// Calm on purpose. The recording is safe in the browser and (once it got+// that far) on the server, so this is an inconvenience, not a loss — the+// copy and the download button both have to make that obvious. Retrying is+// the orb itself, so only the secondary exits live here.+export function FailureState({ onDownload, onSkip }: Props) {+ return (+ <div className="flex flex-col items-center gap-4 text-center">+ <Text variant="lead" className="!text-base !text-zinc-500">+ Your recording is safe. Try again.+ </Text>+ <div className="flex items-center gap-3">+ <Button variant="primary" size="small" onClick={onDownload}>+ Download recording+ </Button>+ <Button variant="ghost" size="small" onClick={onSkip}>+ Continue without it+ </Button>+ </div>+ </div>+ );+}diff --git a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/components/MicButton.tsx b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/components/MicButton.tsxnew file mode 100644index 0000000000..80ea74ccdf--- /dev/null+++ b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/components/MicButton.tsx@@ -0,0 +1,117 @@+"use client";++import { Icon } from "@/components/atoms/Icon/Icon";+import {+ ArrowReloadHorizontalIcon,+ HandIcon,+ Mic01Icon,+} from "@hugeicons/core-free-icons";+import { AnimatePresence, motion, useReducedMotion } from "framer-motion";+import { AudioWaveform } from "@/app/(platform)/copilot/components/ChatInput/components/AudioWaveform";+import { GlassParams } from "@/components/molecules/GlassOrb/GlassSurface";+import { OrbFrame } from "./OrbFrame";++export type OrbScreen = "rest" | "recording" | "processing" | "failed";++const GLYPH_CLASS =+ "text-white/95 drop-shadow-[0_2px_12px_rgba(90,40,180,0.5)]";++const ARIA_LABEL: Record<OrbScreen, string | undefined> = {+ rest: "Start recording",+ recording: "I'm done",+ processing: undefined,+ failed: "Try again",+};++interface Props {+ screen: OrbScreen;+ progress: number;+ audioStream: MediaStream | null;+ glassParams: GlassParams;+ onClick?: () => void;+}++export function MicButton({+ screen,+ progress,+ audioStream,+ glassParams,+ onClick,+}: Props) {+ const prefersReducedMotion = useReducedMotion();++ return (+ <OrbFrame+ glassParams={glassParams}+ progress={screen === "recording" ? progress : undefined}+ isLoading={screen === "processing"}+ onClick={onClick}+ ariaLabel={ARIA_LABEL[screen]}+ >+ {/* Only the glyph inside the orb changes as the step advances — the+ orb, the ring and the button itself stay mounted throughout. */}+ <AnimatePresence mode="wait" initial={false}>+ <motion.span+ key={screen}+ initial={+ prefersReducedMotion+ ? { opacity: 0 }+ : { opacity: 0, scale: 0.72, filter: "blur(6px)" }+ }+ animate={+ prefersReducedMotion+ ? { opacity: 1 }+ : { opacity: 1, scale: 1, filter: "blur(0px)" }+ }+ exit={+ prefersReducedMotion+ ? { opacity: 0 }+ : { opacity: 0, scale: 0.72, filter: "blur(6px)" }+ }+ transition={{ duration: 0.2, ease: [0.32, 0.72, 0, 1] }}+ className="flex items-center justify-center"+ >+ <OrbGlyph screen={screen} audioStream={audioStream} />+ </motion.span>+ </AnimatePresence>+ </OrbFrame>+ );+}++function OrbGlyph({+ screen,+ audioStream,+}: {+ screen: OrbScreen;+ audioStream: MediaStream | null;+}) {+ if (screen === "processing") {+ return <Icon icon={HandIcon} size={56} className={GLYPH_CLASS} />;+ }++ if (screen === "failed") {+ return (+ <Icon+ icon={ArrowReloadHorizontalIcon}+ size={56}+ className={GLYPH_CLASS}+ />+ );+ }++ if (screen === "recording") {+ return (+ <AudioWaveform+ stream={audioStream}+ barCount={9}+ barWidth={4}+ barGap={4}+ barColor="rgba(255,255,255,0.95)"+ minBarHeight={7}+ maxBarHeight={44}+ />+ );+ }++ return <Icon icon={Mic01Icon} size={56} className={GLYPH_CLASS} />;+}diff --git a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/components/OrbFrame.tsx b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/components/OrbFrame.tsxnew file mode 100644index 0000000000..4c28bc019e--- /dev/null+++ b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/components/OrbFrame.tsx@@ -0,0 +1,109 @@+"use client";++import { ReactNode } from "react";+import { GlassOrb } from "@/components/molecules/GlassOrb/GlassOrb";+import { GlassParams } from "@/components/molecules/GlassOrb/GlassSurface";++export const ORB_SIZE = 160;+const STROKE = 6;+const RADIUS = (ORB_SIZE - STROKE) / 2;+const CIRCUMFERENCE = 2 * Math.PI * RADIUS;++// A quarter of the ring, so the gap reads as motion rather than a dotted line.+const LOADER_ARC = CIRCUMFERENCE * 0.25;++interface Props {+ glassParams: GlassParams;+ // Omitted when there is nothing to meter — the arc is not rendered at all.+ progress?: number;+ // Indeterminate: a single arc chasing the ring while work is in flight.+ isLoading?: boolean;+ onClick?: () => void;+ ariaLabel?: string;+ children?: ReactNode;+}++// The orb as it appears everywhere in this step: the same glass ball seated+// in the same neumorphic ring, whether or not it is interactive.+export function OrbFrame({+ glassParams,+ progress,+ isLoading,+ onClick,+ ariaLabel,+ children,+}: Props) {+ const orb = <GlassOrb params={glassParams}>{children}</GlassOrb>;++ return (+ <div className="relative" style={{ width: ORB_SIZE, height: ORB_SIZE }}>+ <div+ className="pointer-events-none absolute rounded-full bg-[#f1f1f4]"+ style={{+ inset: -glassParams.ringWidth,+ boxShadow: `${glassParams.ringDepth}px ${glassParams.ringDepth}px ${glassParams.ringDepth * 2.3}px rgba(166,171,189,${glassParams.ringDark}), -${glassParams.ringDepth}px -${glassParams.ringDepth}px ${glassParams.ringDepth * 2.3}px #ffffff, inset 1px 1px 3px rgba(255,255,255,0.95), inset -1px -1px 3px rgba(166,171,189,${glassParams.ringDark * 0.55})`,+ }}+ />+ {/* A depth meter, not a limit — the ring fills toward three minutes+ and then simply holds, so passing it reads as an achievement+ rather than a warning. */}+ <svg+ className="absolute inset-0 -rotate-90"+ width={ORB_SIZE}+ height={ORB_SIZE}+ aria-hidden+ >+ {progress !== undefined && (+ <circle+ cx={ORB_SIZE / 2}+ cy={ORB_SIZE / 2}+ r={RADIUS}+ fill="none"+ stroke="#C084FC"+ strokeOpacity={0.95}+ strokeWidth={STROKE}+ strokeLinecap="round"+ strokeDasharray={CIRCUMFERENCE}+ strokeDashoffset={CIRCUMFERENCE * (1 - progress)}+ className="transition-[stroke-dashoffset] duration-500 ease-linear [filter:drop-shadow(0_0_6px_rgba(192,132,252,0.6))]"+ />+ )}+ </svg>++ {isLoading && (+ <svg+ className="absolute inset-0 motion-safe:animate-spin"+ width={ORB_SIZE}+ height={ORB_SIZE}+ aria-hidden+ >+ <circle+ cx={ORB_SIZE / 2}+ cy={ORB_SIZE / 2}+ r={RADIUS}+ fill="none"+ stroke="#C084FC"+ strokeOpacity={0.95}+ strokeWidth={STROKE}+ strokeLinecap="round"+ strokeDasharray={`${LOADER_ARC} ${CIRCUMFERENCE - LOADER_ARC}`}+ className="[filter:drop-shadow(0_0_6px_rgba(192,132,252,0.6))]"+ />+ </svg>+ )}++ {/* Always a button, even when inert: swapping the element type would+ tear down the orb and replay its animation on every state change. */}+ <button+ type="button"+ onClick={onClick}+ disabled={!onClick}+ aria-label={ariaLabel}+ aria-hidden={!onClick}+ className="absolute inset-[8px] rounded-full"+ >+ {orb}+ </button>+ </div>+ );+}diff --git a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/components/PrivacyNote.tsx b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/components/PrivacyNote.tsxnew file mode 100644index 0000000000..6d151ecfff--- /dev/null+++ b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/components/PrivacyNote.tsx@@ -0,0 +1,22 @@+"use client";++import { Icon } from "@/components/atoms/Icon/Icon";+import { Text } from "@/components/atoms/Text/Text";+import { LockIcon } from "@hugeicons/core-free-icons";++const PRIVACY_COPY =+ "Private to you · Saved as AutoPilot memory · Downloadable anytime";++export function PrivacyNote() {+ return (+ <div className="fixed inset-x-0 bottom-20 flex items-center justify-center gap-2 px-4">+ <Icon icon={LockIcon} size={14} className="shrink-0 text-zinc-400" />+ <Text+ variant="small"+ className="text-center !text-sm !text-zinc-400 sm:whitespace-nowrap"+ >+ {PRIVACY_COPY}+ </Text>+ </div>+ );+}diff --git a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/components/RecordingStatus.tsx b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/components/RecordingStatus.tsxnew file mode 100644index 0000000000..e27154ebf3--- /dev/null+++ b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/components/RecordingStatus.tsx@@ -0,0 +1,67 @@+"use client";++import { Icon } from "@/components/atoms/Icon/Icon";+import { Text } from "@/components/atoms/Text/Text";+import { cn } from "@/lib/utils";+import { CloudOffIcon } from "@hugeicons/core-free-icons";+import { encouragementAt, SILENCE_NUDGE_COPY } from "../helpers";++interface Props {+ elapsedSeconds: number;+ showSilenceNudge: boolean;+ isOffline: boolean;+ isSavedLocally: boolean;+}++const OFFLINE_COPY = "You're offline — we'll send this when you're back.";+const SAVED_LOCALLY_COPY = "Saved on this device as you talk";++export function RecordingStatus({+ elapsedSeconds,+ showSilenceNudge,+ isOffline,+ isSavedLocally,+}: Props) {+ const encouragement = encouragementAt(elapsedSeconds);++ return (+ <div className="flex flex-col items-center gap-2">+ <Text+ variant="small"+ className={cn(+ "h-5 text-center transition-opacity duration-500",+ encouragement ? "!text-purple-600 opacity-100" : "opacity-0",+ )}+ >+ {encouragement ?? ""}+ </Text>++ {showSilenceNudge && (+ <Text variant="small" className="max-w-sm text-center !text-zinc-500">+ {SILENCE_NUDGE_COPY}+ </Text>+ )}++ {/* A dropped connection is not a lost recording, and saying so is the+ whole point of writing every chunk to the device first. */}+ {isOffline && (+ <div className="flex items-center gap-2 rounded-full bg-zinc-100 px-3 py-1">+ <Icon+ icon={CloudOffIcon}+ size={14}+ className="shrink-0 text-zinc-500"+ />+ <Text variant="small" className="!text-zinc-500">+ {OFFLINE_COPY}+ </Text>+ </div>+ )}++ {isSavedLocally && (+ <Text variant="small" className="text-center !text-zinc-400">+ {SAVED_LOCALLY_COPY}+ </Text>+ )}+ </div>+ );+}diff --git a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/components/RecoveryPrompt.tsx b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/components/RecoveryPrompt.tsxnew file mode 100644index 0000000000..8757788bea--- /dev/null+++ b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/components/RecoveryPrompt.tsx@@ -0,0 +1,44 @@+"use client";++import { Button } from "@/components/atoms/Button/Button";+import { Text } from "@/components/atoms/Text/Text";+import { formatElapsed } from "../helpers";++interface Props {+ durationSecs: number;+ onResume: () => void;+ onDiscard: () => void;+}++// Reached when the tab was closed, refreshed or crashed mid-recording.+// Everything captured is still in IndexedDB, so the only question is+// whether the user wants it.+export function RecoveryPrompt({ durationSecs, onResume, onDiscard }: Props) {+ return (+ <div className="flex w-full max-w-md flex-col items-center gap-6 text-center">+ <Text+ variant="h3"+ className="!text-[1.125rem] !leading-[1.625rem] md:!text-[1.25rem] md:!leading-[1.75rem]"+ >+ Pick up where you left off?+ </Text>+ <Text variant="lead" className="!text-base !text-zinc-500">+ We kept the{" "}+ <span className="text-purple-500">{formatElapsed(durationSecs)}</span>{" "}+ you already recorded.+ </Text>+ <div className="flex w-full flex-col items-center gap-3">+ <Button onClick={onResume} className="w-full max-w-xs">+ Use that recording+ </Button>+ <button+ type="button"+ onClick={onDiscard}+ className="text-sm text-zinc-700 transition-colors hover:text-zinc-900"+ >+ Start over+ </button>+ </div>+ </div>+ );+}diff --git a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/components/TapHint.tsx b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/components/TapHint.tsxnew file mode 100644index 0000000000..bfceca95ab--- /dev/null+++ b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/components/TapHint.tsx@@ -0,0 +1,11 @@+"use client";++import { Text } from "@/components/atoms/Text/Text";++export function TapHint({ caption }: { caption: string }) {+ return (+ <Text variant="lead" className="text-center !text-base !text-zinc-500">+ {caption}+ </Text>+ );+}diff --git a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/components/TypedFallback.tsx b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/components/TypedFallback.tsxnew file mode 100644index 0000000000..ebb6693456--- /dev/null+++ b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/components/TypedFallback.tsx@@ -0,0 +1,35 @@+"use client";++import { Button } from "@/components/atoms/Button/Button";+import { FadeIn } from "@/components/atoms/FadeIn/FadeIn";+import { Input } from "@/components/atoms/Input/Input";++interface Props {+ value: string;+ onChange: (value: string) => void;+ onSubmit: () => void;+}++export function TypedFallback({ value, onChange, onSubmit }: Props) {+ return (+ <FadeIn className="w-full">+ <div className="flex w-full flex-col items-center gap-6">+ <Input+ id="brain-dump-text"+ type="textarea"+ label="Your brain dump"+ hideLabel+ rows={8}+ className="rounded-[1.5rem]"+ placeholder="What repeats every week? What would you hand off first?"+ value={value}+ onChange={(e) => onChange(e.target.value)}+ autoFocus+ />+ <Button size="large" onClick={onSubmit} disabled={!value.trim()}>+ I'm done+ </Button>+ </div>+ </FadeIn>+ );+}diff --git a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/helpers.ts b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/helpers.tsnew file mode 100644index 0000000000..ca3e9e5d68--- /dev/null+++ b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/helpers.ts@@ -0,0 +1,82 @@+// Pure helpers for the brain dump. Timings and copy live here so the hook+// stays about behaviour and the component stays about pixels.++// A depth meter, not a limit: the ring fills toward 3:00 and then holds.+// Nothing stops at 3:00 — the longer someone talks, the better the dump.+export const RING_TARGET_SECONDS = 180;++// Recording keeps going far past the ring: 30 min is the hard stop, and+// even then everything captured is kept.+export const HARD_STOP_SECONDS = 1800;++// Every 3s the recorder hands us a chunk to persist. Small enough that a+// crash costs almost nothing, large enough not to thrash IndexedDB.+export const TIMESLICE_MS = 3000;++// How often the take's metadata row is refreshed with the duration so+// far. A crash never reaches `stop()`, so without this the recovery+// prompt would offer back a take it believes is 0:00 long.+export const META_REFRESH_SECONDS = 5;++export const SILENCE_NUDGE_SECONDS = 5;++// Waveform peak (0-127 either side of the 128 midpoint) that counts as+// somebody talking rather than a quiet room.+export const SPEECH_PEAK_THRESHOLD = 12;+export const SILENCE_NUDGE_COPY =+ "Start anywhere. What did you do yesterday that bored you?";++// Never any copy that caps effort — "30 seconds is plenty" is the exact+// message this screen exists to avoid sending.+const ENCOURAGEMENTS = [+ { atSeconds: 10, text: "Keep going, this is gold" },+ { atSeconds: 25, text: "The more you share, the sharper AutoPilot gets" },+ { atSeconds: 45, text: "You're building AutoPilot's memory right now" },+] as const;++// After the last line the screen goes quiet — a nag every 20s would turn+// encouragement into pressure.+const ENCOURAGEMENT_VISIBLE_SECONDS = 6;++export function encouragementAt(elapsedSeconds: number): string | null {+ const active = ENCOURAGEMENTS.find(+ (line) =>+ elapsedSeconds >= line.atSeconds &&+ elapsedSeconds < line.atSeconds + ENCOURAGEMENT_VISIBLE_SECONDS,+ );+ return active?.text ?? null;+}++export function formatElapsed(totalSeconds: number) {+ const minutes = Math.floor(totalSeconds / 60);+ const seconds = Math.floor(totalSeconds % 60);+ return `${minutes}:${String(seconds).padStart(2, "0")}`;+}++export function ringProgress(elapsedSeconds: number) {+ return Math.min(1, elapsedSeconds / RING_TARGET_SECONDS);+}++// Chrome/Edge give us webm/opus; Safari only offers mp4. Mirrors the+// existing copilot recorder so both paths hit the same server allowlist.+export function pickMimeType() {+ if (typeof MediaRecorder === "undefined") return "audio/webm";+ return MediaRecorder.isTypeSupported("audio/webm")+ ? "audio/webm"+ : "audio/mp4";+}++export function newRecordingId() {+ return crypto.randomUUID();+}++export function isPermissionDenied(error: unknown) {+ return error instanceof DOMException && error.name === "NotAllowedError";+}++export function headline(name: string) {+ const trimmed = name.trim();+ return trimmed+ ? `What keeps stealing your week, ${trimmed}?`+ : "What keeps stealing your week?";+}diff --git a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/recordingStore.ts b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/recordingStore.tsnew file mode 100644index 0000000000..2cdf27d342--- /dev/null+++ b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/recordingStore.ts@@ -0,0 +1,197 @@+// Local-first storage for brain-dump audio. Every MediaRecorder chunk lands+// here BEFORE it is offered to the network, and the store is only cleared+// once the server has reported the dump completed. That ordering is the+// whole zero-loss guarantee: a crash, a refresh, a dead tunnel or a closed+// laptop can cost an upload, never a recording.+//+// Hand-rolled rather than pulling in `idb`: the surface used here is four+// object-store calls, and the wizard should not carry a dependency for it.++const DB_NAME = "autogpt-onboarding-brain-dump";+// v2 re-keys the meta store by recordingId. v1 kept a single "current"+// row, so two tabs recording at once clobbered each other's metadata and+// recovery could only ever find the newer take.+const DB_VERSION = 2;+const PARTS_STORE = "parts";+const META_STORE = "meta";++export interface RecordingPart {+ id: string;+ recordingId: string;+ partIndex: number;+ blob: Blob;+ savedAt: number;+ uploaded: boolean;+}++export interface RecordingMeta {+ recordingId: string;+ mimeType: string;+ startedAt: number;+ durationSecs: number;+ finalized: boolean;+}++export function isIndexedDBAvailable(): boolean {+ return typeof window !== "undefined" && "indexedDB" in window;+}++function openDB(): Promise<IDBDatabase> {+ return new Promise((resolve, reject) => {+ const request = indexedDB.open(DB_NAME, DB_VERSION);+ request.onupgradeneeded = function handleUpgrade() {+ const db = request.result;+ if (!db.objectStoreNames.contains(PARTS_STORE)) {+ const store = db.createObjectStore(PARTS_STORE, { keyPath: "id" });+ store.createIndex("recordingId", "recordingId");+ }+ // Recreate rather than migrate: the only thing lost is which take+ // was in flight, and the parts (the irreplaceable bit) live in a+ // different store and are untouched.+ if (db.objectStoreNames.contains(META_STORE)) {+ db.deleteObjectStore(META_STORE);+ }+ db.createObjectStore(META_STORE, { keyPath: "recordingId" });+ };+ // Another tab still holding v1 open blocks the upgrade, and without+ // this the promise never settles — the recorder would hang before it+ // ever reached the mic. Failing here instead falls back to+ // upload-only, which is degraded but alive.+ let blocked = false;+ request.onsuccess = () => {+ // The other tab can close after we have already given up, at which+ // point the upgrade completes and hands us a connection nobody is+ // holding. Closing it keeps it from blocking the *next* upgrade.+ if (blocked) {+ request.result.close();+ return;+ }+ resolve(request.result);+ };+ request.onerror = () => reject(request.error);+ request.onblocked = () => {+ blocked = true;+ reject(new Error("IndexedDB upgrade blocked by another tab"));+ };+ });+}++// Safari in private mode, hardened browser profiles and the test+// environment all lack a usable IndexedDB. Losing local persistence+// degrades the zero-loss guarantee to "the upload queue is the only+// backup" — worth continuing on, not worth crashing the step for.+class IndexedDBUnavailableError extends Error {}++function runTransaction<T>(+ storeName: string,+ mode: IDBTransactionMode,+ run: (store: IDBObjectStore) => IDBRequest<T>,+): Promise<T> {+ if (!isIndexedDBAvailable()) {+ return Promise.reject(new IndexedDBUnavailableError());+ }+ return openDB().then(+ (db) =>+ new Promise<T>((resolve, reject) => {+ const transaction = db.transaction(storeName, mode);+ const request = run(transaction.objectStore(storeName));+ let result: T;+ request.onsuccess = () => {+ result = request.result;+ };+ // Settled on the transaction, not the request. `onsuccess` fires+ // before the transaction commits, so resolving there would report+ // a chunk as persisted that a later abort (a quota error on a big+ // blob, say) silently throws away — precisely the thing the+ // zero-loss guarantee at the top of this file promises not to do.+ transaction.oncomplete = () => {+ db.close();+ resolve(result);+ };+ // Closing on every exit, not just the happy one: a leaked+ // connection also blocks the next version upgrade.+ transaction.onabort = () => {+ db.close();+ reject(transaction.error ?? request.error);+ };+ transaction.onerror = () => {+ db.close();+ reject(transaction.error ?? request.error);+ };+ }),+ );+}++export async function savePart(part: RecordingPart) {+ await runTransaction(PARTS_STORE, "readwrite", (store) => store.put(part));+}++export async function markPartUploaded(id: string) {+ const existing = await runTransaction<RecordingPart | undefined>(+ PARTS_STORE,+ "readonly",+ (store) => store.get(id),+ );+ if (!existing) return;+ await savePart({ ...existing, uploaded: true });+}++export async function getParts(recordingId: string) {+ const all = await runTransaction<RecordingPart[]>(+ PARTS_STORE,+ "readonly",+ (store) => store.index("recordingId").getAll(recordingId),+ );+ return all.sort((a, b) => a.partIndex - b.partIndex);+}++export async function saveMeta(meta: RecordingMeta) {+ await runTransaction(META_STORE, "readwrite", (store) => store.put(meta));+}++// The take a returning user should be offered. Two tabs can each have a+// recording in flight, so prefer the newest *unfinalized* one — a+// finished take is not something to recover from.+export async function getMeta() {+ const all = await runTransaction<RecordingMeta[]>(+ META_STORE,+ "readonly",+ (store) => store.getAll(),+ );+ const unfinalized = all.filter((meta) => !meta.finalized);+ const candidates = unfinalized.length > 0 ? unfinalized : all;+ return candidates.sort((a, b) => b.startedAt - a.startedAt)[0] ?? null;+}++// The finalize path knows exactly which take it just submitted, and must+// say so: `getMeta()` answers "what should we offer to recover", which in+// a second tab is a different, newer take. Marking that one finalized+// would strand the take that actually completed.+export async function getMetaById(recordingId: string) {+ const meta = await runTransaction<RecordingMeta | undefined>(+ META_STORE,+ "readonly",+ (store) => store.get(recordingId),+ );+ return meta ?? null;+}++// Called only after the server reports `completed` — an unfinalized+// recording must survive every other kind of exit.+export async function clearRecording(recordingId: string) {+ const parts = await getParts(recordingId);+ await Promise.all(+ parts.map((part) =>+ runTransaction(PARTS_STORE, "readwrite", (store) =>+ store.delete(part.id),+ ),+ ),+ );+ await runTransaction(META_STORE, "readwrite", (store) =>+ store.delete(recordingId),+ );+}++export function partId(recordingId: string, partIndex: number) {+ return `${recordingId}:${partIndex}`;+}diff --git a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/useBrainDumpRecorder.ts b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/useBrainDumpRecorder.tsnew file mode 100644index 0000000000..eb679e3c19--- /dev/null+++ b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/useBrainDumpRecorder.ts@@ -0,0 +1,322 @@+// The recorder. Ordering is the contract: chunk → IndexedDB → upload+// queue. "Saved locally" only turns on once the IndexedDB write resolved,+// so the reassurance on screen is a fact rather than a hope.++import { useEffect, useRef, useState } from "react";+import { trackBrainDump } from "@/services/onboarding/brain-dump-analytics";+import {+ HARD_STOP_SECONDS,+ isPermissionDenied,+ META_REFRESH_SECONDS,+ newRecordingId,+ pickMimeType,+ SPEECH_PEAK_THRESHOLD,+ TIMESLICE_MS,+} from "./helpers";+import { getMeta, getParts, savePart, saveMeta } from "./recordingStore";+import { buildPart, useUploadQueue } from "./useUploadQueue";++export type RecorderPhase = "idle" | "recording" | "stopping" | "stopped";++export function useBrainDumpRecorder() {+ const [phase, setPhase] = useState<RecorderPhase>("idle");+ const [elapsedSeconds, setElapsedSeconds] = useState(0);+ const [isSavedLocally, setIsSavedLocally] = useState(false);+ const [permissionDenied, setPermissionDenied] = useState(false);+ const [hasSpoken, setHasSpoken] = useState(false);+ // The recorder stopping itself is indistinguishable from any other stop+ // by phase alone, and the step has to tell them apart: this one has to+ // submit the take, a restart has to throw it away.+ const [hitTimeLimit, setHitTimeLimit] = useState(false);++ const recordingIdRef = useRef<string | null>(null);+ const mimeTypeRef = useRef<string>("audio/webm");+ const recorderRef = useRef<MediaRecorder | null>(null);+ const streamRef = useRef<MediaStream | null>(null);+ const partIndexRef = useRef(0);+ const startedAtRef = useRef(0);+ // `start()` is only half a function until the permission prompt is+ // answered, and the orb stays tappable throughout. A second tap without+ // this would reset `partIndexRef` under the first take, leaving two live+ // recorders interleaving their part indices into one recording id.+ const isStartingRef = useRef(false);+ // Elapsed seconds as of the last metadata write, so the row is refreshed+ // on a schedule rather than on every 250ms tick.+ const metaWrittenAtRef = useRef(0);+ // Metadata writes are chained so the final duration written by `stop()`+ // cannot be overtaken by a refresh issued a moment earlier.+ const metaWriteRef = useRef<Promise<void>>(Promise.resolve());+ // Mirrors `elapsedSeconds` because the state value a caller reads is the+ // one from its own render. `handleDone` awaits `stop()` — which itself+ // waits on the recorder and the pending IndexedDB writes — so by the+ // time it reports a duration, React has not re-rendered and the state+ // is short. The backend splits recordings over 20 minutes on this+ // number, so it has to be the real one.+ const elapsedSecondsRef = useRef(0);+ // Whether the mic has picked up anything above room noise yet. The+ // silence nudge used to key off `isSavedLocally`, but MediaRecorder+ // emits a chunk every timeslice whether or not anyone spoke, so that+ // flag was always true well before the nudge was due.+ const hasSpokenRef = useRef(false);+ const analyserRef = useRef<AnalyserNode | null>(null);+ const audioContextRef = useRef<AudioContext | null>(null);+ const timerRef = useRef<ReturnType<typeof setInterval> | null>(null);+ // `ondataavailable` fires before `onstop`, and persisting is async — so+ // stopping has to wait on the in-flight writes or the final few seconds+ // of speech would be flushed to the server after we'd already decided+ // the queue was empty.+ const pendingWritesRef = useRef<Promise<void>[]>([]);++ const queue = useUploadQueue();++ useEffect(() => stopTracks, []);++ function stopTracks() {+ if (timerRef.current) clearInterval(timerRef.current);+ timerRef.current = null;+ streamRef.current?.getTracks().forEach((track) => track.stop());+ streamRef.current = null;+ void audioContextRef.current?.close().catch(() => undefined);+ audioContextRef.current = null;+ analyserRef.current = null;+ }++ function listenForSpeech(stream: MediaStream) {+ try {+ const context = new AudioContext();+ const analyser = context.createAnalyser();+ analyser.fftSize = 256;+ context.createMediaStreamSource(stream).connect(analyser);+ audioContextRef.current = context;+ analyserRef.current = analyser;+ } catch {+ // No AudioContext available. The nudge simply never fires; the+ // recording itself does not depend on this.+ }+ }++ async function start() {+ // A tap that lands while the permission prompt from the previous one+ // is still open is the same tap, not a second take.+ if (isStartingRef.current) return false;+ isStartingRef.current = true;+ try {+ return await openMicAndRecord();+ } finally {+ isStartingRef.current = false;+ }+ }++ async function openMicAndRecord() {+ // Permission is requested on the first tap, never on screen load —+ // a browser prompt before the user has read the headline is how+ // denial rates go up.+ let stream: MediaStream;+ try {+ stream = await navigator.mediaDevices.getUserMedia({ audio: true });+ } catch (error) {+ if (isPermissionDenied(error)) {+ setPermissionDenied(true);+ trackBrainDump("brain_dump_permission_denied");+ }+ return false;+ }++ const recordingId = newRecordingId();+ const mimeType = pickMimeType();+ recordingIdRef.current = recordingId;+ mimeTypeRef.current = mimeType;+ partIndexRef.current = 0;+ startedAtRef.current = Date.now();+ elapsedSecondsRef.current = 0;+ metaWrittenAtRef.current = 0;+ hasSpokenRef.current = false;+ setHasSpoken(false);+ streamRef.current = stream;+ listenForSpeech(stream);+ setElapsedSeconds(0);+ setIsSavedLocally(false);+ setHitTimeLimit(false);++ await rememberMeta({+ recordingId,+ mimeType,+ startedAt: startedAtRef.current,+ durationSecs: 0,+ finalized: false,+ });++ const recorder = new MediaRecorder(stream, { mimeType });+ recorder.ondataavailable = function handleChunk(event) {+ if (event.data.size === 0) return;+ pendingWritesRef.current.push(persistChunk(recordingId, event.data));+ };+ recorder.start(TIMESLICE_MS);+ recorderRef.current = recorder;++ timerRef.current = setInterval(tick, 250);+ setPhase("recording");+ trackBrainDump("brain_dump_started");+ return true;+ }++ async function persistChunk(recordingId: string, blob: Blob) {+ const part = buildPart(recordingId, partIndexRef.current, blob);+ partIndexRef.current += 1;+ try {+ await savePart(part);+ setIsSavedLocally(true);+ } catch {+ // No local persistence available (private-mode Safari, hardened+ // profiles). The upload queue becomes the only backup, so the chunk+ // still goes out — but "saved locally" stays off rather than lying.+ }+ queue.enqueue(part);+ }++ function tick() {+ const seconds = (Date.now() - startedAtRef.current) / 1000;+ elapsedSecondsRef.current = seconds;+ setElapsedSeconds(seconds);+ detectSpeech();+ refreshMetaDuration(seconds);+ // 30 minutes stops the recorder but keeps every second captured —+ // the dump still submits, it just stops growing.+ if (seconds < HARD_STOP_SECONDS) return;+ setHitTimeLimit(true);+ void stop();+ }++ // The duration only used to be written by `stop()`, and a crash or a+ // refresh — the exact case recovery exists for — never gets there. The+ // recovery prompt quotes this number back to the user and finalize+ // submits it, and the backend splits long recordings on it.+ function refreshMetaDuration(seconds: number) {+ if (seconds - metaWrittenAtRef.current < META_REFRESH_SECONDS) return;+ metaWrittenAtRef.current = seconds;+ void rememberMeta({+ recordingId: recordingIdRef.current ?? "",+ mimeType: mimeTypeRef.current,+ startedAt: startedAtRef.current,+ durationSecs: seconds,+ finalized: false,+ });+ }++ // One-way: the nudge is for someone who has not started yet, so it+ // must not come back during a pause for breath.+ function detectSpeech() {+ const analyser = analyserRef.current;+ if (hasSpokenRef.current || !analyser) return;+ const samples = new Uint8Array(analyser.frequencyBinCount);+ analyser.getByteTimeDomainData(samples);+ const peak = samples.reduce(+ (loudest, sample) => Math.max(loudest, Math.abs(sample - 128)),+ 0,+ );+ if (peak < SPEECH_PEAK_THRESHOLD) return;+ hasSpokenRef.current = true;+ setHasSpoken(true);+ }++ async function stop(): Promise<number> {+ const recorder = recorderRef.current;+ if (!recorder || recorder.state === "inactive") {+ return elapsedSecondsRef.current;+ }+ setPhase("stopping");+ await new Promise<void>((resolve) => {+ recorder.onstop = () => resolve();+ recorder.stop();+ });+ await Promise.all(pendingWritesRef.current);+ pendingWritesRef.current = [];+ stopTracks();+ recorderRef.current = null;+ // Measured here rather than read off state: the awaits above mean the+ // last tick is already behind, and the tail of the take counts.+ const durationSecs = (Date.now() - startedAtRef.current) / 1000;+ elapsedSecondsRef.current = durationSecs;+ setElapsedSeconds(durationSecs);+ await rememberMeta({+ recordingId: recordingIdRef.current ?? "",+ mimeType: mimeTypeRef.current,+ startedAt: startedAtRef.current,+ durationSecs,+ finalized: false,+ });+ setPhase("stopped");+ return durationSecs;+ }++ // On mount, an unfinalized recording in IndexedDB means the last+ // session ended in a crash, a refresh or a back button. The parts are+ // still there, so the user is offered them back rather than told to+ // start over.+ async function findRecoverable() {+ const meta = await getMeta().catch(() => null);+ if (!meta || meta.finalized) return null;+ return meta;+ }++ // After a crash the upload queue is gone with the page, so the parts+ // have to be replayed out of IndexedDB. Parts already marked uploaded+ // are replayed too — the server keys them by index and overwrites, so a+ // duplicate is free while a missing one would punch a hole in the audio.+ async function adoptRecovered(recordingId: string, mimeType: string) {+ recordingIdRef.current = recordingId;+ mimeTypeRef.current = mimeType;+ const parts = await resendAllParts(recordingId);+ partIndexRef.current = parts.length;+ setPhase("stopped");+ }++ // Re-queue every part on disk, including ones already marked uploaded.+ //+ // The server's part buffer expires, so "we uploaded this once" is not+ // the same as "the server still has it" — and a missing part 0 means a+ // headless stream that can never be decoded. Rather than reason about+ // how long the buffer lives (clock skew, a constant duplicated on two+ // sides, a TTL someone later tunes), just re-send everything: the+ // server keys parts by index and overwrites, so a duplicate costs+ // bandwidth and nothing else. A whole dump is ~6 MB.+ async function resendAllParts(recordingId: string) {+ const parts = await getParts(recordingId).catch(() => []);+ parts.forEach((part) => queue.enqueue(part));+ return parts;+ }++ // Metadata is a convenience for crash recovery, not part of the audio —+ // failing to write it must never abort a take in progress. Writes are+ // serialised so the last one issued is the last one to land.+ async function rememberMeta(meta: Parameters<typeof saveMeta>[0]) {+ const write = metaWriteRef.current.then(() =>+ saveMeta(meta).catch(() => undefined),+ );+ metaWriteRef.current = write;+ await write;+ }++ return {+ phase,+ elapsedSeconds,+ isSavedLocally,+ permissionDenied,+ hasSpoken,+ hitTimeLimit,+ isOffline: queue.isOffline,+ audioStream: streamRef.current,+ // The live figure, for callers that need it outside a render (the+ // retry path). `elapsedSeconds` above is this render's snapshot.+ getElapsedSeconds: () => elapsedSecondsRef.current,+ recordingId: recordingIdRef.current,+ mimeType: mimeTypeRef.current,+ start,+ stop,+ flushUploads: queue.flush,+ resetQueue: queue.reset,+ findRecoverable,+ adoptRecovered,+ resendAllParts,+ };+}diff --git a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/useBrainDumpStep.ts b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/useBrainDumpStep.tsnew file mode 100644index 0000000000..d010be5301--- /dev/null+++ b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/useBrainDumpStep.ts@@ -0,0 +1,393 @@+import {+ discardBrainDump,+ finalizeBrainDump,+} from "@/app/api/__generated__/endpoints/brain-dump/brain-dump";+import { setIntroPath } from "@/services/onboarding/brain-dump-handoff";+import { useEffect, useRef, useState } from "react";+import { useOnboardingWizardStore } from "../../store";+import { trackBrainDump } from "@/services/onboarding/brain-dump-analytics";+import { headline, SILENCE_NUDGE_SECONDS } from "./helpers";+import {+ clearRecording,+ getMetaById,+ getParts,+ saveMeta,+} from "./recordingStore";+import { useBrainDumpRecorder } from "./useBrainDumpRecorder";++export type ScreenState =+ | "rest"+ | "recording"+ | "processing"+ | "typing"+ | "recovery"+ | "failed";++export function useBrainDumpStep() {+ const name = useOnboardingWizardStore((s) => s.name);+ const nextStep = useOnboardingWizardStore((s) => s.nextStep);+ const setStepBusy = useOnboardingWizardStore((s) => s.setStepBusy);+ const recorder = useBrainDumpRecorder();++ const [screen, setScreen] = useState<ScreenState>("rest");+ const [typedText, setTypedText] = useState("");+ const [reachedTimeLimit, setReachedTimeLimit] = useState(false);++ // While the recording is being processed there is no way back — the+ // wizard's Back button hides itself on this flag.+ useEffect(() => {+ setStepBusy(screen === "processing");+ return () => setStepBusy(false);+ }, [screen, setStepBusy]);+ const [recoverable, setRecoverable] = useState<{+ recordingId: string;+ mimeType: string;+ durationSecs: number;+ } | null>(null);+ const retryCountRef = useRef(0);+ // A submit in flight owns the screen: "Skip for now" must not race+ // `completeAndAdvance` to `nextStep()`, or the wizard advances twice and+ // lands past the last step with nothing to render.+ const isSubmittingRef = useRef(false);++ useEffect(() => {+ async function checkForRecovery() {+ const meta = await recorder.findRecoverable();+ if (!meta) return;+ const parts = await getParts(meta.recordingId).catch(() => []);+ if (parts.length === 0) return;+ setRecoverable(meta);+ setScreen("recovery");+ trackBrainDump("brain_dump_recovery_shown", { parts: parts.length });+ }+ void checkForRecovery();+ // Recovery is a mount-time question only — re-asking mid-recording+ // would fight the user for control of the screen.+ // eslint-disable-next-line react-hooks/exhaustive-deps+ }, []);++ // Permission denial is never a dead end: the typing composer takes over+ // under the same headline.+ useEffect(() => {+ if (!recorder.permissionDenied) return;+ setScreen("typing");+ trackBrainDump("brain_dump_typed_fallback", {+ reason: "permission_denied",+ });+ }, [recorder.permissionDenied]);++ // Keyed on whether the mic has actually heard anything. It used to key+ // on `!isSavedLocally`, which flips as soon as the first chunk is+ // persisted — MediaRecorder produces one every timeslice whether or not+ // the user spoke, so the nudge could never reach its own threshold.+ const showSilenceNudge =+ screen === "recording" &&+ recorder.elapsedSeconds >= SILENCE_NUDGE_SECONDS &&+ !recorder.hasSpoken;++ // The recorder enforces the 30-minute cap from the inside, so without+ // this the mic would close while the screen still said "recording" — a+ // frozen timer and dead captions until the user thought to press "I'm+ // done". Keyed on the cap specifically, not on the recorder having+ // stopped: a restart stops it too, and that take is being thrown away.+ useEffect(() => {+ if (screen !== "recording" || !recorder.hitTimeLimit) return;+ setReachedTimeLimit(true);+ setScreen("processing");+ void submitRecording(recorder.recordingId, recorder.getElapsedSeconds());+ // eslint-disable-next-line react-hooks/exhaustive-deps+ }, [screen, recorder.hitTimeLimit]);++ async function handleStart() {+ const started = await recorder.start();+ if (started) setScreen("recording");+ }++ async function handleDone() {+ setScreen("processing");+ // Duration comes back from `stop()` for the same reason the id is+ // passed in below — `recorder.elapsedSeconds` here is this render's+ // value, and it is short by however long stopping took.+ const durationSecs = await recorder.stop();+ await submitRecording(recorder.recordingId, durationSecs);+ }++ // The id is passed in rather than read off the recorder: `adoptRecovered`+ // writes it to a ref, and this function's closure would still be holding+ // the previous render's `null`.+ async function submitRecording(+ recordingId: string | null,+ durationSecs: number,+ ) {+ isSubmittingRef.current = true;+ try {+ await finalizeRecording(recordingId, durationSecs);+ } finally {+ isSubmittingRef.current = false;+ }+ }++ async function finalizeRecording(+ recordingId: string | null,+ durationSecs: number,+ ) {+ if (!recordingId) {+ setScreen("failed");+ return;+ }+ const allUploaded = await recorder.flushUploads();+ if (!allUploaded) {+ setScreen("failed");+ return;+ }++ const startedAt = performance.now();+ try {+ const response = await finalizeBrainDump({+ recording_id: recordingId,+ input_mode: "voice",+ duration_secs: durationSecs,+ mime_type: recorder.mimeType,+ });+ trackBrainDump("finalize_latency_ms", {+ ms: Math.round(performance.now() - startedAt),+ input_mode: "voice",+ });+ if (response.status !== 200 || response.data.status === "failed") {+ trackBrainDump("transcription_failed", {+ error_code:+ response.status === 200+ ? response.data.error_code+ : response.status,+ });+ setScreen("failed");+ return;+ }+ } catch {+ setScreen("failed");+ return;+ }++ trackBrainDump("brain_dump_completed", {+ duration_secs: Math.round(durationSecs),+ input_mode: "voice",+ });+ await completeAndAdvance(recordingId, "A");+ }++ // A fresh take: the current recording is stopped and thrown away — the+ // local parts and the server's half-uploaded buffer both — before the+ // orb starts listening again under a new recording id.+ async function handleRestart() {+ trackBrainDump("brain_dump_restarted");+ const previousId = recorder.recordingId;+ await recorder.stop();+ recorder.resetQueue();+ if (previousId) await clearRecording(previousId).catch(() => undefined);+ // Say which take: without an id the server drops whatever the row+ // currently points at, which in a second tab is somebody else's+ // buffer still being filled.+ if (previousId) {+ await discardBrainDump({ recording_id: previousId }).catch(+ () => undefined,+ );+ }+ const started = await recorder.start();+ if (!started) setScreen("rest");+ }++ async function handleRetry() {+ retryCountRef.current += 1;+ trackBrainDump("brain_dump_retry", { attempt: retryCountRef.current });+ setScreen("processing");+ const recordingId = recorder.recordingId;+ if (recordingId) await recorder.resendAllParts(recordingId);+ // Same reason `handleDone` takes the duration from `stop()`: the+ // state value on this closure is whatever the last render saw.+ await submitRecording(recordingId, recorder.getElapsedSeconds());+ }++ function handleShowTyping() {+ setScreen("typing");+ trackBrainDump("brain_dump_typed_fallback", { reason: "chose_to_type" });+ }++ function handleShowRecording() {+ setScreen("rest");+ }++ async function handleSubmitTyped() {+ const text = typedText.trim();+ if (!text) return;+ isSubmittingRef.current = true;+ try {+ await submitTyped(text);+ } finally {+ isSubmittingRef.current = false;+ }+ }++ async function submitTyped(text: string) {+ setScreen("processing");+ const recordingId = recorder.recordingId ?? crypto.randomUUID();+ try {+ const response = await finalizeBrainDump({+ recording_id: recordingId,+ input_mode: "typed",+ text,+ });+ // A 2xx envelope can still carry a failed pipeline, exactly as on+ // the voice path — advancing on it would hand the user a copilot+ // home built from nothing.+ if (response.status !== 200 || response.data.status === "failed") {+ setScreen("failed");+ return;+ }+ } catch {+ setScreen("failed");+ return;+ }+ trackBrainDump("brain_dump_completed", {+ input_mode: "typed",+ chars: text.length,+ });+ await completeAndAdvance(recordingId, "A");+ }++ async function handleSkip() {+ // A skip that lands while the dump is being submitted would advance+ // the wizard a second time behind `completeAndAdvance`.+ if (isSubmittingRef.current) return;+ trackBrainDump("brain_dump_skipped");+ const recordedId = recorder.recordingId;+ // Best effort: a failed skip-record still has to let the user+ // through — being unable to say "no thanks" would be absurd.+ try {+ await finalizeBrainDump({+ recording_id: recordedId ?? crypto.randomUUID(),+ input_mode: "skipped",+ });+ } catch {+ // Path B is the safe default anyway.+ }+ // Someone who talked for minutes and then skipped leaves multi-MB+ // blobs behind, and an unfinalized meta row makes the next visit+ // offer back a take they explicitly abandoned.+ if (recordedId) {+ await releaseTake(recordedId);+ await discardBrainDump({ recording_id: recordedId }).catch(+ () => undefined,+ );+ }+ recorder.resetQueue();+ setIntroPath("B");+ nextStep();+ }++ // IndexedDB is only cleared once the server has confirmed the dump —+ // until then the browser is the backup of record.+ async function completeAndAdvance(recordingId: string, path: "A" | "B") {+ await releaseTake(recordingId);+ recorder.resetQueue();+ setIntroPath(path);+ nextStep();+ }++ // Marking finalized before clearing is belt and braces: if the delete+ // fails, the flag alone is enough to stop the take being offered back.+ async function releaseTake(recordingId: string) {+ // Best-effort: the dump is either already safe on the server or+ // deliberately abandoned, so a storage error here must not strand+ // the user on the loading screen.+ try {+ // By id, not "the newest take": a second tab may have started a+ // more recent one, and finalizing that instead would leave the+ // take we actually completed looking recoverable forever.+ const meta = await getMetaById(recordingId);+ if (meta) await saveMeta({ ...meta, finalized: true });+ await clearRecording(recordingId);+ } catch {+ // Nothing left to protect.+ }+ }++ async function handleResumeRecovered() {+ if (!recoverable) return;+ trackBrainDump("brain_dump_recovery_used");+ setScreen("processing");+ await recorder.adoptRecovered(+ recoverable.recordingId,+ recoverable.mimeType,+ );+ await submitRecording(recoverable.recordingId, recoverable.durationSecs);+ }++ async function handleDiscardRecovered() {+ await dropRecoverable();+ setScreen("rest");+ }++ async function handleTypeInsteadOfRecovered() {+ await dropRecoverable();+ handleShowTyping();+ }++ // Abandoning a take also releases the server's half-uploaded buffer —+ // otherwise those chunks sit in Redis until their TTL for a recording+ // nobody will ever finalize.+ async function dropRecoverable() {+ if (!recoverable) return;+ await clearRecording(recoverable.recordingId).catch(() => undefined);+ await discardBrainDump({ recording_id: recoverable.recordingId }).catch(+ () => undefined,+ );+ setRecoverable(null);+ }++ async function handleDownloadRecording() {+ const recordingId = recorder.recordingId;+ if (!recordingId) return;+ trackBrainDump("brain_dump_download");+ const parts = await getParts(recordingId).catch(() => []);+ if (parts.length === 0) return;+ const blob = new Blob(+ parts.map((part) => part.blob),+ { type: recorder.mimeType },+ );+ const url = URL.createObjectURL(blob);+ const link = document.createElement("a");+ link.href = url;+ link.download = `brain-dump-${recordingId}.webm`;+ link.click();+ URL.revokeObjectURL(url);+ }++ return {+ headline: headline(name),+ screen,+ typedText,+ setTypedText,+ elapsedSeconds: recorder.elapsedSeconds,+ isOffline: recorder.isOffline,+ isSavedLocally: recorder.isSavedLocally,+ audioStream: recorder.audioStream,+ showSilenceNudge,+ reachedTimeLimit,+ recoverable,+ handleStart,+ handleDone,+ handleRestart,+ handleRetry,+ handleSkip,+ handleSubmitTyped,+ handleResumeRecovered,+ handleDiscardRecovered,+ handleTypeInsteadOfRecovered,+ handleDownloadRecording,+ showTyping: handleShowTyping,+ showRecording: handleShowRecording,+ // Going back to the orb is a dead end when the browser already refused+ // the microphone, so the way back is offered only when it can work.+ isMicBlocked: recorder.permissionDenied,+ };+}diff --git a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/useUploadQueue.ts b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/useUploadQueue.tsnew file mode 100644index 0000000000..df28de20a4--- /dev/null+++ b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/BrainDumpStep/useUploadQueue.ts@@ -0,0 +1,172 @@+// Background upload queue for recording parts.+//+// The queue exists so the network is never on the critical path of the+// recording. Parts are already durable in IndexedDB by the time they get+// here, so a failure is a retry, not an error: nothing is surfaced to the+// user while they are still talking, and a dropped connection just parks+// the queue until the browser says it is back.++import { uploadBrainDumpPart } from "@/app/api/__generated__/endpoints/brain-dump/brain-dump";+import { ApiError } from "@/lib/autogpt-server-api/helpers";+import { useEffect, useRef, useState } from "react";+import { markPartUploaded, partId, RecordingPart } from "./recordingStore";++const RETRY_DELAYS_MS = [1000, 3000, 8000];+const FLUSH_PASSES = 3;++// 408 and 429 are the server asking us to come back. Every other 4xx is a+// verdict on the request itself — a part over the size cap, a body the+// server will not accept, an expired session — and retrying it produces+// the identical rejection three more times.+const RETRYABLE_CLIENT_STATUSES = [408, 429];++export function useUploadQueue() {+ const queueRef = useRef<RecordingPart[]>([]);+ const drainRef = useRef<Promise<void> | null>(null);+ // Bumped by `reset()`. A drain sitting on an upload holds a reference to a+ // queue that a restart has since replaced, and everything it does when the+ // upload lands — dropping the head, updating the count, marking the part+ // uploaded, resolving `flush()` — would land on the new take instead.+ const takeRef = useRef(0);+ const [pendingCount, setPendingCount] = useState(0);++ const [isOffline, setIsOffline] = useState(false);+ useEffect(() => {+ function handleOnline() {+ setIsOffline(false);+ void drain();+ }+ function handleOffline() {+ setIsOffline(true);+ }+ setIsOffline(typeof navigator !== "undefined" && !navigator.onLine);+ window.addEventListener("online", handleOnline);+ window.addEventListener("offline", handleOffline);+ return () => {+ window.removeEventListener("online", handleOnline);+ window.removeEventListener("offline", handleOffline);+ };+ }, []);++ // Returns the IN-FLIGHT drain rather than a no-op when one is already+ // running. A bare `return` here resolves instantly, which made `flush()`+ // await nothing, observe the not-yet-uploaded tail and report failure —+ // on every single dump, since the final chunk is enqueued microseconds+ // before "I'm done" runs.+ function drain(): Promise<void> {+ if (drainRef.current) return drainRef.current;+ const take = takeRef.current;+ const running = drainQueue(take).finally(() => {+ // A drain from a discarded take must not clear the handle of the one+ // now running for the new take.+ if (takeRef.current === take) drainRef.current = null;+ });+ drainRef.current = running;+ return running;+ }++ async function drainQueue(take: number) {+ while (takeRef.current === take && queueRef.current.length > 0) {+ const part = queueRef.current[0];+ const uploaded = await uploadWithRetries(part);+ if (takeRef.current !== take) return;+ if (!uploaded) {+ // Leave it at the head — reconnecting replays from here so+ // parts still reach the server in order.+ return;+ }+ queueRef.current = queueRef.current.slice(1);+ setPendingCount(queueRef.current.length);+ // Bookkeeping only, and the part is already on the server. Where+ // IndexedDB is unavailable this rejects — and an unhandled+ // rejection here would take out `flush()` (reporting failure for a+ // dump that fully uploaded) and leave `enqueue()`'s `void drain()`+ // dangling.+ await markPartUploaded(part.id).catch(() => undefined);+ }+ }++ function enqueue(part: RecordingPart) {+ queueRef.current = [...queueRef.current, part];+ setPendingCount(queueRef.current.length);+ void drain();+ }++ // Called on "I'm done": every part must be at the server before+ // finalize runs, otherwise the assembled audio would have holes.+ //+ // Loops because a part can be enqueued in the gap between the running+ // drain emptying the queue and this check — one extra pass catches the+ // straggler, and the bound stops a pathological ping-pong.+ async function flush() {+ const take = takeRef.current;+ for (let attempt = 0; attempt < FLUSH_PASSES; attempt++) {+ await drain();+ // The take this was flushing has been thrown away. An empty queue now+ // says nothing about it, and answering "all uploaded" would finalize a+ // recording the server has already been told to discard.+ if (takeRef.current !== take) return false;+ if (queueRef.current.length === 0) return true;+ }+ return queueRef.current.length === 0;+ }++ function reset() {+ takeRef.current += 1;+ queueRef.current = [];+ // A drain mid-upload belongs to the take just discarded and will stop on+ // the bumped counter. Dropping the handle lets the next enqueue start a+ // drain for the new take instead of waiting behind that one.+ drainRef.current = null;+ setPendingCount(0);+ }++ return { enqueue, flush, reset, pendingCount, isOffline };+}++async function uploadWithRetries(part: RecordingPart) {+ for (let attempt = 0; attempt <= RETRY_DELAYS_MS.length; attempt++) {+ try {+ await uploadBrainDumpPart({+ file: part.blob,+ recording_id: part.recordingId,+ part_index: part.partIndex,+ });+ return true;+ } catch (error) {+ // A part the server will never accept would otherwise be retried+ // for 12s here and then again on every 3-second chunk that follows,+ // holding the whole queue — and its blobs — at the head for the+ // rest of the take.+ if (isPermanentFailure(error)) return false;+ const delay = RETRY_DELAYS_MS[attempt];+ if (delay === undefined) return false;+ await new Promise((resolve) => setTimeout(resolve, delay));+ }+ }+ return false;+}++function isPermanentFailure(error: unknown) {+ if (!(error instanceof ApiError)) return false;+ return (+ error.status >= 400 &&+ error.status < 500 &&+ !RETRYABLE_CLIENT_STATUSES.includes(error.status)+ );+}++export function buildPart(+ recordingId: string,+ partIndex: number,+ blob: Blob,+): RecordingPart {+ return {+ id: partId(recordingId, partIndex),+ recordingId,+ partIndex,+ blob,+ savedAt: Date.now(),+ uploaded: false,+ };+}diff --git a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/PreparingStep.tsx b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/PreparingStep.tsxindex d49b84f35b..b04509c62d 100644--- a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/PreparingStep.tsx+++ b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/PreparingStep.tsx@@ -1,62 +1,26 @@"use client";import { AutoGPTLogo } from "@/components/atoms/AutoGPTLogo/AutoGPTLogo";+import { Icon } from "@/components/atoms/Icon/Icon";import { Text } from "@/components/atoms/Text/Text";import { TypingText } from "@/components/molecules/TypingText/TypingText";import { cn } from "@/lib/utils";-import { useEffect, useRef, useState } from "react";import { Tick02Icon } from "@hugeicons/core-free-icons";-import { Icon } from "@/components/atoms/Icon/Icon";--const CHECKLIST = [- "Personalizing your experience",- "Connecting automation engines",- "Building your space",-] as const;--const STEP_DURATION_MS = 4000;-const STEP_INTERVAL = STEP_DURATION_MS / CHECKLIST.length;+import { usePreparingStep } from "./usePreparingStep";interface Props {onComplete: () => void;+ isBrainDumpEnabled?: boolean;}-export function PreparingStep({ onComplete }: Props) {- const [started, setStarted] = useState(false);- const [completedItems, setCompletedItems] = useState(0);- const [progress, setProgress] = useState(0);- const onCompleteRef = useRef(onComplete);- onCompleteRef.current = onComplete;-- useEffect(() => {- const timer = setTimeout(() => setStarted(true), 300);- return () => clearTimeout(timer);- }, []);-- useEffect(() => {- if (!started) return;-- const startTime = Date.now();-- const progressInterval = setInterval(() => {- const elapsed = Date.now() - startTime;- const pct = Math.min(100, (elapsed / STEP_DURATION_MS) * 100);- setProgress(pct);-- const items = Math.min(- CHECKLIST.length,- Math.floor(elapsed / STEP_INTERVAL) + 1,- );- setCompletedItems(items);-- if (elapsed >= STEP_DURATION_MS) {- clearInterval(progressInterval);- onCompleteRef.current();- }- }, 50);-- return () => clearInterval(progressInterval);- }, [started]);+export function PreparingStep({+ onComplete,+ isBrainDumpEnabled = false,+}: Props) {+ const { started, progress, completedItems, checklist } = usePreparingStep({+ onComplete,+ isBrainDumpEnabled,+ });return (<div className="flex w-full max-w-md flex-col items-center gap-8 px-4">@@ -83,7 +47,7 @@ export function PreparingStep({ onComplete }: Props) {</div><ul className="flex flex-col gap-3">- {CHECKLIST.map((item, i) => (+ {checklist.map((item, i) => (<li key={item} className="flex items-center gap-3"><divclassName={cn(diff --git a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/__tests__/PreparingStep.test.tsx b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/__tests__/PreparingStep.test.tsxnew file mode 100644index 0000000000..0c7c9bef8d--- /dev/null+++ b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/__tests__/PreparingStep.test.tsx@@ -0,0 +1,81 @@+import { getGetBrainDumpStatusMockHandler200 } from "@/app/api/__generated__/endpoints/brain-dump/brain-dump.msw";+import { server } from "@/mocks/mock-server";+import { setIntroPath } from "@/services/onboarding/brain-dump-handoff";+import { cleanup, render, screen } from "@/tests/integrations/test-utils";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";+import { PreparingStep } from "../PreparingStep";++const INTRO_PATH_KEY = "autogpt:onboarding-intro-path";++const GENERIC_CHECKLIST = [+ "Personalizing your experience",+ "Connecting automation engines",+ "Building your space",+];+const DUMP_CHECKLIST = [+ "Reading your brain dump",+ "Briefing AutoPilot on your work",+ "Building your space",+ "Finding tools for your work",+];++beforeEach(() => {+ window.sessionStorage.removeItem(INTRO_PATH_KEY);+ // The dump path polls for pipeline status once the bar fills.+ server.use(+ getGetBrainDumpStatusMockHandler200(() => ({+ status: "completed" as const,+ greeting_ready: true,+ })),+ );+});++afterEach(() => {+ cleanup();+});++describe("PreparingStep checklist copy", () => {+ it("shows the brain-dump checklist on the dump path (intro path A, flag on)", async () => {+ setIntroPath("A");++ render(<PreparingStep onComplete={vi.fn()} isBrainDumpEnabled />);++ for (const item of DUMP_CHECKLIST) {+ expect(await screen.findByText(item)).toBeDefined();+ }+ expect(screen.queryByText("Personalizing your experience")).toBeNull();+ expect(screen.queryByText("Connecting automation engines")).toBeNull();+ });++ it("shows the generic checklist on the skip path (intro path B, flag on)", async () => {+ setIntroPath("B");++ render(<PreparingStep onComplete={vi.fn()} isBrainDumpEnabled />);++ for (const item of GENERIC_CHECKLIST) {+ expect(await screen.findByText(item)).toBeDefined();+ }+ expect(screen.queryByText("Reading your brain dump")).toBeNull();+ expect(screen.queryByText("Briefing AutoPilot on your work")).toBeNull();+ });++ it("shows the generic checklist when the brain-dump flag is off, even with a stale path A", async () => {+ setIntroPath("A");++ render(<PreparingStep onComplete={vi.fn()} isBrainDumpEnabled={false} />);++ for (const item of GENERIC_CHECKLIST) {+ expect(await screen.findByText(item)).toBeDefined();+ }+ expect(screen.queryByText("Reading your brain dump")).toBeNull();+ });++ it("shows the generic checklist when no intro path was recorded", async () => {+ render(<PreparingStep onComplete={vi.fn()} isBrainDumpEnabled />);++ for (const item of GENERIC_CHECKLIST) {+ expect(await screen.findByText(item)).toBeDefined();+ }+ expect(screen.queryByText("Reading your brain dump")).toBeNull();+ });+});diff --git a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/__tests__/usePreparingStep.test.ts b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/__tests__/usePreparingStep.test.tsnew file mode 100644index 0000000000..b247be35db--- /dev/null+++ b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/__tests__/usePreparingStep.test.ts@@ -0,0 +1,272 @@+import { getGetBrainDumpRecommendedProvidersMockHandler200 } from "@/app/api/__generated__/endpoints/brain-dump/brain-dump.msw";+import { server } from "@/mocks/mock-server";+import { setIntroPath } from "@/services/onboarding/brain-dump-handoff";+import { QueryClient, QueryClientProvider } from "@tanstack/react-query";+import { act, renderHook } from "@testing-library/react";+import { createElement, type ReactNode } from "react";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";+import { usePreparingStep } from "../usePreparingStep";++const START_DELAY_MS = 300;+const GENERIC_TOTAL_MS = 4_000;+const DUMP_TOTAL_MS = 10_000;+const RECOMMENDATIONS_MAX_WAIT_MS = 60_000;++const GENERIC_CHECKLIST = [+ "Personalizing your experience",+ "Connecting automation engines",+ "Building your space",+];+const DUMP_CHECKLIST = [+ "Reading your brain dump",+ "Briefing AutoPilot on your work",+ "Building your space",+ "Finding tools for your work",+];++function makeWrapper() {+ const client = new QueryClient({+ defaultOptions: { queries: { retry: false } },+ });+ return function Wrapper({ children }: { children: ReactNode }) {+ return createElement(QueryClientProvider, { client }, children);+ };+}++function renderPreparing(args: {+ onComplete?: () => void;+ isBrainDumpEnabled?: boolean;+}) {+ return renderHook(+ () =>+ usePreparingStep({+ onComplete: args.onComplete ?? vi.fn(),+ isBrainDumpEnabled: args.isBrainDumpEnabled ?? false,+ }),+ { wrapper: makeWrapper() },+ );+}++function mockRecommendations(ready: boolean) {+ server.use(+ getGetBrainDumpRecommendedProvidersMockHandler200({+ ready,+ providers: [],+ }),+ );+}++async function advance(ms: number) {+ await act(async () => {+ await vi.advanceTimersByTimeAsync(ms);+ });+}++beforeEach(() => {+ window.sessionStorage.clear();+ mockRecommendations(true);+ vi.useFakeTimers();+});++afterEach(() => {+ vi.useRealTimers();+});++describe("usePreparingStep — checklist copy", () => {+ it("uses the honest dump copy only when the user actually dumped", () => {+ setIntroPath("A");++ const { result } = renderPreparing({ isBrainDumpEnabled: true });++ expect(result.current.checklist).toEqual(DUMP_CHECKLIST);+ });++ it("falls back to the generic copy on the skip path and with the flag off", () => {+ setIntroPath("B");+ const skipped = renderPreparing({ isBrainDumpEnabled: true });+ expect(skipped.result.current.checklist).toEqual(GENERIC_CHECKLIST);++ setIntroPath("A");+ const flagOff = renderPreparing({ isBrainDumpEnabled: false });+ expect(flagOff.result.current.checklist).toEqual(GENERIC_CHECKLIST);+ });++ it("does not consume the intro path — the copilot home still needs it", () => {+ setIntroPath("A");++ renderPreparing({ isBrainDumpEnabled: true });++ expect(window.sessionStorage.getItem("autogpt:onboarding-intro-path")).toBe(+ "A",+ );+ });+});++describe("usePreparingStep — progress", () => {+ it("holds everything still until the entrance delay elapses", async () => {+ const { result } = renderPreparing({});++ expect(result.current.started).toBe(false);+ expect(result.current.progress).toBe(0);+ expect(result.current.completedItems).toBe(0);++ await advance(START_DELAY_MS - 50);+ expect(result.current.started).toBe(false);+ expect(result.current.progress).toBe(0);++ await advance(50);+ expect(result.current.started).toBe(true);+ });++ it("fills the bar in proportion to elapsed time and ticks the checklist along", async () => {+ const { result } = renderPreparing({});++ await advance(START_DELAY_MS);+ // Halfway: past the first of three ~1.3s slices, so item two is lit.+ await advance(GENERIC_TOTAL_MS / 2);++ expect(result.current.progress).toBe(50);+ expect(result.current.completedItems).toBe(2);++ await advance(GENERIC_TOTAL_MS / 4);++ expect(result.current.progress).toBe(75);+ expect(result.current.completedItems).toBe(3);+ });++ it("caps progress at 100 and the checklist at its own length", async () => {+ const { result } = renderPreparing({});++ await advance(START_DELAY_MS);+ await advance(GENERIC_TOTAL_MS + 5_000);++ expect(result.current.progress).toBe(100);+ expect(result.current.completedItems).toBe(result.current.checklist.length);+ });+});++describe("usePreparingStep — completion", () => {+ it("calls onComplete exactly once, and not before the full duration", async () => {+ const onComplete = vi.fn();+ renderPreparing({ onComplete });++ await advance(START_DELAY_MS);+ await advance(GENERIC_TOTAL_MS - 100);+ expect(onComplete).not.toHaveBeenCalled();++ await advance(100);+ expect(onComplete).toHaveBeenCalledTimes(1);++ await advance(5_000);+ expect(onComplete).toHaveBeenCalledTimes(1);+ });++ it("calls the latest onComplete, not the one captured on the first render", async () => {+ const stale = vi.fn();+ const fresh = vi.fn();+ const { rerender } = renderHook(+ ({ onComplete }) =>+ usePreparingStep({ onComplete, isBrainDumpEnabled: false }),+ { initialProps: { onComplete: stale }, wrapper: makeWrapper() },+ );++ await advance(START_DELAY_MS + GENERIC_TOTAL_MS / 2);+ rerender({ onComplete: fresh });+ await advance(GENERIC_TOTAL_MS);++ expect(stale).not.toHaveBeenCalled();+ expect(fresh).toHaveBeenCalledTimes(1);+ });++ it("stops the ticker on unmount so a navigated-away wizard never completes", async () => {+ const onComplete = vi.fn();+ const { unmount } = renderPreparing({ onComplete });++ await advance(START_DELAY_MS + 1_000);+ unmount();+ await advance(GENERIC_TOTAL_MS);++ expect(onComplete).not.toHaveBeenCalled();+ });+});++describe("usePreparingStep — recommendation gate (dump path)", () => {+ it("completes after the full duration once recommendations are ready", async () => {+ setIntroPath("A");+ const onComplete = vi.fn();+ renderPreparing({ onComplete, isBrainDumpEnabled: true });++ await advance(START_DELAY_MS);+ await advance(DUMP_TOTAL_MS - 100);+ expect(onComplete).not.toHaveBeenCalled();++ await advance(200);+ expect(onComplete).toHaveBeenCalledTimes(1);+ });++ it("holds the last step and the bar while the recommendation job is still running", async () => {+ setIntroPath("A");+ mockRecommendations(false);+ const onComplete = vi.fn();+ const { result } = renderPreparing({+ onComplete,+ isBrainDumpEnabled: true,+ });++ await advance(START_DELAY_MS);+ await advance(DUMP_TOTAL_MS + 5_000);++ expect(onComplete).not.toHaveBeenCalled();+ expect(result.current.completedItems).toBe(DUMP_CHECKLIST.length - 1);+ expect(result.current.progress).toBeLessThanOrEqual(95);+ });++ it("releases the gate on the poll that reports the job finished", async () => {+ setIntroPath("A");+ mockRecommendations(false);+ const onComplete = vi.fn();+ const { result } = renderPreparing({+ onComplete,+ isBrainDumpEnabled: true,+ });++ await advance(START_DELAY_MS);+ await advance(DUMP_TOTAL_MS + 2_000);+ expect(onComplete).not.toHaveBeenCalled();++ mockRecommendations(true);+ // Next poll (2.5s cadence) picks up ready=true; the response settles+ // at the end of the first window, so a second window lets the ticker+ // observe it and complete the run.+ await advance(3_000);+ await advance(1_000);++ expect(onComplete).toHaveBeenCalledTimes(1);+ expect(result.current.progress).toBe(100);+ expect(result.current.completedItems).toBe(DUMP_CHECKLIST.length);+ });++ it("never strands the user: advances anyway after the max wait ceiling", async () => {+ setIntroPath("A");+ mockRecommendations(false);+ const onComplete = vi.fn();+ renderPreparing({ onComplete, isBrainDumpEnabled: true });++ await advance(START_DELAY_MS);+ await advance(RECOMMENDATIONS_MAX_WAIT_MS + 1_000);++ expect(onComplete).toHaveBeenCalledTimes(1);+ });++ it("does not gate the generic path on the recommendation job", async () => {+ setIntroPath("B");+ mockRecommendations(false);+ const onComplete = vi.fn();+ renderPreparing({ onComplete, isBrainDumpEnabled: true });++ await advance(START_DELAY_MS);+ await advance(GENERIC_TOTAL_MS + 200);++ expect(onComplete).toHaveBeenCalledTimes(1);+ });+});diff --git a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/usePreparingStep.ts b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/usePreparingStep.tsnew file mode 100644index 0000000000..ae423c15bd--- /dev/null+++ b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/steps/usePreparingStep.ts@@ -0,0 +1,124 @@+import { useGetBrainDumpRecommendedProviders } from "@/app/api/__generated__/endpoints/brain-dump/brain-dump";+import { peekIntroPath } from "@/services/onboarding/brain-dump-handoff";+import { useEffect, useRef, useState } from "react";++const GENERIC_CHECKLIST = [+ "Personalizing your experience",+ "Connecting automation engines",+ "Building your space",+] as const;++// Honest copy for the dump path — these steps are things that are+// actually happening on the server while the bar fills. The last one+// gates on the provider-recommendation job so the connect dialog never+// opens on an empty "Recommended" section.+const BRAIN_DUMP_CHECKLIST = [+ "Reading your brain dump",+ "Briefing AutoPilot on your work",+ "Building your space",+ "Finding tools for your work",+] as const;++// The generic path keeps its original fixed duration; the dump path runs+// longer because its steps mirror real server work.+const GENERIC_DURATION_MS = 4_000;+const BRAIN_DUMP_DURATION_MS = 10_000;++const RECOMMENDATIONS_POLL_MS = 2_500;+// A job the backend never finished (process restart mid-run) must not+// strand the user on this screen — advance anyway after this ceiling.+const RECOMMENDATIONS_MAX_WAIT_MS = 60_000;+// While waiting on the job the bar parks just short of full so it still+// reads as "almost there", not "stuck at done".+const WAITING_PROGRESS_CAP = 95;++export function usePreparingStep({+ onComplete,+ isBrainDumpEnabled,+}: {+ onComplete: () => void;+ isBrainDumpEnabled: boolean;+}) {+ const [started, setStarted] = useState(false);+ const [completedItems, setCompletedItems] = useState(0);+ const [progress, setProgress] = useState(0);+ const onCompleteRef = useRef(onComplete);+ onCompleteRef.current = onComplete;++ // Only a user who actually dumped gets the honest copy; a skip would+ // make "Reading your brain dump" a lie.+ const isDumpPath = isBrainDumpEnabled && peekIntroPath() === "A";+ const checklist = isDumpPath ? BRAIN_DUMP_CHECKLIST : GENERIC_CHECKLIST;+ const duration = isDumpPath ? BRAIN_DUMP_DURATION_MS : GENERIC_DURATION_MS;+ const stepInterval = duration / checklist.length;++ const recommendedQuery = useGetBrainDumpRecommendedProviders({+ query: {+ enabled: isDumpPath,+ refetchInterval: (query) => {+ const response = query.state.data;+ if (response && (response.status !== 200 || response.data.ready)) {+ return false;+ }+ return RECOMMENDATIONS_POLL_MS;+ },+ },+ });+ // A non-200 answer counts as ready: the endpoint won't recover inside+ // this screen, and the dialog copes with an empty recommendation list.+ const isRecommendationsReady =+ !isDumpPath ||+ (recommendedQuery.data !== undefined &&+ (recommendedQuery.data.status !== 200 ||+ recommendedQuery.data.data.ready));+ const isRecommendationsReadyRef = useRef(isRecommendationsReady);+ isRecommendationsReadyRef.current = isRecommendationsReady;++ useEffect(() => {+ const timer = setTimeout(() => setStarted(true), 300);+ return () => clearTimeout(timer);+ }, []);++ useEffect(() => {+ if (!started) return;++ const startTime = Date.now();+ let finished = false;++ const progressInterval = setInterval(() => {+ const elapsed = Date.now() - startTime;+ const isWaitingOnRecommendations =+ !isRecommendationsReadyRef.current &&+ elapsed < RECOMMENDATIONS_MAX_WAIT_MS;++ if (elapsed >= duration && !isWaitingOnRecommendations && !finished) {+ finished = true;+ clearInterval(progressInterval);+ setProgress(100);+ setCompletedItems(checklist.length);+ onCompleteRef.current();+ return;+ }++ const pct = Math.min(100, (elapsed / duration) * 100);+ const items = Math.min(+ checklist.length,+ Math.floor(elapsed / stepInterval) + 1,+ );+ // The last step stays unchecked while its work is genuinely still+ // running server-side.+ setProgress(+ isWaitingOnRecommendations ? Math.min(pct, WAITING_PROGRESS_CAP) : pct,+ );+ setCompletedItems(+ isWaitingOnRecommendations+ ? Math.min(items, checklist.length - 1)+ : items,+ );+ }, 50);++ return () => clearInterval(progressInterval);+ }, [started, checklist.length, stepInterval, duration]);++ return { started, progress, completedItems, checklist };+}diff --git a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/store.ts b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/store.tsindex 98044ad850..182e125e46 100644--- a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/store.ts+++ b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/store.ts@@ -34,6 +34,11 @@ interface OnboardingWizardState {selectedBilling: "monthly" | "yearly";hasUserSelectedBilling: boolean;selectedCountryCode: string;+ /** True while the current step is mid-flight (e.g. the brain dump is+ * being processed) — navigation away must be blocked. Transient, never+ * persisted. */+ isStepBusy: boolean;+ setStepBusy(busy: boolean): void;setName(name: string): void;setRole(role: string): void;setOtherRole(otherRole: string): void;@@ -62,6 +67,10 @@ export const useOnboardingWizardStore = create<OnboardingWizardState>()(selectedBilling: "monthly",hasUserSelectedBilling: false,selectedCountryCode: "US",+ isStepBusy: false,+ setStepBusy(busy) {+ set({ isStepBusy: busy });+ },setName(name) {set({ name });},diff --git a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/useOnboardingPage.ts b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/useOnboardingPage.tsindex a7a32f4389..f1ec2f13f7 100644--- a/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/useOnboardingPage.ts+++ b/autogpt_platform/frontend/src/app/(no-navbar)/onboarding/useOnboardingPage.ts@@ -92,6 +92,16 @@ export function useOnboardingPage() {paymentEnabledSnapshot.current = livePaymentEnabled;}+ // Snapshotted for the same reason as the paywall flag: the brain dump+ // and the pillboxes occupy the same step, and swapping them under a+ // user who is already recording would drop the take.+ const liveBrainDumpEnabled = useGetFlag(Flag.ONBOARDING_BRAIN_DUMP);+ const brainDumpEnabledSnapshot = useRef<boolean | null>(null);+ if (brainDumpEnabledSnapshot.current === null && areFlagsReady) {+ brainDumpEnabledSnapshot.current = liveBrainDumpEnabled;+ }+ const isBrainDumpEnabled = brainDumpEnabledSnapshot.current ?? false;+// Skip the paywall for users already on a paid tier (admin grants or// pre-VISIT_COPILOT accounts) so they aren't asked to pay again to escape.const { data: tier, isLoading: isTierLoading } = useGetSubscriptionStatus({@@ -249,6 +259,7 @@ export function useOnboardingPage() {isLoading: isOnboardingStateLoading || !isReady,handlePreparingComplete,isPaymentEnabled,+ isBrainDumpEnabled,steps,preparingStep,totalSteps,diff --git a/autogpt_platform/frontend/src/app/(platform)/PlatformChrome/PlatformChrome.tsx b/autogpt_platform/frontend/src/app/(platform)/PlatformChrome/PlatformChrome.tsxindex 34d9f0b27c..6d93aebf41 100644--- a/autogpt_platform/frontend/src/app/(platform)/PlatformChrome/PlatformChrome.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/PlatformChrome/PlatformChrome.tsx@@ -13,6 +13,7 @@ import {SidebarTrigger,} from "@/components/ui/sidebar";+import { Flag, useGetFlag } from "@/services/feature-flags/use-get-flag";import { AdminImpersonationBanner } from "../admin/components/AdminImpersonationBanner";import { GlobalSearchOverlay } from "../components/GlobalSearchModal/GlobalSearchOverlay";import { PaywallGate } from "../PaywallGate/PaywallGate";@@ -32,6 +33,8 @@ export function PlatformChrome({ children }: Props) {hasInsetHeaderTitle,isSettingsRoute,} = usePlatformChrome();+ // The collapsed-by-default sidebar ships with the brain-dump experience.+ const isBrainDumpEnabled = useGetFlag(Flag.ONBOARDING_BRAIN_DUMP);const content = (<TopUpPromptProvider>@@ -58,6 +61,7 @@ export function PlatformChrome({ children }: Props) {if (showNewLayout) {return (<SidebarProvider+ defaultOpen={!isBrainDumpEnabled}style={{ "--sidebar-width": "18.25rem" } as CSSProperties}><AppSidebar />diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/CopilotPage.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/CopilotPage.tsxindex 4ae00de3a8..90f9d020c9 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/CopilotPage.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/CopilotPage.tsx@@ -43,6 +43,9 @@ export function CopilotPage() {const [droppedFiles, setDroppedFiles] = useState<File[]>([]);const isMobile = useIsMobile();const isArtifactsEnabled = useGetFlag(Flag.ARTIFACTS);+ // The brain-dump experience swaps the dotted backdrop + notification+ // opt-in dialog for the quieter greeting surface (banner to follow).+ const isBrainDumpEnabled = useGetFlag(Flag.ONBOARDING_BRAIN_DUMP);// Use the same mount-gated decision as PlatformChrome so the ChatSidebar is// hidden in lockstep with the layout swap — avoids a one-frame flash where// the classic shell renders without its sidebar before the new layout mounts.@@ -89,6 +92,7 @@ export function CopilotPage() {<MainAreaisMobile={isMobile}isArtifactsEnabled={isArtifactsEnabled}+ isBrainDumpEnabled={Boolean(isBrainDumpEnabled)}sessionId={sessionId}droppedFiles={droppedFiles}setDroppedFiles={setDroppedFiles}@@ -98,7 +102,7 @@ export function CopilotPage() {)}{isMobile && isArtifactsEnabled && <ArtifactPanel mobile />}{isMobile && !showNewLayout && <MobileDrawer />}- <NotificationDialog />+ {!isBrainDumpEnabled && <NotificationDialog />}<CopilotModals /></SidebarProvider>);@@ -107,6 +111,7 @@ export function CopilotPage() {interface MainAreaProps {isMobile: boolean;isArtifactsEnabled: boolean;+ isBrainDumpEnabled: boolean;sessionId: string | null;droppedFiles: File[];setDroppedFiles: (files: File[]) => void;@@ -115,6 +120,7 @@ interface MainAreaProps {function MainArea({isMobile,isArtifactsEnabled,+ isBrainDumpEnabled,sessionId,droppedFiles,setDroppedFiles,@@ -123,7 +129,7 @@ function MainArea({return (<div className="flex h-full w-full flex-row overflow-hidden"><div className="relative flex min-w-0 flex-1 overflow-hidden bg-[#fafafa]">- {hasSession && (+ {!isBrainDumpEnabled && hasSession && (<DotDistortionShaderdotGap={14}dotSize={1}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/ChatInput.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/ChatInput.tsxindex 9d9afb1da6..3312c0028b 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/ChatInput.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/ChatInput.tsx@@ -32,6 +32,7 @@ import {workspaceItemToAttachment,} from "../../helpers/workspaceAttachments";import { ComposerPlusMenu } from "./components/ComposerPlusMenu";+import { ComposerTray } from "./components/ComposerTray";import { DryRunToggleButton } from "./components/DryRunToggleButton";import { FileChips } from "./components/FileChips";import { MentionDropdown } from "./components/MentionDropdown";@@ -44,6 +45,7 @@ import { useCopilotUIStore } from "../../store";import { getFilesFromClipboard } from "./helpers";import { useChatInput } from "./useChatInput";import { useChatMentions } from "./useChatMentions";+import { useOnboardingMicGlow } from "./useOnboardingMicGlow";import { useVoiceRecording } from "./useVoiceRecording";import { ArrowUp02Icon } from "@hugeicons/core-free-icons";import { Icon } from "@/components/atoms/Icon/Icon";@@ -69,6 +71,8 @@ interface Props {onDroppedFilesConsumed?: () => void;/** When true, the dry-run toggle is disabled (session is active and immutable). */hasSession?: boolean;+ /** When true, the submit button is hidden until there is something to send. */+ hideSubmitWhenEmpty?: boolean;/** Recipient picker chip rendered before the mode chips (new-task state). */recipientPicker?: ReactNode;}@@ -86,6 +90,7 @@ export function ChatInput({droppedFiles,onDroppedFilesConsumed,hasSession = false,+ hideSubmitWhenEmpty = false,recipientPicker,}: Props) {const {@@ -219,6 +224,15 @@ export function ChatInput({isStreaming,});+ const { isGlowing: isMicGlowing, dismissGlow } = useOnboardingMicGlow({+ isTranscribing,+ });++ // The composer restyle (flat card, chips relocated into the tray+ // below) ships with the brain-dump experience; off keeps the original+ // glowing composer with pill toggles in the footer.+ const isBrainDumpEnabled = useGetFlag(Flag.ONBOARDING_BRAIN_DUMP);+function handleChange(e: ChangeEvent<HTMLTextAreaElement>) {if (isRecording) return;baseHandleChange(e);@@ -244,6 +258,9 @@ export function ChatInput({? "Transcribing...": placeholder;+ const hasTrayItems =+ (showModeToggle && !isStreaming) || (showDryRunToggle && !hasSession);+const canSend =!disabled &&(!!value.trim() || hasAttachments) &&@@ -295,10 +312,16 @@ export function ChatInput({)}<InputGroupclassName={cn(- "overflow-hidden border-zinc-200 has-[[data-slot=input-group-control]:focus-visible]:border-neutral-200 has-[[data-slot=input-group-control]:focus-visible]:ring-0",- "shadow-[0_2px_8px_rgba(0,0,0,0.04),0_0_32px_-4px_rgba(99,102,241,0.4)] transition-shadow has-[[data-slot=input-group-control]:focus-visible]:shadow-[0_2px_8px_rgba(0,0,0,0.04),0_0_36px_-4px_rgba(99,102,241,0.45)]",+ isBrainDumpEnabled+ ? "relative z-10 overflow-hidden border-neutral-200 shadow-none has-[[data-slot=input-group-control]:focus-visible]:border-neutral-200 has-[[data-slot=input-group-control]:focus-visible]:ring-0"+ : [+ "overflow-hidden border-zinc-200 has-[[data-slot=input-group-control]:focus-visible]:border-neutral-200 has-[[data-slot=input-group-control]:focus-visible]:ring-0",+ "shadow-[0_2px_8px_rgba(0,0,0,0.04),0_0_32px_-4px_rgba(99,102,241,0.4)] transition-shadow has-[[data-slot=input-group-control]:focus-visible]:shadow-[0_2px_8px_rgba(0,0,0,0.04),0_0_36px_-4px_rgba(99,102,241,0.45)]",+ ],isRecording &&- "border-red-400 shadow-[0_2px_8px_rgba(0,0,0,0.04),0_0_32px_-4px_rgba(248,113,113,0.45)] ring-1 ring-red-400 has-[[data-slot=input-group-control]:focus-visible]:border-red-400 has-[[data-slot=input-group-control]:focus-visible]:shadow-[0_2px_8px_rgba(0,0,0,0.04),0_0_32px_-4px_rgba(248,113,113,0.45)] has-[[data-slot=input-group-control]:focus-visible]:ring-red-400",+ (isBrainDumpEnabled+ ? "border-red-400 ring-1 ring-red-400 has-[[data-slot=input-group-control]:focus-visible]:border-red-400 has-[[data-slot=input-group-control]:focus-visible]:ring-red-400"+ : "border-red-400 shadow-[0_2px_8px_rgba(0,0,0,0.04),0_0_32px_-4px_rgba(248,113,113,0.45)] ring-1 ring-red-400 has-[[data-slot=input-group-control]:focus-visible]:border-red-400 has-[[data-slot=input-group-control]:focus-visible]:shadow-[0_2px_8px_rgba(0,0,0,0.04),0_0_32px_-4px_rgba(248,113,113,0.45)] has-[[data-slot=input-group-control]:focus-visible]:ring-red-400"),)}><FileChips@@ -341,28 +364,24 @@ export function ChatInput({disabled={isBusy}/>{recipientPicker}- {/* Mode and model are per-message settings sent with each stream request,- so they can be freely changed between turns in an existing session.- Hide only while actively streaming (too late to change for that turn). */}- {showModeToggle && !isStreaming && (- <ModeToggleButton- mode={copilotChatMode}- onToggle={handleToggleMode}- pinned={copilotModePinned}- />- )}- {showModeToggle && !isStreaming && (- <ModelToggleButton- model={copilotLlmModel}- onToggle={handleToggleModel}- />+ {!isBrainDumpEnabled && showModeToggle && !isStreaming && (+ <>+ <ModeToggleButton+ variant="pill"+ mode={copilotChatMode}+ onToggle={handleToggleMode}+ pinned={copilotModePinned}+ />+ <ModelToggleButton+ variant="pill"+ model={copilotLlmModel}+ onToggle={handleToggleModel}+ />+ </>)}- {/* DryRun button only on new chats: once a session exists its- dry_run flag is locked and should be read from session metadata- (sessionDryRun in useCopilotPage), not toggled here. The banner- in CopilotPage.tsx reflects the actual session state. */}- {showDryRunToggle && !hasSession && (+ {!isBrainDumpEnabled && showDryRunToggle && !hasSession && (<DryRunToggleButton+ variant="pill"isDryRun={isDryRun}onToggle={handleToggleDryRun}/>@@ -376,7 +395,11 @@ export function ChatInput({isTranscribing={isTranscribing}isStreaming={isStreaming}disabled={disabled || isTranscribing || isStreaming}- onClick={toggleRecording}+ highlight={isMicGlowing}+ onClick={() => {+ dismissGlow();+ toggleRecording();+ }}/>)}{isStreaming && canSend && onEnqueue && (@@ -410,13 +433,43 @@ export function ChatInput({</TooltipTrigger><TooltipContent side="top">Stop</TooltipContent></Tooltip>- ) : (+ ) : hideSubmitWhenEmpty && !canSend ? null : (<PromptInputSubmit disabled={!canSend} />)}</div></PromptInputFooter></InputGroup>+ {/* Mode and model are per-message settings sent with each stream request,+ so they can be freely changed between turns in an existing session.+ Hide only while actively streaming (too late to change for that turn).+ DryRun is new-chat only: once a session exists its dry_run flag is+ locked and read from session metadata (sessionDryRun in useCopilotPage),+ with the banner in CopilotPage.tsx reflecting the actual state. */}+ {Boolean(isBrainDumpEnabled) && hasTrayItems && (+ <ComposerTray>+ {showModeToggle && !isStreaming && (+ <>+ <ModeToggleButton+ mode={copilotChatMode}+ onToggle={handleToggleMode}+ pinned={copilotModePinned}+ />+ <ModelToggleButton+ model={copilotLlmModel}+ onToggle={handleToggleModel}+ />+ </>+ )}+ {showDryRunToggle && !hasSession && (+ <DryRunToggleButton+ isDryRun={isDryRun}+ onToggle={handleToggleDryRun}+ />+ )}+ </ComposerTray>+ )}+{showWorkspaceFiles && (<WorkspaceFilePickerisOpen={isPickerOpen}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/ComposerTray.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/ComposerTray.tsxnew file mode 100644index 0000000000..52f7a7c865--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/ComposerTray.tsx@@ -0,0 +1,18 @@+"use client";++import type { ReactNode } from "react";++interface Props {+ children: ReactNode;+}++// Secondary row tucked under the composer card (Claude-style): the card+// overlaps its top edge, so the tray only peeks out below. Rounded on its+// own bottom edge so it also reads correctly outside a clipping wrapper.+export function ComposerTray({ children }: Props) {+ return (+ <div className="-mt-5 flex items-center gap-2 rounded-b-xlarge bg-[#EFEFF0] px-3 pb-1.5 pt-[1.625rem]">+ {children}+ </div>+ );+}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/DryRunToggleButton.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/DryRunToggleButton.tsxindex da0625dabf..b711c60260 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/DryRunToggleButton.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/DryRunToggleButton.tsx@@ -1,48 +1,70 @@"use client";-import { cn } from "@/lib/utils";import {Tooltip,TooltipContent,TooltipTrigger,} from "@/components/ui/tooltip";-import { FlaskConicalIcon } from "@hugeicons/core-free-icons";import { Icon } from "@/components/atoms/Icon/Icon";+import { ToggleChip } from "@/components/atoms/ToggleChip/ToggleChip";+import { cn } from "@/lib/utils";+import { FlaskConicalIcon } from "@hugeicons/core-free-icons";// This button is only rendered on NEW chats (no active session).// Once a session exists, it is hidden — the session's dry_run flag is-// immutable and reflected in the banner in CopilotPage.tsx instead.-// Do NOT add readOnly/hasSession handling here; hide it at the call site.+// locked and surfaced via the banner in CopilotPage.tsx instead.+interface Props {isDryRun: boolean;onToggle: () => void;+ /** "pill" is the pre-brain-dump footer style; "chip" lives in the tray. */+ variant?: "chip" | "pill";}-export function DryRunToggleButton({ isDryRun, onToggle }: Props) {+export function DryRunToggleButton({+ isDryRun,+ onToggle,+ variant = "chip",+}: Props) {+ const tooltip = isDryRun+ ? "Test mode on — new sessions run without performing real actions (click to turn off)."+ : "Turn on test mode to try prompts without performing real actions.";+ const ariaLabel = isDryRun ? "Test mode active" : "Enable Test mode";++ if (variant === "pill") {+ return (+ <Tooltip>+ <TooltipTrigger asChild>+ <button+ type="button"+ aria-pressed={isDryRun}+ onClick={onToggle}+ className={cn(+ "inline-flex h-9 items-center justify-center gap-1 rounded-full border border-neutral-200 bg-white px-2.5 text-xs font-medium shadow-sm transition-colors hover:bg-neutral-50",+ isDryRun ? "text-amber-900" : "text-zinc-950 hover:text-zinc-950",+ )}+ aria-label={ariaLabel}+ >+ <Icon icon={FlaskConicalIcon} size={14} />+ <span className="hidden sm:inline">+ {isDryRun ? "Test mode enabled" : "Enable test mode"}+ </span>+ </button>+ </TooltipTrigger>+ <TooltipContent>{tooltip}</TooltipContent>+ </Tooltip>+ );+ }+return (- <Tooltip>- <TooltipTrigger asChild>- <button- type="button"- aria-pressed={isDryRun}- onClick={onToggle}- className={cn(- "inline-flex h-9 items-center justify-center gap-1 rounded-full border border-neutral-200 bg-white px-2.5 text-xs font-medium shadow-sm transition-colors hover:bg-neutral-50",- isDryRun ? "text-amber-900" : "text-zinc-950 hover:text-zinc-950",- )}- aria-label={isDryRun ? "Test mode active" : "Enable Test mode"}- >- <Icon icon={FlaskConicalIcon} size={14} />- <span className="hidden sm:inline">- {isDryRun ? "Test mode enabled" : "Enable test mode"}- </span>- </button>- </TooltipTrigger>- <TooltipContent>- {isDryRun- ? "Test mode on — new sessions run without performing real actions (click to turn off)."- : "Turn on test mode to try prompts without performing real actions."}- </TooltipContent>- </Tooltip>+ <ToggleChip+ icon={<Icon icon={FlaskConicalIcon} size={14} />}+ label={isDryRun ? "Test mode enabled" : "Enable test mode"}+ tooltip={tooltip}+ ariaLabel={ariaLabel}+ pressed={isDryRun}+ onToggle={onToggle}+ className="sm:min-w-[9.5rem]"+ />);}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/ModeToggleButton.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/ModeToggleButton.tsxindex 4aa51d0ca0..8481227b24 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/ModeToggleButton.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/ModeToggleButton.tsx@@ -1,70 +1,88 @@"use client";-import { cn } from "@/lib/utils";import {Tooltip,TooltipContent,TooltipTrigger,} from "@/components/ui/tooltip";-import type { CopilotMode } from "../../../store";-import { BrainIcon, FlashIcon, LockIcon } from "@hugeicons/core-free-icons";import { Icon } from "@/components/atoms/Icon/Icon";+import { ToggleChip } from "@/components/atoms/ToggleChip/ToggleChip";+import { cn } from "@/lib/utils";+import { BrainIcon, FlashIcon, LockIcon } from "@hugeicons/core-free-icons";+import type { CopilotMode } from "../../../store";interface Props {mode: CopilotMode;onToggle: () => void;+ /** When true the mode is locked to Extended Thinking (agent build flow). */pinned?: boolean;+ /** "pill" is the pre-brain-dump footer style; "chip" lives in the tray. */+ variant?: "chip" | "pill";}-export function ModeToggleButton({ mode, onToggle, pinned = false }: Props) {+export function ModeToggleButton({+ mode,+ onToggle,+ pinned = false,+ variant = "chip",+}: Props) {const isExtended = mode === "extended_thinking";-const tooltipText = pinned- ? "Locked to Extended Thinking — building sessions stay on this engine"+ ? "Extended Thinking is required while building an agent": isExtended? "Extended Thinking — deeper reasoning (click to switch to Fast)": "Fast mode — quicker responses (click to switch to Thinking)";+ const ariaLabel = pinned+ ? "Mode locked to Extended Thinking while building an agent"+ : isExtended+ ? "Switch to Fast mode"+ : "Switch to Extended Thinking mode";++ function getIcon() {+ if (pinned) return <Icon icon={LockIcon} size={14} />;+ return isExtended ? (+ <Icon icon={BrainIcon} size={14} />+ ) : (+ <Icon icon={FlashIcon} size={14} />+ );+ }++ if (variant === "pill") {+ return (+ <Tooltip>+ <TooltipTrigger asChild>+ <button+ type="button"+ aria-pressed={isExtended}+ aria-disabled={pinned}+ onClick={onToggle}+ className={cn(+ "ml-2 inline-flex h-9 min-w-[6rem] items-center justify-center gap-1 rounded-full border border-neutral-200 bg-white px-2.5 text-xs font-medium shadow-sm transition-colors hover:bg-neutral-50",+ isExtended ? "text-purple-500" : "text-orange-600",+ pinned && "cursor-not-allowed opacity-70 hover:bg-white",+ )}+ aria-label={ariaLabel}+ >+ {getIcon()}+ {pinned || isExtended ? "Thinking" : "Fast"}+ </button>+ </TooltipTrigger>+ <TooltipContent>{tooltipText}</TooltipContent>+ </Tooltip>+ );+ }+return (- <Tooltip>- <TooltipTrigger asChild>- <button- type="button"- aria-pressed={isExtended}- aria-disabled={pinned}- onClick={onToggle}- className={cn(- "ml-2 inline-flex h-9 min-w-[6rem] items-center justify-center gap-1 rounded-full border border-neutral-200 bg-white px-2.5 text-xs font-medium shadow-sm transition-colors hover:bg-neutral-50",- isExtended ? "text-purple-500" : "text-orange-600",- pinned && "cursor-not-allowed opacity-70 hover:bg-white",- )}- aria-label={- pinned- ? "Mode locked to Extended Thinking while building an agent"- : isExtended- ? "Switch to Fast mode"- : "Switch to Extended Thinking mode"- }- >- {pinned ? (- <>- <Icon icon={LockIcon} size={14} />- Thinking- </>- ) : isExtended ? (- <>- <Icon icon={BrainIcon} size={14} />- Thinking- </>- ) : (- <>- <Icon icon={FlashIcon} size={14} />- Fast- </>- )}- </button>- </TooltipTrigger>- <TooltipContent>{tooltipText}</TooltipContent>- </Tooltip>+ <ToggleChip+ icon={getIcon()}+ label={pinned || isExtended ? "Thinking" : "Fast"}+ tooltip={tooltipText}+ ariaLabel={ariaLabel}+ pressed={isExtended}+ onToggle={onToggle}+ locked={pinned}+ className="sm:min-w-[5.5rem]"+ />);}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/ModelToggleButton.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/ModelToggleButton.tsxindex 30fda8a85f..36c18b0f5f 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/ModelToggleButton.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/ModelToggleButton.tsx@@ -1,48 +1,72 @@"use client";-import { cn } from "@/lib/utils";import {Tooltip,TooltipContent,TooltipTrigger,} from "@/components/ui/tooltip";-import type { CopilotLlmModel } from "../../../store";-import { CpuIcon } from "@hugeicons/core-free-icons";import { Icon } from "@/components/atoms/Icon/Icon";+import { ToggleChip } from "@/components/atoms/ToggleChip/ToggleChip";+import { cn } from "@/lib/utils";+import { CpuIcon } from "@hugeicons/core-free-icons";+import type { CopilotLlmModel } from "../../../store";interface Props {model: CopilotLlmModel;onToggle: () => void;+ /** "pill" is the pre-brain-dump footer style; "chip" lives in the tray. */+ variant?: "chip" | "pill";}-export function ModelToggleButton({ model, onToggle }: Props) {+export function ModelToggleButton({+ model,+ onToggle,+ variant = "chip",+}: Props) {const isAdvanced = model === "advanced";+ const tooltip = isAdvanced+ ? "Using the highest-capability model (click to switch to Balanced)."+ : "Using the balanced default model (click to switch to Advanced).";+ const ariaLabel = isAdvanced+ ? "Switch to Balanced model"+ : "Switch to Advanced model";++ if (variant === "pill") {+ return (+ <Tooltip>+ <TooltipTrigger asChild>+ <button+ type="button"+ aria-pressed={isAdvanced}+ onClick={onToggle}+ className={cn(+ "inline-flex h-9 items-center justify-center gap-1 rounded-full border border-neutral-200 bg-white px-2.5 text-xs font-medium shadow-sm transition-colors hover:bg-neutral-50",+ isAdvanced+ ? "text-emerald-500"+ : "text-sky-600 hover:text-sky-700",+ )}+ aria-label={ariaLabel}+ >+ <Icon icon={CpuIcon} size={14} />+ <span className="hidden sm:inline">+ {isAdvanced ? "Advanced" : "Balanced"}+ </span>+ </button>+ </TooltipTrigger>+ <TooltipContent>{tooltip}</TooltipContent>+ </Tooltip>+ );+ }+return (- <Tooltip>- <TooltipTrigger asChild>- <button- type="button"- aria-pressed={isAdvanced}- onClick={onToggle}- className={cn(- "inline-flex h-9 items-center justify-center gap-1 rounded-full border border-neutral-200 bg-white px-2.5 text-xs font-medium shadow-sm transition-colors hover:bg-neutral-50",- isAdvanced ? "text-emerald-500" : "text-sky-600 hover:text-sky-700",- )}- aria-label={- isAdvanced ? "Switch to Balanced model" : "Switch to Advanced model"- }- >- <Icon icon={CpuIcon} size={14} />- <span className="hidden sm:inline">- {isAdvanced ? "Advanced" : "Balanced"}- </span>- </button>- </TooltipTrigger>- <TooltipContent>- {isAdvanced- ? "Using the highest-capability model (click to switch to Balanced)."- : "Using the balanced default model (click to switch to Advanced)."}- </TooltipContent>- </Tooltip>+ <ToggleChip+ icon={<Icon icon={CpuIcon} size={14} />}+ label={isAdvanced ? "Advanced" : "Balanced"}+ tooltip={tooltip}+ ariaLabel={ariaLabel}+ pressed={isAdvanced}+ onToggle={onToggle}+ className="sm:min-w-[5.75rem]"+ />);}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/RecordingButton.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/RecordingButton.tsxindex 83f6df5a6e..f3873ce774 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/RecordingButton.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/RecordingButton.tsx@@ -11,6 +11,10 @@ interface Props {isStreaming: boolean;disabled: boolean;onClick: () => void;+ // One-time highlight for the user who skipped the onboarding brain+ // dump: AutoPilot's intro invites them to record, so the button it+ // points at has to be findable.+ highlight?: boolean;}export function RecordingButton({@@ -19,6 +23,7 @@ export function RecordingButton({isStreaming,disabled,onClick,+ highlight = false,}: Props) {return (<Button@@ -34,6 +39,9 @@ export function RecordingButton({isRecording && "animate-pulse bg-red-500 text-white hover:bg-red-600",isTranscribing && "bg-zinc-100 text-zinc-400",isStreaming && "opacity-40",+ highlight &&+ !isRecording &&+ "border-purple-300 text-purple-600 ring-4 ring-purple-100",)}>{isTranscribing ? (diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/useOnboardingMicGlow.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/useOnboardingMicGlow.tsnew file mode 100644index 0000000000..c84f7f1bd8--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/useOnboardingMicGlow.ts@@ -0,0 +1,40 @@+import { trackBrainDump } from "@/services/onboarding/brain-dump-analytics";+import {+ takeMicGlow,+ takePendingLaterDump,+} from "@/services/onboarding/brain-dump-handoff";+import { useEffect, useRef, useState } from "react";++// Long enough to survive AutoPilot's intro streaming in, short enough that+// it reads as a pointer rather than a permanent state.+const GLOW_DURATION_MS = 30_000;++export function useOnboardingMicGlow({+ isTranscribing,+}: {+ isTranscribing: boolean;+}) {+ const [isGlowing, setIsGlowing] = useState(false);++ useEffect(() => {+ if (!takeMicGlow()) return;+ setIsGlowing(true);+ const timer = setTimeout(() => setIsGlowing(false), GLOW_DURATION_MS);+ return () => clearTimeout(timer);+ }, []);++ // A user who skipped the onboarding dump is invited by AutoPilot's Path+ // B intro to record one here instead. Transcription finishing is the+ // closest thing to "they took the invitation" — the flag is consumed on+ // the first one, so later voice messages aren't counted as the dump.+ const wasTranscribingRef = useRef(false);+ useEffect(() => {+ const justFinished = wasTranscribingRef.current && !isTranscribing;+ wasTranscribingRef.current = isTranscribing;+ if (justFinished && takePendingLaterDump()) {+ trackBrainDump("later_dump_completed");+ }+ }, [isTranscribing]);++ return { isGlowing, dismissGlow: () => setIsGlowing(false) };+}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/useVoiceRecording.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/useVoiceRecording.tsindex 655ec6df1d..b520bbec07 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/useVoiceRecording.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/useVoiceRecording.ts@@ -1,4 +1,5 @@import { useToast } from "@/components/molecules/Toast/use-toast";+import { Flag, useGetFlag } from "@/services/feature-flags/use-get-flag";import React, {KeyboardEvent,useCallback,@@ -37,6 +38,16 @@ export function useVoiceRecording({const isRecordingRef = useRef(false);const [isSupported, setIsSupported] = useState(false);+ // Sending the draft as transcription context ships with the brain-dump+ // experience (Path B records a dump on top of AutoPilot's intro text).+ const isBrainDumpEnabled = useGetFlag(Flag.ONBOARDING_BRAIN_DUMP);+ const isBrainDumpEnabledRef = useRef(isBrainDumpEnabled);+ isBrainDumpEnabledRef.current = isBrainDumpEnabled;+ const valueRef = useRef(value);++ useEffect(() => {+ valueRef.current = value;+ }, [value]);useEffect(() => {setIsSupported(@@ -83,6 +94,10 @@ export function useVoiceRecording({try {const formData = new FormData();formData.append("audio", audioBlob);+ const draft = valueRef.current.trim();+ if (isBrainDumpEnabledRef.current && draft) {+ formData.append("context", draft);+ }const response = await fetch("/api/transcribe", {method: "POST",diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/ChatMessagesContainer.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/ChatMessagesContainer.tsxindex af50be126d..0f37bf2d43 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/ChatMessagesContainer.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/ChatMessagesContainer.tsx@@ -334,6 +334,8 @@ export function ChatMessagesContainer({// sidebar surface — hide it entirely when the task bar is on.const isTaskBarEnabled = useGetFlag(Flag.TASK_PROGRESS_BAR);const isContextPanelEnabled = useGetFlag(Flag.ARTIFACTS);+ // Bubble restyle ships with the brain-dump experience.+ const isBrainDumpEnabled = useGetFlag(Flag.ONBOARDING_BRAIN_DUMP);const isChatStreaming = status === "streaming" || status === "submitted";const hasActiveTaskList =!isTaskBarEnabled &&@@ -629,7 +631,9 @@ export function ChatMessagesContainer({<MessageContentclassName={"text-[1rem] leading-relaxed " +- "group-[.is-user]:rounded-xl group-[.is-user]:bg-purple-100 group-[.is-user]:px-3 group-[.is-user]:py-2.5 group-[.is-user]:text-slate-900 group-[.is-user]:[border-bottom-right-radius:0] " ++ (isBrainDumpEnabled+ ? "group-[.is-user]:rounded-3xl group-[.is-user]:bg-gradient-to-br group-[.is-user]:from-[#f3edff] group-[.is-user]:to-[#e4d4ff] group-[.is-user]:px-4 group-[.is-user]:py-3 group-[.is-user]:text-[#3b1e75] group-[.is-user]:[border-bottom-right-radius:0.5rem] "+ : "group-[.is-user]:rounded-xl group-[.is-user]:bg-purple-100 group-[.is-user]:px-3 group-[.is-user]:py-2.5 group-[.is-user]:text-slate-900 group-[.is-user]:[border-bottom-right-radius:0] ") +"group-[.is-user]:[&_h1]:text-lg group-[.is-user]:[&_h1]:font-semibold group-[.is-user]:[&_h2]:text-lg group-[.is-user]:[&_h2]:font-semibold group-[.is-user]:[&_h3]:text-lg group-[.is-user]:[&_h3]:font-semibold group-[.is-user]:[&_h4]:text-lg group-[.is-user]:[&_h4]:font-semibold group-[.is-user]:[&_h5]:text-lg group-[.is-user]:[&_h5]:font-semibold group-[.is-user]:[&_h6]:text-lg group-[.is-user]:[&_h6]:font-semibold " +"group-[.is-assistant]:bg-transparent group-[.is-assistant]:text-slate-900"}@@ -780,7 +784,13 @@ export function ChatMessagesContainer({{!readOnly &&queuedMessages?.map((msg, idx) => (<Message key={idx} from="user">- <MessageContent className="flex flex-col gap-1 rounded-xl border border-dashed border-purple-400 bg-purple-100 px-3 py-2.5 text-[1rem] leading-relaxed text-slate-900 opacity-60 [border-bottom-right-radius:0]">+ <MessageContent+ className={+ isBrainDumpEnabled+ ? "flex flex-col gap-1 rounded-3xl border border-dashed border-[#b18aff] bg-gradient-to-br from-[#f3edff] to-[#e4d4ff] px-4 py-3 text-[1rem] leading-relaxed text-[#3b1e75] opacity-60 [border-bottom-right-radius:0.5rem]"+ : "flex flex-col gap-1 rounded-xl border border-dashed border-purple-400 bg-purple-100 px-3 py-2.5 text-[1rem] leading-relaxed text-slate-900 opacity-60 [border-bottom-right-radius:0]"+ }+ ><span>{msg}</span><span className="flex items-center gap-1 text-xs text-slate-500"><Icon icon={Clock01Icon} className="size-3" />diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/EmptySession/EmptySession.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/EmptySession/EmptySession.tsxindex 641e33bc0a..abb10d2efb 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/EmptySession/EmptySession.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/EmptySession/EmptySession.tsx@@ -3,10 +3,9 @@import { ChatInput } from "@/app/(platform)/copilot/components/ChatInput/ChatInput";import { useGetV2GetSuggestedPrompts } from "@/app/api/__generated__/endpoints/chat/chat";import { Skeleton } from "@/components/atoms/Skeleton/Skeleton";-import { Text } from "@/components/atoms/Text/Text";import { useAuth } from "@/lib/auth/hooks/useAuth";import { DotDistortionShader } from "@/components/ui/dot-distortion-shader";-import { TextGenerateEffect } from "@/components/ui/text-generate-effect";+import { cn } from "@/lib/utils";import { motion } from "framer-motion";import { useEffect, useState } from "react";import {@@ -15,11 +14,14 @@ import {getSuggestionThemes,} from "./helpers";import { SuggestionThemes } from "./components/SuggestionThemes/SuggestionThemes";+import { OnboardingIntroCard } from "../OnboardingIntroCard/OnboardingIntroCard";+import { OnboardingWelcomeDialog } from "../OnboardingWelcomeDialog/OnboardingWelcomeDialog";+import { useOnboardingIntroCard } from "../OnboardingIntroCard/useOnboardingIntroCard";import { PulseChips } from "../PulseChips/PulseChips";import { usePulseChips } from "../PulseChips/usePulseChips";import { Flag, useGetFlag } from "@/services/feature-flags/use-get-flag";import type { WorkspaceAttachment } from "../../helpers/workspaceAttachments";-import { EditNameDialog } from "./components/EditNameDialog/EditNameDialog";+import { EmptyHero } from "./components/EmptyHero";import { RecipientChip } from "../ChatInput/components/RecipientChip";import { useRecipientPicker } from "./useRecipientPicker";@@ -49,6 +51,8 @@ export function EmptySession({}: Props) {const { user } = useAuth();const greetingName = getGreetingName(user);+ const intro = useOnboardingIntroCard();+ const isBrainDumpEnabled = useGetFlag(Flag.ONBOARDING_BRAIN_DUMP);const isAgentBriefingEnabled = useGetFlag(Flag.AGENT_BRIEFING);const isExpertsEnabled = useGetFlag(Flag.HIRE_EXPERTS);const pulseChips = usePulseChips();@@ -86,14 +90,28 @@ export function EmptySession({}, []);return (- <div className="relative flex h-full flex-1 items-center justify-center overflow-y-auto px-0 py-5 md:px-6 md:py-10">- <DotDistortionShader- dotGap={14}- dotSize={1}- opacity={0.2}- enableMouseInteraction={false}- breathingSpeed={0.4}- className="pointer-events-none absolute inset-0 !bg-transparent [&_canvas]:opacity-70"+ <div+ className={cn(+ "relative flex h-full flex-1 justify-center overflow-y-auto px-0 py-5 md:px-6 md:py-10",+ // The whole greeting flow reads top-down like a letter, so it+ // anchors to the top from its first visible frame; the regular+ // hero stays vertically centered.+ intro.anchorTop ? "items-start" : "items-center",+ )}+ >+ {!isBrainDumpEnabled && (+ <DotDistortionShader+ dotGap={14}+ dotSize={1}+ opacity={0.2}+ enableMouseInteraction={false}+ breathingSpeed={0.4}+ className="pointer-events-none absolute inset-0 !bg-transparent [&_canvas]:opacity-70"+ />+ )}+ <OnboardingWelcomeDialog+ isOpen={intro.isWelcomeOpen}+ onClose={intro.closeWelcome}/><motion.divclassName="relative z-10 w-full max-w-[52rem] text-center"@@ -102,63 +120,110 @@ export function EmptySession({transition={{ duration: 0.3 }}><div className="mx-auto max-w-[52rem]">- <Text variant="h3" className="mb-1 !text-[1.375rem] text-zinc-700">- Hey, <span className="text-violet-600">{greetingName}</span>- <EditNameDialog currentName={greetingName} />- </Text>- <TextGenerateEffect- className="mb-8 !font-normal [&>div]:!mt-0 [&_div]:!text-[1.375rem] [&_div]:!leading-normal [&_div]:!tracking-normal"- duration={0.6}- words="Tell me about your work — I'll find what to automate."- />-- {isAgentBriefingEnabled && (- <PulseChips chips={pulseChips} onChipClick={onSend} />+ {intro.isVisible ? (+ <OnboardingIntroCard+ name={greetingName}+ greeting={intro.greeting}+ prompts={intro.prompts}+ transcript={intro.transcript}+ onSelectPrompt={onSend}+ disabled={isComposerDisabled}+ />+ ) : (+ // The regular hero also renders behind the welcome modal's+ // blur and while the greeting is still generating — it swaps+ // to the greeting the moment the real one arrives. Through+ // that whole flow it wears the greeting page's own layout so+ // the heading never moves when the swap happens.+ <EmptyHero+ name={greetingName}+ isAwaitingGreeting={intro.isAwaitingGreeting}+ isGreetingFlow={intro.anchorTop}+ />)}- <div className="mb-6">- <motion.div- layoutId={inputLayoutId}- transition={{ type: "spring", bounce: 0.2, duration: 0.65 }}- className="w-full px-2"- >- <ChatInput- inputId="chat-input-empty"- onSend={onSend}- disabled={isComposerDisabled}- isUploadingFiles={isUploadingFiles}- placeholder={inputPlaceholder}- className="w-full"- droppedFiles={droppedFiles}- onDroppedFilesConsumed={onDroppedFilesConsumed}- recipientPicker={- isExpertsEnabled ? (- <RecipientChip- recipient={recipient}- options={options}- isLoading={isLoadingRecipient}- onSelect={selectRecipient}- />- ) : undefined+ {isAgentBriefingEnabled &&+ !intro.isVisible &&+ !intro.isAwaitingGreeting && (+ <PulseChips chips={pulseChips} onChipClick={onSend} />+ )}++ {/* Held back while the greeting is on its way — it enters with+ the greeting page instead of sitting under a bare hero. */}+ {!intro.isAwaitingGreeting && (+ <div className={cn("mb-6", intro.isVisible && "max-w-[48rem]")}>+ <motion.div+ layoutId={inputLayoutId}+ transition={{ type: "spring", bounce: 0.2, duration: 0.65 }}+ className={cn(+ isBrainDumpEnabled+ ? "overflow-hidden rounded-xlarge border text-left transition-colors duration-300 ease-out"+ : "w-full px-2",+ // The greeting's prompt card bleeds 1.25rem past the text+ // (-mx-5); the composer stretches the same amount so their+ // borders line up. The regular hero keeps it centered.+ isBrainDumpEnabled &&+ (intro.isVisible+ ? "-mx-5 max-w-[50.5rem]"+ : "mx-auto w-full max-w-[42rem]"),+ )}+ style={+ isBrainDumpEnabled+ ? {+ borderColor: "#e4e4e7",+ boxShadow: "0 2px 8px rgba(0,0,0,0.04)",+ }+ : undefined}- />- </motion.div>- </div>+ >+ <ChatInput+ inputId="chat-input-empty"+ onSend={onSend}+ disabled={isComposerDisabled}+ hideSubmitWhenEmpty={Boolean(isBrainDumpEnabled)}+ isUploadingFiles={isUploadingFiles}+ placeholder={inputPlaceholder}+ className={+ isBrainDumpEnabled+ ? "w-full [&_textarea]:min-h-[4.5rem]"+ : "w-full"+ }+ droppedFiles={droppedFiles}+ onDroppedFilesConsumed={onDroppedFilesConsumed}+ recipientPicker={+ isExpertsEnabled ? (+ <RecipientChip+ recipient={recipient}+ options={options}+ isLoading={isLoadingRecipient}+ onSelect={selectRecipient}+ />+ ) : undefined+ }+ />+ </motion.div>+ </div>+ )}</div>- {isLoadingPrompts ? (- <div className="flex flex-wrap items-center justify-center gap-3">- {Array.from({ length: 4 }, (_, i) => (- <Skeleton key={i} className="h-10 w-28 shrink-0 rounded-full" />- ))}- </div>- ) : (- <SuggestionThemes- themes={themes}- onSend={onSend}- disabled={isComposerDisabled}- />- )}+ {/* The greeting page is deliberately quiet: its own prompts are+ the suggestions, so the theme chips stay out of the way. Also+ held while the greeting decision is pending. */}+ {!intro.isVisible &&+ !intro.isAwaitingGreeting &&+ (isLoadingPrompts ? (+ <div className="flex flex-wrap items-center justify-center gap-3">+ {Array.from({ length: 4 }, (_, i) => (+ <Skeleton key={i} className="h-10 w-28 shrink-0 rounded-full" />+ ))}+ </div>+ ) : (+ <SuggestionThemes+ themes={themes}+ onSend={onSend}+ disabled={isComposerDisabled}+ />+ ))}</motion.div></div>);diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/EmptySession/components/EmptyHero.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/EmptySession/components/EmptyHero.tsxnew file mode 100644index 0000000000..85acaa720a--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/EmptySession/components/EmptyHero.tsx@@ -0,0 +1,71 @@+"use client";++import { Text } from "@/components/atoms/Text/Text";+import { GlassOrb } from "@/components/molecules/GlassOrb/GlassOrb";+import { TextGenerateEffect } from "@/components/ui/text-generate-effect";+import { cn } from "@/lib/utils";+import {+ ORB_PURPLE,+ SMALL_ORB_PARAMS,+} from "../../OnboardingIntroCard/OnboardingIntroCard";+import { EditNameDialog } from "./EditNameDialog/EditNameDialog";++interface Props {+ name: string;+ isAwaitingGreeting: boolean;+ isGreetingFlow: boolean;+}++// The regular empty-session hero. It also stands in for the greeting page+// while the greeting is still generating, and in that mode the heading row+// is laid out exactly where OnboardingIntroCard is about to put it — same+// left edge, same type — so the swap leaves it where it already was+// instead of throwing it in from the centre of the page.+export function EmptyHero({ name, isAwaitingGreeting, isGreetingFlow }: Props) {+ return (+ <>+ <div+ className={cn(+ "mb-1 flex items-center gap-3",+ isGreetingFlow+ ? "w-full max-w-[48rem] justify-start"+ : "justify-center",+ )}+ >+ {isAwaitingGreeting && (+ <span className="relative size-8 shrink-0">+ <GlassOrb params={SMALL_ORB_PARAMS} />+ </span>+ )}+ <Text+ variant="h3"+ className={+ isGreetingFlow+ ? "!text-[1.25rem] text-zinc-800"+ : "!text-[1.375rem] text-zinc-700"+ }+ >+ Hey,{" "}+ {isGreetingFlow ? (+ <span style={{ color: ORB_PURPLE }}>{name}</span>+ ) : (+ <span className="text-violet-600">{name}</span>+ )}+ {/* The greeting page carries no name editing, so keeping the+ trigger here would pop it out mid-swap. */}+ {!isGreetingFlow && <EditNameDialog currentName={name} />}+ </Text>+ </div>+ {/* Held back with the composer: while the greeting decision is+ pending this line appearing then vanishing read as the page+ changing its mind on refresh. */}+ {!isAwaitingGreeting && (+ <TextGenerateEffect+ className="mb-8 !font-normal [&>div]:!mt-0 [&_div]:!text-[1.375rem] [&_div]:!leading-normal [&_div]:!tracking-normal"+ duration={0.6}+ words="Tell me about your work — I'll find what to automate."+ />+ )}+ </>+ );+}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/OnboardingIntroCard/OnboardingIntroCard.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/OnboardingIntroCard/OnboardingIntroCard.tsxnew file mode 100644index 0000000000..98a949bd58--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/OnboardingIntroCard/OnboardingIntroCard.tsx@@ -0,0 +1,270 @@+"use client";++import { GlassOrb } from "@/components/molecules/GlassOrb/GlassOrb";+import type { GlassParams } from "@/components/molecules/GlassOrb/GlassSurface";+import type { SuggestedPrompt } from "@/app/api/__generated__/models/suggestedPrompt";+import { Icon } from "@/components/atoms/Icon/Icon";+import { Text } from "@/components/atoms/Text/Text";+import { useToast } from "@/components/molecules/Toast/use-toast";+import { TextGenerateEffect } from "@/components/ui/text-generate-effect";+import {+ Tooltip,+ TooltipContent,+ TooltipTrigger,+} from "@/components/ui/tooltip";+import {+ Analytics01Icon,+ ArrowRight01Icon,+ CalendarCheckIcon,+ Chat01Icon,+ Clock01Icon,+ CodeIcon,+ Copy01Icon,+ DollarCircleIcon,+ File02Icon,+ FlashIcon,+ GlobeIcon,+ Mail01Icon,+ Megaphone01Icon,+ News01Icon,+ Notification02Icon,+ Robot01Icon,+ Rocket01Icon,+ Search01Icon,+ ShoppingCart01Icon,+ SparklesIcon,+ Target01Icon,+ Tick02Icon,+ UserGroupIcon,+} from "@hugeicons/core-free-icons";+import type { IconSvgElement } from "@hugeicons/react";+import { motion, useReducedMotion } from "framer-motion";+import { useState } from "react";++// Slugs the backend may emit (see intro.PROMPT_ICONS); anything unknown+// falls back to the sparkle.+const PROMPT_ICONS: Record<string, IconSvgElement> = {+ sparkle: SparklesIcon,+ "chart-bar": Analytics01Icon,+ envelope: Mail01Icon,+ "magnifying-glass": Search01Icon,+ "calendar-check": CalendarCheckIcon,+ bell: Notification02Icon,+ "rocket-launch": Rocket01Icon,+ "file-text": File02Icon,+ globe: GlobeIcon,+ code: CodeIcon,+ newspaper: News01Icon,+ users: UserGroupIcon,+ "shopping-cart": ShoppingCart01Icon,+ chats: Chat01Icon,+ lightning: FlashIcon,+ target: Target01Icon,+ robot: Robot01Icon,+ clock: Clock01Icon,+ megaphone: Megaphone01Icon,+ "currency-dollar": DollarCircleIcon,+};++interface Props {+ name: string;+ greeting: string;+ prompts: SuggestedPrompt[];+ transcript?: string;+ onSelectPrompt: (prompt: string) => void;+ disabled?: boolean;+}++// The default glass params are tuned for the big onboarding orb; at 32px+// that much frost and distortion collapses into a flat purple ball. Light+// frost + gentle refraction keeps the drifting blobs readable this small.+// Also rendered by EmptySession's hero while the greeting is on its way,+// so the orb is already on screen before the reveal.+export const SMALL_ORB_PARAMS: GlassParams = {+ frost: 1.5,+ saturation: 1.5,+ tint: 0.12,+ edge: 0.55,+ distortion: 8,+ ringWidth: 1,+ ringDepth: 2,+ ringDark: 0.25,+};++// The purple the orb's blobs blend into — the name mirrors it. Shared with+// the hero heading this card replaces so the swap is invisible.+export const ORB_PURPLE = "#8a4dff";++const GREETING_START = 0.35;+const WORD_STAGGER = 0.08;+const ROW_STAGGER = 0.12;+const ROW_START_BUFFER = 0.3;+const FOOTER_BUFFER = 0.35;++// One reveal schedule shared with EmptySession so the composer can enter+// after everything here has finished. All timings hang off the greeting's+// word count — the prompts wait for the last word, the footer waits for+// the last row, the composer comes after the footer.+export function introRevealTimings(greeting: string, promptCount: number) {+ const words = greeting.split(" ").filter(Boolean).length;+ const promptsStart = GREETING_START + words * WORD_STAGGER + ROW_START_BUFFER;+ const footerStart = promptsStart + promptCount * ROW_STAGGER + FOOTER_BUFFER;+ const composerStart = footerStart + 0.4;+ return { promptsStart, footerStart, composerStart };+}++// The first thing a user sees after onboarding. Replaces the regular+// empty-session hero entirely: heading first, then the greeting word by+// word, then the prompt rows one after another, footer line last.+export function OnboardingIntroCard({+ name,+ greeting,+ prompts,+ transcript = "",+ onSelectPrompt,+ disabled = false,+}: Props) {+ const prefersReducedMotion = useReducedMotion();+ const [isCopied, setIsCopied] = useState(false);+ const { toast } = useToast();++ async function handleCopyTranscript() {+ // Denied clipboard permission, or a non-secure origin. Showing the+ // tick regardless would claim a copy that never happened.+ try {+ await navigator.clipboard.writeText(transcript);+ } catch {+ toast({+ title: "Could not copy the transcript",+ description: "Your browser blocked clipboard access.",+ variant: "destructive",+ });+ return;+ }+ setIsCopied(true);+ setTimeout(() => setIsCopied(false), 2000);+ }+ const { promptsStart, footerStart } = introRevealTimings(+ greeting,+ prompts.length,+ );++ function reveal(delay: number) {+ if (prefersReducedMotion) {+ return {+ initial: { opacity: 0 },+ animate: { opacity: 1 },+ transition: { duration: 0.3, delay },+ };+ }+ return {+ initial: { opacity: 0, y: 10, filter: "blur(4px)" },+ animate: { opacity: 1, y: 0, filter: "blur(0px)" },+ transition: { duration: 0.45, ease: [0, 0, 0.2, 1] as const, delay },+ };+ }++ return (+ <div+ className="mb-8 w-full max-w-[48rem] text-left"+ data-testid="onboarding-intro-card"+ >+ {/* Not revealed: this exact row is already on screen as the hero's+ heading while the greeting generates, in this exact spot. Fading+ and rising it here would blink a heading that never moved. */}+ <div className="mb-4 flex items-center gap-3">+ <span className="relative size-8 shrink-0">+ <GlassOrb params={SMALL_ORB_PARAMS} />+ </span>+ <Text variant="h3" className="!text-[1.25rem] text-zinc-800">+ Hey, <span style={{ color: ORB_PURPLE }}>{name}</span>+ </Text>+ {transcript && (+ <Tooltip>+ <TooltipTrigger asChild>+ <button+ type="button"+ onClick={handleCopyTranscript}+ aria-label="Copy your recording's transcript"+ className="ml-auto rounded-full p-2 text-zinc-400 transition-colors hover:bg-zinc-100 hover:text-zinc-700"+ >+ {isCopied ? (+ <Icon+ icon={Tick02Icon}+ size={16}+ className="text-emerald-600"+ />+ ) : (+ <Icon icon={Copy01Icon} size={16} />+ )}+ </button>+ </TooltipTrigger>+ <TooltipContent>+ {isCopied ? "Copied!" : "Copy everything you told me"}+ </TooltipContent>+ </Tooltip>+ )}+ </div>++ <TextGenerateEffect+ words={greeting}+ duration={0.4}+ delay={GREETING_START}+ className="text-left !font-normal [&>div]:!mt-0 [&_div]:!text-[1.25rem] [&_div]:!leading-normal [&_div]:!tracking-normal [&_span]:!text-zinc-700"+ />++ {prompts.length > 0 && (+ <motion.ol+ {...reveal(promptsStart)}+ // Negative margins let the card's border breathe outward while+ // the row content (px-5) stays aligned with the text above it.+ className="-mx-5 mt-6 divide-y divide-zinc-100 overflow-hidden rounded-2xl border border-zinc-100 bg-white shadow-sm"+ >+ {prompts.map((prompt, index) => {+ const promptIcon = PROMPT_ICONS[prompt.icon ?? ""] ?? SparklesIcon;+ return (+ <motion.li+ key={prompt.title}+ {...reveal(promptsStart + 0.15 + index * ROW_STAGGER)}+ data-testid="onboarding-intro-prompt"+ >+ <button+ type="button"+ disabled={disabled}+ // Sends the full prompt as the user's first message —+ // which also creates the session and retires the greeting+ // via the regular first-send path in useCopilotPage.+ onClick={() => onSelectPrompt(prompt.prompt)}+ className="group flex w-full cursor-pointer items-center gap-4 px-5 py-4 text-left transition-colors duration-150 hover:bg-violet-50/60 disabled:cursor-default disabled:opacity-60 disabled:hover:bg-transparent"+ >+ <Icon+ icon={promptIcon}+ size={18}+ className="shrink-0 text-violet-500"+ />+ <Text+ variant="body-medium"+ className="!text-[0.9375rem] !text-zinc-800"+ >+ {prompt.title}+ </Text>+ <Icon+ icon={ArrowRight01Icon}+ size={16}+ className="ml-auto shrink-0 text-violet-500 transition-transform duration-150 ease-out group-hover:translate-x-1"+ />+ </button>+ </motion.li>+ );+ })}+ </motion.ol>+ )}++ <motion.div {...reveal(footerStart)}>+ <Text variant="body" className="mt-6 !text-[0.9375rem] !text-zinc-500">+ Want to do something else? Just write it in the textbox below.+ </Text>+ </motion.div>+ </div>+ );+}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/OnboardingIntroCard/__tests__/OnboardingIntroCard.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/OnboardingIntroCard/__tests__/OnboardingIntroCard.test.tsxnew file mode 100644index 0000000000..e4b542fa35--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/OnboardingIntroCard/__tests__/OnboardingIntroCard.test.tsx@@ -0,0 +1,278 @@+import type { SuggestedPrompt } from "@/app/api/__generated__/models/suggestedPrompt";+import {+ act,+ cleanup,+ fireEvent,+ normalizeWhitespace,+ render,+ screen,+ waitFor,+} from "@/tests/integrations/test-utils";+import userEvent from "@testing-library/user-event";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";+import {+ introRevealTimings,+ OnboardingIntroCard,+} from "../OnboardingIntroCard";++const toastSpy = vi.fn();+vi.mock("@/components/molecules/Toast/use-toast", () => ({+ toast: (...args: unknown[]) => toastSpy(...(args as [])),+ useToast: () => ({ toast: toastSpy }),+}));++const PROMPTS: SuggestedPrompt[] = [+ {+ title: "Summarise my inbox",+ prompt: "Every morning, summarise my unread email and flag what matters.",+ icon: "envelope",+ },+ {+ title: "Watch competitor pricing",+ prompt: "Check competitor pricing pages weekly and tell me what moved.",+ icon: "not-a-real-icon-slug",+ },+];++const GREETING = "You spend most of your week chasing updates.";+const COPY_LABEL = "Copy your recording's transcript";+const FOOTER = "Want to do something else? Just write it in the textbox below.";++function writeTextMock(impl: () => Promise<void>) {+ const writeText = vi.fn(impl);+ Object.defineProperty(navigator, "clipboard", {+ configurable: true,+ writable: true,+ value: { writeText },+ });+ return writeText;+}++beforeEach(() => {+ toastSpy.mockReset();+});++afterEach(() => {+ cleanup();+});++describe("OnboardingIntroCard — content", () => {+ it("greets the user by name and renders the greeting, prompts and footer", () => {+ const { container } = render(+ <OnboardingIntroCard+ name="Alex"+ greeting={GREETING}+ prompts={PROMPTS}+ onSelectPrompt={vi.fn()}+ />,+ );++ const text = normalizeWhitespace(container);+ expect(text).toContain("Hey, Alex");+ expect(text).toContain(GREETING);++ expect(screen.getByText("Summarise my inbox")).toBeDefined();+ expect(screen.getByText("Watch competitor pricing")).toBeDefined();+ expect(screen.getByText(FOOTER)).toBeDefined();+ });++ it("renders a row for a prompt whose icon slug the frontend does not know", () => {+ render(+ <OnboardingIntroCard+ name="Alex"+ greeting={GREETING}+ prompts={[PROMPTS[1]]}+ onSelectPrompt={vi.fn()}+ />,+ );++ expect(+ screen.getByRole("button", { name: "Watch competitor pricing" }),+ ).toBeDefined();+ });++ it("omits the prompt list entirely when the server sent no prompts", () => {+ render(+ <OnboardingIntroCard+ name="Alex"+ greeting={GREETING}+ prompts={[]}+ onSelectPrompt={vi.fn()}+ />,+ );++ expect(screen.queryByRole("list")).toBeNull();+ expect(screen.getByText(FOOTER)).toBeDefined();+ });+});++describe("OnboardingIntroCard — picking a prompt", () => {+ it("sends the full prompt, not the short title shown in the row", async () => {+ const onSelectPrompt = vi.fn();+ render(+ <OnboardingIntroCard+ name="Alex"+ greeting={GREETING}+ prompts={PROMPTS}+ onSelectPrompt={onSelectPrompt}+ />,+ );++ await userEvent.click(+ screen.getByRole("button", { name: "Summarise my inbox" }),+ );++ expect(onSelectPrompt).toHaveBeenCalledExactlyOnceWith(PROMPTS[0].prompt);+ });++ it("does not fire while the composer is disabled", async () => {+ const onSelectPrompt = vi.fn();+ render(+ <OnboardingIntroCard+ name="Alex"+ greeting={GREETING}+ prompts={PROMPTS}+ onSelectPrompt={onSelectPrompt}+ disabled+ />,+ );++ const row = screen.getByRole("button", { name: "Summarise my inbox" });+ expect(row).toHaveProperty("disabled", true);++ await userEvent.click(row, { pointerEventsCheck: 0 });++ expect(onSelectPrompt).not.toHaveBeenCalled();+ });+});++describe("OnboardingIntroCard — transcript copy", () => {+ it("offers no copy affordance when there is no transcript", () => {+ render(+ <OnboardingIntroCard+ name="Alex"+ greeting={GREETING}+ prompts={PROMPTS}+ onSelectPrompt={vi.fn()}+ />,+ );++ expect(screen.queryByRole("button", { name: COPY_LABEL })).toBeNull();+ });++ it("copies the transcript verbatim and confirms it visually", async () => {+ const writeText = writeTextMock(() => Promise.resolve());+ const { container } = render(+ <OnboardingIntroCard+ name="Alex"+ greeting={GREETING}+ prompts={PROMPTS}+ transcript="I spend Mondays rebuilding the same report."+ onSelectPrompt={vi.fn()}+ />,+ );++ await userEvent.click(screen.getByRole("button", { name: COPY_LABEL }));++ expect(writeText).toHaveBeenCalledExactlyOnceWith(+ "I spend Mondays rebuilding the same report.",+ );+ await waitFor(() =>+ expect(container.querySelector(".text-emerald-600")).not.toBeNull(),+ );+ expect(toastSpy).not.toHaveBeenCalled();+ });++ it("warns instead of claiming a copy the browser refused", async () => {+ const writeText = writeTextMock(() =>+ Promise.reject(new Error("clipboard blocked")),+ );+ const { container } = render(+ <OnboardingIntroCard+ name="Alex"+ greeting={GREETING}+ prompts={PROMPTS}+ transcript="I spend Mondays rebuilding the same report."+ onSelectPrompt={vi.fn()}+ />,+ );++ await userEvent.click(screen.getByRole("button", { name: COPY_LABEL }));++ expect(writeText).toHaveBeenCalledOnce();+ await waitFor(() =>+ expect(toastSpy).toHaveBeenCalledWith({+ title: "Could not copy the transcript",+ description: "Your browser blocked clipboard access.",+ variant: "destructive",+ }),+ );+ // No tick: the copy never happened.+ expect(container.querySelector(".text-emerald-600")).toBeNull();+ });++ it("drops the confirmation tick again after a couple of seconds", async () => {+ vi.useFakeTimers();+ writeTextMock(() => Promise.resolve());+ const { container } = render(+ <OnboardingIntroCard+ name="Alex"+ greeting={GREETING}+ prompts={PROMPTS}+ transcript="I spend Mondays rebuilding the same report."+ onSelectPrompt={vi.fn()}+ />,+ );++ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: COPY_LABEL }));+ });+ expect(container.querySelector(".text-emerald-600")).not.toBeNull();++ await act(async () => {+ await vi.advanceTimersByTimeAsync(2000);+ });++ expect(container.querySelector(".text-emerald-600")).toBeNull();+ vi.useRealTimers();+ });+});++describe("introRevealTimings", () => {+ it("pushes every stage back as the greeting gets longer", () => {+ const short = introRevealTimings("Hello there", 0);+ const long = introRevealTimings("Hello there, this is a longer line", 0);++ expect(short.promptsStart).toBeCloseTo(0.35 + 2 * 0.08 + 0.3, 5);+ expect(long.promptsStart).toBeGreaterThan(short.promptsStart);+ expect(long.footerStart).toBeGreaterThan(short.footerStart);+ expect(long.composerStart).toBeGreaterThan(short.composerStart);+ });++ it("staggers the footer behind the last prompt row and the composer behind the footer", () => {+ const none = introRevealTimings("Hello there", 0);+ const three = introRevealTimings("Hello there", 3);++ expect(three.promptsStart).toBe(none.promptsStart);+ expect(three.footerStart - none.footerStart).toBeCloseTo(3 * 0.12, 5);+ expect(three.composerStart - three.footerStart).toBeCloseTo(0.4, 5);+ });++ it("ignores empty words so padding does not delay the reveal", () => {+ const padded = introRevealTimings(" Hello there ", 2);+ const tidy = introRevealTimings("Hello there", 2);++ expect(padded).toEqual(tidy);+ });++ it("still produces an ordered schedule for an empty greeting", () => {+ const { promptsStart, footerStart, composerStart } = introRevealTimings(+ "",+ 0,+ );++ expect(promptsStart).toBeCloseTo(0.65, 5);+ expect(footerStart).toBeGreaterThan(promptsStart);+ expect(composerStart).toBeGreaterThan(footerStart);+ });+});diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/OnboardingIntroCard/__tests__/useOnboardingIntroCard.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/OnboardingIntroCard/__tests__/useOnboardingIntroCard.test.tsxnew file mode 100644index 0000000000..b214873e80--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/OnboardingIntroCard/__tests__/useOnboardingIntroCard.test.tsx@@ -0,0 +1,411 @@+import { getGetBrainDumpIntroMockHandler200 } from "@/app/api/__generated__/endpoints/brain-dump/brain-dump.msw";+import type { IntroCardResponse } from "@/app/api/__generated__/models/introCardResponse";+import { server } from "@/mocks/mock-server";+import { setIntroPath } from "@/services/onboarding/brain-dump-handoff";+import { QueryClient, QueryClientProvider } from "@tanstack/react-query";+import { act, renderHook, waitFor } from "@testing-library/react";+import { HttpResponse, http } from "msw";+import type { ReactNode } from "react";+import { beforeEach, describe, expect, it, vi } from "vitest";++const capture = vi.hoisted(() => vi.fn());+vi.mock("posthog-js", () => ({ default: { capture } }));++const authUser = vi.hoisted(() => ({+ current: { id: "user-1" } as { id: string } | null,+}));+vi.mock("@/lib/auth/hooks/useAuth", () => ({+ useAuth: () => ({ user: authUser.current }),+}));++const flags = vi.hoisted(() => ({ current: {} as Record<string, boolean> }));+const flagsReady = vi.hoisted(() => ({ current: true }));+vi.mock("@/services/feature-flags/use-get-flag", () => ({+ Flag: { ONBOARDING_BRAIN_DUMP: "onboarding-brain-dump" },+ useGetFlag: (flag: string) => flags.current[flag] ?? false,+ useFlagStatus: (flag: string) => ({+ enabled: flags.current[flag] ?? false,+ ready: flagsReady.current,+ }),+}));++import { useOnboardingIntroCard } from "../useOnboardingIntroCard";++const INTRO_URL =+ "http://localhost:3000/api/proxy/api/onboarding/brain-dump/intro";+const MIC_GLOW_KEY = "autogpt:onboarding-mic-glow";+const LATER_DUMP_KEY = "autogpt:onboarding-pending-later-dump";+const FOLLOWUP_KEY = "autogpt:onboarding-intro-awaiting-followup";+const WELCOME_PENDING_KEY = "autogpt:onboarding-welcome-pending";+const GREETING_DONE_KEY = "autogpt:copilot-greeting-done";+const CAPABILITY_CARDS_KEY = "autogpt:copilot-capability-cards-seen";++const READY_INTRO: IntroCardResponse = {+ path: "A",+ greeting: "You spend most of your week chasing updates.",+ greeting_done: false,+ prompts: [{ title: "Summarise my inbox", prompt: "Summarise my inbox" }],+ transcript: "I rebuild the same report every Monday.",+};++const PENDING_INTRO: IntroCardResponse = {+ path: "A",+ greeting: "",+ greeting_done: false,+ prompts: [],+};++function makeWrapper() {+ const client = new QueryClient({+ defaultOptions: { queries: { retry: false } },+ });+ return function Wrapper({ children }: { children: ReactNode }) {+ return (+ <QueryClientProvider client={client}>{children}</QueryClientProvider>+ );+ };+}++function renderIntro() {+ return renderHook(() => useOnboardingIntroCard(), { wrapper: makeWrapper() });+}++// Counts every intro request so "the endpoint is never called" means silence+// rather than a handler that happens not to be registered.+function countIntroRequests(body: IntroCardResponse = READY_INTRO) {+ const urls: string[] = [];+ server.use(+ http.get(INTRO_URL, ({ request }) => {+ urls.push(request.url);+ return HttpResponse.json(body);+ }),+ );+ return urls;+}++beforeEach(() => {+ window.sessionStorage.clear();+ window.localStorage.clear();+ capture.mockReset();+ authUser.current = { id: "user-1" };+ flags.current = { "onboarding-brain-dump": true };+ flagsReady.current = true;+});++describe("useOnboardingIntroCard — flag gating", () => {+ it("never asks for an intro while the brain-dump flag is off", async () => {+ flags.current = {};+ const urls = countIntroRequests();++ const { result } = renderIntro();+ await waitFor(() => expect(result.current.isVisible).toBe(false));++ expect(urls).toEqual([]);+ expect(result.current.greeting).toBe("");+ });++ it("releases the composer once LaunchDarkly says the flag is off", async () => {+ // The regression: with the flag off the intro query is disabled, so it+ // never answers — and "no answer yet" was read as "still generating".+ // isAwaitingGreeting stayed true forever and EmptySession hides the+ // composer, PulseChips and SuggestionThemes behind it, so the default+ // flag-off copilot rendered a hero with no way to type.+ flags.current = {};++ const { result } = renderIntro();++ await waitFor(() => expect(result.current.isAwaitingGreeting).toBe(false));+ expect(result.current.isVisible).toBe(false);+ });++ it("renders the plain hero while LaunchDarkly has not answered for a user with no handoff", async () => {+ // Holding on "no answer yet" alone hid the composer on every flag-off+ // /copilot load until LaunchDarkly replied — the flag-off page has to+ // render untouched.+ flags.current = {};+ flagsReady.current = false;++ const { result } = renderIntro();++ await waitFor(() => expect(result.current.isAwaitingGreeting).toBe(false));+ expect(result.current.isVisible).toBe(false);+ });++ it("still holds the composer while LaunchDarkly has not answered for a user coming out of the wizard", async () => {+ // The hold exists to stop the regular hero flashing before the+ // greeting page takes over, and the pending overlay is seeded+ // synchronously — so this user is held before the flag resolves.+ window.sessionStorage.setItem(WELCOME_PENDING_KEY, "1");+ flags.current = {};+ flagsReady.current = false;++ const { result } = renderIntro();++ await waitFor(() => expect(result.current).toBeDefined());+ expect(result.current.isAwaitingGreeting).toBe(true);+ });++ it("never asks again once this browser has seen this user finish the greeting", async () => {+ window.localStorage.setItem(GREETING_DONE_KEY, "user-1");+ const urls = countIntroRequests();++ const { result } = renderIntro();+ await waitFor(() => expect(result.current.isAwaitingGreeting).toBe(false));++ expect(urls).toEqual([]);+ expect(result.current.isVisible).toBe(false);+ });++ it("still asks when the cached flag belongs to a different account", async () => {+ window.localStorage.setItem(GREETING_DONE_KEY, "someone-else");+ const urls = countIntroRequests();++ const { result } = renderIntro();+ await waitFor(() => expect(result.current.isVisible).toBe(true));++ expect(urls).toHaveLength(1);+ });+});++describe("useOnboardingIntroCard — handoff from the wizard", () => {+ it("consumes the recorded path once, opens the welcome overlay and reports it", async () => {+ setIntroPath("A");+ countIntroRequests();++ const { result } = renderIntro();+ await waitFor(() => expect(result.current.isWelcomeOpen).toBe(true));++ expect(window.sessionStorage.getItem("autogpt:onboarding-intro-path")).toBe(+ null,+ );+ expect(window.sessionStorage.getItem(WELCOME_PENDING_KEY)).toBe("1");+ expect(window.sessionStorage.getItem(FOLLOWUP_KEY)).toBe("1");+ expect(capture).toHaveBeenCalledWith("intro_path", { path: "A" });+ // Path A already dumped — nothing to point the mic at.+ expect(window.sessionStorage.getItem(MIC_GLOW_KEY)).toBeNull();+ });++ it("glows the composer mic only for the skip path, and arms the later dump", async () => {+ setIntroPath("B");+ countIntroRequests();++ const { result } = renderIntro();+ await waitFor(() => expect(result.current.isWelcomeOpen).toBe(true));++ expect(window.sessionStorage.getItem(MIC_GLOW_KEY)).toBe("1");+ // The invitation AutoPilot just issued: the first voice message in the+ // composer is the dump this user skipped, and is reported as such.+ expect(window.sessionStorage.getItem(LATER_DUMP_KEY)).toBe("1");+ expect(capture).toHaveBeenCalledWith("intro_path", { path: "B" });+ });++ it("drops the handoff instead of running it when the flag is off", async () => {+ // The wizard wrote these while the flag was on; a rollback in between+ // must not still buy the user the overlay and its 404-ing polls.+ flags.current = {};+ setIntroPath("B");+ window.sessionStorage.setItem(WELCOME_PENDING_KEY, "1");+ const urls = countIntroRequests();++ const { result } = renderIntro();+ await waitFor(() => expect(result.current.isAwaitingGreeting).toBe(false));++ expect(result.current.isWelcomeOpen).toBe(false);+ expect(window.sessionStorage.getItem(WELCOME_PENDING_KEY)).toBeNull();+ expect(window.sessionStorage.getItem("autogpt:onboarding-intro-path")).toBe(+ null,+ );+ expect(window.sessionStorage.getItem(MIC_GLOW_KEY)).toBeNull();+ expect(window.sessionStorage.getItem(LATER_DUMP_KEY)).toBeNull();+ expect(capture).not.toHaveBeenCalledWith("intro_path", expect.anything());+ expect(urls).toEqual([]);+ });++ it("waits for LaunchDarkly before consuming the handoff", async () => {+ flagsReady.current = false;+ setIntroPath("A");+ countIntroRequests();++ const { result } = renderIntro();+ await waitFor(() => expect(result.current).toBeDefined());++ expect(result.current.isWelcomeOpen).toBe(false);+ expect(window.sessionStorage.getItem("autogpt:onboarding-intro-path")).toBe(+ "A",+ );+ expect(capture).not.toHaveBeenCalledWith("intro_path", expect.anything());+ });++ it("shows no overlay for a user who did not just come out of onboarding", async () => {+ countIntroRequests();++ const { result } = renderIntro();+ await waitFor(() => expect(result.current.isVisible).toBe(true));++ expect(result.current.isWelcomeOpen).toBe(false);+ expect(capture).not.toHaveBeenCalledWith("intro_path", expect.anything());+ });++ it("keeps the overlay up across a refresh via the pending flag", async () => {+ window.sessionStorage.setItem(WELCOME_PENDING_KEY, "1");+ const urls = countIntroRequests();++ const { result } = renderIntro();+ await waitFor(() => expect(result.current.isWelcomeOpen).toBe(true));++ // The greeting is not fetched behind a modal the user has not dismissed.+ expect(urls).toEqual([]);+ expect(result.current.isAwaitingGreeting).toBe(true);+ });++ it("drops a stale pending overlay for a user who already saw the cards", async () => {+ window.sessionStorage.setItem(WELCOME_PENDING_KEY, "1");+ window.localStorage.setItem(CAPABILITY_CARDS_KEY, "user-1");+ countIntroRequests();++ const { result } = renderIntro();+ await waitFor(() => expect(result.current.isWelcomeOpen).toBe(false));++ expect(window.sessionStorage.getItem(WELCOME_PENDING_KEY)).toBeNull();+ await waitFor(() => expect(result.current.isVisible).toBe(true));+ });+});++describe("useOnboardingIntroCard — closing the welcome overlay", () => {+ it("reports the close, remembers the cards, and reveals the greeting", async () => {+ setIntroPath("A");+ const urls = countIntroRequests();++ const { result } = renderIntro();+ await waitFor(() => expect(result.current.isWelcomeOpen).toBe(true));+ // The greeting stays hidden behind the overlay however the fetch went.+ expect(result.current.isVisible).toBe(false);+ expect(result.current.isAwaitingGreeting).toBe(true);++ act(() => result.current.closeWelcome());++ expect(capture).toHaveBeenCalledWith("welcome_dialog_closed", {});+ expect(window.sessionStorage.getItem(WELCOME_PENDING_KEY)).toBeNull();+ expect(window.localStorage.getItem(CAPABILITY_CARDS_KEY)).toBe("user-1");++ await waitFor(() => expect(result.current.isVisible).toBe(true));+ expect(result.current.greeting).toBe(READY_INTRO.greeting);+ expect(urls.length).toBeGreaterThan(0);+ });+});++describe("useOnboardingIntroCard — the greeting itself", () => {+ it("exposes the server's greeting, prompts, transcript and path", async () => {+ server.use(getGetBrainDumpIntroMockHandler200(READY_INTRO));++ const { result } = renderIntro();+ await waitFor(() => expect(result.current.isVisible).toBe(true));++ expect(result.current.greeting).toBe(READY_INTRO.greeting);+ expect(result.current.prompts).toEqual(READY_INTRO.prompts);+ expect(result.current.transcript).toBe(READY_INTRO.transcript);+ expect(result.current.path).toBe("A");+ expect(result.current.isAwaitingGreeting).toBe(false);+ });++ it("treats a non-string transcript as no transcript", async () => {+ server.use(+ getGetBrainDumpIntroMockHandler200({ ...READY_INTRO, transcript: null }),+ );++ const { result } = renderIntro();+ await waitFor(() => expect(result.current.isVisible).toBe(true));++ expect(result.current.transcript).toBe("");+ });++ it("defaults the path to B when the payload does not carry one", async () => {+ server.use(+ getGetBrainDumpIntroMockHandler200({+ greeting: "Tell me what eats your week.",+ prompts: [],+ } as unknown as IntroCardResponse),+ );++ const { result } = renderIntro();+ await waitFor(() => expect(result.current.isVisible).toBe(true));++ expect(result.current.path).toBe("B");+ expect(result.current.prompts).toEqual([]);+ });++ it("holds the composer back while the pipeline is still writing the greeting", async () => {+ server.use(getGetBrainDumpIntroMockHandler200(PENDING_INTRO));++ const { result } = renderIntro();+ await waitFor(() => expect(result.current.isAwaitingGreeting).toBe(true));++ expect(result.current.isVisible).toBe(false);+ expect(result.current.greeting).toBe("");+ });++ it("polls past the pending answer and reveals the greeting when it lands", async () => {+ let calls = 0;+ server.use(+ http.get(INTRO_URL, () => {+ calls += 1;+ return HttpResponse.json(calls === 1 ? PENDING_INTRO : READY_INTRO);+ }),+ );++ const { result } = renderIntro();+ await waitFor(() => expect(result.current.isAwaitingGreeting).toBe(true));++ await waitFor(() => expect(result.current.isVisible).toBe(true), {+ timeout: 6000,+ });+ expect(calls).toBeGreaterThan(1);+ expect(result.current.greeting).toBe(READY_INTRO.greeting);+ }, 10_000);++ it("shows nothing and caches the server's 'already seen it' verdict", async () => {+ const urls = countIntroRequests({+ path: "A",+ greeting: "Welcome back.",+ greeting_done: true,+ });++ const { result } = renderIntro();+ await waitFor(() =>+ expect(window.localStorage.getItem(GREETING_DONE_KEY)).toBe("user-1"),+ );++ expect(result.current.isVisible).toBe(false);+ expect(result.current.isAwaitingGreeting).toBe(false);+ expect(urls).toHaveLength(1);+ });++ it("writes no cache entry while the user record is still loading", async () => {+ authUser.current = null;+ countIntroRequests({+ path: "A",+ greeting: "Welcome back.",+ greeting_done: true,+ });++ const { result } = renderIntro();+ await waitFor(() => expect(result.current.isAwaitingGreeting).toBe(false));++ // A cache entry keyed to nobody would be inherited by whoever signs in.+ expect(window.localStorage.getItem(GREETING_DONE_KEY)).toBeNull();+ });++ it("releases the composer when the endpoint is unavailable", async () => {+ server.use(+ http.get(INTRO_URL, () =>+ HttpResponse.json({ detail: "nope" }, { status: 500 }),+ ),+ );++ const { result } = renderIntro();+ await waitFor(() => expect(result.current.isAwaitingGreeting).toBe(false));++ expect(result.current.isVisible).toBe(false);+ expect(result.current.greeting).toBe("");+ });+});diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/OnboardingIntroCard/useOnboardingIntroCard.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/OnboardingIntroCard/useOnboardingIntroCard.tsnew file mode 100644index 0000000000..aa70b3af0f--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/OnboardingIntroCard/useOnboardingIntroCard.ts@@ -0,0 +1,206 @@+import { useGetBrainDumpIntro } from "@/app/api/__generated__/endpoints/brain-dump/brain-dump";+import { useAuth } from "@/lib/auth/hooks/useAuth";+import { Flag, useFlagStatus } from "@/services/feature-flags/use-get-flag";+import { trackBrainDump } from "@/services/onboarding/brain-dump-analytics";+import {+ clearWelcomePending,+ peekCapabilityCardsSeen,+ peekGreetingDone,+ peekWelcomePending,+ setCapabilityCardsSeen,+ setGreetingDone,+ setIntroAwaitingFollowup,+ setMicGlow,+ setPendingLaterDump,+ setWelcomePending,+ takeIntroPath,+} from "@/services/onboarding/brain-dump-handoff";+import { useEffect, useState } from "react";++// While the pipeline is still writing the greeting the intro endpoint+// answers with an empty Path A — poll at this cadence until the real+// greeting lands (or the pipeline terminally resolves).+const PENDING_POLL_MS = 1500;+// A pipeline killed between transcription and completion leaves the dump+// in a non-terminal status forever — without a ceiling this poll would+// spin for the rest of the session and the composer would never appear.+const PENDING_GIVE_UP_MS = 120_000;++export function useOnboardingIntroCard() {+ const { user } = useAuth();+ const userId = user?.id ?? null;+ // The endpoint 404s with the flag off, so without this every copilot+ // visit for every user pays for a request that cannot succeed.+ const { enabled: isBrainDumpEnabled, ready: isFlagReady } = useFlagStatus(+ Flag.ONBOARDING_BRAIN_DUMP,+ );++ // localStorage answers first so a returning user never flashes the+ // greeting; only when it has no answer do we ask the server. The flag+ // is keyed to the user id, so a fresh account on the same browser+ // starts clean.+ const [isDone, setIsDone] = useState(() => peekGreetingDone(userId));++ // A user fresh out of onboarding gets the full-screen welcome overlay+ // first. The greeting is not even fetched until they close it — the+ // reveal animation must start on a page they are actually looking at.+ // Seeded from sessionStorage so a refresh keeps the overlay up rather+ // than skipping it.+ const [isWelcomeOpen, setIsWelcomeOpen] = useState(peekWelcomePending);++ // The server render can't see localStorage or the intro answer, so it+ // always emits the held hero; the first client frame must match it or+ // hydration flashes the composer before the greeting page arrives.+ // Everyone gets one held frame, then the real state takes over.+ const [isMounted, setIsMounted] = useState(false);+ useEffect(() => setIsMounted(true), []);++ // Give-up switch for a greeting pipeline that never terminates; once+ // flipped the hero releases the composer instead of holding forever.+ const [gaveUpWaiting, setGaveUpWaiting] = useState(false);++ useEffect(() => {+ // The user record can arrive after mount — re-check once it does.+ if (peekGreetingDone(userId)) setIsDone(true);+ }, [userId]);++ useEffect(() => {+ // This browser already saw this user finish or skip the cards —+ // never reshow, even if the pending flag somehow survived.+ if (isWelcomeOpen && peekCapabilityCardsSeen(userId)) {+ clearWelcomePending();+ setIsWelcomeOpen(false);+ }+ }, [isWelcomeOpen, userId]);++ useEffect(() => {+ // The handoff keys are written by the wizard while the flag is on, so+ // they can outlive it: wait for LaunchDarkly, then either run the+ // handoff or drop it. Consuming it with the flag off would put the+ // overlay and its 404-ing polls in front of a rolled-back user.+ if (!isFlagReady) return;+ if (!isBrainDumpEnabled) {+ takeIntroPath();+ clearWelcomePending();+ setIsWelcomeOpen(false);+ return;+ }+ const path = takeIntroPath();+ if (!path) return;+ if (path === "B") {+ setMicGlow();+ // Path B's intro asks for the dump from the composer instead, so the+ // first voice message there is the answer to that invitation.+ setPendingLaterDump();+ }+ // Measures whether the greeting actually started a conversation —+ // consumed by the first real message afterwards.+ setIntroAwaitingFollowup();+ trackBrainDump("intro_path", { path });+ setWelcomePending();+ setIsWelcomeOpen(true);+ }, [isFlagReady, isBrainDumpEnabled]);++ const { data, isError } = useGetBrainDumpIntro({+ query: {+ enabled: Boolean(isBrainDumpEnabled) && !isDone && !isWelcomeOpen,+ staleTime: Infinity,+ gcTime: Infinity,+ refetchInterval: (query) => {+ if (gaveUpWaiting) return false;+ const latest = query.state.data;+ if (!latest || latest.status !== 200) return false;+ const body = latest.data;+ // Empty Path A greeting = pipeline still generating; keep asking.+ if (!body.greeting_done && body.path === "A" && !body.greeting) {+ return PENDING_POLL_MS;+ }+ return false;+ },+ },+ });++ const intro = data?.status === 200 ? data.data : null;+ // "Answered" rather than "not loading": isLoading is false on the+ // server render and on the first client frames before the fetch+ // subscribes, which flashed the regular hero + composer for a beat+ // before the greeting page took over on refresh. A non-200 or a+ // failed request counts as an answer so the composer is never held+ // hostage by an unavailable endpoint — and so does a flag we know is+ // off, since that query is disabled and will never answer at all. Wait+ // for LaunchDarkly to actually say so: treating "not answered yet" as+ // off is what flashes the composer before the greeting page arrives.+ const hasIntroAnswer =+ (isFlagReady && !isBrainDumpEnabled) || data !== undefined || isError;+ const serverSaysDone = Boolean(intro?.greeting_done);+ const isPendingPerServer = Boolean(+ intro && !intro.greeting_done && intro.path === "A" && !intro.greeting,+ );+ const isPendingGeneration = isPendingPerServer && !gaveUpWaiting;++ useEffect(() => {+ if (!isPendingPerServer) return;+ const timer = setTimeout(() => setGaveUpWaiting(true), PENDING_GIVE_UP_MS);+ return () => clearTimeout(timer);+ }, [isPendingPerServer]);++ useEffect(() => {+ // The server already saw the first message (possibly from another+ // device) — cache that locally so we never ask again.+ if (serverSaysDone && userId) {+ setGreetingDone(userId);+ setIsDone(true);+ }+ }, [serverSaysDone, userId]);++ function closeWelcome() {+ trackBrainDump("welcome_dialog_closed", {});+ clearWelcomePending();+ setCapabilityCardsSeen(userId);+ setIsWelcomeOpen(false);+ }++ // The whole greeting flow reads top-down like a letter, so it anchors+ // to the top from its first visible frame — flipping the container+ // from centered to top only when the greeting arrived made the "Hey"+ // heading visibly jump. `isWelcomeOpen` needs no flag check (only the+ // gated handoff ever sets it) and is seeded synchronously, so the+ // fresh-out-of-onboarding user is anchored before LaunchDarkly answers.+ const isGreetingFlow =+ isWelcomeOpen ||+ (Boolean(isBrainDumpEnabled) &&+ (!hasIntroAnswer || isPendingGeneration || Boolean(intro?.greeting)));++ // Holding the composer is only ever right while this flow is in play:+ // the overlay is up (seeded synchronously from the handoff) or the flag+ // already reads on. Holding on "LaunchDarkly has not answered" alone+ // hid the composer on every flag-off /copilot load until it did.+ const isGreetingExpected = isWelcomeOpen || Boolean(isBrainDumpEnabled);++ return {+ // The page renders the regular hero behind the welcome modal and+ // while the greeting is generating; it swaps to the greeting the+ // moment the real one arrives.+ isVisible:+ isMounted &&+ !isDone &&+ !isWelcomeOpen &&+ hasIntroAnswer &&+ Boolean(intro?.greeting),+ // The greeting is on its way (modal up, no server answer yet, or the+ // pipeline still writing) — the hero shows the orb and holds the+ // composer back until the greeting page takes over.+ isAwaitingGreeting:+ !isMounted ||+ (!isDone &&+ isGreetingExpected &&+ (isWelcomeOpen || !hasIntroAnswer || isPendingGeneration)),+ anchorTop: isMounted && !isDone && isGreetingFlow,+ isWelcomeOpen: !isDone && isWelcomeOpen,+ closeWelcome,+ greeting: intro?.greeting ?? "",+ prompts: intro?.prompts ?? [],+ transcript: typeof intro?.transcript === "string" ? intro.transcript : "",+ path: intro?.path ?? "B",+ };+}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/OnboardingWelcomeDialog/ConnectMethodView.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/OnboardingWelcomeDialog/ConnectMethodView.tsxnew file mode 100644index 0000000000..890d89f4c5--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/OnboardingWelcomeDialog/ConnectMethodView.tsx@@ -0,0 +1,196 @@+"use client";++import { AutoGPTLogo } from "@/components/atoms/AutoGPTLogo/AutoGPTLogo";+import { Icon } from "@/components/atoms/Icon/Icon";+import { Text } from "@/components/atoms/Text/Text";+import { ProviderAvatar } from "@/components/contextual/IntegrationsPanel/components/ConnectServiceDialog/components/DetailView/ProviderAvatar";+import type { ApiKeyConnectFormValues } from "@/components/contextual/IntegrationsPanel/components/ConnectServiceDialog/components/DetailView/schema";+import { UnsupportedNotice } from "@/components/contextual/IntegrationsPanel/components/ConnectServiceDialog/components/DetailView/UnsupportedNotice";+import {+ AuthType,+ type AuthMethod,+ type ConnectableProvider,+} from "@/components/contextual/IntegrationsPanel/components/ConnectServiceDialog/helpers";+import type { UseFormReturn } from "react-hook-form";+import { InlineApiKeyForm } from "./InlineApiKeyForm";+import {+ GlobeIcon,+ Key01Icon,+ SecurityCheckIcon,+ UserIcon,+} from "@hugeicons/core-free-icons";+import type { IconSvgElement } from "@hugeicons/react";+import { AnimatePresence, motion } from "framer-motion";++interface Props {+ provider: ConnectableProvider;+ selectedMethod: AuthMethod | null;+ onSelectMethod: (method: AuthMethod) => void;+ apiKeyForm: UseFormReturn<ApiKeyConnectFormValues>;+ onApiKeySubmit: (values: ApiKeyConnectFormValues) => void;+}++const METHOD_ORDER: AuthMethod[] = [+ AuthType.oauth2,+ AuthType.api_key,+ AuthType.user_password,+ AuthType.host_scoped,+];++const METHOD_COPY: Record<+ AuthMethod,+ {+ label: string;+ description: string;+ icon: IconSvgElement;+ recommended?: boolean;+ }+> = {+ [AuthType.oauth2]: {+ label: "OAuth",+ description: "One-click secure access.",+ icon: SecurityCheckIcon,+ recommended: true,+ },+ [AuthType.api_key]: {+ label: "API Key",+ description: "Paste your token for a custom setup.",+ icon: Key01Icon,+ },+ [AuthType.user_password]: {+ label: "Username & password",+ description: "Sign in with account credentials.",+ icon: UserIcon,+ },+ [AuthType.host_scoped]: {+ label: "Host",+ description: "Scope credentials to one host.",+ icon: GlobeIcon,+ },+};++// The connect step behind a provider click: logo pair, "Connect AutoGPT+// to X", method radio cards. Form methods expand their inputs inline as+// an accordion; OAuth is driven by the panel footer's Continue.+export function ConnectMethodView({+ provider,+ selectedMethod,+ onSelectMethod,+ apiKeyForm,+ onApiKeySubmit,+}: Props) {+ const methods = METHOD_ORDER.filter((method) =>+ provider.supportedAuthTypes.includes(method),+ );++ return (+ <div className="flex flex-col gap-5 pt-2">+ <div className="flex items-center justify-center gap-6">+ <span className="relative flex h-20 w-20 items-center justify-center rounded-full bg-white shadow-[0_8px_24px_rgba(0,0,0,0.08)] ring-1 ring-zinc-100">+ <AutoGPTLogo+ hideText+ className="absolute left-1/2 top-1/2 h-8 w-[4.4rem] -translate-x-[77%] -translate-y-1/2"+ />+ </span>+ <span aria-hidden className="grid grid-cols-3 gap-1.5">+ {Array.from({ length: 9 }, (_, dot) => (+ <span key={dot} className="h-1 w-1 rounded-full bg-[#5b21b6]/30" />+ ))}+ </span>+ <span className="flex h-20 w-20 items-center justify-center rounded-full bg-white shadow-[0_8px_24px_rgba(0,0,0,0.08)] ring-1 ring-zinc-100">+ <ProviderAvatar id={provider.id} name={provider.name} />+ </span>+ </div>++ <div className="flex flex-col gap-1.5 text-center">+ <Text variant="h3" className="!text-[1.25rem] text-zinc-900">+ Connect AutoGPT to {provider.name}+ </Text>+ <Text variant="body" className="!text-zinc-500">+ Choose how you'd like to connect your {provider.name} account.+ </Text>+ </div>++ <div className="flex flex-col gap-1 rounded-2xl bg-neutral-100 p-1.5">+ {methods.map((method) => {+ const copy = METHOD_COPY[method];+ const isSelected = selectedMethod === method;+ const hasInlineForm = method !== AuthType.oauth2;+ return (+ <div+ key={method}+ className={isSelected ? "rounded-xl bg-white shadow-sm" : ""}+ >+ <button+ type="button"+ onClick={() => onSelectMethod(method)}+ className={+ isSelected+ ? "flex w-full items-center gap-3 rounded-xl p-3 text-left"+ : "flex w-full items-center gap-3 rounded-xl p-3 text-left transition-colors hover:bg-white/60"+ }+ >+ <span className="flex h-11 w-11 shrink-0 items-center justify-center rounded-lg bg-white shadow-sm">+ <Icon icon={copy.icon} size={22} className="text-zinc-700" />+ </span>+ <span className="flex min-w-0 flex-1 flex-col gap-0.5">+ <span className="flex items-center gap-2">+ <span className="text-sm font-semibold text-zinc-900">+ {copy.label}+ </span>+ {copy.recommended && (+ <span className="rounded-md bg-violet-100 px-1.5 py-0.5 text-[10px] font-semibold tracking-wide text-violet-700">+ RECOMMENDED+ </span>+ )}+ </span>+ <span className="text-xs text-zinc-500">+ {copy.description}+ </span>+ </span>+ <span+ aria-hidden+ className={+ isSelected+ ? "flex h-5 w-5 shrink-0 items-center justify-center rounded-full border-2 border-violet-600"+ : "h-5 w-5 shrink-0 rounded-full border-2 border-zinc-200"+ }+ >+ {isSelected && (+ <span className="h-2.5 w-2.5 rounded-full bg-violet-600" />+ )}+ </span>+ </button>+ {/* Form methods open in place: the card grows to reveal the+ inputs, submit included — no separate step. */}+ <AnimatePresence initial={false}>+ {isSelected && hasInlineForm && (+ <motion.div+ key="form"+ initial={{ height: 0, opacity: 0 }}+ animate={{ height: "auto", opacity: 1 }}+ exit={{ height: 0, opacity: 0 }}+ transition={{ duration: 0.25, ease: [0, 0, 0.2, 1] }}+ className="overflow-hidden"+ >+ <div className="px-3 pb-3">+ {method === AuthType.api_key ? (+ <InlineApiKeyForm+ form={apiKeyForm}+ providerName={provider.name}+ onSubmit={onApiKeySubmit}+ />+ ) : (+ <UnsupportedNotice providerName={provider.name} />+ )}+ </div>+ </motion.div>+ )}+ </AnimatePresence>+ </div>+ );+ })}+ </div>+ </div>+ );+}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/OnboardingWelcomeDialog/ConnectProviderRow.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/OnboardingWelcomeDialog/ConnectProviderRow.tsxnew file mode 100644index 0000000000..0f41557204--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/OnboardingWelcomeDialog/ConnectProviderRow.tsx@@ -0,0 +1,84 @@+"use client";++import { Icon } from "@/components/atoms/Icon/Icon";+import { Add01Icon, CheckmarkCircle02Icon } from "@hugeicons/core-free-icons";+import Image from "next/image";+import { useState } from "react";++import type { ConnectableProvider } from "@/components/contextual/IntegrationsPanel/components/ConnectServiceDialog/helpers";++interface Props {+ provider: ConnectableProvider;+ onSelect: (id: string) => void;+ isConnected?: boolean;+ /** Second line, used by the recommended list to show the model's reason. */+ description?: string | null;+}++// The welcome dialog's provider card: compact (two per row), name plus an+// optional one-line reason, with a Connected state once credentials exist+// for the provider.+export function ConnectProviderRow({+ provider,+ onSelect,+ isConnected,+ description,+}: Props) {+ const src = `/integrations/${provider.id}.png`;+ const [brokenSrc, setBrokenSrc] = useState<string | null>(null);+ const broken = brokenSrc === src;++ return (+ <button+ type="button"+ onClick={() => onSelect(provider.id)}+ className="group flex h-14 w-full items-center gap-2.5 rounded-xl bg-neutral-100 px-3 text-left transition-colors hover:bg-neutral-200 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-purple-400 active:bg-neutral-200"+ >+ {broken ? (+ <div+ aria-hidden+ className="flex size-7 shrink-0 items-center justify-center rounded-md bg-white text-[12px] font-semibold uppercase text-zinc-600"+ >+ {provider.name?.charAt(0) ?? provider.id.charAt(0)}+ </div>+ ) : (+ <Image+ src={src}+ alt=""+ width={28}+ height={28}+ loading="lazy"+ className="size-7 shrink-0 object-contain"+ onError={() => setBrokenSrc(src)}+ />+ )}+ <span className="flex min-w-0 flex-1 flex-col">+ <span className="flex min-w-0 items-center gap-1.5">+ <span className="truncate text-[14px] font-medium leading-[22px] text-zinc-800">+ {provider.name}+ </span>+ {/* Connected mark beside the title — the + stays because a+ provider can hold multiple credentials. */}+ {isConnected && (+ <Icon+ icon={CheckmarkCircle02Icon}+ size={18}+ className="shrink-0 text-emerald-500"+ />+ )}+ </span>+ {description && (+ <span className="truncate text-[11px] leading-[16px] text-zinc-500">+ {description}+ </span>+ )}+ </span>+ <span+ aria-hidden+ className="flex size-6 shrink-0 items-center justify-center rounded-lg bg-zinc-700 text-white transition-transform group-hover:bg-zinc-800 group-active:scale-[0.96]"+ >+ <Icon icon={Add01Icon} size={14} />+ </span>+ </button>+ );+}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/OnboardingWelcomeDialog/ConnectToolsPanel.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/OnboardingWelcomeDialog/ConnectToolsPanel.tsxnew file mode 100644index 0000000000..9884640370--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/OnboardingWelcomeDialog/ConnectToolsPanel.tsx@@ -0,0 +1,236 @@+"use client";++import { Button } from "@/components/atoms/Button/Button";+import { Icon } from "@/components/atoms/Icon/Icon";+import { Skeleton } from "@/components/atoms/Skeleton/Skeleton";+import { Text } from "@/components/atoms/Text/Text";+import { ConnectMethodView } from "./ConnectMethodView";+import { ConnectProviderRow } from "./ConnectProviderRow";+import { ErrorCard } from "@/components/molecules/ErrorCard/ErrorCard";+import { Plug01Icon, Search01Icon } from "@hugeicons/core-free-icons";+import { AnimatePresence, motion, useReducedMotion } from "framer-motion";+import { useEffect } from "react";+import { useConnectToolsPanel } from "./useConnectToolsPanel";++interface Props {+ onBack: () => void;+ onNext: () => void;+}++const stepVariants = {+ initial: (direction: number) => ({ x: 16 * direction, opacity: 0 }),+ active: { x: 0, opacity: 1 },+ exit: (direction: number) => ({ x: -16 * direction, opacity: 0 }),+};++const reducedVariants = {+ initial: { opacity: 0 },+ active: { opacity: 1 },+ exit: { opacity: 0 },+};++const STEP_TRANSITION = { duration: 0.15, ease: [0, 0, 0.2, 1] as const };++// The provider picker embedded in the welcome dialog: same list → detail+// flow as ConnectServiceDialog, but connecting never closes the dialog —+// success slides back to the list so more tools can be wired up in one go.+export function ConnectToolsPanel({ onBack, onNext }: Props) {+ const {+ query,+ setQuery,+ providers,+ recommendedProviders,+ isLoading,+ isError,+ error,+ refetch,+ selectedProvider,+ direction,+ connectedProviders,+ selectedMethod,+ setSelectedMethod,+ apiKeyForm,+ handleApiKeySubmit,+ showContinue,+ isContinueDisabled,+ isConnecting,+ handleSelect,+ handleBackToList,+ handleContinue,+ } = useConnectToolsPanel();+ const reduceMotion = useReducedMotion();+ const variants = reduceMotion ? reducedVariants : stepVariants;++ // Escape steps back one level (detail → list → cards) instead of+ // reaching the dialog's skip handler, which would end onboarding.+ useEffect(() => {+ function handleKeyDown(event: KeyboardEvent) {+ if (event.key !== "Escape") return;+ if (selectedProvider) {+ handleBackToList();+ return;+ }+ onBack();+ }+ window.addEventListener("keydown", handleKeyDown);+ return () => window.removeEventListener("keydown", handleKeyDown);+ }, [selectedProvider, handleBackToList, onBack]);++ return (+ <div className="flex flex-col gap-4 p-6">+ <Text variant="h3" className="!text-[1.25rem] text-zinc-900">+ Connect your tools+ </Text>++ <div className="overflow-hidden">+ <AnimatePresence mode="wait" initial={false} custom={direction}>+ {selectedProvider ? (+ <motion.div+ key={`detail-${selectedProvider.id}`}+ custom={direction}+ variants={variants}+ initial="initial"+ animate="active"+ exit="exit"+ transition={STEP_TRANSITION}+ >+ <ConnectMethodView+ provider={selectedProvider}+ selectedMethod={selectedMethod}+ onSelectMethod={setSelectedMethod}+ apiKeyForm={apiKeyForm}+ onApiKeySubmit={handleApiKeySubmit}+ />+ </motion.div>+ ) : (+ <motion.div+ key="list"+ custom={direction}+ variants={variants}+ initial="initial"+ animate="active"+ exit="exit"+ transition={STEP_TRANSITION}+ className="flex flex-col gap-4"+ >+ {isLoading ? (+ <div className="flex flex-col gap-2">+ {[0, 1, 2].map((row) => (+ <Skeleton key={row} className="h-16 w-full rounded-xl" />+ ))}+ </div>+ ) : isError ? (+ <ErrorCard+ context="providers"+ responseError={+ error instanceof Error+ ? { message: error.message }+ : undefined+ }+ onRetry={() => refetch()}+ />+ ) : (+ <div className="flex flex-col gap-4">+ <div className="relative w-full">+ <Icon+ icon={Search01Icon}+ size={20}+ className="pointer-events-none absolute left-4 top-1/2 -translate-y-1/2 text-[#83838C]"+ />+ <input+ type="text"+ value={query}+ onChange={(e) => setQuery(e.target.value)}+ placeholder="Search services..."+ aria-label="Search services"+ className="h-[46px] w-full rounded-xl border border-[#DADADC] bg-white pl-12 pr-4 text-sm leading-[22px] text-[#1F1F20] transition-colors placeholder:text-[#83838C] focus:border-violet-500 focus:outline-none"+ />+ </div>++ {query.trim() ? (+ providers.length === 0 ? (+ <div className="flex flex-col items-center justify-center gap-2 rounded-2xl border border-dashed border-[#DADADC] py-8 text-center">+ <Icon+ icon={Plug01Icon}+ size={24}+ className="text-[#83838C]"+ />+ <Text variant="body" className="text-[#505057]">+ {`No services match "${query.trim()}"`}+ </Text>+ </div>+ ) : (+ <ul className="grid max-h-[13.5rem] grid-cols-2 gap-2 overflow-y-auto pr-1">+ {providers.map((provider) => (+ <li key={provider.id}>+ <ConnectProviderRow+ provider={provider}+ onSelect={handleSelect}+ isConnected={connectedProviders.has(provider.id)}+ />+ </li>+ ))}+ </ul>+ )+ ) : recommendedProviders.length > 0 ? (+ <div className="flex flex-col gap-2">+ <span className="text-xs font-medium text-violet-600">+ Recommended from our conversation+ </span>+ <ul className="grid grid-cols-2 gap-2">+ {recommendedProviders.map((provider) => (+ <li key={provider.id}>+ <ConnectProviderRow+ provider={provider}+ onSelect={handleSelect}+ isConnected={connectedProviders.has(provider.id)}+ description={provider.description}+ />+ </li>+ ))}+ </ul>+ </div>+ ) : (+ <Text variant="body" className="text-center text-[#505057]">+ Search to find a service to connect.+ </Text>+ )}+ </div>+ )}+ </motion.div>+ )}+ </AnimatePresence>+ </div>++ <div className="flex items-center justify-end gap-3">+ {selectedProvider ? (+ <>+ <Button variant="secondary" size="small" onClick={handleBackToList}>+ Back+ </Button>+ {showContinue && (+ <Button+ variant="primary"+ size="small"+ disabled={isContinueDisabled}+ loading={isConnecting}+ onClick={handleContinue}+ >+ {isConnecting ? "Connecting…" : "Continue"}+ </Button>+ )}+ </>+ ) : (+ <>+ <Button variant="secondary" size="small" onClick={onBack}>+ Back+ </Button>+ <Button variant="primary" size="small" onClick={onNext}>+ Next+ </Button>+ </>+ )}+ </div>+ </div>+ );+}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/OnboardingWelcomeDialog/InlineApiKeyForm.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/OnboardingWelcomeDialog/InlineApiKeyForm.tsxnew file mode 100644index 0000000000..6c3a5ff22c--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/OnboardingWelcomeDialog/InlineApiKeyForm.tsx@@ -0,0 +1,95 @@+"use client";++import { Input } from "@/components/atoms/Input/Input";+import {+ Form,+ FormControl,+ FormField,+ FormItem,+ FormMessage,+} from "@/components/molecules/Form/Form";+import type { ApiKeyConnectFormValues } from "@/components/contextual/IntegrationsPanel/components/ConnectServiceDialog/components/DetailView/schema";+import type { UseFormReturn } from "react-hook-form";++interface Props {+ form: UseFormReturn<ApiKeyConnectFormValues>;+ providerName: string;+ onSubmit: (values: ApiKeyConnectFormValues) => void;+}++// The API-key inputs for the expanded method card: compact spacing and+// small text, and no submit button of its own — the panel footer's+// Continue drives the same submit (Enter still works via the form).+export function InlineApiKeyForm({ form, providerName, onSubmit }: Props) {+ return (+ <Form form={form} onSubmit={onSubmit} className="space-y-2.5">+ <FormField+ control={form.control}+ name="title"+ render={({ field }) => (+ <FormItem>+ <FormControl>+ <Input+ {...field}+ id={field.name}+ autoComplete="off"+ label="Name"+ labelVariant="small-medium"+ size="small"+ placeholder={`My ${providerName} key`}+ wrapperClassName="!mb-0"+ />+ </FormControl>+ <FormMessage />+ </FormItem>+ )}+ />++ <FormField+ control={form.control}+ name="apiKey"+ render={({ field }) => (+ <FormItem>+ <FormControl>+ <Input+ {...field}+ id={field.name}+ type="password"+ autoComplete="new-password"+ spellCheck={false}+ label="API key"+ labelVariant="small-medium"+ size="small"+ placeholder="sk-..."+ wrapperClassName="!mb-0"+ />+ </FormControl>+ <FormMessage />+ </FormItem>+ )}+ />++ <FormField+ control={form.control}+ name="expiresAt"+ render={({ field }) => (+ <FormItem>+ <FormControl>+ <Input+ {...field}+ value={field.value ?? ""}+ id={field.name}+ type="date"+ label="Expires (optional)"+ labelVariant="small-medium"+ size="small"+ wrapperClassName="!mb-0"+ />+ </FormControl>+ <FormMessage />+ </FormItem>+ )}+ />+ </Form>+ );+}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/OnboardingWelcomeDialog/OnboardingWelcomeDialog.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/OnboardingWelcomeDialog/OnboardingWelcomeDialog.tsxnew file mode 100644index 0000000000..a88e728565--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/OnboardingWelcomeDialog/OnboardingWelcomeDialog.tsx@@ -0,0 +1,290 @@+"use client";++import { usePostV1CompleteOnboardingStep } from "@/app/api/__generated__/endpoints/onboarding/onboarding";+import { Button } from "@/components/atoms/Button/Button";+import { Icon } from "@/components/atoms/Icon/Icon";+import { Text } from "@/components/atoms/Text/Text";+import { useToast } from "@/components/molecules/Toast/use-toast";+import { trackBrainDump } from "@/services/onboarding/brain-dump-analytics";+import {+ ArrowLeft01Icon,+ BrainIcon,+ ElectricPlugsIcon,+ GraduationCapIcon,+ SparklesIcon,+} from "@hugeicons/core-free-icons";+import type { IconSvgElement } from "@hugeicons/react";+import { AnimatePresence, motion } from "framer-motion";+import { useEffect, useRef, useState } from "react";+import { useMeasuredHeight } from "@/components/contextual/IntegrationsPanel/components/ConnectServiceDialog/useMeasuredHeight";+import { ConnectToolsPanel } from "./ConnectToolsPanel";+import { GlassPixelBackdrop } from "@/components/atoms/GlassPixelBackdrop/GlassPixelBackdrop";++interface Props {+ isOpen: boolean;+ onClose: () => void;+}++interface CapabilityCard {+ title: string;+ // Capped at 20 words — an icon, short title, one sentence.+ body: string;+ /** Stage art: big duotone-violet icon in a white tile. */+ icon?: IconSvgElement;+ /** Expands the dialog into the embedded provider picker. */+ cta?: { label: string };+}++const CARDS: CapabilityCard[] = [+ {+ title: "Meet AutoPilot.",+ body: "It does the work. Ask once, or put it on a schedule. It delivers while you do something else.",+ icon: SparklesIcon,+ },+ {+ title: "It works inside your tools.",+ body: "Slack, Gmail, Notion, GitHub and 40+ more. 500+ blocks under the hood.",+ icon: ElectricPlugsIcon,+ cta: { label: "Connect your tools" },+ },+ {+ title: "It learns how you operate.",+ body: "Teach it Skills, like an employee handbook. It keeps its own files, so it never starts from scratch.",+ icon: GraduationCapIcon,+ },+ {+ title: "It remembers everything.",+ body: "Memory beyond any human brain. It even dreams. Manage it all in the Agents tab.",+ icon: BrainIcon,+ },+];++// First-run capability cards on the copilot home (ChatGPT/Claude style):+// a tinted stage on top carrying the card's icon, copy below,+// skippable at any card, shown once — completion is recorded server-side+// as the CAPABILITY_CARDS onboarding step. It also buys the background+// pipeline its last seconds: the greeting is only fetched, and only+// starts animating, after this closes.+export function OnboardingWelcomeDialog({ isOpen, onClose }: Props) {+ const [cardIndex, setCardIndex] = useState(0);+ // The CTA resizes the dialog into the provider picker instead of+ // navigating away — connecting must never close this dialog.+ const [isConnectOpen, setIsConnectOpen] = useState(false);+ const [contentRef, contentHeight] = useMeasuredHeight<HTMLDivElement>();+ const dialogRef = useRef<HTMLDivElement>(null);+ const { toast } = useToast();+ const { mutate: completeStep } = usePostV1CompleteOnboardingStep({+ mutation: {+ onError: () =>+ toast({+ title: "Could not save your onboarding progress",+ description: "You may see this introduction again next time.",+ variant: "destructive",+ }),+ },+ });+ const card = CARDS[cardIndex];+ const isLastCard = cardIndex === CARDS.length - 1;++ function finish(outcome: "completed" | "skipped") {+ trackBrainDump(+ outcome === "completed"+ ? "capability_cards_completed"+ : "capability_cards_skipped",+ { card_index: cardIndex },+ );+ completeStep({ params: { step: "CAPABILITY_CARDS" } });+ onClose();+ }++ // Escape ends the cards, and focus starts inside the card rather than on+ // whatever was behind the overlay. While the provider picker is open+ // Escape belongs to it — stepping back out of a half-typed API key must+ // not end onboarding for good.+ useEffect(() => {+ if (!isOpen) return;+ dialogRef.current?.focus();+ function handleKeyDown(event: KeyboardEvent) {+ if (event.key === "Escape" && !isConnectOpen) finish("skipped");+ }+ window.addEventListener("keydown", handleKeyDown);+ return () => window.removeEventListener("keydown", handleKeyDown);+ // eslint-disable-next-line react-hooks/exhaustive-deps+ }, [isOpen, cardIndex, isConnectOpen]);++ function handleNext() {+ if (isLastCard) {+ finish("completed");+ return;+ }+ trackBrainDump("capability_card_viewed", { card_index: cardIndex + 1 });+ setCardIndex(cardIndex + 1);+ }++ return (+ <AnimatePresence>+ {isOpen && (+ <motion.div+ initial={{ opacity: 0 }}+ animate={{ opacity: 1 }}+ exit={{ opacity: 0 }}+ transition={{ duration: 0.4, ease: [0.4, 0, 0.2, 1] }}+ className="fixed inset-0 z-[100] flex items-center justify-center bg-white/30 px-4 backdrop-blur-sm"+ data-testid="onboarding-welcome-overlay"+ role="dialog"+ aria-modal="true"+ aria-label="Welcome to AutoPilot"+ >+ <motion.div+ initial={{ opacity: 0, y: 16, scale: 0.97, maxWidth: "26rem" }}+ animate={{+ opacity: 1,+ y: 0,+ scale: 1,+ maxWidth: isConnectOpen ? "30rem" : "26rem",+ }}+ transition={{ duration: 0.45, ease: [0, 0, 0.2, 1] }}+ className="w-full max-w-[26rem] overflow-hidden rounded-3xl bg-white shadow-[0_24px_80px_-24px_rgba(0,0,0,0.3)] outline-none"+ ref={dialogRef}+ tabIndex={-1}+ >+ {/* Card-resize morph: the dialog animates between the compact+ capability card and the wider provider picker; height tracks+ whichever view is mounted. */}+ <motion.div+ animate={{ height: contentHeight ?? "auto" }}+ transition={{ duration: 0.35, ease: [0, 0, 0.2, 1] }}+ >+ <div ref={contentRef}>+ <AnimatePresence mode="wait" initial={false}>+ {isConnectOpen ? (+ <motion.div+ key="connect"+ initial={{ opacity: 0, y: 8 }}+ animate={{ opacity: 1, y: 0 }}+ exit={{ opacity: 0, y: -8 }}+ transition={{ duration: 0.2, ease: [0, 0, 0.2, 1] }}+ >+ <ConnectToolsPanel+ onBack={() => setIsConnectOpen(false)}+ onNext={() => {+ setIsConnectOpen(false);+ handleNext();+ }}+ />+ </motion.div>+ ) : (+ <motion.div+ key="cards"+ initial={{ opacity: 0, y: 8 }}+ animate={{ opacity: 1, y: 0 }}+ exit={{ opacity: 0, y: -8 }}+ transition={{ duration: 0.2, ease: [0, 0, 0.2, 1] }}+ >+ {/* Tinted stage: the card's icon floats here. */}+ <div className="relative h-44 bg-gradient-to-br from-[#e6dbff] via-[#ddccff] to-[#d0b9ff]">+ <GlassPixelBackdrop />+ <span className="absolute left-5 top-4 z-10 flex items-center gap-1 text-xs font-medium text-[#5b21b6]/70">+ {cardIndex > 0 && (+ <button+ type="button"+ aria-label="Previous card"+ onClick={() => setCardIndex(cardIndex - 1)}+ className="-ml-1 flex h-5 w-5 items-center justify-center rounded-full transition-colors hover:bg-white/50"+ >+ <Icon icon={ArrowLeft01Icon} size={13} />+ </button>+ )}+ {cardIndex + 1} of {CARDS.length}+ </span>+ <AnimatePresence mode="wait">+ <motion.div+ key={cardIndex}+ initial={{ opacity: 0, y: 12 }}+ animate={{ opacity: 1, y: 0 }}+ exit={{ opacity: 0, y: -12 }}+ transition={{ duration: 0.3, ease: [0, 0, 0.2, 1] }}+ className="absolute inset-0"+ data-testid="capability-card"+ >+ {card.icon && (+ <div className="flex h-full items-center justify-center">+ <div className="flex h-20 w-20 items-center justify-center rounded-3xl bg-white shadow-lg">+ <Icon+ icon={card.icon}+ size={40}+ className="text-violet-600"+ />+ </div>+ </div>+ )}+ </motion.div>+ </AnimatePresence>+ </div>++ {/* Copy + controls, reference-style white lower half. */}+ <div className="flex flex-col gap-3 px-7 pb-7 pt-6 text-left">+ <Text+ variant="h3"+ className="!text-[1.25rem] text-zinc-900"+ >+ {card.title}+ </Text>+ <Text+ variant="body"+ className="!text-[0.9375rem] !text-zinc-600"+ >+ {card.body}+ </Text>+ {card.cta && (+ <button+ type="button"+ onClick={() => setIsConnectOpen(true)}+ className="w-fit text-sm font-medium text-violet-600 underline-offset-4 hover:underline"+ >+ {card.cta.label}+ </button>+ )}++ <div className="mt-3 flex items-center justify-between">+ <div className="flex items-center gap-2">+ {CARDS.map((_, index) => (+ <span+ key={index}+ className={+ index === cardIndex+ ? "h-2 w-6 rounded-full bg-violet-500 transition-all"+ : "h-2 w-2 rounded-full bg-zinc-200 transition-all"+ }+ />+ ))}+ </div>+ <div className="flex items-center gap-3">+ <Button+ variant="secondary"+ size="small"+ onClick={() => finish("skipped")}+ >+ Skip+ </Button>+ <Button+ variant="primary"+ size="small"+ onClick={handleNext}+ >+ {isLastCard ? "Meet AutoPilot" : "Next"}+ </Button>+ </div>+ </div>+ </div>+ </motion.div>+ )}+ </AnimatePresence>+ </div>+ </motion.div>+ </motion.div>+ </motion.div>+ )}+ </AnimatePresence>+ );+}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/OnboardingWelcomeDialog/__tests__/connect-flow.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/OnboardingWelcomeDialog/__tests__/connect-flow.test.tsxnew file mode 100644index 0000000000..5c9c3d93f8--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/OnboardingWelcomeDialog/__tests__/connect-flow.test.tsx@@ -0,0 +1,439 @@+import { server } from "@/mocks/mock-server";+import {+ render,+ screen,+ waitFor,+ within,+} from "@/tests/integrations/test-utils";+import userEvent from "@testing-library/user-event";+import { http, HttpResponse } from "msw";+import { beforeEach, describe, expect, it, vi } from "vitest";++const toastSpy = vi.fn();+vi.mock("@/components/molecules/Toast/use-toast", () => ({+ toast: (...args: unknown[]) => toastSpy(...args),+ useToast: () => ({ toast: toastSpy }),+ useToastOnFail: () => () => {},+}));++// The recommended list — the panel's default view — is flag-gated.+vi.mock("@/services/feature-flags/use-get-flag", async (importOriginal) => {+ const actual =+ await importOriginal<+ typeof import("@/services/feature-flags/use-get-flag")+ >();+ return {+ ...actual,+ useGetFlag: (flag: string) => flag === actual.Flag.ONBOARDING_BRAIN_DUMP,+ };+});++const openOAuthPopup = vi.fn();+vi.mock("@/lib/oauth-popup", () => ({+ OAUTH_ERROR_POPUP_BLOCKED: "Popup blocked",+ preOpenOAuthPopup: () => null,+ openOAuthPopup: (...args: unknown[]) => openOAuthPopup(...args),+}));++import { ConnectToolsPanel } from "../ConnectToolsPanel";++const PROVIDERS_URL =+ "http://localhost:3000/api/proxy/api/integrations/providers";+const CREDENTIALS_URL =+ "http://localhost:3000/api/proxy/api/integrations/credentials";+const RECOMMENDED_URL =+ "http://localhost:3000/api/proxy/api/onboarding/brain-dump/recommended-providers";+const CREATE_CREDENTIALS_URL =+ "http://localhost:3000/api/proxy/api/integrations/:provider/credentials";+const OAUTH_LOGIN_URL =+ "http://localhost:3000/api/proxy/api/integrations/:provider/login";+const OAUTH_CALLBACK_URL =+ "http://localhost:3000/api/proxy/api/integrations/:provider/callback";++const REGISTRY = [+ {+ name: "github",+ description: "Code host",+ supported_auth_types: ["oauth2", "api_key"],+ },+ {+ name: "smtp",+ description: "Mail relay",+ supported_auth_types: ["user_password"],+ },+];++function stubPanel({+ connectedProviders = [] as string[],+ recommended = ["github", "smtp"],+} = {}) {+ server.use(+ http.get(PROVIDERS_URL, () => HttpResponse.json(REGISTRY)),+ http.get(CREDENTIALS_URL, () =>+ HttpResponse.json(+ connectedProviders.map((provider, index) => ({+ id: `cred-${index}`,+ provider,+ type: "api_key",+ title: `${provider} key`,+ scopes: null,+ username: null,+ })),+ ),+ ),+ http.get(RECOMMENDED_URL, () =>+ HttpResponse.json({+ ready: true,+ providers: recommended.map((provider) => ({+ provider,+ reason: `Because you mentioned ${provider}`,+ })),+ }),+ ),+ );+}++function renderPanel() {+ return render(<ConnectToolsPanel onBack={vi.fn()} onNext={vi.fn()} />);+}++async function openGithub() {+ const user = userEvent.setup();+ await user.click(await screen.findByRole("button", { name: /GitHub/ }));+ await screen.findByRole("heading", { name: "Connect AutoGPT to GitHub" });+ return user;+}++beforeEach(() => {+ toastSpy.mockClear();+ openOAuthPopup.mockReset();+});++describe("ConnectToolsPanel — picking a provider", () => {+ it("opens the connect step with only the methods the provider supports", async () => {+ stubPanel();+ renderPanel();++ const user = await openGithub();++ expect(+ screen.getByText("Choose how you'd like to connect your GitHub account."),+ ).toBeDefined();+ expect(screen.getByRole("button", { name: /OAuth/ })).toBeDefined();+ expect(screen.getByRole("button", { name: /API Key/ })).toBeDefined();+ expect(+ screen.queryByRole("button", { name: /Username & password/ }),+ ).toBeNull();+ // Continue stays inert until a method is chosen.+ expect(screen.getByRole("button", { name: "Continue" })).toHaveProperty(+ "disabled",+ true,+ );++ await user.click(screen.getByRole("button", { name: "Back" }));++ expect(await screen.findByLabelText("Search services")).toBeDefined();+ expect(+ screen.queryByRole("heading", { name: "Connect AutoGPT to GitHub" }),+ ).toBeNull();+ });++ it("offers no Continue for a method the dialog cannot drive", async () => {+ stubPanel();+ renderPanel();++ const user = userEvent.setup();+ await user.click(await screen.findByRole("button", { name: /Smtp/ }));+ await screen.findByRole("heading", { name: "Connect AutoGPT to Smtp" });++ await user.click(+ screen.getByRole("button", { name: /Username & password/ }),+ );++ expect(+ await screen.findByText("No connection method available"),+ ).toBeDefined();+ expect(screen.queryByRole("button", { name: "Continue" })).toBeNull();+ });++ it("returns to the list on Escape rather than leaving the picker", async () => {+ stubPanel();+ const onBack = vi.fn();+ render(<ConnectToolsPanel onBack={onBack} onNext={vi.fn()} />);+ const user = await openGithub();++ await user.keyboard("{Escape}");++ expect(await screen.findByLabelText("Search services")).toBeDefined();+ expect(onBack).not.toHaveBeenCalled();++ // Only from the list does Escape hand back to the dialog.+ await user.keyboard("{Escape}");++ expect(onBack).toHaveBeenCalledTimes(1);+ });++ it("marks providers that already hold credentials as connected", async () => {+ stubPanel({ connectedProviders: ["github"] });+ renderPanel();++ const github = await screen.findByRole("button", { name: /GitHub/ });+ const smtp = screen.getByRole("button", { name: /Smtp/ });++ // The connected mark is a decorative icon with no accessible name, so+ // it can only be identified by its tint.+ expect(github.querySelector(".text-emerald-500")).not.toBeNull();+ expect(smtp.querySelector(".text-emerald-500")).toBeNull();+ });+});++describe("ConnectToolsPanel — inline API key flow", () => {+ it("expands the key form in place and gates Continue on a valid form", async () => {+ stubPanel();+ renderPanel();+ const user = await openGithub();++ await user.click(screen.getByRole("button", { name: /API Key/ }));++ const apiKeyField = await screen.findByLabelText("API key", {+ selector: "input",+ });+ expect(screen.getByLabelText("Name")).toBeDefined();+ expect(screen.getByLabelText("Expires (optional)")).toBeDefined();+ expect(screen.getByRole("button", { name: "Continue" })).toHaveProperty(+ "disabled",+ true,+ );++ await user.type(screen.getByLabelText("Name"), "Work key");+ await user.type(apiKeyField, "sk-live-123");++ await waitFor(() =>+ expect(screen.getByRole("button", { name: "Continue" })).toHaveProperty(+ "disabled",+ false,+ ),+ );+ });++ it("posts the key to the picked provider and slides back to the list", async () => {+ stubPanel();+ const requests: { provider: string; body: unknown }[] = [];+ server.use(+ http.post(CREATE_CREDENTIALS_URL, async ({ params, request }) => {+ requests.push({+ provider: String(params.provider),+ body: await request.json(),+ });+ return HttpResponse.json(+ {+ id: "cred-new",+ provider: "github",+ type: "api_key",+ title: "Work key",+ scopes: null,+ username: null,+ },+ { status: 201 },+ );+ }),+ );++ renderPanel();+ const user = await openGithub();+ await user.click(screen.getByRole("button", { name: /API Key/ }));+ await user.type(await screen.findByLabelText("Name"), "Work key");+ await user.type(+ screen.getByLabelText("API key", { selector: "input" }),+ "sk-live-123",+ );++ await user.click(screen.getByRole("button", { name: "Continue" }));++ await waitFor(() => expect(requests).toHaveLength(1));+ expect(requests[0].provider).toBe("github");+ expect(requests[0].body).toMatchObject({+ provider: "github",+ type: "api_key",+ title: "Work key",+ api_key: "sk-live-123",+ });+ expect(toastSpy).toHaveBeenCalledWith({+ title: "API key saved",+ variant: "success",+ });++ // Success returns to the list so more tools can be wired up.+ expect(await screen.findByLabelText("Search services")).toBeDefined();+ expect(+ screen.queryByRole("heading", { name: "Connect AutoGPT to GitHub" }),+ ).toBeNull();+ });++ it("keeps the form open and reports the failure when the key is rejected", async () => {+ stubPanel();+ server.use(+ http.post(CREATE_CREDENTIALS_URL, () =>+ HttpResponse.json({ detail: "Invalid API key" }, { status: 400 }),+ ),+ );++ renderPanel();+ const user = await openGithub();+ await user.click(screen.getByRole("button", { name: /API Key/ }));+ await user.type(await screen.findByLabelText("Name"), "Work key");+ await user.type(+ screen.getByLabelText("API key", { selector: "input" }),+ "nope",+ );++ await user.click(screen.getByRole("button", { name: "Continue" }));++ await waitFor(() =>+ expect(toastSpy).toHaveBeenCalledWith(+ expect.objectContaining({+ title: "Couldn't save API key",+ variant: "destructive",+ }),+ ),+ );+ expect(+ screen.getByRole("heading", { name: "Connect AutoGPT to GitHub" }),+ ).toBeDefined();+ });+});++describe("ConnectToolsPanel — inline OAuth flow", () => {+ it("runs the whole OAuth exchange without leaving the panel", async () => {+ stubPanel();+ const exchanges: { provider: string; body: unknown }[] = [];+ let loginUrlRequests = 0;+ server.use(+ http.get(OAUTH_LOGIN_URL, () => {+ loginUrlRequests++;+ return HttpResponse.json({+ login_url: "https://github.test/oauth",+ state_token: "state-abc",+ });+ }),+ http.post(OAUTH_CALLBACK_URL, async ({ params, request }) => {+ exchanges.push({+ provider: String(params.provider),+ body: await request.json(),+ });+ return HttpResponse.json({+ id: "cred-oauth",+ provider: "github",+ type: "oauth2",+ title: "GitHub",+ scopes: [],+ username: "octocat",+ });+ }),+ );+ openOAuthPopup.mockReturnValue({+ promise: Promise.resolve({ code: "auth-code", state: "state-abc" }),+ cleanup: { abort: vi.fn() },+ popupBlocked: false,+ fallbackBlocked: false,+ });++ renderPanel();+ const user = await openGithub();++ await user.click(screen.getByRole("button", { name: /OAuth/ }));+ // OAuth has no inline inputs — it is driven entirely by Continue.+ expect(+ screen.queryByLabelText("API key", { selector: "input" }),+ ).toBeNull();++ await user.click(screen.getByRole("button", { name: "Continue" }));++ await waitFor(() => expect(exchanges).toHaveLength(1));+ expect(loginUrlRequests).toBe(1);+ expect(openOAuthPopup).toHaveBeenCalledWith(+ "https://github.test/oauth",+ expect.objectContaining({ stateToken: "state-abc" }),+ );+ expect(exchanges[0].provider).toBe("github");+ expect(exchanges[0].body).toMatchObject({+ code: "auth-code",+ state_token: "state-abc",+ });+ expect(toastSpy).toHaveBeenCalledWith({+ title: "Connected via OAuth",+ variant: "success",+ });+ expect(await screen.findByLabelText("Search services")).toBeDefined();+ });+});++describe("ConnectToolsPanel — search and failure states", () => {+ it("searches the whole registry and reports when nothing matches", async () => {+ stubPanel({ recommended: [] });+ renderPanel();++ const user = userEvent.setup();+ const search = await screen.findByLabelText("Search services");++ await user.type(search, "github");++ // The query is debounced, so the unmatched row is dropped a beat later.+ await waitFor(() =>+ expect(screen.queryByRole("button", { name: /Smtp/ })).toBeNull(),+ );+ expect(screen.getByRole("button", { name: /GitHub/ })).toBeDefined();++ await user.clear(search);+ await user.type(search, "zzz");++ expect(await screen.findByText('No services match "zzz"')).toBeDefined();+ });++ it("surfaces a retryable error when the provider list cannot be loaded", async () => {+ let attempts = 0;+ server.use(+ http.get(PROVIDERS_URL, () => {+ attempts++;+ if (attempts === 1) {+ return HttpResponse.json({ detail: "boom" }, { status: 500 });+ }+ return HttpResponse.json(REGISTRY);+ }),+ http.get(CREDENTIALS_URL, () => HttpResponse.json([])),+ http.get(RECOMMENDED_URL, () =>+ HttpResponse.json({ ready: true, providers: [] }),+ ),+ );++ renderPanel();++ const errorCard = await screen.findByText(+ "We had the following error when retrieving providers:",+ );+ expect(errorCard).toBeDefined();+ expect(screen.queryByLabelText("Search services")).toBeNull();++ await userEvent.click(screen.getByRole("button", { name: /Try Again/ }));++ expect(await screen.findByLabelText("Search services")).toBeDefined();+ expect(attempts).toBeGreaterThan(1);+ });+});++describe("ConnectProviderRow — missing artwork", () => {+ it("falls back to the provider initial when the logo fails to load", async () => {+ stubPanel({ recommended: ["github"] });+ renderPanel();++ const row = await screen.findByRole("button", { name: /GitHub/ });+ const logo = row.querySelector("img");+ expect(logo).not.toBeNull();++ await waitFor(() => {+ logo?.dispatchEvent(new Event("error"));+ expect(within(row).getByText("G")).toBeDefined();+ });+ expect(row.querySelector("img")).toBeNull();+ });+});diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/OnboardingWelcomeDialog/__tests__/recommended-providers.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/OnboardingWelcomeDialog/__tests__/recommended-providers.test.tsxnew file mode 100644index 0000000000..fe3dabfe13--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/OnboardingWelcomeDialog/__tests__/recommended-providers.test.tsx@@ -0,0 +1,200 @@+import { server } from "@/mocks/mock-server";+import { render, screen, waitFor } from "@/tests/integrations/test-utils";+import { http, HttpResponse } from "msw";+import { afterEach, describe, expect, it, vi } from "vitest";++// Recommendations only exist behind the brain-dump flag; these tests are+// about what the panel does once it is on.+vi.mock("@/services/feature-flags/use-get-flag", async (importOriginal) => {+ const actual =+ await importOriginal<+ typeof import("@/services/feature-flags/use-get-flag")+ >();+ return {+ ...actual,+ useGetFlag: (flag: string) => flag === actual.Flag.ONBOARDING_BRAIN_DUMP,+ };+});++import { ConnectToolsPanel } from "../ConnectToolsPanel";++const PROVIDERS_URL =+ "http://localhost:3000/api/proxy/api/integrations/providers";+const CREDENTIALS_URL =+ "http://localhost:3000/api/proxy/api/integrations/credentials";+const RECOMMENDED_URL =+ "http://localhost:3000/api/proxy/api/onboarding/brain-dump/recommended-providers";++const POLL_INTERVAL_MS = 2_500;+const RECOMMENDED_HEADING = "Recommended from our conversation";+const SEARCH_PROMPT = "Search to find a service to connect.";++type RecommendedResponse = {+ ready: boolean;+ providers?: { provider: string; reason?: string }[] | null;+};++function stubRegistry() {+ server.use(+ http.get(PROVIDERS_URL, () =>+ HttpResponse.json([+ {+ name: "notion",+ description: "Docs and wikis",+ supported_auth_types: ["oauth2"],+ },+ {+ name: "slack",+ description: "Team chat",+ supported_auth_types: ["oauth2"],+ },+ ]),+ ),+ http.get(CREDENTIALS_URL, () => HttpResponse.json([])),+ );+}++// Serves one scripted response per poll, reusing the last one forever, and+// reports how many times the endpoint was actually hit.+function scriptRecommendations(...responses: RecommendedResponse[]) {+ const hits: number[] = [];+ server.use(+ http.get(RECOMMENDED_URL, () => {+ const index = Math.min(hits.length, responses.length - 1);+ hits.push(index);+ return HttpResponse.json(responses[index]);+ }),+ );+ return hits;+}++function renderPanel() {+ return render(<ConnectToolsPanel onBack={vi.fn()} onNext={vi.fn()} />);+}++async function pollTimes(count: number) {+ for (let i = 0; i < count; i++) {+ await vi.advanceTimersByTimeAsync(POLL_INTERVAL_MS);+ }+}++afterEach(() => {+ vi.useRealTimers();+});++describe("ConnectToolsPanel — recommendations", () => {+ it("renders the model's picks under the recommended heading", async () => {+ stubRegistry();+ scriptRecommendations({+ ready: true,+ providers: [+ { provider: "notion", reason: "You mentioned meeting notes" },+ ],+ });++ renderPanel();++ expect(await screen.findByText(RECOMMENDED_HEADING)).toBeDefined();+ expect(screen.getByRole("button", { name: /Notion/ })).toBeDefined();+ // The reason is the whole point of the section — it has to be on screen,+ // not just in the mapped data.+ expect(screen.getByText("You mentioned meeting notes")).toBeDefined();+ // Only what the model picked — not the whole registry.+ expect(screen.queryByRole("button", { name: /Slack/ })).toBeNull();+ expect(screen.queryByText(SEARCH_PROMPT)).toBeNull();+ });++ it("falls back to the registry blurb when the model gave no reason", async () => {+ stubRegistry();+ scriptRecommendations({+ ready: true,+ providers: [{ provider: "notion", reason: "" }],+ });++ renderPanel();++ expect(await screen.findByText("Docs and wikis")).toBeDefined();+ });++ it("drops recommendations whose provider is no longer in the registry", async () => {+ stubRegistry();+ scriptRecommendations({+ ready: true,+ providers: [+ { provider: "retired_tool", reason: "Renamed since the job ran" },+ { provider: "slack", reason: "You said your team lives in chat" },+ ],+ });++ renderPanel();++ expect(await screen.findByRole("button", { name: /Slack/ })).toBeDefined();+ expect(screen.queryByRole("button", { name: /Retired/ })).toBeNull();+ expect(screen.getAllByText(RECOMMENDED_HEADING)).toHaveLength(1);+ });++ it("shows the search prompt and stops polling when the model recommends nothing", async () => {+ vi.useFakeTimers({ shouldAdvanceTime: true });+ stubRegistry();+ // An empty array is a real answer, not "still working".+ const hits = scriptRecommendations({ ready: true, providers: [] });++ renderPanel();++ await waitFor(() => expect(screen.getByText(SEARCH_PROMPT)).toBeDefined());+ expect(screen.queryByText(RECOMMENDED_HEADING)).toBeNull();++ await pollTimes(4);+ expect(hits).toHaveLength(1);+ });++ it("keeps polling while the job is unfinished and renders the answer when it lands", async () => {+ vi.useFakeTimers({ shouldAdvanceTime: true });+ stubRegistry();+ // `providers: null` while `ready` is false is the "job still running"+ // signal — it must not be mistaken for "nothing to recommend".+ const hits = scriptRecommendations(+ { ready: false, providers: null },+ { ready: false, providers: null },+ {+ ready: true,+ providers: [{ provider: "notion", reason: "You mentioned notes" }],+ },+ );++ renderPanel();++ await waitFor(() => expect(screen.getByText(SEARCH_PROMPT)).toBeDefined());+ expect(hits).toHaveLength(1);++ await pollTimes(2);++ await waitFor(() =>+ expect(screen.getByRole("button", { name: /Notion/ })).toBeDefined(),+ );+ expect(screen.queryByText(SEARCH_PROMPT)).toBeNull();+ expect(hits).toHaveLength(3);++ // Results arrived, so the panel must go quiet.+ await pollTimes(4);+ expect(hits).toHaveLength(3);+ });++ it("gives up polling after roughly a minute when the job never reports ready", async () => {+ vi.useFakeTimers({ shouldAdvanceTime: true });+ stubRegistry();+ const hits = scriptRecommendations({ ready: false, providers: null });++ renderPanel();++ await waitFor(() => expect(hits.length).toBeGreaterThan(0));++ await pollTimes(40);+ const settled = hits.length;+ // 24 refetches after the initial load — see MAX_POLLS in the hook.+ expect(settled).toBe(25);++ await pollTimes(10);+ expect(hits).toHaveLength(settled);+ });+});diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/OnboardingWelcomeDialog/__tests__/useConnectToolsPanel.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/OnboardingWelcomeDialog/__tests__/useConnectToolsPanel.test.tsxnew file mode 100644index 0000000000..461b7107dd--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/OnboardingWelcomeDialog/__tests__/useConnectToolsPanel.test.tsx@@ -0,0 +1,168 @@+import { server } from "@/mocks/mock-server";+import { QueryClient, QueryClientProvider } from "@tanstack/react-query";+import { act, renderHook, waitFor } from "@testing-library/react";+import { http, HttpResponse } from "msw";+import type { ReactNode } from "react";+import { describe, expect, it, vi } from "vitest";++// Recommendations only exist behind the brain-dump flag; these tests are+// about what the panel does once it is on.+vi.mock("@/services/feature-flags/use-get-flag", async (importOriginal) => {+ const actual =+ await importOriginal<+ typeof import("@/services/feature-flags/use-get-flag")+ >();+ return {+ ...actual,+ useGetFlag: (flag: string) => flag === actual.Flag.ONBOARDING_BRAIN_DUMP,+ };+});++import { useConnectToolsPanel } from "../useConnectToolsPanel";++const PROVIDERS_URL =+ "http://localhost:3000/api/proxy/api/integrations/providers";+const CREDENTIALS_URL =+ "http://localhost:3000/api/proxy/api/integrations/credentials";+const RECOMMENDED_URL =+ "http://localhost:3000/api/proxy/api/onboarding/brain-dump/recommended-providers";++function stub(recommendations: { provider: string; reason?: string }[]) {+ server.use(+ http.get(PROVIDERS_URL, () =>+ HttpResponse.json([+ {+ name: "notion",+ description: "Docs and wikis",+ supported_auth_types: ["api_key"],+ },+ {+ name: "slack",+ description: "Team chat",+ supported_auth_types: ["oauth2"],+ },+ ]),+ ),+ http.get(CREDENTIALS_URL, () => HttpResponse.json([])),+ http.get(RECOMMENDED_URL, () =>+ HttpResponse.json({ ready: true, providers: recommendations }),+ ),+ );+}++function wrapper({ children }: { children: ReactNode }) {+ const queryClient = new QueryClient({+ defaultOptions: { queries: { retry: false } },+ });+ return (+ <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>+ );+}++function setup() {+ return renderHook(() => useConnectToolsPanel(), { wrapper });+}++describe("useConnectToolsPanel — recommendation mapping", () => {+ it("swaps the registry blurb for the model's reason", async () => {+ stub([{ provider: "notion", reason: "You said your notes live there" }]);++ const { result } = setup();++ await waitFor(() =>+ expect(result.current.recommendedProviders).toHaveLength(1),+ );+ expect(result.current.recommendedProviders[0]).toMatchObject({+ id: "notion",+ name: "Notion",+ description: "You said your notes live there",+ });+ });++ it("keeps the registry blurb when the model returns no reason", async () => {+ stub([{ provider: "notion", reason: "" }]);++ const { result } = setup();++ await waitFor(() =>+ expect(result.current.recommendedProviders).toHaveLength(1),+ );+ expect(result.current.recommendedProviders[0].description).toBe(+ "Docs and wikis",+ );+ });++ it("drops picks whose provider is not in the live registry", async () => {+ stub([+ { provider: "retired_tool", reason: "Gone since the job ran" },+ { provider: "slack", reason: "Your team lives in chat" },+ ]);++ const { result } = setup();++ await waitFor(() =>+ expect(result.current.recommendedProviders).toHaveLength(1),+ );+ expect(result.current.recommendedProviders[0].id).toBe("slack");+ });+});++describe("useConnectToolsPanel — selection", () => {+ it("clears a half-typed API key when a different provider is picked", async () => {+ stub([{ provider: "notion" }, { provider: "slack" }]);++ const { result } = setup();+ await waitFor(() => expect(result.current.providers).toHaveLength(2));++ act(() => result.current.handleSelect("notion"));+ act(() =>+ result.current.apiKeyForm.setValue("apiKey", "sk-half-typed", {+ shouldValidate: true,+ }),+ );+ expect(result.current.apiKeyForm.getValues("apiKey")).toBe("sk-half-typed");++ act(() => result.current.handleSelect("slack"));++ expect(result.current.selectedProvider?.id).toBe("slack");+ expect(result.current.selectedMethod).toBeNull();+ // A key typed for the previous provider must not carry over.+ expect(result.current.apiKeyForm.getValues("apiKey")).toBe("");+ });++ it("returns to the list and forgets the chosen method on back", async () => {+ stub([{ provider: "slack" }]);++ const { result } = setup();+ await waitFor(() => expect(result.current.providers).toHaveLength(2));++ act(() => result.current.handleSelect("slack"));+ act(() => result.current.setSelectedMethod("oauth2"));+ expect(result.current.isContinueDisabled).toBe(false);++ act(() => result.current.handleBackToList());++ expect(result.current.selectedProvider).toBeNull();+ expect(result.current.selectedMethod).toBeNull();+ expect(result.current.isContinueDisabled).toBe(true);+ expect(result.current.direction).toBe(-1);+ });++ it("hides Continue for methods it cannot drive", async () => {+ stub([]);++ const { result } = setup();+ await waitFor(() => expect(result.current.providers).toHaveLength(2));++ act(() => result.current.handleSelect("notion"));+ act(() => result.current.setSelectedMethod("host_scoped"));++ expect(result.current.showContinue).toBe(false);++ act(() => result.current.setSelectedMethod("api_key"));++ expect(result.current.showContinue).toBe(true);+ // An empty key form is not submittable.+ expect(result.current.isContinueDisabled).toBe(true);+ });+});diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/OnboardingWelcomeDialog/__tests__/welcome-dialog.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/OnboardingWelcomeDialog/__tests__/welcome-dialog.test.tsxnew file mode 100644index 0000000000..c3fd8a6d88--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/OnboardingWelcomeDialog/__tests__/welcome-dialog.test.tsx@@ -0,0 +1,263 @@+import { server } from "@/mocks/mock-server";+import { render, screen, waitFor } from "@/tests/integrations/test-utils";+import userEvent from "@testing-library/user-event";+import { http, HttpResponse } from "msw";+import { beforeEach, describe, expect, it, vi } from "vitest";++const capture = vi.fn();+vi.mock("posthog-js", () => ({+ default: { capture: (...a: unknown[]) => capture(...a) },+}));++const toastSpy = vi.fn();+vi.mock("@/components/molecules/Toast/use-toast", () => ({+ toast: (...args: unknown[]) => toastSpy(...args),+ useToast: () => ({ toast: toastSpy }),+ useToastOnFail: () => () => {},+}));++import { OnboardingWelcomeDialog } from "../OnboardingWelcomeDialog";++const STEP_URL = "http://localhost:3000/api/proxy/api/onboarding/step";+const PROVIDERS_URL =+ "http://localhost:3000/api/proxy/api/integrations/providers";+const CREDENTIALS_URL =+ "http://localhost:3000/api/proxy/api/integrations/credentials";+const RECOMMENDED_URL =+ "http://localhost:3000/api/proxy/api/onboarding/brain-dump/recommended-providers";++// Every step completion the dialog posts, with the step name it claimed.+function recordCompletedSteps(status = 200) {+ const steps: (string | null)[] = [];+ server.use(+ http.post(STEP_URL, ({ request }) => {+ steps.push(new URL(request.url).searchParams.get("step"));+ return new HttpResponse(null, { status });+ }),+ );+ return steps;+}++function stubConnectPanelEndpoints() {+ server.use(+ http.get(PROVIDERS_URL, () =>+ HttpResponse.json([+ {+ name: "github",+ description: "Code host",+ supported_auth_types: ["oauth2"],+ },+ ]),+ ),+ http.get(CREDENTIALS_URL, () => HttpResponse.json([])),+ http.get(RECOMMENDED_URL, () =>+ HttpResponse.json({ ready: true, providers: [] }),+ ),+ );+}++async function advanceToCard(index: number) {+ const user = userEvent.setup();+ for (let i = 0; i < index; i++) {+ await user.click(screen.getByRole("button", { name: "Next" }));+ }+ return user;+}++beforeEach(() => {+ capture.mockClear();+ toastSpy.mockClear();+});++describe("OnboardingWelcomeDialog — deck", () => {+ it("renders the first card with its position in the deck and no way back", async () => {+ render(<OnboardingWelcomeDialog isOpen onClose={vi.fn()} />);++ expect(await screen.findByText("Meet AutoPilot.")).toBeDefined();+ expect(+ screen.getByText(+ "It does the work. Ask once, or put it on a schedule. It delivers while you do something else.",+ ),+ ).toBeDefined();+ expect(screen.getByText("1 of 4")).toBeDefined();+ expect(screen.getByRole("button", { name: "Next" })).toBeDefined();+ expect(screen.getByRole("button", { name: "Skip" })).toBeDefined();+ expect(screen.queryByRole("button", { name: "Previous card" })).toBeNull();+ });++ it("walks forward and back through the deck", async () => {+ render(<OnboardingWelcomeDialog isOpen onClose={vi.fn()} />);+ const user = await advanceToCard(1);++ expect(+ await screen.findByText("It works inside your tools."),+ ).toBeDefined();+ expect(screen.getByText("2 of 4")).toBeDefined();+ expect(capture).toHaveBeenCalledWith("capability_card_viewed", {+ card_index: 1,+ });++ await user.click(screen.getByRole("button", { name: "Previous card" }));++ expect(await screen.findByText("Meet AutoPilot.")).toBeDefined();+ expect(screen.getByText("1 of 4")).toBeDefined();+ });++ it("renders nothing and completes no step while closed", async () => {+ const steps = recordCompletedSteps();++ render(<OnboardingWelcomeDialog isOpen={false} onClose={vi.fn()} />);++ expect(screen.queryByRole("dialog")).toBeNull();+ expect(screen.queryByText("Meet AutoPilot.")).toBeNull();+ expect(steps).toEqual([]);+ });+});++describe("OnboardingWelcomeDialog — completion", () => {+ it("records CAPABILITY_CARDS and closes once the last card is finished", async () => {+ const steps = recordCompletedSteps();+ const onClose = vi.fn();++ render(<OnboardingWelcomeDialog isOpen onClose={onClose} />);+ const user = await advanceToCard(3);++ expect(await screen.findByText("It remembers everything.")).toBeDefined();+ await user.click(screen.getByRole("button", { name: "Meet AutoPilot" }));++ expect(onClose).toHaveBeenCalledTimes(1);+ await waitFor(() => expect(steps).toEqual(["CAPABILITY_CARDS"]));+ expect(capture).toHaveBeenCalledWith("capability_cards_completed", {+ card_index: 3,+ });+ });++ it("records the same step when skipped, tagged with the card the user quit on", async () => {+ const steps = recordCompletedSteps();+ const onClose = vi.fn();++ render(<OnboardingWelcomeDialog isOpen onClose={onClose} />);+ const user = await advanceToCard(1);+ await screen.findByText("It works inside your tools.");++ await user.click(screen.getByRole("button", { name: "Skip" }));++ expect(onClose).toHaveBeenCalledTimes(1);+ await waitFor(() => expect(steps).toEqual(["CAPABILITY_CARDS"]));+ expect(capture).toHaveBeenCalledWith("capability_cards_skipped", {+ card_index: 1,+ });+ });++ it("treats Escape as a skip", async () => {+ const steps = recordCompletedSteps();+ const onClose = vi.fn();++ render(<OnboardingWelcomeDialog isOpen onClose={onClose} />);+ await screen.findByText("Meet AutoPilot.");++ await userEvent.keyboard("{Escape}");++ expect(onClose).toHaveBeenCalledTimes(1);+ await waitFor(() => expect(steps).toEqual(["CAPABILITY_CARDS"]));+ expect(capture).toHaveBeenCalledWith("capability_cards_skipped", {+ card_index: 0,+ });+ });++ it("warns the user when the progress could not be saved", async () => {+ recordCompletedSteps(500);++ render(<OnboardingWelcomeDialog isOpen onClose={vi.fn()} />);+ await screen.findByText("Meet AutoPilot.");++ await userEvent.click(screen.getByRole("button", { name: "Skip" }));++ await waitFor(() =>+ expect(toastSpy).toHaveBeenCalledWith({+ title: "Could not save your onboarding progress",+ description: "You may see this introduction again next time.",+ variant: "destructive",+ }),+ );+ });+});++describe("OnboardingWelcomeDialog — connect tools CTA", () => {+ it("swaps the card for the provider picker without closing the dialog", async () => {+ stubConnectPanelEndpoints();+ const steps = recordCompletedSteps();+ const onClose = vi.fn();++ render(<OnboardingWelcomeDialog isOpen onClose={onClose} />);+ const user = await advanceToCard(1);+ await screen.findByText("It works inside your tools.");++ await user.click(+ screen.getByRole("button", { name: "Connect your tools" }),+ );++ expect(+ await screen.findByRole("heading", { name: "Connect your tools" }),+ ).toBeDefined();+ expect(screen.queryByText("It works inside your tools.")).toBeNull();+ // Connecting tools must never end the introduction.+ expect(onClose).not.toHaveBeenCalled();+ expect(steps).toEqual([]);++ await user.click(screen.getByRole("button", { name: "Back" }));++ expect(+ await screen.findByText("It works inside your tools."),+ ).toBeDefined();+ expect(screen.getByText("2 of 4")).toBeDefined();+ });++ it("steps back out of the picker on Escape instead of ending onboarding", async () => {+ stubConnectPanelEndpoints();+ const steps = recordCompletedSteps();+ const onClose = vi.fn();++ render(<OnboardingWelcomeDialog isOpen onClose={onClose} />);+ const user = await advanceToCard(1);+ await user.click(+ screen.getByRole("button", { name: "Connect your tools" }),+ );+ await screen.findByRole("heading", { name: "Connect your tools" });++ await user.keyboard("{Escape}");++ // Back to the deck, with the introduction still running.+ expect(+ await screen.findByText("It works inside your tools."),+ ).toBeDefined();+ expect(onClose).not.toHaveBeenCalled();+ expect(steps).toEqual([]);+ expect(capture).not.toHaveBeenCalledWith(+ "capability_cards_skipped",+ expect.anything(),+ );++ // Escape is a skip again once the picker is gone.+ await user.keyboard("{Escape}");++ expect(onClose).toHaveBeenCalledTimes(1);+ });++ it("advances to the next card when the picker's Next is used", async () => {+ stubConnectPanelEndpoints();++ render(<OnboardingWelcomeDialog isOpen onClose={vi.fn()} />);+ const user = await advanceToCard(1);+ await screen.findByText("It works inside your tools.");+ await user.click(+ screen.getByRole("button", { name: "Connect your tools" }),+ );+ await screen.findByRole("heading", { name: "Connect your tools" });++ await user.click(screen.getByRole("button", { name: "Next" }));++ expect(await screen.findByText("It learns how you operate.")).toBeDefined();+ expect(screen.getByText("3 of 4")).toBeDefined();+ });+});diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/OnboardingWelcomeDialog/useConnectToolsPanel.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/OnboardingWelcomeDialog/useConnectToolsPanel.tsnew file mode 100644index 0000000000..226e025efb--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/OnboardingWelcomeDialog/useConnectToolsPanel.ts@@ -0,0 +1,160 @@+"use client";++import { useGetBrainDumpRecommendedProviders } from "@/app/api/__generated__/endpoints/brain-dump/brain-dump";+import {+ useGetV1ListCredentials,+ useGetV1ListProviders,+} from "@/app/api/__generated__/endpoints/integrations/integrations";+import { useApiKeyConnectForm } from "@/components/contextual/IntegrationsPanel/components/ConnectServiceDialog/components/DetailView/useApiKeyConnectForm";+import { useOAuthConnect } from "@/components/contextual/IntegrationsPanel/components/ConnectServiceDialog/components/DetailView/useOAuthConnect";+import {+ AuthType,+ filterConnectableProviders,+ toConnectableProviders,+ type AuthMethod,+ type ConnectableProvider,+} from "@/components/contextual/IntegrationsPanel/components/ConnectServiceDialog/helpers";+import { useDebouncedValue } from "@/hooks/useDebouncedValue";+import { Flag, useGetFlag } from "@/services/feature-flags/use-get-flag";+import { useState } from "react";++const POLL_INTERVAL_MS = 2_500;+// ~1 minute of polling; a job the backend never finished (process restart+// mid-run) must not keep this dialog requesting forever.+const MAX_POLLS = 24;++export function useConnectToolsPanel() {+ // The endpoint 404s with the flag off, and a 404 never settles the poll+ // below on its own — so the request is not made at all.+ const isBrainDumpEnabled = useGetFlag(Flag.ONBOARDING_BRAIN_DUMP);+ const [query, setQuery] = useState("");+ const debouncedQuery = useDebouncedValue(query, 250);+ const [selectedId, setSelectedId] = useState<string | null>(null);+ const [direction, setDirection] = useState<1 | -1>(1);+ const [selectedMethod, setSelectedMethod] = useState<AuthMethod | null>(null);++ const providersQuery = useGetV1ListProviders({+ query: {+ select: (response) => (response.status === 200 ? response.data : []),+ },+ });++ // Connected state per provider; the OAuth and API-key flows invalidate+ // this query on success, so freshly connected rows flip immediately.+ const credentialsQuery = useGetV1ListCredentials({+ query: {+ select: (response) => (response.status === 200 ? response.data : []),+ },+ });+ const connectedProviders = new Set(+ (credentialsQuery.data ?? []).map((credential) => credential.provider),+ );++ const recommendedQuery = useGetBrainDumpRecommendedProviders({+ query: {+ enabled: Boolean(isBrainDumpEnabled),+ refetchInterval: (q) => {+ const response = q.state.data;+ if (response && (response.status !== 200 || response.data.ready)) {+ return false;+ }+ // A failed request never reaches `data` — it rejects — so the+ // budget counts errors too; otherwise a 404 or a 500 would poll+ // for as long as the dialog stays open.+ const attempts = q.state.dataUpdateCount + q.state.errorUpdateCount;+ return attempts > MAX_POLLS ? false : POLL_INTERVAL_MS;+ },+ },+ });++ const allProviders = toConnectableProviders(providersQuery.data ?? []);+ const providers = filterConnectableProviders(allProviders, debouncedQuery);+ const selectedProvider: ConnectableProvider | null = selectedId+ ? (allProviders.find((provider) => provider.id === selectedId) ?? null)+ : null;++ const recommendations =+ recommendedQuery.data?.status === 200+ ? (recommendedQuery.data.data.providers ?? [])+ : [];+ // The model's reason replaces the generic provider description — that+ // line is what makes the section feel picked for this user. Unknown ids+ // (provider renamed or removed since the job ran) are dropped.+ const recommendedProviders = recommendations+ .map((recommendation): ConnectableProvider | null => {+ const provider = allProviders.find(+ (p) => p.id === recommendation.provider,+ );+ if (!provider) return null;+ return {+ ...provider,+ description: recommendation.reason || provider.description,+ };+ })+ .filter((provider) => provider !== null);++ function handleSelect(providerId: string) {+ setDirection(1);+ setSelectedId(providerId);+ setSelectedMethod(null);+ apiKey.form.reset();+ }++ function handleBackToList() {+ setDirection(-1);+ setSelectedId(null);+ setSelectedMethod(null);+ }++ // Completing a connection (OAuth or the API-key form) lands back on the+ // list so more tools can be wired up without leaving the dialog.+ const oauth = useOAuthConnect({+ provider: selectedProvider?.id ?? "",+ onSuccess: handleBackToList,+ });+ const apiKey = useApiKeyConnectForm({+ provider: selectedProvider?.id ?? "",+ onSuccess: handleBackToList,+ });++ function handleContinue() {+ if (selectedMethod === AuthType.oauth2) {+ oauth.connect();+ return;+ }+ if (selectedMethod === AuthType.api_key) {+ apiKey.form.handleSubmit(apiKey.handleSubmit)();+ }+ }++ return {+ query,+ setQuery,+ providers,+ recommendedProviders,+ isLoading: providersQuery.isLoading,+ isError: providersQuery.isError,+ error: providersQuery.error,+ refetch: providersQuery.refetch,+ selectedProvider,+ direction,+ connectedProviders,+ selectedMethod,+ setSelectedMethod,+ apiKeyForm: apiKey.form,+ handleApiKeySubmit: apiKey.handleSubmit,+ // The footer's Continue drives OAuth and the API-key form; the+ // remaining methods only show an unsupported notice.+ showContinue:+ !selectedMethod ||+ selectedMethod === AuthType.oauth2 ||+ selectedMethod === AuthType.api_key,+ isContinueDisabled:+ !selectedMethod ||+ (selectedMethod === AuthType.api_key && !apiKey.form.formState.isValid),+ isConnecting: oauth.isPending || apiKey.isPending,+ handleSelect,+ handleBackToList,+ handleContinue,+ };+}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPage.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPage.tsindex 252417a504..354ab1f114 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPage.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPage.ts@@ -20,6 +20,13 @@ import { useLoadMoreMessages } from "./useLoadMoreMessages";import { useSendMessage } from "./useSendMessage";import { useSessionTitlePoll } from "./useSessionTitlePoll";import { useWorkflowImportAutoSubmit } from "./useWorkflowImportAutoSubmit";+import { useCompleteBrainDumpGreeting } from "@/app/api/__generated__/endpoints/brain-dump/brain-dump";+import { trackBrainDump } from "@/services/onboarding/brain-dump-analytics";+import {+ peekGreetingDone,+ setGreetingDone,+ takeIntroAwaitingFollowup,+} from "@/services/onboarding/brain-dump-handoff";function trimVisibleMessagesForActiveRestore(messages: UIMessage[]) {const lastUserIndex = messages.findLastIndex(@@ -39,14 +46,16 @@ function hasAssistantTail(messages: UIMessage[]) {}export function useCopilotPage() {- const { isUserLoading, isLoggedIn } = useAuth();+ const { user, isUserLoading, isLoggedIn } = useAuth();const isModeToggleEnabled = useGetFlag(Flag.CHAT_MODE_OPTION);const isExpertsEnabled = useGetFlag(Flag.HIRE_EXPERTS);+ const isBrainDumpEnabled = useGetFlag(Flag.ONBOARDING_BRAIN_DUMP);const [expertIdParam] = useQueryState("expertId", parseAsString);const expertId = isExpertsEnabled ? expertIdParam : null;const { expertsById } = useExpertMap();const { copilotChatMode, copilotLlmModel, isDryRun } = useCopilotUIStore();+ const { mutate: completeGreeting } = useCompleteBrainDumpGreeting();const {sessionId,@@ -199,6 +208,24 @@ export function useCopilotPage() {(files?.length ?? 0) > 0 || (workspaceFiles?.length ?? 0) > 0;if (!trimmed && !hasAttachments) return;+ // Sending anything retires the greeting for good: flag it done on+ // the server (kept in the DB, just never shown again) and cache the+ // fact locally so no future visit even has to ask.+ // Gated on the flag: the endpoint 404s without it, and because the+ // local cache is only written on success, an ungated call retried on+ // every single message the user ever sent.+ if (isBrainDumpEnabled && !peekGreetingDone(user?.id)) {+ completeGreeting(undefined, {+ onSuccess: () => setGreetingDone(user?.id),+ // Retiring the greeting is bookkeeping — the worst case is+ // seeing it once more — so a failure must never surface here.+ onError: () => undefined,+ });+ }+ if (takeIntroAwaitingFollowup()) {+ trackBrainDump("intro_followup_sent", { chars: trimmed.length });+ }+if (sessionId && isInflightRef.current) {if (hasAttachments) {toast({diff --git a/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/__tests__/finalize.test.tsx b/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/__tests__/finalize.test.tsxnew file mode 100644index 0000000000..1a42b32c1e--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/__tests__/finalize.test.tsx@@ -0,0 +1,321 @@+import {+ getFinalizeBrainDumpMockHandler200,+ getGetBrainDumpStatusMockHandler200,+ getGetBrainDumpStatusMockHandler401,+} from "@/app/api/__generated__/endpoints/brain-dump/brain-dump.msw";+import type { DumpStatusResponse } from "@/app/api/__generated__/models/dumpStatusResponse";+import { server } from "@/mocks/mock-server";+import { render, screen, within } from "@/tests/integrations/test-utils";+import userEvent from "@testing-library/user-event";+import { http, HttpResponse } from "msw";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";++const FINALIZE_URL =+ "http://localhost:3000/api/proxy/api/onboarding/brain-dump/finalize";++interface FakeMeta {+ recordingId: string;+ mimeType: string;+ startedAt: number;+ durationSecs: number;+ finalized: boolean;+}++const { store, flagStatus } = vi.hoisted(() => ({+ store: {+ meta: null as FakeMeta | null,+ },+ flagStatus: { enabled: true, ready: true },+}));++vi.mock(+ "@/app/(no-navbar)/onboarding/steps/BrainDumpStep/recordingStore",+ () => ({+ isIndexedDBAvailable: () => true,+ getMeta: async () => store.meta,+ getParts: async () => [],+ }),+);++vi.mock("@/services/feature-flags/use-get-flag", async (importOriginal) => {+ const actual =+ await importOriginal<+ typeof import("@/services/feature-flags/use-get-flag")+ >();+ return {+ ...actual,+ useFlagStatus: (flag: string) =>+ flag === "onboarding-brain-dump"+ ? flagStatus+ : actual.useFlagStatus(flag as never),+ };+});++vi.mock("next/navigation", () => ({+ useRouter: () => ({+ push: vi.fn(),+ replace: vi.fn(),+ prefetch: vi.fn(),+ back: vi.fn(),+ forward: vi.fn(),+ refresh: vi.fn(),+ }),+ usePathname: () => "/dev/brain-dump-debug",+ useSearchParams: () => new URLSearchParams(),+ useParams: () => ({}),+ notFound: () => {+ throw new Error("NEXT_NOT_FOUND");+ },+}));++import BrainDumpDebugPage from "../page";++const STORED_META: FakeMeta = {+ recordingId: "rec-42",+ mimeType: "audio/webm;codecs=opus",+ startedAt: 1_700_000_000_000,+ durationSecs: 91.5,+ finalized: false,+};++function panel(title: string) {+ const section = screen.getByText(title).closest("section");+ if (!section) throw new Error(`no panel titled ${title}`);+ return within(section);+}++function serveStatus(...responses: DumpStatusResponse[]) {+ let call = 0;+ server.use(+ getGetBrainDumpStatusMockHandler200(() => {+ const response = responses[Math.min(call, responses.length - 1)];+ call += 1;+ return response;+ }),+ );+}++// Records the finalize request bodies so the debug call can be checked+// against the meta row it claims to replay.+function captureFinalize(response: {+ status: "completed" | "failed";+ input_mode: "voice" | "typed" | "skipped";+ transcript_preview?: string;+ error_code?: string;+}) {+ const bodies: unknown[] = [];+ server.use(+ getFinalizeBrainDumpMockHandler200(async (info) => {+ bodies.push(await info.request.json());+ return response;+ }),+ );+ return bodies;+}++beforeEach(() => {+ store.meta = null;+ window.sessionStorage.clear();+ serveStatus({ status: "transcribing", input_mode: "voice", has_audio: true });+});++afterEach(() => {+ vi.unstubAllEnvs();+});++describe("server status panel", () => {+ it("renders the polled status fields", async () => {+ serveStatus({+ status: "extracting",+ input_mode: "typed",+ has_audio: false,+ error_code: null,+ });++ render(<BrainDumpDebugPage />);++ const status = panel("Server status");+ expect(await status.findByText("extracting")).toBeDefined();+ expect(status.getByText("typed")).toBeDefined();+ // has_audio false renders as "no", error_code null as an em dash.+ expect(status.getByText("no")).toBeDefined();+ expect(status.getAllByText("—").length).toBeGreaterThan(0);+ });++ it("explains a failing status request rather than showing a blank panel", async () => {+ server.use(getGetBrainDumpStatusMockHandler401());++ render(<BrainDumpDebugPage />);++ const status = panel("Server status");+ expect(+ await status.findByText(/the endpoint 404s when ONBOARDING_BRAIN_DUMP/),+ ).toBeDefined();+ });++ it("links the download at the proxied recording endpoint", async () => {+ render(<BrainDumpDebugPage />);++ const link = await screen.findByRole("link", {+ name: /Download server-side recording/,+ });+ expect(link.getAttribute("href")).toBe(+ "/api/proxy/api/onboarding/brain-dump/recording",+ );+ });+});++describe("debug finalize", () => {+ it("cannot run without a stored recording", async () => {+ render(<BrainDumpDebugPage />);++ const button = await screen.findByRole("button", {+ name: "Run finalize on the stored recording",+ });+ expect((button as HTMLButtonElement).disabled).toBe(true);+ });++ it("replays the stored meta row and shows the response it got back", async () => {+ store.meta = STORED_META;+ const bodies = captureFinalize({+ status: "completed",+ input_mode: "voice",+ transcript_preview: "I spend every Monday rebuilding the same report.",+ });++ render(<BrainDumpDebugPage />);++ const button = await screen.findByRole("button", {+ name: "Run finalize on the stored recording",+ });+ await vi.waitFor(() =>+ expect((button as HTMLButtonElement).disabled).toBe(false),+ );+ await userEvent.click(button);++ expect(await screen.findByText(/^round-trip \d/)).toBeDefined();+ expect(bodies).toEqual([+ {+ recording_id: "rec-42",+ input_mode: "voice",+ duration_secs: 91.5,+ mime_type: "audio/webm;codecs=opus",+ },+ ]);++ // The finalize response, not the polled status, which is "transcribing".+ const status = panel("Server status");+ expect(status.getByText("completed")).toBeDefined();++ const transcript = panel("Transcript & extraction JSON");+ expect(+ transcript.getByText("I spend every Monday rebuilding the same report."),+ ).toBeDefined();+ expect(transcript.queryByText(/None captured/)).toBeNull();++ // The round-trip is also surfaced as the whole post-Done budget.+ expect(+ panel("Timing waterfall").getByText(/^finalize round-trip \d/),+ ).toBeDefined();+ });++ it("surfaces the server's message when finalize fails", async () => {+ store.meta = STORED_META;+ server.use(+ http.post(FINALIZE_URL, () =>+ HttpResponse.json(+ { detail: "recording already consumed" },+ {+ status: 409,+ },+ ),+ ),+ );++ render(<BrainDumpDebugPage />);++ const button = await screen.findByRole("button", {+ name: "Run finalize on the stored recording",+ });+ await vi.waitFor(() =>+ expect((button as HTMLButtonElement).disabled).toBe(false),+ );+ await userEvent.click(button);++ expect(+ await screen.findByText(/Finalize failed: recording already consumed/),+ ).toBeDefined();+ // A failed call must not leave a stale preview behind.+ expect(+ panel("Transcript & extraction JSON").getByText(/None captured/),+ ).toBeDefined();+ });+});++describe("timing waterfall", () => {+ it("measures transcribe once the poll has seen both ends of the phase", async () => {+ serveStatus(+ { status: "transcribing", input_mode: "voice" },+ { status: "completed", input_mode: "voice" },+ );++ render(<BrainDumpDebugPage />);++ const timing = panel("Timing waterfall");+ // Transcribe and extract both start out unobserved.+ expect(timing.getAllByText(/this page did not observe both/)).toHaveLength(+ 2,+ );++ // The second poll lands a second later and closes the transcribe phase.+ expect(+ await timing.findByText(+ /first "recording_uploaded\/transcribing" . first "transcribed"/,+ {},+ { timeout: 4000 },+ ),+ ).toBeDefined();+ expect(timing.getByText("measured")).toBeDefined();+ // Extract never saw `transcribed`, so it stays unmeasured.+ expect(timing.getAllByText(/this page did not observe both/)).toHaveLength(+ 1,+ );+ });++ it("keeps stages the page cannot time labelled unmeasured", async () => {+ render(<BrainDumpDebugPage />);++ const timing = panel("Timing waterfall");+ expect(+ await timing.findByText(/no recording currently in IndexedDB/),+ ).toBeDefined();+ expect(+ timing.getByText(/0 of 0 part\(s\) are still not marked uploaded/),+ ).toBeDefined();+ expect(timing.getAllByText("unmeasured")).toHaveLength(5);+ expect(timing.getAllByText("no budget")).toHaveLength(3);+ });+});++describe("intro handoff", () => {+ it("shows the pending intro path from sessionStorage in the header and the waterfall", async () => {+ window.sessionStorage.setItem("autogpt:onboarding-intro-path", "B");++ render(<BrainDumpDebugPage />);++ expect(await screen.findByText("intro path B")).toBeDefined();+ expect(+ panel("Timing waterfall").getByText(/Pending handoff path: B/),+ ).toBeDefined();+ });++ it("points at the sessionStorage key when no handoff is queued", async () => {+ render(<BrainDumpDebugPage />);++ const timing = panel("Timing waterfall");+ expect(+ await timing.findByText(/autogpt:onboarding-intro-path/),+ ).toBeDefined();+ expect(screen.queryByText(/^intro path/)).toBeNull();+ });+});diff --git a/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/__tests__/helpers.test.ts b/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/__tests__/helpers.test.tsnew file mode 100644index 0000000000..8417e0d137--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/__tests__/helpers.test.ts@@ -0,0 +1,275 @@+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";++interface FakeMeta {+ recordingId: string;+ mimeType: string;+ startedAt: number;+ durationSecs: number;+ finalized: boolean;+}++interface FakePart {+ id: string;+ recordingId: string;+ partIndex: number;+ blob: Blob;+ savedAt: number;+ uploaded: boolean;+}++// IndexedDB does not exist in happy-dom, so the recording store is replaced+// with an in-memory double. `readRecordingSnapshot` itself runs for real.+const { store } = vi.hoisted(() => ({+ store: {+ available: true,+ meta: null as FakeMeta | null,+ parts: [] as FakePart[],+ getPartsError: null as unknown,+ },+}));++vi.mock(+ "@/app/(no-navbar)/onboarding/steps/BrainDumpStep/recordingStore",+ () => ({+ isIndexedDBAvailable: () => store.available,+ getMeta: async () => store.meta,+ getParts: async (recordingId: string) => {+ if (store.getPartsError) throw store.getPartsError;+ return store.parts.filter((part) => part.recordingId === recordingId);+ },+ }),+);++import {+ describeError,+ formatBytes,+ formatClock,+ formatMs,+ formatSeconds,+ formatValue,+ isProductionEnvironment,+ readRecordingSnapshot,+ recordingDownloadHref,+} from "../helpers";++function part(overrides: Partial<FakePart> = {}): FakePart {+ const partIndex = overrides.partIndex ?? 0;+ return {+ id: `rec-1:${partIndex}`,+ recordingId: "rec-1",+ partIndex,+ blob: new Blob(["x"]),+ savedAt: 1000,+ uploaded: false,+ ...overrides,+ };+}++beforeEach(() => {+ store.available = true;+ store.meta = null;+ store.parts = [];+ store.getPartsError = null;+});++afterEach(() => {+ vi.unstubAllEnvs();+ vi.useRealTimers();+});++describe("isProductionEnvironment", () => {+ it("is true only for the prod app env", () => {+ vi.stubEnv("NEXT_PUBLIC_APP_ENV", "prod");+ expect(isProductionEnvironment()).toBe(true);++ // The env var is spelled both ways across our deploys.+ vi.stubEnv("NEXT_PUBLIC_APP_ENV", "production");+ expect(isProductionEnvironment()).toBe(true);++ vi.stubEnv("NEXT_PUBLIC_APP_ENV", "dev");+ expect(isProductionEnvironment()).toBe(false);++ vi.stubEnv("NEXT_PUBLIC_APP_ENV", "local");+ expect(isProductionEnvironment()).toBe(false);++ vi.stubEnv("NEXT_PUBLIC_APP_ENV", "");+ expect(isProductionEnvironment()).toBe(false);+ });+});++describe("recordingDownloadHref", () => {+ it("routes the generated recording URL through the proxy", () => {+ expect(recordingDownloadHref()).toBe(+ "/api/proxy/api/onboarding/brain-dump/recording",+ );+ });+});++describe("describeError", () => {+ it("uses the message for Errors and stringifies anything else", () => {+ expect(describeError(new Error("boom"))).toBe("boom");+ expect(describeError(new TypeError("bad type"))).toBe("bad type");+ expect(describeError("plain string")).toBe("plain string");+ expect(describeError(404)).toBe("404");+ expect(describeError(null)).toBe("null");+ expect(describeError(undefined)).toBe("undefined");+ });+});++describe("readRecordingSnapshot", () => {+ it("reports unsupported without touching the store when IndexedDB is missing", async () => {+ store.available = false;+ store.meta = {+ recordingId: "rec-1",+ mimeType: "audio/webm",+ startedAt: 1,+ durationSecs: 2,+ finalized: false,+ };++ const snapshot = await readRecordingSnapshot();++ expect(snapshot.supported).toBe(false);+ // The meta above must not leak through — nothing was read.+ expect(snapshot.meta).toBeNull();+ expect(snapshot.parts).toEqual([]);+ expect(snapshot.error).toBeNull();+ expect(snapshot.readAt).not.toBeNull();+ });++ it("returns an empty snapshot when no meta row is stored", async () => {+ store.parts = [part()];++ const snapshot = await readRecordingSnapshot();++ expect(snapshot.meta).toBeNull();+ expect(snapshot.parts).toEqual([]);+ expect(snapshot.totalBytes).toBe(0);+ expect(snapshot.pendingUploads).toBe(0);+ expect(snapshot.supported).toBe(true);+ expect(snapshot.readAt).not.toBeNull();+ });++ it("summarises the stored parts of the current recording", async () => {+ store.meta = {+ recordingId: "rec-1",+ mimeType: "audio/webm",+ startedAt: 500,+ durationSecs: 91.5,+ finalized: false,+ };+ store.parts = [+ part({+ partIndex: 0,+ blob: new Blob(["aaa"]),+ savedAt: 700,+ uploaded: true,+ }),+ part({ partIndex: 1, blob: new Blob(["bbbbb"]), savedAt: 900 }),+ part({ partIndex: 2, blob: new Blob(["cc"]), savedAt: 1100 }),+ // Belongs to a different take; must not be counted.+ part({+ partIndex: 0,+ recordingId: "rec-2",+ id: "rec-2:0",+ blob: new Blob(["zzzzzzzzzz"]),+ }),+ ];++ const snapshot = await readRecordingSnapshot();++ expect(snapshot.meta).toEqual(store.meta);+ expect(snapshot.parts).toEqual([+ { partIndex: 0, bytes: 3, savedAt: 700, uploaded: true },+ { partIndex: 1, bytes: 5, savedAt: 900, uploaded: false },+ { partIndex: 2, bytes: 2, savedAt: 1100, uploaded: false },+ ]);+ expect(snapshot.totalBytes).toBe(10);+ expect(snapshot.pendingUploads).toBe(2);+ expect(snapshot.error).toBeNull();+ });++ it("surfaces a read failure instead of throwing", async () => {+ store.meta = {+ recordingId: "rec-1",+ mimeType: "audio/webm",+ startedAt: 1,+ durationSecs: 2,+ finalized: false,+ };+ store.getPartsError = new Error("QuotaExceededError");++ const snapshot = await readRecordingSnapshot();++ expect(snapshot.error).toBe("QuotaExceededError");+ expect(snapshot.meta).toBeNull();+ expect(snapshot.parts).toEqual([]);+ expect(snapshot.readAt).not.toBeNull();+ });++ it("stamps readAt with the clock at read time", async () => {+ vi.useFakeTimers();+ vi.setSystemTime(new Date("2024-03-01T10:00:00.000Z"));++ const snapshot = await readRecordingSnapshot();++ expect(snapshot.readAt).toBe(Date.parse("2024-03-01T10:00:00.000Z"));+ });+});++describe("formatBytes", () => {+ it("climbs units only once the value reaches 1024", () => {+ expect(formatBytes(0)).toBe("0 B");+ expect(formatBytes(512)).toBe("512 B");+ expect(formatBytes(1023)).toBe("1023 B");+ expect(formatBytes(1024)).toBe("1 KB");+ expect(formatBytes(1536)).toBe("1.5 KB");+ expect(formatBytes(1024 * 1024)).toBe("1 MB");+ expect(formatBytes(1024 * 1024 * 1024)).toBe("1 GB");+ });++ it("stops at GB rather than inventing a unit", () => {+ expect(formatBytes(1024 ** 4)).toBe("1024 GB");+ });+});++describe("formatClock", () => {+ it("renders a 24-hour local clock and an em dash for nothing", () => {+ expect(formatClock(null)).toBe("—");+ expect(formatClock(undefined)).toBe("—");+ // Built from local-time parts so the assertion holds in any timezone.+ const localAfternoon = new Date(2024, 2, 1, 13, 5, 9).getTime();+ expect(formatClock(localAfternoon)).toMatch(/^13.05.09$/);+ });+});++describe("formatMs", () => {+ it("switches from whole milliseconds to two-decimal seconds at 1s", () => {+ expect(formatMs(null)).toBe("—");+ expect(formatMs(0)).toBe("0 ms");+ expect(formatMs(12.4)).toBe("12 ms");+ expect(formatMs(999)).toBe("999 ms");+ expect(formatMs(1000)).toBe("1.00 s");+ expect(formatMs(5432)).toBe("5.43 s");+ });+});++describe("formatSeconds", () => {+ it("renders one decimal place, or an em dash for nothing", () => {+ expect(formatSeconds(null)).toBe("—");+ expect(formatSeconds(undefined)).toBe("—");+ expect(formatSeconds(0)).toBe("0.0 s");+ expect(formatSeconds(95.44)).toBe("95.4 s");+ });+});++describe("formatValue", () => {+ it("maps booleans to yes/no and treats empty string as absent", () => {+ expect(formatValue(null)).toBe("—");+ expect(formatValue(undefined)).toBe("—");+ expect(formatValue("")).toBe("—");+ expect(formatValue(true)).toBe("yes");+ expect(formatValue(false)).toBe("no");+ expect(formatValue("transcribing")).toBe("transcribing");+ });+});diff --git a/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/__tests__/main.test.tsx b/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/__tests__/main.test.tsxnew file mode 100644index 0000000000..001e4b9b50--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/__tests__/main.test.tsx@@ -0,0 +1,321 @@+import { getGetBrainDumpStatusMockHandler200 } from "@/app/api/__generated__/endpoints/brain-dump/brain-dump.msw";+import { server } from "@/mocks/mock-server";+import { render, screen, within } from "@/tests/integrations/test-utils";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";++interface FakeMeta {+ recordingId: string;+ mimeType: string;+ startedAt: number;+ durationSecs: number;+ finalized: boolean;+}++interface FakePart {+ id: string;+ recordingId: string;+ partIndex: number;+ blob: Blob;+ savedAt: number;+ uploaded: boolean;+}++// happy-dom has no IndexedDB, so the recording store is an in-memory+// double. Everything above it — the snapshot hook, the panels — is real.+const { store, flagStatus } = vi.hoisted(() => ({+ store: {+ available: true,+ meta: null as FakeMeta | null,+ parts: [] as FakePart[],+ },+ flagStatus: { enabled: true as boolean, ready: true },+}));++vi.mock(+ "@/app/(no-navbar)/onboarding/steps/BrainDumpStep/recordingStore",+ () => ({+ isIndexedDBAvailable: () => store.available,+ getMeta: async () => store.meta,+ getParts: async (recordingId: string) =>+ store.parts.filter((part) => part.recordingId === recordingId),+ }),+);++vi.mock("@/services/feature-flags/use-get-flag", async (importOriginal) => {+ const actual =+ await importOriginal<+ typeof import("@/services/feature-flags/use-get-flag")+ >();+ return {+ ...actual,+ useFlagStatus: (flag: string) =>+ flag === "onboarding-brain-dump"+ ? flagStatus+ : actual.useFlagStatus(flag as never),+ };+});++const notFoundMock = vi.hoisted(() => vi.fn());+vi.mock("next/navigation", () => ({+ useRouter: () => ({+ push: vi.fn(),+ replace: vi.fn(),+ prefetch: vi.fn(),+ back: vi.fn(),+ forward: vi.fn(),+ refresh: vi.fn(),+ }),+ usePathname: () => "/dev/brain-dump-debug",+ useSearchParams: () => new URLSearchParams(),+ useParams: () => ({}),+ notFound: () => {+ notFoundMock();+ throw new Error("NEXT_NOT_FOUND");+ },+}));++import BrainDumpDebugPage from "../page";++let statusCalls: number;++function panel(title: string) {+ const section = screen.getByText(title).closest("section");+ if (!section) throw new Error(`no panel titled ${title}`);+ return within(section);+}++function renderPage() {+ try {+ render(<BrainDumpDebugPage />);+ } catch {+ // The mocked notFound() throws the way Next's does; the assertions+ // below are what the test actually cares about.+ }+}++beforeEach(() => {+ statusCalls = 0;+ store.available = true;+ store.meta = null;+ store.parts = [];+ flagStatus.enabled = true;+ flagStatus.ready = true;+ notFoundMock.mockClear();+ window.sessionStorage.clear();+ server.use(+ getGetBrainDumpStatusMockHandler200(() => {+ statusCalls += 1;+ return { status: "transcribing", input_mode: "voice", has_audio: true };+ }),+ );+});++afterEach(() => {+ vi.unstubAllEnvs();+});++describe("brain dump debug page — gating", () => {+ it("404s in production even with the flag on", () => {+ vi.stubEnv("NEXT_PUBLIC_APP_ENV", "prod");++ renderPage();++ expect(notFoundMock).toHaveBeenCalled();+ expect(screen.queryByText("Brain dump debug")).toBeNull();+ expect(statusCalls).toBe(0);+ });++ it("404s when the onboarding-brain-dump flag is off", () => {+ flagStatus.enabled = false;++ renderPage();++ expect(notFoundMock).toHaveBeenCalled();+ expect(screen.queryByText("Brain dump debug")).toBeNull();+ expect(statusCalls).toBe(0);+ });++ it("waits with skeletons instead of 404ing while the flag is unresolved", async () => {+ // Short-circuiting to notFound() before LaunchDarkly answers would 404+ // users who actually have the flag on.+ flagStatus.ready = false;+ flagStatus.enabled = false;++ const { container } = render(<BrainDumpDebugPage />);++ expect(notFoundMock).not.toHaveBeenCalled();+ expect(container.querySelectorAll(".animate-pulse")).toHaveLength(3);+ expect(screen.queryByText("Brain dump debug")).toBeNull();+ // The status query stays disabled until the page is known to be+ // available, so nothing is polled while the flag is in flight.+ expect(statusCalls).toBe(0);+ });++ it("renders every panel once the flag resolves on outside production", async () => {+ renderPage();++ expect(await screen.findByText("Brain dump debug")).toBeDefined();+ expect(screen.getByText("dev only")).toBeDefined();+ expect(screen.getByText("IndexedDB recording state")).toBeDefined();+ expect(screen.getByText("Upload queue")).toBeDefined();+ expect(screen.getByText("Server status")).toBeDefined();+ expect(screen.getByText("Transcript & extraction JSON")).toBeDefined();+ expect(screen.getByText("Timing waterfall")).toBeDefined();+ expect(notFoundMock).not.toHaveBeenCalled();+ });+});++describe("brain dump debug page — recording state", () => {+ it("shows the stored meta row, the parts table and the byte totals", async () => {+ store.meta = {+ recordingId: "rec-42",+ mimeType: "audio/webm;codecs=opus",+ startedAt: new Date(2024, 2, 1, 13, 5, 9).getTime(),+ durationSecs: 91.5,+ finalized: false,+ };+ store.parts = [+ {+ id: "rec-42:0",+ recordingId: "rec-42",+ partIndex: 0,+ blob: new Blob(["a".repeat(2048)]),+ savedAt: 1,+ uploaded: true,+ },+ {+ id: "rec-42:1",+ recordingId: "rec-42",+ partIndex: 1,+ blob: new Blob(["b".repeat(1024)]),+ savedAt: 2,+ uploaded: false,+ },+ ];++ renderPage();++ const recording = panel("IndexedDB recording state");+ expect(await recording.findByText("rec-42")).toBeDefined();+ expect(recording.getByText("audio/webm;codecs=opus")).toBeDefined();+ expect(recording.getByText("91.5 s")).toBeDefined();+ expect(recording.getByText(/^13.05.09$/)).toBeDefined();+ expect(recording.getByText("2 parts")).toBeDefined();+ expect(recording.getByText("3 KB total")).toBeDefined();++ const rows = recording.getAllByRole("row").slice(1);+ expect(rows).toHaveLength(2);+ expect(within(rows[0]).getByText("2 KB")).toBeDefined();+ expect(within(rows[0]).getByText("yes")).toBeDefined();+ expect(within(rows[1]).getByText("1 KB")).toBeDefined();+ expect(within(rows[1]).getByText("no")).toBeDefined();++ expect(recording.getByRole("button", { name: /Refresh/ })).toBeDefined();+ });++ it("explains an empty store instead of rendering an empty table", async () => {+ renderPage();++ const recording = panel("IndexedDB recording state");+ expect(await recording.findByText(/No meta row stored/)).toBeDefined();+ expect(recording.getByText("0 parts")).toBeDefined();+ expect(recording.getByText("0 B total")).toBeDefined();+ expect(recording.queryByRole("table")).toBeNull();+ });++ it("says so when the browser has no IndexedDB", async () => {+ store.available = false;++ renderPage();++ const recording = panel("IndexedDB recording state");+ expect(+ await recording.findByText("IndexedDB is not available in this browser."),+ ).toBeDefined();+ });+});++describe("brain dump debug page — upload queue", () => {+ it("counts the parts that have not been marked uploaded", async () => {+ store.meta = {+ recordingId: "rec-42",+ mimeType: "audio/webm",+ startedAt: 1,+ durationSecs: 60,+ finalized: false,+ };+ store.parts = [+ {+ id: "rec-42:0",+ recordingId: "rec-42",+ partIndex: 0,+ blob: new Blob(["a".repeat(100)]),+ savedAt: 1,+ uploaded: true,+ },+ {+ id: "rec-42:1",+ recordingId: "rec-42",+ partIndex: 1,+ blob: new Blob(["b".repeat(250)]),+ savedAt: 2,+ uploaded: false,+ },+ {+ id: "rec-42:2",+ recordingId: "rec-42",+ partIndex: 2,+ blob: new Blob(["c".repeat(250)]),+ savedAt: 3,+ uploaded: false,+ },+ ];++ renderPage();++ const queue = panel("Upload queue");+ expect(await queue.findByText("2 pending")).toBeDefined();+ // pending count, uploaded count, pending bytes+ expect(queue.getByText("2")).toBeDefined();+ expect(queue.getByText("1")).toBeDefined();+ expect(queue.getByText("500 B")).toBeDefined();+ });++ it("reports the queue as drained when every part is uploaded", async () => {+ store.meta = {+ recordingId: "rec-42",+ mimeType: "audio/webm",+ startedAt: 1,+ durationSecs: 60,+ finalized: true,+ };+ store.parts = [+ {+ id: "rec-42:0",+ recordingId: "rec-42",+ partIndex: 0,+ blob: new Blob(["a"]),+ savedAt: 1,+ uploaded: true,+ },+ ];++ renderPage();++ const queue = panel("Upload queue");+ expect(await queue.findByText("drained")).toBeDefined();+ expect(queue.getByText("0 B")).toBeDefined();+ });+});++describe("brain dump debug page — transcript", () => {+ it("keeps the 'not exposed by any endpoint' warning and offers no preview", async () => {+ renderPage();++ const transcript = panel("Transcript & extraction JSON");+ expect(+ await transcript.findByText("Not exposed by any current endpoint"),+ ).toBeDefined();+ expect(transcript.getByText(/None captured/)).toBeDefined();+ });+});diff --git a/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/__tests__/useRecordingSnapshot.test.ts b/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/__tests__/useRecordingSnapshot.test.tsnew file mode 100644index 0000000000..987975bb9a--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/__tests__/useRecordingSnapshot.test.ts@@ -0,0 +1,118 @@+import { act, renderHook } from "@testing-library/react";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";++// The poll interval and the manual refresh both read the same store, so+// they cannot be told apart through the page UI without waiting a whole+// second per assertion. This is the one piece of the debug page that has+// to be driven directly.+interface FakeMeta {+ recordingId: string;+ mimeType: string;+ startedAt: number;+ durationSecs: number;+ finalized: boolean;+}++const { store } = vi.hoisted(() => ({+ store: {+ meta: null as FakeMeta | null,+ reads: 0,+ },+}));++vi.mock(+ "@/app/(no-navbar)/onboarding/steps/BrainDumpStep/recordingStore",+ () => ({+ isIndexedDBAvailable: () => true,+ getMeta: async () => {+ store.reads += 1;+ return store.meta;+ },+ getParts: async () => [],+ }),+);++import { SNAPSHOT_POLL_MS } from "../helpers";+import { useRecordingSnapshot } from "../useRecordingSnapshot";++function meta(recordingId: string): FakeMeta {+ return {+ recordingId,+ mimeType: "audio/webm",+ startedAt: 1,+ durationSecs: 60,+ finalized: false,+ };+}++beforeEach(() => {+ store.meta = null;+ store.reads = 0;+ vi.useFakeTimers();+});++afterEach(() => {+ vi.useRealTimers();+});++describe("useRecordingSnapshot", () => {+ it("starts empty, then reads once on mount", async () => {+ store.meta = meta("rec-1");++ const { result } = renderHook(() => useRecordingSnapshot());++ expect(result.current.snapshot.meta).toBeNull();+ expect(result.current.snapshot.readAt).toBeNull();++ await act(async () => {+ await vi.advanceTimersByTimeAsync(0);+ });++ expect(store.reads).toBe(1);+ expect(result.current.snapshot.meta?.recordingId).toBe("rec-1");+ });++ it("keeps re-reading on the poll interval and stops on unmount", async () => {+ const { unmount } = renderHook(() => useRecordingSnapshot());++ await act(async () => {+ await vi.advanceTimersByTimeAsync(0);+ });+ expect(store.reads).toBe(1);++ await act(async () => {+ await vi.advanceTimersByTimeAsync(SNAPSHOT_POLL_MS * 3);+ });+ expect(store.reads).toBe(4);++ unmount();++ await act(async () => {+ await vi.advanceTimersByTimeAsync(SNAPSHOT_POLL_MS * 5);+ });+ // The interval must be torn down, or the page keeps hammering+ // IndexedDB after it is gone.+ expect(store.reads).toBe(4);+ });++ it("applies the store's current contents as soon as refresh resolves", async () => {+ store.meta = meta("rec-1");+ const { result } = renderHook(() => useRecordingSnapshot());++ await act(async () => {+ await vi.advanceTimersByTimeAsync(0);+ });+ expect(result.current.snapshot.meta?.recordingId).toBe("rec-1");++ store.meta = meta("rec-2");+ const readsBeforeRefresh = store.reads;++ // No timers are advanced here, so the poll cannot be what updates it.+ await act(async () => {+ await result.current.refresh();+ });++ expect(store.reads).toBe(readsBeforeRefresh + 1);+ expect(result.current.snapshot.meta?.recordingId).toBe("rec-2");+ });+});diff --git a/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/__tests__/waterfall.test.ts b/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/__tests__/waterfall.test.tsnew file mode 100644index 0000000000..e7bcb1bff4--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/__tests__/waterfall.test.ts@@ -0,0 +1,227 @@+import { describe, expect, it } from "vitest";+import {+ EMPTY_SNAPSHOT,+ EXTRACT_BUDGET_MS,+ TRANSCRIBE_BUDGET_MS,+ type DebugPart,+ type RecordingSnapshot,+} from "../helpers";+import { buildWaterfall, isOverBudget, type StatusSeenAt } from "../waterfall";++function snapshotWith(+ parts: DebugPart[],+ meta: RecordingSnapshot["meta"] = null,+): RecordingSnapshot {+ return {+ ...EMPTY_SNAPSHOT,+ meta,+ parts,+ totalBytes: parts.reduce((total, part) => total + part.bytes, 0),+ pendingUploads: parts.filter((part) => !part.uploaded).length,+ };+}++function part(partIndex: number, savedAt: number, uploaded = false): DebugPart {+ return { partIndex, bytes: 10, savedAt, uploaded };+}++function meta(startedAt: number): RecordingSnapshot["meta"] {+ return {+ recordingId: "rec-1",+ mimeType: "audio/webm",+ startedAt,+ durationSecs: 60,+ finalized: false,+ };+}++function build(args: {+ snapshot?: RecordingSnapshot;+ seenAt?: StatusSeenAt;+ introPath?: "A" | "B" | null;+}) {+ const stages = buildWaterfall({+ snapshot: args.snapshot ?? EMPTY_SNAPSHOT,+ seenAt: args.seenAt ?? {},+ introPath: args.introPath ?? null,+ });+ return {+ stages,+ byId: (id: string) => {+ const stage = stages.find((candidate) => candidate.id === id);+ if (!stage) throw new Error(`no stage ${id}`);+ return stage;+ },+ };+}++describe("buildWaterfall", () => {+ it("returns the five pipeline stages in pipeline order", () => {+ const { stages } = build({});++ expect(stages.map((stage) => stage.id)).toEqual([+ "record",+ "upload",+ "transcribe",+ "extract",+ "intro",+ ]);+ expect(stages.map((stage) => stage.label)).toEqual([+ "Record",+ "Upload",+ "Transcribe",+ "Extract",+ "Intro message",+ ]);+ });++ it("only holds transcribe and extract to a budget", () => {+ const { byId } = build({});++ expect(byId("transcribe").budgetMs).toBe(TRANSCRIBE_BUDGET_MS);+ expect(byId("extract").budgetMs).toBe(EXTRACT_BUDGET_MS);+ expect(byId("record").budgetMs).toBeNull();+ expect(byId("upload").budgetMs).toBeNull();+ expect(byId("intro").budgetMs).toBeNull();+ });+});++describe("record stage", () => {+ it("measures meta.startedAt to the last part's savedAt", () => {+ const { byId } = build({+ snapshot: snapshotWith(+ [part(0, 1500), part(1, 2500), part(2, 4000)],+ meta(1000),+ ),+ });++ expect(byId("record").durationMs).toBe(3000);+ expect(byId("record").source).toContain("Measured from IndexedDB");+ });++ it("falls back to the first part when there is no meta row", () => {+ const { byId } = build({+ snapshot: snapshotWith([part(0, 1500), part(1, 4000)]),+ });++ expect(byId("record").durationMs).toBe(2500);+ });++ it("is unmeasured when nothing is stored", () => {+ const { byId } = build({ snapshot: snapshotWith([], meta(1000)) });++ expect(byId("record").durationMs).toBeNull();+ expect(byId("record").source).toContain("Unmeasured");+ });+});++describe("upload stage", () => {+ it("stays unmeasured and reports how much of the queue is undrained", () => {+ const { byId } = build({+ snapshot: snapshotWith([part(0, 1, true), part(1, 2), part(2, 3)]),+ });++ expect(byId("upload").durationMs).toBeNull();+ expect(byId("upload").source).toContain("2 of 3 part(s)");+ });+});++describe("transcribe stage", () => {+ it("spans the first upload/transcribing sighting to the first transcribed-or-later one", () => {+ const { byId } = build({+ seenAt: {+ recording_uploaded: 1000,+ transcribing: 1200,+ transcribed: 4000,+ completed: 9000,+ },+ });++ expect(byId("transcribe").durationMs).toBe(3000);+ expect(byId("transcribe").source).toContain(+ "Measured from polled status transitions",+ );+ });++ it("uses the earliest sighting on each side when a status was missed", () => {+ // The poll never caught `recording_uploaded` or `transcribed`; the+ // remaining statuses still bracket the phase.+ const { byId } = build({+ seenAt: { transcribing: 2000, completed: 6000, extracting: 5000 },+ });++ expect(byId("transcribe").durationMs).toBe(3000);+ });++ it("is unmeasured when only one side was observed", () => {+ const { byId } = build({ seenAt: { transcribing: 2000 } });++ expect(byId("transcribe").durationMs).toBeNull();+ expect(byId("transcribe").source).toContain("Unmeasured");+ expect(byId("transcribe").source).toContain(+ "Keep it open across a finalize",+ );+ });+});++describe("extract stage", () => {+ it("spans the first transcribed/extracting sighting to completed", () => {+ const { byId } = build({+ seenAt: { transcribed: 4000, extracting: 4500, completed: 9000 },+ });++ expect(byId("extract").durationMs).toBe(5000);+ });++ it("is unmeasured when the run never reached completed", () => {+ const { byId } = build({ seenAt: { transcribed: 4000, failed: 5000 } });++ expect(byId("extract").durationMs).toBeNull();+ });++ it("refuses a negative span when timestamps arrive out of order", () => {+ // A clock jump backwards must not be reported as a duration.+ const { byId } = build({ seenAt: { transcribed: 9000, completed: 4000 } });++ expect(byId("extract").durationMs).toBeNull();+ });++ it("reports a zero-length span when both statuses land on the same poll", () => {+ const { byId } = build({ seenAt: { transcribed: 4000, completed: 4000 } });++ expect(byId("extract").durationMs).toBe(0);+ });+});++describe("intro stage", () => {+ it("names the pending handoff path when one is queued", () => {+ const { byId } = build({ introPath: "B" });++ expect(byId("intro").durationMs).toBeNull();+ expect(byId("intro").source).toContain("Pending handoff path: B");+ });++ it("points at the sessionStorage key when there is no handoff", () => {+ const { byId } = build({ introPath: null });++ expect(byId("intro").source).toContain("autogpt:onboarding-intro-path");+ });+});++describe("isOverBudget", () => {+ it("is true only when a measured duration exceeds a set budget", () => {+ const stage = {+ id: "transcribe",+ label: "Transcribe",+ source: "",+ budgetMs: 5000,+ durationMs: 5001,+ };++ expect(isOverBudget(stage)).toBe(true);+ expect(isOverBudget({ ...stage, durationMs: 5000 })).toBe(false);+ expect(isOverBudget({ ...stage, durationMs: 4999 })).toBe(false);+ expect(isOverBudget({ ...stage, durationMs: null })).toBe(false);+ expect(isOverBudget({ ...stage, budgetMs: null })).toBe(false);+ });+});diff --git a/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/components/DebugPanel.tsx b/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/components/DebugPanel.tsxnew file mode 100644index 0000000000..7b615b2ebe--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/components/DebugPanel.tsx@@ -0,0 +1,70 @@+import { Icon } from "@/components/atoms/Icon/Icon";+import { Text } from "@/components/atoms/Text/Text";+import type { IconSvgElement } from "@hugeicons/react";+import type { ReactNode } from "react";++interface Props {+ title: string;+ description?: string;+ icon: IconSvgElement;+ action?: ReactNode;+ children: ReactNode;+}++export function DebugPanel({+ title,+ description,+ icon,+ action,+ children,+}: Props) {+ return (+ <section className="rounded-2xlarge border border-zinc-200 bg-white p-8">+ <div className="mb-6 flex flex-wrap items-start justify-between gap-4">+ <div className="flex items-start gap-3">+ <Icon+ icon={icon}+ size={22}+ className="mt-0.5 shrink-0 text-zinc-500"+ />+ <div className="flex flex-col gap-1">+ <Text variant="h5">{title}</Text>+ {description ? (+ <Text variant="small" className="max-w-prose text-zinc-500">+ {description}+ </Text>+ ) : null}+ </div>+ </div>+ {action}+ </div>+ {children}+ </section>+ );+}++interface FieldProps {+ label: string;+ value: string;+}++export function DebugField({ label, value }: FieldProps) {+ return (+ <div className="flex flex-col gap-1 rounded-large bg-zinc-50 px-4 py-3">+ <Text variant="label" className="text-zinc-500">+ {label}+ </Text>+ <Text variant="body" unmask={false} className="break-all font-mono">+ {value}+ </Text>+ </div>+ );+}++export function DebugNote({ children }: { children: ReactNode }) {+ return (+ <Text variant="small" className="max-w-prose text-zinc-500">+ {children}+ </Text>+ );+}diff --git a/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/components/RecordingStatePanel.tsx b/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/components/RecordingStatePanel.tsxnew file mode 100644index 0000000000..9c9e100925--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/components/RecordingStatePanel.tsx@@ -0,0 +1,113 @@+import { Badge } from "@/components/atoms/Badge/Badge";+import { Button } from "@/components/atoms/Button/Button";+import { Icon } from "@/components/atoms/Icon/Icon";+import { Text } from "@/components/atoms/Text/Text";+import {+ ArrowReloadHorizontalIcon,+ DatabaseIcon,+} from "@hugeicons/core-free-icons";+import {+ formatBytes,+ formatClock,+ formatSeconds,+ formatValue,+ type RecordingSnapshot,+} from "../helpers";+import { DebugField, DebugNote, DebugPanel } from "./DebugPanel";++interface Props {+ snapshot: RecordingSnapshot;+ onRefresh: () => void;+}++export function RecordingStatePanel({ snapshot, onRefresh }: Props) {+ const { meta, parts } = snapshot;++ return (+ <DebugPanel+ title="IndexedDB recording state"+ description="Read live from the autogpt-onboarding-brain-dump database every second."+ icon={DatabaseIcon}+ action={+ <Button+ variant="outline"+ size="small"+ onClick={onRefresh}+ leftIcon={<Icon icon={ArrowReloadHorizontalIcon} size={16} />}+ >+ Refresh+ </Button>+ }+ >+ {!snapshot.supported ? (+ <DebugNote>IndexedDB is not available in this browser.</DebugNote>+ ) : null}+ {snapshot.error ? (+ <DebugNote>Read failed: {snapshot.error}</DebugNote>+ ) : null}++ {meta ? (+ <div className="grid grid-cols-1 gap-3 sm:grid-cols-2 lg:grid-cols-3">+ <DebugField label="recordingId" value={meta.recordingId} />+ <DebugField label="mimeType" value={meta.mimeType} />+ <DebugField label="startedAt" value={formatClock(meta.startedAt)} />+ <DebugField+ label="durationSecs"+ value={formatSeconds(meta.durationSecs)}+ />+ <DebugField label="finalized" value={formatValue(meta.finalized)} />+ <DebugField+ label="parts read at"+ value={formatClock(snapshot.readAt)}+ />+ </div>+ ) : (+ <DebugNote>+ No meta row stored — nothing has been recorded in this browser, or the+ dump completed and the store was cleared.+ </DebugNote>+ )}++ <div className="mt-6 flex flex-wrap items-center gap-3">+ <Badge variant="info">{parts.length} parts</Badge>+ <Badge variant="info">{formatBytes(snapshot.totalBytes)} total</Badge>+ </div>++ {parts.length > 0 ? (+ <div className="mt-4 overflow-hidden rounded-large border border-zinc-200">+ <table className="w-full border-collapse text-left">+ <thead className="bg-zinc-50">+ <tr>+ {["partIndex", "bytes", "savedAt", "uploaded"].map((column) => (+ <th key={column} className="px-4 py-2">+ <Text variant="label" className="text-zinc-500">+ {column}+ </Text>+ </th>+ ))}+ </tr>+ </thead>+ <tbody>+ {parts.map((part) => (+ <tr key={part.partIndex} className="border-t border-zinc-100">+ <td className="px-4 py-2 font-mono text-sm">+ {part.partIndex}+ </td>+ <td className="px-4 py-2 font-mono text-sm">+ {formatBytes(part.bytes)}+ </td>+ <td className="px-4 py-2 font-mono text-sm">+ {formatClock(part.savedAt)}+ </td>+ <td className="px-4 py-2 font-mono text-sm">+ {formatValue(part.uploaded)}+ </td>+ </tr>+ ))}+ </tbody>+ </table>+ </div>+ ) : null}+ </DebugPanel>+ );+}diff --git a/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/components/ServerStatusPanel.tsx b/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/components/ServerStatusPanel.tsxnew file mode 100644index 0000000000..14c24423a4--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/components/ServerStatusPanel.tsx@@ -0,0 +1,121 @@+import type { DumpStatusResponse } from "@/app/api/__generated__/models/dumpStatusResponse";+import type { FinalizeResponse } from "@/app/api/__generated__/models/finalizeResponse";+import { Badge } from "@/components/atoms/Badge/Badge";+import { Button } from "@/components/atoms/Button/Button";+import { Icon } from "@/components/atoms/Icon/Icon";+import { Link } from "@/components/atoms/Link/Link";+import { AudioWaveformIcon, Download01Icon } from "@hugeicons/core-free-icons";+import {+ formatMs,+ formatValue,+ recordingDownloadHref,+ STATUS_POLL_MS,+} from "../helpers";+import { DebugField, DebugNote, DebugPanel } from "./DebugPanel";++interface Props {+ dump: DumpStatusResponse | null;+ isLoading: boolean;+ isError: boolean;+ finalizeResponse: FinalizeResponse | null;+ finalizeRoundTripMs: number | null;+ finalizeError: string | null;+ canFinalize: boolean;+ isFinalizing: boolean;+ onFinalize: () => void;+}++export function ServerStatusPanel({+ dump,+ isLoading,+ isError,+ finalizeResponse,+ finalizeRoundTripMs,+ finalizeError,+ canFinalize,+ isFinalizing,+ onFinalize,+}: Props) {+ return (+ <DebugPanel+ title="Server status"+ description={`GET /onboarding/brain-dump/status, polled every ${STATUS_POLL_MS}ms.`}+ icon={AudioWaveformIcon}+ action={+ <Link href={recordingDownloadHref()} isExternal variant="secondary">+ <span className="inline-flex items-center gap-2">+ <Icon icon={Download01Icon} size={16} />+ Download server-side recording+ </span>+ </Link>+ }+ >+ {isError ? (+ <DebugNote>+ Status request failed — the endpoint 404s when ONBOARDING_BRAIN_DUMP+ is off for this user, and 401s when signed out.+ </DebugNote>+ ) : null}+ {isLoading && !dump ? <DebugNote>Loading status…</DebugNote> : null}++ <div className="grid grid-cols-1 gap-3 sm:grid-cols-2 lg:grid-cols-4">+ <DebugField label="status" value={formatValue(dump?.status)} />+ <DebugField label="input_mode" value={formatValue(dump?.input_mode)} />+ <DebugField label="error_code" value={formatValue(dump?.error_code)} />+ <DebugField label="has_audio" value={formatValue(dump?.has_audio)} />+ </div>++ <div className="mt-8 flex flex-wrap items-center justify-between gap-4">+ <div className="flex items-center gap-3">+ <Badge variant="info">last finalize response</Badge>+ {finalizeRoundTripMs !== null ? (+ <Badge variant="info">+ round-trip {formatMs(finalizeRoundTripMs)}+ </Badge>+ ) : null}+ </div>+ <Button+ variant="outline"+ size="small"+ onClick={onFinalize}+ disabled={!canFinalize}+ loading={isFinalizing}+ >+ Run finalize on the stored recording+ </Button>+ </div>++ {finalizeError ? (+ <div className="mt-4">+ <DebugNote>Finalize failed: {finalizeError}</DebugNote>+ </div>+ ) : null}++ {finalizeResponse ? (+ <div className="mt-4 grid grid-cols-1 gap-3 sm:grid-cols-3">+ <DebugField+ label="status"+ value={formatValue(finalizeResponse.status)}+ />+ <DebugField+ label="input_mode"+ value={formatValue(finalizeResponse.input_mode)}+ />+ <DebugField+ label="error_code"+ value={formatValue(finalizeResponse.error_code)}+ />+ </div>+ ) : (+ <div className="mt-4">+ <DebugNote>+ No finalize response captured. Nothing persists the response from+ the real onboarding run, so this page can only show one it issued+ itself. Finalize is idempotent per recording_id and consumes the+ server-side part buffer.+ </DebugNote>+ </div>+ )}+ </DebugPanel>+ );+}diff --git a/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/components/TimingPanel.tsx b/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/components/TimingPanel.tsxnew file mode 100644index 0000000000..b86bc48932--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/components/TimingPanel.tsx@@ -0,0 +1,83 @@+import { Badge } from "@/components/atoms/Badge/Badge";+import { Text } from "@/components/atoms/Text/Text";+import { Timer01Icon } from "@hugeicons/core-free-icons";+import {+ BUDGET_DUMP_LENGTH,+ EXTRACT_BUDGET_MS,+ formatMs,+ TRANSCRIBE_BUDGET_MS,+} from "../helpers";+import { isOverBudget, type WaterfallStage } from "../waterfall";+import { DebugNote, DebugPanel } from "./DebugPanel";++interface Props {+ stages: WaterfallStage[];+ finalizeRoundTripMs: number | null;+}++export function TimingPanel({ stages, finalizeRoundTripMs }: Props) {+ return (+ <DebugPanel+ title="Timing waterfall"+ description={`Budget for a ${BUDGET_DUMP_LENGTH}: transcription within ${formatMs(TRANSCRIBE_BUDGET_MS)} of pressing Done, extraction within ${formatMs(EXTRACT_BUDGET_MS)} after that.`}+ icon={Timer01Icon}+ action={+ finalizeRoundTripMs !== null ? (+ <Badge variant="info">+ finalize round-trip {formatMs(finalizeRoundTripMs)}+ </Badge>+ ) : undefined+ }+ >+ <div className="flex flex-col gap-3">+ {stages.map((stage) => (+ <StageRow key={stage.id} stage={stage} />+ ))}+ </div>+ <div className="mt-6">+ <DebugNote>+ Finalize is a single synchronous call that assembles, scans, stores,+ transcribes and extracts, so the round-trip above is the whole+ post-Done budget. Per-phase numbers come from polled status+ transitions and are only available while this page is open across a+ run.+ </DebugNote>+ </div>+ </DebugPanel>+ );+}++function StageRow({ stage }: { stage: WaterfallStage }) {+ const measured = stage.durationMs !== null;+ const exceeded = isOverBudget(stage);++ return (+ <div className="flex flex-col gap-2 rounded-large border border-zinc-200 px-4 py-3 sm:flex-row sm:items-center sm:justify-between">+ <div className="flex flex-col gap-1">+ <div className="flex items-center gap-3">+ <Text variant="body-medium">{stage.label}</Text>+ {measured ? (+ <Badge variant={exceeded ? "error" : "success"}>+ {exceeded ? "over budget" : "measured"}+ </Badge>+ ) : (+ <Badge variant="info">unmeasured</Badge>+ )}+ </div>+ <Text variant="small" className="max-w-prose text-zinc-500">+ {stage.source}+ </Text>+ </div>+ <div className="flex shrink-0 flex-col sm:items-end">+ <Text variant="large-semibold" className="font-mono">+ {formatMs(stage.durationMs)}+ </Text>+ <Text variant="small" className="text-zinc-500">+ {stage.budgetMs === null+ ? "no budget"+ : `budget ${formatMs(stage.budgetMs)}`}+ </Text>+ </div>+ </div>+ );+}diff --git a/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/components/TranscriptPanel.tsx b/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/components/TranscriptPanel.tsxnew file mode 100644index 0000000000..ccb893b24e--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/components/TranscriptPanel.tsx@@ -0,0 +1,71 @@+import type { FinalizeResponse } from "@/app/api/__generated__/models/finalizeResponse";+import { Text } from "@/components/atoms/Text/Text";+import {+ Alert,+ AlertDescription,+ AlertTitle,+} from "@/components/molecules/Alert/Alert";+import { File02Icon } from "@hugeicons/core-free-icons";+import { TRANSCRIPT_PREVIEW_CHARS } from "../helpers";+import { DebugNote, DebugPanel } from "./DebugPanel";++interface Props {+ finalizeResponse: FinalizeResponse | null;+}++export function TranscriptPanel({ finalizeResponse }: Props) {+ const preview = finalizeResponse?.transcript_preview ?? null;++ return (+ <DebugPanel+ title="Transcript & extraction JSON"+ description="What the pipeline produced from the dump."+ icon={File02Icon}+ >+ <Alert variant="warning">+ <AlertTitle>Not exposed by any current endpoint</AlertTitle>+ <AlertDescription>+ <Text variant="small" className="mb-2 text-zinc-700">+ The full transcript and the extracted CoPilotUnderstanding.data are+ written server-side and never returned to the browser. Verified+ against the whole generated client: the brain-dump tag has only+ parts, finalize, status, recording and discard;{" "}+ <span className="font-mono">GET /onboarding/brain-dump/status</span>{" "}+ returns status, input_mode, error_code and has_audio only, and+ UnderstandingUpdatedResponse is a copilot tool-result shape, not a+ readable endpoint.+ </Text>+ <Text variant="small" className="text-zinc-700">+ Showing them here would need a new backend route — e.g.{" "}+ <span className="font-mono">+ GET /onboarding/brain-dump/transcript+ </span>{" "}+ returning BrainDump.transcript plus transcriptLang, and a read+ endpoint for the user's CoPilotUnderstanding row. Neither was+ added.+ </Text>+ </AlertDescription>+ </Alert>++ <div className="mt-6">+ <Text variant="label" className="text-zinc-500">+ transcript_preview (from finalize)+ </Text>+ {preview ? (+ <pre className="mt-2 whitespace-pre-wrap rounded-large bg-zinc-50 p-4 font-mono text-sm text-zinc-800">+ {preview}+ </pre>+ ) : (+ <div className="mt-2">+ <DebugNote>+ None captured. Run finalize from the server-status panel above to+ capture one — the server truncates it to{" "}+ {TRANSCRIPT_PREVIEW_CHARS} characters, so even this is not the+ full transcript.+ </DebugNote>+ </div>+ )}+ </div>+ </DebugPanel>+ );+}diff --git a/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/components/UploadQueuePanel.tsx b/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/components/UploadQueuePanel.tsxnew file mode 100644index 0000000000..d2f007c48e--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/components/UploadQueuePanel.tsx@@ -0,0 +1,44 @@+import { Badge } from "@/components/atoms/Badge/Badge";+import { CloudUploadIcon } from "@hugeicons/core-free-icons";+import { formatBytes, type RecordingSnapshot } from "../helpers";+import { DebugField, DebugNote, DebugPanel } from "./DebugPanel";++interface Props {+ snapshot: RecordingSnapshot;+}++export function UploadQueuePanel({ snapshot }: Props) {+ const pending = snapshot.pendingUploads;+ const pendingBytes = snapshot.parts+ .filter((part) => !part.uploaded)+ .reduce((total, part) => total + part.bytes, 0);++ return (+ <DebugPanel+ title="Upload queue"+ description="Derived from the parts table — a part is pending until markPartUploaded flips its flag."+ icon={CloudUploadIcon}+ action={+ <Badge variant={pending === 0 ? "success" : "error"}>+ {pending === 0 ? "drained" : `${pending} pending`}+ </Badge>+ }+ >+ <div className="grid grid-cols-1 gap-3 sm:grid-cols-3">+ <DebugField label="parts pending upload" value={String(pending)} />+ <DebugField+ label="parts uploaded"+ value={String(snapshot.parts.length - pending)}+ />+ <DebugField label="pending bytes" value={formatBytes(pendingBytes)} />+ </div>+ <div className="mt-4">+ <DebugNote>+ The in-memory queue inside useUploadQueue is not observable from here;+ this panel reflects the durable IndexedDB flags, which is what+ survives a refresh.+ </DebugNote>+ </div>+ </DebugPanel>+ );+}diff --git a/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/helpers.ts b/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/helpers.tsnew file mode 100644index 0000000000..2ddaa06849--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/helpers.ts@@ -0,0 +1,138 @@+import {+ getMeta,+ getParts,+ isIndexedDBAvailable,+ type RecordingMeta,+ type RecordingPart,+} from "@/app/(no-navbar)/onboarding/steps/BrainDumpStep/recordingStore";+import { getDownloadBrainDumpRecordingUrl } from "@/app/api/__generated__/endpoints/brain-dump/brain-dump";+import { AppEnv, environment } from "@/services/environment";++// Processing budget the brain-dump feature is held to for a 60-90s dump.+export const TRANSCRIBE_BUDGET_MS = 5000;+export const EXTRACT_BUDGET_MS = 5000;+export const BUDGET_DUMP_LENGTH = "60-90s dump";++export const STATUS_POLL_MS = 1000;+export const SNAPSHOT_POLL_MS = 1000;++// Server truncates the finalize preview; the full transcript never leaves+// the backend.+export const TRANSCRIPT_PREVIEW_CHARS = 280;++export interface DebugPart {+ partIndex: number;+ bytes: number;+ savedAt: number;+ uploaded: boolean;+}++export interface RecordingSnapshot {+ supported: boolean;+ meta: RecordingMeta | null;+ parts: DebugPart[];+ totalBytes: number;+ pendingUploads: number;+ readAt: number | null;+ error: string | null;+}++export const EMPTY_SNAPSHOT: RecordingSnapshot = {+ supported: true,+ meta: null,+ parts: [],+ totalBytes: 0,+ pendingUploads: 0,+ readAt: null,+ error: null,+};++export function isProductionEnvironment() {+ return environment.getAppEnv() === AppEnv.PROD;+}++export function recordingDownloadHref() {+ return `/api/proxy${getDownloadBrainDumpRecordingUrl()}`;+}++export function describeError(error: unknown) {+ if (error instanceof Error) return error.message;+ return String(error);+}++export async function readRecordingSnapshot(): Promise<RecordingSnapshot> {+ if (!isIndexedDBAvailable()) {+ return { ...EMPTY_SNAPSHOT, supported: false, readAt: Date.now() };+ }+ try {+ const meta = await getMeta();+ if (!meta) return { ...EMPTY_SNAPSHOT, readAt: Date.now() };++ const stored = await getParts(meta.recordingId);+ const parts = stored.map(toDebugPart);+ return {+ supported: true,+ meta,+ parts,+ totalBytes: parts.reduce((total, part) => total + part.bytes, 0),+ pendingUploads: parts.filter((part) => !part.uploaded).length,+ readAt: Date.now(),+ error: null,+ };+ } catch (error) {+ return {+ ...EMPTY_SNAPSHOT,+ readAt: Date.now(),+ error: describeError(error),+ };+ }+}++function toDebugPart(part: RecordingPart): DebugPart {+ return {+ partIndex: part.partIndex,+ bytes: part.blob.size,+ savedAt: part.savedAt,+ uploaded: part.uploaded,+ };+}++const BYTE_UNITS = ["B", "KB", "MB", "GB"];++export function formatBytes(bytes: number) {+ let value = bytes;+ let unit = 0;+ while (value >= 1024 && unit < BYTE_UNITS.length - 1) {+ value = value / 1024;+ unit += 1;+ }+ const rounded = unit === 0 ? value : Number(value.toFixed(1));+ return `${rounded} ${BYTE_UNITS[unit]}`;+}++export function formatClock(epochMs: number | null | undefined) {+ if (epochMs === null || epochMs === undefined) return "—";+ return new Date(epochMs).toLocaleTimeString(undefined, {+ hour12: false,+ hour: "2-digit",+ minute: "2-digit",+ second: "2-digit",+ });+}++export function formatMs(durationMs: number | null) {+ if (durationMs === null) return "—";+ if (durationMs < 1000) return `${Math.round(durationMs)} ms`;+ return `${(durationMs / 1000).toFixed(2)} s`;+}++export function formatSeconds(seconds: number | null | undefined) {+ if (seconds === null || seconds === undefined) return "—";+ return `${seconds.toFixed(1)} s`;+}++export function formatValue(value: string | boolean | null | undefined) {+ if (value === null || value === undefined) return "—";+ if (typeof value === "boolean") return value ? "yes" : "no";+ return value === "" ? "—" : value;+}diff --git a/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/page.tsx b/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/page.tsxnew file mode 100644index 0000000000..d089470dc7--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/page.tsx@@ -0,0 +1,73 @@+"use client";++import { Badge } from "@/components/atoms/Badge/Badge";+import { Skeleton } from "@/components/atoms/Skeleton/Skeleton";+import { Text } from "@/components/atoms/Text/Text";+import { Flag, useFlagStatus } from "@/services/feature-flags/use-get-flag";+import { notFound } from "next/navigation";+import { RecordingStatePanel } from "./components/RecordingStatePanel";+import { ServerStatusPanel } from "./components/ServerStatusPanel";+import { TimingPanel } from "./components/TimingPanel";+import { TranscriptPanel } from "./components/TranscriptPanel";+import { UploadQueuePanel } from "./components/UploadQueuePanel";+import { isProductionEnvironment } from "./helpers";+import { useBrainDumpDebugPage } from "./useBrainDumpDebugPage";++const MAIN_CLASS = "container flex min-h-screen flex-col gap-6 pb-20 pt-16";++export default function BrainDumpDebugPage() {+ const isProduction = isProductionEnvironment();+ const { enabled, ready } = useFlagStatus(Flag.ONBOARDING_BRAIN_DUMP);+ const isAvailable = !isProduction && ready && Boolean(enabled);+ const { snapshot, refreshSnapshot, timeline, finalize, introPath, stages } =+ useBrainDumpDebugPage({ enabled: isAvailable });++ if (isProduction) notFound();++ if (!ready) {+ return (+ <main className={MAIN_CLASS}>+ {[0, 1, 2].map((index) => (+ <Skeleton key={index} className="h-48 w-full rounded-2xlarge" />+ ))}+ </main>+ );+ }++ if (!enabled) notFound();++ return (+ <main className={MAIN_CLASS}>+ <header className="flex flex-col gap-2">+ <div className="flex flex-wrap items-center gap-3">+ <Text variant="h3">Brain dump debug</Text>+ <Badge variant="error">dev only</Badge>+ {introPath ? (+ <Badge variant="info">intro path {introPath}</Badge>+ ) : null}+ </div>+ <Text variant="body" className="max-w-prose text-zinc-600">+ Local recording state, the upload queue, the server pipeline and the+ processing budget for the onboarding brain dump. Not reachable in+ production and gated on the onboarding-brain-dump flag.+ </Text>+ </header>++ <RecordingStatePanel snapshot={snapshot} onRefresh={refreshSnapshot} />+ <UploadQueuePanel snapshot={snapshot} />+ <ServerStatusPanel+ dump={timeline.dump}+ isLoading={timeline.isLoading}+ isError={timeline.isError}+ finalizeResponse={finalize.response}+ finalizeRoundTripMs={finalize.roundTripMs}+ finalizeError={finalize.errorMessage}+ canFinalize={finalize.canRun}+ isFinalizing={finalize.isRunning}+ onFinalize={finalize.run}+ />+ <TranscriptPanel finalizeResponse={finalize.response} />+ <TimingPanel stages={stages} finalizeRoundTripMs={finalize.roundTripMs} />+ </main>+ );+}diff --git a/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/useBrainDumpDebugPage.ts b/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/useBrainDumpDebugPage.tsnew file mode 100644index 0000000000..3360bb5379--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/useBrainDumpDebugPage.ts@@ -0,0 +1,33 @@+import {+ peekIntroPath,+ type IntroPath,+} from "@/services/onboarding/brain-dump-handoff";+import { useEffect, useState } from "react";+import { useDebugFinalize } from "./useDebugFinalize";+import { useRecordingSnapshot } from "./useRecordingSnapshot";+import { useStatusTimeline } from "./useStatusTimeline";+import { buildWaterfall } from "./waterfall";++interface Args {+ enabled: boolean;+}++export function useBrainDumpDebugPage({ enabled }: Args) {+ const { snapshot, refresh } = useRecordingSnapshot();+ const timeline = useStatusTimeline({ enabled });+ const finalize = useDebugFinalize(snapshot);++ const [introPath, setIntroPath] = useState<IntroPath | null>(null);+ useEffect(() => {+ setIntroPath(peekIntroPath());+ }, []);++ return {+ snapshot,+ refreshSnapshot: refresh,+ timeline,+ finalize,+ introPath,+ stages: buildWaterfall({ snapshot, seenAt: timeline.seenAt, introPath }),+ };+}diff --git a/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/useDebugFinalize.ts b/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/useDebugFinalize.tsnew file mode 100644index 0000000000..8e5f4aaa00--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/useDebugFinalize.ts@@ -0,0 +1,47 @@+import { useFinalizeBrainDump } from "@/app/api/__generated__/endpoints/brain-dump/brain-dump";+import type { FinalizeResponse } from "@/app/api/__generated__/models/finalizeResponse";+import { useState } from "react";+import { describeError, type RecordingSnapshot } from "./helpers";++// Nothing persists the finalize response from the real onboarding flow, so+// the only way this page can show one is to issue its own call.+export function useDebugFinalize(snapshot: RecordingSnapshot) {+ const [response, setResponse] = useState<FinalizeResponse | null>(null);+ const [roundTripMs, setRoundTripMs] = useState<number | null>(null);+ const [errorMessage, setErrorMessage] = useState<string | null>(null);+ const { mutateAsync, isPending } = useFinalizeBrainDump();++ const meta = snapshot.meta;++ async function run() {+ if (!meta) return;+ setErrorMessage(null);+ const startedAt = performance.now();+ try {+ const result = await mutateAsync({+ data: {+ recording_id: meta.recordingId,+ input_mode: "voice",+ duration_secs: meta.durationSecs,+ mime_type: meta.mimeType,+ },+ });+ setRoundTripMs(performance.now() - startedAt);+ setResponse(result.status === 200 ? result.data : null);+ } catch (error) {+ setRoundTripMs(performance.now() - startedAt);+ setResponse(null);+ setErrorMessage(describeError(error));+ }+ }++ return {+ run,+ response,+ roundTripMs,+ errorMessage,+ isRunning: isPending,+ canRun: meta !== null && !isPending,+ recordingId: meta?.recordingId ?? null,+ };+}diff --git a/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/useRecordingSnapshot.ts b/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/useRecordingSnapshot.tsnew file mode 100644index 0000000000..bdcb366353--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/useRecordingSnapshot.ts@@ -0,0 +1,36 @@+import { useEffect, useState } from "react";+import {+ EMPTY_SNAPSHOT,+ readRecordingSnapshot,+ SNAPSHOT_POLL_MS,+ type RecordingSnapshot,+} from "./helpers";++export function useRecordingSnapshot() {+ const [snapshot, setSnapshot] = useState<RecordingSnapshot>(EMPTY_SNAPSHOT);++ useEffect(() => {+ let isActive = true;++ async function poll() {+ const next = await readRecordingSnapshot();+ if (isActive) setSnapshot(next);+ }++ void poll();+ const timer = setInterval(function tick() {+ void poll();+ }, SNAPSHOT_POLL_MS);++ return () => {+ isActive = false;+ clearInterval(timer);+ };+ }, []);++ async function refresh() {+ setSnapshot(await readRecordingSnapshot());+ }++ return { snapshot, refresh };+}diff --git a/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/useStatusTimeline.ts b/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/useStatusTimeline.tsnew file mode 100644index 0000000000..66cd3a58f0--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/useStatusTimeline.ts@@ -0,0 +1,30 @@+import { useGetBrainDumpStatus } from "@/app/api/__generated__/endpoints/brain-dump/brain-dump";+import { useEffect, useState } from "react";+import { STATUS_POLL_MS } from "./helpers";+import type { StatusSeenAt } from "./waterfall";++interface Args {+ enabled: boolean;+}++export function useStatusTimeline({ enabled }: Args) {+ const { data, isLoading, isError, error, refetch } = useGetBrainDumpStatus({+ query: { enabled, refetchInterval: STATUS_POLL_MS },+ });++ const [seenAt, setSeenAt] = useState<StatusSeenAt>({});++ const dump = data && data.status === 200 ? data.data : null;+ const status = dump?.status ?? null;++ useEffect(() => {+ if (!status) return;+ setSeenAt((current) =>+ current[status] === undefined+ ? { ...current, [status]: Date.now() }+ : current,+ );+ }, [status]);++ return { dump, seenAt, isLoading, isError, error, refetch };+}diff --git a/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/waterfall.ts b/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/waterfall.tsnew file mode 100644index 0000000000..f19bfdb499--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/dev/brain-dump-debug/waterfall.ts@@ -0,0 +1,142 @@+import type { BrainDumpStatus } from "@/app/api/__generated__/models/brainDumpStatus";+import type { IntroPath } from "@/services/onboarding/brain-dump-handoff";+import {+ EXTRACT_BUDGET_MS,+ TRANSCRIBE_BUDGET_MS,+ type RecordingSnapshot,+} from "./helpers";++export type StatusSeenAt = Partial<Record<BrainDumpStatus, number>>;++export interface WaterfallStage {+ id: string;+ label: string;+ durationMs: number | null;+ budgetMs: number | null;+ source: string;+}++interface Args {+ snapshot: RecordingSnapshot;+ seenAt: StatusSeenAt;+ introPath: IntroPath | null;+}++export function buildWaterfall({ snapshot, seenAt, introPath }: Args) {+ return [+ recordStage(snapshot),+ uploadStage(snapshot),+ transcribeStage(seenAt),+ extractStage(seenAt),+ introStage(introPath),+ ];+}++export function isOverBudget(stage: WaterfallStage) {+ return stage.durationMs !== null && stage.budgetMs !== null+ ? stage.durationMs > stage.budgetMs+ : false;+}++function recordStage(snapshot: RecordingSnapshot): WaterfallStage {+ const firstSavedAt = snapshot.parts.at(0)?.savedAt ?? null;+ const lastSavedAt = snapshot.parts.at(-1)?.savedAt ?? null;+ const startedAt = snapshot.meta?.startedAt ?? firstSavedAt;+ const durationMs =+ startedAt !== null && lastSavedAt !== null ? lastSavedAt - startedAt : null;++ return {+ id: "record",+ label: "Record",+ durationMs,+ budgetMs: null,+ source:+ durationMs === null+ ? "Unmeasured — no recording currently in IndexedDB."+ : "Measured from IndexedDB: meta.startedAt → savedAt of the last part.",+ };+}++function uploadStage(snapshot: RecordingSnapshot): WaterfallStage {+ return {+ id: "upload",+ label: "Upload",+ durationMs: null,+ budgetMs: null,+ source:+ "Unmeasured — RecordingPart records `uploaded` as a boolean with no " ++ "`uploadedAt`, so per-part upload latency cannot be derived. " ++ `${snapshot.pendingUploads} of ${snapshot.parts.length} part(s) are still not marked uploaded.`,+ };+}++function transcribeStage(seenAt: StatusSeenAt): WaterfallStage {+ const startedAt = firstSeen(seenAt, ["recording_uploaded", "transcribing"]);+ const endedAt = firstSeen(seenAt, ["transcribed", "extracting", "completed"]);+ return {+ id: "transcribe",+ label: "Transcribe",+ durationMs: span(startedAt, endedAt),+ budgetMs: TRANSCRIBE_BUDGET_MS,+ source: transitionSource(+ startedAt,+ endedAt,+ "recording_uploaded/transcribing",+ "transcribed",+ ),+ };+}++function extractStage(seenAt: StatusSeenAt): WaterfallStage {+ const startedAt = firstSeen(seenAt, ["transcribed", "extracting"]);+ const endedAt = firstSeen(seenAt, ["completed"]);+ return {+ id: "extract",+ label: "Extract",+ durationMs: span(startedAt, endedAt),+ budgetMs: EXTRACT_BUDGET_MS,+ source: transitionSource(+ startedAt,+ endedAt,+ "transcribed/extracting",+ "completed",+ ),+ };+}++function introStage(introPath: IntroPath | null): WaterfallStage {+ return {+ id: "intro",+ label: "Intro message",+ durationMs: null,+ budgetMs: null,+ source: introPath+ ? `Unmeasured — the intro turn is composed on the copilot home after handoff. Pending handoff path: ${introPath}.`+ : "Unmeasured — no intro handoff in sessionStorage (autogpt:onboarding-intro-path), and no endpoint reports intro latency.",+ };+}++function firstSeen(seenAt: StatusSeenAt, statuses: BrainDumpStatus[]) {+ const times = statuses+ .map((status) => seenAt[status])+ .filter((time): time is number => time !== undefined);+ return times.length > 0 ? Math.min(...times) : null;+}++function span(startedAt: number | null, endedAt: number | null) {+ return startedAt !== null && endedAt !== null && endedAt >= startedAt+ ? endedAt - startedAt+ : null;+}++function transitionSource(+ startedAt: number | null,+ endedAt: number | null,+ from: string,+ to: string,+) {+ if (startedAt !== null && endedAt !== null) {+ return `Measured from polled status transitions: first "${from}" → first "${to}" (1s poll resolution).`;+ }+ return `Unmeasured — this page did not observe both the "${from}" and "${to}" statuses while polling. Keep it open across a finalize to capture them.`;+}diff --git a/autogpt_platform/frontend/src/app/api/openapi.json b/autogpt_platform/frontend/src/app/api/openapi.jsonindex c4e303c7dc..27d192d2e9 100644--- a/autogpt_platform/frontend/src/app/api/openapi.json+++ b/autogpt_platform/frontend/src/app/api/openapi.json@@ -8929,6 +8929,238 @@"security": [{ "HTTPBearerJWT": [] }]}},+ "/api/onboarding/brain-dump": {+ "delete": {+ "tags": ["v1", "onboarding", "onboarding", "brain-dump"],+ "summary": "Discard Brain Dump",+ "description": "Drop a half-uploaded take's server buffer when the user re-records.\n\nThe row and any stored audio are left alone — only the disposable\npart buffer is cleared.\n\n``recording_id`` says *which* take to drop. Without it the row's\ncurrent take is assumed, which is wrong as soon as a second tab has\nmoved the row on: the caller would clear a buffer that is still being\nfilled. Callers know their own id, so they send it.",+ "operationId": "discard_brain_dump",+ "security": [{ "HTTPBearerJWT": [] }],+ "parameters": [+ {+ "name": "recording_id",+ "in": "query",+ "required": false,+ "schema": {+ "anyOf": [+ { "type": "string", "pattern": "^[A-Za-z0-9_-]{1,64}$" },+ { "type": "null" }+ ],+ "title": "Recording Id"+ }+ }+ ],+ "responses": {+ "200": {+ "description": "Successful Response",+ "content": {+ "application/json": {+ "schema": { "$ref": "#/components/schemas/DumpStatusResponse" }+ }+ }+ },+ "401": {+ "$ref": "#/components/responses/HTTP401NotAuthenticatedError"+ },+ "422": {+ "description": "Validation Error",+ "content": {+ "application/json": {+ "schema": { "$ref": "#/components/schemas/HTTPValidationError" }+ }+ }+ }+ }+ }+ },+ "/api/onboarding/brain-dump/finalize": {+ "post": {+ "tags": ["v1", "onboarding", "onboarding", "brain-dump"],+ "summary": "Finalize Brain Dump",+ "description": "Close out a take: assemble, scan, store, transcribe.\n\nExtraction and greeting generation continue in the background after\nthis responds — the preparing step polls ``/status`` until\n``greeting_ready``. Idempotent per ``recording_id`` — a client that\nretries after a timeout gets the stored result rather than a second\ntranscription.",+ "operationId": "finalize_brain_dump",+ "requestBody": {+ "content": {+ "application/json": {+ "schema": { "$ref": "#/components/schemas/FinalizeRequest" }+ }+ },+ "required": true+ },+ "responses": {+ "200": {+ "description": "Successful Response",+ "content": {+ "application/json": {+ "schema": { "$ref": "#/components/schemas/FinalizeResponse" }+ }+ }+ },+ "401": {+ "$ref": "#/components/responses/HTTP401NotAuthenticatedError"+ },+ "422": {+ "description": "Validation Error",+ "content": {+ "application/json": {+ "schema": { "$ref": "#/components/schemas/HTTPValidationError" }+ }+ }+ }+ },+ "security": [{ "HTTPBearerJWT": [] }]+ }+ },+ "/api/onboarding/brain-dump/intro": {+ "get": {+ "tags": ["v1", "onboarding", "onboarding", "brain-dump"],+ "summary": "Get Brain Dump Intro",+ "description": "Content for the copilot home's onboarding greeting.\n\nPre-generated during finalize, so this is a plain row read — the\ncopilot home never waits on a model to render its first screen.\n``greeting_done=true`` means the client should show nothing and cache\nthat locally.",+ "operationId": "get_brain_dump_intro",+ "responses": {+ "200": {+ "description": "Successful Response",+ "content": {+ "application/json": {+ "schema": { "$ref": "#/components/schemas/IntroCardResponse" }+ }+ }+ },+ "401": {+ "$ref": "#/components/responses/HTTP401NotAuthenticatedError"+ }+ },+ "security": [{ "HTTPBearerJWT": [] }]+ }+ },+ "/api/onboarding/brain-dump/intro/complete": {+ "post": {+ "tags": ["v1", "onboarding", "onboarding", "brain-dump"],+ "summary": "Complete Brain Dump Greeting",+ "description": "Mark the greeting as done — called when the first message is sent.\n\nPermanent and content-preserving: the flag flips, the stored greeting\nstays in the row.",+ "operationId": "complete_brain_dump_greeting",+ "responses": {+ "200": {+ "description": "Successful Response",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/GreetingDoneResponse"+ }+ }+ }+ },+ "401": {+ "$ref": "#/components/responses/HTTP401NotAuthenticatedError"+ }+ },+ "security": [{ "HTTPBearerJWT": [] }]+ }+ },+ "/api/onboarding/brain-dump/parts": {+ "post": {+ "tags": ["v1", "onboarding", "onboarding", "brain-dump"],+ "summary": "Upload Brain Dump Part",+ "description": "Accept one MediaRecorder timeslice blob.\n\nParts arrive while the user is still talking, so this stays cheap:\nsize checks, buffer, done. Validation that needs the whole stream\n(virus scan, transcription) happens on finalize.",+ "operationId": "upload_brain_dump_part",+ "requestBody": {+ "content": {+ "multipart/form-data": {+ "schema": {+ "$ref": "#/components/schemas/Body_upload_brain_dump_part"+ }+ }+ },+ "required": true+ },+ "responses": {+ "200": {+ "description": "Successful Response",+ "content": {+ "application/json": {+ "schema": { "$ref": "#/components/schemas/UploadPartResponse" }+ }+ }+ },+ "401": {+ "$ref": "#/components/responses/HTTP401NotAuthenticatedError"+ },+ "422": {+ "description": "Validation Error",+ "content": {+ "application/json": {+ "schema": { "$ref": "#/components/schemas/HTTPValidationError" }+ }+ }+ }+ },+ "security": [{ "HTTPBearerJWT": [] }]+ }+ },+ "/api/onboarding/brain-dump/recommended-providers": {+ "get": {+ "tags": ["v1", "onboarding", "onboarding", "brain-dump"],+ "summary": "Get Brain Dump Recommended Providers",+ "description": "Provider picks for the welcome dialog's \"Connect your tools\" panel.\n\nWritten by a background job that runs beside the greeting one; a plain\nrow read here. ``ready=false`` means keep polling.",+ "operationId": "get_brain_dump_recommended_providers",+ "responses": {+ "200": {+ "description": "Successful Response",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/RecommendedProvidersResponse"+ }+ }+ }+ },+ "401": {+ "$ref": "#/components/responses/HTTP401NotAuthenticatedError"+ }+ },+ "security": [{ "HTTPBearerJWT": [] }]+ }+ },+ "/api/onboarding/brain-dump/recording": {+ "get": {+ "tags": ["v1", "onboarding", "onboarding", "brain-dump"],+ "summary": "Download Brain Dump Recording",+ "description": "Hand back the user's own recording.\n\nKept working even when ``status`` is ``failed`` — \"your recording is\nsafe\" has to be true, and this is what makes it checkable.",+ "operationId": "download_brain_dump_recording",+ "responses": {+ "200": {+ "description": "Successful Response",+ "content": { "application/json": { "schema": {} } }+ },+ "401": {+ "$ref": "#/components/responses/HTTP401NotAuthenticatedError"+ }+ },+ "security": [{ "HTTPBearerJWT": [] }]+ }+ },+ "/api/onboarding/brain-dump/status": {+ "get": {+ "tags": ["v1", "onboarding", "onboarding", "brain-dump"],+ "summary": "Get Brain Dump Status",+ "description": "Polled by the loading screen while the pipeline runs.",+ "operationId": "get_brain_dump_status",+ "responses": {+ "200": {+ "description": "Successful Response",+ "content": {+ "application/json": {+ "schema": { "$ref": "#/components/schemas/DumpStatusResponse" }+ }+ }+ },+ "401": {+ "$ref": "#/components/responses/HTTP401NotAuthenticatedError"+ }+ },+ "security": [{ "HTTPBearerJWT": [] }]+ }+ },"/api/onboarding/completed": {"get": {"tags": ["v1", "onboarding", "public"],@@ -9029,7 +9261,8 @@"AGENT_INPUT","CONGRATS","VISIT_COPILOT",- "BUILDER_OPEN"+ "BUILDER_OPEN",+ "CAPABILITY_CARDS"],"type": "string","title": "Step"@@ -14638,6 +14871,24 @@"required": ["file"],"title": "Body_uploadWorkspaceFile"},+ "Body_upload_brain_dump_part": {+ "properties": {+ "file": { "type": "string", "format": "binary", "title": "File" },+ "recording_id": {+ "type": "string",+ "pattern": "^[A-Za-z0-9_-]{1,64}$",+ "title": "Recording Id"+ },+ "part_index": {+ "type": "integer",+ "minimum": 0.0,+ "title": "Part Index"+ }+ },+ "type": "object",+ "required": ["file", "recording_id", "part_index"],+ "title": "Body_upload_brain_dump_part"+ },"BotAnalyticsSummary": {"properties": {"platform": {@@ -14781,6 +15032,23 @@"required": ["date", "messages", "replies", "errors"],"title": "BotTimeseriesPoint"},+ "BrainDumpInputMode": {+ "type": "string",+ "enum": ["voice", "typed", "skipped"],+ "title": "BrainDumpInputMode"+ },+ "BrainDumpStatus": {+ "type": "string",+ "enum": [+ "recording_uploaded",+ "transcribing",+ "transcribed",+ "extracting",+ "completed",+ "failed"+ ],+ "title": "BrainDumpStatus"+ },"BulkMoveAgentsRequest": {"properties": {"agent_ids": {@@ -16254,6 +16522,38 @@"title": "DreamPassUsage","description": "Aggregate usage across all phases of one dream pass.\n\nSurfaced on ``DreamPassResult.usage`` so eval / admin UI / billing\ncan read tokens + cost without recomputing. Written to\n``PlatformCostLog`` from apply.py with ``provider='dream_pass'``;\ncharged against the user's rate-limit window via\n``persist_and_record_usage`` so dream-pass spend rolls into the\nsame daily/weekly subscription tier budget as chat-execution\noverage."},+ "DumpStatusResponse": {+ "properties": {+ "status": {+ "anyOf": [+ { "$ref": "#/components/schemas/BrainDumpStatus" },+ { "type": "null" }+ ]+ },+ "input_mode": {+ "anyOf": [+ { "$ref": "#/components/schemas/BrainDumpInputMode" },+ { "type": "null" }+ ]+ },+ "error_code": {+ "anyOf": [{ "type": "string" }, { "type": "null" }],+ "title": "Error Code"+ },+ "has_audio": {+ "type": "boolean",+ "title": "Has Audio",+ "default": false+ },+ "greeting_ready": {+ "type": "boolean",+ "title": "Greeting Ready",+ "default": false+ }+ },+ "type": "object",+ "title": "DumpStatusResponse"+ },"EnableShareRequest": {"properties": {"auto_share_executions": {@@ -16915,6 +17215,58 @@"title": "FailedExecutionsListResponse","description": "Response model for list of failed executions"},+ "FinalizeRequest": {+ "properties": {+ "recording_id": {+ "type": "string",+ "pattern": "^[A-Za-z0-9_-]{1,64}$",+ "title": "Recording Id"+ },+ "input_mode": {+ "$ref": "#/components/schemas/BrainDumpInputMode",+ "default": "voice"+ },+ "duration_secs": {+ "anyOf": [+ { "type": "number", "maximum": 14400.0, "minimum": 0.0 },+ { "type": "null" }+ ],+ "title": "Duration Secs"+ },+ "mime_type": {+ "anyOf": [{ "type": "string" }, { "type": "null" }],+ "title": "Mime Type"+ },+ "text": {+ "anyOf": [+ { "type": "string", "maxLength": 20000 },+ { "type": "null" }+ ],+ "title": "Text"+ }+ },+ "type": "object",+ "required": ["recording_id"],+ "title": "FinalizeRequest",+ "description": "Finalize one take.\n\n``text`` is the typed fallback: when set, no audio is expected and the\ndump is stored with ``input_mode=typed``. ``input_mode=skipped``\nrecords the deliberate skip so Path B can be chosen server-side."+ },+ "FinalizeResponse": {+ "properties": {+ "status": { "$ref": "#/components/schemas/BrainDumpStatus" },+ "input_mode": { "$ref": "#/components/schemas/BrainDumpInputMode" },+ "transcript_preview": {+ "anyOf": [{ "type": "string" }, { "type": "null" }],+ "title": "Transcript Preview"+ },+ "error_code": {+ "anyOf": [{ "type": "string" }, { "type": "null" }],+ "title": "Error Code"+ }+ },+ "type": "object",+ "required": ["status", "input_mode"],+ "title": "FinalizeResponse"+ },"FolderCreateRequest": {"properties": {"name": {@@ -17860,6 +18212,14 @@"required": ["provider", "config_schema", "credentials_input_name"],"title": "GraphTriggerInfo"},+ "GreetingDoneResponse": {+ "properties": {+ "greeting_done": { "type": "boolean", "title": "Greeting Done" }+ },+ "type": "object",+ "required": ["greeting_done"],+ "title": "GreetingDoneResponse"+ },"HTTPValidationError": {"properties": {"detail": {@@ -18005,6 +18365,31 @@"required": ["store_listing_version_id"],"title": "InstallWorkflowRequest"},+ "IntroCardResponse": {+ "properties": {+ "path": { "type": "string", "title": "Path" },+ "greeting": { "type": "string", "title": "Greeting" },+ "prompts": {+ "items": { "$ref": "#/components/schemas/SuggestedPrompt" },+ "type": "array",+ "title": "Prompts",+ "default": []+ },+ "greeting_done": {+ "type": "boolean",+ "title": "Greeting Done",+ "default": false+ },+ "transcript": {+ "anyOf": [{ "type": "string" }, { "type": "null" }],+ "title": "Transcript"+ }+ },+ "type": "object",+ "required": ["path", "greeting"],+ "title": "IntroCardResponse",+ "description": "Content for the copilot home's onboarding greeting.\n\n``path`` is \"A\" when we have something to reflect back and \"B\" when\nthe greeting's job is to invite a recording instead. ``greeting_done``\nis the server-side \"already seen it\" flag — when true the client\nshows nothing and caches the fact locally."+ },"InvitationCreateResponse": {"properties": {"id": { "type": "string", "title": "Id" },@@ -19705,7 +20090,8 @@"RUN_14_DAYS","RUN_AGENTS_100","BUILDER_OPEN",- "BUILDER_RUN_AGENT"+ "BUILDER_RUN_AGENT",+ "CAPABILITY_CARDS"],"title": "OnboardingStep"},@@ -21082,6 +21468,31 @@"title": "RecentExecution","description": "Summary of a recent execution for quality assessment.\n\nUsed by the LLM to understand the agent's recent performance with specific examples\nrather than just aggregate statistics."},+ "RecommendedProvider": {+ "properties": {+ "provider": { "type": "string", "title": "Provider" },+ "reason": { "type": "string", "title": "Reason", "default": "" }+ },+ "type": "object",+ "required": ["provider"],+ "title": "RecommendedProvider",+ "description": "One provider Claude picked from the transcript.\n\n``provider`` is a registry id (validated against the live provider\nlist before storage); ``reason`` is the one-liner shown under it in\nthe welcome dialog's Recommended section."+ },+ "RecommendedProvidersResponse": {+ "properties": {+ "ready": { "type": "boolean", "title": "Ready" },+ "providers": {+ "items": { "$ref": "#/components/schemas/RecommendedProvider" },+ "type": "array",+ "title": "Providers",+ "default": []+ }+ },+ "type": "object",+ "required": ["ready"],+ "title": "RecommendedProvidersResponse",+ "description": "Recommendations for the \"Connect your tools\" panel.\n\n``ready`` is false while the background job is still running — the\nclient keeps polling. Once true, ``providers`` is the final answer,\nempty included (\"nothing worth recommending\" is a real result)."+ },"RefundRequest": {"properties": {"id": { "type": "string", "title": "Id" },@@ -23045,6 +23456,17 @@"title": "SuggestedGoalResponse","description": "Response when the goal needs refinement with a suggested alternative."},+ "SuggestedPrompt": {+ "properties": {+ "title": { "type": "string", "title": "Title" },+ "prompt": { "type": "string", "title": "Prompt" },+ "icon": { "type": "string", "title": "Icon", "default": "sparkle" }+ },+ "type": "object",+ "required": ["title", "prompt"],+ "title": "SuggestedPrompt",+ "description": "One pickable prompt under the greeting.\n\n``title`` is the short line the user sees; ``prompt`` is the full\nmessage that gets sent when they pick it. ``icon`` is a Phosphor icon\nslug from ``intro.PROMPT_ICONS`` — the frontend maps it to the actual\nicon component and falls back to a sparkle for anything unknown."+ },"SuggestedPromptsResponse": {"properties": {"themes": {@@ -24949,6 +25371,22 @@"title": "UploadCopilotSkillRequest","description": "Body for the library UI's \"upload skill\" action.\n\nCarries the raw ``SKILL.md`` text (YAML frontmatter + markdown body) the\nuser picked from disk; the server parses + validates it so the upload and\nthe copilot's ``store_skill`` tool share one source of truth."},+ "UploadPartResponse": {+ "properties": {+ "recording_id": { "type": "string", "title": "Recording Id" },+ "part_index": { "type": "integer", "title": "Part Index" },+ "received_bytes": { "type": "integer", "title": "Received Bytes" },+ "total_bytes": { "type": "integer", "title": "Total Bytes" }+ },+ "type": "object",+ "required": [+ "recording_id",+ "part_index",+ "received_bytes",+ "total_bytes"+ ],+ "title": "UploadPartResponse"+ },"UsageWindowPublic": {"properties": {"percent_used": {diff --git a/autogpt_platform/frontend/src/app/api/transcribe/__tests__/route.test.ts b/autogpt_platform/frontend/src/app/api/transcribe/__tests__/route.test.tsindex 938f5987b6..2626b48b59 100644--- a/autogpt_platform/frontend/src/app/api/transcribe/__tests__/route.test.ts+++ b/autogpt_platform/frontend/src/app/api/transcribe/__tests__/route.test.ts@@ -8,15 +8,27 @@ vi.mock("@/lib/auth/server/getServerAuthToken", () => ({import { getServerAuthToken } from "@/lib/auth/server/getServerAuthToken";import { POST } from "../route";-function makeAudioRequest(type = "audio/webm"): NextRequest {+function makeAudioRequest(type = "audio/webm", context?: string): NextRequest {const formData = new FormData();formData.append("audio", new Blob(["audio-bytes"], { type }));+ if (context !== undefined) formData.append("context", context);return new Request("http://localhost/api/transcribe", {method: "POST",body: formData,}) as NextRequest;}+function stubOkFetch() {+ const fetchMock = vi.fn().mockResolvedValue(+ new Response(JSON.stringify({ text: "ok" }), {+ status: 200,+ headers: { "content-type": "application/json" },+ }),+ );+ vi.stubGlobal("fetch", fetchMock);+ return fetchMock;+}+describe("transcribe route", () => {beforeEach(() => {vi.clearAllMocks();@@ -144,6 +156,41 @@ describe("transcribe route", () => {});});+ it("forwards the composer draft as the transcription prompt", async () => {+ process.env.OPENAI_API_KEY = "openai-token";+ const fetchMock = stubOkFetch();++ await POST(makeAudioRequest("audio/webm", " Ship the Q3 recap "));++ const upstreamBody = (fetchMock.mock.calls[0]?.[1] as RequestInit)+ .body as FormData;+ expect(upstreamBody.get("prompt")).toBe("Ship the Q3 recap");+ });++ it("sends no prompt when the request carries no draft", async () => {+ process.env.OPENAI_API_KEY = "openai-token";+ const fetchMock = stubOkFetch();++ await POST(makeAudioRequest("audio/webm", " "));++ const upstreamBody = (fetchMock.mock.calls[0]?.[1] as RequestInit)+ .body as FormData;+ expect(upstreamBody.get("prompt")).toBeNull();+ });++ it("trims an oversized draft to the tail the model actually reads", async () => {+ process.env.OPENAI_API_KEY = "openai-token";+ const fetchMock = stubOkFetch();++ await POST(makeAudioRequest("audio/webm", `${"a".repeat(900)}tail`));++ const upstreamBody = (fetchMock.mock.calls[0]?.[1] as RequestInit)+ .body as FormData;+ const prompt = upstreamBody.get("prompt") as string;+ expect(prompt).toHaveLength(800);+ expect(prompt.endsWith("tail")).toBe(true);+ });+it("keeps requiring an API key for the default OpenAI transcription endpoint", async () => {const response = await POST(makeAudioRequest());diff --git a/autogpt_platform/frontend/src/app/api/transcribe/route.ts b/autogpt_platform/frontend/src/app/api/transcribe/route.tsindex 7ed62aa54e..4cb22e2521 100644--- a/autogpt_platform/frontend/src/app/api/transcribe/route.ts+++ b/autogpt_platform/frontend/src/app/api/transcribe/route.ts@@ -4,6 +4,11 @@ import { NextRequest, NextResponse } from "next/server";const DEFAULT_TRANSCRIPTION_API_BASE_URL = "https://api.openai.com/v1";const DEFAULT_TRANSCRIPTION_MODEL = "whisper-1";const MAX_FILE_SIZE = 25 * 1024 * 1024; // 25MB - Whisper's limit+// The composer sends its current draft as `context`; it rides upstream as+// the transcription `prompt` so names already on screen are spelled the+// same way in the transcript. Whisper only reads the last ~224 tokens of a+// prompt, so a long draft is trimmed from the front rather than sent whole.+const MAX_CONTEXT_CHARS = 800;function getTranscriptionApiBaseUrl(): string {return (@@ -79,6 +84,16 @@ export async function POST(request: NextRequest) {const whisperFormData = new FormData();whisperFormData.append("file", audioFile, `recording.${ext}`);whisperFormData.append("model", getTranscriptionModel());++ const context = formData.get("context");+ const prompt =+ typeof context === "string"+ ? context.trim().slice(-MAX_CONTEXT_CHARS)+ : "";+ if (prompt) {+ whisperFormData.append("prompt", prompt);+ }+const headers = new Headers();if (apiKey) {headers.set("Authorization", `Bearer ${apiKey}`);diff --git a/autogpt_platform/frontend/src/components/atoms/FadeIn/FadeIn.tsx b/autogpt_platform/frontend/src/components/atoms/FadeIn/FadeIn.tsxindex ed69e0995a..c120182d06 100644--- a/autogpt_platform/frontend/src/components/atoms/FadeIn/FadeIn.tsx+++ b/autogpt_platform/frontend/src/components/atoms/FadeIn/FadeIn.tsx@@ -6,15 +6,17 @@ import { ReactNode } from "react";interface Props {children: ReactNode;onComplete?: () => void;+ className?: string;}-export function FadeIn({ children, onComplete }: Props) {+export function FadeIn({ children, onComplete, className }: Props) {return (<motion.divinitial={{ opacity: 0, y: 12 }}animate={{ opacity: 1, y: 0 }}transition={{ duration: 0.4, ease: "easeOut" }}onAnimationComplete={onComplete}+ className={className}>{children}</motion.div>diff --git a/autogpt_platform/frontend/src/components/atoms/GlassPixelBackdrop/GlassPixelBackdrop.stories.tsx b/autogpt_platform/frontend/src/components/atoms/GlassPixelBackdrop/GlassPixelBackdrop.stories.tsxnew file mode 100644index 0000000000..8ad20ace06--- /dev/null+++ b/autogpt_platform/frontend/src/components/atoms/GlassPixelBackdrop/GlassPixelBackdrop.stories.tsx@@ -0,0 +1,30 @@+import type { Meta, StoryObj } from "@storybook/nextjs";+import { GlassPixelBackdrop } from "./GlassPixelBackdrop";++const meta: Meta<typeof GlassPixelBackdrop> = {+ title: "Atoms/GlassPixelBackdrop",+ component: GlassPixelBackdrop,+ tags: ["autodocs"],+ parameters: {+ layout: "centered",+ docs: {+ description: {+ component:+ "A glass pixel mosaic backdrop: a grid of frosted white tiles at deterministic varying opacities, letting the gradient behind it glow through each pixel differently. Positioned absolutely, so it needs a relatively-positioned parent.",+ },+ },+ },+};++export default meta;+type Story = StoryObj<typeof meta>;++export const Basic: Story = {+ render: function BasicStory() {+ return (+ <div className="relative h-64 w-96 overflow-hidden rounded-2xl bg-gradient-to-br from-violet-500 via-purple-500 to-indigo-600">+ <GlassPixelBackdrop />+ </div>+ );+ },+};diff --git a/autogpt_platform/frontend/src/components/atoms/GlassPixelBackdrop/GlassPixelBackdrop.tsx b/autogpt_platform/frontend/src/components/atoms/GlassPixelBackdrop/GlassPixelBackdrop.tsxnew file mode 100644index 0000000000..ea4018122c--- /dev/null+++ b/autogpt_platform/frontend/src/components/atoms/GlassPixelBackdrop/GlassPixelBackdrop.tsx@@ -0,0 +1,34 @@+const COLS = 16;+const ROWS = 7;++// Deterministic pseudo-random per cell (Knuth multiplicative hash) so the+// server and client render the identical mosaic — Math.random here would+// be a hydration mismatch.+function cellOpacity(index: number) {+ const noise = ((index + 1) * 2654435761) % 1000;+ return 0.02 + (noise / 1000) * 0.14;+}++// A "glass pixel" mosaic over the stage gradient: a grid of frosted white+// tiles at varying opacities, letting the same violet gradient glow+// through each pixel differently.+export function GlassPixelBackdrop() {+ return (+ <div+ aria-hidden+ className="pointer-events-none absolute inset-0 grid"+ style={{+ gridTemplateColumns: `repeat(${COLS}, 1fr)`,+ gridTemplateRows: `repeat(${ROWS}, 1fr)`,+ }}+ >+ {Array.from({ length: COLS * ROWS }, (_, index) => (+ <span+ key={index}+ className="border-[0.5px] border-white/10 bg-white"+ style={{ opacity: cellOpacity(index) }}+ />+ ))}+ </div>+ );+}diff --git a/autogpt_platform/frontend/src/components/atoms/Input/Input.tsx b/autogpt_platform/frontend/src/components/atoms/Input/Input.tsxindex 81607ad184..b2d87bcce8 100644--- a/autogpt_platform/frontend/src/components/atoms/Input/Input.tsx+++ b/autogpt_platform/frontend/src/components/atoms/Input/Input.tsx@@ -107,7 +107,7 @@ export const Input = forwardRef<InputElement, TextFieldProps>(function Input(className={cn(baseStyles,errorStyles,- "-mb-1 h-auto min-h-[2.875rem] rounded-xl",+ "-mb-1 h-auto min-h-[2.875rem]",// Size variants for textareasize === "small" && ["min-h-[2.25rem]", // 36px minimumdiff --git a/autogpt_platform/frontend/src/components/atoms/Reveal/Reveal.stories.tsx b/autogpt_platform/frontend/src/components/atoms/Reveal/Reveal.stories.tsxnew file mode 100644index 0000000000..1339db786b--- /dev/null+++ b/autogpt_platform/frontend/src/components/atoms/Reveal/Reveal.stories.tsx@@ -0,0 +1,47 @@+import type { Meta, StoryObj } from "@storybook/nextjs";+import { RevealGroup, RevealItem } from "./Reveal";++const meta: Meta<typeof RevealGroup> = {+ title: "Atoms/Reveal",+ component: RevealGroup,+ tags: ["autodocs"],+ parameters: {+ layout: "centered",+ docs: {+ description: {+ component:+ "Entrance reveal for stacked content: each RevealItem blurs and lifts into place independently, so items mounted later still animate. Honors prefers-reduced-motion by falling back to a plain fade.",+ },+ },+ },+ argTypes: {+ className: { control: "text", description: "Optional className." },+ },+};++export default meta;+type Story = StoryObj<typeof meta>;++export const Basic: Story = {+ render: function BasicStory(args) {+ return (+ <RevealGroup {...args} className="flex w-72 flex-col gap-3">+ <RevealItem>+ <h3 className="text-lg font-semibold text-zinc-900">+ Tell us about your work+ </h3>+ </RevealItem>+ <RevealItem>+ <p className="text-sm text-zinc-600">+ Most people talk for two to three minutes.+ </p>+ </RevealItem>+ <RevealItem>+ <p className="text-sm text-zinc-600">+ Each item reveals on its own, even if it mounts later.+ </p>+ </RevealItem>+ </RevealGroup>+ );+ },+};diff --git a/autogpt_platform/frontend/src/components/atoms/Reveal/Reveal.tsx b/autogpt_platform/frontend/src/components/atoms/Reveal/Reveal.tsxnew file mode 100644index 0000000000..dd19feae67--- /dev/null+++ b/autogpt_platform/frontend/src/components/atoms/Reveal/Reveal.tsx@@ -0,0 +1,55 @@+"use client";++import { motion, useReducedMotion, Variants } from "framer-motion";+import { ReactNode } from "react";++const ITEM: Variants = {+ hidden: { opacity: 0, y: 14, filter: "blur(8px)" },+ show: {+ opacity: 1,+ y: 0,+ filter: "blur(0px)",+ transition: { duration: 0.45, ease: [0.16, 1, 0.3, 1] },+ },+};++const ITEM_REDUCED: Variants = {+ hidden: { opacity: 0 },+ show: { opacity: 1, transition: { duration: 0.2 } },+};++export function RevealGroup({+ children,+ className,+}: {+ children: ReactNode;+ className?: string;+}) {+ return <div className={className}>{children}</div>;+}++// Each item drives its own reveal instead of inheriting a variant from the+// group: variant propagation only reaches children that exist when the+// parent animates, so items mounted later (the mic block returning after+// processing, the subline after the recovery prompt) would render stuck at+// `hidden` — an invisible page with a working headline.+export function RevealItem({+ children,+ className,+}: {+ children: ReactNode;+ className?: string;+}) {+ const prefersReducedMotion = useReducedMotion();++ return (+ <motion.div+ variants={prefersReducedMotion ? ITEM_REDUCED : ITEM}+ initial="hidden"+ animate="show"+ className={className}+ >+ {children}+ </motion.div>+ );+}diff --git a/autogpt_platform/frontend/src/components/atoms/SwapFade/SwapFade.stories.tsx b/autogpt_platform/frontend/src/components/atoms/SwapFade/SwapFade.stories.tsxnew file mode 100644index 0000000000..b88af2be6a--- /dev/null+++ b/autogpt_platform/frontend/src/components/atoms/SwapFade/SwapFade.stories.tsx@@ -0,0 +1,53 @@+import type { Meta, StoryObj } from "@storybook/nextjs";+import { useState } from "react";+import { SwapFade } from "./SwapFade";++const meta: Meta<typeof SwapFade> = {+ title: "Atoms/SwapFade",+ component: SwapFade,+ tags: ["autodocs"],+ parameters: {+ layout: "centered",+ docs: {+ description: {+ component:+ "Swaps one piece of content for another in place: the outgoing content lifts and blurs away before the incoming one settles, keyed by swapKey. Honors prefers-reduced-motion with a plain crossfade.",+ },+ },+ },+ argTypes: {+ swapKey: {+ control: "text",+ description: "Key that triggers the swap when it changes.",+ },+ className: { control: "text", description: "Optional className." },+ },+};++export default meta;+type Story = StoryObj<typeof meta>;++export const Basic: Story = {+ render: function BasicStory() {+ const [label, setLabel] = useState<"Tap to talk" | "Listening...">(+ "Tap to talk",+ );++ return (+ <div className="flex flex-col items-center gap-4">+ <SwapFade swapKey={label} className="text-base text-zinc-900">+ {label}+ </SwapFade>+ <button+ type="button"+ className="rounded-full border border-zinc-300 px-3 py-1 text-sm"+ onClick={() =>+ setLabel(label === "Tap to talk" ? "Listening..." : "Tap to talk")+ }+ >+ Swap content+ </button>+ </div>+ );+ },+};diff --git a/autogpt_platform/frontend/src/components/atoms/SwapFade/SwapFade.tsx b/autogpt_platform/frontend/src/components/atoms/SwapFade/SwapFade.tsxnew file mode 100644index 0000000000..642506ccdf--- /dev/null+++ b/autogpt_platform/frontend/src/components/atoms/SwapFade/SwapFade.tsx@@ -0,0 +1,44 @@+"use client";++import { AnimatePresence, motion, useReducedMotion } from "framer-motion";+import { ReactNode } from "react";++interface Props {+ swapKey: string;+ children: ReactNode;+ className?: string;+}++// Swaps one piece of content for another in place: the outgoing content+// lifts and blurs away before the incoming one settles, so a state change+// reads as a change rather than a re-render.+export function SwapFade({ swapKey, children, className }: Props) {+ const prefersReducedMotion = useReducedMotion();++ const states = prefersReducedMotion+ ? {+ initial: { opacity: 0 },+ animate: { opacity: 1 },+ exit: { opacity: 0 },+ }+ : {+ initial: { opacity: 0, y: 8, filter: "blur(4px)" },+ animate: { opacity: 1, y: 0, filter: "blur(0px)" },+ exit: { opacity: 0, y: -8, filter: "blur(4px)" },+ };++ return (+ <AnimatePresence mode="wait" initial={false}>+ <motion.div+ key={swapKey}+ initial={states.initial}+ animate={states.animate}+ exit={states.exit}+ transition={{ duration: 0.22, ease: [0.32, 0.72, 0, 1] }}+ className={className}+ >+ {children}+ </motion.div>+ </AnimatePresence>+ );+}diff --git a/autogpt_platform/frontend/src/components/atoms/ToggleChip/ToggleChip.stories.tsx b/autogpt_platform/frontend/src/components/atoms/ToggleChip/ToggleChip.stories.tsxnew file mode 100644index 0000000000..9d5c5ac28d--- /dev/null+++ b/autogpt_platform/frontend/src/components/atoms/ToggleChip/ToggleChip.stories.tsx@@ -0,0 +1,71 @@+import { Icon } from "@/components/atoms/Icon/Icon";+import { TooltipProvider } from "@/components/ui/tooltip";+import { BrainIcon } from "@hugeicons/core-free-icons";+import type { Meta, StoryObj } from "@storybook/nextjs";+import { useState } from "react";+import { ToggleChip } from "./ToggleChip";++const meta: Meta<typeof ToggleChip> = {+ title: "Atoms/ToggleChip",+ component: ToggleChip,+ tags: ["autodocs"],+ decorators: [+ (Story) => (+ <TooltipProvider>+ <Story />+ </TooltipProvider>+ ),+ ],+ parameters: {+ layout: "centered",+ docs: {+ description: {+ component:+ "A pill-shaped toggle with an icon, label, and tooltip. Hovering swaps the icon for a toggle glyph, and the label blurs between states. Locked chips keep their icon and refuse the hover affordance.",+ },+ },+ },+ argTypes: {+ label: { control: "text", description: "Visible chip label." },+ tooltip: { control: "text", description: "Tooltip content." },+ ariaLabel: { control: "text", description: "Accessible button label." },+ pressed: { control: "boolean", description: "Pressed state (controlled)." },+ locked: { control: "boolean", description: "Disable toggling." },+ className: { control: "text", description: "Optional className." },+ onToggle: { action: "toggle", description: "Toggle handler." },+ },+ args: {+ icon: <Icon icon={BrainIcon} size={14} />,+ label: "Extended Thinking",+ tooltip: "Switch copilot mode",+ ariaLabel: "Toggle Extended Thinking",+ },+};++export default meta;+type Story = StoryObj<typeof meta>;++export const Basic: Story = {+ render: function BasicStory(args) {+ const [pressed, setPressed] = useState<boolean>(false);+ return (+ <ToggleChip+ {...args}+ pressed={pressed}+ onToggle={() => {+ setPressed(!pressed);+ if (args.onToggle) args.onToggle();+ }}+ />+ );+ },+};++export const Locked: Story = {+ args: {+ pressed: true,+ locked: true,+ tooltip: "Locked to Extended Thinking",+ onToggle: () => {},+ },+};diff --git a/autogpt_platform/frontend/src/components/atoms/ToggleChip/ToggleChip.tsx b/autogpt_platform/frontend/src/components/atoms/ToggleChip/ToggleChip.tsxnew file mode 100644index 0000000000..b890a68b40--- /dev/null+++ b/autogpt_platform/frontend/src/components/atoms/ToggleChip/ToggleChip.tsx@@ -0,0 +1,89 @@+"use client";++import { Icon } from "@/components/atoms/Icon/Icon";+import {+ Tooltip,+ TooltipContent,+ TooltipTrigger,+} from "@/components/ui/tooltip";+import { cn } from "@/lib/utils";+import { ArrowDataTransferVerticalIcon } from "@hugeicons/core-free-icons";+import { AnimatePresence, motion, useReducedMotion } from "framer-motion";+import type { ReactNode } from "react";++interface Props {+ icon: ReactNode;+ label: string;+ tooltip: string;+ ariaLabel: string;+ pressed: boolean;+ onToggle: () => void;+ locked?: boolean;+ className?: string;+}++export function ToggleChip({+ icon,+ label,+ tooltip,+ ariaLabel,+ pressed,+ onToggle,+ locked = false,+ className,+}: Props) {+ const prefersReducedMotion = useReducedMotion();+ // Blur bridges the two label states so the swap reads as one motion rather+ // than a hard cut; opacity alone carries it under reduced motion.+ const blurred = prefersReducedMotion ? "blur(0px)" : "blur(3px)";++ return (+ <Tooltip>+ <TooltipTrigger asChild>+ <button+ type="button"+ aria-pressed={pressed}+ aria-disabled={locked}+ onClick={onToggle}+ aria-label={ariaLabel}+ className={cn(+ "group inline-flex h-7 items-center justify-center gap-1 rounded-full px-2.5 text-xs font-medium text-zinc-950 transition-colors hover:bg-white",+ locked && "cursor-not-allowed opacity-70 hover:bg-transparent",+ className,+ )}+ >+ <span className="relative inline-flex size-3.5 items-center justify-center">+ <span+ className={cn(+ "transition-opacity duration-150 ease-out",+ !locked && "group-hover:opacity-0",+ )}+ >+ {icon}+ </span>+ {!locked && (+ <Icon+ icon={ArrowDataTransferVerticalIcon}+ size={14}+ className="absolute opacity-0 transition-opacity duration-150 ease-out group-hover:opacity-100"+ />+ )}+ </span>+ <AnimatePresence mode="wait" initial={false}>+ <motion.span+ key={label}+ initial={{ filter: blurred, opacity: 0 }}+ animate={{ filter: "blur(0px)", opacity: 1 }}+ exit={{ filter: blurred, opacity: 0 }}+ transition={{ duration: 0.12, ease: [0, 0, 0.2, 1] }}+ className="hidden sm:inline"+ >+ {label}+ </motion.span>+ </AnimatePresence>+ </button>+ </TooltipTrigger>+ <TooltipContent>{tooltip}</TooltipContent>+ </Tooltip>+ );+}diff --git a/autogpt_platform/frontend/src/components/layout/AppSidebar/AppSidebar.tsx b/autogpt_platform/frontend/src/components/layout/AppSidebar/AppSidebar.tsxindex cfdc3a1ed6..3b513abf2c 100644--- a/autogpt_platform/frontend/src/components/layout/AppSidebar/AppSidebar.tsx+++ b/autogpt_platform/frontend/src/components/layout/AppSidebar/AppSidebar.tsx@@ -100,10 +100,23 @@ function NewTaskIcon() {);}+// The stronger active state + grey shell ship with the brain-dump+// experience; off keeps the original white sidebar.+function useNavItemClassName() {+ const isBrainDumpEnabled = useGetFlag(Flag.ONBOARDING_BRAIN_DUMP);+ return cn(+ "h-auto rounded-xl p-2 pl-3 font-normal data-[active=true]:font-normal group-data-[collapsible=icon]:!p-1.5 hover:!bg-zinc-100 [&>svg]:size-4 group-data-[collapsible=icon]:[&>svg]:size-4.5",+ isBrainDumpEnabled+ ? "data-[active=true]:!bg-zinc-200 data-[active=true]:hover:!bg-zinc-200"+ : "data-[active=true]:!bg-zinc-100",+ );+}+// New Task shares the nav-item styling with the main links so it sits in the// same section with a uniform gap, instead of being a standalone CTA button.function NewTaskItem() {const pathname = usePathname();+ const navItemClassName = useNavItemClassName();return (<SidebarMenuItem>@@ -111,7 +124,7 @@ function NewTaskItem() {asChildtooltip="New Task"isActive={isLinkActive(pathname, "/copilot")}- className="h-auto rounded-xl p-2 pl-3 font-normal data-[active=true]:!bg-zinc-100 data-[active=true]:font-normal group-data-[collapsible=icon]:!p-1.5 hover:!bg-zinc-100 [&>svg]:size-4 group-data-[collapsible=icon]:[&>svg]:size-4.5"+ className={navItemClassName}><Link href="/copilot"><NewTaskIcon />@@ -131,6 +144,7 @@ function NavMenu({leading?: ReactNode;}) {const pathname = usePathname();+ const navItemClassName = useNavItemClassName();return (<SidebarMenu className="group-data-[collapsible=icon]:gap-1">@@ -141,7 +155,7 @@ function NavMenu({asChildtooltip={link.name}isActive={isLinkActive(pathname, link.href)}- className="h-auto rounded-xl p-2 pl-3 font-normal data-[active=true]:!bg-zinc-100 data-[active=true]:font-normal group-data-[collapsible=icon]:!p-1.5 hover:!bg-zinc-100 [&>svg]:size-4 group-data-[collapsible=icon]:[&>svg]:size-4.5"+ className={navItemClassName}><Link href={link.href}><Icon@@ -218,6 +232,7 @@ export function AppSidebar(props: Props) {const itemVariants = getSidebarItemVariants(!!reduceMotion);const router = useRouter();const isHireExpertsEnabled = useGetFlag(Flag.HIRE_EXPERTS);+ const isBrainDumpEnabled = useGetFlag(Flag.ONBOARDING_BRAIN_DUMP);const mainLinks = isHireExpertsEnabled? MAIN_LINKS.filter((link) => link.href !== "/library"): MAIN_LINKS;@@ -245,7 +260,11 @@ export function AppSidebar(props: Props) {<Sidebarcollapsible="icon"{...props}- className="[&_[data-sidebar=sidebar]]:bg-[#ffffff]"+ className={+ isBrainDumpEnabled+ ? "[&_[data-sidebar=sidebar]]:bg-[#F4F4F4]"+ : "[&_[data-sidebar=sidebar]]:bg-[#ffffff]"+ }><AppSidebarHeader />diff --git a/autogpt_platform/frontend/src/components/molecules/GlassOrb/GlassOrb.module.css b/autogpt_platform/frontend/src/components/molecules/GlassOrb/GlassOrb.module.cssnew file mode 100644index 0000000000..515189c11b--- /dev/null+++ b/autogpt_platform/frontend/src/components/molecules/GlassOrb/GlassOrb.module.css@@ -0,0 +1,137 @@+.blob {+ position: absolute;+ opacity: 0.5;+ filter: blur(8px);+}++.spinner {+ position: absolute;+ inset: 0;+ animation: spin 14s linear infinite;+}++.spinner1 {+ animation-duration: 12s;+}++.spinner2 {+ animation-duration: 19s;+ animation-direction: reverse;+}++.spinner3 {+ animation-duration: 15s;+}++.spinner4 {+ animation-duration: 24s;+ animation-direction: reverse;+}++.spinner5 {+ animation-duration: 9s;+}++@keyframes spin {+ from {+ transform: rotate(0deg);+ }+ to {+ transform: rotate(360deg);+ }+}++.blob1 {+ width: 78%;+ height: 46%;+ left: 6%;+ top: 10%;+ background: linear-gradient(120deg, #9c43fe, #5b8cff);+ border-radius: 78% 22% 58% 42% / 30% 72% 28% 70%;+ animation: drift1 9s ease-in-out infinite alternate;+}++.blob2 {+ width: 55%;+ height: 70%;+ left: 34%;+ top: 18%;+ background: linear-gradient(200deg, #7c5bff, #4062ff);+ border-radius: 30% 70% 25% 75% / 65% 28% 72% 35%;+ animation: drift2 11s ease-in-out infinite alternate;+}++.blob3 {+ width: 68%;+ height: 38%;+ left: 12%;+ top: 46%;+ background: linear-gradient(90deg, #b06bff, #6d7dff);+ border-radius: 85% 15% 70% 30% / 20% 80% 25% 75%;+ animation: drift3 7.5s ease-in-out infinite alternate;+}++.blob4 {+ width: 44%;+ height: 58%;+ left: 8%;+ top: 30%;+ background: linear-gradient(160deg, #8a4dff, #4d8bff);+ border-radius: 45% 55% 80% 20% / 75% 30% 70% 25%;+ animation: drift4 13s ease-in-out infinite alternate;+}++.blob5 {+ width: 60%;+ height: 34%;+ left: 26%;+ top: 58%;+ background: linear-gradient(45deg, #a35bff, #5470ff);+ border-radius: 20% 80% 35% 65% / 80% 25% 75% 20%;+ animation: drift5 8.5s ease-in-out infinite alternate;+}++@keyframes drift1 {+ from {+ transform: rotate(-10deg) translate(0%, 0%) scale(1);+ }+ to {+ transform: rotate(32deg) translate(8%, 12%) scale(1.12);+ }+}++@keyframes drift2 {+ from {+ transform: rotate(14deg) translate(0%, 0%) scale(1.05);+ }+ to {+ transform: rotate(-24deg) translate(-10%, 6%) scale(0.92);+ }+}++@keyframes drift3 {+ from {+ transform: rotate(4deg) translate(-4%, 0%) scale(0.95);+ }+ to {+ transform: rotate(-30deg) translate(10%, -12%) scale(1.15);+ }+}++@keyframes drift4 {+ from {+ transform: rotate(-22deg) translate(6%, -6%) scale(1.1);+ }+ to {+ transform: rotate(18deg) translate(-6%, 10%) scale(0.9);+ }+}++@keyframes drift5 {+ from {+ transform: rotate(26deg) translate(-8%, 4%) scale(1);+ }+ to {+ transform: rotate(-14deg) translate(6%, -8%) scale(1.18);+ }+}diff --git a/autogpt_platform/frontend/src/components/molecules/GlassOrb/GlassOrb.stories.tsx b/autogpt_platform/frontend/src/components/molecules/GlassOrb/GlassOrb.stories.tsxnew file mode 100644index 0000000000..59eab62e4c--- /dev/null+++ b/autogpt_platform/frontend/src/components/molecules/GlassOrb/GlassOrb.stories.tsx@@ -0,0 +1,49 @@+import type { Meta, StoryObj } from "@storybook/nextjs";+import { GlassOrb } from "./GlassOrb";++const meta: Meta<typeof GlassOrb> = {+ title: "Molecules/GlassOrb",+ component: GlassOrb,+ tags: ["autodocs"],+ parameters: {+ layout: "centered",+ docs: {+ description: {+ component:+ "A glass orb: distorted gradient blobs drifting over each other, sealed under an Aave-style frosted glass pane with refraction. Fills its parent, so it needs a relatively-positioned sized container.",+ },+ },+ },+ argTypes: {+ params: {+ control: "object",+ description:+ "Glass tuning knobs: frost, saturation, tint, edge, distortion, ringWidth, ringDepth, ringDark.",+ },+ },+ args: {+ params: {+ frost: 1.5,+ saturation: 1.5,+ tint: 0.12,+ edge: 0.55,+ distortion: 8,+ ringWidth: 1,+ ringDepth: 2,+ ringDark: 0.25,+ },+ },+};++export default meta;+type Story = StoryObj<typeof meta>;++export const Basic: Story = {+ render: function BasicStory(args) {+ return (+ <span className="relative block size-24">+ <GlassOrb {...args} />+ </span>+ );+ },+};diff --git a/autogpt_platform/frontend/src/components/molecules/GlassOrb/GlassOrb.tsx b/autogpt_platform/frontend/src/components/molecules/GlassOrb/GlassOrb.tsxnew file mode 100644index 0000000000..f4c14b9a00--- /dev/null+++ b/autogpt_platform/frontend/src/components/molecules/GlassOrb/GlassOrb.tsx@@ -0,0 +1,70 @@+"use client";++import { ReactNode } from "react";+import { GlassParams, GlassSurface } from "./GlassSurface";+import styles from "./GlassOrb.module.css";++// Orb v2: a stack of distorted gradient ellipses (banana / kidney / egg+// shapes, 0.5 opacity each) drifting over each other, sealed under an+// Aave-style glass pane. The displacement filter bends the blobs the way a+// real lens would (feDisplacementMap, per the Aave glass article).+interface Props {+ params: GlassParams;+ children?: ReactNode;+}++export function GlassOrb({ params, children }: Props) {+ return (+ <div className="relative h-full w-full" aria-hidden>+ <svg className="absolute h-0 w-0" aria-hidden>+ <filter id="glass-orb-refraction">+ <feTurbulence+ type="fractalNoise"+ baseFrequency="0.012 0.012"+ numOctaves="2"+ seed="7"+ result="noise"+ />+ <feDisplacementMap+ in="SourceGraphic"+ in2="noise"+ scale={params.distortion}+ xChannelSelector="R"+ yChannelSelector="G"+ />+ </filter>+ </svg>++ <div className="absolute inset-0 overflow-hidden rounded-full">+ <div+ className="absolute inset-0"+ style={{ filter: "url(#glass-orb-refraction)" }}+ >+ <div className={`${styles.spinner} ${styles.spinner1}`}>+ <div className={`${styles.blob} ${styles.blob1}`} />+ </div>+ <div className={`${styles.spinner} ${styles.spinner2}`}>+ <div className={`${styles.blob} ${styles.blob2}`} />+ </div>+ <div className={`${styles.spinner} ${styles.spinner3}`}>+ <div className={`${styles.blob} ${styles.blob3}`} />+ </div>+ <div className={`${styles.spinner} ${styles.spinner4}`}>+ <div className={`${styles.blob} ${styles.blob4}`} />+ </div>+ <div className={`${styles.spinner} ${styles.spinner5}`}>+ <div className={`${styles.blob} ${styles.blob5}`} />+ </div>+ </div>+ </div>++ <GlassSurface params={params} />++ {children && (+ <div className="pointer-events-none absolute inset-0 flex items-center justify-center">+ {children}+ </div>+ )}+ </div>+ );+}diff --git a/autogpt_platform/frontend/src/components/molecules/GlassOrb/GlassSurface.tsx b/autogpt_platform/frontend/src/components/molecules/GlassOrb/GlassSurface.tsxnew file mode 100644index 0000000000..e63ce5c709--- /dev/null+++ b/autogpt_platform/frontend/src/components/molecules/GlassOrb/GlassSurface.tsx@@ -0,0 +1,53 @@+"use client";++// Aave-style glass pane (aave.com/design/building-glass-for-the-web):+// frosted backdrop blur + saturation, gradient tint, rim highlights and a+// heavier frost band around the edge. All knobs exposed for the design lab.+export interface GlassParams {+ frost: number;+ saturation: number;+ tint: number;+ edge: number;+ distortion: number;+ ringWidth: number;+ ringDepth: number;+ ringDark: number;+}++export const DEFAULT_GLASS_PARAMS: GlassParams = {+ frost: 8,+ saturation: 1.4,+ tint: 0.35,+ edge: 0.8,+ distortion: 40,+ ringWidth: 2,+ ringDepth: 6,+ ringDark: 0.45,+};++export function GlassSurface({ params }: { params: GlassParams }) {+ const { frost, saturation, tint, edge } = params;+ const backdropFilter = `blur(${frost}px) saturate(${saturation})`;++ return (+ <>+ <div+ className="pointer-events-none absolute inset-0 rounded-full"+ style={{+ backdropFilter,+ WebkitBackdropFilter: backdropFilter,+ backgroundImage: `linear-gradient(155deg, rgba(255,255,255,${tint}), rgba(255,255,255,${tint * 0.2}) 48%, rgba(255,255,255,${tint * 0.6}))`,+ border: `1px solid rgba(255,255,255,${Math.min(edge, 1)})`,+ boxShadow: `inset 0 1px 3px rgba(255,255,255,${Math.min(edge * 1.2, 1)}), inset 0 -12px 28px rgba(255,255,255,${edge * 0.5}), 0 12px 40px rgba(96,64,224,0.18)`,+ }}+ />+ <div+ className="pointer-events-none absolute inset-0 rounded-full [mask-image:radial-gradient(closest-side,transparent_50%,black_80%)]"+ style={{+ backdropFilter: `blur(${frost * 2.5}px)`,+ WebkitBackdropFilter: `blur(${frost * 2.5}px)`,+ }}+ />+ </>+ );+}diff --git a/autogpt_platform/frontend/src/components/ui/text-generate-effect.tsx b/autogpt_platform/frontend/src/components/ui/text-generate-effect.tsxindex fbb9ee5d49..882cc5ee22 100644--- a/autogpt_platform/frontend/src/components/ui/text-generate-effect.tsx+++ b/autogpt_platform/frontend/src/components/ui/text-generate-effect.tsx@@ -8,11 +8,14 @@ export const TextGenerateEffect = ({className,filter = true,duration = 0.5,+ delay = 0,}: {words: string;className?: string;filter?: boolean;duration?: number;+ /** Seconds to wait before the first word starts revealing. */+ delay?: number;}) => {const [scope, animate] = useAnimate();const wordsArray = words.split(" ");@@ -25,7 +28,7 @@ export const TextGenerateEffect = ({},{duration: duration ? duration : 1,- delay: stagger(0.08),+ delay: stagger(0.08, { startDelay: delay }),},);}, [scope.current]);diff --git a/autogpt_platform/frontend/src/lib/autogpt-server-api/types.ts b/autogpt_platform/frontend/src/lib/autogpt-server-api/types.tsindex 1324f0d920..f5290b166d 100644--- a/autogpt_platform/frontend/src/lib/autogpt-server-api/types.ts+++ b/autogpt_platform/frontend/src/lib/autogpt-server-api/types.ts@@ -880,6 +880,7 @@ export type OnboardingStep =| "CONGRATS"// First Wins| "VISIT_COPILOT"+ | "CAPABILITY_CARDS"| "GET_RESULTS"| "MARKETPLACE_VISIT"| "MARKETPLACE_ADD_AGENT"diff --git a/autogpt_platform/frontend/src/mocks/mock-handlers.ts b/autogpt_platform/frontend/src/mocks/mock-handlers.tsindex a01b6e1c86..6f2ebd1655 100644--- a/autogpt_platform/frontend/src/mocks/mock-handlers.ts+++ b/autogpt_platform/frontend/src/mocks/mock-handlers.ts@@ -3,6 +3,11 @@ import { getAnalyticsMock } from "@/app/api/__generated__/endpoints/analytics/animport { getApiKeysMock } from "@/app/api/__generated__/endpoints/api-keys/api-keys.msw";import { getAuthMock } from "@/app/api/__generated__/endpoints/auth/auth.msw";import { getBlocksMock } from "@/app/api/__generated__/endpoints/blocks/blocks.msw";+import {+ getCompleteBrainDumpGreetingMockHandler200,+ getGetBrainDumpIntroMockHandler200,+ getGetBrainDumpRecommendedProvidersMockHandler200,+} from "@/app/api/__generated__/endpoints/brain-dump/brain-dump.msw";import { getChatMock } from "@/app/api/__generated__/endpoints/chat/chat.msw";import { getCreditsMock } from "@/app/api/__generated__/endpoints/credits/credits.msw";import { getDefaultMock } from "@/app/api/__generated__/endpoints/default/default.msw";@@ -31,6 +36,21 @@ export const mockHandlers = [...getApiKeysMock(),...getAuthMock(),...getBlocksMock(),+ // Greeting already done by default so the onboarding greeting stays out+ // of unrelated tests; greeting tests override with their own handler.+ getGetBrainDumpIntroMockHandler200({+ path: "A",+ greeting: "",+ prompts: [],+ greeting_done: true,+ }),+ getCompleteBrainDumpGreetingMockHandler200({ greeting_done: true }),+ // Ready by default so the preparing step's recommendation gate never+ // holds unrelated tests; gate tests override with ready: false.+ getGetBrainDumpRecommendedProvidersMockHandler200({+ ready: true,+ providers: [],+ }),...getChatMock(),...getCreditsMock(),...getDefaultMock(),diff --git a/autogpt_platform/frontend/src/services/feature-flags/__tests__/flag-defaults.test.ts b/autogpt_platform/frontend/src/services/feature-flags/__tests__/flag-defaults.test.tsindex b13869bcee..2db15e99ab 100644--- a/autogpt_platform/frontend/src/services/feature-flags/__tests__/flag-defaults.test.ts+++ b/autogpt_platform/frontend/src/services/feature-flags/__tests__/flag-defaults.test.ts@@ -46,3 +46,34 @@ describe("dream/graphiti flag defaults fail closed", () => {expect(result.current).toBe(true);});});++// These two have been flipped to `true` in the defaults map twice now, and+// both times it silently swapped the onboarding wizard for every+// LaunchDarkly-less environment — local dev, CI, Playwright — which is how+// auth-happy-path stopped finding the pillbox step.+const ONBOARDING_FLAGS = [+ Flag.ONBOARDING_BRAIN_DUMP,+ Flag.AUTOGPT_NEW_LAYOUT,+] as const;++describe("onboarding flag defaults fail closed", () => {+ beforeEach(() => {+ Object.keys(process.env)+ .filter((k) => k.startsWith("NEXT_PUBLIC_FORCE_FLAG_"))+ .forEach((k) => delete process.env[k]);+ });++ it.each(ONBOARDING_FLAGS)(+ "resolves %s to false when LaunchDarkly has not answered, so the default flow renders untouched",+ (flag) => {+ const { result } = renderHook(() => useGetFlag(flag));+ expect(result.current).toBe(false);+ },+ );++ it("still lets local dev force-enable the brain dump via the env override", () => {+ process.env.NEXT_PUBLIC_FORCE_FLAG_ONBOARDING_BRAIN_DUMP = "true";+ const { result } = renderHook(() => useGetFlag(Flag.ONBOARDING_BRAIN_DUMP));+ expect(result.current).toBe(true);+ });+});diff --git a/autogpt_platform/frontend/src/services/feature-flags/use-get-flag.ts b/autogpt_platform/frontend/src/services/feature-flags/use-get-flag.tsindex 99619b0e93..9d7475f3d4 100644--- a/autogpt_platform/frontend/src/services/feature-flags/use-get-flag.ts+++ b/autogpt_platform/frontend/src/services/feature-flags/use-get-flag.ts@@ -22,6 +22,10 @@ export enum Flag {CHAT_PINNING = "chat-pinning",TASK_PROGRESS_BAR = "task-progress-bar",HIRE_EXPERTS = "hire-experts",+ // Replaces the onboarding pillbox step with the voice brain dump.+ // Mirror of the backend ``Flag`` enum — the endpoints 404 when off, so+ // both sides must agree. Off renders the pillbox flow untouched.+ ONBOARDING_BRAIN_DUMP = "onboarding-brain-dump",// Graphiti memory + dream-system gates. Mirror of the backend// ``Flag`` enum in ``backend/util/feature_flag.py``. Frontend reads// them when memory/dream-related UI surfaces ship (P6+ on the@@ -61,6 +65,11 @@ const defaultFlags = {[Flag.CHAT_PINNING]: false,[Flag.TASK_PROGRESS_BAR]: false,[Flag.HIRE_EXPERTS]: false,+ // Off by default: with no LaunchDarkly key (local dev, CI, Playwright)+ // the wizard falls back to this map, and a ``true`` here renders the+ // brain dump for everyone — which is what the backend 404s are meant to+ // prevent. Use NEXT_PUBLIC_FORCE_FLAG_ONBOARDING_BRAIN_DUMP locally.+ [Flag.ONBOARDING_BRAIN_DUMP]: false,[Flag.GRAPHITI_MEMORY]: false,[Flag.GRAPHITI_COMMUNITIES_ENABLED]: false,[Flag.DREAM_PASS_ENABLED]: false,@@ -122,6 +131,8 @@ function readEnvOverride(flag: Flag): string | undefined {return process.env.NEXT_PUBLIC_FORCE_FLAG_TASK_PROGRESS_BAR;case Flag.HIRE_EXPERTS:return process.env.NEXT_PUBLIC_FORCE_FLAG_HIRE_EXPERTS;+ case Flag.ONBOARDING_BRAIN_DUMP:+ return process.env.NEXT_PUBLIC_FORCE_FLAG_ONBOARDING_BRAIN_DUMP;case Flag.GRAPHITI_MEMORY:return process.env.NEXT_PUBLIC_FORCE_FLAG_GRAPHITI_MEMORY;case Flag.GRAPHITI_COMMUNITIES_ENABLED:diff --git a/autogpt_platform/frontend/src/services/onboarding/__tests__/brain-dump-analytics.test.ts b/autogpt_platform/frontend/src/services/onboarding/__tests__/brain-dump-analytics.test.tsnew file mode 100644index 0000000000..a7687daf48--- /dev/null+++ b/autogpt_platform/frontend/src/services/onboarding/__tests__/brain-dump-analytics.test.ts@@ -0,0 +1,48 @@+import { beforeEach, describe, expect, it, vi } from "vitest";++const capture = vi.hoisted(() => vi.fn());+vi.mock("posthog-js", () => ({ default: { capture } }));++import { trackBrainDump } from "../brain-dump-analytics";++beforeEach(() => {+ capture.mockReset();+});++describe("trackBrainDump", () => {+ it("forwards the event name and properties to posthog", () => {+ trackBrainDump("brain_dump_completed", { duration_secs: 42 });++ expect(capture).toHaveBeenCalledWith("brain_dump_completed", {+ duration_secs: 42,+ });+ });++ it("passes undefined properties through rather than inventing an object", () => {+ trackBrainDump("brain_dump_started");++ expect(capture).toHaveBeenCalledWith("brain_dump_started", undefined);+ });++ it("swallows a throwing analytics host so a recording is never interrupted", () => {+ capture.mockImplementation(() => {+ throw new Error("posthog host blocked");+ });++ expect(() => trackBrainDump("brain_dump_permission_denied")).not.toThrow();+ });++ it("keeps capturing after a failed capture", () => {+ capture.mockImplementationOnce(() => {+ throw new Error("posthog host blocked");+ });++ trackBrainDump("brain_dump_retry");+ trackBrainDump("intro_followup_sent", { source: "suggestion" });++ expect(capture).toHaveBeenCalledTimes(2);+ expect(capture).toHaveBeenLastCalledWith("intro_followup_sent", {+ source: "suggestion",+ });+ });+});diff --git a/autogpt_platform/frontend/src/services/onboarding/__tests__/brain-dump-handoff.test.ts b/autogpt_platform/frontend/src/services/onboarding/__tests__/brain-dump-handoff.test.tsnew file mode 100644index 0000000000..fb3a42ecf9--- /dev/null+++ b/autogpt_platform/frontend/src/services/onboarding/__tests__/brain-dump-handoff.test.ts@@ -0,0 +1,188 @@+import { beforeEach, describe, expect, it } from "vitest";+import {+ clearWelcomePending,+ peekCapabilityCardsSeen,+ peekGreetingDone,+ peekIntroPath,+ peekWelcomePending,+ setCapabilityCardsSeen,+ setGreetingDone,+ setIntroAwaitingFollowup,+ setIntroPath,+ setMicGlow,+ setPendingLaterDump,+ setWelcomePending,+ takeIntroAwaitingFollowup,+ takeIntroPath,+ takeMicGlow,+ takePendingLaterDump,+} from "../brain-dump-handoff";++const INTRO_PATH_KEY = "autogpt:onboarding-intro-path";+const MIC_GLOW_KEY = "autogpt:onboarding-mic-glow";+const WELCOME_PENDING_KEY = "autogpt:onboarding-welcome-pending";+const GREETING_DONE_KEY = "autogpt:copilot-greeting-done";+const CAPABILITY_CARDS_KEY = "autogpt:copilot-capability-cards-seen";++beforeEach(() => {+ window.sessionStorage.clear();+ window.localStorage.clear();+});++describe("intro path handoff", () => {+ it("round-trips the path the wizard recorded", () => {+ setIntroPath("A");++ expect(peekIntroPath()).toBe("A");+ });++ it("peeking leaves the path in place so a re-render still sees it", () => {+ setIntroPath("B");++ expect(peekIntroPath()).toBe("B");+ expect(peekIntroPath()).toBe("B");+ expect(window.sessionStorage.getItem(INTRO_PATH_KEY)).toBe("B");+ });++ it("taking the path clears it so a refresh cannot replay the intro", () => {+ setIntroPath("A");++ expect(takeIntroPath()).toBe("A");+ expect(takeIntroPath()).toBeNull();+ expect(window.sessionStorage.getItem(INTRO_PATH_KEY)).toBeNull();+ });++ it("returns null for a stored value that is not a known path", () => {+ window.sessionStorage.setItem(INTRO_PATH_KEY, "C");++ expect(peekIntroPath()).toBeNull();+ expect(takeIntroPath()).toBeNull();+ });++ it("taking an absent path is a no-op rather than a write", () => {+ expect(takeIntroPath()).toBeNull();+ expect(window.sessionStorage.getItem(INTRO_PATH_KEY)).toBeNull();+ });+});++describe("one-shot session flags", () => {+ it("mic glow is consumed by the first take", () => {+ setMicGlow();++ expect(takeMicGlow()).toBe(true);+ expect(takeMicGlow()).toBe(false);+ expect(window.sessionStorage.getItem(MIC_GLOW_KEY)).toBeNull();+ });++ it("mic glow is false when it was never set", () => {+ expect(takeMicGlow()).toBe(false);+ });++ it("intro-awaiting-followup is consumed by the first take", () => {+ setIntroAwaitingFollowup();++ expect(takeIntroAwaitingFollowup()).toBe(true);+ expect(takeIntroAwaitingFollowup()).toBe(false);+ });++ it("pending-later-dump is consumed by the first take", () => {+ setPendingLaterDump();++ expect(takePendingLaterDump()).toBe(true);+ expect(takePendingLaterDump()).toBe(false);+ });++ it("keeps the three flags independent of each other", () => {+ setMicGlow();++ expect(takeIntroAwaitingFollowup()).toBe(false);+ expect(takePendingLaterDump()).toBe(false);+ expect(takeMicGlow()).toBe(true);+ });+});++describe("welcome pending", () => {+ it("peeking does not consume it — the overlay survives a refresh", () => {+ setWelcomePending();++ expect(peekWelcomePending()).toBe(true);+ expect(peekWelcomePending()).toBe(true);+ });++ it("is only cleared deliberately", () => {+ setWelcomePending();+ clearWelcomePending();++ expect(peekWelcomePending()).toBe(false);+ expect(window.sessionStorage.getItem(WELCOME_PENDING_KEY)).toBeNull();+ });++ it("lives in sessionStorage, so it does not survive a new browser session", () => {+ setWelcomePending();++ expect(window.localStorage.getItem(WELCOME_PENDING_KEY)).toBeNull();+ window.sessionStorage.clear();+ expect(peekWelcomePending()).toBe(false);+ });++ it("clearing when nothing is pending is harmless", () => {+ clearWelcomePending();++ expect(peekWelcomePending()).toBe(false);+ });+});++describe("greeting done", () => {+ it("is stored against the user id, not as a boolean", () => {+ setGreetingDone("user-1");++ expect(window.localStorage.getItem(GREETING_DONE_KEY)).toBe("user-1");+ expect(peekGreetingDone("user-1")).toBe(true);+ });++ it("does not leak to a different account on the same browser", () => {+ setGreetingDone("user-1");++ expect(peekGreetingDone("user-2")).toBe(false);+ });++ it("ignores a missing user id on both read and write", () => {+ setGreetingDone(null);+ setGreetingDone(undefined);++ expect(window.localStorage.getItem(GREETING_DONE_KEY)).toBeNull();+ expect(peekGreetingDone(null)).toBe(false);+ expect(peekGreetingDone(undefined)).toBe(false);+ });++ it("survives a new session because it is kept in localStorage", () => {+ setGreetingDone("user-1");+ window.sessionStorage.clear();++ expect(peekGreetingDone("user-1")).toBe(true);+ });+});++describe("capability cards seen", () => {+ it("is stored against the user id", () => {+ setCapabilityCardsSeen("user-1");++ expect(window.localStorage.getItem(CAPABILITY_CARDS_KEY)).toBe("user-1");+ expect(peekCapabilityCardsSeen("user-1")).toBe(true);+ expect(peekCapabilityCardsSeen("user-2")).toBe(false);+ });++ it("ignores a missing user id on both read and write", () => {+ setCapabilityCardsSeen(null);++ expect(window.localStorage.getItem(CAPABILITY_CARDS_KEY)).toBeNull();+ expect(peekCapabilityCardsSeen(null)).toBe(false);+ expect(peekCapabilityCardsSeen(undefined)).toBe(false);+ });++ it("is tracked separately from the greeting flag", () => {+ setCapabilityCardsSeen("user-1");++ expect(peekGreetingDone("user-1")).toBe(false);+ });+});diff --git a/autogpt_platform/frontend/src/services/onboarding/brain-dump-analytics.ts b/autogpt_platform/frontend/src/services/onboarding/brain-dump-analytics.tsnew file mode 100644index 0000000000..e7c666109f--- /dev/null+++ b/autogpt_platform/frontend/src/services/onboarding/brain-dump-analytics.ts@@ -0,0 +1,56 @@+// Brain-dump funnel events. These are the release-blocker instrumentation:+// completion rate, dump length distribution, and every way a dump can go+// wrong. Capture is best-effort — a blocked analytics host must never+// interrupt someone mid-recording.+//+// Lives in services/ rather than beside the onboarding step because the+// copilot home fires the tail of this funnel (the intro and the follow-up+// dump), and a feature folder reaching into another feature's internals+// is how import graphs rot.++import posthog from "posthog-js";++type BrainDumpEvent =+ | "brain_dump_started"+ | "brain_dump_completed"+ | "brain_dump_skipped"+ | "brain_dump_recovery_shown"+ | "brain_dump_recovery_used"+ | "brain_dump_retry"+ | "brain_dump_restarted"+ | "brain_dump_download"+ | "brain_dump_permission_denied"+ | "brain_dump_typed_fallback"+ // Wall-clock of the whole finalize round trip — upload flush, virus+ // scan, storage, transcription and extraction. Named for what it+ // actually measures: the client cannot see the transcription step on+ // its own, so calling this "transcription latency" overstated it.+ | "finalize_latency_ms"+ | "intro_card_dismissed"+ // The welcome dialog shown on first copilot landing was closed — the+ // greeting fetch and reveal animation start from this moment.+ | "welcome_dialog_closed"+ // Capability-cards first-run funnel: which cards were reached and how+ // the modal ended (finished the deck vs skipped at card_index).+ | "capability_card_viewed"+ | "capability_cards_completed"+ | "capability_cards_skipped"+ | "transcription_failed"+ | "intro_path"+ // The user's first real message after seeing the intro card — the+ // signal that the card actually started a conversation. The suggested+ // prompts rendered beneath it are personalised from the same dump, so+ // this covers both a suggestion click and a typed reply.+ | "intro_followup_sent"+ | "later_dump_completed";++export function trackBrainDump(+ event: BrainDumpEvent,+ properties?: Record<string, unknown>,+) {+ try {+ posthog.capture(event, properties);+ } catch {+ // Analytics is never worth a broken recording.+ }+}diff --git a/autogpt_platform/frontend/src/services/onboarding/brain-dump-handoff.ts b/autogpt_platform/frontend/src/services/onboarding/brain-dump-handoff.tsnew file mode 100644index 0000000000..df6bd8e1c0--- /dev/null+++ b/autogpt_platform/frontend/src/services/onboarding/brain-dump-handoff.ts@@ -0,0 +1,134 @@+// Handoff between the onboarding wizard and the copilot home.+//+// The wizard records which path the user took; the copilot home consumes+// it exactly once and asks the server for the matching intro. Only the+// path letter travels — the transcript stays on the server, which is the+// point of composing the prompt there.++const INTRO_PATH_KEY = "autogpt:onboarding-intro-path";+const MIC_GLOW_KEY = "autogpt:onboarding-mic-glow";+const AWAITING_FOLLOWUP_KEY = "autogpt:onboarding-intro-awaiting-followup";+const PENDING_LATER_DUMP_KEY = "autogpt:onboarding-pending-later-dump";++export type IntroPath = "A" | "B";++// Path B ends with AutoPilot asking for a dump, so the mic it points at+// gets a one-time highlight.+export function setMicGlow() {+ setFlag(MIC_GLOW_KEY);+}++export function takeMicGlow() {+ return takeFlag(MIC_GLOW_KEY);+}++// Set when AutoPilot's intro goes out, consumed by the user's first real+// message afterwards. Measures whether the intro actually started a+// conversation.+export function setIntroAwaitingFollowup() {+ setFlag(AWAITING_FOLLOWUP_KEY);+}++export function takeIntroAwaitingFollowup() {+ return takeFlag(AWAITING_FOLLOWUP_KEY);+}++// Path B only: the user skipped the dump, so AutoPilot invited them to+// record one from the copilot composer instead. Consumed the first time+// they finish a voice message there.+export function setPendingLaterDump() {+ setFlag(PENDING_LATER_DUMP_KEY);+}++export function takePendingLaterDump() {+ return takeFlag(PENDING_LATER_DUMP_KEY);+}++// Set when the user leaves onboarding for the copilot home, cleared when+// they close the full-screen welcome overlay there. Refresh-safe: the+// overlay must keep covering the page (and keep the greeting unfetched)+// until it is deliberately dismissed.+const WELCOME_PENDING_KEY = "autogpt:onboarding-welcome-pending";++export function setWelcomePending() {+ setFlag(WELCOME_PENDING_KEY);+}++export function peekWelcomePending() {+ if (typeof window === "undefined") return false;+ return window.sessionStorage.getItem(WELCOME_PENDING_KEY) === "1";+}++export function clearWelcomePending() {+ if (typeof window === "undefined") return;+ window.sessionStorage.removeItem(WELCOME_PENDING_KEY);+}++// The capability-cards first-run modal was completed or skipped. The+// backend's CAPABILITY_CARDS onboarding step is the source of truth;+// this is its local cache so the modal never reshows even if that call+// failed or hasn't been checked. Value is the user id, same reasoning+// as the greeting flag below.+const CAPABILITY_CARDS_KEY = "autogpt:copilot-capability-cards-seen";++export function peekCapabilityCardsSeen(userId: string | null | undefined) {+ if (typeof window === "undefined" || !userId) return false;+ return window.localStorage.getItem(CAPABILITY_CARDS_KEY) === userId;+}++export function setCapabilityCardsSeen(userId: string | null | undefined) {+ if (typeof window === "undefined" || !userId) return;+ window.localStorage.setItem(CAPABILITY_CARDS_KEY, userId);+}++// The greeting is retired for good the first time the user sends a+// message. localStorage rather than sessionStorage because it must+// survive new tabs and restarts; the backend flag is the source of+// truth and this is only its cache — absence means "ask the server".+// The stored value is the user id, not a boolean: a different account+// signing in on the same browser must not inherit the previous+// account's "done" and silently skip its greeting.+const GREETING_DONE_KEY = "autogpt:copilot-greeting-done";++export function peekGreetingDone(userId: string | null | undefined) {+ if (typeof window === "undefined" || !userId) return false;+ return window.localStorage.getItem(GREETING_DONE_KEY) === userId;+}++export function setGreetingDone(userId: string | null | undefined) {+ if (typeof window === "undefined" || !userId) return;+ window.localStorage.setItem(GREETING_DONE_KEY, userId);+}++function setFlag(key: string) {+ if (typeof window === "undefined") return;+ window.sessionStorage.setItem(key, "1");+}++function takeFlag(key: string) {+ if (typeof window === "undefined") return false;+ const value = window.sessionStorage.getItem(key) === "1";+ if (value) window.sessionStorage.removeItem(key);+ return value;+}++export function setIntroPath(path: IntroPath) {+ if (typeof window === "undefined") return;+ window.sessionStorage.setItem(INTRO_PATH_KEY, path);+}++export function peekIntroPath(): IntroPath | null {+ if (typeof window === "undefined") return null;+ const raw = window.sessionStorage.getItem(INTRO_PATH_KEY);+ return raw === "A" || raw === "B" ? raw : null;+}++// Read-and-clear: the intro is a one-time event, and a refresh of the+// copilot home must not replay it.+export function takeIntroPath(): IntroPath | null {+ const path = peekIntroPath();+ if (path && typeof window !== "undefined") {+ window.sessionStorage.removeItem(INTRO_PATH_KEY);+ }+ return path;+}