diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0d479d41e31..82a8d9ba6dd 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -408,6 +408,7 @@ jobs: cargo +1.95.0 test --manifest-path apps/desktop-gpui/Cargo.toml --locked --target ${{ matrix.settings.target }} -p cap-gpui-kernels cargo +1.95.0 test --manifest-path apps/desktop-gpui/Cargo.toml --locked --target ${{ matrix.settings.target }} -p cap-glass cargo +1.95.0 test --manifest-path apps/desktop-gpui/Cargo.toml --locked --target ${{ matrix.settings.target }} -p cap-i18n-build + cargo +1.95.0 test --manifest-path apps/desktop-gpui/Cargo.toml --locked --target ${{ matrix.settings.target }} -p cap-notes -p cap-calendar -p cap-meetings -p cap-meeting-platform -p cap-notes-ui -p cap-diarize # Cap only builds gpui's crates as dependencies, so their own tests never run # in the app jobs above. Run the ones covering Cap's patches against real diff --git a/.github/workflows/infra-database.yml b/.github/workflows/infra-database.yml index 6df49f21433..6decf581232 100644 --- a/.github/workflows/infra-database.yml +++ b/.github/workflows/infra-database.yml @@ -13,6 +13,7 @@ on: - "apps/web/app/api/developer/**" - "apps/web/app/api/cron/developer-storage/**" - "apps/web/lib/user-preferences-sql.ts" + - "apps/web/lib/meetings/trial.ts" - "apps/web/lib/media-input-policy-strikes.ts" - "apps/web/lib/organization-member-removal.ts" - "packages/web-backend/src/Spaces/**" @@ -44,6 +45,7 @@ on: - "apps/web/app/api/developer/**" - "apps/web/app/api/cron/developer-storage/**" - "apps/web/lib/user-preferences-sql.ts" + - "apps/web/lib/meetings/trial.ts" - "apps/web/lib/media-input-policy-strikes.ts" - "apps/web/lib/organization-member-removal.ts" - "packages/web-backend/src/Spaces/**" @@ -141,4 +143,4 @@ jobs: CAP_INFRA_TEST_DATABASE_URL: mysql://root@127.0.0.1:3306/cap_infra_tests CAP_INFRA_ROUND_TRIPS_DATABASE_URL: mysql://root@127.0.0.1:3306/cap_infra_tests CAP_PASSKEYS_TEST_DATABASE_URL: mysql://root@127.0.0.1:3306/cap_infra_tests - run: bun run vitest run __tests__/integration/infra-*-database.test.ts __tests__/integration/user-preferences-database.test.ts __tests__/integration/space-creator-membership-database.test.ts __tests__/integration/dashboard-library-database.test.ts __tests__/integration/messenger-poll-database.test.ts __tests__/integration/passkeys-database.test.ts __tests__/integration/email-change-database.test.ts + run: bun run vitest run __tests__/integration/infra-*-database.test.ts __tests__/integration/user-preferences-database.test.ts __tests__/integration/space-creator-membership-database.test.ts __tests__/integration/dashboard-library-database.test.ts __tests__/integration/messenger-poll-database.test.ts __tests__/integration/passkeys-database.test.ts __tests__/integration/email-change-database.test.ts __tests__/integration/meetings-trial-database.test.ts diff --git a/.github/workflows/meetings-reliability.yml b/.github/workflows/meetings-reliability.yml new file mode 100644 index 00000000000..0a7fe3ea2f2 --- /dev/null +++ b/.github/workflows/meetings-reliability.yml @@ -0,0 +1,252 @@ +name: Meetings reliability (real app) +# Runs a scripted two minute meeting (apps/desktop-gpui/tests/fixtures/meetings) through the real +# app on Linux and Windows and checks what it made: both sides recorded through the OS audio stack, +# the transcript against the script, one voice per person with the user as You, notes written, a +# question answered through the Ask bar, a Cloud meeting's clips, and the live indicator's banner +# staying on screen. The account is a Pro account against a local stub of the meeting endpoints +# (scripts/ci/meetings/stub_server.py); nothing reaches a Cap server or an AI provider. On Windows +# the call's voices must also be named after the call page's tiles, read through UI Automation. + +on: + pull_request: + types: [opened, synchronize, reopened, ready_for_review] + paths: + - "apps/desktop-gpui/src/meetings.rs" + - "apps/desktop-gpui/src/meetings/**" + - "apps/desktop-gpui/src/meetings_ui/**" + - "apps/desktop-gpui/src/transcription.rs" + - "apps/desktop-gpui/src/transcription/**" + - "apps/desktop-gpui/meetings/**" + - "apps/desktop-gpui/meeting-platform/**" + - "apps/desktop-gpui/diarize/**" + - "apps/desktop-gpui/tests/fixtures/meetings/**" + - "scripts/ci/meetings/**" + - ".github/workflows/meetings-reliability.yml" + schedule: + - cron: "10 5 * * *" + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: meetings-reliability-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: true + +env: + # Release speed for live transcription, with the debug assertions the scripted-meeting hooks need. + CARGO_PROFILE_RELEASE_DEBUG: "0" + CARGO_PROFILE_RELEASE_DEBUG_ASSERTIONS: "true" + +jobs: + linux: + name: Meetings on Linux (real app) + if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository + runs-on: blacksmith-8vcpu-ubuntu-2404 + timeout-minutes: 75 + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Install Linux desktop dependencies + uses: ./.github/actions/install-desktop-deps + + - name: Install the meeting harness tools + run: | + sudo apt-get install -y -qq --no-install-recommends xvfb x11-utils xdotool imagemagick dbus \ + pulseaudio pulseaudio-utils libasound2-plugins ffmpeg openssl openbox > /dev/null + if ! command -v google-chrome > /dev/null; then + curl -fsSL -o "$RUNNER_TEMP/chrome.deb" https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb + sudo apt-get install -y -qq "$RUNNER_TEMP/chrome.deb" > /dev/null + fi + google-chrome --version + + - name: Rust setup + uses: dtolnay/rust-toolchain@1.95.0 + with: + targets: x86_64-unknown-linux-gnu + + - uses: ./.github/actions/setup-rust-cache + with: + target: x86_64-unknown-linux-gnu + prefix-key: v1-rust-meetings + workspaces: apps/desktop-gpui -> target + + - name: Run setup + run: node scripts/setup.js + env: + RUST_TARGET_TRIPLE: x86_64-unknown-linux-gnu + + - name: Prepare GPUI dependency + run: | + node scripts/sync-desktop-versions.mjs + ./scripts/prepare-gpui-dependency.sh + + - name: Build the app + run: cargo +1.95.0 build --manifest-path apps/desktop-gpui/Cargo.toml --locked --release --bin cap-gpui + + - name: Restore the meeting models + uses: actions/cache@v4 + with: + path: ${{ runner.temp }}/meeting-models + key: meeting-models-v1-${{ hashFiles('scripts/ci/meetings/models.sh') }} + + - name: Download the meeting models + run: bash scripts/ci/meetings/models.sh "$RUNNER_TEMP/meeting-models" + + - name: Scripted meetings on Xvfb + run: | + env -u WAYLAND_DISPLAY XDG_SESSION_TYPE=x11 \ + LD_LIBRARY_PATH="$GITHUB_WORKSPACE/target/native-deps/lib" \ + ORT_DYLIB_PATH="$GITHUB_WORKSPACE/target/native-deps/onnxruntime/lib/libonnxruntime.so" \ + CAP_MEETINGS_MODELS="$RUNNER_TEMP/meeting-models" \ + dbus-run-session -- bash scripts/ci/meetings/linux.sh \ + "$GITHUB_WORKSPACE/apps/desktop-gpui/target/release/cap-gpui" "$RUNNER_TEMP/meetings" + + - name: Upload the runs + if: always() + uses: actions/upload-artifact@v4 + with: + name: meetings-reliability-linux + path: | + ${{ runner.temp }}/meetings/*/check.txt + ${{ runner.temp }}/meetings/*/score.json + ${{ runner.temp }}/meetings/*/stub.jsonl + ${{ runner.temp }}/meetings/*/page_started.log + ${{ runner.temp }}/meetings/*/app.log + ${{ runner.temp }}/meetings/*/shots/*.png + ${{ runner.temp }}/meetings/indicator/indicator.png + retention-days: 7 + + build-windows: + name: Build for Windows meetings + if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository + runs-on: blacksmith-8vcpu-windows-2025 + timeout-minutes: 75 + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Rust setup + uses: dtolnay/rust-toolchain@1.95.0 + with: + targets: x86_64-pc-windows-msvc + + - uses: ./.github/actions/setup-rust-cache + with: + target: x86_64-pc-windows-msvc + prefix-key: v1-rust-meetings + workspaces: apps/desktop-gpui -> target + + - name: Run setup + shell: bash + run: node scripts/setup.js + env: + RUST_TARGET_TRIPLE: x86_64-pc-windows-msvc + + - name: Prepare GPUI dependency + shell: bash + run: | + node scripts/sync-desktop-versions.mjs + ./scripts/prepare-gpui-dependency.sh + + - name: Build the app + shell: bash + run: cargo +1.95.0 build --manifest-path apps/desktop-gpui/Cargo.toml --locked --release --bin cap-gpui + + # The build compiles GPUI's shaders at startup from its source tree in ../zed-cap, by + # absolute path: the run job puts them back there (as picker-smoke.yml does). + - name: Stage the app + shell: bash + run: | + stage="$RUNNER_TEMP/stage" + mkdir -p "$stage/gpui-windows-shaders" + cp apps/desktop-gpui/target/release/cap-gpui.exe "$stage/" + cp ../zed-cap/crates/gpui_windows/src/*.hlsl "$stage/gpui-windows-shaders/" + printf '%s' "$GITHUB_WORKSPACE" > "$stage/build-workspace.txt" + printf '%s' "$(cd ../zed-cap && pwd -W)" > "$stage/build-zed.txt" + + - uses: actions/upload-artifact@v4 + with: + name: meetings-windows-app + path: ${{ runner.temp }}/stage + retention-days: 1 + + windows: + name: Meetings on Windows (real app) + needs: build-windows + runs-on: windows-2022 + timeout-minutes: 45 + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - uses: actions/download-artifact@v4 + with: + name: meetings-windows-app + path: ${{ runner.temp }}/app + + - name: Put the build's source files back at its paths + shell: pwsh + run: | + $app = "$env:RUNNER_TEMP\app" + $workspace = (Get-Content "$app\build-workspace.txt" -Raw).Trim() + if ($workspace -ne $env:GITHUB_WORKSPACE) { + New-Item -ItemType Directory -Force (Split-Path $workspace) | Out-Null + cmd /c mklink /J "$workspace" "$env:GITHUB_WORKSPACE" + if ($LASTEXITCODE -ne 0) { throw "mklink failed" } + } + $shaders = Join-Path (Get-Content "$app\build-zed.txt" -Raw).Trim() "crates\gpui_windows\src" + New-Item -ItemType Directory -Force $shaders | Out-Null + Copy-Item "$app\gpui-windows-shaders\*.hlsl" $shaders + + # FFmpeg and ONNX Runtime DLLs, next to the app. + - name: Run setup + shell: bash + run: node scripts/setup.js + env: + RUST_TARGET_TRIPLE: x86_64-pc-windows-msvc + + - name: Virtual sound card + shell: pwsh + run: scripts/ci/meetings/setup_audio.ps1 + + - name: Harness tools + shell: pwsh + run: | + if (-not (Get-Command ffmpeg -ErrorAction SilentlyContinue)) { choco install ffmpeg -y --no-progress | Out-Null } + ffmpeg -version | Select-Object -First 1 + + - name: Restore the meeting models + uses: actions/cache@v4 + with: + path: ${{ runner.temp }}/meeting-models + key: meeting-models-v1-${{ hashFiles('scripts/ci/meetings/models.sh') }} + + - name: Download the meeting models + shell: bash + run: bash scripts/ci/meetings/models.sh "$RUNNER_TEMP/meeting-models" + + - name: Scripted meetings + shell: pwsh + run: | + $bin = "$env:RUNNER_TEMP\app\cap-gpui.exe" + Copy-Item "$env:GITHUB_WORKSPACE\target\release\*.dll" "$env:RUNNER_TEMP\app\" + $env:CAP_MEETINGS_MODELS = "$env:RUNNER_TEMP\meeting-models" + & scripts/ci/meetings/windows.ps1 -Bin $bin -Work "$env:RUNNER_TEMP\meetings" + exit $LASTEXITCODE + + - name: Upload the runs + if: always() + uses: actions/upload-artifact@v4 + with: + name: meetings-reliability-windows + path: | + ${{ runner.temp }}/meetings/*/check.txt + ${{ runner.temp }}/meetings/*/score.json + ${{ runner.temp }}/meetings/*/stub.jsonl + ${{ runner.temp }}/meetings/*/page_started.log + ${{ runner.temp }}/meetings/*/app.log + ${{ runner.temp }}/meetings/*/shots/*.png + ${{ runner.temp }}/meetings/indicator/indicator.png + retention-days: 7 diff --git a/apps/desktop-gpui/Cargo.lock b/apps/desktop-gpui/Cargo.lock index 33ea76a47c0..62f920e18b0 100644 --- a/apps/desktop-gpui/Cargo.lock +++ b/apps/desktop-gpui/Cargo.lock @@ -1412,6 +1412,21 @@ dependencies = [ "workspace-hack", ] +[[package]] +name = "cap-calendar" +version = "0.1.0" +dependencies = [ + "base64 0.22.1", + "chrono", + "chrono-tz", + "rrule", + "serde", + "serde_json", + "sha2", + "thiserror 2.0.20", + "url", +] + [[package]] name = "cap-camera" version = "0.1.0" @@ -1560,10 +1575,12 @@ dependencies = [ "bytes", "cap-audio", "cap-automation", + "cap-calendar", "cap-camera", "cap-camera-effects", "cap-cli-install", "cap-cursor-info", + "cap-diarize", "cap-editor", "cap-enc-ffmpeg", "cap-export", @@ -1571,6 +1588,10 @@ dependencies = [ "cap-gpui-kernels", "cap-i18n-build", "cap-media-info", + "cap-meeting-platform", + "cap-meetings", + "cap-notes", + "cap-notes-ui", "cap-project", "cap-recording", "cap-rendering", @@ -1590,16 +1611,19 @@ dependencies = [ "ffmpeg-next", "flume 0.11.1", "futures-util", + "getrandom 0.2.17", "global-hotkey", "gpui", "gpui_platform", "gpui_tokio", + "iana-time-zone", "image 0.25.10", "jpeg-decoder", "jxl-oxide", "kameo", "libc", "lyon_tessellation", + "memmap2", "minisign-verify", "notify-rust", "objc2 0.6.5", @@ -1616,6 +1640,7 @@ dependencies = [ "rodio", "same-file", "sanitize-filename", + "scap-cpal", "scap-targets", "semver", "serde", @@ -1640,6 +1665,19 @@ dependencies = [ "x11rb", ] +[[package]] +name = "cap-diarize" +version = "0.1.0" +dependencies = [ + "cap-meetings", + "ort", + "realfft", + "serde", + "serde_json", + "tempfile", + "thiserror 2.0.20", +] + [[package]] name = "cap-editor" version = "0.1.0" @@ -1869,6 +1907,36 @@ dependencies = [ "workspace-hack", ] +[[package]] +name = "cap-meeting-platform" +version = "0.1.0" +dependencies = [ + "block2 0.6.2", + "cidre", + "flume 0.11.1", + "libc", + "objc2 0.6.5", + "objc2-event-kit", + "objc2-foundation 0.3.2", + "thiserror 2.0.20", + "tracing", + "windows 0.61.3", + "windows-sys 0.59.0", +] + +[[package]] +name = "cap-meetings" +version = "0.1.0" +dependencies = [ + "cap-diarize", + "chrono", + "serde", + "serde_json", + "tempfile", + "thiserror 2.0.20", + "unicode-segmentation", +] + [[package]] name = "cap-muxer-protocol" version = "0.1.0" @@ -1878,6 +1946,25 @@ dependencies = [ "thiserror 1.0.69", ] +[[package]] +name = "cap-notes" +version = "0.1.0" +dependencies = [ + "serde", + "serde_json", + "unicode-segmentation", +] + +[[package]] +name = "cap-notes-ui" +version = "0.1.0" +dependencies = [ + "cap-notes", + "gpui", + "gpui_platform", + "unicode-segmentation", +] + [[package]] name = "cap-project" version = "0.1.0" @@ -2275,6 +2362,16 @@ dependencies = [ "windows-link 0.2.1", ] +[[package]] +name = "chrono-tz" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6139a8597ed92cf816dfb33f5dd6cf0bb93a6adc938f11039f371bc5bcd26c3" +dependencies = [ + "chrono", + "phf 0.12.1", +] + [[package]] name = "cidre" version = "0.11.0" @@ -7503,6 +7600,22 @@ version = "4.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ef25abbcd74fb2609453eb695bd2f860d389e457f67dc17cafc8b8cbc89d0c33" +[[package]] +name = "objc2-event-kit" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51bc9ad7325642172110196bacd6af64027ec5549ded7fc6589ea03e0f792bf8" +dependencies = [ + "bitflags 2.13.1", + "block2 0.6.2", + "objc2 0.6.5", + "objc2-app-kit 0.3.2", + "objc2-core-graphics", + "objc2-core-location", + "objc2-foundation 0.3.2", + "objc2-map-kit", +] + [[package]] name = "objc2-exception-helper" version = "0.1.1" @@ -7569,6 +7682,16 @@ dependencies = [ "objc2-core-foundation", ] +[[package]] +name = "objc2-map-kit" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "579edede1c244621cd8229b70ea6e20e6ec3bab5a74afdfd494b446b681e1e64" +dependencies = [ + "objc2 0.6.5", + "objc2-foundation 0.3.2", +] + [[package]] name = "objc2-media-toolbox" version = "0.3.2" @@ -8137,6 +8260,15 @@ dependencies = [ "phf_shared 0.11.3", ] +[[package]] +name = "phf" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "913273894cec178f401a31ec4b656318d95473527be05c0752cc41cdc32be8b7" +dependencies = [ + "phf_shared 0.12.1", +] + [[package]] name = "phf" version = "0.13.1" @@ -8262,6 +8394,15 @@ dependencies = [ "siphasher 1.0.3", ] +[[package]] +name = "phf_shared" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06005508882fb681fd97892ecff4b7fd0fee13ef1aa569f8695dae7ab9099981" +dependencies = [ + "siphasher 1.0.3", +] + [[package]] name = "phf_shared" version = "0.13.1" @@ -9493,6 +9634,19 @@ dependencies = [ "memchr", ] +[[package]] +name = "rrule" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "720acfb4980b9d8a6a430f6d7a11933e701ebbeba5eee39cc9d8c5f932aaff74" +dependencies = [ + "chrono", + "chrono-tz", + "log", + "regex", + "thiserror 2.0.20", +] + [[package]] name = "rustc-demangle" version = "0.1.28" diff --git a/apps/desktop-gpui/Cargo.toml b/apps/desktop-gpui/Cargo.toml index db0159e9937..e6c2b6a0e14 100644 --- a/apps/desktop-gpui/Cargo.toml +++ b/apps/desktop-gpui/Cargo.toml @@ -5,7 +5,7 @@ # gpui's tree does not want. [workspace] resolver = "2" -members = [".", "kernels", "i18n-build", "glass"] +members = [".", "kernels", "i18n-build", "glass", "notes", "notes-ui", "calendar", "meetings", "meeting-platform", "diarize"] [package] name = "cap-desktop-gpui" @@ -24,10 +24,18 @@ name = "studio-finalization-benchmark" path = "../../crates/recording/examples/studio-finalization-benchmark.rs" [dependencies] +memmap2 = "0.9" cap-audio = { path = "../../crates/audio" } cap-cli-install = { path = "../../crates/cli-install" } cap-gpui-kernels = { path = "kernels" } cap-glass = { path = "glass" } +cap-meetings = { path = "meetings" } +cap-meeting-platform = { path = "meeting-platform" } +cap-calendar = { path = "calendar" } +cap-diarize = { path = "diarize" } +cap-notes = { path = "notes" } +cap-notes-ui = { path = "notes-ui" } +iana-time-zone = "0.1" # gpui — the wingleeio/zed fork Comet ships. The pinned rev carries f596cde # ("destination alpha on transparent windows: Porter-Duff OVER, not additive"), # which our transparent rounded main-window shell depends on. @@ -137,6 +145,7 @@ chrono = "0.4" serde_json = "1" serde = { version = "1", features = ["derive"] } sanitize-filename = "0.6.0" +tempfile = "3" # Moves media of clips removed from a project to the OS trash, so the user # can still recover it. Its platform deps (objc2 0.6, windows 0.62) are # already in the tree. @@ -177,6 +186,7 @@ reqwest = { version = "0.12", default-features = false, features = [ "rustls-tls", ] } futures-util = "0.3" +getrandom = "0.2" dirs = "6" dunce = "1.0.5" # Caption transcription (`src/transcription.rs`): the engine pin 0.6.0 @@ -214,6 +224,7 @@ libc = "0.2" clipboard-rs = "0.2.4" raw-window-handle = "0.6" same-file = "1.0.6" +scap-cpal = { path = "../../crates/scap-cpal" } tray-icon = { version = "0.21.1", default-features = false } windows-sys = { version = "0.59", features = [ "Win32_Foundation", diff --git a/apps/desktop-gpui/Info.plist b/apps/desktop-gpui/Info.plist index 4939978f032..d5b34279754 100644 --- a/apps/desktop-gpui/Info.plist +++ b/apps/desktop-gpui/Info.plist @@ -37,6 +37,12 @@ Cap uses your camera to record you in your Caps NSMicrophoneUsageDescription Cap uses your microphone to record you in your Caps + NSAudioCaptureUsageDescription + Cap records your computer's audio output during meetings so everyone in the call is transcribed + NSCalendarsFullAccessUsageDescription + Cap shows your upcoming meetings so it can offer to take notes + NSCalendarsUsageDescription + Cap shows your upcoming meetings so it can offer to take notes NSScreenCaptureUsageDescription Cap captures your screen as part of recording your Caps NSCameraUseContinuityCameraDeviceType diff --git a/apps/desktop-gpui/assets/icons/arrow-down.svg b/apps/desktop-gpui/assets/icons/arrow-down.svg new file mode 100644 index 00000000000..f79668ce02c --- /dev/null +++ b/apps/desktop-gpui/assets/icons/arrow-down.svg @@ -0,0 +1 @@ + diff --git a/apps/desktop-gpui/assets/icons/audio-lines.svg b/apps/desktop-gpui/assets/icons/audio-lines.svg new file mode 100644 index 00000000000..d3af0f1119c --- /dev/null +++ b/apps/desktop-gpui/assets/icons/audio-lines.svg @@ -0,0 +1 @@ + diff --git a/apps/desktop-gpui/assets/icons/calendar.svg b/apps/desktop-gpui/assets/icons/calendar.svg new file mode 100644 index 00000000000..22b18bbd5e9 --- /dev/null +++ b/apps/desktop-gpui/assets/icons/calendar.svg @@ -0,0 +1 @@ + diff --git a/apps/desktop-gpui/assets/icons/cloud.svg b/apps/desktop-gpui/assets/icons/cloud.svg new file mode 100644 index 00000000000..5e72c1dadcd --- /dev/null +++ b/apps/desktop-gpui/assets/icons/cloud.svg @@ -0,0 +1 @@ + diff --git a/apps/desktop-gpui/assets/icons/corner-down-left.svg b/apps/desktop-gpui/assets/icons/corner-down-left.svg new file mode 100644 index 00000000000..45b009f1038 --- /dev/null +++ b/apps/desktop-gpui/assets/icons/corner-down-left.svg @@ -0,0 +1 @@ + diff --git a/apps/desktop-gpui/assets/icons/file-text.svg b/apps/desktop-gpui/assets/icons/file-text.svg new file mode 100644 index 00000000000..3d2b0a48600 --- /dev/null +++ b/apps/desktop-gpui/assets/icons/file-text.svg @@ -0,0 +1 @@ + diff --git a/apps/desktop-gpui/assets/icons/folder-plus.svg b/apps/desktop-gpui/assets/icons/folder-plus.svg new file mode 100644 index 00000000000..c679daec3f1 --- /dev/null +++ b/apps/desktop-gpui/assets/icons/folder-plus.svg @@ -0,0 +1,4 @@ + + + + diff --git a/apps/desktop-gpui/assets/icons/google.svg b/apps/desktop-gpui/assets/icons/google.svg new file mode 100644 index 00000000000..5ea88b48f7c --- /dev/null +++ b/apps/desktop-gpui/assets/icons/google.svg @@ -0,0 +1 @@ + diff --git a/apps/desktop-gpui/assets/icons/house.svg b/apps/desktop-gpui/assets/icons/house.svg new file mode 100644 index 00000000000..b77271bfdd3 --- /dev/null +++ b/apps/desktop-gpui/assets/icons/house.svg @@ -0,0 +1,4 @@ + + + + diff --git a/apps/desktop-gpui/assets/icons/languages.svg b/apps/desktop-gpui/assets/icons/languages.svg new file mode 100644 index 00000000000..7d977bc54f1 --- /dev/null +++ b/apps/desktop-gpui/assets/icons/languages.svg @@ -0,0 +1,3 @@ + + + diff --git a/apps/desktop-gpui/assets/icons/microsoft.svg b/apps/desktop-gpui/assets/icons/microsoft.svg new file mode 100644 index 00000000000..2c70b1cffd1 --- /dev/null +++ b/apps/desktop-gpui/assets/icons/microsoft.svg @@ -0,0 +1 @@ + diff --git a/apps/desktop-gpui/assets/icons/notebook.svg b/apps/desktop-gpui/assets/icons/notebook.svg new file mode 100644 index 00000000000..2efb56522b6 --- /dev/null +++ b/apps/desktop-gpui/assets/icons/notebook.svg @@ -0,0 +1 @@ + diff --git a/apps/desktop-gpui/assets/icons/panel-left.svg b/apps/desktop-gpui/assets/icons/panel-left.svg new file mode 100644 index 00000000000..95798a8605f --- /dev/null +++ b/apps/desktop-gpui/assets/icons/panel-left.svg @@ -0,0 +1 @@ + diff --git a/apps/desktop-gpui/assets/icons/pin.svg b/apps/desktop-gpui/assets/icons/pin.svg new file mode 100644 index 00000000000..3b5b2732876 --- /dev/null +++ b/apps/desktop-gpui/assets/icons/pin.svg @@ -0,0 +1 @@ + diff --git a/apps/desktop-gpui/assets/icons/square-pen.svg b/apps/desktop-gpui/assets/icons/square-pen.svg new file mode 100644 index 00000000000..56ca1e39bc6 --- /dev/null +++ b/apps/desktop-gpui/assets/icons/square-pen.svg @@ -0,0 +1 @@ + diff --git a/apps/desktop-gpui/assets/icons/users.svg b/apps/desktop-gpui/assets/icons/users.svg new file mode 100644 index 00000000000..d652cdeed87 --- /dev/null +++ b/apps/desktop-gpui/assets/icons/users.svg @@ -0,0 +1 @@ + diff --git a/apps/desktop-gpui/calendar/Cargo.toml b/apps/desktop-gpui/calendar/Cargo.toml new file mode 100644 index 00000000000..68c32f6dfc2 --- /dev/null +++ b/apps/desktop-gpui/calendar/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "cap-calendar" +version = "0.1.0" +edition = "2024" +license = "AGPL-3.0" +publish = false + +[dependencies] +base64 = "0.22" +chrono = { version = "0.4", features = ["serde"] } +chrono-tz = "0.10.4" +rrule = "0.14.0" +serde = { version = "1", features = ["derive"] } +serde_json = "1" +sha2 = "0.10" +thiserror = "2" +url = "2.5.8" diff --git a/apps/desktop-gpui/calendar/src/agenda.rs b/apps/desktop-gpui/calendar/src/agenda.rs new file mode 100644 index 00000000000..450c04c7275 --- /dev/null +++ b/apps/desktop-gpui/calendar/src/agenda.rs @@ -0,0 +1,339 @@ +use std::cmp::Ordering; +use std::collections::{HashMap, HashSet}; + +use chrono::{DateTime, Duration, NaiveDate, NaiveTime, TimeZone, Utc}; +use chrono_tz::Tz; + +use crate::model::{CalendarEvent, SourceKind}; +use crate::text::normalize_title; + +pub const DEFAULT_LEAD: Duration = Duration::seconds(60); +pub const MAX_LEAD: Duration = Duration::minutes(15); + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Filter { + pub hide_declined: bool, + pub hide_all_day: bool, + pub hide_without_participants: bool, + pub hidden_calendar_ids: HashSet, +} + +impl Default for Filter { + fn default() -> Self { + Self { + hide_declined: true, + hide_all_day: true, + hide_without_participants: false, + hidden_calendar_ids: HashSet::new(), + } + } +} + +impl Filter { + pub fn allows(&self, event: &CalendarEvent) -> bool { + !event.is_cancelled() + && !(self.hide_declined && event.is_declined()) + && !(self.hide_all_day && event.all_day) + && !(self.hide_without_participants && !event.has_participants()) + && !self.hidden_calendar_ids.contains(&event.calendar_id) + } +} + +fn source_rank(source: SourceKind) -> u8 { + match source { + SourceKind::Google | SourceKind::Microsoft => 3, + SourceKind::System => 2, + SourceKind::Ics => 1, + } +} + +fn richness(e: &CalendarEvent) -> (u8, bool, usize, bool) { + ( + source_rank(e.source), + e.link.is_some(), + e.attendees.len(), + e.description.is_some(), + ) +} + +fn fill_missing(keep: &mut CalendarEvent, other: CalendarEvent) { + if keep.link.is_none() { + keep.link = other.link; + } + if keep.location.is_none() { + keep.location = other.location; + } + if keep.description.is_none() { + keep.description = other.description; + } + if keep.web_url.is_none() { + keep.web_url = other.web_url; + } + if keep.attendees.is_empty() { + keep.attendees = other.attendees; + } + if keep.organizer.is_none() { + keep.organizer = other.organizer; + } +} + +fn dedup_keys(e: &CalendarEvent) -> Vec { + let start = e.start.timestamp(); + let mut keys = Vec::with_capacity(2); + if let Some(link) = &e.link + && let Some(id) = &link.meeting_id + { + keys.push(format!( + "m|{}|{}|{start}", + link.provider.slug(), + id.to_ascii_lowercase() + )); + } + let title = normalize_title(&e.title); + if !title.is_empty() { + keys.push(format!("t|{title}|{start}|{}", e.all_day)); + } + keys +} + +pub fn dedupe(events: Vec) -> Vec { + let mut unique: Vec = Vec::with_capacity(events.len()); + let mut ids: HashMap<(SourceKind, String, String), usize> = HashMap::new(); + for event in events { + let id_key = (event.source, event.calendar_id.clone(), event.id.clone()); + match ids.get(&id_key) { + Some(&i) => unique[i] = event, + None => { + ids.insert(id_key, unique.len()); + unique.push(event); + } + } + } + let mut parent: Vec = (0..unique.len()).collect(); + let mut owner: HashMap = HashMap::new(); + for (i, event) in unique.iter().enumerate() { + for key in dedup_keys(event) { + match owner.get(&key) { + Some(&j) => union(&mut parent, i, j), + None => { + owner.insert(key, i); + } + } + } + } + let mut groups: HashMap> = HashMap::new(); + for (i, event) in unique.into_iter().enumerate() { + groups.entry(root(&mut parent, i)).or_default().push(event); + } + let mut out: Vec = groups.into_values().filter_map(merge_group).collect(); + out.sort_by(compare_events); + out +} + +fn root(parent: &mut [usize], mut i: usize) -> usize { + while parent[i] != i { + parent[i] = parent[parent[i]]; + i = parent[i]; + } + i +} + +fn union(parent: &mut [usize], a: usize, b: usize) { + let (a, b) = (root(parent, a), root(parent, b)); + if a != b { + parent[a.max(b)] = a.min(b); + } +} + +fn source_order(source: SourceKind) -> u8 { + match source { + SourceKind::Google => 0, + SourceKind::Microsoft => 1, + SourceKind::System => 2, + SourceKind::Ics => 3, + } +} + +fn merge_group(mut group: Vec) -> Option { + group.sort_by(|a, b| { + richness(b) + .cmp(&richness(a)) + .then_with(|| source_order(a.source).cmp(&source_order(b.source))) + .then_with(|| a.calendar_id.cmp(&b.calendar_id)) + .then_with(|| a.id.cmp(&b.id)) + }); + let mut members = group.into_iter(); + let mut keep = members.next()?; + for other in members { + fill_missing(&mut keep, other); + } + Some(keep) +} + +fn preference(e: &CalendarEvent) -> (bool, usize) { + (e.link.is_some(), e.other_participants()) +} + +fn compare_events(a: &CalendarEvent, b: &CalendarEvent) -> Ordering { + a.start + .cmp(&b.start) + .then_with(|| preference(b).cmp(&preference(a))) + .then_with(|| a.end.cmp(&b.end)) + .then_with(|| a.title.cmp(&b.title)) + .then_with(|| a.id.cmp(&b.id)) + .then_with(|| a.calendar_id.cmp(&b.calendar_id)) + .then_with(|| source_order(a.source).cmp(&source_order(b.source))) +} + +fn effective_end(e: &CalendarEvent) -> DateTime { + if e.end > e.start { + e.end + } else { + e.start + .checked_add_signed(Duration::minutes(1)) + .unwrap_or(e.start) + } +} + +fn fire_time(e: &CalendarEvent, lead: Duration) -> DateTime { + e.start.checked_sub_signed(lead).unwrap_or(e.start) +} + +#[derive(Clone, Debug, Default, PartialEq)] +pub struct Agenda { + events: Vec, +} + +impl Agenda { + pub fn new(events: Vec, filter: &Filter) -> Self { + let events = dedupe(events) + .into_iter() + .filter(|e| filter.allows(e)) + .collect(); + Self { events } + } + + pub fn events(&self) -> &[CalendarEvent] { + &self.events + } + + fn timed(&self) -> impl Iterator { + self.events.iter().filter(|e| !e.all_day) + } + + pub fn current(&self, now: DateTime) -> Vec<&CalendarEvent> { + let mut out: Vec<&CalendarEvent> = self + .timed() + .filter(|e| e.start <= now && now < effective_end(e)) + .collect(); + out.sort_by(|a, b| { + b.start + .cmp(&a.start) + .then_with(|| preference(b).cmp(&preference(a))) + }); + out + } + + pub fn next(&self, now: DateTime) -> Option<&CalendarEvent> { + self.timed().find(|e| e.start > now) + } + + pub fn upcoming(&self, now: DateTime, horizon: Duration) -> Vec<&CalendarEvent> { + let until = now + .checked_add_signed(horizon) + .unwrap_or(DateTime::::MAX_UTC); + self.timed() + .filter(|e| effective_end(e) > now && e.start < until) + .collect() + } + + pub fn for_day(&self, date: NaiveDate, tz: Tz) -> Vec<&CalendarEvent> { + let day_start = local_midnight(date, tz); + let day_end = date + .succ_opt() + .map_or(DateTime::::MAX_UTC, |d| local_midnight(d, tz)); + self.events + .iter() + .filter(|e| { + if e.all_day { + let first = e.start.date_naive(); + let last = e.end.date_naive(); + first <= date && (date < last || (first == last && first == date)) + } else { + e.start < day_end && effective_end(e) > day_start + } + }) + .collect() + } +} + +fn local_midnight(date: NaiveDate, tz: Tz) -> DateTime { + let naive = date.and_time(NaiveTime::MIN); + tz.from_local_datetime(&naive) + .earliest() + .map(|d| d.with_timezone(&Utc)) + .unwrap_or_else(|| crate::tz::Zone::Named(tz).to_utc(naive)) +} + +#[derive(Clone, Debug, PartialEq)] +pub struct Reminder { + pub key: String, + pub fire_at: DateTime, + pub event: CalendarEvent, +} + +pub fn reminder_key(event: &CalendarEvent) -> String { + format!( + "{}|{}|{}", + event.calendar_id, + event.id, + event.start.timestamp() + ) +} + +fn remindable(e: &CalendarEvent) -> bool { + !e.all_day && !e.is_cancelled() && !e.is_declined() +} + +fn clamp_lead(lead: Duration) -> Duration { + lead.clamp(Duration::zero(), MAX_LEAD) +} + +pub fn due_reminders( + events: &[CalendarEvent], + now: DateTime, + lead: Duration, + already_notified: &HashSet, +) -> Vec { + let lead = clamp_lead(lead); + let mut out: Vec = events + .iter() + .filter(|e| remindable(e)) + .filter(|e| fire_time(e, lead) <= now && now < effective_end(e)) + .filter_map(|e| { + let key = reminder_key(e); + (!already_notified.contains(&key)).then(|| Reminder { + key, + fire_at: fire_time(e, lead), + event: e.clone(), + }) + }) + .collect(); + out.sort_by(|a, b| compare_events(&a.event, &b.event)); + out.dedup_by(|a, b| a.key == b.key); + out +} + +pub fn next_wake( + events: &[CalendarEvent], + now: DateTime, + lead: Duration, +) -> Option> { + let lead = clamp_lead(lead); + events + .iter() + .filter(|e| remindable(e)) + .map(|e| fire_time(e, lead)) + .filter(|fire| *fire > now) + .min() +} diff --git a/apps/desktop-gpui/calendar/src/error.rs b/apps/desktop-gpui/calendar/src/error.rs new file mode 100644 index 00000000000..3e944e40734 --- /dev/null +++ b/apps/desktop-gpui/calendar/src/error.rs @@ -0,0 +1,75 @@ +#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] +pub enum Error { + #[error("invalid JSON: {0}")] + Json(String), + #[error("unexpected response shape: {0}")] + InvalidResponse(String), + #[error("provider error {code}: {message}")] + Api { + status: Option, + code: String, + message: String, + }, + #[error("refusing to follow a link outside the provider API: {0}")] + UntrustedUrl(String), + #[error("invalid input: {0}")] + InvalidInput(String), +} + +impl Error { + pub fn is_unauthorized(&self) -> bool { + match self { + Error::Api { status, code, .. } => { + *status == Some(401) + || code.eq_ignore_ascii_case("InvalidAuthenticationToken") + || code.eq_ignore_ascii_case("UNAUTHENTICATED") + } + _ => false, + } + } +} + +pub(crate) fn api_error_from(value: &serde_json::Value) -> Option { + let error = value.get("error")?; + if let Some(code) = error.as_str() { + return Some(Error::Api { + status: None, + code: code.to_string(), + message: value + .get("error_description") + .and_then(|v| v.as_str()) + .unwrap_or_default() + .to_string(), + }); + } + let status = error + .get("code") + .and_then(|c| c.as_u64()) + .and_then(|c| u16::try_from(c).ok()); + let code = error + .get("status") + .and_then(|s| s.as_str()) + .or_else(|| error.get("code").and_then(|c| c.as_str())) + .map(str::to_string) + .or_else(|| status.map(|s| s.to_string())) + .unwrap_or_else(|| "unknown".to_string()); + let message = error + .get("message") + .and_then(|m| m.as_str()) + .unwrap_or_default() + .to_string(); + Some(Error::Api { + status, + code, + message, + }) +} + +pub(crate) fn parse_json(json: &str) -> Result { + let value: serde_json::Value = + serde_json::from_str(json).map_err(|e| Error::Json(e.to_string()))?; + if let Some(err) = api_error_from(&value) { + return Err(err); + } + Ok(value) +} diff --git a/apps/desktop-gpui/calendar/src/google.rs b/apps/desktop-gpui/calendar/src/google.rs new file mode 100644 index 00000000000..3644fcdda85 --- /dev/null +++ b/apps/desktop-gpui/calendar/src/google.rs @@ -0,0 +1,361 @@ +use std::ops::Range; + +use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime, SecondsFormat, Utc}; +use serde::Deserialize; +use serde_json::Value; + +use crate::error::{Error, parse_json}; +use crate::http::{HttpRequestSpec, api_url}; +use crate::link::{ConferenceEntry, ConferenceKind, event_meeting_link}; +use crate::model::{ + CalendarEvent, CalendarInfo, EventStatus, Page, Person, Response, SourceKind, + derive_self_response, is_self_email, +}; +use crate::text::non_empty; +use crate::tz::resolve_tzid; + +const API: &str = "https://www.googleapis.com/calendar/v3"; + +const EVENT_FIELDS: &str = "nextPageToken,timeZone,items(id,status,htmlLink,summary,description,location,start,end,organizer(email,displayName,self),attendees(email,displayName,self,responseStatus,resource),hangoutLink,conferenceData(entryPoints(entryPointType,uri,meetingCode),conferenceSolution(name,key(type)),conferenceId),recurringEventId,originalStartTime,eventType,transparency,iCalUID)"; + +const CALENDAR_FIELDS: &str = "nextPageToken,items(id,summary,summaryOverride,primary,backgroundColor,selected,accessRole,timeZone,hidden,deleted)"; + +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct GoogleOptions { + pub include_status_events: bool, + pub include_cancelled: bool, + pub self_emails: Vec, +} + +fn rfc3339(dt: DateTime) -> String { + dt.to_rfc3339_opts(SecondsFormat::Secs, true) +} + +fn calendar_url(path: &[&str]) -> String { + api_url(API, path) +} + +pub fn google_events_request( + calendar_id: &str, + window: &Range>, + page_token: Option<&str>, +) -> HttpRequestSpec { + let mut spec = HttpRequestSpec::get(calendar_url(&["calendars", calendar_id, "events"])) + .with_query("singleEvents", "true") + .with_query("orderBy", "startTime") + .with_query("timeMin", rfc3339(window.start)) + .with_query("timeMax", rfc3339(window.end)) + .with_query("maxResults", "250") + .with_query("fields", EVENT_FIELDS); + if let Some(token) = page_token.filter(|t| !t.is_empty()) { + spec = spec.with_query("pageToken", token); + } + spec +} + +pub fn google_calendar_list_request(page_token: Option<&str>) -> HttpRequestSpec { + let mut spec = HttpRequestSpec::get(calendar_url(&["users", "me", "calendarList"])) + .with_query("maxResults", "250") + .with_query("fields", CALENDAR_FIELDS); + if let Some(token) = page_token.filter(|t| !t.is_empty()) { + spec = spec.with_query("pageToken", token); + } + spec +} + +#[derive(Deserialize, Default)] +#[serde(rename_all = "camelCase", default)] +struct GTime { + date: Option, + date_time: Option, + time_zone: Option, +} + +#[derive(Deserialize, Default)] +#[serde(rename_all = "camelCase", default)] +struct GPerson { + email: Option, + display_name: Option, + #[serde(rename = "self")] + is_self: bool, + response_status: Option, + resource: bool, +} + +#[derive(Deserialize, Default)] +#[serde(rename_all = "camelCase", default)] +struct GEntryPoint { + entry_point_type: Option, + uri: Option, + meeting_code: Option, +} + +#[derive(Deserialize, Default)] +#[serde(rename_all = "camelCase", default)] +struct GConference { + entry_points: Vec, +} + +#[derive(Deserialize, Default)] +#[serde(rename_all = "camelCase", default)] +struct GEvent { + id: Option, + status: Option, + html_link: Option, + summary: Option, + description: Option, + location: Option, + start: Option, + end: Option, + original_start_time: Option, + organizer: Option, + attendees: Vec, + hangout_link: Option, + conference_data: Option, + recurring_event_id: Option, + event_type: Option, + transparency: Option, +} + +enum Resolved { + Timed(DateTime), + Day(NaiveDate), +} + +fn resolve_time(t: >ime, fallback_zone: Option<&str>) -> Option { + if let Some(dt) = t.date_time.as_deref() { + if let Ok(parsed) = DateTime::parse_from_rfc3339(dt.trim()) { + return Some(Resolved::Timed(parsed.with_timezone(&Utc))); + } + let naive = NaiveDateTime::parse_from_str(dt.trim(), "%Y-%m-%dT%H:%M:%S%.f").ok()?; + let zone = t + .time_zone + .as_deref() + .or(fallback_zone) + .and_then(resolve_tzid)?; + return Some(Resolved::Timed(zone.to_utc(naive))); + } + let date = NaiveDate::parse_from_str(t.date.as_deref()?.trim(), "%Y-%m-%d").ok()?; + Some(Resolved::Day(date)) +} + +fn google_response(value: Option<&str>) -> Response { + match value.unwrap_or_default() { + "accepted" => Response::Accepted, + "declined" => Response::Declined, + "tentative" => Response::Tentative, + "needsAction" => Response::NeedsAction, + _ => Response::Unknown, + } +} + +fn person(p: &GPerson, self_emails: &[String]) -> Person { + Person { + name: non_empty(p.display_name.as_deref()), + email: non_empty(p.email.as_deref()), + is_self: p.is_self || is_self_email(p.email.as_deref(), self_emails), + response: google_response(p.response_status.as_deref()), + } +} + +const STATUS_EVENT_TYPES: [&str; 3] = ["workingLocation", "outOfOffice", "focusTime"]; + +fn map_event( + e: GEvent, + calendar_id: &str, + zone: Option<&str>, + opts: &GoogleOptions, +) -> Option { + let status = match e.status.as_deref() { + Some("cancelled") => EventStatus::Cancelled, + Some("tentative") => EventStatus::Tentative, + _ => EventStatus::Confirmed, + }; + if status == EventStatus::Cancelled && !opts.include_cancelled { + return None; + } + if !opts.include_status_events + && e.event_type + .as_deref() + .is_some_and(|t| STATUS_EVENT_TYPES.contains(&t)) + { + return None; + } + let id = non_empty(e.id.as_deref())?; + let start = e + .start + .as_ref() + .or(e.original_start_time.as_ref()) + .and_then(|t| resolve_time(t, zone))?; + let end = e.end.as_ref().and_then(|t| resolve_time(t, zone)); + let midnight = NaiveTime::MIN; + let (start, end, all_day) = match (start, end) { + (Resolved::Day(s), end) => { + let e_date = match end { + Some(Resolved::Day(d)) if d > s => d, + _ => s.succ_opt().unwrap_or(s), + }; + ( + s.and_time(midnight).and_utc(), + e_date.and_time(midnight).and_utc(), + true, + ) + } + (Resolved::Timed(s), Some(Resolved::Timed(end))) if end >= s => (s, end, false), + (Resolved::Timed(s), _) => (s, s, false), + }; + let organizer = e.organizer.as_ref().map(|o| person(o, &opts.self_emails)); + let attendees: Vec = e + .attendees + .iter() + .filter(|a| !a.resource) + .map(|a| person(a, &opts.self_emails)) + .collect(); + let mut conference: Vec = e + .conference_data + .as_ref() + .map(|c| { + c.entry_points + .iter() + .filter_map(|ep| { + Some(ConferenceEntry { + kind: ConferenceKind::from_entry_point_type( + ep.entry_point_type.as_deref().unwrap_or_default(), + ), + uri: non_empty(ep.uri.as_deref())?, + meeting_code: non_empty(ep.meeting_code.as_deref()), + }) + }) + .collect() + }) + .unwrap_or_default(); + if let Some(hangout) = non_empty(e.hangout_link.as_deref()) + && !conference.iter().any(|c| c.kind == ConferenceKind::Video) + { + conference.push(ConferenceEntry::video(hangout)); + } + let link = event_meeting_link( + &conference, + e.location.as_deref(), + e.description.as_deref(), + None, + ); + let self_response = derive_self_response(organizer.as_ref(), &attendees, Response::Unknown); + Some(CalendarEvent { + id, + series_id: non_empty(e.recurring_event_id.as_deref()), + calendar_id: calendar_id.to_string(), + source: SourceKind::Google, + title: e.summary.map(|s| s.trim().to_string()).unwrap_or_default(), + start, + end, + all_day, + location: non_empty(e.location.as_deref()), + description: non_empty(e.description.as_deref()), + organizer, + attendees, + self_response, + link, + status, + web_url: non_empty(e.html_link.as_deref()), + free: e.transparency.as_deref() == Some("transparent"), + }) +} + +pub(crate) fn array_field<'a>(value: &'a Value, name: &str) -> Result<&'a [Value], Error> { + match value.get(name) { + None | Some(Value::Null) => Ok(&[]), + Some(Value::Array(items)) => Ok(items), + Some(_) => Err(Error::InvalidResponse(format!("{name} is not an array"))), + } +} + +fn items(value: &Value) -> Result<&[Value], Error> { + array_field(value, "items") +} + +fn next_token(value: &Value) -> Option { + non_empty(value.get("nextPageToken").and_then(Value::as_str)) +} + +pub fn parse_google_events(json: &str, calendar_id: &str) -> Result, Error> { + parse_google_events_with(json, calendar_id, &GoogleOptions::default()) +} + +pub fn parse_google_events_with( + json: &str, + calendar_id: &str, + opts: &GoogleOptions, +) -> Result, Error> { + let value = parse_json(json)?; + if !value.is_object() { + return Err(Error::InvalidResponse("expected an object".to_string())); + } + let zone = value.get("timeZone").and_then(Value::as_str); + let mut page = Page { + items: Vec::new(), + next: next_token(&value), + skipped: 0, + }; + for item in items(&value)? { + let mapped = serde_json::from_value::(item.clone()) + .ok() + .and_then(|e| map_event(e, calendar_id, zone, opts)); + match mapped { + Some(event) => page.items.push(event), + None => page.skipped += 1, + } + } + Ok(page) +} + +#[derive(Deserialize, Default)] +#[serde(rename_all = "camelCase", default)] +struct GCalendar { + id: Option, + summary: Option, + summary_override: Option, + primary: bool, + background_color: Option, + selected: bool, + access_role: Option, + time_zone: Option, + deleted: bool, +} + +pub fn parse_google_calendar_list(json: &str) -> Result, Error> { + let value = parse_json(json)?; + let mut page = Page { + items: Vec::new(), + next: next_token(&value), + skipped: 0, + }; + for item in items(&value)? { + let Some(cal) = serde_json::from_value::(item.clone()) + .ok() + .filter(|c| !c.deleted) + else { + page.skipped += 1; + continue; + }; + let Some(id) = non_empty(cal.id.as_deref()) else { + page.skipped += 1; + continue; + }; + let name = non_empty(cal.summary_override.as_deref()) + .or_else(|| non_empty(cal.summary.as_deref())) + .unwrap_or_else(|| id.clone()); + page.items.push(CalendarInfo { + owner_email: (cal.primary && id.contains('@')).then(|| id.clone()), + id, + name, + source: SourceKind::Google, + primary: cal.primary, + color: non_empty(cal.background_color.as_deref()), + selected: cal.selected || cal.primary, + access_role: non_empty(cal.access_role.as_deref()), + time_zone: non_empty(cal.time_zone.as_deref()), + }); + } + Ok(page) +} diff --git a/apps/desktop-gpui/calendar/src/graph.rs b/apps/desktop-gpui/calendar/src/graph.rs new file mode 100644 index 00000000000..d299a2ea7e2 --- /dev/null +++ b/apps/desktop-gpui/calendar/src/graph.rs @@ -0,0 +1,419 @@ +use std::ops::Range; + +use chrono::{DateTime, NaiveDateTime, NaiveTime, SecondsFormat, Utc}; +use serde::Deserialize; +use serde_json::Value; + +use crate::error::{Error, parse_json}; +use crate::google::array_field; +use crate::http::{HttpRequestSpec, api_url}; +use crate::link::{ConferenceEntry, event_meeting_link}; +use crate::model::{ + CalendarEvent, CalendarInfo, EventStatus, Page, Person, Response, SourceKind, + derive_self_response, is_self_email, +}; +use crate::text::{html_to_text, looks_like_html, non_empty}; +use crate::tz::{Zone, resolve_tzid}; + +const API: &str = "https://graph.microsoft.com/v1.0"; +const TRUSTED_PREFIX: &str = "https://graph.microsoft.com/"; + +const EVENT_SELECT: &str = "id,subject,body,bodyPreview,start,end,isAllDay,isCancelled,location,locations,organizer,attendees,responseStatus,onlineMeeting,onlineMeetingUrl,isOnlineMeeting,onlineMeetingProvider,seriesMasterId,type,webLink,showAs"; + +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct GraphOptions { + pub include_cancelled: bool, + pub self_emails: Vec, +} + +fn with_common_headers(spec: HttpRequestSpec) -> HttpRequestSpec { + spec.with_header("Prefer", "outlook.timezone=\"UTC\"") + .with_header("Accept", "application/json") +} + +fn trusted_next_link(next_link: &str) -> Result { + let trimmed = next_link.trim(); + let parsed = url::Url::parse(trimmed).map_err(|_| Error::UntrustedUrl(trimmed.to_string()))?; + if !trimmed.starts_with(TRUSTED_PREFIX) + || parsed.host_str() != Some("graph.microsoft.com") + || parsed.scheme() != "https" + || !parsed.username().is_empty() + || parsed.port().is_some() + { + return Err(Error::UntrustedUrl(trimmed.to_string())); + } + Ok(with_common_headers(HttpRequestSpec::get( + parsed.to_string(), + ))) +} + +pub fn graph_calendar_view_request( + calendar_id: Option<&str>, + window: &Range>, + next_link: Option<&str>, +) -> Result { + if let Some(next) = next_link.filter(|n| !n.trim().is_empty()) { + return trusted_next_link(next); + } + let url = match calendar_id.filter(|c| !c.is_empty()) { + Some(id) => api_url(API, &["me", "calendars", id, "calendarView"]), + None => api_url(API, &["me", "calendarView"]), + }; + let spec = HttpRequestSpec::get(url) + .with_query( + "startDateTime", + window.start.to_rfc3339_opts(SecondsFormat::Secs, true), + ) + .with_query( + "endDateTime", + window.end.to_rfc3339_opts(SecondsFormat::Secs, true), + ) + .with_query("$select", EVENT_SELECT) + .with_query("$orderby", "start/dateTime") + .with_query("$top", "250"); + Ok(with_common_headers(spec)) +} + +pub fn graph_calendars_request(next_link: Option<&str>) -> Result { + if let Some(next) = next_link.filter(|n| !n.trim().is_empty()) { + return trusted_next_link(next); + } + Ok(with_common_headers( + HttpRequestSpec::get(api_url(API, &["me", "calendars"])) + .with_query( + "$select", + "id,name,color,hexColor,isDefaultCalendar,canEdit,owner", + ) + .with_query("$top", "100"), + )) +} + +#[derive(Deserialize, Default)] +#[serde(rename_all = "camelCase", default)] +struct MTime { + date_time: Option, + time_zone: Option, +} + +#[derive(Deserialize, Default)] +#[serde(rename_all = "camelCase", default)] +struct MEmail { + name: Option, + address: Option, +} + +#[derive(Deserialize, Default)] +#[serde(rename_all = "camelCase", default)] +struct MStatus { + response: Option, +} + +#[derive(Deserialize, Default)] +#[serde(rename_all = "camelCase", default)] +struct MAttendee { + #[serde(rename = "type")] + kind: Option, + status: Option, + email_address: Option, +} + +#[derive(Deserialize, Default)] +#[serde(rename_all = "camelCase", default)] +struct MOrganizer { + email_address: Option, +} + +#[derive(Deserialize, Default)] +#[serde(rename_all = "camelCase", default)] +struct MBody { + content_type: Option, + content: Option, +} + +#[derive(Deserialize, Default)] +#[serde(rename_all = "camelCase", default)] +struct MLocation { + display_name: Option, + location_uri: Option, +} + +#[derive(Deserialize, Default)] +#[serde(rename_all = "camelCase", default)] +struct MOnlineMeeting { + join_url: Option, + conference_id: Option, +} + +#[derive(Deserialize, Default)] +#[serde(rename_all = "camelCase", default)] +struct MEvent { + id: Option, + subject: Option, + body: Option, + body_preview: Option, + start: Option, + end: Option, + is_all_day: bool, + is_cancelled: bool, + location: Option, + locations: Vec, + organizer: Option, + attendees: Vec, + response_status: Option, + online_meeting: Option, + online_meeting_url: Option, + series_master_id: Option, + web_link: Option, + show_as: Option, +} + +fn graph_response(value: Option<&str>) -> Response { + match value.unwrap_or_default() { + "accepted" | "organizer" => Response::Accepted, + "declined" => Response::Declined, + "tentativelyAccepted" => Response::Tentative, + "notResponded" => Response::NeedsAction, + _ => Response::Unknown, + } +} + +fn parse_graph_time(t: &MTime) -> Option<(NaiveDateTime, Zone)> { + let raw = t.date_time.as_deref()?.trim(); + let naive = NaiveDateTime::parse_from_str(raw, "%Y-%m-%dT%H:%M:%S%.f") + .or_else(|_| NaiveDateTime::parse_from_str(raw, "%Y-%m-%dT%H:%M")) + .ok() + .or_else(|| { + DateTime::parse_from_rfc3339(raw) + .ok() + .map(|d| d.with_timezone(&Utc).naive_utc()) + })?; + let zone = t + .time_zone + .as_deref() + .and_then(resolve_tzid) + .unwrap_or(Zone::Utc); + Some((naive, zone)) +} + +fn email_person(email: &MEmail, self_emails: &[String], response: Response) -> Person { + Person { + name: non_empty(email.name.as_deref()), + email: non_empty(email.address.as_deref()), + is_self: is_self_email(email.address.as_deref(), self_emails), + response, + } +} + +fn map_event(e: MEvent, calendar_id: &str, opts: &GraphOptions) -> Option { + let status = if e.is_cancelled { + EventStatus::Cancelled + } else { + EventStatus::Confirmed + }; + if status == EventStatus::Cancelled && !opts.include_cancelled { + return None; + } + let id = non_empty(e.id.as_deref())?; + let (start_naive, start_zone) = e.start.as_ref().and_then(parse_graph_time)?; + let end = e.end.as_ref().and_then(parse_graph_time); + let (start, end) = if e.is_all_day { + let start_date = start_naive.date(); + let end_date = end + .map(|(n, _)| n.date()) + .filter(|d| *d > start_date) + .or_else(|| start_date.succ_opt()) + .unwrap_or(start_date); + ( + start_date.and_time(NaiveTime::MIN).and_utc(), + end_date.and_time(NaiveTime::MIN).and_utc(), + ) + } else { + let start = start_zone.to_utc(start_naive); + let end = end + .map(|(n, z)| z.to_utc(n)) + .filter(|e| *e >= start) + .unwrap_or(start); + (start, end) + }; + let organizer = e + .organizer + .as_ref() + .and_then(|o| o.email_address.as_ref()) + .map(|em| email_person(em, &opts.self_emails, Response::Accepted)); + let attendees: Vec = e + .attendees + .iter() + .filter(|a| { + !a.kind + .as_deref() + .is_some_and(|k| k.eq_ignore_ascii_case("resource")) + }) + .filter_map(|a| { + let response = graph_response(a.status.as_ref().and_then(|s| s.response.as_deref())); + a.email_address + .as_ref() + .map(|em| email_person(em, &opts.self_emails, response)) + }) + .collect(); + let fallback = graph_response( + e.response_status + .as_ref() + .and_then(|s| s.response.as_deref()), + ); + let mut organizer = organizer; + if e.response_status + .as_ref() + .and_then(|s| s.response.as_deref()) + == Some("organizer") + && let Some(org) = organizer.as_mut() + { + org.is_self = true; + } + let self_response = derive_self_response(organizer.as_ref(), &attendees, fallback); + + let mut conference = Vec::new(); + if let Some(join) = e + .online_meeting + .as_ref() + .and_then(|m| non_empty(m.join_url.as_deref())) + { + conference.push(ConferenceEntry { + meeting_code: e + .online_meeting + .as_ref() + .and_then(|m| non_empty(m.conference_id.as_deref())), + ..ConferenceEntry::video(join) + }); + } + if let Some(url) = non_empty(e.online_meeting_url.as_deref()) { + conference.push(ConferenceEntry::video(url)); + } + let location = e + .location + .as_ref() + .and_then(|l| non_empty(l.display_name.as_deref())); + let location_uris: Vec<&str> = e + .location + .iter() + .chain(e.locations.iter()) + .filter_map(|l| l.location_uri.as_deref()) + .collect(); + let body = e.body.as_ref().and_then(|b| b.content.as_deref()); + let link = event_meeting_link(&conference, location.as_deref(), body, None).or_else(|| { + location_uris + .iter() + .find_map(|u| crate::link::classify_url(u)) + }); + let is_html = e + .body + .as_ref() + .and_then(|b| b.content_type.as_deref()) + .is_some_and(|t| t.eq_ignore_ascii_case("html")) + || body.is_some_and(looks_like_html); + let description = body + .map(|b| { + if is_html { + html_to_text(b) + } else { + b.to_string() + } + }) + .and_then(|d| non_empty(Some(&d))) + .or_else(|| non_empty(e.body_preview.as_deref())); + + Some(CalendarEvent { + id, + series_id: non_empty(e.series_master_id.as_deref()), + calendar_id: calendar_id.to_string(), + source: SourceKind::Microsoft, + title: e.subject.map(|s| s.trim().to_string()).unwrap_or_default(), + start, + end, + all_day: e.is_all_day, + location, + description, + organizer, + attendees, + self_response, + link, + status, + web_url: non_empty(e.web_link.as_deref()), + free: e.show_as.as_deref() == Some("free"), + }) +} + +fn next_link(value: &Value) -> Option { + non_empty(value.get("@odata.nextLink").and_then(Value::as_str)) +} + +pub fn parse_graph_events(json: &str, calendar_id: &str) -> Result, Error> { + parse_graph_events_with(json, calendar_id, &GraphOptions::default()) +} + +pub fn parse_graph_events_with( + json: &str, + calendar_id: &str, + opts: &GraphOptions, +) -> Result, Error> { + let value = parse_json(json)?; + if !value.is_object() { + return Err(Error::InvalidResponse("expected an object".to_string())); + } + let mut page = Page { + items: Vec::new(), + next: next_link(&value), + skipped: 0, + }; + for item in array_field(&value, "value")? { + let mapped = serde_json::from_value::(item.clone()) + .ok() + .and_then(|e| map_event(e, calendar_id, opts)); + match mapped { + Some(event) => page.items.push(event), + None => page.skipped += 1, + } + } + Ok(page) +} + +#[derive(Deserialize, Default)] +#[serde(rename_all = "camelCase", default)] +struct MCalendar { + id: Option, + name: Option, + hex_color: Option, + is_default_calendar: bool, + can_edit: bool, + owner: Option, +} + +pub fn parse_graph_calendars(json: &str) -> Result, Error> { + let value = parse_json(json)?; + let mut page = Page { + items: Vec::new(), + next: next_link(&value), + skipped: 0, + }; + for item in array_field(&value, "value")? { + let Some(cal) = serde_json::from_value::(item.clone()).ok() else { + page.skipped += 1; + continue; + }; + let Some(id) = non_empty(cal.id.as_deref()) else { + page.skipped += 1; + continue; + }; + page.items.push(CalendarInfo { + name: non_empty(cal.name.as_deref()).unwrap_or_else(|| id.clone()), + id, + source: SourceKind::Microsoft, + primary: cal.is_default_calendar, + color: non_empty(cal.hex_color.as_deref()), + selected: true, + access_role: Some(if cal.can_edit { "writer" } else { "reader" }.to_string()), + owner_email: cal.owner.and_then(|o| non_empty(o.address.as_deref())), + time_zone: None, + }); + } + Ok(page) +} diff --git a/apps/desktop-gpui/calendar/src/http.rs b/apps/desktop-gpui/calendar/src/http.rs new file mode 100644 index 00000000000..0fdca9eb88e --- /dev/null +++ b/apps/desktop-gpui/calendar/src/http.rs @@ -0,0 +1,100 @@ +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum HttpMethod { + Get, + Post, +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct HttpRequestSpec { + pub method: HttpMethod, + pub url: String, + pub query: Vec<(String, String)>, + pub headers: Vec<(String, String)>, + pub form: Vec<(String, String)>, +} + +impl HttpRequestSpec { + pub(crate) fn get(url: impl Into) -> Self { + Self { + method: HttpMethod::Get, + url: url.into(), + query: Vec::new(), + headers: Vec::new(), + form: Vec::new(), + } + } + + pub(crate) fn post_form(url: impl Into, form: Vec<(String, String)>) -> Self { + Self { + method: HttpMethod::Post, + url: url.into(), + query: Vec::new(), + headers: vec![( + "Content-Type".to_string(), + "application/x-www-form-urlencoded".to_string(), + )], + form, + } + } + + pub(crate) fn with_query(mut self, key: &str, value: impl Into) -> Self { + self.query.push((key.to_string(), value.into())); + self + } + + pub(crate) fn with_header(mut self, key: &str, value: impl Into) -> Self { + self.headers.push((key.to_string(), value.into())); + self + } + + pub fn url_with_query(&self) -> String { + if self.query.is_empty() { + return self.url.clone(); + } + match url::Url::parse(&self.url) { + Ok(mut parsed) => { + parsed + .query_pairs_mut() + .extend_pairs(self.query.iter().map(|(k, v)| (k.as_str(), v.as_str()))); + parsed.to_string() + } + Err(_) => { + let encoded = encode_pairs(&self.query); + let sep = if self.url.contains('?') { '&' } else { '?' }; + format!("{}{sep}{encoded}", self.url) + } + } + } + + pub fn form_body(&self) -> Option { + if self.method != HttpMethod::Post { + return None; + } + Some(encode_pairs(&self.form)) + } + + pub fn header(&self, name: &str) -> Option<&str> { + self.headers + .iter() + .find(|(k, _)| k.eq_ignore_ascii_case(name)) + .map(|(_, v)| v.as_str()) + } +} + +pub(crate) fn encode_pairs(pairs: &[(String, String)]) -> String { + url::form_urlencoded::Serializer::new(String::new()) + .extend_pairs(pairs.iter().map(|(k, v)| (k.as_str(), v.as_str()))) + .finish() +} + +pub(crate) fn api_url(base: &str, segments: &[&str]) -> String { + match url::Url::parse(base) { + Ok(mut url) => { + if let Ok(mut path) = url.path_segments_mut() { + path.pop_if_empty().extend(segments); + } + url.to_string() + } + Err(_) => base.to_string(), + } +} diff --git a/apps/desktop-gpui/calendar/src/ics/budget.rs b/apps/desktop-gpui/calendar/src/ics/budget.rs new file mode 100644 index 00000000000..0954bfa7e37 --- /dev/null +++ b/apps/desktop-gpui/calendar/src/ics/budget.rs @@ -0,0 +1,25 @@ +pub(crate) const FEED_BUDGET: u64 = 3_000_000; + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) struct Budget { + left: u64, +} + +impl Budget { + pub fn new(left: u64) -> Self { + Self { left } + } + + #[cfg(test)] + pub fn left(&self) -> u64 { + self.left + } + + pub fn can_afford(&self, cost: u64) -> bool { + self.left >= cost + } + + pub fn charge(&mut self, cost: u64) { + self.left = self.left.saturating_sub(cost); + } +} diff --git a/apps/desktop-gpui/calendar/src/ics/expand.rs b/apps/desktop-gpui/calendar/src/ics/expand.rs new file mode 100644 index 00000000000..e153b7cfb1f --- /dev/null +++ b/apps/desktop-gpui/calendar/src/ics/expand.rs @@ -0,0 +1,274 @@ +use std::collections::{HashMap, HashSet}; +use std::ops::Range; + +use chrono::{DateTime, Duration, NaiveDate, NaiveDateTime, Utc}; + +use super::budget::Budget; +use super::{Exclusion, ParsedCalendar, RawEvent, RawPerson}; +use crate::link::{classify_url, event_meeting_link, find_meeting_link}; +use crate::model::{ + CalendarEvent, EventStatus, Person, Response, SourceKind, derive_self_response, is_self_email, +}; +use crate::text::html_to_text; + +pub fn expand( + cal: &ParsedCalendar, + window: Range>, + self_emails: &[String], +) -> Vec { + if window.start >= window.end { + return Vec::new(); + } + let opts = &cal.options; + let mut budget = cal.budget; + let mut overrides: HashMap<&str, Vec<&RawEvent>> = HashMap::new(); + let mut masters: Vec<&RawEvent> = Vec::new(); + for event in &cal.events { + if event.recurrence_id.is_some() { + overrides.entry(event.uid.as_str()).or_default().push(event); + } else { + masters.push(event); + } + } + let builder = Builder { + calendar_id: &opts.calendar_id, + self_emails, + }; + let keep = |status: EventStatus| status != EventStatus::Cancelled || opts.include_cancelled; + let overlaps = + |start: DateTime, end: DateTime| start < window.end && end > window.start; + + let mut out: Vec = Vec::new(); + for master in masters { + let room = opts.max_events.saturating_sub(out.len()); + if room == 0 { + break; + } + if !keep(master.status) { + continue; + } + let replaced: HashSet = overrides + .get(master.uid.as_str()) + .map(|list| { + list.iter() + .filter_map(|o| o.recurrence_id.map(|r| r.timestamp())) + .collect() + }) + .unwrap_or_default(); + let recurring = master.is_recurring() || !replaced.is_empty(); + let excluded = Excluded::new(master); + let skip = |wall: NaiveDateTime, start: DateTime| { + replaced.contains(&start.timestamp()) || excluded.contains(wall, start) + }; + let cap = opts.max_instances_per_series.min(room); + for (wall, start) in occurrences(master, &window, cap, &skip, &mut budget) { + let end = master.end_for(wall, start); + let original = recurring.then_some(start); + out.push(builder.build(master, start, end, original)); + } + } + let cancelled_series: HashSet<&str> = cal + .events + .iter() + .filter(|e| e.recurrence_id.is_none() && !keep(e.status)) + .map(|e| e.uid.as_str()) + .collect(); + 'overrides: for (uid, list) in overrides { + if cancelled_series.contains(uid) { + continue; + } + for event in list { + if out.len() >= opts.max_events { + break 'overrides; + } + if !keep(event.status) { + continue; + } + let start = event.start_utc(); + let end = event.end_for(event.start_wall, start); + if overlaps(start, end) { + out.push(builder.build(event, start, end, event.recurrence_id)); + } + } + } + out.sort_by(|a, b| a.start.cmp(&b.start).then_with(|| a.id.cmp(&b.id))); + out.truncate(opts.max_events); + let mut seen: HashMap = HashMap::new(); + for event in &mut out { + let count = seen.entry(event.id.clone()).or_insert(0); + *count += 1; + if *count > 1 { + event.id = format!("{}#{}", event.id, count); + } + } + out +} + +struct Excluded { + instants: HashSet>, + days: HashSet, +} + +impl Excluded { + fn new(event: &RawEvent) -> Self { + let mut instants = HashSet::new(); + let mut days = HashSet::new(); + for ex in &event.exdates { + match ex { + Exclusion::Instant(i) => { + instants.insert(*i); + } + Exclusion::Day(d) => { + days.insert(*d); + } + } + } + Self { instants, days } + } + + fn contains(&self, wall: NaiveDateTime, start: DateTime) -> bool { + self.instants.contains(&start) || self.days.contains(&wall.date()) + } +} + +fn occurrences( + event: &RawEvent, + window: &Range>, + cap: usize, + skip: &impl Fn(NaiveDateTime, DateTime) -> bool, + budget: &mut Budget, +) -> Vec<(NaiveDateTime, DateTime)> { + let wanted = |wall: NaiveDateTime, utc: DateTime| { + !skip(wall, utc) && utc < window.end && event.end_for(wall, utc) > window.start + }; + let start = event.start_utc(); + let mut found = Vec::new(); + if wanted(event.start_wall, start) { + found.push((event.start_wall, start)); + } + if let Some(rule) = &event.rule { + let margin = Duration::days(2); + let wall_end = event + .zone + .from_utc(window.end) + .checked_add_signed(margin) + .unwrap_or(NaiveDateTime::MAX); + let anchor = event + .zone + .from_utc(window.start) + .checked_sub_signed(event.span.upper_bound()) + .and_then(|a| a.checked_sub_signed(margin)) + .unwrap_or(event.start_wall); + let mut in_window = 0usize; + for wall in rule.walls(event.start_wall, anchor, budget) { + if wall > wall_end || in_window >= cap { + break; + } + let utc = event.zone.to_utc(wall); + if utc == start || !wanted(wall, utc) { + continue; + } + in_window += 1; + found.push((wall, utc)); + } + } + found.extend( + event + .rdates + .iter() + .map(|r| (event.zone.from_utc(*r), *r)) + .filter(|(wall, utc)| wanted(*wall, *utc)), + ); + found.sort_by_key(|(_, utc)| *utc); + found.dedup_by_key(|(_, utc)| *utc); + found.truncate(cap); + found +} + +fn occurrence_key(start: DateTime, all_day: bool) -> String { + if all_day { + start.format("%Y%m%d").to_string() + } else { + start.format("%Y%m%dT%H%M%SZ").to_string() + } +} + +struct Builder<'a> { + calendar_id: &'a str, + self_emails: &'a [String], +} + +impl Builder<'_> { + fn person(&self, raw: &RawPerson) -> Person { + Person { + name: raw.name.clone(), + email: raw.email.clone(), + is_self: is_self_email(raw.email.as_deref(), self.self_emails), + response: raw.response, + } + } + + fn build( + &self, + raw: &RawEvent, + start: DateTime, + end: DateTime, + original: Option>, + ) -> CalendarEvent { + let organizer = raw.organizer.as_ref().map(|o| self.person(o)); + let mut attendees: Vec = Vec::with_capacity(raw.attendees.len()); + for person in raw.attendees.iter().map(|a| self.person(a)) { + let duplicate = person.email.as_deref().is_some_and(|email| { + attendees.iter().any(|p| { + p.email + .as_deref() + .is_some_and(|e| e.eq_ignore_ascii_case(email)) + }) + }); + if !duplicate { + attendees.push(person); + } + } + let self_response = derive_self_response(organizer.as_ref(), &attendees, Response::Unknown); + let link = event_meeting_link( + &raw.conference, + raw.location.as_deref(), + raw.description.as_deref(), + None, + ) + .or_else(|| raw.alt_description.as_deref().and_then(find_meeting_link)) + .or_else(|| raw.url.as_deref().and_then(classify_url)); + let description = raw.description.clone().or_else(|| { + raw.alt_description + .as_deref() + .map(html_to_text) + .filter(|d| !d.trim().is_empty()) + }); + let id = match original { + Some(original) => format!("{}_{}", raw.uid, occurrence_key(original, raw.all_day)), + None => raw.uid.clone(), + }; + CalendarEvent { + id, + series_id: original.map(|_| raw.uid.clone()), + calendar_id: self.calendar_id.to_string(), + source: SourceKind::Ics, + title: raw.title.clone(), + start, + end, + all_day: raw.all_day, + location: raw.location.clone(), + description, + organizer, + attendees, + self_response, + link, + status: raw.status, + web_url: raw + .url + .clone() + .filter(|u| u.starts_with("https://") || u.starts_with("http://")), + free: raw.free, + } + } +} diff --git a/apps/desktop-gpui/calendar/src/ics/lexer.rs b/apps/desktop-gpui/calendar/src/ics/lexer.rs new file mode 100644 index 00000000000..5eb366abc0a --- /dev/null +++ b/apps/desktop-gpui/calendar/src/ics/lexer.rs @@ -0,0 +1,283 @@ +#[derive(Clone, Debug, PartialEq, Eq)] +pub(crate) struct Property { + pub name: String, + pub params: Vec<(String, Vec)>, + pub value: String, +} + +impl Property { + pub fn param(&self, name: &str) -> Option<&str> { + self.params + .iter() + .find(|(k, _)| k == name) + .and_then(|(_, v)| v.first()) + .map(String::as_str) + } +} + +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub(crate) struct Component { + pub name: String, + pub props: Vec, + pub children: Vec, +} + +impl Component { + pub fn prop(&self, name: &str) -> Option<&Property> { + self.props.iter().find(|p| p.name == name) + } + + pub fn props_named<'a>(&'a self, name: &'a str) -> impl Iterator + 'a { + self.props.iter().filter(move |p| p.name == name) + } + + pub fn text(&self, name: &str) -> Option { + self.prop(name).map(|p| unescape_text(&p.value)) + } +} + +pub(crate) struct Lexed { + pub root: Component, + pub warnings: Vec, +} + +const MAX_DEPTH: usize = 16; + +pub(crate) fn unfold(text: &str) -> Vec { + let text = text.strip_prefix('\u{feff}').unwrap_or(text); + let mut lines: Vec = Vec::new(); + for raw in text.split('\n') { + let line = raw.strip_suffix('\r').unwrap_or(raw); + if let Some(rest) = line.strip_prefix([' ', '\t']) { + if let Some(last) = lines.last_mut() { + last.push_str(rest); + continue; + } + if rest.trim().is_empty() { + continue; + } + lines.push(rest.to_string()); + continue; + } + if line.is_empty() { + continue; + } + lines.push(line.to_string()); + } + lines +} + +pub(crate) fn parse_content_line(line: &str) -> Option { + let mut name_end = None; + for (i, c) in line.char_indices() { + if c == ';' || c == ':' { + name_end = Some(i); + break; + } + } + let name_end = name_end?; + let name = line[..name_end].trim().to_ascii_uppercase(); + if name.is_empty() { + return None; + } + let mut params = Vec::new(); + let mut rest = &line[name_end..]; + while let Some(after) = rest.strip_prefix(';') { + let eq = after.find(['=', ':', ';'])?; + if after.as_bytes()[eq] != b'=' { + rest = &after[eq..]; + continue; + } + let key = after[..eq].trim().to_ascii_uppercase(); + let mut values = Vec::new(); + let mut cursor = &after[eq + 1..]; + loop { + if let Some(quoted) = cursor.strip_prefix('"') { + let close = quoted.find('"').unwrap_or(quoted.len()); + values.push(quoted[..close].to_string()); + cursor = quoted.get(close + 1..).unwrap_or(""); + } else { + let end = cursor.find([',', ';', ':']).unwrap_or(cursor.len()); + values.push(cursor[..end].to_string()); + cursor = &cursor[end..]; + } + match cursor.strip_prefix(',') { + Some(next) => cursor = next, + None => break, + } + } + params.push((key, values)); + rest = cursor; + if !rest.starts_with(';') && !rest.starts_with(':') { + let skip = rest.find([';', ':'])?; + rest = &rest[skip..]; + } + } + let value = rest.strip_prefix(':')?; + Some(Property { + name, + params, + value: value.to_string(), + }) +} + +pub(crate) fn lex(text: &str) -> Lexed { + let mut warnings = Vec::new(); + let mut stack: Vec = vec![Component { + name: "ROOT".to_string(), + ..Component::default() + }]; + for line in unfold(text) { + let Some(prop) = parse_content_line(&line) else { + warnings.push(format!("unparseable line: {}", truncate(&line))); + continue; + }; + match prop.name.as_str() { + "BEGIN" => { + let name = prop.value.trim().to_ascii_uppercase(); + if stack.len() > MAX_DEPTH { + warnings.push(format!("component nesting too deep at {name}")); + continue; + } + stack.push(Component { + name, + ..Component::default() + }); + } + "END" => { + let name = prop.value.trim().to_ascii_uppercase(); + let Some(pos) = stack.iter().skip(1).rposition(|c| c.name == name) else { + warnings.push(format!("END:{name} without BEGIN")); + continue; + }; + let target = pos + 1; + while stack.len() > target { + let Some(done) = stack.pop() else { break }; + if stack.len() > target { + warnings.push(format!("component {} not closed", done.name)); + } + if let Some(parent) = stack.last_mut() { + parent.children.push(done); + } + } + } + _ => { + if let Some(current) = stack.last_mut() { + current.props.push(prop); + } + } + } + } + while stack.len() > 1 { + let Some(done) = stack.pop() else { break }; + warnings.push(format!( + "component {} not closed at end of input", + done.name + )); + if let Some(parent) = stack.last_mut() { + parent.children.push(done); + } + } + let root = stack.pop().unwrap_or_default(); + Lexed { root, warnings } +} + +fn truncate(line: &str) -> String { + line.chars().take(60).collect() +} + +pub(crate) fn unescape_text(value: &str) -> String { + let mut out = String::with_capacity(value.len()); + let mut chars = value.chars(); + while let Some(c) = chars.next() { + if c != '\\' { + out.push(c); + continue; + } + match chars.next() { + Some('n' | 'N') => out.push('\n'), + Some(',') => out.push(','), + Some(';') => out.push(';'), + Some('\\') => out.push('\\'), + Some(':') => out.push(':'), + Some(other) => { + out.push('\\'); + out.push(other); + } + None => out.push('\\'), + } + } + out +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn unfolds_crlf_lf_space_and_tab() { + let text = + "BEGIN:VEVENT\r\nSUMMARY:Quarterly \r\n planning\r\nDESCRIPTION:a\n\tb\nEND:VEVENT\r\n"; + let lines = unfold(text); + assert_eq!( + lines, + vec![ + "BEGIN:VEVENT", + "SUMMARY:Quarterly planning", + "DESCRIPTION:ab", + "END:VEVENT" + ] + ); + } + + #[test] + fn parses_quoted_params_with_separators() { + let prop = parse_content_line( + "ATTENDEE;CN=\"Doe, Jane: PM\";PARTSTAT=ACCEPTED;DELEGATED-FROM=\"mailto:a@x.com\",\"mailto:b@x.com\":mailto:jane@x.com", + ) + .unwrap(); + assert_eq!(prop.name, "ATTENDEE"); + assert_eq!(prop.param("CN"), Some("Doe, Jane: PM")); + assert_eq!(prop.param("PARTSTAT"), Some("ACCEPTED")); + assert_eq!( + prop.params[2].1, + vec!["mailto:a@x.com".to_string(), "mailto:b@x.com".to_string()] + ); + assert_eq!(prop.value, "mailto:jane@x.com"); + } + + #[test] + fn value_keeps_colons() { + let prop = parse_content_line("URL;VALUE=URI:https://example.com:8443/a?b=c").unwrap(); + assert_eq!(prop.value, "https://example.com:8443/a?b=c"); + assert_eq!(prop.param("VALUE"), Some("URI")); + } + + #[test] + fn rejects_lines_without_value() { + assert!(parse_content_line("JUSTTEXT").is_none()); + assert!(parse_content_line(":value").is_none()); + assert!(parse_content_line("X;CN=\"unterminated").is_none()); + } + + #[test] + fn unescapes_text() { + assert_eq!( + unescape_text("Line one\\nLine two\\, with comma\\; semi \\\\ backslash\\N"), + "Line one\nLine two, with comma; semi \\ backslash\n" + ); + assert_eq!(unescape_text("trailing\\"), "trailing\\"); + } + + #[test] + fn lex_recovers_from_mismatched_components() { + let lexed = lex( + "BEGIN:VCALENDAR\nBEGIN:VEVENT\nUID:1\nBEGIN:VALARM\nEND:VEVENT\nEND:BOGUS\nBEGIN:VEVENT\nUID:2\n", + ); + let cal = &lexed.root.children[0]; + assert_eq!(cal.children.len(), 2); + assert_eq!(cal.children[0].prop("UID").unwrap().value, "1"); + assert_eq!(cal.children[1].prop("UID").unwrap().value, "2"); + assert!(lexed.warnings.len() >= 3); + } +} diff --git a/apps/desktop-gpui/calendar/src/ics/mod.rs b/apps/desktop-gpui/calendar/src/ics/mod.rs new file mode 100644 index 00000000000..caf36a84a1d --- /dev/null +++ b/apps/desktop-gpui/calendar/src/ics/mod.rs @@ -0,0 +1,584 @@ +mod budget; +mod expand; +mod lexer; +mod rule; +mod time; +mod vtimezone; + +use std::collections::{HashMap, HashSet}; + +use chrono::{DateTime, Duration, NaiveDate, NaiveDateTime, NaiveTime, Utc}; +use chrono_tz::Tz; + +use crate::link::{ConferenceEntry, ConferenceKind}; +use crate::model::{EventStatus, Response, response_from_partstat}; +use crate::text::non_empty; +use crate::tz::{Zone, resolve_tzid}; + +pub use expand::expand; +pub use vtimezone::ZoneResolution; + +use budget::{Budget, FEED_BUDGET}; +use lexer::{Component, Property, lex, unescape_text}; +use time::{IcsTime, parse_duration_parts, parse_property}; + +const MAX_VTIMEZONES: usize = 64; + +#[derive(Clone, Debug, PartialEq)] +pub struct ParseOptions { + pub calendar_id: String, + pub default_zone: Tz, + pub default_duration: Duration, + pub max_instances_per_series: usize, + pub max_events: usize, + pub include_cancelled: bool, +} + +impl Default for ParseOptions { + fn default() -> Self { + Self { + calendar_id: String::new(), + default_zone: Tz::UTC, + default_duration: Duration::minutes(30), + max_instances_per_series: 1_000, + max_events: 50_000, + include_cancelled: false, + } + } +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct ResolvedZone { + pub tzid: String, + pub zone_name: String, + pub how: ZoneResolution, +} + +#[derive(Clone, Debug)] +pub struct ParsedCalendar { + pub name: Option, + pub time_zone: Option, + pub warnings: usize, + pub warning_messages: Vec, + pub zones: Vec, + pub(crate) events: Vec, + pub(crate) options: ParseOptions, + pub(crate) budget: Budget, +} + +impl ParsedCalendar { + pub fn component_count(&self) -> usize { + self.events.len() + } + + fn warn(&mut self, message: impl Into) { + self.warnings += 1; + if self.warning_messages.len() < 32 { + self.warning_messages.push(message.into()); + } + } +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) struct Span { + pub nominal_days: i64, + pub exact: Duration, +} + +impl Span { + fn days(days: i64) -> Self { + Self { + nominal_days: days, + exact: Duration::zero(), + } + } + + fn exact(exact: Duration) -> Self { + Self { + nominal_days: 0, + exact, + } + } + + pub fn upper_bound(&self) -> Duration { + Duration::try_days(self.nominal_days.saturating_add(1)) + .and_then(|d| d.checked_add(&self.exact)) + .unwrap_or(Duration::MAX) + } +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) enum Exclusion { + Instant(DateTime), + Day(NaiveDate), +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub(crate) struct RawPerson { + pub name: Option, + pub email: Option, + pub response: Response, +} + +#[derive(Clone, Debug)] +pub(crate) struct RawEvent { + pub uid: String, + pub title: String, + pub description: Option, + pub alt_description: Option, + pub location: Option, + pub url: Option, + pub status: EventStatus, + pub free: bool, + pub start_wall: NaiveDateTime, + pub zone: Zone, + pub all_day: bool, + pub span: Span, + pub organizer: Option, + pub attendees: Vec, + pub rule: Option, + pub exdates: Vec, + pub rdates: Vec>, + pub recurrence_id: Option>, + pub conference: Vec, +} + +impl RawEvent { + pub fn start_utc(&self) -> DateTime { + self.zone.to_utc(self.start_wall) + } + + pub fn end_for(&self, start_wall: NaiveDateTime, start_utc: DateTime) -> DateTime { + let Span { + nominal_days, + exact, + } = self.span; + let nominal = if nominal_days == 0 { + start_utc + } else { + Duration::try_days(nominal_days) + .and_then(|d| start_wall.checked_add_signed(d)) + .map(|w| self.zone.to_utc(w)) + .unwrap_or(start_utc) + }; + let end = nominal.checked_add_signed(exact).unwrap_or(nominal); + if end > start_utc { + end + } else { + Duration::try_days(nominal_days) + .and_then(|d| d.checked_add(&exact)) + .and_then(|d| start_utc.checked_add_signed(d)) + .unwrap_or(start_utc) + .max(start_utc) + } + } + + pub fn is_recurring(&self) -> bool { + self.rule.is_some() || !self.rdates.is_empty() || self.recurrence_id.is_some() + } +} + +struct ZoneContext { + zones: HashMap, + looked_up: HashMap>, + floating: Zone, +} + +impl ZoneContext { + fn zone_for(&mut self, tzid: &str, cal: &mut ParsedCalendar) -> Zone { + if let Some(zone) = self.zones.get(tzid) { + return *zone; + } + let found = match self.looked_up.get(tzid) { + Some(found) => *found, + None => { + let found = resolve_tzid(tzid); + self.looked_up.insert(tzid.to_string(), found); + found + } + }; + if let Some(zone) = found { + return zone; + } + cal.warn(format!("unknown TZID {tzid}, using the default zone")); + self.floating + } + + fn wall_and_zone(&mut self, time: &IcsTime, cal: &mut ParsedCalendar) -> (NaiveDateTime, Zone) { + match time { + IcsTime::Date(d) => (d.and_time(NaiveTime::MIN), Zone::Utc), + IcsTime::Utc(n) => (*n, Zone::Utc), + IcsTime::Floating(n) => (*n, self.floating), + IcsTime::Zoned(n, tzid) => (*n, self.zone_for(tzid, cal)), + } + } + + fn instant(&mut self, time: &IcsTime, cal: &mut ParsedCalendar) -> DateTime { + let (wall, zone) = self.wall_and_zone(time, cal); + zone.to_utc(wall) + } +} + +pub fn parse_calendar(text: &str, opts: &ParseOptions) -> ParsedCalendar { + let lexed = lex(text); + let mut cal = ParsedCalendar { + name: None, + time_zone: None, + warnings: 0, + warning_messages: Vec::new(), + zones: Vec::new(), + events: Vec::new(), + options: opts.clone(), + budget: Budget::new(FEED_BUDGET), + }; + for w in lexed.warnings { + cal.warn(w); + } + let calendars: Vec<&Component> = if lexed.root.children.iter().any(|c| c.name == "VCALENDAR") { + lexed + .root + .children + .iter() + .filter(|c| c.name == "VCALENDAR") + .collect() + } else { + if !lexed.root.children.is_empty() { + cal.warn("no VCALENDAR wrapper"); + } + vec![&lexed.root] + }; + for vcal in calendars { + parse_vcalendar(vcal, &mut cal); + } + cal +} + +pub fn parse_calendar_bytes(bytes: &[u8], opts: &ParseOptions) -> ParsedCalendar { + let bytes = bytes.strip_prefix(b"\xef\xbb\xbf").unwrap_or(bytes); + let mut joined = Vec::with_capacity(bytes.len()); + let mut i = 0; + while i < bytes.len() { + let fold = match (bytes[i], bytes.get(i + 1), bytes.get(i + 2)) { + (b'\r', Some(b'\n'), Some(b' ' | b'\t')) => 3, + (b'\n', Some(b' ' | b'\t'), _) => 2, + _ => 0, + }; + if fold > 0 { + i += fold; + continue; + } + joined.push(bytes[i]); + i += 1; + } + parse_calendar(&String::from_utf8_lossy(&joined), opts) +} + +fn parse_vcalendar(vcal: &Component, cal: &mut ParsedCalendar) { + if cal.name.is_none() { + cal.name = vcal + .text("X-WR-CALNAME") + .or_else(|| vcal.text("NAME")) + .and_then(|n| non_empty(Some(&n))); + } + let wr_tz = vcal + .prop("X-WR-TIMEZONE") + .map(|p| p.value.trim().to_string()); + if cal.time_zone.is_none() { + cal.time_zone = wr_tz.clone().filter(|t| !t.is_empty()); + } + let default_zone = Zone::Named(cal.options.default_zone); + let floating = match wr_tz.as_deref().filter(|t| !t.is_empty()) { + Some(tz) => resolve_tzid(tz).unwrap_or_else(|| { + cal.warn(format!("unknown X-WR-TIMEZONE {tz}")); + default_zone + }), + None => default_zone, + }; + let mut ctx = ZoneContext { + zones: HashMap::new(), + looked_up: HashMap::new(), + floating, + }; + let mut seen_tzids: HashSet = HashSet::new(); + for (index, comp) in vcal + .children + .iter() + .filter(|c| c.name == "VTIMEZONE") + .enumerate() + { + if index >= MAX_VTIMEZONES { + cal.warn("too many VTIMEZONE blocks, ignoring the rest"); + break; + } + let tzid = comp + .prop("TZID") + .map(|p| p.value.trim().to_string()) + .unwrap_or_default(); + if !tzid.is_empty() && !seen_tzids.insert(tzid.clone()) { + cal.warn(format!("duplicate VTIMEZONE {tzid}")); + continue; + } + match vtimezone::resolve_vtimezone(comp, &mut cal.budget) { + Some((tzid, zone, how)) => { + let zone_name = match zone { + Zone::Utc => "UTC".to_string(), + Zone::Named(tz) => tz.name().to_string(), + Zone::Fixed(offset) => offset.to_string(), + }; + cal.zones.push(ResolvedZone { + tzid: tzid.clone(), + zone_name, + how, + }); + ctx.zones.insert(tzid, zone); + } + None => cal.warn("VTIMEZONE without usable TZID or offsets"), + } + } + for comp in vcal.children.iter().filter(|c| c.name == "VEVENT") { + if cal.events.len() >= cal.options.max_events { + cal.warn("event limit reached, ignoring the rest of the feed"); + break; + } + match parse_event(comp, &mut ctx, cal) { + Ok(event) => cal.events.push(event), + Err(message) => cal.warn(message), + } + } +} + +fn first_time(prop: &Property) -> Option { + parse_property(prop).into_iter().next() +} + +fn parse_person(prop: &Property) -> RawPerson { + let value = prop.value.trim(); + let email = prop + .param("EMAIL") + .map(str::to_string) + .or_else(|| { + let lower = value.to_ascii_lowercase(); + lower + .starts_with("mailto:") + .then(|| value["mailto:".len()..].to_string()) + .or_else(|| { + (value.contains('@') && !value.contains(':')).then(|| value.to_string()) + }) + }) + .map(|e| e.trim().to_string()) + .filter(|e| e.contains('@')); + RawPerson { + name: non_empty(prop.param("CN")), + email, + response: prop + .param("PARTSTAT") + .map_or(Response::Unknown, response_from_partstat), + } +} + +fn is_resource(prop: &Property) -> bool { + prop.param("CUTYPE") + .is_some_and(|c| c.eq_ignore_ascii_case("ROOM") || c.eq_ignore_ascii_case("RESOURCE")) + || prop + .param("ROLE") + .is_some_and(|r| r.eq_ignore_ascii_case("NON-PARTICIPANT")) +} + +fn conference_entries(comp: &Component) -> Vec { + let mut out = Vec::new(); + for name in [ + "X-GOOGLE-CONFERENCE", + "X-MICROSOFT-SKYPETEAMSMEETINGURL", + "X-MICROSOFT-ONLINEMEETINGEXTERNALLINK", + "X-MICROSOFT-ONLINEMEETINGCONFLINK", + "CONFERENCE", + ] { + for prop in comp.props_named(name) { + let uri = unescape_text(prop.value.trim()); + let lower = uri.to_ascii_lowercase(); + if !(lower.starts_with("https://") || lower.starts_with("http://")) { + continue; + } + let is_video = name != "CONFERENCE" + || prop + .param("FEATURE") + .is_none_or(|f| f.to_ascii_uppercase().contains("VIDEO")); + out.push(ConferenceEntry { + kind: if is_video { + ConferenceKind::Video + } else { + ConferenceKind::Other + }, + uri, + meeting_code: None, + }); + } + } + out +} + +fn parse_event( + comp: &Component, + ctx: &mut ZoneContext, + cal: &mut ParsedCalendar, +) -> Result { + let start_time = comp + .prop("DTSTART") + .and_then(first_time) + .ok_or_else(|| "VEVENT without a valid DTSTART".to_string())?; + let alldayish = comp + .prop("X-MICROSOFT-CDO-ALLDAYEVENT") + .is_some_and(|p| p.value.trim().eq_ignore_ascii_case("TRUE")); + let start_time = match start_time { + IcsTime::Floating(n) | IcsTime::Zoned(n, _) if alldayish && n.time() == NaiveTime::MIN => { + IcsTime::Date(n.date()) + } + other => other, + }; + let all_day = matches!(start_time, IcsTime::Date(_)); + let (start_wall, zone) = ctx.wall_and_zone(&start_time, cal); + let start_utc = zone.to_utc(start_wall); + let default_span = if all_day { + Span::days(1) + } else { + Span::exact(cal.options.default_duration) + }; + let span = if let Some(end_time) = comp.prop("DTEND").and_then(first_time) { + let (end_wall, end_zone) = ctx.wall_and_zone(&end_time, cal); + if all_day { + let days = end_wall + .date() + .signed_duration_since(start_wall.date()) + .num_days(); + if days > 0 { + Span::days(days) + } else { + default_span + } + } else { + let mut exact = end_zone.to_utc(end_wall).signed_duration_since(start_utc); + if exact <= Duration::zero() && end_zone == zone { + exact = end_wall.signed_duration_since(start_wall); + } + if exact > Duration::zero() { + Span::exact(exact) + } else { + default_span + } + } + } else if let Some((days, seconds)) = comp + .prop("DURATION") + .and_then(|p| parse_duration_parts(&p.value)) + { + if days.saturating_mul(86_400).saturating_add(seconds) <= 0 { + default_span + } else if all_day { + Span::days(days.saturating_add(seconds / 86_400).max(1)) + } else { + Span { + nominal_days: days, + exact: Duration::seconds(seconds), + } + } + } else { + default_span + }; + + let title = comp + .text("SUMMARY") + .map(|s| s.trim().to_string()) + .unwrap_or_default(); + let uid = comp + .text("UID") + .map(|u| u.trim().to_string()) + .filter(|u| !u.is_empty()) + .unwrap_or_else(|| synthetic_uid(&title, start_utc)); + + let status = match comp + .prop("STATUS") + .map(|p| p.value.trim().to_ascii_uppercase()) + .as_deref() + { + Some("CANCELLED") => EventStatus::Cancelled, + Some("TENTATIVE") => EventStatus::Tentative, + _ => EventStatus::Confirmed, + }; + let free = comp + .prop("TRANSP") + .is_some_and(|p| p.value.trim().eq_ignore_ascii_case("TRANSPARENT")) + || comp + .prop("X-MICROSOFT-CDO-BUSYSTATUS") + .is_some_and(|p| p.value.trim().eq_ignore_ascii_case("FREE")); + + let rule = match comp.prop("RRULE") { + Some(prop) => match rule::build_rule(&prop.value, start_wall, zone) { + Ok(rule) => Some(rule), + Err(e) => { + cal.warn(format!("{uid}: ignoring RRULE ({e})")); + None + } + }, + None => None, + }; + + let mut exdates = Vec::new(); + for time in comp.props_named("EXDATE").flat_map(parse_property) { + match time { + IcsTime::Date(d) => exdates.push(Exclusion::Day(d)), + other => exdates.push(Exclusion::Instant(ctx.instant(&other, cal))), + } + } + let mut rdates = Vec::new(); + for time in comp.props_named("RDATE").flat_map(parse_property) { + let instant = match time { + IcsTime::Date(d) if !all_day => zone.to_utc(d.and_time(start_wall.time())), + other => ctx.instant(&other, cal), + }; + rdates.push(instant); + } + let recurrence_id = comp + .prop("RECURRENCE-ID") + .and_then(first_time) + .map(|t| match t { + IcsTime::Date(d) if !all_day => zone.to_utc(d.and_time(start_wall.time())), + other => ctx.instant(&other, cal), + }); + + let organizer = comp.prop("ORGANIZER").map(parse_person); + let attendees = comp + .props_named("ATTENDEE") + .filter(|p| !is_resource(p)) + .map(parse_person) + .collect(); + + Ok(RawEvent { + uid, + title, + description: comp.text("DESCRIPTION").and_then(|d| non_empty(Some(&d))), + alt_description: comp.prop("X-ALT-DESC").map(|p| unescape_text(&p.value)), + location: comp.text("LOCATION").and_then(|l| non_empty(Some(&l))), + url: comp.text("URL").and_then(|u| non_empty(Some(&u))), + status, + free, + start_wall, + zone, + all_day, + span, + organizer, + attendees, + rule, + exdates, + rdates, + recurrence_id, + conference: conference_entries(comp), + }) +} + +fn synthetic_uid(title: &str, start: DateTime) -> String { + let mut hash: u64 = 0xcbf2_9ce4_8422_2325; + for byte in title.bytes() { + hash ^= u64::from(byte); + hash = hash.wrapping_mul(0x0100_0000_01b3); + } + format!("generated-{hash:016x}-{}", start.timestamp()) +} diff --git a/apps/desktop-gpui/calendar/src/ics/rule.rs b/apps/desktop-gpui/calendar/src/ics/rule.rs new file mode 100644 index 00000000000..18d80edb294 --- /dev/null +++ b/apps/desktop-gpui/calendar/src/ics/rule.rs @@ -0,0 +1,528 @@ +use chrono::{ + Datelike, Duration, NaiveDate, NaiveDateTime, NaiveTime, TimeZone, Timelike, Weekday, +}; +use rrule::{Frequency, RRule, RRuleSet, RRuleSetIter, Unvalidated}; + +use super::budget::Budget; +use super::time::{IcsTime, parse_one}; +use crate::tz::Zone; + +const RRULE_LOOP_LIMIT: u64 = 100_000; +const RRULE_MAX_YEAR: i32 = 10_000; +const MAX_TIMES_PER_DAY: usize = 96; +const YIELD_COST: u64 = 8; + +const KNOWN_PARTS: [&str; 13] = [ + "FREQ", + "INTERVAL", + "COUNT", + "BYSECOND", + "BYMINUTE", + "BYHOUR", + "BYDAY", + "BYMONTHDAY", + "BYYEARDAY", + "BYWEEKNO", + "BYMONTH", + "BYSETPOS", + "WKST", +]; + +fn as_wall(dt: NaiveDateTime) -> chrono::DateTime { + rrule::Tz::UTC.from_utc_datetime(&dt) +} + +#[derive(Clone, Debug)] +pub(crate) struct Recurrence { + rule: RRule, + until: Option, + count: Option, + freq: Frequency, + interval: i64, + week_start: Weekday, + period_cost: u64, +} + +impl Recurrence { + fn period_index(&self, wall: NaiveDateTime) -> i64 { + match self.freq { + Frequency::Yearly => i64::from(wall.year()), + Frequency::Monthly => i64::from(wall.year()) * 12 + i64::from(wall.month0()), + Frequency::Weekly => week_start_of(wall.date(), self.week_start) + .num_days_from_ce() + .div_euclid(7) + .into(), + Frequency::Daily => wall.date().num_days_from_ce().into(), + _ => i64::from(wall.date().num_days_from_ce()) * 24 + i64::from(wall.time().hour()), + } + } + + fn period_start(&self, index: i64) -> Option { + let midnight = |date: NaiveDate| date.and_time(NaiveTime::MIN); + match self.freq { + Frequency::Yearly => { + NaiveDate::from_ymd_opt(i32::try_from(index).ok()?, 1, 1).map(midnight) + } + Frequency::Monthly => { + let year = i32::try_from(index.div_euclid(12)).ok()?; + let month = u32::try_from(index.rem_euclid(12) + 1).ok()?; + NaiveDate::from_ymd_opt(year, month, 1).map(midnight) + } + Frequency::Weekly => { + let last_day = index.checked_mul(7)?.checked_add(6)?; + NaiveDate::from_num_days_from_ce_opt(i32::try_from(last_day).ok()?) + .map(|d| midnight(week_start_of(d, self.week_start))) + } + Frequency::Daily => { + NaiveDate::from_num_days_from_ce_opt(i32::try_from(index).ok()?).map(midnight) + } + _ => { + let day = i32::try_from(index.div_euclid(24)).ok()?; + let hour = u32::try_from(index.rem_euclid(24)).ok()?; + NaiveDate::from_num_days_from_ce_opt(day)?.and_hms_opt(hour, 0, 0) + } + } + } + + fn periods_between(&self, from: NaiveDateTime, to: NaiveDateTime) -> u64 { + let steps = (self.period_index(to) - self.period_index(from)).max(0) / self.interval; + u64::try_from(steps).unwrap_or(0).max(1) + } + + fn worst_call(&self, position: NaiveDateTime) -> u64 { + let loops = match self.freq { + Frequency::Yearly => { + let years = i64::from(RRULE_MAX_YEAR - position.year()).max(0) / self.interval; + u64::try_from(years + 1).unwrap_or(1).min(RRULE_LOOP_LIMIT) + } + _ => RRULE_LOOP_LIMIT, + }; + loops.saturating_mul(self.period_cost) + } + + fn fast_forward(&self, dtstart_wall: NaiveDateTime, anchor: NaiveDateTime) -> NaiveDateTime { + if self.count.is_some() || anchor <= dtstart_wall { + return dtstart_wall; + } + let first = self.period_index(dtstart_wall); + let skip = (self.period_index(anchor) - first).div_euclid(self.interval); + if skip < 1 { + return dtstart_wall; + } + skip.checked_mul(self.interval) + .and_then(|offset| first.checked_add(offset)) + .and_then(|index| self.period_start(index)) + .filter(|start| *start > dtstart_wall && *start <= anchor) + .unwrap_or(dtstart_wall) + } + + pub fn walls<'a>( + &'a self, + dtstart_wall: NaiveDateTime, + anchor: NaiveDateTime, + budget: &'a mut Budget, + ) -> Walls<'a> { + let start = self.fast_forward(dtstart_wall, anchor); + let set = RRuleSet::new(as_wall(start)) + .rrule(self.rule.clone()) + .limit(); + Walls { + iter: (&set).into_iter(), + rec: self, + budget, + position: start, + yielded: 0, + done: false, + } + } +} + +fn week_start_of(date: NaiveDate, week_start: Weekday) -> NaiveDate { + let back = (7 + date.weekday().num_days_from_monday() - week_start.num_days_from_monday()) % 7; + date.checked_sub_signed(Duration::days(i64::from(back))) + .unwrap_or(date) +} + +pub(crate) struct Walls<'a> { + iter: RRuleSetIter, + rec: &'a Recurrence, + budget: &'a mut Budget, + position: NaiveDateTime, + yielded: u32, + done: bool, +} + +impl Iterator for Walls<'_> { + type Item = NaiveDateTime; + + fn next(&mut self) -> Option { + if self.done { + return None; + } + // rrule scans up to 100,000 empty periods inside one call, so a call is only made when + // the budget could pay for that scan; the real cost is charged once the result is known. + if !self.budget.can_afford(self.rec.worst_call(self.position)) { + self.budget.charge(u64::MAX); + self.done = true; + return None; + } + let Some(next) = self.iter.next().map(|dt| dt.naive_utc()) else { + let finished_by_count = self.rec.count.is_some_and(|c| self.yielded >= c); + self.budget.charge(if finished_by_count { + 1 + } else { + self.rec.worst_call(self.position) + }); + self.done = true; + return None; + }; + let periods = self.rec.periods_between(self.position, next); + self.budget.charge( + periods + .saturating_mul(self.rec.period_cost) + .saturating_add(YIELD_COST), + ); + self.position = next; + self.yielded = self.yielded.saturating_add(1); + if self.rec.until.is_some_and(|until| next > until) { + self.done = true; + return None; + } + Some(next) + } +} + +fn times_per_day(rule: &RRule) -> usize { + let minutes_seconds = rule.get_by_minute().len().max(1) * rule.get_by_second().len().max(1); + match rule.get_freq() { + Frequency::Hourly => { + let interval = usize::from(rule.get_interval().max(1)); + let hours = if rule.get_by_hour().is_empty() { + 24usize.div_ceil(interval) + } else { + rule.get_by_hour().len().min(24usize.div_ceil(interval)) + }; + hours * minutes_seconds + } + _ => rule.get_by_hour().len().max(1) * minutes_seconds, + } +} + +fn period_cost(freq: Frequency, times_per_day: usize) -> u64 { + let (base, days) = match freq { + Frequency::Yearly => (16, 366), + Frequency::Monthly => (2, 31), + Frequency::Weekly => (1, 7), + Frequency::Hourly => (2, 1), + _ => (1, 1), + }; + base + days * times_per_day as u64 / 32 +} + +pub(crate) fn build_rule( + value: &str, + dtstart_wall: NaiveDateTime, + zone: Zone, +) -> Result { + // rrule 0.14 slices BYDAY values by byte offset and panics on non-ASCII input. + if !value.is_ascii() { + return Err("RRULE contains non-ASCII characters".to_string()); + } + let mut parts: Vec = Vec::new(); + let mut until = None; + let mut has_freq = false; + for part in value.split(';') { + let Some((key, val)) = part.split_once('=') else { + continue; + }; + let key = key.trim().to_ascii_uppercase(); + let val = val.trim().to_ascii_uppercase(); + if val.is_empty() { + continue; + } + if key == "UNTIL" { + until = Some(val); + continue; + } + if !KNOWN_PARTS.contains(&key.as_str()) + || parts + .iter() + .any(|p| p.split('=').next() == Some(key.as_str())) + { + continue; + } + if key == "FREQ" { + if val == "MINUTELY" || val == "SECONDLY" { + return Err(format!("FREQ={val} is not a meeting cadence")); + } + has_freq = true; + } + if key == "INTERVAL" && val.parse::().is_ok_and(|n| n == 0) { + continue; + } + if key == "COUNT" && val.parse::().is_ok_and(|n| n == 0) { + return Err("COUNT=0".to_string()); + } + parts.push(format!("{key}={val}")); + } + if !has_freq { + return Err(format!("RRULE without FREQ: {value}")); + } + let rule: RRule = parts + .join(";") + .parse() + .map_err(|e: rrule::RRuleError| e.to_string())?; + let until = match until { + Some(until) => { + let wall = match parse_one(&until, None, false) { + Some(IcsTime::Date(date)) => { + date.and_time(NaiveTime::from_hms_opt(23, 59, 59).unwrap_or_default()) + } + Some(IcsTime::Utc(naive)) => zone.from_utc(naive.and_utc()), + Some(IcsTime::Floating(naive) | IcsTime::Zoned(naive, _)) => naive, + None => return Err(format!("bad UNTIL: {until}")), + }; + if wall < dtstart_wall { + return Err("UNTIL before DTSTART".to_string()); + } + Some(wall) + } + None => None, + }; + let rule = rule + .validate(as_wall(dtstart_wall)) + .map_err(|e| e.to_string())?; + let times = times_per_day(&rule); + if times > MAX_TIMES_PER_DAY { + return Err(format!( + "{times} occurrences a day is not a meeting cadence" + )); + } + let freq = rule.get_freq(); + Ok(Recurrence { + count: rule.get_count(), + interval: i64::from(rule.get_interval().max(1)), + week_start: rule.get_week_start(), + period_cost: period_cost(freq, times), + freq, + until, + rule, + }) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::ics::budget::FEED_BUDGET; + + fn wall(y: i32, m: u32, d: u32, h: u32) -> NaiveDateTime { + NaiveDate::from_ymd_opt(y, m, d) + .and_then(|d| d.and_hms_opt(h, 0, 0)) + .unwrap() + } + + fn all(rule: &Recurrence, start: NaiveDateTime) -> Vec { + let mut budget = Budget::new(FEED_BUDGET); + rule.walls(start, start, &mut budget).collect() + } + + #[test] + fn rebuilds_messy_rules() { + let start = wall(2026, 1, 5, 9); + let rule = build_rule( + "freq=weekly;;byday=MO,WE;X-NAME=foo;INTERVAL=0;WKST=SU;", + start, + Zone::Utc, + ) + .unwrap(); + let mut budget = Budget::new(FEED_BUDGET); + let got: Vec<_> = rule.walls(start, start, &mut budget).take(3).collect(); + assert_eq!(got, vec![start, wall(2026, 1, 7, 9), wall(2026, 1, 12, 9)]); + } + + #[test] + fn until_in_utc_is_converted_to_wall_clock() { + let start = wall(2026, 3, 2, 9); + let ny = Zone::Named(chrono_tz::Tz::America__New_York); + let rule = build_rule("FREQ=DAILY;UNTIL=20260304T140000Z", start, ny).unwrap(); + assert_eq!(all(&rule, start).len(), 3); + } + + #[test] + fn rejects_bad_rules() { + let start = wall(2026, 1, 5, 9); + assert!(build_rule("BYDAY=MO", start, Zone::Utc).is_err()); + assert!(build_rule("FREQ=SOMETIMES", start, Zone::Utc).is_err()); + assert!(build_rule("FREQ=DAILY;UNTIL=20200101T000000Z", start, Zone::Utc).is_err()); + assert!(build_rule("FREQ=DAILY;COUNT=0", start, Zone::Utc).is_err()); + assert!(build_rule("FREQ=MONTHLY;BYMONTHDAY=40", start, Zone::Utc).is_err()); + assert!(build_rule("FREQ=DAILY;UNTIL=nope", start, Zone::Utc).is_err()); + } + + #[test] + fn sub_hourly_cadences_are_not_rules() { + let start = wall(2026, 1, 5, 9); + assert!(build_rule("FREQ=SECONDLY", start, Zone::Utc).is_err()); + assert!(build_rule("FREQ=MINUTELY;INTERVAL=30", start, Zone::Utc).is_err()); + assert!(build_rule("FREQ=HOURLY;BYMINUTE=0,5,10,15,20", start, Zone::Utc).is_err()); + assert!( + build_rule( + "FREQ=DAILY;BYHOUR=0,1,2,3,4,5,6,7,8,9,10,11;BYMINUTE=0,5,10,15,20,25,30,35,40", + start, + Zone::Utc + ) + .is_err() + ); + assert!(build_rule("FREQ=HOURLY;INTERVAL=2;BYMINUTE=0,30", start, Zone::Utc).is_ok()); + assert!( + build_rule( + "FREQ=DAILY;BYHOUR=9,10,11,12;BYMINUTE=0,15,30,45", + start, + Zone::Utc + ) + .is_ok() + ); + } + + fn naive_window( + rule: &Recurrence, + start: NaiveDateTime, + from: NaiveDateTime, + to: NaiveDateTime, + ) -> Vec { + let mut budget = Budget::new(u64::MAX / 2); + rule.walls(start, start, &mut budget) + .skip_while(|w| *w < from) + .take_while(|w| *w <= to) + .collect() + } + + fn fast_window( + rule: &Recurrence, + start: NaiveDateTime, + from: NaiveDateTime, + to: NaiveDateTime, + ) -> (Vec, u64) { + let mut budget = Budget::new(FEED_BUDGET); + let got = rule + .walls(start, from, &mut budget) + .skip_while(|w| *w < from) + .take_while(|w| *w <= to) + .collect(); + (got, FEED_BUDGET - budget.left()) + } + + #[test] + fn fast_forward_matches_naive_stepping() { + let rules = [ + "FREQ=YEARLY", + "FREQ=YEARLY;INTERVAL=3", + "FREQ=YEARLY;BYMONTH=2;BYMONTHDAY=29", + "FREQ=YEARLY;BYMONTH=3,11;BYDAY=2SU", + "FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU;BYHOUR=1,3", + "FREQ=YEARLY;BYWEEKNO=1,20,53;BYDAY=MO", + "FREQ=YEARLY;BYYEARDAY=1,100,-1", + "FREQ=YEARLY;INTERVAL=2;BYMONTH=1,7;BYDAY=FR;BYSETPOS=-1", + "FREQ=MONTHLY", + "FREQ=MONTHLY;INTERVAL=5", + "FREQ=MONTHLY;BYMONTHDAY=31", + "FREQ=MONTHLY;BYMONTHDAY=-1,15", + "FREQ=MONTHLY;BYDAY=FR;BYMONTHDAY=13", + "FREQ=MONTHLY;INTERVAL=2;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=1,-1", + "FREQ=MONTHLY;BYDAY=-2TH;BYMONTH=1,4,7,10", + "FREQ=WEEKLY", + "FREQ=WEEKLY;INTERVAL=2;BYDAY=MO,TH", + "FREQ=WEEKLY;INTERVAL=3;BYDAY=SU,SA;WKST=SU", + "FREQ=WEEKLY;INTERVAL=2;BYDAY=TU;WKST=WE", + "FREQ=WEEKLY;INTERVAL=4;BYDAY=MO,FR;BYHOUR=9,15;BYMINUTE=0,30", + "FREQ=WEEKLY;BYMONTH=6,12;BYDAY=WE", + "FREQ=DAILY", + "FREQ=DAILY;INTERVAL=3", + "FREQ=DAILY;INTERVAL=10;BYDAY=MO,WE,FR", + "FREQ=DAILY;BYMONTH=2;BYMONTHDAY=28,29", + "FREQ=DAILY;BYHOUR=8,12,17;BYMINUTE=15", + "FREQ=DAILY;INTERVAL=2;BYDAY=TU,TH;BYSETPOS=1", + "FREQ=HOURLY", + "FREQ=HOURLY;INTERVAL=5", + "FREQ=HOURLY;INTERVAL=7;BYHOUR=9,10,11,12,13,14,15,16,17", + "FREQ=HOURLY;INTERVAL=3;BYDAY=MO,TU,WE,TH,FR;BYMINUTE=0,30", + "FREQ=HOURLY;INTERVAL=25", + ]; + let starts = [ + wall(2019, 1, 1, 0), + NaiveDate::from_ymd_opt(2020, 2, 29) + .and_then(|d| d.and_hms_opt(23, 45, 10)) + .unwrap(), + wall(2021, 7, 18, 13), + wall(2022, 12, 31, 9), + ]; + let anchors = [ + wall(2026, 1, 1, 0), + NaiveDate::from_ymd_opt(2026, 3, 29) + .and_then(|d| d.and_hms_opt(5, 17, 0)) + .unwrap(), + wall(2026, 11, 2, 23), + ]; + let mut cases = 0; + let mut empty = 0; + for text in rules { + for start in starts { + let rule = build_rule(text, start, Zone::Utc).unwrap(); + let span = match rule.freq { + Frequency::Yearly => Duration::days(366 * 9), + Frequency::Monthly => Duration::days(400), + Frequency::Weekly => Duration::days(120), + _ => Duration::days(21), + }; + for from in anchors { + let to = from + span; + let naive = naive_window(&rule, start, from, to); + let (fast, cost) = fast_window(&rule, start, from, to); + assert_eq!(fast, naive, "{text} from {start} window {from}..{to}"); + empty += usize::from(naive.is_empty()); + assert!( + cost <= 2 * naive.len() as u64 * rule.period_cost + 50_000, + "{text} from {start}: cost {cost} for {} walls", + naive.len() + ); + cases += 1; + } + } + } + assert_eq!(cases, rules.len() * starts.len() * anchors.len()); + assert!(empty * 10 < cases, "{empty} of {cases} windows were empty"); + } + + #[test] + fn hourly_series_from_years_ago_reaches_the_window() { + let start = wall(2015, 1, 1, 9); + let rule = build_rule("FREQ=HOURLY", start, Zone::Utc).unwrap(); + let from = wall(2026, 9, 1, 0); + let (got, cost) = fast_window(&rule, start, from, wall(2026, 9, 1, 23)); + assert_eq!(got.len(), 24); + assert_eq!(got[0], from); + assert!(cost < 1_000, "{cost}"); + } + + #[test] + fn counted_rules_step_from_the_start_and_stay_exact() { + let start = wall(2025, 1, 6, 9); + let rule = build_rule("FREQ=WEEKLY;COUNT=60", start, Zone::Utc).unwrap(); + let (got, _) = fast_window(&rule, start, wall(2026, 2, 1, 0), wall(2026, 12, 31, 0)); + assert_eq!(got.len(), 4); + assert_eq!(got.last(), Some(&wall(2026, 2, 23, 9))); + } + + #[test] + fn rules_without_occurrences_are_bounded_by_the_budget() { + let start = wall(2000, 1, 1, 0); + let rule = build_rule("FREQ=YEARLY;BYMONTH=2;BYMONTHDAY=30", start, Zone::Utc).unwrap(); + let mut budget = Budget::new(FEED_BUDGET); + assert_eq!(rule.walls(start, start, &mut budget).count(), 0); + assert!(budget.left() < FEED_BUDGET); + let mut empty = Budget::new(10); + let rule = build_rule("FREQ=DAILY", start, Zone::Utc).unwrap(); + assert_eq!(rule.walls(start, start, &mut empty).next(), None); + assert_eq!(empty.left(), 0); + } +} diff --git a/apps/desktop-gpui/calendar/src/ics/time.rs b/apps/desktop-gpui/calendar/src/ics/time.rs new file mode 100644 index 00000000000..905afb99610 --- /dev/null +++ b/apps/desktop-gpui/calendar/src/ics/time.rs @@ -0,0 +1,200 @@ +use chrono::{NaiveDate, NaiveDateTime, NaiveTime}; + +use super::lexer::Property; + +#[derive(Clone, Debug, PartialEq, Eq)] +pub(crate) enum IcsTime { + Date(NaiveDate), + Floating(NaiveDateTime), + Utc(NaiveDateTime), + Zoned(NaiveDateTime, String), +} + +const MIN_YEAR: i32 = 1000; +const MAX_YEAR: i32 = 3000; + +pub(crate) fn parse_date(value: &str) -> Option { + let digits: String = value.chars().filter(|c| *c != '-').collect(); + if digits.len() != 8 || !digits.bytes().all(|b| b.is_ascii_digit()) { + return None; + } + let year: i32 = digits[..4].parse().ok()?; + if !(MIN_YEAR..=MAX_YEAR).contains(&year) { + return None; + } + NaiveDate::from_ymd_opt(year, digits[4..6].parse().ok()?, digits[6..8].parse().ok()?) +} + +pub(crate) fn parse_date_time(value: &str) -> Option<(NaiveDateTime, bool)> { + let value = value.trim(); + let (body, utc) = match value.strip_suffix(['Z', 'z']) { + Some(body) => (body, true), + None => (value, false), + }; + let (date_part, time_part) = body.split_once(['T', 't'])?; + let date = parse_date(date_part)?; + let time_digits: String = time_part.chars().filter(|c| *c != ':').collect(); + let time_digits = time_digits.split('.').next().unwrap_or(""); + if !time_digits.bytes().all(|b| b.is_ascii_digit()) { + return None; + } + let (h, m, s) = match time_digits.len() { + 4 => ( + time_digits[..2].parse().ok()?, + time_digits[2..].parse().ok()?, + 0, + ), + 6 => ( + time_digits[..2].parse().ok()?, + time_digits[2..4].parse().ok()?, + time_digits[4..].parse::().ok()?.min(59), + ), + _ => return None, + }; + let time = NaiveTime::from_hms_opt(h, m, s)?; + Some((date.and_time(time), utc)) +} + +pub(crate) fn parse_one(value: &str, tzid: Option<&str>, is_date: bool) -> Option { + let value = value.trim(); + let looks_like_date = !value.contains(['T', 't']); + if is_date || looks_like_date { + if let Some(date) = parse_date(value) { + return Some(IcsTime::Date(date)); + } + if looks_like_date { + return None; + } + } + let (naive, utc) = parse_date_time(value)?; + if utc { + return Some(IcsTime::Utc(naive)); + } + match tzid.map(str::trim).filter(|t| !t.is_empty()) { + Some(tzid) => Some(IcsTime::Zoned(naive, tzid.to_string())), + None => Some(IcsTime::Floating(naive)), + } +} + +pub(crate) fn parse_property(prop: &Property) -> Vec { + let is_date = prop + .param("VALUE") + .is_some_and(|v| v.eq_ignore_ascii_case("DATE")); + let is_period = prop + .param("VALUE") + .is_some_and(|v| v.eq_ignore_ascii_case("PERIOD")); + let tzid = prop.param("TZID"); + prop.value + .split(',') + .filter(|v| !v.trim().is_empty()) + .filter_map(|v| { + let v = if is_period || v.contains('/') { + v.split('/').next().unwrap_or(v) + } else { + v + }; + parse_one(v, tzid, is_date) + }) + .collect() +} + +#[cfg(test)] +pub(crate) fn parse_duration(value: &str) -> Option { + let (days, seconds) = parse_duration_parts(value)?; + Some(chrono::Duration::seconds( + days.checked_mul(86_400)?.checked_add(seconds)?, + )) +} + +pub(crate) fn parse_duration_parts(value: &str) -> Option<(i64, i64)> { + let value = value.trim(); + let (negative, rest) = match value.as_bytes().first()? { + b'-' => (true, &value[1..]), + b'+' => (false, &value[1..]), + _ => (false, value), + }; + let rest = rest.strip_prefix(['P', 'p'])?; + let mut days: i64 = 0; + let mut seconds: i64 = 0; + let mut number = String::new(); + let mut in_time = false; + let mut any = false; + for c in rest.chars() { + match c.to_ascii_uppercase() { + d if d.is_ascii_digit() => number.push(d), + 'T' => in_time = true, + unit => { + let n: i64 = number.parse().ok()?; + number.clear(); + match (unit, in_time) { + ('W', false) => days = days.checked_add(n.checked_mul(7)?)?, + ('D', false) => days = days.checked_add(n)?, + ('H', true) => seconds = seconds.checked_add(n.checked_mul(3_600)?)?, + ('M', true) => seconds = seconds.checked_add(n.checked_mul(60)?)?, + ('S', true) => seconds = seconds.checked_add(n)?, + _ => return None, + } + any = true; + } + } + } + let total = days.checked_mul(86_400)?.checked_add(seconds)?; + if !any || !number.is_empty() || total > 366 * 86_400 * 10 { + return None; + } + Some(if negative { + (-days, -seconds) + } else { + (days, seconds) + }) +} + +#[cfg(test)] +mod tests { + use super::*; + use chrono::Duration; + + #[test] + fn parses_all_value_forms() { + let d = NaiveDate::from_ymd_opt(2026, 3, 9).unwrap(); + assert_eq!(parse_one("20260309", None, true), Some(IcsTime::Date(d))); + assert_eq!(parse_one("20260309", None, false), Some(IcsTime::Date(d))); + let dt = d.and_hms_opt(9, 30, 0).unwrap(); + assert_eq!( + parse_one("20260309T093000Z", None, false), + Some(IcsTime::Utc(dt)) + ); + assert_eq!( + parse_one("20260309T093000", Some("Europe/Paris"), false), + Some(IcsTime::Zoned(dt, "Europe/Paris".into())) + ); + assert_eq!( + parse_one("20260309T0930", None, false), + Some(IcsTime::Floating(dt)) + ); + assert_eq!( + parse_one("2026-03-09T09:30:00Z", None, false), + Some(IcsTime::Utc(dt)) + ); + assert_eq!(parse_one("20261309T093000Z", None, false), None); + assert_eq!(parse_one("99999999T000000Z", None, false), None); + assert_eq!(parse_one("garbage", None, false), None); + } + + #[test] + fn parses_durations() { + assert_eq!(parse_duration("PT1H30M"), Some(Duration::minutes(90))); + assert_eq!(parse_duration("P1D"), Some(Duration::days(1))); + assert_eq!(parse_duration("P2W"), Some(Duration::weeks(2))); + assert_eq!(parse_duration("-PT15M"), Some(Duration::minutes(-15))); + assert_eq!(parse_duration("P1DT2H"), Some(Duration::hours(26))); + assert_eq!(parse_duration_parts("P1DT2H"), Some((1, 7_200))); + assert_eq!(parse_duration_parts("P1W"), Some((7, 0))); + assert_eq!(parse_duration_parts("PT24H"), Some((0, 86_400))); + assert_eq!(parse_duration_parts("-P1D"), Some((-1, 0))); + assert_eq!(parse_duration("PT"), None); + assert_eq!(parse_duration("P1H"), None); + assert_eq!(parse_duration("P99999999999999999999D"), None); + assert_eq!(parse_duration(""), None); + } +} diff --git a/apps/desktop-gpui/calendar/src/ics/vtimezone.rs b/apps/desktop-gpui/calendar/src/ics/vtimezone.rs new file mode 100644 index 00000000000..7561e788ec3 --- /dev/null +++ b/apps/desktop-gpui/calendar/src/ics/vtimezone.rs @@ -0,0 +1,241 @@ +use chrono::{DateTime, Datelike, Duration, FixedOffset, NaiveDate, NaiveDateTime, Utc}; + +use super::budget::Budget; +use super::lexer::Component; +use super::rule::build_rule; +use super::time::{IcsTime, parse_property}; +use crate::tz::{Zone, parse_offset, preferred_zones, resolve_tzid}; + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum ZoneResolution { + Named, + MatchedRules, + FixedStandardOffset, +} + +#[derive(Clone, Debug)] +struct Observance { + daylight: bool, + start: NaiveDateTime, + from: FixedOffset, + to: FixedOffset, + rule: Option, + rdates: Vec, +} + +fn observances(comp: &Component) -> Vec { + comp.children + .iter() + .filter(|c| c.name == "STANDARD" || c.name == "DAYLIGHT") + .filter_map(|c| { + let to = c.prop("TZOFFSETTO").and_then(|p| parse_offset(&p.value))?; + let from = c + .prop("TZOFFSETFROM") + .and_then(|p| parse_offset(&p.value)) + .unwrap_or(to); + let start = c + .prop("DTSTART") + .map(parse_property) + .and_then(|v| v.into_iter().next()) + .and_then(|t| match t { + IcsTime::Floating(n) | IcsTime::Utc(n) | IcsTime::Zoned(n, _) => Some(n), + IcsTime::Date(d) => d.and_hms_opt(0, 0, 0), + })?; + let rdates = c + .props_named("RDATE") + .flat_map(parse_property) + .filter_map(|t| match t { + IcsTime::Floating(n) | IcsTime::Utc(n) | IcsTime::Zoned(n, _) => Some(n), + IcsTime::Date(d) => d.and_hms_opt(0, 0, 0), + }) + .collect(); + Some(Observance { + daylight: c.name == "DAYLIGHT", + start, + from, + to, + rule: c.prop("RRULE").map(|p| p.value.clone()), + rdates, + }) + }) + .collect() +} + +struct Transition { + at: DateTime, + from: i32, + to: i32, +} + +const ZONE_MATCH_COST: u64 = 5_000; + +fn transitions_in(obs: &Observance, year: i32, budget: &mut Budget) -> Vec { + let mut walls: Vec = Vec::new(); + if let Some(rule) = &obs.rule { + let year_start = NaiveDate::from_ymd_opt(year, 1, 1).and_then(|d| d.and_hms_opt(0, 0, 0)); + if let (Ok(rule), Some(year_start)) = ( + build_rule(rule, obs.start, Zone::Fixed(obs.from)), + year_start, + ) { + walls.extend( + rule.walls(obs.start, year_start, budget) + .take_while(|w| w.year() <= year) + .filter(|w| w.year() == year) + .take(64), + ); + } + } else { + walls.extend( + obs.rdates + .iter() + .chain(std::iter::once(&obs.start)) + .filter(|w| w.year() == year) + .copied(), + ); + } + walls + .into_iter() + .map(|wall| Transition { + at: Zone::Fixed(obs.from).to_utc(wall), + from: obs.from.local_minus_utc(), + to: obs.to.local_minus_utc(), + }) + .collect() +} + +fn latest(obs: &[Observance], daylight: bool) -> Option<&Observance> { + obs.iter() + .filter(|o| o.daylight == daylight) + .max_by_key(|o| o.start) +} + +pub(crate) fn resolve_vtimezone( + comp: &Component, + budget: &mut Budget, +) -> Option<(String, Zone, ZoneResolution)> { + let tzid = comp.prop("TZID")?.value.trim().to_string(); + if tzid.is_empty() { + return None; + } + if let Some(zone @ (Zone::Named(_) | Zone::Utc)) = resolve_tzid(&tzid) { + return Some((tzid, zone, ZoneResolution::Named)); + } + let obs = observances(comp); + let standard = latest(&obs, false).or_else(|| latest(&obs, true))?; + let std_offset = standard.to; + let daylight = latest(&obs, true).filter(|d| !std::ptr::eq(*d, standard)); + let base_year = obs + .iter() + .map(|o| o.start.year()) + .max() + .unwrap_or(2025) + .max(2025); + let years = [base_year, base_year + 1]; + let mut transitions = Vec::new(); + for year in years { + transitions.extend(transitions_in(standard, year, budget)); + if let Some(d) = daylight { + transitions.extend(transitions_in(d, year, budget)); + } + } + transitions.retain(|t| t.from != t.to); + let dst_offset = daylight.map(|d| d.to.local_minus_utc()); + let samples: Vec> = years + .iter() + .flat_map(|y| [1, 4, 7, 10].map(|m| (*y, m))) + .filter_map(|(y, m)| NaiveDate::from_ymd_opt(y, m, 15)?.and_hms_opt(12, 0, 0)) + .map(|n| n.and_utc()) + .collect(); + let std_secs = std_offset.local_minus_utc(); + let matches = |zone: Zone| { + let ok_transitions = transitions.iter().all(|t| { + zone.offset_seconds_at(t.at - Duration::minutes(1)) == t.from + && zone.offset_seconds_at(t.at + Duration::minutes(1)) == t.to + }); + let ok_samples = samples.iter().all(|s| { + let off = zone.offset_seconds_at(*s); + if transitions.is_empty() { + off == std_secs + } else { + off == std_secs || Some(off) == dst_offset + } + }); + ok_transitions && ok_samples + }; + if budget.can_afford(ZONE_MATCH_COST) { + budget.charge(ZONE_MATCH_COST); + if let Some(tz) = preferred_zones().find(|tz| matches(Zone::Named(*tz))) { + return Some((tzid, Zone::Named(tz), ZoneResolution::MatchedRules)); + } + } + Some(( + tzid, + Zone::Fixed(std_offset), + ZoneResolution::FixedStandardOffset, + )) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::ics::budget::FEED_BUDGET; + use crate::ics::lexer::lex; + use chrono_tz::Tz; + + fn resolve(text: &str) -> (Zone, ZoneResolution) { + let lexed = lex(text); + let mut budget = Budget::new(FEED_BUDGET); + let (_, zone, how) = resolve_vtimezone(&lexed.root.children[0], &mut budget).unwrap(); + (zone, how) + } + + #[test] + fn matches_outlook_custom_eastern() { + let (zone, how) = resolve( + "BEGIN:VTIMEZONE\nTZID:Customized Time Zone\nBEGIN:STANDARD\nDTSTART:16010101T020000\nTZOFFSETFROM:-0400\nTZOFFSETTO:-0500\nRRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=1SU;BYMONTH=11\nEND:STANDARD\nBEGIN:DAYLIGHT\nDTSTART:16010101T020000\nTZOFFSETFROM:-0500\nTZOFFSETTO:-0400\nRRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=2SU;BYMONTH=3\nEND:DAYLIGHT\nEND:VTIMEZONE\n", + ); + assert_eq!(how, ZoneResolution::MatchedRules); + assert_eq!(zone, Zone::Named(Tz::America__New_York)); + } + + #[test] + fn matches_southern_hemisphere_rules() { + let (zone, how) = resolve( + "BEGIN:VTIMEZONE\nTZID:(UTC+10:00) Canberra\\, Melbourne\\, Sydney\nBEGIN:STANDARD\nDTSTART:16010101T030000\nTZOFFSETFROM:+1100\nTZOFFSETTO:+1000\nRRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=4\nEND:STANDARD\nBEGIN:DAYLIGHT\nDTSTART:16010101T020000\nTZOFFSETFROM:+1000\nTZOFFSETTO:+1100\nRRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=10\nEND:DAYLIGHT\nEND:VTIMEZONE\n", + ); + assert_eq!(how, ZoneResolution::MatchedRules); + assert_eq!(zone, Zone::Named(Tz::Australia__Sydney)); + } + + #[test] + fn falls_back_to_fixed_standard_offset() { + let (zone, how) = resolve( + "BEGIN:VTIMEZONE\nTZID:Mars Colony\nBEGIN:STANDARD\nDTSTART:19700101T000000\nTZOFFSETFROM:+0317\nTZOFFSETTO:+0317\nEND:STANDARD\nEND:VTIMEZONE\n", + ); + assert_eq!(how, ZoneResolution::FixedStandardOffset); + assert_eq!( + zone, + FixedOffset::east_opt(3 * 3600 + 17 * 60) + .map(Zone::Fixed) + .unwrap() + ); + } + + #[test] + fn named_tzid_wins_over_rules() { + let (zone, how) = resolve( + "BEGIN:VTIMEZONE\nTZID:Europe/Berlin\nBEGIN:STANDARD\nDTSTART:19701025T030000\nTZOFFSETFROM:+0200\nTZOFFSETTO:+0100\nEND:STANDARD\nEND:VTIMEZONE\n", + ); + assert_eq!(how, ZoneResolution::Named); + assert_eq!(zone, Zone::Named(Tz::Europe__Berlin)); + } + + #[test] + fn matches_zone_without_daylight_saving() { + let (zone, how) = resolve( + "BEGIN:VTIMEZONE\nTZID:India\nBEGIN:STANDARD\nDTSTART:16010101T000000\nTZOFFSETFROM:+0530\nTZOFFSETTO:+0530\nEND:STANDARD\nEND:VTIMEZONE\n", + ); + assert_eq!(how, ZoneResolution::MatchedRules); + assert_eq!(zone, Zone::Named(Tz::Asia__Kolkata)); + } +} diff --git a/apps/desktop-gpui/calendar/src/lib.rs b/apps/desktop-gpui/calendar/src/lib.rs new file mode 100644 index 00000000000..a2be1214bb5 --- /dev/null +++ b/apps/desktop-gpui/calendar/src/lib.rs @@ -0,0 +1,22 @@ +pub mod agenda; +mod error; +pub mod google; +pub mod graph; +mod http; +pub mod ics; +pub mod link; +mod model; +pub mod oauth; +pub mod system; +mod text; +pub mod tz; + +pub use agenda::{Agenda, Filter, Reminder, due_reminders, next_wake}; +pub use chrono_tz::Tz; +pub use error::Error; +pub use http::{HttpMethod, HttpRequestSpec}; +pub use link::{ + ConferenceEntry, ConferenceKind, MeetingLink, Provider, classify_url, event_meeting_link, + find_meeting_link, +}; +pub use model::{CalendarEvent, CalendarInfo, EventStatus, Page, Person, Response, SourceKind}; diff --git a/apps/desktop-gpui/calendar/src/link.rs b/apps/desktop-gpui/calendar/src/link.rs new file mode 100644 index 00000000000..99b444ecccc --- /dev/null +++ b/apps/desktop-gpui/calendar/src/link.rs @@ -0,0 +1,1233 @@ +use serde::{Deserialize, Serialize}; +use url::Url; + +use crate::text::decode_entities; + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub enum Provider { + #[serde(rename = "zoom")] + Zoom, + #[serde(rename = "google-meet")] + GoogleMeet, + #[serde(rename = "teams")] + Teams, + #[serde(rename = "webex")] + Webex, + #[serde(rename = "slack")] + Slack, + #[serde(rename = "whereby")] + Whereby, + #[serde(rename = "around")] + Around, + #[serde(rename = "goto-meeting")] + GoToMeeting, + #[serde(rename = "goto-webinar")] + GoToWebinar, + #[serde(rename = "bluejeans")] + BlueJeans, + #[serde(rename = "jitsi")] + Jitsi, + #[serde(rename = "discord")] + Discord, + #[serde(rename = "skype")] + Skype, + #[serde(rename = "chime")] + Chime, + #[serde(rename = "ringcentral")] + RingCentral, + #[serde(rename = "dialpad")] + Dialpad, + #[serde(rename = "zoho-meeting")] + ZohoMeeting, + #[serde(rename = "lark")] + Lark, + #[serde(rename = "tuple")] + Tuple, + #[serde(rename = "gather")] + Gather, + #[serde(rename = "riverside")] + Riverside, + #[serde(rename = "streamyard")] + StreamYard, + #[serde(rename = "whatsapp")] + WhatsApp, + #[serde(rename = "facetime")] + FaceTime, + #[serde(rename = "cal-video")] + CalVideo, + #[serde(rename = "daily")] + Daily, + #[serde(rename = "livestorm")] + Livestorm, + #[serde(rename = "pop")] + Pop, + #[serde(rename = "tandem")] + Tandem, + #[serde(rename = "vowel")] + Vowel, + #[serde(rename = "demio")] + Demio, + #[serde(rename = "butter")] + Butter, + #[serde(rename = "other", other)] + Other, +} + +impl Provider { + pub const ALL: [Provider; 33] = [ + Provider::Zoom, + Provider::GoogleMeet, + Provider::Teams, + Provider::Webex, + Provider::Slack, + Provider::Whereby, + Provider::Around, + Provider::GoToMeeting, + Provider::GoToWebinar, + Provider::BlueJeans, + Provider::Jitsi, + Provider::Discord, + Provider::Skype, + Provider::Chime, + Provider::RingCentral, + Provider::Dialpad, + Provider::ZohoMeeting, + Provider::Lark, + Provider::Tuple, + Provider::Gather, + Provider::Riverside, + Provider::StreamYard, + Provider::WhatsApp, + Provider::FaceTime, + Provider::CalVideo, + Provider::Daily, + Provider::Livestorm, + Provider::Pop, + Provider::Tandem, + Provider::Vowel, + Provider::Demio, + Provider::Butter, + Provider::Other, + ]; + + pub fn display_name(self) -> &'static str { + match self { + Provider::Zoom => "Zoom", + Provider::GoogleMeet => "Google Meet", + Provider::Teams => "Microsoft Teams", + Provider::Webex => "Webex", + Provider::Slack => "Slack Huddle", + Provider::Whereby => "Whereby", + Provider::Around => "Around", + Provider::GoToMeeting => "GoTo Meeting", + Provider::GoToWebinar => "GoTo Webinar", + Provider::BlueJeans => "BlueJeans", + Provider::Jitsi => "Jitsi Meet", + Provider::Discord => "Discord", + Provider::Skype => "Skype", + Provider::Chime => "Amazon Chime", + Provider::RingCentral => "RingCentral", + Provider::Dialpad => "Dialpad Meetings", + Provider::ZohoMeeting => "Zoho Meeting", + Provider::Lark => "Lark", + Provider::Tuple => "Tuple", + Provider::Gather => "Gather", + Provider::Riverside => "Riverside", + Provider::StreamYard => "StreamYard", + Provider::WhatsApp => "WhatsApp", + Provider::FaceTime => "FaceTime", + Provider::CalVideo => "Cal Video", + Provider::Daily => "Daily", + Provider::Livestorm => "Livestorm", + Provider::Pop => "Pop", + Provider::Tandem => "Tandem", + Provider::Vowel => "Vowel", + Provider::Demio => "Demio", + Provider::Butter => "Butter", + Provider::Other => "Video call", + } + } + + pub fn slug(self) -> &'static str { + match self { + Provider::Zoom => "zoom", + Provider::GoogleMeet => "google-meet", + Provider::Teams => "teams", + Provider::Webex => "webex", + Provider::Slack => "slack", + Provider::Whereby => "whereby", + Provider::Around => "around", + Provider::GoToMeeting => "goto-meeting", + Provider::GoToWebinar => "goto-webinar", + Provider::BlueJeans => "bluejeans", + Provider::Jitsi => "jitsi", + Provider::Discord => "discord", + Provider::Skype => "skype", + Provider::Chime => "chime", + Provider::RingCentral => "ringcentral", + Provider::Dialpad => "dialpad", + Provider::ZohoMeeting => "zoho-meeting", + Provider::Lark => "lark", + Provider::Tuple => "tuple", + Provider::Gather => "gather", + Provider::Riverside => "riverside", + Provider::StreamYard => "streamyard", + Provider::WhatsApp => "whatsapp", + Provider::FaceTime => "facetime", + Provider::CalVideo => "cal-video", + Provider::Daily => "daily", + Provider::Livestorm => "livestorm", + Provider::Pop => "pop", + Provider::Tandem => "tandem", + Provider::Vowel => "vowel", + Provider::Demio => "demio", + Provider::Butter => "butter", + Provider::Other => "other", + } + } + + pub fn from_slug(slug: &str) -> Option { + Provider::ALL.into_iter().find(|p| p.slug() == slug) + } +} + +#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct MeetingLink { + pub provider: Provider, + pub url: String, + #[serde(default)] + pub meeting_id: Option, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum ConferenceKind { + Video, + Phone, + Sip, + More, + Other, +} + +impl ConferenceKind { + pub fn from_entry_point_type(value: &str) -> ConferenceKind { + match value.trim().to_ascii_lowercase().as_str() { + "video" => ConferenceKind::Video, + "phone" => ConferenceKind::Phone, + "sip" => ConferenceKind::Sip, + "more" => ConferenceKind::More, + _ => ConferenceKind::Other, + } + } +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct ConferenceEntry { + pub kind: ConferenceKind, + pub uri: String, + pub meeting_code: Option, +} + +impl ConferenceEntry { + pub fn video(uri: impl Into) -> Self { + Self { + kind: ConferenceKind::Video, + uri: uri.into(), + meeting_code: None, + } + } +} + +pub fn event_meeting_link( + conference: &[ConferenceEntry], + location: Option<&str>, + description: Option<&str>, + url: Option<&str>, +) -> Option { + conference + .iter() + .filter(|e| e.kind == ConferenceKind::Video) + .find_map(structured_link) + .or_else(|| location.and_then(find_meeting_link)) + .or_else(|| description.and_then(find_meeting_link)) + .or_else(|| url.and_then(classify_url)) +} + +fn structured_link(entry: &ConferenceEntry) -> Option { + let code = entry + .meeting_code + .as_deref() + .map(str::trim) + .filter(|c| !c.is_empty()) + .map(str::to_string); + if let Some(mut link) = classify_url(&entry.uri) { + if link.meeting_id.is_none() { + link.meeting_id = code; + } + return Some(link); + } + let parsed = Url::parse(entry.uri.trim()).ok()?; + if !matches!(parsed.scheme(), "http" | "https") || parsed.host_str().is_none() { + return None; + } + Some(MeetingLink { + provider: Provider::Other, + url: parsed.to_string(), + meeting_id: code, + }) +} + +pub fn find_meeting_link(text: &str) -> Option { + let decoded = decode_entities(text); + candidates(&decoded).find_map(|c| classify_url(&c)) +} + +pub fn find_all_meeting_links(text: &str) -> Vec { + let decoded = decode_entities(text); + let mut out: Vec = Vec::new(); + for link in candidates(&decoded).filter_map(|c| classify_url(&c)) { + if !out.iter().any(|l| l.url == link.url) { + out.push(link); + } + } + out +} + +pub fn classify_url(url: &str) -> Option { + classify_depth(url, 0) +} + +const SCHEMES: [&str; 5] = ["https://", "http://", "zoommtg://", "zoomus://", "msteams:"]; + +fn is_delimiter(c: char) -> bool { + c.is_whitespace() + || matches!( + c, + '<' | '>' | '"' | '\'' | '`' | '|' | '{' | '}' | '\\' | '^' + ) +} + +fn candidates(text: &str) -> impl Iterator + '_ { + text.split(is_delimiter) + .filter(|t| !t.is_empty()) + .filter_map(token_candidate) +} + +fn token_candidate(token: &str) -> Option { + let lower = token.to_ascii_lowercase(); + if let Some(idx) = SCHEMES.iter().filter_map(|s| lower.find(s)).min() { + return Some(token[idx..].to_string()); + } + let start = token + .find(|c: char| c.is_ascii_alphanumeric()) + .unwrap_or(token.len()); + let bare = &token[start..]; + let slash = bare.find('/')?; + let host = &bare[..slash]; + let host_ok = host.contains('.') + && host.len() >= 4 + && host + .chars() + .all(|c| c.is_ascii_alphanumeric() || c == '.' || c == '-') + && host + .rsplit('.') + .next() + .is_some_and(|tld| tld.len() >= 2 && tld.chars().all(|c| c.is_ascii_alphabetic())); + if !host_ok { + return None; + } + Some(format!("https://{bare}")) +} + +fn strip_trailing(s: &str) -> &str { + let mut out = s.trim(); + loop { + let Some(last) = out.chars().last() else { + return out; + }; + let strip = match last { + '.' | ',' | ';' | ':' | '!' | '?' | '*' | ']' | '}' | '\'' | '"' => true, + ')' => out.matches(')').count() > out.matches('(').count(), + _ => false, + }; + if !strip { + return out; + } + out = &out[..out.len() - last.len_utf8()]; + } +} + +fn classify_depth(raw: &str, depth: u8) -> Option { + if depth > 4 || raw.len() > 8192 { + return None; + } + let trimmed = strip_trailing(raw.trim().trim_start_matches(['<', '(', '['])); + if trimmed.is_empty() { + return None; + } + let with_scheme = if trimmed.contains(':') { + trimmed.to_string() + } else { + format!("https://{trimmed}") + }; + let mut url = Url::parse(&with_scheme).ok()?; + match url.scheme() { + "http" | "https" => {} + "zoommtg" | "zoomus" => return zoom_app_link(&url), + "msteams" => { + let rest = with_scheme.split_once(':')?.1.trim_start_matches('/'); + return classify_depth(&format!("https://teams.microsoft.com/{rest}"), depth + 1); + } + _ => return None, + } + if let Some(inner) = unwrap_redirect(&url, &with_scheme) { + return classify_depth(&inner, depth + 1); + } + let host = url.host_str()?.trim_end_matches('.').to_ascii_lowercase(); + let segs: Vec = url + .path_segments() + .map(|s| s.filter(|p| !p.is_empty()).map(percent_decode).collect()) + .unwrap_or_default(); + let ctx = Ctx { + url: &url, + host: &host, + segs: &segs, + }; + let (provider, hit) = RULES + .iter() + .find_map(|(provider, rule)| rule(&ctx).map(|hit| (*provider, hit)))?; + if url.scheme() == "http" { + url.set_scheme("https").ok()?; + } + if !hit.keep_fragment { + url.set_fragment(None); + } + strip_tracking(&mut url); + Some(MeetingLink { + provider, + url: url.to_string(), + meeting_id: hit.id.filter(|id| !id.is_empty()), + }) +} + +fn strip_tracking(url: &mut Url) { + if url.query().is_none() { + return; + } + let kept: Vec<(String, String)> = url + .query_pairs() + .filter(|(k, _)| !k.to_ascii_lowercase().starts_with("utm_")) + .map(|(k, v)| (k.into_owned(), v.into_owned())) + .collect(); + if kept.is_empty() { + url.set_query(None); + return; + } + let original = url.query_pairs().count(); + if kept.len() != original { + url.query_pairs_mut().clear().extend_pairs(kept); + } +} + +fn percent_decode(s: &str) -> String { + url::form_urlencoded::parse(format!("x={}", s.replace('+', "%2B")).as_bytes()) + .next() + .map(|(_, v)| v.into_owned()) + .unwrap_or_else(|| s.to_string()) +} + +fn query_param(url: &Url, name: &str) -> Option { + url.query_pairs() + .find(|(k, _)| k.eq_ignore_ascii_case(name)) + .map(|(_, v)| v.into_owned()) + .filter(|v| !v.is_empty()) +} + +fn unwrap_redirect(url: &Url, raw: &str) -> Option { + let host = url.host_str()?.to_ascii_lowercase(); + let path = url.path(); + if host.ends_with("safelinks.protection.outlook.com") || path.ends_with("safelinks.html") { + return query_param(url, "url"); + } + if (host == "google.com" || host.starts_with("www.google.") || host == "google.co.uk") + && path == "/url" + { + return query_param(url, "q").or_else(|| query_param(url, "url")); + } + if host == "slack-redir.net" && path == "/link" { + return query_param(url, "url"); + } + if host == "urldefense.com" && path.starts_with("/v3/__") { + let start = raw.find("/v3/__")? + "/v3/__".len(); + let rest = &raw[start..]; + let end = rest.find("__;").unwrap_or(rest.len()); + return Some(rest[..end].to_string()); + } + if host == "urldefense.proofpoint.com" && path.starts_with("/v2/url") { + return query_param(url, "u").map(|u| proofpoint_v2_decode(&u)); + } + None +} + +fn proofpoint_v2_decode(value: &str) -> String { + let replaced = value.replace('_', "/"); + let bytes = replaced.as_bytes(); + let mut out = Vec::with_capacity(bytes.len()); + let mut i = 0; + while i < bytes.len() { + if bytes[i] == b'-' + && i + 2 < bytes.len() + && let Some(byte) = std::str::from_utf8(&bytes[i + 1..i + 3]) + .ok() + .and_then(|h| u8::from_str_radix(h, 16).ok()) + { + out.push(byte); + i += 3; + continue; + } + out.push(bytes[i]); + i += 1; + } + String::from_utf8_lossy(&out).into_owned() +} + +fn zoom_app_link(url: &Url) -> Option { + let id = query_param(url, "confno").filter(|c| is_digits(c, 9, 12))?; + let mut out = format!("https://zoom.us/j/{id}"); + if let Some(pwd) = query_param(url, "pwd") { + out.push_str("?pwd="); + out.push_str(&url::form_urlencoded::byte_serialize(pwd.as_bytes()).collect::()); + } + Some(MeetingLink { + provider: Provider::Zoom, + url: out, + meeting_id: Some(id), + }) +} + +struct Ctx<'a> { + url: &'a Url, + host: &'a str, + segs: &'a [String], +} + +impl Ctx<'_> { + fn seg(&self, i: usize) -> Option<&str> { + self.segs.get(i).map(String::as_str) + } + + fn seg_lower(&self, i: usize) -> Option { + self.seg(i).map(str::to_ascii_lowercase) + } + + fn host_is(&self, domain: &str) -> bool { + self.host == domain + } + + fn host_under(&self, domain: &str) -> bool { + self.host == domain + || (self.host.len() > domain.len() + && self.host.ends_with(domain) + && self.host.as_bytes()[self.host.len() - domain.len() - 1] == b'.') + } + + fn is_root_or_www(&self, domain: &str) -> bool { + self.host == domain || self.host.strip_prefix("www.") == Some(domain) + } + + fn param(&self, name: &str) -> Option { + query_param(self.url, name) + } +} + +struct Hit { + id: Option, + keep_fragment: bool, +} + +fn hit(id: Option) -> Option { + Some(Hit { + id, + keep_fragment: false, + }) +} + +fn hit_with_fragment(id: Option) -> Option { + Some(Hit { + id, + keep_fragment: true, + }) +} + +fn is_digits(s: &str, min: usize, max: usize) -> bool { + (min..=max).contains(&s.len()) && s.bytes().all(|b| b.is_ascii_digit()) +} + +fn digits_of(s: &str) -> String { + s.chars().filter(char::is_ascii_digit).collect() +} + +fn is_slug(s: &str) -> bool { + !s.is_empty() + && s.len() <= 128 + && s.chars() + .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_') +} + +type Rule = fn(&Ctx) -> Option; + +const RULES: [(Provider, Rule); 32] = [ + (Provider::Zoom, zoom), + (Provider::GoogleMeet, google_meet), + (Provider::Teams, teams), + (Provider::Webex, webex), + (Provider::Slack, slack), + (Provider::Whereby, whereby), + (Provider::Around, around), + (Provider::GoToMeeting, goto_meeting), + (Provider::GoToWebinar, goto_webinar), + (Provider::BlueJeans, bluejeans), + (Provider::Jitsi, jitsi), + (Provider::Discord, discord), + (Provider::Skype, skype), + (Provider::Chime, chime), + (Provider::RingCentral, ringcentral), + (Provider::Dialpad, dialpad), + (Provider::ZohoMeeting, zoho), + (Provider::Lark, lark), + (Provider::Tuple, tuple), + (Provider::Gather, gather), + (Provider::Riverside, riverside), + (Provider::StreamYard, streamyard), + (Provider::WhatsApp, whatsapp), + (Provider::FaceTime, facetime), + (Provider::CalVideo, cal_video), + (Provider::Daily, daily), + (Provider::Livestorm, livestorm), + (Provider::Pop, pop), + (Provider::Tandem, tandem), + (Provider::Vowel, vowel), + (Provider::Demio, demio), + (Provider::Butter, butter), +]; + +fn zoom(c: &Ctx) -> Option { + if !(c.host_under("zoom.us") || c.host_under("zoom.com") || c.host_under("zoomgov.com")) { + return None; + } + let numeric = |s: Option<&str>| s.filter(|s| is_digits(s, 9, 12)).map(str::to_string); + match c.seg_lower(0)?.as_str() { + "j" | "w" | "s" => numeric(c.seg(1)).and_then(|id| hit(Some(id))), + "my" => c + .seg(1) + .filter(|s| is_slug(&s.replace('.', ""))) + .and_then(|_| hit(None)), + "wc" => { + let id = match (c.seg(1), c.seg(2)) { + (Some("join"), id) => numeric(id), + (id, Some("join" | "start")) => numeric(id), + _ => None, + }?; + hit(Some(id)) + } + "meeting" | "webinar" if c.seg(1) == Some("register") && c.seg(2).is_some() => hit(None), + "join" => { + let id = c.param("confno").filter(|s| is_digits(s, 9, 12))?; + hit(Some(id)) + } + _ => None, + } +} + +fn is_meet_code(s: &str) -> bool { + let parts: Vec<&str> = s.split('-').collect(); + parts.len() == 3 + && parts[0].len() == 3 + && parts[1].len() == 4 + && parts[2].len() == 3 + && parts + .iter() + .all(|p| p.bytes().all(|b| b.is_ascii_alphabetic())) +} + +fn google_meet(c: &Ctx) -> Option { + if c.host_is("meet.google.com") { + let first = c.seg_lower(0)?; + if is_meet_code(&first) { + return hit(Some(first)); + } + if first == "lookup" { + return c.seg(1).filter(|s| is_slug(s)).and_then(|_| hit(None)); + } + return None; + } + if (c.host_is("hangouts.google.com") || c.host_is("plus.google.com")) + && c.seg(0) == Some("hangouts") + && c.seg(1) == Some("_") + && c.segs.len() >= 3 + { + return hit(None); + } + if c.host_is("hangouts.google.com") && c.seg(0) == Some("call") && c.seg(1).is_some() { + return hit(c.seg(1).map(str::to_string)); + } + None +} + +const TEAMS_HOSTS: [&str; 6] = [ + "teams.microsoft.com", + "teams.live.com", + "teams.cloud.microsoft", + "teams.microsoft.us", + "gov.teams.microsoft.us", + "dod.teams.microsoft.us", +]; + +fn teams_path(segs: &[&str]) -> Option> { + let lower: Vec = segs.iter().map(|s| s.to_ascii_lowercase()).collect(); + match lower.first().map(String::as_str) { + Some("l") if lower.get(1).map(String::as_str) == Some("meetup-join") => { + let thread = segs.get(2).filter(|s| !s.is_empty())?; + Some(Some(percent_decode(thread))) + } + Some("meet") => { + let id = segs.get(1).filter(|s| !s.is_empty())?; + Some(Some(digits_of(id)).filter(|d| !d.is_empty())) + } + _ => None, + } +} + +fn teams(c: &Ctx) -> Option { + if !TEAMS_HOSTS.iter().any(|h| c.host_is(h)) { + return None; + } + let segs: Vec<&str> = c.segs.iter().map(String::as_str).collect(); + if let Some(id) = teams_path(&segs) { + return hit(id); + } + if let Some(fragment) = c.url.fragment() { + let fragment = percent_decode(fragment); + let frag_segs: Vec<&str> = fragment + .split(['/', '?']) + .filter(|s| !s.is_empty()) + .collect(); + if let Some(id) = teams_path(&frag_segs) { + return hit_with_fragment(id); + } + } + if c.url.path().contains("launcher") + && let Some(target) = c.param("url") + { + let target_segs: Vec<&str> = target + .split(['/', '#', '?']) + .filter(|s| !s.is_empty() && *s != "_") + .collect(); + if let Some(id) = teams_path(&target_segs) { + return hit(id); + } + } + None +} + +fn webex(c: &Ctx) -> Option { + if !(c.host_under("webex.com") || c.host_under("webexgov.us")) { + return None; + } + let lower: Vec = c.segs.iter().map(|s| s.to_ascii_lowercase()).collect(); + if lower.last().is_some_and(|l| l == "j.php" || l == "e.php") { + let id = c.param("MTID")?; + return hit(Some(id)); + } + match lower.first().map(String::as_str) { + Some("meet" | "join") => { + c.seg(1).filter(|s| is_slug(s) || s.contains('.'))?; + hit(None) + } + Some("weblink") if lower.get(1).map(String::as_str) == Some("register") => { + c.seg(2)?; + hit(None) + } + _ => { + let download = lower.iter().position(|s| s == "download")?; + if download == 0 || lower[download - 1] != "meeting" { + return None; + } + let id = c.seg(download + 1)?; + hit(Some(id.to_string())) + } + } +} + +fn slack(c: &Ctx) -> Option { + if !c.host_under("slack.com") || c.seg_lower(0)? != "huddle" { + return None; + } + c.seg(1)?; + let channel = c.seg(2)?; + hit(Some(channel.to_string())) +} + +const WHEREBY_RESERVED: [&str; 38] = [ + "information", + "pricing", + "user", + "login", + "signup", + "sign-up", + "org", + "blog", + "terms", + "privacy", + "cookies", + "developers", + "support", + "about", + "careers", + "contact", + "embed", + "api", + "help", + "download", + "product", + "products", + "features", + "integrations", + "security", + "legal", + "resources", + "customers", + "solutions", + "enterprise", + "partners", + "press", + "jobs", + "status", + "app", + "dashboard", + "assets", + "static", +]; + +fn whereby(c: &Ctx) -> Option { + let room = c.seg_lower(0)?; + if !is_slug(&room) || WHEREBY_RESERVED.contains(&room.as_str()) { + return None; + } + if c.is_root_or_www("whereby.com") || c.is_root_or_www("appear.in") { + return (c.segs.len() == 1).then_some(()).and_then(|_| hit(None)); + } + if c.host_under("whereby.com") { + return hit(None); + } + None +} + +fn around(c: &Ctx) -> Option { + if !c.host_under("around.co") || c.seg_lower(0)? != "r" { + return None; + } + let room = c.seg(1).filter(|s| is_slug(s))?; + hit(Some(room.to_string())) +} + +fn goto_meeting(c: &Ctx) -> Option { + if c.host_under("gotomeeting.com") { + let first = c.seg_lower(0)?; + if first == "join" || first == "install" { + let id = digits_of(c.seg(1)?); + if (9..=12).contains(&id.len()) { + return hit(Some(id)); + } + } + return None; + } + if c.is_root_or_www("gotomeet.me") { + c.seg(0).filter(|s| is_slug(s))?; + if c.segs.len() != 1 { + return None; + } + return hit(None); + } + if c.host_is("meet.goto.com") { + let first = c.seg(0).filter(|s| is_slug(s))?; + if matches!(first, "login" | "settings" | "signup" | "install") { + return None; + } + let id = digits_of(first); + let id = (id.len() >= 9 && id.len() == first.len()).then_some(id); + return hit(id); + } + if c.host_is("app.goto.com") && c.seg(0) == Some("meeting") { + let id = c.seg(1).filter(|s| is_digits(s, 9, 12))?; + return hit(Some(id.to_string())); + } + None +} + +fn goto_webinar(c: &Ctx) -> Option { + if !c.host_under("gotowebinar.com") || c.is_root_or_www("gotowebinar.com") { + return None; + } + match c.seg_lower(0)?.as_str() { + "register" | "join" => { + let key = c.seg(1).filter(|s| !s.is_empty())?; + hit(Some(key.to_string())) + } + _ => None, + } +} + +fn bluejeans(c: &Ctx) -> Option { + if !c.host_under("bluejeans.com") { + return None; + } + let first = c.seg(0)?; + if is_digits(first, 6, 14) { + return hit(Some(first.to_string())); + } + None +} + +const JITSI_RESERVED: [&str; 8] = [ + "static", + "libs", + "images", + "sounds", + "fonts", + "lang", + "css", + "favicon.ico", +]; + +fn jitsi_room(s: &str) -> bool { + !s.is_empty() && !s.contains('.') && !JITSI_RESERVED.contains(&s.to_ascii_lowercase().as_str()) +} + +fn jitsi(c: &Ctx) -> Option { + if c.host_is("meet.jit.si") { + let room = c.seg(0).filter(|s| jitsi_room(s))?; + return (c.segs.len() == 1) + .then_some(()) + .and_then(|_| hit(Some(room.to_string()))); + } + if c.host_is("8x8.vc") { + if !(1..=2).contains(&c.segs.len()) || !c.segs.iter().all(|s| jitsi_room(s)) { + return None; + } + return hit(c.segs.last().cloned()); + } + None +} + +fn discord(c: &Ctx) -> Option { + if !(c.host_under("discord.com") || c.host_under("discordapp.com")) { + return None; + } + let kind = c.seg_lower(0)?; + if kind != "channels" && kind != "events" { + return None; + } + let guild = c.seg(1)?; + if guild != "@me" && !is_digits(guild, 5, 25) { + return None; + } + let channel = c.seg(2).filter(|s| is_digits(s, 5, 25))?; + hit(Some(channel.to_string())) +} + +fn skype(c: &Ctx) -> Option { + if c.host_is("join.skype.com") { + let code = c.seg(0).filter(|s| is_slug(s))?; + if code.eq_ignore_ascii_case("invite") { + return None; + } + return hit(Some(code.to_string())); + } + if c.host_is("meet.lync.com") && c.segs.len() >= 2 { + return hit(c.segs.last().cloned()); + } + None +} + +fn chime(c: &Ctx) -> Option { + if c.host_is("chime.aws") { + let id = c.seg(0).filter(|s| is_digits(s, 10, 13))?; + return hit(Some(id.to_string())); + } + if c.host_is("app.chime.aws") && c.seg(0) == Some("meetings") { + let id = c + .param("pin") + .or_else(|| c.seg(1).map(str::to_string)) + .filter(|s| is_digits(s, 10, 13))?; + return hit(Some(id)); + } + None +} + +fn ringcentral(c: &Ctx) -> Option { + if !c.host_under("ringcentral.com") || c.is_root_or_www("ringcentral.com") { + return None; + } + match c.seg_lower(0)?.as_str() { + "j" | "join" => { + let id = c.seg(1).filter(|s| is_slug(s))?; + let digits = digits_of(id); + hit(Some(if digits.len() == id.len() { + digits + } else { + id.to_string() + })) + } + "my" => c.seg(1).filter(|s| is_slug(s)).and_then(|_| hit(None)), + _ => None, + } +} + +fn dialpad(c: &Ctx) -> Option { + if c.host_is("meetings.dialpad.com") { + let room = c.seg(0).filter(|s| is_slug(s))?; + if matches!(room, "login" | "signup" | "settings" | "app") || c.segs.len() != 1 { + return None; + } + return hit(None); + } + if c.is_root_or_www("dialpad.com") && c.seg(0) == Some("meetings") { + c.seg(1).filter(|s| is_slug(s))?; + return hit(None); + } + None +} + +/// Zoho's regional data centres. Only these count, so a lookalike such as +/// `meeting.zoho.example.com` is never taken for a Zoho meeting. +const ZOHO_DOMAINS: &[&str] = &[ + "zoho.com", + "zoho.eu", + "zoho.in", + "zoho.com.au", + "zoho.com.cn", + "zoho.jp", + "zoho.uk", + "zoho.sa", + "zohocloud.ca", +]; + +fn zoho_host(host: &str, prefix: &str) -> bool { + host.strip_prefix(prefix) + .is_some_and(|rest| ZOHO_DOMAINS.contains(&rest)) +} + +fn zoho(c: &Ctx) -> Option { + if zoho_host(c.host, "meeting.") { + let path = c.url.path().to_ascii_lowercase(); + if path.contains("join") { + let key = c.param("key")?; + return hit(Some(key)); + } + if path.contains("/register") && c.segs.len() >= 2 { + return hit(None); + } + return None; + } + if zoho_host(c.host, "meet.") { + let code = c.seg(0).filter(|s| is_slug(s))?; + return (c.segs.len() == 1) + .then_some(()) + .and_then(|_| hit(Some(code.to_string()))); + } + None +} + +fn lark(c: &Ctx) -> Option { + let known = ["larksuite.com", "feishu.cn", "larkoffice.com"] + .iter() + .any(|d| c.host_under(d) && c.host.starts_with("vc.")); + if !known || c.seg_lower(0)? != "j" { + return None; + } + let id = c.seg(1).filter(|s| is_digits(s, 6, 12))?; + hit(Some(id.to_string())) +} + +fn tuple(c: &Ctx) -> Option { + if !c.is_root_or_www("tuple.app") || c.seg_lower(0)? != "c" { + return None; + } + let code = c.seg(1).filter(|s| is_slug(s))?; + hit(Some(code.to_string())) +} + +fn gather(c: &Ctx) -> Option { + if !c.host_under("gather.town") { + return None; + } + match c.seg_lower(0)?.as_str() { + "app" => { + let space = c.seg(1).filter(|s| is_slug(s))?; + hit(Some(space.to_string())) + } + "invite" => { + c.param("token")?; + hit(None) + } + _ => None, + } +} + +fn riverside(c: &Ctx) -> Option { + if !(c.host_under("riverside.fm") || c.host_under("riverside.com")) + || c.seg_lower(0)? != "studio" + { + return None; + } + c.seg(1).filter(|s| is_slug(s))?; + hit(None) +} + +fn streamyard(c: &Ctx) -> Option { + if !c.is_root_or_www("streamyard.com") || c.segs.len() != 1 { + return None; + } + let code = c.seg(0)?; + let ok = (8..=16).contains(&code.len()) + && code.bytes().all(|b| b.is_ascii_alphanumeric()) + && code.bytes().any(|b| b.is_ascii_digit()) + && code.bytes().any(|b| b.is_ascii_alphabetic()); + if !ok { + return None; + } + hit(Some(code.to_string())) +} + +fn whatsapp(c: &Ctx) -> Option { + if !c.host_is("call.whatsapp.com") { + return None; + } + match c.seg_lower(0)?.as_str() { + "voice" | "video" => { + let token = c.seg(1).filter(|s| is_slug(s))?; + hit(Some(token.to_string())) + } + _ => None, + } +} + +fn facetime(c: &Ctx) -> Option { + if !c.host_is("facetime.apple.com") || c.seg_lower(0)? != "join" { + return None; + } + c.url.fragment().filter(|f| !f.is_empty())?; + hit_with_fragment(None) +} + +fn cal_video(c: &Ctx) -> Option { + if !c.host_under("cal.com") || c.seg_lower(0)? != "video" { + return None; + } + let uid = c.seg(1).filter(|s| is_slug(s))?; + hit(Some(uid.to_string())) +} + +const DAILY_RESERVED_HOSTS: [&str; 8] = [ + "www", + "docs", + "dashboard", + "blog", + "help", + "status", + "api", + "go", +]; + +fn daily(c: &Ctx) -> Option { + let sub = c.host.strip_suffix(".daily.co")?; + if sub.contains('.') || DAILY_RESERVED_HOSTS.contains(&sub) || c.segs.len() != 1 { + return None; + } + c.seg(0).filter(|s| is_slug(s))?; + hit(None) +} + +fn livestorm(c: &Ctx) -> Option { + if !c.host_is("app.livestorm.co") || c.segs.len() < 2 { + return None; + } + let first = c.seg_lower(0)?; + if first == "p" { + return hit(c.seg(1).map(str::to_string)); + } + if matches!( + first.as_str(), + "login" | "signup" | "settings" | "auth" | "api" | "i" | "dashboard" + ) { + return None; + } + hit(None) +} + +fn pop(c: &Ctx) -> Option { + if !c.is_root_or_www("pop.com") || c.seg_lower(0)? != "j" { + return None; + } + let id = c.seg(1).filter(|s| is_slug(s))?; + hit(Some(id.to_string())) +} + +fn tandem(c: &Ctx) -> Option { + if !c.host_under("tandem.chat") { + return None; + } + match c.seg_lower(0)?.as_str() { + "join" | "room" | "r" => { + let room = c.seg(1).filter(|s| is_slug(s))?; + hit(Some(room.to_string())) + } + _ => None, + } +} + +fn vowel(c: &Ctx) -> Option { + if !c.host_under("vowel.com") || c.is_root_or_www("vowel.com") { + return None; + } + let fragment = c.url.fragment().map(percent_decode).unwrap_or_default(); + let all: Vec<&str> = c + .segs + .iter() + .map(String::as_str) + .chain(fragment.split('/')) + .filter(|s| !s.is_empty()) + .collect(); + let idx = all.iter().position(|s| matches!(*s, "room" | "g"))?; + let id = all.get(idx + 1).filter(|s| is_slug(s))?; + hit_with_fragment(Some(id.to_string())) +} + +fn demio(c: &Ctx) -> Option { + if !c.host_under("demio.com") || c.is_root_or_www("demio.com") { + return None; + } + match c.seg_lower(0)?.as_str() { + "ref" | "room" | "join" => { + let id = c.seg(1).filter(|s| is_slug(s))?; + hit(Some(id.to_string())) + } + _ => None, + } +} + +fn butter(c: &Ctx) -> Option { + if !c.host_is("app.butter.us") || c.segs.len() < 2 { + return None; + } + let first = c.seg_lower(0)?; + if matches!( + first.as_str(), + "login" | "signup" | "settings" | "dashboard" | "api" | "auth" | "templates" + ) || !is_slug(&first) + { + return None; + } + hit(None) +} diff --git a/apps/desktop-gpui/calendar/src/model.rs b/apps/desktop-gpui/calendar/src/model.rs new file mode 100644 index 00000000000..cea34007b7a --- /dev/null +++ b/apps/desktop-gpui/calendar/src/model.rs @@ -0,0 +1,157 @@ +use chrono::{DateTime, Utc}; +use serde::{Deserialize, Serialize}; + +use crate::link::MeetingLink; + +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub enum SourceKind { + #[default] + Ics, + Google, + Microsoft, + System, +} + +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub enum Response { + Accepted, + Declined, + Tentative, + NeedsAction, + #[default] + Unknown, +} + +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub enum EventStatus { + #[default] + Confirmed, + Tentative, + Cancelled, +} + +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase", default)] +pub struct Person { + pub name: Option, + pub email: Option, + pub is_self: bool, + pub response: Response, +} + +impl Person { + pub fn display_name(&self) -> Option<&str> { + self.name + .as_deref() + .filter(|n| !n.trim().is_empty()) + .or(self.email.as_deref()) + } +} + +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase", default)] +pub struct CalendarEvent { + pub id: String, + pub series_id: Option, + pub calendar_id: String, + pub source: SourceKind, + pub title: String, + pub start: DateTime, + pub end: DateTime, + pub all_day: bool, + pub location: Option, + pub description: Option, + pub organizer: Option, + pub attendees: Vec, + pub self_response: Response, + pub link: Option, + pub status: EventStatus, + pub web_url: Option, + pub free: bool, +} + +impl CalendarEvent { + pub fn is_cancelled(&self) -> bool { + self.status == EventStatus::Cancelled + } + + pub fn is_declined(&self) -> bool { + self.self_response == Response::Declined + } + + pub fn other_participants(&self) -> usize { + let attendees = self.attendees.iter().filter(|p| !p.is_self).count(); + if attendees > 0 { + return attendees; + } + usize::from(self.organizer.as_ref().is_some_and(|o| !o.is_self)) + } + + pub fn has_participants(&self) -> bool { + self.other_participants() > 0 + } +} + +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase", default)] +pub struct CalendarInfo { + pub id: String, + pub name: String, + pub source: SourceKind, + pub primary: bool, + pub color: Option, + pub selected: bool, + pub access_role: Option, + pub owner_email: Option, + pub time_zone: Option, +} + +#[derive(Clone, Debug, PartialEq)] +pub struct Page { + pub items: Vec, + pub next: Option, + pub skipped: usize, +} + +pub(crate) fn response_from_partstat(value: &str) -> Response { + match value.trim().to_ascii_uppercase().as_str() { + "ACCEPTED" => Response::Accepted, + "DECLINED" => Response::Declined, + "TENTATIVE" => Response::Tentative, + "NEEDS-ACTION" => Response::NeedsAction, + _ => Response::Unknown, + } +} + +pub(crate) fn email_eq(a: &str, b: &str) -> bool { + a.trim().eq_ignore_ascii_case(b.trim()) +} + +pub(crate) fn is_self_email(email: Option<&str>, self_emails: &[String]) -> bool { + email.is_some_and(|e| self_emails.iter().any(|s| email_eq(s, e))) +} + +pub(crate) fn derive_self_response( + organizer: Option<&Person>, + attendees: &[Person], + fallback: Response, +) -> Response { + if let Some(me) = attendees.iter().find(|p| p.is_self) + && me.response != Response::Unknown + { + return me.response; + } + if fallback != Response::Unknown { + return fallback; + } + if organizer.is_some_and(|o| o.is_self) { + return Response::Accepted; + } + attendees + .iter() + .find(|p| p.is_self) + .map_or(Response::Unknown, |p| p.response) +} diff --git a/apps/desktop-gpui/calendar/src/oauth.rs b/apps/desktop-gpui/calendar/src/oauth.rs new file mode 100644 index 00000000000..0bfefb337d6 --- /dev/null +++ b/apps/desktop-gpui/calendar/src/oauth.rs @@ -0,0 +1,321 @@ +use base64::Engine; +use base64::engine::general_purpose::{URL_SAFE, URL_SAFE_NO_PAD}; +use serde_json::Value; +use sha2::{Digest, Sha256}; + +use crate::http::HttpRequestSpec; +use crate::text::non_empty; + +pub const GOOGLE_AUTHORIZE_URL: &str = "https://accounts.google.com/o/oauth2/v2/auth"; +pub const GOOGLE_TOKEN_URL: &str = "https://oauth2.googleapis.com/token"; +pub const GOOGLE_SCOPES: &str = "openid email https://www.googleapis.com/auth/calendar.readonly"; +pub const MICROSOFT_SCOPES: &str = "openid email offline_access Calendars.Read User.Read"; + +#[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] +#[serde(rename_all = "camelCase")] +pub enum OAuthProvider { + Google, + Microsoft, +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct OAuthClient { + pub provider: OAuthProvider, + pub client_id: String, + pub client_secret: Option, + pub tenant: Option, +} + +impl OAuthClient { + pub fn google(client_id: impl Into, client_secret: Option) -> Self { + Self { + provider: OAuthProvider::Google, + client_id: client_id.into(), + client_secret, + tenant: None, + } + } + + pub fn microsoft(client_id: impl Into) -> Self { + Self { + provider: OAuthProvider::Microsoft, + client_id: client_id.into(), + client_secret: None, + tenant: None, + } + } + + fn microsoft_base(&self) -> String { + let tenant = self + .tenant + .as_deref() + .map(str::trim) + .filter(|t| { + !t.is_empty() + && t.chars() + .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '.') + }) + .unwrap_or("common"); + format!("https://login.microsoftonline.com/{tenant}/oauth2/v2.0") + } + + pub fn authorize_endpoint(&self) -> String { + match self.provider { + OAuthProvider::Google => GOOGLE_AUTHORIZE_URL.to_string(), + OAuthProvider::Microsoft => format!("{}/authorize", self.microsoft_base()), + } + } + + pub fn token_endpoint(&self) -> String { + match self.provider { + OAuthProvider::Google => GOOGLE_TOKEN_URL.to_string(), + OAuthProvider::Microsoft => format!("{}/token", self.microsoft_base()), + } + } + + pub fn scopes(&self) -> &'static str { + match self.provider { + OAuthProvider::Google => GOOGLE_SCOPES, + OAuthProvider::Microsoft => MICROSOFT_SCOPES, + } + } +} + +#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)] +pub enum AuthError { + #[error("need at least 32 random bytes, got {0}")] + NotEnoughRandomness(usize), + #[error("the user denied access")] + AccessDenied, + #[error("authorization failed: {error} {description}")] + Provider { error: String, description: String }, + #[error("state did not match")] + StateMismatch, + #[error("no authorization code in the redirect")] + MissingCode, + #[error("invalid token response: {0}")] + InvalidResponse(String), +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Pkce { + pub verifier: String, + pub challenge: String, +} + +impl Pkce { + pub fn from_random(bytes: &[u8]) -> Result { + if bytes.len() < 32 { + return Err(AuthError::NotEnoughRandomness(bytes.len())); + } + let verifier = URL_SAFE_NO_PAD.encode(&bytes[..bytes.len().min(96)]); + let challenge = s256_challenge(&verifier); + Ok(Self { + verifier, + challenge, + }) + } +} + +pub fn s256_challenge(verifier: &str) -> String { + URL_SAFE_NO_PAD.encode(Sha256::digest(verifier.as_bytes())) +} + +pub fn state_from_random(bytes: &[u8]) -> Result { + if bytes.len() < 16 { + return Err(AuthError::NotEnoughRandomness(bytes.len())); + } + Ok(URL_SAFE_NO_PAD.encode(bytes)) +} + +pub fn authorize_url( + client: &OAuthClient, + redirect_uri: &str, + state: &str, + challenge: &str, + login_hint: Option<&str>, +) -> String { + let mut pairs: Vec<(&str, &str)> = vec![ + ("client_id", client.client_id.as_str()), + ("redirect_uri", redirect_uri), + ("response_type", "code"), + ("scope", client.scopes()), + ("state", state), + ("code_challenge", challenge), + ("code_challenge_method", "S256"), + ]; + match client.provider { + OAuthProvider::Google => { + pairs.push(("access_type", "offline")); + pairs.push(("prompt", "consent")); + pairs.push(("include_granted_scopes", "true")); + } + OAuthProvider::Microsoft => { + pairs.push(("response_mode", "query")); + } + } + if let Some(hint) = login_hint.map(str::trim).filter(|h| !h.is_empty()) { + pairs.push(("login_hint", hint)); + } + let query = url::form_urlencoded::Serializer::new(String::new()) + .extend_pairs(pairs) + .finish(); + format!("{}?{query}", client.authorize_endpoint()) +} + +fn client_pairs(client: &OAuthClient, form: &mut Vec<(String, String)>) { + form.push(("client_id".to_string(), client.client_id.clone())); + if let Some(secret) = client.client_secret.as_deref().filter(|s| !s.is_empty()) { + form.push(("client_secret".to_string(), secret.to_string())); + } + if client.provider == OAuthProvider::Microsoft { + form.push(("scope".to_string(), client.scopes().to_string())); + } +} + +pub fn token_request( + client: &OAuthClient, + code: &str, + verifier: &str, + redirect_uri: &str, +) -> HttpRequestSpec { + let mut form = vec![ + ("grant_type".to_string(), "authorization_code".to_string()), + ("code".to_string(), code.to_string()), + ("redirect_uri".to_string(), redirect_uri.to_string()), + ("code_verifier".to_string(), verifier.to_string()), + ]; + client_pairs(client, &mut form); + HttpRequestSpec::post_form(client.token_endpoint(), form) +} + +pub fn refresh_request(client: &OAuthClient, refresh_token: &str) -> HttpRequestSpec { + let mut form = vec![ + ("grant_type".to_string(), "refresh_token".to_string()), + ("refresh_token".to_string(), refresh_token.to_string()), + ]; + client_pairs(client, &mut form); + HttpRequestSpec::post_form(client.token_endpoint(), form) +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Tokens { + pub access_token: String, + pub refresh_token: Option, + pub expires_in: Option, + pub id_token_email: Option, + pub scope: Option, +} + +fn provider_error(value: &Value) -> Option { + let error = value.get("error")?; + let code = error + .as_str() + .or_else(|| error.get("code").and_then(Value::as_str)) + .unwrap_or("error") + .to_string(); + let description = value + .get("error_description") + .and_then(Value::as_str) + .or_else(|| error.get("message").and_then(Value::as_str)) + .unwrap_or_default() + .to_string(); + if code == "access_denied" { + return Some(AuthError::AccessDenied); + } + Some(AuthError::Provider { + error: code, + description, + }) +} + +pub fn parse_token_response(json: &str) -> Result { + let value: Value = + serde_json::from_str(json).map_err(|e| AuthError::InvalidResponse(e.to_string()))?; + if let Some(err) = provider_error(&value) { + return Err(err); + } + let access_token = non_empty(value.get("access_token").and_then(Value::as_str)) + .ok_or_else(|| AuthError::InvalidResponse("missing access_token".to_string()))?; + let expires_in = match value.get("expires_in") { + Some(Value::Number(n)) => n.as_u64(), + Some(Value::String(s)) => s.trim().parse().ok(), + _ => None, + }; + Ok(Tokens { + access_token, + refresh_token: non_empty(value.get("refresh_token").and_then(Value::as_str)), + expires_in, + id_token_email: value + .get("id_token") + .and_then(Value::as_str) + .and_then(id_token_email), + scope: non_empty(value.get("scope").and_then(Value::as_str)), + }) +} + +pub fn id_token_email(id_token: &str) -> Option { + let payload = id_token.split('.').nth(1)?; + let bytes = URL_SAFE_NO_PAD + .decode(payload.trim_end_matches('=')) + .or_else(|_| URL_SAFE.decode(payload)) + .ok()?; + let claims: Value = serde_json::from_slice(&bytes).ok()?; + ["email", "preferred_username", "upn"] + .iter() + .filter_map(|k| claims.get(*k).and_then(Value::as_str)) + .map(str::trim) + .find(|v| v.contains('@')) + .map(str::to_string) +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct AuthCode { + pub code: String, +} + +fn constant_time_eq(a: &[u8], b: &[u8]) -> bool { + if a.len() != b.len() { + return false; + } + a.iter().zip(b).fold(0u8, |acc, (x, y)| acc | (x ^ y)) == 0 +} + +pub fn parse_redirect(query: &str, expected_state: &str) -> Result { + let raw = query.trim(); + let query = match url::Url::parse(raw) { + Ok(url) if raw.contains("://") => url.query().unwrap_or_default().to_string(), + _ => raw.trim_start_matches('?').to_string(), + }; + let pairs: Vec<(String, String)> = url::form_urlencoded::parse(query.as_bytes()) + .map(|(k, v)| (k.into_owned(), v.into_owned())) + .collect(); + let get = |name: &str| { + pairs + .iter() + .find(|(k, _)| k == name) + .map(|(_, v)| v.as_str()) + }; + let state_ok = get("state").is_some_and(|s| { + !expected_state.is_empty() && constant_time_eq(s.as_bytes(), expected_state.as_bytes()) + }); + if !state_ok { + return Err(AuthError::StateMismatch); + } + if let Some(error) = get("error") { + if error == "access_denied" { + return Err(AuthError::AccessDenied); + } + return Err(AuthError::Provider { + error: error.to_string(), + description: get("error_description").unwrap_or_default().to_string(), + }); + } + let code = get("code") + .map(str::trim) + .filter(|c| !c.is_empty()) + .ok_or(AuthError::MissingCode)?; + Ok(AuthCode { + code: code.to_string(), + }) +} diff --git a/apps/desktop-gpui/calendar/src/system.rs b/apps/desktop-gpui/calendar/src/system.rs new file mode 100644 index 00000000000..0008ee17e66 --- /dev/null +++ b/apps/desktop-gpui/calendar/src/system.rs @@ -0,0 +1,103 @@ +use chrono::{DateTime, NaiveTime, Utc}; +use chrono_tz::Tz; +use serde::{Deserialize, Serialize}; + +use crate::link::event_meeting_link; +use crate::model::{ + CalendarEvent, EventStatus, Person, Response, SourceKind, derive_self_response, +}; +use crate::text::non_empty; + +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase", default)] +pub struct SystemAttendee { + pub name: Option, + pub email: Option, + pub is_self: bool, + pub status: Response, +} + +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase", default)] +pub struct SystemEvent { + pub event_id: String, + pub calendar_id: String, + pub calendar_title: Option, + pub title: String, + pub start: DateTime, + pub end: DateTime, + pub all_day: bool, + pub location: Option, + pub notes: Option, + pub url: Option, + pub organizer: Option, + pub attendees: Vec, + pub is_recurring: bool, + pub status: EventStatus, + pub free: bool, +} + +fn person(a: &SystemAttendee) -> Person { + Person { + name: non_empty(a.name.as_deref()), + email: non_empty(a.email.as_deref()), + is_self: a.is_self, + response: a.status, + } +} + +pub fn from_system_event(event: &SystemEvent, local_zone: Tz) -> CalendarEvent { + let (start, end) = if event.all_day { + let start_date = event.start.with_timezone(&local_zone).date_naive(); + let last = event + .end + .checked_sub_signed(chrono::Duration::seconds(1)) + .unwrap_or(event.end) + .max(event.start) + .with_timezone(&local_zone) + .date_naive(); + let end_date = last.succ_opt().unwrap_or(last); + ( + start_date.and_time(NaiveTime::MIN).and_utc(), + end_date.and_time(NaiveTime::MIN).and_utc(), + ) + } else { + (event.start, event.end.max(event.start)) + }; + let organizer = event.organizer.as_ref().map(person); + let attendees: Vec = event.attendees.iter().map(person).collect(); + let self_response = derive_self_response(organizer.as_ref(), &attendees, Response::Unknown); + let id = if event.is_recurring { + format!( + "{}_{}", + event.event_id, + event.start.format("%Y%m%dT%H%M%SZ") + ) + } else { + event.event_id.clone() + }; + CalendarEvent { + id, + series_id: event.is_recurring.then(|| event.event_id.clone()), + calendar_id: event.calendar_id.clone(), + source: SourceKind::System, + title: event.title.trim().to_string(), + start, + end, + all_day: event.all_day, + location: non_empty(event.location.as_deref()), + description: non_empty(event.notes.as_deref()), + organizer, + attendees, + self_response, + link: event_meeting_link( + &[], + event.location.as_deref(), + event.notes.as_deref(), + event.url.as_deref(), + ), + status: event.status, + web_url: None, + free: event.free, + } +} diff --git a/apps/desktop-gpui/calendar/src/text.rs b/apps/desktop-gpui/calendar/src/text.rs new file mode 100644 index 00000000000..6ec938b9a34 --- /dev/null +++ b/apps/desktop-gpui/calendar/src/text.rs @@ -0,0 +1,143 @@ +use std::borrow::Cow; + +pub(crate) fn decode_entities(text: &str) -> Cow<'_, str> { + if !text.contains('&') { + return Cow::Borrowed(text); + } + let mut out = String::with_capacity(text.len()); + let mut rest = text; + while let Some(idx) = rest.find('&') { + out.push_str(&rest[..idx]); + let after = &rest[idx + 1..]; + let end = after + .char_indices() + .take(12) + .find(|(_, c)| *c == ';') + .map(|(i, _)| i); + let decoded = end.and_then(|end| decode_entity(&after[..end]).map(|c| (c, end))); + match decoded { + Some((c, end)) => { + out.push(c); + rest = &after[end + 1..]; + } + None => { + out.push('&'); + rest = after; + } + } + } + out.push_str(rest); + Cow::Owned(out) +} + +fn decode_entity(name: &str) -> Option { + if let Some(num) = name.strip_prefix('#') { + let code = match num.strip_prefix(['x', 'X']) { + Some(hex) => u32::from_str_radix(hex, 16).ok()?, + None => num.parse::().ok()?, + }; + return char::from_u32(code); + } + Some(match name { + "amp" => '&', + "lt" => '<', + "gt" => '>', + "quot" => '"', + "apos" => '\'', + "nbsp" => ' ', + "ndash" => '-', + "hellip" => '…', + _ => return None, + }) +} + +pub(crate) fn html_to_text(html: &str) -> String { + let lower = html.to_ascii_lowercase(); + let mut out = String::with_capacity(html.len() / 2); + let mut i = 0; + let bytes = html.as_bytes(); + while i < bytes.len() { + if bytes[i] == b'<' { + let skip_block = ["style", "script", "head"].into_iter().find(|tag| { + lower[i + 1..].starts_with(tag) + && lower[i + 1 + tag.len()..] + .chars() + .next() + .is_some_and(|c| c == '>' || c.is_whitespace()) + }); + if let Some(tag) = skip_block { + let close = format!(" i + pos + close.len(), + None => bytes.len(), + }; + if let Some(gt) = lower[i..].find('>') { + i += gt + 1; + } + continue; + } + let end = lower[i..].find('>').map_or(bytes.len(), |p| i + p + 1); + let tag = &lower[i..end]; + if tag.starts_with(" = Vec::new(); + for line in decoded.lines().map(str::trim_end) { + if line.trim().is_empty() && lines.last().is_none_or(|l| l.trim().is_empty()) { + continue; + } + lines.push(line); + } + while lines.last().is_some_and(|l| l.trim().is_empty()) { + lines.pop(); + } + lines.join("\n") +} + +pub(crate) fn looks_like_html(text: &str) -> bool { + let lower = text.to_ascii_lowercase(); + [ + "", "

String { + let mut out = String::with_capacity(title.len()); + let mut space = false; + for c in title.chars().flat_map(char::to_lowercase) { + if c.is_alphanumeric() { + if space && !out.is_empty() { + out.push(' '); + } + space = false; + out.push(c); + } else { + space = true; + } + } + out +} + +pub(crate) fn non_empty(value: Option<&str>) -> Option { + value + .map(str::trim) + .filter(|v| !v.is_empty()) + .map(str::to_string) +} diff --git a/apps/desktop-gpui/calendar/src/tz.rs b/apps/desktop-gpui/calendar/src/tz.rs new file mode 100644 index 00000000000..630aa617aff --- /dev/null +++ b/apps/desktop-gpui/calendar/src/tz.rs @@ -0,0 +1,430 @@ +use std::collections::HashMap; +use std::sync::OnceLock; + +use chrono::{DateTime, Duration, FixedOffset, LocalResult, NaiveDateTime, Offset, TimeZone, Utc}; +use chrono_tz::Tz; + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Zone { + Utc, + Named(Tz), + Fixed(FixedOffset), +} + +impl Zone { + pub fn to_utc(self, local: NaiveDateTime) -> DateTime { + match self { + Zone::Utc => local.and_utc(), + Zone::Fixed(offset) => { + let shifted = local + .checked_sub_signed(Duration::seconds(i64::from(offset.local_minus_utc()))) + .unwrap_or(local); + shifted.and_utc() + } + Zone::Named(tz) => named_to_utc(tz, local), + } + } + + pub fn from_utc(self, instant: DateTime) -> NaiveDateTime { + match self { + Zone::Utc => instant.naive_utc(), + Zone::Fixed(offset) => instant.with_timezone(&offset).naive_local(), + Zone::Named(tz) => instant.with_timezone(&tz).naive_local(), + } + } + + pub fn offset_seconds_at(self, instant: DateTime) -> i32 { + match self { + Zone::Utc => 0, + Zone::Fixed(offset) => offset.local_minus_utc(), + Zone::Named(tz) => tz + .offset_from_utc_datetime(&instant.naive_utc()) + .fix() + .local_minus_utc(), + } + } +} + +fn named_to_utc(tz: Tz, local: NaiveDateTime) -> DateTime { + match tz.from_local_datetime(&local) { + LocalResult::Single(dt) => dt.with_timezone(&Utc), + LocalResult::Ambiguous(earliest, _) => earliest.with_timezone(&Utc), + LocalResult::None => { + for minutes in [30, 60, 90, 120, 180] { + let Some(shifted) = local.checked_add_signed(Duration::minutes(minutes)) else { + break; + }; + if let LocalResult::Single(dt) | LocalResult::Ambiguous(dt, _) = + tz.from_local_datetime(&shifted) + { + let correction = Duration::seconds(i64::from(gap_offset_change(tz, dt))) + - Duration::minutes(minutes); + let utc = dt.with_timezone(&Utc); + return utc.checked_add_signed(correction).unwrap_or(utc); + } + } + local.and_utc() + } + } +} + +fn gap_offset_change(tz: Tz, after: DateTime) -> i32 { + let after_offset = after.offset().fix().local_minus_utc(); + let after_utc = after.with_timezone(&Utc); + let before = after_utc + .checked_sub_signed(Duration::hours(4)) + .unwrap_or(after_utc); + let before_offset = tz + .offset_from_utc_datetime(&before.naive_utc()) + .fix() + .local_minus_utc(); + after_offset - before_offset +} + +pub fn resolve_tzid(raw: &str) -> Option { + let name = raw.trim().trim_matches('"').trim(); + if name.is_empty() { + return None; + } + if let Some(zone) = resolve_plain(name) { + return Some(zone); + } + let parts: Vec<&str> = name.split('/').filter(|p| !p.is_empty()).collect(); + for take in [3, 2, 1] { + if parts.len() > take { + let candidate = parts[parts.len() - take..].join("/"); + if let Some(zone) = resolve_plain(&candidate) { + return Some(zone); + } + } + } + if let Some(rest) = name.strip_prefix('(') { + let label = rest.split_once(')').map(|(_, l)| l.trim()).unwrap_or(""); + if let Some(zone) = resolve_plain(label) { + return Some(zone); + } + } + None +} + +fn resolve_plain(name: &str) -> Option { + let upper = name.to_ascii_uppercase(); + if matches!( + upper.as_str(), + "UTC" | "Z" | "GMT" | "ETC/UTC" | "ETC/GMT" | "UCT" | "ZULU" | "COORDINATED UNIVERSAL TIME" + ) || upper == "TZONE://MICROSOFT/UTC" + { + return Some(Zone::Utc); + } + if let Ok(tz) = name.parse::() { + return Some(Zone::Named(tz)); + } + let lower = name.trim().to_ascii_lowercase(); + let index = lowercase_index(); + if let Some(found) = index.windows.get(&lower) { + return found.map(Zone::Named); + } + if let Some(tz) = index.iana.get(&lower) { + return Some(Zone::Named(*tz)); + } + parse_utc_offset_label(name).map(Zone::Fixed) +} + +struct LowercaseIndex { + windows: HashMap>, + iana: HashMap, +} + +fn lowercase_index() -> &'static LowercaseIndex { + static INDEX: OnceLock = OnceLock::new(); + INDEX.get_or_init(|| { + let mut windows = HashMap::new(); + for (name, iana) in WINDOWS_ZONES { + windows + .entry(name.to_ascii_lowercase()) + .or_insert_with(|| iana.parse::().ok()); + } + let mut iana = HashMap::new(); + for tz in chrono_tz::TZ_VARIANTS { + iana.entry(tz.name().to_ascii_lowercase()).or_insert(tz); + } + LowercaseIndex { windows, iana } + }) +} + +pub fn parse_utc_offset_label(label: &str) -> Option { + let trimmed = label.trim().trim_start_matches('('); + let upper = trimmed.to_ascii_uppercase(); + let rest = upper + .strip_prefix("UTC") + .or_else(|| upper.strip_prefix("GMT"))?; + let rest = rest.split(')').next().unwrap_or("").trim(); + if rest.is_empty() { + return FixedOffset::east_opt(0); + } + parse_offset(rest) +} + +pub fn parse_offset(value: &str) -> Option { + let value = value.trim(); + let (sign, digits) = match value.as_bytes().first()? { + b'+' => (1, &value[1..]), + b'-' => (-1, &value[1..]), + _ => return None, + }; + let digits: String = digits.chars().filter(|c| *c != ':').collect(); + if !digits.bytes().all(|b| b.is_ascii_digit()) { + return None; + } + let (h, m, s) = match digits.len() { + 1 | 2 => (digits.parse::().ok()?, 0, 0), + 4 => (digits[..2].parse().ok()?, digits[2..].parse().ok()?, 0), + 6 => ( + digits[..2].parse().ok()?, + digits[2..4].parse().ok()?, + digits[4..].parse().ok()?, + ), + _ => return None, + }; + if h > 23 || m > 59 || s > 59 { + return None; + } + FixedOffset::east_opt(sign * (h * 3600 + m * 60 + s)) +} + +pub fn windows_to_iana(name: &str) -> Option<&'static str> { + let wanted = name.trim(); + WINDOWS_ZONES + .iter() + .find(|(windows, _)| windows.eq_ignore_ascii_case(wanted)) + .map(|(_, iana)| *iana) +} + +pub fn preferred_zones() -> impl Iterator { + WINDOWS_ZONES + .iter() + .filter_map(|(_, iana)| iana.parse::().ok()) + .filter(|tz| !tz.name().starts_with("Etc/")) + .chain(chrono_tz::TZ_VARIANTS.iter().copied()) +} + +pub const WINDOWS_ZONES: &[(&str, &str)] = &[ + ("Dateline Standard Time", "Etc/GMT+12"), + ("UTC-11", "Etc/GMT+11"), + ("Aleutian Standard Time", "America/Adak"), + ("Hawaiian Standard Time", "Pacific/Honolulu"), + ("Marquesas Standard Time", "Pacific/Marquesas"), + ("Alaskan Standard Time", "America/Anchorage"), + ("UTC-09", "Etc/GMT+9"), + ("Pacific Standard Time (Mexico)", "America/Tijuana"), + ("UTC-08", "Etc/GMT+8"), + ("Pacific Standard Time", "America/Los_Angeles"), + ("US Mountain Standard Time", "America/Phoenix"), + ("Mountain Standard Time (Mexico)", "America/Mazatlan"), + ("Mountain Standard Time", "America/Denver"), + ("Yukon Standard Time", "America/Whitehorse"), + ("Central America Standard Time", "America/Guatemala"), + ("Central Standard Time", "America/Chicago"), + ("Easter Island Standard Time", "Pacific/Easter"), + ("Central Standard Time (Mexico)", "America/Mexico_City"), + ("Mexico Standard Time", "America/Mexico_City"), + ("Mexico Standard Time 2", "America/Chihuahua"), + ("Canada Central Standard Time", "America/Regina"), + ("SA Pacific Standard Time", "America/Bogota"), + ("Eastern Standard Time (Mexico)", "America/Cancun"), + ("Eastern Standard Time", "America/New_York"), + ("Haiti Standard Time", "America/Port-au-Prince"), + ("Cuba Standard Time", "America/Havana"), + ("US Eastern Standard Time", "America/Indiana/Indianapolis"), + ("Turks And Caicos Standard Time", "America/Grand_Turk"), + ("Paraguay Standard Time", "America/Asuncion"), + ("Atlantic Standard Time", "America/Halifax"), + ("Venezuela Standard Time", "America/Caracas"), + ("Central Brazilian Standard Time", "America/Cuiaba"), + ("SA Western Standard Time", "America/La_Paz"), + ("Pacific SA Standard Time", "America/Santiago"), + ("Newfoundland Standard Time", "America/St_Johns"), + ("Tocantins Standard Time", "America/Araguaina"), + ("E. South America Standard Time", "America/Sao_Paulo"), + ("SA Eastern Standard Time", "America/Cayenne"), + ("Argentina Standard Time", "America/Argentina/Buenos_Aires"), + ("Greenland Standard Time", "America/Nuuk"), + ("Montevideo Standard Time", "America/Montevideo"), + ("Magallanes Standard Time", "America/Punta_Arenas"), + ("Saint Pierre Standard Time", "America/Miquelon"), + ("Bahia Standard Time", "America/Bahia"), + ("UTC-02", "Etc/GMT+2"), + ("Mid-Atlantic Standard Time", "Etc/GMT+2"), + ("Azores Standard Time", "Atlantic/Azores"), + ("Cape Verde Standard Time", "Atlantic/Cape_Verde"), + ("UTC", "Etc/UTC"), + ("GMT Standard Time", "Europe/London"), + ("Greenwich Standard Time", "Atlantic/Reykjavik"), + ("Sao Tome Standard Time", "Africa/Sao_Tome"), + ("Morocco Standard Time", "Africa/Casablanca"), + ("W. Europe Standard Time", "Europe/Berlin"), + ("Central Europe Standard Time", "Europe/Budapest"), + ("Romance Standard Time", "Europe/Paris"), + ("Central European Standard Time", "Europe/Warsaw"), + ("W. Central Africa Standard Time", "Africa/Lagos"), + ("Jordan Standard Time", "Asia/Amman"), + ("GTB Standard Time", "Europe/Bucharest"), + ("Middle East Standard Time", "Asia/Beirut"), + ("Egypt Standard Time", "Africa/Cairo"), + ("E. Europe Standard Time", "Europe/Chisinau"), + ("Syria Standard Time", "Asia/Damascus"), + ("West Bank Standard Time", "Asia/Hebron"), + ("South Africa Standard Time", "Africa/Johannesburg"), + ("FLE Standard Time", "Europe/Kyiv"), + ("Israel Standard Time", "Asia/Jerusalem"), + ("South Sudan Standard Time", "Africa/Juba"), + ("Kaliningrad Standard Time", "Europe/Kaliningrad"), + ("Sudan Standard Time", "Africa/Khartoum"), + ("Libya Standard Time", "Africa/Tripoli"), + ("Namibia Standard Time", "Africa/Windhoek"), + ("Arabic Standard Time", "Asia/Baghdad"), + ("Turkey Standard Time", "Europe/Istanbul"), + ("Arab Standard Time", "Asia/Riyadh"), + ("Belarus Standard Time", "Europe/Minsk"), + ("Russian Standard Time", "Europe/Moscow"), + ("E. Africa Standard Time", "Africa/Nairobi"), + ("Volgograd Standard Time", "Europe/Volgograd"), + ("Iran Standard Time", "Asia/Tehran"), + ("Arabian Standard Time", "Asia/Dubai"), + ("Astrakhan Standard Time", "Europe/Astrakhan"), + ("Azerbaijan Standard Time", "Asia/Baku"), + ("Russia Time Zone 3", "Europe/Samara"), + ("Mauritius Standard Time", "Indian/Mauritius"), + ("Saratov Standard Time", "Europe/Saratov"), + ("Georgian Standard Time", "Asia/Tbilisi"), + ("Caucasus Standard Time", "Asia/Yerevan"), + ("Armenian Standard Time", "Asia/Yerevan"), + ("Afghanistan Standard Time", "Asia/Kabul"), + ("West Asia Standard Time", "Asia/Tashkent"), + ("Qyzylorda Standard Time", "Asia/Qyzylorda"), + ("Ekaterinburg Standard Time", "Asia/Yekaterinburg"), + ("Pakistan Standard Time", "Asia/Karachi"), + ("India Standard Time", "Asia/Kolkata"), + ("Sri Lanka Standard Time", "Asia/Colombo"), + ("Nepal Standard Time", "Asia/Kathmandu"), + ("Central Asia Standard Time", "Asia/Almaty"), + ("Bangladesh Standard Time", "Asia/Dhaka"), + ("Omsk Standard Time", "Asia/Omsk"), + ("Myanmar Standard Time", "Asia/Yangon"), + ("SE Asia Standard Time", "Asia/Bangkok"), + ("Altai Standard Time", "Asia/Barnaul"), + ("W. Mongolia Standard Time", "Asia/Hovd"), + ("North Asia Standard Time", "Asia/Krasnoyarsk"), + ("N. Central Asia Standard Time", "Asia/Novosibirsk"), + ("Tomsk Standard Time", "Asia/Tomsk"), + ("China Standard Time", "Asia/Shanghai"), + ("North Asia East Standard Time", "Asia/Irkutsk"), + ("Singapore Standard Time", "Asia/Singapore"), + ("Malay Peninsula Standard Time", "Asia/Kuala_Lumpur"), + ("W. Australia Standard Time", "Australia/Perth"), + ("Taipei Standard Time", "Asia/Taipei"), + ("Ulaanbaatar Standard Time", "Asia/Ulaanbaatar"), + ("Aus Central W. Standard Time", "Australia/Eucla"), + ("Transbaikal Standard Time", "Asia/Chita"), + ("Tokyo Standard Time", "Asia/Tokyo"), + ("North Korea Standard Time", "Asia/Pyongyang"), + ("Korea Standard Time", "Asia/Seoul"), + ("Yakutsk Standard Time", "Asia/Yakutsk"), + ("Cen. Australia Standard Time", "Australia/Adelaide"), + ("AUS Central Standard Time", "Australia/Darwin"), + ("E. Australia Standard Time", "Australia/Brisbane"), + ("AUS Eastern Standard Time", "Australia/Sydney"), + ("West Pacific Standard Time", "Pacific/Port_Moresby"), + ("Tasmania Standard Time", "Australia/Hobart"), + ("Vladivostok Standard Time", "Asia/Vladivostok"), + ("Lord Howe Standard Time", "Australia/Lord_Howe"), + ("Bougainville Standard Time", "Pacific/Bougainville"), + ("Russia Time Zone 10", "Asia/Srednekolymsk"), + ("Magadan Standard Time", "Asia/Magadan"), + ("Norfolk Standard Time", "Pacific/Norfolk"), + ("Sakhalin Standard Time", "Asia/Sakhalin"), + ("Central Pacific Standard Time", "Pacific/Guadalcanal"), + ("Russia Time Zone 11", "Asia/Kamchatka"), + ("Kamchatka Standard Time", "Asia/Kamchatka"), + ("New Zealand Standard Time", "Pacific/Auckland"), + ("UTC+12", "Etc/GMT-12"), + ("Fiji Standard Time", "Pacific/Fiji"), + ("Chatham Islands Standard Time", "Pacific/Chatham"), + ("UTC+13", "Etc/GMT-13"), + ("Tonga Standard Time", "Pacific/Tongatapu"), + ("Samoa Standard Time", "Pacific/Apia"), + ("Line Islands Standard Time", "Pacific/Kiritimati"), +]; + +#[cfg(test)] +mod tests { + use super::*; + use chrono::NaiveDate; + + fn naive(y: i32, mo: u32, d: u32, h: u32, mi: u32) -> NaiveDateTime { + NaiveDate::from_ymd_opt(y, mo, d) + .and_then(|date| date.and_hms_opt(h, mi, 0)) + .unwrap() + } + + #[test] + fn every_windows_zone_maps_to_a_known_iana_zone() { + for (windows, iana) in WINDOWS_ZONES { + assert!(iana.parse::().is_ok(), "{windows} -> {iana}"); + assert!(resolve_tzid(windows).is_some(), "{windows}"); + } + } + + #[test] + fn resolves_common_forms() { + assert_eq!( + resolve_tzid("Pacific Standard Time"), + Some(Zone::Named(Tz::America__Los_Angeles)) + ); + assert_eq!( + resolve_tzid("\"America/New_York\""), + Some(Zone::Named(Tz::America__New_York)) + ); + assert_eq!( + resolve_tzid("/mozilla.org/20050126_1/Europe/Berlin"), + Some(Zone::Named(Tz::Europe__Berlin)) + ); + assert_eq!( + resolve_tzid("/freeassociation.sourceforge.net/Tzfile/America/Argentina/Buenos_Aires"), + Some(Zone::Named(Tz::America__Argentina__Buenos_Aires)) + ); + assert_eq!(resolve_tzid("utc"), Some(Zone::Utc)); + assert_eq!(resolve_tzid("tzone://Microsoft/Utc"), Some(Zone::Utc)); + assert_eq!( + resolve_tzid("(UTC+05:30) Chennai, Kolkata, Mumbai, New Delhi"), + FixedOffset::east_opt(19800).map(Zone::Fixed) + ); + assert_eq!(resolve_tzid("Not A Zone"), None); + assert_eq!(resolve_tzid(""), None); + } + + #[test] + fn gap_times_move_forward_and_overlaps_take_the_first() { + let ny = Zone::Named(Tz::America__New_York); + let gap = ny.to_utc(naive(2026, 3, 8, 2, 30)); + assert_eq!(gap, naive(2026, 3, 8, 7, 30).and_utc()); + let overlap = ny.to_utc(naive(2026, 11, 1, 1, 30)); + assert_eq!(overlap, naive(2026, 11, 1, 5, 30).and_utc()); + let lord_howe = Zone::Named(Tz::Australia__Lord_Howe); + let half_hour_gap = lord_howe.to_utc(naive(2026, 10, 4, 2, 15)); + assert_eq!(lord_howe.from_utc(half_hour_gap), naive(2026, 10, 4, 2, 45)); + } + + #[test] + fn offsets_parse() { + assert_eq!(parse_offset("-0500"), FixedOffset::west_opt(5 * 3600)); + assert_eq!( + parse_offset("+05:45"), + FixedOffset::east_opt(5 * 3600 + 45 * 60) + ); + assert_eq!(parse_offset("+1"), FixedOffset::east_opt(3600)); + assert_eq!(parse_offset("0500"), None); + assert_eq!(parse_offset("+99"), None); + assert_eq!(parse_offset("+"), None); + } +} diff --git a/apps/desktop-gpui/calendar/tests/agenda.rs b/apps/desktop-gpui/calendar/tests/agenda.rs new file mode 100644 index 00000000000..b45de68f498 --- /dev/null +++ b/apps/desktop-gpui/calendar/tests/agenda.rs @@ -0,0 +1,375 @@ +use std::collections::HashSet; + +use cap_calendar::agenda::{DEFAULT_LEAD, dedupe, reminder_key}; +use cap_calendar::{ + Agenda, CalendarEvent, EventStatus, Filter, MeetingLink, Person, Provider, Response, + SourceKind, Tz, due_reminders, next_wake, +}; +use chrono::{DateTime, Duration, NaiveDate, Utc}; + +fn utc(s: &str) -> DateTime { + s.parse().unwrap_or_else(|e| panic!("{s}: {e}")) +} + +fn ev(id: &str, title: &str, start: &str, minutes: i64) -> CalendarEvent { + let start = utc(start); + CalendarEvent { + id: id.to_string(), + calendar_id: "cal".to_string(), + source: SourceKind::Google, + title: title.to_string(), + start, + end: start + Duration::minutes(minutes), + ..CalendarEvent::default() + } +} + +fn zoom(id: &str) -> Option { + Some(MeetingLink { + provider: Provider::Zoom, + url: format!("https://zoom.us/j/{id}"), + meeting_id: Some(id.to_string()), + }) +} + +fn guest(email: &str) -> Person { + Person { + email: Some(email.to_string()), + ..Person::default() + } +} + +fn ids(events: &[&CalendarEvent]) -> Vec { + events.iter().map(|e| e.id.clone()).collect() +} + +#[test] +fn dedupes_the_same_meeting_from_two_sources() { + let google = CalendarEvent { + link: zoom("84512345678"), + attendees: vec![guest("a@x.example"), guest("b@x.example")], + ..ev("g1", "Globex renewal", "2026-03-10T17:00:00Z", 45) + }; + let ics = CalendarEvent { + source: SourceKind::Ics, + calendar_id: "feed".to_string(), + link: zoom("84512345678"), + description: Some("From the feed".to_string()), + ..ev( + "feed-1", + "Renewal call (Globex)", + "2026-03-10T17:00:00Z", + 45, + ) + }; + let next_week = CalendarEvent { + link: zoom("84512345678"), + ..ev("g2", "Globex renewal", "2026-03-17T17:00:00Z", 45) + }; + let merged = dedupe(vec![ics, google, next_week]); + assert_eq!(merged.len(), 2); + assert_eq!(merged[0].id, "g1"); + assert_eq!(merged[0].description.as_deref(), Some("From the feed")); + assert_eq!(merged[1].id, "g2"); + + let system = CalendarEvent { + source: SourceKind::System, + location: Some("Room 4".to_string()), + ..ev("s1", "Weekly Sync", "2026-03-11T09:00:00Z", 30) + }; + let outlook = CalendarEvent { + source: SourceKind::Microsoft, + ..ev("m1", "weekly sync!", "2026-03-11T09:00:00Z", 30) + }; + let different_time = ev("m2", "Weekly sync", "2026-03-11T10:00:00Z", 30); + let merged = dedupe(vec![system, outlook, different_time]); + assert_eq!(merged.len(), 2); + assert_eq!(merged[0].id, "m1"); + assert_eq!(merged[0].location.as_deref(), Some("Room 4")); + + let page_overlap = vec![ + ev("same", "Old title", "2026-03-11T09:00:00Z", 30), + ev("same", "New title", "2026-03-11T09:30:00Z", 30), + ]; + let merged = dedupe(page_overlap); + assert_eq!(merged.len(), 1); + assert_eq!(merged[0].title, "New title"); +} + +#[test] +fn filters() { + let declined = CalendarEvent { + self_response: Response::Declined, + ..ev("declined", "Declined", "2026-03-10T10:00:00Z", 30) + }; + let all_day = CalendarEvent { + all_day: true, + ..ev("allday", "Holiday", "2026-03-10T00:00:00Z", 24 * 60) + }; + let cancelled = CalendarEvent { + status: EventStatus::Cancelled, + ..ev("cancelled", "Cancelled", "2026-03-10T11:00:00Z", 30) + }; + let hidden = CalendarEvent { + calendar_id: "hidden".to_string(), + ..ev("hidden", "Hidden", "2026-03-10T12:00:00Z", 30) + }; + let solo = ev("solo", "Solo", "2026-03-10T13:00:00Z", 30); + let group = CalendarEvent { + attendees: vec![guest("a@x.example")], + ..ev("group", "Group", "2026-03-10T14:00:00Z", 30) + }; + let all = vec![declined, all_day, cancelled, hidden, solo, group]; + + let default = Agenda::new(all.clone(), &Filter::default()); + let visible: Vec<&str> = default.events().iter().map(|e| e.id.as_str()).collect(); + assert_eq!(visible, vec!["hidden", "solo", "group"]); + + let strict = Filter { + hide_without_participants: true, + hidden_calendar_ids: HashSet::from(["hidden".to_string()]), + ..Filter::default() + }; + let strict_agenda = Agenda::new(all.clone(), &strict); + let visible: Vec<&str> = strict_agenda + .events() + .iter() + .map(|e| e.id.as_str()) + .collect(); + assert_eq!(visible, vec!["group"]); + + let loose = Filter { + hide_declined: false, + hide_all_day: false, + ..Filter::default() + }; + assert_eq!(Agenda::new(all, &loose).events().len(), 5); +} + +#[test] +fn current_next_upcoming_and_days() { + let events = vec![ + ev("a", "Standup", "2026-03-10T09:00:00Z", 15), + ev("b", "Long workshop", "2026-03-10T08:00:00Z", 180), + ev("c", "Lunch", "2026-03-10T12:00:00Z", 60), + ev("d", "Late", "2026-03-10T23:30:00Z", 60), + ev("zero", "Zero length", "2026-03-10T15:00:00Z", 0), + ]; + let agenda = Agenda::new(events, &Filter::default()); + let now = utc("2026-03-10T09:05:00Z"); + assert_eq!(ids(&agenda.current(now)), vec!["a", "b"]); + assert_eq!(agenda.next(now).map(|e| e.id.as_str()), Some("c")); + assert_eq!( + ids(&agenda.upcoming(now, Duration::hours(4))), + vec!["b", "a", "c"] + ); + assert_eq!(ids(&agenda.current(utc("2026-03-10T09:15:00Z"))), vec!["b"]); + assert_eq!( + ids(&agenda.current(utc("2026-03-10T15:00:30Z"))), + vec!["zero"] + ); + assert!(agenda.next(utc("2026-03-11T00:00:00Z")).is_none()); + + let day = NaiveDate::from_ymd_opt(2026, 3, 10).unwrap(); + let next_day = NaiveDate::from_ymd_opt(2026, 3, 11).unwrap(); + assert_eq!(agenda.for_day(day, Tz::Europe__London).len(), 5); + assert_eq!( + ids(&agenda.for_day(next_day, Tz::Europe__London)), + vec!["d"] + ); + assert_eq!( + ids(&agenda.for_day(day, Tz::Asia__Tokyo)), + vec!["b", "a", "c"] + ); + assert_eq!( + ids(&agenda.for_day(next_day, Tz::Asia__Tokyo)), + vec!["zero", "d"] + ); + assert_eq!(agenda.for_day(day, Tz::America__Los_Angeles).len(), 5); +} + +#[test] +fn reminder_timing_boundaries() { + let meeting = ev("m", "Meeting", "2026-03-10T10:00:00Z", 30); + let events = vec![meeting.clone()]; + let none = HashSet::new(); + let due = |now: &str, lead: Duration| due_reminders(&events, utc(now), lead, &none); + + assert!(due("2026-03-10T09:58:59Z", DEFAULT_LEAD).is_empty()); + let exactly = due("2026-03-10T09:59:00Z", DEFAULT_LEAD); + assert_eq!(exactly.len(), 1); + assert_eq!(exactly[0].fire_at, utc("2026-03-10T09:59:00Z")); + assert_eq!(exactly[0].key, reminder_key(&meeting)); + assert_eq!(due("2026-03-10T10:05:00Z", DEFAULT_LEAD).len(), 1); + assert_eq!(due("2026-03-10T10:29:59Z", DEFAULT_LEAD).len(), 1); + assert!(due("2026-03-10T10:30:00Z", DEFAULT_LEAD).is_empty()); + assert!(due("2026-03-10T09:59:59Z", Duration::zero()).is_empty()); + assert_eq!(due("2026-03-10T10:00:00Z", Duration::zero()).len(), 1); + assert_eq!(due("2026-03-10T09:45:00Z", Duration::minutes(15)).len(), 1); + assert_eq!(due("2026-03-10T09:45:00Z", Duration::hours(2)).len(), 1); + assert!(due("2026-03-10T09:44:59Z", Duration::hours(2)).is_empty()); + assert!(due("2026-03-10T09:59:30Z", Duration::seconds(-30)).is_empty()); + + let notified = HashSet::from([reminder_key(&meeting)]); + assert!( + due_reminders( + &events, + utc("2026-03-10T09:59:30Z"), + DEFAULT_LEAD, + ¬ified + ) + .is_empty() + ); + + let moved = CalendarEvent { + start: utc("2026-03-10T11:00:00Z"), + end: utc("2026-03-10T11:30:00Z"), + ..meeting + }; + let after_move = due_reminders( + &[moved], + utc("2026-03-10T10:59:00Z"), + DEFAULT_LEAD, + ¬ified, + ); + assert_eq!(after_move.len(), 1); +} + +#[test] +fn reminders_skip_noise_and_order_ties() { + let plain = ev("plain", "Plain", "2026-03-10T10:00:00Z", 30); + let linked = CalendarEvent { + link: zoom("1234567890"), + attendees: vec![guest("a@x.example")], + ..ev("linked", "Linked", "2026-03-10T10:00:00Z", 30) + }; + let crowded = CalendarEvent { + link: zoom("1234567891"), + attendees: vec![ + guest("a@x.example"), + guest("b@x.example"), + guest("c@x.example"), + ], + ..ev("crowded", "Crowded", "2026-03-10T10:00:00Z", 30) + }; + let declined = CalendarEvent { + self_response: Response::Declined, + ..ev("declined", "Declined", "2026-03-10T10:00:00Z", 30) + }; + let cancelled = CalendarEvent { + status: EventStatus::Cancelled, + ..ev("cancelled", "Cancelled", "2026-03-10T10:00:00Z", 30) + }; + let all_day = CalendarEvent { + all_day: true, + ..ev("allday", "All day", "2026-03-10T00:00:00Z", 24 * 60) + }; + let later = ev("later", "Later", "2026-03-10T10:20:00Z", 30); + let events = vec![plain, linked, declined, cancelled, all_day, crowded, later]; + let due = due_reminders( + &events, + utc("2026-03-10T09:59:10Z"), + DEFAULT_LEAD, + &HashSet::new(), + ); + let order: Vec<&str> = due.iter().map(|r| r.event.id.as_str()).collect(); + assert_eq!(order, vec!["crowded", "linked", "plain"]); + + assert_eq!( + next_wake(&events, utc("2026-03-10T09:58:00Z"), DEFAULT_LEAD), + Some(utc("2026-03-10T09:59:00Z")) + ); + assert_eq!( + next_wake(&events, utc("2026-03-10T09:59:00Z"), DEFAULT_LEAD), + Some(utc("2026-03-10T10:19:00Z")) + ); + assert_eq!( + next_wake(&events, utc("2026-03-10T10:19:00Z"), DEFAULT_LEAD), + None + ); + assert_eq!( + next_wake(&events, utc("2026-03-10T09:00:00Z"), Duration::hours(1)), + Some(utc("2026-03-10T09:45:00Z")) + ); + assert_eq!( + next_wake(&[], utc("2026-03-10T09:00:00Z"), DEFAULT_LEAD), + None + ); +} + +fn permutations(items: &[T]) -> Vec> { + if items.len() <= 1 { + return vec![items.to_vec()]; + } + let mut out = Vec::new(); + for i in 0..items.len() { + let mut rest = items.to_vec(); + let first = rest.remove(i); + for mut tail in permutations(&rest) { + tail.insert(0, first.clone()); + out.push(tail); + } + } + out +} + +#[test] +fn dedupe_is_the_same_for_every_input_order() { + let feed = CalendarEvent { + source: SourceKind::Ics, + calendar_id: "feed".to_string(), + link: zoom("84512345678"), + description: Some("From the feed".to_string()), + ..ev("feed-1", "Design review", "2026-03-10T17:00:00Z", 45) + }; + let provider = CalendarEvent { + link: zoom("84512345678"), + attendees: vec![guest("a@x.example"), guest("b@x.example")], + ..ev("g1", "Design review (v2)", "2026-03-10T17:00:00Z", 45) + }; + let system = CalendarEvent { + source: SourceKind::System, + calendar_id: "local".to_string(), + location: Some("Room 4".to_string()), + web_url: Some("https://calendar.example/e/1".to_string()), + ..ev("s1", "design review!", "2026-03-10T17:00:00Z", 45) + }; + let other = ev("g2", "Design review", "2026-03-10T18:00:00Z", 45); + let inputs = [feed, provider, system, other]; + let reference = dedupe(inputs.to_vec()); + assert_eq!(reference.len(), 2, "{reference:#?}"); + let merged = &reference[0]; + assert_eq!(merged.id, "g1"); + assert_eq!(merged.attendees.len(), 2); + assert_eq!(merged.description.as_deref(), Some("From the feed")); + assert_eq!(merged.location.as_deref(), Some("Room 4")); + assert_eq!( + merged.web_url.as_deref(), + Some("https://calendar.example/e/1") + ); + assert_eq!(reference[1].id, "g2"); + for order in permutations(&inputs) { + let ids: Vec<&str> = order.iter().map(|e| e.id.as_str()).collect(); + assert_eq!(dedupe(order.clone()), reference, "order {ids:?}"); + } + + let refreshed = CalendarEvent { + title: "Design review (final)".to_string(), + ..inputs[1].clone() + }; + let with_refresh = [ + inputs[0].clone(), + inputs[1].clone(), + inputs[2].clone(), + refreshed, + ]; + for order in permutations(&with_refresh[..3]) { + let mut order = order; + order.push(with_refresh[3].clone()); + let merged = dedupe(order); + assert_eq!(merged.len(), 1, "{merged:#?}"); + assert_eq!(merged[0].title, "Design review (final)"); + assert_eq!(merged[0].location.as_deref(), Some("Room 4")); + assert_eq!(merged[0].description.as_deref(), Some("From the feed")); + } +} diff --git a/apps/desktop-gpui/calendar/tests/fixtures/apple_export.ics b/apps/desktop-gpui/calendar/tests/fixtures/apple_export.ics new file mode 100644 index 00000000000..e1b85c0f1c9 --- /dev/null +++ b/apps/desktop-gpui/calendar/tests/fixtures/apple_export.ics @@ -0,0 +1,109 @@ +BEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//Apple Inc.//macOS 15.0//EN +CALSCALE:GREGORIAN +X-WR-CALNAME:Home +BEGIN:VTIMEZONE +TZID:Australia/Sydney +BEGIN:STANDARD +DTSTART:20080406T030000 +RRULE:FREQ=YEARLY;BYMONTH=4;BYDAY=1SU +TZNAME:AEST +TZOFFSETFROM:+1100 +TZOFFSETTO:+1000 +END:STANDARD +BEGIN:DAYLIGHT +DTSTART:20081005T020000 +RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=1SU +TZNAME:AEDT +TZOFFSETFROM:+1000 +TZOFFSETTO:+1100 +END:DAYLIGHT +END:VTIMEZONE +BEGIN:VTIMEZONE +TZID:Sydney Custom +BEGIN:STANDARD +DTSTART:20080406T030000 +RRULE:FREQ=YEARLY;BYMONTH=4;BYDAY=1SU +TZNAME:AEST +TZOFFSETFROM:+1100 +TZOFFSETTO:+1000 +END:STANDARD +BEGIN:DAYLIGHT +DTSTART:20081005T020000 +RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=1SU +TZNAME:AEDT +TZOFFSETFROM:+1000 +TZOFFSETTO:+1100 +END:DAYLIGHT +END:VTIMEZONE +BEGIN:VTIMEZONE +TZID:America/Los_Angeles +BEGIN:DAYLIGHT +TZOFFSETFROM:-0800 +RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=2SU +DTSTART:20070311T020000 +TZNAME:PDT +TZOFFSETTO:-0700 +END:DAYLIGHT +BEGIN:STANDARD +TZOFFSETFROM:-0700 +RRULE:FREQ=YEARLY;BYMONTH=11;BYDAY=1SU +DTSTART:20071104T020000 +TZNAME:PST +TZOFFSETTO:-0800 +END:STANDARD +END:VTIMEZONE +BEGIN:VEVENT +CREATED:20260301T010101Z +UID:5D1F3C2B-1A2B-4C3D-9E8F-0A1B2C3D4E5F +DTEND;TZID=Australia/Sydney:20260327T093000 +RRULE:FREQ=WEEKLY;COUNT=4;BYDAY=FR +TRANSP:OPAQUE +SUMMARY:Sydney standup +DTSTART;TZID=Australia/Sydney:20260327T090000 +DTSTAMP:20260301T010101Z +SEQUENCE:0 +X-APPLE-TRAVEL-ADVISORY-BEHAVIOR:AUTOMATIC +END:VEVENT +BEGIN:VEVENT +UID:8A7B6C5D-4E3F-2A1B-0C9D-8E7F6A5B4C3D +DTSTART;TZID=Sydney Custom:20260928T100000 +DTEND;TZID=Sydney Custom:20260928T104500 +RRULE:FREQ=WEEKLY;COUNT=3 +SUMMARY:Spring check-in +LOCATION:Zoom: https://zoom.us/my/sydneyteam +END:VEVENT +BEGIN:VEVENT +UID:FACETIME-0001 +DTSTART:20260401T180000Z +DTEND:20260401T183000Z +RRULE:FREQ=WEEKLY;INTERVAL=2;COUNT=3 +RDATE:20260403T180000Z +SUMMARY:Family call +URL;VALUE=URI:https://facetime.apple.com/join#v=1&p=AbCdEfGh123&k=XyZ987 +X-APPLE-STRUCTURED-LOCATION;VALUE=URI;X-APPLE-RADIUS=70;X-TITLE=Home:geo:-33.86,151.20 +END:VEVENT +BEGIN:VEVENT +UID:THANKSGIVING-US +DTSTART;VALUE=DATE:20231123 +DTEND;VALUE=DATE:20231124 +RRULE:FREQ=YEARLY;BYMONTH=11;BYDAY=4TH +SUMMARY:Thanksgiving +TRANSP:TRANSPARENT +END:VEVENT +BEGIN:VEVENT +UID:MONTH-END-31 +DTSTART;TZID=America/Los_Angeles:20260131T120000 +DTEND;TZID=America/Los_Angeles:20260131T123000 +RRULE:FREQ=MONTHLY;BYMONTHDAY=31 +SUMMARY:Month end close +END:VEVENT +BEGIN:VEVENT +UID:LAST-WORKDAY +DTSTART;TZID=America/Los_Angeles:20260130T160000 +DTEND;TZID=America/Los_Angeles:20260130T170000 +RRULE:FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=-1 +SUMMARY:Last workday drinks +END:VEVENT +END:VCALENDAR diff --git a/apps/desktop-gpui/calendar/tests/fixtures/google_calendar_list.json b/apps/desktop-gpui/calendar/tests/fixtures/google_calendar_list.json new file mode 100644 index 00000000000..0729dd117f8 --- /dev/null +++ b/apps/desktop-gpui/calendar/tests/fixtures/google_calendar_list.json @@ -0,0 +1,34 @@ +{ + "kind": "calendar#calendarList", + "etag": "\"p32g8r7rmf6l8g0g\"", + "nextSyncToken": "CICqyPj0iIIDEhBtZUBhY21lLmV4YW1wbGU=", + "items": [ + { + "kind": "calendar#calendarListEntry", + "id": "me@acme.example", + "summary": "me@acme.example", + "timeZone": "America/New_York", + "colorId": "14", + "backgroundColor": "#9fe1e7", + "foregroundColor": "#000000", + "selected": true, + "accessRole": "owner", + "primary": true + }, + { + "id": "en.usa#holiday@group.v.calendar.google.com", + "summary": "Holidays in United States", + "summaryOverride": "US holidays", + "backgroundColor": "#16a765", + "accessRole": "reader" + }, + { + "id": "c_0123456789abcdef@group.calendar.google.com", + "summary": "Team rota", + "selected": true, + "accessRole": "writer", + "deleted": true + }, + { "summary": "No id" } + ] +} diff --git a/apps/desktop-gpui/calendar/tests/fixtures/google_events_page1.json b/apps/desktop-gpui/calendar/tests/fixtures/google_events_page1.json new file mode 100644 index 00000000000..e217442543e --- /dev/null +++ b/apps/desktop-gpui/calendar/tests/fixtures/google_events_page1.json @@ -0,0 +1,238 @@ +{ + "kind": "calendar#events", + "etag": "\"p33c9ff7tq2m8g0g\"", + "summary": "me@acme.example", + "updated": "2026-03-01T12:00:00.000Z", + "timeZone": "America/New_York", + "accessRole": "owner", + "nextPageToken": "CigKGjdrdWt1cXJmZWRsbTJmOXQ2dm8ybWgxbG41GAEggICA2r", + "items": [ + { + "id": "7kukuqrfedlm2f9t6vo2mh1ln5_20260309T130000Z", + "status": "confirmed", + "htmlLink": "https://www.google.com/calendar/event?eid=N2t1a3VxcmZlZGxtMmY5dDZ2bzJtaDFsbjVfMjAyNjAzMDlUMTMwMDAwWiBtZUBhY21lLmV4YW1wbGU", + "summary": "Product sync", + "description": "Weekly product sync, bring updates.", + "location": "NYC-4-Hudson (8)", + "start": { + "dateTime": "2026-03-09T09:00:00-04:00", + "timeZone": "America/New_York" + }, + "end": { + "dateTime": "2026-03-09T09:30:00-04:00", + "timeZone": "America/New_York" + }, + "recurringEventId": "7kukuqrfedlm2f9t6vo2mh1ln5", + "originalStartTime": { + "dateTime": "2026-03-09T09:00:00-04:00", + "timeZone": "America/New_York" + }, + "organizer": { + "email": "alice@acme.example", + "displayName": "Alice Chen" + }, + "attendees": [ + { + "email": "alice@acme.example", + "displayName": "Alice Chen", + "organizer": true, + "responseStatus": "accepted" + }, + { + "email": "me@acme.example", + "self": true, + "responseStatus": "accepted" + }, + { "email": "bob@acme.example", "responseStatus": "needsAction" }, + { + "email": "c_188abc@resource.calendar.google.com", + "displayName": "NYC-4-Hudson (8)", + "resource": true, + "responseStatus": "accepted" + } + ], + "hangoutLink": "https://meet.google.com/abc-defg-hij", + "conferenceData": { + "entryPoints": [ + { + "entryPointType": "video", + "uri": "https://meet.google.com/abc-defg-hij", + "label": "meet.google.com/abc-defg-hij" + }, + { + "entryPointType": "more", + "uri": "https://tel.meet/abc-defg-hij?pin=123456789", + "pin": "123456789" + }, + { + "regionCode": "US", + "entryPointType": "phone", + "uri": "tel:+1-555-0100", + "label": "+1 555-0100", + "pin": "123456789" + } + ], + "conferenceSolution": { + "key": { "type": "hangoutsMeet" }, + "name": "Google Meet" + }, + "conferenceId": "abc-defg-hij" + }, + "eventType": "default" + }, + { + "id": "birthday_20260312", + "status": "confirmed", + "summary": "Sam's birthday", + "start": { "date": "2026-03-12" }, + "end": { "date": "2026-03-13" }, + "transparency": "transparent", + "eventType": "birthday" + }, + { + "id": "wl_20260310", + "status": "confirmed", + "summary": "Home", + "start": { "date": "2026-03-10" }, + "end": { "date": "2026-03-11" }, + "eventType": "workingLocation" + }, + { + "id": "ooo_1", + "status": "confirmed", + "summary": "Out of office", + "start": { "dateTime": "2026-03-11T13:00:00-04:00" }, + "end": { "dateTime": "2026-03-11T17:00:00-04:00" }, + "eventType": "outOfOffice" + }, + { + "id": "focus_1", + "status": "confirmed", + "summary": "Focus time", + "start": { "dateTime": "2026-03-11T09:00:00-04:00" }, + "end": { "dateTime": "2026-03-11T11:00:00-04:00" }, + "eventType": "focusTime" + }, + { + "id": "zoomaddon1", + "status": "confirmed", + "htmlLink": "https://www.google.com/calendar/event?eid=em9vbWFkZG9uMQ", + "summary": "Globex renewal", + "description": "Agenda
Pricing

Join: link", + "start": { "dateTime": "2026-03-10T13:00:00-04:00" }, + "end": { "dateTime": "2026-03-10T13:45:00-04:00" }, + "organizer": { "email": "me@acme.example", "self": true }, + "attendees": [ + { + "email": "me@acme.example", + "self": true, + "organizer": true, + "responseStatus": "accepted" + }, + { + "email": "bob@globex.example", + "displayName": "Bob Stone", + "responseStatus": "tentative" + } + ], + "conferenceData": { + "entryPoints": [ + { + "entryPointType": "video", + "uri": "https://acme.zoom.us/j/84512345678?pwd=aBcDeF", + "meetingCode": "84512345678", + "passcode": "123456" + } + ], + "conferenceSolution": { + "key": { "type": "addOn" }, + "name": "Zoom Meeting" + }, + "conferenceId": "84512345678" + }, + "eventType": "default" + }, + { + "id": "declined1", + "status": "confirmed", + "summary": "Optional brown bag", + "start": { "dateTime": "2026-03-10T16:00:00Z" }, + "end": { "dateTime": "2026-03-10T17:00:00Z" }, + "organizer": { "email": "hr@acme.example" }, + "attendees": [ + { + "email": "me@acme.example", + "self": true, + "responseStatus": "declined" + }, + { + "email": "hr@acme.example", + "organizer": true, + "responseStatus": "accepted" + } + ], + "hangoutLink": "https://meet.google.com/xyz-abcd-efg" + }, + { + "id": "7kukuqrfedlm2f9t6vo2mh1ln5_20260330T130000Z", + "status": "cancelled", + "recurringEventId": "7kukuqrfedlm2f9t6vo2mh1ln5", + "originalStartTime": { + "dateTime": "2026-03-30T09:00:00-04:00", + "timeZone": "America/New_York" + } + }, + { + "id": "teamsdesc1", + "status": "tentative", + "summary": "Partner intro", + "location": "Room 4", + "description": "________________________________________________________________________________\nMicrosoft Teams meeting\nJoin on your computer, mobile app or room device\nClick here to join the meeting\nMeeting ID: 111 222 333 444\n", + "start": { "dateTime": "2026-03-12T10:00:00-04:00" }, + "end": { "dateTime": "2026-03-12T10:30:00-04:00" }, + "organizer": { "email": "partner@initech.example" }, + "attendees": [ + { + "email": "me@acme.example", + "self": true, + "responseStatus": "needsAction" + }, + { + "email": "partner@initech.example", + "organizer": true, + "responseStatus": "accepted" + } + ] + }, + { + "id": "solo1", + "status": "confirmed", + "summary": "Write quarterly letter", + "start": { "dateTime": "2026-03-12T14:00:00-04:00" }, + "end": { "dateTime": "2026-03-12T15:00:00-04:00" }, + "organizer": { "email": "me@acme.example", "self": true }, + "creator": { "email": "me@acme.example", "self": true } + }, + { + "id": "nooffset1", + "status": "confirmed", + "summary": "London catch-up", + "start": { + "dateTime": "2026-03-13T10:00:00", + "timeZone": "Europe/London" + }, + "end": { "dateTime": "2026-03-13T10:30:00", "timeZone": "Europe/London" } + }, + { + "id": "garbage1", + "status": "confirmed", + "summary": "Broken start", + "start": "tomorrow" + }, + { + "id": "garbage2", + "summary": "No start at all" + }, + 42 + ] +} diff --git a/apps/desktop-gpui/calendar/tests/fixtures/google_events_page2.json b/apps/desktop-gpui/calendar/tests/fixtures/google_events_page2.json new file mode 100644 index 00000000000..56e193d8d33 --- /dev/null +++ b/apps/desktop-gpui/calendar/tests/fixtures/google_events_page2.json @@ -0,0 +1,14 @@ +{ + "kind": "calendar#events", + "timeZone": "America/New_York", + "items": [ + { + "id": "lastpage1", + "status": "confirmed", + "summary": "Late review", + "start": { "dateTime": "2026-03-20T15:00:00-04:00" }, + "end": { "dateTime": "2026-03-20T15:30:00-04:00" }, + "location": "https://acme.zoom.us/j/99887766554" + } + ] +} diff --git a/apps/desktop-gpui/calendar/tests/fixtures/google_export.ics b/apps/desktop-gpui/calendar/tests/fixtures/google_export.ics new file mode 100644 index 00000000000..dae344ce3d4 --- /dev/null +++ b/apps/desktop-gpui/calendar/tests/fixtures/google_export.ics @@ -0,0 +1,141 @@ +BEGIN:VCALENDAR +PRODID:-//Google Inc//Google Calendar 70.9054//EN +VERSION:2.0 +CALSCALE:GREGORIAN +METHOD:PUBLISH +X-WR-CALNAME:Acme Work +X-WR-TIMEZONE:America/New_York +BEGIN:VTIMEZONE +TZID:America/New_York +X-LIC-LOCATION:America/New_York +BEGIN:DAYLIGHT +TZOFFSETFROM:-0500 +TZOFFSETTO:-0400 +TZNAME:EDT +DTSTART:19700308T020000 +RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=2SU +END:DAYLIGHT +BEGIN:STANDARD +TZOFFSETFROM:-0400 +TZOFFSETTO:-0500 +TZNAME:EST +DTSTART:19701101T020000 +RRULE:FREQ=YEARLY;BYMONTH=11;BYDAY=1SU +END:STANDARD +END:VTIMEZONE +BEGIN:VEVENT +DTSTART;TZID=America/New_York:20260223T090000 +DTEND;TZID=America/New_York:20260223T093000 +RRULE:FREQ=WEEKLY;WKST=SU;BYDAY=MO +EXDATE;TZID=America/New_York:20260316T090000 +DTSTAMP:20260301T120000Z +ORGANIZER;CN=Alice Chen:mailto:alice@acme.example +UID:7kukuqrfedlm2f9t6vo2mh1ln5@google.com +ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=ACCEPTED;CN=Alice Ch + en;X-NUM-GUESTS=0:mailto:alice@acme.example +ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=ACCEPTED;CN=me@acme. + example;X-NUM-GUESTS=0:mailto:me@acme.example +ATTENDEE;CUTYPE=RESOURCE;ROLE=REQ-PARTICIPANT;PARTSTAT=ACCEPTED;CN=NYC-4-Huds + on (8);X-NUM-GUESTS=0:mailto:c_188abc@resource.calendar.google.com +X-GOOGLE-CONFERENCE:https://meet.google.com/abc-defg-hij +CREATED:20260201T100000Z +DESCRIPTION:Weekly product sync\, bring updates\; demos welcome.\n\n-::~:~::~ + :~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~ + :~:~:~:~:~:~:~:~:~:~:~:~:~:~::~:~::-\nJoin with Google Meet: https://meet.go + ogle.com/abc-defg-hij\nOr dial: (US) +1 555-0100 PIN: 123456789#\nMore phone + numbers: https://tel.meet/abc-defg-hij?pin=123456789\n\nLearn more about Me + et at: https://support.google.com/a/users/answer/9282720\n\nPlease do not ed + it this section.\n-::~:~::~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~: + ~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~:~::~:~::- +LAST-MODIFIED:20260301T120000Z +LOCATION:NYC-4-Hudson (8) +SEQUENCE:0 +STATUS:CONFIRMED +SUMMARY:Product sync +TRANSP:OPAQUE +BEGIN:VALARM +ACTION:DISPLAY +DESCRIPTION:This is an event reminder +TRIGGER:-P0DT0H10M0S +END:VALARM +END:VEVENT +BEGIN:VEVENT +DTSTART;TZID=America/New_York:20260323T110000 +DTEND;TZID=America/New_York:20260323T113000 +DTSTAMP:20260301T120000Z +ORGANIZER;CN=Alice Chen:mailto:alice@acme.example +UID:7kukuqrfedlm2f9t6vo2mh1ln5@google.com +RECURRENCE-ID;TZID=America/New_York:20260323T090000 +ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=ACCEPTED;CN=Alice Ch + en;X-NUM-GUESTS=0:mailto:alice@acme.example +ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=TENTATIVE;CN=me@acme + .example;X-NUM-GUESTS=0:mailto:me@acme.example +X-GOOGLE-CONFERENCE:https://meet.google.com/abc-defg-hij +SEQUENCE:1 +STATUS:CONFIRMED +SUMMARY:Product sync (moved) +TRANSP:OPAQUE +END:VEVENT +BEGIN:VEVENT +DTSTART;TZID=America/New_York:20260330T090000 +DTEND;TZID=America/New_York:20260330T093000 +DTSTAMP:20260301T120000Z +UID:7kukuqrfedlm2f9t6vo2mh1ln5@google.com +RECURRENCE-ID;TZID=America/New_York:20260330T090000 +SEQUENCE:2 +STATUS:CANCELLED +SUMMARY:Product sync +END:VEVENT +BEGIN:VEVENT +DTSTART;VALUE=DATE:20260310 +DTEND;VALUE=DATE:20260312 +DTSTAMP:20260301T120000Z +UID:offsite-2026@google.com +SUMMARY:Team offsite +TRANSP:TRANSPARENT +STATUS:CONFIRMED +END:VEVENT +BEGIN:VEVENT +DTSTART:20260304T170000Z +DTEND:20260304T174500Z +DTSTAMP:20260301T120000Z +UID:0abc123zoom@google.com +SUMMARY:Customer call: Globex +DESCRIPTION:

Hi all\,

Join Zoom Meeting
https://acme.zoom.us/j/84512345678?pwd=aBcDeF

< + p>Meeting ID: 845 1234 5678

Find your local number: https://acme.zoom.us/u/abcdEfGh

+ORGANIZER;CN=me@acme.example:mailto:me@acme.example +ATTENDEE;PARTSTAT=NEEDS-ACTION;CN=Bob Stone:mailto:bob@globex.example +ATTENDEE;PARTSTAT=ACCEPTED:mailto:me@acme.example +LOCATION: +STATUS:CONFIRMED +END:VEVENT +BEGIN:VEVENT +DTSTART;TZID=America/New_York:20260305T150000 +DTEND;TZID=America/New_York:20260305T160000 +DTSTAMP:20260301T120000Z +UID:declined-1@google.com +SUMMARY:All hands +ATTENDEE;PARTSTAT=DECLINED;CN=Me:mailto:ME@Acme.Example +ORGANIZER;CN=CEO:mailto:ceo@acme.example +LOCATION:https://teams.microsoft.com/l/meetup-join/19%3ameeting_YmFkZ2Vy%40th + read.v2/0?context=%7b%22Tid%22%3a%22t%22%7d +END:VEVENT +BEGIN:VEVENT +DTSTART:20260309T080000 +DURATION:PT45M +RRULE:FREQ=DAILY;COUNT=5 +DTSTAMP:20260301T120000Z +UID:focus-daily@google.com +SUMMARY:Focus block +END:VEVENT +BEGIN:VEVENT +DTSTART;TZID=America/New_York:20260306T120000 +DTSTAMP:20260301T120000Z +UID:quick-chat@google.com +SUMMARY:Quick chat +URL:https://whereby.com/acme-standup +END:VEVENT +END:VCALENDAR diff --git a/apps/desktop-gpui/calendar/tests/fixtures/graph_calendar_view.json b/apps/desktop-gpui/calendar/tests/fixtures/graph_calendar_view.json new file mode 100644 index 00000000000..b2928c5c38d --- /dev/null +++ b/apps/desktop-gpui/calendar/tests/fixtures/graph_calendar_view.json @@ -0,0 +1,184 @@ +{ + "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('me%40contoso.example')/calendarView(id,subject,body,start,end)", + "@odata.nextLink": "https://graph.microsoft.com/v1.0/me/calendarView?startDateTime=2026-03-01T00%3a00%3a00Z&endDateTime=2026-04-01T00%3a00%3a00Z&%24select=id%2csubject&%24top=250&%24skip=250", + "value": [ + { + "@odata.etag": "W/\"DwAAABYAAAA\"", + "id": "AAMkAGI2TG93AAA=", + "subject": "Design review", + "bodyPreview": "Microsoft Teams meeting", + "body": { + "contentType": "html", + "content": "
Please review the mocks.
Microsoft Teams meeting
Meeting ID: 245 678 901 234
Meeting options
" + }, + "start": { "dateTime": "2026-03-10T17:00:00.0000000", "timeZone": "UTC" }, + "end": { "dateTime": "2026-03-10T18:00:00.0000000", "timeZone": "UTC" }, + "isAllDay": false, + "isCancelled": false, + "isOnlineMeeting": true, + "onlineMeetingProvider": "teamsForBusiness", + "onlineMeetingUrl": null, + "onlineMeeting": { + "joinUrl": "https://teams.microsoft.com/l/meetup-join/19%3ameeting_NjE2ODk3%40thread.v2/0?context=%7b%22Tid%22%3a%2272f988bf%22%7d", + "conferenceId": "245678901", + "tollNumber": "+1 555 0100" + }, + "location": { + "displayName": "Microsoft Teams Meeting", + "locationType": "default", + "uniqueIdType": "unknown" + }, + "locations": [ + { "displayName": "Microsoft Teams Meeting", "locationType": "default" } + ], + "responseStatus": { + "response": "accepted", + "time": "2026-03-01T10:00:00Z" + }, + "organizer": { + "emailAddress": { + "name": "Dana Ruiz", + "address": "dana@contoso.example" + } + }, + "attendees": [ + { + "type": "required", + "status": { "response": "accepted", "time": "2026-03-01T10:00:00Z" }, + "emailAddress": { + "name": "Me Myself", + "address": "me@contoso.example" + } + }, + { + "type": "optional", + "status": { "response": "none", "time": "0001-01-01T00:00:00Z" }, + "emailAddress": { "name": "Lee", "address": "lee@contoso.example" } + }, + { + "type": "resource", + "status": { "response": "accepted" }, + "emailAddress": { + "name": "Conf Room 7", + "address": "room7@contoso.example" + } + } + ], + "type": "occurrence", + "seriesMasterId": "AAMkAGI2SERIES=", + "webLink": "https://outlook.office365.com/owa/?itemid=AAMkAGI2TG93AAA%3D&exvsurl=1&path=/calendar/item", + "showAs": "busy" + }, + { + "id": "AAMkAGI2HOLIDAY=", + "subject": "Company holiday", + "body": { "contentType": "text", "content": "" }, + "start": { "dateTime": "2026-03-20T00:00:00.0000000", "timeZone": "UTC" }, + "end": { "dateTime": "2026-03-21T00:00:00.0000000", "timeZone": "UTC" }, + "isAllDay": true, + "isCancelled": false, + "showAs": "free", + "responseStatus": { "response": "organizer" }, + "organizer": { + "emailAddress": { "name": "Me Myself", "address": "me@contoso.example" } + }, + "attendees": [], + "type": "singleInstance" + }, + { + "id": "AAMkAGI2PACIFIC=", + "subject": "West coast sync", + "body": { "contentType": "text", "content": "Agenda: roadmap" }, + "start": { + "dateTime": "2026-03-12T09:00:00.0000000", + "timeZone": "Pacific Standard Time" + }, + "end": { + "dateTime": "2026-03-12T09:30:00.0000000", + "timeZone": "Pacific Standard Time" + }, + "isAllDay": false, + "isCancelled": false, + "location": { "displayName": "https://meet.google.com/qrs-tuvw-xyz" }, + "responseStatus": { "response": "tentativelyAccepted" }, + "organizer": { + "emailAddress": { "name": "Sam", "address": "sam@partner.example" } + }, + "attendees": [ + { + "type": "required", + "status": { "response": "tentativelyAccepted" }, + "emailAddress": { + "name": "Me Myself", + "address": "me@contoso.example" + } + } + ], + "type": "singleInstance" + }, + { + "id": "AAMkAGI2ZOOMBODY=", + "subject": "Vendor demo", + "body": { + "contentType": "html", + "content": "

Join Zoom Meeting
https://us02web.zoom.us/j/81234567890?pwd=ZmFrZQ.1

" + }, + "start": { "dateTime": "2026-03-13T15:00:00.0000000", "timeZone": "UTC" }, + "end": { "dateTime": "2026-03-13T15:30:00.0000000", "timeZone": "UTC" }, + "isAllDay": false, + "isCancelled": false, + "isOnlineMeeting": false, + "onlineMeeting": null, + "location": { "displayName": "" }, + "responseStatus": { "response": "notResponded" }, + "organizer": { + "emailAddress": { "name": "Vendor", "address": "sales@vendor.example" } + }, + "attendees": [ + { + "type": "required", + "status": { "response": "none" }, + "emailAddress": { + "name": "Me Myself", + "address": "me@contoso.example" + } + }, + { + "type": "required", + "status": { "response": "accepted" }, + "emailAddress": { + "name": "Vendor", + "address": "sales@vendor.example" + } + } + ], + "type": "singleInstance" + }, + { + "id": "AAMkAGI2CANCELLED=", + "subject": "Canceled: Weekly ops", + "start": { "dateTime": "2026-03-16T14:00:00.0000000", "timeZone": "UTC" }, + "end": { "dateTime": "2026-03-16T14:30:00.0000000", "timeZone": "UTC" }, + "isCancelled": true, + "type": "occurrence", + "seriesMasterId": "AAMkAGI2OPS=" + }, + { + "id": "AAMkAGI2DECLINED=", + "subject": "Optional town hall", + "start": { "dateTime": "2026-03-17T16:00:00.0000000", "timeZone": "UTC" }, + "end": { "dateTime": "2026-03-17T17:00:00.0000000", "timeZone": "UTC" }, + "responseStatus": { "response": "declined" }, + "organizer": { + "emailAddress": { "name": "Comms", "address": "comms@contoso.example" } + }, + "onlineMeetingUrl": "https://teams.microsoft.com/l/meetup-join/19%3ameeting_VE9XTkhBTEw%40thread.v2/0", + "type": "singleInstance" + }, + { + "id": "AAMkAGI2NOSTART=", + "subject": "Corrupt", + "start": null + } + ] +} diff --git a/apps/desktop-gpui/calendar/tests/fixtures/graph_calendars.json b/apps/desktop-gpui/calendar/tests/fixtures/graph_calendars.json new file mode 100644 index 00000000000..3b130d1fe69 --- /dev/null +++ b/apps/desktop-gpui/calendar/tests/fixtures/graph_calendars.json @@ -0,0 +1,24 @@ +{ + "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('me')/calendars(id,name)", + "value": [ + { + "id": "AAMkAGI2CAL1=", + "name": "Calendar", + "color": "auto", + "hexColor": "", + "isDefaultCalendar": true, + "canEdit": true, + "owner": { "name": "Me Myself", "address": "me@contoso.example" } + }, + { + "id": "AAMkAGI2CAL2=", + "name": "United States holidays", + "color": "lightGreen", + "hexColor": "#87d28e", + "isDefaultCalendar": false, + "canEdit": false, + "owner": { "name": "Me Myself", "address": "me@contoso.example" } + }, + { "name": "missing id" } + ] +} diff --git a/apps/desktop-gpui/calendar/tests/fixtures/malformed.ics b/apps/desktop-gpui/calendar/tests/fixtures/malformed.ics new file mode 100644 index 00000000000..ae9d1d1b87e Binary files /dev/null and b/apps/desktop-gpui/calendar/tests/fixtures/malformed.ics differ diff --git a/apps/desktop-gpui/calendar/tests/fixtures/outlook_export.ics b/apps/desktop-gpui/calendar/tests/fixtures/outlook_export.ics new file mode 100644 index 00000000000..7629149ad7f --- /dev/null +++ b/apps/desktop-gpui/calendar/tests/fixtures/outlook_export.ics @@ -0,0 +1,147 @@ +BEGIN:VCALENDAR +METHOD:PUBLISH +PRODID:Microsoft Exchange Server 2010 +VERSION:2.0 +X-WR-CALNAME:Calendar +BEGIN:VTIMEZONE +TZID:Pacific Standard Time +BEGIN:STANDARD +DTSTART:16010101T020000 +TZOFFSETFROM:-0700 +TZOFFSETTO:-0800 +RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=1SU;BYMONTH=11 +END:STANDARD +BEGIN:DAYLIGHT +DTSTART:16010101T020000 +TZOFFSETFROM:-0800 +TZOFFSETTO:-0700 +RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=2SU;BYMONTH=3 +END:DAYLIGHT +END:VTIMEZONE +BEGIN:VTIMEZONE +TZID:W. Europe Standard Time +BEGIN:STANDARD +DTSTART:16010101T030000 +TZOFFSETFROM:+0200 +TZOFFSETTO:+0100 +RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=-1SU;BYMONTH=10 +END:STANDARD +BEGIN:DAYLIGHT +DTSTART:16010101T020000 +TZOFFSETFROM:+0100 +TZOFFSETTO:+0200 +RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=-1SU;BYMONTH=3 +END:DAYLIGHT +END:VTIMEZONE +BEGIN:VTIMEZONE +TZID:Customized Time Zone +BEGIN:STANDARD +DTSTART:16010101T020000 +TZOFFSETFROM:+0100 +TZOFFSETTO:+0000 +RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=-1SU;BYMONTH=10 +END:STANDARD +BEGIN:DAYLIGHT +DTSTART:16010101T010000 +TZOFFSETFROM:+0000 +TZOFFSETTO:+0100 +RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=-1SU;BYMONTH=3 +END:DAYLIGHT +END:VTIMEZONE +BEGIN:VEVENT +DESCRIPTION;LANGUAGE=en-US:\n______________________________________________ + __________________________________\nMicrosoft Teams meeting\nJoin on your co + mputer\, mobile app or room device\nClick here to join the meeting\nMeeting ID: 245 67 + 8 901 234\nPasscode: Xy7pQ2\nDownload Teams | Join on the web\nLearn More | M + eeting options\n_____________________________________________________________________ + ___________\n +UID:040000008200E00074C5B7101A82E00800000000D0F0B8B6A1B0DC0100000000000000001 + 0000000A1B2C3D4E5F60718293A4B5C6D7E8F90 +SUMMARY;LANGUAGE=en-US:Design review +DTSTART;TZID=Pacific Standard Time:20260310T100000 +DTEND;TZID=Pacific Standard Time:20260310T110000 +CLASS:PUBLIC +PRIORITY:5 +DTSTAMP:20260301T090000Z +TRANSP:OPAQUE +STATUS:CONFIRMED +SEQUENCE:0 +LOCATION;LANGUAGE=en-US:Microsoft Teams Meeting +X-MICROSOFT-CDO-APPT-SEQUENCE:0 +X-MICROSOFT-CDO-BUSYSTATUS:BUSY +X-MICROSOFT-CDO-INTENDEDSTATUS:BUSY +X-MICROSOFT-CDO-ALLDAYEVENT:FALSE +X-MICROSOFT-CDO-IMPORTANCE:1 +X-MICROSOFT-CDO-INSTTYPE:0 +X-MICROSOFT-SKYPETEAMSMEETINGURL:https://teams.microsoft.com/l/meetup-join/19 + %3ameeting_NjE2ODk3%40thread.v2/0?context=%7b%22Tid%22%3a%2272f988bf%22%2c%2 + 2Oid%22%3a%22abc%22%7d +X-MICROSOFT-ONLINEMEETINGCONFLINK:conf:sip:dana@contoso.example\;gruu\;opaque + =app:conf:focus:id:teams:2:0!19:meeting_NjE2ODk3-thread.v2!abc!72f988bf +X-MICROSOFT-DONOTFORWARDMEETING:FALSE +X-MICROSOFT-DISALLOW-COUNTER:FALSE +ORGANIZER;CN=Dana Ruiz:mailto:dana@contoso.example +ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=TENTATIVE;RSVP=TRUE;CN=Me Myself:mailt + o:me@contoso.example +ATTENDEE;ROLE=OPT-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN="Ruiz, Dana + ":mailto:dana@contoso.example +END:VEVENT +BEGIN:VEVENT +DESCRIPTION;LANGUAGE=en-US:Monthly board review.\nJoin: https://nam12.safelin + ks.protection.outlook.com/?url=https%3A%2F%2Fus06web.zoom.us%2Fj%2F87654321 + 098%3Fpwd%3DQwErTy&data=05%7C02%7Cme%40contoso.example%7C1&sdata=abc%3D&res + erved=0\n +RRULE:FREQ=MONTHLY;COUNT=6;BYDAY=2TU;WKST=MO +UID:040000008200E00074C5B7101A82E00800000000BOARD +SUMMARY;LANGUAGE=en-US:Board review +DTSTART;TZID=W. Europe Standard Time:20260113T150000 +DTEND;TZID=W. Europe Standard Time:20260113T163000 +STATUS:CONFIRMED +X-MICROSOFT-CDO-BUSYSTATUS:BUSY +X-MICROSOFT-CDO-ALLDAYEVENT:FALSE +ORGANIZER;CN=Board Office:mailto:board@contoso.example +ATTENDEE;PARTSTAT=ACCEPTED;CN=Me Myself:mailto:me@contoso.example +END:VEVENT +BEGIN:VEVENT +UID:040000008200E00074C5B7101A82E00800000000HOLIDAY +SUMMARY;LANGUAGE=en-US:Company holiday +DTSTART;VALUE=DATE:20260320 +DTEND;VALUE=DATE:20260321 +TRANSP:TRANSPARENT +X-MICROSOFT-CDO-BUSYSTATUS:FREE +X-MICROSOFT-CDO-ALLDAYEVENT:TRUE +END:VEVENT +BEGIN:VEVENT +UID:040000008200E00074C5B7101A82E00800000000SICK +SUMMARY;LANGUAGE=en-US:Out sick +DTSTART;TZID=W. Europe Standard Time:20260325T000000 +DTEND;TZID=W. Europe Standard Time:20260326T000000 +X-MICROSOFT-CDO-BUSYSTATUS:OOF +X-MICROSOFT-CDO-ALLDAYEVENT:TRUE +END:VEVENT +BEGIN:VEVENT +UID:040000008200E00074C5B7101A82E00800000000ONEONONE +SUMMARY;LANGUAGE=en-GB:1:1 Priya / Me +DTSTART;TZID=Customized Time Zone:20260319T093000 +DTEND;TZID=Customized Time Zone:20260319T100000 +RRULE:FREQ=WEEKLY;UNTIL=20260430T083000Z;INTERVAL=2;BYDAY=TH;WKST=MO +LOCATION;LANGUAGE=en-GB:https://acme.webex.com/acme/j.php?MTID=m1234567890ab + cdef1234567890abcdef +ORGANIZER;CN=Priya:mailto:priya@contoso.example +ATTENDEE;PARTSTAT=ACCEPTED;CN=Me Myself:mailto:me@contoso.example +END:VEVENT +BEGIN:VEVENT +UID:040000008200E00074C5B7101A82E00800000000YEARLY +SUMMARY:Annual planning +DTSTART;TZID=Pacific Standard Time:20240615T100000 +DTEND;TZID=Pacific Standard Time:20240615T120000 +RRULE:FREQ=YEARLY;BYMONTH=6;BYMONTHDAY=15 +ORGANIZER;CN=Dana Ruiz:mailto:dana@contoso.example +END:VEVENT +END:VCALENDAR diff --git a/apps/desktop-gpui/calendar/tests/fuzz.rs b/apps/desktop-gpui/calendar/tests/fuzz.rs new file mode 100644 index 00000000000..7a82251fbf4 --- /dev/null +++ b/apps/desktop-gpui/calendar/tests/fuzz.rs @@ -0,0 +1,203 @@ +use cap_calendar::google::parse_google_events; +use cap_calendar::graph::parse_graph_events; +use cap_calendar::ics::{ParseOptions, expand, parse_calendar, parse_calendar_bytes}; +use cap_calendar::oauth::{parse_redirect, parse_token_response}; +use cap_calendar::{Agenda, Filter, Tz, due_reminders, find_meeting_link, next_wake}; +use chrono::{DateTime, Duration, Utc}; + +struct Rng(u64); + +impl Rng { + fn next(&mut self) -> u64 { + self.0 ^= self.0 << 13; + self.0 ^= self.0 >> 7; + self.0 ^= self.0 << 17; + self.0 + } + + fn below(&mut self, n: usize) -> usize { + if n == 0 { + return 0; + } + (self.next() % n as u64) as usize + } +} + +const NOISE: &[&[u8]] = &[ + b":", + b";", + b"\r\n", + b"\n ", + b"\"", + b",", + b"\\", + b"Z", + b"T", + b"0", + b"9", + b"=", + b"-", + b"/", + b"\xc3\xa9", + b"\xff", + b"\0", + b"BEGIN:VEVENT\n", + b"END:VEVENT\n", + b"RRULE:FREQ=SECONDLY\n", + b"RRULE:FREQ=DAILY;BYSETPOS=-1;BYDAY=MO\n", + b"DTSTART:00000101T000000Z\n", + b"DTSTART:99991231T235959Z\n", + b"TZID=", + b"%", + b"&", + b"{", + b"}", + b"[", + b"]", + b"null", + b"\"dateTime\":\"", + b"https://", + b"zoom.us/j/", + b"meet.google.com/", +]; + +fn mutate(rng: &mut Rng, input: &[u8]) -> Vec { + let mut data = input.to_vec(); + for _ in 0..=rng.below(6) { + let len = data.len(); + match rng.below(6) { + 0 if len > 0 => { + let at = rng.below(len); + let span = rng.below(64).min(len - at); + data.drain(at..at + span); + } + 1 => { + let at = rng.below(len + 1); + let noise = NOISE[rng.below(NOISE.len())]; + data.splice(at..at, noise.iter().copied()); + } + 2 if len > 0 => { + let at = rng.below(len); + data[at] = (rng.next() & 0xff) as u8; + } + 3 if len > 2 => { + let a = rng.below(len); + let b = rng.below(len); + let (lo, hi) = (a.min(b), a.max(b)); + let chunk: Vec = data[lo..hi.min(lo + 200)].to_vec(); + let at = rng.below(data.len() + 1); + data.splice(at..at, chunk); + } + 4 if len > 0 => data.truncate(rng.below(len)), + _ => { + let at = rng.below(len + 1); + let digit = b'0' + (rng.below(10) as u8); + data.insert(at, digit); + } + } + } + data +} + +fn fixture(name: &str) -> Vec { + let path = format!("{}/tests/fixtures/{name}", env!("CARGO_MANIFEST_DIR")); + std::fs::read(&path).unwrap_or_else(|e| panic!("{path}: {e}")) +} + +fn utc(s: &str) -> DateTime { + s.parse().unwrap_or_else(|e| panic!("{s}: {e}")) +} + +#[test] +fn mutated_feeds_never_panic() { + let window = utc("2025-06-01T00:00:00Z")..utc("2027-06-01T00:00:00Z"); + let opts = ParseOptions { + max_instances_per_series: 200, + default_zone: Tz::Europe__Berlin, + ..ParseOptions::default() + }; + let started = std::time::Instant::now(); + let mut rng = Rng(0x9e37_79b9_7f4a_7c15); + let mut produced = 0usize; + for name in [ + "google_export.ics", + "outlook_export.ics", + "apple_export.ics", + "malformed.ics", + ] { + let original = fixture(name); + for _ in 0..150 { + let bytes = mutate(&mut rng, &original); + let cal = parse_calendar_bytes(&bytes, &opts); + let events = expand(&cal, window.clone(), &["me@acme.example".to_string()]); + for event in &events { + assert!(event.end >= event.start, "{name}: {event:?}"); + assert!(event.start < window.end && event.end > window.start); + } + produced += events.len(); + let agenda = Agenda::new(events.clone(), &Filter::default()); + let now = utc("2026-03-10T09:00:00Z"); + let _ = agenda.current(now); + let _ = agenda.next(now); + let _ = due_reminders(&events, now, Duration::seconds(60), &Default::default()); + let _ = next_wake(&events, now, Duration::seconds(60)); + let text = String::from_utf8_lossy(&bytes); + let _ = parse_calendar(&text, &opts); + let _ = find_meeting_link(&text); + } + } + assert!(produced > 0); + assert!(started.elapsed() < std::time::Duration::from_secs(120)); +} + +#[test] +fn mutated_provider_json_never_panics() { + let mut rng = Rng(0x2545_f491_4f6c_dd1d); + for name in ["google_events_page1.json", "google_calendar_list.json"] { + let original = fixture(name); + for _ in 0..300 { + let text = String::from_utf8_lossy(&mutate(&mut rng, &original)).into_owned(); + let _ = parse_google_events(&text, "primary"); + let _ = cap_calendar::google::parse_google_calendar_list(&text); + } + } + for name in ["graph_calendar_view.json", "graph_calendars.json"] { + let original = fixture(name); + for _ in 0..300 { + let text = String::from_utf8_lossy(&mutate(&mut rng, &original)).into_owned(); + let _ = parse_graph_events(&text, "cal"); + let _ = cap_calendar::graph::parse_graph_calendars(&text); + } + } +} + +#[test] +fn random_bytes_and_strings_never_panic() { + let mut rng = Rng(0xdead_beef_cafe_f00d); + let seeds: &[&[u8]] = &[ + b"code=abc&state=xyz", + br#"{"access_token":"a","expires_in":"12","id_token":"a.eyJlbWFpbCI6ImFAYi5jIn0.c"}"#, + b"https://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fzoom.us%2Fj%2F1234567890", + b"Join", + b"https://urldefense.proofpoint.com/v2/url?u=https-3A__zoom.us_j_1234567890-", + ]; + for _ in 0..400 { + let seed = seeds[rng.below(seeds.len())]; + let text = String::from_utf8_lossy(&mutate(&mut rng, seed)).into_owned(); + let _ = parse_redirect(&text, "xyz"); + let _ = parse_token_response(&text); + let _ = find_meeting_link(&text); + let _ = cap_calendar::classify_url(&text); + } + for _ in 0..200 { + let len = rng.below(400); + let bytes: Vec = (0..len).map(|_| (rng.next() & 0xff) as u8).collect(); + let cal = parse_calendar_bytes(&bytes, &ParseOptions::default()); + let _ = expand( + &cal, + utc("2026-01-01T00:00:00Z")..utc("2027-01-01T00:00:00Z"), + &[], + ); + let _ = find_meeting_link(&String::from_utf8_lossy(&bytes)); + } +} diff --git a/apps/desktop-gpui/calendar/tests/hostile.rs b/apps/desktop-gpui/calendar/tests/hostile.rs new file mode 100644 index 00000000000..6482b0ac210 --- /dev/null +++ b/apps/desktop-gpui/calendar/tests/hostile.rs @@ -0,0 +1,227 @@ +use std::alloc::{GlobalAlloc, Layout, System}; +use std::sync::Mutex; +use std::sync::atomic::{AtomicUsize, Ordering}; +use std::time::{Duration, Instant}; + +use cap_calendar::Tz; +use cap_calendar::ics::{ParseOptions, expand, parse_calendar}; +use chrono::{DateTime, Utc}; + +struct Counting; + +static LIVE: AtomicUsize = AtomicUsize::new(0); +static PEAK: AtomicUsize = AtomicUsize::new(0); +static SERIAL: Mutex<()> = Mutex::new(()); + +unsafe impl GlobalAlloc for Counting { + unsafe fn alloc(&self, layout: Layout) -> *mut u8 { + let ptr = unsafe { System.alloc(layout) }; + if !ptr.is_null() { + let live = LIVE.fetch_add(layout.size(), Ordering::Relaxed) + layout.size(); + PEAK.fetch_max(live, Ordering::Relaxed); + } + ptr + } + + unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { + unsafe { System.dealloc(ptr, layout) }; + LIVE.fetch_sub(layout.size(), Ordering::Relaxed); + } +} + +#[global_allocator] +static ALLOCATOR: Counting = Counting; + +const MB: usize = 1024 * 1024; + +fn utc(s: &str) -> DateTime { + s.parse().unwrap() +} + +fn feed(n: usize, body: impl Fn(usize) -> String) -> String { + let mut s = String::from("BEGIN:VCALENDAR\r\nVERSION:2.0\r\n"); + for i in 0..n { + s.push_str(&body(i)); + } + s.push_str("END:VCALENDAR\r\n"); + s +} + +fn vevent(i: usize, lines: &str) -> String { + format!("BEGIN:VEVENT\r\nUID:e{i}\r\nSUMMARY:Event {i}\r\n{lines}END:VEVENT\r\n") +} + +struct Run { + events: usize, + elapsed: Duration, + peak_bytes: usize, +} + +fn run(text: &str) -> Run { + let opts = ParseOptions { + default_zone: Tz::UTC, + ..ParseOptions::default() + }; + let baseline = LIVE.load(Ordering::Relaxed); + PEAK.store(baseline, Ordering::Relaxed); + let started = Instant::now(); + let cal = parse_calendar(text, &opts); + let events = expand( + &cal, + utc("2026-01-01T00:00:00Z")..utc("2026-03-01T00:00:00Z"), + &[], + ); + let elapsed = started.elapsed(); + let peak_bytes = PEAK.load(Ordering::Relaxed).saturating_sub(baseline); + Run { + events: events.len(), + elapsed, + peak_bytes, + } +} + +fn check(name: &str, text: &str, max_events: usize, max_peak: usize, max_time: Duration) { + let _serial = SERIAL.lock().unwrap_or_else(|e| e.into_inner()); + let r = run(text); + eprintln!( + "{name}: {} KB feed, {} events, {:?}, peak {} MB", + text.len() / 1024, + r.events, + r.elapsed, + r.peak_bytes / MB + ); + assert!(r.events <= max_events, "{name}: {} events", r.events); + assert!( + r.peak_bytes <= max_peak, + "{name}: peak {} MB", + r.peak_bytes / MB + ); + assert!(r.elapsed <= max_time, "{name}: took {:?}", r.elapsed); +} + +#[test] +fn feed_wide_event_cap_bounds_memory() { + let text = feed(2_000, |i| { + vevent(i, "DTSTART:20260101T000000Z\r\nRRULE:FREQ=HOURLY\r\n") + }); + let _serial = SERIAL.lock().unwrap_or_else(|e| e.into_inner()); + let r = run(&text); + eprintln!( + "hourly x2000: {} KB feed, {} events, {:?}, peak {} MB", + text.len() / 1024, + r.events, + r.elapsed, + r.peak_bytes / MB + ); + assert_eq!(r.events, ParseOptions::default().max_events); + assert!(r.peak_bytes < 160 * MB, "peak {} MB", r.peak_bytes / MB); +} + +#[test] +fn sub_hourly_rules_far_before_the_window_are_cheap() { + let secondly = feed(2_000, |i| { + vevent(i, "DTSTART:20000101T000000Z\r\nRRULE:FREQ=SECONDLY\r\n") + }); + check( + "secondly x2000", + &secondly, + 2_000, + 32 * MB, + Duration::from_millis(1_500), + ); + let minutely = feed(2_000, |i| { + vevent(i, "DTSTART:20260101T000000Z\r\nRRULE:FREQ=MINUTELY\r\n") + }); + check( + "minutely x2000", + &minutely, + 2_000, + 32 * MB, + Duration::from_millis(1_500), + ); +} + +#[test] +fn hostile_vtimezones_are_bounded() { + let secondly = feed(2_000, |i| { + format!( + "BEGIN:VTIMEZONE\r\nTZID:Zone {i}\r\nBEGIN:STANDARD\r\nDTSTART:20000101T000000\r\nTZOFFSETFROM:+0317\r\nTZOFFSETTO:+0217\r\nRRULE:FREQ=SECONDLY\r\nEND:STANDARD\r\nBEGIN:DAYLIGHT\r\nDTSTART:20000101T000000\r\nTZOFFSETFROM:+0217\r\nTZOFFSETTO:+0317\r\nRRULE:FREQ=HOURLY;BYMONTH=2;BYMONTHDAY=30\r\nEND:DAYLIGHT\r\nEND:VTIMEZONE\r\n{}", + vevent(i, &format!("DTSTART;TZID=Zone {i}:20260110T090000\r\n")) + ) + }); + check( + "vtimezone x2000", + &secondly, + 2_000, + 64 * MB, + Duration::from_millis(1_500), + ); + let rules = feed(2_000, |i| { + format!( + "BEGIN:VTIMEZONE\r\nTZID:Custom {i}\r\nBEGIN:STANDARD\r\nDTSTART:16010101T020000\r\nTZOFFSETFROM:-0400\r\nTZOFFSETTO:-0500\r\nRRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=11\r\nEND:STANDARD\r\nBEGIN:DAYLIGHT\r\nDTSTART:16010101T020000\r\nTZOFFSETFROM:-0500\r\nTZOFFSETTO:-0400\r\nRRULE:FREQ=YEARLY;BYDAY=2SU;BYMONTH=3\r\nEND:DAYLIGHT\r\nEND:VTIMEZONE\r\n" + ) + }); + check( + "rule vtimezone x2000", + &rules, + 0, + 64 * MB, + Duration::from_millis(1_500), + ); +} + +#[test] +fn rules_that_never_match_share_one_budget() { + for freq in ["YEARLY", "MONTHLY", "WEEKLY", "DAILY", "HOURLY"] { + let text = feed(2_000, |i| { + vevent( + i, + &format!( + "DTSTART:20250101T090000Z\r\nRRULE:FREQ={freq};BYMONTH=2;BYMONTHDAY=30\r\n" + ), + ) + }); + check( + &format!("impossible {freq} x2000"), + &text, + 2_000, + 64 * MB, + Duration::from_millis(1_500), + ); + } + let counted = feed(2_000, |i| { + vevent( + i, + "DTSTART:10000101T090000Z\r\nRRULE:FREQ=DAILY;COUNT=4000000000\r\n", + ) + }); + check( + "counted from year 1000 x2000", + &counted, + 2_000, + 64 * MB, + Duration::from_millis(1_500), + ); +} + +#[test] +fn megabytes_of_mixed_hostile_input_finish_quickly() { + let shapes = [ + "DTSTART:20000101T000000Z\r\nRRULE:FREQ=SECONDLY\r\n", + "DTSTART:20250101T090000Z\r\nRRULE:FREQ=MONTHLY;BYMONTH=4;BYMONTHDAY=31\r\n", + "DTSTART:20250101T090000Z\r\nRRULE:FREQ=HOURLY;BYMONTH=2;BYMONTHDAY=30\r\n", + "DTSTART:10000101T090000Z\r\nRRULE:FREQ=DAILY;COUNT=4000000000\r\n", + "DTSTART:20260101T000000Z\r\nRRULE:FREQ=HOURLY\r\n", + "DTSTART;TZID=Nowhere/Unknown:20260105T090000\r\nRRULE:FREQ=WEEKLY\r\n", + "DTSTART:20260105T090000Z\r\nRRULE:FREQ=YEARLY;BYMONTH=1,2,3,4,5,6,7,8,9,10,11,12;BYMONTHDAY=1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28;BYHOUR=0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23;BYMINUTE=0,15,30,45\r\n", + ]; + let text = feed(30_000, |i| vevent(i, shapes[i % shapes.len()])); + assert!(text.len() > 2 * MB, "{}", text.len()); + check( + "mixed", + &text, + ParseOptions::default().max_events, + 256 * MB, + Duration::from_millis(3_000), + ); +} diff --git a/apps/desktop-gpui/calendar/tests/ics.rs b/apps/desktop-gpui/calendar/tests/ics.rs new file mode 100644 index 00000000000..64908cc1dc7 --- /dev/null +++ b/apps/desktop-gpui/calendar/tests/ics.rs @@ -0,0 +1,961 @@ +use std::ops::Range; + +use cap_calendar::ics::{ParseOptions, ParsedCalendar, ZoneResolution, expand, parse_calendar}; +use cap_calendar::{Agenda, CalendarEvent, EventStatus, Filter, Provider, Response, Tz}; +use chrono::{DateTime, Duration, NaiveDate, Utc}; + +fn utc(s: &str) -> DateTime { + s.parse().unwrap_or_else(|e| panic!("{s}: {e}")) +} + +fn fixture(name: &str) -> String { + let path = format!("{}/tests/fixtures/{name}", env!("CARGO_MANIFEST_DIR")); + std::fs::read_to_string(&path).unwrap_or_else(|e| panic!("{path}: {e}")) +} + +fn window(start: &str, end: &str) -> Range> { + utc(start)..utc(end) +} + +fn opts(zone: Tz) -> ParseOptions { + ParseOptions { + calendar_id: "feed".to_string(), + default_zone: zone, + ..ParseOptions::default() + } +} + +fn run( + text: &str, + zone: Tz, + w: Range>, + me: &str, +) -> (ParsedCalendar, Vec) { + let cal = parse_calendar(text, &opts(zone)); + let events = expand(&cal, w, &[me.to_string()]); + (cal, events) +} + +fn starts(events: &[CalendarEvent], title_prefix: &str) -> Vec> { + events + .iter() + .filter(|e| e.title.starts_with(title_prefix)) + .map(|e| e.start) + .collect() +} + +fn one<'a>(events: &'a [CalendarEvent], title: &str) -> &'a CalendarEvent { + let found: Vec<&CalendarEvent> = events.iter().filter(|e| e.title == title).collect(); + assert_eq!(found.len(), 1, "{title}"); + found[0] +} + +fn wrap(body: &str) -> String { + format!("BEGIN:VCALENDAR\r\nVERSION:2.0\r\n{body}END:VCALENDAR\r\n") +} + +fn event(lines: &str) -> String { + wrap(&format!("BEGIN:VEVENT\r\n{lines}END:VEVENT\r\n")) +} + +#[test] +fn google_export() { + let (cal, events) = run( + &fixture("google_export.ics"), + Tz::UTC, + window("2026-02-20T00:00:00Z", "2026-04-10T00:00:00Z"), + "me@acme.example", + ); + assert_eq!(cal.name.as_deref(), Some("Acme Work")); + assert_eq!(cal.time_zone.as_deref(), Some("America/New_York")); + assert_eq!(cal.warnings, 0, "{:?}", cal.warning_messages); + assert_eq!(events.len(), 14); + + let sync = starts(&events, "Product sync"); + assert_eq!( + sync, + vec![ + utc("2026-02-23T14:00:00Z"), + utc("2026-03-02T14:00:00Z"), + utc("2026-03-09T13:00:00Z"), + utc("2026-03-23T15:00:00Z"), + utc("2026-04-06T13:00:00Z"), + ] + ); + let first = &events[0]; + assert_eq!( + first.id, + "7kukuqrfedlm2f9t6vo2mh1ln5@google.com_20260223T140000Z" + ); + assert_eq!( + first.series_id.as_deref(), + Some("7kukuqrfedlm2f9t6vo2mh1ln5@google.com") + ); + assert_eq!(first.end - first.start, Duration::minutes(30)); + assert_eq!(first.calendar_id, "feed"); + assert_eq!(first.self_response, Response::Accepted); + assert_eq!(first.attendees.len(), 2); + assert_eq!( + first.organizer.as_ref().and_then(|o| o.name.as_deref()), + Some("Alice Chen") + ); + assert_eq!(first.location.as_deref(), Some("NYC-4-Hudson (8)")); + let description = first.description.as_deref().unwrap(); + assert!(description.starts_with("Weekly product sync, bring updates; demos welcome.\n\n")); + assert!(description.contains("More phone numbers: https://tel.meet/")); + let link = first.link.as_ref().unwrap(); + assert_eq!(link.provider, Provider::GoogleMeet); + assert_eq!(link.meeting_id.as_deref(), Some("abc-defg-hij")); + + let moved = one(&events, "Product sync (moved)"); + assert_eq!( + moved.id, + "7kukuqrfedlm2f9t6vo2mh1ln5@google.com_20260323T130000Z" + ); + assert_eq!(moved.self_response, Response::Tentative); + assert!(events.iter().all(|e| !e.id.ends_with("20260316T130000Z"))); + assert!(events.iter().all(|e| !e.id.ends_with("20260330T130000Z"))); + + let offsite = one(&events, "Team offsite"); + assert!(offsite.all_day && offsite.free); + assert_eq!(offsite.start, utc("2026-03-10T00:00:00Z")); + assert_eq!(offsite.end, utc("2026-03-12T00:00:00Z")); + + let call = one(&events, "Customer call: Globex"); + let link = call.link.as_ref().unwrap(); + assert_eq!(link.url, "https://acme.zoom.us/j/84512345678?pwd=aBcDeF"); + assert_eq!(link.meeting_id.as_deref(), Some("84512345678")); + assert_eq!(call.self_response, Response::Accepted); + assert_eq!(call.location, None); + assert_eq!(call.end - call.start, Duration::minutes(45)); + + let declined = one(&events, "All hands"); + assert_eq!(declined.self_response, Response::Declined); + assert_eq!(declined.start, utc("2026-03-05T20:00:00Z")); + assert_eq!( + declined.link.as_ref().and_then(|l| l.meeting_id.as_deref()), + Some("19:meeting_YmFkZ2Vy@thread.v2") + ); + + let focus = starts(&events, "Focus block"); + assert_eq!(focus.len(), 5); + assert_eq!(focus[0], utc("2026-03-09T12:00:00Z")); + assert_eq!(focus[4], utc("2026-03-13T12:00:00Z")); + + let chat = one(&events, "Quick chat"); + assert_eq!(chat.start, utc("2026-03-06T17:00:00Z")); + assert_eq!(chat.end, utc("2026-03-06T17:30:00Z")); + assert_eq!( + chat.link.as_ref().map(|l| l.provider), + Some(Provider::Whereby) + ); + assert_eq!( + chat.web_url.as_deref(), + Some("https://whereby.com/acme-standup") + ); + + let mut sorted = events.clone(); + sorted.sort_by_key(|e| e.start); + assert_eq!( + sorted.iter().map(|e| &e.id).collect::>(), + events.iter().map(|e| &e.id).collect::>() + ); +} + +#[test] +fn cancelled_instances_can_be_kept_on_request() { + let cal = parse_calendar( + &fixture("google_export.ics"), + &ParseOptions { + include_cancelled: true, + ..opts(Tz::UTC) + }, + ); + let events = expand( + &cal, + window("2026-03-29T00:00:00Z", "2026-03-31T00:00:00Z"), + &[], + ); + let cancelled: Vec<_> = events + .iter() + .filter(|e| e.status == EventStatus::Cancelled) + .collect(); + assert_eq!(cancelled.len(), 1); + assert_eq!( + cancelled[0].id, + "7kukuqrfedlm2f9t6vo2mh1ln5@google.com_20260330T130000Z" + ); +} + +#[test] +fn outlook_export() { + let (cal, events) = run( + &fixture("outlook_export.ics"), + Tz::UTC, + window("2026-01-01T00:00:00Z", "2026-07-01T00:00:00Z"), + "me@contoso.example", + ); + assert_eq!(cal.warnings, 0, "{:?}", cal.warning_messages); + let custom = cal + .zones + .iter() + .find(|z| z.tzid == "Customized Time Zone") + .unwrap(); + assert_eq!(custom.how, ZoneResolution::MatchedRules); + assert_eq!(custom.zone_name, "Europe/London"); + let pacific = cal + .zones + .iter() + .find(|z| z.tzid == "Pacific Standard Time") + .unwrap(); + assert_eq!(pacific.how, ZoneResolution::Named); + assert_eq!(pacific.zone_name, "America/Los_Angeles"); + + let review = one(&events, "Design review"); + assert_eq!(review.start, utc("2026-03-10T17:00:00Z")); + assert_eq!(review.end, utc("2026-03-10T18:00:00Z")); + assert_eq!(review.self_response, Response::Tentative); + assert_eq!(review.location.as_deref(), Some("Microsoft Teams Meeting")); + let link = review.link.as_ref().unwrap(); + assert_eq!(link.provider, Provider::Teams); + assert_eq!( + link.meeting_id.as_deref(), + Some("19:meeting_NjE2ODk3@thread.v2") + ); + assert!( + review + .description + .as_deref() + .unwrap() + .contains("Passcode: Xy7pQ2") + ); + assert_eq!(review.attendees.len(), 2); + assert_eq!(review.attendees[1].name.as_deref(), Some("Ruiz, Dana")); + + assert_eq!( + starts(&events, "Board review"), + vec![ + utc("2026-01-13T14:00:00Z"), + utc("2026-02-10T14:00:00Z"), + utc("2026-03-10T14:00:00Z"), + utc("2026-04-14T13:00:00Z"), + utc("2026-05-12T13:00:00Z"), + utc("2026-06-09T13:00:00Z"), + ] + ); + let board = events.iter().find(|e| e.title == "Board review").unwrap(); + assert_eq!(board.end - board.start, Duration::minutes(90)); + let link = board.link.as_ref().unwrap(); + assert_eq!(link.url, "https://us06web.zoom.us/j/87654321098?pwd=QwErTy"); + + let holiday = one(&events, "Company holiday"); + assert!(holiday.all_day && holiday.free); + assert_eq!(holiday.start, utc("2026-03-20T00:00:00Z")); + let sick = one(&events, "Out sick"); + assert!(sick.all_day); + assert_eq!(sick.start, utc("2026-03-25T00:00:00Z")); + assert_eq!(sick.end, utc("2026-03-26T00:00:00Z")); + + assert_eq!( + starts(&events, "1:1 Priya"), + vec![ + utc("2026-03-19T09:30:00Z"), + utc("2026-04-02T08:30:00Z"), + utc("2026-04-16T08:30:00Z"), + utc("2026-04-30T08:30:00Z"), + ] + ); + let one_on_one = events.iter().find(|e| e.title.starts_with("1:1")).unwrap(); + assert_eq!( + one_on_one.link.as_ref().map(|l| l.provider), + Some(Provider::Webex) + ); + + assert_eq!( + starts(&events, "Annual planning"), + vec![utc("2026-06-15T17:00:00Z")] + ); +} + +#[test] +fn os_export_with_southern_hemisphere_dst() { + let (cal, events) = run( + &fixture("apple_export.ics"), + Tz::UTC, + window("2026-01-01T00:00:00Z", "2027-01-01T08:00:00Z"), + "", + ); + assert_eq!(cal.name.as_deref(), Some("Home")); + assert_eq!(cal.warnings, 0, "{:?}", cal.warning_messages); + let custom = cal + .zones + .iter() + .find(|z| z.tzid == "Sydney Custom") + .unwrap(); + assert_eq!(custom.how, ZoneResolution::MatchedRules); + assert_eq!(custom.zone_name, "Australia/Sydney"); + + assert_eq!( + starts(&events, "Sydney standup"), + vec![ + utc("2026-03-26T22:00:00Z"), + utc("2026-04-02T22:00:00Z"), + utc("2026-04-09T23:00:00Z"), + utc("2026-04-16T23:00:00Z"), + ] + ); + assert_eq!( + starts(&events, "Spring check-in"), + vec![ + utc("2026-09-28T00:00:00Z"), + utc("2026-10-04T23:00:00Z"), + utc("2026-10-11T23:00:00Z"), + ] + ); + let checkin = events + .iter() + .find(|e| e.title == "Spring check-in") + .unwrap(); + assert_eq!( + checkin.link.as_ref().map(|l| l.provider), + Some(Provider::Zoom) + ); + + assert_eq!( + starts(&events, "Family call"), + vec![ + utc("2026-04-01T18:00:00Z"), + utc("2026-04-03T18:00:00Z"), + utc("2026-04-15T18:00:00Z"), + utc("2026-04-29T18:00:00Z"), + ] + ); + let call = events.iter().find(|e| e.title == "Family call").unwrap(); + assert_eq!( + call.link.as_ref().map(|l| l.url.as_str()), + Some("https://facetime.apple.com/join#v=1&p=AbCdEfGh123&k=XyZ987") + ); + + let thanksgiving = one(&events, "Thanksgiving"); + assert!(thanksgiving.all_day); + assert_eq!(thanksgiving.start, utc("2026-11-26T00:00:00Z")); + assert_eq!(thanksgiving.id, "THANKSGIVING-US_20261126"); + + assert_eq!( + starts(&events, "Month end close"), + vec![ + utc("2026-01-31T20:00:00Z"), + utc("2026-03-31T19:00:00Z"), + utc("2026-05-31T19:00:00Z"), + utc("2026-07-31T19:00:00Z"), + utc("2026-08-31T19:00:00Z"), + utc("2026-10-31T19:00:00Z"), + utc("2026-12-31T20:00:00Z"), + ] + ); + let last_workdays: Vec = events + .iter() + .filter(|e| e.title == "Last workday drinks") + .map(|e| { + e.start + .with_timezone(&Tz::America__Los_Angeles) + .date_naive() + }) + .collect(); + let expected: Vec = [ + (1, 30), + (2, 27), + (3, 31), + (4, 30), + (5, 29), + (6, 30), + (7, 31), + (8, 31), + (9, 30), + (10, 30), + (11, 30), + (12, 31), + ] + .iter() + .map(|(m, d)| NaiveDate::from_ymd_opt(2026, *m, *d).unwrap()) + .collect(); + assert_eq!(last_workdays, expected); +} + +#[test] +fn malformed_feed_never_panics_and_keeps_good_events() { + let (cal, events) = run( + &fixture("malformed.ics"), + Tz::Europe__Paris, + window("2026-03-01T00:00:00Z", "2026-04-01T00:00:00Z"), + "", + ); + assert_eq!(cal.name.as_deref(), Some("Broken, but alive")); + assert!( + cal.warnings >= 10, + "{} {:?}", + cal.warnings, + cal.warning_messages + ); + let ids: Vec<&str> = events.iter().map(|e| e.id.as_str()).collect(); + for (index, uid) in [ + "bad-rule", + "unknown-tz", + "negative", + "backwards", + "until-before", + "dupe", + "dupe#2", + "quoted", + "unclosed", + ] + .into_iter() + .enumerate() + { + assert!(ids.contains(&uid), "missing expected event {index}"); + } + assert!(!ids.contains(&"no-start")); + assert!(!ids.contains(&"bad-date")); + let seconds: Vec<&CalendarEvent> = events + .iter() + .filter(|e| e.title == "Runaway rule") + .collect(); + assert_eq!(seconds.len(), 1); + assert_eq!(seconds[0].start, utc("2026-03-16T00:00:00Z")); + assert!( + cal.warning_messages + .iter() + .any(|w| w.contains("FREQ=SECONDLY")), + "{:?}", + cal.warning_messages + ); + + let unknown = events.iter().find(|e| e.id == "unknown-tz").unwrap(); + assert_eq!(unknown.start, utc("2026-03-11T08:00:00Z")); + let negative = events.iter().find(|e| e.id == "negative").unwrap(); + assert_eq!(negative.end - negative.start, Duration::minutes(30)); + let backwards = events.iter().find(|e| e.id == "backwards").unwrap(); + assert_eq!(backwards.end - backwards.start, Duration::minutes(30)); + let quoted = events.iter().find(|e| e.id == "quoted").unwrap(); + assert!(quoted.attendees.is_empty()); + assert_eq!( + quoted.organizer.as_ref().and_then(|o| o.email.as_deref()), + Some("fine@example.com") + ); +} + +#[test] +fn recurrence_shapes() { + let w = window("2026-01-01T00:00:00Z", "2027-01-01T00:00:00Z"); + let cases: &[(&str, &str, &[&str])] = &[ + ( + "daily interval", + "DTSTART:20260105T100000Z\r\nRRULE:FREQ=DAILY;INTERVAL=3;COUNT=4\r\n", + &[ + "2026-01-05T10:00:00Z", + "2026-01-08T10:00:00Z", + "2026-01-11T10:00:00Z", + "2026-01-14T10:00:00Z", + ], + ), + ( + "weekly byday until date", + "DTSTART;VALUE=DATE:20260106\r\nRRULE:FREQ=WEEKLY;BYDAY=TU,TH;UNTIL=20260115\r\n", + &[ + "2026-01-06T00:00:00Z", + "2026-01-08T00:00:00Z", + "2026-01-13T00:00:00Z", + "2026-01-15T00:00:00Z", + ], + ), + ( + "monthly last day", + "DTSTART:20260131T090000Z\r\nRRULE:FREQ=MONTHLY;BYMONTHDAY=-1;COUNT=3\r\n", + &[ + "2026-01-31T09:00:00Z", + "2026-02-28T09:00:00Z", + "2026-03-31T09:00:00Z", + ], + ), + ( + "monthly by day of month", + "DTSTART:20260115T090000Z\r\nRRULE:FREQ=MONTHLY;COUNT=3\r\n", + &[ + "2026-01-15T09:00:00Z", + "2026-02-15T09:00:00Z", + "2026-03-15T09:00:00Z", + ], + ), + ( + "monthly nth weekday from the end", + "DTSTART:20260126T170000Z\r\nRRULE:FREQ=MONTHLY;BYDAY=-1MO;COUNT=3\r\n", + &[ + "2026-01-26T17:00:00Z", + "2026-02-23T17:00:00Z", + "2026-03-30T17:00:00Z", + ], + ), + ( + "yearly leap day", + "DTSTART:20240229T120000Z\r\nRRULE:FREQ=YEARLY;BYMONTH=2;BYMONTHDAY=29\r\n", + &[], + ), + ( + "yearly", + "DTSTART:20200704T160000Z\r\nRRULE:FREQ=YEARLY\r\n", + &["2026-07-04T16:00:00Z"], + ), + ( + "exdate as date on timed series", + "DTSTART:20260105T100000Z\r\nRRULE:FREQ=DAILY;COUNT=3\r\nEXDATE;VALUE=DATE:20260106\r\n", + &["2026-01-05T10:00:00Z", "2026-01-07T10:00:00Z"], + ), + ( + "exdate list with utc instants on zoned series", + "DTSTART;TZID=Europe/Berlin:20260105T100000\r\nRRULE:FREQ=DAILY;COUNT=4\r\nEXDATE:20260106T090000Z,20260107T090000Z\r\n", + &["2026-01-05T09:00:00Z", "2026-01-08T09:00:00Z"], + ), + ( + "rdate period", + "DTSTART:20260105T100000Z\r\nRDATE;VALUE=PERIOD:20260110T150000Z/20260110T160000Z\r\n", + &["2026-01-05T10:00:00Z", "2026-01-10T15:00:00Z"], + ), + ( + "all day exdate", + "DTSTART;VALUE=DATE:20260105\r\nRRULE:FREQ=DAILY;COUNT=3\r\nEXDATE;VALUE=DATE:20260106\r\n", + &["2026-01-05T00:00:00Z", "2026-01-07T00:00:00Z"], + ), + ( + "dtstart off pattern still counts", + "DTSTART:20260105T100000Z\r\nRRULE:FREQ=WEEKLY;BYDAY=WE;COUNT=2\r\n", + &[ + "2026-01-05T10:00:00Z", + "2026-01-07T10:00:00Z", + "2026-01-14T10:00:00Z", + ], + ), + ]; + for (name, lines, expected) in cases { + let text = event(&format!("UID:{name}\r\nSUMMARY:{name}\r\n{lines}")); + let (cal, events) = run(&text, Tz::UTC, w.clone(), ""); + assert_eq!(cal.warnings, 0, "{name}: {:?}", cal.warning_messages); + let expected: Vec> = expected.iter().map(|s| utc(s)).collect(); + assert_eq!(starts(&events, name), expected, "{name}"); + } + + let leap = event( + "UID:leap\r\nSUMMARY:leap\r\nDTSTART:20240229T120000Z\r\nRRULE:FREQ=YEARLY;BYMONTH=2;BYMONTHDAY=29\r\n", + ); + let (_, events) = run( + &leap, + Tz::UTC, + window("2024-01-01T00:00:00Z", "2033-01-01T00:00:00Z"), + "", + ); + assert_eq!( + starts(&events, "leap"), + vec![ + utc("2024-02-29T12:00:00Z"), + utc("2028-02-29T12:00:00Z"), + utc("2032-02-29T12:00:00Z") + ] + ); +} + +#[test] +fn overrides_move_instances_across_the_window_edge() { + let text = wrap( + "BEGIN:VEVENT\r\nUID:s1\r\nSUMMARY:Series\r\nDTSTART:20260105T100000Z\r\nDTEND:20260105T110000Z\r\nRRULE:FREQ=DAILY;COUNT=10\r\nEND:VEVENT\r\n\ +BEGIN:VEVENT\r\nUID:s1\r\nSUMMARY:Pulled in\r\nRECURRENCE-ID:20260112T100000Z\r\nDTSTART:20260107T150000Z\r\nDTEND:20260107T160000Z\r\nEND:VEVENT\r\n\ +BEGIN:VEVENT\r\nUID:s1\r\nSUMMARY:Pushed out\r\nRECURRENCE-ID:20260106T100000Z\r\nDTSTART:20260120T100000Z\r\nDTEND:20260120T110000Z\r\nEND:VEVENT\r\n\ +BEGIN:VEVENT\r\nUID:orphan\r\nSUMMARY:Only instance\r\nRECURRENCE-ID;TZID=America/New_York:20260108T090000\r\nDTSTART;TZID=America/New_York:20260108T093000\r\nEND:VEVENT\r\n", + ); + let (_, events) = run( + &text, + Tz::UTC, + window("2026-01-05T00:00:00Z", "2026-01-09T00:00:00Z"), + "", + ); + let titles: Vec<(&str, &str)> = events + .iter() + .map(|e| (e.title.as_str(), e.id.as_str())) + .collect(); + assert_eq!( + titles, + vec![ + ("Series", "s1_20260105T100000Z"), + ("Series", "s1_20260107T100000Z"), + ("Pulled in", "s1_20260112T100000Z"), + ("Series", "s1_20260108T100000Z"), + ("Only instance", "orphan_20260108T140000Z"), + ] + ); + let cancelled_series = wrap( + "BEGIN:VEVENT\r\nUID:gone\r\nSUMMARY:Gone\r\nSTATUS:CANCELLED\r\nDTSTART:20260105T100000Z\r\nRRULE:FREQ=DAILY;COUNT=5\r\nEND:VEVENT\r\n\ +BEGIN:VEVENT\r\nUID:gone\r\nSUMMARY:Gone moved\r\nRECURRENCE-ID:20260106T100000Z\r\nDTSTART:20260106T120000Z\r\nEND:VEVENT\r\n", + ); + let (_, gone) = run( + &cancelled_series, + Tz::UTC, + window("2026-01-05T00:00:00Z", "2026-01-09T00:00:00Z"), + "", + ); + assert!(gone.is_empty(), "{gone:?}"); + let orphan = events.iter().find(|e| e.id.starts_with("orphan")).unwrap(); + assert_eq!(orphan.series_id.as_deref(), Some("orphan")); + assert_eq!(orphan.start, utc("2026-01-08T14:30:00Z")); +} + +#[test] +fn dst_in_both_hemispheres_keeps_local_time() { + let cases: &[(&str, &str, &[&str])] = &[ + ( + "America/New_York", + "20260302T090000", + &[ + "2026-03-02T14:00:00Z", + "2026-03-09T13:00:00Z", + "2026-10-26T13:00:00Z", + "2026-11-02T14:00:00Z", + ], + ), + ( + "Europe/London", + "20260323T090000", + &[ + "2026-03-23T09:00:00Z", + "2026-03-30T08:00:00Z", + "2026-10-19T08:00:00Z", + "2026-10-26T09:00:00Z", + ], + ), + ( + "Pacific/Auckland", + "20260330T080000", + &[ + "2026-03-29T19:00:00Z", + "2026-04-05T20:00:00Z", + "2026-09-20T20:00:00Z", + "2026-09-27T19:00:00Z", + ], + ), + ( + "America/Sao_Paulo", + "20260302T090000", + &["2026-03-02T12:00:00Z", "2026-03-09T12:00:00Z"], + ), + ]; + for (zone, start, expected) in cases { + let text = event(&format!( + "UID:w\r\nSUMMARY:Weekly\r\nDTSTART;TZID={zone}:{start}\r\nDURATION:PT1H\r\nRRULE:FREQ=WEEKLY\r\n" + )); + let (_, events) = run( + &text, + Tz::UTC, + window("2026-01-01T00:00:00Z", "2027-01-01T00:00:00Z"), + "", + ); + let tz: Tz = zone.parse().unwrap(); + for e in &events { + let local = e.start.with_timezone(&tz); + assert_eq!( + local.format("%H:%M").to_string(), + format!("{}:{}", &start[9..11], &start[11..13]), + "{zone}" + ); + assert_eq!(e.end - e.start, Duration::hours(1), "{zone}"); + } + for want in *expected { + assert!( + events.iter().any(|e| e.start == utc(want)), + "{zone} missing {want}" + ); + } + } +} + +#[test] +fn floating_times_use_the_caller_zone_and_gaps_move_forward() { + let text = event("UID:f\r\nSUMMARY:Floating\r\nDTSTART:20260310T090000\r\n"); + let (_, events) = run( + &text, + Tz::Asia__Tokyo, + window("2026-03-01T00:00:00Z", "2026-04-01T00:00:00Z"), + "", + ); + assert_eq!(events[0].start, utc("2026-03-10T00:00:00Z")); + assert_eq!(events[0].end, utc("2026-03-10T00:30:00Z")); + + let gap = event( + "UID:g\r\nSUMMARY:Gap\r\nDTSTART;TZID=America/New_York:20260308T023000\r\nDTEND;TZID=America/New_York:20260308T033000\r\n", + ); + let (_, events) = run( + &gap, + Tz::UTC, + window("2026-03-01T00:00:00Z", "2026-04-01T00:00:00Z"), + "", + ); + assert_eq!(events[0].start, utc("2026-03-08T07:30:00Z")); + assert_eq!(events[0].end, utc("2026-03-08T08:30:00Z")); +} + +#[test] +fn all_day_events_are_zone_independent() { + let text = event( + "UID:a\r\nSUMMARY:Conference\r\nDTSTART;VALUE=DATE:20260415\r\nDTEND;VALUE=DATE:20260417\r\n", + ); + let w = window("2026-04-01T00:00:00Z", "2026-05-01T00:00:00Z"); + let (_, la) = run(&text, Tz::America__Los_Angeles, w.clone(), ""); + let (_, tokyo) = run(&text, Tz::Asia__Tokyo, w, ""); + assert_eq!(la, tokyo); + assert_eq!(la[0].start, utc("2026-04-15T00:00:00Z")); + + let filter = Filter { + hide_all_day: false, + ..Filter::default() + }; + let agenda = Agenda::new(la, &filter); + for tz in [ + Tz::America__Los_Angeles, + Tz::Asia__Tokyo, + Tz::Pacific__Kiritimati, + Tz::Pacific__Pago_Pago, + ] { + let on = |d: u32| { + agenda + .for_day(NaiveDate::from_ymd_opt(2026, 4, d).unwrap(), tz) + .len() + }; + assert_eq!((on(14), on(15), on(16), on(17)), (0, 1, 1, 0), "{tz}"); + } + + let no_end = event("UID:b\r\nSUMMARY:Holiday\r\nDTSTART;VALUE=DATE:20260415\r\n"); + let (_, events) = run( + &no_end, + Tz::UTC, + window("2026-04-01T00:00:00Z", "2026-05-01T00:00:00Z"), + "", + ); + assert_eq!(events[0].end - events[0].start, Duration::days(1)); +} + +#[test] +fn folded_and_escaped_lines() { + let text = "BEGIN:VCALENDAR\nBEGIN:VEVENT\nUID:x\nDTSTART:20260310T090000Z\nSUMMARY:Caf\u{e9} \\, r\u{e9}sum\u{e9}\\; plan\n\t ning \\\\ notes\nDESCRIPTION;LANGUAGE=en:Line one\\nLine two\\NLine three\nLOCATION:Room \"A\"\\, floor 2\nEND:VEVENT\nEND:VCALENDAR"; + let (_, events) = run( + text, + Tz::UTC, + window("2026-03-01T00:00:00Z", "2026-04-01T00:00:00Z"), + "", + ); + assert_eq!( + events[0].title, + "Caf\u{e9} , r\u{e9}sum\u{e9}; plan ning \\ notes" + ); + assert_eq!( + events[0].description.as_deref(), + Some("Line one\nLine two\nLine three") + ); + assert_eq!(events[0].location.as_deref(), Some("Room \"A\", floor 2")); +} + +#[test] +fn folds_inside_multibyte_characters_are_repaired_from_bytes() { + let mut bytes = + b"BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nUID:x\r\nDTSTART:20260310T090000Z\r\nSUMMARY:Caf" + .to_vec(); + bytes.push(0xc3); + bytes.extend_from_slice(b"\r\n "); + bytes.push(0xa9); + bytes.extend_from_slice(b" chat\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n"); + let cal = cap_calendar::ics::parse_calendar_bytes(&bytes, &opts(Tz::UTC)); + let events = expand( + &cal, + window("2026-03-01T00:00:00Z", "2026-04-01T00:00:00Z"), + &[], + ); + assert_eq!(events[0].title, "Caf\u{e9} chat"); +} + +#[test] +fn expansion_is_bounded() { + let daily = event("UID:d\r\nSUMMARY:Daily\r\nDTSTART:19900101T090000Z\r\nRRULE:FREQ=DAILY\r\n"); + let (_, events) = run( + &daily, + Tz::UTC, + window("2026-03-01T00:00:00Z", "2026-04-01T00:00:00Z"), + "", + ); + assert_eq!(events.len(), 31); + + let cal = parse_calendar( + &daily, + &ParseOptions { + max_instances_per_series: 10, + ..opts(Tz::UTC) + }, + ); + let events = expand( + &cal, + window("2026-01-01T00:00:00Z", "2027-01-01T00:00:00Z"), + &[], + ); + assert_eq!(events.len(), 10); + + let started = std::time::Instant::now(); + let (_, far) = run( + &daily, + Tz::UTC, + window("2900-01-01T00:00:00Z", "2900-02-01T00:00:00Z"), + "", + ); + assert!(far.len() <= 31); + assert!(started.elapsed() < std::time::Duration::from_secs(10)); + + let (_, empty) = run( + &daily, + Tz::UTC, + utc("2026-03-02T00:00:00Z")..utc("2026-03-01T00:00:00Z"), + "", + ); + assert!(empty.is_empty()); +} + +#[test] +fn self_detection_and_conference_properties() { + let text = event( + "UID:c\r\nSUMMARY:Teams via property\r\nDTSTART:20260310T090000Z\r\nORGANIZER;CN=Me:mailto:Me@Example.com\r\nATTENDEE;PARTSTAT=ACCEPTED;CN=Other:mailto:other@example.com\r\nX-MICROSOFT-SKYPETEAMSMEETINGURL:https://teams.microsoft.com/l/meetup-join/19%3ameeting_PROP%40thread.v2/0\r\nDESCRIPTION:Backup https://zoom.us/j/1234567890\r\n", + ); + let (_, events) = run( + &text, + Tz::UTC, + window("2026-03-01T00:00:00Z", "2026-04-01T00:00:00Z"), + "me@example.com", + ); + let e = &events[0]; + assert!(e.organizer.as_ref().unwrap().is_self); + assert_eq!(e.self_response, Response::Accepted); + assert_eq!(e.link.as_ref().map(|l| l.provider), Some(Provider::Teams)); + assert!(e.has_participants()); +} + +#[test] +fn events_survive_a_json_round_trip() { + let (_, events) = run( + &fixture("google_export.ics"), + Tz::UTC, + window("2026-02-20T00:00:00Z", "2026-04-10T00:00:00Z"), + "me@acme.example", + ); + let json = serde_json::to_string(&events).unwrap(); + assert!(json.contains("\"selfResponse\":\"accepted\"")); + assert!(json.contains("\"meetingId\":\"abc-defg-hij\"")); + assert!(json.contains("\"provider\":\"google-meet\"")); + let back: Vec = serde_json::from_str(&json).unwrap(); + assert_eq!(back, events); + let partial: CalendarEvent = + serde_json::from_str("{\"id\":\"x\",\"title\":\"Old cache\"}").unwrap(); + assert_eq!(partial.id, "x"); + assert_eq!(partial.self_response, Response::Unknown); +} + +#[test] +fn hourly_series_started_years_ago_has_instances_in_the_window() { + let text = + event("UID:h\r\nSUMMARY:Hourly\r\nDTSTART:20150101T090000Z\r\nRRULE:FREQ=HOURLY\r\n"); + let (_, events) = run( + &text, + Tz::UTC, + window("2026-09-01T00:00:00Z", "2026-09-02T00:00:00Z"), + "", + ); + assert_eq!(events.len(), 24); + assert_eq!(events[0].start, utc("2026-09-01T00:00:00Z")); + assert_eq!(events[0].id, "h_20260901T000000Z"); +} + +#[test] +fn durations_follow_rfc_5545_across_dst() { + let ny = |s: &str| { + NaiveDate::parse_from_str(&s[..8], "%Y%m%d") + .unwrap() + .and_hms_opt(s[9..11].parse().unwrap(), s[11..13].parse().unwrap(), 0) + .unwrap() + .and_local_timezone(Tz::America__New_York) + .unwrap() + .with_timezone(&Utc) + }; + let cases: &[(&str, &str, &str)] = &[ + ("spring nominal day", "20260307T120000", "DURATION:P1D"), + ("spring exact day", "20260307T120000", "DURATION:PT24H"), + ("fall nominal day", "20261031T120000", "DURATION:P1D"), + ("fall exact day", "20261031T120000", "DURATION:PT24H"), + ("fall mixed", "20261031T120000", "DURATION:P1DT1H"), + ]; + let expected_end = [ + ny("20260308T120000"), + ny("20260308T130000"), + ny("20261101T120000"), + ny("20261101T110000"), + ny("20261101T130000"), + ]; + for ((name, start, duration), want) in cases.iter().zip(expected_end) { + let text = event(&format!( + "UID:{name}\r\nSUMMARY:{name}\r\nDTSTART;TZID=America/New_York:{start}\r\n{duration}\r\n" + )); + let (_, events) = run( + &text, + Tz::UTC, + window("2026-01-01T00:00:00Z", "2027-01-01T00:00:00Z"), + "", + ); + let e = one(&events, name); + assert_eq!(e.start, ny(start), "{name}"); + assert_eq!(e.end, want, "{name}"); + } + + let spring = event( + "UID:s\r\nSUMMARY:Late show\r\nDTSTART;TZID=America/New_York:20260307T230000\r\nDTEND;TZID=America/New_York:20260308T040000\r\nRRULE:FREQ=DAILY;COUNT=3\r\n", + ); + let (_, events) = run( + &spring, + Tz::UTC, + window("2026-03-01T00:00:00Z", "2026-03-31T00:00:00Z"), + "", + ); + assert_eq!(events.len(), 3); + assert_eq!(events[0].end, ny("20260308T040000")); + for e in &events { + assert_eq!(e.end - e.start, Duration::hours(4), "{}", e.id); + } + assert_eq!(events[1].end, ny("20260309T030000")); + + let fall = event( + "UID:f\r\nSUMMARY:Night shift\r\nDTSTART;TZID=America/New_York:20261031T220000\r\nDTEND;TZID=America/New_York:20261101T060000\r\nRRULE:FREQ=WEEKLY;COUNT=2\r\n", + ); + let (_, events) = run( + &fall, + Tz::UTC, + window("2026-10-01T00:00:00Z", "2026-12-01T00:00:00Z"), + "", + ); + assert_eq!(events.len(), 2); + for e in &events { + assert_eq!(e.end - e.start, Duration::hours(9), "{}", e.id); + } + assert_eq!(events[1].end, ny("20261108T070000")); + + let all_day = event( + "UID:a\r\nSUMMARY:Offsite\r\nDTSTART;VALUE=DATE:20260307\r\nDTEND;VALUE=DATE:20260309\r\nRRULE:FREQ=WEEKLY;COUNT=2\r\n", + ); + let (_, events) = run( + &all_day, + Tz::UTC, + window("2026-03-01T00:00:00Z", "2026-04-01T00:00:00Z"), + "", + ); + assert_eq!(events.len(), 2); + for e in &events { + assert_eq!(e.end - e.start, Duration::days(2), "{}", e.id); + } +} diff --git a/apps/desktop-gpui/calendar/tests/links.rs b/apps/desktop-gpui/calendar/tests/links.rs new file mode 100644 index 00000000000..86ae32d32dd --- /dev/null +++ b/apps/desktop-gpui/calendar/tests/links.rs @@ -0,0 +1,776 @@ +use std::collections::HashSet; + +use cap_calendar::link::find_all_meeting_links; +use cap_calendar::{ + ConferenceEntry, ConferenceKind, MeetingLink, Provider, classify_url, event_meeting_link, + find_meeting_link, +}; + +fn check(url: &str, provider: Provider, id: Option<&str>) { + let link = classify_url(url).unwrap_or_else(|| panic!("expected {provider:?} for {url}")); + assert_eq!(link.provider, provider, "{url}"); + assert_eq!(link.meeting_id.as_deref(), id, "{url}"); + assert!(link.url.starts_with("https://"), "{url} -> {}", link.url); +} + +fn reject(url: &str) { + assert_eq!(classify_url(url), None, "{url} must not match"); +} + +const POSITIVE: &[(&str, Provider, Option<&str>)] = &[ + ( + "https://zoom.us/j/1234567890", + Provider::Zoom, + Some("1234567890"), + ), + ( + "https://us02web.zoom.us/j/81234567890?pwd=abc123", + Provider::Zoom, + Some("81234567890"), + ), + ("https://acme.zoom.us/my/jane.doe", Provider::Zoom, None), + ( + "https://zoom.us/w/98765432101?tk=x", + Provider::Zoom, + Some("98765432101"), + ), + ( + "https://zoomgov.com/j/1612345678", + Provider::Zoom, + Some("1612345678"), + ), + ( + "https://acme.zoomgov.com/j/1612345678?pwd=q", + Provider::Zoom, + Some("1612345678"), + ), + ( + "https://zoom.com/j/1234567890", + Provider::Zoom, + Some("1234567890"), + ), + ( + "https://app.zoom.us/wc/join/1234567890", + Provider::Zoom, + Some("1234567890"), + ), + ( + "https://us05web.zoom.us/wc/1234567890/join?pwd=x", + Provider::Zoom, + Some("1234567890"), + ), + ( + "https://acme.zoom.us/s/1234567890", + Provider::Zoom, + Some("1234567890"), + ), + ( + "https://zoom.us/meeting/register/tJ0kcuqrrzMsHtE", + Provider::Zoom, + None, + ), + ( + "https://zoom.us/join?confno=1234567890", + Provider::Zoom, + Some("1234567890"), + ), + ( + "https://meet.google.com/abc-defg-hij", + Provider::GoogleMeet, + Some("abc-defg-hij"), + ), + ( + "https://meet.google.com/ABC-DEFG-HIJ?authuser=1", + Provider::GoogleMeet, + Some("abc-defg-hij"), + ), + ( + "meet.google.com/abc-defg-hij", + Provider::GoogleMeet, + Some("abc-defg-hij"), + ), + ( + "https://meet.google.com/lookup/acme-standup", + Provider::GoogleMeet, + None, + ), + ( + "https://hangouts.google.com/hangouts/_/acme.example/weekly", + Provider::GoogleMeet, + None, + ), + ( + "https://teams.microsoft.com/l/meetup-join/19%3ameeting_NjE2ODk3%40thread.v2/0?context=%7b%22Tid%22%3a%22t%22%7d", + Provider::Teams, + Some("19:meeting_NjE2ODk3@thread.v2"), + ), + ( + "https://teams.live.com/meet/9876543210123?p=AbCd", + Provider::Teams, + Some("9876543210123"), + ), + ( + "https://teams.microsoft.com/meet/2345678901234?p=XyZ", + Provider::Teams, + Some("2345678901234"), + ), + ( + "https://teams.cloud.microsoft/l/meetup-join/19%3ameeting_abc%40thread.v2/0", + Provider::Teams, + Some("19:meeting_abc@thread.v2"), + ), + ( + "https://gov.teams.microsoft.us/l/meetup-join/19%3ameeting_gcc%40thread.v2/0?context=x", + Provider::Teams, + Some("19:meeting_gcc@thread.v2"), + ), + ( + "https://dod.teams.microsoft.us/l/meetup-join/19%3ameeting_dod%40thread.v2/0", + Provider::Teams, + Some("19:meeting_dod@thread.v2"), + ), + ( + "https://teams.microsoft.com/v2/?meetingjoin=true#/l/meetup-join/19:meeting_v2frag@thread.v2/0?context=x", + Provider::Teams, + Some("19:meeting_v2frag@thread.v2"), + ), + ( + "https://teams.microsoft.com/dl/launcher/launcher.html?url=%2F_%23%2Fl%2Fmeetup-join%2F19%3Ameeting_launch%40thread.v2%2F0%3Fcontext%3Dx&type=meetup-join", + Provider::Teams, + Some("19:meeting_launch@thread.v2"), + ), + ( + "msteams:/l/meetup-join/19%3ameeting_app%40thread.v2/0", + Provider::Teams, + Some("19:meeting_app@thread.v2"), + ), + ( + "https://acme.webex.com/acme/j.php?MTID=m1234abcd", + Provider::Webex, + Some("m1234abcd"), + ), + ( + "https://acme.webex.com/acme/e.php?MTID=e5678", + Provider::Webex, + Some("e5678"), + ), + ( + "https://acme.webex.com/meet/jane.doe", + Provider::Webex, + None, + ), + ("https://web.webex.com/meet/jdoe", Provider::Webex, None), + ("https://acme.webexgov.us/meet/jdoe", Provider::Webex, None), + ( + "https://acme.webex.com/wbxmjs/joinservice/sites/acme/meeting/download/abc123def?siteurl=acme", + Provider::Webex, + Some("abc123def"), + ), + ( + "https://app.slack.com/huddle/T01ABCDEF/C02GHIJKL", + Provider::Slack, + Some("C02GHIJKL"), + ), + ( + "https://acme.slack.com/huddle/T01ABCDEF/C02GHIJKL", + Provider::Slack, + Some("C02GHIJKL"), + ), + ("https://whereby.com/acme-standup", Provider::Whereby, None), + ( + "https://acme.whereby.com/design-room", + Provider::Whereby, + None, + ), + ("https://appear.in/old-room", Provider::Whereby, None), + ( + "https://meet.around.co/r/acme-team", + Provider::Around, + Some("acme-team"), + ), + ( + "https://around.co/r/standup", + Provider::Around, + Some("standup"), + ), + ( + "https://global.gotomeeting.com/join/123456789", + Provider::GoToMeeting, + Some("123456789"), + ), + ( + "https://meet.goto.com/123456789", + Provider::GoToMeeting, + Some("123456789"), + ), + ( + "https://meet.goto.com/acme-weekly", + Provider::GoToMeeting, + None, + ), + ("https://gotomeet.me/janedoe", Provider::GoToMeeting, None), + ( + "https://app.goto.com/meeting/987654321", + Provider::GoToMeeting, + Some("987654321"), + ), + ( + "https://attendee.gotowebinar.com/register/1234567890123456789", + Provider::GoToWebinar, + Some("1234567890123456789"), + ), + ( + "https://global.gotowebinar.com/join/1234567890123456789/987654321", + Provider::GoToWebinar, + Some("1234567890123456789"), + ), + ( + "https://bluejeans.com/123456789", + Provider::BlueJeans, + Some("123456789"), + ), + ( + "https://bluejeans.com/123456789/4321/webrtc", + Provider::BlueJeans, + Some("123456789"), + ), + ( + "https://acme.bluejeans.com/5551234567", + Provider::BlueJeans, + Some("5551234567"), + ), + ( + "https://meet.jit.si/AcmeWeeklySync", + Provider::Jitsi, + Some("AcmeWeeklySync"), + ), + ( + "https://8x8.vc/acme-tenant/weekly", + Provider::Jitsi, + Some("weekly"), + ), + ("https://8x8.vc/RoomName", Provider::Jitsi, Some("RoomName")), + ( + "https://discord.com/channels/123456789012345678/876543210987654321", + Provider::Discord, + Some("876543210987654321"), + ), + ( + "https://discordapp.com/channels/123456789012345678/876543210987654321", + Provider::Discord, + Some("876543210987654321"), + ), + ( + "https://discord.com/events/123456789012345678/112233445566778899", + Provider::Discord, + Some("112233445566778899"), + ), + ( + "https://join.skype.com/AbCdEfGhIjKl", + Provider::Skype, + Some("AbCdEfGhIjKl"), + ), + ( + "https://meet.lync.com/contoso/jdoe/AB12CD34", + Provider::Skype, + Some("AB12CD34"), + ), + ( + "https://chime.aws/1234567890", + Provider::Chime, + Some("1234567890"), + ), + ( + "https://app.chime.aws/meetings/?pin=1234567890", + Provider::Chime, + Some("1234567890"), + ), + ( + "https://meetings.ringcentral.com/j/1234567890", + Provider::RingCentral, + Some("1234567890"), + ), + ( + "https://v.ringcentral.com/join/123456789", + Provider::RingCentral, + Some("123456789"), + ), + ( + "https://video.ringcentral.com/join/123456789", + Provider::RingCentral, + Some("123456789"), + ), + ( + "https://meetings.dialpad.com/acme-weekly", + Provider::Dialpad, + None, + ), + ( + "https://dialpad.com/meetings/janedoe", + Provider::Dialpad, + None, + ), + ( + "https://meeting.zoho.com/meeting/login/join.jsp?key=1234567890", + Provider::ZohoMeeting, + Some("1234567890"), + ), + ( + "https://meeting.zoho.eu/join?key=987654321", + Provider::ZohoMeeting, + Some("987654321"), + ), + ( + "https://meeting.zoho.com.au/meeting/login/join.jsp?key=555", + Provider::ZohoMeeting, + Some("555"), + ), + ( + "https://vc.larksuite.com/j/123456789", + Provider::Lark, + Some("123456789"), + ), + ( + "https://vc.feishu.cn/j/987654321", + Provider::Lark, + Some("987654321"), + ), + ( + "https://tuple.app/c/AbCdEf123", + Provider::Tuple, + Some("AbCdEf123"), + ), + ("https://www.tuple.app/c/xyz", Provider::Tuple, Some("xyz")), + ( + "https://app.gather.town/app/AbCdEfGh12345678/acme-office", + Provider::Gather, + Some("AbCdEfGh12345678"), + ), + ( + "https://gather.town/invite?token=abc", + Provider::Gather, + None, + ), + ( + "https://riverside.fm/studio/acme-podcast?t=abc", + Provider::Riverside, + None, + ), + ( + "https://riverside.com/studio/weekly-show", + Provider::Riverside, + None, + ), + ( + "https://streamyard.com/n8yuqz6ttd", + Provider::StreamYard, + Some("n8yuqz6ttd"), + ), + ( + "https://streamyard.com/a1b2c3d4e5", + Provider::StreamYard, + Some("a1b2c3d4e5"), + ), + ( + "https://call.whatsapp.com/video/AbCdEfGhIjKlMn", + Provider::WhatsApp, + Some("AbCdEfGhIjKlMn"), + ), + ( + "https://call.whatsapp.com/voice/ZyXwVuTs", + Provider::WhatsApp, + Some("ZyXwVuTs"), + ), + ( + "https://facetime.apple.com/join#v=1&p=AbCdEf&k=XyZ", + Provider::FaceTime, + None, + ), + ( + "http://facetime.apple.com/join?src=msg#v=1&p=Q1w2E3&k=r4T5", + Provider::FaceTime, + None, + ), + ( + "https://app.cal.com/video/abc123XYZ", + Provider::CalVideo, + Some("abc123XYZ"), + ), + ( + "https://cal.com/video/9f8e7d6c", + Provider::CalVideo, + Some("9f8e7d6c"), + ), + ("https://acme.daily.co/standup", Provider::Daily, None), + ("https://team-x.daily.co/AbC123", Provider::Daily, None), + ( + "https://app.livestorm.co/p/0b1c2d3e-aaaa-bbbb-cccc-111122223333/live?s=abc", + Provider::Livestorm, + Some("0b1c2d3e-aaaa-bbbb-cccc-111122223333"), + ), + ( + "https://app.livestorm.co/acme/product-launch", + Provider::Livestorm, + None, + ), + ( + "https://pop.com/j/123-456-789", + Provider::Pop, + Some("123-456-789"), + ), + ("https://pop.com/j/abcDEF", Provider::Pop, Some("abcDEF")), + ( + "https://tandem.chat/join/acme-team", + Provider::Tandem, + Some("acme-team"), + ), + ( + "https://tandem.chat/room/abc123", + Provider::Tandem, + Some("abc123"), + ), + ( + "https://app.vowel.com/#/g/AbCdEf123", + Provider::Vowel, + Some("AbCdEf123"), + ), + ( + "https://acme.vowel.com/room/weekly", + Provider::Vowel, + Some("weekly"), + ), + ( + "https://my.demio.com/ref/AbCdEfGh", + Provider::Demio, + Some("AbCdEfGh"), + ), + ( + "https://event.demio.com/room/XyZ123", + Provider::Demio, + Some("XyZ123"), + ), + ( + "https://app.butter.us/acme/weekly-workshop", + Provider::Butter, + None, + ), + ("https://app.butter.us/jane/retro", Provider::Butter, None), +]; + +const NEGATIVE: &[&str] = &[ + "https://zoom.us/pricing", + "https://zoom.us/", + "https://zoom.us/j/", + "https://zoom.us/j/abc", + "https://zoom.us/j/12345", + "https://acme.zoom.us/u/abcdEfGh", + "https://zoom.us/rec/share/xyz", + "https://zoom.us/download", + "https://notzoom.us/j/1234567890", + "https://zoom.us.evil.example/j/1234567890", + "https://meet.google.com", + "https://meet.google.com/", + "https://meet.google.com/new", + "https://meet.google.com/landing", + "https://meet.google.com/abc-defg", + "https://meet.google.com/abc-defg-hi1", + "https://tel.meet/abc-defg-hij?pin=1", + "https://support.google.com/a/users/answer/9282720", + "https://teams.microsoft.com/l/channel/19%3aabc%40thread.skype/General", + "https://teams.microsoft.com/meetingOptions/?organizerId=abc", + "https://www.microsoft.com/microsoft-teams/join-a-meeting", + "https://teams.microsoft.com/", + "https://teams.microsoft.com/l/meetup-join/", + "https://www.webex.com/pricing/index.html", + "https://acme.webex.com/acme/j.php", + "https://help.webex.com/en-us/article/abc", + "https://app.slack.com/client/T01ABCDEF/C02GHIJKL", + "https://slack.com/huddles", + "https://whereby.com/information/pricing", + "https://whereby.com/pricing", + "https://whereby.com/", + "https://around.co/pricing", + "https://www.goto.com/meeting", + "https://www.gotomeeting.com/pricing", + "https://meet.goto.com/", + "https://www.gotowebinar.com/pricing", + "https://attendee.gotowebinar.com/rt/123", + "https://www.bluejeans.com/pricing", + "https://meet.jit.si/", + "https://meet.jit.si/static/close.html", + "https://jitsi.org/meet", + "https://discord.com/download", + "https://discord.gg/abc", + "https://discord.com/channels/@me", + "https://join.skype.com/invite/AbC", + "https://www.skype.com/en/", + "https://aws.amazon.com/chime/", + "https://chime.aws/download", + "https://www.ringcentral.com/video.html", + "https://ringcentral.com/join", + "https://www.dialpad.com/meetings/", + "https://meetings.dialpad.com/login", + "https://www.zoho.com/meeting/pricing.html", + "https://meeting.zoho.com/", + "https://meeting.zoho.example.com/join?key=1234567890", + "https://meeting.zoho.co.example/join?key=1234567890", + "https://meet.zoho.example.com/abc123", + "https://www.larksuite.com/en_us/product/video-conferencing", + "https://vc.larksuite.com/", + "https://tuple.app/pricing", + "https://www.gather.town/pricing", + "https://gather.town/", + "https://riverside.fm/pricing", + "https://riverside.fm/studio", + "https://streamyard.com/pricing", + "https://streamyard.com/watch/abc123def4", + "https://streamyard.com/resources", + "https://www.whatsapp.com/download", + "https://call.whatsapp.com/", + "https://facetime.apple.com/join", + "https://www.apple.com/facetime/", + "https://cal.com/pricing", + "https://cal.com/jane/30min", + "https://www.daily.co/pricing", + "https://docs.daily.co/reference", + "https://daily.co/standup", + "https://livestorm.co/pricing", + "https://app.livestorm.co/login", + "https://pop.com/pricing", + "https://tandem.chat/pricing", + "https://www.vowel.com/pricing", + "https://vowel.com/", + "https://www.demio.com/pricing", + "https://demio.com/", + "https://www.butter.us/pricing", + "https://app.butter.us/login/x", + "https://example.com/j/1234567890", + "ftp://zoom.us/j/1234567890", + "javascript:alert(1)", + "", + "not a url", +]; + +#[test] +fn recognises_every_provider_shape() { + for (url, provider, id) in POSITIVE { + check(url, *provider, *id); + } + let covered: HashSet = POSITIVE.iter().map(|(_, p, _)| *p).collect(); + for provider in Provider::ALL { + if provider == Provider::Other { + continue; + } + let count = POSITIVE.iter().filter(|(_, p, _)| *p == provider).count(); + assert!(count >= 2, "{provider:?} needs two shapes, has {count}"); + assert!(covered.contains(&provider)); + } +} + +#[test] +fn rejects_marketing_pages_and_lookalikes() { + for url in NEGATIVE { + reject(url); + } +} + +#[test] +fn normalises_urls() { + let link = + classify_url("http://ZOOM.us/j/1234567890?pwd=x&utm_source=email&utm_medium=x").unwrap(); + assert_eq!(link.url, "https://zoom.us/j/1234567890?pwd=x"); + let link = classify_url("https://meet.google.com/abc-defg-hij#section").unwrap(); + assert_eq!(link.url, "https://meet.google.com/abc-defg-hij"); + let link = classify_url("https://facetime.apple.com/join#v=1&p=AbCdEf&k=XyZ").unwrap(); + assert_eq!( + link.url, + "https://facetime.apple.com/join#v=1&p=AbCdEf&k=XyZ" + ); + let link = + classify_url("zoommtg://zoom.us/join?action=join&confno=1234567890&pwd=xyz").unwrap(); + assert_eq!(link.url, "https://zoom.us/j/1234567890?pwd=xyz"); + assert_eq!(link.meeting_id.as_deref(), Some("1234567890")); + let link = classify_url("https://zoom.us/j/1234567890).").unwrap(); + assert_eq!(link.url, "https://zoom.us/j/1234567890"); +} + +#[test] +fn unwraps_safelinks_and_redirects() { + let cases = [ + "https://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fus06web.zoom.us%2Fj%2F87654321098%3Fpwd%3DQwErTy&data=05%7C02%7C&sdata=abc%3D&reserved=0", + "https://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fus06web.zoom.us%2Fj%2F87654321098%3Fpwd%3DQwErTy", + "https://statics.teams.cdn.office.net/evergreen-assets/safelinks/1/atp-safelinks.html?url=https%3A%2F%2Fus06web.zoom.us%2Fj%2F87654321098%3Fpwd%3DQwErTy", + "https://www.google.com/url?q=https://us06web.zoom.us/j/87654321098?pwd%3DQwErTy&sa=D&source=calendar&usg=AOvVaw", + "https://urldefense.com/v3/__https://us06web.zoom.us/j/87654321098?pwd=QwErTy__;!!abc$", + "https://urldefense.proofpoint.com/v2/url?u=https-3A__us06web.zoom.us_j_87654321098-3Fpwd-3DQwErTy&d=DwMF&c=x", + ]; + for case in cases { + let link = classify_url(case).unwrap_or_else(|| panic!("{case}")); + assert_eq!(link.provider, Provider::Zoom, "{case}"); + assert_eq!(link.meeting_id.as_deref(), Some("87654321098"), "{case}"); + assert_eq!( + link.url, "https://us06web.zoom.us/j/87654321098?pwd=QwErTy", + "{case}" + ); + } + assert_eq!( + classify_url( + "https://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fexample.com%2F" + ), + None + ); +} + +#[test] +fn finds_links_in_text() { + let cases: &[(&str, &str)] = &[ + ( + "Join at https://zoom.us/j/1234567890.", + "https://zoom.us/j/1234567890", + ), + ( + "(https://meet.google.com/abc-defg-hij)", + "https://meet.google.com/abc-defg-hij", + ), + ( + "[Join](https://meet.google.com/abc-defg-hij)", + "https://meet.google.com/abc-defg-hij", + ), + ( + "Dial in or go to meet.google.com/abc-defg-hij, thanks", + "https://meet.google.com/abc-defg-hij", + ), + ( + "Join", + "https://zoom.us/j/1234567890?pwd=a&from=addon", + ), + ( + "Join", + "https://acme.webex.com/acme/j.php?MTID=m42", + ), + ( + "Join:https://whereby.com/acme-standup!", + "https://whereby.com/acme-standup", + ), + ( + "Room: \"https://meet.jit.si/Acme\"", + "https://meet.jit.si/Acme", + ), + ]; + for (text, expected) in cases { + let link = find_meeting_link(text).unwrap_or_else(|| panic!("{text}")); + assert_eq!(link.url, *expected, "{text}"); + } +} + +#[test] +fn picks_the_join_link_out_of_teams_boilerplate() { + let text = "________________\nMicrosoft Teams meeting\nJoin on your computer, mobile app or room device\nDownload Teams | Join on the web\nClick here to join the meeting\nLearn More | Meeting options"; + let link = find_meeting_link(text).unwrap(); + assert_eq!(link.provider, Provider::Teams); + assert_eq!( + link.meeting_id.as_deref(), + Some("19:meeting_NjE2ODk3@thread.v2") + ); + let zoom = "Join Zoom Meeting\nhttps://acme.zoom.us/j/84512345678?pwd=aBcDeF\n\nMeeting ID: 845 1234 5678\nFind your local number: https://acme.zoom.us/u/abcdEfGh"; + assert_eq!( + find_meeting_link(zoom).unwrap().meeting_id.as_deref(), + Some("84512345678") + ); + assert_eq!(find_all_meeting_links(zoom).len(), 1); +} + +#[test] +fn text_without_meeting_links() { + for text in [ + "Meeting ID: 845 1234 5678 Passcode: 123", + "See https://zoom.us/pricing and https://www.microsoft.com/microsoft-teams", + "Lunch at the usual place", + "https://", + "&&#xZZ;� Vec<(String, String)> { + url::Url::parse(url) + .unwrap() + .query_pairs() + .map(|(k, v)| (k.into_owned(), v.into_owned())) + .collect() +} + +fn get<'a>(pairs: &'a [(String, String)], key: &str) -> Option<&'a str> { + pairs + .iter() + .find(|(k, _)| k == key) + .map(|(_, v)| v.as_str()) +} + +#[test] +fn pkce_matches_rfc_7636_vector() { + let bytes = [ + 116, 24, 223, 180, 151, 153, 224, 37, 79, 250, 96, 125, 216, 173, 187, 186, 22, 212, 37, + 77, 105, 214, 191, 240, 91, 88, 5, 88, 83, 132, 141, 121, + ]; + let pkce = Pkce::from_random(&bytes).unwrap(); + assert_eq!(pkce.verifier, "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk"); + assert_eq!( + pkce.challenge, + "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM" + ); + assert_eq!( + Pkce::from_random(&[0; 31]), + Err(AuthError::NotEnoughRandomness(31)) + ); + assert_eq!(Pkce::from_random(&[7; 500]).unwrap().verifier.len(), 128); + assert!(state_from_random(&[1; 8]).is_err()); + assert_eq!( + state_from_random(&[0xff; 16]).unwrap(), + "_____________________w" + ); +} + +#[test] +fn google_authorize_and_token_requests() { + let client = OAuthClient::google( + "123.apps.googleusercontent.com", + Some("desktop-secret".to_string()), + ); + let url = authorize_url( + &client, + "http://127.0.0.1:53682/callback", + "st4te", + "chal", + Some("me@acme.example"), + ); + assert!(url.starts_with("https://accounts.google.com/o/oauth2/v2/auth?")); + let q = query_of(&url); + assert_eq!(get(&q, "client_id"), Some("123.apps.googleusercontent.com")); + assert_eq!( + get(&q, "redirect_uri"), + Some("http://127.0.0.1:53682/callback") + ); + assert_eq!(get(&q, "response_type"), Some("code")); + assert_eq!( + get(&q, "scope"), + Some("openid email https://www.googleapis.com/auth/calendar.readonly") + ); + assert_eq!(get(&q, "access_type"), Some("offline")); + assert_eq!(get(&q, "prompt"), Some("consent")); + assert_eq!(get(&q, "code_challenge"), Some("chal")); + assert_eq!(get(&q, "code_challenge_method"), Some("S256")); + assert_eq!(get(&q, "state"), Some("st4te")); + assert_eq!(get(&q, "login_hint"), Some("me@acme.example")); + + let spec = token_request( + &client, + "4/0Ab_code", + "verifier", + "http://127.0.0.1:53682/callback", + ); + assert_eq!(spec.method, HttpMethod::Post); + assert_eq!(spec.url, "https://oauth2.googleapis.com/token"); + assert_eq!( + spec.header("content-type"), + Some("application/x-www-form-urlencoded") + ); + let body = spec.form_body().unwrap(); + let form: Vec<(String, String)> = url::form_urlencoded::parse(body.as_bytes()) + .map(|(k, v)| (k.into_owned(), v.into_owned())) + .collect(); + assert_eq!(get(&form, "grant_type"), Some("authorization_code")); + assert_eq!(get(&form, "code"), Some("4/0Ab_code")); + assert_eq!(get(&form, "code_verifier"), Some("verifier")); + assert_eq!(get(&form, "client_secret"), Some("desktop-secret")); + assert_eq!(get(&form, "scope"), None); + + let refresh = refresh_request(&client, "1//refresh"); + assert_eq!(get(&refresh.form, "grant_type"), Some("refresh_token")); + assert_eq!(get(&refresh.form, "refresh_token"), Some("1//refresh")); +} + +#[test] +fn microsoft_authorize_and_token_requests() { + let client = OAuthClient::microsoft("00000000-1111-2222-3333-444444444444"); + let url = authorize_url(&client, "http://localhost:53682/callback", "s", "c", None); + assert!(url.starts_with("https://login.microsoftonline.com/common/oauth2/v2.0/authorize?")); + let q = query_of(&url); + let scope = get(&q, "scope").unwrap(); + for needed in ["offline_access", "Calendars.Read", "User.Read"] { + assert!(scope.split(' ').any(|s| s == needed), "{needed}"); + } + assert_eq!(get(&q, "login_hint"), None); + assert_eq!(get(&q, "access_type"), None); + + let spec = token_request(&client, "code", "v", "http://localhost:53682/callback"); + assert_eq!( + spec.url, + "https://login.microsoftonline.com/common/oauth2/v2.0/token" + ); + assert!(get(&spec.form, "scope").is_some()); + assert_eq!(get(&spec.form, "client_secret"), None); + + let tenant = OAuthClient { + tenant: Some("contoso.onmicrosoft.com".to_string()), + ..client.clone() + }; + assert!( + authorize_url(&tenant, "x", "s", "c", None) + .starts_with("https://login.microsoftonline.com/contoso.onmicrosoft.com/") + ); + let hostile = OAuthClient { + tenant: Some("evil.example/../x?".to_string()), + ..client + }; + assert!( + authorize_url(&hostile, "x", "s", "c", None) + .starts_with("https://login.microsoftonline.com/common/") + ); +} + +fn jwt(payload: &str) -> String { + format!( + "{}.{}.sig", + URL_SAFE_NO_PAD.encode(br#"{"alg":"RS256"}"#), + URL_SAFE_NO_PAD.encode(payload.as_bytes()) + ) +} + +#[test] +fn token_responses() { + let google = format!( + r#"{{"access_token":"ya29.a0","expires_in":3599,"refresh_token":"1//0g","scope":"openid https://www.googleapis.com/auth/calendar.readonly email","token_type":"Bearer","id_token":"{}"}}"#, + jwt( + r#"{"iss":"https://accounts.google.com","email":"me@acme.example","email_verified":true}"# + ) + ); + let tokens = parse_token_response(&google).unwrap(); + assert_eq!(tokens.access_token, "ya29.a0"); + assert_eq!(tokens.refresh_token.as_deref(), Some("1//0g")); + assert_eq!(tokens.expires_in, Some(3599)); + assert_eq!(tokens.id_token_email.as_deref(), Some("me@acme.example")); + + let microsoft = format!( + r#"{{"token_type":"Bearer","scope":"Calendars.Read User.Read","expires_in":"4371","ext_expires_in":4371,"access_token":"eyJ0","refresh_token":"0.AAA","id_token":"{}"}}"#, + jwt(r#"{"preferred_username":"me@contoso.example","name":"Me"}"#) + ); + let tokens = parse_token_response(µsoft).unwrap(); + assert_eq!(tokens.expires_in, Some(4371)); + assert_eq!(tokens.id_token_email.as_deref(), Some("me@contoso.example")); + + let refreshed = parse_token_response(r#"{"access_token":"new","expires_in":3600}"#).unwrap(); + assert_eq!(refreshed.refresh_token, None); + assert_eq!(refreshed.id_token_email, None); + + assert_eq!( + parse_token_response( + r#"{"error":"invalid_grant","error_description":"Token has been expired or revoked."}"# + ), + Err(AuthError::Provider { + error: "invalid_grant".to_string(), + description: "Token has been expired or revoked.".to_string(), + }) + ); + assert_eq!( + parse_token_response(r#"{"error":"access_denied"}"#), + Err(AuthError::AccessDenied) + ); + assert!(matches!( + parse_token_response(""), + Err(AuthError::InvalidResponse(_)) + )); + assert!(matches!( + parse_token_response("{}"), + Err(AuthError::InvalidResponse(_)) + )); + assert_eq!(id_token_email("only.two"), None); + assert_eq!(id_token_email("a.!!!.c"), None); + assert_eq!(id_token_email(&jwt(r#"{"sub":"123"}"#)), None); +} + +#[test] +fn redirects() { + assert_eq!( + parse_redirect("code=4%2F0Ab&state=xyz&scope=email", "xyz") + .unwrap() + .code, + "4/0Ab" + ); + assert_eq!( + parse_redirect("?state=xyz&code=abc", "xyz").unwrap().code, + "abc" + ); + assert_eq!( + parse_redirect("http://127.0.0.1:53682/callback?code=abc&state=xyz", "xyz") + .unwrap() + .code, + "abc" + ); + assert_eq!( + parse_redirect("code=abc&state=nope", "xyz"), + Err(AuthError::StateMismatch) + ); + assert_eq!( + parse_redirect("code=abc", "xyz"), + Err(AuthError::StateMismatch) + ); + assert_eq!( + parse_redirect("code=abc&state=", ""), + Err(AuthError::StateMismatch) + ); + assert_eq!( + parse_redirect("error=access_denied&state=xyz", "xyz"), + Err(AuthError::AccessDenied) + ); + assert_eq!( + parse_redirect( + "error=consent_required&error_description=Need+admin&state=xyz", + "xyz" + ), + Err(AuthError::Provider { + error: "consent_required".to_string(), + description: "Need admin".to_string(), + }) + ); + assert_eq!( + parse_redirect("state=xyz", "xyz"), + Err(AuthError::MissingCode) + ); + assert_eq!( + parse_redirect("state=xyz&code=", "xyz"), + Err(AuthError::MissingCode) + ); +} diff --git a/apps/desktop-gpui/calendar/tests/providers.rs b/apps/desktop-gpui/calendar/tests/providers.rs new file mode 100644 index 00000000000..84b32578f64 --- /dev/null +++ b/apps/desktop-gpui/calendar/tests/providers.rs @@ -0,0 +1,484 @@ +use cap_calendar::google::{ + GoogleOptions, google_calendar_list_request, google_events_request, parse_google_calendar_list, + parse_google_events, parse_google_events_with, +}; +use cap_calendar::graph::{ + GraphOptions, graph_calendar_view_request, graph_calendars_request, parse_graph_calendars, + parse_graph_events_with, +}; +use cap_calendar::system::{SystemAttendee, SystemEvent, from_system_event}; +use cap_calendar::{ + CalendarEvent, Error, EventStatus, HttpMethod, Provider, Response, SourceKind, Tz, +}; +use chrono::{DateTime, Utc}; + +fn utc(s: &str) -> DateTime { + s.parse().unwrap_or_else(|e| panic!("{s}: {e}")) +} + +fn fixture(name: &str) -> String { + let path = format!("{}/tests/fixtures/{name}", env!("CARGO_MANIFEST_DIR")); + std::fs::read_to_string(&path).unwrap_or_else(|e| panic!("{path}: {e}")) +} + +fn by_id<'a>(events: &'a [CalendarEvent], id: &str) -> &'a CalendarEvent { + events + .iter() + .find(|e| e.id == id) + .unwrap_or_else(|| panic!("missing {id}")) +} + +fn google_opts() -> GoogleOptions { + GoogleOptions { + self_emails: vec!["me@acme.example".to_string()], + ..GoogleOptions::default() + } +} + +#[test] +fn google_events_request_shape() { + let window = utc("2026-03-01T00:00:00Z")..utc("2026-04-01T00:00:00Z"); + let spec = google_events_request( + "en.usa#holiday@group.v.calendar.google.com", + &window, + Some("tok123"), + ); + assert_eq!(spec.method, HttpMethod::Get); + assert_eq!( + spec.url, + "https://www.googleapis.com/calendar/v3/calendars/en.usa%23holiday@group.v.calendar.google.com/events" + ); + let get = |k: &str| { + spec.query + .iter() + .find(|(key, _)| key == k) + .map(|(_, v)| v.as_str()) + }; + assert_eq!(get("singleEvents"), Some("true")); + assert_eq!(get("orderBy"), Some("startTime")); + assert_eq!(get("timeMin"), Some("2026-03-01T00:00:00Z")); + assert_eq!(get("timeMax"), Some("2026-04-01T00:00:00Z")); + assert_eq!(get("maxResults"), Some("250")); + assert_eq!(get("pageToken"), Some("tok123")); + let fields = get("fields").unwrap(); + for needed in [ + "nextPageToken", + "conferenceData", + "hangoutLink", + "attendees(", + "recurringEventId", + "eventType", + "htmlLink", + ] { + assert!(fields.contains(needed), "{needed}"); + } + let full = spec.url_with_query(); + assert!(full.contains("timeMin=2026-03-01T00%3A00%3A00Z"), "{full}"); + assert!(spec.form_body().is_none()); + + let first = google_events_request("primary", &window, None); + assert!(first.query.iter().all(|(k, _)| k != "pageToken")); + let list = google_calendar_list_request(None); + assert_eq!( + list.url, + "https://www.googleapis.com/calendar/v3/users/me/calendarList" + ); +} + +#[test] +fn google_events_page() { + let page = parse_google_events_with( + &fixture("google_events_page1.json"), + "primary", + &google_opts(), + ) + .unwrap(); + assert_eq!( + page.next.as_deref(), + Some("CigKGjdrdWt1cXJmZWRsbTJmOXQ2dm8ybWgxbG41GAEggICA2r") + ); + assert_eq!(page.items.len(), 7); + assert_eq!(page.skipped, 7); + assert!( + page.items + .iter() + .all(|e| e.source == SourceKind::Google && e.calendar_id == "primary") + ); + + let sync = by_id(&page.items, "7kukuqrfedlm2f9t6vo2mh1ln5_20260309T130000Z"); + assert_eq!(sync.start, utc("2026-03-09T13:00:00Z")); + assert_eq!(sync.end, utc("2026-03-09T13:30:00Z")); + assert_eq!( + sync.series_id.as_deref(), + Some("7kukuqrfedlm2f9t6vo2mh1ln5") + ); + assert_eq!(sync.attendees.len(), 3); + assert_eq!(sync.self_response, Response::Accepted); + assert!( + sync.web_url + .as_deref() + .unwrap() + .starts_with("https://www.google.com/calendar/event?eid=") + ); + let link = sync.link.as_ref().unwrap(); + assert_eq!( + (link.provider, link.meeting_id.as_deref()), + (Provider::GoogleMeet, Some("abc-defg-hij")) + ); + + let birthday = by_id(&page.items, "birthday_20260312"); + assert!(birthday.all_day && birthday.free); + assert_eq!(birthday.start, utc("2026-03-12T00:00:00Z")); + assert_eq!(birthday.end, utc("2026-03-13T00:00:00Z")); + + let zoom = by_id(&page.items, "zoomaddon1"); + let link = zoom.link.as_ref().unwrap(); + assert_eq!(link.provider, Provider::Zoom); + assert_eq!(link.meeting_id.as_deref(), Some("84512345678")); + assert_eq!(zoom.self_response, Response::Accepted); + assert!(zoom.organizer.as_ref().unwrap().is_self); + + let declined = by_id(&page.items, "declined1"); + assert_eq!(declined.self_response, Response::Declined); + assert_eq!( + declined.link.as_ref().and_then(|l| l.meeting_id.as_deref()), + Some("xyz-abcd-efg") + ); + + let teams = by_id(&page.items, "teamsdesc1"); + assert_eq!(teams.status, EventStatus::Tentative); + assert_eq!(teams.self_response, Response::NeedsAction); + assert_eq!(teams.location.as_deref(), Some("Room 4")); + assert_eq!( + teams.link.as_ref().and_then(|l| l.meeting_id.as_deref()), + Some("19:meeting_ZDQ4YTI2@thread.v2") + ); + + let solo = by_id(&page.items, "solo1"); + assert_eq!(solo.self_response, Response::Accepted); + assert!(!solo.has_participants()); + assert!(solo.link.is_none()); + + let london = by_id(&page.items, "nooffset1"); + assert_eq!(london.start, utc("2026-03-13T10:00:00Z")); +} + +#[test] +fn google_flags_include_status_events_and_cancellations() { + let opts = GoogleOptions { + include_status_events: true, + include_cancelled: true, + ..google_opts() + }; + let page = + parse_google_events_with(&fixture("google_events_page1.json"), "primary", &opts).unwrap(); + assert_eq!(page.items.len(), 11); + let cancelled = by_id(&page.items, "7kukuqrfedlm2f9t6vo2mh1ln5_20260330T130000Z"); + assert_eq!(cancelled.status, EventStatus::Cancelled); + assert_eq!(cancelled.start, utc("2026-03-30T13:00:00Z")); + by_id(&page.items, "wl_20260310"); + by_id(&page.items, "ooo_1"); + by_id(&page.items, "focus_1"); +} + +#[test] +fn google_last_page_and_errors() { + let page = parse_google_events(&fixture("google_events_page2.json"), "primary").unwrap(); + assert_eq!(page.next, None); + assert_eq!(page.items.len(), 1); + assert_eq!( + page.items[0] + .link + .as_ref() + .and_then(|l| l.meeting_id.as_deref()), + Some("99887766554") + ); + + let err = parse_google_events( + r#"{"error":{"code":401,"message":"Request had invalid authentication credentials.","errors":[{"reason":"authError"}],"status":"UNAUTHENTICATED"}}"#, + "primary", + ) + .unwrap_err(); + assert!(err.is_unauthorized()); + assert!(matches!( + err, + Error::Api { + status: Some(401), + .. + } + )); + assert!(matches!(parse_google_events("{", "p"), Err(Error::Json(_)))); + assert!(matches!( + parse_google_events("[]", "p"), + Err(Error::InvalidResponse(_)) + )); + assert!(matches!( + parse_google_events(r#"{"items":5}"#, "p"), + Err(Error::InvalidResponse(_)) + )); + assert_eq!( + parse_google_events(r#"{"kind":"calendar#events"}"#, "p") + .unwrap() + .items + .len(), + 0 + ); +} + +#[test] +fn google_calendar_list() { + let page = parse_google_calendar_list(&fixture("google_calendar_list.json")).unwrap(); + assert_eq!(page.items.len(), 2); + assert_eq!(page.skipped, 2); + let primary = &page.items[0]; + assert!(primary.primary && primary.selected); + assert_eq!(primary.color.as_deref(), Some("#9fe1e7")); + assert_eq!(primary.access_role.as_deref(), Some("owner")); + assert_eq!(primary.owner_email.as_deref(), Some("me@acme.example")); + assert_eq!(primary.time_zone.as_deref(), Some("America/New_York")); + let holidays = &page.items[1]; + assert_eq!(holidays.name, "US holidays"); + assert!(!holidays.selected); +} + +#[test] +fn graph_requests() { + let window = utc("2026-03-01T00:00:00Z")..utc("2026-04-01T00:00:00Z"); + let spec = graph_calendar_view_request(None, &window, None).unwrap(); + assert_eq!(spec.url, "https://graph.microsoft.com/v1.0/me/calendarView"); + assert_eq!(spec.header("prefer"), Some("outlook.timezone=\"UTC\"")); + let get = |k: &str| { + spec.query + .iter() + .find(|(key, _)| key == k) + .map(|(_, v)| v.as_str()) + }; + assert_eq!(get("startDateTime"), Some("2026-03-01T00:00:00Z")); + assert_eq!(get("endDateTime"), Some("2026-04-01T00:00:00Z")); + assert_eq!(get("$orderby"), Some("start/dateTime")); + for field in [ + "onlineMeeting", + "onlineMeetingUrl", + "isOnlineMeeting", + "body", + "responseStatus", + "seriesMasterId", + "webLink", + "isAllDay", + "isCancelled", + ] { + assert!( + get("$select").unwrap().split(',').any(|f| f == field), + "{field}" + ); + } + + let spec = graph_calendar_view_request(Some("AAMk/abc="), &window, None).unwrap(); + assert_eq!( + spec.url, + "https://graph.microsoft.com/v1.0/me/calendars/AAMk%2Fabc=/calendarView" + ); + + let next = "https://graph.microsoft.com/v1.0/me/calendarView?startDateTime=2026-03-01T00%3a00%3a00Z&%24skip=250"; + let spec = graph_calendar_view_request(None, &window, Some(next)).unwrap(); + assert!(spec.query.is_empty()); + assert!( + spec.url + .starts_with("https://graph.microsoft.com/v1.0/me/calendarView?") + ); + assert!(spec.url.contains("%24skip=250")); + assert_eq!(spec.header("Prefer"), Some("outlook.timezone=\"UTC\"")); + + for bad in [ + "https://evil.example/v1.0/me/calendarView", + "https://graph.microsoft.com.evil.example/v1.0", + "http://graph.microsoft.com/v1.0/me/calendarView", + "https://graph.microsoft.com:8443/v1.0/me", + "https://user@graph.microsoft.com/v1.0/me", + "not a url", + ] { + assert!( + matches!( + graph_calendar_view_request(None, &window, Some(bad)), + Err(Error::UntrustedUrl(_)) + ), + "{bad}" + ); + } + assert!( + graph_calendars_request(None) + .unwrap() + .url + .ends_with("/me/calendars") + ); +} + +#[test] +fn graph_calendar_view_page() { + let opts = GraphOptions { + self_emails: vec!["me@contoso.example".to_string()], + ..GraphOptions::default() + }; + let page = + parse_graph_events_with(&fixture("graph_calendar_view.json"), "cal-1", &opts).unwrap(); + assert!(page.next.as_deref().unwrap().contains("%24skip=250")); + assert_eq!(page.items.len(), 5); + assert_eq!(page.skipped, 2); + assert!(page.items.iter().all(|e| e.source == SourceKind::Microsoft)); + + let review = by_id(&page.items, "AAMkAGI2TG93AAA="); + assert_eq!(review.start, utc("2026-03-10T17:00:00Z")); + assert_eq!(review.end, utc("2026-03-10T18:00:00Z")); + assert_eq!(review.self_response, Response::Accepted); + assert_eq!(review.attendees.len(), 2); + assert!(review.attendees[0].is_self); + assert_eq!(review.series_id.as_deref(), Some("AAMkAGI2SERIES=")); + let link = review.link.as_ref().unwrap(); + assert_eq!(link.provider, Provider::Teams); + assert_eq!( + link.meeting_id.as_deref(), + Some("19:meeting_NjE2ODk3@thread.v2") + ); + let description = review.description.as_deref().unwrap(); + assert!(description.contains("Please review the mocks.")); + assert!(!description.contains('<') && !description.contains("margin")); + assert!( + review + .web_url + .as_deref() + .unwrap() + .starts_with("https://outlook.office365.com/") + ); + + let holiday = by_id(&page.items, "AAMkAGI2HOLIDAY="); + assert!(holiday.all_day && holiday.free); + assert_eq!(holiday.start, utc("2026-03-20T00:00:00Z")); + assert_eq!(holiday.end, utc("2026-03-21T00:00:00Z")); + assert_eq!(holiday.self_response, Response::Accepted); + + let west = by_id(&page.items, "AAMkAGI2PACIFIC="); + assert_eq!(west.start, utc("2026-03-12T16:00:00Z")); + assert_eq!(west.end, utc("2026-03-12T16:30:00Z")); + assert_eq!(west.self_response, Response::Tentative); + assert_eq!( + west.link.as_ref().map(|l| l.provider), + Some(Provider::GoogleMeet) + ); + + let vendor = by_id(&page.items, "AAMkAGI2ZOOMBODY="); + let link = vendor.link.as_ref().unwrap(); + assert_eq!( + link.url, + "https://us02web.zoom.us/j/81234567890?pwd=ZmFrZQ.1" + ); + assert_eq!(vendor.self_response, Response::NeedsAction); + assert_eq!(vendor.location, None); + + let declined = by_id(&page.items, "AAMkAGI2DECLINED="); + assert_eq!(declined.self_response, Response::Declined); + assert_eq!( + declined.link.as_ref().map(|l| l.provider), + Some(Provider::Teams) + ); + + let with_cancelled = GraphOptions { + include_cancelled: true, + ..opts + }; + let page = parse_graph_events_with( + &fixture("graph_calendar_view.json"), + "cal-1", + &with_cancelled, + ) + .unwrap(); + assert_eq!( + by_id(&page.items, "AAMkAGI2CANCELLED=").status, + EventStatus::Cancelled + ); + + let err = parse_graph_events_with( + r#"{"error":{"code":"InvalidAuthenticationToken","message":"Access token has expired or is not yet valid."}}"#, + "cal-1", + &GraphOptions::default(), + ) + .unwrap_err(); + assert!(err.is_unauthorized()); +} + +#[test] +fn graph_calendars() { + let page = parse_graph_calendars(&fixture("graph_calendars.json")).unwrap(); + assert_eq!(page.items.len(), 2); + assert_eq!(page.skipped, 1); + assert!(page.items[0].primary); + assert_eq!(page.items[0].access_role.as_deref(), Some("writer")); + assert_eq!(page.items[0].color, None); + assert_eq!(page.items[1].color.as_deref(), Some("#87d28e")); + assert_eq!( + page.items[1].owner_email.as_deref(), + Some("me@contoso.example") + ); +} + +#[test] +fn system_events() { + let base = SystemEvent { + event_id: "EK-123".to_string(), + calendar_id: "work".to_string(), + calendar_title: Some("Work".to_string()), + title: " Weekly planning ".to_string(), + start: utc("2026-03-10T16:00:00Z"), + end: utc("2026-03-10T16:30:00Z"), + notes: Some("Agenda\nhttps://acme.zoom.us/j/84512345678".to_string()), + url: Some("https://whereby.com/acme".to_string()), + attendees: vec![ + SystemAttendee { + name: Some("Me".to_string()), + email: Some("me@acme.example".to_string()), + is_self: true, + status: Response::Tentative, + }, + SystemAttendee { + name: Some("Ana".to_string()), + email: None, + is_self: false, + status: Response::Accepted, + }, + ], + is_recurring: true, + ..SystemEvent::default() + }; + let event = from_system_event(&base, Tz::Europe__London); + assert_eq!(event.id, "EK-123_20260310T160000Z"); + assert_eq!(event.series_id.as_deref(), Some("EK-123")); + assert_eq!(event.title, "Weekly planning"); + assert_eq!(event.source, SourceKind::System); + assert_eq!(event.self_response, Response::Tentative); + assert_eq!( + event.link.as_ref().map(|l| l.provider), + Some(Provider::Zoom) + ); + assert!(event.has_participants()); + + let all_day = SystemEvent { + all_day: true, + is_recurring: false, + start: utc("2026-03-09T15:00:00Z"), + end: utc("2026-03-10T14:59:59Z"), + notes: None, + ..base.clone() + }; + let event = from_system_event(&all_day, Tz::Asia__Tokyo); + assert_eq!(event.id, "EK-123"); + assert!(event.all_day); + assert_eq!(event.start, utc("2026-03-10T00:00:00Z")); + assert_eq!(event.end, utc("2026-03-11T00:00:00Z")); + assert_eq!( + event.link.as_ref().map(|l| l.provider), + Some(Provider::Whereby) + ); + + let json = serde_json::to_string(&base).unwrap(); + let back: SystemEvent = serde_json::from_str(&json).unwrap(); + assert_eq!(back, base); +} diff --git a/apps/desktop-gpui/diarize/Cargo.toml b/apps/desktop-gpui/diarize/Cargo.toml new file mode 100644 index 00000000000..a95fd4dcbff --- /dev/null +++ b/apps/desktop-gpui/diarize/Cargo.toml @@ -0,0 +1,52 @@ +[package] +name = "cap-diarize" +version = "0.1.0" +edition = "2024" +license = "AGPL-3.0" +publish = false + +[features] +default = ["onnx"] +# The speaker model itself. Without it the crate still offers the clustering, +# names and profile types, so the transcript model can use them without +# linking ONNX Runtime. +onnx = ["dep:ort"] + +[dependencies] +realfft = "3.5" +serde = { version = "1", features = ["derive"] } +serde_json = "1" +thiserror = "2" + +# Same ONNX Runtime build as the rest of the app (see crates/camera-effects): +# the shared library is loaded at run time and the app initialises it once. +[target.'cfg(target_os = "linux")'.dependencies] +ort = { version = "2.0.0-rc.12", default-features = false, features = ["std", "ndarray", "load-dynamic", "api-23"], optional = true } + +[target.'cfg(not(any(target_os = "macos", target_os = "windows", target_os = "linux")))'.dependencies] +ort = { version = "2.0.0-rc.12", default-features = false, features = ["std", "ndarray", "download-binaries", "copy-dylibs", "tls-native"], optional = true } + +[target.'cfg(target_os = "macos")'.dependencies] +ort = { version = "2.0.0-rc.12", default-features = false, features = ["std", "ndarray", "load-dynamic", "coreml", "api-23"], optional = true } + +[target.'cfg(target_os = "windows")'.dependencies] +ort = { version = "2.0.0-rc.12", default-features = false, features = ["std", "ndarray", "load-dynamic", "directml", "api-23"], optional = true } + +[dev-dependencies] +cap-meetings = { path = "../meetings" } +tempfile = "3" + +[[example]] +name = "diarize-eval" +required-features = ["onnx"] + +[[example]] +name = "hybrid-replay" + +[[example]] +name = "nemo-dump" +required-features = ["onnx"] + +[[example]] +name = "embed-dump" +required-features = ["onnx"] diff --git a/apps/desktop-gpui/diarize/examples/diarize-eval.rs b/apps/desktop-gpui/diarize/examples/diarize-eval.rs new file mode 100644 index 00000000000..f1a7e730d47 --- /dev/null +++ b/apps/desktop-gpui/diarize/examples/diarize-eval.rs @@ -0,0 +1,521 @@ +//! Runs one recording through the app's speech segmenter and the diarizer +//! the way a meeting would, and writes live and final labels for scoring. +//! +//! diarize-eval [mic.wav] [key=value ...] +//! +//! Keys: threads, optimize (0|1), profile (path), save_profile (path), +//! peek (0|1: label partial words with `MeetingDiarizer::peek` the way the +//! app does; 0 = the last speaker on the track), words (a JSON file +//! `{"mic": [ms], "system": [ms]}` of word middles, reported for each segment +//! as the app reports transcribed words), any `Config` field. Debug environment: DIARIZE_DUMMY (no model), +//! DIARIZE_SEGMENTS_ONLY (segmenter only), DIARIZE_SHAPES_LOG= (input +//! lengths the model saw). + +use std::path::Path; +use std::time::Instant; + +use cap_diarize::{ + Config, Embed, MeetingDiarizer, OnnxEmbedder, Segment, SegmentLabels, SpeakerId, SpeakerNames, + Track, VoiceProfile, +}; +use cap_meetings::segment::{Segmenter, SegmenterConfig}; + +const CHUNK: usize = 1_600; +const PARTIAL_EVERY: usize = 16_000; +const PARTIAL_MIN: usize = 24_000; +const PARTIAL_GROWTH: usize = 32_000; + +fn read_wav(path: &Path) -> Vec { + let bytes = std::fs::read(path).unwrap_or_else(|e| panic!("{}: {e}", path.display())); + assert_eq!(&bytes[0..4], b"RIFF"); + let mut at = 12; + let mut format = (0u16, 0u16, 0u32, 0u16); + while at + 8 <= bytes.len() { + let id = &bytes[at..at + 4]; + let len = u32::from_le_bytes(bytes[at + 4..at + 8].try_into().unwrap()) as usize; + let body = &bytes[at + 8..(at + 8 + len).min(bytes.len())]; + if id == b"fmt " { + format = ( + u16::from_le_bytes(body[0..2].try_into().unwrap()), + u16::from_le_bytes(body[2..4].try_into().unwrap()), + u32::from_le_bytes(body[4..8].try_into().unwrap()), + u16::from_le_bytes(body[14..16].try_into().unwrap()), + ); + } else if id == b"data" { + assert_eq!(format, (1, 1, 16_000, 16), "expected 16 kHz mono PCM16"); + return body + .as_chunks::<2>() + .0 + .iter() + .map(|b| i16::from_le_bytes(*b)) + .collect(); + } + at += 8 + len + (len & 1); + } + panic!("no data chunk in {}", path.display()); +} + +/// What the segmenter hands over while streaming. +enum Event<'a> { + /// A segment that is still open, offered for partial words with the + /// app's cadence (`capture.rs`: checked every second once 1.5 s long, + /// again after 2 s more). + Partial(Track, u64, &'a [f32]), + Closed(Track, u64, Vec), +} + +/// Feeds both tracks through the app's segmenter in 100 ms chunks, side by +/// side, and hands each closed segment over in the order the app would see +/// them, so nothing but the open segments is held in memory. +fn stream(tracks: &[(Track, &[i16])], seg: &[SegmenterConfig; 2], mut on_event: impl FnMut(Event<'_>)) { + let mut segmenters: Vec = tracks + .iter() + .map(|(track, _)| Segmenter::new(seg[usize::from(*track == Track::Mic)].clone())) + .collect(); + let mut since_partial = vec![0usize; tracks.len()]; + let mut last_partial: Vec> = vec![None; tracks.len()]; + let longest = tracks.iter().map(|(_, a)| a.len()).max().unwrap_or(0); + let mut chunk = Vec::with_capacity(CHUNK); + let mut closed = Vec::new(); + for start in (0..longest).step_by(CHUNK) { + for ((track, audio), segmenter) in tracks.iter().zip(&mut segmenters) { + if start >= audio.len() { + continue; + } + chunk.clear(); + chunk.extend( + audio[start..(start + CHUNK).min(audio.len())] + .iter() + .map(|s| *s as f32 / 32_768.0), + ); + for s in segmenter.push(start as u64, &chunk) { + closed.push((s.end_sample(), *track, s.start_sample, s.samples)); + } + } + closed.sort_by_key(|c| (c.0, c.1)); + for (_, track, start, samples) in closed.drain(..) { + on_event(Event::Closed(track, start, samples)); + } + for (i, ((track, audio), segmenter)) in tracks.iter().zip(&segmenters).enumerate() { + if start >= audio.len() { + continue; + } + since_partial[i] += CHUNK; + if since_partial[i] < PARTIAL_EVERY { + continue; + } + since_partial[i] = 0; + let Some((open_start, samples)) = segmenter.open_snapshot() else { + continue; + }; + if samples.len() < PARTIAL_MIN + || last_partial[i] + .is_some_and(|(s, n)| s == open_start && samples.len() < n + PARTIAL_GROWTH) + { + continue; + } + last_partial[i] = Some((open_start, samples.len())); + on_event(Event::Partial(*track, open_start, samples)); + } + } + for ((track, _), segmenter) in tracks.iter().zip(&mut segmenters) { + if let Some(s) = segmenter.flush() { + on_event(Event::Closed(*track, s.start_sample, s.samples)); + } + } +} + +fn set(config: &mut Config, key: &str, value: &str) -> bool { + let f = || value.parse::().unwrap(); + let u = || value.parse::().unwrap(); + match key { + "window_ms" => config.window_ms = u(), + "hop_ms" => config.hop_ms = u(), + "whole_max_ms" => config.whole_max_ms = u(), + "min_embed_ms" => config.min_embed_ms = u(), + "assign" => config.assign = f(), + "new_speaker" => config.new_speaker = f(), + "new_min_ms" => config.new_min_ms = u(), + "mic_new_speaker" => config.mic_new_speaker = f(), + "mic_new_min_ms" => config.mic_new_min_ms = u(), + "merge" => config.merge = f(), + "small_ms" => config.small_ms = u(), + "small_merge" => config.small_merge = f(), + "refine_rounds" => config.refine_rounds = u() as usize, + "split" => config.split = f(), + "split_min_ms" => config.split_min_ms = u(), + "confirm_ms" => config.confirm_ms = u(), + "min_speaker_ms" => config.min_speaker_ms = u(), + "profile_match" => config.profile_match = f(), + "profile_reject" => config.profile_reject = f(), + "enrol_min_ms" => config.enrol_min_ms = u(), + "enrol_max_unlike" => config.enrol_max_unlike = value.parse().unwrap(), + "echo_slack_ms" => config.echo_slack_ms = u(), + "max_embeddings" => config.max_embeddings = u() as usize, + "leak" => config.leak = f(), + "fold_ms" => config.fold_ms = u(), + "fold_share" => config.fold_share = f(), + "fold_merge" => config.fold_merge = f(), + _ => return false, + } + true +} + +/// Debug stand-in for the model: a coarse spectral-energy vector. +struct Dummy; + +impl Embed for Dummy { + fn dim(&self) -> usize { + 64 + } + + fn model_id(&self) -> &str { + "dummy" + } + + fn embed(&mut self, windows: &[&[f32]]) -> Result>, cap_diarize::Error> { + Ok(windows + .iter() + .map(|w| { + let mut v: Vec = w + .chunks(w.len().div_ceil(64)) + .map(|c| c.iter().map(|x| x.abs()).sum::() + 1e-3) + .collect(); + v.resize(64, 1e-3); + cap_diarize::embed::normalized(&v) + }) + .collect()) + } +} + +/// Debug wrapper: records every input length the model sees. +struct ShapeLog { + inner: OnnxEmbedder, + path: String, + seen: std::collections::BTreeMap, +} + +impl Embed for ShapeLog { + fn dim(&self) -> usize { + self.inner.dim() + } + + fn model_id(&self) -> &str { + self.inner.model_id() + } + + fn embed(&mut self, windows: &[&[f32]]) -> Result>, cap_diarize::Error> { + for w in windows { + *self.seen.entry(w.len()).or_default() += 1; + } + let _ = std::fs::write(&self.path, format!("{:?}", self.seen)); + self.inner.embed(windows) + } +} + +fn rss_kb() -> u64 { + std::process::Command::new("ps") + .args(["-o", "rss=", "-p", &std::process::id().to_string()]) + .output() + .ok() + .and_then(|o| String::from_utf8_lossy(&o.stdout).trim().parse().ok()) + .unwrap_or(0) +} + +fn labels_json( + track: Track, + start: u64, + len: usize, + labels: &SegmentLabels, + push_ms: f64, +) -> serde_json::Value { + serde_json::json!({ + "track": track, + "key": labels.key, + "startMs": start, + "endMs": start + len as u64 / 16, + "pushMs": push_ms, + "spans": labels.spans, + }) +} + +fn main() { + let args: Vec = std::env::args().skip(1).collect(); + let model = Path::new(&args[0]); + let out = Path::new(&args[1]); + let system = read_wav(Path::new(&args[2])); + let mut mic = None; + let mut config = Config::default(); + let mut threads = 1; + let mut optimize = false; + let mut profile = None; + let mut save_profile = None; + let mut peek = false; + let mut words: Option<(Vec, Vec)> = None; + let mut nemo: Option = None; + let mut nemo_threads = 2; + // nemo_after=1: Nemotron as the app runs it off Apple Silicon (speech kept until Stop, one pass after) + let mut nemo_after = false; + let mut seg_config = [SegmenterConfig::default(), SegmenterConfig::microphone()]; // system, mic (as the app) + for arg in &args[3..] { + match arg.split_once('=') { + Some(("threads", v)) => threads = v.parse().unwrap(), + Some(("optimize", v)) => optimize = v == "1", + Some(("profile", v)) => profile = VoiceProfile::load(Path::new(v)).ok(), + Some(("save_profile", v)) => save_profile = Some(v.to_string()), + Some(("peek", v)) => peek = v == "1", + Some(("nemo", v)) => nemo = Some(v.to_string()), + Some(("nemo_threads", v)) => nemo_threads = v.parse().unwrap(), + Some(("nemo_after", v)) => nemo_after = v == "1", + Some(("seg_min_db", v)) => seg_config.iter_mut().for_each(|c| c.min_level_db = v.parse().unwrap()), + Some(("seg_min_db_mic", v)) => seg_config[1].min_level_db = v.parse().unwrap(), + Some(("seg_max_ms_mic", v)) => seg_config[1].max_segment_ms = v.parse().unwrap(), + Some(("seg_enter_db", v)) => seg_config.iter_mut().for_each(|c| c.enter_db = v.parse().unwrap()), + Some(("words", v)) => { + let w: serde_json::Value = + serde_json::from_slice(&std::fs::read(v).expect("words file")).unwrap(); + let list = |k: &str| -> Vec { + let mut l: Vec = w[k] + .as_array() + .map(|a| a.iter().filter_map(|x| x.as_u64()).collect()) + .unwrap_or_default(); + l.sort_unstable(); + l + }; + words = Some((list("mic"), list("system"))); + } + Some((k, v)) => assert!(set(&mut config, k, v), "unknown key {k}"), + None => mic = Some(read_wav(Path::new(arg))), + } + } + + let load_started = Instant::now(); + let embedder = OnnxEmbedder::load(model, threads, optimize).expect("load model"); + let load_ms = load_started.elapsed().as_secs_f64() * 1000.0; + let model_id = embedder.info().id.clone(); + let embedder: Box = if std::env::var_os("DIARIZE_DUMMY").is_some() { + Box::new(Dummy) + } else { + match std::env::var("DIARIZE_SHAPES_LOG") { + Ok(path) => Box::new(ShapeLog { + inner: embedder, + path, + seen: std::collections::BTreeMap::new(), + }), + Err(_) => Box::new(embedder), + } + }; + let mut diarizer = MeetingDiarizer::new(config.clone(), embedder, profile); + let mut nemo_load_ms = 0.0; + if let Some(path) = &nemo { + let t = Instant::now(); + let model = cap_diarize::nemotron::OnnxNemotron::load(Path::new(path), nemo_threads).expect("load nemotron"); + nemo_load_ms = t.elapsed().as_secs_f64() * 1000.0; + if nemo_after { + diarizer.set_nemotron_with(Box::new(model), cap_diarize::nemotron::Schedule::AfterStop, || {}); + } else { + diarizer.set_nemotron(Box::new(model)); + } + } + + let mut tracks: Vec<(Track, &[i16])> = vec![(Track::System, &system)]; + if let Some(mic) = &mic { + tracks.push((Track::Mic, mic)); + } + let mut live = Vec::new(); + let mut push_times = Vec::new(); + let mut memory = vec![(0u64, rss_kb())]; + let mut segments = 0usize; + let mut speech_secs = 0f64; + let started = Instant::now(); + let segments_only = std::env::var_os("DIARIZE_SEGMENTS_ONLY").is_some(); + let mut partials = Vec::new(); + let mut last: [Option; 2] = [None, None]; + let mut heard: Vec = Vec::new(); + let mut names = SpeakerNames::new(None); + let slot = |track: Track| match track { + Track::Mic => 0, + Track::System => 1, + }; + stream(&tracks, &seg_config, |event| { + let (track, start, samples) = match event { + Event::Partial(track, start, samples) => { + if segments_only { + return; + } + let t = Instant::now(); + let peeked = if peek { + diarizer + .peek(Segment { + track, + key: start, + start_ms: start / 16, + samples, + }) + .expect("peek") + } else { + None + }; + partials.push(serde_json::json!({ + "track": track, + "key": start, + "atMs": (start + samples.len() as u64) / 16, + "last": last[slot(track)], + "peek": peeked, + "ms": t.elapsed().as_secs_f64() * 1000.0, + })); + return; + } + Event::Closed(track, start, samples) => (track, start, samples), + }; + if segments_only { + segments += 1; + if segments.is_multiple_of(10) { + memory.push(((start + samples.len() as u64) / 16, rss_kb())); + } + return; + } + let t = Instant::now(); + let labels = diarizer + .push(Segment { + track, + key: start, + start_ms: start / 16, + samples: &samples, + }) + .expect("push"); + let ms = t.elapsed().as_secs_f64() * 1000.0; + push_times.push(ms); + if let Some((mic_words, system_words)) = &words { + let list = match track { + Track::Mic => mic_words, + Track::System => system_words, + }; + let (a, b) = (start / 16, (start + samples.len() as u64) / 16); + let from = list.partition_point(|&m| m < a); + let to = list.partition_point(|&m| m < b); + diarizer.transcribed(track, start, &list[from..to]); + } + segments += 1; + speech_secs += samples.len() as f64 / 16_000.0; + if let Some(span) = labels.spans.iter().rev().find(|s| !s.echo) { + last[slot(track)] = Some(span.speaker); + } + for span in labels.spans.iter().filter(|s| !s.echo) { + if !heard.contains(&span.speaker) { + heard.push(span.speaker); + } + } + let speaking: Vec<_> = diarizer + .speakers() + .into_iter() + .filter(|s| heard.contains(&s.id)) + .collect(); + names.observe(&speaking); + live.push(labels_json(track, start / 16, samples.len(), &labels, ms)); + if live.len().is_multiple_of(10) { + memory.push(((start + samples.len() as u64) / 16, rss_kb())); + } + }); + memory.push((u64::MAX, rss_kb())); + let live_secs = started.elapsed().as_secs_f64(); + let live_speakers = diarizer.speakers(); + let t = Instant::now(); + let mut pass = diarizer.finish(); + let final_ms = t.elapsed().as_secs_f64() * 1000.0; + // the recording is over: a CPU probe reading stdout splits recording from after Stop here + { + use std::io::Write as _; + println!("AF_PHASE stop"); + let _ = std::io::stdout().flush(); + } + let mut after_stop: Option = None; + if let Some(late) = diarizer.take_late() { + let t = Instant::now(); + after_stop = Some(match late.wait() { + Ok((corrected, h)) => { + pass = corrected; + serde_json::json!({"ms": t.elapsed().as_secs_f64() * 1000.0, "relabelledMs": h.relabelled_ms, + "newVoices": h.new_voices, "mergedIds": h.merged_ids, "error": null}) + } + Err(e) => serde_json::json!({"ms": t.elapsed().as_secs_f64() * 1000.0, "error": e}), + }); + } + { + use std::io::Write as _; + println!("AF_PHASE done"); + let _ = std::io::stdout().flush(); + } + let label_map = |names: &SpeakerNames, ids: &[SpeakerId]| -> serde_json::Value { + ids.iter() + .filter_map(|id| { + names + .label(*id) + .map(|l| (id.0.to_string(), serde_json::Value::from(format!("{l:?}")))) + }) + .collect::>() + .into() + }; + let mut all_ids: Vec = heard.clone(); + all_ids.extend(pass.speakers.iter().map(|s| s.id)); + all_ids.sort(); + all_ids.dedup(); + let live_names = label_map(&names, &all_ids); + let name_changes = names.apply_final(&pass).len(); + let final_names = label_map(&names, &all_ids); + if let (Some(path), Some(profile)) = (&save_profile, &pass.enrolment) { + profile.save(Path::new(path)).expect("save profile"); + } + let stats = diarizer.stats(); + let mut sorted = push_times.clone(); + sorted.sort_by(f64::total_cmp); + let pct = |p: f64| { + sorted + .get(((sorted.len() as f64 - 1.0) * p) as usize) + .copied() + .unwrap_or(0.0) + }; + let audio_secs = system.len().max(mic.as_ref().map_or(0, Vec::len)) as f64 / 16_000.0; + let report = serde_json::json!({ + "model": model_id, + "config": format!("{config:?}"), + "threads": threads, + "optimize": optimize, + "audioSecs": audio_secs, + "speechSecs": speech_secs, + "segments": segments, + "windows": stats.windows, + "loadMs": load_ms, + "liveSecs": live_secs, + "embedSecs": stats.embed_time.as_secs_f64(), + "finalMs": final_ms, + "pushMs": {"p50": pct(0.5), "p90": pct(0.9), "p99": pct(0.99), "max": pct(1.0)}, + "embeddingsKept": stats.embeddings_kept, + "rssKb": memory, + "liveSpeakers": live_speakers, + "peek": peek, + "reusedWindows": stats.reused_windows, + "micAbsorbedMs": stats.mic_absorbed_ms, + "nemotron": {"model": nemo, "threads": nemo_threads, "loadMs": nemo_load_ms, "applied": stats.nemotron_applied, + "relabelledMs": stats.nemotron_relabelled_ms, "newVoices": stats.nemotron_new_voices, + "mergedIds": stats.nemotron_merged_ids, "error": diarizer.nemotron_error(), + "activeSecs": diarizer.nemotron_voices().map(|v| [v[0].active_secs().to_vec(), v[1].active_secs().to_vec()]), + "afterStop": after_stop}, + "earlyWindows": stats.early_windows, + "partials": partials, + "liveNames": live_names, + "finalNames": final_names, + "nameChanges": name_changes, + "live": live, + "final": pass, + }); + std::fs::write(out, serde_json::to_vec(&report).unwrap()).expect("write report"); + // DIARIZE_NEMO_DUMP=: the Nemotron voices of both tracks, to try final-pass rules offline + if let (Ok(dir), Some(v)) = (std::env::var("DIARIZE_NEMO_DUMP"), diarizer.nemotron_voices()) { + let stem = out.file_stem().unwrap().to_string_lossy().to_string(); + for (name, tv) in [("mic", &v[0]), ("system", &v[1])] { + std::fs::write(format!("{dir}/{stem}.{name}.u8"), tv.raw()).expect("dump"); + std::fs::write(format!("{dir}/{stem}.{name}.parts.json"), serde_json::to_vec(&tv.parts()).unwrap()).expect("dump"); + } + } +} diff --git a/apps/desktop-gpui/diarize/examples/embed-dump.rs b/apps/desktop-gpui/diarize/examples/embed-dump.rs new file mode 100644 index 00000000000..ab277c10073 --- /dev/null +++ b/apps/desktop-gpui/diarize/examples/embed-dump.rs @@ -0,0 +1,190 @@ +//! Feature parity, speed and memory checks for the speaker model. +//! +//! embed-dump fbank +//! embed-dump embed ... +//! embed-dump bench +//! embed-dump shapes +//! embed-dump segments +//! +//! fbank and embed print JSON (the feature matrix, or one embedding per +//! window cut from the start of the file). bench prints milliseconds per +//! window (FRESH copies each input first). shapes and segments print resident +//! memory while embedding many input lengths, or random stretches of audio +//! (SHORT, FIXED_START, SLICE_ONLY narrow down what drives growth). + +use std::path::Path; + +use cap_diarize::fbank::{Fbank, Window}; +use cap_diarize::{Embed, OnnxEmbedder}; + +fn read(path: &str) -> Vec { + std::fs::read(path) + .expect("read audio") + .as_chunks::<4>() + .0 + .iter() + .map(|b| f32::from_le_bytes(*b)) + .collect() +} + +fn main() { + let args: Vec = std::env::args().skip(1).collect(); + if args[0] == "fbank" { + let audio = read(&args[1]); + let window = if args[2] == "hamming" { + Window::Hamming + } else { + Window::Povey + }; + let mut out = Vec::new(); + Fbank::new(window).compute(&audio, args[3].parse().unwrap(), args[4] == "1", &mut out); + println!("{}", serde_json::to_string(&out).unwrap()); + return; + } + if args[0] == "segments" { + let mut embedder = OnnxEmbedder::load( + Path::new(&args[1]), + 1, + std::env::var_os("OPTIMIZE").is_some(), + ) + .expect("load model"); + let audio = read(&args[2]); + let count: usize = args[3].parse().unwrap(); + let rss = || { + std::process::Command::new("ps") + .args(["-o", "rss=", "-p", &std::process::id().to_string()]) + .output() + .map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string()) + .unwrap_or_default() + }; + let mut seed = 7u64; + for i in 0..count { + seed = seed.wrapping_mul(6_364_136_223_846_793_005).wrapping_add(1); + let mut len = 24_000 + (seed >> 33) as usize % 360_000; + let mut start = (seed >> 13) as usize % (audio.len() - len); + if std::env::var_os("FIXED_START").is_some() { + start = 0; + } + if std::env::var_os("SLICE_ONLY").is_some() { + len = 24_000; + let window = &audio[start..start + len]; + embedder.embed(&[window]).expect("embed"); + if i.is_multiple_of(10) { + println!("{i} segments rss {}", rss()); + } + continue; + } + let segment: Vec = if std::env::var_os("REUSE").is_some() { + audio[start..start + len] + .iter() + .map(|x| x * 1.0) + .collect::>() + } else { + audio[start..start + len].to_vec() + }; + let segment = if std::env::var_os("SHORT").is_some() { + segment[..segment.len().min(48_000)].to_vec() + } else { + segment + }; + let windows: Vec<&[f32]> = (0..) + .map(|k| k * 12_000) + .take_while(|a| a + 24_000 <= segment.len()) + .map(|a| &segment[a..a + 24_000]) + .collect(); + embedder.embed(&windows).expect("embed"); + if i.is_multiple_of(10) { + println!("{i} segments rss {}", rss()); + } + } + #[cfg(target_os = "macos")] + { + unsafe extern "C" { + fn malloc_zone_pressure_relief(zone: *mut std::ffi::c_void, goal: usize) -> usize; + } + let freed = unsafe { malloc_zone_pressure_relief(std::ptr::null_mut(), 0) }; + println!("released {} MB, rss now {}", freed >> 20, rss()); + } + return; + } + if args[0] == "shapes" { + let mut embedder = OnnxEmbedder::load( + Path::new(&args[1]), + 1, + std::env::var_os("OPTIMIZE").is_some(), + ) + .expect("load model"); + let audio = read(&args[2]); + let step: usize = args[3].parse().unwrap(); + let rss = || { + std::process::Command::new("ps") + .args(["-o", "rss=", "-p", &std::process::id().to_string()]) + .output() + .map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string()) + .unwrap_or_default() + }; + println!("start rss {}", rss()); + for (i, len) in (8_000..48_000).step_by(step).enumerate() { + embedder.embed(&[&audio[..len]]).expect("embed"); + if i.is_multiple_of(50) { + println!("{i} shapes rss {}", rss()); + } + } + for _ in 0..3 { + for len in (8_000..48_000).step_by(step) { + embedder.embed(&[&audio[..len]]).expect("embed"); + } + println!("repeat rss {}", rss()); + } + return; + } + if args[0] == "bench" { + let mut embedder = OnnxEmbedder::load( + Path::new(&args[1]), + 1, + std::env::var_os("OPTIMIZE").is_some(), + ) + .expect("load model"); + let audio = read(&args[2]); + let len: usize = args[3].parse().unwrap(); + let batch: usize = args[4].parse().unwrap(); + let rounds: usize = args[5].parse().unwrap(); + let windows: Vec<&[f32]> = (0..batch) + .map(|i| &audio[(i * len) % (audio.len() - len)..][..len]) + .collect(); + embedder.embed(&windows).expect("warm up"); + let started = std::time::Instant::now(); + let fresh = std::env::var_os("FRESH").is_some(); + for _ in 0..rounds { + if fresh { + let copies: Vec> = windows.iter().map(|w| w.to_vec()).collect(); + let views: Vec<&[f32]> = copies.iter().map(Vec::as_slice).collect(); + embedder.embed(&views).expect("embed"); + } else { + embedder.embed(&windows).expect("embed"); + } + } + let ms = started.elapsed().as_secs_f64() * 1000.0 / (rounds * batch) as f64; + let rss = std::process::Command::new("ps") + .args(["-o", "rss=", "-p", &std::process::id().to_string()]) + .output() + .map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string()) + .unwrap_or_default(); + eprintln!("rss after {} windows: {rss} KB", rounds * batch); + println!("{{\"msPerWindow\": {ms:.3}}}"); + return; + } + let mut embedder = OnnxEmbedder::load( + Path::new(&args[1]), + 1, + std::env::var_os("OPTIMIZE").is_some(), + ) + .expect("load model"); + let audio = read(&args[2]); + let mut out = Vec::new(); + for len in &args[3..] { + let len: usize = len.parse().unwrap(); + out.extend(embedder.embed(&[&audio[..len]]).expect("embed")); + } + println!("{}", serde_json::to_string(&out).unwrap()); +} diff --git a/apps/desktop-gpui/diarize/examples/hybrid-replay.rs b/apps/desktop-gpui/diarize/examples/hybrid-replay.rs new file mode 100644 index 00000000000..b18dc006049 --- /dev/null +++ b/apps/desktop-gpui/diarize/examples/hybrid-replay.rs @@ -0,0 +1,26 @@ +//! hybrid-replay : applies the crate's final-pass hybrid to a harness report made +//! without Nemotron, using Nemotron voices dumped by another run (`.mic.u8`, `.mic.parts.json`, `.system.*`). +//! Writes the report with the new final labels and the hybrid's stats under "replay". + +use cap_diarize::FinalPass; +use cap_diarize::nemotron::TrackVoices; + +fn load(stem: &str, track: &str) -> TrackVoices { + let raw = std::fs::read(format!("{stem}.{track}.u8")).unwrap_or_default(); + let parts: Vec<(usize, u64, usize)> = std::fs::read(format!("{stem}.{track}.parts.json")) + .ok() + .and_then(|b| serde_json::from_slice(&b).ok()) + .unwrap_or_default(); + TrackVoices::from_raw(raw, &parts) +} + +fn main() { + let args: Vec = std::env::args().skip(1).collect(); + let mut report: serde_json::Value = serde_json::from_slice(&std::fs::read(&args[0]).expect("run json")).unwrap(); + let mut pass: FinalPass = serde_json::from_value(report["final"].clone()).expect("final pass"); + let voices = [load(&args[1], "mic"), load(&args[1], "system")]; + let stats = cap_diarize::hybrid::apply(&mut pass, &voices); + report["final"] = serde_json::to_value(&pass).unwrap(); + report["replay"] = serde_json::json!({"relabelledMs": stats.relabelled_ms, "newVoices": stats.new_voices, "tracksKept": stats.tracks_kept}); + std::fs::write(&args[2], serde_json::to_vec(&report).unwrap()).expect("write"); +} diff --git a/apps/desktop-gpui/diarize/examples/nemo-dump.rs b/apps/desktop-gpui/diarize/examples/nemo-dump.rs new file mode 100644 index 00000000000..658b2a83f8b --- /dev/null +++ b/apps/desktop-gpui/diarize/examples/nemo-dump.rs @@ -0,0 +1,47 @@ +//! nemo-dump [threads] [segment_s]: streams a wav through the crate's Nemotron path +//! (as one segment, or cut into `segment_s` pieces pushed back to back) and writes the 10 ms voice probabilities +//! (u8, 8 per frame) of the joined timeline, for comparison with the reference implementation. + +use std::path::Path; +use std::time::Instant; + +use cap_diarize::Track; +use cap_diarize::nemotron::{NemoRunner, OnnxNemotron}; + +/// 16 kHz mono PCM16 wav (the eval corpus format). +fn read_wav(path: &Path) -> Vec { + let bytes = std::fs::read(path).expect("wav"); + let mut at = 12; + while at + 8 <= bytes.len() { + let len = u32::from_le_bytes(bytes[at + 4..at + 8].try_into().unwrap()) as usize; + if &bytes[at..at + 4] == b"data" { + let body = &bytes[at + 8..(at + 8 + len).min(bytes.len())]; + return body.chunks_exact(2).map(|c| i16::from_le_bytes([c[0], c[1]]) as f32 / 32768.0).collect(); + } + at += 8 + len + (len & 1); + } + panic!("no data chunk") +} + +fn main() { + let args: Vec = std::env::args().skip(1).collect(); + let threads: usize = args.get(3).map_or(2, |v| v.parse().unwrap()); + let seg_s: Option = args.get(4).map(|v| v.parse().unwrap()); + let samples = read_wav(Path::new(&args[1])); + let model = OnnxNemotron::load(Path::new(&args[0]), threads).expect("model"); + let t = Instant::now(); + let runner = NemoRunner::start(Box::new(model)); + match seg_s { + None => runner.push(Track::System, 0, &samples), + Some(s) => { + let n = (s * 16_000.0) as usize; + for (k, chunk) in samples.chunks(n).enumerate() { + runner.push(Track::System, (k * n / 16) as u64, chunk); + } + } + } + let [_, sys] = runner.finish().expect("voices"); + let secs = t.elapsed().as_secs_f64(); + std::fs::write(&args[2], sys.raw()).expect("write"); + eprintln!("frames {} voices {} wall {:.2}s audio {:.0}s", sys.frames(), sys.voices(), secs, samples.len() as f64 / 16_000.0); +} diff --git a/apps/desktop-gpui/diarize/src/cluster.rs b/apps/desktop-gpui/diarize/src/cluster.rs new file mode 100644 index 00000000000..63b04e1dc5e --- /dev/null +++ b/apps/desktop-gpui/diarize/src/cluster.rs @@ -0,0 +1,84 @@ +pub fn dot(a: &[f32], b: &[f32]) -> f32 { + a.iter().zip(b).map(|(x, y)| x * y).sum() +} + +pub fn normalize(v: &mut [f32]) { + let norm = dot(v, v).sqrt(); + if norm > 0.0 { + for x in v { + *x /= norm; + } + } +} + +/// A weighted running mean of unit vectors; `unit` is kept normalised so +/// comparisons are plain dot products. +#[derive(Clone, Debug)] +pub struct Centroid { + sum: Vec, + pub unit: Vec, + pub weight: f32, +} + +impl Centroid { + pub fn new(dim: usize) -> Self { + Self { + sum: vec![0.0; dim], + unit: vec![0.0; dim], + weight: 0.0, + } + } + + pub fn from(v: &[f32], weight: f32) -> Self { + let mut c = Self::new(v.len()); + c.add(v, weight); + c + } + + pub fn add(&mut self, v: &[f32], weight: f32) { + for (s, x) in self.sum.iter_mut().zip(v) { + *s += x * weight; + } + self.weight += weight; + self.unit.copy_from_slice(&self.sum); + normalize(&mut self.unit); + } + + pub fn absorb(&mut self, other: &Centroid) { + for (s, x) in self.sum.iter_mut().zip(&other.sum) { + *s += x; + } + self.weight += other.weight; + self.unit.copy_from_slice(&self.sum); + normalize(&mut self.unit); + } + + pub fn similarity(&self, v: &[f32]) -> f32 { + dot(&self.unit, v) + } +} + +/// Greedy one-to-one matching of rows to columns by the largest weights, +/// ignoring zero weights. Returns `(row, column)` pairs. +pub fn greedy_match(weights: &[Vec]) -> Vec<(usize, usize)> { + let mut cells: Vec<(u64, usize, usize)> = weights + .iter() + .enumerate() + .flat_map(|(r, row)| row.iter().enumerate().map(move |(c, w)| (*w, r, c))) + .filter(|(w, _, _)| *w > 0) + .collect(); + cells.sort_by(|a, b| b.0.cmp(&a.0).then(a.1.cmp(&b.1)).then(a.2.cmp(&b.2))); + let rows = weights.len(); + let cols = weights.iter().map(Vec::len).max().unwrap_or(0); + let mut used_r = vec![false; rows]; + let mut used_c = vec![false; cols]; + let mut out = Vec::new(); + for (_, r, c) in cells { + if !used_r[r] && !used_c[c] { + used_r[r] = true; + used_c[c] = true; + out.push((r, c)); + } + } + out +} diff --git a/apps/desktop-gpui/diarize/src/diarizer.rs b/apps/desktop-gpui/diarize/src/diarizer.rs new file mode 100644 index 00000000000..d248241ba66 --- /dev/null +++ b/apps/desktop-gpui/diarize/src/diarizer.rs @@ -0,0 +1,2406 @@ +use std::time::{Duration, Instant}; + +use crate::cluster::{Centroid, dot, greedy_match, normalize}; +use crate::{ + Embed, Error, FinalPass, SegmentLabels, Span, SpeakerId, SpeakerInfo, Track, VoiceProfile, +}; + +#[cfg(test)] +mod tests; + +const SAMPLES_PER_MS: u64 = 16; +const MAX_SPLITS: usize = 8; +const WINDOW_GRID_MS: u64 = 250; +const ECHO_COVERAGE: f64 = 0.6; +const SPLIT_SEEDS: usize = 6; +/// Speech level is kept every 20 ms of each segment, as 0.5 dB steps above +/// -100 dB, to tell the call heard through the speakers from a person. +const LEVEL_MS: u64 = 20; +/// The microphone hears the speakers up to this much after the call track +/// (output and room delay), or up to 100 ms before it: the call track can +/// arrive later than the sound leaves the speakers (about 50 ms measured on +/// a MacBook). Much earlier than that, the call track is the copy instead +/// (the user's voice returned by the far end), which the reverse check +/// below catches. +const LEAK_LAG_FRAMES: (i64, i64) = (-5, 20); +/// A copy is the quieter of the two against its own track's speech: a +/// microphone stretch this much louder (dB, relative to the microphone's +/// usual level) than the call is (relative to the call's) is the original. +const LEAK_LOUDER_DB: f32 = 3.0; +const LEAK_MIN_FRAMES: u64 = 10; +/// Share of a microphone stretch the call must be speaking through for the +/// stretch to be compared with it. +const LEAK_MIN_COVER: f64 = 0.5; +/// Level frames kept per track (about 5.5 hours of speech). +const LEVELS_MAX: usize = 1_000_000; +/// A microphone voice is judged on its speech from this far back that the +/// call's levels can be compared with: mostly the call's rhythm (the share +/// below) and it is the call heard through the speakers, hardly ever and +/// it is a person. +const LEAK_REVIEW_MS: u64 = 120_000; +const LEAK_DECIDE_MS: u64 = 4_000; +const LEAK_VOICE_SHARE: f64 = 0.8; +const PERSON_DECIDE_MS: u64 = 8_000; +/// A microphone voice heard this long and this share of its reviewed speech +/// while the call is silent, in at least this many of the call's silences, +/// is a person: the speakers only play the call while it speaks. "Silent" is known once the call's levels run this far +/// past the stretch, and means the call speaks through at most +/// `CALL_SILENT_COVER` of it (its delay either side included). +const ALONE_PERSON_SHARE: f64 = 0.25; +const ALONE_PERSON_SILENCES: usize = 2; +const CALL_KNOWN_AFTER_MS: u64 = 1_000; +const CALL_SILENT_COVER: f64 = 0.1; +const PERSON_VOICE_SHARE: f64 = 0.2; +/// At the end, a whole microphone voice heard mostly while the call talks +/// (this share of its speech comparable with the call) whose speech follows +/// the call's levels this closely on average is the call too: what is left +/// of the speakers once someone talks over them. +const LEAK_VOICE_TESTED: f64 = 0.6; +const LEAK_VOICE_MEAN: f32 = 0.45; +/// A minor microphone voice heard this much inside the call's speech is no +/// one new: the call through the speakers, or the user talking over it. +const OVER_CALL_COVERAGE: f64 = 0.9; +const OVER_CALL_MAX_MS: u64 = 60_000; +/// A voice whose transcribed speech holds fewer words a second than this, +/// and well under the track's talker, is laughter, music or noise in a +/// voice of its own, not a person: it folds into the nearest person. +const WORDLESS_RATE: f64 = 1.0; +const WORDLESS_OF_MAIN: f64 = 0.35; +const WORDLESS_MIN_MS: u64 = 5_000; +const WORDLESS_TRANSCRIBED: f64 = 0.6; +/// It folds into the nearest voice that talks: at least this share of the +/// track's best rate. +const TALKER_OF_MAIN: f64 = 0.5; +/// How far back `transcribed` looks for its segment's stretches. +const TRANSCRIBED_LOOKBACK: usize = 2_000; + +#[derive(Clone, Debug, PartialEq)] +pub struct Config { + /// Long segments are embedded in windows this long, `hop_ms` apart. + pub window_ms: u64, + pub hop_ms: u64, + /// Segments up to this long are embedded whole. + pub whole_max_ms: u64, + /// Shorter segments are not embedded; they inherit a neighbour's speaker. + pub min_embed_ms: u64, + /// Cosine similarity at which a window joins a speaker and refines them. + pub assign: f32, + /// Below this similarity to every speaker, a long enough stretch of + /// speech becomes a new speaker. Between the two it joins the nearest + /// speaker without refining them. + pub new_speaker: f32, + pub new_min_ms: u64, + /// A new voice is shown as a new person once this much of it arrives in + /// one go, or once a later stretch of speech confidently matches it. + /// Until then its speech is shown as the nearest known person, so a + /// cough or two people talking over each other never flashes a stranger. + pub confirm_ms: u64, + /// The microphone usually holds only the user, so new voices there need + /// stronger evidence. + pub mic_new_speaker: f32, + pub mic_new_min_ms: u64, + /// Final pass: speakers this similar are one person. + pub merge: f32, + /// Final pass: speakers with less speech than this fold into their + /// nearest neighbour when at least `small_merge` similar, and below + /// `min_speaker_ms` they always do. + pub small_ms: u64, + pub small_merge: f32, + pub min_speaker_ms: u64, + /// Final pass: a minor voice (less than `fold_ms` and `fold_share` of + /// its track's speech) this similar to a bigger one is a part of it. + pub fold_ms: u64, + pub fold_share: f32, + pub fold_merge: f32, + pub refine_rounds: usize, + /// Final pass: a speaker whose speech falls into two groups this unlike, + /// each with at least `split_min_ms`, was two people heard as one. + pub split: f32, + pub split_min_ms: u64, + /// Similarity to the voice profile that identifies the user. + pub profile_match: f32, + /// A first microphone voice this unlike the profile is someone else. + pub profile_reject: f32, + pub enrol_min_ms: u64, + /// Share of the user's speech allowed to sound unlike the voice being + /// learnt; above it the meeting had other people on the microphone. + pub enrol_max_unlike: f64, + /// A voice is the user by loudness when the microphone hears it this + /// much louder (dB) than the voice with the most speech, and it has at + /// least `user_min_ms` and `user_min_share` of the microphone's speech. + pub user_louder_db: f64, + pub user_min_ms: u64, + pub user_min_share: f64, + /// How far apart in time two tracks' speech may be and still be the + /// same sound arriving twice. + pub echo_slack_ms: u64, + /// Embeddings kept for the final pass; older pieces beyond this keep only + /// their label, which bounds memory in very long meetings. + pub max_embeddings: usize, + /// A microphone stretch whose loudness rises and falls with the call's + /// this closely (correlation of their levels) is the call coming out of + /// the speakers, whatever it sounds like after the trip through the room. + pub leak: f32, +} + +impl Default for Config { + fn default() -> Self { + Self { + window_ms: 1_500, + hop_ms: 750, + whole_max_ms: 3_000, + min_embed_ms: 500, + assign: 0.55, + new_speaker: 0.3, + new_min_ms: 1_500, + confirm_ms: 4_000, + mic_new_speaker: 0.3, + mic_new_min_ms: 3_000, + merge: 0.62, + small_ms: 8_000, + small_merge: 0.25, + min_speaker_ms: 1_500, + fold_ms: 30_000, + fold_share: 0.1, + fold_merge: 0.5, + refine_rounds: 4, + split: 0.55, + split_min_ms: 10_000, + profile_match: 0.5, + profile_reject: 0.15, + enrol_min_ms: 30_000, + enrol_max_unlike: 0.08, + user_louder_db: 2.0, + user_min_ms: 8_000, + user_min_share: 0.15, + echo_slack_ms: 1_000, + max_embeddings: 40_000, + leak: 0.6, + } + } +} + +#[derive(Clone, Copy, Debug)] +pub struct Segment<'a> { + pub track: Track, + /// The caller's key for the segment, reported back with its labels. + pub key: u64, + pub start_ms: u64, + pub samples: &'a [f32], +} + +#[derive(Clone, Copy, Debug, Default, PartialEq)] +pub struct Stats { + pub segments: u64, + pub windows: u64, + pub embed_calls: u64, + pub embed_time: Duration, + pub total_time: Duration, + pub embeddings_kept: usize, + /// Windows embedded early by `peek` and reused when the segment closed. + pub reused_windows: u64, + /// Windows `peek` embedded (reused or not). + pub early_windows: u64, + pub mic_absorbed_ms: u64, + /// Final pass: whether the Nemotron voices were applied, the speech they + /// moved to another speaker, the people they added and the ids they merged. + pub nemotron_applied: bool, + pub nemotron_relabelled_ms: u64, + pub nemotron_new_voices: u32, + pub nemotron_merged_ids: u32, +} + +/// Window embeddings of a segment that is still open, computed by `peek` +/// and reused when it closes. A window is only reused when its samples are +/// exactly the ones it was computed from. +struct Early { + key: u64, + samples: Vec, + windows: Vec<((usize, usize), Vec)>, +} + +impl Early { + fn get(&self, key: u64, samples: &[f32], (a, b): (usize, usize)) -> Option<&Vec> { + if key != self.key || b > samples.len() || b > self.samples.len() { + return None; + } + self.windows + .iter() + .find(|(w, _)| *w == (a, b)) + .filter(|_| samples[a..b] == self.samples[a..b]) + .map(|(_, e)| e) + } +} + +struct Cluster { + id: SpeakerId, + track: Track, + centroid: Centroid, + first_ms: u64, + speech_ms: u64, + /// Speech-weighted signal power, for how loud the microphone hears them. + power_ms: f64, + confirmed: bool, + created_key: u64, + /// Whether this microphone voice is the call coming out of the + /// speakers; `None` until the call's levels have said. + leak: Option, +} + +struct Piece { + track: Track, + key: u64, + start_ms: u64, + end_ms: u64, + embedding: Option>, + /// The voice this stretch matched, which may still be provisional. + cluster: Option, + /// The person shown for it. + speaker: Option, + echo: bool, + /// Mean signal power of the stretch. + power: f32, + /// How closely the stretch follows the call's levels, once known. + leak: Option, + /// Words transcribed in the stretch, once its segment is transcribed. + words: Option, +} + +impl Piece { + fn ms(&self) -> u64 { + self.end_ms.saturating_sub(self.start_ms) + } +} + +#[derive(Clone, Copy, PartialEq)] +enum Decision { + Join(SpeakerId, bool), + Echo(SpeakerId), + Unknown, +} + +/// One segment's speech level, every `LEVEL_MS` from `first` (in frames +/// from the start of the meeting). +struct Levels { + first: u64, + q: Vec, +} + +pub struct MeetingDiarizer { + config: Config, + embedder: Box, + profile: Option, + clusters: Vec, + pieces: Vec, + next_id: u32, + user: Option, + embeddings_kept: usize, + oldest_embedding: usize, + /// Microphone speech in an unknown voice too short to become anyone, + /// folded into the nearest speaker. + mic_absorbed_ms: u64, + stats: Stats, + early: [Option; 2], + levels: [Vec; 2], + level_frames: [usize; 2], + /// Each track's level summed over the frames where the other track + /// speaks too, and their count: the usual level of what is heard while + /// both are active (on the microphone, the call through the speakers). + level_sum: [(f64, u64); 2], + /// Nemotron 3 Diarization on its own thread, when its model is available. + nemotron: Option, + nemotron_error: Option, + nemotron_voices: Option<[crate::nemotron::TrackVoices; 2]>, + /// The after-Stop Nemotron pass this meeting's final pass left running. + late: Option, +} + +/// Nemotron hearing a finished meeting after Stop (`Schedule::AfterStop`): the +/// final pass went out with the speaker model's labels, and this corrects it +/// once the model is done. +pub struct LateNemotron { + voices: std::sync::mpsc::Receiver>, + pass: FinalPass, +} + +impl LateNemotron { + /// Blocks until Nemotron is done (it works on its own low-priority + /// thread): the final pass with its corrections, or why there are none. + pub fn wait(self) -> Result<(FinalPass, crate::hybrid::HybridStats), String> { + let voices = self.voices.recv().unwrap_or_else(|_| Err("the model thread stopped".into()))?; + let mut pass = self.pass; + let stats = crate::hybrid::apply(&mut pass, &voices); + Ok((pass, stats)) + } +} + +impl MeetingDiarizer { + pub fn new(config: Config, embedder: Box, profile: Option) -> Self { + let profile = profile.filter(|p| p.usable_with(embedder.model_id(), embedder.dim())); + Self { + config, + embedder, + profile, + clusters: Vec::new(), + pieces: Vec::new(), + next_id: 1, + user: None, + embeddings_kept: 0, + oldest_embedding: 0, + mic_absorbed_ms: 0, + stats: Stats::default(), + early: [None, None], + levels: [Vec::new(), Vec::new()], + level_frames: [0, 0], + level_sum: [(0.0, 0), (0.0, 0)], + nemotron: None, + nemotron_error: None, + nemotron_voices: None, + late: None, + } + } + + /// Lets Nemotron 3 Diarization decide who is who at the end (see + /// `hybrid`). Every segment pushed from now on is fed to it. + pub fn set_nemotron(&mut self, model: Box) { + self.nemotron = Some(crate::nemotron::NemoRunner::start(model)); + } + + /// As `set_nemotron`, on `schedule`: with `AfterStop` the final pass does + /// not wait for Nemotron, and `take_late` hands over the correction. + pub fn set_nemotron_with( + &mut self, + model: Box, + schedule: crate::nemotron::Schedule, + lower_priority: fn(), + ) { + self.nemotron = Some(crate::nemotron::NemoRunner::start_with(model, schedule, lower_priority)); + } + + /// Stops an `AfterStop` Nemotron before the final pass, when another + /// source of its voices (the farm) stands in. + pub fn cancel_after_stop_nemotron(&mut self) { + if self.nemotron.as_ref().is_some_and(|n| n.schedule() == crate::nemotron::Schedule::AfterStop) { + self.nemotron = None; + } + } + + /// The after-Stop correction the last `finish` left running. + pub fn take_late(&mut self) -> Option { + self.late.take() + } + + /// The Nemotron voices the final pass used (mic, system), for diagnostics. + pub fn nemotron_voices(&self) -> Option<&[crate::nemotron::TrackVoices; 2]> { + self.nemotron_voices.as_ref() + } + + /// Why Nemotron was not applied at the end, when it was set and failed. + pub fn nemotron_error(&self) -> Option<&str> { + self.nemotron_error.as_deref() + } + + pub fn config(&self) -> &Config { + &self.config + } + + pub fn stats(&self) -> Stats { + Stats { + embeddings_kept: self.embeddings_kept, + mic_absorbed_ms: self.mic_absorbed_ms, + ..self.stats + } + } + + pub fn user(&self) -> Option { + self.user + } + + pub fn speakers(&self) -> Vec { + self.clusters + .iter() + .filter(|c| c.confirmed && c.leak != Some(true)) + .map(|c| SpeakerInfo { + id: c.id, + track: c.track, + first_ms: c.first_ms, + speech_ms: c.speech_ms, + is_user: Some(c.id) == self.user, + }) + .collect() + } + + /// Labels one closed speech segment. Segments of one track come in time + /// order while a meeting runs; one transcribed again later (a repair + /// from the saved audio) is labelled the same way, against the levels + /// and voices around its own time. + pub fn push(&mut self, segment: Segment<'_>) -> Result { + let started = Instant::now(); + self.stats.segments += 1; + if let Some(n) = &self.nemotron { + n.push(segment.track, segment.start_ms, segment.samples); + } + let early = self.early[track_index(segment.track)].take(); + let result = self.label(segment, early.as_ref()); + self.stats.total_time += started.elapsed(); + result + } + + /// Who is speaking at the start of a segment that is still open, for its + /// partial words. Changes nothing the meeting's labels depend on: the + /// windows it embeds are the ones the segment will use when it closes, + /// and `push` reuses them, so the segment's labels come out the same and + /// sooner. + pub fn peek(&mut self, segment: Segment<'_>) -> Result, Error> { + let track = segment.track; + let window = (self.config.window_ms * SAMPLES_PER_MS) as usize; + let hop = (self.config.hop_ms * SAMPLES_PER_MS).max(1) as usize; + let len = segment.samples.len(); + if len < window { + return Ok(None); + } + let slot = track_index(track); + let mut early = match self.early[slot].take() { + Some(e) if e.key == segment.key => e, + _ => Early { + key: segment.key, + samples: Vec::new(), + windows: Vec::new(), + }, + }; + let wanted: Vec<(usize, usize)> = (0..) + .map(|k| (k * hop, k * hop + window)) + .take_while(|&(_, b)| b <= len) + .collect(); + let missing: Vec<(usize, usize)> = wanted + .iter() + .copied() + .filter(|&w| early.get(segment.key, segment.samples, w).is_none()) + .collect(); + if !missing.is_empty() { + let slices: Vec<&[f32]> = missing + .iter() + .map(|&(a, b)| &segment.samples[a..b]) + .collect(); + let started = Instant::now(); + let embeddings = self.embedder.embed(&slices)?; + self.stats.embed_time += started.elapsed(); + self.stats.embed_calls += 1; + self.stats.early_windows += missing.len() as u64; + early + .windows + .retain(|&(w, _)| w.1 <= len && !missing.contains(&w)); + early.windows.extend(missing.into_iter().zip(embeddings)); + early.samples.clear(); + early.samples.extend_from_slice(segment.samples); + } + let at = |sample: usize| segment.start_ms + sample as u64 / SAMPLES_PER_MS; + self.keep_levels(track, segment.start_ms, segment.samples, false); + let mut labels: Vec> = wanted + .iter() + .map(|&(a, b)| { + let e = early.get(segment.key, segment.samples, (a, b))?; + match self.decide(track, e, at(a), at(b)) { + Decision::Join(id, _) => self.shown(id, e).map(|id| (id, false)), + Decision::Echo(id) => Some((id, true)), + Decision::Unknown => self.nearest_confirmed(track, e).map(|id| (id, false)), + } + }) + .collect(); + self.forget_levels(track, segment.start_ms); + self.early[slot] = Some(early); + smooth(&mut labels); + Ok(labels + .into_iter() + .flatten() + .find(|(_, echo)| !echo) + .map(|(id, _)| id)) + } + + /// The words transcribed in a closed segment, by the middle of each + /// word (ms from the start of the meeting), once its words are final. + pub fn transcribed(&mut self, track: Track, key: u64, word_ms: &[u64]) { + let mut found = false; + for piece in self.pieces.iter_mut().rev().take(TRANSCRIBED_LOOKBACK) { + if piece.track != track || piece.key != key { + if found { + break; + } + continue; + } + found = true; + let (a, b) = (piece.start_ms, piece.end_ms); + piece.words = Some(word_ms.iter().filter(|&&m| m >= a && m < b).count() as u32); + } + } + + fn nearest_confirmed(&self, track: Track, embedding: &[f32]) -> Option { + self.clusters + .iter() + .filter(|c| c.track == track && c.confirmed && c.leak != Some(true)) + .max_by(|a, b| { + a.centroid + .similarity(embedding) + .total_cmp(&b.centroid.similarity(embedding)) + }) + .map(|c| c.id) + } + + fn label( + &mut self, + segment: Segment<'_>, + early: Option<&Early>, + ) -> Result { + let track = segment.track; + let len = segment.samples.len() as u64; + let end_ms = segment.start_ms + len / SAMPLES_PER_MS; + self.keep_levels(track, segment.start_ms, segment.samples, true); + if len / SAMPLES_PER_MS < self.config.min_embed_ms { + let speaker = self.previous_speaker(track, segment.start_ms); + self.pieces.push(Piece { + track, + key: segment.key, + start_ms: segment.start_ms, + end_ms, + embedding: None, + cluster: speaker, + speaker, + echo: false, + power: power(segment.samples), + leak: None, + words: None, + }); + return Ok(SegmentLabels { + track, + key: segment.key, + spans: speaker + .map(|speaker| Span { + start_ms: segment.start_ms, + end_ms, + speaker, + echo: false, + }) + .into_iter() + .collect(), + }); + } + + let windows = self.windows(segment.samples.len()); + let early: Vec>> = windows + .iter() + .map(|&w| early.and_then(|e| e.get(segment.key, segment.samples, w))) + .collect(); + let slices: Vec<&[f32]> = windows + .iter() + .zip(&early) + .filter(|(_, e)| e.is_none()) + .map(|(&(a, b), _)| &segment.samples[a..b]) + .collect(); + let mut fresh = if slices.is_empty() { + Vec::new() + } else { + let embed_started = Instant::now(); + let fresh = self.embedder.embed(&slices)?; + self.stats.embed_time += embed_started.elapsed(); + self.stats.embed_calls += 1; + fresh + } + .into_iter(); + let mut embeddings = Vec::with_capacity(windows.len()); + for e in &early { + match e { + Some(e) => { + self.stats.reused_windows += 1; + embeddings.push((*e).clone()); + } + None => embeddings.push( + fresh + .next() + .ok_or_else(|| Error::Model("fewer embeddings than windows".into()))?, + ), + } + } + self.stats.windows += windows.len() as u64; + + let at = |sample: usize| segment.start_ms + sample as u64 / SAMPLES_PER_MS; + let mut labels: Vec> = vec![None; windows.len()]; + let mut pending: Vec = Vec::new(); + for (i, embedding) in embeddings.iter().enumerate() { + let (a, b) = windows[i]; + match self.decide(track, embedding, at(a), at(b)) { + Decision::Join(id, confident) => { + self.settle( + track, + &windows, + &embeddings, + &mut pending, + &mut labels, + segment, + ); + labels[i] = Some((id, false)); + if confident { + self.refine_cluster(id, embedding, b - a, segment.key); + } + } + Decision::Echo(id) => { + self.settle( + track, + &windows, + &embeddings, + &mut pending, + &mut labels, + segment, + ); + labels[i] = Some((id, true)); + } + Decision::Unknown => pending.push(i), + } + } + self.settle( + track, + &windows, + &embeddings, + &mut pending, + &mut labels, + segment, + ); + smooth(&mut labels); + + let mut spans: Vec = Vec::new(); + let mut run_start = 0; + for i in 0..windows.len() { + let last = i + 1 == windows.len(); + if !last && labels[i + 1] == labels[i] { + continue; + } + let from = if run_start == 0 { + 0 + } else { + (windows[run_start - 1].1 + windows[run_start].0) / 2 + }; + let to = if last { + segment.samples.len() + } else { + (windows[i].1 + windows[i + 1].0) / 2 + }; + let mut mean = vec![0.0; self.embedder.dim()]; + for e in &embeddings[run_start..=i] { + for (m, x) in mean.iter_mut().zip(e) { + *m += x; + } + } + normalize(&mut mean); + let (cluster, echo) = match labels[i] { + Some((id, echo)) => (Some(id), echo), + None => (None, false), + }; + let speaker = match cluster { + Some(id) if !echo => self.shown(id, &mean), + other => other, + }; + let piece = Piece { + track, + key: segment.key, + start_ms: at(from), + end_ms: if last { end_ms } else { at(to) }, + embedding: Some(mean), + cluster, + speaker, + echo, + power: power(&segment.samples[from..to]), + leak: None, + words: None, + }; + if let (Some(id), false) = (cluster, echo) + && let Some(c) = self.clusters.iter_mut().find(|c| c.id == id) + { + c.speech_ms += piece.ms(); + c.power_ms += f64::from(piece.power) * piece.ms() as f64; + } + if let Some(speaker) = speaker { + match spans.last_mut() { + Some(span) if span.speaker == speaker && span.echo == echo => { + span.end_ms = piece.end_ms; + } + _ => spans.push(Span { + start_ms: piece.start_ms, + end_ms: piece.end_ms, + speaker, + echo, + }), + } + } + self.pieces.push(piece); + self.embeddings_kept += 1; + run_start = i + 1; + } + self.review_leaks(); + if track == Track::Mic { + self.follow_loudest_user(); + } + self.bound_memory(); + Ok(SegmentLabels { + track, + key: segment.key, + spans, + }) + } + + /// The person to show for a stretch matched to `cluster`: itself once + /// confirmed, otherwise the nearest confirmed person on its track. + fn shown(&self, cluster: SpeakerId, embedding: &[f32]) -> Option { + let c = self.clusters.iter().find(|c| c.id == cluster)?; + if c.confirmed { + return Some(cluster); + } + self.clusters + .iter() + .filter(|o| o.track == c.track && o.confirmed && o.leak != Some(true)) + .max_by(|a, b| { + a.centroid + .similarity(embedding) + .total_cmp(&b.centroid.similarity(embedding)) + }) + .map(|o| o.id) + } + + /// Windows come in a handful of lengths only: ONNX Runtime keeps about + /// 8 MB for every distinct input length it has seen and never frees it, + /// so whole short segments are centre-cropped to a 250 ms grid. + fn windows(&self, samples: usize) -> Vec<(usize, usize)> { + let whole = (self.config.whole_max_ms * SAMPLES_PER_MS) as usize; + if samples <= whole { + let grid = (WINDOW_GRID_MS * SAMPLES_PER_MS) as usize; + let kept = (samples / grid * grid).max(samples.min(grid)); + let start = (samples - kept) / 2; + return vec![(start, start + kept)]; + } + let window = (self.config.window_ms * SAMPLES_PER_MS) as usize; + let hop = (self.config.hop_ms * SAMPLES_PER_MS).max(1) as usize; + let mut out = Vec::new(); + let mut start = 0; + while start + window < samples { + out.push((start, start + window)); + start += hop; + } + let tail = samples.saturating_sub(window); + if out.last().is_none_or(|&(a, _)| a < tail) { + out.push((tail, samples)); + } + out + } + + fn decide(&self, track: Track, embedding: &[f32], start_ms: u64, end_ms: u64) -> Decision { + if track == Track::Mic + && self.leak(start_ms, end_ms) >= Some(self.config.leak) + && let Some(id) = self.call_speaker(start_ms, end_ms) + { + return Decision::Echo(id); + } + let own = self.best(track, embedding); + let other = self + .best_overlapping(other_track(track), embedding, start_ms, end_ms) + .filter(|(id, _)| track == Track::Mic || Some(*id) == self.user); + // A stretch of the call whose copy the microphone is hearing through + // the speakers is the call itself, never the user sent back: when + // the call is louder than the user, its copy can sound like them. + let call_heard_in_the_room = + || track == Track::System && self.leak(start_ms, end_ms) >= Some(self.config.leak); + if let Some((id, sim)) = other + && sim >= self.config.assign + && own.is_none_or(|(_, own)| sim > own) + && !call_heard_in_the_room() + { + return Decision::Echo(id); + } + if let Some((id, sim)) = own + && sim >= self.new_threshold(track) + && self + .clusters + .iter() + .any(|c| c.id == id && c.leak == Some(true)) + { + return Decision::Echo(self.call_speaker(start_ms, end_ms).unwrap_or(id)); + } + match own { + Some((id, sim)) if sim >= self.config.assign => Decision::Join(id, true), + Some((id, sim)) if sim >= self.new_threshold(track) => Decision::Join(id, false), + _ => Decision::Unknown, + } + } + + fn new_threshold(&self, track: Track) -> f32 { + match track { + Track::Mic => self.config.mic_new_speaker, + Track::System => self.config.new_speaker, + } + } + + fn new_min_ms(&self, track: Track) -> u64 { + match track { + Track::Mic => self.config.mic_new_min_ms, + Track::System => self.config.new_min_ms, + } + } + + fn best(&self, track: Track, embedding: &[f32]) -> Option<(SpeakerId, f32)> { + self.clusters + .iter() + .filter(|c| c.track == track) + .map(|c| (c.id, c.centroid.similarity(embedding))) + .max_by(|a, b| a.1.total_cmp(&b.1)) + } + + fn best_overlapping( + &self, + track: Track, + embedding: &[f32], + start_ms: u64, + end_ms: u64, + ) -> Option<(SpeakerId, f32)> { + let slack = self.config.echo_slack_ms; + let mut ids: Vec = Vec::new(); + for piece in self.pieces.iter().rev() { + if piece.track != track || piece.echo { + continue; + } + if piece.end_ms + slack < start_ms { + if piece.end_ms + 60_000 < start_ms { + break; + } + continue; + } + if piece.start_ms > end_ms + slack { + continue; + } + if let Some(id) = piece.speaker + && !ids.contains(&id) + { + ids.push(id); + } + } + self.clusters + .iter() + .filter(|c| ids.contains(&c.id)) + .map(|c| (c.id, c.centroid.similarity(embedding))) + .max_by(|a, b| a.1.total_cmp(&b.1)) + } + + /// Resolves a run of windows unlike every known speaker. On the + /// microphone the run is first cut where the voice changes, because one + /// segment can hold several people nobody has heard yet (people in a room + /// trading turns without a pause, from the first minute). Each long enough part is a + /// new speaker, or the one an earlier part just became; short parts join + /// the nearest speaker. + fn settle( + &mut self, + track: Track, + windows: &[(usize, usize)], + embeddings: &[Vec], + pending: &mut Vec, + labels: &mut [Option<(SpeakerId, bool)>], + segment: Segment<'_>, + ) { + if pending.is_empty() { + return; + } + // How much speech a set of windows covers (they overlap). + let run_ms = |run: &[usize]| { + let mut covered = 0usize; + let mut until = 0usize; + for &i in run { + let (a, b) = windows[i]; + let from = a.max(until); + if b > from { + covered += b - from; + until = b; + } + } + covered as u64 / SAMPLES_PER_MS + }; + let threshold = self.new_threshold(track); + // Each window joins the part of this run it sounds most like, so a + // stray window starts a small part of its own instead of cutting the + // voice around it into pieces too short to be anyone. + let mut runs: Vec<(Vec, Vec)> = Vec::new(); + for &i in pending.iter() { + let e = &embeddings[i]; + let nearest = runs + .iter() + .enumerate() + .map(|(r, (_, sum))| (r, dot(sum, e) / dot(sum, sum).sqrt().max(1e-9))) + .max_by(|a, b| a.1.total_cmp(&b.1)); + match nearest { + Some((r, sim)) if track == Track::System || sim >= threshold => { + let (members, sum) = &mut runs[r]; + members.push(i); + for (s, x) in sum.iter_mut().zip(e) { + *s += x; + } + } + _ => runs.push((vec![i], e.clone())), + } + } + let has_any = |this: &Self| { + this.clusters + .iter() + .any(|c| c.track == track && c.confirmed) + }; + if !has_any(self) && runs.iter().all(|(r, _)| run_ms(r) < self.new_min_ms(track)) { + let all = pending.clone(); + let mut sum = vec![0.0; self.embedder.dim()]; + for (_, s) in &runs { + for (a, x) in sum.iter_mut().zip(s) { + *a += x; + } + } + runs = vec![(all, sum)]; + } + let mut order: Vec = (0..runs.len()).collect(); + order.sort_by_key(|&r| run_ms(&runs[r].0) < self.new_min_ms(track)); + let mut created: Vec = Vec::new(); + // Speech each new voice got in this run, all its parts together. + let mut heard: Vec<(SpeakerId, u64)> = Vec::new(); + for r in order { + let (members, sum) = &runs[r]; + let mut mean = sum.clone(); + normalize(&mut mean); + let long = run_ms(members); + let known = has_any(self); + let again = self + .clusters + .iter() + .filter(|c| created.contains(&c.id)) + .map(|c| (c.id, c.centroid.similarity(&mean))) + .max_by(|a, b| a.1.total_cmp(&b.1)) + .filter(|(_, sim)| *sim >= threshold); + let id = match (again, self.best(track, &mean)) { + (Some((id, _)), _) => { + if let Some(c) = self.clusters.iter_mut().find(|c| c.id == id) { + c.centroid.add(&mean, long as f32 / 1000.0); + } + if let Some(h) = heard.iter_mut().find(|h| h.0 == id) { + h.1 += long; + } + id + } + _ if long >= self.new_min_ms(track) || !known => { + let id = SpeakerId(self.next_id); + self.next_id += 1; + let first_ms = segment.start_ms + windows[members[0]].0 as u64 / SAMPLES_PER_MS; + let confirmed = !known || long >= self.config.confirm_ms; + self.clusters.push(Cluster { + id, + track, + centroid: Centroid::from(&mean, long as f32 / 1000.0), + first_ms, + speech_ms: 0, + power_ms: 0.0, + confirmed, + created_key: segment.key, + leak: None, + }); + if confirmed { + self.on_confirmed(id); + } + created.push(id); + heard.push((id, long)); + id + } + (None, Some((id, _))) => { + if track == Track::Mic { + self.mic_absorbed_ms += long; + } + id + } + (None, None) => continue, + }; + for &i in members { + labels[i] = Some((id, false)); + } + } + // A voice heard in several parts of one run is confirmed by all of + // them together, as it would be had it spoken in one stretch. + for (id, ms) in heard { + if ms >= self.config.confirm_ms + && let Some(c) = self + .clusters + .iter_mut() + .find(|c| c.id == id && !c.confirmed) + { + c.confirmed = true; + self.on_confirmed(id); + } + } + pending.clear(); + } + + fn on_confirmed(&mut self, id: SpeakerId) { + let Some(c) = self.clusters.iter().find(|c| c.id == id) else { + return; + }; + if c.track == Track::Mic && self.user.is_none() && c.leak != Some(true) { + let rejected = self + .profile + .as_ref() + .is_some_and(|p| p.similarity(&c.centroid.unit) < self.config.profile_reject); + if !rejected { + self.user = Some(id); + } + } + } + + fn refine_cluster(&mut self, id: SpeakerId, embedding: &[f32], samples: usize, key: u64) { + let mut confirmed_now = false; + if let Some(c) = self.clusters.iter_mut().find(|c| c.id == id) { + c.centroid + .add(embedding, samples as f32 / (SAMPLES_PER_MS as f32 * 1000.0)); + if !c.confirmed && c.created_key != key { + c.confirmed = true; + confirmed_now = true; + } + } + if confirmed_now { + self.on_confirmed(id); + } + if self.user.is_none() + && let Some(profile) = &self.profile + && let Some(c) = self.clusters.iter().find(|c| c.id == id) + && c.track == Track::Mic + && c.confirmed + && c.leak != Some(true) + && profile.similarity(&c.centroid.unit) >= self.config.profile_match + { + self.user = Some(id); + } + } + + /// Decides, as the call's levels arrive, which microphone voices are the + /// call coming out of the speakers. Such a voice is nobody: its speech + /// is an echo of the call from then on, and it stops being the user. + fn review_leaks(&mut self) { + let Some(now) = self.pieces.iter().map(|p| p.end_ms).max() else { + return; + }; + let since = now.saturating_sub(LEAK_REVIEW_MS); + // The call's levels are known up to here: a stretch before it that + // the call was not speaking through was heard with the call silent. + let call_known_ms = self.levels[track_index(Track::System)] + .last() + .map_or(0, |r| (r.first + r.q.len() as u64) * LEVEL_MS); + let call_runs = &self.levels[track_index(Track::System)]; + // (voice, tested ms, leaked ms, ms heard with the call silent, the + // call's silences it was heard in, by the call speech ending each) + let mut totals: Vec<(SpeakerId, u64, u64, u64, Vec)> = Vec::new(); + for i in (0..self.pieces.len()).rev() { + let p = &self.pieces[i]; + if p.end_ms < since { + break; + } + let Some(id) = p.cluster.filter(|_| p.track == Track::Mic && !p.echo) else { + continue; + }; + if !self.clusters.iter().any(|c| c.id == id && c.leak.is_none()) { + continue; + } + let score = match p.leak { + Some(score) => Some(score), + None => self.leak(p.start_ms, p.end_ms), + }; + self.pieces[i].leak = score; + let p = &self.pieces[i]; + let ms = p.ms(); + let (tested, leaked, alone) = match score { + Some(score) => (ms, if score >= self.config.leak { ms } else { 0 }, 0), + None if p.end_ms + CALL_KNOWN_AFTER_MS <= call_known_ms + && self.call_silent(p.start_ms, p.end_ms) => + { + (0, 0, ms) + } + None => continue, + }; + let silence = + (alone > 0).then(|| call_runs.partition_point(|r| r.first * LEVEL_MS <= p.end_ms)); + let t = match totals.iter_mut().position(|t| t.0 == id) { + Some(at) => &mut totals[at], + None => { + totals.push((id, 0, 0, 0, Vec::new())); + totals.last_mut().unwrap() + } + }; + t.1 += tested; + t.2 += leaked; + t.3 += alone; + if let Some(silence) = silence.filter(|s| !t.4.contains(s)) { + t.4.push(silence); + } + } + let mut lost_user = false; + for (id, tested, leaked, alone, silences) in totals { + let share = leaked as f64 / tested.max(1) as f64; + // The speakers only play the call while the call is speaking, so + // speech heard with the call silent counts against the call. A + // voice heard that way in several of the call's silences is + // someone in the room; a single silence could be the computer + // audio dropping out while the speakers kept playing. + let heard_alone = alone >= LEAK_DECIDE_MS + && silences.len() >= ALONE_PERSON_SILENCES + && alone as f64 >= ALONE_PERSON_SHARE * (alone + tested) as f64; + let verdict = if tested >= LEAK_DECIDE_MS + && leaked as f64 >= LEAK_VOICE_SHARE * (tested + alone) as f64 + { + Some(true) + } else if heard_alone || (tested >= PERSON_DECIDE_MS && share <= PERSON_VOICE_SHARE) { + Some(false) + } else { + None + }; + if let Some(c) = self.clusters.iter_mut().find(|c| c.id == id) { + c.leak = verdict; + } + lost_user |= verdict == Some(true) && self.user == Some(id); + } + if lost_user { + self.user = None; + let next = self + .clusters + .iter() + .filter(|c| c.track == Track::Mic && c.confirmed && c.leak != Some(true)) + .max_by_key(|c| c.speech_ms) + .map(|c| c.id); + if let Some(id) = next { + self.on_confirmed(id); + } + } + } + + fn keep_levels(&mut self, track: Track, start_ms: u64, samples: &[f32], count: bool) { + let frame = (LEVEL_MS * SAMPLES_PER_MS) as usize; + let first = start_ms.div_ceil(LEVEL_MS); + let skip = ((first * LEVEL_MS - start_ms) * SAMPLES_PER_MS) as usize; + let q: Vec = samples + .get(skip..) + .unwrap_or(&[]) + .chunks_exact(frame) + .map(|c| { + let db = 10.0 * power(c).max(1e-12).log10(); + ((db + 100.0) * 2.0).round().clamp(0.0, 255.0) as u8 + }) + .collect(); + let t = track_index(track); + if count { + // Frames the other track already holds; frames it adds later are + // counted when they arrive. + let other = self.levels(other_track(track), first, first + q.len() as u64); + for (&mine, theirs) in q.iter().zip(other) { + if let Some(theirs) = theirs { + self.level_sum[t].0 += f64::from(mine); + self.level_sum[t].1 += 1; + let o = 1 - t; + self.level_sum[o].0 += f64::from((theirs + 100.0) * 2.0); + self.level_sum[o].1 += 1; + } + } + } + self.level_frames[t] += q.len(); + // In time order, so lookups can search; a late segment goes where it + // belongs (for one in order that is the end). + let at = self.levels[t].partition_point(|r| r.first <= first); + self.levels[t].insert(at, Levels { first, q }); + while self.level_frames[t] > LEVELS_MAX && self.levels[t].len() > 1 { + let old = self.levels[t].remove(0); + self.level_frames[t] -= old.q.len(); + } + } + + /// Drops the run `keep_levels` just added for a segment starting at + /// `start_ms` (a peek's, which must not count). + fn forget_levels(&mut self, track: Track, start_ms: u64) { + let t = track_index(track); + let first = start_ms.div_ceil(LEVEL_MS); + if let Some(at) = self.levels[t].iter().rposition(|r| r.first == first) { + let old = self.levels[t].remove(at); + self.level_frames[t] -= old.q.len(); + } + } + + /// The track's level (dB) in frames `from..to`, `None` where it was + /// not speaking. + fn levels(&self, track: Track, from: u64, to: u64) -> Vec> { + let runs = &self.levels[track_index(track)]; + let mut out = vec![None; to.saturating_sub(from) as usize]; + let start = runs.partition_point(|r| r.first + r.q.len() as u64 <= from); + for run in &runs[start..] { + if run.first >= to { + break; + } + for (k, &q) in run.q.iter().enumerate() { + let f = run.first + k as u64; + if f >= from && f < to { + out[(f - from) as usize] = Some(f32::from(q) / 2.0 - 100.0); + } + } + } + out + } + + /// How closely a microphone stretch's loudness follows the call's (best + /// correlation over the speakers' delay), when the call was speaking + /// through most of it. The call heard through the speakers keeps the + /// call's rhythm syllable by syllable; someone talking over the call + /// does not. + fn leak(&self, start_ms: u64, end_ms: u64) -> Option { + let (from, to) = (start_ms.div_ceil(LEVEL_MS), end_ms / LEVEL_MS); + if to < from + LEAK_MIN_FRAMES { + return None; + } + let mic: Vec = self + .levels(Track::Mic, from, to) + .into_iter() + .collect::>>()?; + let (early, late) = LEAK_LAG_FRAMES; + let lo = from.saturating_sub(late as u64); + let call = self.levels(Track::System, lo, to + (late - early) as u64); + let at = (from - lo) as usize; + let covered = call[at..at + mic.len()].iter().flatten().count(); + if (covered as f64) < mic.len() as f64 * LEAK_MIN_COVER { + return None; + } + // The copy is the quieter one, each track against its usual level + // while both are active: a stretch clearly louder than the + // microphone's usual level over the call holds someone talking. + let usual = |t: usize| { + let (sum, n) = self.level_sum[t]; + (n > 0).then(|| (sum / n as f64) as f32 / 2.0 - 100.0) + }; + let mean = |v: &[f32]| v.iter().sum::() / v.len().max(1) as f32; + let heard: Vec = call[at..at + mic.len()].iter().flatten().copied().collect(); + if let (Some(mic_usual), Some(call_usual)) = (usual(0), usual(1)) + && mean(&mic) - mic_usual > mean(&heard) - call_usual + LEAK_LOUDER_DB + { + return None; + } + let floor = call.iter().flatten().copied().fold(f32::MAX, f32::min); + let call: Vec = call.into_iter().map(|v| v.unwrap_or(floor)).collect(); + let at_lag = |lag: i64| { + let begin = at as i64 - lag; + if begin < 0 || begin as usize + mic.len() > call.len() { + return None; + } + correlation(&mic, &call[begin as usize..begin as usize + mic.len()]) + }; + let follows = (early..=late).filter_map(at_lag).max_by(f32::total_cmp)?; + // The call following the microphone instead (the user's voice sent + // back by the far end) beats the other way round: not a leak. + let leads = (early - late..early) + .filter_map(at_lag) + .max_by(f32::total_cmp); + (leads.is_none_or(|leads| follows >= leads)).then_some(follows) + } + + /// Whether the call says (almost) nothing through a stretch, counting the + /// speakers' delay either side: its levels there are known. + fn call_silent(&self, start_ms: u64, end_ms: u64) -> bool { + let (early, late) = LEAK_LAG_FRAMES; + let from = (start_ms / LEVEL_MS).saturating_sub(late as u64); + let to = end_ms.div_ceil(LEVEL_MS) + (-early) as u64; + let call = self.levels(Track::System, from, to); + let spoken = call.iter().flatten().count(); + (spoken as f64) <= call.len() as f64 * CALL_SILENT_COVER + } + + /// The call's voice speaking most during a stretch. + fn call_speaker(&self, start_ms: u64, end_ms: u64) -> Option { + let mut totals: Vec<(SpeakerId, u64)> = Vec::new(); + for piece in self.pieces.iter().rev() { + if piece.track != Track::System || piece.echo { + continue; + } + if piece.end_ms + 60_000 < start_ms { + break; + } + let overlap = piece + .end_ms + .min(end_ms) + .saturating_sub(piece.start_ms.max(start_ms)); + if let (Some(id), true) = (piece.speaker, overlap > 0) { + match totals.iter_mut().find(|t| t.0 == id) { + Some(t) => t.1 += overlap, + None => totals.push((id, overlap)), + } + } + } + totals.into_iter().max_by_key(|t| t.1).map(|t| t.0) + } + + fn previous_speaker(&self, track: Track, before_ms: u64) -> Option { + self.pieces + .iter() + .rev() + .filter(|p| p.track == track && !p.echo && p.start_ms <= before_ms) + .find_map(|p| p.speaker) + } + + fn bound_memory(&mut self) { + while self.embeddings_kept > self.config.max_embeddings + && self.oldest_embedding < self.pieces.len() + { + if self.pieces[self.oldest_embedding] + .embedding + .take() + .is_some() + { + self.embeddings_kept -= 1; + } + self.oldest_embedding += 1; + } + } + + /// Revisits the whole meeting: re-assigns every stretch to the speaker it + /// is closest to now that every voice is known, folds speakers that turn + /// out to be one person, and re-checks echoes across the two tracks. + pub fn finish(&mut self) -> FinalPass { + let started = Instant::now(); + let mut finals: Vec = Vec::new(); + let mut assignment: Vec> = vec![None; self.pieces.len()]; + let scores: Vec> = self + .pieces + .iter() + .map(|p| match p.track { + Track::Mic => self.leak(p.start_ms, p.end_ms), + Track::System => None, + }) + .collect(); + let mut leaked: Vec = scores + .iter() + .map(|s| *s >= Some(self.config.leak)) + .collect(); + for track in [Track::Mic, Track::System] { + self.refine_track(track, &leaked, &mut finals, &mut assignment); + } + let call_voices = self.leaky_voices(&finals, &scores, &assignment, &mut leaked); + self.mark_leaks(&leaked, &mut assignment); + let folded = self.fold_echo_voices(&finals, &mut assignment); + let user = self.final_user(&finals, &assignment); + self.recheck_echo( + &finals, + user, + &call_voices, + &folded, + &mut leaked, + &mut assignment, + ); + self.mark_leaks(&leaked, &mut assignment); + self.fill_unembedded(&mut assignment); + self.fold_tiny_mic_voices(&finals, &mut assignment); + self.fold_wordless_voices(&finals, &mut assignment); + + let live: Vec = self + .clusters + .iter() + .filter(|c| c.confirmed) + .map(|c| c.id) + .collect(); + let mut overlap = vec![vec![0u64; live.len()]; finals.len()]; + for (piece, f) in self.pieces.iter().zip(&assignment) { + let heard_as = piece.cluster.filter(|c| live.contains(c)).or(piece.speaker); + if let (Some(FinalLabel::Speaker(f)), Some(l)) = (f.map(FinalLabel::from), heard_as) + && !piece.echo + && let Some(l) = live.iter().position(|id| *id == l) + { + overlap[f][l] += piece.ms().max(1); + } + } + let mut ids: Vec> = vec![None; finals.len()]; + for (f, l) in greedy_match(&overlap) { + ids[f] = Some(live[l]); + } + let mut next = self.next_id; + let ids: Vec = ids + .into_iter() + .map(|id| { + id.unwrap_or_else(|| { + let id = SpeakerId(next); + next += 1; + id + }) + }) + .collect(); + let mut merged = Vec::new(); + for (l, id) in live.iter().enumerate() { + if ids.contains(id) { + continue; + } + if let Some(f) = (0..finals.len()) + .filter(|&f| overlap[f][l] > 0) + .max_by_key(|&f| overlap[f][l]) + { + merged.push((*id, ids[f])); + } + } + + let resolve = |a: Option| -> Option<(SpeakerId, bool)> { + a.map(|a| match FinalLabel::from(a) { + FinalLabel::Speaker(f) => (ids[f], false), + FinalLabel::Echo(f) => (ids[f], true), + }) + }; + let mut segments: Vec = Vec::new(); + let mut speech = vec![0u64; finals.len()]; + let mut first = vec![u64::MAX; finals.len()]; + for (piece, a) in self.pieces.iter().zip(&assignment) { + if let Some(FinalLabel::Speaker(f)) = a.map(FinalLabel::from) { + speech[f] += piece.ms(); + first[f] = first[f].min(piece.start_ms); + } + let open = segments + .last() + .is_some_and(|s| s.key == piece.key && s.track == piece.track); + if !open { + segments.push(SegmentLabels { + track: piece.track, + key: piece.key, + spans: Vec::new(), + }); + } + let Some(current) = segments.last_mut() else { + continue; + }; + if let Some((speaker, echo)) = resolve(*a) { + match current.spans.last_mut() { + Some(span) if span.speaker == speaker && span.echo == echo => { + span.end_ms = piece.end_ms; + } + _ => current.spans.push(Span { + start_ms: piece.start_ms, + end_ms: piece.end_ms, + speaker, + echo, + }), + } + } + } + + let user = self.final_user(&finals, &assignment); + let speakers: Vec = finals + .iter() + .enumerate() + .filter(|(f, _)| speech[*f] > 0) + .map(|(f, c)| SpeakerInfo { + id: ids[f], + track: c.track, + first_ms: first[f], + speech_ms: speech[f], + is_user: user == Some(f), + }) + .collect(); + let enrolment = self.enrolment(&finals, &speech, user, &assignment); + let mut pass = FinalPass { + segments, + merged, + speakers, + user: user.map(|f| ids[f]), + enrolment, + }; + if let Some(runner) = self.nemotron.take_if(|n| n.schedule() == crate::nemotron::Schedule::AfterStop) { + let mut later = pass.clone(); + // the voice profile is learnt once, from this pass + later.enrolment = None; + self.late = Some(LateNemotron { voices: runner.finish_later(), pass: later }); + } + if let Some(result) = self.nemotron.take().map(|n| n.finish()) { + let voices = match result { + Ok(v) => v, + Err(e) => { + self.nemotron_error = Some(e); + self.stats.total_time += started.elapsed(); + return pass; + } + }; + let h = crate::hybrid::apply(&mut pass, &voices); + self.nemotron_voices = Some(voices); + self.stats.nemotron_applied = true; + self.stats.nemotron_relabelled_ms = h.relabelled_ms; + self.stats.nemotron_new_voices = h.new_voices; + self.stats.nemotron_merged_ids = h.merged_ids; + } + self.stats.total_time += started.elapsed(); + pass + } + + fn refine_track( + &self, + track: Track, + leaked: &[bool], + finals: &mut Vec, + assignment: &mut [Option], + ) { + let members: Vec = (0..self.pieces.len()) + .filter(|&i| { + let p = &self.pieces[i]; + p.track == track && !p.echo && !leaked[i] && p.embedding.is_some() + }) + .collect(); + let mut centroids: Vec = self + .clusters + .iter() + .filter(|c| c.track == track) + .map(|c| c.centroid.clone()) + .collect(); + if members.is_empty() || centroids.is_empty() { + return; + } + let embedding = |i: usize| self.pieces[i].embedding.as_deref().unwrap_or(&[]); + let weight = |i: usize| self.pieces[i].ms() as f32 / 1000.0; + let mut owner = vec![0usize; members.len()]; + let assign_all = |centroids: &[Centroid], owner: &mut [usize]| { + for (slot, &i) in owner.iter_mut().zip(&members) { + let e = embedding(i); + *slot = (0..centroids.len()) + .max_by(|&a, &b| { + centroids[a] + .similarity(e) + .total_cmp(¢roids[b].similarity(e)) + }) + .unwrap_or(0); + } + }; + let rebuild = |owner: &[usize], count: usize| -> Vec { + let mut out: Vec = (0..count) + .map(|_| Centroid::new(self.embedder.dim())) + .collect(); + for (&o, &i) in owner.iter().zip(&members) { + out[o].add(embedding(i), weight(i)); + } + out + }; + let compact = |owner: &mut [usize], centroids: Vec| -> Vec { + let mut remap = vec![usize::MAX; centroids.len()]; + let mut kept = Vec::new(); + for (c, centroid) in centroids.into_iter().enumerate() { + if centroid.weight > 0.0 { + remap[c] = kept.len(); + kept.push(centroid); + } + } + for o in owner.iter_mut() { + *o = remap[*o]; + } + kept + }; + + for _ in 0..self.config.refine_rounds { + assign_all(¢roids, &mut owner); + let rebuilt = rebuild(&owner, centroids.len()); + centroids = compact(&mut owner, rebuilt); + } + + loop { + let speech: Vec = centroids.iter().map(|c| c.weight).collect(); + let total_ms = speech.iter().sum::() * 1000.0; + let mut best: Option<(usize, usize, bool, f32)> = None; + for a in 0..centroids.len() { + for b in a + 1..centroids.len() { + let sim = dot(¢roids[a].unit, ¢roids[b].unit); + let smaller_ms = speech[a].min(speech[b]) * 1000.0; + let forced = smaller_ms < self.config.min_speaker_ms as f32; + let threshold = if smaller_ms < self.config.small_ms as f32 { + self.config.small_merge + } else if track == Track::System + && smaller_ms < self.config.fold_ms as f32 + && smaller_ms < total_ms * self.config.fold_share + { + self.config.fold_merge + } else { + self.config.merge + }; + let margin = sim - threshold; + if (forced || margin >= 0.0) + && best.is_none_or(|(_, _, f, m)| (forced, margin) > (f, m)) + { + best = Some((a, b, forced, margin)); + } + } + } + let Some((a, b, _, _)) = best else { + break; + }; + let absorbed = centroids.remove(b); + centroids[a].absorb(&absorbed); + for o in owner.iter_mut() { + if *o == b { + *o = a; + } else if *o > b { + *o -= 1; + } + } + } + let min_side = self.config.split_min_ms as f32 / 1000.0; + let mut checked = vec![false; centroids.len()]; + let mut splits = 0; + while splits < MAX_SPLITS { + let Some(c) = (0..centroids.len()) + .find(|&c| !checked[c] && centroids[c].weight >= 2.0 * min_side) + else { + break; + }; + checked[c] = true; + let held: Vec = (0..members.len()).filter(|&k| owner[k] == c).collect(); + let items: Vec<(&[f32], f32)> = held + .iter() + .map(|&k| (embedding(members[k]), weight(members[k]))) + .collect(); + if let Some(side) = two_way(&items, self.config.split, min_side) { + let new = centroids.len(); + for (&k, &moved) in held.iter().zip(&side) { + if moved { + owner[k] = new; + } + } + centroids = rebuild(&owner, new + 1); + checked[c] = false; + checked.push(false); + splits += 1; + } + } + + assign_all(¢roids, &mut owner); + let rebuilt = rebuild(&owner, centroids.len()); + centroids = compact(&mut owner, rebuilt); + + let base = finals.len(); + finals.extend( + centroids + .into_iter() + .map(|centroid| FinalCluster { track, centroid }), + ); + for (&o, &i) in owner.iter().zip(&members) { + assignment[i] = Some(FinalLabel::Speaker(base + o).into()); + } + } + + fn leaky_voices( + &self, + finals: &[FinalCluster], + scores: &[Option], + assignment: &[Option], + leaked: &mut [bool], + ) -> Vec { + // (speech, compared speech, sum of score x speech) per final voice. + let mut voices = vec![(0u64, 0u64, 0f64); finals.len()]; + for (i, piece) in self.pieces.iter().enumerate() { + if let Some(FinalLabel::Speaker(f)) = assignment[i].map(FinalLabel::from) + && finals[f].track == Track::Mic + { + let ms = piece.ms(); + voices[f].0 += ms; + if let Some(score) = scores[i] { + voices[f].1 += ms; + voices[f].2 += f64::from(score) * ms as f64; + } + } + } + let call: Vec = voices + .iter() + .map(|&(ms, tested, sum)| { + tested > 0 + && tested as f64 >= ms as f64 * LEAK_VOICE_TESTED + && sum / tested as f64 >= f64::from(LEAK_VOICE_MEAN) + }) + .collect(); + for (i, a) in assignment.iter().enumerate() { + if let Some(FinalLabel::Speaker(f)) = a.map(FinalLabel::from) + && call[f] + { + leaked[i] = true; + } + } + call + } + + /// Microphone stretches that are the call coming out of the speakers are + /// echoes of the call's voice speaking then. + fn mark_leaks(&self, leaked: &[bool], assignment: &mut [Option]) { + let mut call: Vec<(u64, u64, usize)> = self + .pieces + .iter() + .zip(assignment.iter()) + .filter(|(p, _)| p.track == Track::System) + .filter_map(|(p, a)| match a.map(FinalLabel::from) { + Some(FinalLabel::Speaker(f)) => Some((p.start_ms, p.end_ms, f)), + _ => None, + }) + .collect(); + call.sort_unstable(); + let longest = call.iter().map(|(a, b, _)| b - a).max().unwrap_or(0); + for (i, piece) in self.pieces.iter().enumerate() { + if !leaked[i] { + continue; + } + let first = call.partition_point(|c| c.0 + longest < piece.start_ms); + let mut totals: Vec<(usize, u64)> = Vec::new(); + for &(a, b, f) in &call[first..] { + if a >= piece.end_ms { + break; + } + let overlap = b.min(piece.end_ms).saturating_sub(a.max(piece.start_ms)); + if overlap == 0 { + continue; + } + match totals.iter_mut().find(|t| t.0 == f) { + Some(t) => t.1 += overlap, + None => totals.push((f, overlap)), + } + } + if let Some((f, _)) = totals.into_iter().max_by_key(|t| t.1) { + assignment[i] = Some(FinalLabel::Echo(f).into()); + } + } + } + + /// A whole voice that sounds like the other track's speaker and is heard + /// almost only while they talk is that speaker arriving twice: the user + /// returned by the call, or the call leaking from the speakers into the + /// microphone. Checked per voice because a voice built only from echoes + /// matches itself better than the original, piece by piece. + /// Returns where each folded voice went, for stretches placed later. + fn fold_echo_voices( + &self, + finals: &[FinalCluster], + assignment: &mut [Option], + ) -> Vec> { + let slack = self.config.echo_slack_ms; + let mut speech = vec![0u64; finals.len()]; + let mut spans: Vec> = vec![Vec::new(); finals.len()]; + for (piece, a) in self.pieces.iter().zip(assignment.iter()) { + if let Some(FinalLabel::Speaker(f)) = a.map(FinalLabel::from) { + speech[f] += piece.ms(); + spans[f].push((piece.start_ms, piece.end_ms)); + } + } + for s in &mut spans { + s.sort_unstable(); + } + // All of the call's speech: the microphone hears whoever is talking. + let mut call: Vec<(u64, u64)> = (0..finals.len()) + .filter(|&g| finals[g].track == Track::System) + .flat_map(|g| spans[g].iter().copied()) + .collect(); + call.sort_unstable(); + let covered_by = |f: usize, other: &[(u64, u64)]| -> f64 { + let mut hit = 0u64; + for &(a, b) in &spans[f] { + let first = other.partition_point(|&(_, e)| e + slack < a); + for &(c, d) in &other[first..] { + if c > b + slack { + break; + } + hit += b + .min(d + slack) + .saturating_sub(a.max(c.saturating_sub(slack))); + } + } + hit.min(speech[f]) as f64 / speech[f].max(1) as f64 + }; + let covered = |f: usize, g: usize| match (finals[f].track, finals[g].track) { + (Track::Mic, Track::System) => covered_by(f, &call), + _ => covered_by(f, &spans[g]), + }; + let user = self.final_user(finals, assignment); + let mut echo_of: Vec> = vec![None; finals.len()]; + for f in 0..finals.len() { + if speech[f] == 0 || Some(f) == user { + continue; + } + let originals: Vec = match finals[f].track { + Track::System => user.into_iter().collect(), + Track::Mic => (0..finals.len()) + .filter(|&g| finals[g].track == Track::System && speech[g] > 0) + .collect(), + }; + let sim = |g: usize| dot(&finals[f].centroid.unit, &finals[g].centroid.unit); + let nearest = originals + .iter() + .map(|&g| (g, sim(g))) + .max_by(|a, b| a.1.total_cmp(&b.1)); + echo_of[f] = originals + .into_iter() + .map(|g| (g, sim(g))) + .filter(|&(g, sim)| sim >= self.config.merge && covered(f, g) >= ECHO_COVERAGE) + .max_by(|a, b| a.1.total_cmp(&b.1)) + .map(|(g, _)| FinalLabel::Echo(g).into()); + if echo_of[f].is_none() + && finals[f].track == Track::Mic + && speech[f] < OVER_CALL_MAX_MS + && let (Some(u), Some((g, call_sim))) = (user, nearest) + && covered(f, g) >= OVER_CALL_COVERAGE + { + echo_of[f] = Some(if call_sim >= sim(u) { + FinalLabel::Echo(g).into() + } else { + FinalLabel::Speaker(u).into() + }); + } + } + for a in assignment.iter_mut() { + if let Some(FinalLabel::Speaker(f)) = a.map(FinalLabel::from) + && let Some(to) = echo_of[f] + { + *a = Some(to); + } + } + echo_of + } + + fn final_speech(&self, assignment: &[Option], count: usize) -> Vec { + let mut speech = vec![0u64; count]; + for (piece, a) in self.pieces.iter().zip(assignment) { + if let Some(FinalLabel::Speaker(f)) = a.map(FinalLabel::from) { + speech[f] += piece.ms(); + } + } + speech + } + + /// Echo runs one way per track: the call can only return the user's + /// voice, and the microphone can only pick up the call. + fn recheck_echo( + &self, + finals: &[FinalCluster], + user: Option, + call_voices: &[bool], + folded: &[Option], + leaked: &mut [bool], + assignment: &mut [Option], + ) { + let slack = self.config.echo_slack_ms; + let mut spans: [Vec<(u64, u64, usize)>; 2] = [Vec::new(), Vec::new()]; + for (piece, a) in self.pieces.iter().zip(assignment.iter()) { + if let Some(FinalLabel::Speaker(f)) = a.map(FinalLabel::from) { + spans[track_index(piece.track)].push((piece.start_ms, piece.end_ms, f)); + } + } + for s in &mut spans { + s.sort_unstable(); + } + let max_len = |s: &[(u64, u64, usize)]| s.iter().map(|(a, b, _)| b - a).max().unwrap_or(0); + let longest = [max_len(&spans[0]), max_len(&spans[1])]; + for (i, piece) in self.pieces.iter().enumerate() { + let Some(e) = piece.embedding.as_deref() else { + continue; + }; + if leaked[i] && assignment[i].is_some() { + continue; + } + let own = match assignment[i].map(FinalLabel::from) { + Some(FinalLabel::Speaker(f)) => finals[f].centroid.similarity(e), + _ => f32::MIN, + }; + let other = track_index(other_track(piece.track)); + let from = piece.start_ms.saturating_sub(slack + longest[other]); + let first = spans[other].partition_point(|s| s.0 < from); + let mut best: Option<(usize, f32)> = None; + for &(a, b, f) in &spans[other][first..] { + if a > piece.end_ms + slack { + break; + } + if b + slack < piece.start_ms || (piece.track == Track::System && Some(f) != user) { + continue; + } + let sim = finals[f].centroid.similarity(e); + if best.is_none_or(|(_, s)| sim > s) { + best = Some((f, sim)); + } + } + if let Some((f, sim)) = best + && sim >= self.config.assign + && sim > own + { + assignment[i] = Some(FinalLabel::Echo(f).into()); + } else if piece.echo { + // Nearest to what is left of the speakers: the call too. + let nearest = self.nearest_final(piece.track, e, finals); + if nearest.is_some_and(|f| call_voices[f]) { + leaked[i] = true; + assignment[i] = None; + } else { + assignment[i] = + nearest.map(|f| folded[f].unwrap_or(FinalLabel::Speaker(f).into())); + } + } + } + } + + fn nearest_final(&self, track: Track, e: &[f32], finals: &[FinalCluster]) -> Option { + (0..finals.len()) + .filter(|&f| finals[f].track == track) + .max_by(|&a, &b| { + finals[a] + .centroid + .similarity(e) + .total_cmp(&finals[b].centroid.similarity(e)) + }) + } + + /// A microphone voice left with less than `min_speaker_ms` once echoes + /// are taken out is a scrap of someone else, not a person. + fn fold_tiny_mic_voices(&self, finals: &[FinalCluster], assignment: &mut [Option]) { + let speech = self.final_speech(assignment, finals.len()); + let real = + |f: usize| finals[f].track == Track::Mic && speech[f] >= self.config.min_speaker_ms; + let into: Vec> = (0..finals.len()) + .map(|f| { + if finals[f].track != Track::Mic || speech[f] == 0 || real(f) { + return None; + } + (0..finals.len()) + .filter(|&g| g != f && real(g)) + .max_by(|&a, &b| { + dot(&finals[f].centroid.unit, &finals[a].centroid.unit) + .total_cmp(&dot(&finals[f].centroid.unit, &finals[b].centroid.unit)) + }) + }) + .collect(); + for a in assignment.iter_mut() { + if let Some(FinalLabel::Speaker(f)) = a.map(FinalLabel::from) + && let Some(g) = into[f] + { + *a = Some(FinalLabel::Speaker(g).into()); + } + } + } + + fn fold_wordless_voices(&self, finals: &[FinalCluster], assignment: &mut [Option]) { + if self.pieces.iter().all(|p| p.words.is_none()) { + return; + } + // (speech, transcribed speech, words) per final voice. + let mut v = vec![(0u64, 0u64, 0u64); finals.len()]; + for (piece, a) in self.pieces.iter().zip(assignment.iter()) { + if let Some(FinalLabel::Speaker(f)) = a.map(FinalLabel::from) { + v[f].0 += piece.ms(); + if let Some(words) = piece.words { + v[f].1 += piece.ms(); + v[f].2 += u64::from(words); + } + } + } + let rate = |f: usize| v[f].2 as f64 * 1000.0 / v[f].1.max(1) as f64; + let user = self.final_user(finals, assignment); + let main = |f: usize| { + (0..finals.len()) + .filter(|&g| finals[g].track == finals[f].track && v[g].1 >= WORDLESS_MIN_MS) + .map(rate) + .fold(0.0, f64::max) + }; + let wordless: Vec = (0..finals.len()) + .map(|f| { + let main = main(f); + Some(f) != user + && v[f].1 >= WORDLESS_MIN_MS + && v[f].1 as f64 >= v[f].0 as f64 * WORDLESS_TRANSCRIBED + && rate(f) < WORDLESS_RATE + && rate(f) < main * WORDLESS_OF_MAIN + }) + .collect(); + let into: Vec> = (0..finals.len()) + .map(|f| { + if !wordless[f] { + return None; + } + (0..finals.len()) + .filter(|&g| { + g != f + && finals[g].track == finals[f].track + && v[g].1 >= WORDLESS_MIN_MS + && rate(g) >= main(f) * TALKER_OF_MAIN + }) + .max_by(|&a, &b| { + dot(&finals[f].centroid.unit, &finals[a].centroid.unit) + .total_cmp(&dot(&finals[f].centroid.unit, &finals[b].centroid.unit)) + }) + }) + .collect(); + for a in assignment.iter_mut() { + if let Some(FinalLabel::Speaker(f)) = a.map(FinalLabel::from) + && let Some(g) = into[f] + { + *a = Some(FinalLabel::Speaker(g).into()); + } + } + } + + fn fill_unembedded(&self, assignment: &mut [Option]) { + for track in [Track::Mic, Track::System] { + let labelled: Vec<(usize, u64)> = self + .pieces + .iter() + .enumerate() + .filter(|(i, p)| { + p.track == track + && matches!( + assignment[*i].map(FinalLabel::from), + Some(FinalLabel::Speaker(_)) + ) + }) + .map(|(i, p)| (i, (p.start_ms + p.end_ms) / 2)) + .collect(); + if labelled.is_empty() { + continue; + } + for i in 0..self.pieces.len() { + let p = &self.pieces[i]; + if p.track != track || assignment[i].is_some() { + continue; + } + let mid = (p.start_ms + p.end_ms) / 2; + let at = labelled.partition_point(|(_, m)| *m < mid); + let candidates = [at.checked_sub(1), (at < labelled.len()).then_some(at)]; + let nearest = candidates + .into_iter() + .flatten() + .min_by_key(|&k| { + let (j, m) = labelled[k]; + (self.pieces[j].key != p.key, m.abs_diff(mid)) + }) + .map(|k| labelled[k].0); + if let Some(j) = nearest { + assignment[i] = assignment[j]; + } + } + } + } + + fn final_user(&self, finals: &[FinalCluster], assignment: &[Option]) -> Option { + let speech = self.final_speech(assignment, finals.len()); + let mut power = vec![0f64; finals.len()]; + for (piece, a) in self.pieces.iter().zip(assignment) { + if let Some(FinalLabel::Speaker(f)) = a.map(FinalLabel::from) { + power[f] += f64::from(piece.power) * piece.ms() as f64; + } + } + let mic: Vec = (0..finals.len()) + .filter(|&f| finals[f].track == Track::Mic && speech[f] > 0) + .collect(); + if let Some(profile) = &self.profile + && let Some(&f) = mic.iter().max_by(|&&a, &&b| { + profile + .similarity(&finals[a].centroid.unit) + .total_cmp(&profile.similarity(&finals[b].centroid.unit)) + }) + && profile.similarity(&finals[f].centroid.unit) >= self.config.profile_match + { + return Some(f); + } + let mut voices: Vec<(usize, u64, f64)> = mic + .iter() + .map(|&f| (f, speech[f], power[f] / speech[f].max(1) as f64)) + .collect(); + if loudest(&voices, &self.config).is_some() { + voices.retain(|v| { + !self.heard_with_the_call(|i, _| { + assignment[i] + .map(FinalLabel::from) + .is_some_and(|a| matches!(a, FinalLabel::Speaker(f) if f == v.0)) + }) + }); + } + // Voices set aside as heard with the call never come back as the + // fallback: the most speech among the rest. + loudest(&voices, &self.config) + .or_else(|| voices.iter().max_by_key(|v| v.1).map(|v| v.0)) + .or_else(|| mic.into_iter().max_by_key(|&f| speech[f])) + } + + /// Whether a microphone voice is heard mostly while the call is talking: + /// the call leaking from the speakers, never the person at the computer. + fn heard_with_the_call(&self, owns: impl Fn(usize, &Piece) -> bool) -> bool { + let slack = self.config.echo_slack_ms; + let mut call: Vec<(u64, u64)> = self + .pieces + .iter() + .filter(|p| p.track == Track::System && !p.echo && p.speaker.is_some()) + .map(|p| (p.start_ms.saturating_sub(slack), p.end_ms + slack)) + .collect(); + call.sort_unstable(); + let (mut hit, mut total) = (0u64, 0u64); + for (i, p) in self.pieces.iter().enumerate() { + if p.track != Track::Mic || p.echo || !owns(i, p) { + continue; + } + total += p.ms(); + let first = call.partition_point(|&(_, e)| e <= p.start_ms); + let mut covered = 0u64; + let mut until = p.start_ms; + for &(a, b) in &call[first..] { + if a >= p.end_ms { + break; + } + let from = a.max(until); + let to = b.min(p.end_ms); + if to > from { + covered += to - from; + until = to; + } + } + hit += covered; + } + total > 0 && hit as f64 >= total as f64 * ECHO_COVERAGE + } + + /// Several people can share the microphone; the user is the one it hears + /// clearly loudest, sitting at the computer. Without a voice profile the + /// live choice follows that as soon as it is clear, so the end of the + /// meeting agrees with what was shown. + fn follow_loudest_user(&mut self) { + if self.profile.is_some() { + return; + } + let mut voices: Vec<(SpeakerId, u64, f64)> = self + .clusters + .iter() + .filter(|c| { + c.track == Track::Mic && c.confirmed && c.speech_ms > 0 && c.leak != Some(true) + }) + .map(|c| (c.id, c.speech_ms, c.power_ms / c.speech_ms as f64)) + .collect(); + if loudest(&voices, &self.config).is_none() { + return; + } + voices.retain(|v| !self.heard_with_the_call(|_, p| p.cluster == Some(v.0))); + let Some(id) = loudest(&voices, &self.config) else { + return; + }; + let level = |id: SpeakerId| voices.iter().find(|v| v.0 == id).map(|v| v.2); + let clearer = match self.user.and_then(level) { + Some(current) => { + 10.0 * (level(id).unwrap_or(0.0) / current.max(1e-12)) + .max(1e-12) + .log10() + >= self.config.user_louder_db + } + None => true, + }; + if clearer { + self.user = Some(id); + } + } + + fn enrolment( + &self, + finals: &[FinalCluster], + speech: &[u64], + user: Option, + assignment: &[Option], + ) -> Option { + let mic: Vec = (0..finals.len()) + .filter(|&f| finals[f].track == Track::Mic && speech[f] > 0) + .collect(); + let [only] = mic[..] else { + return None; + }; + if user != Some(only) || speech[only] < self.config.enrol_min_ms { + return None; + } + // One voice on the microphone is not proof of one person: voices the + // live pass could not tell apart end up together. Learn only from + // speech that holds together as one voice. + let (mut unlike, mut total) = (0u64, 0u64); + for (piece, a) in self.pieces.iter().zip(assignment) { + if let (Some(FinalLabel::Speaker(f)), Some(e)) = + (a.map(FinalLabel::from), piece.embedding.as_deref()) + && f == only + { + total += piece.ms(); + if finals[only].centroid.similarity(e) < self.config.mic_new_speaker { + unlike += piece.ms(); + } + } + } + if (unlike + self.mic_absorbed_ms) as f64 > total as f64 * self.config.enrol_max_unlike { + return None; + } + let learnt = VoiceProfile::new( + self.embedder.model_id(), + &finals[only].centroid.unit, + speech[only], + ); + match &self.profile { + Some(profile) if profile.similarity(&learnt.embedding) >= self.config.profile_match => { + Some(profile.merged(&learnt)) + } + Some(_) => None, + None => Some(learnt), + } + } +} + +struct FinalCluster { + track: Track, + centroid: Centroid, +} + +/// Final assignments pack "speaker f" and "echo of speaker f" into one +/// `usize` so the per-piece table stays a flat vector. +#[derive(Clone, Copy)] +enum FinalLabel { + Speaker(usize), + Echo(usize), +} + +impl From for FinalLabel { + fn from(v: usize) -> Self { + if v & 1 == 0 { + FinalLabel::Speaker(v >> 1) + } else { + FinalLabel::Echo(v >> 1) + } + } +} + +impl From for usize { + fn from(v: FinalLabel) -> Self { + match v { + FinalLabel::Speaker(f) => f << 1, + FinalLabel::Echo(f) => (f << 1) | 1, + } + } +} + +fn other_track(track: Track) -> Track { + match track { + Track::Mic => Track::System, + Track::System => Track::Mic, + } +} + +fn track_index(track: Track) -> usize { + match track { + Track::Mic => 0, + Track::System => 1, + } +} + +/// Two-means over one speaker's stretches. Seeds are the stretches of at +/// least a second least like the whole, each paired with the whole and with +/// its own opposite; three people heard as one rarely split well from a +/// single seed. Returns which stretches form the second group for the +/// clearest split whose groups are unlike enough and both hold `min_side` +/// seconds. +fn two_way(items: &[(&[f32], f32)], threshold: f32, min_side: f32) -> Option> { + let dim = items.first()?.0.len(); + let mut all = Centroid::new(dim); + for (e, w) in items { + all.add(e, *w); + } + let mut seeds: Vec<&[f32]> = items + .iter() + .filter(|(_, w)| *w >= 1.0) + .map(|(e, _)| *e) + .collect(); + seeds.sort_by(|a, b| all.similarity(a).total_cmp(&all.similarity(b))); + seeds.truncate(SPLIT_SEEDS); + let mut best: Option<(f32, Vec)> = None; + for seed in &seeds { + let opposite = items + .iter() + .filter(|(_, w)| *w >= 1.0) + .min_by(|a, b| dot(seed, a.0).total_cmp(&dot(seed, b.0))) + .map(|(e, _)| *e)?; + for other in [all.unit.as_slice(), opposite] { + let Some((sim, side)) = two_means(items, seed, other, min_side) else { + continue; + }; + if sim < threshold && best.as_ref().is_none_or(|(s, _)| sim < *s) { + best = Some((sim, side)); + } + } + } + best.map(|(_, side)| side) +} + +fn two_means( + items: &[(&[f32], f32)], + first: &[f32], + second: &[f32], + min_side: f32, +) -> Option<(f32, Vec)> { + let dim = first.len(); + let mut one = Centroid::from(first, 1.0); + let mut two = Centroid::from(second, 1.0); + let mut side = vec![false; items.len()]; + for _ in 0..8 { + for (s, (e, _)) in side.iter_mut().zip(items) { + *s = two.similarity(e) > one.similarity(e); + } + let mut a = Centroid::new(dim); + let mut b = Centroid::new(dim); + for (s, (e, w)) in side.iter().zip(items) { + if *s { + b.add(e, *w); + } else { + a.add(e, *w); + } + } + if a.weight == 0.0 || b.weight == 0.0 { + return None; + } + one = a; + two = b; + } + (one.weight >= min_side && two.weight >= min_side).then(|| (dot(&one.unit, &two.unit), side)) +} + +/// Pearson correlation; `None` when either side is flat. +fn correlation(a: &[f32], b: &[f32]) -> Option { + let n = a.len().min(b.len()) as f32; + if n < 2.0 { + return None; + } + let (ma, mb) = (a.iter().sum::() / n, b.iter().sum::() / n); + let (mut ab, mut aa, mut bb) = (0.0, 0.0, 0.0); + for (x, y) in a.iter().zip(b) { + let (x, y) = (x - ma, y - mb); + ab += x * y; + aa += x * x; + bb += y * y; + } + (aa > 1e-6 && bb > 1e-6).then(|| ab / (aa * bb).sqrt()) +} + +/// A single window whose neighbours agree with each other is a blip. +fn smooth(labels: &mut [Option<(SpeakerId, bool)>]) { + if labels.len() < 3 { + return; + } + for i in 1..labels.len() - 1 { + if labels[i - 1] == labels[i + 1] && labels[i] != labels[i - 1] && labels[i - 1].is_some() { + labels[i] = labels[i - 1]; + } + } +} + +fn power(samples: &[f32]) -> f32 { + if samples.is_empty() { + return 0.0; + } + samples.iter().map(|x| x * x).sum::() / samples.len() as f32 +} + +/// The voice clearly loudest on the microphone among those with enough +/// speech: `(id, speech ms, mean power)`. None when there is one such voice +/// or no clear winner. +fn loudest(voices: &[(T, u64, f64)], config: &Config) -> Option { + let most = voices.iter().map(|v| v.1).max()?; + let enough: Vec<&(T, u64, f64)> = voices + .iter() + .filter(|v| v.1 >= config.user_min_ms && v.1 as f64 >= most as f64 * config.user_min_share) + .collect(); + if enough.len() < 2 { + return None; + } + let db = |p: f64| 10.0 * p.max(1e-12).log10(); + let top = enough.iter().max_by_key(|v| v.1)?; + let loud = enough.iter().max_by(|a, b| a.2.total_cmp(&b.2))?; + (loud.0 != top.0 && db(loud.2) >= db(top.2) + config.user_louder_db).then_some(loud.0) +} diff --git a/apps/desktop-gpui/diarize/src/diarizer/tests.rs b/apps/desktop-gpui/diarize/src/diarizer/tests.rs new file mode 100644 index 00000000000..d73819ff554 --- /dev/null +++ b/apps/desktop-gpui/diarize/src/diarizer/tests.rs @@ -0,0 +1,1277 @@ +use super::*; +use crate::{SpeakerNames, names::Label}; + +const DIM: usize = 16; + +/// Returns `table[k]` for a window whose median sample is `k / 100`, so a +/// test writes speaker `k` by filling samples with that value. +struct Scripted { + table: Vec>, +} + +impl Embed for Scripted { + fn dim(&self) -> usize { + DIM + } + + fn model_id(&self) -> &str { + "scripted" + } + + fn embed(&mut self, windows: &[&[f32]]) -> Result>, Error> { + Ok(windows + .iter() + .map(|w| { + let mut sorted = w.to_vec(); + sorted.sort_by(f32::total_cmp); + let k = (sorted[sorted.len() / 2] * 100.0).round() as usize; + self.table[k].clone() + }) + .collect()) + } +} + +fn axis(i: usize) -> Vec { + let mut v = vec![0.0; DIM]; + v[i] = 1.0; + v +} + +/// A unit vector at `sim` cosine similarity to axis `base`, leaning on axis +/// `lean`. +fn near(base: usize, lean: usize, sim: f32) -> Vec { + let mut v = vec![0.0; DIM]; + v[base] = sim; + v[lean] = (1.0 - sim * sim).sqrt(); + v +} + +fn blend(parts: &[(usize, f32)]) -> Vec { + let mut v = vec![0.0; DIM]; + for &(i, x) in parts { + v[i] = x; + } + crate::embed::normalized(&v) +} + +fn speakers(n: usize) -> Vec> { + (0..n).map(axis).collect() +} + +fn diarizer(table: Vec>) -> MeetingDiarizer { + MeetingDiarizer::new(Config::default(), Box::new(Scripted { table }), None) +} + +/// New voices are shown as soon as they are long enough to be a speaker. +fn eager() -> Config { + Config { + confirm_ms: 1_000, + ..Config::default() + } +} + +fn audio(parts: &[(usize, u64)]) -> Vec { + let mut out = Vec::new(); + for &(k, ms) in parts { + out.extend(std::iter::repeat_n(k as f32 / 100.0, (ms * 16) as usize)); + } + out +} + +fn push( + d: &mut MeetingDiarizer, + track: Track, + start_ms: u64, + parts: &[(usize, u64)], +) -> SegmentLabels { + let samples = audio(parts); + d.push(Segment { + track, + key: start_ms, + start_ms, + samples: &samples, + }) + .unwrap() +} + +fn only(labels: &SegmentLabels) -> SpeakerId { + assert_eq!(labels.spans.len(), 1, "{labels:?}"); + labels.spans[0].speaker +} + +#[test] +fn one_voice_keeps_one_id() { + let mut d = diarizer(speakers(2)); + let ids: Vec = (0..20) + .map(|i| only(&push(&mut d, Track::System, i * 5_000, &[(1, 2_500)]))) + .collect(); + assert!(ids.iter().all(|id| *id == ids[0])); + let pass = d.finish(); + assert!(pass.merged.is_empty()); + assert_eq!(pass.speakers.len(), 1); + assert_eq!(pass.segments.len(), 20); +} + +#[test] +fn alternating_voices_get_stable_ids_in_order_of_first_speech() { + let mut d = diarizer(speakers(4)); + let mut seen = Vec::new(); + for i in 0..30u64 { + let k = 1 + (i % 3) as usize; + let id = only(&push(&mut d, Track::System, i * 4_000, &[(k, 2_000)])); + seen.push((k, id)); + } + assert_eq!( + seen[1].1, + SpeakerId(1), + "a new voice waits for a second stretch" + ); + assert_eq!(seen[2].1, SpeakerId(1)); + for (k, id) in &seen[3..] { + assert_eq!(id.0 as usize, *k, "{seen:?}"); + } + assert_eq!(d.speakers().len(), 3); + let pass = d.finish(); + assert!(pass.merged.is_empty()); + for (i, last) in pass.segments.iter().enumerate() { + assert_eq!(only(last).0 as usize, 1 + i % 3); + } +} + +#[test] +fn a_long_segment_is_split_where_the_voice_changes() { + let mut d = diarizer(speakers(3)); + push(&mut d, Track::System, 0, &[(1, 2_000)]); + push(&mut d, Track::System, 3_000, &[(2, 2_000)]); + let labels = push(&mut d, Track::System, 10_000, &[(1, 6_000), (2, 6_000)]); + assert_eq!(labels.spans.len(), 2, "{labels:?}"); + assert_eq!(labels.spans[0].speaker, SpeakerId(1)); + assert_eq!(labels.spans[1].speaker, SpeakerId(2)); + assert_eq!(labels.spans[0].start_ms, 10_000); + assert_eq!(labels.spans[1].end_ms, 22_000); + let change = labels.spans[1].start_ms; + assert!(change.abs_diff(16_000) <= 750, "change at {change}"); + assert_eq!(labels.spans[0].end_ms, change); +} + +#[test] +fn a_brief_window_inside_one_voice_is_not_a_speaker_change() { + let mut d = diarizer(speakers(3)); + push(&mut d, Track::System, 0, &[(1, 2_000)]); + push(&mut d, Track::System, 3_000, &[(2, 2_000)]); + let labels = push( + &mut d, + Track::System, + 10_000, + &[(1, 5_000), (2, 900), (1, 5_000)], + ); + assert_eq!(only(&labels), SpeakerId(1)); +} + +#[test] +fn short_segments_inherit_the_previous_voice_and_never_start_a_speaker() { + let mut d = diarizer(speakers(3)); + let first = push(&mut d, Track::System, 0, &[(2, 300)]); + assert!(first.spans.is_empty()); + let a = only(&push(&mut d, Track::System, 1_000, &[(1, 2_000)])); + let short = only(&push(&mut d, Track::System, 4_000, &[(2, 300)])); + assert_eq!(short, a); + let pass = d.finish(); + assert_eq!(pass.speakers.len(), 1); + assert_eq!(pass.segments.len(), 3); + assert!(pass.segments.iter().all(|s| s.main_speaker() == Some(a))); +} + +#[test] +fn a_new_voice_needs_enough_speech_before_it_becomes_someone() { + let mut d = diarizer(speakers(4)); + let a = only(&push(&mut d, Track::System, 0, &[(1, 2_000)])); + let short = only(&push(&mut d, Track::System, 3_000, &[(2, 1_000)])); + assert_eq!(short, a); + let first = only(&push(&mut d, Track::System, 5_000, &[(2, 2_000)])); + assert_eq!(first, a, "provisional until heard again"); + assert_eq!(d.speakers().len(), 1); + let again = only(&push(&mut d, Track::System, 8_000, &[(2, 2_000)])); + assert_ne!(again, a); + assert_eq!(d.speakers().len(), 2); + let monologue = only(&push(&mut d, Track::System, 11_000, &[(3, 4_500)])); + assert!( + monologue != a && monologue != again, + "a long first stretch is shown at once" + ); + let pass = d.finish(); + assert_eq!(pass.segments[2].main_speaker(), Some(again)); +} + +#[test] +fn a_brief_distinct_voice_is_held_back_live_and_kept_at_the_end() { + let mut d = diarizer(speakers(4)); + let a = only(&push(&mut d, Track::System, 0, &[(1, 3_000)])); + let b = only(&push(&mut d, Track::System, 4_000, &[(2, 4_000)])); + let noise = only(&push(&mut d, Track::System, 9_000, &[(3, 1_800)])); + assert!(noise == a || noise == b); + for i in 0..10u64 { + push( + &mut d, + Track::System, + 12_000 + i * 5_000, + &[(1 + (i % 2) as usize, 3_000)], + ); + } + assert_eq!(d.speakers().len(), 2); + let pass = d.finish(); + assert_eq!(pass.speakers.len(), 3, "unlike everyone, so a real person"); + assert!( + pass.speakers + .iter() + .all(|s| s.id != a && s.id != b || s.speech_ms > 10_000) + ); +} + +#[test] +fn final_pass_folds_a_speaker_that_was_split_live_and_reports_it() { + let mut table = speakers(4); + table.push(near(1, 3, 0.28)); + let mut config = eager(); + config.small_merge = 0.25; + let mut d = MeetingDiarizer::new(config, Box::new(Scripted { table }), None); + let a = only(&push(&mut d, Track::System, 0, &[(1, 2_000)])); + let b = only(&push(&mut d, Track::System, 3_000, &[(2, 2_000)])); + let split = only(&push(&mut d, Track::System, 6_000, &[(4, 2_000)])); + assert!(split != a && split != b); + for i in 0..10u64 { + push( + &mut d, + Track::System, + 10_000 + i * 3_000, + &[(1 + (i % 2) as usize, 2_000)], + ); + } + let pass = d.finish(); + assert_eq!(pass.merged, vec![(split, a)]); + assert_eq!(pass.speakers.len(), 2); + assert_eq!(pass.segments[2].main_speaker(), Some(a)); + assert_eq!(pass.segments[1].main_speaker(), Some(b)); +} + +#[test] +fn final_pass_corrects_an_early_mistake_with_full_context() { + let mut table = speakers(4); + table.push(blend(&[(1, 0.35), (2, 0.6), (3, 0.7193)])); + let mut d = MeetingDiarizer::new(eager(), Box::new(Scripted { table }), None); + let a = only(&push(&mut d, Track::System, 0, &[(1, 2_000)])); + let early = only(&push(&mut d, Track::System, 3_000, &[(4, 2_000)])); + assert_eq!(early, a, "joins the only speaker so far, unconfidently"); + let b = only(&push(&mut d, Track::System, 6_000, &[(2, 2_000)])); + assert_ne!(a, b); + for i in 0..10u64 { + push( + &mut d, + Track::System, + 10_000 + i * 3_000, + &[(1 + (i % 2) as usize, 2_000)], + ); + } + let pass = d.finish(); + assert_eq!(pass.segments[1].main_speaker(), Some(b)); + assert!(pass.merged.is_empty()); +} + +#[test] +fn the_user_echoed_on_the_system_track_is_not_a_new_person() { + let mut d = diarizer(speakers(4)); + let user = only(&push(&mut d, Track::Mic, 0, &[(1, 4_000)])); + assert_eq!(d.user(), Some(user)); + let echo = push(&mut d, Track::System, 100, &[(1, 3_800)]); + let remote = only(&push(&mut d, Track::System, 5_000, &[(2, 3_000)])); + assert_eq!(echo.spans.len(), 1); + assert_eq!(echo.spans[0].speaker, user); + assert!(echo.spans[0].echo); + let pass = d.finish(); + assert_eq!( + pass.speakers + .iter() + .filter(|s| s.track == Track::System) + .count(), + 1 + ); + let final_echo = pass.segments.iter().find(|s| s.key == 100).unwrap(); + assert!(final_echo.spans.iter().all(|s| s.echo && s.speaker == user)); + assert!(pass.speakers.iter().any(|s| s.id == remote)); +} + +#[test] +fn a_remote_voice_leaking_into_the_microphone_is_marked_as_echo() { + let mut d = diarizer(speakers(4)); + let user = only(&push(&mut d, Track::Mic, 0, &[(1, 4_000)])); + let remote = only(&push(&mut d, Track::System, 10_000, &[(2, 5_000)])); + let bleed = push(&mut d, Track::Mic, 10_200, &[(2, 4_000)]); + assert_eq!(bleed.spans.len(), 1); + assert_eq!(bleed.spans[0].speaker, remote); + assert!(bleed.spans[0].echo); + let pass = d.finish(); + assert_eq!(pass.user, Some(user)); + assert_eq!( + pass.speakers + .iter() + .filter(|s| s.track == Track::Mic) + .count(), + 1 + ); +} + +#[test] +fn the_same_voice_on_the_system_track_while_the_microphone_is_quiet_is_someone_else() { + let mut table = speakers(3); + table.push(near(1, 2, 0.6)); + let mut d = diarizer(table); + let user = only(&push(&mut d, Track::Mic, 0, &[(1, 4_000)])); + let other = push(&mut d, Track::System, 20_000, &[(3, 4_000)]); + assert!(!other.spans[0].echo); + assert_ne!(other.spans[0].speaker, user); +} + +#[test] +fn in_person_voices_on_the_microphone_are_separated_and_the_user_is_the_main_voice() { + let mut d = diarizer(speakers(4)); + let user = only(&push(&mut d, Track::Mic, 0, &[(1, 5_000)])); + let guest = only(&push(&mut d, Track::Mic, 6_000, &[(2, 4_000)])); + assert_ne!(user, guest); + for i in 0..8u64 { + push(&mut d, Track::Mic, 12_000 + i * 6_000, &[(1, 4_000)]); + } + let pass = d.finish(); + assert_eq!(pass.user, Some(user)); + assert!(pass.enrolment.is_none(), "two voices on the microphone"); +} + +#[test] +fn a_profile_picks_the_user_even_when_someone_else_speaks_first() { + let profile = VoiceProfile::new("scripted", &axis(2), 60_000); + let mut d = MeetingDiarizer::new( + Config::default(), + Box::new(Scripted { table: speakers(4) }), + Some(profile), + ); + let guest = only(&push(&mut d, Track::Mic, 0, &[(1, 5_000)])); + assert_eq!(d.user(), None, "the first voice is unlike the profile"); + let user = only(&push(&mut d, Track::Mic, 6_000, &[(2, 4_000)])); + assert_eq!(d.user(), Some(user)); + for i in 0..6u64 { + push(&mut d, Track::Mic, 12_000 + i * 6_000, &[(1, 5_000)]); + } + let pass = d.finish(); + assert_eq!(pass.user, Some(user)); + assert_ne!(pass.user, Some(guest)); +} + +#[test] +fn a_profile_from_another_model_is_ignored() { + let profile = VoiceProfile::new("other model", &axis(2), 60_000); + let mut d = MeetingDiarizer::new( + Config::default(), + Box::new(Scripted { table: speakers(4) }), + Some(profile), + ); + let first = only(&push(&mut d, Track::Mic, 0, &[(1, 5_000)])); + assert_eq!(d.user(), Some(first)); +} + +#[test] +fn enrolment_learns_a_single_microphone_voice_and_refreshes_a_matching_profile() { + let mut d = diarizer(speakers(3)); + for i in 0..12u64 { + push(&mut d, Track::Mic, i * 4_000, &[(1, 3_000)]); + } + let learnt = d.finish().enrolment.expect("36 s of one voice"); + assert_eq!(learnt.model, "scripted"); + assert!(learnt.similarity(&axis(1)) > 0.99); + + let mut d = MeetingDiarizer::new( + Config::default(), + Box::new(Scripted { table: speakers(3) }), + Some(learnt.clone()), + ); + for i in 0..12u64 { + push(&mut d, Track::Mic, i * 4_000, &[(1, 3_000)]); + } + let refreshed = d.finish().enrolment.unwrap(); + assert_eq!(refreshed.meetings, 2); + + let mut d = MeetingDiarizer::new( + Config::default(), + Box::new(Scripted { table: speakers(3) }), + Some(learnt), + ); + for i in 0..12u64 { + push(&mut d, Track::Mic, i * 4_000, &[(2, 3_000)]); + } + assert!( + d.finish().enrolment.is_none(), + "someone else used the computer" + ); +} + +#[test] +fn enrolment_needs_enough_speech() { + let mut d = diarizer(speakers(2)); + for i in 0..3u64 { + push(&mut d, Track::Mic, i * 4_000, &[(1, 3_000)]); + } + assert!(d.finish().enrolment.is_none()); +} + +#[test] +fn memory_stays_bounded_and_every_segment_is_still_labelled() { + let config = Config { + max_embeddings: 50, + ..Config::default() + }; + let mut d = MeetingDiarizer::new(config, Box::new(Scripted { table: speakers(3) }), None); + for i in 0..400u64 { + push( + &mut d, + Track::System, + i * 3_000, + &[(1 + (i % 2) as usize, 2_000)], + ); + } + assert!(d.stats().embeddings_kept <= 50); + let pass = d.finish(); + assert_eq!(pass.segments.len(), 400); + assert!(pass.segments.iter().all(|s| !s.spans.is_empty())); + assert_eq!(pass.speakers.len(), 2); +} + +#[test] +fn spans_tile_each_segment() { + let mut d = diarizer(speakers(4)); + push(&mut d, Track::System, 0, &[(1, 2_000)]); + push(&mut d, Track::System, 3_000, &[(2, 2_000)]); + push(&mut d, Track::System, 6_000, &[(3, 2_000)]); + let labels = push( + &mut d, + Track::System, + 10_000, + &[(1, 4_000), (3, 4_000), (2, 4_000)], + ); + assert_eq!(labels.spans.len(), 3); + assert_eq!(labels.spans.first().unwrap().start_ms, 10_000); + assert_eq!(labels.spans.last().unwrap().end_ms, 22_000); + for pair in labels.spans.windows(2) { + assert_eq!(pair[0].end_ms, pair[1].start_ms); + } + let pass = d.finish(); + let last = pass.segments.last().unwrap(); + assert_eq!(last.spans, labels.spans); +} + +#[test] +fn names_follow_the_meeting_from_live_labels_to_the_final_pass() { + let mut table = speakers(5); + table.push(near(2, 4, 0.28)); + let mut config = eager(); + config.small_merge = 0.25; + let mut d = MeetingDiarizer::new(config, Box::new(Scripted { table }), None); + let mut names = SpeakerNames::new(None); + let user = only(&push(&mut d, Track::Mic, 0, &[(1, 4_000)])); + let p1 = only(&push(&mut d, Track::System, 5_000, &[(2, 3_000)])); + let p2 = only(&push(&mut d, Track::System, 9_000, &[(5, 2_000)])); + let p3 = only(&push(&mut d, Track::System, 12_000, &[(3, 3_000)])); + names.observe(&d.speakers()); + assert_eq!(names.label(user), Some(Label::User)); + assert_eq!(names.label(p1), Some(Label::Person(1))); + assert_eq!(names.label(p2), Some(Label::Person(2))); + assert_eq!(names.label(p3), Some(Label::Person(3))); + names.rename(p2, "Dana"); + for i in 0..6u64 { + push( + &mut d, + Track::System, + 20_000 + i * 4_000, + &[(2 + (i % 2) as usize, 3_000)], + ); + } + let pass = d.finish(); + assert_eq!(pass.merged, vec![(p2, p1)]); + let changes = names.apply_final(&pass); + assert_eq!(names.label(p1), Some(Label::Named("Dana".into()))); + assert_eq!(names.label(p3), Some(Label::Person(2))); + assert_eq!(names.label(p2), None); + assert_eq!(names.label(user), Some(Label::User)); + assert_eq!(changes.len(), 3, "{changes:?}"); +} + +#[test] +fn final_pass_splits_two_voices_that_were_heard_as_one() { + let mut table = speakers(4); + table.push(blend(&[(1, 0.45), (3, 0.893)])); + let mut d = diarizer(table); + let a = only(&push(&mut d, Track::System, 0, &[(1, 3_000)])); + let mut mixed = Vec::new(); + for i in 0..12u64 { + let k = if i % 2 == 0 { 1 } else { 4 }; + mixed.push(( + k, + only(&push( + &mut d, + Track::System, + 4_000 + i * 4_000, + &[(k, 3_000)], + )), + )); + } + assert!( + mixed.iter().all(|(_, id)| *id == a), + "the second voice joins live" + ); + let pass = d.finish(); + assert_eq!(pass.speakers.len(), 2); + for (i, (k, _)) in mixed.iter().enumerate() { + let speaker = pass.segments[i + 1].main_speaker().unwrap(); + assert_eq!(speaker == a, *k == 1, "segment {i}"); + } +} + +#[test] +fn final_pass_keeps_one_steady_voice_whole() { + let mut table = speakers(4); + table.push(blend(&[(1, 0.9), (3, 0.436)])); + table.push(blend(&[(1, 0.9), (2, 0.436)])); + let mut d = diarizer(table); + for i in 0..20u64 { + push( + &mut d, + Track::System, + i * 4_000, + &[(4 + (i % 2) as usize, 3_000)], + ); + } + let pass = d.finish(); + assert_eq!(pass.speakers.len(), 1); +} + +#[test] +fn a_voice_made_only_of_the_users_echo_folds_into_the_user_at_the_end() { + let mut d = diarizer(speakers(4)); + let remote = only(&push(&mut d, Track::System, 0, &[(2, 4_000)])); + let mut ghost = None; + for i in 0..6u64 { + let t = 10_000 + i * 8_000; + let echo = push(&mut d, Track::System, t + 150, &[(1, 3_000)]); + ghost.get_or_insert(echo.spans[0].speaker); + push(&mut d, Track::Mic, t, &[(1, 3_200)]); + push(&mut d, Track::System, t + 4_000, &[(2, 3_000)]); + } + assert!(ghost.is_some()); + let pass = d.finish(); + let user = pass.user.unwrap(); + let system: Vec = pass + .speakers + .iter() + .filter(|s| s.track == Track::System) + .map(|s| s.id) + .collect(); + assert_eq!(system, vec![remote]); + for seg in pass + .segments + .iter() + .filter(|s| s.track == Track::System && s.key % 8_000 == 2_150) + { + assert!( + seg.spans.iter().all(|s| s.echo && s.speaker == user), + "{seg:?}" + ); + } +} + +#[test] +fn the_call_leaking_into_the_microphone_never_becomes_a_person_in_the_room() { + let mut d = diarizer(speakers(4)); + let user = only(&push(&mut d, Track::Mic, 0, &[(1, 5_000)])); + for i in 0..6u64 { + let t = 10_000 + i * 10_000; + push(&mut d, Track::Mic, t + 30, &[(2, 4_000)]); + push(&mut d, Track::System, t, &[(2, 4_000)]); + push(&mut d, Track::Mic, t + 5_000, &[(1, 4_000)]); + } + let pass = d.finish(); + assert_eq!(pass.user, Some(user)); + let mic: Vec<&SpeakerInfo> = pass + .speakers + .iter() + .filter(|s| s.track == Track::Mic) + .collect(); + assert_eq!(mic.len(), 1, "{:?}", pass.speakers); + assert!(pass.enrolment.is_some() || mic[0].speech_ms < 30_000); +} + +#[test] +fn the_model_only_ever_sees_a_few_input_lengths() { + let d = diarizer(speakers(2)); + let mut lengths = std::collections::BTreeSet::new(); + for ms in (500..30_000u64).step_by(7) { + for (a, b) in d.windows((ms * 16) as usize) { + assert!(b <= (ms * 16) as usize); + lengths.insert(b - a); + } + } + assert!(lengths.len() <= 12, "{lengths:?}"); +} + +/// Partial words show who is speaking now, and listening early changes +/// nothing the meeting's labels depend on: every segment comes out the same +/// with or without it, the work done early is reused at the close, and the +/// final pass agrees. +#[test] +fn peeking_at_an_open_segment_names_the_voice_now_and_changes_no_label() { + let script: Vec<(usize, u64)> = (0..24u64) + .map(|i| (1 + (i % 3) as usize, 2_000 + (i % 4) * 2_500)) + .collect(); + let run = |peek: bool| { + let mut d = diarizer(speakers(4)); + let mut labels = Vec::new(); + let mut peeked = Vec::new(); + let mut at = 0; + for &(k, ms) in &script { + let samples = audio(&[(k, ms)]); + if peek { + for len in [1_500u64, 3_500, 5_500, 7_500] { + let len = (len * 16) as usize; + if len < samples.len() { + peeked.push(( + k, + d.peek(Segment { + track: Track::System, + key: at, + start_ms: at, + samples: &samples[..len], + }) + .unwrap(), + )); + } + } + } + labels.push( + d.push(Segment { + track: Track::System, + key: at, + start_ms: at, + samples: &samples, + }) + .unwrap(), + ); + at += ms + 1_000; + } + let stats = d.stats(); + (labels, d.finish(), peeked, stats) + }; + let (plain, plain_final, _, plain_stats) = run(false); + let (early, early_final, peeked, early_stats) = run(true); + assert_eq!(plain, early); + assert_eq!(plain_final, early_final); + assert_eq!(plain_stats.reused_windows, 0); + assert!(early_stats.reused_windows > 0, "{early_stats:?}"); + let settled: Vec<_> = peeked.iter().skip(peeked.len() / 2).collect(); + assert!( + settled + .iter() + .all(|(k, id)| *id == Some(SpeakerId(*k as u32))), + "{peeked:?}" + ); +} + +/// People in one room around one microphone often trade turns with no pause, +/// so one segment holds several voices nobody has heard yet. Each becomes +/// its own person instead of one voice averaged from all of them. +#[test] +fn people_in_one_room_trading_turns_without_a_pause_are_told_apart() { + let mut d = diarizer(speakers(4)); + let labels = push(&mut d, Track::Mic, 0, &[(1, 8_000), (2, 9_000), (1, 5_000)]); + let ids: Vec = labels.spans.iter().map(|s| s.speaker).collect(); + assert_eq!(ids.len(), 3, "{labels:?}"); + assert_eq!(ids[0], ids[2], "the first voice again"); + assert_ne!(ids[0], ids[1]); + assert_eq!(d.speakers().len(), 2); + let pass = d.finish(); + let mic = pass + .speakers + .iter() + .filter(|s| s.track == Track::Mic) + .count(); + assert_eq!(mic, 2); +} + +/// Other people heard too briefly to become anyone are still other people: +/// the voice is not learnt from a meeting where they spoke. +#[test] +fn enrolment_skips_a_meeting_with_other_voices_on_the_microphone() { + let meeting = |guests: bool| { + let mut d = diarizer(speakers(3)); + let mut at = 0; + for i in 0..10u64 { + push(&mut d, Track::Mic, at, &[(1, 4_000)]); + at += 5_000; + if guests && i % 3 == 0 { + push(&mut d, Track::Mic, at, &[(2, 2_000)]); + at += 3_000; + } + } + (d.stats().mic_absorbed_ms, d.finish().enrolment) + }; + let (absorbed, learnt) = meeting(false); + assert_eq!(absorbed, 0); + assert!(learnt.is_some(), "40 s of one voice"); + let (absorbed, learnt) = meeting(true); + assert!(absorbed > 0); + assert!(learnt.is_none(), "a guest spoke {absorbed} ms"); +} + +/// Several people around one laptop: the person at the computer is the one +/// its microphone hears clearly loudest, even when someone further away +/// spoke first and talked most. Live and final agree. +#[test] +fn on_a_shared_microphone_the_user_is_the_clearly_loudest_voice() { + let mut d = diarizer(speakers(5)); + let mut at = 0; + for _ in 0..4 { + push(&mut d, Track::Mic, at, &[(1, 6_000)]); + at += 7_000; + push(&mut d, Track::Mic, at, &[(3, 4_000)]); + at += 5_000; + } + let near = d + .speakers() + .iter() + .find(|s| s.id != d.speakers()[0].id) + .map(|s| s.id); + assert_eq!(d.user(), near, "the louder voice, live"); + let pass = d.finish(); + assert_eq!(pass.user, near, "and at the end"); +} + +/// Two voices about as loud: nothing changes, the first voice stays the +/// user live and the voice with the most speech is the user at the end. +#[test] +fn similar_levels_keep_the_usual_choice() { + let mut d = diarizer(speakers(12)); + let first = only(&push(&mut d, Track::Mic, 0, &[(10, 6_000)])); + let mut at = 7_000; + for _ in 0..4 { + push(&mut d, Track::Mic, at, &[(11, 4_000)]); + at += 5_000; + push(&mut d, Track::Mic, at, &[(10, 6_000)]); + at += 7_000; + } + assert_eq!(d.user(), Some(first)); + assert_eq!(d.finish().user, Some(first)); +} + +/// Speech of voice `k` whose loudness rises and falls frame by frame (20 ms) +/// in the rhythm numbered `rhythm`, `lag` frames late. The scripted model +/// still hears voice `k`: most of every frame stays at its value. +fn spoken(k: usize, start_ms: u64, ms: u64, rhythm: u64, lag: u64, gain: f32) -> Vec { + let frame = 320; + let base = k as f32 / 100.0; + let first = start_ms / 20; + (0..(ms * 16) as usize) + .map(|i| { + let f = first + (i / frame) as u64; + let mut x = f.wrapping_sub(lag).wrapping_mul(2_654_435_761) ^ rhythm.wrapping_mul(97); + x ^= x >> 13; + let g = 1.0 + (x % 50) as f32 / 10.0; + gain * if i % frame < 192 { base } else { base * g } + }) + .collect() +} + +fn push_samples( + d: &mut MeetingDiarizer, + track: Track, + start_ms: u64, + samples: &[f32], +) -> SegmentLabels { + d.push(Segment { + track, + key: start_ms, + start_ms, + samples, + }) + .unwrap() +} + +#[test] +fn the_call_heard_through_the_speakers_is_never_the_user() { + // The call speaks first and the laptop speakers play it into the + // microphone, where it sounds like nobody on the call (room, speaker). + let mut d = diarizer(speakers(4)); + let mut leak_live_ids = Vec::new(); + for i in 0..3u64 { + let t = i * 4_000; + // The microphone's copy closes first, before the call's segment. + let leak = push_samples(&mut d, Track::Mic, t, &spoken(3, t, 3_900, 7, 0, 1.0)); + leak_live_ids.extend(leak.spans.iter().filter(|s| !s.echo).map(|s| s.speaker)); + push_samples(&mut d, Track::System, t, &spoken(2, t, 3_900, 7, 0, 1.0)); + } + let user = only(&push_samples( + &mut d, + Track::Mic, + 15_000, + &spoken(1, 15_000, 6_000, 11, 0, 1.0), + )); + assert_eq!(d.user(), Some(user), "the user takes over live"); + assert!( + d.speakers() + .iter() + .all(|s| s.track != Track::Mic || s.id == user), + "the speakers' copy is no one: {:?}", + d.speakers() + ); + for i in 0..4u64 { + let t = 25_000 + i * 8_000; + let system = spoken(2, t, 3_500, 13 + i, 0, 1.0); + push_samples(&mut d, Track::System, t, &system); + let leak = push_samples(&mut d, Track::Mic, t, &spoken(3, t, 3_500, 13 + i, 1, 1.0)); + assert!(leak.spans.iter().all(|s| s.echo), "{leak:?}"); + push_samples( + &mut d, + Track::Mic, + t + 4_000, + &spoken(1, t + 4_000, 3_500, 40 + i, 0, 1.0), + ); + } + let pass = d.finish(); + assert_eq!(pass.user, Some(user)); + let mic: Vec<&SpeakerInfo> = pass + .speakers + .iter() + .filter(|s| s.track == Track::Mic) + .collect(); + assert_eq!(mic.len(), 1, "{:?}", pass.speakers); + for seg in pass + .segments + .iter() + .filter(|s| s.track == Track::Mic && s.key < 12_000) + { + assert!(seg.spans.iter().all(|s| s.echo), "{seg:?}"); + } +} + +#[test] +fn the_users_voice_sent_back_by_the_call_is_not_a_leak() { + // The far end returns the user's voice 160 ms later and quieter: the + // microphone holds the original. + let mut d = diarizer(speakers(4)); + let user = only(&push_samples( + &mut d, + Track::Mic, + 0, + &spoken(1, 0, 5_000, 3, 0, 1.0), + )); + for i in 0..5u64 { + let t = 6_000 + i * 6_000; + push_samples( + &mut d, + Track::System, + t, + &spoken(1, t, 5_000, 20 + i, 8, 0.4), + ); + let mine = push_samples(&mut d, Track::Mic, t, &spoken(1, t, 5_000, 20 + i, 0, 1.0)); + assert!( + mine.spans.iter().all(|s| !s.echo && s.speaker == user), + "{mine:?}" + ); + } + let pass = d.finish(); + assert_eq!(pass.user, Some(user)); + let mic_speech: u64 = pass + .segments + .iter() + .filter(|s| s.track == Track::Mic) + .flat_map(|s| &s.spans) + .filter(|s| !s.echo) + .map(|s| s.end_ms - s.start_ms) + .sum(); + assert!(mic_speech >= 29_000, "{mic_speech}"); +} + +#[test] +fn a_voice_with_almost_no_words_folds_into_the_nearest_person_and_names_survive() { + // Laughter on the call sounds unlike its speech; it says nothing. + let mut table = speakers(4); + table.push(near(2, 3, 0.2)); + let run = |words: bool| { + let mut d = MeetingDiarizer::new( + eager(), + Box::new(Scripted { + table: table.clone(), + }), + None, + ); + let mut names = SpeakerNames::new(None); + let mut laugh = None; + let mut remote = None; + for i in 0..8u64 { + let t = i * 10_000; + let talk = push(&mut d, Track::System, t, &[(2, 6_000)]); + remote = remote.or(Some(only(&talk))); + if words { + let ms: Vec = (0..18).map(|w| t + 200 + w * 300).collect(); + d.transcribed(Track::System, t, &ms); + } + let l = push(&mut d, Track::System, t + 7_000, &[(4, 2_500)]); + laugh = laugh.or(Some(only(&l))); + if words { + d.transcribed(Track::System, t + 7_000, &[]); + } + } + names.observe(&d.speakers()); + let remote = remote.unwrap(); + names.rename(remote, "Dana"); + let pass = d.finish(); + names.apply_final(&pass); + (pass, names, remote, laugh.unwrap()) + }; + let (pass, _, _, laugh) = run(false); + assert_eq!( + pass.speakers.len(), + 2, + "without words nothing changes: {:?}", + pass.speakers + ); + assert!(pass.speakers.iter().any(|s| s.id == laugh)); + let (pass, names, remote, laugh) = run(true); + assert_eq!(pass.speakers.len(), 1, "{:?}", pass.speakers); + assert_eq!(pass.speakers[0].id, remote); + assert!(pass.merged.contains(&(laugh, remote)), "{:?}", pass.merged); + assert_eq!(names.label(remote), Some(Label::Named("Dana".into()))); + assert_eq!(names.label(laugh), None); +} + +#[test] +fn a_different_person_on_the_microphone_still_stands_apart_at_the_end() { + for (sim, voices) in [(0.5, 2), (0.7, 1)] { + let mut table = speakers(4); + table.push(near(1, 3, sim)); + let mut d = MeetingDiarizer::new(Config::default(), Box::new(Scripted { table }), None); + for i in 0..6u64 { + push(&mut d, Track::Mic, i * 12_000, &[(1, 5_000)]); + push(&mut d, Track::Mic, i * 12_000 + 6_000, &[(4, 4_500)]); + } + let pass = d.finish(); + let mic = pass + .speakers + .iter() + .filter(|s| s.track == Track::Mic) + .count(); + assert_eq!(mic, voices, "similarity {sim}: {:?}", pass.speakers); + } +} + +#[test] +fn the_speakers_copy_louder_than_a_quiet_user_is_still_the_call() { + // Voice 3 on the microphone is about 10 dB louder than the user (voice 1). + let mut d = diarizer(speakers(4)); + for i in 0..3u64 { + let t = i * 4_000; + push_samples(&mut d, Track::Mic, t, &spoken(3, t, 3_900, 7 + i, 0, 1.0)); + push_samples( + &mut d, + Track::System, + t, + &spoken(2, t, 3_900, 7 + i, 0, 1.0), + ); + } + let user = only(&push_samples( + &mut d, + Track::Mic, + 15_000, + &spoken(1, 15_000, 6_000, 50, 0, 1.0), + )); + let pass = d.finish(); + assert_eq!(pass.user, Some(user)); + for seg in pass + .segments + .iter() + .filter(|s| s.track == Track::Mic && s.key < 12_000) + { + assert!(seg.spans.iter().all(|s| s.echo), "{seg:?}"); + } +} + +#[test] +fn a_voice_heard_while_the_call_is_silent_is_never_judged_the_call() { + // The user talks alone, then the laptop speakers start playing the call + // into the microphone. Its copy sounds like the user (same room, same + // microphone) and closes before the call's own segment, so it joins the + // user's voice untested. The user's speech alone, with the call silent, + // must keep that voice a person: later the user's words are not echo. + let mut table = speakers(4); + table.push(near(1, 3, 0.7)); + let mut d = diarizer(table); + let user = only(&push_samples( + &mut d, + Track::Mic, + 0, + &spoken(1, 0, 6_000, 3, 0, 1.0), + )); + push_samples( + &mut d, + Track::System, + 7_000, + &spoken(2, 7_000, 3_000, 5, 0, 1.0), + ); + push_samples( + &mut d, + Track::Mic, + 11_000, + &spoken(1, 11_000, 4_000, 6, 0, 1.0), + ); + for i in 0..4u64 { + let t = 16_000 + i * 4_000; + // The microphone's copy of the call (voice 4, like the user's) + // closes first. + push_samples(&mut d, Track::Mic, t, &spoken(4, t, 3_900, 20 + i, 0, 1.0)); + push_samples( + &mut d, + Track::System, + t, + &spoken(2, t, 3_900, 20 + i, 0, 1.0), + ); + } + for i in 0..3u64 { + let t = 34_000 + i * 6_000; + let mine = push_samples(&mut d, Track::Mic, t, &spoken(1, t, 5_000, 40 + i, 0, 1.0)); + assert!( + mine.spans.iter().all(|s| !s.echo && s.speaker == user), + "the user alone, the call silent: {mine:?}" + ); + push_samples( + &mut d, + Track::System, + t + 5_200, + &spoken(2, t + 5_200, 600, 60 + i, 0, 1.0), + ); + } + assert_eq!(d.user(), Some(user)); + let pass = d.finish(); + assert_eq!(pass.user, Some(user)); +} + +#[test] +fn a_brief_remark_in_the_room_never_turns_the_call_into_a_person() { + // The user says nothing. The speakers' copy of the call (voice 3) is a + // voice of its own; someone in the room who sounds like it (voice 5) + // says a word or two twice while the call is silent. The copy stays the + // call: later copies that close before the call's segment are echo. + let mut table = speakers(5); + table.push(near(3, 4, 0.75)); + let mut d = diarizer(table); + for i in 0..6u64 { + let t = i * 8_000; + push_samples(&mut d, Track::Mic, t, &spoken(3, t, 3_900, 7 + i, 0, 1.0)); + push_samples( + &mut d, + Track::System, + t, + &spoken(2, t, 3_900, 7 + i, 0, 1.0), + ); + if i == 1 || i == 3 { + push_samples( + &mut d, + Track::Mic, + t + 4_600, + &spoken(5, t + 4_600, 2_000, 30 + i, 0, 1.0), + ); + } + } + for i in 6..10u64 { + let t = i * 8_000; + let copy = push_samples(&mut d, Track::Mic, t, &spoken(3, t, 3_900, 7 + i, 0, 1.0)); + assert!(copy.spans.iter().all(|s| s.echo), "{copy:?}"); + push_samples( + &mut d, + Track::System, + t, + &spoken(2, t, 3_900, 7 + i, 0, 1.0), + ); + } +} + +#[test] +fn the_computer_audio_dropping_out_never_turns_the_call_into_a_person() { + // The call's copy (voice 3) closes before the call's segment. Then the + // computer audio drops for 12 s while the speakers keep playing: the + // copy is heard with the call "silent", in one silence. It must still + // be judged the call once the computer audio is back. + let mut d = diarizer(speakers(4)); + push_samples(&mut d, Track::Mic, 0, &spoken(3, 0, 3_900, 7, 0, 1.0)); + push_samples(&mut d, Track::System, 0, &spoken(2, 0, 3_900, 7, 0, 1.0)); + for i in 1..4u64 { + let t = i * 4_000; + push_samples(&mut d, Track::Mic, t, &spoken(3, t, 3_900, 7 + i, 0, 1.0)); + } + for i in 4..24u64 { + let t = i * 4_000; + push_samples(&mut d, Track::Mic, t, &spoken(3, t, 3_900, 7 + i, 0, 1.0)); + push_samples( + &mut d, + Track::System, + t, + &spoken(2, t, 3_900, 7 + i, 0, 1.0), + ); + } + let t = 24 * 4_000; + let copy = push_samples(&mut d, Track::Mic, t, &spoken(3, t, 3_900, 40, 0, 1.0)); + assert!(copy.spans.iter().all(|s| s.echo), "{copy:?}"); + assert!( + d.speakers().iter().all(|s| s.track != Track::Mic), + "the call's copy is no one: {:?}", + d.speakers() + ); +} + +#[test] +fn the_call_louder_than_the_user_is_never_echo_of_the_user() { + // The user talks first. Then a remote whose voice sounds like the + // user's (voice 4) talks, louder than the user, and the laptop speakers + // play them into the microphone a little later; that copy closes first + // and joins the user's voice. The call's own words are not the user sent + // back by the far end: the microphone follows the call, not the reverse. + let mut table = speakers(4); + table.push(near(1, 2, 0.75)); + let mut d = diarizer(table); + let user = only(&push_samples( + &mut d, + Track::Mic, + 0, + &spoken(1, 0, 6_000, 3, 0, 1.0), + )); + for i in 0..3u64 { + let t = 8_000 + i * 6_000; + push_samples(&mut d, Track::Mic, t, &spoken(4, t, 5_000, 20 + i, 2, 1.0)); + let call = push_samples( + &mut d, + Track::System, + t, + &spoken(4, t, 5_000, 20 + i, 0, 1.0), + ); + assert!( + call.spans.iter().all(|s| !s.echo && s.speaker != user), + "{call:?}" + ); + } +} + +#[test] +fn quieter_chatter_in_the_room_heard_through_the_call_never_takes_the_user() { + // A cafe: other people's chatter, about 10 dB under the user, fills the + // microphone the whole time the call talks; the user speaks less but + // clearly louder, with the call quiet. (The scripted model hears the + // sample value: the user at 0.09 is voice 9, the chatter at 0.03 voice 3.) + let mut d = diarizer(speakers(11)); + let mut user = None; + for i in 0..6u64 { + let t = i * 22_000; + push_samples( + &mut d, + Track::System, + t, + &spoken(2, t, 12_000, 5 + i, 0, 1.0), + ); + push_samples( + &mut d, + Track::Mic, + t, + &spoken(10, t, 12_000, 30 + i, 0, 0.3), + ); + let mine = push_samples( + &mut d, + Track::Mic, + t + 13_000, + &spoken(9, t + 13_000, 7_000, 60 + i, 0, 1.0), + ); + user = user.or(Some(only(&mine))); + } + let pass = d.finish(); + assert_eq!(pass.user, user, "{:?}", pass.speakers); +} + +/// Hears each 30 s step as one voice, alternating: voice 0, then 1, then 0. +struct Alternate { + step: usize, +} + +impl crate::nemotron::NemoModel for Alternate { + fn step(&mut self, feats: &[f32], cached: &[f32], _mask: &[i64]) -> Result { + use crate::nemotron::cache::{HIDDEN, SPEAKERS, SUB}; + let n_mel = feats.len() / crate::nemotron::mel::N_MELS; + let n_emb = n_mel.div_ceil(SUB); + let n_cached = cached.len() / HIDDEN; + let v = self.step % 2; + self.step += 1; + let mut logits = vec![-8.0f32; (n_cached + n_emb) * SUB * SPEAKERS]; + for f in 0..n_mel { + logits[(n_cached * SUB + f) * SPEAKERS + v] = 8.0; + } + let mut chunk_embeds = vec![0.0f32; n_emb * HIDDEN]; + for e in 0..n_emb { + chunk_embeds[e * HIDDEN + v] = 1.0; + } + Ok(crate::nemotron::StepOut { logits, chunk_embeds, silence: vec![0.0; HIDDEN] }) + } +} + +/// Two minutes of the call that the speaker model hears as one voice. +fn one_voice_call(d: &mut MeetingDiarizer) { + for i in 0..12 { + push(d, Track::System, i * 10_500, &[(1, 10_000)]); + } +} + +#[test] +fn after_stop_nemotron_corrects_the_final_pass_exactly_as_live_nemotron_does() { + let mut plain = diarizer(speakers(2)); + one_voice_call(&mut plain); + let plain = plain.finish(); + + let mut live = diarizer(speakers(2)); + live.set_nemotron(Box::new(Alternate { step: 0 })); + one_voice_call(&mut live); + let live_pass = live.finish(); + assert!(live.stats().nemotron_applied); + assert!(live.take_late().is_none(), "live Nemotron is already in the final pass"); + assert!(live_pass.speakers.len() > plain.speakers.len(), "Nemotron split the call: {:?}", live_pass.speakers); + + let mut later = diarizer(speakers(2)); + later.set_nemotron_with(Box::new(Alternate { step: 0 }), crate::nemotron::Schedule::AfterStop, || {}); + one_voice_call(&mut later); + let first = later.finish(); + assert_eq!(first, plain, "the final pass goes out with the speaker model's labels"); + assert!(!later.stats().nemotron_applied); + let (corrected, stats) = later.take_late().expect("a correction on its way").wait().expect("Nemotron's voices"); + assert_eq!(corrected.segments, live_pass.segments); + assert_eq!(corrected.speakers, live_pass.speakers); + assert_eq!(corrected.merged, live_pass.merged); + assert_eq!(corrected.user, live_pass.user); + assert_eq!(corrected.enrolment, None); + assert_eq!(stats.new_voices, live.stats().nemotron_new_voices); + assert!(later.take_late().is_none()); +} + +#[test] +fn the_farm_standing_in_cancels_the_after_stop_pass() { + let mut d = diarizer(speakers(2)); + d.set_nemotron_with(Box::new(Alternate { step: 0 }), crate::nemotron::Schedule::AfterStop, || {}); + one_voice_call(&mut d); + d.cancel_after_stop_nemotron(); + d.finish(); + assert!(d.take_late().is_none()); + // a live Nemotron is never cancelled: its voices are already heard + let mut live = diarizer(speakers(2)); + live.set_nemotron(Box::new(Alternate { step: 0 })); + one_voice_call(&mut live); + live.cancel_after_stop_nemotron(); + live.finish(); + assert!(live.stats().nemotron_applied); +} diff --git a/apps/desktop-gpui/diarize/src/embed.rs b/apps/desktop-gpui/diarize/src/embed.rs new file mode 100644 index 00000000000..d0059ff11c4 --- /dev/null +++ b/apps/desktop-gpui/diarize/src/embed.rs @@ -0,0 +1,186 @@ +#[cfg(feature = "onnx")] +use std::path::Path; + +#[cfg(feature = "onnx")] +use ort::session::{Session, builder::GraphOptimizationLevel}; +#[cfg(feature = "onnx")] +use ort::value::TensorRef; + +use crate::Error; +use crate::fbank::Window; +#[cfg(feature = "onnx")] +use crate::fbank::{Fbank, NUM_BINS}; + +/// Turns a stretch of 16 kHz mono speech into a speaker embedding. Returned +/// vectors are L2-normalised so a dot product is the cosine similarity. +pub trait Embed: Send { + fn dim(&self) -> usize; + /// Identifies the model, so voice profiles made with another model are + /// never compared with this one. + fn model_id(&self) -> &str; + fn embed(&mut self, windows: &[&[f32]]) -> Result>, Error>; +} + +#[derive(Clone, Debug, PartialEq)] +pub struct ModelInfo { + pub id: String, + pub input: String, + pub output: String, + pub dim: usize, + pub sample_scale: f32, + pub mean_normalize: bool, + pub window: Window, +} + +#[cfg(feature = "onnx")] +pub struct OnnxEmbedder { + session: Session, + info: ModelInfo, + fbank: Fbank, + feats: Vec, +} + +#[cfg(feature = "onnx")] +impl OnnxEmbedder { + /// Loads a filterbank-input speaker model. The ONNX Runtime library must + /// already be initialised by the caller. + /// + /// `optimize` must stay off for the CAM++ family: ONNX Runtime 1.24 folds + /// its graph for the export's example length, after which every shorter + /// input embeds to noise (cosine ~0 against the unoptimised graph). + pub fn load(path: &Path, threads: usize, optimize: bool) -> Result { + let level = if optimize { + GraphOptimizationLevel::Level3 + } else { + GraphOptimizationLevel::Disable + }; + let session = Session::builder() + .map_err(Error::runtime)? + .with_optimization_level(level) + .map_err(Error::runtime)? + .with_intra_threads(threads.max(1)) + .map_err(Error::runtime)? + .with_inter_threads(1) + .map_err(Error::runtime)? + .with_intra_op_spinning(false) + .map_err(Error::runtime)? + .with_inter_op_spinning(false) + .map_err(Error::runtime)? + .commit_from_file(path) + .map_err(Error::runtime)?; + let info = model_info(&session, path)?; + Ok(Self { + fbank: Fbank::new(info.window), + session, + info, + feats: Vec::new(), + }) + } + + pub fn info(&self) -> &ModelInfo { + &self.info + } + + fn run(&mut self, window: &[f32]) -> Result, Error> { + let frames = Fbank::frames(window.len()); + if frames == 0 { + return Err(Error::TooShort); + } + self.feats.clear(); + self.fbank.compute( + window, + self.info.sample_scale, + self.info.mean_normalize, + &mut self.feats, + ); + let input = TensorRef::from_array_view(([1, frames, NUM_BINS], &*self.feats)) + .map_err(Error::runtime)?; + let outputs = self + .session + .run(ort::inputs![self.info.input.as_str() => input]) + .map_err(Error::runtime)?; + let (_, data) = outputs[self.info.output.as_str()] + .try_extract_tensor::() + .map_err(Error::runtime)?; + if data.len() != self.info.dim { + return Err(Error::Model(format!( + "expected {} values, got {}", + self.info.dim, + data.len() + ))); + } + Ok(normalized(data)) + } +} + +#[cfg(feature = "onnx")] +impl Embed for OnnxEmbedder { + fn dim(&self) -> usize { + self.info.dim + } + + fn model_id(&self) -> &str { + &self.info.id + } + + /// One window per run: on a single CPU thread a batch of windows costs + /// about 2.5 times more per window than running them one by one. + fn embed(&mut self, windows: &[&[f32]]) -> Result>, Error> { + windows.iter().map(|w| self.run(w)).collect() + } +} + +#[cfg(feature = "onnx")] +fn model_info(session: &Session, path: &Path) -> Result { + let input = session + .inputs() + .first() + .map(|i| i.name().to_string()) + .ok_or_else(|| Error::Model("model has no inputs".into()))?; + let output = session + .outputs() + .first() + .map(|o| o.name().to_string()) + .ok_or_else(|| Error::Model("model has no outputs".into()))?; + let meta = session.metadata().map_err(Error::runtime)?; + let custom = |key: &str| meta.custom(key); + let dim = custom("output_dim") + .and_then(|d| d.parse::().ok()) + .ok_or_else(|| Error::Model("model metadata lacks output_dim".into()))?; + if custom("sample_rate").is_some_and(|r| r != "16000") { + return Err(Error::Model("model is not 16 kHz".into())); + } + let normalized_samples = custom("normalize_samples").is_none_or(|v| v != "0"); + // Each family's training recipe: one family trains on Hamming windows + // over samples in 16-bit range, the other on Povey windows over [-1, 1]. + let window = if normalized_samples { + Window::Povey + } else { + Window::Hamming + }; + let id = custom("url") + .or_else(|| custom("comment")) + .unwrap_or_else(|| { + path.file_stem() + .map(|s| s.to_string_lossy().into_owned()) + .unwrap_or_default() + }); + Ok(ModelInfo { + id: format!("{id}#{dim}"), + input, + output, + dim, + sample_scale: if normalized_samples { 1.0 } else { 32_768.0 }, + mean_normalize: true, + window, + }) +} + +pub fn normalized(v: &[f32]) -> Vec { + let norm = v.iter().map(|x| x * x).sum::().sqrt(); + if norm > 0.0 { + v.iter().map(|x| x / norm).collect() + } else { + v.to_vec() + } +} diff --git a/apps/desktop-gpui/diarize/src/fbank.rs b/apps/desktop-gpui/diarize/src/fbank.rs new file mode 100644 index 00000000000..e611f57e11b --- /dev/null +++ b/apps/desktop-gpui/diarize/src/fbank.rs @@ -0,0 +1,179 @@ +use std::sync::Arc; + +use realfft::{RealFftPlanner, RealToComplex, num_complex::Complex32}; + +pub const SAMPLE_RATE: usize = 16_000; +pub const NUM_BINS: usize = 80; +const FRAME_LEN: usize = 400; +const FRAME_SHIFT: usize = 160; +const FFT_LEN: usize = 512; +const PREEMPHASIS: f32 = 0.97; +const LOW_HZ: f32 = 20.0; + +#[cfg(test)] +mod tests; + +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub enum Window { + #[default] + Povey, + Hamming, +} + +/// Log mel filterbank features computed the way the speaker models were +/// trained: 25 ms windows every 10 ms, DC removal, pre-emphasis, a 512-point +/// power spectrum and 80 triangular mel bins from 20 Hz to Nyquist, with +/// frames snipped to the edges and no dither. +pub struct Fbank { + window: Vec, + banks: Vec<(usize, Vec)>, + fft: Arc>, + frame: Vec, + spectrum: Vec, + scratch: Vec, + power: Vec, +} + +impl Default for Fbank { + fn default() -> Self { + Self::new(Window::Povey) + } +} + +impl Fbank { + pub fn new(window: Window) -> Self { + let window = (0..FRAME_LEN) + .map(|i| { + let a = 2.0 * std::f64::consts::PI * i as f64 / (FRAME_LEN - 1) as f64; + match window { + Window::Povey => (0.5 - 0.5 * a.cos()).powf(0.85) as f32, + Window::Hamming => (0.54 - 0.46 * a.cos()) as f32, + } + }) + .collect(); + let fft = RealFftPlanner::::new().plan_fft_forward(FFT_LEN); + let spectrum = fft.make_output_vec(); + let scratch = fft.make_scratch_vec(); + Self { + window, + banks: mel_banks(), + fft, + frame: vec![0.0; FFT_LEN], + spectrum, + scratch, + power: vec![0.0; FFT_LEN / 2 + 1], + } + } + + pub fn frames(samples: usize) -> usize { + if samples < FRAME_LEN { + 0 + } else { + 1 + (samples - FRAME_LEN) / FRAME_SHIFT + } + } + + /// Appends `frames(samples.len()) * NUM_BINS` values to `out`, scaling the + /// samples by `scale` first, and subtracts the per-bin mean over the + /// frames when `mean_normalize` is set. + pub fn compute( + &mut self, + samples: &[f32], + scale: f32, + mean_normalize: bool, + out: &mut Vec, + ) { + let frames = Self::frames(samples.len()); + let base = out.len(); + out.reserve(frames * NUM_BINS); + for f in 0..frames { + let chunk = &samples[f * FRAME_SHIFT..f * FRAME_SHIFT + FRAME_LEN]; + let mean = chunk.iter().sum::() * scale / FRAME_LEN as f32; + for (dst, &s) in self.frame.iter_mut().zip(chunk) { + *dst = s * scale - mean; + } + for i in (1..FRAME_LEN).rev() { + self.frame[i] -= PREEMPHASIS * self.frame[i - 1]; + } + self.frame[0] -= PREEMPHASIS * self.frame[0]; + for (x, w) in self.frame.iter_mut().zip(&self.window) { + *x *= w; + } + self.frame[FRAME_LEN..].fill(0.0); + if self + .fft + .process_with_scratch(&mut self.frame, &mut self.spectrum, &mut self.scratch) + .is_err() + { + out.extend(std::iter::repeat_n(f32::EPSILON.ln(), NUM_BINS)); + continue; + } + for (p, c) in self.power.iter_mut().zip(&self.spectrum) { + *p = c.norm_sqr(); + } + for (first, weights) in &self.banks { + let energy: f32 = weights + .iter() + .zip(&self.power[*first..]) + .map(|(w, p)| w * p) + .sum(); + out.push(energy.max(f32::EPSILON).ln()); + } + } + if mean_normalize && frames > 0 { + let mut mean = [0f32; NUM_BINS]; + for row in out[base..].as_chunks::().0 { + for (m, v) in mean.iter_mut().zip(row) { + *m += v; + } + } + for m in &mut mean { + *m /= frames as f32; + } + for row in out[base..].as_chunks_mut::().0 { + for (v, m) in row.iter_mut().zip(&mean) { + *v -= m; + } + } + } + } +} + +fn mel(hz: f32) -> f32 { + 1127.0 * (1.0 + hz / 700.0).ln() +} + +fn mel_banks() -> Vec<(usize, Vec)> { + let bin_hz = SAMPLE_RATE as f32 / FFT_LEN as f32; + let low = mel(LOW_HZ); + let high = mel(SAMPLE_RATE as f32 / 2.0); + let delta = (high - low) / (NUM_BINS + 1) as f32; + (0..NUM_BINS) + .map(|m| { + let left = low + m as f32 * delta; + let center = left + delta; + let right = center + delta; + let mut first = None; + let mut weights = Vec::new(); + for i in 0..FFT_LEN / 2 { + let x = mel(bin_hz * i as f32); + let w = if x > left && x < right { + if x <= center { + (x - left) / (center - left) + } else { + (right - x) / (right - center) + } + } else { + 0.0 + }; + if w > 0.0 { + first.get_or_insert(i); + weights.push(w); + } else if first.is_some() { + break; + } + } + (first.unwrap_or(0), weights) + }) + .collect() +} diff --git a/apps/desktop-gpui/diarize/src/fbank/tests.rs b/apps/desktop-gpui/diarize/src/fbank/tests.rs new file mode 100644 index 00000000000..1aeaebe55a1 --- /dev/null +++ b/apps/desktop-gpui/diarize/src/fbank/tests.rs @@ -0,0 +1,72 @@ +use super::*; + +fn tone(hz: f32, ms: usize) -> Vec { + (0..ms * 16) + .map(|i| 0.3 * (2.0 * std::f32::consts::PI * hz * i as f32 / 16_000.0).sin()) + .collect() +} + +#[test] +fn frame_count_snips_to_the_edges() { + assert_eq!(Fbank::frames(0), 0); + assert_eq!(Fbank::frames(399), 0); + assert_eq!(Fbank::frames(400), 1); + assert_eq!(Fbank::frames(559), 1); + assert_eq!(Fbank::frames(560), 2); + assert_eq!(Fbank::frames(16_000), 98); +} + +#[test] +fn a_tone_peaks_in_the_bin_holding_its_frequency() { + let mut fbank = Fbank::default(); + let mut out = Vec::new(); + fbank.compute(&tone(1_000.0, 500), 1.0, false, &mut out); + assert_eq!(out.len(), Fbank::frames(8_000) * NUM_BINS); + let row = &out[10 * NUM_BINS..11 * NUM_BINS]; + let peak = (0..NUM_BINS) + .max_by(|&a, &b| row[a].total_cmp(&row[b])) + .unwrap(); + let low = mel(LOW_HZ); + let delta = (mel(8_000.0) - low) / (NUM_BINS + 1) as f32; + let expected = ((mel(1_000.0) - low) / delta - 1.0).round() as usize; + assert!( + peak.abs_diff(expected) <= 1, + "peak {peak}, expected {expected}" + ); +} + +#[test] +fn mean_normalisation_centres_every_bin() { + let mut fbank = Fbank::default(); + let mut out = vec![7.0; 3]; + let mut audio = tone(300.0, 400); + audio.extend(tone(2_500.0, 400)); + fbank.compute(&audio, 32_768.0, true, &mut out); + assert_eq!(&out[..3], &[7.0, 7.0, 7.0]); + let frames = (out.len() - 3) / NUM_BINS; + for bin in 0..NUM_BINS { + let mean: f32 = (0..frames) + .map(|f| out[3 + f * NUM_BINS + bin]) + .sum::() + / frames as f32; + assert!(mean.abs() < 1e-3, "bin {bin} mean {mean}"); + } +} + +#[test] +fn silence_gives_finite_floor_values() { + let mut fbank = Fbank::default(); + let mut out = Vec::new(); + fbank.compute(&vec![0.0; 4_000], 1.0, false, &mut out); + assert!(!out.is_empty()); + assert!(out.iter().all(|v| v.is_finite())); + assert!(out.iter().all(|v| (*v - f32::EPSILON.ln()).abs() < 1e-3)); +} + +#[test] +fn every_mel_bin_has_weights() { + for (first, weights) in mel_banks() { + assert!(!weights.is_empty()); + assert!(first + weights.len() <= FFT_LEN / 2); + } +} diff --git a/apps/desktop-gpui/diarize/src/hybrid.rs b/apps/desktop-gpui/diarize/src/hybrid.rs new file mode 100644 index 00000000000..7e4d872e023 --- /dev/null +++ b/apps/desktop-gpui/diarize/src/hybrid.rs @@ -0,0 +1,350 @@ +//! Who is who at the end of a meeting: the speaker-embedding labels decide +//! where speech, echoes and the call's leak into the microphone are, and who +//! is who; the Nemotron voices (see `nemotron`) correct the count where they +//! hear a track's people differently. +//! +//! Per track, each existing speaker id gets the Nemotron voice covering most +//! of its non-echo speech; voices are counted over that speech only (the +//! call's leak into the microphone is not a person). People count with at +//! least `BIG_MS` of speech, or `PERSON_SHARE` of the track's speech on a +//! track too short for that (never under `MIN_PERSON_MS`): in a 30 s meeting +//! nobody but the main talker reaches 10 s. +//! - More voices than ids: a voice of at least `NEW_VOICE_MS` that is nobody's +//! main voice is cut out of the id holding most of it (if it is at least +//! `P_SPLIT` of that id): someone had been folded into someone else. At +//! most as many as Nemotron hears more people than there are ids, biggest +//! first, so a track never ends with more people than the model heard. +//! - Fewer voices than ids: a minor id (under `MINOR_MS` and `MINOR_SHARE` of +//! the track) whose speech is at least `P_MERGE` one voice joins the biggest +//! id with that main voice: a fragment of a person. Bigger ids never merge: +//! on codec-degraded call audio Nemotron hears fewer people than there are, +//! and merging two real people is the worst error. +//! - Equal counts: nothing moves. +//! +//! Echo spans never move. A track whose model filled all its voices keeps the +//! old labels (the model holds 8 voices per track), as does a track it never +//! heard. + +use std::collections::{BTreeMap, HashMap}; + +use crate::nemotron::{TrackVoices, track_slot}; +use crate::{FinalPass, Span, SpeakerId, SpeakerInfo, Track}; + +/// Speech a Nemotron voice needs before it may become a new person. +pub const NEW_VOICE_MS: u64 = 10_000; +/// Speech an id or a voice needs to count as a person on its track. +pub const BIG_MS: u64 = 10_000; +/// Share of the holder's speech a new voice must hold to be cut out. +pub const P_SPLIT: f64 = 0.2; +/// Share of a fragment's speech one voice must hold for it to merge. +pub const P_MERGE: f64 = 0.8; +/// A fragment: less speech than this, and less than `MINOR_SHARE` of its track. +pub const MINOR_MS: u64 = 60_000; +pub const MINOR_SHARE: f64 = 0.15; +/// On a short track a person needs this share of its speech (at most +/// `BIG_MS`, so tracks with 66.7 s of speech or more use `BIG_MS` as before). +pub const PERSON_SHARE: f64 = 0.15; +/// Least speech anyone counts as a person with, however short the track. +pub const MIN_PERSON_MS: u64 = 3_000; + +/// Speech an id or a voice needs to count as a person on a track holding +/// `track_ms` of speech. +pub fn person_ms(track_ms: u64) -> u64 { + ((track_ms as f64 * PERSON_SHARE) as u64).clamp(MIN_PERSON_MS, BIG_MS) +} + +/// A microphone voice heard this share of its time while the call is talking +/// (the call's spans widened by `CALL_SLACK_MS`) is chatter or the call in +/// the room, never the user: the same test the speaker model's end-of-meeting +/// choice of the user makes. +const HEARD_WITH_CALL: f64 = 0.6; +const CALL_SLACK_MS: u64 = 1_000; + +/// Shortest piece a microphone span is cut into. +const MIN_PIECE_MS: u64 = 300; +const SPEAKERS: usize = crate::nemotron::cache::SPEAKERS; + +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub struct HybridStats { + pub relabelled_ms: u64, + pub new_voices: u32, + pub merged_ids: u32, + pub tracks_kept: u32, +} + +pub fn apply(pass: &mut FinalPass, voices: &[TrackVoices; 2]) -> HybridStats { + let mut stats = HybridStats::default(); + let mut next_id = pass + .speakers + .iter() + .map(|s| s.id.0) + .chain(pass.merged.iter().flat_map(|(a, b)| [a.0, b.0])) + .chain(pass.segments.iter().flat_map(|s| s.spans.iter().map(|sp| sp.speaker.0))) + .max() + .unwrap_or(0) + + 1; + for track in [Track::Mic, Track::System] { + let tv = &voices[track_slot(track)]; + let used = tv.voices(); + if tv.frames() == 0 || used == 0 || used >= SPEAKERS { + stats.tracks_kept += 1; + continue; + } + relabel_track(pass, tv, track, &mut next_id, &mut stats); + } + recount(pass); + stats +} + +fn relabel_track(pass: &mut FinalPass, tv: &TrackVoices, track: Track, next_id: &mut u32, stats: &mut HybridStats) { + // speech and Nemotron voices of each id on this track + let mut speech: HashMap = HashMap::new(); + let mut share: HashMap = HashMap::new(); + for seg in pass.segments.iter().filter(|s| s.track == track) { + for sp in seg.spans.iter().filter(|s| !s.echo && s.end_ms > s.start_ms) { + *speech.entry(sp.speaker).or_default() += sp.end_ms - sp.start_ms; + let c = tv.cover(sp.start_ms, sp.end_ms); + let e = share.entry(sp.speaker).or_insert([0; SPEAKERS]); + for v in 0..SPEAKERS { + e[v] += c[v]; + } + } + } + // voices over this track's non-echo speech only + let mut size = [0u64; SPEAKERS]; + for c in share.values() { + for v in 0..SPEAKERS { + size[v] += c[v]; + } + } + let person = person_ms(speech.values().sum()); + let n_app = speech.values().filter(|&&n| n >= person).count(); + let n_nem = size.iter().filter(|&&n| n >= person).count(); + let main_of = |c: &[u64; SPEAKERS]| -> Option { + let (v, n) = c.iter().enumerate().max_by_key(|(v, n)| (**n, std::cmp::Reverse(*v)))?; + (*n > 0).then_some(v) + }; + let share_of = |c: &[u64; SPEAKERS], v: usize| -> f64 { + let total: u64 = c.iter().sum(); + if total == 0 { 0.0 } else { c[v] as f64 / total as f64 } + }; + let mut ids: Vec = share.keys().copied().collect(); + ids.sort(); + if n_nem < n_app { + // fragments of one person + let track_ms: u64 = speech.values().sum::().max(1); + let mut groups: BTreeMap> = BTreeMap::new(); + for id in &ids { + let c = &share[id]; + if let Some(v) = main_of(c).filter(|&v| share_of(c, v) >= P_MERGE) { + groups.entry(v).or_default().push(*id); + } + } + for members in groups.values().filter(|m| m.len() > 1) { + let keep = *members.iter().max_by_key(|id| (speech[*id], std::cmp::Reverse(id.0))).unwrap(); + for &old in members.iter().filter(|id| **id != keep) { + let minor = speech[&old] < MINOR_MS && (speech[&old] as f64) < MINOR_SHARE * track_ms as f64; + if !minor { + continue; + } + for seg in pass.segments.iter_mut().filter(|s| s.track == track) { + for sp in seg.spans.iter_mut().filter(|s| !s.echo && s.speaker == old) { + stats.relabelled_ms += sp.end_ms.saturating_sub(sp.start_ms); + sp.speaker = keep; + } + } + pass.merged.push((old, keep)); + stats.merged_ids += 1; + if pass.user == Some(old) { + pass.user = Some(keep); + } + } + } + } else if n_nem > n_app { + // someone folded into someone else + let mains: Vec> = ids.iter().map(|id| main_of(&share[id])).collect(); + let mut candidates: Vec = (0..SPEAKERS) + .filter(|&v| size[v] >= NEW_VOICE_MS.min(person) && !mains.contains(&Some(v))) + .collect(); + candidates.sort_by_key(|&v| (std::cmp::Reverse(size[v]), v)); + let mut room = n_nem - n_app; + for v in candidates { + if room == 0 { + break; + } + let Some(&holder) = ids.iter().max_by_key(|id| (share[*id][v], std::cmp::Reverse(id.0))) else { + continue; + }; + if share_of(&share[&holder], v) < P_SPLIT { + continue; + } + let new = SpeakerId(*next_id); + *next_id += 1; + stats.new_voices += 1; + room -= 1; + for seg in pass.segments.iter_mut().filter(|s| s.track == track) { + let mut out: Vec = Vec::with_capacity(seg.spans.len()); + for sp in std::mem::take(&mut seg.spans) { + if sp.echo || sp.speaker != holder || sp.end_ms <= sp.start_ms { + out.push(sp); + continue; + } + let pieces = if track == Track::Mic { cut(tv, sp.start_ms, sp.end_ms) } else { Vec::new() }; + if pieces.len() > 1 { + for (a, b, pv) in pieces { + let speaker = if pv == Some(v) { new } else { holder }; + if speaker == new { + stats.relabelled_ms += b - a; + } + out.push(Span { start_ms: a, end_ms: b, speaker, echo: false }); + } + } else { + let c = tv.cover(sp.start_ms, sp.end_ms); + let moved = main_of(&c) == Some(v); + if moved { + stats.relabelled_ms += sp.end_ms - sp.start_ms; + } + out.push(Span { speaker: if moved { new } else { holder }, ..sp }); + } + } + seg.spans = join_spans(out); + } + // The user's id held two voices: the user stays with the part + // that talks when the call is quiet, not with chatter heard over it. + if track == Track::Mic + && pass.user == Some(holder) + && heard_with_call(pass, holder) >= HEARD_WITH_CALL + && heard_with_call(pass, new) < HEARD_WITH_CALL + { + pass.user = Some(new); + } + } + } +} + +/// Share of a microphone id's speech the call is talking through. +fn heard_with_call(pass: &FinalPass, id: SpeakerId) -> f64 { + let mut call: Vec<(u64, u64)> = pass + .segments + .iter() + .filter(|s| s.track == Track::System) + .flat_map(|s| s.spans.iter()) + .filter(|s| !s.echo && s.end_ms > s.start_ms) + .map(|s| { + ( + s.start_ms.saturating_sub(CALL_SLACK_MS), + s.end_ms + CALL_SLACK_MS, + ) + }) + .collect(); + call.sort_unstable(); + let (mut covered, mut total) = (0u64, 0u64); + for sp in pass + .segments + .iter() + .filter(|s| s.track == Track::Mic) + .flat_map(|s| s.spans.iter()) + .filter(|s| !s.echo && s.speaker == id && s.end_ms > s.start_ms) + { + total += sp.end_ms - sp.start_ms; + let mut until = sp.start_ms; + for &(a, b) in &call { + if a >= sp.end_ms { + break; + } + let from = a.max(until); + let to = b.min(sp.end_ms); + if to > from { + covered += to - from; + until = to; + } + } + } + if total == 0 { + 0.0 + } else { + covered as f64 / total as f64 + } +} + +fn join_spans(mut spans: Vec) -> Vec { + spans.sort_by_key(|s| (s.start_ms, s.end_ms)); + let mut merged: Vec = Vec::new(); + for s in spans { + match merged.last_mut() { + Some(last) if last.speaker == s.speaker && last.echo == s.echo && last.end_ms >= s.start_ms => { + last.end_ms = last.end_ms.max(s.end_ms); + } + _ => merged.push(s), + } + } + merged +} + +/// Pieces of a microphone span where one Nemotron voice holds at least +/// `MIN_PIECE_MS`; silent frames join the piece before them. +fn cut(tv: &TrackVoices, start_ms: u64, end_ms: u64) -> Vec<(u64, u64, Option)> { + let turns = tv.turns(start_ms, end_ms); + let mut runs: Vec<(u64, u64, usize)> = Vec::new(); + for (ms, v) in turns { + let Some(v) = v else { continue }; + match runs.last_mut() { + Some((_, e, rv)) if *rv == v => *e = ms + 10, + _ => runs.push((ms, ms + 10, v)), + } + } + // runs shorter than MIN_PIECE_MS join a neighbour + let mut kept: Vec<(u64, u64, usize)> = Vec::new(); + for r in runs { + if r.1 - r.0 < MIN_PIECE_MS { + if let Some(last) = kept.last_mut() { + last.1 = r.1; + continue; + } + } + match kept.last_mut() { + Some(last) if last.2 == r.2 => last.1 = r.1, + _ => kept.push(r), + } + } + if kept.len() < 2 { + return Vec::new(); + } + let mut out: Vec<(u64, u64, Option)> = Vec::new(); + for (i, (a, _, v)) in kept.iter().enumerate() { + let a = if i == 0 { start_ms } else { *a }; + let b = kept.get(i + 1).map_or(end_ms, |n| n.0); + if b > a { + out.push((a, b, Some(*v))); + } + } + out +} + +/// Speakers list from the rewritten spans. +fn recount(pass: &mut FinalPass) { + let mut info: BTreeMap = BTreeMap::new(); + for seg in &pass.segments { + for s in &seg.spans { + if s.echo || s.end_ms <= s.start_ms { + continue; + } + let e = info.entry(s.speaker.0).or_insert(SpeakerInfo { + id: s.speaker, + track: seg.track, + first_ms: s.start_ms, + speech_ms: 0, + is_user: false, + }); + e.first_ms = e.first_ms.min(s.start_ms); + e.speech_ms += s.end_ms - s.start_ms; + } + } + for i in info.values_mut() { + i.is_user = pass.user == Some(i.id); + } + pass.speakers = info.into_values().collect(); +} + +#[cfg(test)] +mod tests; diff --git a/apps/desktop-gpui/diarize/src/hybrid/tests.rs b/apps/desktop-gpui/diarize/src/hybrid/tests.rs new file mode 100644 index 00000000000..9be045f8959 --- /dev/null +++ b/apps/desktop-gpui/diarize/src/hybrid/tests.rs @@ -0,0 +1,241 @@ +use super::*; +use crate::SegmentLabels; +use crate::nemotron::TrackVoices; + +fn span(a: u64, b: u64, id: u32) -> Span { + Span { start_ms: a, end_ms: b, speaker: SpeakerId(id), echo: false } +} + +fn echo(a: u64, b: u64, id: u32) -> Span { + Span { echo: true, ..span(a, b, id) } +} + +fn seg(track: Track, key: u64, spans: Vec) -> SegmentLabels { + SegmentLabels { track, key, spans } +} + +fn pass(segments: Vec, user: Option) -> FinalPass { + let mut p = FinalPass { segments, user: user.map(SpeakerId), ..Default::default() }; + recount(&mut p); + p +} + +/// Voices on a track: (start_ms, end_ms, voice) over one continuous part from 0. +fn voices(track_ms: u64, turns: &[(u64, u64, usize)]) -> TrackVoices { + TrackVoices::scripted(track_ms, turns) +} + +fn ids(p: &FinalPass, track: Track) -> Vec<(u64, u64, u32, bool)> { + p.segments + .iter() + .filter(|s| s.track == track) + .flat_map(|s| s.spans.iter().map(|sp| (sp.start_ms, sp.end_ms, sp.speaker.0, sp.echo))) + .collect() +} + +#[test] +fn nemotron_never_merges_big_ids_even_when_it_hears_fewer_people() { + // two call-side ids of 60 s and 40 s; the model hears one voice: on call audio it under-counts, so no merge + let mut p = pass( + vec![seg(Track::System, 1, vec![span(0, 60_000, 2)]), seg(Track::System, 2, vec![span(60_000, 100_000, 5)])], + None, + ); + let before = p.clone(); + let st = apply(&mut p, &[TrackVoices::default(), voices(100_000, &[(0, 100_000, 0)])]); + assert_eq!(p.segments, before.segments); + assert!(p.merged.is_empty()); + assert_eq!(st.merged_ids, 0); +} + +#[test] +fn a_small_fragment_of_one_person_joins_them() { + // 200 s and a 12 s fragment of the same remote person (6%): the fragment merges into the big id + let mut p = pass( + vec![seg(Track::System, 1, vec![span(0, 200_000, 2)]), seg(Track::System, 2, vec![span(205_000, 217_000, 9)])], + None, + ); + let st = apply(&mut p, &[TrackVoices::default(), voices(220_000, &[(0, 220_000, 0)])]); + assert_eq!(p.speakers.len(), 1); + assert_eq!(p.speakers[0].id, SpeakerId(2)); + assert_eq!(p.merged, vec![(SpeakerId(9), SpeakerId(2))]); + assert_eq!(st.merged_ids, 1); +} + +#[test] +fn the_calls_leak_on_the_microphone_is_not_counted_as_a_person() { + // the mic holds the user plus the call heard through the speakers (echo); the model hears both as voices + let mut p = pass( + vec![seg(Track::Mic, 1, vec![span(0, 30_000, 1), echo(30_000, 90_000, 4)])], + Some(1), + ); + let before = p.clone(); + apply(&mut p, &[voices(90_000, &[(0, 30_000, 0), (30_000, 90_000, 1)]), TrackVoices::default()]); + assert_eq!(p.segments, before.segments, "one person on the mic: nothing to split"); +} + +#[test] +fn two_people_at_one_microphone_are_cut_apart_and_the_newcomer_gets_a_new_id() { + // one mic segment the embeddings gave entirely to the user; the model hears a second voice for 12 s + let mut p = pass(vec![seg(Track::Mic, 1, vec![span(0, 40_000, 1)])], Some(1)); + let v = [voices(40_000, &[(0, 20_000, 0), (20_000, 32_000, 1), (32_000, 40_000, 0)]), TrackVoices::default()]; + let st = apply(&mut p, &v); + let spans = ids(&p, Track::Mic); + assert_eq!(spans.len(), 3, "{spans:?}"); + assert_eq!(spans[0].2, 1); + assert_ne!(spans[1].2, 1); + assert_eq!(spans[2].2, 1); + assert_eq!(p.user, Some(SpeakerId(1)), "the user stays the user"); + assert_eq!(st.new_voices, 1); +} + +#[test] +fn a_short_extra_voice_does_not_become_a_person() { + let mut p = pass(vec![seg(Track::Mic, 1, vec![span(0, 40_000, 1)])], Some(1)); + let v = [voices(40_000, &[(0, 20_000, 0), (20_000, 24_000, 1), (24_000, 40_000, 0)]), TrackVoices::default()]; + apply(&mut p, &v); + assert!(ids(&p, Track::Mic).iter().all(|s| s.2 == 1), "{:?}", ids(&p, Track::Mic)); + assert_eq!(p.speakers.len(), 1); +} + +#[test] +fn echoes_and_leaks_stay_as_they_were() { + let mut p = pass( + vec![seg(Track::Mic, 1, vec![span(0, 10_000, 1), echo(10_000, 30_000, 3)]), seg(Track::System, 2, vec![span(10_000, 30_000, 3)])], + Some(1), + ); + // the model hears the leaked call on the mic as a second voice: the echo flag wins + let v = [voices(30_000, &[(0, 10_000, 0), (10_000, 30_000, 1)]), voices(30_000, &[(10_000, 30_000, 0)])]; + apply(&mut p, &v); + let mic = ids(&p, Track::Mic); + assert!(mic.contains(&(10_000, 30_000, 3, true)), "{mic:?}"); + assert!(mic.iter().filter(|s| !s.3).all(|s| s.2 == 1)); +} + +#[test] +fn a_track_with_all_eight_voices_keeps_its_labels() { + let spans: Vec = (0..10).map(|k| span(k * 10_000, k * 10_000 + 9_000, 10 + k as u32)).collect(); + let mut p = pass(vec![seg(Track::System, 1, spans.clone())], None); + let turns: Vec<(u64, u64, usize)> = (0..10).map(|k| (k * 10_000, k * 10_000 + 9_000, (k as usize).min(7))).collect(); + let st = apply(&mut p, &[TrackVoices::default(), voices(100_000, &turns)]); + assert_eq!(st.tracks_kept, 2); + assert_eq!(p.segments[0].spans, spans); +} + +#[test] +fn no_model_output_changes_nothing() { + let mut p = pass(vec![seg(Track::System, 1, vec![span(0, 5_000, 2), span(5_000, 9_000, 4)])], None); + let before = p.clone(); + let st = apply(&mut p, &[TrackVoices::default(), TrackVoices::default()]); + assert_eq!(p, before); + assert_eq!(st.tracks_kept, 2); +} + +#[test] +fn the_user_keeps_their_id_when_a_room_voice_is_cut_out() { + let mut p = pass(vec![seg(Track::Mic, 1, vec![span(0, 60_000, 7)])], Some(7)); + apply(&mut p, &[voices(60_000, &[(0, 40_000, 0), (40_000, 60_000, 1)]), TrackVoices::default()]); + assert_eq!(p.user, Some(SpeakerId(7))); + assert!(p.speakers.iter().any(|s| s.id == SpeakerId(7) && s.is_user && s.speech_ms == 40_000)); + assert!(p.speakers.iter().any(|s| s.id != SpeakerId(7) && !s.is_user && s.speech_ms == 20_000)); +} + +#[test] +fn the_user_follows_their_own_voice_when_chatter_was_folded_into_them() { + // cafe chatter the whole time the call talks, folded into the user's id (1) with the user's own + // words in the call's pauses; the model hears the two apart and the chatter is the bigger voice + let mut mic = Vec::new(); + let mut sys = Vec::new(); + let mut turns = Vec::new(); + for i in 0..6u64 { + let t = i * 22_000; + mic.push(seg(Track::Mic, t, vec![span(t, t + 12_000, 1)])); + mic.push(seg( + Track::Mic, + t + 13_000, + vec![span(t + 13_000, t + 20_000, 1)], + )); + sys.push(seg(Track::System, t, vec![span(t, t + 12_000, 2)])); + turns.push((t, t + 12_000, 0)); + turns.push((t + 13_000, t + 20_000, 1)); + } + mic.extend(sys); + let mut p = pass(mic, Some(1)); + let st = apply(&mut p, &[voices(132_000, &turns), TrackVoices::default()]); + assert_eq!(st.new_voices, 1); + let own = ids(&p, Track::Mic) + .iter() + .find(|s| s.0 == 13_000) + .map(|s| s.2); + assert_eq!(p.user.map(|u| u.0), own, "{:?}", ids(&p, Track::Mic)); + assert_ne!(p.user, Some(SpeakerId(1)), "the chatter is not the user"); +} + +#[test] +fn when_the_counts_agree_nothing_moves() { + // two remote people; the model gives most of the second one's speech to the first (the room-1 failure of a + // per-span relabel): with both counting two people the embedding labels stand + let mut p = pass( + vec![seg(Track::System, 1, vec![span(0, 60_000, 2)]), seg(Track::System, 2, vec![span(60_000, 100_000, 3)])], + None, + ); + let before = p.clone(); + let st = apply(&mut p, &[TrackVoices::default(), voices(100_000, &[(0, 80_000, 0), (80_000, 100_000, 1)])]); + assert_eq!(p.segments, before.segments); + assert_eq!((st.merged_ids, st.new_voices), (0, 0)); +} + +#[test] +fn a_person_counts_from_a_share_of_a_short_track_and_from_10_s_on_a_long_one() { + assert_eq!(person_ms(28_000), 4_200); + assert_eq!(person_ms(10_000), MIN_PERSON_MS); + assert_eq!(person_ms(66_700), BIG_MS); + assert_eq!(person_ms(3_600_000), BIG_MS); +} + +#[test] +fn a_third_person_in_a_short_meeting_is_cut_out() { + // 28 s at one microphone: the embeddings hear two people, Nemotron three (the third talks 6 s) + let mut p = pass( + vec![ + seg(Track::Mic, 1, vec![span(0, 12_000, 1)]), + seg(Track::Mic, 2, vec![span(12_000, 22_000, 2)]), + seg(Track::Mic, 3, vec![span(22_000, 28_000, 2)]), + ], + Some(1), + ); + let v = [voices(28_000, &[(0, 12_000, 0), (12_000, 22_000, 1), (22_000, 28_000, 2)]), TrackVoices::default()]; + let stats = apply(&mut p, &v); + assert_eq!(stats.new_voices, 1, "{:?}", ids(&p, Track::Mic)); + let mic = ids(&p, Track::Mic); + let third = mic.iter().find(|s| s.0 == 22_000).expect("the last span").2; + assert!(third != 1 && third != 2, "{mic:?}"); + assert_eq!(p.speakers.len(), 3); + assert_eq!(p.user, Some(SpeakerId(1))); +} + +#[test] +fn a_brief_voice_in_a_short_meeting_is_still_not_a_person() { + // 28 s, a 2 s interjection: under MIN_PERSON_MS + let mut p = pass(vec![seg(Track::Mic, 1, vec![span(0, 14_000, 1)]), seg(Track::Mic, 2, vec![span(14_000, 28_000, 2)])], Some(1)); + let v = [voices(28_000, &[(0, 14_000, 0), (14_000, 26_000, 1), (26_000, 28_000, 2)]), TrackVoices::default()]; + let stats = apply(&mut p, &v); + assert_eq!(stats.new_voices, 0, "{:?}", ids(&p, Track::Mic)); + assert_eq!(p.speakers.len(), 2); +} + +#[test] +fn a_short_meeting_never_ends_with_more_people_than_nemotron_heard() { + // two ids, Nemotron hears three voices, two of them nobody's main voice: one new person, the bigger voice + let mut p = pass(vec![seg(Track::Mic, 1, vec![span(0, 18_000, 1)]), seg(Track::Mic, 2, vec![span(18_000, 28_000, 2)])], Some(1)); + let v = [ + voices(28_000, &[(0, 10_000, 0), (10_000, 16_000, 1), (16_000, 18_000, 0), (18_000, 23_000, 0), (23_000, 28_000, 2)]), + TrackVoices::default(), + ]; + let stats = apply(&mut p, &v); + assert_eq!(stats.new_voices, 1, "{:?}", ids(&p, Track::Mic)); + assert_eq!(p.speakers.len(), 3, "{:?}", p.speakers); + let mic = ids(&p, Track::Mic); + // the 6 s voice (10-16 s) is the one cut out, the 5 s one stays with its id + let new_id = mic.iter().find(|s| s.0 == 10_000).map(|s| s.2).expect("a cut at 10 s"); + assert!(new_id != 1 && new_id != 2, "{mic:?}"); +} diff --git a/apps/desktop-gpui/diarize/src/lib.rs b/apps/desktop-gpui/diarize/src/lib.rs new file mode 100644 index 00000000000..50b872c7517 --- /dev/null +++ b/apps/desktop-gpui/diarize/src/lib.rs @@ -0,0 +1,125 @@ +//! Who said what: separates the voices inside each meeting track. +//! +//! The app feeds every speech segment of a track (16 kHz mono) as it closes. +//! Each segment comes back with one or more spans, each carrying a speaker id +//! that never changes meaning while the meeting runs. When the meeting ends, +//! [`MeetingDiarizer::finish`] revisits every segment with full context and +//! reports the corrected labels plus which live ids were folded into which, so +//! stored utterances can be relabelled without losing renames. + +mod cluster; +mod diarizer; +pub mod hybrid; +pub mod nemotron; +pub mod embed; +pub mod fbank; +mod names; +mod profile; + +use serde::{Deserialize, Serialize}; + +pub use diarizer::{Config, LateNemotron, MeetingDiarizer, Segment, Stats}; +pub use embed::{Embed, ModelInfo}; +#[cfg(feature = "onnx")] +pub use embed::OnnxEmbedder; +pub use names::{Label, NameChange, SpeakerNames}; +pub use profile::VoiceProfile; + +pub const SAMPLE_RATE: u32 = 16_000; + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub enum Track { + Mic, + System, +} + +#[derive( + Clone, Copy, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize, +)] +#[serde(transparent)] +pub struct SpeakerId(pub u32); + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Span { + pub start_ms: u64, + pub end_ms: u64, + pub speaker: SpeakerId, + /// The voice belongs to the other track's speaker and reached this track + /// through the room or the speakers: a duplicate, not a new person. + pub echo: bool, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct SegmentLabels { + pub track: Track, + pub key: u64, + pub spans: Vec, +} + +impl SegmentLabels { + /// The speaker holding most of the segment, ignoring echoes when anyone + /// else is in it. + pub fn main_speaker(&self) -> Option { + let mut totals: Vec<(SpeakerId, bool, u64)> = Vec::new(); + for span in &self.spans { + let len = span.end_ms.saturating_sub(span.start_ms); + match totals + .iter_mut() + .find(|(id, echo, _)| *id == span.speaker && *echo == span.echo) + { + Some(entry) => entry.2 += len, + None => totals.push((span.speaker, span.echo, len)), + } + } + let direct = totals.iter().filter(|t| !t.1).max_by_key(|t| t.2); + direct + .or_else(|| totals.iter().max_by_key(|t| t.2)) + .map(|t| t.0) + } +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct SpeakerInfo { + pub id: SpeakerId, + pub track: Track, + pub first_ms: u64, + pub speech_ms: u64, + pub is_user: bool, +} + +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct FinalPass { + /// Every segment the diarizer saw, with its final spans. + pub segments: Vec, + /// Live ids that no longer exist, each with the id that absorbed it. + pub merged: Vec<(SpeakerId, SpeakerId)>, + pub speakers: Vec, + pub user: Option, + /// A refreshed voice profile when the microphone held a single voice for + /// long enough to learn from. + pub enrolment: Option, +} + +#[derive(Debug, thiserror::Error)] +pub enum Error { + #[error("speaker model runtime: {0}")] + Runtime(String), + #[error("speaker model: {0}")] + Model(String), + #[error("audio is too short to embed")] + TooShort, + #[error("voice profile: {0}")] + Profile(String), +} + +impl Error { + #[cfg(feature = "onnx")] + pub(crate) fn runtime(error: impl std::fmt::Display) -> Self { + Self::Runtime(error.to_string()) + } +} diff --git a/apps/desktop-gpui/diarize/src/names.rs b/apps/desktop-gpui/diarize/src/names.rs new file mode 100644 index 00000000000..15d00ff536d --- /dev/null +++ b/apps/desktop-gpui/diarize/src/names.rs @@ -0,0 +1,297 @@ +use std::collections::BTreeMap; + +use serde::{Deserialize, Serialize}; + +use crate::{FinalPass, SpeakerId, SpeakerInfo, Track}; + +#[cfg(test)] +mod tests; + +/// When the calendar lists one other attendee but the computer's audio holds +/// a second voice briefly (someone saying hello from their side of the call), +/// the attendee's name stays on the voice holding at least this share. +const ATTENDEE_DOMINANCE: f64 = 0.85; + +/// How a speaker is shown. The app turns `User` into the user's name (or its +/// word for "you") and `Person(n)` into its localised "Person n". +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(tag = "kind", content = "value", rename_all = "camelCase")] +pub enum Label { + User, + Named(String), + Person(u32), +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct NameChange { + pub id: SpeakerId, + pub before: Option