@agent-native/core 0.111.4 → 0.113.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/corpus/README.md +4 -2
- package/corpus/core/CHANGELOG.md +34 -0
- package/corpus/core/docs/AGENTS.md +7 -0
- package/corpus/core/docs/content/integrations.mdx +63 -2
- package/corpus/core/docs/design/provider-integration-authority.md +117 -0
- package/corpus/core/package.json +8 -1
- package/corpus/core/scripts/materialize-source-corpus.mjs +9 -2
- package/corpus/core/src/cli/atomic-json-file.ts +324 -0
- package/corpus/core/src/cli/claude-code-participant.ts +515 -0
- package/corpus/core/src/cli/code-agent-executor.ts +35 -15
- package/corpus/core/src/cli/code-agent-runs.concurrent-worker.ts +33 -0
- package/corpus/core/src/cli/code-agent-runs.ts +70 -36
- package/corpus/core/src/cli/codex-cli-participant.ts +496 -0
- package/corpus/core/src/cli/multi-frontier-runs.ts +1952 -0
- package/corpus/core/src/client/chat/ChatHistoryList.tsx +7 -404
- package/corpus/core/src/coding-tools/index.ts +27 -8
- package/corpus/core/src/embeddings/index.ts +233 -0
- package/corpus/core/src/index.ts +2 -0
- package/corpus/core/src/integrations/index.ts +10 -0
- package/corpus/core/src/integrations/slack-oauth.ts +1 -0
- package/corpus/core/src/integrations/webhook-delivery.ts +55 -0
- package/corpus/core/src/notifications/channels.ts +4 -10
- package/corpus/core/src/provider-api/actions/custom-provider-registration.ts +197 -0
- package/corpus/core/src/provider-api/actions/delete-staged-dataset.ts +3 -42
- package/corpus/core/src/provider-api/actions/list-staged-datasets.ts +3 -45
- package/corpus/core/src/provider-api/actions/provider-api-audit.ts +88 -0
- package/corpus/core/src/provider-api/actions/provider-api.ts +460 -0
- package/corpus/core/src/provider-api/actions/query-staged-dataset.ts +3 -140
- package/corpus/core/src/provider-api/actions/staged-datasets.ts +359 -0
- package/corpus/core/src/provider-api/gong.ts +108 -0
- package/corpus/core/src/scripts/docs/source-search.ts +2 -1
- package/corpus/core/src/search/index.ts +413 -0
- package/corpus/core/src/server/google-oauth-credentials.ts +80 -3
- package/corpus/core/src/server/index.ts +6 -0
- package/corpus/core/src/styles/chat-history-list.css +1 -313
- package/corpus/core/src/templates/default/.agents/skills/actions/SKILL.md +32 -12
- package/corpus/core/src/templates/default/.agents/skills/agent-native-docs/SKILL.md +10 -0
- package/corpus/core/src/templates/default/.agents/skills/customizing-agent-native/SKILL.md +102 -0
- package/corpus/core/src/templates/default/.agents/skills/integration-webhooks/SKILL.md +323 -0
- package/corpus/core/src/templates/default/.agents/skills/onboarding/SKILL.md +56 -0
- package/corpus/core/src/templates/default/.agents/skills/secrets/SKILL.md +273 -0
- package/corpus/core/src/templates/default/.agents/skills/self-modifying-code/SKILL.md +11 -2
- package/corpus/core/src/templates/default/.agents/skills/upgrade-agent-native/SKILL.md +6 -0
- package/corpus/core/src/templates/default/AGENTS.md +25 -18
- package/corpus/core/src/templates/headless/.agents/skills/actions/SKILL.md +512 -0
- package/corpus/core/src/templates/headless/.agents/skills/agent-native-docs/SKILL.md +10 -0
- package/corpus/core/src/templates/headless/.agents/skills/customizing-agent-native/SKILL.md +102 -0
- package/corpus/core/src/templates/headless/.agents/skills/integration-webhooks/SKILL.md +323 -0
- package/corpus/core/src/templates/headless/.agents/skills/secrets/SKILL.md +273 -0
- package/corpus/core/src/templates/headless/.agents/skills/upgrade-agent-native/SKILL.md +6 -0
- package/corpus/core/src/templates/headless/AGENTS.md +7 -0
- package/corpus/core/src/templates/workspace-core/.agents/skills/actions/SKILL.md +32 -12
- package/corpus/core/src/templates/workspace-core/.agents/skills/agent-native-docs/SKILL.md +10 -0
- package/corpus/core/src/templates/workspace-core/.agents/skills/agent-native-toolkit/SKILL.md +6 -0
- package/corpus/core/src/templates/workspace-core/.agents/skills/customizing-agent-native/SKILL.md +102 -0
- package/corpus/core/src/templates/workspace-core/.agents/skills/self-modifying-code/SKILL.md +11 -2
- package/corpus/core/src/templates/workspace-core/.agents/skills/upgrade-agent-native/SKILL.md +6 -0
- package/corpus/core/src/templates/workspace-core/AGENTS.md +4 -0
- package/corpus/core/src/templates/workspace-root/AGENTS.md +4 -0
- package/corpus/templates/analytics/.agents/skills/actions/SKILL.md +32 -12
- package/corpus/templates/analytics/.agents/skills/customizing-agent-native/SKILL.md +102 -0
- package/corpus/templates/analytics/.agents/skills/self-modifying-code/SKILL.md +11 -2
- package/corpus/templates/analytics/.agents/skills/upgrade-agent-native/SKILL.md +6 -0
- package/corpus/templates/analytics/actions/delete-staged-dataset.ts +3 -23
- package/corpus/templates/analytics/actions/list-staged-datasets.ts +3 -27
- package/corpus/templates/analytics/actions/provider-api-catalog.ts +16 -19
- package/corpus/templates/analytics/actions/provider-api-docs.ts +3 -5
- package/corpus/templates/analytics/actions/provider-api-request.ts +162 -185
- package/corpus/templates/analytics/actions/query-staged-dataset.ts +3 -47
- package/corpus/templates/analytics/app/components/layout/Sidebar.tsx +52 -161
- package/corpus/templates/assets/.agents/skills/actions/SKILL.md +32 -12
- package/corpus/templates/assets/.agents/skills/customizing-agent-native/SKILL.md +102 -0
- package/corpus/templates/assets/.agents/skills/self-modifying-code/SKILL.md +11 -2
- package/corpus/templates/assets/.agents/skills/upgrade-agent-native/SKILL.md +6 -0
- package/corpus/templates/assets/app/components/layout/Sidebar.tsx +63 -160
- package/corpus/templates/brain/.agents/skills/actions/SKILL.md +32 -12
- package/corpus/templates/brain/.agents/skills/brain/RUNBOOK.md +48 -5
- package/corpus/templates/brain/.agents/skills/brain/SKILL.md +46 -14
- package/corpus/templates/brain/.agents/skills/customizing-agent-native/SKILL.md +102 -0
- package/corpus/templates/brain/.agents/skills/self-modifying-code/SKILL.md +11 -2
- package/corpus/templates/brain/.agents/skills/upgrade-agent-native/SKILL.md +6 -0
- package/corpus/templates/brain/README.md +34 -3
- package/corpus/templates/brain/actions/_schemas.ts +13 -7
- package/corpus/templates/brain/actions/approve-proposal.ts +2 -0
- package/corpus/templates/brain/actions/claim-distillation.ts +24 -27
- package/corpus/templates/brain/actions/create-source.ts +1 -1
- package/corpus/templates/brain/actions/delete-staged-dataset.ts +3 -24
- package/corpus/templates/brain/actions/get-knowledge.ts +6 -2
- package/corpus/templates/brain/actions/get-pilot-report.ts +44 -8
- package/corpus/templates/brain/actions/import-capture.ts +21 -9
- package/corpus/templates/brain/actions/import-transcript.ts +42 -14
- package/corpus/templates/brain/actions/list-captures.ts +10 -1
- package/corpus/templates/brain/actions/list-distillation-queue.ts +39 -14
- package/corpus/templates/brain/actions/list-projects.ts +51 -0
- package/corpus/templates/brain/actions/list-proposals.ts +80 -4
- package/corpus/templates/brain/actions/list-sources.ts +17 -3
- package/corpus/templates/brain/actions/list-staged-datasets.ts +3 -28
- package/corpus/templates/brain/actions/manage-project.ts +118 -0
- package/corpus/templates/brain/actions/mark-capture-distilled.ts +68 -20
- package/corpus/templates/brain/actions/navigate.ts +1 -0
- package/corpus/templates/brain/actions/provider-api-catalog.ts +5 -11
- package/corpus/templates/brain/actions/provider-api-docs.ts +3 -5
- package/corpus/templates/brain/actions/provider-api-request.ts +3 -37
- package/corpus/templates/brain/actions/query-staged-dataset.ts +3 -48
- package/corpus/templates/brain/actions/reject-proposal.ts +6 -1
- package/corpus/templates/brain/actions/resanitize-captures.ts +159 -33
- package/corpus/templates/brain/actions/review-proposal.ts +2 -0
- package/corpus/templates/brain/actions/search-everything.ts +10 -1
- package/corpus/templates/brain/actions/search-knowledge.ts +1 -1
- package/corpus/templates/brain/actions/set-settings.ts +36 -1
- package/corpus/templates/brain/actions/update-proposal.ts +2 -0
- package/corpus/templates/brain/actions/update-source.ts +5 -1
- package/corpus/templates/brain/app/components/layout/Sidebar.tsx +48 -154
- package/corpus/templates/brain/app/hooks/use-distillation-bridge.ts +18 -9
- package/corpus/templates/brain/app/hooks/use-navigation-state.ts +3 -0
- package/corpus/templates/brain/app/i18n-data.ts +731 -2
- package/corpus/templates/brain/app/lib/brain.ts +88 -1
- package/corpus/templates/brain/app/lib/ingest-handoff.ts +35 -0
- package/corpus/templates/brain/app/routes/ops.tsx +47 -0
- package/corpus/templates/brain/app/routes/review.tsx +41 -2
- package/corpus/templates/brain/app/routes/search.tsx +33 -2
- package/corpus/templates/brain/app/routes/settings.tsx +158 -0
- package/corpus/templates/brain/app/routes/sources.tsx +31 -0
- package/corpus/templates/brain/changelog/2026-07-19-added-private-source-safe-semantic-search.md +6 -0
- package/corpus/templates/brain/changelog/2026-07-19-slack-backfills-can-now-join-configured-public-channels-befo.md +6 -0
- package/corpus/templates/brain/changelog/2026-07-19-slack-backfills-now-continue-across-the-configured-number-of.md +6 -0
- package/corpus/templates/brain/changelog/2026-07-19-slack-backfills-now-start-with-a-safe-four-week-window-and-r.md +6 -0
- package/corpus/templates/brain/jobs/process-ingest-queue.ts +270 -36
- package/corpus/templates/brain/package.json +1 -0
- package/corpus/templates/brain/server/db/index.ts +18 -0
- package/corpus/templates/brain/server/db/schema.ts +197 -1
- package/corpus/templates/brain/server/jobs/distillation-queue.ts +12 -5
- package/corpus/templates/brain/server/lib/audiences.ts +708 -0
- package/corpus/templates/brain/server/lib/brain-health.ts +251 -38
- package/corpus/templates/brain/server/lib/brain.ts +877 -50
- package/corpus/templates/brain/server/lib/capture-sanitization.ts +217 -53
- package/corpus/templates/brain/server/lib/connectors.ts +609 -72
- package/corpus/templates/brain/server/lib/demo.ts +17 -4
- package/corpus/templates/brain/server/lib/hybrid-search.ts +363 -0
- package/corpus/templates/brain/server/lib/ingest-queue.ts +104 -0
- package/corpus/templates/brain/server/lib/privacy-readiness.ts +24 -0
- package/corpus/templates/brain/server/lib/search-index-contracts.ts +72 -0
- package/corpus/templates/brain/server/lib/search-index.ts +714 -0
- package/corpus/templates/brain/server/lib/search.ts +120 -4
- package/corpus/templates/brain/server/lib/sensitivity-policy.ts +174 -0
- package/corpus/templates/brain/server/lib/slack-channel-membership.ts +44 -0
- package/corpus/templates/brain/server/lib/slack-events.ts +281 -0
- package/corpus/templates/brain/server/onboarding.ts +29 -0
- package/corpus/templates/brain/server/plugins/db.ts +204 -0
- package/corpus/templates/brain/server/register-secrets.ts +46 -0
- package/corpus/templates/brain/server/routes/api/_agent-native/brain/ingest.post.ts +36 -16
- package/corpus/templates/brain/server/routes/api/_agent-native/brain/slack-events.post.ts +28 -0
- package/corpus/templates/brain/shared/slack-source-config.ts +60 -0
- package/corpus/templates/brain/shared/types.ts +10 -0
- package/corpus/templates/calendar/.agents/skills/actions/SKILL.md +32 -12
- package/corpus/templates/calendar/.agents/skills/customizing-agent-native/SKILL.md +102 -0
- package/corpus/templates/calendar/.agents/skills/self-modifying-code/SKILL.md +11 -2
- package/corpus/templates/calendar/.agents/skills/upgrade-agent-native/SKILL.md +6 -0
- package/corpus/templates/calendar/actions/delete-staged-dataset.ts +3 -24
- package/corpus/templates/calendar/actions/list-staged-datasets.ts +3 -28
- package/corpus/templates/calendar/actions/provider-api-catalog.ts +5 -11
- package/corpus/templates/calendar/actions/provider-api-docs.ts +3 -5
- package/corpus/templates/calendar/actions/provider-api-request.ts +3 -37
- package/corpus/templates/calendar/actions/query-staged-dataset.ts +3 -48
- package/corpus/templates/calendar/server/handlers/gong.ts +6 -61
- package/corpus/templates/calendar/server/handlers/google-auth.ts +10 -8
- package/corpus/templates/calendar/server/lib/google-calendar.ts +7 -45
- package/corpus/templates/chat/.agents/skills/actions/SKILL.md +32 -12
- package/corpus/templates/chat/.agents/skills/agent-native-docs/SKILL.md +10 -0
- package/corpus/templates/chat/.agents/skills/customizing-agent-native/SKILL.md +102 -0
- package/corpus/templates/chat/.agents/skills/self-modifying-code/SKILL.md +11 -2
- package/corpus/templates/chat/.agents/skills/upgrade-agent-native/SKILL.md +6 -0
- package/corpus/templates/chat/AGENTS.md +5 -2
- package/corpus/templates/chat/app/components/layout/Sidebar.tsx +51 -157
- package/corpus/templates/clips/.agents/skills/actions/SKILL.md +32 -12
- package/corpus/templates/clips/.agents/skills/customizing-agent-native/SKILL.md +102 -0
- package/corpus/templates/clips/.agents/skills/self-modifying-code/SKILL.md +11 -2
- package/corpus/templates/clips/.agents/skills/upgrade-agent-native/SKILL.md +6 -0
- package/corpus/templates/clips/AGENTS.md +11 -0
- package/corpus/templates/clips/actions/export-to-brain.ts +505 -171
- package/corpus/templates/clips/changelog/2026-07-19-ready-clips-transcripts-can-be-backfilled-to-brain-for-a-bou.md +6 -0
- package/corpus/templates/clips/server/lib/calendar-event-meetings.ts +1 -1
- package/corpus/templates/clips/server/lib/google-calendar-client.ts +12 -22
- package/corpus/templates/clips/server/lib/google-calendar-oauth.ts +9 -6
- package/corpus/templates/clips/server/lib/slack-oauth.ts +12 -47
- package/corpus/templates/clips/server/routes/_agent-native/google/auth-url.get.ts +2 -6
- package/corpus/templates/clips/server/routes/api/auth/google-calendar.get.ts +4 -4
- package/corpus/templates/content/.agents/skills/actions/SKILL.md +32 -12
- package/corpus/templates/content/.agents/skills/customizing-agent-native/SKILL.md +102 -0
- package/corpus/templates/content/.agents/skills/self-modifying-code/SKILL.md +11 -2
- package/corpus/templates/content/.agents/skills/upgrade-agent-native/SKILL.md +6 -0
- package/corpus/templates/content/actions/delete-staged-dataset.ts +3 -24
- package/corpus/templates/content/actions/list-staged-datasets.ts +3 -28
- package/corpus/templates/content/actions/provider-api-catalog.ts +5 -11
- package/corpus/templates/content/actions/provider-api-docs.ts +3 -5
- package/corpus/templates/content/actions/provider-api-request.ts +3 -37
- package/corpus/templates/content/actions/query-staged-dataset.ts +3 -48
- package/corpus/templates/design/.agents/skills/actions/SKILL.md +32 -12
- package/corpus/templates/design/.agents/skills/customizing-agent-native/SKILL.md +102 -0
- package/corpus/templates/design/.agents/skills/self-modifying-code/SKILL.md +11 -2
- package/corpus/templates/design/.agents/skills/upgrade-agent-native/SKILL.md +6 -0
- package/corpus/templates/design/actions/delete-staged-dataset.ts +4 -23
- package/corpus/templates/design/actions/list-staged-datasets.ts +4 -27
- package/corpus/templates/design/actions/provider-api-catalog.ts +15 -18
- package/corpus/templates/design/actions/provider-api-docs.ts +57 -48
- package/corpus/templates/design/actions/provider-api-request.ts +140 -164
- package/corpus/templates/design/actions/query-staged-dataset.ts +4 -47
- package/corpus/templates/dispatch/.agents/skills/actions/SKILL.md +32 -12
- package/corpus/templates/dispatch/.agents/skills/customizing-agent-native/SKILL.md +102 -0
- package/corpus/templates/dispatch/.agents/skills/self-modifying-code/SKILL.md +11 -2
- package/corpus/templates/dispatch/.agents/skills/upgrade-agent-native/SKILL.md +6 -0
- package/corpus/templates/dispatch/actions/delete-staged-dataset.ts +4 -23
- package/corpus/templates/dispatch/actions/list-staged-datasets.ts +4 -30
- package/corpus/templates/dispatch/actions/provider-api-catalog.ts +15 -18
- package/corpus/templates/dispatch/actions/provider-api-docs.ts +57 -48
- package/corpus/templates/dispatch/actions/provider-api-request.ts +138 -163
- package/corpus/templates/dispatch/actions/query-staged-dataset.ts +4 -47
- package/corpus/templates/forms/.agents/skills/actions/SKILL.md +32 -12
- package/corpus/templates/forms/.agents/skills/customizing-agent-native/SKILL.md +102 -0
- package/corpus/templates/forms/.agents/skills/self-modifying-code/SKILL.md +11 -2
- package/corpus/templates/forms/.agents/skills/upgrade-agent-native/SKILL.md +6 -0
- package/corpus/templates/forms/server/lib/integrations.ts +23 -22
- package/corpus/templates/macros/.agents/skills/customizing-agent-native/SKILL.md +102 -0
- package/corpus/templates/macros/.agents/skills/upgrade-agent-native/SKILL.md +6 -0
- package/corpus/templates/mail/.agents/skills/actions/SKILL.md +32 -12
- package/corpus/templates/mail/.agents/skills/customizing-agent-native/SKILL.md +102 -0
- package/corpus/templates/mail/.agents/skills/self-modifying-code/SKILL.md +11 -2
- package/corpus/templates/mail/.agents/skills/upgrade-agent-native/SKILL.md +6 -0
- package/corpus/templates/mail/actions/delete-staged-dataset.ts +4 -23
- package/corpus/templates/mail/actions/list-staged-datasets.ts +4 -27
- package/corpus/templates/mail/actions/provider-api-catalog.ts +15 -18
- package/corpus/templates/mail/actions/provider-api-docs.ts +57 -48
- package/corpus/templates/mail/actions/provider-api-request.ts +146 -173
- package/corpus/templates/mail/actions/query-staged-dataset.ts +4 -47
- package/corpus/templates/mail/server/handlers/gong.ts +6 -61
- package/corpus/templates/mail/server/lib/google-auth.ts +10 -13
- package/corpus/templates/plan/.agents/skills/actions/SKILL.md +32 -12
- package/corpus/templates/plan/.agents/skills/customizing-agent-native/SKILL.md +102 -0
- package/corpus/templates/plan/.agents/skills/self-modifying-code/SKILL.md +11 -2
- package/corpus/templates/plan/.agents/skills/upgrade-agent-native/SKILL.md +6 -0
- package/corpus/templates/plan/app/components/layout/Sidebar.tsx +47 -156
- package/corpus/templates/slides/.agents/skills/actions/SKILL.md +32 -12
- package/corpus/templates/slides/.agents/skills/customizing-agent-native/SKILL.md +102 -0
- package/corpus/templates/slides/.agents/skills/self-modifying-code/SKILL.md +11 -2
- package/corpus/templates/slides/.agents/skills/upgrade-agent-native/SKILL.md +6 -0
- package/corpus/templates/slides/actions/delete-staged-dataset.ts +4 -23
- package/corpus/templates/slides/actions/list-staged-datasets.ts +4 -27
- package/corpus/templates/slides/actions/provider-api-catalog.ts +15 -18
- package/corpus/templates/slides/actions/provider-api-docs.ts +57 -48
- package/corpus/templates/slides/actions/provider-api-request.ts +145 -172
- package/corpus/templates/slides/actions/query-staged-dataset.ts +4 -47
- package/corpus/templates/slides/server/lib/google-docs-oauth.ts +11 -36
- package/corpus/templates/tasks/.agents/skills/actions/SKILL.md +32 -12
- package/corpus/templates/tasks/.agents/skills/agent-native-docs/SKILL.md +10 -0
- package/corpus/templates/tasks/.agents/skills/customizing-agent-native/SKILL.md +102 -0
- package/corpus/templates/tasks/.agents/skills/self-modifying-code/SKILL.md +11 -2
- package/corpus/templates/tasks/.agents/skills/upgrade-agent-native/SKILL.md +6 -0
- package/corpus/toolkit/CHANGELOG.md +125 -0
- package/corpus/toolkit/README.md +60 -0
- package/corpus/toolkit/export-snapshot.json +35 -0
- package/corpus/toolkit/migration-manifest.json +4 -0
- package/corpus/toolkit/package.json +281 -0
- package/corpus/toolkit/scripts/finalize-build.mjs +55 -0
- package/corpus/toolkit/src/app-shell/header-actions.tsx +84 -0
- package/corpus/toolkit/src/app-shell/index.ts +1 -0
- package/corpus/toolkit/src/chat-history/ChatHistoryList.tsx +469 -0
- package/corpus/toolkit/src/chat-history/index.ts +7 -0
- package/corpus/toolkit/src/chat-history.css +314 -0
- package/corpus/toolkit/src/collab-ui/AgentPresenceChip.tsx +69 -0
- package/corpus/toolkit/src/collab-ui/LiveCursorOverlay.tsx +241 -0
- package/corpus/toolkit/src/collab-ui/PresenceBar.tsx +346 -0
- package/corpus/toolkit/src/collab-ui/RecentEditHighlights.tsx +225 -0
- package/corpus/toolkit/src/collab-ui/RemoteSelectionRings.tsx +227 -0
- package/corpus/toolkit/src/collab-ui/agent-identity.ts +16 -0
- package/corpus/toolkit/src/collab-ui/index.ts +39 -0
- package/corpus/toolkit/src/collab-ui/lead-client.ts +32 -0
- package/corpus/toolkit/src/collab-ui/types.ts +96 -0
- package/corpus/toolkit/src/composer/AgentComposerFrame.tsx +59 -0
- package/corpus/toolkit/src/composer/ComposerPlusMenu.tsx +983 -0
- package/corpus/toolkit/src/composer/MentionPopover.tsx +412 -0
- package/corpus/toolkit/src/composer/PastedTextChip.tsx +150 -0
- package/corpus/toolkit/src/composer/PromptComposer.tsx +718 -0
- package/corpus/toolkit/src/composer/RealtimeVoiceMode.tsx +856 -0
- package/corpus/toolkit/src/composer/TiptapComposer.tsx +2936 -0
- package/corpus/toolkit/src/composer/VoiceButton.tsx +255 -0
- package/corpus/toolkit/src/composer/asset-picker-url.ts +80 -0
- package/corpus/toolkit/src/composer/attachment-accept.ts +71 -0
- package/corpus/toolkit/src/composer/draft-key.ts +7 -0
- package/corpus/toolkit/src/composer/extensions/FileReference.tsx +55 -0
- package/corpus/toolkit/src/composer/extensions/MentionReference.tsx +93 -0
- package/corpus/toolkit/src/composer/extensions/SkillReference.tsx +56 -0
- package/corpus/toolkit/src/composer/index.ts +79 -0
- package/corpus/toolkit/src/composer/pasted-text.ts +160 -0
- package/corpus/toolkit/src/composer/prompt-attachments.ts +93 -0
- package/corpus/toolkit/src/composer/realtime-voice-audio-level.ts +64 -0
- package/corpus/toolkit/src/composer/realtime-voice-transcript.ts +134 -0
- package/corpus/toolkit/src/composer/runtime-adapters.tsx +343 -0
- package/corpus/toolkit/src/composer/types.ts +66 -0
- package/corpus/toolkit/src/composer/use-file-search.ts +64 -0
- package/corpus/toolkit/src/composer/use-mention-search.ts +90 -0
- package/corpus/toolkit/src/composer/use-skills.ts +45 -0
- package/corpus/toolkit/src/composer/useRealtimeVoiceMode.tsx +2117 -0
- package/corpus/toolkit/src/composer/useVoiceDictation.ts +1191 -0
- package/corpus/toolkit/src/context-ui/ContextMeter.tsx +101 -0
- package/corpus/toolkit/src/context-ui/ContextSegmentRow.tsx +184 -0
- package/corpus/toolkit/src/context-ui/ContextTreemap.tsx +125 -0
- package/corpus/toolkit/src/context-ui/ContextXRayPanel.tsx +377 -0
- package/corpus/toolkit/src/context-ui/format.ts +42 -0
- package/corpus/toolkit/src/context-ui/index.ts +15 -0
- package/corpus/toolkit/src/context-ui/types.ts +43 -0
- package/corpus/toolkit/src/design-tweaks/index.ts +1 -0
- package/corpus/toolkit/src/design-tweaks/visual-style-controls.tsx +1084 -0
- package/corpus/toolkit/src/editor/BubbleToolbar.tsx +253 -0
- package/corpus/toolkit/src/editor/CodeBlockNode.tsx +254 -0
- package/corpus/toolkit/src/editor/DragHandle.ts +1437 -0
- package/corpus/toolkit/src/editor/ImageExtension.ts +306 -0
- package/corpus/toolkit/src/editor/RegistryBlockContext.tsx +107 -0
- package/corpus/toolkit/src/editor/RegistryBlockNode.tsx +652 -0
- package/corpus/toolkit/src/editor/RichMarkdownEditor.tsx +81 -0
- package/corpus/toolkit/src/editor/RunId.ts +61 -0
- package/corpus/toolkit/src/editor/SharedRichEditor.tsx +321 -0
- package/corpus/toolkit/src/editor/SlashCommandMenu.tsx +310 -0
- package/corpus/toolkit/src/editor/extensions.ts +402 -0
- package/corpus/toolkit/src/editor/gfmDoc.ts +92 -0
- package/corpus/toolkit/src/editor/index.ts +93 -0
- package/corpus/toolkit/src/editor/registrySlashCommands.ts +131 -0
- package/corpus/toolkit/src/editor/surgical-apply.ts +192 -0
- package/corpus/toolkit/src/editor/useCollabReconcile.ts +589 -0
- package/corpus/toolkit/src/editor.css +510 -0
- package/corpus/toolkit/src/hooks/index.ts +2 -0
- package/corpus/toolkit/src/hooks/use-mobile.tsx +21 -0
- package/corpus/toolkit/src/hooks/use-toast.ts +188 -0
- package/corpus/toolkit/src/index.ts +8 -0
- package/corpus/toolkit/src/onboarding/index.ts +1 -0
- package/corpus/toolkit/src/provider.tsx +47 -0
- package/corpus/toolkit/src/sharing/VisibilityBadge.tsx +37 -0
- package/corpus/toolkit/src/sharing/index.ts +4 -0
- package/corpus/toolkit/src/styles.css +21 -0
- package/corpus/toolkit/src/ui/accordion.tsx +56 -0
- package/corpus/toolkit/src/ui/alert-dialog.tsx +141 -0
- package/corpus/toolkit/src/ui/alert.tsx +59 -0
- package/corpus/toolkit/src/ui/aspect-ratio.tsx +5 -0
- package/corpus/toolkit/src/ui/avatar.tsx +48 -0
- package/corpus/toolkit/src/ui/badge.tsx +37 -0
- package/corpus/toolkit/src/ui/breadcrumb.tsx +115 -0
- package/corpus/toolkit/src/ui/button.tsx +78 -0
- package/corpus/toolkit/src/ui/calendar.tsx +214 -0
- package/corpus/toolkit/src/ui/card.tsx +86 -0
- package/corpus/toolkit/src/ui/carousel.tsx +260 -0
- package/corpus/toolkit/src/ui/chart.tsx +375 -0
- package/corpus/toolkit/src/ui/checkbox.tsx +28 -0
- package/corpus/toolkit/src/ui/collapsible.tsx +9 -0
- package/corpus/toolkit/src/ui/command.tsx +174 -0
- package/corpus/toolkit/src/ui/context-menu.tsx +202 -0
- package/corpus/toolkit/src/ui/date-picker.tsx +64 -0
- package/corpus/toolkit/src/ui/dialog.tsx +146 -0
- package/corpus/toolkit/src/ui/drawer.tsx +126 -0
- package/corpus/toolkit/src/ui/dropdown-menu.tsx +218 -0
- package/corpus/toolkit/src/ui/form.tsx +177 -0
- package/corpus/toolkit/src/ui/hover-card.tsx +27 -0
- package/corpus/toolkit/src/ui/index.ts +52 -0
- package/corpus/toolkit/src/ui/input-otp.tsx +69 -0
- package/corpus/toolkit/src/ui/input.tsx +22 -0
- package/corpus/toolkit/src/ui/label.tsx +24 -0
- package/corpus/toolkit/src/ui/logo.tsx +36 -0
- package/corpus/toolkit/src/ui/menubar.tsx +235 -0
- package/corpus/toolkit/src/ui/navigation-menu.tsx +128 -0
- package/corpus/toolkit/src/ui/pagination.tsx +121 -0
- package/corpus/toolkit/src/ui/popover.tsx +44 -0
- package/corpus/toolkit/src/ui/progress.tsx +26 -0
- package/corpus/toolkit/src/ui/radio-group.tsx +42 -0
- package/corpus/toolkit/src/ui/resizable.tsx +43 -0
- package/corpus/toolkit/src/ui/scroll-area.tsx +46 -0
- package/corpus/toolkit/src/ui/select.tsx +158 -0
- package/corpus/toolkit/src/ui/separator.tsx +29 -0
- package/corpus/toolkit/src/ui/sheet.tsx +160 -0
- package/corpus/toolkit/src/ui/sidebar.tsx +777 -0
- package/corpus/toolkit/src/ui/skeleton.tsx +18 -0
- package/corpus/toolkit/src/ui/slider.tsx +26 -0
- package/corpus/toolkit/src/ui/sonner.tsx +58 -0
- package/corpus/toolkit/src/ui/spinner.tsx +17 -0
- package/corpus/toolkit/src/ui/switch.tsx +27 -0
- package/corpus/toolkit/src/ui/table.tsx +117 -0
- package/corpus/toolkit/src/ui/tabs.tsx +53 -0
- package/corpus/toolkit/src/ui/textarea.tsx +23 -0
- package/corpus/toolkit/src/ui/toast.tsx +127 -0
- package/corpus/toolkit/src/ui/toaster.tsx +33 -0
- package/corpus/toolkit/src/ui/toggle-group.tsx +59 -0
- package/corpus/toolkit/src/ui/toggle.tsx +43 -0
- package/corpus/toolkit/src/ui/tooltip.tsx +53 -0
- package/corpus/toolkit/src/ui/use-toast.ts +3 -0
- package/corpus/toolkit/src/utils.ts +8 -0
- package/corpus/toolkit/tsconfig.json +23 -0
- package/dist/cli/atomic-json-file.d.ts +30 -0
- package/dist/cli/atomic-json-file.d.ts.map +1 -0
- package/dist/cli/atomic-json-file.js +240 -0
- package/dist/cli/atomic-json-file.js.map +1 -0
- package/dist/cli/claude-code-participant.d.ts +77 -0
- package/dist/cli/claude-code-participant.d.ts.map +1 -0
- package/dist/cli/claude-code-participant.js +355 -0
- package/dist/cli/claude-code-participant.js.map +1 -0
- package/dist/cli/code-agent-executor.d.ts +8 -9
- package/dist/cli/code-agent-executor.d.ts.map +1 -1
- package/dist/cli/code-agent-executor.js +27 -13
- package/dist/cli/code-agent-executor.js.map +1 -1
- package/dist/cli/code-agent-runs.concurrent-worker.d.ts +2 -0
- package/dist/cli/code-agent-runs.concurrent-worker.d.ts.map +1 -0
- package/dist/cli/code-agent-runs.concurrent-worker.js +32 -0
- package/dist/cli/code-agent-runs.concurrent-worker.js.map +1 -0
- package/dist/cli/code-agent-runs.d.ts +5 -0
- package/dist/cli/code-agent-runs.d.ts.map +1 -1
- package/dist/cli/code-agent-runs.js +51 -33
- package/dist/cli/code-agent-runs.js.map +1 -1
- package/dist/cli/codex-cli-participant.d.ts +77 -0
- package/dist/cli/codex-cli-participant.d.ts.map +1 -0
- package/dist/cli/codex-cli-participant.js +354 -0
- package/dist/cli/codex-cli-participant.js.map +1 -0
- package/dist/cli/multi-frontier-runs.d.ts +246 -0
- package/dist/cli/multi-frontier-runs.d.ts.map +1 -0
- package/dist/cli/multi-frontier-runs.js +1247 -0
- package/dist/cli/multi-frontier-runs.js.map +1 -0
- package/dist/client/chat/ChatHistoryList.d.ts +1 -87
- package/dist/client/chat/ChatHistoryList.d.ts.map +1 -1
- package/dist/client/chat/ChatHistoryList.js +1 -82
- package/dist/client/chat/ChatHistoryList.js.map +1 -1
- package/dist/coding-tools/index.d.ts +1 -0
- package/dist/coding-tools/index.d.ts.map +1 -1
- package/dist/coding-tools/index.js +25 -7
- package/dist/coding-tools/index.js.map +1 -1
- package/dist/collab/awareness.d.ts +2 -2
- package/dist/collab/awareness.d.ts.map +1 -1
- package/dist/embeddings/index.d.ts +24 -0
- package/dist/embeddings/index.d.ts.map +1 -0
- package/dist/embeddings/index.js +172 -0
- package/dist/embeddings/index.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/integrations/index.d.ts +2 -1
- package/dist/integrations/index.d.ts.map +1 -1
- package/dist/integrations/index.js +2 -1
- package/dist/integrations/index.js.map +1 -1
- package/dist/integrations/slack-manifest.d.ts +1 -1
- package/dist/integrations/slack-oauth.d.ts +1 -1
- package/dist/integrations/slack-oauth.d.ts.map +1 -1
- package/dist/integrations/slack-oauth.js +1 -0
- package/dist/integrations/slack-oauth.js.map +1 -1
- package/dist/integrations/webhook-delivery.d.ts +23 -0
- package/dist/integrations/webhook-delivery.d.ts.map +1 -0
- package/dist/integrations/webhook-delivery.js +32 -0
- package/dist/integrations/webhook-delivery.js.map +1 -0
- package/dist/notifications/channels.d.ts.map +1 -1
- package/dist/notifications/channels.js +4 -9
- package/dist/notifications/channels.js.map +1 -1
- package/dist/notifications/routes.d.ts +3 -3
- package/dist/progress/routes.d.ts +1 -1
- package/dist/provider-api/actions/custom-provider-registration.d.ts +136 -0
- package/dist/provider-api/actions/custom-provider-registration.d.ts.map +1 -0
- package/dist/provider-api/actions/custom-provider-registration.js +154 -0
- package/dist/provider-api/actions/custom-provider-registration.js.map +1 -0
- package/dist/provider-api/actions/delete-staged-dataset.d.ts +1 -1
- package/dist/provider-api/actions/delete-staged-dataset.d.ts.map +1 -1
- package/dist/provider-api/actions/delete-staged-dataset.js +3 -34
- package/dist/provider-api/actions/delete-staged-dataset.js.map +1 -1
- package/dist/provider-api/actions/list-staged-datasets.d.ts +2 -3
- package/dist/provider-api/actions/list-staged-datasets.d.ts.map +1 -1
- package/dist/provider-api/actions/list-staged-datasets.js +3 -40
- package/dist/provider-api/actions/list-staged-datasets.js.map +1 -1
- package/dist/provider-api/actions/provider-api-audit.d.ts +7 -0
- package/dist/provider-api/actions/provider-api-audit.d.ts.map +1 -0
- package/dist/provider-api/actions/provider-api-audit.js +74 -0
- package/dist/provider-api/actions/provider-api-audit.js.map +1 -0
- package/dist/provider-api/actions/provider-api.d.ts +370 -0
- package/dist/provider-api/actions/provider-api.d.ts.map +1 -0
- package/dist/provider-api/actions/provider-api.js +280 -0
- package/dist/provider-api/actions/provider-api.js.map +1 -0
- package/dist/provider-api/actions/query-staged-dataset.d.ts +1 -1
- package/dist/provider-api/actions/query-staged-dataset.d.ts.map +1 -1
- package/dist/provider-api/actions/query-staged-dataset.js +3 -116
- package/dist/provider-api/actions/query-staged-dataset.js.map +1 -1
- package/dist/provider-api/actions/staged-datasets.d.ts +190 -0
- package/dist/provider-api/actions/staged-datasets.d.ts.map +1 -0
- package/dist/provider-api/actions/staged-datasets.js +210 -0
- package/dist/provider-api/actions/staged-datasets.js.map +1 -0
- package/dist/provider-api/gong.d.ts +44 -0
- package/dist/provider-api/gong.d.ts.map +1 -0
- package/dist/provider-api/gong.js +56 -0
- package/dist/provider-api/gong.js.map +1 -0
- package/dist/resources/handlers.d.ts +1 -1
- package/dist/scripts/docs/source-search.d.ts +2 -1
- package/dist/scripts/docs/source-search.d.ts.map +1 -1
- package/dist/scripts/docs/source-search.js +2 -1
- package/dist/scripts/docs/source-search.js.map +1 -1
- package/dist/search/index.d.ts +88 -0
- package/dist/search/index.d.ts.map +1 -0
- package/dist/search/index.js +267 -0
- package/dist/search/index.js.map +1 -0
- package/dist/server/agent-engine-api-key-route.d.ts +1 -1
- package/dist/server/google-oauth-credentials.d.ts +35 -0
- package/dist/server/google-oauth-credentials.d.ts.map +1 -1
- package/dist/server/google-oauth-credentials.js +48 -2
- package/dist/server/google-oauth-credentials.js.map +1 -1
- package/dist/server/index.d.ts +1 -1
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +1 -1
- package/dist/server/index.js.map +1 -1
- package/dist/styles/chat-history-list.css +1 -313
- package/dist/templates/chat/.agents/skills/actions/SKILL.md +32 -12
- package/dist/templates/chat/.agents/skills/agent-native-docs/SKILL.md +10 -0
- package/dist/templates/chat/.agents/skills/customizing-agent-native/SKILL.md +102 -0
- package/dist/templates/chat/.agents/skills/self-modifying-code/SKILL.md +11 -2
- package/dist/templates/chat/.agents/skills/upgrade-agent-native/SKILL.md +6 -0
- package/dist/templates/chat/AGENTS.md +5 -2
- package/dist/templates/chat/app/components/layout/Sidebar.tsx +51 -157
- package/dist/templates/default/.agents/skills/actions/SKILL.md +32 -12
- package/dist/templates/default/.agents/skills/agent-native-docs/SKILL.md +10 -0
- package/dist/templates/default/.agents/skills/customizing-agent-native/SKILL.md +102 -0
- package/dist/templates/default/.agents/skills/integration-webhooks/SKILL.md +323 -0
- package/dist/templates/default/.agents/skills/onboarding/SKILL.md +56 -0
- package/dist/templates/default/.agents/skills/secrets/SKILL.md +273 -0
- package/dist/templates/default/.agents/skills/self-modifying-code/SKILL.md +11 -2
- package/dist/templates/default/.agents/skills/upgrade-agent-native/SKILL.md +6 -0
- package/dist/templates/default/AGENTS.md +25 -18
- package/dist/templates/headless/.agents/skills/actions/SKILL.md +512 -0
- package/dist/templates/headless/.agents/skills/agent-native-docs/SKILL.md +10 -0
- package/dist/templates/headless/.agents/skills/customizing-agent-native/SKILL.md +102 -0
- package/dist/templates/headless/.agents/skills/integration-webhooks/SKILL.md +323 -0
- package/dist/templates/headless/.agents/skills/secrets/SKILL.md +273 -0
- package/dist/templates/headless/.agents/skills/upgrade-agent-native/SKILL.md +6 -0
- package/dist/templates/headless/AGENTS.md +7 -0
- package/dist/templates/workspace-core/.agents/skills/actions/SKILL.md +32 -12
- package/dist/templates/workspace-core/.agents/skills/agent-native-docs/SKILL.md +10 -0
- package/dist/templates/workspace-core/.agents/skills/agent-native-toolkit/SKILL.md +6 -0
- package/dist/templates/workspace-core/.agents/skills/customizing-agent-native/SKILL.md +102 -0
- package/dist/templates/workspace-core/.agents/skills/self-modifying-code/SKILL.md +11 -2
- package/dist/templates/workspace-core/.agents/skills/upgrade-agent-native/SKILL.md +6 -0
- package/dist/templates/workspace-core/AGENTS.md +4 -0
- package/dist/templates/workspace-root/AGENTS.md +4 -0
- package/docs/AGENTS.md +7 -0
- package/docs/content/integrations.mdx +63 -2
- package/docs/design/provider-integration-authority.md +117 -0
- package/package.json +9 -2
- package/src/templates/chat/.agents/skills/actions/SKILL.md +32 -12
- package/src/templates/chat/.agents/skills/agent-native-docs/SKILL.md +10 -0
- package/src/templates/chat/.agents/skills/customizing-agent-native/SKILL.md +102 -0
- package/src/templates/chat/.agents/skills/self-modifying-code/SKILL.md +11 -2
- package/src/templates/chat/.agents/skills/upgrade-agent-native/SKILL.md +6 -0
- package/src/templates/chat/AGENTS.md +5 -2
- package/src/templates/chat/app/components/layout/Sidebar.tsx +51 -157
- package/src/templates/default/.agents/skills/actions/SKILL.md +32 -12
- package/src/templates/default/.agents/skills/agent-native-docs/SKILL.md +10 -0
- package/src/templates/default/.agents/skills/customizing-agent-native/SKILL.md +102 -0
- package/src/templates/default/.agents/skills/integration-webhooks/SKILL.md +323 -0
- package/src/templates/default/.agents/skills/onboarding/SKILL.md +56 -0
- package/src/templates/default/.agents/skills/secrets/SKILL.md +273 -0
- package/src/templates/default/.agents/skills/self-modifying-code/SKILL.md +11 -2
- package/src/templates/default/.agents/skills/upgrade-agent-native/SKILL.md +6 -0
- package/src/templates/default/AGENTS.md +25 -18
- package/src/templates/headless/.agents/skills/actions/SKILL.md +512 -0
- package/src/templates/headless/.agents/skills/agent-native-docs/SKILL.md +10 -0
- package/src/templates/headless/.agents/skills/customizing-agent-native/SKILL.md +102 -0
- package/src/templates/headless/.agents/skills/integration-webhooks/SKILL.md +323 -0
- package/src/templates/headless/.agents/skills/secrets/SKILL.md +273 -0
- package/src/templates/headless/.agents/skills/upgrade-agent-native/SKILL.md +6 -0
- package/src/templates/headless/AGENTS.md +7 -0
- package/src/templates/workspace-core/.agents/skills/actions/SKILL.md +32 -12
- package/src/templates/workspace-core/.agents/skills/agent-native-docs/SKILL.md +10 -0
- package/src/templates/workspace-core/.agents/skills/agent-native-toolkit/SKILL.md +6 -0
- package/src/templates/workspace-core/.agents/skills/customizing-agent-native/SKILL.md +102 -0
- package/src/templates/workspace-core/.agents/skills/self-modifying-code/SKILL.md +11 -2
- package/src/templates/workspace-core/.agents/skills/upgrade-agent-native/SKILL.md +6 -0
- package/src/templates/workspace-core/AGENTS.md +4 -0
- package/src/templates/workspace-root/AGENTS.md +4 -0
|
@@ -0,0 +1,1191 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Voice dictation hook for the agent composer.
|
|
3
|
+
*
|
|
4
|
+
* Wires voice providers behind a single state machine:
|
|
5
|
+
* - "auto" / "openai" / "builder" / "builder-gemini" / "gemini" / "groq"
|
|
6
|
+
* — MediaRecorder → POST /_agent-native/transcribe-voice
|
|
7
|
+
* - "google-realtime"
|
|
8
|
+
* — MediaRecorder chunks → POST /_agent-native/transcribe-stream/session
|
|
9
|
+
* → managed WebSocket → Google Speech-to-Text streaming
|
|
10
|
+
* - "browser" — Web Speech API (low quality, offline capable)
|
|
11
|
+
*
|
|
12
|
+
* Provider preference lives in application_state under
|
|
13
|
+
* `voice-transcription-prefs` (`{ transcriptionMode, provider, instructions }`).
|
|
14
|
+
* The composer reads it on every start so settings changes take effect
|
|
15
|
+
* immediately without unmounting the composer.
|
|
16
|
+
*
|
|
17
|
+
* The hook exposes amplitude (0..1) and duration (ms) so the composer can
|
|
18
|
+
* render the Lovable-style live waveform + MM:SS timer.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
22
|
+
|
|
23
|
+
import {
|
|
24
|
+
applyVoiceContextReplacements,
|
|
25
|
+
type VoiceContextPack,
|
|
26
|
+
useComposerRuntimeAdapters,
|
|
27
|
+
} from "./runtime-adapters.js";
|
|
28
|
+
|
|
29
|
+
export type VoiceProvider =
|
|
30
|
+
| "auto"
|
|
31
|
+
| "openai"
|
|
32
|
+
| "browser"
|
|
33
|
+
| "google-realtime"
|
|
34
|
+
| "builder-gemini"
|
|
35
|
+
| "builder"
|
|
36
|
+
| "gemini"
|
|
37
|
+
| "groq";
|
|
38
|
+
|
|
39
|
+
export type TranscriptionMode = "mac-native" | "google-realtime" | "batch";
|
|
40
|
+
|
|
41
|
+
export interface VoicePrefs {
|
|
42
|
+
provider: VoiceProvider;
|
|
43
|
+
transcriptionMode?: TranscriptionMode;
|
|
44
|
+
instructions?: string;
|
|
45
|
+
cleanupEnabled?: boolean;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface VoiceCleanupPrefs {
|
|
49
|
+
enabled?: boolean;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
type VoiceContextPackSource =
|
|
53
|
+
| VoiceContextPack
|
|
54
|
+
| (() => VoiceContextPack | undefined | Promise<VoiceContextPack | undefined>)
|
|
55
|
+
| undefined;
|
|
56
|
+
|
|
57
|
+
const PREFS_KEY = "voice-transcription-prefs";
|
|
58
|
+
const CLEANUP_PREFS_KEY = "voice-cleanup-prefs";
|
|
59
|
+
const PREFS_URL = `/_agent-native/application-state/${PREFS_KEY}`;
|
|
60
|
+
const CLEANUP_PREFS_URL = `/_agent-native/application-state/${CLEANUP_PREFS_KEY}`;
|
|
61
|
+
const TRANSCRIBE_URL = "/_agent-native/transcribe-voice";
|
|
62
|
+
const GOOGLE_REALTIME_SESSION_URL = "/_agent-native/transcribe-stream/session";
|
|
63
|
+
const GOOGLE_REALTIME_WS_PROTOCOL = "google-realtime.v1";
|
|
64
|
+
function isVoiceProvider(value: unknown): value is VoiceProvider {
|
|
65
|
+
return (
|
|
66
|
+
value === "auto" ||
|
|
67
|
+
value === "openai" ||
|
|
68
|
+
value === "browser" ||
|
|
69
|
+
value === "google-realtime" ||
|
|
70
|
+
value === "builder-gemini" ||
|
|
71
|
+
value === "builder" ||
|
|
72
|
+
value === "gemini" ||
|
|
73
|
+
value === "groq"
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function isTranscriptionMode(value: unknown): value is TranscriptionMode {
|
|
78
|
+
return (
|
|
79
|
+
value === "mac-native" || value === "google-realtime" || value === "batch"
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async function defaultProvider(): Promise<VoiceProvider> {
|
|
84
|
+
return "auto";
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function normalizeProviderForMode(
|
|
88
|
+
mode: TranscriptionMode | undefined,
|
|
89
|
+
provider: VoiceProvider | null,
|
|
90
|
+
): VoiceProvider | null {
|
|
91
|
+
if (mode === "mac-native") return "browser";
|
|
92
|
+
if (mode === "google-realtime") return "google-realtime";
|
|
93
|
+
if (mode === "batch") {
|
|
94
|
+
if (!provider || provider === "browser") return "auto";
|
|
95
|
+
return provider === "builder" ? "builder-gemini" : provider;
|
|
96
|
+
}
|
|
97
|
+
if (!provider) return null;
|
|
98
|
+
return provider === "builder" ? "builder-gemini" : provider;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export type VoiceState =
|
|
102
|
+
| "idle"
|
|
103
|
+
| "starting"
|
|
104
|
+
| "recording"
|
|
105
|
+
| "transcribing"
|
|
106
|
+
| "error";
|
|
107
|
+
|
|
108
|
+
export interface UseVoiceDictationOptions {
|
|
109
|
+
onTranscript: (text: string) => void;
|
|
110
|
+
onError?: (message: string) => void;
|
|
111
|
+
/** Called with (accumulatedFinalText, currentInterimText) as speech is recognized in real time. */
|
|
112
|
+
onLiveUpdate?: (finalText: string, interimText: string) => void;
|
|
113
|
+
contextPack?: VoiceContextPackSource;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export interface VoiceDictationApi {
|
|
117
|
+
state: VoiceState;
|
|
118
|
+
amplitude: number;
|
|
119
|
+
durationMs: number;
|
|
120
|
+
errorMessage: string | null;
|
|
121
|
+
provider: VoiceProvider;
|
|
122
|
+
supported: boolean;
|
|
123
|
+
start: () => Promise<void>;
|
|
124
|
+
stop: () => void;
|
|
125
|
+
cancel: () => void;
|
|
126
|
+
dismissError: () => void;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
async function readVoicePrefs(
|
|
130
|
+
resolvePath: (path: string) => string,
|
|
131
|
+
): Promise<VoicePrefs> {
|
|
132
|
+
const cleanupPrefs = await readVoiceCleanupPrefs(resolvePath);
|
|
133
|
+
try {
|
|
134
|
+
const res = await fetch(resolvePath(PREFS_URL));
|
|
135
|
+
if (!res.ok) {
|
|
136
|
+
return {
|
|
137
|
+
provider: await defaultProvider(),
|
|
138
|
+
cleanupEnabled: cleanupPrefs.enabled,
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
const body = (await res.json()) as
|
|
142
|
+
| VoicePrefs
|
|
143
|
+
| { value?: VoicePrefs }
|
|
144
|
+
| null;
|
|
145
|
+
const value =
|
|
146
|
+
(body as { value?: VoicePrefs } | null)?.value ??
|
|
147
|
+
(body as VoicePrefs | null);
|
|
148
|
+
const mode = isTranscriptionMode(value?.transcriptionMode)
|
|
149
|
+
? value.transcriptionMode
|
|
150
|
+
: undefined;
|
|
151
|
+
const p =
|
|
152
|
+
(body as VoicePrefs | null)?.provider ??
|
|
153
|
+
(body as { value?: VoicePrefs } | null)?.value?.provider;
|
|
154
|
+
const instructions =
|
|
155
|
+
(body as VoicePrefs | null)?.instructions ??
|
|
156
|
+
(body as { value?: VoicePrefs } | null)?.value?.instructions;
|
|
157
|
+
const provider = normalizeProviderForMode(
|
|
158
|
+
mode,
|
|
159
|
+
isVoiceProvider(p) ? p : null,
|
|
160
|
+
);
|
|
161
|
+
if (provider) {
|
|
162
|
+
return {
|
|
163
|
+
transcriptionMode: mode,
|
|
164
|
+
provider,
|
|
165
|
+
cleanupEnabled: cleanupPrefs.enabled,
|
|
166
|
+
instructions:
|
|
167
|
+
typeof instructions === "string" ? instructions.trim() : undefined,
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
} catch {
|
|
171
|
+
/* fall through */
|
|
172
|
+
}
|
|
173
|
+
return {
|
|
174
|
+
provider: await defaultProvider(),
|
|
175
|
+
cleanupEnabled: cleanupPrefs.enabled,
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
async function readVoiceCleanupPrefs(
|
|
180
|
+
resolvePath: (path: string) => string,
|
|
181
|
+
): Promise<VoiceCleanupPrefs> {
|
|
182
|
+
try {
|
|
183
|
+
const res = await fetch(resolvePath(CLEANUP_PREFS_URL));
|
|
184
|
+
if (!res.ok) return {};
|
|
185
|
+
const body = (await res.json()) as
|
|
186
|
+
| VoiceCleanupPrefs
|
|
187
|
+
| { value?: VoiceCleanupPrefs }
|
|
188
|
+
| null;
|
|
189
|
+
const value =
|
|
190
|
+
(body as { value?: VoiceCleanupPrefs } | null)?.value ??
|
|
191
|
+
(body as VoiceCleanupPrefs | null);
|
|
192
|
+
return { enabled: value?.enabled === true };
|
|
193
|
+
} catch {
|
|
194
|
+
return {};
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function getSpeechRecognitionCtor(): any {
|
|
199
|
+
if (typeof window === "undefined") return null;
|
|
200
|
+
return (
|
|
201
|
+
(window as any).SpeechRecognition ||
|
|
202
|
+
(window as any).webkitSpeechRecognition ||
|
|
203
|
+
null
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function pickMimeType(): string {
|
|
208
|
+
if (typeof MediaRecorder === "undefined") return "audio/webm";
|
|
209
|
+
const candidates = [
|
|
210
|
+
"audio/webm;codecs=opus",
|
|
211
|
+
"audio/webm",
|
|
212
|
+
"audio/mp4",
|
|
213
|
+
"audio/ogg;codecs=opus",
|
|
214
|
+
];
|
|
215
|
+
for (const mime of candidates) {
|
|
216
|
+
try {
|
|
217
|
+
if (MediaRecorder.isTypeSupported(mime)) return mime;
|
|
218
|
+
} catch {
|
|
219
|
+
/* ignore */
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
return "audio/webm";
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
async function resolveVoiceContextPack(
|
|
226
|
+
source: VoiceContextPackSource,
|
|
227
|
+
): Promise<VoiceContextPack | undefined> {
|
|
228
|
+
try {
|
|
229
|
+
const value = typeof source === "function" ? await source() : source;
|
|
230
|
+
return value && typeof value === "object" ? value : undefined;
|
|
231
|
+
} catch {
|
|
232
|
+
return undefined;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function appendVoiceContext(
|
|
237
|
+
form: FormData,
|
|
238
|
+
contextPack: VoiceContextPack | undefined,
|
|
239
|
+
): void {
|
|
240
|
+
if (!contextPack) return;
|
|
241
|
+
try {
|
|
242
|
+
form.append("voiceContext", JSON.stringify(contextPack));
|
|
243
|
+
} catch {
|
|
244
|
+
/* best effort */
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
export function providerForTextCleanup(provider: VoiceProvider): VoiceProvider {
|
|
249
|
+
return provider === "google-realtime" ? "auto" : provider;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
async function cleanupRecognizedText({
|
|
253
|
+
text,
|
|
254
|
+
provider,
|
|
255
|
+
instructions,
|
|
256
|
+
contextPack,
|
|
257
|
+
resolvePath,
|
|
258
|
+
replaceContext,
|
|
259
|
+
}: {
|
|
260
|
+
text: string;
|
|
261
|
+
provider: VoiceProvider;
|
|
262
|
+
instructions?: string;
|
|
263
|
+
contextPack?: VoiceContextPack;
|
|
264
|
+
resolvePath: (path: string) => string;
|
|
265
|
+
replaceContext: (
|
|
266
|
+
text: string,
|
|
267
|
+
context: VoiceContextPack | undefined,
|
|
268
|
+
) => string;
|
|
269
|
+
}): Promise<string> {
|
|
270
|
+
if (provider === "browser") {
|
|
271
|
+
return replaceContext(text, contextPack);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
const form = new FormData();
|
|
275
|
+
form.append("text", text);
|
|
276
|
+
form.append("provider", providerForTextCleanup(provider));
|
|
277
|
+
if (instructions?.trim()) {
|
|
278
|
+
form.append("instructions", instructions.trim());
|
|
279
|
+
}
|
|
280
|
+
appendVoiceContext(form, contextPack);
|
|
281
|
+
|
|
282
|
+
const res = await fetch(resolvePath(TRANSCRIBE_URL), {
|
|
283
|
+
method: "POST",
|
|
284
|
+
body: form,
|
|
285
|
+
});
|
|
286
|
+
if (!res.ok) {
|
|
287
|
+
const body = await res
|
|
288
|
+
.json()
|
|
289
|
+
.catch(() => ({ error: `HTTP ${res.status}` }));
|
|
290
|
+
throw new Error(body.error || `Transcript cleanup failed (${res.status})`);
|
|
291
|
+
}
|
|
292
|
+
const data = (await res.json()) as { text?: string };
|
|
293
|
+
return (data.text ?? "").trim() || text;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
type BrowserDocumentPolicy = {
|
|
297
|
+
allowsFeature: (feature: string) => boolean;
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
function microphoneBlockedByPermissionsPolicy(): boolean {
|
|
301
|
+
if (typeof document === "undefined") return false;
|
|
302
|
+
const doc = document as Document & {
|
|
303
|
+
permissionsPolicy?: BrowserDocumentPolicy;
|
|
304
|
+
featurePolicy?: BrowserDocumentPolicy;
|
|
305
|
+
};
|
|
306
|
+
const policy = doc.permissionsPolicy ?? doc.featurePolicy ?? null;
|
|
307
|
+
if (!policy?.allowsFeature) return false;
|
|
308
|
+
try {
|
|
309
|
+
return !policy.allowsFeature("microphone");
|
|
310
|
+
} catch {
|
|
311
|
+
return false;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
export function voiceDictationStartErrorMessage(error: unknown): string {
|
|
316
|
+
const name =
|
|
317
|
+
typeof (error as { name?: unknown } | null)?.name === "string"
|
|
318
|
+
? (error as { name: string }).name
|
|
319
|
+
: "";
|
|
320
|
+
const message =
|
|
321
|
+
typeof (error as { message?: unknown } | null)?.message === "string"
|
|
322
|
+
? (error as { message: string }).message
|
|
323
|
+
: "";
|
|
324
|
+
|
|
325
|
+
if (
|
|
326
|
+
microphoneBlockedByPermissionsPolicy() ||
|
|
327
|
+
/permissions[- ]policy|feature policy/i.test(message)
|
|
328
|
+
) {
|
|
329
|
+
return "This app is blocking microphone access through its browser permissions policy. Reload the app, or open it directly in a browser tab.";
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
if (
|
|
333
|
+
name === "NotAllowedError" ||
|
|
334
|
+
name === "SecurityError" ||
|
|
335
|
+
/permission|denied|not allowed/i.test(message)
|
|
336
|
+
) {
|
|
337
|
+
return "Microphone access is blocked. Click the site controls icon in the address bar, set Microphone to Allow for this app, then try again.";
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
if (name === "NotFoundError" || name === "DevicesNotFoundError") {
|
|
341
|
+
return "No microphone was found. Plug one in or choose a different input, then try again.";
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
if (name === "NotReadableError" || name === "TrackStartError") {
|
|
345
|
+
return "Your microphone is busy in another app. Close the other app or choose a different input, then try again.";
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
return message || "Could not start recording";
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
function voiceDictationSpeechErrorMessage(error: string | undefined): string {
|
|
352
|
+
if (error === "not-allowed" || error === "service-not-allowed") {
|
|
353
|
+
return voiceDictationStartErrorMessage({
|
|
354
|
+
name: "NotAllowedError",
|
|
355
|
+
message: error,
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
if (error === "audio-capture") {
|
|
359
|
+
return "No microphone was found. Plug one in or choose a different input, then try again.";
|
|
360
|
+
}
|
|
361
|
+
return `Speech recognition error: ${error ?? "unknown"}`;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
export function useVoiceDictation(
|
|
365
|
+
options: UseVoiceDictationOptions,
|
|
366
|
+
): VoiceDictationApi {
|
|
367
|
+
const adapters = useComposerRuntimeAdapters();
|
|
368
|
+
const resolvePath = adapters.resolvePath!;
|
|
369
|
+
const replaceContext =
|
|
370
|
+
adapters.voice!.applyContextReplacements ?? applyVoiceContextReplacements;
|
|
371
|
+
const { onTranscript, onError, onLiveUpdate, contextPack } = options;
|
|
372
|
+
const onTranscriptRef = useRef(onTranscript);
|
|
373
|
+
const onErrorRef = useRef(onError);
|
|
374
|
+
const onLiveUpdateRef = useRef(onLiveUpdate);
|
|
375
|
+
const contextPackRef = useRef(contextPack);
|
|
376
|
+
onTranscriptRef.current = onTranscript;
|
|
377
|
+
onErrorRef.current = onError;
|
|
378
|
+
onLiveUpdateRef.current = onLiveUpdate;
|
|
379
|
+
contextPackRef.current = contextPack;
|
|
380
|
+
|
|
381
|
+
const [state, setState] = useState<VoiceState>("idle");
|
|
382
|
+
const [amplitude, setAmplitude] = useState(0);
|
|
383
|
+
const [durationMs, setDurationMs] = useState(0);
|
|
384
|
+
const [errorMessage, setErrorMessage] = useState<string | null>(null);
|
|
385
|
+
const [provider, setProvider] = useState<VoiceProvider>("auto");
|
|
386
|
+
|
|
387
|
+
// Keep refs for teardown / cross-branch access.
|
|
388
|
+
const mediaStreamRef = useRef<MediaStream | null>(null);
|
|
389
|
+
const mediaRecorderRef = useRef<MediaRecorder | null>(null);
|
|
390
|
+
const chunksRef = useRef<Blob[]>([]);
|
|
391
|
+
const audioContextRef = useRef<AudioContext | null>(null);
|
|
392
|
+
const analyserRef = useRef<AnalyserNode | null>(null);
|
|
393
|
+
const rafRef = useRef<number | null>(null);
|
|
394
|
+
const timerRef = useRef<ReturnType<typeof setInterval> | null>(null);
|
|
395
|
+
const startedAtRef = useRef<number>(0);
|
|
396
|
+
const cancelledRef = useRef(false);
|
|
397
|
+
const speechRef = useRef<any>(null);
|
|
398
|
+
const speechTranscriptRef = useRef<string>("");
|
|
399
|
+
const activeProviderRef = useRef<VoiceProvider>("browser");
|
|
400
|
+
// Parallel live recognition for OpenAI mode (provides instant preview while MediaRecorder captures)
|
|
401
|
+
const liveSpeechRef = useRef<any>(null);
|
|
402
|
+
const liveTextRef = useRef<string>("");
|
|
403
|
+
const realtimeSocketRef = useRef<WebSocket | null>(null);
|
|
404
|
+
const realtimeFinalRef = useRef<string>("");
|
|
405
|
+
const realtimeInterimRef = useRef<string>("");
|
|
406
|
+
const realtimeStopTimeoutRef = useRef<number | null>(null);
|
|
407
|
+
|
|
408
|
+
const mediaRecorderSupported =
|
|
409
|
+
typeof window !== "undefined" &&
|
|
410
|
+
typeof navigator !== "undefined" &&
|
|
411
|
+
!!navigator.mediaDevices?.getUserMedia &&
|
|
412
|
+
typeof (window as any).MediaRecorder !== "undefined";
|
|
413
|
+
const speechSupported = !!getSpeechRecognitionCtor();
|
|
414
|
+
const supported = mediaRecorderSupported || speechSupported;
|
|
415
|
+
|
|
416
|
+
const teardown = useCallback(() => {
|
|
417
|
+
if (rafRef.current != null) {
|
|
418
|
+
cancelAnimationFrame(rafRef.current);
|
|
419
|
+
rafRef.current = null;
|
|
420
|
+
}
|
|
421
|
+
if (timerRef.current != null) {
|
|
422
|
+
clearInterval(timerRef.current);
|
|
423
|
+
timerRef.current = null;
|
|
424
|
+
}
|
|
425
|
+
if (mediaStreamRef.current) {
|
|
426
|
+
for (const track of mediaStreamRef.current.getTracks()) {
|
|
427
|
+
track.stop();
|
|
428
|
+
}
|
|
429
|
+
mediaStreamRef.current = null;
|
|
430
|
+
}
|
|
431
|
+
if (audioContextRef.current) {
|
|
432
|
+
audioContextRef.current.close().catch(() => {});
|
|
433
|
+
audioContextRef.current = null;
|
|
434
|
+
}
|
|
435
|
+
if (speechRef.current) {
|
|
436
|
+
// Stop the Web Speech session before dropping the ref so the browser
|
|
437
|
+
// releases the mic and stops dispatching onresult events into a stale
|
|
438
|
+
// closure. abort() is fire-and-forget (no final result); stop() would
|
|
439
|
+
// deliver remaining partials but we've already cleared state.
|
|
440
|
+
try {
|
|
441
|
+
speechRef.current.abort?.();
|
|
442
|
+
} catch {
|
|
443
|
+
/* ignore */
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
if (liveSpeechRef.current) {
|
|
447
|
+
try {
|
|
448
|
+
liveSpeechRef.current.abort?.();
|
|
449
|
+
} catch {
|
|
450
|
+
/* ignore */
|
|
451
|
+
}
|
|
452
|
+
liveSpeechRef.current = null;
|
|
453
|
+
}
|
|
454
|
+
if (realtimeSocketRef.current) {
|
|
455
|
+
const socket = realtimeSocketRef.current;
|
|
456
|
+
realtimeSocketRef.current = null;
|
|
457
|
+
try {
|
|
458
|
+
socket.close();
|
|
459
|
+
} catch {
|
|
460
|
+
/* ignore */
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
if (realtimeStopTimeoutRef.current) {
|
|
464
|
+
clearTimeout(realtimeStopTimeoutRef.current);
|
|
465
|
+
realtimeStopTimeoutRef.current = null;
|
|
466
|
+
}
|
|
467
|
+
analyserRef.current = null;
|
|
468
|
+
mediaRecorderRef.current = null;
|
|
469
|
+
chunksRef.current = [];
|
|
470
|
+
speechRef.current = null;
|
|
471
|
+
speechTranscriptRef.current = "";
|
|
472
|
+
liveTextRef.current = "";
|
|
473
|
+
realtimeFinalRef.current = "";
|
|
474
|
+
realtimeInterimRef.current = "";
|
|
475
|
+
setAmplitude(0);
|
|
476
|
+
}, []);
|
|
477
|
+
|
|
478
|
+
useEffect(() => teardown, [teardown]);
|
|
479
|
+
|
|
480
|
+
const failWith = useCallback(
|
|
481
|
+
(message: string) => {
|
|
482
|
+
setErrorMessage(message);
|
|
483
|
+
setState("error");
|
|
484
|
+
onErrorRef.current?.(message);
|
|
485
|
+
teardown();
|
|
486
|
+
},
|
|
487
|
+
[teardown],
|
|
488
|
+
);
|
|
489
|
+
|
|
490
|
+
const getVoiceContextPack = useCallback(
|
|
491
|
+
() => resolveVoiceContextPack(contextPackRef.current),
|
|
492
|
+
[],
|
|
493
|
+
);
|
|
494
|
+
|
|
495
|
+
const startMeter = useCallback((stream: MediaStream) => {
|
|
496
|
+
try {
|
|
497
|
+
const AudioCtor =
|
|
498
|
+
typeof window !== "undefined"
|
|
499
|
+
? window.AudioContext || (window as any).webkitAudioContext || null
|
|
500
|
+
: null;
|
|
501
|
+
if (!AudioCtor) return;
|
|
502
|
+
const ctx: AudioContext = new AudioCtor();
|
|
503
|
+
const source = ctx.createMediaStreamSource(stream);
|
|
504
|
+
const analyser = ctx.createAnalyser();
|
|
505
|
+
analyser.fftSize = 512;
|
|
506
|
+
source.connect(analyser);
|
|
507
|
+
audioContextRef.current = ctx;
|
|
508
|
+
analyserRef.current = analyser;
|
|
509
|
+
|
|
510
|
+
const buffer = new Uint8Array(analyser.frequencyBinCount);
|
|
511
|
+
const tick = () => {
|
|
512
|
+
if (!analyserRef.current) return;
|
|
513
|
+
analyserRef.current.getByteTimeDomainData(buffer);
|
|
514
|
+
let sumSquares = 0;
|
|
515
|
+
for (let i = 0; i < buffer.length; i++) {
|
|
516
|
+
const n = (buffer[i] - 128) / 128;
|
|
517
|
+
sumSquares += n * n;
|
|
518
|
+
}
|
|
519
|
+
const rms = Math.sqrt(sumSquares / buffer.length);
|
|
520
|
+
setAmplitude(Math.min(1, rms * 2.5));
|
|
521
|
+
rafRef.current = requestAnimationFrame(tick);
|
|
522
|
+
};
|
|
523
|
+
rafRef.current = requestAnimationFrame(tick);
|
|
524
|
+
} catch {
|
|
525
|
+
/* analyser is best-effort */
|
|
526
|
+
}
|
|
527
|
+
}, []);
|
|
528
|
+
|
|
529
|
+
const startTimer = useCallback(() => {
|
|
530
|
+
startedAtRef.current = Date.now();
|
|
531
|
+
setDurationMs(0);
|
|
532
|
+
timerRef.current = setInterval(() => {
|
|
533
|
+
setDurationMs(Date.now() - startedAtRef.current);
|
|
534
|
+
}, 100);
|
|
535
|
+
}, []);
|
|
536
|
+
|
|
537
|
+
const startOpenAi = useCallback(
|
|
538
|
+
async (providerPref: VoiceProvider, instructions?: string) => {
|
|
539
|
+
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
|
540
|
+
// User may have pressed Escape (cancel) while the permission prompt was
|
|
541
|
+
// open. If so, stop the stream and bail before we start recording.
|
|
542
|
+
if (cancelledRef.current) {
|
|
543
|
+
for (const track of stream.getTracks()) track.stop();
|
|
544
|
+
cancelledRef.current = false;
|
|
545
|
+
setState("idle");
|
|
546
|
+
return;
|
|
547
|
+
}
|
|
548
|
+
mediaStreamRef.current = stream;
|
|
549
|
+
const mimeType = pickMimeType();
|
|
550
|
+
const recorder = new MediaRecorder(stream, { mimeType });
|
|
551
|
+
mediaRecorderRef.current = recorder;
|
|
552
|
+
chunksRef.current = [];
|
|
553
|
+
|
|
554
|
+
recorder.ondataavailable = (e) => {
|
|
555
|
+
if (e.data && e.data.size > 0) chunksRef.current.push(e.data);
|
|
556
|
+
};
|
|
557
|
+
recorder.onstop = async () => {
|
|
558
|
+
const localChunks = chunksRef.current.slice();
|
|
559
|
+
const localMime = recorder.mimeType || mimeType;
|
|
560
|
+
const liveSnapshot = liveTextRef.current;
|
|
561
|
+
teardown();
|
|
562
|
+
if (cancelledRef.current) {
|
|
563
|
+
cancelledRef.current = false;
|
|
564
|
+
setState("idle");
|
|
565
|
+
return;
|
|
566
|
+
}
|
|
567
|
+
if (localChunks.length === 0) {
|
|
568
|
+
if (liveSnapshot) onTranscriptRef.current?.(liveSnapshot.trim());
|
|
569
|
+
setState("idle");
|
|
570
|
+
return;
|
|
571
|
+
}
|
|
572
|
+
setState("transcribing");
|
|
573
|
+
try {
|
|
574
|
+
const audioBlob = new Blob(localChunks, { type: localMime });
|
|
575
|
+
const form = new FormData();
|
|
576
|
+
form.append(
|
|
577
|
+
"audio",
|
|
578
|
+
audioBlob,
|
|
579
|
+
`voice.${localMime.split("/")[1] ?? "webm"}`,
|
|
580
|
+
);
|
|
581
|
+
form.append("provider", providerPref);
|
|
582
|
+
if (instructions?.trim()) {
|
|
583
|
+
form.append("instructions", instructions.trim());
|
|
584
|
+
}
|
|
585
|
+
appendVoiceContext(form, await getVoiceContextPack());
|
|
586
|
+
const res = await fetch(resolvePath(TRANSCRIBE_URL), {
|
|
587
|
+
method: "POST",
|
|
588
|
+
body: form,
|
|
589
|
+
});
|
|
590
|
+
if (!res.ok) {
|
|
591
|
+
const body = await res
|
|
592
|
+
.json()
|
|
593
|
+
.catch(() => ({ error: `HTTP ${res.status}` }));
|
|
594
|
+
throw new Error(
|
|
595
|
+
body.error || `Transcription failed (${res.status})`,
|
|
596
|
+
);
|
|
597
|
+
}
|
|
598
|
+
if (cancelledRef.current) {
|
|
599
|
+
cancelledRef.current = false;
|
|
600
|
+
setState("idle");
|
|
601
|
+
return;
|
|
602
|
+
}
|
|
603
|
+
const data = (await res.json()) as { text?: string };
|
|
604
|
+
const text = (data.text ?? "").trim();
|
|
605
|
+
if (text) {
|
|
606
|
+
onTranscriptRef.current?.(text);
|
|
607
|
+
} else if (liveSnapshot) {
|
|
608
|
+
onTranscriptRef.current?.(liveSnapshot.trim());
|
|
609
|
+
}
|
|
610
|
+
setState("idle");
|
|
611
|
+
} catch (err) {
|
|
612
|
+
if (cancelledRef.current) {
|
|
613
|
+
cancelledRef.current = false;
|
|
614
|
+
setState("idle");
|
|
615
|
+
return;
|
|
616
|
+
}
|
|
617
|
+
if (liveSnapshot) {
|
|
618
|
+
onTranscriptRef.current?.(liveSnapshot.trim());
|
|
619
|
+
setState("idle");
|
|
620
|
+
} else {
|
|
621
|
+
failWith(
|
|
622
|
+
(err as Error)?.message ??
|
|
623
|
+
"Transcription failed. Check your voice transcription provider in settings.",
|
|
624
|
+
);
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
};
|
|
628
|
+
|
|
629
|
+
startMeter(stream);
|
|
630
|
+
startTimer();
|
|
631
|
+
setState("recording");
|
|
632
|
+
recorder.start();
|
|
633
|
+
|
|
634
|
+
// Start parallel Web Speech recognition for live preview text.
|
|
635
|
+
// This runs alongside MediaRecorder so the user sees words appear
|
|
636
|
+
// immediately while the server provider processes the full recording later.
|
|
637
|
+
const SpeechCtor = getSpeechRecognitionCtor();
|
|
638
|
+
if (SpeechCtor) {
|
|
639
|
+
const liveSpeech = new SpeechCtor();
|
|
640
|
+
liveSpeech.continuous = true;
|
|
641
|
+
liveSpeech.interimResults = true;
|
|
642
|
+
liveSpeech.lang =
|
|
643
|
+
(typeof navigator !== "undefined" && navigator.language) || "en-US";
|
|
644
|
+
liveSpeechRef.current = liveSpeech;
|
|
645
|
+
liveTextRef.current = "";
|
|
646
|
+
|
|
647
|
+
liveSpeech.onresult = (event: any) => {
|
|
648
|
+
let interim = "";
|
|
649
|
+
for (let i = event.resultIndex; i < event.results.length; i++) {
|
|
650
|
+
const result = event.results[i];
|
|
651
|
+
const text = result[0]?.transcript ?? "";
|
|
652
|
+
if (result.isFinal) {
|
|
653
|
+
liveTextRef.current += text;
|
|
654
|
+
} else {
|
|
655
|
+
interim += text;
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
onLiveUpdateRef.current?.(liveTextRef.current, interim);
|
|
659
|
+
};
|
|
660
|
+
|
|
661
|
+
liveSpeech.onend = () => {
|
|
662
|
+
if (liveSpeechRef.current === liveSpeech) {
|
|
663
|
+
try {
|
|
664
|
+
liveSpeech.start();
|
|
665
|
+
} catch {
|
|
666
|
+
/* ignore */
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
};
|
|
670
|
+
|
|
671
|
+
liveSpeech.onerror = () => {};
|
|
672
|
+
|
|
673
|
+
try {
|
|
674
|
+
liveSpeech.start();
|
|
675
|
+
} catch {
|
|
676
|
+
/* best effort — live preview just won't appear */
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
},
|
|
680
|
+
[
|
|
681
|
+
startMeter,
|
|
682
|
+
startTimer,
|
|
683
|
+
teardown,
|
|
684
|
+
failWith,
|
|
685
|
+
getVoiceContextPack,
|
|
686
|
+
resolvePath,
|
|
687
|
+
],
|
|
688
|
+
);
|
|
689
|
+
|
|
690
|
+
const startBrowser = useCallback(
|
|
691
|
+
async (prefs: VoicePrefs) => {
|
|
692
|
+
const Ctor = getSpeechRecognitionCtor();
|
|
693
|
+
if (!Ctor) {
|
|
694
|
+
throw new Error(
|
|
695
|
+
"Your browser doesn't support speech recognition. Add an OpenAI API key in settings for Whisper transcription.",
|
|
696
|
+
);
|
|
697
|
+
}
|
|
698
|
+
// Still request mic to drive the amplitude meter, so the UI doesn't look
|
|
699
|
+
// dead while the user talks. SpeechRecognition manages its own capture
|
|
700
|
+
// under the hood in most browsers.
|
|
701
|
+
let stream: MediaStream | null = null;
|
|
702
|
+
try {
|
|
703
|
+
stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
|
704
|
+
mediaStreamRef.current = stream;
|
|
705
|
+
startMeter(stream);
|
|
706
|
+
} catch {
|
|
707
|
+
/* non-fatal — recognition can still work without our analyser */
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
if (cancelledRef.current) {
|
|
711
|
+
if (stream) for (const track of stream.getTracks()) track.stop();
|
|
712
|
+
mediaStreamRef.current = null;
|
|
713
|
+
cancelledRef.current = false;
|
|
714
|
+
setState("idle");
|
|
715
|
+
return;
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
const recognition = new Ctor();
|
|
719
|
+
recognition.continuous = true;
|
|
720
|
+
recognition.interimResults = true;
|
|
721
|
+
recognition.lang =
|
|
722
|
+
(typeof navigator !== "undefined" && navigator.language) || "en-US";
|
|
723
|
+
speechRef.current = recognition;
|
|
724
|
+
speechTranscriptRef.current = "";
|
|
725
|
+
|
|
726
|
+
recognition.onresult = (event: any) => {
|
|
727
|
+
let interim = "";
|
|
728
|
+
for (let i = event.resultIndex; i < event.results.length; i++) {
|
|
729
|
+
const result = event.results[i];
|
|
730
|
+
const text = result[0]?.transcript ?? "";
|
|
731
|
+
if (result.isFinal) {
|
|
732
|
+
speechTranscriptRef.current += text;
|
|
733
|
+
} else {
|
|
734
|
+
interim += text;
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
onLiveUpdateRef.current?.(speechTranscriptRef.current, interim);
|
|
738
|
+
};
|
|
739
|
+
recognition.onerror = (event: any) => {
|
|
740
|
+
if (event?.error === "no-speech" || event?.error === "aborted") return;
|
|
741
|
+
failWith(voiceDictationSpeechErrorMessage(event?.error));
|
|
742
|
+
};
|
|
743
|
+
recognition.onend = () => {
|
|
744
|
+
const text = speechTranscriptRef.current.trim();
|
|
745
|
+
const wasCancelled = cancelledRef.current;
|
|
746
|
+
cancelledRef.current = false;
|
|
747
|
+
teardown();
|
|
748
|
+
if (wasCancelled || !text) {
|
|
749
|
+
setState("idle");
|
|
750
|
+
return;
|
|
751
|
+
}
|
|
752
|
+
void (async () => {
|
|
753
|
+
let finalText = text;
|
|
754
|
+
if (prefs.cleanupEnabled) {
|
|
755
|
+
setState("transcribing");
|
|
756
|
+
try {
|
|
757
|
+
finalText = await cleanupRecognizedText({
|
|
758
|
+
text,
|
|
759
|
+
provider: prefs.provider,
|
|
760
|
+
instructions: prefs.instructions,
|
|
761
|
+
contextPack: await getVoiceContextPack(),
|
|
762
|
+
resolvePath,
|
|
763
|
+
replaceContext,
|
|
764
|
+
});
|
|
765
|
+
} catch {
|
|
766
|
+
finalText = text;
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
if (!cancelledRef.current && finalText) {
|
|
770
|
+
onTranscriptRef.current?.(finalText);
|
|
771
|
+
}
|
|
772
|
+
cancelledRef.current = false;
|
|
773
|
+
setState("idle");
|
|
774
|
+
})();
|
|
775
|
+
};
|
|
776
|
+
|
|
777
|
+
startTimer();
|
|
778
|
+
setState("recording");
|
|
779
|
+
recognition.start();
|
|
780
|
+
},
|
|
781
|
+
[
|
|
782
|
+
startMeter,
|
|
783
|
+
startTimer,
|
|
784
|
+
teardown,
|
|
785
|
+
failWith,
|
|
786
|
+
getVoiceContextPack,
|
|
787
|
+
replaceContext,
|
|
788
|
+
resolvePath,
|
|
789
|
+
],
|
|
790
|
+
);
|
|
791
|
+
|
|
792
|
+
const startGoogleRealtime = useCallback(
|
|
793
|
+
async (prefs: VoicePrefs) => {
|
|
794
|
+
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
|
795
|
+
if (cancelledRef.current) {
|
|
796
|
+
for (const track of stream.getTracks()) track.stop();
|
|
797
|
+
cancelledRef.current = false;
|
|
798
|
+
setState("idle");
|
|
799
|
+
return;
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
const sessionRes = await fetch(resolvePath(GOOGLE_REALTIME_SESSION_URL), {
|
|
803
|
+
method: "POST",
|
|
804
|
+
headers: { "Content-Type": "application/json" },
|
|
805
|
+
body: JSON.stringify({
|
|
806
|
+
language:
|
|
807
|
+
(typeof navigator !== "undefined" && navigator.language) || "en-US",
|
|
808
|
+
}),
|
|
809
|
+
});
|
|
810
|
+
const sessionBody = (await sessionRes
|
|
811
|
+
.json()
|
|
812
|
+
.catch(() => ({ error: `HTTP ${sessionRes.status}` }))) as {
|
|
813
|
+
websocketUrl?: string;
|
|
814
|
+
websocketProtocol?: string;
|
|
815
|
+
sessionToken?: string;
|
|
816
|
+
error?: string;
|
|
817
|
+
};
|
|
818
|
+
if (
|
|
819
|
+
!sessionRes.ok ||
|
|
820
|
+
!sessionBody.websocketUrl ||
|
|
821
|
+
!sessionBody.sessionToken
|
|
822
|
+
) {
|
|
823
|
+
for (const track of stream.getTracks()) track.stop();
|
|
824
|
+
throw new Error(
|
|
825
|
+
sessionBody.error ||
|
|
826
|
+
`Could not start Google realtime transcription (${sessionRes.status})`,
|
|
827
|
+
);
|
|
828
|
+
}
|
|
829
|
+
if (cancelledRef.current) {
|
|
830
|
+
for (const track of stream.getTracks()) track.stop();
|
|
831
|
+
cancelledRef.current = false;
|
|
832
|
+
setState("idle");
|
|
833
|
+
return;
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
mediaStreamRef.current = stream;
|
|
837
|
+
const mimeType = pickMimeType();
|
|
838
|
+
const recorder = new MediaRecorder(stream, { mimeType });
|
|
839
|
+
mediaRecorderRef.current = recorder;
|
|
840
|
+
realtimeFinalRef.current = "";
|
|
841
|
+
realtimeInterimRef.current = "";
|
|
842
|
+
|
|
843
|
+
let finalized = false;
|
|
844
|
+
let recorderStarted = false;
|
|
845
|
+
const finish = (errorMessage?: string) => {
|
|
846
|
+
if (finalized) return;
|
|
847
|
+
finalized = true;
|
|
848
|
+
void (async () => {
|
|
849
|
+
const finalText = realtimeFinalRef.current.trim();
|
|
850
|
+
const interimText = realtimeInterimRef.current.trim();
|
|
851
|
+
const text = [finalText, interimText]
|
|
852
|
+
.filter(Boolean)
|
|
853
|
+
.join(" ")
|
|
854
|
+
.trim();
|
|
855
|
+
teardown();
|
|
856
|
+
if (cancelledRef.current) {
|
|
857
|
+
cancelledRef.current = false;
|
|
858
|
+
setState("idle");
|
|
859
|
+
return;
|
|
860
|
+
}
|
|
861
|
+
if (text) {
|
|
862
|
+
let cleanedText = text;
|
|
863
|
+
if (prefs.cleanupEnabled) {
|
|
864
|
+
setState("transcribing");
|
|
865
|
+
try {
|
|
866
|
+
cleanedText = await cleanupRecognizedText({
|
|
867
|
+
text,
|
|
868
|
+
provider: prefs.provider,
|
|
869
|
+
instructions: prefs.instructions,
|
|
870
|
+
contextPack: await getVoiceContextPack(),
|
|
871
|
+
resolvePath,
|
|
872
|
+
replaceContext,
|
|
873
|
+
});
|
|
874
|
+
} catch {
|
|
875
|
+
cleanedText = text;
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
if (!cancelledRef.current && cleanedText) {
|
|
879
|
+
onTranscriptRef.current?.(cleanedText);
|
|
880
|
+
}
|
|
881
|
+
cancelledRef.current = false;
|
|
882
|
+
setState("idle");
|
|
883
|
+
return;
|
|
884
|
+
}
|
|
885
|
+
if (errorMessage) {
|
|
886
|
+
failWith(errorMessage);
|
|
887
|
+
return;
|
|
888
|
+
}
|
|
889
|
+
setState("idle");
|
|
890
|
+
})();
|
|
891
|
+
};
|
|
892
|
+
|
|
893
|
+
recorder.ondataavailable = async (event) => {
|
|
894
|
+
if (!event.data || event.data.size === 0) return;
|
|
895
|
+
const socket = realtimeSocketRef.current;
|
|
896
|
+
if (!socket || socket.readyState !== WebSocket.OPEN) return;
|
|
897
|
+
try {
|
|
898
|
+
socket.send(await event.data.arrayBuffer());
|
|
899
|
+
} catch (err) {
|
|
900
|
+
finish((err as Error)?.message ?? "Realtime audio upload failed");
|
|
901
|
+
}
|
|
902
|
+
};
|
|
903
|
+
|
|
904
|
+
recorder.onstop = () => {
|
|
905
|
+
const socket = realtimeSocketRef.current;
|
|
906
|
+
if (!socket || socket.readyState !== WebSocket.OPEN) {
|
|
907
|
+
finish();
|
|
908
|
+
return;
|
|
909
|
+
}
|
|
910
|
+
setState("transcribing");
|
|
911
|
+
try {
|
|
912
|
+
socket.send(JSON.stringify({ type: "stop" }));
|
|
913
|
+
} catch {
|
|
914
|
+
finish();
|
|
915
|
+
return;
|
|
916
|
+
}
|
|
917
|
+
realtimeStopTimeoutRef.current = window.setTimeout(() => {
|
|
918
|
+
const activeSocket = realtimeSocketRef.current;
|
|
919
|
+
if (activeSocket?.readyState === WebSocket.OPEN) {
|
|
920
|
+
try {
|
|
921
|
+
activeSocket.close();
|
|
922
|
+
} catch {
|
|
923
|
+
finish();
|
|
924
|
+
}
|
|
925
|
+
return;
|
|
926
|
+
}
|
|
927
|
+
finish();
|
|
928
|
+
}, 10000);
|
|
929
|
+
};
|
|
930
|
+
|
|
931
|
+
const socket = new WebSocket(sessionBody.websocketUrl, [
|
|
932
|
+
sessionBody.websocketProtocol || GOOGLE_REALTIME_WS_PROTOCOL,
|
|
933
|
+
sessionBody.sessionToken,
|
|
934
|
+
]);
|
|
935
|
+
socket.binaryType = "arraybuffer";
|
|
936
|
+
realtimeSocketRef.current = socket;
|
|
937
|
+
|
|
938
|
+
socket.onmessage = (event) => {
|
|
939
|
+
if (cancelledRef.current) {
|
|
940
|
+
finish();
|
|
941
|
+
return;
|
|
942
|
+
}
|
|
943
|
+
const raw =
|
|
944
|
+
typeof event.data === "string"
|
|
945
|
+
? event.data
|
|
946
|
+
: event.data instanceof Blob
|
|
947
|
+
? null
|
|
948
|
+
: new TextDecoder().decode(event.data);
|
|
949
|
+
if (!raw) return;
|
|
950
|
+
let message:
|
|
951
|
+
| {
|
|
952
|
+
type?: string;
|
|
953
|
+
text?: string;
|
|
954
|
+
error?: string;
|
|
955
|
+
}
|
|
956
|
+
| undefined;
|
|
957
|
+
try {
|
|
958
|
+
message = JSON.parse(raw);
|
|
959
|
+
} catch {
|
|
960
|
+
return;
|
|
961
|
+
}
|
|
962
|
+
if (!message?.type) return;
|
|
963
|
+
if (message.type === "ready") {
|
|
964
|
+
if (!recorderStarted) {
|
|
965
|
+
recorderStarted = true;
|
|
966
|
+
startMeter(stream);
|
|
967
|
+
startTimer();
|
|
968
|
+
setState("recording");
|
|
969
|
+
recorder.start(100);
|
|
970
|
+
}
|
|
971
|
+
return;
|
|
972
|
+
}
|
|
973
|
+
if (message.type === "partial") {
|
|
974
|
+
realtimeInterimRef.current = (message.text ?? "").trim();
|
|
975
|
+
onLiveUpdateRef.current?.(
|
|
976
|
+
realtimeFinalRef.current,
|
|
977
|
+
realtimeInterimRef.current,
|
|
978
|
+
);
|
|
979
|
+
return;
|
|
980
|
+
}
|
|
981
|
+
if (message.type === "final") {
|
|
982
|
+
const next = (message.text ?? "").trim();
|
|
983
|
+
if (next) {
|
|
984
|
+
realtimeFinalRef.current = [realtimeFinalRef.current.trim(), next]
|
|
985
|
+
.filter(Boolean)
|
|
986
|
+
.join(" ");
|
|
987
|
+
}
|
|
988
|
+
realtimeInterimRef.current = "";
|
|
989
|
+
onLiveUpdateRef.current?.(realtimeFinalRef.current, "");
|
|
990
|
+
return;
|
|
991
|
+
}
|
|
992
|
+
if (message.type === "error") {
|
|
993
|
+
finish(message.error || "Google realtime transcription failed");
|
|
994
|
+
return;
|
|
995
|
+
}
|
|
996
|
+
if (message.type === "end") {
|
|
997
|
+
finish();
|
|
998
|
+
}
|
|
999
|
+
};
|
|
1000
|
+
|
|
1001
|
+
socket.onerror = () => {
|
|
1002
|
+
finish("Google realtime transcription connection failed");
|
|
1003
|
+
};
|
|
1004
|
+
|
|
1005
|
+
socket.onclose = () => {
|
|
1006
|
+
finish();
|
|
1007
|
+
};
|
|
1008
|
+
|
|
1009
|
+
socket.onopen = () => {
|
|
1010
|
+
if (cancelledRef.current) {
|
|
1011
|
+
finish();
|
|
1012
|
+
return;
|
|
1013
|
+
}
|
|
1014
|
+
try {
|
|
1015
|
+
socket.send(
|
|
1016
|
+
JSON.stringify({
|
|
1017
|
+
type: "start",
|
|
1018
|
+
language:
|
|
1019
|
+
(typeof navigator !== "undefined" && navigator.language) ||
|
|
1020
|
+
"en-US",
|
|
1021
|
+
interimResults: true,
|
|
1022
|
+
mimeType,
|
|
1023
|
+
}),
|
|
1024
|
+
);
|
|
1025
|
+
} catch (err) {
|
|
1026
|
+
finish((err as Error)?.message ?? "Could not start realtime stream");
|
|
1027
|
+
}
|
|
1028
|
+
};
|
|
1029
|
+
},
|
|
1030
|
+
[
|
|
1031
|
+
startMeter,
|
|
1032
|
+
startTimer,
|
|
1033
|
+
teardown,
|
|
1034
|
+
failWith,
|
|
1035
|
+
getVoiceContextPack,
|
|
1036
|
+
replaceContext,
|
|
1037
|
+
resolvePath,
|
|
1038
|
+
],
|
|
1039
|
+
);
|
|
1040
|
+
|
|
1041
|
+
const start = useCallback(async () => {
|
|
1042
|
+
if (state === "recording" || state === "starting") return;
|
|
1043
|
+
setErrorMessage(null);
|
|
1044
|
+
setState("starting");
|
|
1045
|
+
cancelledRef.current = false;
|
|
1046
|
+
|
|
1047
|
+
const prefs = await readVoicePrefs(resolvePath);
|
|
1048
|
+
const pref = prefs.provider;
|
|
1049
|
+
setProvider(pref);
|
|
1050
|
+
|
|
1051
|
+
// In "auto" mode, prefer browser-native SpeechRecognition when available.
|
|
1052
|
+
// It requires no server-side API key, streams words incrementally into the
|
|
1053
|
+
// composer, and matches the macros-app record-button experience. Fall back
|
|
1054
|
+
// to the server upload path only when SpeechRecognition isn't supported.
|
|
1055
|
+
// Explicit server providers (builder, gemini, groq, openai) always use the
|
|
1056
|
+
// MediaRecorder → server upload path regardless.
|
|
1057
|
+
const resolvedProvider: VoiceProvider =
|
|
1058
|
+
pref === "auto" && speechSupported
|
|
1059
|
+
? "browser"
|
|
1060
|
+
: pref === "auto" ||
|
|
1061
|
+
pref === "builder" ||
|
|
1062
|
+
pref === "builder-gemini" ||
|
|
1063
|
+
pref === "gemini" ||
|
|
1064
|
+
pref === "groq"
|
|
1065
|
+
? "openai"
|
|
1066
|
+
: pref;
|
|
1067
|
+
activeProviderRef.current = resolvedProvider;
|
|
1068
|
+
|
|
1069
|
+
try {
|
|
1070
|
+
if (resolvedProvider === "openai") {
|
|
1071
|
+
if (!mediaRecorderSupported) {
|
|
1072
|
+
throw new Error(
|
|
1073
|
+
"Your browser doesn't support audio recording. Use the browser provider in Settings → Voice Transcription.",
|
|
1074
|
+
);
|
|
1075
|
+
}
|
|
1076
|
+
await startOpenAi(pref, prefs.instructions);
|
|
1077
|
+
} else if (resolvedProvider === "google-realtime") {
|
|
1078
|
+
if (!mediaRecorderSupported) {
|
|
1079
|
+
throw new Error(
|
|
1080
|
+
"Your browser doesn't support audio recording, so Google realtime transcription can't start.",
|
|
1081
|
+
);
|
|
1082
|
+
}
|
|
1083
|
+
await startGoogleRealtime(prefs);
|
|
1084
|
+
} else {
|
|
1085
|
+
await startBrowser(prefs);
|
|
1086
|
+
}
|
|
1087
|
+
} catch (err) {
|
|
1088
|
+
if (cancelledRef.current) {
|
|
1089
|
+
cancelledRef.current = false;
|
|
1090
|
+
teardown();
|
|
1091
|
+
setState("idle");
|
|
1092
|
+
return;
|
|
1093
|
+
}
|
|
1094
|
+
failWith(voiceDictationStartErrorMessage(err));
|
|
1095
|
+
}
|
|
1096
|
+
}, [
|
|
1097
|
+
state,
|
|
1098
|
+
speechSupported,
|
|
1099
|
+
mediaRecorderSupported,
|
|
1100
|
+
startOpenAi,
|
|
1101
|
+
startGoogleRealtime,
|
|
1102
|
+
startBrowser,
|
|
1103
|
+
failWith,
|
|
1104
|
+
]);
|
|
1105
|
+
|
|
1106
|
+
const stop = useCallback(() => {
|
|
1107
|
+
if (state !== "recording") return;
|
|
1108
|
+
cancelledRef.current = false;
|
|
1109
|
+
if (
|
|
1110
|
+
(activeProviderRef.current === "openai" ||
|
|
1111
|
+
activeProviderRef.current === "google-realtime") &&
|
|
1112
|
+
mediaRecorderRef.current
|
|
1113
|
+
) {
|
|
1114
|
+
try {
|
|
1115
|
+
mediaRecorderRef.current.stop();
|
|
1116
|
+
} catch {
|
|
1117
|
+
teardown();
|
|
1118
|
+
setState("idle");
|
|
1119
|
+
}
|
|
1120
|
+
} else if (speechRef.current) {
|
|
1121
|
+
try {
|
|
1122
|
+
speechRef.current.stop();
|
|
1123
|
+
} catch {
|
|
1124
|
+
teardown();
|
|
1125
|
+
setState("idle");
|
|
1126
|
+
}
|
|
1127
|
+
} else {
|
|
1128
|
+
teardown();
|
|
1129
|
+
setState("idle");
|
|
1130
|
+
}
|
|
1131
|
+
}, [state, teardown]);
|
|
1132
|
+
|
|
1133
|
+
const cancel = useCallback(() => {
|
|
1134
|
+
if (
|
|
1135
|
+
state !== "recording" &&
|
|
1136
|
+
state !== "starting" &&
|
|
1137
|
+
state !== "transcribing"
|
|
1138
|
+
)
|
|
1139
|
+
return;
|
|
1140
|
+
cancelledRef.current = true;
|
|
1141
|
+
if (
|
|
1142
|
+
(activeProviderRef.current === "openai" ||
|
|
1143
|
+
activeProviderRef.current === "google-realtime") &&
|
|
1144
|
+
mediaRecorderRef.current
|
|
1145
|
+
) {
|
|
1146
|
+
try {
|
|
1147
|
+
mediaRecorderRef.current.stop();
|
|
1148
|
+
} catch {
|
|
1149
|
+
/* ignore */
|
|
1150
|
+
}
|
|
1151
|
+
} else if (speechRef.current) {
|
|
1152
|
+
try {
|
|
1153
|
+
speechRef.current.abort?.();
|
|
1154
|
+
} catch {
|
|
1155
|
+
/* ignore */
|
|
1156
|
+
}
|
|
1157
|
+
}
|
|
1158
|
+
teardown();
|
|
1159
|
+
setState("idle");
|
|
1160
|
+
}, [state, teardown]);
|
|
1161
|
+
|
|
1162
|
+
// Auto-dismiss error after 8s so a stale "permission denied" message doesn't
|
|
1163
|
+
// sit forever after the user fixes the underlying permission. Manual dismiss
|
|
1164
|
+
// (via dismissError) and click-to-retry both also clear the error sooner.
|
|
1165
|
+
useEffect(() => {
|
|
1166
|
+
if (state !== "error") return;
|
|
1167
|
+
const handle = setTimeout(() => {
|
|
1168
|
+
setErrorMessage(null);
|
|
1169
|
+
setState("idle");
|
|
1170
|
+
}, 8000);
|
|
1171
|
+
return () => clearTimeout(handle);
|
|
1172
|
+
}, [state]);
|
|
1173
|
+
|
|
1174
|
+
const dismissError = useCallback(() => {
|
|
1175
|
+
setErrorMessage(null);
|
|
1176
|
+
if (state === "error") setState("idle");
|
|
1177
|
+
}, [state]);
|
|
1178
|
+
|
|
1179
|
+
return {
|
|
1180
|
+
state,
|
|
1181
|
+
amplitude,
|
|
1182
|
+
durationMs,
|
|
1183
|
+
errorMessage,
|
|
1184
|
+
provider,
|
|
1185
|
+
supported,
|
|
1186
|
+
start,
|
|
1187
|
+
stop,
|
|
1188
|
+
cancel,
|
|
1189
|
+
dismissError,
|
|
1190
|
+
};
|
|
1191
|
+
}
|