Skip to content

Open source Grok Bot alternative, Bring your own agent and subscription: message Claude Code, Codex, Gemini CLI, Cursor, Copilot and 40+ AI agents as bots on your own computer, from iPhone, Mac, Windows, Linux or terminal. Agent-agnostic and free. Also a Muse and Dots alternative. Built in Rust.

feat(desktop): port the remaining Mac screens and services

leepokai7a41069292c4
diff --git a/.github/workflows/host.yml b/.github/workflows/host.yml
index 32f11a46..231f3968 100644
--- a/.github/workflows/host.yml
+++ b/.github/workflows/host.yml
@@ -3,10 +3,10 @@ name: codync-host
on:
push:
branches: [main, v2]
- paths: ["host/**", "apps/linux/**", "packaging/updates/**", "docs/reference/fixtures/remote-relay-vectors.json", ".github/workflows/host.yml"]
- tags: ["v*"] # one version tag ships the Mac app, the host and the Linux app
+ paths: ["host/**", "packaging/updates/**", "docs/reference/fixtures/remote-relay-vectors.json", ".github/workflows/host.yml"]
+ tags: ["v*"] # one version tag ships the host and the desktop app (release-desktop.yml)
pull_request:
- paths: ["host/**", "apps/linux/**", "packaging/updates/**", "docs/reference/fixtures/remote-relay-vectors.json"]
+ paths: ["host/**", "packaging/updates/**", "docs/reference/fixtures/remote-relay-vectors.json"]
jobs:
test:
@@ -28,62 +28,6 @@ jobs:
- run: cargo clippy --all-targets -- -D warnings
- run: cargo test
- linux-app:
- strategy:
- matrix:
- include:
- - { os: ubuntu-24.04, target: x86_64-unknown-linux-gnu, name: linux-x86_64 }
- - { os: ubuntu-24.04-arm, target: aarch64-unknown-linux-gnu, name: linux-arm64 }
- runs-on: ${{ matrix.os }}
- permissions:
- contents: write
- defaults:
- run:
- working-directory: apps/linux
- steps:
- - uses: actions/checkout@v4
- - run: sudo apt-get update && sudo apt-get install -y libgtk-4-dev libadwaita-1-dev libvte-2.91-gtk4-dev xvfb dbus-x11 imagemagick
- - name: Install Rust (rust-toolchain.toml)
- run: rustup toolchain install
- - uses: Swatinem/rust-cache@v2
- with:
- workspaces: apps/linux
- - name: Version matches the host
- # The host's minApp is compared with this app's version (docs/reference/compatibility.md).
- run: |
- read_version() { python3 -c 'import sys, tomllib; print(tomllib.load(open(sys.argv[1], "rb"))["package"]["version"])' "$1"; }
- test "$(read_version Cargo.toml)" = "$(read_version ../../host/Cargo.toml)" || { echo 'apps/linux and host versions must match'; exit 1; }
- - run: cargo clippy -- -D warnings
- - run: cargo test
- - name: Verify native UI flows
- env:
- GTK_A11Y: none
- CODYNC_UI_ARTIFACTS: ${{ github.workspace }}/build/ui-parity
- run: xvfb-run -a dbus-run-session -- python3 tests/ui_fixture.py
- - uses: actions/upload-artifact@v4
- if: always()
- with:
- name: native-ui-${{ matrix.name }}
- path: build/ui-parity/
- - name: Package
- if: startsWith(github.ref, 'refs/tags/v')
- run: |
- # <product>-<os>-<arch>, no version: the website links to releases/latest/download/<name>.
- NAME=codync-${{ matrix.name }}
- cargo build --release
- mkdir -p $NAME/share/applications $NAME/share/icons/hicolor/scalable/apps $NAME/bin
- cp target/release/codync $NAME/bin/
- cp data/com.pokai.Codync.desktop $NAME/share/applications/
- cp data/com.pokai.Codync.svg $NAME/share/icons/hicolor/scalable/apps/
- tar czf $GITHUB_WORKSPACE/$NAME.tar.gz $NAME
- cd $GITHUB_WORKSPACE && shasum -a 256 $NAME.tar.gz > $NAME.tar.gz.sha256
- - uses: softprops/action-gh-release@v2
- if: startsWith(github.ref, 'refs/tags/v')
- with:
- files: |
- codync-linux-*.tar.gz
- codync-linux-*.tar.gz.sha256
-
release:
if: startsWith(github.ref, 'refs/tags/v')
needs: test
diff --git a/.github/workflows/release-desktop.yml b/.github/workflows/release-desktop.yml
new file mode 100644
index 00000000..4ffe9871
--- /dev/null
+++ b/.github/workflows/release-desktop.yml
@@ -0,0 +1,206 @@
+name: Release desktop
+
+# The Electron app (apps/desktop) for macOS and Linux. A v* tag publishes it; anything else is a
+# dry run that builds, signs and notarizes but publishes nothing.
+on:
+ push:
+ tags: ["v*"]
+ pull_request:
+ paths: ["apps/desktop/**", ".github/workflows/release-desktop.yml"]
+ workflow_dispatch:
+
+permissions:
+ contents: write
+
+jobs:
+ check:
+ runs-on: ubuntu-latest
+ defaults:
+ run:
+ working-directory: apps/desktop
+ steps:
+ - uses: actions/checkout@v4
+ - uses: actions/setup-node@v4
+ with:
+ node-version: 24
+ cache: npm
+ cache-dependency-path: apps/desktop/package-lock.json
+ - run: npm ci
+ - name: Version matches the host
+ # The host's minApp is compared with this app's version (docs/reference/compatibility.md).
+ run: |
+ APP=$(node -p 'require("./package.json").version')
+ HOST=$(python3 -c 'import tomllib; print(tomllib.load(open("../../host/Cargo.toml", "rb"))["package"]["version"])')
+ test "$APP" = "$HOST" || { echo "apps/desktop ($APP) and host ($HOST) versions must match"; exit 1; }
+ - run: npm run typecheck
+ - run: npm test
+ - run: npm run build
+
+ macos:
+ needs: check
+ runs-on: macos-26
+ steps:
+ - uses: actions/checkout@v4
+ - uses: actions/setup-node@v4
+ with:
+ node-version: 24
+ cache: npm
+ cache-dependency-path: apps/desktop/package-lock.json
+ - name: Install Rust (rust-toolchain.toml)
+ run: rustup toolchain install
+ - run: rustup target add aarch64-apple-darwin x86_64-apple-darwin
+
+ - name: Install Apple certificate
+ env:
+ APPLE_CERTIFICATE_P12: ${{ secrets.APPLE_CERTIFICATE_P12 }}
+ APPLE_CERTIFICATE_PASSWORD: ${{ secrets.APPLE_CERTIFICATE_PASSWORD }}
+ run: |
+ KEYCHAIN_FILE=$RUNNER_TEMP/app-signing.keychain-db
+ KEYCHAIN_PASSWORD=$(openssl rand -base64 32)
+ echo "$APPLE_CERTIFICATE_P12" | base64 --decode > "$RUNNER_TEMP/certificate.p12"
+ security create-keychain -p "$KEYCHAIN_PASSWORD" "$KEYCHAIN_FILE"
+ security set-keychain-settings -lut 21600 "$KEYCHAIN_FILE"
+ security unlock-keychain -p "$KEYCHAIN_PASSWORD" "$KEYCHAIN_FILE"
+ security import "$RUNNER_TEMP/certificate.p12" -P "$APPLE_CERTIFICATE_PASSWORD" -A -t cert -f pkcs12 -k "$KEYCHAIN_FILE"
+ security set-key-partition-list -S apple-tool:,apple:,codesign: -s -k "$KEYCHAIN_PASSWORD" "$KEYCHAIN_FILE"
+ security list-keychain -d user -s "$KEYCHAIN_FILE"
+
+ - name: Native pieces
+ run: |
+ mkdir -p apps/desktop/build/native
+ # The host, universal.
+ cargo build --release --manifest-path host/Cargo.toml --target aarch64-apple-darwin
+ cargo build --release --manifest-path host/Cargo.toml --target x86_64-apple-darwin
+ lipo -create -output apps/desktop/build/native/codync-host \
+ host/target/aarch64-apple-darwin/release/codync-host host/target/x86_64-apple-darwin/release/codync-host
+ # On-device speech for calls.
+ swift build -c release --arch arm64 --arch x86_64 --package-path apps/desktop/native/speech-macos
+ cp "$(swift build -c release --arch arm64 --arch x86_64 --package-path apps/desktop/native/speech-macos --show-bin-path)/codync-speech" \
+ apps/desktop/build/native/codync-speech
+ # Codync Screen (Remote screen capture and input), built unsigned; electron-builder signs it.
+ xcodebuild -downloadComponent MetalToolchain || true
+ xcodegen generate --spec apps/project.yml
+ xcodebuild build -project apps/Codync.xcodeproj -scheme Screen -configuration Release \
+ -destination 'generic/platform=macOS' -derivedDataPath build/derived -quiet \
+ CODE_SIGN_IDENTITY="-" CODE_SIGNING_REQUIRED=NO CODE_SIGNING_ALLOWED=NO
+ cp -R "$(find build/derived -name CodyncScreen.app -path '*/Release/*' | head -1)" apps/desktop/build/native/
+ swift packaging/dmg/background.swift apps/desktop/build/dmg-background.tiff
+
+ - name: Build, sign and notarize
+ working-directory: apps/desktop
+ env:
+ CSC_IDENTITY_AUTO_DISCOVERY: "true"
+ APPLE_ID: ${{ secrets.APPLE_ID }}
+ APPLE_TEAM_ID: ${{ secrets.APPLE_DEVELOPMENT_TEAM }}
+ APPLE_APP_SPECIFIC_PASSWORD: ${{ secrets.APPLE_APP_PASSWORD }}
+ run: |
+ npm ci
+ node tools/account-config.mjs main
+ npm run icons
+ npm run build
+ npx electron-builder --mac --publish never
+ codesign -d --entitlements - dist/mac-universal/Codync.app | grep -q com.apple.security.device.audio-input ||
+ { echo "audio-input entitlement missing"; exit 1; }
+ spctl --assess --type execute -v dist/mac-universal/Codync.app
+
+ - name: Update feeds
+ env:
+ SPARKLE_PRIVATE_KEY: ${{ secrets.SPARKLE_PRIVATE_KEY }}
+ run: |
+ VERSION=$(node -p 'require("./apps/desktop/package.json").version')
+ # The iPhone app this release needs: Macs with iPhones wait until the App Store has it.
+ MIN_APP=$(apps/desktop/build/native/codync-host compat | python3 -c 'import json,sys; print(json.load(sys.stdin)["minApp"])')
+ printf 'minApp: %s\n' "$MIN_APP" >> apps/desktop/dist/latest-mac.yml
+ # Installs of the earlier native app update through Sparkle: one more appcast, now for the zip.
+ test -n "$SPARKLE_PRIVATE_KEY" || { echo 'SPARKLE_PRIVATE_KEY is required'; exit 1; }
+ curl -sSL https://github.com/sparkle-project/Sparkle/releases/download/2.10.0/Sparkle-2.10.0.tar.xz | tar xJ -C "$RUNNER_TEMP"
+ mkdir -p build/updates && cp apps/desktop/dist/codync-macos.zip build/updates/
+ printf '%s' "$SPARKLE_PRIVATE_KEY" | "$RUNNER_TEMP/bin/generate_appcast" --ed-key-file - --maximum-deltas 0 \
+ --download-url-prefix "https://github.com/leepokai/Codync/releases/download/v${VERSION}/" \
+ --link https://www.codync.dev/ -o build/appcast.xml build/updates
+ python3 packaging/updates/annotate-appcast.py build/appcast.xml "$MIN_APP"
+
+ - uses: actions/upload-artifact@v4
+ if: "!startsWith(github.ref, 'refs/tags/')"
+ with:
+ name: codync-macos
+ path: |
+ apps/desktop/dist/codync-macos.dmg
+ apps/desktop/dist/latest-mac.yml
+ build/appcast.xml
+
+ - name: Publish
+ if: startsWith(github.ref, 'refs/tags/')
+ env:
+ GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+ run: |
+ cd apps/desktop/dist
+ shasum -a 256 codync-macos.dmg > codync-macos.dmg.sha256
+ FILES="codync-macos.dmg codync-macos.dmg.sha256 codync-macos.zip codync-macos.zip.blockmap latest-mac.yml ../../../build/appcast.xml"
+ # The host workflow may have created this release already (same tag).
+ gh release create "$GITHUB_REF_NAME" $FILES --title "Codync $GITHUB_REF_NAME" --generate-notes ||
+ gh release upload "$GITHUB_REF_NAME" $FILES --clobber
+
+ - name: Update Homebrew tap
+ if: startsWith(github.ref, 'refs/tags/')
+ run: |
+ VERSION=${GITHUB_REF_NAME#v}
+ SHA=$(shasum -a 256 apps/desktop/dist/codync-macos.dmg | awk '{print $1}')
+ git clone https://x-access-token:${{ secrets.TAP_TOKEN }}@github.com/leepokai/homebrew-codync.git tap
+ mkdir -p tap/Casks
+ sed -e "s/^ version .*/ version \"$VERSION\"/" -e "s/REPLACE_WITH_SHA256/$SHA/" packaging/homebrew/codync.rb > tap/Casks/codync.rb
+ cp packaging/homebrew/README.md tap/
+ cd tap
+ git config user.name "github-actions[bot]"
+ git config user.email "github-actions[bot]@users.noreply.github.com"
+ git add -A
+ git commit -m "Update Codync to $VERSION" || exit 0
+ git push
+
+ linux:
+ needs: check
+ strategy:
+ matrix:
+ include:
+ - { os: ubuntu-24.04, arch: x64, name: linux-x86_64, target: x86_64-unknown-linux-musl }
+ - { os: ubuntu-24.04-arm, arch: arm64, name: linux-arm64, target: aarch64-unknown-linux-musl }
+ runs-on: ${{ matrix.os }}
+ steps:
+ - uses: actions/checkout@v4
+ - uses: actions/setup-node@v4
+ with:
+ node-version: 24
+ cache: npm
+ cache-dependency-path: apps/desktop/package-lock.json
+ - name: Install Rust (rust-toolchain.toml)
+ run: rustup toolchain install
+ - run: rustup target add ${{ matrix.target }} && sudo apt-get update && sudo apt-get install -y musl-tools rpm
+ - name: Host
+ env:
+ CFLAGS_aarch64_unknown_linux_musl: -mno-outline-atomics
+ run: |
+ cargo build --release --manifest-path host/Cargo.toml --target ${{ matrix.target }}
+ mkdir -p apps/desktop/build/native
+ cp host/target/${{ matrix.target }}/release/codync-host apps/desktop/build/native/
+ - name: Build
+ working-directory: apps/desktop
+ run: |
+ npm ci
+ node tools/account-config.mjs main
+ npm run build
+ npx electron-builder --linux --${{ matrix.arch }} --publish never
+ # The website links releases/latest/download/codync-<os>-<arch>.tar.gz.
+ cd dist
+ for f in codync-linux-${{ matrix.arch }}.*; do mv "$f" "${f/linux-${{ matrix.arch }}/${{ matrix.name }}}"; done
+ for f in codync-${{ matrix.name }}.*; do shasum -a 256 "$f" > "$f.sha256"; done
+ - uses: actions/upload-artifact@v4
+ if: "!startsWith(github.ref, 'refs/tags/')"
+ with:
+ name: codync-${{ matrix.name }}
+ path: apps/desktop/dist/codync-${{ matrix.name }}.*
+ - uses: softprops/action-gh-release@v2
+ if: startsWith(github.ref, 'refs/tags/')
+ with:
+ files: |
+ apps/desktop/dist/codync-${{ matrix.name }}.*
+ apps/desktop/dist/latest-linux*.yml
diff --git a/.github/workflows/release-macos.yml b/.github/workflows/release-macos.yml
deleted file mode 100644
index 1fb917a2..00000000
--- a/.github/workflows/release-macos.yml
+++ /dev/null
@@ -1,183 +0,0 @@
-name: Release macOS
-
-on:
- push:
- tags: ["v*"]
- # A dry run from any branch: builds, signs and notarizes, but publishes nothing.
- workflow_dispatch:
-
-permissions:
- contents: write
-
-jobs:
- build:
- runs-on: macos-26
- steps:
- - uses: actions/checkout@v4
-
- - name: Set version
- run: |
- if [[ "$GITHUB_REF" == refs/tags/v* ]]; then VERSION=${GITHUB_REF_NAME#v}
- else VERSION=$(awk '/MARKETING_VERSION:/ {print $2; exit}' apps/project.yml); fi
- echo "VERSION=$VERSION" >> $GITHUB_ENV
-
- - name: Install Rust (rust-toolchain.toml)
- run: rustup toolchain install
- - run: rustup target add aarch64-apple-darwin x86_64-apple-darwin
-
- - name: Install Apple certificate and provisioning profile
- env:
- APPLE_CERTIFICATE_P12: ${{ secrets.APPLE_CERTIFICATE_P12 }}
- APPLE_CERTIFICATE_PASSWORD: ${{ secrets.APPLE_CERTIFICATE_PASSWORD }}
- run: |
- CERT_FILE=$RUNNER_TEMP/certificate.p12
- KEYCHAIN_FILE=$RUNNER_TEMP/app-signing.keychain-db
- KEYCHAIN_PASSWORD=$(openssl rand -base64 32)
-
- echo "$APPLE_CERTIFICATE_P12" | base64 --decode > "$CERT_FILE"
-
- security create-keychain -p "$KEYCHAIN_PASSWORD" "$KEYCHAIN_FILE"
- security set-keychain-settings -lut 21600 "$KEYCHAIN_FILE"
- security unlock-keychain -p "$KEYCHAIN_PASSWORD" "$KEYCHAIN_FILE"
-
- security import "$CERT_FILE" \
- -P "$APPLE_CERTIFICATE_PASSWORD" \
- -A -t cert -f pkcs12 \
- -k "$KEYCHAIN_FILE"
-
- security set-key-partition-list \
- -S apple-tool:,apple:,codesign: \
- -s -k "$KEYCHAIN_PASSWORD" "$KEYCHAIN_FILE"
-
- security list-keychain -d user -s "$KEYCHAIN_FILE"
-
- - name: Metal toolchain
- # SwiftTerm (setup terminals) ships a Metal shader.
- run: xcodebuild -downloadComponent MetalToolchain
-
- - name: Build
- run: |
- xcodebuild build \
- -project apps/Codync.xcodeproj \
- -scheme macOS \
- -configuration Release \
- -destination 'generic/platform=macOS' \
- -derivedDataPath build/derived \
- -quiet \
- CODE_SIGN_IDENTITY="-" \
- CODE_SIGNING_REQUIRED=NO \
- CODE_SIGNING_ALLOWED=NO \
- MARKETING_VERSION=${{ env.VERSION }} \
- CURRENT_PROJECT_VERSION=${{ env.VERSION }}
-
- - name: Sign app with Developer ID
- run: |
- APP_PATH=$(find build/derived -name "Codync.app" -path "*/Release/*" | head -1)
- # Sign-in and the cloud relay read their settings from here.
- test -f "$APP_PATH/Contents/Resources/AccountConfig.plist" || { echo "AccountConfig.plist missing"; exit 1; }
- sign() { codesign --force --options runtime --timestamp --preserve-metadata=entitlements --sign "Developer ID Application" "$1"; }
- # Sparkle's XPC services and updater must share our signing identity.
- # Preserve the framework helpers' sandbox entitlements while resigning.
- find "$APP_PATH" -type f -name Autoupdate -print0 | while IFS= read -r -d '' f; do sign "$f"; done
- sign "$APP_PATH/Contents/MacOS/codync-host"
- find "$APP_PATH" -mindepth 1 -depth \( -name '*.xpc' -o -name '*.app' -o -name '*.framework' \) -print0 |
- while IFS= read -r -d '' f; do sign "$f"; done
- # The build ran unsigned, so there are no entitlements to preserve: pass the app's own.
- codesign --force --options runtime --timestamp --entitlements apps/macos/Resources/macOS.entitlements \
- --sign "Developer ID Application" "$APP_PATH"
- codesign -d --entitlements - "$APP_PATH" | grep -q com.apple.security.device.audio-input ||
- { echo "audio-input entitlement missing"; exit 1; }
- codesign --verify --deep --strict --verbose=2 "$APP_PATH"
- cp -R "$APP_PATH" "$RUNNER_TEMP/Codync.app"
-
- - name: Notarize and staple
- env:
- APPLE_ID: ${{ secrets.APPLE_ID }}
- APPLE_TEAM_ID: ${{ secrets.APPLE_DEVELOPMENT_TEAM }}
- APPLE_APP_PASSWORD: ${{ secrets.APPLE_APP_PASSWORD }}
- run: |
- ditto -c -k --keepParent \
- "$RUNNER_TEMP/Codync.app" \
- "$RUNNER_TEMP/codync-submit.zip"
-
- xcrun notarytool submit "$RUNNER_TEMP/codync-submit.zip" \
- --apple-id "$APPLE_ID" \
- --team-id "$APPLE_TEAM_ID" \
- --password "$APPLE_APP_PASSWORD" \
- --wait
-
- xcrun stapler staple "$RUNNER_TEMP/Codync.app"
-
- - name: Create DMG
- run: |
- DMG_DIR=$RUNNER_TEMP/dmg_staging
- mkdir -p "$DMG_DIR"
- cp -R "$RUNNER_TEMP/Codync.app" "$DMG_DIR/"
- brew install create-dmg
- swift packaging/dmg/background.swift "$RUNNER_TEMP/dmg-background.tiff"
- create-dmg --volname Codync --background "$RUNNER_TEMP/dmg-background.tiff" \
- --window-size 660 400 --icon-size 128 --text-size 13 \
- --icon Codync.app 170 200 --hide-extension Codync.app \
- --app-drop-link 490 200 --no-internet-enable \
- build/codync-macos.dmg "$DMG_DIR"
- spctl --assess --type execute -v "$RUNNER_TEMP/Codync.app"
-
- - name: Generate signed Sparkle appcast
- env:
- SPARKLE_PRIVATE_KEY: ${{ secrets.SPARKLE_PRIVATE_KEY }}
- run: |
- test -n "$SPARKLE_PRIVATE_KEY" || { echo 'SPARKLE_PRIVATE_KEY is required'; exit 1; }
- SPARKLE_BIN=build/derived/SourcePackages/artifacts/sparkle/Sparkle/bin
- test -x "$SPARKLE_BIN/generate_appcast"
- mkdir -p build/updates
- cp build/codync-macos.dmg build/updates/
- printf '%s' "$SPARKLE_PRIVATE_KEY" | "$SPARKLE_BIN/generate_appcast" \
- --ed-key-file - --maximum-deltas 0 \
- --download-url-prefix "https://github.com/leepokai/Codync/releases/download/v${VERSION}/" \
- --link https://www.codync.dev/ \
- -o build/appcast.xml build/updates
- # The iPhone app this release needs: Macs with iPhones wait until the App Store has it.
- MIN_APP=$("$RUNNER_TEMP/Codync.app/Contents/MacOS/codync-host" compat | python3 -c 'import json,sys; print(json.load(sys.stdin)["minApp"])')
- python3 packaging/updates/annotate-appcast.py build/appcast.xml "$MIN_APP"
- python3 -m venv "$RUNNER_TEMP/update-verification"
- "$RUNNER_TEMP/update-verification/bin/pip" install 'cryptography>=44,<47'
- "$RUNNER_TEMP/update-verification/bin/python" packaging/updates/verify-appcast.py \
- build/appcast.xml "$RUNNER_TEMP/Codync.app" build/codync-macos.dmg
-
- - name: Keep the dry run's DMG
- if: "!startsWith(github.ref, 'refs/tags/')"
- uses: actions/upload-artifact@v4
- with:
- name: codync-macos
- path: |
- build/codync-macos.dmg
- build/appcast.xml
-
- - name: Create GitHub Release
- if: startsWith(github.ref, 'refs/tags/')
- run: |
- # The host workflow may have created this release already (same tag).
- (cd build && shasum -a 256 codync-macos.dmg > codync-macos.dmg.sha256)
- gh release create ${{ github.ref_name }} \
- build/codync-macos.dmg build/codync-macos.dmg.sha256 build/appcast.xml \
- --title "Codync ${{ github.ref_name }}" \
- --generate-notes \
- || gh release upload ${{ github.ref_name }} build/codync-macos.dmg build/codync-macos.dmg.sha256 build/appcast.xml --clobber
- env:
- GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
-
- - name: Update Homebrew tap
- if: startsWith(github.ref, 'refs/tags/')
- run: |
- SHA=$(shasum -a 256 build/codync-macos.dmg | awk '{print $1}')
- git clone https://x-access-token:${{ secrets.TAP_TOKEN }}@github.com/leepokai/homebrew-codync.git tap
- mkdir -p tap/Casks
- sed -e "s/^ version .*/ version \"${{ env.VERSION }}\"/" -e "s/REPLACE_WITH_SHA256/$SHA/" \
- packaging/homebrew/codync.rb > tap/Casks/codync.rb
- cp packaging/homebrew/README.md tap/
- cd tap
- git config user.name "github-actions[bot]"
- git config user.email "github-actions[bot]@users.noreply.github.com"
- git add -A
- git commit -m "Update Codync to ${{ env.VERSION }}" || exit 0
- git push
diff --git a/apps/desktop/.gitignore b/apps/desktop/.gitignore
index 2e4f8957..2783f80f 100644
--- a/apps/desktop/.gitignore
+++ b/apps/desktop/.gitignore
@@ -3,3 +3,5 @@ out/
dist/
# SF Symbols masks: generated on macOS at build time (npm run icons), never committed.
src/renderer/public/symbols/
+native/speech-macos/.build/
+resources/account-config.json
diff --git a/apps/desktop/electron-builder.yml b/apps/desktop/electron-builder.yml
new file mode 100644
index 00000000..75bede17
--- /dev/null
+++ b/apps/desktop/electron-builder.yml
@@ -0,0 +1,76 @@
+# Packaging for macOS (DMG + zip for updates) and Linux (AppImage, deb, tar.gz).
+# CI puts the native pieces in place first (see .github/workflows/release-desktop.yml):
+# build/native/codync-host the host for this platform (macOS: universal)
+# build/native/codync-speech macOS on-device speech helper
+# build/native/CodyncScreen.app macOS screen helper (Xcode `Screen` target)
+appId: com.pokai.Codync
+productName: Codync
+copyright: ""
+directories:
+ output: dist
+ buildResources: resources
+files:
+ - out/**
+ - package.json
+extraResources:
+ - from: build/native/codync-host
+ to: codync-host
+ - from: resources/account-config.json
+ to: account-config.json
+ - from: resources/tray
+ to: tray
+protocols:
+ - name: Codync
+ schemes: [codync, com.pokai.Codync]
+publish:
+ provider: github
+ owner: leepokai
+ repo: Codync
+ releaseType: release
+
+mac:
+ category: public.app-category.developer-tools
+ icon: resources/icon.icns
+ target:
+ - target: dmg
+ arch: universal
+ - target: zip
+ arch: universal
+ hardenedRuntime: true
+ entitlements: resources/entitlements.mac.plist
+ entitlementsInherit: resources/entitlements.mac.plist
+ extendInfo:
+ NSMicrophoneUsageDescription: Talk to your bots in a voice call.
+ NSSpeechRecognitionUsageDescription: Your words are transcribed on this Mac and sent to your bot as a message.
+ extraResources:
+ - from: build/native/codync-speech
+ to: codync-speech
+ extraFiles:
+ # Registered with SMAppService.agent when Remote screen is turned on.
+ - from: build/native/CodyncScreen.app
+ to: Library/LoginItems/CodyncScreen.app
+ - from: resources/mac/com.pokai.Codync.screen.plist
+ to: Library/LaunchAgents/com.pokai.Codync.screen.plist
+ notarize: true
+dmg:
+ artifactName: codync-macos.dmg
+ background: build/dmg-background.tiff
+ window: { width: 660, height: 400 }
+ iconSize: 128
+ iconTextSize: 13
+ contents:
+ - { x: 170, y: 200, type: file }
+ - { x: 490, y: 200, type: link, path: /Applications }
+zip:
+ artifactName: codync-macos.zip
+
+linux:
+ category: Development
+ icon: resources/icon.png
+ executableName: codync
+ synopsis: Your coding agents as teammates
+ target: [AppImage, deb, tar.gz]
+ artifactName: codync-linux-${arch}.${ext}
+ desktop:
+ entry:
+ StartupWMClass: Codync
diff --git a/apps/desktop/native/speech-macos/Package.swift b/apps/desktop/native/speech-macos/Package.swift
new file mode 100644
index 00000000..70154d13
--- /dev/null
+++ b/apps/desktop/native/speech-macos/Package.swift
@@ -0,0 +1,9 @@
+// swift-tools-version: 6.0
+import PackageDescription
+
+// On-device speech recognition for the desktop app's voice calls (macOS only).
+let package = Package(
+ name: "codync-speech",
+ platforms: [.macOS(.v14)],
+ targets: [.executableTarget(name: "codync-speech", path: "Sources")]
+)
diff --git a/apps/desktop/native/speech-macos/Sources/main.swift b/apps/desktop/native/speech-macos/Sources/main.swift
new file mode 100644
index 00000000..5b610d51
--- /dev/null
+++ b/apps/desktop/native/speech-macos/Sources/main.swift
@@ -0,0 +1,123 @@
+import AVFoundation
+import Foundation
+import Speech
+
+// codync-speech: transcribes the microphone on device with SFSpeechRecognizer and prints one
+// JSON event per line: {"type":"started"}, {"type":"partial"|"final","text":…},
+// {"type":"level","value":0…1}, {"type":"error","message":…}. It runs until stdin closes or
+// SIGTERM. The desktop app (src/main/speech.ts) owns endpointing: it stops this process when
+// the speaker pauses.
+
+@MainActor
+final class Recognizer {
+ private let engine = AVAudioEngine()
+ private let recognizer: SFSpeechRecognizer?
+ private var request: SFSpeechAudioBufferRecognitionRequest?
+ private var task: SFSpeechRecognitionTask?
+ private var lastLevel = Date.distantPast
+
+ init(locale: String?) {
+ recognizer = locale.map { SFSpeechRecognizer(locale: Locale(identifier: $0)) } ?? SFSpeechRecognizer()
+ }
+
+ func start() async {
+ guard await AVCaptureDevice.requestAccess(for: .audio) else {
+ return fail("Allow the microphone for Codync in System Settings to talk to your bots.")
+ }
+ let status = await withCheckedContinuation { done in SFSpeechRecognizer.requestAuthorization { done.resume(returning: $0) } }
+ guard status == .authorized else {
+ return fail("Allow Speech Recognition for Codync in System Settings to talk to your bots.")
+ }
+ guard let recognizer, recognizer.isAvailable else {
+ return fail("Speech recognition isn't available for this language right now.")
+ }
+ let request = SFSpeechAudioBufferRecognitionRequest()
+ request.shouldReportPartialResults = true
+ if recognizer.supportsOnDeviceRecognition { request.requiresOnDeviceRecognition = true }
+ self.request = request
+ let input = engine.inputNode
+ let format = input.outputFormat(forBus: 0)
+ input.installTap(onBus: 0, bufferSize: 1024, format: format) { buffer, _ in
+ request.append(buffer)
+ let level = Self.level(buffer)
+ Task { @MainActor in self.report(level) }
+ }
+ do {
+ engine.prepare()
+ try engine.start()
+ } catch {
+ return fail("Couldn't start the microphone: \(error.localizedDescription)")
+ }
+ task = recognizer.recognitionTask(with: request) { result, error in
+ let text = result?.bestTranscription.formattedString
+ let final = result?.isFinal ?? false
+ let message = error?.localizedDescription
+ Task { @MainActor in
+ if let text { emit(["type": final ? "final" : "partial", "text": text]) }
+ // An error after the stream was ended on purpose is just the end.
+ if let message, !final, self.request != nil { emit(["type": "error", "message": message]) }
+ }
+ }
+ emit(["type": "started"])
+ }
+
+ func stop() {
+ engine.inputNode.removeTap(onBus: 0)
+ engine.stop()
+ request?.endAudio()
+ request = nil
+ }
+
+ private func report(_ level: Float) {
+ // ~20 updates a second is plenty for a level meter.
+ guard Date().timeIntervalSince(lastLevel) > 0.05 else { return }
+ lastLevel = Date()
+ emit(["type": "level", "value": Double(level)])
+ }
+
+ private func fail(_ message: String) {
+ emit(["type": "error", "message": message])
+ exit(1)
+ }
+
+ nonisolated private static func level(_ buffer: AVAudioPCMBuffer) -> Float {
+ guard let data = buffer.floatChannelData?[0] else { return 0 }
+ let count = Int(buffer.frameLength)
+ guard count > 0 else { return 0 }
+ var sum: Float = 0
+ for i in 0..<count { sum += data[i] * data[i] }
+ let rms = (sum / Float(count)).squareRoot()
+ // -50 dB … 0 dB → 0 … 1
+ let db = 20 * log10(max(rms, 1e-6))
+ return max(0, min(1, (db + 50) / 50))
+ }
+}
+
+@MainActor func emit(_ event: [String: Any]) {
+ guard let data = try? JSONSerialization.data(withJSONObject: event) else { return }
+ FileHandle.standardOutput.write(data + Data("\n".utf8))
+}
+
+let arguments = CommandLine.arguments
+let locale = arguments.firstIndex(of: "--locale").flatMap { arguments.indices.contains($0 + 1) ? arguments[$0 + 1] : nil }
+
+signal(SIGTERM, SIG_IGN)
+let term = DispatchSource.makeSignalSource(signal: SIGTERM, queue: .main)
+let recognizer = MainActor.assumeIsolated { Recognizer(locale: locale) }
+term.setEventHandler {
+ MainActor.assumeIsolated { recognizer.stop() }
+ // Give the recognizer a moment to deliver the final result.
+ DispatchQueue.main.asyncAfter(deadline: .now() + 0.6) { exit(0) }
+}
+term.resume()
+
+// stdin closing (the app quit) ends the process too.
+FileHandle.standardInput.readabilityHandler = { handle in
+ if handle.availableData.isEmpty {
+ handle.readabilityHandler = nil
+ DispatchQueue.main.async { exit(0) }
+ }
+}
+
+Task { @MainActor in await recognizer.start() }
+dispatchMain()
diff --git a/apps/desktop/package-lock.json b/apps/desktop/package-lock.json
index 3d5e021f..0fd2dab8 100644
--- a/apps/desktop/package-lock.json
+++ b/apps/desktop/package-lock.json
@@ -8,12 +8,18 @@
"name": "codync-desktop",
"version": "2.6.3",
"license": "UNLICENSED",
+ "dependencies": {
+ "electron-updater": "^6.8.9"
+ },
"devDependencies": {
"@types/node": "^24.0.0",
"@types/qrcode": "^1.5.6",
"@types/react": "^19.3.0",
"@types/react-dom": "^19.3.0",
"@vitejs/plugin-react": "^5.2.0",
+ "@xterm/addon-fit": "^0.11.0",
+ "@xterm/addon-web-links": "^0.12.0",
+ "@xterm/xterm": "^6.0.0",
"electron": "^44.5.1",
"electron-builder": "^26.15.3",
"electron-vite": "^5.0.0",
@@ -1858,6 +1864,30 @@
"node": ">=10.0.0"
}
},
+ "node_modules/@xterm/addon-fit": {
+ "version": "0.11.0",
+ "resolved": "https://registry.npmjs.org/@xterm/addon-fit/-/addon-fit-0.11.0.tgz",
+ "integrity": "sha512-jYcgT6xtVYhnhgxh3QgYDnnNMYTcf8ElbxxFzX0IZo+vabQqSPAjC3c1wJrKB5E19VwQei89QCiZZP86DCPF7g==",
+ "dev": true,
+ "license": "MIT"
+ },
+ "node_modules/@xterm/addon-web-links": {
+ "version": "0.12.0",
+ "resolved": "https://registry.npmjs.org/@xterm/addon-web-links/-/addon-web-links-0.12.0.tgz",
+ "integrity": "sha512-4Smom3RPyVp7ZMYOYDoC/9eGJJJqYhnPLGGqJ6wOBfB8VxPViJNSKdgRYb8NpaM6YSelEKbA2SStD7lGyqaobw==",
+ "dev": true,
+ "license": "MIT"
+ },
+ "node_modules/@xterm/xterm": {
+ "version": "6.0.0",
+ "resolved": "https://registry.npmjs.org/@xterm/xterm/-/xterm-6.0.0.tgz",
+ "integrity": "sha512-TQwDdQGtwwDt+2cgKDLn0IRaSxYu1tSUjgKarSDkUM0ZNiSRXFpjxEsvc/Zgc5kq5omJ+V0a8/kIM2WD3sMOYg==",
+ "dev": true,
+ "license": "MIT",
+ "workspaces": [
+ "addons/*"
+ ]
+ },
"node_modules/abbrev": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/abbrev/-/abbrev-4.0.0.tgz",
@@ -2088,7 +2118,6 @@
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz",
"integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==",
- "dev": true,
"license": "Python-2.0"
},
"node_modules/asn1js": {
@@ -2291,7 +2320,6 @@
"version": "9.7.0",
"resolved": "https://registry.npmjs.org/builder-util-runtime/-/builder-util-runtime-9.7.0.tgz",
"integrity": "sha512-g/kR520giAFYkSXTzcmF3kqQq7wi8F6N6SzeDgZrqTBN+VHdmgWOyTdD1yD7AATDId/yXLvuP34CxW46/BwCdw==",
- "dev": true,
"license": "MIT",
"dependencies": {
"debug": "^4.3.4",
@@ -2605,7 +2633,6 @@
"version": "4.4.3",
"resolved": "https://registry.npmjs.org/debug/-/debug-4.4.3.tgz",
"integrity": "sha512-RGwwWnwQvkVfavKVt22FGLw+xYSdzARwm0ru6DhTVA3umU5hZc28V3kO4stgYryrTlLpuvgI9GiijltAjNbcqA==",
- "dev": true,
"license": "MIT",
"dependencies": {
"ms": "^2.1.3"
@@ -2939,6 +2966,34 @@
"dev": true,
"license": "ISC"
},
+ "node_modules/electron-updater": {
+ "version": "6.8.9",
+ "resolved": "https://registry.npmjs.org/electron-updater/-/electron-updater-6.8.9.tgz",
+ "integrity": "sha512-ZhVxM9iGONUpZGI1FxdMRgJjUFXi7AYGVa5PwKlO1tV1/4zDxQmfKpXOHVztKrd6L9rLcFjERvi1Mf2vxyTkig==",
+ "license": "MIT",
+ "dependencies": {
+ "builder-util-runtime": "9.7.0",
+ "fs-extra": "^10.1.0",
+ "js-yaml": "^4.1.0",
+ "lazy-val": "^1.0.5",
+ "lodash.escaperegexp": "^4.1.2",
+ "lodash.isequal": "^4.5.0",
+ "semver": "~7.7.3",
+ "tiny-typed-emitter": "^2.1.0"
+ }
+ },
+ "node_modules/electron-updater/node_modules/semver": {
+ "version": "7.7.4",
+ "resolved": "https://registry.npmjs.org/semver/-/semver-7.7.4.tgz",
+ "integrity": "sha512-vFKC2IEtQnVhpT78h1Yp8wzwrf8CM+MzKMHGJZfBtzhZNycRFnXsHk6E5TxIkkMsgNS7mdX3AGB7x2QM2di4lA==",
+ "license": "ISC",
+ "bin": {
+ "semver": "bin/semver.js"
+ },
+ "engines": {
+ "node": ">=10"
+ }
+ },
"node_modules/electron-vite": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/electron-vite/-/electron-vite-5.0.0.tgz",
@@ -3313,7 +3368,6 @@
"version": "10.1.0",
"resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-10.1.0.tgz",
"integrity": "sha512-oRXApq54ETRj4eMiFzGnHWGy+zo5raudjuxN0b8H7s/RU2oW0Wvsx9O0ACRN/kRq9E8Vu/ReskGB5o3ji+FzHQ==",
- "dev": true,
"license": "MIT",
"dependencies": {
"graceful-fs": "^4.2.0",
@@ -3578,7 +3632,6 @@
"version": "4.2.11",
"resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.11.tgz",
"integrity": "sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ==",
- "dev": true,
"license": "ISC"
},
"node_modules/has-flag": {
@@ -3827,7 +3880,6 @@
"version": "4.3.2",
"resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.3.2.tgz",
"integrity": "sha512-SFNOvSJ+Dgf/9An904Yx+CgSlIPCkIpao4qo51lpee25TIRejdH3rhR4EZMGoNx3/TP3O+wzWuiTFl4sqbltzA==",
- "dev": true,
"funding": [
{
"type": "github",
@@ -3898,7 +3950,6 @@
"version": "6.2.1",
"resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-6.2.1.tgz",
"integrity": "sha512-zwOTdL3rFQ/lRdBnntKVOX6k5cKJwEc1HdilT71BWEu7J41gXIB2MRp+vxduPSwZJPWBxEzv4yH1wYLJGUHX4Q==",
- "dev": true,
"license": "MIT",
"dependencies": {
"universalify": "^2.0.0"
@@ -3921,7 +3972,6 @@
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/lazy-val/-/lazy-val-1.0.5.tgz",
"integrity": "sha512-0/BnGCCfyUMkBpeDgWihanIAF9JmZhHBgUhEqzvf+adhNGLoP6TaiI5oF8oyb3I45P+PcnrqihSf01M0l0G5+Q==",
- "dev": true,
"license": "MIT"
},
"node_modules/locate-path": {
@@ -3944,6 +3994,19 @@
"dev": true,
"license": "MIT"
},
+ "node_modules/lodash.escaperegexp": {
+ "version": "4.1.2",
+ "resolved": "https://registry.npmjs.org/lodash.escaperegexp/-/lodash.escaperegexp-4.1.2.tgz",
+ "integrity": "sha512-TM9YBvyC84ZxE3rgfefxUWiQKLilstD6k7PTGt6wfbtXF8ixIJLOL3VYyV/z+ZiPLsVxAsKAFVwWlWeb2Y8Yyw==",
+ "license": "MIT"
+ },
+ "node_modules/lodash.isequal": {
+ "version": "4.5.0",
+ "resolved": "https://registry.npmjs.org/lodash.isequal/-/lodash.isequal-4.5.0.tgz",
+ "integrity": "sha512-pDo3lu8Jhfjqls6GkMgpahsF9kCyayhgykjyLMNFTKWrpVdAQtYyB4muAMWozBB4ig/dtWAmsMxLEI8wuz+DYQ==",
+ "deprecated": "This package is deprecated. Use require('node:util').isDeepStrictEqual instead.",
+ "license": "MIT"
+ },
"node_modules/lowercase-keys": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/lowercase-keys/-/lowercase-keys-2.0.0.tgz",
@@ -4118,7 +4181,6 @@
"version": "2.1.3",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz",
"integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==",
- "dev": true,
"license": "MIT"
},
"node_modules/nanoid": {
@@ -5012,7 +5074,6 @@
"version": "1.6.1",
"resolved": "https://registry.npmjs.org/sax/-/sax-1.6.1.tgz",
"integrity": "sha512-42tBVwLWnaQvW5zc4HbZrTuWccECCZfBi92FDuwtqxasH+JbPB3/FOKb1m222K42R4WxuxzzMsTswfzgtSu64Q==",
- "dev": true,
"license": "BlueOak-1.0.0",
"engines": {
"node": ">=11.0.0"
@@ -5309,6 +5370,12 @@
"semver": "bin/semver"
}
},
+ "node_modules/tiny-typed-emitter": {
+ "version": "2.1.0",
+ "resolved": "https://registry.npmjs.org/tiny-typed-emitter/-/tiny-typed-emitter-2.1.0.tgz",
+ "integrity": "sha512-qVtvMxeXbVej0cQWKqVSSAHmKZEHAvxdF8HEUBFWts8h+xEo5m/lEiPakuyZ3BnCBjOD8i24kzNOiOLLgsSxhA==",
+ "license": "MIT"
+ },
"node_modules/tinyglobby": {
"version": "0.2.17",
"resolved": "https://registry.npmjs.org/tinyglobby/-/tinyglobby-0.2.17.tgz",
@@ -5413,7 +5480,6 @@
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/universalify/-/universalify-2.0.1.tgz",
"integrity": "sha512-gptHNQghINnc/vTGIk0SOFGFNXw7JVrlRUtConJRlvaw6DuX0wO5Jeko9sWrMBhh+PsYAZ7oXAiOnf/UKogyiw==",
- "dev": true,
"license": "MIT",
"engines": {
"node": ">= 10.0.0"
diff --git a/apps/desktop/package.json b/apps/desktop/package.json
index 685c93c1..c6f29a33 100644
--- a/apps/desktop/package.json
+++ b/apps/desktop/package.json
@@ -16,13 +16,15 @@
"dist:mac": "electron-vite build && electron-builder --mac",
"dist:linux": "electron-vite build && electron-builder --linux"
},
- "dependencies": {},
"devDependencies": {
"@types/node": "^24.0.0",
"@types/qrcode": "^1.5.6",
"@types/react": "^19.3.0",
"@types/react-dom": "^19.3.0",
"@vitejs/plugin-react": "^5.2.0",
+ "@xterm/addon-fit": "^0.11.0",
+ "@xterm/addon-web-links": "^0.12.0",
+ "@xterm/xterm": "^6.0.0",
"electron": "^44.5.1",
"electron-builder": "^26.15.3",
"electron-vite": "^5.0.0",
@@ -32,5 +34,8 @@
"react-dom": "^19.3.0",
"typescript": "^6.0.3",
"vite": "^7.3.6"
+ },
+ "dependencies": {
+ "electron-updater": "^6.8.9"
}
}
diff --git a/apps/desktop/resources/entitlements.mac.plist b/apps/desktop/resources/entitlements.mac.plist
new file mode 100644
index 00000000..8efde983
--- /dev/null
+++ b/apps/desktop/resources/entitlements.mac.plist
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+ <!-- Electron's V8 needs JIT under the hardened runtime. -->
+ <key>com.apple.security.cs.allow-jit</key>
+ <true/>
+ <key>com.apple.security.cs.allow-unsigned-executable-memory</key>
+ <true/>
+ <!-- Voice calls: the hardened runtime blocks the microphone without audio-input. -->
+ <key>com.apple.security.device.audio-input</key>
+ <true/>
+</dict>
+</plist>
diff --git a/apps/macos/LaunchAgents/com.pokai.Codync.screen.plist b/apps/desktop/resources/mac/com.pokai.Codync.screen.plist
similarity index 100%
rename from apps/macos/LaunchAgents/com.pokai.Codync.screen.plist
rename to apps/desktop/resources/mac/com.pokai.Codync.screen.plist
diff --git a/apps/desktop/src/main/account.ts b/apps/desktop/src/main/account.ts
index 9f768ba7..f3ae2e7e 100644
--- a/apps/desktop/src/main/account.ts
+++ b/apps/desktop/src/main/account.ts
@@ -1,31 +1,267 @@
import { EventEmitter } from 'node:events'
-import { ipcMain, type BrowserWindow } from 'electron'
+import { existsSync, promises as fs, readFileSync } from 'node:fs'
+import { join } from 'node:path'
+import { app, ipcMain, safeStorage, type BrowserWindow } from 'electron'
import type { AccountState } from '../shared/ipc'
+import { authenticate } from './auth'
/**
- * The Codync account: Clerk's sign-in (Apple, Google) through the system browser, with the
- * session kept in the OS keychain. An account session never grants access to a computer by
- * itself: each one still approves this device (spec §4.2).
+ * The Codync account through Clerk's Frontend API, the way ClerkKit drives it on the native
+ * apps: a device token in the `Authorization` header (`_is_native=true`), OAuth in the system
+ * browser returning to `com.pokai.Codync://callback`, the token kept encrypted with the OS
+ * keychain. An account never grants access to a computer by itself (spec §4.2).
*/
+
+const SCHEME = 'com.pokai.Codync'
+const REDIRECT = `${SCHEME}://callback`
+
+interface Config {
+ clerkPublishableKey: string | null
+ cloudURL: string | null
+}
+
+/** `CODYNC_CLERK_PUBLISHABLE_KEY` / `CODYNC_CLOUD_URL`, else the build's account config. */
+function loadConfig(): Config {
+ let file: Partial<Config> = {}
+ const candidates = [
+ app.isPackaged ? join(process.resourcesPath, 'account-config.json') : null,
+ join(__dirname, '../../resources/account-config.json'),
+ ]
+ for (const path of candidates) {
+ if (path && existsSync(path)) {
+ try {
+ file = JSON.parse(readFileSync(path, 'utf8')) as Partial<Config>
+ break
+ } catch {}
+ }
+ }
+ const key = (process.env.CODYNC_CLERK_PUBLISHABLE_KEY ?? file.clerkPublishableKey ?? '').trim()
+ const cloud = (process.env.CODYNC_CLOUD_URL ?? file.cloudURL ?? '').trim()
+ return {
+ clerkPublishableKey: key.startsWith('pk_test_') || key.startsWith('pk_live_') ? key : null,
+ cloudURL: /^https:\/\//.test(cloud) ? cloud.replace(/\/$/, '') : null,
+ }
+}
+
+/** The Frontend API host is the publishable key's payload: `pk_test_<base64("host$")>`. */
+function frontendAPI(key: string) {
+ const host = Buffer.from(key.replace(/^pk_(test|live)_/, ''), 'base64').toString('utf8').replace(/\$$/, '')
+ return `https://${host}`
+}
+
+interface ClerkUser {
+ id: string
+ image_url?: string
+ primary_email_address_id?: string | null
+ email_addresses?: { id: string; email_address: string }[]
+}
+
+interface ClerkSession {
+ id: string
+ status: string
+ expire_at?: number
+ user?: ClerkUser
+}
+
+interface ClerkClient {
+ sessions?: ClerkSession[]
+ last_active_session_id?: string | null
+}
+
+interface Verification {
+ status?: string
+ external_verification_redirect_url?: string | null
+ error?: { long_message?: string; message?: string } | null
+}
+
+interface SignInResource {
+ id: string
+ status: string
+ created_session_id?: string | null
+ first_factor_verification?: Verification | null
+ second_factor_verification?: Verification | null
+}
+
+class ClerkError extends Error {
+ constructor(
+ readonly status: number,
+ message: string,
+ ) {
+ super(message)
+ }
+}
+
export class AccountService extends EventEmitter {
- state: AccountState = { ready: true, configured: false, busy: false, error: null, user: null, cloudURL: null }
+ private config = loadConfig()
+ private base = this.config.clerkPublishableKey ? frontendAPI(this.config.clerkPublishableKey) : null
+ private deviceToken: string | null = null
+ private client: ClerkClient | null = null
+ private cachedToken: { sessionId: string; jwt: string; until: number } | null = null
+ state: AccountState = {
+ ready: !this.base,
+ configured: !!this.base && !!this.config.cloudURL,
+ busy: false,
+ error: null,
+ user: null,
+ cloudURL: this.config.cloudURL,
+ }
+
+ private get storePath() {
+ return join(app.getPath('userData'), 'account.bin')
+ }
register(windows: () => BrowserWindow[]) {
ipcMain.handle('account:state', () => this.state)
- ipcMain.handle('account:signIn', () => {
- this.set({ error: "Sign-in setup isn't finished yet. You can continue using local pairing." })
- })
- ipcMain.handle('account:signOut', () => {})
- ipcMain.handle('account:token', () => {
- throw new Error('Sign in again to reach your account.')
- })
+ ipcMain.handle('account:signIn', (_e, provider: 'apple' | 'google') => this.signIn(provider))
+ ipcMain.handle('account:signOut', () => this.signOut())
+ ipcMain.handle('account:token', () => this.sessionToken())
this.on('change', () => {
for (const w of windows()) w.webContents.send('account:change', this.state)
})
+ void this.restore()
}
private set(change: Partial<AccountState>) {
this.state = { ...this.state, ...change }
this.emit('change')
}
+
+ /** Clerk restores the persisted session first; fresh sign-in choices wait for it. */
+ private async restore() {
+ if (!this.base) return
+ try {
+ const sealed = await fs.readFile(this.storePath)
+ this.deviceToken = safeStorage.isEncryptionAvailable() ? safeStorage.decryptString(sealed) : sealed.toString('utf8')
+ } catch {}
+ try {
+ if (this.deviceToken) await this.refreshClient()
+ } catch {
+ // Offline at launch: the saved session still counts; the next call refreshes it.
+ }
+ this.set({ ready: true })
+ }
+
+ private async persist() {
+ try {
+ if (!this.deviceToken) return await fs.rm(this.storePath, { force: true })
+ const data = safeStorage.isEncryptionAvailable() ? safeStorage.encryptString(this.deviceToken) : Buffer.from(this.deviceToken)
+ await fs.mkdir(app.getPath('userData'), { recursive: true })
+ await fs.writeFile(this.storePath, data, { mode: 0o600 })
+ } catch {}
+ }
+
+ private async request<T>(method: 'GET' | 'POST' | 'DELETE', path: string, body?: Record<string, string | boolean>, query?: Record<string, string>): Promise<T> {
+ if (!this.base) throw new ClerkError(0, "Sign-in isn't configured in this build.")
+ const url = new URL(`${this.base}${path}`)
+ url.searchParams.set('_is_native', 'true')
+ for (const [k, v] of Object.entries(query ?? {})) url.searchParams.set(k, v)
+ const headers: Record<string, string> = { 'x-device-type': process.platform === 'darwin' ? 'macos' : process.platform, 'x-app-version': app.getVersion(), 'x-bundle-id': 'com.pokai.Codync' }
+ if (this.deviceToken) headers.Authorization = this.deviceToken
+ let payload: string | undefined
+ if (body) {
+ headers['Content-Type'] = 'application/x-www-form-urlencoded'
+ payload = new URLSearchParams(Object.entries(body).map(([k, v]) => [k, String(v)])).toString()
+ }
+ const res = await fetch(url, { method, headers, body: payload, signal: AbortSignal.timeout(20_000) })
+ // A new device token comes back in `Authorization`; "Bearer" alone clears it.
+ const auth = res.headers.get('authorization')?.trim()
+ if (auth !== undefined) {
+ this.deviceToken = !auth || auth.toLowerCase() === 'bearer' ? null : auth
+ void this.persist()
+ }
+ const json = (await res.json().catch(() => ({}))) as { response?: T; client?: ClerkClient | null; errors?: { long_message?: string; message?: string }[] } & T
+ if (!res.ok) throw new ClerkError(res.status, json.errors?.[0]?.long_message ?? json.errors?.[0]?.message ?? `Clerk error ${res.status}`)
+ if ('client' in json) this.applyClient(json.client ?? null)
+ return (json.response ?? json) as T
+ }
+
+ private applyClient(client: ClerkClient | null) {
+ this.client = client
+ const session = this.activeSession
+ const user = session?.user
+ const email = user?.email_addresses?.find((e) => e.id === user.primary_email_address_id)?.email_address ?? user?.email_addresses?.[0]?.email_address ?? null
+ const image = user?.image_url && user.image_url.startsWith('https://') ? user.image_url : null
+ this.set({ user: user ? { userId: user.id, email, avatarURL: image } : null })
+ }
+
+ private get activeSession(): ClerkSession | null {
+ const sessions = this.client?.sessions ?? []
+ const active = sessions.find((s) => s.id === this.client?.last_active_session_id && s.status === 'active')
+ return active ?? null
+ }
+
+ private async refreshClient() {
+ const client = await this.request<ClerkClient | null>('GET', '/v1/client')
+ this.applyClient(client)
+ }
+
+ async signIn(provider: 'apple' | 'google') {
+ if (this.state.busy) return
+ if (!this.base) return this.set({ error: "Sign-in setup isn't finished yet. You can continue using local pairing." })
+ const name = provider === 'apple' ? 'Apple' : 'Google'
+ this.set({ busy: true, error: null })
+ try {
+ const strategy = `oauth_${provider}`
+ const created = await this.request<SignInResource>('POST', '/v1/client/sign_ins', { strategy, redirect_url: REDIRECT })
+ const prepared = await this.request<SignInResource>('POST', `/v1/client/sign_ins/${created.id}/prepare_first_factor`, { strategy, redirect_url: REDIRECT })
+ const redirect = prepared.first_factor_verification?.external_verification_redirect_url
+ if (!redirect) throw new ClerkError(0, 'Redirect URL is missing. Unable to start the sign-in.')
+ const callback = new URL(await authenticate(redirect, SCHEME))
+ const nonce = callback.searchParams.get('rotating_token_nonce')
+ const signIn = await this.request<SignInResource>('GET', `/v1/client/sign_ins/${created.id}`, undefined, nonce ? { rotating_token_nonce: nonce } : undefined)
+ const transferable = signIn.first_factor_verification?.status === 'transferable' || signIn.second_factor_verification?.status === 'transferable'
+ if (!nonce && transferable) {
+ // A new account: Clerk's transfer flow turns the sign-in into a sign-up.
+ const signUp = await this.request<{ status: string }>('POST', '/v1/client/sign_ups', { transfer: true })
+ if (signUp.status !== 'complete') this.set({ error: `${name} sign-up needs additional account information. Your account is not signed in yet.` })
+ } else if (signIn.status !== 'complete') {
+ const error = signIn.first_factor_verification?.error
+ if (error) throw new ClerkError(0, error.long_message ?? error.message ?? 'Sign-in failed.')
+ this.set({ error: `${name} sign-in needs additional verification. Your account is not signed in yet.` })
+ }
+ await this.refreshClient()
+ } catch (error) {
+ const message = error instanceof Error ? error.message : ''
+ if (!/timed out|Another sign-in/.test(message)) this.set({ error: "Couldn't sign in. Check your connection and try again." })
+ } finally {
+ this.set({ busy: false })
+ }
+ }
+
+ async signOut() {
+ const session = this.activeSession
+ if (this.state.busy || !session) return
+ this.set({ busy: true, error: null })
+ try {
+ await this.request('POST', `/v1/client/sessions/${session.id}/remove`)
+ this.cachedToken = null
+ await this.refreshClient()
+ } catch {
+ this.set({ error: "Couldn't sign out. Check your connection and try again." })
+ } finally {
+ this.set({ busy: false })
+ }
+ }
+
+ /** Every account on this device, for starting over. */
+ async signOutAll() {
+ try {
+ await this.request('DELETE', '/v1/client/sessions')
+ } catch {}
+ this.deviceToken = null
+ this.cachedToken = null
+ await this.persist()
+ this.applyClient(null)
+ }
+
+ /** The signed-in session's JWT for the Codync cloud (cached for under a minute, like Clerk). */
+ async sessionToken(): Promise<string> {
+ const session = this.activeSession
+ if (!session) throw new Error('Sign in again to reach your account.')
+ if (this.cachedToken && this.cachedToken.sessionId === session.id && this.cachedToken.until > Date.now()) return this.cachedToken.jwt
+ const res = await this.request<{ jwt?: string } | null>('POST', `/v1/client/sessions/${session.id}/tokens`)
+ if (!res?.jwt) throw new Error('Sign in again to reach your account.')
+ this.cachedToken = { sessionId: session.id, jwt: res.jwt, until: Date.now() + 50_000 }
+ return res.jwt
+ }
}
diff --git a/apps/desktop/src/main/cloud.ts b/apps/desktop/src/main/cloud.ts
new file mode 100644
index 00000000..30a60402
--- /dev/null
+++ b/apps/desktop/src/main/cloud.ts
@@ -0,0 +1,98 @@
+import { createHash, createPrivateKey, createPublicKey, generateKeyPairSync, randomBytes, sign, type KeyObject } from 'node:crypto'
+import { promises as fs } from 'node:fs'
+import { join } from 'node:path'
+import { app, ipcMain, safeStorage } from 'electron'
+import type { AccountService } from './account'
+
+/**
+ * The Codync cloud's user-facing `/v1` API (§8.4) with this install's device key (§3.2): an
+ * Ed25519 key per account, kept encrypted with the OS keychain, that signs `Codync-Sig` (§5).
+ */
+
+const b64url = (b: Buffer) => b.toString('base64url')
+
+interface Identity {
+ key: KeyObject
+ publicKey: string
+}
+
+const identities = new Map<string, Identity>()
+
+async function identity(userId: string): Promise<Identity> {
+ const cached = identities.get(userId)
+ if (cached) return cached
+ const path = join(app.getPath('userData'), `device-key-${createHash('sha256').update(userId).digest('hex').slice(0, 16)}.bin`)
+ let seed: Buffer | null = null
+ try {
+ const sealed = await fs.readFile(path)
+ seed = Buffer.from(safeStorage.isEncryptionAvailable() ? safeStorage.decryptString(sealed) : sealed.toString('utf8'), 'base64url')
+ } catch {}
+ let key: KeyObject
+ if (seed?.length === 32) {
+ key = createPrivateKey({ key: { kty: 'OKP', crv: 'Ed25519', d: b64url(seed), x: publicFromSeed(seed) }, format: 'jwk' })
+ } else {
+ key = generateKeyPairSync('ed25519').privateKey
+ const d = (key.export({ format: 'jwk' }) as { d: string }).d
+ const raw = safeStorage.isEncryptionAvailable() ? safeStorage.encryptString(d) : Buffer.from(d)
+ await fs.mkdir(app.getPath('userData'), { recursive: true })
+ await fs.writeFile(path, raw, { mode: 0o600 })
+ }
+ const publicKey = (key.export({ format: 'jwk' }) as { x: string }).x
+ const made = { key, publicKey }
+ identities.set(userId, made)
+ return made
+}
+
+/** The raw Ed25519 public key (b64url) of a 32-byte seed. */
+function publicFromSeed(seed: Buffer) {
+ // PKCS#8 wrapping of an Ed25519 seed: the fixed 16-byte prefix + the seed.
+ const der = Buffer.concat([Buffer.from('302e020100300506032b657004220420', 'hex'), seed])
+ const pub = createPublicKey(createPrivateKey({ key: der, format: 'der', type: 'pkcs8' }))
+ return (pub.export({ format: 'jwk' }) as { x: string }).x
+}
+
+/** `Codync-Sig: v=1,kid,ts,nonce,sig` over method, authority, path and the body's hash. */
+function signatureHeader(id: Identity, method: string, url: URL, body: Buffer) {
+ const ts = Date.now()
+ const nonce = b64url(randomBytes(16))
+ const input = ['codync-sig-v1', method.toUpperCase(), url.host.toLowerCase(), `${url.pathname || '/'}${url.search}`, String(ts), nonce, b64url(createHash('sha256').update(body).digest())].join('\n')
+ const sig = sign(null, Buffer.from(input), id.key)
+ return `v=1,kid=${id.publicKey},ts=${ts},nonce=${nonce},sig=${b64url(sig)}`
+}
+
+export interface CloudFailure {
+ status: number
+ code: string
+ message: string | null
+}
+
+export function registerCloud(account: AccountService) {
+ ipcMain.handle('cloud:request', async (_e, method: string, path: string, body: unknown, signed: boolean) => {
+ const base = account.state.cloudURL
+ const user = account.state.user
+ if (!base || !user) return { ok: false, error: { status: 0, code: 'signedOut', message: 'Sign in again to reach your account.' } satisfies CloudFailure }
+ const url = new URL(path.replace(/^\//, ''), `${base}/`)
+ const payload = body === undefined ? Buffer.alloc(0) : Buffer.from(JSON.stringify(body))
+ const headers: Record<string, string> = {}
+ try {
+ headers.Authorization = `Bearer ${await account.sessionToken()}`
+ } catch (error) {
+ return { ok: false, error: { status: 401, code: 'signedOut', message: error instanceof Error ? error.message : 'Sign in again.' } }
+ }
+ if (body !== undefined) headers['Content-Type'] = 'application/json'
+ if (signed) headers['Codync-Sig'] = signatureHeader(await identity(user.userId), method, url, payload)
+ try {
+ const res = await fetch(url, { method, headers, body: body === undefined ? undefined : payload, signal: AbortSignal.timeout(20_000) })
+ const text = await res.text()
+ const json = text ? (JSON.parse(text) as Record<string, unknown>) : {}
+ if (!res.ok) {
+ const e = (json.error ?? {}) as { code?: string; message?: string }
+ return { ok: false, error: { status: res.status, code: e.code ?? `http${res.status}`, message: e.message ?? null } }
+ }
+ return { ok: true, value: json }
+ } catch {
+ return { ok: false, error: { status: 0, code: 'unreachable', message: "Can't reach the Codync cloud. Check your connection." } }
+ }
+ })
+ ipcMain.handle('cloud:deviceKey', async () => (account.state.user ? (await identity(account.state.user.userId)).publicKey : null))
+}
diff --git a/apps/desktop/src/main/host-controller.ts b/apps/desktop/src/main/host-controller.ts
index 4bbf5fe7..24085962 100644
--- a/apps/desktop/src/main/host-controller.ts
+++ b/apps/desktop/src/main/host-controller.ts
@@ -6,6 +6,7 @@ import { homedir, platform } from 'node:os'
import { join } from 'node:path'
import { app } from 'electron'
import type { HostState, LocalHost } from '../shared/ipc'
+import { unregisterScreenAgent } from './screen'
/** `CODYNC_PORT` / `CODYNC_HOME` point the app at a dev host started with `codync-host serve`. */
export const devPort = process.env.CODYNC_PORT ? Number(process.env.CODYNC_PORT) : null
@@ -155,6 +156,7 @@ export class HostController extends EventEmitter {
this.preparingForUpdate = true
if (existsSync(serviceFile)) prefs.set('hostRestartAfterAppUpdate', true)
this.stopLoop()
+ unregisterScreenAgent()
const bin = this.binary
if (!bin) throw new Error('The bundled host is missing.')
const result = await run(bin, ['stop'])
diff --git a/apps/desktop/src/main/index.ts b/apps/desktop/src/main/index.ts
index 763fecee..0cd1c833 100644
--- a/apps/desktop/src/main/index.ts
+++ b/apps/desktop/src/main/index.ts
@@ -1,3 +1,5 @@
+import { execFileSync } from 'node:child_process'
+import { hostname } from 'node:os'
import { join } from 'node:path'
import { app, BrowserWindow, clipboard, dialog, ipcMain, Menu, nativeTheme, session, shell } from 'electron'
import type { HostSnapshot, WindowCommand } from '../shared/ipc'
@@ -9,10 +11,23 @@ import { AccountService } from './account'
import { Updates } from './updates'
import { handleURL, registerAuthIPC, registerSchemes, urlFromArgv } from './auth'
import { registerSpeech } from './speech'
+import { registerScreenIPC } from './screen'
+import { registerCloud } from './cloud'
+import { registerSSH } from './ssh'
+
+/** macOS's user-facing computer name, else the host name. */
+function computerName() {
+ if (process.platform === 'darwin') {
+ try {
+ return execFileSync('/usr/sbin/scutil', ['--get', 'ComputerName'], { encoding: 'utf8' }).trim()
+ } catch {}
+ }
+ return hostname()
+}
const host = new HostController()
const account = new AccountService()
-const updates = new Updates()
+const updates = new Updates(host)
let chat: BrowserWindow | null = null
let pairing: BrowserWindow | null = null
let quitting = false
@@ -48,7 +63,8 @@ function openChat() {
titleBarStyle: isMac ? 'hidden' : 'default',
webPreferences: { preload: join(__dirname, '../preload/index.js'), sandbox: true, contextIsolation: true },
})
- chat.on('ready-to-show', () => chat?.show())
+ // CODYNC_SHOW_INACTIVE: development and UI checks open the window without taking focus.
+ chat.on('ready-to-show', () => (process.env.CODYNC_SHOW_INACTIVE ? chat?.showInactive() : chat?.show()))
// Closing hides: the window keeps the stores (and the menu bar's data) alive.
chat.on('close', (e) => {
if (quitting) return
@@ -119,6 +135,7 @@ function menu() {
function registerIPC(tray: Tray) {
registerHostProxy()
ipcMain.on('app:version', (e) => (e.returnValue = app.getVersion()))
+ ipcMain.on('app:computerName', (e) => (e.returnValue = computerName()))
ipcMain.handle('host:snapshot', () => snapshot())
ipcMain.handle('host:health', (_e, url: string) => fetchHealth(url))
ipcMain.on('host:install', () => void host.install())
@@ -149,6 +166,7 @@ function registerIPC(tray: Tray) {
return app.getLoginItemSettings().openAtLogin
})
ipcMain.handle('app:resetAllData', async () => {
+ await account.signOutAll()
await host.uninstall()
const { rm } = await import('node:fs/promises')
const { dataDir } = await import('./host-controller')
@@ -175,10 +193,14 @@ if (!app.requestSingleInstanceLock()) {
// A menu bar app: it keeps running with no window.
})
void app.whenReady().then(() => {
- const tray = new Tray({ openChat, openPairing, send, host })
+ const tray = new Tray({ openChat, openPairing, send, host, updates })
+ updates.on('change', () => tray.hostChanged())
registerIPC(tray)
registerAuthIPC()
registerSpeech()
+ registerScreenIPC()
+ registerCloud(account)
+ registerSSH(() => BrowserWindow.getAllWindows())
// Calls use the microphone (realtime voice); nothing else asks for permissions.
session.defaultSession.setPermissionRequestHandler((_wc, permission, done) => done(permission === 'media' || permission === 'clipboard-sanitized-write'))
account.register(() => BrowserWindow.getAllWindows())
diff --git a/apps/desktop/src/main/screen.ts b/apps/desktop/src/main/screen.ts
new file mode 100644
index 00000000..0b8f81e5
--- /dev/null
+++ b/apps/desktop/src/main/screen.ts
@@ -0,0 +1,58 @@
+import { statSync } from 'node:fs'
+import { join } from 'node:path'
+import { app, ipcMain } from 'electron'
+import { prefs } from './host-controller'
+
+// Codync Screen (capture + input for Remote screen): on macOS a launchd agent inside the app
+// bundle (apps/screen-macos, registered through SMAppService), which owns the Screen Recording
+// and Accessibility grants. Linux starts its helper from the host, so nothing happens here.
+
+const serviceName = 'com.pokai.Codync.screen.plist'
+const isMac = process.platform === 'darwin'
+
+/** Identifies the helper binary inside this app: its path and modification date. */
+function helperStamp() {
+ const path = join(process.resourcesPath, '..', 'Library/LoginItems/CodyncScreen.app/Contents/MacOS/CodyncScreen')
+ try {
+ return `${path}@${statSync(path).mtimeMs}`
+ } catch {
+ return `${path}@0`
+ }
+}
+
+function status(): string {
+ if (!isMac || !app.isPackaged) return 'not-registered'
+ return app.getLoginItemSettings({ type: 'agentService', serviceName }).status ?? 'not-registered'
+}
+
+/** Registers (or removes) the agent; returns whether macOS wants the user to allow it. */
+export function setScreenAgent(on: boolean): { needsApproval: boolean } {
+ if (!isMac || !app.isPackaged) return { needsApproval: false }
+ app.setLoginItemSettings({ type: 'agentService', serviceName, openAtLogin: on })
+ prefs.set('screenHelperStamp', on ? helperStamp() : null)
+ return { needsApproval: status() === 'requires-approval' }
+}
+
+/**
+ * Keeps Codync Screen registered while the host has Remote screen on, and registers it again
+ * when this app ships a different helper binary (launchd keeps running the old one).
+ */
+export function syncScreenAgent(enabled: boolean): { needsApproval: boolean } {
+ if (!isMac || !app.isPackaged || !enabled) return { needsApproval: false }
+ const stale = prefs.get('screenHelperStamp') !== helperStamp()
+ if (!stale && status() === 'enabled') return { needsApproval: false }
+ if (stale && status() === 'enabled') app.setLoginItemSettings({ type: 'agentService', serviceName, openAtLogin: false })
+ return setScreenAgent(true)
+}
+
+/** Before the app is replaced: the old helper stops along with its job. */
+export function unregisterScreenAgent() {
+ if (!isMac || !app.isPackaged || status() !== 'enabled') return
+ app.setLoginItemSettings({ type: 'agentService', serviceName, openAtLogin: false })
+ prefs.set('screenHelperStamp', null)
+}
+
+export function registerScreenIPC() {
+ ipcMain.handle('screen:setAgent', (_e, on: boolean) => setScreenAgent(on))
+ ipcMain.handle('screen:syncAgent', (_e, enabled: boolean) => syncScreenAgent(enabled))
+}
diff --git a/apps/desktop/src/main/speech.ts b/apps/desktop/src/main/speech.ts
index 8b97b42c..62e20ac0 100644
--- a/apps/desktop/src/main/speech.ts
+++ b/apps/desktop/src/main/speech.ts
@@ -18,6 +18,8 @@ function helperPath() {
let child: ChildProcessWithoutNullStreams | null = null
let listener: WebContents | null = null
+/** Bumped per start: a stopping process may still deliver its final result, an older one may not. */
+let generation = 0
function emit(event: SpeechEvent) {
if (listener && !listener.isDestroyed()) listener.send('speech:event', event)
@@ -29,6 +31,7 @@ function start(sender: WebContents, locale: string | null) {
const path = helperPath()
if (!path) return emit({ type: 'error', message: "On-device speech isn't available in this build." })
const proc = spawn(path, locale ? ['--locale', locale] : [], { stdio: 'pipe' })
+ const mine = ++generation
child = proc
let buffer = ''
proc.stdout.on('data', (chunk: Buffer) => {
@@ -38,15 +41,13 @@ function start(sender: WebContents, locale: string | null) {
const line = buffer.slice(0, nl)
buffer = buffer.slice(nl + 1)
try {
- if (child === proc) emit(JSON.parse(line) as SpeechEvent)
+ if (generation === mine) emit(JSON.parse(line) as SpeechEvent)
} catch {}
}
})
proc.on('exit', () => {
- if (child === proc) {
- child = null
- emit({ type: 'stopped' })
- }
+ if (child === proc) child = null
+ if (generation === mine) emit({ type: 'stopped' })
})
proc.on('error', (error) => emit({ type: 'error', message: error.message }))
}
diff --git a/apps/desktop/src/main/ssh.ts b/apps/desktop/src/main/ssh.ts
new file mode 100644
index 00000000..fbfb6b5e
--- /dev/null
+++ b/apps/desktop/src/main/ssh.ts
@@ -0,0 +1,585 @@
+import { spawn, type ChildProcess } from 'node:child_process'
+import { createHash } from 'node:crypto'
+import { existsSync, promises as fs, readFileSync, statSync } from 'node:fs'
+import { createServer } from 'node:net'
+import { homedir, platform } from 'node:os'
+import { join } from 'node:path'
+import { app, dialog, ipcMain, type BrowserWindow } from 'electron'
+import type { SSHAttachment, SSHProfile, SSHState, SSHStatus } from '../shared/ssh'
+
+// SSH computers (port of the Mac app's SSHTunnel.swift): profiles, host key checks and one
+// OpenSSH tunnel per connected profile. Arguments are always an argv array, never a shell string.
+
+const SSH = '/usr/bin/ssh'
+const KEYGEN = '/usr/bin/ssh-keygen'
+const KEYSCAN = '/usr/bin/ssh-keyscan'
+const LSOF = ['/usr/sbin/lsof', '/usr/bin/lsof'].find((p) => existsSync(p)) ?? null
+const home = homedir()
+
+/** Where `codync-host` lives when `sh -l` doesn't see it (Homebrew, install.sh, the Mac app's bundle). */
+const REMOTE_PATH = '$PATH:/opt/homebrew/bin:/usr/local/bin:/home/linuxbrew/.linuxbrew/bin:$HOME/.local/bin:/Applications/Codync.app/Contents/MacOS'
+
+// MARK: pure helpers
+
+/** A reason the profile can't be used, or null. */
+export function validate(p: SSHProfile): string | null {
+ if (p.host.startsWith('-') || !/^[A-Za-z0-9._-]+$/.test(p.host)) {
+ return 'Use an SSH alias or a hostname: letters, digits, dots, dashes and underscores.'
+ }
+ if (p.user !== null && !/^[A-Za-z_][A-Za-z0-9._-]*$/.test(p.user)) return "That user name isn't valid."
+ const inRange = (n: number) => Number.isInteger(n) && n >= 1 && n <= 65535
+ if (p.port !== null && !inRange(p.port)) return 'The SSH port must be between 1 and 65535.'
+ if (!inRange(p.remotePort)) return 'The Codync port must be between 1 and 65535.'
+ if (p.identityFile !== null) {
+ let ok = false
+ try {
+ ok = p.identityFile.startsWith('/') && statSync(p.identityFile).isFile()
+ } catch {}
+ if (!ok) return "The key file doesn't exist."
+ }
+ return null
+}
+
+const knownHostsFiles = (h: string) => [`${h}/.ssh/known_hosts`, `${h}/.codync/ssh_known_hosts`]
+
+/** ssh's config tokenizer splits `UserKnownHostsFile` on spaces, so each path carries literal quotes. */
+export const hostKeyOptions = (h: string) => [
+ '-o', 'UserKnownHostsFile=' + knownHostsFiles(h).map((f) => `"${f}"`).join(' '),
+ '-o', 'StrictHostKeyChecking=yes',
+]
+
+function targetOptions(p: SSHProfile) {
+ const args: string[] = []
+ if (p.port !== null) args.push('-p', String(p.port))
+ if (p.identityFile !== null) args.push('-i', p.identityFile)
+ if (p.user !== null) args.push('-l', p.user)
+ return args
+}
+
+export function resolveArguments(p: SSHProfile) {
+ const args = ['-G']
+ if (p.port !== null) args.push('-p', String(p.port))
+ if (p.user !== null) args.push('-l', p.user)
+ return [...args, '--', p.host]
+}
+
+/** The remote command is fixed; only the validated port number goes into it. */
+export const infoArguments = (p: SSHProfile, h: string) => [
+ '-T', '-o', 'BatchMode=yes', '-o', 'ConnectTimeout=10', '-o', 'ForwardAgent=no', '-o', 'ForwardX11=no',
+ ...hostKeyOptions(h), ...targetOptions(p),
+ '--', p.host, `sh -lc 'PATH="${REMOTE_PATH}" codync-host info --json --port ${p.remotePort}'`,
+]
+
+export const tunnelArguments = (p: SSHProfile, localPort: number, h: string) => [
+ '-N', '-T', '-o', 'ExitOnForwardFailure=yes', '-o', 'ServerAliveInterval=15', '-o', 'ServerAliveCountMax=3',
+ '-o', 'ForwardAgent=no', '-o', 'ForwardX11=no', '-o', 'BatchMode=yes',
+ ...hostKeyOptions(h),
+ '-L', `127.0.0.1:${localPort}:127.0.0.1:${p.remotePort}`,
+ ...targetOptions(p), '--', p.host,
+]
+
+/** The far side rejected our key (BatchMode never prompts, so a password or passphrase can't help). */
+export const authRefused = (stderr: string) => stderr.includes('Permission denied') || stderr.includes('Too many authentication failures')
+
+/** What `ssh -G` resolved the destination to. */
+export interface Resolved {
+ hostname: string
+ port: number
+ hostKeyAlias: string | null
+ proxyJump: string | null
+ proxyCommand: string | null
+}
+
+const usesProxy = (r: Resolved) => r.proxyJump !== null || r.proxyCommand !== null
+/** The name ssh looks up in known_hosts. */
+export const knownHostsName = (r: Resolved) => {
+ const name = r.hostKeyAlias ?? r.hostname
+ return r.port === 22 ? name : `[${name}]:${r.port}`
+}
+
+export function parseConfig(text: string): Resolved | null {
+ const values = new Map<string, string>()
+ for (const line of text.split(/\r?\n/)) {
+ const at = line.indexOf(' ')
+ if (at < 0) continue
+ const key = line.slice(0, at).toLowerCase()
+ if (!values.has(key)) values.set(key, line.slice(at + 1).trim())
+ }
+ const set = (k: string) => {
+ const v = values.get(k)
+ return v && v !== 'none' ? v : null
+ }
+ const hostname = set('hostname')
+ const port = Number(values.get('port'))
+ if (!hostname || !Number.isInteger(port) || !values.get('port')) return null
+ return { hostname, port, hostKeyAlias: set('hostkeyalias'), proxyJump: set('proxyjump'), proxyCommand: set('proxycommand') }
+}
+
+const keyscanArguments = (r: Resolved) => ['-T', '5', '-p', String(r.port), '--', r.hostname]
+
+/** `type key` of each host key line (known_hosts or ssh-keyscan output); comments and revoked keys skipped. */
+export function hostKeys(text: string, certAuthorities = true): Set<string> {
+ const keys = new Set<string>()
+ for (const line of text.split(/\r?\n/)) {
+ const fields = line.split(/\s+/).filter(Boolean)
+ const first = fields[0]
+ if (!first || first.startsWith('#')) continue
+ if (first.startsWith('@')) {
+ if (!certAuthorities || first !== '@cert-authority') continue
+ fields.shift()
+ }
+ if (fields.length >= 3) keys.add(`${fields[1]} ${fields[2]}`)
+ }
+ return keys
+}
+
+/** ssh-keyscan's lines, filed under the name ssh will look up. */
+export const knownHostsLines = (scan: string, name: string) => [...hostKeys(scan)].sort().map((k) => `${name} ${k}`)
+
+/** A computer id is the first 16 bytes of SHA-256 of its signing key, base64url. */
+function consistent(id: string, signKey: string) {
+ const key = Buffer.from(signKey, 'base64url')
+ return key.length === 32 && createHash('sha256').update(key).digest().subarray(0, 16).toString('base64url') === id
+}
+
+// MARK: processes
+
+interface Result {
+ status: number
+ stdout: string
+ stderr: string
+}
+
+/** The last thing the tool said, for showing next to an error. */
+const lastLine = (text: string) => text.split(/\r?\n/).filter(Boolean).pop() ?? ''
+
+/** Runs a tool to completion; both pipes are drained while it runs. */
+function run(bin: string, args: string[], input?: string): Promise<Result> {
+ return new Promise((resolve) => {
+ let stdout = ''
+ let stderr = ''
+ let child: ChildProcess
+ try {
+ child = spawn(bin, args, { stdio: [input === undefined ? 'ignore' : 'pipe', 'pipe', 'pipe'] })
+ } catch {
+ return resolve({ status: -1, stdout, stderr })
+ }
+ child.stdout?.on('data', (d: Buffer) => (stdout += d.toString()))
+ child.stderr?.on('data', (d: Buffer) => (stderr += d.toString()))
+ child.on('error', () => resolve({ status: -1, stdout, stderr }))
+ child.on('close', (code) => resolve({ status: code ?? -1, stdout, stderr }))
+ if (input !== undefined) child.stdin?.end(input)
+ })
+}
+
+/** Tunnels a crashed or force-quit Codync left running: only Codync's tunnels carry this known_hosts file. */
+const killOrphanTunnels = () => run('/usr/bin/pkill', ['-f', '--', '^/usr/bin/ssh -N .*\\.codync/ssh_known_hosts'])
+
+/** Binds port 0 on loopback to learn a free port. */
+function freeLocalPort(): Promise<number | null> {
+ return new Promise((resolve) => {
+ const server = createServer()
+ server.once('error', () => resolve(null))
+ server.listen(0, '127.0.0.1', () => {
+ const address = server.address()
+ server.close(() => resolve(typeof address === 'object' && address ? address.port : null))
+ })
+ })
+}
+
+/** Whether process `pid` holds the listening socket on 127.0.0.1:`port`. */
+async function sshListens(pid: number, port: number): Promise<boolean> {
+ if (LSOF) {
+ const found = await run(LSOF, ['-a', '-n', '-P', '-p', String(pid), `[email protected]:${port}`, '-sTCP:LISTEN', '-t'])
+ return found.status === 0 && found.stdout.split(/\r?\n/).includes(String(pid))
+ }
+ if (platform() !== 'linux') return false
+ // No lsof: the listening socket's inode from /proc/net/tcp, then whether `pid` holds it.
+ try {
+ const local = `0100007F:${port.toString(16).toUpperCase().padStart(4, '0')}`
+ const inodes = (await fs.readFile('/proc/net/tcp', 'utf8'))
+ .split('\n')
+ .map((l) => l.trim().split(/\s+/))
+ .filter((f) => f[1] === local && f[3] === '0A')
+ .map((f) => `socket:[${f[9]}]`)
+ if (inodes.length === 0) return false
+ for (const fd of await fs.readdir(`/proc/${pid}/fd`)) {
+ const link = await fs.readlink(`/proc/${pid}/fd/${fd}`).catch(() => '')
+ if (inodes.includes(link)) return true
+ }
+ } catch {}
+ return false
+}
+
+async function healthOf(baseURL: string): Promise<{ computerId?: string } | null> {
+ try {
+ const res = await fetch(`${baseURL}/health`, { signal: AbortSignal.timeout(1000) })
+ return res.ok ? ((await res.json()) as { computerId?: string }) : null
+ } catch {
+ return null
+ }
+}
+
+const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms))
+
+/** What `codync-host info --json` prints. */
+interface RemoteInfo {
+ name: string
+ computerId: string
+ signKey: string
+ boxKey?: string | null
+ token: string
+ running: boolean
+}
+
+function parseInfo(text: string): RemoteInfo | null {
+ try {
+ const v = JSON.parse(text) as Partial<RemoteInfo>
+ return typeof v.name === 'string' && typeof v.computerId === 'string' && typeof v.signKey === 'string' && typeof v.token === 'string' && typeof v.running === 'boolean'
+ ? (v as RemoteInfo)
+ : null
+ } catch {
+ return null
+ }
+}
+
+// MARK: the manager
+
+type Outcome = { kind: 'attached' } | { kind: 'stop'; status: SSHStatus } | { kind: 'retry'; message: string }
+interface Token {
+ cancelled: boolean
+}
+
+const signInRefused = (p: SSHProfile) =>
+ `${p.host} refused the key. Codync signs in with an SSH key (ssh-agent or the key file) and can't type a password or passphrase; make \`ssh ${p.host}\` work in Terminal without prompting, then connect again.`
+
+class SSHComputers {
+ profiles: SSHProfile[] = []
+ private status = new Map<string, SSHStatus>()
+ private attachments = new Map<string, SSHAttachment>()
+ private tunnels = new Map<string, ChildProcess>()
+ private tasks = new Map<string, Token>()
+ private backoff = new Map<string, number>()
+ private connectedAt = new Map<string, number>()
+ /** Host key lines waiting for the user's "Trust and connect". */
+ private pendingKeys = new Map<string, string[]>()
+
+ constructor(private file: string, private emit: (s: SSHState) => void) {
+ try {
+ this.profiles = JSON.parse(readFileSync(file, 'utf8')) as SSHProfile[]
+ } catch {}
+ }
+
+ get state(): SSHState {
+ return { profiles: this.profiles, status: Object.fromEntries(this.status), attachments: [...this.attachments.values()] }
+ }
+
+ private changed() {
+ this.emit(this.state)
+ }
+
+ private setStatus(id: string, s: SSHStatus) {
+ this.status.set(id, s)
+ this.changed()
+ }
+
+ async connectAll() {
+ // Before this copy has tunnels of its own, so only leftovers from an earlier copy match.
+ if (this.tunnels.size === 0 && this.tasks.size === 0) await killOrphanTunnels()
+ for (const p of this.profiles) if (!this.tasks.has(p.id) && !this.tunnels.has(p.id)) this.connect(p.id)
+ }
+
+ save(profile: SSHProfile): string | null {
+ const old = this.profiles.find((p) => p.id === profile.id)
+ // A different address may be a different computer: learn its identity again.
+ const p = old && (old.host !== profile.host || old.port !== profile.port) ? { ...profile, computerId: null } : profile
+ const problem = validate(p)
+ if (problem) return problem
+ if (old) {
+ this.disconnect(p.id)
+ this.profiles = this.profiles.map((x) => (x.id === p.id ? p : x))
+ } else {
+ this.profiles = [...this.profiles, p]
+ }
+ this.persist()
+ this.connect(p.id)
+ return null
+ }
+
+ remove(id: string) {
+ this.disconnect(id)
+ this.profiles = this.profiles.filter((p) => p.id !== id)
+ this.status.delete(id)
+ this.backoff.delete(id)
+ this.persist()
+ this.changed()
+ }
+
+ connect(id: string) {
+ const old = this.tasks.get(id)
+ if (old) old.cancelled = true
+ const token: Token = { cancelled: false }
+ this.tasks.set(id, token)
+ void this.run(id, token)
+ }
+
+ /** Ends the tunnel for this session (bots on it go away until the next connect). */
+ disconnect(id: string) {
+ const task = this.tasks.get(id)
+ if (task) task.cancelled = true
+ this.tasks.delete(id)
+ this.stopTunnel(id)
+ this.setStatus(id, { kind: 'idle' })
+ }
+
+ disconnectAll() {
+ for (const id of new Set([...this.tunnels.keys(), ...this.tasks.keys()])) this.disconnect(id)
+ }
+
+ /** The user compared the fingerprints: remember the key in Codync's known_hosts and go on. */
+ async trustHostKey(id: string) {
+ const lines = this.pendingKeys.get(id)
+ if (this.status.get(id)?.kind !== 'confirmHostKey' || !lines) return
+ try {
+ const dir = join(home, '.codync')
+ await fs.mkdir(dir, { recursive: true, mode: 0o700 })
+ await fs.appendFile(join(dir, 'ssh_known_hosts'), lines.join('\n') + '\n', { mode: 0o600 })
+ } catch (e) {
+ return this.setStatus(id, { kind: 'failed', message: `Couldn't save the host key: ${e instanceof Error ? e.message : String(e)}` })
+ }
+ this.pendingKeys.delete(id)
+ this.connect(id)
+ }
+
+ // MARK: connecting
+
+ private async run(id: string, token: Token) {
+ while (!token.cancelled) {
+ const profile = this.profiles.find((p) => p.id === id)
+ if (!profile) return
+ const outcome = await this.attach(profile, token)
+ // Disconnected or replaced meanwhile: the newer task owns the state now.
+ if (token.cancelled) return
+ switch (outcome.kind) {
+ case 'attached':
+ this.backoff.delete(id)
+ this.tasks.delete(id)
+ return
+ case 'stop':
+ this.tasks.delete(id)
+ return this.setStatus(id, outcome.status)
+ case 'retry': {
+ const delay = this.backoff.get(id) ?? 1
+ this.backoff.set(id, Math.min(delay * 2, 30))
+ this.setStatus(id, { kind: 'retrying', message: outcome.message })
+ // Full jitter, like the device relay backoff.
+ await sleep(1000 * (0.5 + Math.random() * Math.max(0, delay - 0.5)))
+ }
+ }
+ }
+ }
+
+ private async attach(profile: SSHProfile, token: Token): Promise<Outcome> {
+ const id = profile.id
+ const stop = (status: SSHStatus): Outcome => ({ kind: 'stop', status })
+ const idle = stop({ kind: 'idle' })
+ const problem = validate(profile)
+ if (problem) return stop({ kind: 'failed', message: problem })
+ this.setStatus(id, { kind: 'connecting', step: 'Reading SSH settings…' })
+ const config = await run(SSH, resolveArguments(profile))
+ if (token.cancelled) return idle
+ const resolved = config.status === 0 ? parseConfig(config.stdout) : null
+ if (!resolved) return stop({ kind: 'failed', message: `SSH couldn't read the settings for ${profile.host}. ${lastLine(config.stderr)}` })
+ const name = knownHostsName(resolved)
+
+ // Host key (never by reading ssh's error text).
+ if ((await this.recordedKeys(name)).size === 0) {
+ if (usesProxy(resolved)) {
+ return stop({ kind: 'failed', message: `${profile.host} goes through a jump host. Connect once with ssh in Terminal to confirm its host key, then try again.` })
+ }
+ this.setStatus(id, { kind: 'connecting', step: 'Reading the host key…' })
+ const scan = await run(KEYSCAN, keyscanArguments(resolved))
+ const lines = knownHostsLines(scan.stdout, name)
+ if (lines.length === 0) return { kind: 'retry', message: `Can't reach ${resolved.hostname}:${resolved.port}.` }
+ const prints = await run(KEYGEN, ['-l', '-f', '-'], lines.join('\n') + '\n')
+ const fingerprints = prints.stdout.split(/\r?\n/).filter(Boolean)
+ if (prints.status !== 0 || fingerprints.length === 0) return stop({ kind: 'failed', message: `Couldn't read the host key of ${profile.host}.` })
+ this.pendingKeys.set(id, lines)
+ return stop({ kind: 'confirmHostKey', name, fingerprints })
+ }
+
+ this.setStatus(id, { kind: 'connecting', step: 'Signing in…' })
+ const result = await run(SSH, infoArguments(profile, home))
+ if (token.cancelled) return idle
+ if (result.status === 255) {
+ if (authRefused(result.stderr)) return stop({ kind: 'failed', message: signInRefused(profile) })
+ if (!usesProxy(resolved)) {
+ const current = hostKeys((await run(KEYSCAN, keyscanArguments(resolved))).stdout)
+ // Only plain host keys can be compared: a `@cert-authority` key is never what keyscan returns.
+ const pinned = await this.recordedKeys(name, false)
+ if (current.size > 0 && pinned.size > 0 && ![...current].some((k) => pinned.has(k))) {
+ return stop({ kind: 'failed', message: `The host key of ${profile.host} changed. This can mean someone is intercepting the connection. Codync won't connect until the old key is removed from known_hosts.` })
+ }
+ }
+ return { kind: 'retry', message: `SSH couldn't connect to ${profile.host}. ${lastLine(result.stderr)}` }
+ }
+ if (result.status === 127) return stop({ kind: 'notInstalled' })
+ const info = parseInfo(result.stdout)
+ if (!info) {
+ if (result.status === 0) return stop({ kind: 'notInstalled' })
+ return stop({ kind: 'failed', message: `codync-host on ${profile.host} couldn't report its identity. Run \`codync-host install\` there. ${lastLine(result.stderr)}` })
+ }
+ if (!consistent(info.computerId, info.signKey)) return stop({ kind: 'failed', message: `${profile.host} reported an invalid identity.` })
+ if (profile.computerId && profile.computerId !== info.computerId) {
+ return stop({ kind: 'failed', message: `${profile.host} is now a different computer than the one saved. If it was reinstalled, remove it and add it again.` })
+ }
+ if (!profile.computerId) this.remember(id, info.computerId, info.name)
+ if (!info.running) return { kind: 'retry', message: `codync-host is installed on ${info.name} but not running. Run \`codync-host install\` there.` }
+
+ // Tunnel: a free loopback port each attempt; a port taken in between just makes ssh exit (ExitOnForwardFailure).
+ let lastProblem = ''
+ for (let attempt = 0; attempt < 3; attempt++) {
+ const port = await freeLocalPort()
+ if (port === null) return { kind: 'retry', message: 'No free local port for the tunnel.' }
+ this.setStatus(id, { kind: 'connecting', step: 'Opening the tunnel…' })
+ const tunnel = this.startTunnel(profile, port)
+ if (!tunnel) return stop({ kind: 'failed', message: "Couldn't start ssh." })
+ const { child, tail } = tunnel
+ const alive = () => child.exitCode === null && child.signalCode === null
+ const baseURL = `http://127.0.0.1:${port}`
+ let health: { computerId?: string } | null = null
+ for (let i = 0; i < 60 && alive() && !token.cancelled; i++) {
+ health = await healthOf(baseURL)
+ if (health) break
+ await sleep(250)
+ }
+ if (token.cancelled) {
+ child.kill()
+ return idle
+ }
+ if (!health) {
+ if (alive()) child.kill()
+ else lastProblem = await tail.lastLine()
+ continue
+ }
+ if (health.computerId !== info.computerId) {
+ child.kill()
+ return stop({ kind: 'failed', message: `The tunnel to ${profile.host} reached a different computer than expected.` })
+ }
+ // /health is public, so the answer alone doesn't prove it came through ssh: another
+ // local user could have taken the port. Only a listener ssh owns gets the token.
+ const listens = child.pid !== undefined && (await sshListens(child.pid, port))
+ // Disconnected, removed or edited meanwhile: this attempt must not register its tunnel.
+ if (token.cancelled) {
+ child.kill()
+ return idle
+ }
+ if (!listens || !alive()) {
+ console.error(`ssh: tunnel port ${port} answered by something other than ssh`)
+ child.kill()
+ lastProblem = 'Another program answered on the tunnel\'s local port.'
+ continue
+ }
+ this.watch(child, id, tail)
+ this.tunnels.set(id, child)
+ this.connectedAt.set(id, Date.now())
+ this.attachments.set(id, {
+ profileId: id,
+ computer: { id: info.computerId, name: info.name, signKey: info.signKey, boxKey: info.boxKey ?? null },
+ baseURL,
+ token: info.token,
+ })
+ this.setStatus(id, { kind: 'connected', localPort: port })
+ return { kind: 'attached' }
+ }
+ if (authRefused(lastProblem)) return stop({ kind: 'failed', message: signInRefused(profile) })
+ return { kind: 'retry', message: `Couldn't open the tunnel to ${profile.host}. ${lastProblem}` }
+ }
+
+ private async recordedKeys(name: string, certAuthorities = true) {
+ const keys = new Set<string>()
+ for (const file of knownHostsFiles(home)) {
+ if (!existsSync(file)) continue
+ const found = await run(KEYGEN, ['-F', name, '-f', file])
+ if (found.status === 0) for (const k of hostKeys(found.stdout, certAuthorities)) keys.add(k)
+ }
+ return keys
+ }
+
+ /** ssh's stderr is read as it arrives: left unread, the pipe fills and ssh blocks mid-write, freezing the tunnel. */
+ private startTunnel(profile: SSHProfile, localPort: number) {
+ let text = ''
+ let done = false
+ let child: ChildProcess
+ try {
+ child = spawn(SSH, tunnelArguments(profile, localPort, home), { stdio: ['ignore', 'ignore', 'pipe'] })
+ } catch {
+ return null
+ }
+ child.on('error', () => {})
+ child.stderr?.on('data', (d: Buffer) => (text = (text + d.toString()).slice(-4096)))
+ child.stderr?.on('close', () => (done = true))
+ const tail = {
+ /** ssh's last line, once it exited (waits briefly for the pipe to reach its end). */
+ async lastLine() {
+ for (let i = 0; i < 40 && !done; i++) await sleep(25)
+ return lastLine(text)
+ },
+ }
+ return { child, tail }
+ }
+
+ /** A tunnel that drops is reopened with backoff (reset once it stayed up a minute). */
+ private watch(child: ChildProcess, id: string, tail: { lastLine(): Promise<string> }) {
+ child.once('exit', () => {
+ void tail.lastLine().then((detail) => {
+ // Only the current tunnel: a replaced or disconnected one ends without effect.
+ if (this.tunnels.get(id) !== child) return
+ console.log(`ssh tunnel ended: ${detail}`)
+ const since = this.connectedAt.get(id)
+ if (since !== undefined && Date.now() - since >= 60_000) this.backoff.delete(id)
+ this.stopTunnel(id)
+ this.setStatus(id, { kind: 'retrying', message: `The SSH connection dropped. ${detail}` })
+ this.connect(id)
+ })
+ })
+ }
+
+ private stopTunnel(id: string) {
+ const child = this.tunnels.get(id)
+ this.tunnels.delete(id)
+ child?.kill()
+ this.connectedAt.delete(id)
+ if (this.attachments.delete(id)) this.changed()
+ }
+
+ private remember(id: string, computerId: string, name: string) {
+ this.profiles = this.profiles.map((p) => (p.id === id ? { ...p, computerId, name: p.name || name } : p))
+ this.persist()
+ this.changed()
+ }
+
+ private persist() {
+ void fs.mkdir(app.getPath('userData'), { recursive: true }).then(() => fs.writeFile(this.file, JSON.stringify(this.profiles)))
+ }
+}
+
+/** SSH computers for every window: `ssh:*` handlers, `ssh:change` events, tunnels stopped on quit. */
+export function registerSSH(getWindows: () => BrowserWindow[]) {
+ const ssh = new SSHComputers(join(app.getPath('userData'), 'ssh-profiles.json'), (state) => {
+ for (const w of getWindows()) if (!w.isDestroyed()) w.webContents.send('ssh:change', state)
+ })
+ ipcMain.handle('ssh:state', () => ssh.state)
+ ipcMain.handle('ssh:save', (_e, profile: SSHProfile) => ssh.save(profile))
+ ipcMain.on('ssh:remove', (_e, id: string) => ssh.remove(id))
+ ipcMain.on('ssh:connect', (_e, id: string) => ssh.connect(id))
+ ipcMain.on('ssh:disconnect', (_e, id: string) => ssh.disconnect(id))
+ ipcMain.on('ssh:trustHostKey', (_e, id: string) => void ssh.trustHostKey(id))
+ ipcMain.handle('ssh:chooseKey', async () => {
+ const result = await dialog.showOpenDialog({ defaultPath: join(home, '.ssh'), properties: ['openFile', 'showHiddenFiles'] })
+ return result.canceled ? null : (result.filePaths[0] ?? null)
+ })
+ // ssh children outlive the app unless stopped.
+ app.on('will-quit', () => ssh.disconnectAll())
+ void ssh.connectAll()
+}
diff --git a/apps/desktop/src/main/tray.ts b/apps/desktop/src/main/tray.ts
index a1c5a571..7e91b5a0 100644
--- a/apps/desktop/src/main/tray.ts
+++ b/apps/desktop/src/main/tray.ts
@@ -2,12 +2,14 @@ import { join } from 'node:path'
import { app, clipboard, Menu, nativeImage, shell, Tray as ElectronTray } from 'electron'
import type { TraySummary, WindowCommand } from '../shared/ipc'
import type { HostController } from './host-controller'
+import type { Updates } from './updates'
interface Deps {
openChat: () => unknown
openPairing: () => void
send: (command: WindowCommand, show?: boolean) => void
host: HostController
+ updates: Updates
}
const resources = () => (app.isPackaged ? process.resourcesPath : join(__dirname, '../../resources'))
@@ -145,6 +147,27 @@ export class Tray {
return items
}
+ private updatesMenu(): Electron.MenuItemConstructorOptions[] {
+ const { updates } = this.deps
+ const u = updates.state
+ const items: Electron.MenuItemConstructorOptions[] = [
+ { label: u.availableVersion ? `Update to ${u.availableVersion}…` : 'Check for Updates…', enabled: u.canCheck || u.staged, click: () => updates.check() },
+ ]
+ // The release waits until paired iPhones can get the app it needs.
+ if (u.waitingForApp) items.push({ label: `Waiting for iPhone app ${u.waitingForApp} to pass App Store review`, enabled: false })
+ items.push(
+ { label: 'Automatically check for updates', type: 'checkbox', checked: u.autoCheck, enabled: u.supported, click: () => updates.emit('setAutoCheck', !u.autoCheck) },
+ { label: 'Automatically download and install', type: 'checkbox', checked: u.autoDownload, enabled: u.supported, click: () => updates.emit('setAutoDownload', !u.autoDownload) },
+ )
+ if (!u.supported) items.push({ label: 'Updates are available in release builds.', enabled: false })
+ if (u.lastCheck) items.push({ label: `Last checked: ${new Date(u.lastCheck).toLocaleString()}`, enabled: false })
+ if (u.error) {
+ items.push({ label: u.error, enabled: false })
+ items.push({ label: 'Retry installing update', click: () => updates.check() })
+ }
+ return items
+ }
+
private settingsMenu(): Electron.MenuItemConstructorOptions[] {
const { host, send } = this.deps
const s = this.summary
@@ -178,6 +201,8 @@ export class Tray {
},
},
{ type: 'separator' },
+ { label: 'Updates', submenu: this.updatesMenu() },
+ { type: 'separator' },
{ label: 'Restart host', click: () => host.restart() },
{ label: 'Open log', click: () => void shell.openPath(host.logPath) },
{ type: 'separator' },
diff --git a/apps/desktop/src/main/updates.ts b/apps/desktop/src/main/updates.ts
index f907c73a..bafb9bab 100644
--- a/apps/desktop/src/main/updates.ts
+++ b/apps/desktop/src/main/updates.ts
@@ -1,8 +1,17 @@
import { EventEmitter } from 'node:events'
-import { app, ipcMain, type BrowserWindow } from 'electron'
+import { app, BrowserWindow, ipcMain, powerMonitor } from 'electron'
+import electronUpdater, { type UpdateInfo } from 'electron-updater'
+import { isBelow, parseVersion } from '../shared/compat'
import type { UpdateState } from '../shared/ipc'
+import { prefs, type HostController } from './host-controller'
-/** The app's own updates. Release builds only. */
+const { autoUpdater } = electronUpdater
+
+/**
+ * The app's own updates (electron-updater, GitHub releases; release builds only). Codync
+ * coordinates its background services before the installer replaces the app: the host is
+ * stopped first, and a restart marker makes the next launch reinstall it from the new bundle.
+ */
export class Updates extends EventEmitter {
state: UpdateState = {
supported: app.isPackaged,
@@ -10,27 +19,184 @@ export class Updates extends EventEmitter {
checking: false,
availableVersion: null,
staged: false,
- autoCheck: true,
- autoDownload: true,
- lastCheck: null,
+ autoCheck: prefs.get('updatesAutoCheck') !== false,
+ autoDownload: prefs.get('updatesAutoDownload') === true,
+ lastCheck: (prefs.get('updatesLastCheck') as number | undefined) ?? null,
error: null,
waitingForApp: null,
}
+ private userInitiated = false
+ private preparing: Promise<boolean> | null = null
+ private prepared = false
+ private appStoreVersion: string | null = null
+
+ constructor(private host: HostController) {
+ super()
+ }
register(windows: () => BrowserWindow[]) {
ipcMain.handle('updates:state', () => this.state)
ipcMain.on('updates:check', () => this.check())
- ipcMain.on('updates:autoCheck', (_e, on: boolean) => this.set({ autoCheck: on }))
- ipcMain.on('updates:autoDownload', (_e, on: boolean) => this.set({ autoDownload: on }))
+ ipcMain.on('updates:autoCheck', (_e, on: boolean) => this.setAutoCheck(on))
+ ipcMain.on('updates:autoDownload', (_e, on: boolean) => this.setAutoDownload(on))
+ this.on('setAutoCheck', (on: boolean) => this.setAutoCheck(on))
+ this.on('setAutoDownload', (on: boolean) => this.setAutoDownload(on))
this.on('change', () => {
for (const w of windows()) w.webContents.send('updates:change', this.state)
})
+ if (!app.isPackaged) return
+
+ autoUpdater.autoDownload = false
+ autoUpdater.autoInstallOnAppQuit = true
+ autoUpdater.on('checking-for-update', () => this.set({ checking: true, error: null }))
+ autoUpdater.on('update-not-available', () => this.checked({ availableVersion: null }))
+ autoUpdater.on('update-available', (info) => void this.available(info))
+ autoUpdater.on('update-downloaded', (info) => {
+ this.set({ staged: true, availableVersion: info.version })
+ if (this.userInitiated) void this.install()
+ else this.installWhenIdle()
+ })
+ autoUpdater.on('error', (error) => {
+ this.checked({ error: this.userInitiated ? error.message : null })
+ this.userInitiated = false
+ })
+
+ // A staged update installs on Quit, once the host has actually stopped.
+ app.on('before-quit', (event) => {
+ if (!this.state.staged || this.prepared) return
+ event.preventDefault()
+ void this.prepare().then((ready) => ready && app.quit())
+ })
+
+ setTimeout(() => this.scheduled(), 30_000)
+ setInterval(() => this.scheduled(), 3600_000)
+ }
+
+ setAutoCheck(on: boolean) {
+ prefs.set('updatesAutoCheck', on)
+ this.set({ autoCheck: on })
}
- check() {}
+ setAutoDownload(on: boolean) {
+ prefs.set('updatesAutoDownload', on)
+ this.set({ autoDownload: on })
+ }
- protected set(change: Partial<UpdateState>) {
+ private set(change: Partial<UpdateState>) {
this.state = { ...this.state, ...change }
this.emit('change')
}
+
+ private checked(change: Partial<UpdateState>) {
+ const now = Date.now()
+ prefs.set('updatesLastCheck', now)
+ this.set({ checking: false, lastCheck: now, ...change })
+ }
+
+ /** Daily, or hourly while a release waits for the iPhone app to pass review. */
+ private scheduled() {
+ if (!this.state.autoCheck || this.state.checking || this.state.staged) return
+ const interval = this.state.waitingForApp ? 3600_000 : 24 * 3600_000
+ if (this.state.lastCheck && Date.now() - this.state.lastCheck < interval - 60_000) return
+ this.userInitiated = false
+ void autoUpdater.checkForUpdates().catch(() => {})
+ }
+
+ check() {
+ if (!app.isPackaged) return
+ if (this.state.staged) return void this.install()
+ this.userInitiated = true
+ this.set({ error: null })
+ void autoUpdater.checkForUpdates().catch(() => {})
+ }
+
+ /**
+ * A release that needs a newer iPhone app than the App Store has (still in review) waits,
+ * so paired iPhones aren't asked for an update they can't get yet.
+ */
+ private async available(info: UpdateInfo) {
+ const required = (info as UpdateInfo & { minApp?: string }).minApp
+ let waiting: string | null = null
+ if (required) {
+ const [hostMinApp, iphones] = await this.localHostFacts()
+ this.appStoreVersion = (await appStoreVersion()) ?? this.appStoreVersion
+ const storeHas = this.appStoreVersion && parseVersion(this.appStoreVersion) && !isBelow(this.appStoreVersion, required)
+ if (isBelow(hostMinApp ?? '0', required) && iphones !== false && !storeHas && !(this.appStoreVersion === null && !this.userInitiated)) {
+ waiting = required
+ }
+ }
+ this.checked({ availableVersion: info.version, waitingForApp: waiting })
+ if (waiting) {
+ if (this.userInitiated) this.set({ error: `Codync ${info.version} needs the iPhone app ${waiting}, which isn't in the App Store yet. It installs once that version passes review.` })
+ this.userInitiated = false
+ return
+ }
+ if (this.userInitiated || this.state.autoDownload) void autoUpdater.downloadUpdate().catch(() => {})
+ }
+
+ /** The local host's `minApp` and whether an iPhone is paired with it. */
+ private async localHostFacts(): Promise<[string | null, boolean | null]> {
+ const local = this.host.local
+ if (!local) return [null, null]
+ const call = async <T>(method: string) => {
+ const res = await fetch(`${local.baseURL}/api/${method}`, {
+ method: 'POST',
+ headers: { Authorization: `Bearer ${local.token}`, 'Content-Type': 'application/json' },
+ body: '{}',
+ signal: AbortSignal.timeout(5000),
+ })
+ return (await res.json()) as T
+ }
+ try {
+ const [hello, devices] = await Promise.all([call<{ minApp?: string }>('hello'), call<{ devices: { platform?: string }[] }>('devices')])
+ return [hello.minApp ?? null, devices.devices.some((d) => d.platform === 'ios')]
+ } catch {
+ return [null, null]
+ }
+ }
+
+ /** Waits until the app is in the background, nobody has typed for ten minutes and no bot is working. */
+ private installWhenIdle() {
+ const timer = setInterval(async () => {
+ if (!this.state.staged) return clearInterval(timer)
+ if (!this.state.autoDownload || BrowserWindow.getFocusedWindow() || powerMonitor.getSystemIdleTime() < 600) return
+ if (!(await this.host.isIdleForUpdate())) return
+ clearInterval(timer)
+ void this.install()
+ }, 30_000)
+ }
+
+ private prepare(): Promise<boolean> {
+ if (this.prepared) return Promise.resolve(true)
+ this.preparing ??= (async () => {
+ try {
+ await this.host.prepareForUpdate()
+ this.prepared = true
+ this.set({ error: null })
+ return true
+ } catch (error) {
+ this.set({ error: `Update paused: ${error instanceof Error ? error.message : error}` })
+ this.host.resumeAfterCancelledUpdate()
+ return false
+ } finally {
+ this.preparing = null
+ }
+ })()
+ return this.preparing
+ }
+
+ private async install() {
+ if (await this.prepare()) autoUpdater.quitAndInstall(true, true)
+ }
+}
+
+/** The iPhone app's version live in the App Store (iTunes lookup); null when unknown. */
+async function appStoreVersion(): Promise<string | null> {
+ try {
+ const res = await fetch('https://itunes.apple.com/lookup?id=6760984418', { signal: AbortSignal.timeout(10_000), cache: 'no-store' })
+ const json = (await res.json()) as { results?: { version?: string }[] }
+ return json.results?.[0]?.version ?? null
+ } catch {
+ return null
+ }
}
diff --git a/apps/desktop/src/preload/index.ts b/apps/desktop/src/preload/index.ts
index e3c4ce41..0a5f0d70 100644
--- a/apps/desktop/src/preload/index.ts
+++ b/apps/desktop/src/preload/index.ts
@@ -1,4 +1,5 @@
import { contextBridge, ipcRenderer, type IpcRendererEvent } from 'electron'
+import { sshBridge } from './ssh'
import type { CallError, CodyncBridge, HostSnapshot, TraySummary, WindowCommand } from '../shared/ipc'
let streamId = 0
@@ -20,6 +21,7 @@ function listen<T>(channel: string, cb: (value: T) => void) {
const bridge: CodyncBridge = {
platform: process.platform as CodyncBridge['platform'],
appVersion: ipcRenderer.sendSync('app:version') as string,
+ computerName: ipcRenderer.sendSync('app:computerName') as string,
host: {
snapshot: () => ipcRenderer.invoke('host:snapshot') as Promise<HostSnapshot>,
onChange: (cb) => listen('host:change', cb),
@@ -58,6 +60,10 @@ const bridge: CodyncBridge = {
stop: () => ipcRenderer.send('speech:stop'),
onEvent: (cb) => listen('speech:event', cb),
},
+ ssh: sshBridge,
+ cloud: {
+ request: (method, path, body, signed) => ipcRenderer.invoke('cloud:request', method, path, body, signed),
+ },
updates: {
state: () => ipcRenderer.invoke('updates:state'),
onChange: (cb) => listen('updates:change', cb),
@@ -79,6 +85,8 @@ const bridge: CodyncBridge = {
launchAtLogin: () => ipcRenderer.invoke('app:launchAtLogin'),
openSettings: (url) => ipcRenderer.send('app:openSettings', url),
authenticate: (url, scheme) => ipcRenderer.invoke('app:authenticate', url, scheme),
+ setScreenAgent: (on) => ipcRenderer.invoke('screen:setAgent', on),
+ syncScreenAgent: (enabled) => ipcRenderer.invoke('screen:syncAgent', enabled),
},
}
diff --git a/apps/desktop/src/preload/ssh.ts b/apps/desktop/src/preload/ssh.ts
new file mode 100644
index 00000000..4c1f6d88
--- /dev/null
+++ b/apps/desktop/src/preload/ssh.ts
@@ -0,0 +1,18 @@
+import { ipcRenderer, type IpcRendererEvent } from 'electron'
+import type { SSHBridge, SSHState } from '../shared/ssh'
+
+/** `window.codync.ssh`: SSH computers, run by the main process (main/ssh.ts). */
+export const sshBridge: SSHBridge = {
+ state: () => ipcRenderer.invoke('ssh:state') as Promise<SSHState>,
+ onChange(cb) {
+ const handler = (_e: IpcRendererEvent, s: SSHState) => cb(s)
+ ipcRenderer.on('ssh:change', handler)
+ return () => void ipcRenderer.removeListener('ssh:change', handler)
+ },
+ save: (profile) => ipcRenderer.invoke('ssh:save', profile) as Promise<string | null>,
+ remove: (id) => ipcRenderer.send('ssh:remove', id),
+ connect: (id) => ipcRenderer.send('ssh:connect', id),
+ disconnect: (id) => ipcRenderer.send('ssh:disconnect', id),
+ trustHostKey: (id) => ipcRenderer.send('ssh:trustHostKey', id),
+ chooseKey: () => ipcRenderer.invoke('ssh:chooseKey') as Promise<string | null>,
+}
diff --git a/apps/desktop/src/renderer/components/symbols.json b/apps/desktop/src/renderer/components/symbols.json
index 0a0fa609..00cea764 100644
--- a/apps/desktop/src/renderer/components/symbols.json
+++ b/apps/desktop/src/renderer/components/symbols.json
@@ -21,6 +21,7 @@
"book",
"book.closed",
"book.pages",
+"brain",
"bubble.left",
"bubble.left.and.bubble.right",
"calendar",
@@ -76,6 +77,7 @@
"hand.point.up.left",
"hand.raised",
"hand.raised.slash",
+"hourglass",
"info.circle",
"iphone",
"key",
diff --git a/apps/desktop/src/renderer/lib/observable.ts b/apps/desktop/src/renderer/lib/observable.ts
index fb880178..7bdf120b 100644
--- a/apps/desktop/src/renderer/lib/observable.ts
+++ b/apps/desktop/src/renderer/lib/observable.ts
@@ -2,27 +2,27 @@ import { useCallback, useRef, useSyncExternalStore } from 'react'
/** A model object views follow: `changed()` re-renders every view that uses it. */
export class Observable {
- private listeners = new Set<() => void>()
- private version = 0
- private pending = false
+ #listeners = new Set<() => void>()
+ #version = 0
+ #scheduled = false
/** Coalesced to one notification per microtask, like SwiftUI's batched updates. */
protected changed() {
- if (this.pending) return
- this.pending = true
+ if (this.#scheduled) return
+ this.#scheduled = true
queueMicrotask(() => {
- this.pending = false
- this.version++
- for (const listener of [...this.listeners]) listener()
+ this.#scheduled = false
+ this.#version++
+ for (const listener of [...this.#listeners]) listener()
})
}
subscribe = (listener: () => void) => {
- this.listeners.add(listener)
- return () => void this.listeners.delete(listener)
+ this.#listeners.add(listener)
+ return () => void this.#listeners.delete(listener)
}
- getVersion = () => this.version
+ getVersion = () => this.#version
}
/** Re-renders the component when `model` changes; returns it for convenience. */
diff --git a/apps/desktop/src/renderer/store/app-model.ts b/apps/desktop/src/renderer/store/app-model.ts
index 62f321a9..ab03d06e 100644
--- a/apps/desktop/src/renderer/store/app-model.ts
+++ b/apps/desktop/src/renderer/store/app-model.ts
@@ -3,6 +3,7 @@ import type { AccessRequest, Bot, Computer } from '@shared/models'
import { LoopbackTransport } from '../client/host-client'
import { Observable } from '../lib/observable'
import { BotStore } from './bot-store'
+import { CloudModel } from './cloud'
/** A bot on a computer: bot ids are only unique per computer. */
export interface BotRef {
@@ -42,12 +43,25 @@ export class AppModel extends Observable {
stores = new Map<string, BotStore>()
selection: BotRef | null = null
lastError: string | null = null
+ /** Codync Screen waits for the user to allow it in System Settings → Login Items. */
+ screenNeedsApproval = false
+ screenError: string | null = null
+ private screenSynced = false
/** Approvals closed with "later"; they come back when the request changes (its code arrives). */
private deferred = new Set<string>()
private storeSubscriptions = new Map<string, () => void>()
+ /** The signed-in account's side of this computer (claims, cloud default, account computers). */
+ readonly cloud: CloudModel
+
constructor(private mirrors = true) {
super()
+ this.cloud = new CloudModel(this)
+ }
+
+ setError(message: string | null) {
+ this.lastError = message
+ this.changed()
}
async start() {
@@ -64,7 +78,9 @@ export class AppModel extends Observable {
if (old && old.computerId !== local.computerId) this.detach(old.computerId)
const cached = this.computers.find((c) => c.id === local.computerId)
const computer: Computer = cached ?? { id: local.computerId, name: 'This computer', signKey: '', urls: [] }
- this.attach(computer, new BotStore(computer, true, () => new LoopbackTransport(local.baseURL, local.token)))
+ const store = new BotStore(computer, true, () => new LoopbackTransport(local.baseURL, local.token))
+ this.attach(computer, store)
+ store.subscribe(() => this.syncScreen(store))
} else if (!local && old) {
this.detach(old.computerId)
}
@@ -177,6 +193,30 @@ export class AppModel extends Observable {
return null
}
+ /** Remote screen: starts/stops Codync Screen and tells the host (it only accepts this from the computer itself). */
+ async setRemoteScreen(on: boolean) {
+ const local = this.local
+ if (!local) return
+ try {
+ this.screenNeedsApproval = (await window.codync.app.setScreenAgent(on)).needsApproval
+ await local.setScreenEnabled(on)
+ this.screenError = null
+ } catch (error) {
+ this.screenError = error instanceof Error ? error.message : String(error)
+ }
+ this.changed()
+ }
+
+ /** Once per launch, keeps the agent registered while the host has Remote screen on. */
+ private syncScreen(store: BotStore) {
+ if (this.screenSynced || !store.screen) return
+ this.screenSynced = true
+ void window.codync.app.syncScreenAgent(store.screen.enabled).then((r) => {
+ this.screenNeedsApproval = r.needsApproval
+ this.changed()
+ })
+ }
+
clearErrors() {
this.lastError = null
for (const store of this.stores.values()) store.lastError = null
diff --git a/apps/desktop/src/renderer/store/cloud.ts b/apps/desktop/src/renderer/store/cloud.ts
new file mode 100644
index 00000000..9b18d333
--- /dev/null
+++ b/apps/desktop/src/renderer/store/cloud.ts
@@ -0,0 +1,294 @@
+import type { CloudStatus, ClaimSignature } from '@shared/models'
+import { Observable } from '../lib/observable'
+import { pref } from '../lib/prefs'
+import { account } from './account'
+import type { AppModel } from './app-model'
+import type { BotStore } from './bot-store'
+
+// Wire types of the Codync cloud (`/v1`, cloud/src/api.ts); times are epoch milliseconds.
+
+/** A computer in the account (`GET /v1/computers`). */
+export interface CloudComputer {
+ computerId: string
+ name: string
+ platform?: string | null
+ device?: string | null
+ signKey: string
+ boxKey?: string | null
+ version?: string | null
+ online?: boolean | null
+ lastSeenAt?: number | null
+ claimedAt?: number | null
+ /** `none` · `pending` · `granted`. */
+ access?: string | null
+}
+
+export interface CloudDevice {
+ deviceId: string
+ deviceKey: string
+ name: string
+ platform?: string | null
+ createdAt?: number | null
+ lastUsedAt?: number | null
+ revoked?: boolean | null
+}
+
+export interface CloudGrant {
+ grantId: string
+ deviceId: string
+ deviceName?: string | null
+ platform?: string | null
+ scopes?: string[] | null
+ createdAt?: number | null
+}
+
+export class CloudError extends Error {
+ constructor(
+ readonly status: number,
+ readonly code: string,
+ message: string | null,
+ ) {
+ super(
+ code === 'requestExpired' ? 'The request expired. Ask again.'
+ : code === 'accountDeleted' ? 'This account was deleted.'
+ : code === 'rateLimited' ? 'Too many requests. Try again in a little while.'
+ : (message ?? `Codync cloud error (${code})`),
+ )
+ }
+
+ /** A busy or failing cloud, or a session token that wasn't fresh yet: trying again later settles it. */
+ get isTransient() {
+ return this.status === 0 || this.status >= 500 || this.status === 429 || this.status === 401
+ }
+}
+
+async function request<T>(method: string, path: string, body?: unknown, signed = false): Promise<T> {
+ const res = await window.codync.cloud.request(method, path, body, signed)
+ if (!res.ok) throw new CloudError(res.error.status, res.error.code, res.error.message)
+ return res.value as T
+}
+
+/** Host identities whose cloud default has been applied (or found already on). */
+const cloudDefaultApplied = pref<string[]>('cloudDefaultApplied', [])
+/** The identities this computer claimed: a later key reset can clean them up. */
+const claimedComputerIds = pref<string[]>('claimedComputerIds', [])
+const keptOut = (userId: string) => pref<string[]>(`keptOutOfAccount.${userId}`, [])
+
+/**
+ * The signed-in account's side of this computer (ports of the Mac `HostController` account code
+ * and kit `CloudClient`): registering this device, the account's computers, claiming this
+ * computer's host, and turning its cloud on by default.
+ */
+export class CloudModel extends Observable {
+ computers: CloudComputer[] = []
+ private registeredFor: string | null = null
+ private enablingCloud = false
+ private autoClaiming = false
+
+ constructor(private app: AppModel) {
+ super()
+ account.subscribe(() => this.tick())
+ app.subscribe(() => this.tick())
+ }
+
+ private fail(error: unknown, quiet = false) {
+ if (quiet || (error instanceof CloudError && error.isTransient)) return
+ this.app.setError(error instanceof Error ? error.message : String(error))
+ }
+
+ private get userId() {
+ return account.user?.userId ?? null
+ }
+
+ /** Re-evaluated on every account or store change. */
+ private tick() {
+ const user = this.userId
+ if (!user) {
+ if (this.computers.length) {
+ this.computers = []
+ this.changed()
+ }
+ this.registeredFor = null
+ return
+ }
+ if (this.registeredFor !== user) {
+ this.registeredFor = user
+ // Signed in: make this computer known to the account, then list its computers.
+ void (async () => {
+ try {
+ await request('POST', 'v1/devices', { name: await deviceName(), platform: window.codync.platform === 'darwin' ? 'macos' : 'linux' }, true)
+ } catch (error) {
+ this.fail(error)
+ }
+ await this.refresh()
+ })()
+ }
+ this.applyCloudDefault()
+ this.autoClaim()
+ }
+
+ async refresh() {
+ if (!this.userId) return
+ try {
+ const res = await request<{ computers: CloudComputer[] }>('GET', 'v1/computers', undefined, true)
+ this.computers = res.computers
+ this.changed()
+ } catch (error) {
+ this.fail(error)
+ }
+ }
+
+ /**
+ * The cloud is on by default, so a computer without Tailscale is reachable away from home. It's
+ * turned on once per host identity, the first time it's seen off; after that off stays off.
+ */
+ private applyCloudDefault() {
+ const store = this.app.local
+ if (!account.cloudURL || !store || store.connection.kind !== 'online' || !store.cloud) return
+ const id = store.computer.id
+ if (cloudDefaultApplied.get().includes(id)) return
+ cloudDefaultApplied.set([...cloudDefaultApplied.get(), id])
+ if (store.cloud.enabled || this.enablingCloud) return
+ this.enablingCloud = true
+ void this.enableCloud(store)
+ .catch(() => {})
+ .finally(() => (this.enablingCloud = false))
+ }
+
+ /** Signed in, this computer joins the account on its own, unless the user took it out of it. */
+ private autoClaim() {
+ const user = this.userId
+ const store = this.app.local
+ if (user && store && store.cloud?.owner?.userId === user) claimedComputerIds.set([store.computer.id])
+ const target = this.autoClaimTarget
+ if (!target || this.autoClaiming) return
+ this.autoClaiming = true
+ void (async () => {
+ // A failed join (network hiccup) is retried with backoff for as long as it's still wanted.
+ let delay = 30_000
+ while (!(await this.claim(target, true))) {
+ await new Promise((r) => setTimeout(r, delay))
+ delay = Math.min(delay * 2, 600_000)
+ if (this.autoClaimTarget?.computer.id !== target.computer.id) break
+ }
+ this.autoClaiming = false
+ })()
+ }
+
+ private get autoClaimTarget(): BotStore | null {
+ // Only with the cloud on: joining needs it, and turning it back on is never done silently.
+ const user = this.userId
+ const store = this.app.local
+ if (!user || !store || store.connection.kind !== 'online') return null
+ const status = store.cloud
+ if (!status?.enabled || status.owner || keptOut(user).get().includes(store.computer.id)) return null
+ return store
+ }
+
+ /** §4.2 A: makes a computer this app manages part of the account; its cloud is turned on first. */
+ async claim(store: BotStore, quiet = false): Promise<boolean> {
+ const user = this.userId
+ if (!user || !store.client) {
+ if (!quiet) this.app.setError('Sign in first.')
+ return false
+ }
+ const out = keptOut(user)
+ out.set(out.get().filter((x) => x !== store.computer.id))
+ try {
+ const client = await store.ready()
+ if (!store.cloud?.enabled) await this.enableCloud(store)
+ const challenge = await request<{ claimId: string; nonce: string }>('POST', 'v1/claims', {})
+ const signed = await client.call<ClaimSignature>('claimSign', { claimId: challenge.claimId, nonce: challenge.nonce, userId: user })
+ await request('POST', `v1/claims/${challenge.claimId}/complete`, signed)
+ await this.refresh()
+ if (store === this.app.local) await this.forgetEarlierIdentities(store.computer.id)
+ return true
+ } catch (error) {
+ this.fail(error, quiet)
+ return false
+ }
+ }
+
+ /**
+ * This computer's host got new keys (a reset data folder, a reinstall): the identities claimed
+ * before are dead copies of it, so they leave the account instead of showing up twice.
+ */
+ private async forgetEarlierIdentities(current: string) {
+ const earlier = new Set(claimedComputerIds.get().filter((id) => id !== current))
+ for (const c of this.computers) if (earlier.has(c.computerId) && !c.online) await this.removeFromAccount(c.computerId)
+ claimedComputerIds.set([current])
+ }
+
+ async unclaim(store: BotStore) {
+ const user = this.userId
+ if (user) {
+ const out = keptOut(user)
+ if (!out.get().includes(store.computer.id)) out.set([...out.get(), store.computer.id])
+ }
+ try {
+ await (await store.ready()).call('unclaim', {}, 30_000)
+ await this.refresh()
+ } catch (error) {
+ this.fail(error)
+ }
+ }
+
+ async setCloud(store: BotStore, enabled: boolean) {
+ if (!cloudDefaultApplied.get().includes(store.computer.id)) cloudDefaultApplied.set([...cloudDefaultApplied.get(), store.computer.id])
+ try {
+ if (enabled) await this.enableCloud(store)
+ else await (await store.ready()).call<CloudStatus>('setCloud', { enabled: false }, 30_000)
+ } catch (error) {
+ this.fail(error)
+ }
+ }
+
+ /** A host without a cloud URL of its own gets this app's. */
+ private async enableCloud(store: BotStore) {
+ const url = store.cloud?.url ? null : account.cloudURL
+ if (!store.cloud?.url && !url) throw new CloudError(400, 'badRequest', 'This build of Codync has no cloud to connect to.')
+ return (await store.ready()).call<CloudStatus>('setCloud', { enabled: true, url }, 30_000)
+ }
+
+ async removeFromAccount(computerId: string) {
+ try {
+ await request('DELETE', `v1/computers/${computerId}`)
+ await this.refresh()
+ } catch (error) {
+ this.fail(error)
+ }
+ }
+
+ /** An offline account computer named like one this app uses or sees online: an earlier identity of it. */
+ isOlderCopy(c: CloudComputer) {
+ if (c.online) return false
+ return this.app.computers.some((x) => x.id !== c.computerId && x.name === c.name) ||
+ this.computers.some((o) => o.computerId !== c.computerId && o.name === c.name && (this.app.store(o.computerId) !== null || !!o.online))
+ }
+
+ devices() {
+ return request<{ devices: CloudDevice[] }>('GET', 'v1/devices').then((r) => r.devices)
+ }
+
+ revokeDevice(deviceId: string) {
+ return request('DELETE', `v1/devices/${deviceId}`)
+ }
+
+ grants(computerId: string) {
+ return request<{ grants: CloudGrant[] }>('GET', `v1/computers/${computerId}/grants`).then((r) => r.grants)
+ }
+
+ revokeGrant(computerId: string, grantId: string) {
+ return request('DELETE', `v1/computers/${computerId}/grants/${grantId}`)
+ }
+
+ async renameComputer(computerId: string, name: string) {
+ await request('PATCH', `v1/computers/${computerId}`, { name })
+ await this.refresh()
+ }
+}
+
+/** This computer's name for the account's device list. */
+async function deviceName() {
+ return window.codync.computerName || (window.codync.platform === 'darwin' ? 'Mac' : 'Linux')
+}
diff --git a/apps/desktop/src/renderer/views/ChatWindow.tsx b/apps/desktop/src/renderer/views/ChatWindow.tsx
index 0fc4cf6e..b777356e 100644
--- a/apps/desktop/src/renderer/views/ChatWindow.tsx
+++ b/apps/desktop/src/renderer/views/ChatWindow.tsx
@@ -22,6 +22,7 @@ import { ApprovalSheet } from './settings/ApprovalSheet'
import { UpdateNeededCard } from './UpdateNeededCard'
import { AccountWelcomeView } from './AccountWelcomeView'
import { useTraySummary } from './tray-summary'
+import { useSSHAttachments } from './settings/ssh-model'
import './chat-window.css'
const isMac = window.codync.platform === 'darwin'
@@ -32,6 +33,7 @@ export function ChatWindow() {
const [onboarded, setOnboarded] = usePref(prefs.onboardingDone)
const [confirmReset, setConfirmReset] = useState(false)
useTraySummary(app)
+ useSSHAttachments(app)
const showsChat = app.host.state.kind === 'running' || app.computers.length > 0
useEffect(
@@ -59,7 +61,7 @@ export function ChatWindow() {
return
}
case 'setRemoteScreen':
- void app.local?.setScreenEnabled(c.on).catch((e: unknown) => app.local?.setError(e instanceof Error ? e.message : String(e)))
+ void app.setRemoteScreen(c.on)
return
}
}),
diff --git a/apps/desktop/src/renderer/views/bots/AgentModelPicker.tsx b/apps/desktop/src/renderer/views/bots/AgentModelPicker.tsx
new file mode 100644
index 00000000..6b556f2d
--- /dev/null
+++ b/apps/desktop/src/renderer/views/bots/AgentModelPicker.tsx
@@ -0,0 +1,107 @@
+import { useEffect, useRef, useState } from 'react'
+import { ChoicePicker, IconButton, Pill, Spinner } from '../../components/Controls'
+import { font } from '../../lib/fonts'
+import { useStore } from '../../store/context'
+import { errorText } from './drafts'
+
+/** Model identifiers and display names advertised by the selected computer's ACP agent. */
+interface AgentModels {
+ models: { id: string; name: string; description?: string | null }[]
+ currentModelId?: string | null
+}
+
+/** Models come from the selected computer's agent, never a bundled provider list. */
+export function AgentModelPicker({ backend, selection, onChange }: { backend: string; selection: string | null | undefined; onChange: (model: string | null) => void }) {
+ const store = useStore()
+ const online = store.connection.kind === 'online'
+ const [catalog, setCatalog] = useState<AgentModels | null>(null)
+ const scope = useRef<string | null>(null)
+ const [loading, setLoading] = useState(false)
+ const [failure, setFailure] = useState<string | null>(null)
+ const [retry, setRetry] = useState(0)
+ const custom = backend === 'custom'
+
+ // A different agent starts on its own default model.
+ const lastBackend = useRef(backend)
+ useEffect(() => {
+ if (lastBackend.current === backend) return
+ lastBackend.current = backend
+ onChange(null)
+ }, [backend, onChange])
+
+ useEffect(() => {
+ const key = `${store.computer.id}/${backend}`
+ if (scope.current !== key) {
+ setCatalog(null)
+ scope.current = key
+ }
+ setFailure(null)
+ setLoading(false)
+ if (custom) return
+ const client = store.client
+ if (!online || !client) {
+ setFailure('Connect to this computer to load its models. You can still enter a model ID.')
+ return
+ }
+ let cancelled = false
+ setLoading(true)
+ client
+ .call<AgentModels>('agentModels', { backend }, 300_000)
+ .then((res) => !cancelled && setCatalog(res))
+ .catch((e: unknown) => !cancelled && setFailure(`Couldn't load models: ${errorText(e)}`))
+ .finally(() => !cancelled && setLoading(false))
+ return () => {
+ cancelled = true
+ }
+ }, [store.computer.id, store.client, backend, online, retry, custom])
+
+ // The agent's own "Default (recommended)" entry is the same choice as leaving the model unset,
+ // so it folds into the single Default row instead of showing twice.
+ const agentDefault = catalog?.models.find((m) => m.name.toLowerCase().startsWith('default'))
+ const available = (catalog?.models ?? []).filter((m) => m.id !== agentDefault?.id)
+ const current = available.find((m) => m.id === catalog?.currentModelId)?.name
+ const options = [{ id: '', label: agentDefault?.name ?? (current ? `Default · ${current}` : 'Default') }, ...available.map((m) => ({ id: m.id, label: m.name }))]
+ if (selection && selection !== agentDefault?.id && !available.some((m) => m.id === selection)) options.push({ id: selection, label: selection })
+ const value = selection === agentDefault?.id ? '' : (selection ?? '')
+ const set = (v: string) => onChange(v ? v : null)
+ const empty = catalog?.models.length === 0
+
+ return (
+ <div style={{ display: 'flex', flexDirection: 'column', gap: 6 }}>
+ <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
+ <span style={{ color: 'var(--text)', whiteSpace: 'nowrap' }}>Model</span>
+ <span style={{ flex: 1 }} />
+ <div style={{ display: 'flex', alignItems: 'center', gap: 4, minWidth: 0 }}>
+ {custom ? null : (
+ // Loading and refresh share one fixed slot, so the row never grows for loading.
+ <div style={{ width: 28, height: 28, display: 'grid', placeItems: 'center', flex: 'none' }}>
+ <div style={{ gridArea: '1 / 1', opacity: loading ? 0 : 1, transition: 'opacity var(--fade)', pointerEvents: loading ? 'none' : undefined }}>
+ <IconButton title={failure ? 'Retry model list' : 'Refresh model list'} icon="arrow.clockwise" disabled={loading} onClick={() => setRetry((r) => r + 1)} />
+ </div>
+ {loading ? (
+ <div className="fade-in" style={{ gridArea: '1 / 1', display: 'flex' }} aria-label="Loading available models">
+ <Spinner size={12} />
+ </div>
+ ) : null}
+ </div>
+ )}
+ {custom || (!loading && (!catalog || empty)) ? (
+ <Pill outlined style={{ minWidth: 0, maxWidth: 160 }}>
+ <input value={value} placeholder="Default" spellCheck={false} onChange={(e) => set(e.target.value)} style={{ textAlign: 'right', width: 142, maxWidth: '100%', minWidth: 0, color: 'var(--text)', userSelect: 'text' }} />
+ </Pill>
+ ) : (
+ <div style={{ display: 'flex', minWidth: 0 }}>
+ <ChoicePicker selection={value} options={options} onChange={set} />
+ </div>
+ )}
+ </div>
+ </div>
+ {!custom && !loading && failure ? (
+ <span style={{ ...font('caption'), color: 'var(--warning)' }}>{failure}</span>
+ ) : !custom && !loading && empty ? (
+ <span style={{ ...font('caption'), color: 'var(--secondary)' }}>This agent doesn't advertise a model list. Leave Default or enter a model ID.</span>
+ ) : null}
+ <span style={{ ...font('caption'), color: 'var(--secondary)' }}>Changing the model starts a new agent session. Chat history is kept.</span>
+ </div>
+ )
+}
diff --git a/apps/desktop/src/renderer/views/bots/BotEditorView.tsx b/apps/desktop/src/renderer/views/bots/BotEditorView.tsx
index 1fe3c338..9437b8a7 100644
--- a/apps/desktop/src/renderer/views/bots/BotEditorView.tsx
+++ b/apps/desktop/src/renderer/views/bots/BotEditorView.tsx
@@ -1,7 +1,388 @@
-import type { BotDraft } from '@shared/models'
-export function BotEditorView(_: { draft: BotDraft }) {
- return <div style={{ padding: 24 }}>Bot editor</div>
+import { useCallback, useEffect, useRef, useState } from 'react'
+import { draftOf, type BotDraft, type DirListing } from '@shared/models'
+import { CharacterAvatar } from '../../components/Avatar'
+import { CardSection, ChoicePicker, DropdownMenu, IconButton, Pill, SearchField, Spinner, ValueRow } from '../../components/Controls'
+import { Icon } from '../../components/Icon'
+import { ModalHeader, Sheet, useDismiss } from '../../components/Overlay'
+import { font } from '../../lib/fonts'
+import { AVATAR_COLORS, AVATAR_SHAPES } from '../../lib/theme'
+import { useStore } from '../../store/context'
+import { AgentModelPicker } from './AgentModelPicker'
+import { errorText, fillDefaults, isValid, lastPathComponent, normalized, usePlugins, type Plugins } from './drafts'
+import { MemoryCard } from './MemoryView'
+import { AnchoredPanel, AutoTextArea, Field, SwitchRow } from './parts'
+import './bots.css'
+
+/** Create or edit a bot in a modal: the settings form plus Close / Save. */
+export function BotEditorView({ draft: initial }: { draft: BotDraft }) {
+ const store = useStore()
+ const dismiss = useDismiss()
+ const plugins = usePlugins(store)
+ const isNew = !initial.id
+ const [draft, setDraft] = useState(() => (isNew ? fillDefaults(store, initial, []) : initial))
+ const [saving, setSaving] = useState(false)
+ const [error, setError] = useState<string | null>(null)
+ const online = store.connection.kind === 'online'
+
+ // A new bot's connectors start on once the computer has listed them.
+ useEffect(() => {
+ if (isNew && plugins) setDraft((d) => fillDefaults(store, d, plugins.connectors))
+ }, [isNew, plugins, store])
+
+ const save = () => {
+ if (!isValid(draft) || saving || !online) return
+ setSaving(true)
+ setError(null)
+ store
+ .save(normalized(draft))
+ .then((bot) => {
+ dismiss()
+ if (isNew) store.setSelection(bot.id)
+ })
+ .catch((e: unknown) => setError(errorText(e)))
+ .finally(() => setSaving(false))
+ }
+
+ return (
+ <div style={{ display: 'flex', flexDirection: 'column', height: '100%', background: 'var(--background)' }}>
+ <ModalHeader
+ title={isNew ? 'New bot' : 'Settings'}
+ trailing={saving ? <Spinner /> : <IconButton title={isNew ? 'Create' : 'Save'} icon="checkmark" disabled={!isValid(draft) || saving || !online} onClick={save} />}
+ />
+ <div style={{ flex: 1, minHeight: 0 }}>
+ <BotSettingsForm draft={draft} onChange={setDraft} error={error} plugins={plugins} />
+ </div>
+ </div>
+ )
}
-export function BotSettingsPanel(_: { botId: string }) {
- return <div style={{ padding: 24 }}>Bot settings</div>
+
+/** The desktop inspector next to a conversation: the same form, saved as you edit. */
+export function BotSettingsPanel({ botId }: { botId: string }) {
+ const store = useStore()
+ const plugins = usePlugins(store)
+ const [draft, setDraft] = useState<BotDraft | null>(null)
+ const [error, setError] = useState<string | null>(null)
+ const change = useCallback((update: (d: BotDraft) => BotDraft) => setDraft((d) => (d ? update(d) : d)), [])
+
+ useEffect(() => {
+ const bot = store.bots.get(botId)
+ setDraft(bot ? draftOf(bot) : null)
+ // Only a different bot reloads the form; the host's echo of a save must not undo typing.
+ }, [botId])
+
+ // Autosave a moment after the last change.
+ useEffect(() => {
+ const bot = store.bots.get(botId)
+ if (!draft || !bot || JSON.stringify(draft) === JSON.stringify(draftOf(bot)) || !isValid(draft)) return
+ const timer = setTimeout(() => {
+ store
+ .save(normalized(draft))
+ .then(() => setError(null))
+ .catch((e: unknown) => setError(errorText(e)))
+ }, 600)
+ return () => clearTimeout(timer)
+ }, [draft])
+
+ return draft ? <BotSettingsForm draft={draft} onChange={change} error={error} plugins={plugins} /> : null
+}
+
+/** Bot settings: a big avatar (Grok Bot's), then sections in the Settings style (`CardSection`). */
+function BotSettingsForm({ draft, onChange, error, plugins }: {
+ draft: BotDraft
+ onChange: (update: (d: BotDraft) => BotDraft) => void
+ error: string | null
+ plugins: Plugins | null
+}) {
+ const store = useStore()
+ const [pickingAvatar, setPickingAvatar] = useState(false)
+ const [pickingFolder, setPickingFolder] = useState(false)
+ const avatarRef = useRef<HTMLButtonElement>(null)
+ const set = <K extends keyof BotDraft>(key: K, value: BotDraft[K]) => onChange((d) => ({ ...d, [key]: value }))
+ const setModel = useCallback((model: string | null) => onChange((d) => ({ ...d, model })), [onChange])
+
+ const backends = store.hello?.backends ?? []
+ const backend = backends.find((b) => b.id === draft.backend)
+ const connectors = plugins?.connectors ?? []
+ const skills = plugins?.skills ?? []
+
+ return (
+ <div className="bot-form">
+ <div className="bot-form-content">
+ <div style={{ display: 'flex', justifyContent: 'center' }}>
+ <button ref={avatarRef} className="avatar-button" title="Edit Bot avatar" aria-label="Edit Bot avatar" onClick={() => setPickingAvatar((p) => !p)}>
+ <CharacterAvatar shape={draft.avatarShape} color={draft.avatarColor} size={56} />
+ </button>
+ <AnchoredPanel open={pickingAvatar} onClose={() => setPickingAvatar(false)} anchor={avatarRef}>
+ <div style={{ padding: 14, width: 300 }}>
+ <AvatarPicker shape={draft.avatarShape} color={draft.avatarColor} onShape={(s) => set('avatarShape', s)} onColor={(c) => set('avatarColor', c)} />
+ </div>
+ </AnchoredPanel>
+ </div>
+
+ <CardSection title="Profile">
+ {/* A new bot is named from its first conversations (Grok Bot's flow); rename it any time after. */}
+ {draft.id ? (
+ <Field label="Name">
+ <input className="field-box" value={draft.name} placeholder="Name" onChange={(e) => set('name', e.target.value)} />
+ </Field>
+ ) : null}
+ <Field label="Standing instructions" detail="Rules that always apply. Put task-specific requests in the chat instead.">
+ <AutoTextArea className="field-box" value={draft.description} placeholder="e.g. Reviews PRs. Never pushes without asking." minRows={3} maxRows={8} onChange={(v) => set('description', v)} />
+ </Field>
+ </CardSection>
+
+ <CardSection title="Agent">
+ <ValueRow label="Computer">
+ <span style={{ whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' }}>{store.hostName}</span>
+ </ValueRow>
+ <div style={{ display: 'flex', flexDirection: 'column', gap: 8 }}>
+ <ValueRow label="Agent">
+ <ChoicePicker
+ selection={draft.backend}
+ options={[...backends.map((b) => ({ id: b.id, label: b.available ? b.name : `${b.name} (not installed)` })), { id: 'custom', label: 'Custom command' }]}
+ onChange={(id) => set('backend', id)}
+ />
+ </ValueRow>
+ {backend && !backend.available ? <span style={{ ...font('caption'), color: 'var(--warning)' }}>{backend.installHint}</span> : null}
+ {draft.backend === 'custom' ? (
+ <input
+ className="field-box fade-in"
+ style={font('callout', undefined, 'monospaced')}
+ value={draft.command ?? ''}
+ placeholder="ACP command, e.g. my-agent --acp"
+ spellCheck={false}
+ onChange={(e) => set('command', e.target.value || null)}
+ />
+ ) : null}
+ </div>
+ <AgentModelPicker backend={draft.backend} selection={draft.model} onChange={setModel} />
+ <ValueRow
+ label="Workspace"
+ detail={
+ draft.cwd
+ ? 'This project is the default starting folder. The bot can work elsewhere when you ask.'
+ : 'This bot has its own space for files. It can work in other folders when you ask.'
+ }
+ >
+ <DropdownMenu
+ title={draft.cwd || 'A persistent workspace allocated for this bot'}
+ items={() => [
+ { title: 'Personal workspace', selected: !draft.cwd, action: () => set('cwd', '') },
+ { title: 'Choose project folder…', selected: !!draft.cwd, action: () => setPickingFolder(true) },
+ ]}
+ >
+ <Pill outlined style={{ gap: 6, color: 'var(--text)' }}>
+ <span style={{ whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis', maxWidth: 160 }}>{draft.cwd ? lastPathComponent(draft.cwd) : 'Personal'}</span>
+ <Icon name="chevron.down" size={10} weight="semibold" color="var(--secondary)" />
+ </Pill>
+ </DropdownMenu>
+ </ValueRow>
+ <ValueRow
+ label="Permissions"
+ detail={
+ draft.permission === 'auto'
+ ? "Tool requests are approved automatically. Your agent's own settings (like Claude Code's permission rules) still apply."
+ : "You'll get an approval card and a notification whenever the agent asks."
+ }
+ >
+ <ChoicePicker
+ selection={draft.permission}
+ options={[
+ { id: 'ask', label: 'Ask me' },
+ { id: 'auto', label: 'Approve automatically' },
+ ]}
+ onChange={(id) => set('permission', id)}
+ />
+ </ValueRow>
+ </CardSection>
+
+ <CardSection title="Activity">
+ <SwitchRow title="Notifications" detail="Get notified when this bot finishes or needs you" on={draft.notify ?? true} onChange={(on) => set('notify', on)} />
+ {store.screen ? (
+ <SwitchRow
+ title="Use the computer"
+ detail={
+ store.screen.enabled
+ ? 'Let this bot see the screen and use the mouse and keyboard. You can watch and take over from your phone.'
+ : "Let this bot see the screen and use the mouse and keyboard. Turn on Remote screen in Codync's menu on the computer first."
+ }
+ on={draft.computer ?? false}
+ onChange={(on) => set('computer', on)}
+ />
+ ) : null}
+ </CardSection>
+
+ <PluginToggles
+ title="Connectors"
+ empty="No connectors yet. Add GitHub, Linear, Notion and more from Plugins."
+ items={connectors.map((c) => ({ id: c.id, name: c.name, detail: c.command ?? c.url ?? '' }))}
+ selection={draft.connectors ?? []}
+ onChange={(ids) => set('connectors', ids)}
+ />
+ <PluginToggles
+ title="Skills"
+ empty="No skills yet. Get some from Plugins, or write your own."
+ items={skills.map((s) => ({ id: s.id, name: s.name, detail: s.description }))}
+ selection={draft.skills ?? []}
+ onChange={(ids) => set('skills', ids)}
+ />
+ {draft.id ? <MemoryCard botId={draft.id} /> : null}
+
+ {error ? <span className="fade-in" style={{ ...font('footnote'), color: 'var(--danger)' }}>{error}</span> : null}
+ </div>
+ <Sheet open={pickingFolder} onClose={() => setPickingFolder(false)} width={460} height={520}>
+ <FolderPicker
+ start={draft.cwd || store.hello?.home || null}
+ onPick={(path) => {
+ set('cwd', path)
+ setPickingFolder(false)
+ }}
+ />
+ </Sheet>
+ </div>
+ )
+}
+
+/** A card of on/off switches for the connectors or skills installed on the computer. */
+function PluginToggles({ title, empty, items, selection, onChange }: {
+ title: string
+ empty: string
+ items: { id: string; name: string; detail: string }[]
+ selection: string[]
+ onChange: (ids: string[]) => void
+}) {
+ return (
+ <CardSection title={title}>
+ {items.length ? null : <span style={{ color: 'var(--secondary)' }}>{empty}</span>}
+ {items.map((item) => (
+ <SwitchRow
+ key={item.id}
+ title={item.name}
+ detail={item.detail || null}
+ on={selection.includes(item.id)}
+ onChange={(on) => onChange([...selection.filter((id) => id !== item.id), ...(on ? [item.id] : [])])}
+ />
+ ))}
+ </CardSection>
+ )
+}
+
+function AvatarPicker({ shape, color, onShape, onColor }: { shape: string; color: string; onShape: (s: string) => void; onColor: (c: string) => void }) {
+ return (
+ <div style={{ display: 'flex', flexDirection: 'column', gap: 12 }}>
+ <div className="swatch-row">
+ {AVATAR_SHAPES.map((s) => (
+ <button key={s} className={`swatch ${s === shape ? 'on' : ''}`} aria-label={`${s} shape`} aria-pressed={s === shape} onClick={() => onShape(s)}>
+ <CharacterAvatar shape={s} color={color} size={36} />
+ </button>
+ ))}
+ </div>
+ <div className="swatch-row">
+ {AVATAR_COLORS.map((c) => (
+ <button key={c.id} className={`swatch ${c.id === color ? 'on' : ''}`} aria-label={c.label} aria-pressed={c.id === color} onClick={() => onColor(c.id)}>
+ <span style={{ width: 26, height: 26, borderRadius: '50%', background: c.hex, display: 'block' }} />
+ </button>
+ ))}
+ </div>
+ </div>
+ )
+}
+
+/** Browses folders on the host, drilling down in place. */
+function FolderPicker({ start, onPick }: { start: string | null; onPick: (path: string) => void }) {
+ // The folders opened so far; the last one is shown.
+ const [stack, setStack] = useState<(string | null)[]>([start])
+ const [forward, setForward] = useState(true)
+ const current = stack[stack.length - 1] ?? null
+ return (
+ <div style={{ display: 'flex', flexDirection: 'column', height: '100%', background: 'var(--background)' }}>
+ <div style={{ display: 'flex', alignItems: 'center' }}>
+ {stack.length > 1 ? (
+ <div className="fade-in" style={{ paddingLeft: 10, display: 'flex' }}>
+ <IconButton
+ title="Back"
+ icon="chevron.left"
+ onClick={() => {
+ setForward(false)
+ setStack((s) => s.slice(0, -1))
+ }}
+ />
+ </div>
+ ) : null}
+ <div style={{ flex: 1, minWidth: 0 }}>
+ <ModalHeader title={current ? lastPathComponent(current) : 'Folders'} />
+ </div>
+ </div>
+ <div style={{ flex: 1, minHeight: 0, overflow: 'hidden' }}>
+ <FolderLevel
+ key={stack.length}
+ path={current}
+ forward={forward}
+ onPick={onPick}
+ onOpen={(path) => {
+ setForward(true)
+ setStack((s) => [...s, path])
+ }}
+ />
+ </div>
+ </div>
+ )
+}
+
+/** One folder's subfolders. */
+function FolderLevel({ path, forward, onPick, onOpen }: { path: string | null; forward: boolean; onPick: (path: string) => void; onOpen: (path: string) => void }) {
+ const store = useStore()
+ const [listing, setListing] = useState<DirListing | null>(null)
+ const [error, setError] = useState<string | null>(null)
+ const [filter, setFilter] = useState('')
+
+ useEffect(() => {
+ let live = true
+ store
+ .listDirs(path)
+ .then((l) => live && setListing(l))
+ .catch((e: unknown) => live && setError(errorText(e)))
+ return () => {
+ live = false
+ }
+ }, [store, path])
+
+ const dirs = listing?.dirs.filter((d) => !filter || d.name.toLowerCase().includes(filter.toLowerCase())) ?? []
+
+ return (
+ <div className={`bot-form level-slide ${forward ? 'from-trailing' : 'from-leading'}`}>
+ <div className="bot-form-content" style={{ gap: 14 }}>
+ <SearchField value={filter} onChange={setFilter} />
+ {listing ? (
+ <>
+ <CardSection footer={listing.path}>
+ <button className="press" style={{ display: 'flex', alignItems: 'center', gap: 8, ...font('headline'), color: 'var(--accent)' }} onClick={() => onPick(listing.path)}>
+ <Icon name="checkmark.circle.fill" size={13} />
+ <span style={{ whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' }}>{`Use “${lastPathComponent(listing.path)}”`}</span>
+ </button>
+ </CardSection>
+ {dirs.length ? (
+ <CardSection>
+ {dirs.map((dir) => (
+ <button key={dir.path} className="press" style={{ display: 'flex', alignItems: 'center', gap: 10 }} onClick={() => onOpen(dir.path)}>
+ <span style={{ width: 20, display: 'flex', justifyContent: 'center' }}>
+ <Icon name={dir.isGit ? 'arrow.triangle.branch' : 'folder'} size={12} color={dir.isGit ? 'var(--accent)' : 'var(--secondary)'} />
+ </span>
+ <span style={{ flex: 1, minWidth: 0, color: 'var(--text)', whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' }}>{dir.name}</span>
+ <Icon name="chevron.right" size={10} weight="semibold" color="var(--tertiary)" />
+ </button>
+ ))}
+ </CardSection>
+ ) : null}
+ </>
+ ) : error ? (
+ <span style={{ color: 'var(--danger)' }}>{error}</span>
+ ) : (
+ <div style={{ display: 'flex', justifyContent: 'center' }}>
+ <Spinner size={20} />
+ </div>
+ )}
+ </div>
+ </div>
+ )
}
diff --git a/apps/desktop/src/renderer/views/bots/BotTemplateView.tsx b/apps/desktop/src/renderer/views/bots/BotTemplateView.tsx
index ba76f2b2..16e6bf6b 100644
--- a/apps/desktop/src/renderer/views/bots/BotTemplateView.tsx
+++ b/apps/desktop/src/renderer/views/bots/BotTemplateView.tsx
@@ -1,4 +1,65 @@
+import { useEffect, useMemo, useState } from 'react'
import type { BotDraft } from '@shared/models'
-export function BotTemplateView(_: { draft: BotDraft }) {
- return <div style={{ padding: 24 }}>Template</div>
+import { Icon } from '../../components/Icon'
+import { ModalHeader } from '../../components/Overlay'
+import { font } from '../../lib/fonts'
+import './bots.css'
+
+/** Swift's `JSONEncoder` with `.prettyPrinted, .sortedKeys, .withoutEscapingSlashes`; nil fields are left out. */
+export function encodeTemplate(draft: BotDraft): string {
+ const { id: _id, pinned: _pinned, hidden: _hidden, ...rest } = draft
+ const sorted = Object.fromEntries(
+ Object.entries(rest)
+ .filter(([, v]) => v !== null && v !== undefined)
+ .sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)),
+ )
+ return JSON.stringify(sorted, null, 2).replace(/^(\s*"(?:[^"\\]|\\.)*"): /gm, '$1 : ')
+}
+
+/** A portable settings snapshot, with no conversation identity or runtime state. */
+export function BotTemplateView({ draft }: { draft: BotDraft }) {
+ const encoded = useMemo(() => encodeTemplate(draft), [draft])
+ const [copied, setCopied] = useState(false)
+
+ const copy = () => {
+ void navigator.clipboard.writeText(encoded).then(() => setCopied(true))
+ }
+
+ // Return copies (the default action).
+ useEffect(() => {
+ const onKey = (e: KeyboardEvent) => {
+ if (e.key === 'Enter' && !e.isComposing) {
+ e.preventDefault()
+ copy()
+ }
+ }
+ window.addEventListener('keydown', onKey)
+ return () => window.removeEventListener('keydown', onKey)
+ })
+
+ return (
+ <div style={{ width: 560, height: 540, display: 'flex', flexDirection: 'column', background: 'var(--background)' }}>
+ <ModalHeader title="Create template" />
+ <div style={{ flex: 1, minHeight: 0, display: 'flex', flexDirection: 'column', gap: 20, padding: '0 24px 24px' }}>
+ <span style={{ ...font('subheadline'), color: 'var(--secondary)' }}>{draft.name}</span>
+ <span style={{ ...font('callout'), color: 'var(--secondary)' }}>Copy this bot’s settings to reuse as a template. Conversation history and conversation ID are excluded.</span>
+ <div style={{ flex: 1, minHeight: 0, overflowY: 'auto', background: 'var(--surface)', borderRadius: 12 }}>
+ <pre className="selectable" style={{ ...font(12, 'regular', 'monospaced'), margin: 0, padding: 16, whiteSpace: 'pre-wrap', wordBreak: 'break-word' }}>
+ {encoded}
+ </pre>
+ </div>
+ <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
+ <span style={{ ...font('caption'), color: 'var(--secondary)', flex: 1 }}>Includes workspace preferences and agent configuration.</span>
+ <button
+ className="press"
+ style={{ ...font(13, 'semibold'), display: 'flex', alignItems: 'center', gap: 6, padding: '9px 14px', color: 'var(--on-accent)', background: 'var(--accent-fill)', borderRadius: 9 }}
+ onClick={copy}
+ >
+ <Icon key={copied ? 'checkmark' : 'square.on.square'} name={copied ? 'checkmark' : 'square.on.square'} size={13} weight="semibold" className="fade-in" />
+ {copied ? 'Copied' : 'Copy template'}
+ </button>
+ </div>
+ </div>
+ </div>
+ )
}
diff --git a/apps/desktop/src/renderer/views/bots/GroupEditorView.tsx b/apps/desktop/src/renderer/views/bots/GroupEditorView.tsx
index 3be3179b..ce873fbb 100644
--- a/apps/desktop/src/renderer/views/bots/GroupEditorView.tsx
+++ b/apps/desktop/src/renderer/views/bots/GroupEditorView.tsx
@@ -1,4 +1,143 @@
-import type { Bot } from '@shared/models'
-export function GroupEditorView(_: { group: Bot | null }) {
- return <div style={{ padding: 24 }}>Group editor</div>
+import { useState } from 'react'
+import { folderName, isGroup, type Bot } from '@shared/models'
+import { BotAvatar, GroupAvatar } from '../../components/Avatar'
+import { Button, IconButton, Spinner } from '../../components/Controls'
+import { Icon } from '../../components/Icon'
+import { Dialog, ModalHeader, useDismiss } from '../../components/Overlay'
+import { font } from '../../lib/fonts'
+import { useStore } from '../../store/context'
+import { errorText } from './drafts'
+import { AutoTextArea, BotChip, Field } from './parts'
+import './bots.css'
+
+/**
+ * Create a group chat, or change its name, what it's for and who's in it. Bots are
+ * picked like recipients: chips on top, a search, and the bots not in it yet.
+ */
+export function GroupEditorView({ group, members: initialMembers = [] }: { group: Bot | null; members?: string[] }) {
+ const store = useStore()
+ const dismiss = useDismiss()
+ // The group being edited; null creates one.
+ const groupId = group?.id ?? null
+ const [name, setName] = useState(group?.name ?? '')
+ const [about, setAbout] = useState(group?.description ?? '')
+ const [members, setMembers] = useState<string[]>(group?.members ?? initialMembers)
+ const [query, setQuery] = useState('')
+ const [saving, setSaving] = useState(false)
+ const [error, setError] = useState<string | null>(null)
+ const [confirmDelete, setConfirmDelete] = useState<Bot | null>(null)
+
+ const picked = members.map((id) => store.bots.get(id)).filter((b): b is Bot => !!b)
+ // Bots that can still join, filtered by the search.
+ const q = query.trim().toLowerCase()
+ const candidates = store.roster.filter((b) => !isGroup(b) && !members.includes(b.id) && (!q || b.name.toLowerCase().includes(q)))
+ const isNew = groupId === null
+ const canSave = members.length > 0 && !saving
+ // "Alice, Bob" when no name is typed.
+ const defaultName = picked.length ? picked.map((b) => b.name).join(', ') : 'Name'
+ const current = groupId ? store.bots.get(groupId) : undefined
+
+ const toggle = (id: string) => {
+ setMembers((list) => (list.includes(id) ? list.filter((m) => m !== id) : [...list, id]))
+ setQuery('')
+ }
+
+ const save = () => {
+ setSaving(true)
+ setError(null)
+ const typed = name.trim()
+ const finalName = typed || defaultName
+ const description = about.trim()
+ const done = groupId
+ ? store.updateGroup({ id: groupId, kind: 'group', name: finalName, description, members, pinned: store.bots.get(groupId)?.pinned })
+ : store.createGroup(finalName, description, members)
+ void Promise.resolve(done)
+ .then(() => dismiss())
+ .catch((e: unknown) => setError(errorText(e)))
+ .finally(() => setSaving(false))
+ }
+
+ return (
+ <div style={{ display: 'flex', flexDirection: 'column', height: '100%', background: 'var(--background)' }}>
+ <ModalHeader title={isNew ? 'New group chat' : 'Group chat'} trailing={saving ? <Spinner /> : <IconButton title={isNew ? 'Create' : 'Save'} icon="checkmark" disabled={!canSave} onClick={save} />} />
+ <div style={{ flex: 1, minHeight: 0, overflowY: 'auto' }}>
+ <div style={{ display: 'flex', flexDirection: 'column', gap: 12, padding: 16, ...font('body') }}>
+ <div style={{ display: 'flex', justifyContent: 'center' }}>
+ <GroupAvatar members={picked} size={72} />
+ </div>
+ <Field label={`Bots · ${members.length}`}>
+ <div style={{ display: 'flex', flexDirection: 'column', gap: 8 }}>
+ {picked.length ? (
+ <div className="chip-row">
+ {picked.map((bot) => (
+ <BotChip key={bot.id} bot={bot} onRemove={() => toggle(bot.id)} />
+ ))}
+ </div>
+ ) : null}
+ <input
+ className="field-box"
+ value={query}
+ placeholder={picked.length ? 'Add another bot' : 'Search bots'}
+ onChange={(e) => setQuery(e.target.value)}
+ onKeyDown={(e) => {
+ if (e.key === 'Enter' && !e.nativeEvent.isComposing && candidates[0]) toggle(candidates[0].id)
+ }}
+ />
+ <div style={{ display: 'flex', flexDirection: 'column', gap: 2 }}>
+ {candidates.map((bot) => (
+ <button key={bot.id} className="press pick-row fade-in" aria-label={`Add ${bot.name}`} onClick={() => toggle(bot.id)}>
+ <BotAvatar bot={bot} size={26} animated={false} />
+ <span style={{ flex: 1, minWidth: 0, display: 'flex', flexDirection: 'column', gap: 1 }}>
+ <span style={{ color: 'var(--text)', whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' }}>{bot.name}</span>
+ <span style={{ ...font('caption'), color: 'var(--tertiary)', whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' }}>{folderName(bot)}</span>
+ </span>
+ <Icon name="plus.circle" size={20} color="var(--tertiary)" />
+ </button>
+ ))}
+ </div>
+ </div>
+ <span style={{ ...font('caption'), color: 'var(--tertiary)' }}>Everyone answers in turn unless you @mention someone. Each bot works in its own folder with its own tools.</span>
+ </Field>
+ <Field label="Name">
+ <input className="field-box" value={name} placeholder={defaultName} onChange={(e) => setName(e.target.value)} />
+ </Field>
+ <Field label="About">
+ <AutoTextArea className="field-box" value={about} placeholder="What this group works on (optional)" minRows={2} maxRows={5} onChange={setAbout} />
+ </Field>
+ {error ? <span className="fade-in" style={{ ...font('footnote'), color: 'var(--danger)' }}>{error}</span> : null}
+ {current ? (
+ <div style={{ paddingTop: 8 }}>
+ <Button kind="secondary" onClick={() => setConfirmDelete(current)}>
+ Delete group chat
+ </Button>
+ </div>
+ ) : null}
+ </div>
+ </div>
+ <DeleteBotDialog bot={confirmDelete} onClose={() => setConfirmDelete(null)} onDeleted={dismiss} />
+ </div>
+ )
+}
+
+/** The one "Delete <bot>?" confirmation (kit `deleteBotConfirmation`). */
+function DeleteBotDialog({ bot, onClose, onDeleted }: { bot: Bot | null; onClose: () => void; onDeleted: () => void }) {
+ const store = useStore()
+ return (
+ <Dialog
+ open={bot !== null}
+ title={`Delete ${bot?.name ?? 'bot'}?`}
+ message={bot && isGroup(bot) ? 'Its bots and their own chats stay.' : 'Files it changed on your computer stay as they are.'}
+ actions={[
+ {
+ title: 'Delete bot and its conversation',
+ destructive: true,
+ action: () => {
+ if (bot) store.delete(bot)
+ onDeleted()
+ },
+ },
+ ]}
+ onClose={onClose}
+ />
+ )
}
diff --git a/apps/desktop/src/renderer/views/bots/MemoryView.tsx b/apps/desktop/src/renderer/views/bots/MemoryView.tsx
new file mode 100644
index 00000000..be8595f5
--- /dev/null
+++ b/apps/desktop/src/renderer/views/bots/MemoryView.tsx
@@ -0,0 +1,87 @@
+import { useCallback, useEffect, useState } from 'react'
+import { CardSection } from '../../components/Controls'
+import { Icon } from '../../components/Icon'
+import { Dialog } from '../../components/Overlay'
+import { useStore } from '../../store/context'
+import './bots.css'
+
+/** One remembered fact (host `memory` API). */
+interface MemoryFact {
+ id: string
+ content: string
+ createdAt: number
+ /** `profile` (who the user is) or `log` (dated history). */
+ kind: string
+}
+
+/**
+ * What the bot remembers about the user, in the bot's settings. Facts are
+ * learned after each exchange and survive new sessions; remove any here.
+ */
+export function MemoryCard({ botId }: { botId: string }) {
+ const store = useStore()
+ const client = store.client
+ const [facts, setFacts] = useState<MemoryFact[]>([])
+ const [loaded, setLoaded] = useState(false)
+ const [confirmClear, setConfirmClear] = useState(false)
+
+ const load = useCallback(async () => {
+ if (!client) return
+ try {
+ setFacts((await client.call<{ location: string; facts: MemoryFact[] }>('memory', { botId })).facts)
+ } catch {
+ setFacts([])
+ }
+ setLoaded(true)
+ }, [client, botId])
+
+ useEffect(() => {
+ void load()
+ }, [load])
+
+ const forget = (fact: MemoryFact) => {
+ setFacts((list) => list.filter((f) => f.id !== fact.id))
+ void client?.call('forgetMemory', { botId, id: fact.id }).catch(() => {}).then(load)
+ }
+
+ const clear = () => {
+ setFacts([])
+ void client?.call('clearMemory', { botId }).catch(() => {}).then(load)
+ }
+
+ return (
+ <>
+ <CardSection
+ title="Memory"
+ accessory={
+ facts.length ? (
+ <button className="press" style={{ display: 'flex', color: 'var(--secondary)' }} title="Forget everything" aria-label="Forget everything" onClick={() => setConfirmClear(true)}>
+ <Icon name="trash" size={12} />
+ </button>
+ ) : null
+ }
+ >
+ {facts.length ? null : (
+ <span style={{ color: 'var(--secondary)' }}>{loaded ? 'Nothing yet. The bot remembers who you are and what you work on as you chat.' : 'Loading…'}</span>
+ )}
+ {facts.map((fact) => (
+ <div key={fact.id} className="fade-in" style={{ display: 'flex', alignItems: 'baseline', gap: 10 }}>
+ <span style={{ display: 'flex' }} title={fact.kind === 'profile' ? 'About you' : 'History'}>
+ <Icon name={fact.kind === 'profile' ? 'person' : 'clock'} size={10} color="var(--tertiary)" />
+ </span>
+ <span className="selectable" style={{ flex: 1, minWidth: 0, color: 'var(--text)' }}>{fact.content}</span>
+ <button className="press" style={{ display: 'flex', color: 'var(--tertiary)' }} title="Forget" aria-label="Forget" onClick={() => forget(fact)}>
+ <Icon name="xmark" size={10} />
+ </button>
+ </div>
+ ))}
+ </CardSection>
+ <Dialog
+ open={confirmClear}
+ title="Forget everything this bot remembers?"
+ actions={[{ title: 'Forget everything', destructive: true, action: clear }]}
+ onClose={() => setConfirmClear(false)}
+ />
+ </>
+ )
+}
diff --git a/apps/desktop/src/renderer/views/bots/NewChatView.tsx b/apps/desktop/src/renderer/views/bots/NewChatView.tsx
index 6d9a8821..5d1cad04 100644
--- a/apps/desktop/src/renderer/views/bots/NewChatView.tsx
+++ b/apps/desktop/src/renderer/views/bots/NewChatView.tsx
@@ -1,3 +1,220 @@
-export function NewChatView(_: { group: boolean; onClose: () => void }) {
- return <div style={{ padding: 24 }}>New chat</div>
+import { useEffect, useLayoutEffect, useRef, useState, type ReactNode } from 'react'
+import { draftOf, isGroup, type Bot } from '@shared/models'
+import { BotAvatar, GroupAvatar } from '../../components/Avatar'
+import { IconButton } from '../../components/Controls'
+import { Icon } from '../../components/Icon'
+import { font } from '../../lib/fonts'
+import { useStore } from '../../store/context'
+import { createDefaultBot, errorText } from './drafts'
+import { AutoTextArea, BotChip } from './parts'
+import './bots.css'
+
+/**
+ * Desktop "new message" page (Grok Bot's compose): a To: field that searches your
+ * bots or creates a new one, with ⌘1…⌘9 shortcuts, and the message box underneath.
+ * Picked bots become chips in the To: field; one opens its chat, several start a
+ * group chat with them (or open the one they already share). Whatever you typed is sent.
+ * `group` is "New group chat": the same page, but it only gathers bots and needs two.
+ */
+export function NewChatView({ group, onClose }: { group: boolean; onClose: () => void }) {
+ const store = useStore()
+ const [query, setQuery] = useState('')
+ const [draft, setDraft] = useState('')
+ const [recipients, setRecipients] = useState<string[]>([])
+ const [creating, setCreating] = useState(false)
+ const toField = useRef<HTMLInputElement>(null)
+ const chips = useRef<HTMLDivElement>(null)
+
+ // Bots not picked yet; a group can only be opened on its own, so groups go once someone's picked.
+ const q = query.trim().toLowerCase()
+ const matches = store.roster.filter((b) => !recipients.includes(b.id) && !(isGroup(b) && (group || recipients.length > 0)) && (!q || b.name.toLowerCase().includes(q)))
+ const picked = recipients.map((id) => store.bots.get(id)).filter((b): b is Bot => !!b)
+
+ const toPrompt = group ? (recipients.length < 2 ? 'Add bots to the group' : 'Add another bot') : recipients.length ? 'Add another bot' : 'Search or create bots'
+ const placeholder = group
+ ? picked.length < 2
+ ? 'Pick at least two bots'
+ : 'Message the group'
+ : picked.length === 0
+ ? 'Message Bot'
+ : `Message ${picked.map((b) => b.name).join(', ')}`
+
+ useEffect(() => {
+ toField.current?.focus()
+ }, [])
+
+ // The field stays in view as chips are added.
+ useLayoutEffect(() => {
+ const el = chips.current
+ if (el) el.scrollTo({ left: el.scrollWidth, behavior: 'smooth' })
+ }, [recipients])
+
+ const open = (id: string) => {
+ const text = draft.trim()
+ store.setSelection(id)
+ if (text) store.send(text, id)
+ onClose()
+ }
+
+ // A bot becomes a chip; a group (with nobody picked) opens right away.
+ const choose = (bot: Bot) => {
+ if (isGroup(bot)) return open(bot.id)
+ setRecipients((list) => [...list, bot.id])
+ setQuery('')
+ toField.current?.focus()
+ }
+
+ const remove = (id: string) => setRecipients((list) => list.filter((r) => r !== id))
+
+ // One bot: its chat. Several: their group chat.
+ const start = () => {
+ if (recipients.length === 0) return
+ if (recipients.length === 1) {
+ if (!group) open(recipients[0]!)
+ return
+ }
+ store
+ .createGroup(picked.map((b) => b.name).join(', '), '', recipients)
+ .then((g) => open(g.id))
+ .catch((e: unknown) => store.setError(errorText(e)))
+ }
+
+ const create = () => {
+ if (creating) return
+ setCreating(true)
+ const name = query.trim()
+ const alone = recipients.length === 0 && !group
+ void (async () => {
+ try {
+ let bot = await createDefaultBot(store)
+ if (name) bot = await store.save({ ...draftOf(bot), name })
+ if (alone) {
+ open(bot.id)
+ } else {
+ // `createDefaultBot` selected it; stay here and add it to the others.
+ store.setSelection(null)
+ choose(bot)
+ }
+ } catch (e) {
+ store.setError(errorText(e))
+ }
+ setCreating(false)
+ })()
+ }
+
+ // Return in To: picks the top match, or with the field empty, starts the chat.
+ const submitTo = () => {
+ if (q) {
+ if (matches[0]) choose(matches[0])
+ else create()
+ } else {
+ start()
+ }
+ }
+
+ // ⌘1 creates, ⌘2…⌘9 pick the matches; Escape closes.
+ const keys = useRef({ create, choose, matches, onClose })
+ keys.current = { create, choose, matches, onClose }
+ useEffect(() => {
+ const onKey = (e: KeyboardEvent) => {
+ if (e.key === 'Escape') {
+ e.preventDefault()
+ keys.current.onClose()
+ return
+ }
+ if (!e.metaKey || e.shiftKey || e.altKey || e.ctrlKey) return
+ const n = Number(e.key)
+ if (!Number.isInteger(n) || n < 1 || n > 9) return
+ e.preventDefault()
+ if (n === 1) keys.current.create()
+ else {
+ const bot = keys.current.matches[n - 2]
+ if (bot) keys.current.choose(bot)
+ }
+ }
+ window.addEventListener('keydown', onKey)
+ return () => window.removeEventListener('keydown', onKey)
+ }, [])
+
+ return (
+ <div style={{ display: 'flex', flexDirection: 'column', height: '100%', background: 'var(--background)' }}>
+ <div style={{ display: 'flex', alignItems: 'center', gap: 8, padding: '12px 22px', ...font('title3') }}>
+ <span style={{ color: 'var(--secondary)', flex: 'none' }}>To:</span>
+ <div ref={chips} className="chip-row" style={{ flex: 1 }}>
+ {picked.map((bot) => (
+ <BotChip key={bot.id} bot={bot} onRemove={() => remove(bot.id)} />
+ ))}
+ <input
+ ref={toField}
+ value={query}
+ placeholder={toPrompt}
+ style={{ minWidth: 200, flex: 1, color: 'var(--text)', userSelect: 'text' }}
+ onChange={(e) => setQuery(e.target.value)}
+ onKeyDown={(e) => {
+ if (e.nativeEvent.isComposing || e.keyCode === 229) return
+ if (e.key === 'Enter') {
+ e.preventDefault()
+ submitTo()
+ } else if (e.key === 'Backspace' && !query && recipients.length) {
+ // Backspace in an empty field takes the last chip back out.
+ e.preventDefault()
+ remove(recipients[recipients.length - 1]!)
+ }
+ }}
+ />
+ </div>
+ <IconButton title="Close" icon="xmark" onClick={onClose} />
+ </div>
+ <div className="hairline" />
+
+ <div className="compose-list">
+ <PickRow shortcut={1} onClick={create} icon={
+ <span style={{ width: 26, height: 26, borderRadius: '50%', background: 'var(--bubble-agent)', display: 'grid', placeItems: 'center' }}>
+ <Icon key={creating ? 'hourglass' : 'plus'} name={creating ? 'hourglass' : 'plus'} size={13} weight="medium" className="fade-in" />
+ </span>
+ }>
+ {query ? `Create “${query}”` : 'Create new Bot'}
+ </PickRow>
+ {matches.map((bot, i) => (
+ <PickRow key={bot.id} shortcut={i + 2 <= 9 ? i + 2 : null} onClick={() => choose(bot)} icon={isGroup(bot) ? <GroupAvatar members={store.members(bot)} size={26} animated={false} /> : <BotAvatar bot={bot} size={26} animated={false} />}>
+ {bot.name}
+ </PickRow>
+ ))}
+ </div>
+
+ <div style={{ flex: 1 }} />
+
+ <div className="compose-box">
+ <AutoTextArea
+ value={draft}
+ onChange={setDraft}
+ placeholder={placeholder}
+ minRows={1}
+ maxRows={6}
+ onKeyDown={(e) => {
+ // Return sends, Shift-Return adds a line; an input method's Return picks its candidate.
+ if (e.key === 'Enter' && !e.shiftKey && !e.nativeEvent.isComposing && e.keyCode !== 229) {
+ e.preventDefault()
+ start()
+ }
+ }}
+ />
+ </div>
+ </div>
+ )
+}
+
+function PickRow({ shortcut, onClick, icon, children }: { shortcut: number | null; onClick: () => void; icon: ReactNode; children: ReactNode }) {
+ return (
+ <button className="press pick-row hover fade-in" onClick={onClick}>
+ {icon}
+ <span style={{ flex: 1, minWidth: 0, color: 'var(--text)', whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' }}>{children}</span>
+ {shortcut !== null ? (
+ <span style={{ display: 'flex', gap: 3 }}>
+ <span className="key-cap">⌘</span>
+ <span className="key-cap">{shortcut}</span>
+ </span>
+ ) : null}
+ </button>
+ )
}
diff --git a/apps/desktop/src/renderer/views/bots/bots.css b/apps/desktop/src/renderer/views/bots/bots.css
new file mode 100644
index 00000000..945eb81d
--- /dev/null
+++ b/apps/desktop/src/renderer/views/bots/bots.css
@@ -0,0 +1,163 @@
+/* Bot creation and editing (kit Bots/). */
+
+/* A rounded input box (`fieldBox`). */
+.field-box {
+ display: block;
+ width: 100%;
+ padding: 8px 10px;
+ background: var(--background);
+ border-radius: 12px;
+ color: var(--text);
+ user-select: text;
+ cursor: text;
+}
+textarea.field-box {
+ line-height: 1.3;
+}
+
+.bot-form {
+ height: 100%;
+ overflow-y: auto;
+ background: var(--background);
+}
+.bot-form-content {
+ display: flex;
+ flex-direction: column;
+ gap: 24px;
+ padding: 14px;
+ font-size: calc(12px * var(--scale));
+}
+
+.avatar-button {
+ padding: 12px;
+ background: var(--bubble-agent);
+ border-radius: 20px;
+ display: flex;
+}
+
+/* A selectable avatar shape or color, ringed when chosen. */
+.swatch {
+ padding: 4px;
+ border-radius: 50%;
+ flex: none;
+ display: flex;
+ box-shadow: inset 0 0 0 2px transparent;
+ transition: box-shadow var(--hover);
+}
+.swatch.on {
+ box-shadow: inset 0 0 0 2px var(--accent);
+}
+.swatch-row {
+ display: flex;
+ gap: 10px;
+ padding: 4px;
+ overflow-x: auto;
+}
+
+/* A picked bot: To: field and group editor. */
+.bot-chip {
+ display: inline-flex;
+ align-items: center;
+ gap: 6px;
+ padding: 5px 6px 5px 8px;
+ background: var(--bubble-agent);
+ border-radius: 999px;
+ flex: none;
+ animation: chip-in var(--layout) both;
+}
+@keyframes chip-in {
+ from {
+ opacity: 0;
+ transform: scale(0.85);
+ }
+}
+.chip-row {
+ display: flex;
+ align-items: center;
+ gap: 6px;
+ overflow-x: auto;
+ min-width: 0;
+}
+
+/* Rows in the new message list and the group editor. */
+.pick-row {
+ display: flex;
+ align-items: center;
+ gap: 12px;
+ width: 100%;
+ padding: 8px 12px;
+ border-radius: 12px;
+ transition: background-color var(--hover), transform var(--press);
+}
+.pick-row.hover:hover {
+ background: var(--bubble-agent);
+}
+.key-cap {
+ min-width: 18px;
+ min-height: 18px;
+ display: grid;
+ place-items: center;
+ border-radius: 4px;
+ background: var(--background);
+ color: var(--secondary);
+ font-family: var(--mono);
+ font-size: calc(10px * var(--scale));
+}
+
+.compose-list {
+ max-height: 460px;
+ overflow-y: auto;
+ background: var(--surface);
+ border-radius: 18px;
+ box-shadow: 0 6px 32px rgba(0, 0, 0, 0.08);
+ max-width: 620px;
+ margin: 10px 22px 0;
+ padding: 8px;
+ display: flex;
+ flex-direction: column;
+ gap: 2px;
+}
+.compose-box {
+ display: flex;
+ align-items: flex-end;
+ margin: 16px;
+ padding: 6px 6px 6px 18px;
+ background: var(--bubble-user);
+ border-radius: 24px;
+ box-shadow: 0 2px 20px rgba(0, 0, 0, 0.06);
+}
+.compose-box textarea {
+ flex: 1;
+ min-width: 0;
+ font-size: var(--conversation);
+ line-height: calc(round(up, calc(var(--conversation) * 1.25), 1px));
+ padding: 11px 0;
+ user-select: text;
+ cursor: text;
+}
+
+/* Folder levels slide in from the side they were opened toward. */
+.level-slide {
+ animation: level-in var(--layout) both;
+}
+.level-slide.from-trailing {
+ --from: 40px;
+}
+.level-slide.from-leading {
+ --from: -40px;
+}
+@keyframes level-in {
+ from {
+ opacity: 0;
+ transform: translateX(var(--from));
+ }
+}
+
+.fade-in {
+ animation: fade-in var(--fade) both;
+}
+@keyframes fade-in {
+ from {
+ opacity: 0;
+ }
+}
diff --git a/apps/desktop/src/renderer/views/bots/drafts.ts b/apps/desktop/src/renderer/views/bots/drafts.ts
new file mode 100644
index 00000000..92af5de5
--- /dev/null
+++ b/apps/desktop/src/renderer/views/bots/drafts.ts
@@ -0,0 +1,86 @@
+import { useEffect, useState } from 'react'
+import type { Bot, BotDraft } from '@shared/models'
+import { AVATAR_COLORS, AVATAR_SHAPES } from '../../lib/theme'
+import type { BotStore } from '../../store/bot-store'
+
+/** An installed connector (host `connectors`). */
+export interface InstalledConnector {
+ id: string
+ name: string
+ description: string
+ kind: string
+ command?: string | null
+ url?: string | null
+}
+
+/** An installed skill (host `skills`). */
+export interface InstalledSkill {
+ id: string
+ name: string
+ description: string
+}
+
+export interface Plugins {
+ connectors: InstalledConnector[]
+ skills: InstalledSkill[]
+}
+
+const pick = <T>(list: T[]) => list[Math.floor(Math.random() * list.length)]!
+
+/** A fresh draft: a random look, Claude, a personal workspace, automatic approvals. */
+export function newDraft(): BotDraft {
+ return { name: '', description: '', avatarColor: pick(AVATAR_COLORS).id, avatarShape: pick(AVATAR_SHAPES), backend: 'claude', cwd: '', permission: 'auto' }
+}
+
+/** A new bot may go without a name: the host names it after its first conversations. */
+export const isValid = (d: BotDraft) => !d.id ||d.name.trim() !== ''
+
+/** What the host should get: no stale command unless the agent is custom. */
+export const normalized = (d: BotDraft): BotDraft => (d.backend === 'custom' ? d : { ...d, command: null })
+
+/** Picks an installed agent; empty cwd requests a personal workspace from the host. */
+export function fillDefaults(store: BotStore, draft: BotDraft, connectors: InstalledConnector[]): BotDraft {
+ const next = { ...draft }
+ const backends = store.hello?.backends ?? []
+ const first = backends.find((b) => b.available)
+ if (backends.find((b) => b.id === next.backend)?.available !== true && first) next.backend = first.id
+ // Connectors start on; left unset, the host turns on every one.
+ if (next.connectors === undefined && connectors.length) next.connectors = connectors.map((c) => c.id)
+ return next
+}
+
+/** The connectors and skills installed on the computer (kit `refreshPlugins`). */
+export async function loadPlugins(store: BotStore): Promise<Plugins> {
+ const client = store.client
+ if (!client) return { connectors: [], skills: [] }
+ const [c, s] = await Promise.allSettled([
+ client.call<{ items: InstalledConnector[] }>('connectors'),
+ client.call<{ items: InstalledSkill[] }>('skills'),
+ ])
+ return { connectors: c.status === 'fulfilled' ? c.value.items : [], skills: s.status === 'fulfilled' ? s.value.items : [] }
+}
+
+export function usePlugins(store: BotStore): Plugins | null {
+ const [plugins, setPlugins] = useState<Plugins | null>(null)
+ const online = store.connection.kind === 'online'
+ useEffect(() => {
+ let live = true
+ void loadPlugins(store).then((p) => live && setPlugins(p))
+ return () => {
+ live = false
+ }
+ }, [store, online])
+ return plugins
+}
+
+/** Creates an unnamed bot with sensible defaults and opens it (desktop compose flow). */
+export async function createDefaultBot(store: BotStore): Promise<Bot> {
+ const { connectors } = await loadPlugins(store)
+ const bot = await store.save(fillDefaults(store, newDraft(), connectors))
+ store.setSelection(bot.id)
+ return bot
+}
+
+export const lastPathComponent = (path: string) => path.split('/').filter(Boolean).pop() ?? path
+
+export const errorText = (e: unknown) => (e instanceof Error ? e.message : String(e))
diff --git a/apps/desktop/src/renderer/views/bots/parts.tsx b/apps/desktop/src/renderer/views/bots/parts.tsx
new file mode 100644
index 00000000..b658d462
--- /dev/null
+++ b/apps/desktop/src/renderer/views/bots/parts.tsx
@@ -0,0 +1,112 @@
+import { useEffect, useLayoutEffect, useRef, useState, type ReactNode, type RefObject } from 'react'
+import { createPortal } from 'react-dom'
+import type { Bot } from '@shared/models'
+import { BotAvatar } from '../../components/Avatar'
+import { Switch } from '../../components/Controls'
+import { Icon } from '../../components/Icon'
+import { usePresence } from '../../components/Overlay'
+import { font } from '../../lib/fonts'
+import './bots.css'
+
+/** A labeled input inside a section row: label (and an optional note) over the field. */
+export function Field({ label, detail, children }: { label: string; detail?: string | null; children: ReactNode }) {
+ return (
+ <div style={{ display: 'flex', flexDirection: 'column', gap: 8 }}>
+ <div style={{ display: 'flex', flexDirection: 'column', gap: 3 }}>
+ <span style={{ color: 'var(--text)' }}>{label}</span>
+ {detail ? <span style={{ ...font('caption'), color: 'var(--secondary)', lineHeight: 1.35 }}>{detail}</span> : null}
+ </div>
+ {children}
+ </div>
+ )
+}
+
+/** An on/off row: title and a short note, the switch on the right. */
+export function SwitchRow({ title, detail, on, onChange }: { title: string; detail?: string | null; on: boolean; onChange: (on: boolean) => void }) {
+ return (
+ <Switch on={on} onChange={onChange}>
+ <span style={{ display: 'flex', flexDirection: 'column', gap: 3 }}>
+ <span style={{ color: 'var(--text)' }}>{title}</span>
+ {detail ? <span style={{ ...font('caption'), color: 'var(--secondary)', lineHeight: 1.35 }}>{detail}</span> : null}
+ </span>
+ </Switch>
+ )
+}
+
+/** A multi-line field that grows with its text between `minRows` and `maxRows` (`axis: .vertical`). */
+export function AutoTextArea({ value, onChange, placeholder, minRows, maxRows, className, onKeyDown }: {
+ value: string
+ onChange: (v: string) => void
+ placeholder?: string
+ minRows: number
+ maxRows: number
+ className?: string
+ onKeyDown?: (e: React.KeyboardEvent<HTMLTextAreaElement>) => void
+}) {
+ const ref = useRef<HTMLTextAreaElement>(null)
+ useLayoutEffect(() => {
+ const el = ref.current
+ if (!el) return
+ const style = getComputedStyle(el)
+ const line = parseFloat(style.lineHeight) || 15
+ const pad = parseFloat(style.paddingTop) + parseFloat(style.paddingBottom)
+ el.style.height = '0px'
+ el.style.height = `${Math.min(Math.max(el.scrollHeight, line * minRows + pad), line * maxRows + pad)}px`
+ }, [value, minRows, maxRows])
+ return <textarea ref={ref} rows={minRows} className={className} value={value} placeholder={placeholder} onChange={(e) => onChange(e.target.value)} onKeyDown={onKeyDown} />
+}
+
+/** A picked bot (To: field, group editor); its x takes it back out. */
+export function BotChip({ bot, onRemove }: { bot: Bot; onRemove: () => void }) {
+ return (
+ <span className="bot-chip">
+ <BotAvatar bot={bot} size={20} animated={false} />
+ <span style={{ ...font('body'), color: 'var(--text)', maxWidth: 220, whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' }}>{bot.name}</span>
+ <button style={{ width: 18, height: 18, display: 'grid', placeItems: 'center' }} aria-label={`Remove ${bot.name}`} title={`Remove ${bot.name}`} onClick={onRemove}>
+ <Icon name="xmark" size={10} weight="semibold" color="var(--secondary)" />
+ </button>
+ </span>
+ )
+}
+
+/** A floating panel next to `anchor` that a click anywhere else closes (kit `AnchoredPanel`). */
+export function AnchoredPanel({ open, onClose, anchor, children }: { open: boolean; onClose: () => void; anchor: RefObject<HTMLElement | null>; children: ReactNode }) {
+ const { mounted, shown } = usePresence(open, 120)
+ const [rect, setRect] = useState<DOMRect | null>(null)
+ useLayoutEffect(() => {
+ if (open && anchor.current) setRect(anchor.current.getBoundingClientRect())
+ }, [open, anchor])
+ useEffect(() => {
+ if (!open) return
+ const onKey = (e: KeyboardEvent) => {
+ if (e.key !== 'Escape') return
+ e.preventDefault()
+ e.stopPropagation()
+ onClose()
+ }
+ window.addEventListener('keydown', onKey, true)
+ return () => window.removeEventListener('keydown', onKey, true)
+ }, [open, onClose])
+ if (!mounted || !rect) return null
+ const W = window.innerWidth
+ const H = window.innerHeight
+ const below = H - rect.bottom >= rect.top
+ const leading = rect.left + rect.width / 2 < W * 0.6
+ const inset = Math.max(8, W - Math.min(320, W - 16) - 8)
+ return createPortal(
+ <div className={`anchored ${shown ? 'shown' : ''}`} onMouseDown={(e) => e.target === e.currentTarget && onClose()}>
+ <div
+ className="anchored-panel"
+ style={{
+ position: 'absolute',
+ [below ? 'top' : 'bottom']: below ? rect.bottom + 6 : H - rect.top + 6,
+ ...(leading ? { left: Math.min(Math.max(8, rect.left), inset) } : { right: Math.min(Math.max(8, W - rect.right), inset) }),
+ transformOrigin: `${below ? 'top' : 'bottom'} ${leading ? 'left' : 'right'}`,
+ }}
+ >
+ {children}
+ </div>
+ </div>,
+ document.body,
+ )
+}
diff --git a/apps/desktop/src/renderer/views/call/CallView.tsx b/apps/desktop/src/renderer/views/call/CallView.tsx
index 2386244e..9a1ee33a 100644
--- a/apps/desktop/src/renderer/views/call/CallView.tsx
+++ b/apps/desktop/src/renderer/views/call/CallView.tsx
@@ -1,3 +1,272 @@
-export function CallView(_: { botId: string; onSpeaking: (s: boolean) => void; interrupt: { current: (() => void) | null }; onEnd: () => void }) {
- return <div>Call</div>
+import { isWorking } from '@shared/models'
+import { useEffect, useRef, useState } from 'react'
+import { BotAvatar } from '../../components/Avatar'
+import { Icon } from '../../components/Icon'
+import { Sheet } from '../../components/Overlay'
+import { font } from '../../lib/fonts'
+import { useModel } from '../../lib/observable'
+import { avatarColor, useDark, useReduceMotion, withAlpha } from '../../lib/theme'
+import { useStore } from '../../store/context'
+import { VoiceChatSettingsView } from '../settings/VoiceChatSettingsView'
+import { CallOperator } from './call-operator'
+import { CloudSpeechEngine } from './CloudSpeechEngine'
+import { GeminiLiveEngine } from './GeminiLiveEngine'
+import { OnDeviceEngine } from './OnDeviceEngine'
+import { OpenAIRealtimeEngine } from './OpenAIRealtimeEngine'
+import { countVoiceUsage, fromBase64, onDeviceAvailable, onDeviceUnavailableReason, providerName, toBase64, VoiceEngine, VoiceSettings, type VoiceProvider, type VoiceStatus } from './voice'
+import { watchVoiceCall } from './voice-call'
+import './call.css'
+
+/**
+ * A voice call with a bot, as Grok Bot does it: a floating bar over the chat, which stays
+ * readable underneath. What you say goes out as ordinary messages, its final replies are read
+ * aloud, and the chat gets a "Voice chat · 00:16" line when the call ends.
+ */
+export function CallView({ botId, onSpeaking, interrupt, onEnd }: {
+ botId: string
+ onSpeaking: (speaking: boolean) => void
+ interrupt: { current: (() => void) | null }
+ onEnd: () => void
+}) {
+ const store = useStore()
+ const [session, setSession] = useState<VoiceEngine | null>(null)
+ useModel(session)
+ /** One line under the bar, e.g. why the call fell back to another voice. */
+ const [notice, setNotice] = useState<string | null>(null)
+ /** No engine could start at all. */
+ const [blocked, setBlocked] = useState<string | null>(null)
+ const [settings, setSettings] = useState(false)
+ const engine = useRef<VoiceEngine | null>(null)
+ /** When the current cloud engine started, for this month's minutes. */
+ const cloudStartedAt = useRef<number | null>(null)
+ const closed = useRef(false)
+ const bot = store.bots.get(botId)
+ const color = bot ? avatarColor(bot.avatarColor) : null
+
+ const countCloudMinutes = () => {
+ const provider = engine.current?.provider
+ if (!provider || cloudStartedAt.current === null) return
+ countVoiceUsage((Date.now() - cloudStartedAt.current) / 1000, provider)
+ cloudStartedAt.current = null
+ }
+
+ const credential = (provider: VoiceProvider, model: string, voice: string) => async () => {
+ const client = await store.ready()
+ return (await client.call<{ credential: string }>('voiceSession', { provider, model, voice }, 30_000)).credential
+ }
+
+ /** Starts the engine: a realtime provider on the key kept by the computer, or on-device speech. */
+ const begin = (provider: VoiceProvider | null) => {
+ if (closed.current) return
+ if (!provider && !onDeviceAvailable()) return void fallbackFromDevice()
+ const op = new CallOperator(store, botId)
+ const send = (text: string) => store.send(text, botId)
+ let next: VoiceEngine
+ if (provider && VoiceSettings.isSpeechMode(provider)) {
+ const transcribe = VoiceSettings.transcribeModel(provider)
+ const speechModel = VoiceSettings.speechModel(provider)
+ const voice = VoiceSettings.voice(provider)
+ next = new CloudSpeechEngine(provider, {
+ // ponytail: kit turns Simplified Chinese transcripts into Traditional (ICU Hans-Hant); Chromium has no such transform.
+ transcribe: async (audio) =>
+ (await (await store.ready()).call<{ text: string }>('voiceTranscribe', { provider, model: transcribe, audio: toBase64(audio) }, 60_000)).text,
+ speak: async (text) => {
+ const clip = await (await store.ready()).call<{ audio: string; format: string }>('voiceSpeak', { provider, model: speechModel, voice, text }, 90_000)
+ return { audio: fromBase64(clip.audio), format: clip.format }
+ },
+ send,
+ })
+ cloudStartedAt.current = Date.now()
+ } else if (provider) {
+ const model = VoiceSettings.model(provider)
+ const voice = VoiceSettings.voice(provider)
+ next =
+ provider === 'openai'
+ ? new OpenAIRealtimeEngine(credential(provider, model, voice), op)
+ : new GeminiLiveEngine(model, voice, credential(provider, model, voice), op)
+ cloudStartedAt.current = Date.now()
+ } else {
+ next = new OnDeviceEngine(send)
+ }
+ // Picks up the computer's newest models for the next call; this one starts right away.
+ if (provider) void rememberStatus()
+ let stopWatching: (() => void) | null = null
+ next.onActivityChanged = (active) => {
+ if (active && !stopWatching) {
+ stopWatching = watchVoiceCall(store, botId, { speak: (t) => next.speak(t), announce: (t) => next.announce(t) })
+ } else if (!active && stopWatching) {
+ stopWatching()
+ stopWatching = null
+ }
+ }
+ engine.current = next
+ setSession(next)
+ void next.start()
+ }
+
+ const rememberStatus = async () => {
+ try {
+ const status = await (await store.ready()).call<VoiceStatus>('voiceStatus')
+ VoiceSettings.remember(status)
+ return status
+ } catch {
+ return null
+ }
+ }
+
+ /** No on-device speech here (Linux, or a build without the helper): use a provider with a key, if any. */
+ const fallbackFromDevice = async () => {
+ const configured = (await rememberStatus())?.providers.find((p) => p.configured)?.provider
+ if (closed.current) return
+ if (configured) {
+ setNotice(`${onDeviceUnavailableReason()} Using ${providerName(configured)}.`)
+ begin(configured)
+ } else {
+ setBlocked(`${onDeviceUnavailableReason()} Add an OpenAI or Gemini key in call settings.`)
+ }
+ }
+
+ useEffect(() => {
+ const startedAt = Date.now()
+ begin(VoiceSettings.provider())
+ return () => {
+ closed.current = true
+ engine.current?.end()
+ countCloudMinutes()
+ onSpeaking(false)
+ interrupt.current = null
+ store.logCall(botId, Math.floor((Date.now() - startedAt) / 1000))
+ }
+ }, [])
+
+ const phase = session?.phase ?? null
+ useEffect(() => {
+ if (!session) return
+ onSpeaking(phase === 'speaking')
+ interrupt.current = phase === 'speaking' ? () => session.interrupt() : null
+ // A cloud engine that can't start or drops mid-call hands over to on-device voice.
+ if (phase === 'failed' && session.provider && onDeviceAvailable()) {
+ countCloudMinutes()
+ setNotice(`${providerName(session.provider)} isn't available (${session.failure}). Using on-device voice.`)
+ begin(null)
+ }
+ }, [session, phase])
+
+ const speaking = phase === 'speaking'
+ const muted = session?.muted === true
+ const working = bot ? isWorking(bot) : false
+ const statusText =
+ phase === null || phase === 'starting'
+ ? 'Connecting'
+ : phase === 'speaking'
+ ? 'Speaking'
+ : phase === 'failed'
+ ? "Can't listen"
+ : muted
+ ? 'Muted'
+ : working
+ ? 'Working, listening'
+ : 'Listening'
+ const failure = blocked ?? (phase === 'failed' ? session?.failure : null)
+
+ return (
+ <div className="call">
+ <div className="call-bar">
+ <button
+ className={`call-avatar ${speaking ? 'speaking' : ''}`}
+ disabled={!speaking}
+ aria-label={speaking ? 'Interrupt' : (bot?.name ?? '')}
+ title={speaking ? 'Interrupt' : undefined}
+ onClick={() => session?.interrupt()}
+ >
+ {bot ? <BotAvatar bot={bot} size={34} /> : <span style={{ width: 34, height: 34 }} />}
+ </button>
+ <CallLevelDots engine={session} working={working} color={color} label={statusText} />
+ {/* Audio leaves the computer: say where. */}
+ {session?.provider ? (
+ <span style={{ ...font('caption2', 'semibold'), color: 'var(--secondary)' }} aria-label={`Voice by ${providerName(session.provider)}`}>
+ {providerName(session.provider)}
+ </span>
+ ) : null}
+ <button className="call-round" title="Call settings" aria-label="Call settings" onClick={() => setSettings(true)}>
+ <Icon name="gearshape" size={16} weight="medium" scaled={false} />
+ </button>
+ <button
+ className={`call-round ${muted ? 'on' : ''}`}
+ title={muted ? 'Unmute' : 'Mute'}
+ aria-label={muted ? 'Unmute' : 'Mute'}
+ onClick={() => session && (session.muted = !session.muted)}
+ >
+ <Icon name={muted ? 'mic.slash' : 'mic'} size={16} weight="medium" scaled={false} />
+ </button>
+ <button className="call-round end" title="End call" aria-label="End call" onClick={onEnd}>
+ <Icon name="xmark" size={16} weight="semibold" scaled={false} />
+ </button>
+ </div>
+ {failure ? (
+ <div key="failure" className="call-line" style={{ color: 'var(--danger)' }}>
+ {failure}
+ </div>
+ ) : notice ? (
+ <div key={notice} className="call-line" style={{ color: 'var(--secondary)' }}>
+ {notice}
+ </div>
+ ) : null}
+ <Sheet open={settings} onClose={() => setSettings(false)}>
+ <VoiceChatSettingsView />
+ </Sheet>
+ </div>
+ )
+}
+
+const DOTS_WIDTH = 120
+const DOTS_HEIGHT = 16
+
+/**
+ * The dotted line in the call bar: it follows your voice while listening, ripples while the bot
+ * talks, breathes while it works, and rests as faint dots otherwise.
+ */
+function CallLevelDots({ engine, working, color, label }: { engine: VoiceEngine | null; working: boolean; color: string | null; label: string }) {
+ const canvas = useRef<HTMLCanvasElement>(null)
+ const dark = useDark()
+ const reduceMotion = useReduceMotion()
+ const live = useRef({ engine, working, color, dark, reduceMotion })
+ live.current = { engine, working, color, dark, reduceMotion }
+
+ useEffect(() => {
+ const el = canvas.current
+ const ctx = el?.getContext('2d')
+ if (!el || !ctx) return
+ const scale = window.devicePixelRatio || 1
+ el.width = DOTS_WIDTH * scale
+ el.height = DOTS_HEIGHT * scale
+ let frame = 0
+ const draw = (now: number) => {
+ const { engine, working, color, dark, reduceMotion } = live.current
+ const t = reduceMotion ? 0 : now / 1000
+ const speaking = engine?.phase === 'speaking'
+ const level = engine?.level ?? 0
+ const accent = dark ? '#ffffff' : '#000000'
+ const tint = speaking ? accent : (color ?? accent)
+ ctx.setTransform(scale, 0, 0, scale, 0, 0)
+ ctx.clearRect(0, 0, DOTS_WIDTH, DOTS_HEIGHT)
+ const spacing = 8
+ const count = Math.max(1, Math.floor(DOTS_WIDTH / spacing))
+ for (let i = 0; i < count; i++) {
+ const x = i * spacing + spacing / 2
+ const wave = (Math.sin(i * 0.55 - t * 6) + 1) / 2
+ const amount = speaking ? 0.5 + 0.5 * wave : level > 0.05 ? Math.max(0.2, level) * (0.45 + 0.55 * wave) : working ? 0.25 * ((Math.sin(t * 2.4) + 1) / 2) : 0.08
+ const height = 4 + amount * (DOTS_HEIGHT - 4)
+ ctx.fillStyle = withAlpha(tint, 0.5 + 0.5 * amount)
+ ctx.beginPath()
+ ctx.roundRect(x - 1.25, (DOTS_HEIGHT - height) / 2, 2.5, height, 1.25)
+ ctx.fill()
+ }
+ frame = requestAnimationFrame(draw)
+ }
+ frame = requestAnimationFrame(draw)
+ return () => cancelAnimationFrame(frame)
+ }, [])
+
+ return <canvas ref={canvas} role="img" aria-label={label} style={{ width: DOTS_WIDTH, height: DOTS_HEIGHT, flex: 'none' }} />
}
diff --git a/apps/desktop/src/renderer/views/call/CloudSpeechEngine.ts b/apps/desktop/src/renderer/views/call/CloudSpeechEngine.ts
new file mode 100644
index 00000000..0d52743e
--- /dev/null
+++ b/apps/desktop/src/renderer/views/call/CloudSpeechEngine.ts
@@ -0,0 +1,256 @@
+import { errorMessage, micPCM16k, SPEECH_CHUNK, spokenText, VoiceEngine, VoiceSettings, wav, type VoiceProvider } from './voice'
+
+export interface CloudSpeech {
+ transcribe: (wav: Uint8Array) => Promise<string>
+ /** Audio and its format (`aac` or `wav`). */
+ speak: (text: string) => Promise<{ audio: Uint8Array; format: string }>
+ send: (text: string) => void
+}
+
+type Clip = { audio: Uint8Array; format: string } | null
+
+const THRESHOLD = 0.3
+const PREROLL_SAMPLES = (16000 * 3) / 10
+/** An utterance has to fit one channel message once base64-encoded. */
+const MAX_SAMPLES = 16000 * 20
+/** Under 0.4 s of voice is a cough or noise (and speech-to-text models invent words for noise). */
+const MIN_VOICED_SAMPLES = (16000 * 4) / 10
+
+/** Sentences grouped into clips of at most `limit` characters (longer sentences are cut). */
+export function clips(text: string, limit: number): string[] {
+ const out: string[] = []
+ let current = ''
+ let sentence = ''
+ const flush = () => {
+ const t = current.trim()
+ if (t) out.push(t)
+ current = ''
+ }
+ const add = (piece: string) => {
+ if ([...current].length + [...piece].length > limit) flush()
+ let rest = [...piece]
+ while (rest.length > limit) {
+ out.push(rest.slice(0, limit).join(''))
+ rest = rest.slice(limit)
+ }
+ current += rest.join('')
+ }
+ for (const ch of text) {
+ sentence += ch
+ if ('.!?。!?\n'.includes(ch)) {
+ add(sentence)
+ sentence = ''
+ }
+ }
+ add(sentence)
+ flush()
+ return out
+}
+
+/**
+ * The provider's speech models without a live conversation: each utterance is cut at a pause,
+ * transcribed with the provider's speech-to-text, and sent as an ordinary message; the bot's
+ * replies are read aloud with its text-to-speech. Both go through the host, which holds the key.
+ */
+export class CloudSpeechEngine extends VoiceEngine {
+ private stopMic: (() => void) | null = null
+ private chunk: number
+
+ // Listening: PCM since speech began (plus a little before), and when it last went quiet.
+ private preroll: Int16Array[] = []
+ private prerollSamples = 0
+ private utterance: Int16Array[] = []
+ private utteranceSamples = 0
+ private speaking = false
+ private quietSince: number | null = null
+ private voiced = 0
+ /** Playback just ended; the room's echo of it isn't speech. */
+ private deafUntil = 0
+ /** Transcriptions finish in the order they were spoken. */
+ private transcribing: Promise<void> = Promise.resolve()
+
+ // Speaking: the reply's clips, fetched one ahead of the one playing.
+ private queue: string[] = []
+ private player: HTMLAudioElement | null = null
+ private next: Promise<Clip> | null = null
+ private speakGeneration = 0
+
+ constructor(
+ readonly provider: VoiceProvider,
+ private speech: CloudSpeech,
+ ) {
+ super()
+ this.chunk = SPEECH_CHUNK[provider]
+ }
+
+ async start() {
+ try {
+ const stop = await micPCM16k((pcm, level) => this.heard(pcm, level))
+ if (this.ended) return stop()
+ this.stopMic = stop
+ } catch (e) {
+ return this.fail(errorMessage(e))
+ }
+ this.setPhase('listening')
+ }
+
+ end() {
+ this.ended = true
+ this.onActivityChanged?.(false)
+ this.stopSpeaking()
+ this.stopMic?.()
+ this.stopMic = null
+ }
+
+ protected mutedChanged(on: boolean) {
+ if (on) this.dropUtterance()
+ }
+
+ interrupt() {
+ if (this.phase !== 'speaking') return
+ this.stopSpeaking()
+ this.setPhase('listening')
+ }
+
+ speak(reply: string) {
+ this.say(spokenText(reply))
+ }
+
+ announce(notice: string) {
+ this.say(notice)
+ }
+
+ // MARK: listening
+
+ private heard(pcm: Int16Array, level: number) {
+ if (this.ended || this.muted || this.phase !== 'listening' || Date.now() < this.deafUntil) return
+ this.level = level
+ if (level > THRESHOLD) {
+ this.speaking = true
+ this.quietSince = null
+ this.voiced += pcm.length
+ } else if (this.speaking && this.quietSince === null) {
+ this.quietSince = Date.now()
+ }
+ if (this.speaking) {
+ this.utterance.push(pcm)
+ this.utteranceSamples += pcm.length
+ const paused = this.quietSince !== null && Date.now() - this.quietSince >= VoiceSettings.pause() * 1000
+ if (paused || this.utteranceSamples >= MAX_SAMPLES) this.submit()
+ } else {
+ this.preroll.push(pcm)
+ this.prerollSamples += pcm.length
+ while (this.preroll.length > 1 && this.prerollSamples - this.preroll[0]!.length >= PREROLL_SAMPLES) {
+ this.prerollSamples -= this.preroll.shift()!.length
+ }
+ }
+ }
+
+ private submit() {
+ const audio = wav([...this.preroll, ...this.utterance])
+ const enough = this.voiced >= MIN_VOICED_SAMPLES
+ this.dropUtterance()
+ if (!enough) return
+ this.transcribing = this.transcribing.then(async () => {
+ try {
+ const text = (await this.speech.transcribe(audio)).trim()
+ if (!this.ended && text) this.speech.send(text)
+ } catch (e) {
+ console.error('transcribe:', errorMessage(e))
+ if (!this.ended) this.fail(errorMessage(e))
+ }
+ })
+ }
+
+ private dropUtterance() {
+ this.preroll = []
+ this.prerollSamples = 0
+ this.utterance = []
+ this.utteranceSamples = 0
+ this.speaking = false
+ this.quietSince = null
+ this.voiced = 0
+ this.level = 0
+ }
+
+ // MARK: speaking
+
+ /** Queues text in clips the provider can turn into one channel message each. */
+ private say(text: string) {
+ if (this.ended || this.phase === 'failed' || this.phase === 'starting' || !text) return
+ this.queue.push(...clips(text, this.chunk))
+ if (!this.player && !this.next) this.playNext()
+ }
+
+ private playNext() {
+ if (this.ended) return
+ const generation = this.speakGeneration
+ const pending = this.next ?? this.fetch(this.queue.shift() ?? null)
+ this.next = null
+ void pending.then((clip) => {
+ if (generation !== this.speakGeneration || this.ended) return
+ if (!clip) return this.finishedSpeaking()
+ this.play(clip)
+ if (this.queue.length) this.next = this.fetch(this.queue.shift()!)
+ })
+ }
+
+ private async fetch(text: string | null): Promise<Clip> {
+ if (text === null) return null
+ try {
+ return await this.speech.speak(text)
+ } catch (e) {
+ console.error('speak:', errorMessage(e))
+ return null
+ }
+ }
+
+ private play(clip: { audio: Uint8Array; format: string }) {
+ const type = clip.format === 'aac' ? 'audio/aac' : clip.format === 'wav' ? 'audio/wav' : `audio/${clip.format}`
+ const url = URL.createObjectURL(new Blob([clip.audio as BlobPart], { type }))
+ const player = new Audio(url)
+ const generation = this.speakGeneration
+ let finished = false
+ const done = () => {
+ if (finished) return
+ finished = true
+ URL.revokeObjectURL(url)
+ this.played(generation)
+ }
+ player.onended = done
+ player.onerror = () => {
+ console.error('play: unreadable clip')
+ done()
+ }
+ this.player = player
+ this.dropUtterance()
+ this.setPhase('speaking')
+ void player.play().catch(() => player.onerror?.(new Event('error')))
+ }
+
+ private played(generation: number) {
+ if (generation !== this.speakGeneration) return
+ this.player = null
+ if (!this.queue.length && !this.next) this.finishedSpeaking()
+ else this.playNext()
+ }
+
+ private finishedSpeaking() {
+ this.player = null
+ this.deafUntil = Date.now() + 400
+ if (this.phase === 'speaking') this.setPhase('listening')
+ }
+
+ private stopSpeaking() {
+ this.speakGeneration++
+ this.queue = []
+ this.next = null
+ if (this.player) {
+ this.player.onended = null
+ this.player.onerror = null
+ this.player.pause()
+ URL.revokeObjectURL(this.player.src)
+ }
+ this.player = null
+ }
+}
diff --git a/apps/desktop/src/renderer/views/call/GeminiLiveEngine.ts b/apps/desktop/src/renderer/views/call/GeminiLiveEngine.ts
new file mode 100644
index 00000000..eaa08604
--- /dev/null
+++ b/apps/desktop/src/renderer/views/call/GeminiLiveEngine.ts
@@ -0,0 +1,280 @@
+import { OPERATOR_TOOLS, type CallOperator } from './call-operator'
+import { errorMessage, fromBase64, micPCM16k, toBase64, VoiceEngine } from './voice'
+
+const URL_BASE = 'wss://generativelanguage.googleapis.com/ws/google.ai.generativelanguage.v1alpha.GenerativeService.BidiGenerateContentConstrained'
+
+/** Gemini's schema spells types in capitals (`OBJECT`, `STRING`). */
+function geminiSchema(schema: Record<string, unknown>): Record<string, unknown> {
+ const out: Record<string, unknown> = {}
+ for (const [key, value] of Object.entries(schema)) {
+ if (key === 'type' && typeof value === 'string') out[key] = value.toUpperCase()
+ else if (key === 'properties' && value && typeof value === 'object')
+ out[key] = Object.fromEntries(Object.entries(value).map(([k, v]) => [k, geminiSchema(v as Record<string, unknown>)]))
+ else out[key] = value
+ }
+ return out
+}
+
+/**
+ * Gemini Live over a WebSocket, this computer ↔ Google directly: 16 kHz PCM up, 24 kHz PCM down,
+ * echo-cancelled so you can talk over it. The user's key stays with the host, which mints one-use
+ * ephemeral tokens; a session the server ends (`goAway`) is resumed with a fresh one.
+ */
+export class GeminiLiveEngine extends VoiceEngine {
+ readonly provider = 'gemini' as const
+ private socket: WebSocket | null = null
+ /** Setup is done on the current socket. */
+ private ready = false
+ private resumeHandle: string | null = null
+ private reconnects = 0
+ private stopMic: (() => void) | null = null
+ private output: AudioContext | null = null
+ private sources = new Set<AudioBufferSourceNode>()
+ private nextStart = 0
+ /** Buffers scheduled and not played yet; bumping the generation drops late callbacks. */
+ private queued = 0
+ private playGeneration = 0
+ /** The model finished its turn (nothing more is coming until we or the user speak). */
+ private turnDone = true
+ /** After a local interrupt, the rest of the turn's audio is dropped. */
+ private dropping = false
+ private pending: string[] = []
+
+ constructor(
+ private model: string,
+ private voice: string,
+ private credential: () => Promise<string>,
+ private op: CallOperator,
+ ) {
+ super()
+ }
+
+ async start() {
+ await this.connect()
+ }
+
+ private async connect() {
+ try {
+ const token = await this.credential()
+ if (this.ended) return
+ const socket = new WebSocket(`${URL_BASE}?access_token=${encodeURIComponent(token)}`)
+ socket.binaryType = 'arraybuffer'
+ this.socket = socket
+ this.ready = false
+ socket.onopen = () => this.sendSetup(socket)
+ socket.onmessage = (e) => {
+ if (this.socket !== socket) return
+ this.received(typeof e.data === 'string' ? e.data : new TextDecoder().decode(e.data as ArrayBuffer))
+ }
+ socket.onclose = (e) => this.closed(socket, e.reason)
+ } catch (e) {
+ if (!this.ended) this.fail(errorMessage(e))
+ }
+ }
+
+ private sendSetup(socket: WebSocket) {
+ const tools = OPERATOR_TOOLS.map((t) => ({
+ name: t.name,
+ description: t.description,
+ ...(Object.keys(t.parameters.properties).length ? { parameters: geminiSchema(t.parameters) } : {}),
+ }))
+ const setup = {
+ model: `models/${this.model}`,
+ generationConfig: {
+ responseModalities: ['AUDIO'],
+ speechConfig: { voiceConfig: { prebuiltVoiceConfig: { voiceName: this.voice } } },
+ },
+ systemInstruction: { parts: [{ text: this.op.instructions }] },
+ tools: [{ functionDeclarations: tools }],
+ // Audio sessions are otherwise capped at 15 minutes.
+ contextWindowCompression: { slidingWindow: {} },
+ sessionResumption: this.resumeHandle ? { handle: this.resumeHandle } : {},
+ }
+ this.send({ setup }, socket)
+ }
+
+ end() {
+ this.ended = true
+ this.onActivityChanged?.(false)
+ const socket = this.socket
+ this.socket = null
+ socket?.close(1000)
+ this.stopPlayback()
+ this.stopMic?.()
+ this.stopMic = null
+ void this.output?.close()
+ this.output = null
+ }
+
+ protected mutedChanged(on: boolean) {
+ this.level = 0
+ if (on) this.send({ realtimeInput: { audioStreamEnd: true } })
+ }
+
+ interrupt() {
+ if (this.phase !== 'speaking') return
+ this.stopPlayback()
+ this.dropping = !this.turnDone
+ this.setPhase('listening')
+ }
+
+ speak(reply: string) {
+ if (this.ended || this.phase === 'failed') return
+ this.pending.push(this.op.reply(reply))
+ this.flush()
+ }
+
+ announce(notice: string) {
+ if (this.ended || this.phase === 'failed') return
+ this.pending.push(this.op.notice(notice))
+ this.flush()
+ }
+
+ // MARK: socket
+
+ private closed(socket: WebSocket, reason: string) {
+ if (this.ended || this.socket !== socket) return
+ this.socket = null
+ this.ready = false
+ if (this.resumeHandle && this.reconnects < 3) {
+ this.reconnects++
+ void this.connect()
+ } else {
+ this.fail(reason || 'Lost the connection to Gemini.')
+ }
+ }
+
+ private received(data: string) {
+ if (this.ended) return
+ let msg: Record<string, any>
+ try {
+ msg = JSON.parse(data) as Record<string, any>
+ } catch {
+ return
+ }
+ if (msg.setupComplete !== undefined) {
+ this.ready = true
+ this.reconnects = 0
+ if (!this.stopMic) void this.startAudio()
+ if (this.phase === 'starting') this.setPhase('listening')
+ this.flush()
+ }
+ const content = msg.serverContent
+ if (content) {
+ if (content.interrupted === true) {
+ this.stopPlayback()
+ this.dropping = false
+ if (this.phase === 'speaking') this.setPhase('listening')
+ }
+ const parts = content.modelTurn?.parts
+ if (Array.isArray(parts)) {
+ this.turnDone = false
+ for (const part of parts) {
+ const audio = part?.inlineData?.data
+ if (typeof audio === 'string' && !this.dropping) this.play(audio)
+ }
+ }
+ if (content.turnComplete === true) {
+ this.turnDone = true
+ this.dropping = false
+ if (this.queued === 0 && this.phase === 'speaking') this.setPhase('listening')
+ this.flush()
+ }
+ }
+ const calls = msg.toolCall?.functionCalls
+ if (Array.isArray(calls)) {
+ const functionResponses = calls
+ .filter((c) => typeof c?.id === 'string' && typeof c?.name === 'string')
+ .map((c) => {
+ const output = this.op.call(c.name, (c.args as Record<string, unknown>) ?? {})
+ return { id: c.id, name: c.name, response: { result: JSON.parse(output) } }
+ })
+ this.send({ toolResponse: { functionResponses } })
+ }
+ const update = msg.sessionResumptionUpdate
+ if (update?.resumable === true && typeof update.newHandle === 'string') this.resumeHandle = update.newHandle
+ // The server ends this connection soon; carry on in a resumed one.
+ if (msg.goAway !== undefined) this.socket?.close(1000)
+ }
+
+ private send(message: unknown, socket?: WebSocket) {
+ const target = socket ?? (this.ready ? this.socket : null)
+ if (target?.readyState === WebSocket.OPEN) target.send(JSON.stringify(message))
+ }
+
+ /** Replies wait for the model to finish talking; `turnComplete` here would cut it off. */
+ private flush() {
+ if (!this.ready || !this.turnDone || this.queued !== 0 || !this.pending.length) return
+ const text = this.pending.join('\n\n')
+ this.pending = []
+ this.turnDone = false
+ this.send({ clientContent: { turns: [{ role: 'user', parts: [{ text }] }], turnComplete: true } })
+ }
+
+ // MARK: audio
+
+ private async startAudio() {
+ try {
+ this.output = new AudioContext({ sampleRate: 24000 })
+ const stop = await micPCM16k((pcm, level) => this.captured(pcm, level))
+ if (this.ended) return stop()
+ this.stopMic = stop
+ } catch (e) {
+ if (!this.ended) this.fail(errorMessage(e))
+ }
+ }
+
+ private captured(pcm: Int16Array, level: number) {
+ if (this.ended || this.muted || !this.isActive) return
+ this.level = this.phase === 'speaking' ? 0 : level
+ this.send({ realtimeInput: { audio: { data: toBase64(new Uint8Array(pcm.buffer)), mimeType: 'audio/pcm;rate=16000' } } })
+ }
+
+ private play(base64: string) {
+ const ctx = this.output
+ if (!ctx) return
+ const bytes = fromBase64(base64)
+ const frames = bytes.length >> 1
+ if (!frames) return
+ const view = new DataView(bytes.buffer)
+ const buffer = ctx.createBuffer(1, frames, 24000)
+ const out = buffer.getChannelData(0)
+ for (let i = 0; i < frames; i++) out[i] = view.getInt16(i * 2, true) / 32768
+ const source = ctx.createBufferSource()
+ source.buffer = buffer
+ source.connect(ctx.destination)
+ this.nextStart = Math.max(this.nextStart, ctx.currentTime)
+ source.start(this.nextStart)
+ this.nextStart += buffer.duration
+ this.sources.add(source)
+ this.queued++
+ this.level = 0
+ if (this.phase !== 'speaking') this.setPhase('speaking')
+ const generation = this.playGeneration
+ source.onended = () => {
+ this.sources.delete(source)
+ this.played(generation)
+ }
+ }
+
+ private played(generation: number) {
+ if (generation !== this.playGeneration || this.ended) return
+ this.queued = Math.max(0, this.queued - 1)
+ if (this.queued !== 0 || !this.turnDone) return
+ if (this.phase === 'speaking') this.setPhase('listening')
+ this.flush()
+ }
+
+ private stopPlayback() {
+ this.playGeneration++
+ this.queued = 0
+ this.nextStart = 0
+ for (const s of this.sources) {
+ s.onended = null
+ try {
+ s.stop()
+ } catch {}
+ }
+ this.sources.clear()
+ }
+}
diff --git a/apps/desktop/src/renderer/views/call/OnDeviceEngine.ts b/apps/desktop/src/renderer/views/call/OnDeviceEngine.ts
new file mode 100644
index 00000000..e3adc2db
--- /dev/null
+++ b/apps/desktop/src/renderer/views/call/OnDeviceEngine.ts
@@ -0,0 +1,194 @@
+import type { SpeechEvent } from '@shared/ipc'
+import { onDeviceUnavailableReason, spokenText, VoiceEngine, VoiceSettings } from './voice'
+
+/**
+ * The reply's language from its script, for picking a voice (replies may be English while the
+ * computer is in Chinese).
+ * ponytail: script counting, not NLLanguageRecognizer; misreads Latin-script languages other than the system's.
+ */
+function languageOf(text: string): string {
+ const count = (re: RegExp) => text.match(re)?.length ?? 0
+ const latin = count(/[A-Za-z]/g)
+ const system = navigator.language
+ if (count(/[぀-ヿ]/g) > 0) return 'ja'
+ if (count(/[가-힯]/g) > latin / 3) return 'ko'
+ if (count(/\p{Script=Han}/gu) > latin / 3) {
+ const chinese = navigator.languages.find((l) => l.startsWith('zh'))
+ if (chinese) return /Hant|TW|HK|MO/.test(chinese) ? (/HK/.test(chinese) ? 'zh-HK' : 'zh-TW') : 'zh-CN'
+ return 'zh-TW'
+ }
+ return /^(zh|ja|ko)/.test(system) ? 'en' : system.split('-')[0]!
+}
+
+/** A system voice in the text's language: the computer's own region first, then local voices. */
+function voiceFor(text: string): SpeechSynthesisVoice | null {
+ const code = languageOf(text)
+ const voices = speechSynthesis.getVoices().filter((v) => v.lang.replace('_', '-').startsWith(code))
+ const score = (v: SpeechSynthesisVoice) =>
+ (v.lang.replace('_', '-') === navigator.language ? 4 : 0) + (/premium|enhanced/i.test(v.name) ? 2 : 0) + (v.localService ? 1 : 0)
+ return voices.sort((a, b) => score(b) - score(a))[0] ?? null
+}
+
+/**
+ * A hands-free voice loop with one bot, all on this computer: speech is transcribed on device
+ * (SFSpeechRecognizer in the speech helper), sent as an ordinary message, and the bot's final
+ * replies are read aloud with the system voices. The host sees text only.
+ */
+export class OnDeviceEngine extends VoiceEngine {
+ readonly provider = null
+ /** What you're saying right now. */
+ private heard = ''
+ /** A recognition is running (between our start and stop). */
+ private recognizing = false
+ /** The helper said it's listening (permissions granted). */
+ private started = false
+ private everStarted = false
+ private lastRestart = 0
+ private silence: ReturnType<typeof setTimeout> | null = null
+ private offEvents: (() => void) | null = null
+ private speakGeneration = 0
+
+ constructor(private send: (text: string) => void) {
+ super()
+ }
+
+ async start() {
+ if (!window.codync.speech.available) return this.fail(onDeviceUnavailableReason())
+ speechSynthesis.getVoices()
+ this.offEvents = window.codync.speech.onEvent((e) => this.event(e))
+ this.listen()
+ }
+
+ end() {
+ this.ended = true
+ this.onActivityChanged?.(false)
+ this.stopListening()
+ this.offEvents?.()
+ this.offEvents = null
+ this.speakGeneration++
+ speechSynthesis.cancel()
+ }
+
+ protected mutedChanged(on: boolean) {
+ if (this.phase !== 'listening') return
+ if (on) this.stopListening()
+ else this.listen()
+ }
+
+ /** Reads a reply aloud (the bot's final message), pausing the microphone meanwhile. */
+ speak(markdown: string) {
+ const text = spokenText(markdown)
+ if (this.ended || !text || this.phase === 'starting' || this.phase === 'failed') return
+ this.stopListening()
+ this.setPhase('speaking')
+ const generation = ++this.speakGeneration
+ speechSynthesis.cancel()
+ const utterance = new SpeechSynthesisUtterance(text)
+ const voice = voiceFor(text)
+ if (voice) {
+ utterance.voice = voice
+ utterance.lang = voice.lang
+ }
+ // The Mac stores AVSpeechUtterance's rate (0.5 normal); the web's normal is 1.
+ const rate = VoiceSettings.rate()
+ utterance.rate = rate > 0 ? Math.min(10, Math.max(0.1, rate / 0.5)) : 1
+ utterance.onend = () => generation === this.speakGeneration && this.finishedSpeaking()
+ utterance.onerror = () => generation === this.speakGeneration && this.finishedSpeaking()
+ speechSynthesis.speak(utterance)
+ }
+
+ announce(notice: string) {
+ this.speak(notice)
+ }
+
+ /** Cuts the reply short and goes back to listening. */
+ interrupt() {
+ if (this.phase !== 'speaking') return
+ this.speakGeneration++
+ speechSynthesis.cancel()
+ this.listen()
+ }
+
+ // MARK: listening
+
+ private listen() {
+ this.heard = ''
+ if (this.phase !== 'listening' && this.everStarted) this.setPhase('listening')
+ if (this.muted || this.ended) return
+ this.recognizing = true
+ this.started = false
+ window.codync.speech.start(null)
+ }
+
+ private stopListening() {
+ if (this.silence) clearTimeout(this.silence)
+ this.silence = null
+ if (this.recognizing) window.codync.speech.stop()
+ this.recognizing = false
+ this.started = false
+ this.level = 0
+ }
+
+ private event(e: SpeechEvent) {
+ if (this.ended || !this.recognizing) return
+ switch (e.type) {
+ case 'started':
+ this.started = true
+ if (!this.everStarted) {
+ this.everStarted = true
+ this.setPhase('listening')
+ }
+ break
+ case 'level':
+ if (this.phase === 'listening') this.level = e.value
+ break
+ case 'partial':
+ case 'final':
+ this.recognized(e.text, e.type === 'final')
+ break
+ case 'error':
+ // Before it listened: permissions or no recognizer for the language.
+ if (!this.started) return this.fail(e.message)
+ this.restart(e.message)
+ break
+ case 'stopped':
+ if (!this.started) return this.fail("Speech recognition stopped.")
+ this.restart('Speech recognition stopped.')
+ break
+ }
+ }
+
+ /** The helper gave up mid-utterance: send what was heard and listen again, unless it keeps failing. */
+ private restart(message: string) {
+ if (Date.now() - this.lastRestart < 1000) return this.fail(message)
+ this.lastRestart = Date.now()
+ this.submit()
+ }
+
+ private recognized(text: string, final: boolean) {
+ if (this.phase !== 'listening') return
+ this.heard = text
+ if (this.silence) clearTimeout(this.silence)
+ if (final) return this.submit()
+ this.silence = setTimeout(() => this.submit(), VoiceSettings.pause() * 1000)
+ }
+
+ private submit() {
+ const text = this.heard.trim()
+ this.stopListening()
+ if (text) this.send(text)
+ // Keep listening while the bot works: more messages fold into its next turn.
+ this.listen()
+ }
+
+ private finishedSpeaking() {
+ if (this.phase !== 'speaking') return
+ this.listen()
+ }
+
+ protected fail(message: string) {
+ this.stopListening()
+ this.failure = message
+ this.setPhase('failed')
+ }
+}
diff --git a/apps/desktop/src/renderer/views/call/OpenAIRealtimeEngine.ts b/apps/desktop/src/renderer/views/call/OpenAIRealtimeEngine.ts
new file mode 100644
index 00000000..69fda49b
--- /dev/null
+++ b/apps/desktop/src/renderer/views/call/OpenAIRealtimeEngine.ts
@@ -0,0 +1,222 @@
+import { OPERATOR_TOOLS, type CallOperator } from './call-operator'
+import { errorMessage, microphone, VoiceEngine } from './voice'
+
+/**
+ * OpenAI Realtime over WebRTC, this computer ↔ OpenAI directly: the microphone is a WebRTC audio
+ * track, the model's voice plays from the remote track, and events (tools, replies) go over
+ * `oai-events`. The user's key stays with the host, which mints the client secret; the host
+ * never sees audio.
+ */
+export class OpenAIRealtimeEngine extends VoiceEngine {
+ readonly provider = 'openai' as const
+ private pc: RTCPeerConnection | null = null
+ private events: RTCDataChannel | null = null
+ private stream: MediaStream | null = null
+ private audio: HTMLAudioElement | null = null
+ /** A response is being generated; new replies wait for it to finish. */
+ private responding = false
+ private pending: string[] = []
+ private dropTimer: ReturnType<typeof setTimeout> | null = null
+
+ constructor(
+ private credential: () => Promise<string>,
+ private op: CallOperator,
+ ) {
+ super()
+ }
+
+ async start() {
+ try {
+ this.stream = await microphone()
+ if (this.ended) return this.release()
+ const secret = await this.credential()
+ if (this.ended) return
+ await this.connect(secret)
+ } catch (e) {
+ if (!this.ended) this.fail(errorMessage(e))
+ }
+ }
+
+ private async connect(secret: string) {
+ const pc = new RTCPeerConnection({ bundlePolicy: 'max-bundle' })
+ this.pc = pc
+ const track = this.stream!.getAudioTracks()[0]
+ if (!track) throw new Error("Couldn't start the call audio.")
+ track.enabled = !this.muted
+ pc.addTrack(track, this.stream!)
+ const audio = new Audio()
+ audio.autoplay = true
+ this.audio = audio
+ pc.ontrack = (e) => {
+ audio.srcObject = e.streams[0] ?? new MediaStream([e.track])
+ }
+ pc.oniceconnectionstatechange = () => this.iceChanged(pc.iceConnectionState)
+ const events = pc.createDataChannel('oai-events')
+ events.onopen = () => this.opened()
+ events.onmessage = (e) => typeof e.data === 'string' && this.received(e.data)
+ this.events = events
+
+ const offer = await pc.createOffer()
+ await pc.setLocalDescription(offer)
+ const response = await fetch('https://api.openai.com/v1/realtime/calls', {
+ method: 'POST',
+ headers: { Authorization: `Bearer ${secret}`, 'Content-Type': 'application/sdp' },
+ body: offer.sdp,
+ signal: AbortSignal.timeout(15_000),
+ })
+ const body = await response.text()
+ if (this.ended) return
+ if (!response.ok) {
+ let message: string | undefined
+ try {
+ message = (JSON.parse(body) as { error?: { message?: string } }).error?.message
+ } catch {}
+ throw new Error(message ?? `OpenAI answered ${response.status}.`)
+ }
+ await pc.setRemoteDescription({ type: 'answer', sdp: body })
+ this.dropTimer = setTimeout(() => {
+ if (!this.ended && this.phase === 'starting') this.fail("Couldn't reach OpenAI.")
+ }, 15_000)
+ }
+
+ private release() {
+ for (const t of this.stream?.getTracks() ?? []) t.stop()
+ this.stream = null
+ }
+
+ end() {
+ this.ended = true
+ this.onActivityChanged?.(false)
+ if (this.dropTimer) clearTimeout(this.dropTimer)
+ this.events?.close()
+ this.pc?.close()
+ this.events = null
+ this.pc = null
+ this.release()
+ if (this.audio) {
+ this.audio.srcObject = null
+ this.audio = null
+ }
+ }
+
+ protected mutedChanged(on: boolean) {
+ for (const t of this.stream?.getAudioTracks() ?? []) t.enabled = !on
+ if (on) this.level = 0
+ }
+
+ interrupt() {
+ if (this.phase !== 'speaking') return
+ this.send({ type: 'response.cancel' })
+ this.send({ type: 'output_audio_buffer.clear' })
+ this.setPhase('listening')
+ }
+
+ speak(reply: string) {
+ if (this.ended || this.phase === 'failed') return
+ this.pending.push(this.op.reply(reply))
+ this.flush()
+ }
+
+ announce(notice: string) {
+ if (this.ended || this.phase === 'failed') return
+ this.pending.push(this.op.notice(notice))
+ this.flush()
+ }
+
+ // MARK: events
+
+ private opened() {
+ if (this.ended) return
+ if (this.dropTimer) clearTimeout(this.dropTimer)
+ this.send({
+ type: 'session.update',
+ session: {
+ type: 'realtime',
+ instructions: this.op.instructions,
+ tools: OPERATOR_TOOLS.map((t) => ({ type: 'function', name: t.name, description: t.description, parameters: t.parameters })),
+ audio: { input: { turn_detection: { type: 'semantic_vad' } } },
+ },
+ })
+ this.setPhase('listening')
+ this.flush()
+ }
+
+ private received(data: string) {
+ if (this.ended) return
+ let msg: Record<string, unknown>
+ try {
+ msg = JSON.parse(data) as Record<string, unknown>
+ } catch {
+ return
+ }
+ switch (msg.type) {
+ case 'input_audio_buffer.speech_started':
+ this.level = this.muted ? 0 : 0.7
+ break
+ case 'input_audio_buffer.speech_stopped':
+ this.level = 0
+ break
+ case 'output_audio_buffer.started':
+ this.setPhase('speaking')
+ break
+ case 'output_audio_buffer.stopped':
+ case 'output_audio_buffer.cleared':
+ if (this.phase === 'speaking') this.setPhase('listening')
+ break
+ case 'response.created':
+ this.responding = true
+ break
+ case 'response.done':
+ this.responding = false
+ this.flush()
+ break
+ case 'response.function_call_arguments.done': {
+ if (typeof msg.call_id !== 'string' || typeof msg.name !== 'string') return
+ let args: Record<string, unknown> = {}
+ try {
+ if (typeof msg.arguments === 'string') args = JSON.parse(msg.arguments) as Record<string, unknown>
+ } catch {}
+ const output = this.op.call(msg.name, args)
+ this.send({ type: 'conversation.item.create', item: { type: 'function_call_output', call_id: msg.call_id, output } })
+ // An empty entry asks for a response once this one is done, so the model speaks the result.
+ this.pending.push('')
+ break
+ }
+ case 'error':
+ console.error('realtime:', (msg.error as { message?: string } | undefined)?.message ?? '?')
+ break
+ }
+ }
+
+ private iceChanged(state: RTCIceConnectionState) {
+ if (this.ended) return
+ switch (state) {
+ case 'connected':
+ case 'completed':
+ if (this.dropTimer) clearTimeout(this.dropTimer)
+ break
+ case 'disconnected':
+ if (this.dropTimer) clearTimeout(this.dropTimer)
+ this.dropTimer = setTimeout(() => !this.ended && this.fail('Lost the connection to OpenAI.'), 5000)
+ break
+ case 'failed':
+ this.fail('Lost the connection to OpenAI.')
+ break
+ }
+ }
+
+ /** Hands waiting replies (and tool results, as empty entries) to the model once it's free. */
+ private flush() {
+ if (this.responding || !this.isActive || this.events?.readyState !== 'open' || !this.pending.length) return
+ for (const text of this.pending) {
+ if (text) this.send({ type: 'conversation.item.create', item: { type: 'message', role: 'user', content: [{ type: 'input_text', text }] } })
+ }
+ this.pending = []
+ this.responding = true
+ this.send({ type: 'response.create' })
+ }
+
+ private send(event: unknown) {
+ if (this.events?.readyState === 'open') this.events.send(JSON.stringify(event))
+ }
+}
diff --git a/apps/desktop/src/renderer/views/call/call-operator.ts b/apps/desktop/src/renderer/views/call/call-operator.ts
new file mode 100644
index 00000000..ead247cf
--- /dev/null
+++ b/apps/desktop/src/renderer/views/call/call-operator.ts
@@ -0,0 +1,137 @@
+import { isChat, type Entry } from '@shared/models'
+import type { BotStore } from '../../store/bot-store'
+import { spokenText } from './voice'
+
+export interface OperatorTool {
+ name: string
+ description: string
+ /** JSON Schema (both providers take the same shape). */
+ parameters: { type: 'object'; properties: Record<string, { type: string; description: string }>; required?: string[] }
+}
+
+export const OPERATOR_TOOLS: OperatorTool[] = [
+ {
+ name: 'send_to_bot',
+ description: 'Send a message to the bot on the computer, exactly as if the user typed it. Its reply arrives later.',
+ parameters: { type: 'object', properties: { text: { type: 'string', description: 'The message' } }, required: ['text'] },
+ },
+ {
+ name: 'bot_status',
+ description: 'Whether the bot is idle or working, what it is doing, and any approval it is waiting for.',
+ parameters: { type: 'object', properties: {} },
+ },
+ {
+ name: 'recent_messages',
+ description: 'The latest messages in the chat with the bot, oldest first.',
+ parameters: { type: 'object', properties: { count: { type: 'integer', description: 'How many, 1–20' } } },
+ },
+ {
+ name: 'answer_approval',
+ description: "Answer the bot's pending approval request with one of its options.",
+ parameters: { type: 'object', properties: { option: { type: 'string', description: "The option's name" } }, required: ['option'] },
+ },
+]
+
+/**
+ * A realtime model is an operator in front of the bot, not a replacement for it: the bot does the
+ * work, the operator talks. Its tools run here against the store; only `send_to_bot` messages and
+ * the bot's replies become chat entries (kit CallOperator).
+ */
+export class CallOperator {
+ constructor(
+ private store: BotStore,
+ private botId: string,
+ ) {}
+
+ get instructions() {
+ const bot = this.store.bots.get(this.botId)
+ const name = bot?.name ?? 'the bot'
+ const about = bot?.description ? ` (${bot.description})` : ''
+ return [
+ `You are the voice on a phone call between the user and ${name}${about}, a coding agent running on the user's computer. ${name} does all the actual work; you relay.`,
+ '- When the user asks for something to be done, changed, checked or answered about their code or computer, call send_to_bot with their request in their own words (keep details, names and code terms exactly). Don\'t do the work or invent results yourself.',
+ '- Use bot_status and recent_messages for "what are you doing?" or "what did it say?".',
+ '- Call answer_approval only after the user has clearly said which option to pick.',
+ `- Messages starting with [${name} replied] or [Notice] come from the computer: tell the user what they say, shortened for speech (no code blocks, no long lists, no URLs).`,
+ '- Reply in the user\'s language. Keep spoken answers short and natural.',
+ ].join('\n')
+ }
+
+ /** Runs one tool call; the result is a JSON string. */
+ call(name: string, args: Record<string, unknown>): string {
+ let result: Record<string, unknown>
+ switch (name) {
+ case 'send_to_bot':
+ result = this.send(typeof args.text === 'string' ? args.text : '')
+ break
+ case 'bot_status':
+ result = this.status()
+ break
+ case 'recent_messages':
+ result = this.recent(typeof args.count === 'number' ? Math.trunc(args.count) : 6)
+ break
+ case 'answer_approval':
+ result = this.answer(typeof args.option === 'string' ? args.option : '')
+ break
+ default:
+ result = { error: `Unknown tool ${name}.` }
+ }
+ return JSON.stringify(result)
+ }
+
+ /** How a reply is handed to the operator. */
+ reply(text: string) {
+ return `[${this.store.bots.get(this.botId)?.name ?? 'Bot'} replied] ${spokenText(text)}`
+ }
+
+ notice(text: string) {
+ return `[Notice] ${text}`
+ }
+
+ private send(text: string) {
+ const trimmed = text.trim()
+ if (!trimmed) return { error: 'Nothing to send.' }
+ this.store.send(trimmed, this.botId)
+ return { sent: true, note: "The bot's reply will arrive as a separate message." }
+ }
+
+ private status() {
+ const bot = this.store.bots.get(this.botId)
+ if (!bot) return { error: "The bot isn't available." }
+ const out: Record<string, unknown> = { status: bot.status, activity: bot.activity }
+ const card = this.pendingApproval
+ if (card) out.approval = { request: card.data.title ?? card.data.command ?? '', options: (card.data.options ?? []).map((o) => o.name) }
+ return out
+ }
+
+ private recent(count: number) {
+ const name = this.store.bots.get(this.botId)?.name ?? 'bot'
+ const n = Math.min(Math.max(count, 1), 20)
+ const messages = this.store
+ .chat(this.botId)
+ .filter(isChat)
+ .slice(-n)
+ .map((e) => ({
+ from: e.kind === 'user' ? 'user' : e.kind === 'agent' ? name : e.kind === 'permission' ? 'approval request' : 'notice',
+ text: spokenText(e.data.text ?? e.data.title ?? ''),
+ }))
+ return { messages }
+ }
+
+ private answer(option: string) {
+ const card = this.pendingApproval
+ if (!card) return { error: 'Nothing is waiting for approval.' }
+ const options = card.data.options ?? []
+ const wanted = option.toLowerCase()
+ const pick =
+ options.find((o) => o.name.toLowerCase() === wanted || o.optionId === option) ??
+ options.find((o) => o.name.toLowerCase().includes(wanted) || wanted.includes(o.name.toLowerCase()))
+ if (!pick) return { error: 'No such option.', options: options.map((o) => o.name) }
+ this.store.respond(card, pick.optionId)
+ return { answered: pick.name }
+ }
+
+ private get pendingApproval(): Entry | undefined {
+ return this.store.chat(this.botId).findLast((e) => e.kind === 'permission' && e.data.status === 'pending')
+ }
+}
diff --git a/apps/desktop/src/renderer/views/call/call.css b/apps/desktop/src/renderer/views/call/call.css
new file mode 100644
index 00000000..2e4c994d
--- /dev/null
+++ b/apps/desktop/src/renderer/views/call/call.css
@@ -0,0 +1,79 @@
+.call {
+ display: flex;
+ flex-direction: column;
+ align-items: center;
+ gap: 6px;
+ padding: 0 12px;
+ max-width: 100%;
+}
+
+/* Floats over the chat like the title pill: frosted, raised, sized to its content. */
+.call-bar {
+ display: flex;
+ align-items: center;
+ gap: 10px;
+ padding: 10px 10px 10px 18px;
+ border-radius: 999px;
+ background: var(--frosted);
+ backdrop-filter: blur(24px) saturate(1.8);
+ -webkit-backdrop-filter: blur(24px) saturate(1.8);
+ box-shadow: 0 8px 40px rgba(0, 0, 0, 0.25);
+}
+
+.call-avatar {
+ display: flex;
+ transition: transform var(--layout);
+}
+.call-avatar.speaking {
+ animation: call-pulse 0.6s ease-in-out infinite alternate;
+}
+@keyframes call-pulse {
+ to {
+ transform: scale(1.1);
+ }
+}
+
+.call-round {
+ width: 40px;
+ height: 40px;
+ border-radius: 50%;
+ display: grid;
+ place-items: center;
+ flex: none;
+ background: var(--bubble-user);
+ color: var(--secondary);
+ transition: background-color var(--hover), color var(--hover), transform var(--press);
+}
+.call-round.on {
+ background: var(--accent-fill);
+ color: var(--on-accent);
+}
+.call-round.end {
+ background: var(--danger);
+ color: #fff;
+}
+.call-round:active {
+ transform: scale(0.98);
+}
+
+/* One line under the bar: why the call fell back, or why it can't listen. */
+.call-line {
+ font-size: calc(10px * var(--scale));
+ text-align: center;
+ padding: 8px 12px;
+ border-radius: 999px;
+ background: var(--bubble-user);
+ box-shadow: 0 2px 20px rgba(0, 0, 0, 0.06);
+ animation: call-line-in var(--fade) both;
+}
+@keyframes call-line-in {
+ from {
+ opacity: 0;
+ }
+}
+
+@media (prefers-reduced-motion: reduce) {
+ .call-avatar.speaking {
+ animation: none;
+ }
+}
diff --git a/apps/desktop/src/renderer/views/call/voice-call.ts b/apps/desktop/src/renderer/views/call/voice-call.ts
new file mode 100644
index 00000000..390d0b52
--- /dev/null
+++ b/apps/desktop/src/renderer/views/call/voice-call.ts
@@ -0,0 +1,31 @@
+import { needsInput } from '@shared/models'
+import type { BotStore } from '../../store/bot-store'
+
+/**
+ * While a call's audio is active, speaks each new final reply in the bot's main chat and
+ * announces when the bot starts waiting for an approval (kit BotStore `beginVoiceCall`). Only
+ * entries newer (by rev) than the call's start count. Returns the function that stops it.
+ */
+export function watchVoiceCall(store: BotStore, botId: string, handlers: { speak: (text: string) => void; announce: (text: string) => void }) {
+ let startRev = 0
+ for (const b of store.bots.values()) startRev = Math.max(startRev, b.rev)
+ for (const list of store.entries.values()) for (const e of list) startRev = Math.max(startRev, e.rev)
+ // Replies already final when the call began are never spoken (a reaction bumps their rev).
+ const finals = new Set(store.allEntries(botId).filter((e) => e.data.final).map((e) => e.id))
+ let waiting = (() => {
+ const bot = store.bots.get(botId)
+ return bot ? needsInput(bot) : false
+ })()
+
+ return store.subscribe(() => {
+ for (const e of store.allEntries(botId)) {
+ if (!e.data.final || finals.has(e.id)) continue
+ finals.add(e.id)
+ if (e.kind === 'agent' && !e.threadId && e.rev > startRev && e.data.text) handlers.speak(e.data.text)
+ }
+ const bot = store.bots.get(botId)
+ const now = bot ? needsInput(bot) : false
+ if (bot && now && !waiting) handlers.announce(`${bot.name} needs your approval in the chat.`)
+ waiting = now
+ })
+}
diff --git a/apps/desktop/src/renderer/views/call/voice.ts b/apps/desktop/src/renderer/views/call/voice.ts
new file mode 100644
index 00000000..983e7700
--- /dev/null
+++ b/apps/desktop/src/renderer/views/call/voice.ts
@@ -0,0 +1,217 @@
+import { Observable } from '../../lib/observable'
+import {
+ addVoiceUsage,
+ chosenModel,
+ chosenVoice,
+ engine,
+ isVoiceProvider,
+ mode,
+ pause,
+ rate,
+ rememberStatus,
+ voiceInfo,
+ type VoiceProvider,
+} from '../settings/voice-settings'
+
+// Calls use the voice settings the settings view keeps (views/settings/voice-settings.ts: the
+// same keys as the Mac's UserDefaults, kit VoiceSettings).
+
+export type { VoiceProvider, VoiceStatus } from '../settings/voice-settings'
+
+export const providerName = (p: VoiceProvider) => voiceInfo[p].name
+
+/** Longest stretch of a reply turned into audio at once: each clip has to fit one channel message. Gemini returns PCM, OpenAI AAC. */
+export const SPEECH_CHUNK: Record<VoiceProvider, number> = { openai: 600, gemini: 150 }
+
+/** Recognition through the Swift helper: macOS only. */
+export const onDeviceAvailable = () => window.codync.speech.available
+export const onDeviceUnavailableReason = () =>
+ window.codync.platform === 'darwin' ? "On-device speech isn't available in this build." : "On-device speech isn't available on Linux."
+
+export const VoiceSettings = {
+ /** The provider picked in the call settings; null for on-device. */
+ provider: (): VoiceProvider | null => {
+ const value = engine.get()
+ return isVoiceProvider(value) ? value : null
+ },
+ model: (p: VoiceProvider) => chosenModel(p, 'realtime'),
+ transcribeModel: (p: VoiceProvider) => chosenModel(p, 'transcribe'),
+ speechModel: (p: VoiceProvider) => chosenModel(p, 'speech'),
+ voice: chosenVoice,
+ isSpeechMode: (p: VoiceProvider) => mode(p).get() === 'speech',
+ remember: rememberStatus,
+ /** A pause this long (seconds) ends what you're saying and sends it. */
+ pause: () => (pause.get() > 0 ? pause.get() : 1.5),
+ /** AVSpeechUtterance's rate (0…1, 0.5 normal), as the Mac stores it. */
+ rate: () => rate.get(),
+}
+
+/** This month's realtime minutes, counted on this device. */
+export const countVoiceUsage = (seconds: number, p: VoiceProvider) => addVoiceUsage(Math.floor(seconds), p)
+
+// MARK: engines
+
+export type VoicePhase = 'starting' | 'listening' | 'speaking' | 'failed'
+
+/**
+ * What carries a call's audio: on-device speech or a realtime model on the user's own key.
+ * `CallView` only talks to this; nothing on the host changes.
+ */
+export abstract class VoiceEngine extends Observable {
+ phase: VoicePhase = 'starting'
+ failure: string | null = null
+ /** Microphone loudness, 0…1, while listening. Read every frame; not observed. */
+ level = 0
+ abstract readonly provider: VoiceProvider | null
+ /** Keeps the call subscribed to replies while audio is active. */
+ onActivityChanged: ((active: boolean) => void) | null = null
+ protected ended = false
+ private mutedValue = false
+
+ get isActive() {
+ return this.phase === 'listening' || this.phase === 'speaking'
+ }
+
+ get muted() {
+ return this.mutedValue
+ }
+ set muted(on: boolean) {
+ if (on === this.mutedValue) return
+ this.mutedValue = on
+ this.mutedChanged(on)
+ this.changed()
+ }
+ protected mutedChanged(_on: boolean) {}
+
+ protected setPhase(phase: VoicePhase) {
+ this.phase = phase
+ this.changed()
+ this.onActivityChanged?.(!this.ended && this.isActive)
+ }
+
+ protected fail(message: string) {
+ this.end()
+ this.failure = message
+ this.setPhase('failed')
+ }
+
+ abstract start(): Promise<void>
+ /** Idempotent. */
+ abstract end(): void
+ /** Cuts what's being said short. */
+ abstract interrupt(): void
+ /** A new final reply from the bot to say. */
+ abstract speak(reply: string): void
+ /** A notice from the computer (an approval is waiting) to say. */
+ abstract announce(notice: string): void
+}
+
+export const errorMessage = (e: unknown) => (e instanceof Error ? e.message : String(e))
+export const MIC_DENIED = 'Allow the microphone for Codync in Settings to talk to your bots.'
+
+// MARK: audio helpers
+
+/** The microphone, echo-cancelled so the bot can be talked over. */
+export async function microphone(): Promise<MediaStream> {
+ try {
+ return await navigator.mediaDevices.getUserMedia({ audio: { echoCancellation: true, noiseSuppression: true, autoGainControl: true, channelCount: 1 } })
+ } catch (e) {
+ if (e instanceof DOMException && (e.name === 'NotAllowedError' || e.name === 'SecurityError')) throw new Error(MIC_DENIED)
+ throw new Error(`Couldn't use the microphone: ${errorMessage(e)}`)
+ }
+}
+
+/**
+ * Microphone → 16 kHz mono PCM16 with a loudness level, 0…1 (kit `MicTap.pcm16k`). Returns the
+ * function that stops it.
+ */
+export async function micPCM16k(out: (pcm: Int16Array, level: number) => void): Promise<() => void> {
+ const stream = await microphone()
+ const ctx = new AudioContext({ sampleRate: 16000 })
+ const source = ctx.createMediaStreamSource(stream)
+ // ponytail: ScriptProcessor is deprecated but needs no worklet file (the CSP allows only 'self'); AudioWorklet if it's ever removed.
+ const node = ctx.createScriptProcessor(2048, 1, 1)
+ node.onaudioprocess = (e) => {
+ const input = e.inputBuffer.getChannelData(0)
+ const pcm = new Int16Array(input.length)
+ let sum = 0
+ for (let i = 0; i < input.length; i++) {
+ const v = Math.max(-1, Math.min(1, input[i]!))
+ pcm[i] = v < 0 ? v * 32768 : v * 32767
+ sum += v * v
+ }
+ out(pcm, levelOf(sum, input.length))
+ }
+ source.connect(node)
+ node.connect(ctx.destination)
+ if (ctx.state === 'suspended') await ctx.resume()
+ return () => {
+ node.onaudioprocess = null
+ source.disconnect()
+ node.disconnect()
+ for (const t of stream.getTracks()) t.stop()
+ void ctx.close()
+ }
+}
+
+/** -50 dB (quiet room) … 0 dB mapped to 0…1. */
+export function levelOf(sumOfSquares: number, count: number) {
+ const db = 10 * Math.log10(Math.max(sumOfSquares / Math.max(count, 1), 1e-10))
+ return Math.min(1, Math.max(0, (db + 50) / 50))
+}
+
+/** 16 kHz mono PCM16 wrapped in a WAV header. */
+export function wav(chunks: Int16Array[]): Uint8Array {
+ const samples = chunks.reduce((n, c) => n + c.length, 0)
+ const out = new Uint8Array(44 + samples * 2)
+ const v = new DataView(out.buffer)
+ const ascii = (at: number, s: string) => [...s].forEach((c, i) => v.setUint8(at + i, c.charCodeAt(0)))
+ ascii(0, 'RIFF')
+ v.setUint32(4, 36 + samples * 2, true)
+ ascii(8, 'WAVEfmt ')
+ v.setUint32(16, 16, true)
+ v.setUint16(20, 1, true)
+ v.setUint16(22, 1, true)
+ v.setUint32(24, 16000, true)
+ v.setUint32(28, 32000, true)
+ v.setUint16(32, 2, true)
+ v.setUint16(34, 16, true)
+ ascii(36, 'data')
+ v.setUint32(40, samples * 2, true)
+ let at = 44
+ for (const c of chunks) for (let i = 0; i < c.length; i++, at += 2) v.setInt16(at, c[i]!, true)
+ return out
+}
+
+export function toBase64(bytes: Uint8Array) {
+ let s = ''
+ for (let i = 0; i < bytes.length; i += 0x8000) s += String.fromCharCode(...bytes.subarray(i, i + 0x8000))
+ return btoa(s)
+}
+
+export function fromBase64(text: string) {
+ const s = atob(text)
+ const out = new Uint8Array(s.length)
+ for (let i = 0; i < s.length; i++) out[i] = s.charCodeAt(i)
+ return out
+}
+
+// MARK: spoken text
+
+/** A chat reply as it should sound: markdown syntax and code blocks dropped (kit SpokenText). */
+export function spokenText(markdown: string) {
+ const rules: [RegExp, string][] = [
+ [/```[\s\S]*?```/g, ''], // code blocks
+ [/!?\[([^\]]*)\]\([^)]*\)/g, '$1'], // links and images → their text
+ [/`([^`]*)`/g, '$1'], // inline code
+ [/^\s{0,3}(#{1,6}|>|[-*+]|\d+\.)\s+/gm, ''], // headings, quotes, list markers
+ [/(\*\*|\*|~~)(\S[^\n]*?)\1/g, '$2'], // emphasis (not `_`: it's in identifiers)
+ [/^[ \t]*\|?[ \t:|-]{3,}\|?[ \t]*$/gm, ''], // table rules
+ [/^[ \t]*\|[ \t]*|[ \t]*\|[ \t]*$/gm, ''], // table edges
+ [/[ \t]*\|[ \t]*/g, ', '], // table cells
+ [/\n{2,}/g, '\n'],
+ ]
+ let text = markdown
+ for (const [pattern, template] of rules) text = text.replace(pattern, template)
+ return text.trim()
+}
diff --git a/apps/desktop/src/renderer/views/marketplace/AgentSheet.tsx b/apps/desktop/src/renderer/views/marketplace/AgentSheet.tsx
new file mode 100644
index 00000000..570835f3
--- /dev/null
+++ b/apps/desktop/src/renderer/views/marketplace/AgentSheet.tsx
@@ -0,0 +1,312 @@
+import { useEffect, useRef, useState, type ReactNode } from 'react'
+import type { Backend } from '@shared/models'
+import { agentIconURL } from '../../components/AgentIcon'
+import { CardForm, CardSection, IconButton, Spinner } from '../../components/Controls'
+import { Icon } from '../../components/Icon'
+import { ModalHeader } from '../../components/Overlay'
+import { ThinkingOrb } from '../../components/ThinkingOrb'
+import { font } from '../../lib/fonts'
+import { useStore } from '../../store/context'
+import { errorText, market, type AgentAuth, type AuthMethod } from './market-models'
+import { ErrorText, Field, ScreenHeader, SlideStack, WebLink, filled, lineClamp } from './pieces'
+import { SetupTerminalView } from './SetupTerminal'
+
+type SetupRoute = { kind: 'install' } | { kind: 'terminal'; method: AuthMethod | null } | { kind: 'keys'; method: AuthMethod }
+
+/** Registry logos are one-color; paint the ones with a known brand color in it. */
+export function agentTint(registry: string | null | undefined) {
+ switch (registry) {
+ case 'claude-acp': return 'rgb(217, 120, 87)'
+ case 'gemini': return 'linear-gradient(to top right, rgb(71, 150, 227), rgb(145, 120, 199), rgb(201, 102, 115))'
+ case 'antigravity-acp': return 'rgb(66, 133, 245)'
+ case 'mistral-vibe': return 'rgb(250, 82, 15)'
+ case 'qwen-code': return 'rgb(97, 92, 237)'
+ case 'amp-acp': return 'rgb(242, 79, 64)'
+ case 'kiro': return 'rgb(143, 69, 255)'
+ case 'cortex-code': return 'rgb(41, 181, 232)'
+ default: return 'var(--text)'
+ }
+}
+
+/** An agent's registry logo on a rounded tile. */
+export function AgentTile({ registry, icon, tile, radius }: { registry?: string | null; icon: number; tile: number; radius: number }) {
+ return (
+ <div style={{ width: tile, height: tile, flex: 'none', borderRadius: radius, background: 'var(--bubble-agent)', display: 'grid', placeItems: 'center', color: 'var(--text)' }}>
+ <TintedAgentIcon registry={registry} size={icon} />
+ </div>
+ )
+}
+
+/** `AgentIcon` painted with `agentTint` (which can be a gradient, so a mask over `background`). */
+function TintedAgentIcon({ registry, size }: { registry?: string | null; size: number }) {
+ const url = agentIconURL(registry)
+ if (!url) return <Icon name="terminal" size={size * 0.8} scaled={false} />
+ return (
+ <span
+ aria-hidden
+ style={{
+ display: 'inline-block', width: size, height: size, flex: 'none', background: agentTint(registry),
+ WebkitMaskImage: `url("${url}")`, WebkitMaskSize: 'contain', WebkitMaskRepeat: 'no-repeat', WebkitMaskPosition: 'center',
+ }}
+ />
+ )
+}
+
+/**
+ * Getting an agent ready on the computer: install it, then sign in. Sign-in
+ * options come from the agent itself (ACP), plus Codync's own command for
+ * CLIs it knows. Bots are made from New chat, not here.
+ */
+export function AgentSheet({ initial }: { initial: Backend }) {
+ const store = useStore()
+ const [route, setRoute] = useState<SetupRoute | null>(null)
+ const [auth, setAuth] = useState<AgentAuth | null>(null)
+ const [checking, setChecking] = useState(false)
+ const [authenticating, setAuthenticating] = useState<string | null>(null)
+ const [error, setError] = useState<string | null>(null)
+ const checkingRef = useRef(false)
+
+ // Live: an install or sign-in updates the host's list.
+ const backend = store.hello?.backends.find((b) => b.id === initial.id) ?? initial
+ const installed = backend.installed === true
+ const curated = backend.curated === true
+ const signedIn = auth?.signedIn ?? backend.signedIn ?? null
+ const host = store.hostName
+
+ const check = async () => {
+ const client = store.client
+ if (!client || checkingRef.current) return
+ checkingRef.current = true
+ setChecking(true)
+ try {
+ setAuth(await market.agentAuth(client, backend.id))
+ setError(null)
+ } catch (e) {
+ setError(errorText(e))
+ }
+ checkingRef.current = false
+ setChecking(false)
+ await store.refreshBackends()
+ }
+
+ const authenticate = async (m: AuthMethod) => {
+ const client = store.client
+ if (!client) return
+ setAuthenticating(m.id)
+ try {
+ setAuth(await market.agentAuthenticate(client, backend.id, m.id))
+ setError(null)
+ } catch (e) {
+ setError(errorText(e))
+ }
+ setAuthenticating(null)
+ await store.refreshBackends()
+ }
+
+ // A known "signed in" needs no agent start; everything else asks the agent.
+ useEffect(() => {
+ if (backend.signedIn !== true) void check()
+ }, [])
+
+ // Back from a terminal or key form: see what changed.
+ const hadRoute = useRef(false)
+ useEffect(() => {
+ if (route === null && hadRoute.current) void check()
+ hadRoute.current = route !== null
+ }, [route])
+
+ const back = () => setRoute(null)
+ const routed = route === null ? null
+ : route.kind === 'install' ? <SetupTerminalView backend={backend} step="install" back={back} />
+ : route.kind === 'terminal' ? <SetupTerminalView backend={backend} step="login" method={route.method} back={back} />
+ : <AgentKeysForm backend={backend} method={route.method} saved={auth?.savedEnv ?? []} back={back} done={setAuth} />
+
+ const statusText = checking
+ ? (auth === null ? `Checking with ${backend.name}… The first check can download it.` : 'Checking…')
+ : signedIn === true ? 'Signed in.'
+ : signedIn === false ? 'Not signed in yet.'
+ : `Couldn't tell. Skip this if you've already signed in on ${host}.`
+
+ const optionRow = (key: string, title: string, detail: string, icon: string, action: () => void, busy = false) => (
+ <button key={key} className="option-row" disabled={authenticating !== null} onClick={action}>
+ <span aria-hidden style={{ width: 26, display: 'flex', justifyContent: 'center', color: 'var(--secondary)', flex: 'none' }}>
+ <Icon name={icon} size={13} />
+ </span>
+ <span style={{ display: 'flex', flexDirection: 'column', gap: 2, flex: 1, minWidth: 0 }}>
+ <span style={{ ...font('body', 'medium'), color: 'var(--text)' }}>{title}</span>
+ <span style={{ ...font('subheadline'), color: 'var(--secondary)', ...lineClamp(3) }}>{detail}</span>
+ </span>
+ <span style={{ minWidth: 8 }} />
+ {busy ? <ThinkingOrb size={16} color="var(--secondary)" /> : <Icon name="chevron.right" size={10} weight="semibold" color="var(--tertiary)" />}
+ </button>
+ )
+
+ const options: ReactNode[] = []
+ // Some CLIs drop the current sign-in the moment a new one starts: no options once signed in.
+ if (signedIn !== true && !checking) {
+ if (auth?.login) options.push(optionRow('login', 'Sign in', `In a terminal on ${host}. Links open here.`, 'terminal', () => setRoute({ kind: 'terminal', method: null })))
+ for (const m of auth?.methods ?? []) {
+ if (m.kind === 'terminal') options.push(optionRow(m.id, m.name, m.description ?? `In a terminal on ${host}.`, 'terminal', () => setRoute({ kind: 'terminal', method: m })))
+ else if (m.kind === 'envVar') options.push(optionRow(m.id, m.name, m.description ?? `Saved on ${host} only.`, 'key', () => setRoute({ kind: 'keys', method: m })))
+ else {
+ options.push(optionRow(
+ m.id,
+ m.name,
+ authenticating === m.id ? `Finish signing in in the browser on ${host}…` : (m.description ?? `Opens a browser on ${host}.`),
+ 'safari',
+ () => void authenticate(m),
+ authenticating === m.id,
+ ))
+ }
+ }
+ }
+
+ const overview = (
+ <div style={{ display: 'flex', flexDirection: 'column', height: '100%', background: 'var(--background)' }}>
+ <ModalHeader title={backend.name} />
+ <CardForm>
+ <CardSection>
+ <div style={{ display: 'flex', alignItems: 'center', gap: 14, padding: '4px 0' }}>
+ <AgentTile registry={backend.registry} icon={30} tile={52} radius={14} />
+ <div style={{ display: 'flex', flexDirection: 'column', gap: 2, minWidth: 0 }}>
+ <span style={font('title3', 'semibold')}>{backend.name}</span>
+ {backend.description ? <span style={{ ...font('subheadline'), color: 'var(--secondary)', ...lineClamp(3) }}>{backend.description}</span> : null}
+ </div>
+ </div>
+ </CardSection>
+ {curated ? (
+ <CardSection>
+ <SetupStepRow
+ number={1}
+ title="Install"
+ detail={installed ? `Installed on ${host}.` : backend.canInstall ? `Runs the official installer on ${host}.` : backend.installHint}
+ done={installed}
+ action={installed || !backend.canInstall ? null : { label: 'Install', run: () => setRoute({ kind: 'install' }) }}
+ />
+ </CardSection>
+ ) : null}
+ <CardSection
+ footer={signedIn !== true && auth?.methods.some((m) => m.kind === 'agent')
+ ? `Browser sign-ins open on ${host} itself. Away from it? Use Remote screen to finish there.`
+ : null}
+ >
+ <div style={{ display: 'flex', alignItems: 'center' }}>
+ <div style={{ flex: 1, minWidth: 0 }}>
+ <SetupStepRow number={curated ? 2 : 1} title="Sign in" detail={statusText} done={signedIn === true} action={null} />
+ </div>
+ {checking ? (
+ <ThinkingOrb state="connecting" size={16} color="var(--secondary)" />
+ ) : (
+ <button title="Check again" aria-label="Check again" style={{ display: 'flex', color: 'var(--secondary)' }} onClick={() => void check()}>
+ <Icon name="arrow.clockwise" size={13} />
+ </button>
+ )}
+ </div>
+ {signedIn !== true && auth?.detail ? <span className="selectable" style={{ ...font('footnote'), color: 'var(--secondary)', whiteSpace: 'pre-wrap' }}>{auth.detail}</span> : null}
+ {options}
+ </CardSection>
+ {error ? <CardSection><ErrorText text={error} /></CardSection> : null}
+ </CardForm>
+ </div>
+ )
+
+ return <SlideStack base={overview} top={routed} />
+}
+
+/** Keys an agent reads from its environment, kept on the computer. */
+function AgentKeysForm({ backend, method, saved, back, done }: {
+ backend: Backend
+ method: AuthMethod
+ saved: string[]
+ back: () => void
+ done: (auth: AgentAuth) => void
+}) {
+ const store = useStore()
+ const [values, setValues] = useState<Record<string, string>>({})
+ const [saving, setSaving] = useState(false)
+ const [error, setError] = useState<string | null>(null)
+ const vars = method.vars ?? []
+
+ const save = async (vars: Record<string, string>) => {
+ const client = store.client
+ if (!client) return
+ setSaving(true)
+ try {
+ done(await market.setAgentEnv(client, backend.id, vars))
+ back()
+ } catch (e) {
+ setError(errorText(e))
+ }
+ setSaving(false)
+ }
+
+ const missing = vars.some((v) => !v.optional && !values[v.name] && !saved.includes(v.name))
+ return (
+ <div style={{ display: 'flex', flexDirection: 'column', height: '100%', background: 'var(--background)' }}>
+ <ScreenHeader
+ title={method.name}
+ onBack={back}
+ trailing={saving ? <Spinner /> : <IconButton title="Save" icon="checkmark" disabled={missing} onClick={() => void save(filled(values))} />}
+ />
+ <CardForm>
+ <div style={{ display: 'flex', flexDirection: 'column', gap: 6 }}>
+ <CardSection title={method.name}>
+ {vars.map((v) => (
+ <div key={v.name} style={{ display: 'flex', flexDirection: 'column', gap: 4 }}>
+ <Field
+ label={v.label}
+ value={values[v.name] ?? ''}
+ secret={v.secret}
+ placeholder={saved.includes(v.name) ? 'Saved (type to replace)' : v.name}
+ onChange={(x) => setValues((old) => ({ ...old, [v.name]: x }))}
+ />
+ <span style={{ ...font('caption'), color: 'var(--secondary)' }}>{v.label + (v.optional ? ' (optional)' : '')}</span>
+ </div>
+ ))}
+ </CardSection>
+ <div style={{ display: 'flex', flexDirection: 'column', gap: 6, padding: '0 4px', ...font('caption'), color: 'var(--tertiary)' }}>
+ {method.description ? <span>{method.description}</span> : null}
+ <span>Saved on {store.hostName} only and given to {backend.name} when it starts.</span>
+ {method.link && URL.canParse(method.link) ? <WebLink title="Get a key" url={method.link} /> : null}
+ </div>
+ </div>
+ {saved.length ? (
+ <CardSection>
+ <button style={{ color: 'var(--danger)' }} onClick={() => void save(Object.fromEntries(vars.map((v) => [v.name, ''])))}>
+ Remove saved keys
+ </button>
+ </CardSection>
+ ) : null}
+ {error ? <CardSection><ErrorText text={error} /></CardSection> : null}
+ </CardForm>
+ </div>
+ )
+}
+
+function SetupStepRow({ number, title, detail, done, action }: {
+ number: number
+ title: string
+ detail: string
+ done: boolean
+ action: { label: string; run: () => void } | null
+}) {
+ return (
+ <div style={{ display: 'flex', alignItems: 'center', gap: 12, padding: '4px 0' }} aria-label={`${title}. ${detail}${done ? '. Done' : ''}`}>
+ <span aria-hidden style={{ width: 26, height: 26, borderRadius: '50%', flex: 'none', display: 'grid', placeItems: 'center', background: done ? 'var(--accent-fill)' : 'var(--bubble-agent)', transition: 'background-color var(--fade)' }}>
+ {done
+ ? <Icon name="checkmark" size={10} weight="bold" color="var(--on-accent)" />
+ : <span style={{ ...font('caption', 'semibold'), color: 'var(--secondary)' }}>{number}</span>}
+ </span>
+ <span style={{ display: 'flex', flexDirection: 'column', gap: 2, flex: 1, minWidth: 0 }}>
+ <span style={{ ...font('body', 'medium'), color: 'var(--text)' }}>{title}</span>
+ <span className="selectable" style={{ ...font('subheadline'), color: 'var(--secondary)' }}>{detail}</span>
+ </span>
+ <span style={{ minWidth: 8 }} />
+ {action ? (
+ <button className="capsule-action" style={{ ...font('subheadline', 'semibold'), padding: '7px 14px' }} onClick={action.run}>
+ {action.label}
+ </button>
+ ) : null}
+ </div>
+ )
+}
diff --git a/apps/desktop/src/renderer/views/marketplace/ComposioViews.tsx b/apps/desktop/src/renderer/views/marketplace/ComposioViews.tsx
new file mode 100644
index 00000000..63b78ec8
--- /dev/null
+++ b/apps/desktop/src/renderer/views/marketplace/ComposioViews.tsx
@@ -0,0 +1,330 @@
+import { useEffect, useRef, useState } from 'react'
+import { Button, CardForm, CardSection, IconButton, Spinner } from '../../components/Controls'
+import { Icon } from '../../components/Icon'
+import { ModalHeader, useDismiss } from '../../components/Overlay'
+import { font } from '../../lib/fonts'
+import { useStore } from '../../store/context'
+import {
+ appConnected, emptyPage, errorText, hasHiddenItems, market, replacePage, revealMore, sleep, visible,
+ type ComposioApp, type ComposioConnect, type ComposioStatus,
+} from './market-models'
+import { AppLogo, ErrorText, Field, FormSheet, ItemGrid, MarketRow, MarketSection, SkeletonGrid, TileIcon, WebLink, filled, lineClamp } from './pieces'
+import { pluginsFor } from './plugins'
+
+// Apps through Composio (host/src/market/composio.rs): set up a key once, connect
+// apps with Composio's hosted sign-in, then turn them on per bot like any
+// other connector.
+
+export { AppLogo }
+
+/** The Marketplace's "Apps" section. */
+export function ComposioSection({ query, searchToken }: {
+ query: string
+ /** Bumped by the Marketplace when the user submits a search. */
+ searchToken: number
+}) {
+ const store = useStore()
+ const [status, setStatus] = useState<ComposioStatus | null>(null)
+ const [apps, setApps] = useState(emptyPage<ComposioApp>)
+ const [loading, setLoading] = useState(true)
+ const [error, setError] = useState<string | null>(null)
+ const [settingUp, setSettingUp] = useState(false)
+ const [connecting, setConnecting] = useState<ComposioApp | null>(null)
+ const request = useRef(0)
+ const queryRef = useRef(query)
+ queryRef.current = query
+
+ const load = async () => {
+ const client = store.client
+ if (!client) return
+ const id = ++request.current
+ const search = queryRef.current
+ setLoading(true)
+ try {
+ const s = await market.composioStatus(client)
+ const page = s.configured ? await market.composioApps(client, search) : []
+ if (request.current !== id) return
+ setStatus(s)
+ setApps(replacePage(page, (a) => a.slug))
+ setError(null)
+ } catch (e) {
+ if (request.current === id) setError(errorText(e))
+ } finally {
+ if (request.current === id) setLoading(false)
+ }
+ }
+
+ useEffect(() => {
+ void load()
+ return () => void request.current++
+ }, [searchToken])
+
+ const configured = status?.configured === true
+ return (
+ <MarketSection title="Apps">
+ <div style={{ display: 'flex', flexDirection: 'column', gap: 10 }}>
+ {!configured ? (
+ <ItemGrid>
+ <MarketRow
+ title="Composio"
+ subtitle="Gmail, Slack, GitHub, Notion and 1,000 more apps"
+ added={false}
+ busy={status === null && loading}
+ addLabel="Set up"
+ icon={<TileIcon name="square.grid.3x3" />}
+ onAdd={() => setSettingUp(true)}
+ />
+ </ItemGrid>
+ ) : loading ? (
+ <SkeletonGrid />
+ ) : apps.items.length === 0 ? (
+ <span style={{ color: 'var(--secondary)' }}>{query ? `No apps match “${query}”.` : "Couldn't load apps."}</span>
+ ) : (
+ <ItemGrid>
+ {visible(apps).map((app) => (
+ <MarketRow
+ key={app.slug}
+ title={app.name}
+ subtitle={app.description ?? app.slug}
+ added={appConnected(app)}
+ addLabel="Connect"
+ icon={<AppLogo url={app.logo} name={app.name} />}
+ onAdd={() => setConnecting(app)}
+ />
+ ))}
+ </ItemGrid>
+ )}
+ {!loading && hasHiddenItems(apps) && configured ? (
+ <div style={{ display: 'flex', justifyContent: 'center' }}>
+ <Button kind="secondary" onClick={() => setApps(revealMore)}>Load more apps</Button>
+ </div>
+ ) : null}
+ {error ? <span style={{ ...font('footnote'), color: 'var(--danger)' }}>{error}</span> : null}
+ {configured ? (
+ <button style={{ display: 'flex', alignItems: 'center', gap: 4, alignSelf: 'flex-start', ...font('footnote'), color: 'var(--tertiary)' }} onClick={() => setSettingUp(true)}>
+ <Icon name="key" size={10} />
+ Composio settings
+ </button>
+ ) : null}
+ </div>
+ <FormSheet open={settingUp} onClose={() => setSettingUp(false)}>
+ <ComposioKeySheet
+ status={status}
+ done={(s) => {
+ setStatus(s)
+ void load()
+ }}
+ />
+ </FormSheet>
+ <FormSheet open={connecting !== null} onClose={() => setConnecting(null)}>
+ {connecting ? (
+ <ComposioConnectSheet
+ key={connecting.slug}
+ app={connecting}
+ done={() => {
+ void load()
+ void pluginsFor(store).refresh()
+ }}
+ />
+ ) : null}
+ </FormSheet>
+ </MarketSection>
+ )
+}
+
+/** Paste (or remove) the Composio API key; the host checks it with Composio first. */
+export function ComposioKeySheet({ status, done }: { status: ComposioStatus | null; done: (s: ComposioStatus) => void }) {
+ const store = useStore()
+ const dismiss = useDismiss()
+ const [key, setKey] = useState('')
+ const [saving, setSaving] = useState(false)
+ const [error, setError] = useState<string | null>(null)
+
+ const save = async (key: string) => {
+ const client = store.client
+ if (!client) return
+ setSaving(true)
+ try {
+ done(await market.setComposioKey(client, key))
+ dismiss()
+ } catch (e) {
+ setError(errorText(e))
+ }
+ setSaving(false)
+ }
+
+ const keyUrl = status?.keyUrl ?? 'https://platform.composio.dev'
+ return (
+ <div style={{ display: 'flex', flexDirection: 'column', height: '100%', background: 'var(--background)' }}>
+ <ModalHeader title="Composio" trailing={saving ? <Spinner /> : <IconButton title="Save" icon="checkmark" disabled={!key.trim()} onClick={() => void save(key)} />} />
+ <CardForm>
+ <CardSection
+ title="Composio API key"
+ footer={`Composio signs you in to apps like Gmail and Slack and runs their tools for your bots. The key stays on ${store.hostName}.`}
+ >
+ <Field label="API key" value={key} onChange={setKey} secret placeholder={status?.configured ? 'Saved (paste to replace)' : 'ak_…'} />
+ {URL.canParse(keyUrl) ? <WebLink title="Get a key from Composio" url={keyUrl} style={font('footnote')} /> : null}
+ </CardSection>
+ {status?.configured ? (
+ <CardSection footer="Bots lose the apps you connected until you add a key again.">
+ <button style={{ color: 'var(--danger)' }} onClick={() => void save('')}>Remove key</button>
+ </CardSection>
+ ) : null}
+ {error ? <CardSection><ErrorText text={error} /></CardSection> : null}
+ </CardForm>
+ </div>
+ )
+}
+
+/**
+ * Connects one app: Composio's sign-in page (opened here, finished anywhere),
+ * or a form for apps that use a key.
+ */
+export function ComposioConnectSheet({ app, requestId, done }: { app: ComposioApp; requestId?: string; done: () => void }) {
+ const store = useStore()
+ const dismiss = useDismiss()
+ const [plan, setPlan] = useState<ComposioConnect | null>(null)
+ const [values, setValues] = useState<Record<string, string>>({})
+ const [connected, setConnected] = useState(false)
+ const [saving, setSaving] = useState(false)
+ const [error, setError] = useState<string | null>(null)
+ const connectedRef = useRef(false)
+ const doneRef = useRef(done)
+ doneRef.current = done
+
+ const markConnected = () => {
+ setValues({})
+ connectedRef.current = true
+ setConnected(true)
+ }
+
+ useEffect(() => {
+ let cancelled = false
+ void (async () => {
+ const client = store.client
+ if (!client) return
+ try {
+ const plan = await market.composioConnect(client, app.slug)
+ if (cancelled) return
+ setPlan(plan)
+ if (plan.status !== 'redirect' || !plan.connection) return
+ if (plan.url && URL.canParse(plan.url)) window.codync.app.openExternal(plan.url)
+ // Polls until the sign-in finishes (or the sheet goes away).
+ for (let i = 0; i < 300; i++) {
+ await sleep(2000)
+ if (cancelled) return
+ const state = await market.composioConnection(client, plan.connection).catch(() => null)
+ if (cancelled) return
+ if (!state) continue
+ if (state.status === 'active') {
+ if (requestId) {
+ try {
+ await market.finishConnectionRequest(client, requestId, { connectorId: `composio-${app.slug}` })
+ } catch (e) {
+ if (!cancelled) setError(errorText(e))
+ return
+ }
+ }
+ if (!cancelled) markConnected()
+ return
+ }
+ if (state.status === 'failed' || state.status === 'expired') {
+ setError(`Signing in to ${app.name} didn't work (${state.status}). Close and try again.`)
+ return
+ }
+ }
+ setError('Timed out waiting for the sign-in.')
+ } catch (e) {
+ if (!cancelled) setError(errorText(e))
+ }
+ })()
+ return () => {
+ cancelled = true
+ if (connectedRef.current) doneRef.current()
+ }
+ }, [store, app.slug, app.name, requestId])
+
+ const submit = async () => {
+ const client = store.client
+ const mode = plan?.mode
+ if (!client || !mode) return
+ setSaving(true)
+ try {
+ const state = await market.composioConnectFields(client, app.slug, mode, filled(values))
+ if (state.status === 'active') {
+ if (requestId) await market.finishConnectionRequest(client, requestId, { connectorId: `composio-${app.slug}` })
+ markConnected()
+ } else {
+ setError(`${app.name} says the connection is ${state.status}.`)
+ }
+ } catch (e) {
+ setError(errorText(e))
+ }
+ setSaving(false)
+ }
+
+ const needsFields = plan?.status === 'needsFields'
+ const trailing = connected
+ ? <IconButton title="Done" icon="checkmark" onClick={dismiss} />
+ : needsFields
+ ? saving
+ ? <Spinner />
+ : <IconButton title="Connect" icon="checkmark" disabled={plan?.fields?.some((f) => f.required && !values[f.name]) ?? true} onClick={() => void submit()} />
+ : null
+
+ const waitingRow = (text: string) => (
+ <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
+ <Spinner />
+ <span style={{ color: 'var(--secondary)' }}>{text}</span>
+ </div>
+ )
+
+ return (
+ <div style={{ display: 'flex', flexDirection: 'column', height: '100%', background: 'var(--background)' }}>
+ <ModalHeader title={`Connect ${app.name}`} trailing={trailing} />
+ <CardForm>
+ <CardSection>
+ <div style={{ display: 'flex', alignItems: 'center', gap: 14, padding: '4px 0' }}>
+ <AppLogo url={app.logo} name={app.name} size={52} />
+ <div style={{ display: 'flex', flexDirection: 'column', gap: 2, minWidth: 0 }}>
+ <span style={font('title3', 'semibold')}>{app.name}</span>
+ {app.description ? <span style={{ ...font('subheadline'), color: 'var(--secondary)', ...lineClamp(3) }}>{app.description}</span> : null}
+ </div>
+ </div>
+ </CardSection>
+ {connected ? (
+ <CardSection footer={`Turn ${app.name} on for a bot in the bot's settings, under Connectors.`}>
+ <span style={{ display: 'flex', alignItems: 'center', gap: 6, color: 'var(--added)' }}>
+ <Icon name="checkmark.circle.fill" size={12} />
+ Connected
+ </span>
+ </CardSection>
+ ) : needsFields ? (
+ <CardSection footer={`Sent to Composio, which keeps it for ${app.name}.`}>
+ {(plan?.fields ?? []).map((f) => (
+ <div key={f.name} style={{ display: 'flex', flexDirection: 'column', gap: 4 }}>
+ <Field label={f.label} value={values[f.name] ?? ''} secret={f.secret} placeholder={f.label} onChange={(v) => setValues((old) => ({ ...old, [f.name]: v }))} />
+ {f.description ? <span style={{ ...font('caption'), color: 'var(--secondary)' }}>{f.description + (f.required ? '' : ' (optional)')}</span> : null}
+ </div>
+ ))}
+ </CardSection>
+ ) : plan?.status === 'redirect' ? (
+ <CardSection footer="Sign in on the page that opened. This updates by itself when you're done.">
+ {plan.url && URL.canParse(plan.url) ? (
+ <div>
+ <Button kind="secondary" onClick={() => window.codync.app.openExternal(plan.url!)}>
+ <Icon name="safari" size={11} />
+ Open the sign-in page
+ </Button>
+ </div>
+ ) : null}
+ {waitingRow('Waiting for you to finish signing in…')}
+ </CardSection>
+ ) : error === null ? (
+ <CardSection>{waitingRow('Asking Composio…')}</CardSection>
+ ) : null}
+ {error ? <CardSection><ErrorText text={error} /></CardSection> : null}
+ </CardForm>
+ </div>
+ )
+}
diff --git a/apps/desktop/src/renderer/views/marketplace/ConnectionRequestCard.tsx b/apps/desktop/src/renderer/views/marketplace/ConnectionRequestCard.tsx
index 03827743..3fed0634 100644
--- a/apps/desktop/src/renderer/views/marketplace/ConnectionRequestCard.tsx
+++ b/apps/desktop/src/renderer/views/marketplace/ConnectionRequestCard.tsx
@@ -1,4 +1,297 @@
+import { useEffect, useState } from 'react'
import type { Entry } from '@shared/models'
-export function ConnectionRequestCard(_: { entry: Entry }) {
- return <div style={{ padding: 16 }}>Connection request</div>
+import { Button, CardForm, CardSection, IconButton, Spinner } from '../../components/Controls'
+import { Icon } from '../../components/Icon'
+import { ModalHeader, useDismiss } from '../../components/Overlay'
+import { font } from '../../lib/fonts'
+import { useStore } from '../../store/context'
+import { ComposioConnectSheet } from './ComposioViews'
+import { InstallConnectorSheet } from './ConnectorSheets'
+import {
+ errorText, market, needsSignIn,
+ type ComposioApp, type ConnectionRequest, type CredentialStatus, type InstalledConnector, type MarketConnector, type SavedLogin,
+} from './market-models'
+import { ErrorText, Field, FormSheet, filled } from './pieces'
+import { pluginsFor, usePlugins } from './plugins'
+
+const caption = { ...font('caption'), color: 'var(--secondary)' }
+
+/** Credentials go through the setup API; only the request's state is a chat entry. */
+export function ConnectionRequestCard({ entry }: { entry: Entry }) {
+ const store = useStore()
+ const request = entry.data.connectionRequest as unknown as ConnectionRequest
+ const [app, setApp] = useState<ComposioApp | null>(null)
+ const [item, setItem] = useState<MarketConnector | null>(null)
+ const [value, setValue] = useState('')
+ const [username, setUsername] = useState('')
+ const [busy, setBusy] = useState(false)
+ const [error, setError] = useState<string | null>(null)
+ const isLogin = request.kind === 'login'
+ const isSecret = request.kind === 'secret'
+
+ const connect = async () => {
+ const client = store.client
+ if (!client) return
+ setBusy(true)
+ setError(null)
+ try {
+ if (request.kind === 'app' && request.toolkit) {
+ setApp({ slug: request.toolkit, name: request.title })
+ } else if (isLogin) {
+ await market.finishConnectionRequest(client, entry.id, { value, username })
+ setValue('')
+ } else if (isSecret) {
+ await market.finishConnectionRequest(client, entry.id, { value })
+ setValue('')
+ } else if (request.connectorId) {
+ const id = request.connectorId
+ const connector = (await market.connectors(client)).find((c) => c.id === id)
+ if (connector && needsSignIn(connector)) await pluginsFor(store).signIn(id)
+ await market.finishConnectionRequest(client, entry.id, { connectorId: id })
+ } else if (request.registryName) {
+ setItem(await market.connectorInfo(client, request.registryName))
+ }
+ } catch (e) {
+ setError(errorText(e))
+ }
+ setBusy(false)
+ }
+
+ const cancel = async () => {
+ const client = store.client
+ if (!client) return
+ setBusy(true)
+ try {
+ await market.finishConnectionRequest(client, entry.id, { cancel: true })
+ setValue('')
+ } catch (e) {
+ setError(errorText(e))
+ }
+ setBusy(false)
+ }
+
+ return (
+ <div style={{ display: 'flex', flexDirection: 'column', gap: 12, padding: 16, background: 'var(--surface)', borderRadius: 18 }}>
+ <span style={{ display: 'flex', alignItems: 'center', gap: 6, ...font('headline') }}>
+ <Icon name="lock.shield" size={13} />
+ {request.title}
+ </span>
+ {entry.data.text ? <span style={{ color: 'var(--secondary)' }}>{entry.data.text}</span> : null}
+ {request.status === 'pending' ? (
+ <>
+ {isLogin ? (
+ <>
+ <Field value={username} onChange={setUsername} placeholder="Username or email" />
+ <Field value={value} onChange={setValue} placeholder="Password or op:// reference" secret />
+ <span style={caption}>Saved securely on this computer. The bot types it into the sign-in page but never sees it.</span>
+ </>
+ ) : isSecret ? (
+ <>
+ <span style={caption}>{`${request.field ?? 'Credential'} · ${request.location ?? ''}`}</span>
+ <Field value={value} onChange={setValue} placeholder="Credential or op:// reference" secret />
+ <span style={caption}>Saved securely on this computer. Never added to the conversation.</span>
+ </>
+ ) : null}
+ <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
+ <Button disabled={busy || ((isSecret || isLogin) && !value)} onClick={() => void connect()}>
+ {isLogin ? 'Save login' : isSecret ? 'Save and connect' : 'Connect'}
+ </Button>
+ <Button kind="secondary" disabled={busy} onClick={() => void cancel()}>Cancel</Button>
+ {busy ? <Spinner /> : null}
+ </div>
+ </>
+ ) : (
+ <span style={{ color: 'var(--secondary)' }}>{request.status === 'ready' ? (isLogin ? 'Saved' : 'Connected') : 'Cancelled'}</span>
+ )}
+ {error ? <span style={{ ...font('footnote'), color: 'var(--danger)' }}>{error}</span> : null}
+ <FormSheet open={item !== null} onClose={() => setItem(null)}>
+ {item ? <InstallConnectorSheet item={item} requestId={entry.id} done={() => setItem(null)} /> : null}
+ </FormSheet>
+ <FormSheet open={app !== null} onClose={() => setApp(null)}>
+ {app ? <ComposioConnectSheet app={app} requestId={entry.id} done={() => setApp(null)} /> : null}
+ </FormSheet>
+ </div>
+ )
+}
+
+export function CredentialsView() {
+ const store = useStore()
+ const plugins = usePlugins(store)
+ const [selected, setSelected] = useState<InstalledConnector | null>(null)
+ const [token, setToken] = useState('')
+ const [status, setStatus] = useState<CredentialStatus | null>(null)
+ const [busy, setBusy] = useState(false)
+ const [error, setError] = useState<string | null>(null)
+
+ useEffect(() => {
+ void (async () => {
+ await plugins.refresh()
+ const client = store.client
+ if (!client) return
+ try {
+ setStatus(await market.credentialStatus(client))
+ } catch (e) {
+ setError(errorText(e))
+ }
+ })()
+ }, [plugins, store])
+
+ const save = async (value: string) => {
+ const client = store.client
+ if (!client) return
+ setBusy(true)
+ setError(null)
+ try {
+ setStatus(await market.setOnePasswordToken(client, value))
+ setToken('')
+ } catch (e) {
+ setError(errorText(e))
+ }
+ setBusy(false)
+ }
+
+ return (
+ <div style={{ display: 'flex', flexDirection: 'column', height: '100%', background: 'var(--background)' }}>
+ <ModalHeader title="Credentials" />
+ <CardForm>
+ <CardSection title="Storage">
+ <span>{status?.provider ?? 'System credential store'}</span>
+ <span style={{ color: 'var(--secondary)' }}>Credentials are encrypted on the connected computer. Linux uses Secret Service; macOS uses Keychain.</span>
+ </CardSection>
+ <CardSection title="Saved connections">
+ {plugins.installedConnectors.filter((c) => c.keys.length > 0).map((c) => (
+ <button key={c.id} className="plain-row-button" onClick={() => setSelected(c)}>
+ <span style={{ flex: 1 }}>{c.name}</span>
+ <Icon name="chevron.right" size={10} />
+ </button>
+ ))}
+ <span style={caption}>Values stay hidden. Select a connection to replace its credentials.</span>
+ </CardSection>
+ <LoginsSection />
+ <CardSection title="1Password" footer="Use a service account with read access only to a dedicated shared vault. The 1Password CLI must be installed on the computer.">
+ <span>{status?.onePasswordConnected ? 'Connected' : 'Connect a shared vault'}</span>
+ <Field value={token} onChange={setToken} placeholder="Service account token" secret />
+ <div><Button disabled={busy || !token} onClick={() => void save(token)}>Connect</Button></div>
+ {status?.onePasswordConnected ? <div><Button kind="secondary" disabled={busy} onClick={() => void save('')}>Disconnect</Button></div> : null}
+ <span style={{ ...font('footnote'), color: 'var(--secondary)' }}>Use op://vault/item/field in a connector's credential field. Codync retrieves the value when the connector runs.</span>
+ </CardSection>
+ {busy ? <Spinner /> : null}
+ {error ? <ErrorText text={error} /> : null}
+ </CardForm>
+ <FormSheet open={selected !== null} onClose={() => setSelected(null)}>
+ {selected ? <ConnectorCredentialEditor key={selected.id} connector={selected} /> : null}
+ </FormSheet>
+ </div>
+ )
+}
+
+/** Website and app logins bots type through the computer tool; passwords stay hidden. */
+function LoginsSection() {
+ const store = useStore()
+ const [logins, setLogins] = useState<SavedLogin[]>([])
+ const [site, setSite] = useState('')
+ const [username, setUsername] = useState('')
+ const [password, setPassword] = useState('')
+ const [busy, setBusy] = useState(false)
+ const [error, setError] = useState<string | null>(null)
+
+ const load = async () => {
+ const client = store.client
+ try {
+ setLogins(client ? await market.logins(client) : [])
+ } catch (e) {
+ setError(errorText(e))
+ }
+ }
+
+ useEffect(() => {
+ void load()
+ }, [])
+
+ const save = async () => {
+ const client = store.client
+ if (!client) return
+ setBusy(true)
+ setError(null)
+ try {
+ await market.saveLogin(client, site, username, password)
+ setSite('')
+ setUsername('')
+ setPassword('')
+ await load()
+ } catch (e) {
+ setError(errorText(e))
+ }
+ setBusy(false)
+ }
+
+ const remove = async (login: SavedLogin) => {
+ const client = store.client
+ if (!client) return
+ try {
+ await market.removeLogin(client, login.id)
+ setLogins((old) => old.filter((l) => l.id !== login.id))
+ } catch (e) {
+ setError(errorText(e))
+ }
+ }
+
+ return (
+ <CardSection title="Sign-ins" footer="Bots type these into the matching website or app and never see the password. You can use an op:// reference.">
+ {logins.map((login) => (
+ <div key={login.id} style={{ display: 'flex', alignItems: 'center' }}>
+ <div style={{ display: 'flex', flexDirection: 'column', gap: 2, flex: 1, minWidth: 0 }}>
+ <span>{login.site}</span>
+ {login.username ? <span style={caption}>{login.username}</span> : null}
+ </div>
+ <IconButton title="Remove" icon="trash" onClick={() => void remove(login)} />
+ </div>
+ ))}
+ <Field value={site} onChange={setSite} placeholder="Website or app, e.g. github.com" />
+ <Field value={username} onChange={setUsername} placeholder="Username or email" />
+ <Field value={password} onChange={setPassword} placeholder="Password or op:// reference" secret />
+ <div><Button disabled={busy || !site || !password} onClick={() => void save()}>Save sign-in</Button></div>
+ {error ? <span style={{ ...font('footnote'), color: 'var(--danger)' }}>{error}</span> : null}
+ </CardSection>
+ )
+}
+
+function ConnectorCredentialEditor({ connector }: { connector: InstalledConnector }) {
+ const store = useStore()
+ const dismiss = useDismiss()
+ const [fields, setFields] = useState<Record<string, string>>({})
+ const [saving, setSaving] = useState(false)
+ const [error, setError] = useState<string | null>(null)
+
+ const save = async () => {
+ const client = store.client
+ if (!client) return
+ setSaving(true)
+ setError(null)
+ try {
+ await market.updateConnectorCredentials(client, connector.id, filled(fields))
+ setFields({})
+ dismiss()
+ } catch (e) {
+ setError(errorText(e))
+ }
+ setSaving(false)
+ }
+
+ return (
+ <div style={{ display: 'flex', flexDirection: 'column', height: '100%', background: 'var(--background)' }}>
+ <ModalHeader
+ title={connector.name}
+ trailing={saving ? <Spinner /> : <IconButton title="Save credentials" icon="checkmark" disabled={Object.values(fields).every((v) => !v)} onClick={() => void save()} />}
+ />
+ <CardForm>
+ <CardSection footer="Enter only the values you want to replace. You can use an op:// reference.">
+ {connector.keys.map((key) => (
+ <Field key={key} label={key} value={fields[key] ?? ''} placeholder={key} secret onChange={(v) => setFields((old) => ({ ...old, [key]: v }))} />
+ ))}
+ </CardSection>
+ {error ? <ErrorText text={error} /> : null}
+ </CardForm>
+ </div>
+ )
}
diff --git a/apps/desktop/src/renderer/views/marketplace/ConnectorSheets.tsx b/apps/desktop/src/renderer/views/marketplace/ConnectorSheets.tsx
new file mode 100644
index 00000000..41a255fb
--- /dev/null
+++ b/apps/desktop/src/renderer/views/marketplace/ConnectorSheets.tsx
@@ -0,0 +1,219 @@
+import { useEffect, useRef, useState } from 'react'
+import { CardForm, CardSection, ChoiceList, IconButton, SegmentedChoice, Spinner } from '../../components/Controls'
+import { ModalHeader, useDismiss } from '../../components/Overlay'
+import { font } from '../../lib/fonts'
+import { useStore } from '../../store/context'
+import { errorText, market, needsSignIn, type InstalledConnector, type MarketConnector, type MarketInput } from './market-models'
+import { ErrorText, Field, WebLink } from './pieces'
+import { usePlugins } from './plugins'
+
+const sheetBody = { display: 'flex', flexDirection: 'column', height: '100%', background: 'var(--background)' } as const
+
+export function InstallConnectorSheet({ item, requestId, done }: { item: MarketConnector; requestId?: string; done: () => void }) {
+ const store = useStore()
+ const plugins = usePlugins(store)
+ const dismiss = useDismiss()
+ const [optionId, setOptionId] = useState(item.options[0]?.id ?? '')
+ const [values, setValues] = useState<Record<string, string>>({})
+ const [installed, setInstalled] = useState<InstalledConnector | null>(null)
+ const [saving, setSaving] = useState(false)
+ const [error, setError] = useState<string | null>(null)
+ const started = useRef(false)
+ const option = item.options.find((o) => o.id === optionId) ?? item.options[0]
+ const value = (input: MarketInput) => values[input.name] ?? ''
+
+ const install = async () => {
+ if (!option) return
+ setSaving(true)
+ setError(null)
+ try {
+ const client = await store.ready()
+ let c: InstalledConnector
+ if (installed) c = (await market.connectors(client)).find((x) => x.id === installed.id) ?? installed
+ else {
+ c = await plugins.installConnector(item, option.id, values)
+ setInstalled(c)
+ setValues({})
+ }
+ if (needsSignIn(c)) await plugins.signIn(c.id)
+ if (requestId) await market.finishConnectionRequest(client, requestId, { connectorId: c.id })
+ else await market.verifyConnector(client, c.id)
+ await plugins.refresh()
+ done()
+ dismiss()
+ } catch (e) {
+ setError(errorText(e))
+ }
+ setSaving(false)
+ }
+
+ useEffect(() => {
+ if (started.current) return
+ started.current = true
+ if (item.options.length === 1 && item.options[0].inputs.length === 0) void install()
+ }, [])
+
+ const missing = !option || option.inputs.some((i) => i.required && !(values[i.name] ?? i.default ?? ''))
+ return (
+ <div style={sheetBody}>
+ <ModalHeader
+ title="Add connector"
+ trailing={saving ? <Spinner /> : <IconButton title="Add" icon="plus" disabled={installed === null && missing} onClick={() => void install()} />}
+ />
+ <CardForm>
+ <CardSection>
+ <div style={{ display: 'flex', flexDirection: 'column', gap: 6, padding: '4px 0' }}>
+ <span style={font('title3', 'semibold')}>{item.title}</span>
+ {item.description ? <span style={{ color: 'var(--secondary)' }}>{item.description}</span> : null}
+ {item.website && URL.canParse(item.website) ? (
+ <WebLink title={item.website} url={item.website} style={{ ...font('footnote'), whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' }} />
+ ) : null}
+ </div>
+ </CardSection>
+ {item.options.length > 1 ? (
+ <CardSection title="Runs">
+ <ChoiceList
+ selection={optionId}
+ onChange={setOptionId}
+ options={item.options.map((o) => ({ id: o.id, label: o.kind === 'remote' ? `Hosted by ${item.title}` : `On ${store.hostName} (${o.kind})` }))}
+ />
+ </CardSection>
+ ) : null}
+ {option ? (
+ <CardSection title="Setup" footer={`Keys are saved on ${store.hostName} only.`}>
+ {option.inputs.length === 0 ? (
+ <span style={{ color: 'var(--secondary)' }}>
+ {option.kind === 'remote' ? `No keys needed here. If ${item.title} wants you to sign in, its sign-in page opens next.` : 'No setup needed.'}
+ </span>
+ ) : null}
+ {option.inputs.map((input) => (
+ <div key={input.name} style={{ display: 'flex', flexDirection: 'column', gap: 4 }}>
+ <Field
+ label={input.name}
+ value={value(input)}
+ secret={input.secret}
+ placeholder={input.secret ? input.name : (input.default ?? input.placeholder ?? input.name)}
+ onChange={(v) => setValues((old) => ({ ...old, [input.name]: v }))}
+ />
+ {input.description ? (
+ <span style={{ ...font('caption'), color: 'var(--secondary)' }}>{input.description + (input.required ? '' : ' (optional)')}</span>
+ ) : null}
+ </div>
+ ))}
+ </CardSection>
+ ) : null}
+ {error ? <CardSection><ErrorText text={error} /></CardSection> : null}
+ </CardForm>
+ </div>
+ )
+}
+
+type Mode = 'command' | 'url' | 'config'
+
+export function CustomConnectorSheet() {
+ const store = useStore()
+ const plugins = usePlugins(store)
+ const dismiss = useDismiss()
+ const [name, setName] = useState('')
+ const [mode, setMode] = useState<Mode>('command')
+ const [target, setTarget] = useState('')
+ const [envText, setEnvText] = useState('')
+ const [config, setConfig] = useState('')
+ const [saving, setSaving] = useState(false)
+ const [error, setError] = useState<string | null>(null)
+ const ready = mode === 'config' ? config.trim() !== '' : name.trim() !== '' && target.trim() !== ''
+
+ const footer = {
+ command: `Runs on ${store.hostName} in the bot's project folder. Quotes work like in a shell.`,
+ url: 'A remote MCP server, streamable HTTP or SSE.',
+ config: `Paste the MCP config from a README or another app (Claude, Cursor, VS Code). Every server in it is added. Saved on ${store.hostName} only.`,
+ }[mode]
+
+ const save = async () => {
+ setSaving(true)
+ setError(null)
+ const remote = mode === 'url'
+ const pairs: Record<string, string> = {}
+ for (const line of envText.split('\n')) {
+ const at = line.indexOf(remote ? ':' : '=')
+ if (at < 0) continue
+ const key = line.slice(0, at).trim()
+ if (key) pairs[key] = line.slice(at + 1).trim()
+ }
+ try {
+ const added = mode === 'config'
+ ? await plugins.importConnectors(config)
+ : [await plugins.addConnector({ name, command: remote ? null : target, url: remote ? target : null, env: remote ? {} : pairs, headers: remote ? pairs : {} })]
+ for (const c of added) if (needsSignIn(c)) await plugins.signIn(c.id)
+ dismiss()
+ } catch (e) {
+ setError(errorText(e))
+ }
+ setSaving(false)
+ }
+
+ return (
+ <div style={sheetBody}>
+ <ModalHeader title="Custom connector" trailing={saving ? <Spinner /> : <IconButton title="Add" icon="plus" disabled={!ready} onClick={() => void save()} />} />
+ <CardForm>
+ <CardSection footer={footer}>
+ <SegmentedChoice selection={mode} onChange={setMode} options={[{ id: 'command', label: 'Command' }, { id: 'url', label: 'URL' }, { id: 'config', label: 'Config' }]} />
+ {mode === 'config' ? <Field value={config} onChange={setConfig} placeholder={'{ "mcpServers": { … } }'} lines={[6, 16]} mono /> : null}
+ {mode !== 'config' ? <Field value={name} onChange={setName} placeholder="Name" /> : null}
+ {mode !== 'config' ? <Field value={target} onChange={setTarget} placeholder={mode === 'url' ? 'https://example.com/mcp' : 'npx -y @scope/server'} mono /> : null}
+ </CardSection>
+ {mode === 'url' ? (
+ <CardSection title="Headers" footer={`Optional, one Name: value per line. Leave empty if the service has you sign in. Saved on ${store.hostName} only.`}>
+ <Field value={envText} onChange={setEnvText} placeholder="Authorization: Bearer …" lines={[2, 6]} mono />
+ </CardSection>
+ ) : mode === 'command' ? (
+ <CardSection title="Environment" footer={`One KEY=value per line. Saved on ${store.hostName} only.`}>
+ <Field value={envText} onChange={setEnvText} placeholder="API_KEY=…" lines={[2, 6]} mono />
+ </CardSection>
+ ) : null}
+ {error ? <CardSection><ErrorText text={error} /></CardSection> : null}
+ </CardForm>
+ </div>
+ )
+}
+
+export function NewSkillSheet() {
+ const store = useStore()
+ const plugins = usePlugins(store)
+ const dismiss = useDismiss()
+ const [name, setName] = useState('')
+ const [summary, setSummary] = useState('')
+ const [instructions, setInstructions] = useState('')
+ const [saving, setSaving] = useState(false)
+ const [error, setError] = useState<string | null>(null)
+
+ const save = async () => {
+ setSaving(true)
+ try {
+ await plugins.addSkill(name, summary, instructions)
+ dismiss()
+ } catch (e) {
+ setError(errorText(e))
+ }
+ setSaving(false)
+ }
+
+ return (
+ <div style={sheetBody}>
+ <ModalHeader
+ title="New skill"
+ trailing={saving ? <Spinner /> : <IconButton title="Save" icon="checkmark" disabled={!name.trim() || !instructions.trim()} onClick={() => void save()} />}
+ />
+ <CardForm>
+ <CardSection footer="The bot sees the name and when to use it, and reads the instructions only when a task fits.">
+ <Field value={name} onChange={setName} placeholder="Name" />
+ <Field value={summary} onChange={setSummary} placeholder="When to use it" lines={[2, 4]} />
+ </CardSection>
+ <CardSection title="Instructions">
+ <Field value={instructions} onChange={setInstructions} placeholder="Step by step, in plain words…" lines={[6, 20]} />
+ </CardSection>
+ {error ? <CardSection><ErrorText text={error} /></CardSection> : null}
+ </CardForm>
+ </div>
+ )
+}
diff --git a/apps/desktop/src/renderer/views/marketplace/MarketplaceView.tsx b/apps/desktop/src/renderer/views/marketplace/MarketplaceView.tsx
index b3860541..caef343d 100644
--- a/apps/desktop/src/renderer/views/marketplace/MarketplaceView.tsx
+++ b/apps/desktop/src/renderer/views/marketplace/MarketplaceView.tsx
@@ -1,3 +1,456 @@
-export function MarketplaceView(_: { computers: { id: string; name: string }[]; computer: string; onComputer: (id: string) => void }) {
- return <div style={{ padding: 24 }}>Marketplace</div>
+import { useEffect, useRef, useState, type ReactNode } from 'react'
+import type { Backend } from '@shared/models'
+import { Button, CardForm, CardSection, DropdownMenu, IconButton, Spinner } from '../../components/Controls'
+import { Icon } from '../../components/Icon'
+import { Dialog, useDismiss } from '../../components/Overlay'
+import { font } from '../../lib/fonts'
+import { useStore } from '../../store/context'
+import { ComputerBadge } from '../ComputerBadge'
+import { AgentSheet, AgentTile } from './AgentSheet'
+import { ComposioSection } from './ComposioViews'
+import { CredentialsView } from './ConnectionRequestCard'
+import { CustomConnectorSheet, InstallConnectorSheet, NewSkillSheet } from './ConnectorSheets'
+import {
+ appendPage, emptyPage, errorText, hasHiddenItems, market, needsSignIn, replacePage, revealMore, visible,
+ type MarketConnector, type MarketSkill,
+} from './market-models'
+import { AppLogo, FormSheet, ItemGrid, MarketRow, MarketSection, ScreenHeader, ServiceLogo, SkeletonGrid, SkillGlyph, SlideStack, TileIcon } from './pieces'
+import { usePlugins } from './plugins'
+
+const connectorId = (c: MarketConnector) => c.name
+
+/**
+ * The Marketplace (Grok Bot's layout): one page of agents, connectors and
+ * skills for the bots on one computer (the store in context). Installing happens on that
+ * computer; each bot turns connectors and skills on in its settings.
+ * With more than one computer, the header switches between them (the caller swaps the store).
+ */
+export function MarketplaceView({ computers, computer, onComputer }: { computers: { id: string; name: string }[]; computer: string; onComputer: (id: string) => void }) {
+ const store = useStore()
+ const plugins = usePlugins(store)
+ const dismiss = useDismiss()
+ const [search, setSearch] = useState('')
+ /** Bumped on each submitted search, so every section searches again. */
+ const [searchToken, setSearchToken] = useState(0)
+ const [connectors, setConnectors] = useState(emptyPage<MarketConnector>)
+ /** The registry page after the ones shown; null at the end. */
+ const [connectorCursor, setConnectorCursor] = useState<string | null>(null)
+ const [loadingMore, setLoadingMore] = useState(false)
+ const [skills, setSkills] = useState<MarketSkill[]>([])
+ const [loadingConnectors, setLoadingConnectors] = useState(true)
+ const [loadingSkills, setLoadingSkills] = useState(true)
+ const [error, setError] = useState<string | null>(null)
+ const [installing, setInstalling] = useState<MarketConnector | null>(null)
+ const [busySkill, setBusySkill] = useState<string | null>(null)
+ const [addingConnector, setAddingConnector] = useState(false)
+ const [writingSkill, setWritingSkill] = useState(false)
+ const [agent, setAgent] = useState<Backend | null>(null)
+ /** Agents shown before "Load more agents" (two rows of the grid). */
+ const [agentLimit, setAgentLimit] = useState(10)
+ const [showInstalled, setShowInstalled] = useState(false)
+ const [showCredentials, setShowCredentials] = useState(false)
+ const request = useRef(0)
+ const connectorQuery = useRef('')
+
+ const query = search.trim()
+ const matches = (text: string | null | undefined) => (text ?? '').toLowerCase().includes(query.toLowerCase())
+ const allAgents = (store.hello?.backends ?? []).filter((b) => b.available || b.installed === true || b.curated === true)
+ const agents = query ? allAgents.filter((b) => matches(b.name)) : allAgents
+ const shownSkills = query ? skills.filter((s) => matches(s.name) || matches(s.description)) : skills
+ const installedCount = plugins.installedConnectors.length + plugins.installedSkills.length
+
+ const loadConnectors = async (q = query) => {
+ const id = ++request.current
+ connectorQuery.current = q
+ setLoadingConnectors(true)
+ setLoadingMore(false)
+ try {
+ const page = await market.marketConnectors(await store.ready(), q)
+ if (request.current !== id) return
+ setConnectors(replacePage(page.items, connectorId))
+ setConnectorCursor(page.next)
+ setError(null)
+ } catch (e) {
+ if (request.current !== id) return
+ setConnectors(emptyPage())
+ setConnectorCursor(null)
+ setError(errorText(e))
+ } finally {
+ if (request.current === id) setLoadingConnectors(false)
+ }
+ }
+
+ const loadMoreConnectors = async () => {
+ if (loadingMore || loadingConnectors) return
+ if (hasHiddenItems(connectors)) {
+ setConnectors(revealMore)
+ return
+ }
+ if (!connectorCursor) return
+ const id = request.current
+ setLoadingMore(true)
+ try {
+ const page = await market.marketConnectors(await store.ready(), connectorQuery.current, connectorCursor)
+ if (request.current !== id) return
+ setConnectors((old) => revealMore(appendPage(old, page.items, connectorId)))
+ setConnectorCursor(page.next)
+ setError(null)
+ } catch (e) {
+ if (request.current === id) setError(errorText(e))
+ } finally {
+ if (request.current === id) setLoadingMore(false)
+ }
+ }
+
+ useEffect(() => {
+ void plugins.refresh()
+ // Picks up CLIs installed or signed in outside Codync.
+ void store.refreshBackends()
+ void loadConnectors('')
+ void (async () => {
+ try {
+ setSkills(await market.marketSkills(await store.ready()))
+ } catch (e) {
+ setError(errorText(e))
+ }
+ setLoadingSkills(false)
+ })()
+ return () => void request.current++
+ // Mount only, like the view's `.task`s.
+ }, [])
+
+ const submit = (q: string) => {
+ setSearchToken((t) => t + 1)
+ void loadConnectors(q)
+ }
+
+ const installSkill = async (s: MarketSkill) => {
+ setBusySkill(s.source)
+ try {
+ await plugins.installSkill(s.source)
+ } catch (e) {
+ setError(errorText(e))
+ }
+ setBusySkill(null)
+ }
+
+ const header = (
+ <div style={{ display: 'flex', alignItems: 'center', paddingTop: 28, gap: 8 }}>
+ {/* Everything here lives on one computer; say which. */}
+ <div style={{ display: 'flex', alignItems: 'center', gap: 10, minWidth: 0 }}>
+ <ComputerBadge computer={store.computer} size={32} />
+ <div style={{ display: 'flex', flexDirection: 'column', minWidth: 0, whiteSpace: 'nowrap' }}>
+ <span style={{ ...font('title2', 'semibold'), color: 'var(--text)' }}>Marketplace</span>
+ {computers.length > 1 ? (
+ <DropdownMenu
+ title="Switch computer"
+ style={{ display: 'flex', alignItems: 'center', gap: 4, ...font('subheadline'), color: 'var(--secondary)', minWidth: 0 }}
+ items={() => computers.map((c) => ({ title: c.name, selected: c.id === computer, action: () => onComputer(c.id) }))}
+ >
+ <span aria-label={`Computer: ${store.hostName}`} style={{ overflow: 'hidden', textOverflow: 'ellipsis' }}>{store.hostName}</span>
+ <Icon name="chevron.down" size={10} weight="semibold" />
+ </DropdownMenu>
+ ) : (
+ <span style={{ ...font('subheadline'), color: 'var(--secondary)', overflow: 'hidden', textOverflow: 'ellipsis' }}>{store.hostName}</span>
+ )}
+ </div>
+ </div>
+ <span style={{ flex: 1 }} />
+ {installedCount > 0 ? (
+ <button className="press" style={{ display: 'flex', alignItems: 'center', gap: 8, flex: 'none' }} onClick={() => setShowInstalled(true)}>
+ <span style={{ display: 'flex' }}>
+ {plugins.installedConnectors.slice(0, 3).map((c, i) => (
+ <span key={c.id} style={{ marginLeft: i ? -8 : 0, display: 'flex' }}>
+ <ServiceLogo name={c.name} registryName={c.registryName} size={26} />
+ </span>
+ ))}
+ </span>
+ <span style={{ color: 'var(--secondary)', whiteSpace: 'nowrap' }}>{installedCount} installed</span>
+ <Icon name="chevron.right" size={10} weight="semibold" color="var(--tertiary)" />
+ </button>
+ ) : null}
+ </div>
+ )
+
+ const searchField = (
+ <div className="market-search">
+ <Icon name="magnifyingglass" size={13} color="var(--secondary)" />
+ <input
+ value={search}
+ placeholder="Search agents, connectors and skills"
+ aria-label="Search agents, connectors and skills"
+ spellCheck={false}
+ onChange={(e) => {
+ setSearch(e.target.value)
+ setAgentLimit(10)
+ }}
+ onKeyDown={(e) => {
+ if (e.key === 'Enter' && !e.nativeEvent.isComposing) submit(search.trim())
+ }}
+ />
+ {search ? (
+ <button
+ aria-label="Clear search"
+ title="Clear search"
+ style={{ display: 'flex', color: 'var(--tertiary)' }}
+ onClick={() => {
+ setSearch('')
+ setAgentLimit(10)
+ submit('')
+ }}
+ >
+ <Icon name="xmark.circle.fill" size={13} />
+ </button>
+ ) : null}
+ </div>
+ )
+
+ const centered = (node: ReactNode) => <div style={{ display: 'flex', justifyContent: 'center' }}>{node}</div>
+
+ const page = (
+ <div className="market-page">
+ <div className="market-scroll">
+ <div className="market-page-content">
+ {header}
+ {searchField}
+ {agents.length > 0 ? (
+ <MarketSection title={query ? `Agents matching “${query}”` : 'Agents'}>
+ <div className="agent-grid">
+ {agents.slice(0, agentLimit).map((b) => <AgentCard key={b.id} backend={b} onClick={() => setAgent(b)} />)}
+ </div>
+ {agents.length > agentLimit ? centered(<Button kind="secondary" onClick={() => setAgentLimit((n) => n + 10)}>Load more agents</Button>) : null}
+ </MarketSection>
+ ) : null}
+ <MarketSection title="Connectors">
+ {loadingConnectors ? (
+ <SkeletonGrid />
+ ) : connectors.items.length === 0 ? (
+ <span style={{ color: 'var(--secondary)' }}>{query ? `No connectors match “${query}”.` : "Couldn't load connectors."}</span>
+ ) : (
+ <>
+ <ItemGrid>
+ {visible(connectors).map((c) => (
+ <MarketRow
+ key={c.name}
+ title={c.title}
+ subtitle={c.description ?? c.name}
+ added={c.installed}
+ icon={<ServiceLogo website={c.website} name={c.title} registryName={c.name} />}
+ onAdd={() => setInstalling(c)}
+ />
+ ))}
+ </ItemGrid>
+ {hasHiddenItems(connectors) || connectorCursor
+ ? centered(
+ <Button kind="secondary" disabled={loadingMore} onClick={() => void loadMoreConnectors()}>
+ {loadingMore ? 'Loading…' : 'Load more connectors'}
+ </Button>,
+ )
+ : null}
+ </>
+ )}
+ </MarketSection>
+ <ComposioSection query={query} searchToken={searchToken} />
+ <MarketSection title="Skills">
+ {loadingSkills ? (
+ <SkeletonGrid />
+ ) : (
+ <ItemGrid>
+ {shownSkills.map((s) => (
+ <MarketRow
+ key={s.source}
+ title={s.name}
+ subtitle={s.description}
+ added={s.installed || plugins.installedSkills.some((i) => i.id === s.source.toLowerCase())}
+ busy={busySkill === s.source}
+ icon={<SkillGlyph />}
+ onAdd={() => void installSkill(s)}
+ />
+ ))}
+ </ItemGrid>
+ )}
+ </MarketSection>
+ <div>
+ <Button kind="secondary" onClick={() => setShowCredentials(true)}>
+ <Icon name="lock.shield" size={11} />
+ Credentials
+ </Button>
+ </div>
+ <MarketSection title="Make your own">
+ <ItemGrid>
+ <MarketRow
+ title="Custom connector"
+ subtitle="Any MCP server: a command or a URL"
+ added={false}
+ addLabel="New"
+ icon={<TileIcon name="point.3.connected.trianglepath.dotted" />}
+ onAdd={() => setAddingConnector(true)}
+ />
+ <MarketRow
+ title="Your own skill"
+ subtitle="Write instructions a bot can follow"
+ added={false}
+ addLabel="New"
+ icon={<TileIcon name="square.and.pencil" />}
+ onAdd={() => setWritingSkill(true)}
+ />
+ </ItemGrid>
+ </MarketSection>
+ {error ? <span style={{ ...font('footnote'), color: 'var(--danger)' }}>{error}</span> : null}
+ <span style={{ ...font('footnote'), color: 'var(--tertiary)' }}>
+ Connectors come from the official MCP Registry, apps through Composio, skills from Anthropic, agents from the ACP registry. Everything installs on {store.hostName}.
+ </span>
+ </div>
+ </div>
+ {/* Dismissing slides the sheet away; clearing the caller's state would cut it off. */}
+ <div className="market-close">
+ <IconButton title="Close" icon="xmark" onClick={dismiss} />
+ </div>
+ </div>
+ )
+
+ return (
+ <>
+ <SlideStack base={page} top={showInstalled ? <InstalledView back={() => setShowInstalled(false)} /> : null} />
+ <FormSheet open={installing !== null} onClose={() => setInstalling(null)}>
+ {installing ? <InstallConnectorSheet key={installing.name} item={installing} done={() => void loadConnectors(connectorQuery.current)} /> : null}
+ </FormSheet>
+ <FormSheet open={showCredentials} onClose={() => setShowCredentials(false)} width={560} height={720}>
+ <CredentialsView />
+ </FormSheet>
+ <FormSheet open={addingConnector} onClose={() => setAddingConnector(false)}>
+ <CustomConnectorSheet />
+ </FormSheet>
+ <FormSheet open={writingSkill} onClose={() => setWritingSkill(false)}>
+ <NewSkillSheet />
+ </FormSheet>
+ <FormSheet open={agent !== null} onClose={() => setAgent(null)} width={560} height={560}>
+ {agent ? <AgentSheet key={agent.id} initial={agent} /> : null}
+ </FormSheet>
+ </>
+ )
+}
+
+function AgentCard({ backend, onClick }: { backend: Backend; onClick: () => void }) {
+ const status = backend.installed === true
+ ? (backend.signedIn === false ? 'Not signed in' : 'Installed')
+ : backend.curated === true ? 'Not installed' : 'Sets up on first use'
+ return (
+ <button className="agent-card" title={backend.name} onClick={onClick}>
+ <AgentTile registry={backend.registry} icon={34} tile={64} radius={18} />
+ <span style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 2, maxWidth: '100%' }}>
+ <span style={{ ...font('subheadline', 'semibold'), color: 'var(--text)', ...ellipsis }}>{backend.name}</span>
+ <span style={{ ...font('caption'), color: 'var(--secondary)', ...ellipsis }}>{status}</span>
+ </span>
+ </button>
+ )
+}
+
+const ellipsis = { whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis', maxWidth: '100%' } as const
+
+// MARK: - Installed
+
+function InstalledView({ back }: { back: () => void }) {
+ const store = useStore()
+ const plugins = usePlugins(store)
+ const [removing, setRemoving] = useState<{ id: string; name: string; isSkill: boolean } | null>(null)
+ const [signingIn, setSigningIn] = useState<string | null>(null)
+ const empty = plugins.installedConnectors.length === 0 && plugins.installedSkills.length === 0
+
+ const signIn = async (id: string) => {
+ setSigningIn(id)
+ try {
+ await plugins.signIn(id)
+ } catch (e) {
+ store.setError(errorText(e))
+ }
+ setSigningIn(null)
+ }
+
+ const remove = async (r: { id: string; isSkill: boolean }) => {
+ try {
+ if (r.isSkill) await plugins.removeSkill(r.id)
+ else await plugins.removeConnector(r.id)
+ } catch (e) {
+ store.setError(errorText(e))
+ }
+ }
+
+ return (
+ <div style={{ display: 'flex', flexDirection: 'column', height: '100%', background: 'var(--background)' }}>
+ <ScreenHeader title="Installed" onBack={back} />
+ <div style={{ flex: 1, minHeight: 0 }}>
+ <CardForm>
+ {empty ? <EmptyState icon="shippingbox" title="Nothing installed" detail="Connectors and skills you add show up here." /> : null}
+ {plugins.installedConnectors.length ? (
+ <CardSection title="Connectors">
+ {plugins.installedConnectors.map((c) => (
+ <InstalledRow
+ key={c.id}
+ title={c.name}
+ subtitle={needsSignIn(c) ? 'Sign in so bots can use it' : (c.command ?? c.url ?? c.description)}
+ icon={c.kind === 'composio' ? <AppLogo url={c.logo} name={c.name} size={32} /> : <ServiceLogo name={c.name} registryName={c.registryName} size={32} />}
+ accessory={signingIn === c.id ? <Spinner /> : needsSignIn(c) ? <Button onClick={() => void signIn(c.id)}>Sign in</Button> : null}
+ onRemove={() => setRemoving({ id: c.id, name: c.name, isSkill: false })}
+ />
+ ))}
+ </CardSection>
+ ) : null}
+ {plugins.installedSkills.length ? (
+ <CardSection title="Skills">
+ {plugins.installedSkills.map((s) => (
+ <InstalledRow
+ key={s.id}
+ title={s.name}
+ subtitle={s.description}
+ icon={<SkillGlyph size={32} />}
+ onRemove={() => setRemoving({ id: s.id, name: s.name, isSkill: true })}
+ />
+ ))}
+ </CardSection>
+ ) : null}
+ </CardForm>
+ </div>
+ <Dialog
+ open={removing !== null}
+ title={`Remove ${removing?.name ?? ''}?`}
+ message={removing ? (removing.isSkill ? 'Bots stop using this skill.' : 'Bots lose this connector, and the keys saved for it are deleted.') : null}
+ actions={removing ? [{ title: 'Remove', destructive: true, action: () => void remove(removing) }] : []}
+ onClose={() => setRemoving(null)}
+ />
+ </div>
+ )
+}
+
+/** A quiet "nothing here" message (replaces `ContentUnavailableView`). */
+function EmptyState({ icon, title, detail }: { icon: string; title: string; detail: string }) {
+ return (
+ <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 8, padding: '40px 0', textAlign: 'center' }}>
+ <Icon name={icon} size={28} color="var(--tertiary)" />
+ <span style={{ ...font('compactBody', 'semibold'), color: 'var(--text)' }}>{title}</span>
+ <span style={{ ...font('compactSecondary'), color: 'var(--secondary)' }}>{detail}</span>
+ </div>
+ )
+}
+
+function InstalledRow({ title, subtitle, icon, accessory, onRemove }: { title: string; subtitle: string; icon: ReactNode; accessory?: ReactNode; onRemove: () => void }) {
+ return (
+ <div style={{ display: 'flex', alignItems: 'center', gap: 12 }} title={subtitle}>
+ {icon}
+ <div style={{ display: 'flex', flexDirection: 'column', gap: 2, flex: 1, minWidth: 0 }}>
+ <span style={{ color: 'var(--text)', ...ellipsis }}>{title}</span>
+ <span style={{ ...font('caption'), color: 'var(--secondary)', ...ellipsis }}>{subtitle}</span>
+ </div>
+ <span style={{ minWidth: 12 }} />
+ {accessory}
+ <button
+ title="Remove"
+ aria-label={`Remove ${title}`}
+ style={{ minWidth: 44, minHeight: 44, display: 'grid', placeItems: 'center', color: 'var(--secondary)', margin: '-8px 0' }}
+ onClick={onRemove}
+ >
+ <Icon name="trash" size={13} />
+ </button>
+ </div>
+ )
}
diff --git a/apps/desktop/src/renderer/views/marketplace/SetupTerminal.tsx b/apps/desktop/src/renderer/views/marketplace/SetupTerminal.tsx
new file mode 100644
index 00000000..3e75d2df
--- /dev/null
+++ b/apps/desktop/src/renderer/views/marketplace/SetupTerminal.tsx
@@ -0,0 +1,195 @@
+import { FitAddon } from '@xterm/addon-fit'
+import { WebLinksAddon } from '@xterm/addon-web-links'
+import { Terminal } from '@xterm/xterm'
+import '@xterm/xterm/css/xterm.css'
+import { useEffect, useRef, useState } from 'react'
+import { fromBase64, HostError, toBase64 } from '../../client/host-client'
+import { Button } from '../../components/Controls'
+import { Icon } from '../../components/Icon'
+import { ThinkingOrb } from '../../components/ThinkingOrb'
+import { font } from '../../lib/fonts'
+import { useStore } from '../../store/context'
+import type { Backend } from '@shared/models'
+import { errorText, market, type AuthMethod, type SetupStep } from './market-models'
+import { ScreenHeader } from './pieces'
+
+const openLink = (uri: string) => {
+ if (/^https?:/i.test(uri)) window.codync.app.openExternal(uri)
+}
+
+/**
+ * Installing or signing in to an agent, live: the command runs in a terminal
+ * on the computer and this screen is that terminal (links open here).
+ */
+export function SetupTerminalView({ backend, step, method, back }: {
+ backend: Backend
+ step: SetupStep
+ /** A terminal sign-in method the agent offered; null runs Codync's own command. */
+ method?: AuthMethod | null
+ /** Back to the agent's sheet (the terminal is a step inside it). */
+ back: () => void
+}) {
+ const store = useStore()
+ const surface = useRef<HTMLDivElement>(null)
+ const [exitCode, setExitCode] = useState<number | null>(null)
+ const [error, setError] = useState<string | null>(null)
+
+ useEffect(() => {
+ const el = surface.current
+ if (!el) return
+ const css = getComputedStyle(document.documentElement)
+ const scale = parseFloat(css.getPropertyValue('--scale')) || 1
+ const term = new Terminal({
+ fontFamily: css.getPropertyValue('--mono').trim() || 'monospace',
+ fontSize: 12 * scale,
+ cursorBlink: true,
+ allowTransparency: true,
+ theme: {
+ background: css.getPropertyValue('--background').trim(),
+ foreground: css.getPropertyValue('--text').trim(),
+ cursor: css.getPropertyValue('--text').trim(),
+ selectionBackground: `rgba(${css.getPropertyValue('--text-rgb').trim()}, 0.2)`,
+ },
+ linkHandler: { activate: (_e, uri) => openLink(uri) },
+ })
+ const fit = new FitAddon()
+ term.loadAddon(fit)
+ term.loadAddon(new WebLinksAddon((_e, uri) => openLink(uri)))
+ term.open(el)
+ fit.fit()
+ term.focus()
+
+ let cancelled = false
+ let exited: number | null = null
+ let termId: string | null = null
+ let size = { cols: term.cols, rows: term.rows }
+ let sent = { cols: 0, rows: 0 }
+ let stopStream: (() => void) | null = null
+ // One sender, so keystrokes can't overtake each other.
+ let typing = Promise.resolve()
+ const encoder = new TextEncoder()
+
+ const syncSize = () => {
+ const client = store.client
+ if (!client || !termId || (size.cols === sent.cols && size.rows === sent.rows)) return
+ sent = size
+ void market.termResize(client, termId, size.cols, size.rows).catch(() => {})
+ }
+ const type = (bytes: Uint8Array) => {
+ const client = store.client
+ if (exited !== null || !client || !termId) return
+ const id = termId
+ typing = typing.then(() => market.termInput(client, id, toBase64(bytes)).then(() => {}, () => {}))
+ }
+ const subs = [
+ term.onData((d) => type(encoder.encode(d))),
+ term.onBinary((d) => type(Uint8Array.from(d, (c) => c.charCodeAt(0) & 0xff))),
+ term.onResize(({ cols, rows }) => {
+ size = { cols, rows }
+ syncSize()
+ }),
+ ]
+ const observer = new ResizeObserver(() => fit.fit())
+ observer.observe(el)
+
+ let attempt = 0
+ /** Attaches to the output until it ends: `retry` after a dropped connection, `done` otherwise. */
+ const attach = (client: NonNullable<typeof store.client>, id: string) =>
+ new Promise<'retry' | 'done'>((resolve) => {
+ term.reset()
+ stopStream = client.transport.stream(
+ `/term/${encodeURIComponent(id)}`,
+ (line) => {
+ attempt = 0
+ let raw: { type: string; data?: string; code?: number }
+ try {
+ raw = JSON.parse(line)
+ } catch {
+ return
+ }
+ if (raw.type === 'output' && raw.data) term.write(fromBase64(raw.data))
+ else if (raw.type === 'exit') {
+ exited = raw.code ?? -1
+ setExitCode(exited)
+ }
+ },
+ (e) => {
+ stopStream = null
+ if (e?.status === 404) {
+ setError(e.message)
+ resolve('done')
+ } else if (e) {
+ attempt += 1
+ if (attempt > 5) {
+ setError(e.message)
+ resolve('done')
+ } else resolve('retry')
+ } else resolve('retry')
+ },
+ )
+ })
+
+ void (async () => {
+ try {
+ const client = await store.ready()
+ const id = await market.agentSetup(client, backend.id, step, method?.id ?? null, size.cols, size.rows)
+ if (cancelled) {
+ void market.termClose(client, id).catch(() => {})
+ return
+ }
+ termId = id
+ sent = size
+ syncSize()
+ // Reattach after a dropped connection; the host replays the whole scrollback.
+ while (exited === null && !cancelled) {
+ if ((await attach(client, id)) === 'done') break
+ if (exited === null && !cancelled) await new Promise((r) => setTimeout(r, 1000))
+ }
+ } catch (e) {
+ if (!cancelled) setError(e instanceof HostError ? e.message : errorText(e))
+ return
+ }
+ if (!cancelled) await store.refreshBackends()
+ })()
+
+ // Leaving the screen ends whatever is still running.
+ return () => {
+ cancelled = true
+ observer.disconnect()
+ subs.forEach((s) => s.dispose())
+ stopStream?.()
+ const client = store.client
+ if (exited === null && client && termId) void market.termClose(client, termId).catch(() => {})
+ term.dispose()
+ }
+ }, [store, backend.id, step, method?.id])
+
+ const title = step === 'install' ? `Install ${backend.name}` : (method?.name ?? `Sign in to ${backend.name}`)
+ return (
+ <div style={{ display: 'flex', flexDirection: 'column', height: '100%', background: 'var(--background)' }}>
+ <ScreenHeader title={title} onBack={back} />
+ <div className="setup-terminal">
+ <div ref={surface} style={{ width: '100%', height: '100%' }} />
+ </div>
+ <div style={{ display: 'flex', alignItems: 'center', gap: 8, padding: '10px 16px', ...font('footnote') }}>
+ {error ? (
+ <span style={{ color: 'var(--danger)', ...clamp2 }}>{error}</span>
+ ) : exitCode !== null ? (
+ <>
+ <Icon name={exitCode === 0 ? 'checkmark.circle.fill' : 'exclamationmark.circle.fill'} size={10} color={exitCode === 0 ? 'var(--added)' : 'var(--warning)'} />
+ <span style={{ color: 'var(--secondary)', ...clamp2 }}>{exitCode === 0 ? 'Finished.' : `Ended with an error (${exitCode}).`}</span>
+ </>
+ ) : (
+ <>
+ <ThinkingOrb size={14} color="var(--secondary)" />
+ <span style={{ color: 'var(--secondary)', ...clamp2 }}>Running on {store.hostName}. Links open on this device.</span>
+ </>
+ )}
+ <span style={{ flex: 1 }} />
+ {exitCode !== null ? <Button className="fade-in" onClick={back}>Done</Button> : null}
+ </div>
+ </div>
+ )
+}
+
+const clamp2 = { display: '-webkit-box', WebkitLineClamp: 2, WebkitBoxOrient: 'vertical', overflow: 'hidden' } as const
diff --git a/apps/desktop/src/renderer/views/marketplace/market-models.ts b/apps/desktop/src/renderer/views/marketplace/market-models.ts
new file mode 100644
index 00000000..4bb600f1
--- /dev/null
+++ b/apps/desktop/src/renderer/views/marketplace/market-models.ts
@@ -0,0 +1,252 @@
+import type { HostClient } from '../../client/host-client'
+
+// Marketplace wire types (host/src/market/mod.rs): connectors are MCP servers,
+// skills are instruction folders; agents come from `Hello.backends`. (kit Market.swift)
+
+export interface InstalledConnector {
+ id: string
+ name: string
+ description: string
+ registryName?: string | null
+ /** `local` (a command the agent starts), `remote` (a URL) or `composio` (an app connected through Composio). */
+ kind: string
+ command?: string | null
+ url?: string | null
+ /** Names of the keys and headers that are set; their values stay on the computer. */
+ keys: string[]
+ /** Composio apps: the app's logo. */
+ logo?: string | null
+ /** Remote servers: `none`, `signedOut` (waits for sign-in; bots don't get it yet) or `signedIn`. */
+ auth?: string | null
+}
+
+export const needsSignIn = (c: InstalledConnector) => c.auth === 'signedOut'
+
+/** Where a connector's sign-in page is and who catches its return: `app` or `host`. */
+export interface ConnectorSignIn {
+ url: string
+ callback: string
+}
+
+export interface ComposioStatus {
+ configured: boolean
+ /** Where to get an API key. */
+ keyUrl: string
+}
+
+export interface ComposioApp {
+ slug: string
+ name: string
+ description?: string | null
+ logo?: string | null
+ /** Composio's status: `active`, `initiated`, `failed`, `expired`… */
+ connection?: { id: string; status: string } | null
+}
+
+export const appConnected = (a: ComposioApp) => a.connection?.status === 'active'
+
+export interface ComposioField {
+ name: string
+ label: string
+ description?: string | null
+ secret: boolean
+ required: boolean
+}
+
+/** What connecting an app needs: a sign-in page, or fields for a key-based app. */
+export interface ComposioConnect {
+ /** `redirect` or `needsFields`. */
+ status: string
+ url?: string | null
+ connection?: string | null
+ mode?: string | null
+ fields?: ComposioField[] | null
+}
+
+export interface ComposioConnectionState {
+ id: string
+ status: string
+}
+
+export interface MarketInput {
+ name: string
+ description?: string | null
+ secret: boolean
+ required: boolean
+ default?: string | null
+ placeholder?: string | null
+}
+
+export interface InstallOption {
+ id: string
+ /** npm | pypi | oci | remote */
+ kind: string
+ label: string
+ inputs: MarketInput[]
+}
+
+export interface MarketConnector {
+ name: string
+ title: string
+ description?: string | null
+ version?: string | null
+ website?: string | null
+ installed: boolean
+ options: InstallOption[]
+}
+
+export interface InstalledSkill {
+ id: string
+ name: string
+ description: string
+ source: string
+ path: string
+}
+
+export interface MarketSkill {
+ /** Folder name in anthropics/skills. */
+ source: string
+ name: string
+ description: string
+ installed: boolean
+}
+
+export interface ConnectionRequest {
+ toolkit?: string | null
+ kind: string
+ status: string
+ title: string
+ botId: string
+ registryName?: string | null
+ connectorId?: string | null
+ field?: string | null
+ location?: string | null
+ /** Login requests: the website's domain or the app's name. */
+ site?: string | null
+}
+
+/** A saved website or app login; the password stays on the computer. */
+export interface SavedLogin {
+ id: string
+ site: string
+ username: string
+}
+
+export interface CredentialStatus {
+ provider: string
+ onePasswordConnected: boolean
+}
+
+// Agent setup (kit Models.swift)
+
+export type AuthMethodKind = 'terminal' | 'agent' | 'envVar'
+
+export interface AuthVar {
+ name: string
+ label: string
+ secret: boolean
+ optional: boolean
+}
+
+export interface AuthMethod {
+ id: string
+ name: string
+ description?: string | null
+ kind?: AuthMethodKind | null
+ vars?: AuthVar[] | null
+ link?: string | null
+}
+
+/** How an agent can be signed in, from asking the agent itself. */
+export interface AgentAuth {
+ signedIn?: boolean | null
+ /** The agent's own words when it wants signing in (can hold a pairing code). */
+ detail?: string | null
+ methods: AuthMethod[]
+ /** Keys saved on the computer for this agent (names only). */
+ savedEnv?: string[] | null
+ /** Codync's own sign-in command is available. */
+ login?: boolean | null
+}
+
+/** What a setup terminal on the computer runs. */
+export type SetupStep = 'install' | 'login'
+
+const s = (n: number) => n * 1000
+
+// Host calls, same methods and timeouts as kit's HostClient extensions.
+
+export const market = {
+ connectors: async (c: HostClient) => (await c.call<{ items: InstalledConnector[] }>('connectors')).items,
+ /** A page of the MCP Registry (featured first), or of a search (can take ~30 s). */
+ marketConnectors: async (c: HostClient, search: string, cursor: string | null = null) => {
+ const res = await c.call<{ items: MarketConnector[]; nextCursor?: string | null }>('marketConnectors', { search, cursor: cursor ?? '' }, s(60))
+ return { items: res.items, next: res.nextCursor || null }
+ },
+ installConnector: async (c: HostClient, registryName: string, option: string, inputs: Record<string, string>) =>
+ (await c.call<{ connector: InstalledConnector }>('installConnector', { registryName, option, inputs }, s(45))).connector,
+ /** A connector you describe yourself: a command line with env, or an https URL with headers. */
+ addConnector: async (c: HostClient, body: { name: string; command: string | null; url: string | null; env: Record<string, string>; headers: Record<string, string> }) =>
+ (await c.call<{ connector: InstalledConnector }>('installConnector', body, s(45))).connector,
+ /** Adds every server in a pasted MCP config (`mcpServers` / `servers` JSON, or one server's entry). */
+ importConnectors: async (c: HostClient, config: string) => (await c.call<{ items: InstalledConnector[] }>('importConnectors', { config }, s(60))).items,
+ connectorSignIn: (c: HostClient, id: string) => c.call<ConnectorSignIn>('connectorSignIn', { id }, s(60)),
+ finishConnectorSignIn: async (c: HostClient, state: string, code: string | null, error: string | null) =>
+ (await c.call<{ connector: InstalledConnector }>('connectorSignInFinish', { state, code, error }, s(60))).connector,
+ removeConnector: (c: HostClient, id: string) => c.call('removeConnector', { id }),
+ composioStatus: (c: HostClient) => c.call<ComposioStatus>('composioStatus'),
+ /** Checks the key with Composio before keeping it; an empty key forgets Composio. */
+ setComposioKey: (c: HostClient, key: string) => c.call<ComposioStatus>('setComposioKey', { key }, s(60)),
+ composioApps: async (c: HostClient, search: string) => (await c.call<{ items: ComposioApp[] }>('composioToolkits', { search }, s(60))).items,
+ composioConnect: (c: HostClient, app: string) => c.call<ComposioConnect>('composioConnect', { toolkit: app }, s(60)),
+ composioConnectFields: (c: HostClient, app: string, mode: string, fields: Record<string, string>) =>
+ c.call<ComposioConnectionState>('composioConnectFields', { toolkit: app, mode, fields }, s(60)),
+ composioConnection: (c: HostClient, id: string) => c.call<ComposioConnectionState>('composioConnection', { id }, s(30)),
+ skills: async (c: HostClient) => (await c.call<{ items: InstalledSkill[] }>('skills')).items,
+ marketSkills: async (c: HostClient) => (await c.call<{ items: MarketSkill[] }>('marketSkills', {}, s(60))).items,
+ installSkill: (c: HostClient, source: string) => c.call('installSkill', { source }, s(60)),
+ addSkill: (c: HostClient, name: string, description: string, instructions: string) => c.call('installSkill', { name, description, instructions }),
+ removeSkill: (c: HostClient, id: string) => c.call('removeSkill', { id }),
+ connectorInfo: (c: HostClient, registryName: string) => c.call<MarketConnector>('connectorInfo', { registryName }, s(60)),
+ verifyConnector: (c: HostClient, id: string) => c.call<{ status: string }>('connectorVerify', { id }, s(120)),
+ finishConnectionRequest: (c: HostClient, entryId: string, opts: { connectorId?: string; value?: string; username?: string; cancel?: boolean } = {}) =>
+ c.call('connectorRequestFinish', { entryId, connectorId: opts.connectorId ?? null, value: opts.value ?? null, username: opts.username ?? null, cancel: opts.cancel ?? false }, s(120)),
+ logins: async (c: HostClient) => (await c.call<{ items: SavedLogin[] }>('credentialLogins', {}, s(60))).items,
+ saveLogin: (c: HostClient, site: string, username: string, password: string) => c.call('credentialSaveLogin', { site, username, password }, s(60)),
+ removeLogin: (c: HostClient, id: string) => c.call('credentialRemoveLogin', { id }, s(60)),
+ credentialStatus: (c: HostClient) => c.call<CredentialStatus>('credentialStatus', {}, s(60)),
+ setOnePasswordToken: (c: HostClient, token: string) => c.call<CredentialStatus>('credentialSetOnePassword', { token }, s(60)),
+ updateConnectorCredentials: (c: HostClient, id: string, fields: Record<string, string>) => c.call('credentialUpdateConnector', { id, fields }, s(120)),
+
+ // Agent setup terminals
+ /** The first sign-in through a registry build can download it. */
+ agentSetup: async (c: HostClient, backend: string, step: SetupStep, method: string | null, cols: number, rows: number) =>
+ (await c.call<{ term: string }>('agentSetup', { backend, step, method, cols, rows }, s(300))).term,
+ agentAuth: (c: HostClient, backend: string) => c.call<AgentAuth>('agentAuth', { backend }, s(300)),
+ agentAuthenticate: (c: HostClient, backend: string, method: string) => c.call<AgentAuth>('agentAuthenticate', { backend, method }, s(660)),
+ setAgentEnv: (c: HostClient, backend: string, vars: Record<string, string>) => c.call<AgentAuth>('setAgentEnv', { backend, vars }, s(300)),
+ termInput: (c: HostClient, term: string, data: string) => c.call('termInput', { term, data }),
+ termResize: (c: HostClient, term: string, cols: number, rows: number) => c.call('termResize', { term, cols, rows }),
+ termClose: (c: HostClient, term: string) => c.call('termClose', { term }),
+}
+
+/** Keep fetched catalog metadata in memory, but create rows only as the user requests them. */
+export interface MarketplacePage<T> {
+ items: T[]
+ visibleCount: number
+}
+
+export const emptyPage = <T>(): MarketplacePage<T> => ({ items: [], visibleCount: 12 })
+
+export function appendPage<T>(page: MarketplacePage<T>, more: T[], id: (t: T) => string): MarketplacePage<T> {
+ const known = new Set(page.items.map(id))
+ return { ...page, items: [...page.items, ...more.filter((t) => !known.has(id(t)) && (known.add(id(t)), true))] }
+}
+
+export const replacePage = <T>(items: T[], id: (t: T) => string) => appendPage(emptyPage<T>(), items, id)
+export const revealMore = <T>(page: MarketplacePage<T>): MarketplacePage<T> => ({ ...page, visibleCount: Math.min(page.items.length, page.visibleCount + 12) })
+export const visible = <T>(page: MarketplacePage<T>) => page.items.slice(0, page.visibleCount)
+export const hasHiddenItems = <T>(page: MarketplacePage<T>) => page.items.length > page.visibleCount
+
+export const errorText = (e: unknown) => (e instanceof Error ? e.message : String(e))
+export const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms))
diff --git a/apps/desktop/src/renderer/views/marketplace/marketplace.css b/apps/desktop/src/renderer/views/marketplace/marketplace.css
new file mode 100644
index 00000000..0e3bce0f
--- /dev/null
+++ b/apps/desktop/src/renderer/views/marketplace/marketplace.css
@@ -0,0 +1,191 @@
+.market-page {
+ position: relative;
+ height: 100%;
+ background: var(--background);
+}
+.market-scroll {
+ height: 100%;
+ overflow-y: auto;
+}
+.market-page-content {
+ display: flex;
+ flex-direction: column;
+ gap: 28px;
+ padding: 20px 44px;
+ max-width: 980px;
+ margin: 0 auto;
+}
+.market-close {
+ position: absolute;
+ top: 8px;
+ right: 8px;
+}
+
+/* Installed ⇄ page: slides like `.move(edge:)` + opacity. */
+.market-stack {
+ position: relative;
+ height: 100%;
+ overflow: hidden;
+ background: var(--background);
+}
+.market-layer {
+ position: absolute;
+ inset: 0;
+ display: flex;
+ flex-direction: column;
+ transition: transform var(--layout), opacity var(--layout);
+}
+.market-layer.off-leading {
+ transform: translateX(-100%);
+ opacity: 0;
+ pointer-events: none;
+}
+.market-layer.off-trailing {
+ transform: translateX(100%);
+ opacity: 0;
+ pointer-events: none;
+}
+
+.market-search {
+ display: flex;
+ align-items: center;
+ gap: 10px;
+ padding: 0 14px;
+ height: 38px;
+ background: var(--bubble-agent);
+ border-radius: 999px;
+}
+.market-search input {
+ flex: 1;
+ min-width: 0;
+}
+
+.market-section {
+ display: flex;
+ flex-direction: column;
+ gap: 12px;
+}
+
+.agent-grid {
+ display: grid;
+ grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
+ gap: 12px;
+}
+.item-grid {
+ display: grid;
+ grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
+ column-gap: 10px;
+ row-gap: 4px;
+}
+
+.agent-card {
+ display: flex;
+ flex-direction: column;
+ align-items: center;
+ gap: 12px;
+ padding: 18px 10px;
+ border-radius: 18px;
+ min-width: 0;
+ background: color-mix(in srgb, var(--bubble-agent) 45%, transparent);
+ transition: background-color var(--hover), transform var(--press);
+}
+.agent-card:hover {
+ background: color-mix(in srgb, var(--bubble-agent) 80%, transparent);
+}
+.agent-card:active {
+ transform: scale(0.98);
+}
+
+.market-row {
+ display: flex;
+ align-items: center;
+ gap: 14px;
+ padding: 10px 12px;
+ border-radius: 16px;
+ min-width: 0;
+ transition: background-color var(--hover);
+}
+.market-row:hover {
+ background: color-mix(in srgb, var(--bubble-agent) 60%, transparent);
+}
+
+/* The quiet capsule action ("Add", "Install"). */
+.capsule-action {
+ font-weight: 600;
+ color: var(--text);
+ padding: 7px 16px;
+ background: var(--bubble-agent);
+ border-radius: 999px;
+ white-space: nowrap;
+ flex: none;
+ transition: transform var(--press), opacity var(--press);
+}
+.capsule-action:active:not(:disabled) {
+ transform: scale(0.98);
+ opacity: 0.85;
+}
+
+.skeleton {
+ animation: market-pulse 0.9s ease-in-out infinite alternate;
+}
+@keyframes market-pulse {
+ to {
+ opacity: 0.5;
+ }
+}
+@media (prefers-reduced-motion: reduce) {
+ .skeleton {
+ animation: none;
+ }
+}
+
+.plain-link {
+ color: var(--text);
+ text-decoration: underline;
+ text-align: left;
+}
+
+.option-row {
+ display: flex;
+ align-items: center;
+ gap: 12px;
+ width: 100%;
+}
+.option-row:disabled {
+ opacity: 0.5;
+}
+
+.plain-row-button {
+ display: flex;
+ align-items: center;
+ width: 100%;
+}
+
+.market-field {
+ width: 100%;
+ display: block;
+}
+textarea.market-field {
+ field-sizing: content;
+}
+
+.setup-terminal {
+ flex: 1;
+ min-height: 0;
+ padding: 6px 10px 0;
+}
+.setup-terminal .xterm {
+ height: 100%;
+}
+.setup-terminal .xterm-viewport {
+ background: transparent !important;
+}
+
+.fade-in {
+ animation: market-fade-in var(--fade) both;
+}
+@keyframes market-fade-in {
+ from {
+ opacity: 0;
+ }
+}
diff --git a/apps/desktop/src/renderer/views/marketplace/pieces.tsx b/apps/desktop/src/renderer/views/marketplace/pieces.tsx
new file mode 100644
index 00000000..24b4fcf1
--- /dev/null
+++ b/apps/desktop/src/renderer/views/marketplace/pieces.tsx
@@ -0,0 +1,254 @@
+import { useEffect, useRef, useState, type CSSProperties, type ReactNode } from 'react'
+import { IconButton } from '../../components/Controls'
+import { Sheet, usePresence } from '../../components/Overlay'
+import { Icon } from '../../components/Icon'
+import { ThinkingOrb } from '../../components/ThinkingOrb'
+import { font } from '../../lib/fonts'
+import './marketplace.css'
+
+// Building blocks the Marketplace's screens share (kit MarketplaceView.swift "Pieces").
+
+export function MarketSection({ title, children }: { title: string; children: ReactNode }) {
+ return (
+ <section className="market-section">
+ <div style={{ ...font('headline'), color: 'var(--text)', paddingLeft: 4 }}>{title}</div>
+ {children}
+ </section>
+ )
+}
+
+/** Two columns on wide screens, one on a narrow one. */
+export function ItemGrid({ children }: { children: ReactNode }) {
+ return <div className="item-grid">{children}</div>
+}
+
+const oneLine: CSSProperties = { whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' }
+export const lineClamp = (n: number): CSSProperties => ({ display: '-webkit-box', WebkitLineClamp: n, WebkitBoxOrient: 'vertical', overflow: 'hidden' })
+
+export function MarketRow({ title, subtitle, added, busy = false, addLabel = 'Add', icon, onAdd }: {
+ title: string
+ subtitle: string
+ added: boolean
+ busy?: boolean
+ addLabel?: string
+ icon: ReactNode
+ onAdd: () => void
+}) {
+ return (
+ <div className="market-row">
+ <div style={{ width: 46, height: 46, flex: 'none', display: 'grid' }}>{icon}</div>
+ <div style={{ display: 'flex', flexDirection: 'column', gap: 2, minWidth: 0, flex: 1 }}>
+ <span style={{ ...font('body', 'medium'), color: 'var(--text)', ...oneLine }} title={title}>{title}</span>
+ <span style={{ ...font('subheadline'), color: 'var(--secondary)', ...oneLine }} title={subtitle}>{subtitle}</span>
+ </div>
+ {busy ? (
+ <span style={{ width: 60, display: 'flex', justifyContent: 'center', flex: 'none' }}>
+ <ThinkingOrb size={18} color="var(--secondary)" />
+ </span>
+ ) : added ? (
+ <span style={{ width: 60, display: 'flex', justifyContent: 'center', flex: 'none', color: 'var(--secondary)' }} aria-label="Added" title="Added">
+ <Icon name="checkmark" size={11} weight="semibold" />
+ </span>
+ ) : (
+ <button className="capsule-action" style={font('subheadline', 'semibold')} onClick={onAdd}>
+ {addLabel}
+ </button>
+ )}
+ </div>
+ )
+}
+
+/** Placeholder rows while a list loads, shaped like the real ones. */
+export function SkeletonGrid() {
+ return (
+ <div className="item-grid skeleton">
+ {[0, 1, 2, 3].map((i) => (
+ <div key={i} style={{ display: 'flex', alignItems: 'center', gap: 14, padding: '10px 12px' }}>
+ <div style={{ width: 46, height: 46, borderRadius: 12, background: 'var(--bubble-agent)', flex: 'none' }} />
+ <div style={{ display: 'flex', flexDirection: 'column', gap: 8 }}>
+ <div style={{ width: 120, height: 12, borderRadius: 6, background: 'var(--bubble-agent)' }} />
+ <div style={{ width: 190, height: 10, borderRadius: 5, background: 'var(--bubble-agent)' }} />
+ </div>
+ </div>
+ ))}
+ </div>
+ )
+}
+
+/** A loaded image, or nothing until (and unless) it loads. */
+function RemoteImage({ src, style }: { src: string; style: CSSProperties }) {
+ const [loaded, setLoaded] = useState(false)
+ return <img src={src} alt="" draggable={false} onLoad={() => setLoaded(true)} style={{ ...style, display: loaded ? 'block' : 'none' }} />
+}
+
+function serviceDomain(website: string | null | undefined, name: string, registryName: string | null | undefined) {
+ if (website && URL.canParse(website)) {
+ const host = new URL(website).hostname
+ if (host && host !== 'github.com') return host
+ }
+ // MCP Registry names are reverse-DNS ("com.notion/mcp"): the owner's domain.
+ const owner = registryName?.split('/')[0]
+ if (!owner) return null
+ const labels = owner.split('.').filter(Boolean)
+ // io.github.<user> is a GitHub account, not the service's own site.
+ if (labels.length < 2 || (labels[0] === 'io' && labels[1] === 'github')) return name.toLowerCase().includes('github') ? 'github.com' : null
+ return `${labels[1]}.${labels[0]}`
+}
+
+/** A service's logo from its website favicon, on a white tile; its initial otherwise. */
+export function ServiceLogo({ website, name, registryName, size = 46 }: { website?: string | null; name: string; registryName?: string | null; size?: number }) {
+ const domain = serviceDomain(website, name, registryName)
+ const radius = size * 0.26
+ return (
+ <div style={{ position: 'relative', width: size, height: size, borderRadius: radius, overflow: 'hidden', background: 'var(--bubble-agent)', display: 'grid', placeItems: 'center', flex: 'none' }}>
+ <span style={{ ...font(size * 0.4, 'semibold', 'rounded'), color: 'var(--text)' }}>{name.slice(0, 1).toUpperCase() || '?'}</span>
+ {domain ? (
+ <RemoteImage
+ src={`https://www.google.com/s2/favicons?domain=${encodeURIComponent(domain)}&sz=128`}
+ style={{ position: 'absolute', inset: 0, width: '100%', height: '100%', objectFit: 'contain', padding: size * 0.2, boxSizing: 'border-box', background: '#fff' }}
+ />
+ ) : null}
+ </div>
+ )
+}
+
+/** An app's logo from Composio, a letter tile until it loads. */
+export function AppLogo({ url, name, size = 46 }: { url?: string | null; name: string; size?: number }) {
+ const [loaded, setLoaded] = useState(false)
+ return (
+ <div aria-hidden style={{ position: 'relative', width: size, height: size, flex: 'none', borderRadius: size * 0.28, background: 'var(--bubble-agent)', display: 'grid', placeItems: 'center' }}>
+ {!loaded ? <span style={{ ...font(size * 0.4, 'semibold'), color: 'var(--secondary)' }}>{name.slice(0, 1).toUpperCase()}</span> : null}
+ {url ? (
+ <img
+ src={url}
+ alt=""
+ draggable={false}
+ onLoad={() => setLoaded(true)}
+ style={{ position: 'absolute', inset: 0, width: '100%', height: '100%', objectFit: 'contain', padding: size * 0.18, boxSizing: 'border-box', display: loaded ? 'block' : 'none' }}
+ />
+ ) : null}
+ </div>
+ )
+}
+
+export function TileIcon({ name }: { name: string }) {
+ return (
+ <div style={{ width: '100%', height: '100%', borderRadius: 12, background: 'var(--bubble-agent)', display: 'grid', placeItems: 'center', color: 'var(--text)' }}>
+ <Icon name={name} size={18} weight="medium" />
+ </div>
+ )
+}
+
+export function SkillGlyph({ size }: { size?: number }) {
+ return (
+ <div style={{ width: size ?? '100%', height: size ?? '100%', flex: 'none', borderRadius: 12, background: 'var(--bubble-agent)', display: 'grid', placeItems: 'center', color: 'var(--text)' }}>
+ <Icon name="book.pages" size={13} />
+ </div>
+ )
+}
+
+/** Text that opens a web page (replaces the system `Link`). */
+export function WebLink({ title, url, style }: { title: string; url: string; style?: CSSProperties }) {
+ return (
+ <button className="plain-link" style={style} title={url} onClick={() => window.codync.app.openExternal(url)}>
+ {title}
+ </button>
+ )
+}
+
+/** A full screen's top bar (kit `ScreenHeader`): back on the leading side, centered title, trailing. */
+export function ScreenHeader({ title, onBack, trailing }: { title: string; onBack: () => void; trailing?: ReactNode }) {
+ return (
+ <div style={{ position: 'relative', height: 44, padding: '0 12px', display: 'flex', alignItems: 'center', gap: 6, flex: 'none', background: 'var(--background)' }}>
+ <div style={{ position: 'absolute', left: 60, right: 60, textAlign: 'center', ...font('compactBody', 'semibold'), color: 'var(--text)', ...oneLine }}>{title}</div>
+ <IconButton title="Back" icon="chevron.left" onClick={onBack} />
+ <span style={{ flex: 1, minWidth: 8 }} />
+ {trailing}
+ </div>
+ )
+}
+
+/**
+ * A screen pushed over another inside one modal: the base slides out leading as `top` slides
+ * in from the trailing edge (kit's `.move(edge:)` + opacity transitions), and back.
+ */
+export function SlideStack({ base, top }: { base: ReactNode; top: ReactNode | null }) {
+ const { mounted, shown } = usePresence(top !== null)
+ const last = useRef(top)
+ if (top !== null) last.current = top
+ return (
+ <div className="market-stack">
+ <div className={`market-layer ${shown ? 'off-leading' : ''}`} inert={shown}>{base}</div>
+ {mounted ? <div className={`market-layer ${shown ? '' : 'off-trailing'}`}>{top ?? last.current}</div> : null}
+ </div>
+ )
+}
+
+/** Plain text entry for card rows: one line, a secret, or a growing multi-line box. */
+export function Field({ value, onChange, placeholder, secret = false, lines, mono = false, label }: {
+ value: string
+ onChange: (v: string) => void
+ placeholder?: string
+ secret?: boolean
+ /** Multi-line, growing between these line counts (`axis: .vertical` + `.lineLimit`). */
+ lines?: [number, number]
+ mono?: boolean
+ label?: string
+}) {
+ const style: CSSProperties = mono ? font('callout', 'regular', 'monospaced') : {}
+ if (lines) {
+ return (
+ <textarea
+ className="market-field"
+ aria-label={label ?? placeholder}
+ value={value}
+ placeholder={placeholder}
+ spellCheck={false}
+ onChange={(e) => onChange(e.target.value)}
+ style={{ ...style, lineHeight: 1.3, minHeight: `${lines[0] * 1.3}em`, maxHeight: `${lines[1] * 1.3}em` }}
+ />
+ )
+ }
+ return (
+ <input
+ className="market-field"
+ aria-label={label ?? placeholder}
+ type={secret ? 'password' : 'text'}
+ value={value}
+ placeholder={placeholder}
+ spellCheck={false}
+ autoComplete="off"
+ onChange={(e) => onChange(e.target.value)}
+ style={style}
+ />
+ )
+}
+
+/** A danger-colored error line in its own card. */
+export function ErrorText({ text }: { text: string }) {
+ return <span className="selectable" style={{ color: 'var(--danger)' }}>{text}</span>
+}
+
+/** Values with something typed in. */
+export const filled = (values: Record<string, string>) => Object.fromEntries(Object.entries(values).filter(([, v]) => v !== ''))
+
+/** The window's height, following resizes. */
+export function useWindowHeight() {
+ const [height, setHeight] = useState(window.innerHeight)
+ useEffect(() => {
+ const onResize = () => setHeight(window.innerHeight)
+ window.addEventListener('resize', onResize)
+ return () => window.removeEventListener('resize', onResize)
+ }, [])
+ return height
+}
+
+/** A Marketplace form in its own modal card (`.codyncSheet`), sized like the Mac's. */
+export function FormSheet({ open, onClose, width = 520, height = 600, children }: { open: boolean; onClose: () => void; width?: number; height?: number; children: ReactNode }) {
+ const windowHeight = useWindowHeight()
+ return (
+ <Sheet open={open} onClose={onClose} width={width} height={Math.min(height, windowHeight - 76)}>
+ {children}
+ </Sheet>
+ )
+}
diff --git a/apps/desktop/src/renderer/views/marketplace/plugins.ts b/apps/desktop/src/renderer/views/marketplace/plugins.ts
new file mode 100644
index 00000000..6f4e9167
--- /dev/null
+++ b/apps/desktop/src/renderer/views/marketplace/plugins.ts
@@ -0,0 +1,105 @@
+import { Observable, useModel } from '../../lib/observable'
+import type { BotStore } from '../../store/bot-store'
+import { market, needsSignIn, sleep, type InstalledConnector, type InstalledSkill, type MarketConnector } from './market-models'
+
+/** What's installed on one computer, for the Marketplace and bot settings (kit BotStore's plugin state). */
+export class Plugins extends Observable {
+ installedConnectors: InstalledConnector[] = []
+ installedSkills: InstalledSkill[] = []
+
+ constructor(readonly store: BotStore) {
+ super()
+ }
+
+ async refresh() {
+ const client = this.store.client
+ if (!client) return
+ const [c, s] = await Promise.allSettled([market.connectors(client), market.skills(client)])
+ if (c.status === 'fulfilled') this.installedConnectors = c.value
+ if (s.status === 'fulfilled') this.installedSkills = s.value
+ this.changed()
+ }
+
+ private async after<T>(op: Promise<T>) {
+ const result = await op
+ await this.refresh()
+ return result
+ }
+
+ async installConnector(item: MarketConnector, option: string, inputs: Record<string, string>) {
+ return this.after(market.installConnector(await this.store.ready(), item.name, option, inputs))
+ }
+
+ async addConnector(body: Parameters<typeof market.addConnector>[1]) {
+ return this.after(market.addConnector(await this.store.ready(), body))
+ }
+
+ async importConnectors(config: string) {
+ return this.after(market.importConnectors(await this.store.ready(), config))
+ }
+
+ async finishConnectorSignIn(state: string, code: string | null, error: string | null) {
+ return this.after(market.finishConnectorSignIn(await this.store.ready(), state, code, error))
+ }
+
+ /** Waits while the user signs in in the computer's browser (the host catches the return). */
+ async waitForSignIn(id: string) {
+ for (let i = 0; i < 150; i++) {
+ await sleep(2000)
+ await this.refresh()
+ const c = this.installedConnectors.find((c) => c.id === id)
+ if (c && !needsSignIn(c)) return
+ }
+ }
+
+ async removeConnector(id: string) {
+ await this.after(market.removeConnector(await this.store.ready(), id))
+ }
+
+ async installSkill(source: string) {
+ await this.after(market.installSkill(await this.store.ready(), source))
+ }
+
+ async addSkill(name: string, description: string, instructions: string) {
+ await this.after(market.addSkill(await this.store.ready(), name, description, instructions))
+ }
+
+ async removeSkill(id: string) {
+ await this.after(market.removeSkill(await this.store.ready(), id))
+ }
+
+ /**
+ * Signs in to a remote connector (host/src/market/oauth.rs). With an `app` callback the system
+ * browser returns `codync://oauth?…` and the code goes to the host; with `host` the browser
+ * returns to the host directly and we wait for the connector to flip to signed in.
+ * Returns quietly when the user cancels.
+ */
+ async signIn(id: string) {
+ const plan = await market.connectorSignIn(await this.store.ready(), id)
+ if (!URL.canParse(plan.url)) return
+ if (plan.callback !== 'app') {
+ window.codync.app.openExternal(plan.url)
+ await this.waitForSignIn(id)
+ return
+ }
+ let back: string
+ try {
+ back = await window.codync.app.authenticate(plan.url, 'codync')
+ } catch {
+ return
+ }
+ const items = new URL(back).searchParams
+ await this.finishConnectorSignIn(items.get('state') ?? '', items.get('code'), items.get('error_description') ?? items.get('error'))
+ }
+}
+
+const byStore = new WeakMap<BotStore, Plugins>()
+
+export function pluginsFor(store: BotStore) {
+ let p = byStore.get(store)
+ if (!p) byStore.set(store, (p = new Plugins(store)))
+ return p
+}
+
+/** The store's plugin state, re-rendering on its changes. */
+export const usePlugins = (store: BotStore) => useModel(pluginsFor(store))
diff --git a/apps/desktop/src/renderer/views/routines/RoutineEditorView.tsx b/apps/desktop/src/renderer/views/routines/RoutineEditorView.tsx
new file mode 100644
index 00000000..9bb211de
--- /dev/null
+++ b/apps/desktop/src/renderer/views/routines/RoutineEditorView.tsx
@@ -0,0 +1,310 @@
+import { useEffect, useRef, useState } from 'react'
+import { Button, ChoicePicker, IconButton, Spinner } from '../../components/Controls'
+import { Icon } from '../../components/Icon'
+import { Dialog, ModalHeader } from '../../components/Overlay'
+import { font } from '../../lib/fonts'
+import { useStore } from '../../store/context'
+import { emptyDraft, errorText, sameDraft, type Routine, type RoutineScheduleDraft, type RoutineSchedulePreview } from './routine'
+import { Field, RoutineWebhookPanel } from './RoutineWebhookPanel'
+import './routines.css'
+
+const caption = { ...font('caption'), paddingLeft: 4, lineHeight: 1.4 } as const
+
+export function RoutineEditorView({ botId, routine, saved }: {
+ botId: string
+ routine: Routine | null
+ /** Called after a save (the routine) or a delete (null). */
+ saved: (routine: Routine | null) => void
+}) {
+ const store = useStore()
+ const [name, setName] = useState(routine?.name ?? '')
+ const [instruction, setInstruction] = useState(routine?.instruction ?? '')
+ const [schedule, setSchedule] = useState<RoutineScheduleDraft>(emptyDraft)
+ const [timeout, setTimeoutText] = useState(String(routine?.timeoutSeconds ?? 3600))
+ const [busy, setBusy] = useState(false)
+ const [error, setError] = useState<string | null>(null)
+ const [advanced, setAdvanced] = useState(false)
+ const [loaded, setLoaded] = useState(false)
+ const [preview, setPreview] = useState<RoutineSchedulePreview | null>(null)
+ const [previewError, setPreviewError] = useState<string | null>(null)
+ const [checkedSchedule, setCheckedSchedule] = useState<RoutineScheduleDraft | null>(null)
+ const [confirmDelete, setConfirmDelete] = useState(false)
+ const [started, setStarted] = useState(false)
+ const latest = useRef(schedule)
+ latest.current = schedule
+
+ // Anything the bot set up (a one-off time, an interval, events, several triggers) stays as it is until replaced.
+ const keepsOriginal = routine ? (routine.triggers.length === 1 ? !['cron', 'webhook'].includes(routine.triggers[0]!.type) : routine.triggers.length > 0) : false
+ const options = [
+ ...(routine && keepsOriginal ? [{ id: 'keep', label: routine.triggerDescriptions.join(' · ') }] : []),
+ { id: 'cron', label: 'Schedule' },
+ { id: 'webhook', label: 'Webhook' },
+ ]
+ const canSave = !!name.trim() && !!instruction.trim() && !store.isOffline && !busy && loaded && sameDraft(checkedSchedule, schedule) && previewError === null
+
+ const loadSchedule = async () => {
+ try {
+ const client = await store.ready()
+ const result = await client.call<RoutineSchedulePreview>('routineSchedule', {
+ triggers: routine?.triggers ?? [],
+ timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone,
+ })
+ const draft = { ...result.draft, calendarStyle: 'custom' }
+ if (!['cron', 'webhook'].includes(draft.kind)) draft.kind = 'keep'
+ setSchedule(draft)
+ setPreview(result)
+ setCheckedSchedule(result.draft)
+ setLoaded(true)
+ setError(null)
+ } catch (e) {
+ setError(errorText(e))
+ }
+ }
+
+ const checkSchedule = async () => {
+ const requested = latest.current
+ setPreview(null)
+ setPreviewError(null)
+ setCheckedSchedule(null)
+ try {
+ const result = await (await store.ready()).call<RoutineSchedulePreview>('routineSchedule', { draft: requested })
+ if (latest.current !== requested) return
+ setPreview(result)
+ setCheckedSchedule(requested)
+ } catch (e) {
+ if (latest.current !== requested) return
+ setPreviewError(errorText(e))
+ }
+ }
+
+ useEffect(() => {
+ void loadSchedule()
+ }, [])
+
+ // The host checks each edit to the schedule, after a short pause in typing.
+ useEffect(() => {
+ if (!loaded) return
+ const timer = setTimeout(() => void checkSchedule(), 300)
+ return () => clearTimeout(timer)
+ }, [schedule])
+
+ const save = async () => {
+ if (!canSave) return
+ // Saving ends editing (a new webhook routine stays open on its URL and key).
+ ;(document.activeElement as HTMLElement | null)?.blur()
+ setBusy(true)
+ setError(null)
+ try {
+ const client = await store.ready()
+ const result = await client.call<{ routine: Routine }>('saveRoutine', {
+ botId,
+ id: routine?.id ?? null,
+ name,
+ instruction,
+ schedule,
+ timeoutSeconds: timeout,
+ })
+ saved(result.routine)
+ } catch (e) {
+ setError(errorText(e))
+ } finally {
+ setBusy(false)
+ }
+ }
+
+ // ⌘S saves.
+ const saveRef = useRef(save)
+ saveRef.current = save
+ useEffect(() => {
+ const onKey = (e: KeyboardEvent) => {
+ if ((e.metaKey || e.ctrlKey) && e.key === 's') {
+ e.preventDefault()
+ void saveRef.current()
+ }
+ }
+ window.addEventListener('keydown', onKey)
+ return () => window.removeEventListener('keydown', onKey)
+ }, [])
+
+ const update = (change: Partial<RoutineScheduleDraft>) => setSchedule((s) => ({ ...s, ...change }))
+ const shownPreview = sameDraft(checkedSchedule, schedule) ? preview : null
+
+ const triggerFields = () => {
+ switch (schedule.kind) {
+ case 'cron':
+ return (
+ <div className="routine-cron" style={{ display: 'flex', flexDirection: 'column', gap: 8 }}>
+ <div className="routine-cron-fields">
+ <Field label="Cron">
+ <input
+ className="routine-input"
+ style={font('body', undefined, 'monospaced')}
+ value={schedule.expression}
+ placeholder="0 9 * * 1-5"
+ spellCheck={false}
+ autoCorrect="off"
+ onChange={(e) => update({ expression: e.target.value })}
+ />
+ </Field>
+ <Field label="Time zone">
+ <input className="routine-input" value={schedule.zone} placeholder="Asia/Taipei" spellCheck={false} autoCorrect="off" onChange={(e) => update({ zone: e.target.value })} />
+ </Field>
+ </div>
+ <div style={{ paddingLeft: 4 }}>
+ {previewError ? (
+ <span style={{ ...font('caption'), color: 'var(--danger)', lineHeight: 1.4 }}>{previewError}</span>
+ ) : shownPreview ? (
+ <div style={{ display: 'flex', flexDirection: 'column', gap: 2 }}>
+ <span style={font('caption', 'medium')}>{shownPreview.summary}</span>
+ {shownPreview.nextRunAt ? (
+ <span style={{ ...font('caption'), color: 'var(--secondary)' }}>
+ Next {new Date(shownPreview.nextRunAt).toLocaleString(undefined, { dateStyle: 'medium', timeStyle: 'short' })}
+ </span>
+ ) : null}
+ {shownPreview.warning ? <span style={{ ...font('caption'), color: 'var(--secondary)' }}>{shownPreview.warning}</span> : null}
+ </div>
+ ) : (
+ <span style={{ ...font('caption', undefined, 'monospaced'), color: 'var(--tertiary)', whiteSpace: 'pre' }}>minute hour day month weekday</span>
+ )}
+ </div>
+ </div>
+ )
+ case 'webhook':
+ return routine && routine.triggers.some((t) => t.type === 'webhook' || t.type === 'event') ? (
+ <RoutineWebhookPanel botId={botId} routineId={routine.id} />
+ ) : (
+ <span style={{ ...caption, color: 'var(--secondary)' }}>Runs each time something is posted to its URL. Save to get the URL and key.</span>
+ )
+ default:
+ return <span style={{ ...caption, color: 'var(--secondary)' }}>Set up by the bot. Pick Schedule or Webhook to replace it.</span>
+ }
+ }
+
+ return (
+ <div style={{ display: 'flex', flexDirection: 'column', maxHeight: 700, minHeight: 0, background: 'var(--background)', color: 'var(--text)', ...font(13) }}>
+ <ModalHeader title={routine ? 'Edit routine' : 'Set up a routine'} />
+ {/* The card fits the form; it scrolls only when taller than the window allows. */}
+ <fieldset className="routine-form" disabled={busy || !loaded} style={{ flex: '1 1 auto', minHeight: 0, overflowY: 'auto' }}>
+ <div style={{ display: 'flex', flexDirection: 'column', gap: 24, padding: 20 }}>
+ <div style={{ display: 'flex', flexDirection: 'column', gap: 14 }}>
+ <Field label="Name">
+ <input className="routine-input" value={name} placeholder="e.g. Morning summary" onChange={(e) => setName(e.target.value)} />
+ </Field>
+ <Field label="Instruction">
+ <textarea className="routine-input" value={instruction} placeholder="Describe what this bot should do each time it runs." onChange={(e) => setInstruction(e.target.value)} />
+ </Field>
+ </div>
+ <div style={{ display: 'flex', flexDirection: 'column', gap: 14 }}>
+ <Field label="When to run">
+ <ChoicePicker selection={schedule.kind} options={options} onChange={(kind) => update({ kind })} fits />
+ </Field>
+ {loaded ? (
+ triggerFields()
+ ) : (
+ <span style={{ display: 'inline-flex', alignItems: 'center', gap: 6, color: 'var(--secondary)' }}>
+ <Icon name="clock" size={12} />
+ Loading schedule…
+ </span>
+ )}
+ </div>
+ <div style={{ display: 'flex', flexDirection: 'column', gap: 12 }}>
+ <button
+ className="press"
+ aria-expanded={advanced}
+ onClick={() => setAdvanced((a) => !a)}
+ style={{ display: 'flex', alignItems: 'center', gap: 6, paddingLeft: 4, ...font('compactSecondary'), color: 'var(--secondary)', width: '100%' }}
+ >
+ Run settings
+ <span style={{ display: 'flex', transform: `rotate(${advanced ? 180 : 0}deg)`, transition: 'transform var(--layout)' }}>
+ <Icon name="chevron.down" size={10} weight="semibold" />
+ </span>
+ <span style={{ flex: 1 }} />
+ {!advanced ? <span style={{ color: 'var(--tertiary)' }}>Timeout {timeout}s</span> : null}
+ </button>
+ {advanced ? (
+ <div className="routine-fade" style={{ display: 'flex', flexDirection: 'column', gap: 12 }}>
+ <Field label="Timeout (seconds)">
+ <input className="routine-input" value={timeout} placeholder="3600" onChange={(e) => setTimeoutText(e.target.value)} />
+ </Field>
+ <span style={{ ...caption, color: 'var(--secondary)' }}>A run is stopped after this long. 1 second to 24 hours.</span>
+ </div>
+ ) : null}
+ </div>
+ </div>
+ </fieldset>
+
+ <div style={{ display: 'flex', flexDirection: 'column', gap: 12, padding: 20, flex: 'none' }}>
+ {previewError !== null ? (
+ <button className="press" style={{ alignSelf: 'flex-start' }} disabled={store.isOffline} onClick={() => void checkSchedule()}>
+ Check schedule again
+ </button>
+ ) : null}
+ {!loaded && error !== null ? (
+ <button className="press" style={{ alignSelf: 'flex-start' }} onClick={() => void loadSchedule()}>
+ Retry loading schedule
+ </button>
+ ) : null}
+ {schedule.kind !== 'cron' && previewError ? <span style={{ ...font('caption'), color: 'var(--danger)' }}>{previewError}</span> : null}
+ {error ? (
+ <span style={{ display: 'flex', gap: 6, alignItems: 'flex-start', ...font('callout'), color: 'var(--danger)', userSelect: 'text' }}>
+ <Icon name="exclamationmark.circle" size={12} style={{ marginTop: 1 }} />
+ {error}
+ </span>
+ ) : null}
+ {store.isOffline ? (
+ <span style={{ display: 'flex', gap: 6, alignItems: 'center', ...font('caption'), color: 'var(--danger)' }}>
+ <Icon name="wifi.slash" size={10} />
+ Connect to this computer to save your routine.
+ </span>
+ ) : null}
+ <div style={{ display: 'flex', alignItems: 'center', gap: 16 }}>
+ {routine ? (
+ <>
+ <IconButton title="Delete routine" icon="trash" onClick={() => setConfirmDelete(true)} />
+ <IconButton
+ title={started ? 'Started' : 'Test run'}
+ icon={started ? 'checkmark' : 'play.circle'}
+ disabled={started || store.isOffline}
+ onClick={async () => {
+ try {
+ await (await store.ready()).call('runRoutine', { botId, id: routine.id })
+ setStarted(true)
+ } catch (e) {
+ setError(errorText(e))
+ }
+ }}
+ />
+ </>
+ ) : null}
+ <span style={{ flex: 1 }} />
+ <Button disabled={!canSave} onClick={() => void save()} title="Save (⌘S)">
+ {busy ? <Spinner size={12} /> : null}
+ {busy ? 'Saving…' : routine ? 'Save changes' : 'Create routine'}
+ </Button>
+ </div>
+ </div>
+
+ <Dialog
+ open={confirmDelete}
+ title="Delete routine?"
+ message="This deletes the routine and stops its future runs. This can't be undone."
+ actions={[
+ {
+ title: 'Delete routine',
+ destructive: true,
+ action: async () => {
+ if (!routine) return
+ try {
+ await (await store.ready()).call('deleteRoutine', { botId, id: routine.id })
+ saved(null)
+ } catch (e) {
+ setError(errorText(e))
+ }
+ },
+ },
+ ]}
+ onClose={() => setConfirmDelete(false)}
+ />
+ </div>
+ )
+}
diff --git a/apps/desktop/src/renderer/views/routines/RoutineWebhookPanel.tsx b/apps/desktop/src/renderer/views/routines/RoutineWebhookPanel.tsx
new file mode 100644
index 00000000..b8d0e842
--- /dev/null
+++ b/apps/desktop/src/renderer/views/routines/RoutineWebhookPanel.tsx
@@ -0,0 +1,119 @@
+import { useEffect, useState, type ReactNode } from 'react'
+import { IconButton } from '../../components/Controls'
+import { Dialog } from '../../components/Overlay'
+import { font } from '../../lib/fonts'
+import { useStore } from '../../store/context'
+import { errorText, type RoutineWebhook } from './routine'
+
+/** A labelled form field (kit's `Field`): the label, an optional detail, then the control. */
+export function Field({ label, detail, children }: { label: string; detail?: string; children: ReactNode }) {
+ return (
+ <div style={{ display: 'flex', flexDirection: 'column', gap: 8 }}>
+ <div style={{ display: 'flex', flexDirection: 'column', gap: 3 }}>
+ <span style={{ color: 'var(--text)' }}>{label}</span>
+ {detail ? <span style={{ ...font('caption'), color: 'var(--secondary)', lineHeight: 1.4 }}>{detail}</span> : null}
+ </div>
+ {children}
+ </div>
+ )
+}
+
+/** A saved webhook routine's address and key: copy either, reveal or replace the key. */
+export function RoutineWebhookPanel({ botId, routineId }: { botId: string; routineId: string }) {
+ const store = useStore()
+ const [webhook, setWebhook] = useState<RoutineWebhook | null>(null)
+ const [error, setError] = useState<string | null>(null)
+ const [showKey, setShowKey] = useState(false)
+ const [copied, setCopied] = useState<'url' | 'key' | null>(null)
+ const [confirmRotate, setConfirmRotate] = useState(false)
+ const [rotating, setRotating] = useState(false)
+
+ const load = async (rotate: boolean) => {
+ setRotating(rotate)
+ try {
+ const value = await (await store.ready()).call<RoutineWebhook>('routineWebhook', { botId, id: routineId, rotate })
+ setWebhook(value)
+ if (rotate) {
+ setCopied(null)
+ setShowKey(true)
+ }
+ setError(null)
+ } catch (e) {
+ setError(errorText(e))
+ } finally {
+ setRotating(false)
+ }
+ }
+
+ useEffect(() => {
+ void load(false)
+ }, [routineId])
+
+ const value = (text: string, copy: 'url' | 'key', extra?: ReactNode) => (
+ <div style={{ display: 'flex', alignItems: 'center', gap: 8, padding: '8px 6px 8px 12px', background: 'var(--bubble-user)', borderRadius: 12 }}>
+ <span
+ style={{
+ ...font('callout', undefined, 'monospaced'),
+ flex: 1,
+ minWidth: 0,
+ userSelect: 'text',
+ wordBreak: 'break-all',
+ display: '-webkit-box',
+ WebkitLineClamp: 2,
+ WebkitBoxOrient: 'vertical',
+ overflow: 'hidden',
+ }}
+ >
+ {text}
+ </span>
+ {extra}
+ <IconButton
+ title={copied === copy ? 'Copied' : 'Copy'}
+ icon={copied === copy ? 'checkmark' : 'doc.on.doc'}
+ onClick={() => {
+ if (!webhook) return
+ window.codync.app.copy(copy === 'key' ? webhook.key : (webhook.url ?? webhook.localUrl))
+ setCopied(copy)
+ }}
+ />
+ </div>
+ )
+
+ const caption = { ...font('caption'), paddingLeft: 4, lineHeight: 1.4 }
+ return (
+ <div style={{ display: 'flex', flexDirection: 'column', gap: 14 }}>
+ {webhook ? (
+ <>
+ <Field label={webhook.url ? 'URL' : 'Local URL'}>{value(webhook.url ?? webhook.localUrl, 'url')}</Field>
+ <Field label="Key">
+ {value(
+ showKey ? webhook.key : '•'.repeat(16),
+ 'key',
+ <>
+ <IconButton title={showKey ? 'Hide key' : 'Show key'} icon={showKey ? 'eye.slash' : 'eye'} onClick={() => setShowKey((s) => !s)} />
+ <IconButton title="Replace key" icon="arrow.triangle.2.circlepath" disabled={rotating || store.isOffline} onClick={() => setConfirmRotate(true)} />
+ </>,
+ )}
+ </Field>
+ <span style={{ ...caption, color: 'var(--secondary)' }}>
+ {webhook.url
+ ? 'POST with Authorization: Bearer <key>. For GitHub, use content type application/json and the key as the secret. ' +
+ 'Deliveries wait up to 72 hours while this computer is off. They pass through the Codync cloud, which can read them.'
+ : 'The Codync cloud is off, so only this computer can send to it.'}
+ </span>
+ </>
+ ) : error ? (
+ <span style={{ ...caption, color: 'var(--danger)' }}>{error}</span>
+ ) : (
+ <span style={{ ...caption, color: 'var(--secondary)' }}>Loading webhook…</span>
+ )}
+ <Dialog
+ open={confirmRotate}
+ title="Replace the key?"
+ message="Senders using the current key stop working until they get the new one."
+ actions={[{ title: 'Replace key', destructive: true, action: () => void load(true) }]}
+ onClose={() => setConfirmRotate(false)}
+ />
+ </div>
+ )
+}
diff --git a/apps/desktop/src/renderer/views/routines/RoutinesView.tsx b/apps/desktop/src/renderer/views/routines/RoutinesView.tsx
index 73339391..12bb2e31 100644
--- a/apps/desktop/src/renderer/views/routines/RoutinesView.tsx
+++ b/apps/desktop/src/renderer/views/routines/RoutinesView.tsx
@@ -1,3 +1,170 @@
-export function RoutinesView(_: { botId: string; initialId: string | null; onClose?: () => void }) {
- return <div>Routines</div>
+import { Fragment, useEffect, useRef, useState } from 'react'
+import { Hairline, IconButton, Switch } from '../../components/Controls'
+import { Sheet } from '../../components/Overlay'
+import { font } from '../../lib/fonts'
+import { useStore } from '../../store/context'
+import { errorText, isActiveRun, type Routine, type RoutineListing, type RoutineRun } from './routine'
+import { RoutineEditorView } from './RoutineEditorView'
+
+/** The open editor: on a routine, or (`routineId` null) setting up a new one. `id` is one per opening. */
+interface EditorTarget {
+ id: string
+ routineId: string | null
+}
+
+function runLabel(run: RoutineRun) {
+ switch (run.status) {
+ case 'pending':
+ return 'Queued'
+ case 'starting':
+ return 'Starting'
+ case 'running':
+ return 'Running'
+ case 'recovering':
+ return 'Resuming after restart'
+ case 'failed':
+ return 'Failed'
+ case 'interrupted':
+ return 'Interrupted'
+ default:
+ return run.status.charAt(0).toUpperCase() + run.status.slice(1)
+ }
+}
+
+/** The routine list in the details panel (and the routines sheet); a row opens its editor. */
+export function RoutinesView({ botId, initialId, onClose }: { botId: string; initialId: string | null; onClose?: () => void }) {
+ const store = useStore()
+ const [routines, setRoutines] = useState<Routine[]>([])
+ const [runs, setRuns] = useState<RoutineRun[]>([])
+ const [editing, setEditing] = useState<EditorTarget | null>(null)
+ const [loaded, setLoaded] = useState(false)
+ const [busy, setBusy] = useState(false)
+ const [error, setError] = useState<string | null>(null)
+ const [loadError, setLoadError] = useState<string | null>(null)
+ const current = useRef(botId)
+ current.current = botId
+
+ const open = (routineId: string | null) => setEditing({ id: crypto.randomUUID(), routineId })
+
+ const load = async () => {
+ const asked = botId
+ try {
+ const result = await (await store.ready()).call<RoutineListing>('routines', { botId: asked })
+ if (current.current !== asked) return
+ setRoutines(result.routines)
+ setRuns(result.runs)
+ setLoaded(true)
+ setLoadError(null)
+ } catch (e) {
+ if (current.current === asked) setLoadError(errorText(e))
+ }
+ }
+
+ useEffect(() => {
+ if (initialId) open(initialId)
+ }, [initialId])
+
+ useEffect(() => {
+ setRoutines([])
+ setRuns([])
+ setLoaded(false)
+ let stopped = false
+ let timer: ReturnType<typeof setTimeout> | undefined
+ const tick = async () => {
+ await load()
+ if (!stopped) timer = setTimeout(tick, 3000)
+ }
+ void tick()
+ return () => {
+ stopped = true
+ clearTimeout(timer)
+ }
+ }, [botId])
+
+ const action = async (method: string, routine: Routine, enabled?: boolean) => {
+ if (busy) return
+ setBusy(true)
+ try {
+ await (await store.ready()).call(method, { botId, id: routine.id, enabled: enabled ?? null })
+ setError(null)
+ await load()
+ } catch (e) {
+ setError(errorText(e))
+ } finally {
+ setBusy(false)
+ }
+ }
+
+ /** The row's second line: what's happening now, else when it runs. */
+ const summary = (routine: Routine) => {
+ const active = runs.find((r) => r.routineId === routine.id && isActiveRun(r))
+ if (active) return runLabel(active)
+ if (routine.lastError) return 'Schedule needs attention'
+ const last = runs.find((r) => r.routineId === routine.id)
+ if (last && ['failed', 'interrupted'].includes(last.status)) return `Last run ${runLabel(last).toLowerCase()}`
+ return routine.triggerDescriptions.join(' · ')
+ }
+
+ const onSaved = (target: EditorTarget, saved: Routine | null) => {
+ if (saved) setRoutines((list) => (list.some((r) => r.id === saved.id) ? list.map((r) => (r.id === saved.id ? saved : r)) : [...list, saved]))
+ // A new webhook routine stays open: its URL and key exist only now.
+ if (target.routineId === null && saved && saved.triggers.some((t) => t.type === 'webhook')) {
+ setEditing({ id: target.id, routineId: saved.id })
+ } else {
+ setEditing(null)
+ }
+ void load()
+ }
+
+ const editingRoutine = routines.find((r) => r.id === editing?.routineId) ?? null
+ const surface = { background: 'var(--surface)', borderRadius: 16 }
+ return (
+ <div style={{ display: 'flex', flexDirection: 'column', gap: onClose ? 16 : 10, color: 'var(--text)', ...font(13) }}>
+ <div style={{ display: 'flex', alignItems: 'center' }}>
+ <span style={{ ...font(13, 'semibold'), paddingLeft: 2 }}>Routines</span>
+ <span style={{ flex: 1 }} />
+ <IconButton title="Ask the bot for a routine" icon="text.bubble" disabled={store.isOffline} onClick={() => {
+ store.setRoutineDraft(botId, 'I want a routine that ')
+ onClose?.()
+ }} />
+ <IconButton title="Set up a routine" icon="plus" disabled={store.isOffline} onClick={() => open(null)} />
+ {onClose ? <IconButton title="Close routines" icon="xmark" onClick={onClose} /> : null}
+ </div>
+ {loadError ? <span style={{ ...font('caption'), color: 'var(--danger)' }}>{loadError}</span> : null}
+ {error ? <span style={{ ...font('caption'), color: 'var(--danger)', userSelect: 'text' }}>{error}</span> : null}
+ {routines.length === 0 ? (
+ <div style={{ ...surface, ...font(12), lineHeight: 1.45, color: 'var(--secondary)', padding: 14 }}>
+ {loaded ? 'No routines yet. Ask the bot for one, or set it up yourself with +.' : 'Loading routines…'}
+ </div>
+ ) : (
+ <div style={{ ...surface, padding: '0 14px' }}>
+ {routines.map((routine, i) => (
+ <Fragment key={routine.id}>
+ {i > 0 ? <Hairline /> : null}
+ <div style={{ display: 'flex', alignItems: 'center', gap: 12 }}>
+ <button className="routine-row" onClick={() => open(routine.id)}>
+ <span style={{ color: 'var(--text)' }}>{routine.name}</span>
+ <span style={{ ...font('caption'), color: routine.lastError ? 'var(--danger)' : 'var(--secondary)' }}>{summary(routine)}</span>
+ </button>
+ <span aria-label={routine.name} style={{ display: 'flex' }}>
+ <Switch on={routine.enabled} disabled={busy || store.isOffline} onChange={(on) => void action('setRoutineEnabled', routine, on)} />
+ </span>
+ </div>
+ </Fragment>
+ ))}
+ </div>
+ )}
+ {/* A routine opened before the list arrived waits for it. */}
+ <Sheet open={editing !== null && (editing.routineId === null || editingRoutine !== null)} onClose={() => setEditing(null)} width={540}>
+ {editing ? (
+ <RoutineEditorView
+ key={editing.id}
+ botId={botId}
+ routine={editingRoutine}
+ saved={(saved) => onSaved(editing, saved)}
+ />
+ ) : null}
+ </Sheet>
+ </div>
+ )
}
diff --git a/apps/desktop/src/renderer/views/routines/routine.ts b/apps/desktop/src/renderer/views/routines/routine.ts
new file mode 100644
index 00000000..a7a81c5a
--- /dev/null
+++ b/apps/desktop/src/renderer/views/routines/routine.ts
@@ -0,0 +1,101 @@
+// Routines (kit Routine.swift, RoutineScheduleDraft.swift). The host hydrates, compiles,
+// validates and describes schedules; the app only keeps form values.
+
+export interface RoutineTrigger {
+ type: string
+ seconds?: number | null
+ at?: number | null
+ expression?: string | null
+ timeZone?: string | null
+ source?: string | null
+ event?: string | null
+ filters?: Record<string, string> | null
+}
+
+export interface Routine {
+ id: string
+ botId: string
+ name: string
+ instruction: string
+ triggers: RoutineTrigger[]
+ enabled: boolean
+ createdAt: number
+ updatedAt: number
+ triggerDescriptions: string[]
+ nextRunAt?: number | null
+ timeoutSeconds?: number | null
+ lastError?: string | null
+}
+
+export interface RoutineRun {
+ id: string
+ routineId: string
+ botId: string
+ status: string
+ createdAt: number
+ finishedAt?: number | null
+ detail?: string | null
+ rootId?: string | null
+}
+
+export const isActiveRun = (run: RoutineRun) => ['pending', 'starting', 'running', 'recovering'].includes(run.status)
+
+export interface RoutineListing {
+ routines: Routine[]
+ runs: RoutineRun[]
+}
+
+/** `url` is public through the Codync cloud (null while the cloud is off), `localUrl` works on the host itself. */
+export interface RoutineWebhook {
+ url?: string | null
+ localUrl: string
+ key: string
+ connected: boolean
+}
+
+/** Form values only. */
+export interface RoutineScheduleDraft {
+ kind: string
+ amount: string
+ unit: number
+ calendarStyle: string
+ weekday: number
+ selectedDays: number[]
+ monthDay: number
+ minuteStep: number
+ hour: number
+ minute: number
+ at: number
+ expression: string
+ zone: string
+ original: RoutineTrigger[]
+}
+
+export const emptyDraft = (): RoutineScheduleDraft => ({
+ kind: 'cron',
+ amount: '1',
+ unit: 3600,
+ calendarStyle: 'daily',
+ weekday: 1,
+ selectedDays: [],
+ monthDay: 1,
+ minuteStep: 15,
+ hour: 9,
+ minute: 0,
+ at: 0,
+ expression: '',
+ zone: '',
+ original: [],
+})
+
+export const sameDraft = (a: RoutineScheduleDraft | null, b: RoutineScheduleDraft | null) => !!a && !!b && JSON.stringify(a) === JSON.stringify(b)
+
+export interface RoutineSchedulePreview {
+ draft: RoutineScheduleDraft
+ triggers: RoutineTrigger[]
+ summary: string
+ nextRunAt?: number | null
+ warning?: string | null
+}
+
+export const errorText = (e: unknown) => (e instanceof Error ? e.message : String(e))
diff --git a/apps/desktop/src/renderer/views/routines/routines.css b/apps/desktop/src/renderer/views/routines/routines.css
new file mode 100644
index 00000000..190f2fd4
--- /dev/null
+++ b/apps/desktop/src/renderer/views/routines/routines.css
@@ -0,0 +1,80 @@
+/* A routine form's text field: plain, filled, rounded (kit `routineInput`). */
+.routine-input {
+ width: 100%;
+ padding: 12px;
+ border: none;
+ outline: none;
+ border-radius: 12px;
+ background: var(--bubble-user);
+ color: var(--text);
+ font: inherit;
+ user-select: text;
+ resize: none;
+}
+.routine-input::placeholder {
+ color: var(--secondary);
+}
+textarea.routine-input {
+ field-sizing: content;
+ min-height: calc(3lh + 24px);
+ max-height: calc(8lh + 24px);
+}
+
+.routine-form {
+ border: none;
+ margin: 0;
+ padding: 0;
+ min-width: 0;
+}
+.routine-form:disabled {
+ opacity: 0.6;
+}
+
+/* Cron and time zone side by side, stacked when the card is narrow. */
+.routine-cron {
+ container-type: inline-size;
+}
+.routine-cron-fields {
+ display: flex;
+ align-items: flex-start;
+ gap: 10px;
+}
+.routine-cron-fields > :first-child {
+ flex: 1;
+ min-width: 220px;
+}
+.routine-cron-fields > :last-child {
+ width: 170px;
+ flex: none;
+}
+@container (max-width: 400px) {
+ .routine-cron-fields {
+ flex-direction: column;
+ align-items: stretch;
+ gap: 14px;
+ }
+ .routine-cron-fields > :first-child,
+ .routine-cron-fields > :last-child {
+ width: auto;
+ min-width: 0;
+ }
+}
+
+.routine-row {
+ display: flex;
+ flex-direction: column;
+ gap: 3px;
+ padding: 10px 0;
+ flex: 1;
+ min-width: 0;
+ text-align: left;
+}
+
+.routine-fade {
+ animation: routine-fade var(--hover) both;
+}
+@keyframes routine-fade {
+ from {
+ opacity: 0;
+ }
+}
diff --git a/apps/desktop/src/renderer/views/settings/ApprovalSheet.tsx b/apps/desktop/src/renderer/views/settings/ApprovalSheet.tsx
index fa768f1c..bb4360fa 100644
--- a/apps/desktop/src/renderer/views/settings/ApprovalSheet.tsx
+++ b/apps/desktop/src/renderer/views/settings/ApprovalSheet.tsx
@@ -1,4 +1,75 @@
+import { useState } from 'react'
+import { Button, Spinner } from '../../components/Controls'
+import { Icon } from '../../components/Icon'
+import { ModalHeader } from '../../components/Overlay'
+import { font } from '../../lib/fonts'
import type { Approval } from '../../store/app-model'
-export function ApprovalSheet(_: { approval: Approval }) {
- return <div style={{ padding: 24 }}>Approval</div>
+import { useApp } from '../../store/context'
+import { errorText, Reveal, spacedCode } from './parts'
+
+/**
+ * A device asks for access through the account (spec §4.2 B). The code is the only thing that
+ * tells a real request from a cloud swapping keys, so it's big and Approve is never the default.
+ * Closing puts the request off (the sheet's onClose defers it).
+ */
+export function ApprovalSheet({ approval }: { approval: Approval }) {
+ const app = useApp()
+ const [busy, setBusy] = useState(false)
+ const [error, setError] = useState<string | null>(null)
+ const request = approval.request
+
+ const decide = async (approve: boolean) => {
+ setBusy(true)
+ try {
+ await app.decide(approval, approve)
+ // The host's accessRequests event removes it; don't wait for that to close.
+ app.deferApproval(approval)
+ } catch (e) {
+ setError(errorText(e))
+ } finally {
+ setBusy(false)
+ }
+ }
+
+ const center = { textAlign: 'center' as const }
+ return (
+ <div style={{ width: 420, display: 'flex', flexDirection: 'column' }}>
+ <ModalHeader title="Access request" />
+ <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 16, padding: '0 24px 24px' }}>
+ <Icon name={request.platform === 'macos' ? 'laptopcomputer' : 'iphone'} size={40} weight="light" color="var(--secondary)" />
+ <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 4 }}>
+ <div style={{ ...font('title3', 'semibold'), ...center }}>
+ {request.deviceName} wants to use {approval.store.hostName}
+ </div>
+ {request.email ? <div style={{ ...font('callout'), color: 'var(--secondary)' }}>Signed in as {request.email}</div> : null}
+ </div>
+ {request.code ? (
+ <>
+ <div className="selectable settings-appear" style={{ ...font(48, 'semibold', 'monospaced'), color: 'var(--text)' }} aria-label={`Code ${request.code.split('').join(' ')}`}>
+ {spacedCode(request.code)}
+ </div>
+ <div style={{ ...font('callout'), color: 'var(--secondary)', ...center }}>
+ Approve only if {request.deviceName} shows exactly this code. If it doesn't, deny: someone may be trying to get in.
+ </div>
+ </>
+ ) : (
+ <>
+ <Spinner />
+ <div style={{ ...font('callout'), color: 'var(--secondary)' }}>Waiting for {request.deviceName} to show its code…</div>
+ </>
+ )}
+ <Reveal show={error !== null}>
+ <div style={{ ...font('caption'), color: 'var(--danger)', ...center }}>{error}</div>
+ </Reveal>
+ <div style={{ display: 'flex', gap: 12 }}>
+ <Button kind="secondary" onClick={() => void decide(false)} disabled={busy}>
+ Deny
+ </Button>
+ <Button onClick={() => void decide(true)} disabled={busy || !request.code}>
+ Approve
+ </Button>
+ </div>
+ </div>
+ </div>
+ )
}
diff --git a/apps/desktop/src/renderer/views/settings/ComputersView.tsx b/apps/desktop/src/renderer/views/settings/ComputersView.tsx
new file mode 100644
index 00000000..841023a6
--- /dev/null
+++ b/apps/desktop/src/renderer/views/settings/ComputersView.tsx
@@ -0,0 +1,526 @@
+import { useCallback, useEffect, useState } from 'react'
+import QRCode from 'qrcode'
+import type { AccountApproval, AuthorizedDevice, PairingInfo } from '@shared/models'
+import { newSSHProfile, SSH_INSTALL_COMMAND, type SSHProfile, type SSHStatus } from '@shared/ssh'
+import { Button, CardSection, IconButton, Spinner, Switch } from '../../components/Controls'
+import { Icon } from '../../components/Icon'
+import { Dialog, ModalHeader, Sheet, useDismiss } from '../../components/Overlay'
+import { font } from '../../lib/fonts'
+import { useModel } from '../../lib/observable'
+import { useAccount } from '../../store/account'
+import type { BotStore } from '../../store/bot-store'
+import { StoreContext, useApp } from '../../store/context'
+import { ComputerBadge } from '../ComputerBadge'
+import { errorText, liveClient, relativeDay, Reveal, SectionHeader, statusText, thisMac } from './parts'
+import { sshBridge, useSSH } from './ssh-model'
+import { VoiceChatSettingsView } from './VoiceChatSettingsView'
+
+/**
+ * Computers this app manages (itself and SSH), with their account, relay and authorized devices;
+ * SSH profiles; and the account's other computers.
+ */
+export function ComputersView() {
+ const app = useApp()
+ const account = useAccount()
+ const sshState = useSSH()
+ const cloud = useModel(app.cloud)
+ const [editing, setEditing] = useState<{ profile: SSHProfile; isNew: boolean } | null>(null)
+ const managed = app.managedStores
+ const managedIds = new Set(managed.map((s) => s.computer.id))
+ const reached = app.computers.filter((c) => !managedIds.has(c.id))
+ const others = cloud.computers.filter((c) => !managedIds.has(c.computerId) && !reached.some((r) => r.id === c.computerId))
+
+ return (
+ <div style={{ height: '100%', overflowY: 'auto', background: 'var(--background)' }}>
+ <div style={{ display: 'flex', flexDirection: 'column', gap: 26, padding: '14px 14px 20px' }}>
+ {managed.map((store) => (
+ <ManagedComputerCard key={store.computer.id} store={store} ssh={sshState.isSSH(store.computer.id)} />
+ ))}
+
+ <div>
+ <SectionHeader title="Over SSH">
+ {sshBridge() ? <IconButton title="Add SSH computer" icon="plus" onClick={() => setEditing({ profile: newSSHProfile(), isNew: true })} /> : null}
+ </SectionHeader>
+ <Reveal show={sshState.state.profiles.length === 0}>
+ <div style={{ ...font('callout'), color: 'var(--secondary)' }}>
+ Run bots on another computer you reach with SSH. It needs codync-host installed; your SSH keys stay on {thisMac}.
+ </div>
+ </Reveal>
+ {sshState.state.profiles.map((profile) => (
+ <SSHRow key={profile.id} profile={profile} status={sshState.status(profile.id)} edit={() => setEditing({ profile, isNew: false })} />
+ ))}
+ </div>
+
+ <div>
+ <SectionHeader title="In your account">
+ {account.isSignedIn ? <IconButton title="Refresh" icon="arrow.clockwise" onClick={() => void cloud.refresh()} /> : null}
+ </SectionHeader>
+ {!account.isSignedIn ? (
+ <div style={{ ...font('callout'), color: 'var(--secondary)' }}>Sign in to reach the other computers in your account from {thisMac}.</div>
+ ) : reached.length === 0 && others.length === 0 ? (
+ <div style={{ ...font('callout'), color: 'var(--secondary)' }}>No other computers in your account yet.</div>
+ ) : null}
+ {reached.map((computer) => {
+ const store = app.store(computer.id)
+ if (!store) return null
+ const cloudEntry = cloud.computers.find((c) => c.computerId === computer.id)
+ const stale = !!cloudEntry && cloud.isOlderCopy(cloudEntry) && store.connection.kind !== 'unauthorized'
+ return (
+ <div key={computer.id} className="settings-card settings-appear">
+ <div className="settings-row">
+ <ComputerBadge computer={store.computer} size={22} />
+ <div className="settings-stack">
+ <span style={font('callout', 'medium')}>{store.hostName}</span>
+ <span style={{ ...font('caption'), color: stale ? 'var(--warning)' : 'var(--secondary)' }}>
+ {stale ? `Older copy of this computer · ${statusText(store)}` : statusText(store)}
+ </span>
+ </div>
+ <RouteIcon store={store} />
+ <span style={{ flex: 1 }} />
+ <IconButton title={`Remove from ${thisMac}`} icon="minus.circle" onClick={() => app.detach(computer.id)} />
+ </div>
+ </div>
+ )
+ })}
+ {others.map((computer) => (
+ // An account computer this app isn't authorized on. Asking for access lives in the cloud model.
+ <div key={computer.computerId} className="settings-card settings-appear">
+ <div className="settings-row">
+ <ComputerBadge computer={{ id: computer.computerId, name: computer.name, signKey: computer.signKey, urls: [], device: computer.device ?? null }} size={22} />
+ <div className="settings-stack">
+ <span style={font('callout', 'medium')}>{computer.name}</span>
+ <span style={{ ...font('caption'), color: 'var(--secondary)' }}>{computer.online ? 'Online' : 'Offline'}</span>
+ </div>
+ </div>
+ </div>
+ ))}
+ </div>
+ </div>
+
+ <Sheet open={editing !== null} onClose={() => setEditing(null)} width={460}>
+ {editing ? <SSHProfileEditor key={editing.profile.id} profile={editing.profile} isNew={editing.isNew} /> : null}
+ </Sheet>
+ </div>
+ )
+}
+
+/** How this app reaches a computer: direct LAN or the encrypted relay. */
+function RouteIcon({ store }: { store: BotStore }) {
+ if (store.connection.kind !== 'online' || !store.hostRoute || store.hostRoute === 'loopback') return null
+ const relay = store.hostRoute === 'relay'
+ return (
+ <span title={relay ? 'Through Cloudflare (encrypted)' : 'Direct: Wi-Fi or Tailscale'} aria-label={relay ? 'Through Cloudflare' : 'Wi-Fi or Tailscale'} style={{ display: 'flex', color: 'var(--tertiary)' }}>
+ <Icon name={relay ? 'cloud' : 'wifi'} size={12} />
+ </span>
+ )
+}
+
+const isMacLabel = thisMac === 'this Mac' ? 'This Mac' : 'This computer'
+
+/** This computer or an SSH computer: relay, account membership, pairing and authorized devices. */
+function ManagedComputerCard({ store, ssh: overSSH }: { store: BotStore; ssh: boolean }) {
+ const app = useApp()
+ const account = useAccount()
+ useModel(store)
+ const [devices, setDevices] = useState<AuthorizedDevice[] | null>(null)
+ const [busy, setBusy] = useState(false)
+ const [showPairing, setShowPairing] = useState(false)
+ const [showVoice, setShowVoice] = useState(false)
+ const [confirmRevoke, setConfirmRevoke] = useState<AuthorizedDevice | null>(null)
+ const [confirmUnclaim, setConfirmUnclaim] = useState(false)
+ const [confirmAutoApproval, setConfirmAutoApproval] = useState(false)
+ const cloud = store.cloud
+ const autoApproval = cloud?.approval === 'auto'
+ const online = store.connection.kind === 'online'
+ const name = store.hostName
+
+ const run = (work: () => Promise<void>) => {
+ setBusy(true)
+ void work().finally(() => setBusy(false))
+ }
+
+ const loadDevices = useCallback(async () => {
+ const client = liveClient(store)
+ if (!client) return
+ try {
+ setDevices((await client.call<{ devices: AuthorizedDevice[] }>('devices')).devices)
+ } catch {}
+ }, [store])
+
+ useEffect(() => void loadDevices(), [loadDevices, online, store.accessRequests.length])
+
+ const setApproval = (approval: AccountApproval) =>
+ run(async () => {
+ try {
+ await (await store.ready()).call('setApproval', { approval })
+ } catch (e) {
+ store.setError(errorText(e))
+ }
+ })
+
+ const cloudLine = !cloud?.enabled
+ ? 'Off: only Wi-Fi and Tailscale reach it, nothing goes through Cloudflare.'
+ : (cloud.lastError ?? (cloud.connected ? 'Reachable from anywhere through Cloudflare (encrypted).' : 'Connecting to Cloudflare…'))
+
+ const owner = cloud?.owner ?? null
+ const accountLine = (
+ <div className="settings-row" style={{ gap: 8 }}>
+ <Icon name={owner ? 'person.crop.circle.badge.checkmark' : 'person.crop.circle.badge.plus'} size={13} color="var(--secondary)" />
+ <span style={{ ...font('callout'), flex: 1 }}>
+ {owner
+ ? owner.userId === account.user?.userId
+ ? 'In your account'
+ : `In ${owner.email ?? 'another'} account`
+ : account.isSignedIn
+ ? 'Not in your account'
+ : 'Sign in to add it to your account'}
+ </span>
+ {owner ? (
+ <Button kind="secondary" onClick={() => setConfirmUnclaim(true)} disabled={busy || !online}>
+ Remove from account
+ </Button>
+ ) : account.isSignedIn ? (
+ <Button kind="secondary" onClick={() => run(async () => void (await app.cloud.claim(store)))} disabled={busy || !online}>
+ {busy ? 'Adding…' : 'Add to account'}
+ </Button>
+ ) : null}
+ </div>
+ )
+
+ return (
+ <div style={{ display: 'flex', flexDirection: 'column', gap: 28 }}>
+ <CardSection>
+ {[
+ <div key="header" className="settings-row">
+ <ComputerBadge computer={store.computer} size={26} />
+ <div className="settings-stack" style={{ flex: 1 }}>
+ <span style={font('headline')}>{name}</span>
+ <span style={{ ...font('caption'), color: 'var(--secondary)' }}>
+ {overSSH ? 'Over SSH' : isMacLabel} · {statusText(store)}
+ </span>
+ </div>
+ {overSSH ? (
+ // This computer's voice settings are their own Settings page.
+ <IconButton title="Voice chat" icon="waveform" selected={showVoice} disabled={!online} onClick={() => setShowVoice((s) => !s)} />
+ ) : null}
+ <IconButton title="Pair iPhone" icon="qrcode" selected={showPairing} disabled={!online} onClick={() => setShowPairing((s) => !s)} />
+ </div>,
+ <Switch key="cloud" on={cloud?.enabled ?? false} disabled={busy || !online} onChange={(on) => run(() => app.cloud.setCloud(store, on))}>
+ <span className="settings-stack">
+ <span>Reach from anywhere</span>
+ <span style={{ ...font('caption'), color: cloud?.lastError ? 'var(--warning)' : 'var(--secondary)' }}>{cloudLine}</span>
+ </span>
+ </Switch>,
+ accountLine,
+ ...(owner
+ ? [
+ <Switch
+ key="approval"
+ on={autoApproval}
+ disabled={busy || !online}
+ onChange={(on) => (on ? setConfirmAutoApproval(true) : setApproval('code'))}
+ >
+ <span className="settings-stack settings-appear">
+ <span>Skip the 6-digit check</span>
+ <span style={{ ...font('caption'), color: autoApproval ? 'var(--warning)' : 'var(--secondary)' }}>
+ {autoApproval ? 'Devices on your account get in on their own.' : 'Each new device shows a code you approve here.'}
+ </span>
+ </span>
+ </Switch>,
+ ]
+ : []),
+ ]}
+ </CardSection>
+
+ <CardSection title={`Devices that can use ${name}`}>
+ {devices === null
+ ? [<Spinner key="loading" />]
+ : [
+ ...(devices.length === 0
+ ? [<span key="none" style={{ color: 'var(--secondary)' }}>None yet. Pair an iPhone, or approve one from your account.</span>]
+ : []),
+ ...devices.map((device) => (
+ <div key={device.key} className="settings-row settings-appear" style={{ gap: 8 }}>
+ <span style={{ width: 18, display: 'flex', justifyContent: 'center', color: 'var(--secondary)' }}>
+ <Icon name={device.platform === 'macos' ? 'laptopcomputer' : 'iphone'} size={13} />
+ </span>
+ <div className="settings-stack" style={{ flex: 1 }}>
+ <span>{device.name}</span>
+ <span style={{ ...font('caption'), color: 'var(--tertiary)' }}>{deviceDetail(device)}</span>
+ </div>
+ <IconButton title="Revoke access" icon="xmark.circle" onClick={() => setConfirmRevoke(device)} />
+ </div>
+ )),
+ ]}
+ </CardSection>
+
+ <Sheet open={showVoice} onClose={() => setShowVoice(false)}>
+ <StoreContext.Provider value={store}>
+ <VoiceChatSettingsView />
+ </StoreContext.Provider>
+ </Sheet>
+ <Sheet open={showPairing} onClose={() => setShowPairing(false)} width={340}>
+ <ModalHeader title={`Pair iPhone with ${name}`} />
+ <PairingPanel store={store} />
+ </Sheet>
+ <Dialog
+ open={confirmRevoke !== null}
+ title={`Revoke ${confirmRevoke?.name ?? 'device'}?`}
+ message="It disconnects right away and has to pair or ask again."
+ actions={
+ confirmRevoke
+ ? [{
+ title: 'Revoke access',
+ destructive: true,
+ action: () =>
+ run(async () => {
+ try {
+ await (await store.ready()).call('revokeDevice', { key: confirmRevoke.key })
+ } catch (e) {
+ store.setError(errorText(e))
+ }
+ await loadDevices()
+ }),
+ }]
+ : []
+ }
+ onClose={() => setConfirmRevoke(null)}
+ />
+ <Dialog
+ open={confirmAutoApproval}
+ title="Let account devices in without a code?"
+ message={`Any device signed in to your account gets full control of ${name}: its files, terminals and bots, with no check here. If someone gets into your account, or Codync's cloud is ever compromised, they could add their own device and you'd have no chance to stop it. Devices paired with a QR code aren't affected.`}
+ actions={[{ title: 'Skip the check', destructive: true, action: () => setApproval('auto') }]}
+ onClose={() => setConfirmAutoApproval(false)}
+ />
+ <Dialog
+ open={confirmUnclaim}
+ title={`Remove ${name} from the account?`}
+ message="Devices that were approved through the account lose access. Devices paired with a QR code keep it."
+ actions={[{ title: 'Remove from account', destructive: true, action: () => run(() => app.cloud.unclaim(store)) }]}
+ onClose={() => setConfirmUnclaim(false)}
+ />
+ </div>
+ )
+}
+
+function deviceDetail(device: AuthorizedDevice) {
+ const parts = [device.source === 'account' ? 'Approved through the account' : 'Paired with a QR code']
+ if (device.connected) parts.push('connected')
+ else if (device.lastSeenAt) parts.push(`seen ${relativeDay(new Date(device.lastSeenAt))}`)
+ return parts.join(' · ')
+}
+
+/** A one-time pairing QR (v3) from a computer this app manages: its keys, a code, and how to reach it. */
+function PairingPanel({ store }: { store: BotStore }) {
+ useModel(store)
+ const [info, setInfo] = useState<PairingInfo | null>(null)
+ const [qr, setQr] = useState<string | null>(null)
+ const [error, setError] = useState<string | null>(null)
+ const name = store.hostName
+
+ const load = useCallback(async () => {
+ const client = liveClient(store)
+ if (!client) return setError(`${store.hostName} isn't connected.`)
+ try {
+ const pairing = await client.call<PairingInfo>('pairing')
+ setQr(await QRCode.toDataURL(pairing.pairingUrl, { errorCorrectionLevel: 'M', margin: 0, width: 400, color: { dark: '#000000', light: '#ffffff' } }))
+ setInfo(pairing)
+ setError(null)
+ } catch (e) {
+ setError(errorText(e))
+ }
+ }, [store])
+
+ useEffect(() => void load(), [load])
+ // A fresh code when this one expires while it's on screen.
+ useEffect(() => {
+ if (!info?.expiresAt) return
+ const timer = setTimeout(() => void load(), Math.max(1000, info.expiresAt - Date.now()))
+ return () => clearTimeout(timer)
+ }, [info?.expiresAt, load])
+
+ const caption = { ...font('caption'), textAlign: 'center' as const }
+ return (
+ <div style={{ padding: 16, display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 12 }}>
+ {info && qr ? (
+ <>
+ <div className="settings-appear" style={{ padding: 10, background: '#fff', borderRadius: 12 }}>
+ <img src={qr} alt={`Pairing code for ${name}`} width={200} height={200} style={{ display: 'block', imageRendering: 'pixelated' }} />
+ </div>
+ <div style={{ ...caption, color: 'var(--secondary)' }}>
+ Scan with the Codync app or the iPhone Camera. No Tailscale or open ports needed: your iPhone connects directly on the same Wi-Fi, and through the encrypted relay anywhere else.
+ </div>
+ {store.cloud?.enabled !== true ? (
+ <div style={{ ...caption, color: 'var(--warning)' }}>
+ {info.urls.length === 0
+ ? 'No network address found and “Reach from anywhere” is off. Connect to Wi-Fi or turn it on.'
+ : `“Reach from anywhere” is off, so the iPhone only reaches ${name} on the same network.`}
+ </div>
+ ) : null}
+ <div style={{ display: 'flex', alignItems: 'center', gap: 4 }}>
+ {info.expiresAt ? (
+ <span style={{ ...font('caption2'), color: 'var(--tertiary)' }}>
+ Works once, until {new Date(info.expiresAt).toLocaleTimeString(undefined, { hour: 'numeric', minute: '2-digit' })}
+ </span>
+ ) : null}
+ <IconButton title="New code" icon="arrow.clockwise" onClick={() => void load()} />
+ <IconButton title="Copy pairing link" icon="doc.on.doc" onClick={() => window.codync.app.copy(info.pairingUrl)} />
+ </div>
+ </>
+ ) : error ? (
+ <>
+ <div style={{ ...caption, color: 'var(--danger)' }}>{error}</div>
+ <IconButton title="Try again" icon="arrow.clockwise" onClick={() => void load()} />
+ </>
+ ) : (
+ <Spinner size={20} />
+ )}
+ </div>
+ )
+}
+
+// MARK: SSH
+
+function SSHRow({ profile, status, edit }: { profile: SSHProfile; status: SSHStatus; edit: () => void }) {
+ const bridge = sshBridge()
+ const target = `${profile.user ? `${profile.user}@` : ''}${profile.host}${profile.port !== null ? `:${profile.port}` : ''}`
+ const line = (() => {
+ switch (status.kind) {
+ case 'idle':
+ return `${target} · Not connected`
+ case 'connecting':
+ return status.step
+ case 'confirmHostKey':
+ return `${target} · Confirm the host key`
+ case 'connected':
+ return `${target} · Connected`
+ case 'retrying':
+ return `${status.message} Retrying…`
+ case 'failed':
+ return status.message
+ case 'notInstalled':
+ return `codync-host isn't installed on ${profile.host}. Install it there:`
+ }
+ })()
+ const problem = status.kind === 'retrying' || status.kind === 'failed' || status.kind === 'notInstalled'
+ const active = status.kind === 'connected' || status.kind === 'connecting' || status.kind === 'retrying'
+ return (
+ <div className="settings-card settings-appear">
+ <div className="settings-row">
+ <span style={{ width: 22, display: 'flex', justifyContent: 'center', color: 'var(--secondary)' }}>
+ <Icon name="terminal" size={13} />
+ </span>
+ <div className="settings-stack" style={{ flex: 1 }}>
+ <span style={font('callout', 'medium')}>{profile.name || profile.host}</span>
+ <span style={{ ...font('caption'), color: problem ? 'var(--warning)' : 'var(--secondary)' }}>{line}</span>
+ </div>
+ {active ? (
+ <IconButton title="Disconnect" icon="stop.circle" onClick={() => bridge?.disconnect(profile.id)} />
+ ) : (
+ <IconButton title="Connect" icon="arrow.clockwise" onClick={() => bridge?.connect(profile.id)} />
+ )}
+ <IconButton title="Edit" icon="pencil" onClick={edit} />
+ <IconButton title="Remove" icon="trash" onClick={() => bridge?.remove(profile.id)} />
+ </div>
+ <Reveal show={status.kind === 'confirmHostKey'}>
+ {status.kind === 'confirmHostKey' ? (
+ <div style={{ display: 'flex', flexDirection: 'column', gap: 6 }}>
+ <span style={{ ...font('caption'), color: 'var(--secondary)' }}>
+ First connection to {status.name}. Check that its host key fingerprint matches what the computer's owner sees (
+ <code style={{ fontFamily: 'var(--mono)' }}>ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub</code>).
+ </span>
+ {status.fingerprints.map((f) => (
+ <span key={f} className="selectable" style={font('caption', undefined, 'monospaced')}>{f}</span>
+ ))}
+ <div style={{ display: 'flex', gap: 8 }}>
+ <Button onClick={() => bridge?.trustHostKey(profile.id)}>Trust and connect</Button>
+ <Button kind="secondary" onClick={() => bridge?.disconnect(profile.id)}>Cancel</Button>
+ </div>
+ </div>
+ ) : null}
+ </Reveal>
+ <Reveal show={status.kind === 'notInstalled'}>
+ <div className="settings-row" style={{ gap: 6 }}>
+ <span className="settings-code selectable">{SSH_INSTALL_COMMAND}</span>
+ <IconButton title="Copy install command" icon="doc.on.doc" onClick={() => window.codync.app.copy(SSH_INSTALL_COMMAND)} />
+ </div>
+ </Reveal>
+ </div>
+ )
+}
+
+/** Add or change an SSH computer. Only fields ssh takes as separate arguments; nothing goes through a shell. */
+function SSHProfileEditor({ profile: initial, isNew }: { profile: SSHProfile; isNew: boolean }) {
+ const dismiss = useDismiss()
+ const [profile, setProfile] = useState(initial)
+ const [port, setPort] = useState(initial.port?.toString() ?? '')
+ const [remotePort, setRemotePort] = useState(String(initial.remotePort))
+ const [user, setUser] = useState(initial.user ?? '')
+ const [problem, setProblem] = useState<string | null>(null)
+ const bridge = sshBridge()
+
+ const save = async () => {
+ const isInt = (s: string) => /^-?\d+$/.test(s)
+ const portText = port.trim()
+ if (portText && !isInt(portText)) return setProblem('The SSH port must be a number.')
+ const remote = remotePort.trim()
+ if (!isInt(remote)) return setProblem('The Codync port must be a number.')
+ const u = user.trim()
+ const p: SSHProfile = {
+ ...profile,
+ host: profile.host.trim(),
+ name: profile.name.trim(),
+ user: u || null,
+ port: portText ? Number(portText) : null,
+ remotePort: Number(remote),
+ }
+ const found = bridge ? await bridge.save(p) : 'SSH computers need a newer build of Codync.'
+ if (found) return setProblem(found)
+ dismiss()
+ }
+
+ const field = (label: string, value: string, set: (v: string) => void, prompt: string) => (
+ <div className="settings-row" style={{ gap: 12 }}>
+ <span style={{ color: 'var(--text)' }}>{label}</span>
+ <input className="settings-field" aria-label={label} placeholder={prompt} value={value} spellCheck={false} onChange={(e) => set(e.target.value)} />
+ </div>
+ )
+
+ return (
+ // Return in a field saves (the default action).
+ <div style={{ width: 460, display: 'flex', flexDirection: 'column' }} onKeyDown={(e) => e.key === 'Enter' && e.target instanceof HTMLInputElement && void save()}>
+ <ModalHeader title={isNew ? 'Add SSH computer' : 'Edit SSH computer'} trailing={<IconButton title={isNew ? 'Add' : 'Save'} icon="checkmark" onClick={() => void save()} />} />
+ <div style={{ display: 'flex', flexDirection: 'column', gap: 16, padding: '4px 20px 20px' }}>
+ <CardSection>
+ {field('Name', profile.name, (name) => setProfile({ ...profile, name }), 'Optional')}
+ {field('Host', profile.host, (host) => setProfile({ ...profile, host }), 'SSH alias or hostname')}
+ {field('User', user, setUser, 'From SSH config')}
+ {field('SSH port', port, setPort, 'From SSH config')}
+ <div className="settings-row">
+ <span>Key</span>
+ <span style={{ flex: 1 }} />
+ <span style={{ color: 'var(--secondary)' }}>{profile.identityFile?.split('/').pop() ?? 'ssh-agent / SSH config'}</span>
+ <IconButton
+ title="Choose key file"
+ icon="key"
+ onClick={() => void bridge?.chooseKey().then((path) => path && setProfile((p) => ({ ...p, identityFile: path })))}
+ />
+ {profile.identityFile ? (
+ <span className="settings-appear" style={{ display: 'flex' }}>
+ <IconButton title="Use ssh-agent" icon="xmark.circle" onClick={() => setProfile({ ...profile, identityFile: null })} />
+ </span>
+ ) : null}
+ </div>
+ {field('Codync port', remotePort, setRemotePort, '')}
+ </CardSection>
+ <span style={{ ...font('caption'), color: 'var(--secondary)' }}>
+ Codync opens an SSH tunnel to codync-host on that computer's loopback. It uses your SSH config and ssh-agent; agent forwarding stays off.
+ </span>
+ <Reveal show={problem !== null}>
+ <span style={{ ...font('caption'), color: 'var(--danger)' }}>{problem}</span>
+ </Reveal>
+ </div>
+ </div>
+ )
+}
diff --git a/apps/desktop/src/renderer/views/settings/SettingsView.tsx b/apps/desktop/src/renderer/views/settings/SettingsView.tsx
index 3bdb6371..ee22c531 100644
--- a/apps/desktop/src/renderer/views/settings/SettingsView.tsx
+++ b/apps/desktop/src/renderer/views/settings/SettingsView.tsx
@@ -1,4 +1,227 @@
-export type SettingsPage = 'general' | 'usage' | 'computers'
-export function SettingsView(_: { page: SettingsPage }) {
- return <div style={{ padding: 24 }}>Settings</div>
+import { useEffect, useState } from 'react'
+import type { UpdateState } from '@shared/ipc'
+import { Button, CardForm, CardSection, ChoicePicker, IconButton, Switch, ValueRow } from '../../components/Controls'
+import { Icon } from '../../components/Icon'
+import { useDismiss } from '../../components/Overlay'
+import { font } from '../../lib/fonts'
+import { DEFAULT_TEXT_SIZE, prefs, TEXT_SIZES, usePref } from '../../lib/prefs'
+import { useAccount } from '../../store/account'
+import { StoreContext, useApp } from '../../store/context'
+import { ComputerBadge } from '../ComputerBadge'
+import { ComputersView } from './ComputersView'
+import { isMac, thisMac } from './parts'
+import { UsageLimits } from './UsageLimits'
+import { VoiceChatSettingsView } from './VoiceChatSettingsView'
+
+/** The chat window's Settings: pages in a sidebar, ChatGPT's settings layout. */
+export type SettingsPage = 'general' | 'computers' | 'voice' | 'usage' | 'updates'
+
+const thisComputer = isMac ? 'This Mac' : 'This computer'
+const pages: { id: SettingsPage; title: string; label?: string; group: string; icon: string }[] = [
+ { id: 'general', title: 'General', group: 'Personal', icon: 'gearshape' },
+ { id: 'computers', title: 'Computers & devices', label: 'Computers', group: thisComputer, icon: 'desktopcomputer' },
+ { id: 'voice', title: 'Voice chat', group: 'Personal', icon: 'waveform' },
+ { id: 'usage', title: 'Usage', group: 'Personal', icon: 'gauge.with.dots.needle.33percent' },
+ { id: 'updates', title: 'Updates', group: thisComputer, icon: 'arrow.down.circle' },
+]
+
+export function SettingsView({ page: initial }: { page: SettingsPage }) {
+ // Kept here: the sheet's content doesn't follow the window's state once shown.
+ const [page, setPage] = useState(initial)
+ const app = useApp()
+ const dismiss = useDismiss()
+ const current = pages.find((p) => p.id === page)!
+ return (
+ <div className="settings">
+ <nav className="settings-sidebar">
+ {['Personal', thisComputer].map((group) => (
+ <div key={group} style={{ display: 'contents' }}>
+ <div className="settings-group" style={{ paddingTop: group === 'Personal' ? 0 : 16 }}>{group}</div>
+ {pages.filter((p) => p.group === group).map((item) => (
+ <button key={item.id} className={`settings-item ${item.id === page ? 'selected' : ''}`} aria-selected={item.id === page} onClick={() => setPage(item.id)}>
+ <span style={{ width: 18, display: 'flex', justifyContent: 'center', color: 'var(--secondary)' }}>
+ <Icon name={item.icon} size={13} />
+ </span>
+ <span style={{ flex: 1, minWidth: 0, whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' }}>{item.label ?? item.title}</span>
+ {item.id === 'computers' && app.approvals.length > 0 ? (
+ <span style={{ ...font(11, 'semibold'), color: 'var(--secondary)' }}>{app.approvals.length}</span>
+ ) : null}
+ </button>
+ ))}
+ </div>
+ ))}
+ </nav>
+ <div className="settings-divider" />
+ <div className="settings-main">
+ <div style={{ ...font(24, 'semibold'), color: 'var(--text)', padding: '30px 32px 8px' }}>{current.title}</div>
+ <div key={page} className="settings-page">
+ <PageContent page={page} />
+ </div>
+ </div>
+ <span className="settings-close">
+ <IconButton title="Close" icon="xmark" onClick={dismiss} />
+ </span>
+ </div>
+ )
+}
+
+function PageContent({ page }: { page: SettingsPage }) {
+ const app = useApp()
+ switch (page) {
+ case 'general':
+ return <GeneralSettings />
+ case 'computers':
+ return <ComputersView />
+ case 'voice':
+ return app.local ? (
+ <StoreContext.Provider value={app.local}>
+ <VoiceChatSettingsView showsHeader={false} />
+ </StoreContext.Provider>
+ ) : (
+ <CardForm>
+ <span style={{ color: 'var(--secondary)' }}>Voice keys are kept by {thisMac}'s host, which isn't running.</span>
+ </CardForm>
+ )
+ case 'usage':
+ return (
+ <CardForm>
+ <UsageLimitsList />
+ </CardForm>
+ )
+ case 'updates':
+ return <UpdateSettings />
+ }
+}
+
+/** Account, window and app preferences. */
+function GeneralSettings() {
+ const account = useAccount()
+ const [textSize, setTextSize] = usePref(prefs.textSize)
+ const [usageIconStyle, setUsageIconStyle] = usePref(prefs.usageIconStyle)
+ const [compact, setCompact] = usePref(prefs.sidebarCompact)
+ const [launchAtLogin, setLaunchAtLogin] = useState(false)
+ useEffect(() => void window.codync.app.launchAtLogin().then(setLaunchAtLogin), [])
+
+ return (
+ <CardForm>
+ <CardSection title="Account" footer={account.errorMessage}>
+ <div className="settings-row">
+ <Icon name="person.crop.circle" size={26} color="var(--secondary)" />
+ <div className="settings-stack" style={{ flex: 1 }}>
+ <span style={{ whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' }}>
+ {account.isSignedIn ? (account.user?.email ?? 'Signed in') : 'Not signed in'}
+ </span>
+ <span style={{ ...font('caption'), color: 'var(--secondary)' }}>
+ {account.isSignedIn ? 'Reach your computers from anywhere' : 'Sign in to reach your computers from anywhere'}
+ </span>
+ </div>
+ {account.isSignedIn ? (
+ <Button kind="secondary" onClick={() => void account.signOut()} disabled={account.isBusy}>
+ {account.isBusy ? 'Please wait…' : 'Sign out'}
+ </Button>
+ ) : (
+ <>
+ <Button kind="secondary" onClick={() => void account.signIn('apple')} disabled={account.isBusy}>
+ Apple
+ </Button>
+ <Button kind="secondary" onClick={() => void account.signIn('google')} disabled={account.isBusy}>
+ Google
+ </Button>
+ </>
+ )}
+ </div>
+ </CardSection>
+ <CardSection title="Appearance">
+ <ValueRow label="Text size">
+ <ChoicePicker
+ selection={textSize}
+ options={TEXT_SIZES.map((s) => ({ id: s, label: s === DEFAULT_TEXT_SIZE ? `${s} pt (Default)` : `${s} pt` }))}
+ onChange={setTextSize}
+ />
+ </ValueRow>
+ <ValueRow label="Usage icons">
+ <ChoicePicker selection={usageIconStyle} options={[{ id: 'character', label: 'Character' }, { id: 'original', label: 'Original' }]} onChange={setUsageIconStyle} />
+ </ValueRow>
+ <Switch on={compact} onChange={setCompact}>
+ Compact sidebar
+ </Switch>
+ </CardSection>
+ <CardSection title="System">
+ <Switch on={launchAtLogin} onChange={(on) => void window.codync.app.setLaunchAtLogin(on).then(setLaunchAtLogin)}>
+ Open at login
+ </Switch>
+ </CardSection>
+ </CardForm>
+ )
+}
+
+/** Every computer's usage limits, grouped by computer when there's more than one. */
+function UsageLimitsList() {
+ const app = useApp()
+ const stores = app.computers.flatMap((c) => app.store(c.id) ?? [])
+ const withUsage = stores.filter((s) => s.usage.providers.length > 0)
+ if (withUsage.length === 0) return <span style={{ color: 'var(--secondary)' }}>No usage information yet.</span>
+ return (
+ <>
+ {withUsage.flatMap((store) => [
+ stores.length > 1 ? (
+ <div key={`${store.computer.id}-label`} style={{ ...font('headline'), display: 'flex', alignItems: 'center', gap: 6 }}>
+ <ComputerBadge computer={store.computer} size={16} />
+ {store.hostName}
+ </div>
+ ) : null,
+ <UsageLimits key={store.computer.id} usage={store.usage} />,
+ ])}
+ </>
+ )
+}
+
+/** The app's update check and preferences, the same as the menu bar's Updates menu. */
+function UpdateSettings() {
+ const [updates, setUpdates] = useState<UpdateState | null>(null)
+ useEffect(() => {
+ void window.codync.updates.state().then(setUpdates)
+ return window.codync.updates.onChange(setUpdates)
+ }, [])
+ if (!updates) return null
+ const footer = !updates.supported
+ ? 'Updates are available in release builds.'
+ : updates.waitingForApp
+ ? `Waiting for iPhone app ${updates.waitingForApp} to pass App Store review.`
+ : null
+ const lastChecked = updates.lastCheck
+ ? `Last checked ${new Date(updates.lastCheck).toLocaleString(undefined, { dateStyle: 'medium', timeStyle: 'short' })}`
+ : null
+ return (
+ <CardForm>
+ <CardSection title="Codync" footer={footer}>
+ {[
+ <ValueRow key="version" label="Version" detail={lastChecked}>
+ <span style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
+ <span>{window.codync.appVersion}</span>
+ <Button kind="secondary" onClick={() => window.codync.updates.check()} disabled={!updates.canCheck && !updates.staged}>
+ {updates.availableVersion ? `Update to ${updates.availableVersion}` : 'Check for updates'}
+ </Button>
+ </span>
+ </ValueRow>,
+ <Switch key="check" on={updates.autoCheck} onChange={(on) => window.codync.updates.setAutoCheck(on)} disabled={!updates.supported}>
+ Automatically check for updates
+ </Switch>,
+ <Switch key="download" on={updates.autoDownload} onChange={(on) => window.codync.updates.setAutoDownload(on)} disabled={!updates.supported}>
+ Automatically download and install
+ </Switch>,
+ ...(updates.error
+ ? [
+ <div key="error" className="settings-row settings-appear">
+ <span style={{ flex: 1, color: 'var(--warning)' }}>{updates.error}</span>
+ <Button kind="secondary" onClick={() => window.codync.updates.check()} disabled={updates.checking}>
+ Retry
+ </Button>
+ </div>,
+ ]
+ : []),
+ ]}
+ </CardSection>
+ </CardForm>
+ )
}
diff --git a/apps/desktop/src/renderer/views/settings/UsageLimits.tsx b/apps/desktop/src/renderer/views/settings/UsageLimits.tsx
new file mode 100644
index 00000000..2e49c4fd
--- /dev/null
+++ b/apps/desktop/src/renderer/views/settings/UsageLimits.tsx
@@ -0,0 +1,40 @@
+import type { Usage } from '@shared/models'
+import { font } from '../../lib/fonts'
+import { prefs, usePref } from '../../lib/prefs'
+import { ProviderMascot } from '../usage/ProviderMascot'
+import { providerTintCSS, resetDescription, windowTitle } from '../usage/usage-model'
+
+/** Every limit as a bar under its provider, the way the menu bar lists them. */
+export function UsageLimits({ usage }: { usage: Usage }) {
+ const [style] = usePref(prefs.usageIconStyle)
+ return (
+ <div style={{ display: 'flex', flexDirection: 'column', gap: 18 }}>
+ {usage.providers.map((provider) => (
+ <div key={provider.id} style={{ display: 'flex', flexDirection: 'column', gap: 10 }}>
+ <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
+ <ProviderMascot provider={provider} size={18} style={style} />
+ <span style={{ ...font(13, 'semibold'), color: 'var(--text)' }}>{provider.name}</span>
+ </div>
+ {provider.windows.map((w) => (
+ <div key={w.id} style={{ ...font(12), display: 'flex', alignItems: 'center', gap: 10, whiteSpace: 'nowrap' }}>
+ <span style={{ width: 64, flex: 'none', overflow: 'hidden', textOverflow: 'ellipsis', color: 'var(--text)' }}>{windowTitle(w)}</span>
+ <UsageBar percent={w.percent} tint={providerTintCSS(provider.id)} />
+ <span style={{ width: 36, flex: 'none', textAlign: 'right', fontVariantNumeric: 'tabular-nums', color: 'var(--text)' }}>{Math.round(w.percent)}%</span>
+ <span style={{ width: 130, flex: 'none', textAlign: 'right', overflow: 'hidden', textOverflow: 'ellipsis', color: 'var(--secondary)' }}>{resetDescription(w) ?? ''}</span>
+ </div>
+ ))}
+ </div>
+ ))}
+ </div>
+ )
+}
+
+/** A capsule filled to `percent`: the provider's tint below 70%, then amber, red from 90%. */
+function UsageBar({ percent, tint }: { percent: number; tint: string }) {
+ const fill = percent >= 90 ? 'var(--danger)' : percent >= 70 ? 'var(--warning)' : tint
+ return (
+ <span style={{ flex: 1, minWidth: 0, height: 6, borderRadius: 3, background: 'var(--bubble-agent)', overflow: 'hidden' }}>
+ <span style={{ display: 'block', height: '100%', borderRadius: 3, background: fill, width: `${Math.min(1, Math.max(0.02, percent / 100)) * 100}%`, transition: 'width var(--layout)' }} />
+ </span>
+ )
+}
diff --git a/apps/desktop/src/renderer/views/settings/VoiceChatSettingsView.tsx b/apps/desktop/src/renderer/views/settings/VoiceChatSettingsView.tsx
new file mode 100644
index 00000000..98fe7cba
--- /dev/null
+++ b/apps/desktop/src/renderer/views/settings/VoiceChatSettingsView.tsx
@@ -0,0 +1,290 @@
+import { onDeviceUnavailableReason } from '../call/voice'
+import { useEffect, useRef, useState } from 'react'
+import type { HostClient } from '../../client/host-client'
+import { Button, CardForm, CardSection, ChoicePicker, IconButton, Spinner, ValueRow } from '../../components/Controls'
+import { Icon } from '../../components/Icon'
+import { ModalHeader } from '../../components/Overlay'
+import { font } from '../../lib/fonts'
+import { usePref } from '../../lib/prefs'
+import { useStore } from '../../store/context'
+import { errorText, liveClient, Reveal } from './parts'
+import * as voice from './voice-settings'
+import type { VoiceModels, VoiceProvider } from './voice-settings'
+
+/**
+ * Voice chat settings: which voice carries calls (on-device speech, or OpenAI / Gemini on the
+ * user's own key kept by the computer), the mode, models and voice. The computer is the store
+ * in `StoreContext`. `showsHeader` is false inside Settings, which titles the page itself.
+ */
+export function VoiceChatSettingsView({ showsHeader = true }: { showsHeader?: boolean }) {
+ const [engine, setEngine] = usePref(voice.engine)
+ const [rate, setRate] = usePref(voice.rate)
+ const provider = voice.isVoiceProvider(engine) ? engine : null
+ const [mode, setMode] = usePref(voice.mode(provider ?? 'openai'))
+ const footer = provider
+ ? `Your own ${voice.voiceInfo[provider].name} key, billed by ${voice.voiceInfo[provider].name}. Applies from the next call.`
+ : window.codync.speech.available
+ ? 'On-device speech, on this computer. Applies from the next call.'
+ : onDeviceUnavailableReason()
+ return (
+ <div style={{ display: 'flex', flexDirection: 'column', background: 'var(--background)', width: showsHeader ? 440 : undefined, height: showsHeader ? 600 : '100%' }}>
+ {showsHeader ? <ModalHeader title="Voice chat" /> : null}
+ <div style={{ flex: 1, minHeight: 0 }}>
+ <CardForm>
+ <CardSection title="Voice" footer={footer}>
+ {[
+ <ValueRow key="engine" label="Engine">
+ <ChoicePicker
+ selection={engine}
+ options={[...(window.codync.speech.available || engine === 'device' ? [{ id: 'device', label: 'On device' }] : []), ...voice.VOICE_PROVIDERS.map((p) => ({ id: p, label: voice.voiceInfo[p].name }))]}
+ onChange={setEngine}
+ />
+ </ValueRow>,
+ ...(provider
+ ? [
+ <ValueRow key="mode" label="Mode" detail={mode === 'speech' ? 'Sent when you pause, replies read aloud.' : 'A live conversation. Talk over replies.'}>
+ <ChoicePicker selection={mode} options={[{ id: 'realtime', label: 'Realtime' }, { id: 'speech', label: 'Speech' }]} onChange={setMode} />
+ </ValueRow>,
+ ...(mode === 'speech' ? [<PauseRow key="pause" />] : []),
+ ]
+ : [
+ <ValueRow key="rate" label="Reading speed">
+ <ChoicePicker
+ selection={rate}
+ options={[{ id: 0.42, label: 'Slower' }, { id: voice.DEFAULT_RATE, label: 'Normal' }, { id: 0.56, label: 'Faster' }]}
+ onChange={setRate}
+ />
+ </ValueRow>,
+ <PauseRow key="pause" />,
+ ]),
+ ]}
+ </CardSection>
+ {provider ? <ProviderVoiceSettings key={provider} provider={provider} /> : null}
+ </CardForm>
+ </div>
+ </div>
+ )
+}
+
+/** How long a silence ends what you say, before it's sent. */
+function PauseRow() {
+ const [pause, setPause] = usePref(voice.pause)
+ return (
+ <div className="settings-appear">
+ <ValueRow label="Send after a pause of" detail="Longer leaves time to think mid-sentence.">
+ <ChoicePicker selection={pause} options={[{ id: 1, label: '1 s' }, { id: 1.5, label: '1.5 s' }, { id: 2.5, label: '2.5 s' }]} onChange={setPause} />
+ </ValueRow>
+ </div>
+ )
+}
+
+/** One provider: the key the computer keeps, models, voice and this month's minutes. */
+function ProviderVoiceSettings({ provider }: { provider: VoiceProvider }) {
+ const store = useStore()
+ const info = voice.voiceInfo[provider]
+ const [mode] = usePref(voice.mode(provider))
+ /** null while the computer hasn't answered yet. */
+ const [saved, setSaved] = useState<boolean | null>(null)
+ const [replacing, setReplacing] = useState(false)
+ const [key, setKey] = useState('')
+ const [busy, setBusy] = useState(false)
+ const [result, setResult] = useState<{ ok: boolean; text: string } | null>(null)
+ const [chosenVoice, setChosenVoice] = useState(() => voice.chosenVoice(provider))
+ const [model, setModel] = useState(() => voice.chosenModel(provider, 'realtime'))
+ const [transcribeModel, setTranscribeModel] = useState(() => voice.chosenModel(provider, 'transcribe'))
+ const [speechModel, setSpeechModel] = useState(() => voice.chosenModel(provider, 'speech'))
+ const [models, setModels] = useState<VoiceModels>(() => voice.modelsCache(provider).get() ?? { models: [], transcribe: [], speech: [] })
+ const busyRef = useRef(false)
+ const speechMode = mode === 'speech'
+ const hasKey = saved === true
+ const notConnected = { ok: false, text: `Not connected to ${store.hostName}.` }
+
+ const loadModels = async (client: HostClient) => {
+ let list: VoiceModels
+ try {
+ list = await voice.voiceModels(client, provider)
+ } catch {
+ return
+ }
+ setModels(list)
+ // A newer default moves every choice the user hasn't made themselves.
+ voice.rememberDefaults(provider, list.defaults)
+ setModel(voice.chosenModel(provider, 'realtime'))
+ setTranscribeModel(voice.chosenModel(provider, 'transcribe'))
+ setSpeechModel(voice.chosenModel(provider, 'speech'))
+ voice.modelsCache(provider).set(list)
+ }
+
+ useEffect(() => {
+ let cancelled = false
+ void (async () => {
+ const client = liveClient(store)
+ if (!client) {
+ setSaved(false)
+ return setResult(notConnected)
+ }
+ let configured: boolean
+ try {
+ const status = await voice.voiceStatus(client)
+ voice.rememberStatus(status)
+ configured = voice.configured(status, provider)
+ } catch (e) {
+ if (cancelled) return
+ setSaved(false)
+ return setResult({ ok: false, text: errorText(e) })
+ }
+ if (cancelled) return
+ setSaved(configured)
+ if (configured) await loadModels(client)
+ })()
+ return () => void (cancelled = true)
+ }, [provider])
+
+ const pick = (kind: voice.ModelKind, set: (m: string) => void) => (m: string) => {
+ set(m)
+ voice.saveModel(provider, kind, m)
+ }
+
+ const run = (work: (client: HostClient) => Promise<void>) => {
+ if (busyRef.current) return
+ const client = liveClient(store)
+ if (!client) return setResult(notConnected)
+ busyRef.current = true
+ setBusy(true)
+ setResult(null)
+ void work(client)
+ .catch((e: unknown) => setResult({ ok: false, text: errorText(e) }))
+ .finally(() => {
+ busyRef.current = false
+ setBusy(false)
+ })
+ }
+
+ /** A pasted key is checked with the provider by the computer before it's kept. With a key saved, Test starts a call credential with the chosen model and voice. */
+ const test = () => {
+ const typed = key.trim()
+ if (!typed && !hasKey) return
+ run(async (client) => {
+ if (!typed || (!replacing && hasKey)) {
+ if (speechMode) await voice.voiceSpeak(client, provider, speechModel, chosenVoice, 'OK')
+ else await voice.voiceSession(client, provider, model, chosenVoice)
+ setResult({ ok: true, text: 'Key works' })
+ } else {
+ setSaved(voice.configured(await voice.setVoiceKey(client, provider, typed), provider))
+ setKey('')
+ setReplacing(false)
+ setResult({ ok: true, text: `Key works and is saved on ${store.hostName}` })
+ }
+ await loadModels(client)
+ })
+ }
+
+ const remove = () =>
+ run(async (client) => {
+ setSaved(voice.configured(await voice.setVoiceKey(client, provider, ''), provider))
+ setResult(null)
+ })
+
+ const picker = (selection: string, list: string[], onChange: (m: string) => void) => {
+ // The current choice stays listed before the provider's list arrives.
+ const options = list.includes(selection) ? list : [selection, ...list]
+ return <ChoicePicker selection={selection} options={options.map((m) => ({ id: m, label: m }))} onChange={onChange} />
+ }
+
+ const dimmed = { opacity: hasKey ? 1 : 0.55, pointerEvents: hasKey ? undefined : ('none' as const), transition: 'opacity var(--layout)' }
+ const keyArea = (
+ <div key="key" style={{ display: 'flex', flexDirection: 'column', gap: 8 }}>
+ {hasKey && !replacing ? (
+ // The key is on the computer: say so, and offer test / replace / remove.
+ <div className="settings-row">
+ <Icon name="key.fill" size={13} color="var(--secondary)" />
+ <div className="settings-stack" style={{ flex: 1 }}>
+ <span style={{ color: 'var(--text)', whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' }}>Key saved</span>
+ <span style={{ ...font('caption'), color: 'var(--tertiary)', whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' }}>Stays on {store.hostName}</span>
+ </div>
+ {busy ? (
+ <Spinner />
+ ) : (
+ <>
+ <IconButton title="Test key" icon="checkmark.seal" onClick={test} />
+ <IconButton title="Replace key" icon="pencil" onClick={() => { setReplacing(true); setResult(null) }} />
+ <IconButton title="Remove key" icon="trash" onClick={remove} />
+ </>
+ )}
+ </div>
+ ) : (
+ // Paste a key; the computer checks it with the provider before keeping it.
+ <div style={{ display: 'flex', flexDirection: 'column', gap: 10 }}>
+ <div className="settings-row" style={{ gap: 8 }}>
+ <input
+ type="password"
+ aria-label="API key"
+ placeholder={info.keyPrompt}
+ value={key}
+ spellCheck={false}
+ autoComplete="off"
+ style={{ flex: 1, minWidth: 0 }}
+ onChange={(e) => setKey(e.target.value)}
+ onKeyDown={(e) => e.key === 'Enter' && test()}
+ />
+ {busy ? (
+ <Spinner />
+ ) : (
+ <Button onClick={test} disabled={!key.trim()}>
+ Save
+ </Button>
+ )}
+ </div>
+ <div className="settings-row" style={font('footnote')}>
+ <button style={{ color: 'var(--text)', textDecoration: 'underline' }} onClick={() => window.codync.app.openExternal(info.keyPage)}>
+ Get a key from {info.name}
+ </button>
+ <span style={{ flex: 1 }} />
+ {replacing ? (
+ <button style={{ color: 'var(--secondary)' }} onClick={() => { setReplacing(false); setKey(''); setResult(null) }}>
+ Cancel
+ </button>
+ ) : null}
+ </div>
+ </div>
+ )}
+ <Reveal show={result !== null}>
+ {result ? (
+ <span style={{ ...font('footnote'), display: 'flex', alignItems: 'center', gap: 5, color: result.ok ? 'var(--secondary)' : 'var(--danger)' }}>
+ <Icon name={result.ok ? 'checkmark.circle.fill' : 'exclamationmark.circle.fill'} size={10} />
+ {result.text}
+ </span>
+ ) : null}
+ </Reveal>
+ </div>
+ )
+
+ return (
+ <CardSection title={info.name} footer={hasKey ? null : `Add a key to choose from ${info.name}'s models.`}>
+ {[
+ keyArea,
+ ...(speechMode
+ ? [
+ <div key="stt" style={dimmed}><ValueRow label="Speech to text">{picker(transcribeModel, models.transcribe, pick('transcribe', setTranscribeModel))}</ValueRow></div>,
+ <div key="tts" style={dimmed}><ValueRow label="Text to speech">{picker(speechModel, models.speech, pick('speech', setSpeechModel))}</ValueRow></div>,
+ ]
+ : [<div key="model" style={dimmed}><ValueRow label="Model">{picker(model, models.models, pick('realtime', setModel))}</ValueRow></div>]),
+ <div key="voice" style={dimmed}>
+ <ValueRow label="Voice">
+ <ChoicePicker
+ selection={chosenVoice}
+ options={info.voices.map((v) => ({ id: v, label: v }))}
+ onChange={(v) => {
+ setChosenVoice(v)
+ voice.saveVoice(provider, v)
+ }}
+ />
+ </ValueRow>
+ </div>,
+ <div key="minutes" style={dimmed}>
+ <ValueRow label="This month">≈ {voice.voiceMinutes(provider)} min</ValueRow>
+ </div>,
+ ]}
+ </CardSection>
+ )
+}
diff --git a/apps/desktop/src/renderer/views/settings/parts.tsx b/apps/desktop/src/renderer/views/settings/parts.tsx
new file mode 100644
index 00000000..440709f1
--- /dev/null
+++ b/apps/desktop/src/renderer/views/settings/parts.tsx
@@ -0,0 +1,68 @@
+import type { CSSProperties, ReactNode } from 'react'
+import { usePresence } from '../../components/Overlay'
+import { font } from '../../lib/fonts'
+import type { BotStore } from '../../store/bot-store'
+import './settings.css'
+
+export const isMac = window.codync.platform === 'darwin'
+/** "Mac" in the Mac app's copy; "computer" elsewhere. */
+export const thisMac = isMac ? 'this Mac' : 'this computer'
+
+/** Keeps `children` mounted while it fades and folds away. */
+export function Reveal({ show, children, style }: { show: boolean; children: ReactNode; style?: CSSProperties }) {
+ const { mounted, shown } = usePresence(show)
+ if (!mounted) return null
+ return (
+ <div className={`settings-reveal ${shown ? 'shown' : ''}`} style={style}>
+ <div>{children}</div>
+ </div>
+ )
+}
+
+/** A section's title row (the CardSection heading). */
+export function SectionHeader({ title, children }: { title: string; children?: ReactNode }) {
+ return (
+ <div className="settings-section-header">
+ <span role="heading" aria-level={3} style={{ ...font(13, 'semibold'), color: 'var(--text)' }}>{title}</span>
+ <span style={{ flex: 1 }} />
+ {children}
+ </div>
+ )
+}
+
+export const errorText = (e: unknown) => (e instanceof Error ? e.message : typeof e === 'object' && e && 'message' in e ? String(e.message) : String(e))
+
+/** "3:27 PM", "Yesterday", "Monday" or "Sep 16" (kit's `RelativeTime.day`). */
+export function relativeDay(date: Date, now = new Date()) {
+ const day = (d: Date) => new Date(d.getFullYear(), d.getMonth(), d.getDate()).getTime()
+ const days = Math.round((day(now) - day(date)) / 86_400_000)
+ if (days === 0) return date.toLocaleTimeString(undefined, { hour: 'numeric', minute: '2-digit' })
+ if (days === 1) return 'Yesterday'
+ if (now.getTime() - date.getTime() < 6 * 86_400_000) return date.toLocaleDateString(undefined, { weekday: 'long' })
+ return date.toLocaleDateString(undefined, { month: 'short', day: 'numeric' })
+}
+
+/** One short line for a computer's connection, used next to its name. */
+export function statusText(store: BotStore) {
+ const c = store.connection
+ switch (c.kind) {
+ case 'online':
+ return store.mismatch ? 'Needs update' : 'Online'
+ case 'connecting':
+ return 'Connecting…'
+ case 'computerOffline':
+ return c.lastSeen ? `Offline · seen ${relativeDay(new Date(c.lastSeen))}` : 'Offline'
+ case 'offline':
+ return "Can't reach"
+ case 'unauthorized':
+ return c.message
+ case 'unpaired':
+ return 'Not paired'
+ }
+}
+
+/** The live client, or null while the computer isn't connected (kit's `store.client`). */
+export const liveClient = (store: BotStore) => (store.connection.kind === 'online' ? store.client : null)
+
+/** "123 456" for a 6-digit code. */
+export const spacedCode = (code: string) => (code.length === 6 ? `${code.slice(0, 3)} ${code.slice(3)}` : code)
diff --git a/apps/desktop/src/renderer/views/settings/settings.css b/apps/desktop/src/renderer/views/settings/settings.css
new file mode 100644
index 00000000..34bef5e4
--- /dev/null
+++ b/apps/desktop/src/renderer/views/settings/settings.css
@@ -0,0 +1,129 @@
+/* Settings (ChatGPT's desktop settings): a sidebar of pages and B&W cards. */
+.settings {
+ display: flex;
+ height: 100%;
+ background: var(--background);
+ position: relative;
+}
+.settings-sidebar {
+ width: 200px;
+ flex: none;
+ display: flex;
+ flex-direction: column;
+ gap: 2px;
+ padding: 30px 10px 10px;
+}
+.settings-group {
+ font-size: calc(12px * var(--scale));
+ font-weight: 500;
+ color: var(--tertiary);
+ padding: 0 10px 4px;
+}
+.settings-item {
+ display: flex;
+ align-items: center;
+ gap: 10px;
+ height: 32px;
+ padding: 0 10px;
+ border-radius: 10px;
+ font-size: calc(13px * var(--scale));
+ color: var(--text);
+ transition: background-color var(--hover);
+}
+.settings-item.selected {
+ background: rgba(var(--text-rgb), 0.08);
+}
+.settings-divider {
+ width: 1px;
+ flex: none;
+ background: var(--border);
+}
+.settings-main {
+ flex: 1;
+ min-width: 0;
+ display: flex;
+ flex-direction: column;
+}
+.settings-page {
+ flex: 1;
+ min-height: 0;
+ padding: 0 18px;
+ animation: settings-fade var(--fade) both;
+}
+.settings-close {
+ position: absolute;
+ top: 14px;
+ right: 14px;
+}
+@keyframes settings-fade {
+ from {
+ opacity: 0;
+ }
+}
+
+/* One filled group, like a CardSection's. */
+.settings-card {
+ padding: 16px;
+ width: 100%;
+ background: var(--surface);
+ border-radius: 16px;
+ margin-bottom: 8px;
+ display: flex;
+ flex-direction: column;
+ gap: 8px;
+}
+.settings-section-header {
+ display: flex;
+ align-items: center;
+ min-height: 24px;
+ padding: 0 0 10px 2px;
+}
+.settings-row {
+ display: flex;
+ align-items: center;
+ gap: 10px;
+ width: 100%;
+}
+.settings-stack {
+ display: flex;
+ flex-direction: column;
+ gap: 1px;
+ min-width: 0;
+}
+.settings-field {
+ flex: 1;
+ min-width: 0;
+ text-align: right;
+}
+.settings-code {
+ font-family: var(--mono);
+ font-size: calc(10px * var(--scale));
+ padding: 6px;
+ flex: 1;
+ min-width: 0;
+ background: var(--code-background);
+ border-radius: 6px;
+}
+
+/* A row that appears fades in. */
+.settings-appear {
+ animation: settings-fade var(--layout) both;
+}
+
+/* Show/hide: fade with a short drop from the top (`.opacity.combined(with: .move(edge: .top))`). */
+.settings-reveal {
+ display: grid;
+ grid-template-rows: 0fr;
+ opacity: 0;
+ transform: translateY(-4px);
+ transition: grid-template-rows var(--layout), opacity var(--layout), transform var(--layout);
+}
+.settings-reveal.shown {
+ grid-template-rows: 1fr;
+ opacity: 1;
+ transform: none;
+}
+.settings-reveal > div {
+ min-height: 0;
+ overflow: hidden;
+}
diff --git a/apps/desktop/src/renderer/views/settings/ssh-model.ts b/apps/desktop/src/renderer/views/settings/ssh-model.ts
new file mode 100644
index 00000000..a972e46a
--- /dev/null
+++ b/apps/desktop/src/renderer/views/settings/ssh-model.ts
@@ -0,0 +1,78 @@
+import { useEffect } from 'react'
+import type { Computer } from '@shared/models'
+import type { SSHBridge, SSHState } from '@shared/ssh'
+import { LoopbackTransport } from '../../client/host-client'
+import { Observable, useModel } from '../../lib/observable'
+import type { AppModel } from '../../store/app-model'
+import { BotStore } from '../../store/bot-store'
+
+/** `window.codync.ssh` (preload/ssh.ts); missing in a build that doesn't wire it. */
+export const sshBridge = (): SSHBridge | null => window.codync.ssh ?? null
+
+/** The main process's SSH computers, mirrored for views. */
+class SSHModel extends Observable {
+ state: SSHState = { profiles: [], status: {}, attachments: [] }
+ private started = false
+
+ start() {
+ const bridge = sshBridge()
+ if (this.started || !bridge) return
+ this.started = true
+ bridge.onChange((s) => this.apply(s))
+ void bridge.state().then((s) => this.apply(s))
+ }
+
+ private apply(s: SSHState) {
+ this.state = s
+ this.changed()
+ }
+
+ status(id: string) {
+ return this.state.status[id] ?? { kind: 'idle' as const }
+ }
+
+ isSSH(computerId: string) {
+ return this.state.attachments.some((a) => a.computer.id === computerId)
+ }
+}
+
+export const ssh = new SSHModel()
+
+export function useSSH() {
+ useEffect(() => ssh.start(), [])
+ return useModel(ssh)
+}
+
+/**
+ * Attaches each connected SSH computer to the window's model as a loopback store, and detaches
+ * it when its tunnel ends (the Mac app's `ssh.onAttach` / `onDetach`). Call once, from ChatWindow.
+ */
+export function useSSHAttachments(app: AppModel) {
+ useEffect(() => {
+ ssh.start()
+ // computer id → the tunnel it's attached through
+ const attached = new Map<string, string>()
+ const sync = () => {
+ const live = new Map(ssh.state.attachments.map((a) => [a.computer.id, a]))
+ for (const [id, a] of live) {
+ const key = `${a.baseURL}|${a.token}`
+ if (attached.get(id) === key) continue
+ const cached = app.computers.find((c) => c.id === id)
+ const computer: Computer = { ...a.computer, urls: [], color: cached?.color ?? null, device: cached?.device ?? null }
+ app.attach(computer, new BotStore(computer, true, () => new LoopbackTransport(a.baseURL, a.token)))
+ attached.set(id, key)
+ }
+ for (const id of [...attached.keys()]) {
+ if (live.has(id)) continue
+ attached.delete(id)
+ app.detach(id)
+ }
+ }
+ sync()
+ const off = ssh.subscribe(sync)
+ return () => {
+ off()
+ for (const id of attached.keys()) app.detach(id)
+ }
+ }, [app])
+}
diff --git a/apps/desktop/src/renderer/views/settings/voice-settings.ts b/apps/desktop/src/renderer/views/settings/voice-settings.ts
new file mode 100644
index 00000000..c96bbf4f
--- /dev/null
+++ b/apps/desktop/src/renderer/views/settings/voice-settings.ts
@@ -0,0 +1,137 @@
+import type { HostClient } from '../../client/host-client'
+import { pref, type Pref } from '../../lib/prefs'
+
+// Realtime voice on the user's own provider keys (host/src/voice.rs, kit Voice.swift +
+// VoiceSettings). The keys stay on the computer; these are this device's choices, under the
+// same keys as the Swift apps.
+
+export type VoiceProvider = 'openai' | 'gemini'
+export const VOICE_PROVIDERS: VoiceProvider[] = ['openai', 'gemini']
+
+/** The provider's newest model for each voice mode, as the computer last found them. */
+export interface VoiceDefaults {
+ realtime: string
+ transcribe: string
+ speech: string
+}
+
+export interface VoiceStatus {
+ providers: { provider: VoiceProvider; configured: boolean; defaults?: VoiceDefaults | null }[]
+}
+
+export interface VoiceModels {
+ /** Realtime conversation models. */
+ models: string[]
+ transcribe: string[]
+ speech: string[]
+ defaults?: VoiceDefaults | null
+}
+
+export const voiceInfo: Record<VoiceProvider, {
+ name: string
+ keyPage: string
+ keyPrompt: string
+ /** Used until the user picks one, and until the computer has said what's newest. */
+ defaults: VoiceDefaults
+ voices: string[]
+}> = {
+ openai: {
+ name: 'OpenAI',
+ keyPage: 'https://platform.openai.com/api-keys',
+ keyPrompt: 'sk-…',
+ defaults: { realtime: 'gpt-realtime-2.1', transcribe: 'gpt-transcribe', speech: 'gpt-4o-mini-tts' },
+ voices: ['marin', 'cedar', 'alloy', 'ash', 'ballad', 'coral', 'echo', 'sage', 'shimmer', 'verse'],
+ },
+ gemini: {
+ name: 'Gemini',
+ keyPage: 'https://aistudio.google.com/apikey',
+ keyPrompt: 'AIza…',
+ defaults: { realtime: 'gemini-3.8-live', transcribe: 'gemini-3.8-flash', speech: 'gemini-3.8-flash-tts' },
+ voices: ['Kore', 'Puck', 'Charon', 'Fenrir', 'Aoede', 'Leda', 'Orus', 'Zephyr'],
+ },
+}
+
+export const isVoiceProvider = (s: string): s is VoiceProvider => s === 'openai' || s === 'gemini'
+export const configured = (s: VoiceStatus, p: VoiceProvider) => s.providers.some((x) => x.provider === p && x.configured)
+
+/** `device` (on-device speech) or a provider. */
+export const engine = pref('callEngine', 'device')
+/** On-device reading speed (AVSpeechUtterance's scale: 0.5 is normal). */
+export const DEFAULT_RATE = 0.5
+export const rate = pref('callRate', DEFAULT_RATE)
+/** Seconds of silence that end what you say. */
+export const pause = pref('callPause', 1.5)
+
+const cache = new Map<string, Pref<unknown>>()
+function keyed<T>(key: string, fallback: T): Pref<T> {
+ if (!cache.has(key)) cache.set(key, pref<T>(key, fallback) as Pref<unknown>)
+ return cache.get(key) as Pref<T>
+}
+
+/** `realtime` (a live conversation) or `speech` (speech to text, then the reply read aloud). */
+export const mode = (p: VoiceProvider) => keyed<string>(`callMode.${p}`, 'realtime')
+const voicePref = (p: VoiceProvider) => keyed<string | null>(`callVoice.${p}`, null)
+const defaultsPref = (p: VoiceProvider) => keyed<VoiceDefaults | null>(`callDefaults.${p}`, null)
+/** The provider's model list as last fetched with the user's key. */
+export const modelsCache = (p: VoiceProvider) => keyed<VoiceModels | null>(`callModels.${p}`, null)
+const modelPrefs = {
+ realtime: (p: VoiceProvider) => keyed<string | null>(`callModel.${p}`, null),
+ transcribe: (p: VoiceProvider) => keyed<string | null>(`callSpeechToText.${p}`, null),
+ speech: (p: VoiceProvider) => keyed<string | null>(`callSpeechModel.${p}`, null),
+}
+
+/** The newest models as the computer last reported them, or the app's own until it has. */
+export const voiceDefaults = (p: VoiceProvider) => defaultsPref(p).get() ?? voiceInfo[p].defaults
+
+export function rememberDefaults(p: VoiceProvider, d: VoiceDefaults | null | undefined) {
+ if (d) defaultsPref(p).set(d)
+}
+
+export function rememberStatus(s: VoiceStatus) {
+ for (const entry of s.providers) rememberDefaults(entry.provider, entry.defaults)
+}
+
+export type ModelKind = keyof VoiceDefaults
+
+export const chosenModel = (p: VoiceProvider, kind: ModelKind) => modelPrefs[kind](p).get() ?? voiceDefaults(p)[kind]
+
+/** Keeps a model the user picked; the default isn't stored, so a newer default reaches them. */
+export function saveModel(p: VoiceProvider, kind: ModelKind, model: string) {
+ modelPrefs[kind](p).set(!model || model === voiceDefaults(p)[kind] ? null : model)
+}
+
+export function chosenVoice(p: VoiceProvider) {
+ const v = voicePref(p).get()
+ return v && voiceInfo[p].voices.includes(v) ? v : voiceInfo[p].voices[0]!
+}
+
+export const saveVoice = (p: VoiceProvider, v: string) => voicePref(p).set(v)
+
+// Minutes of realtime voice used this month, counted on this device: an estimate, not a bill.
+const usageKey = (p: VoiceProvider, d: Date) => `callUsage.${p}.${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}`
+
+export function addVoiceUsage(seconds: number, p: VoiceProvider, at = new Date()) {
+ if (seconds <= 0) return
+ const u = keyed<number>(usageKey(p, at), 0)
+ u.set(u.get() + seconds)
+}
+
+export const voiceMinutes = (p: VoiceProvider, at = new Date()) => Math.floor((keyed<number>(usageKey(p, at), 0).get() + 59) / 60)
+
+// Host calls
+
+export const voiceStatus = (c: HostClient) => c.call<VoiceStatus>('voiceStatus')
+
+/** Checks the key with the provider before the computer keeps it; an empty key removes it. */
+export const setVoiceKey = (c: HostClient, p: VoiceProvider, key: string) => c.call<VoiceStatus>('setVoiceKey', { provider: p, key }, 30_000)
+
+/** A short-lived credential for one call's audio session. */
+export const voiceSession = async (c: HostClient, p: VoiceProvider, model: string, voice: string) =>
+ (await c.call<{ credential: string }>('voiceSession', { provider: p, model, voice }, 30_000)).credential
+
+/** The models the computer's key can use, per voice mode. */
+export const voiceModels = (c: HostClient, p: VoiceProvider) => c.call<VoiceModels>('voiceModels', { provider: p }, 30_000)
+
+/** Text to speech for a stretch of a reply: base64 audio and its format (`aac` or `wav`). */
+export const voiceSpeak = (c: HostClient, p: VoiceProvider, model: string, voice: string, text: string) =>
+ c.call<{ audio: string; format: string }>('voiceSpeak', { provider: p, model, voice, text }, 90_000)
diff --git a/apps/desktop/src/renderer/views/tray-summary.ts b/apps/desktop/src/renderer/views/tray-summary.ts
index e1a72ad3..5d3b4ad5 100644
--- a/apps/desktop/src/renderer/views/tray-summary.ts
+++ b/apps/desktop/src/renderer/views/tray-summary.ts
@@ -112,10 +112,10 @@ export function useTraySummary(app: AppModel) {
? {
enabled: screen.enabled,
subtitle: screenSubtitle(app),
- needsLoginItem: false,
+ needsLoginItem: app.screenNeedsApproval,
needsCapture: screen.enabled && screen.connected && !screen.capture,
needsInput: screen.enabled && screen.connected && !screen.input,
- error: null,
+ error: app.screenError,
}
: null,
version: local?.hello?.version ?? null,
@@ -133,6 +133,7 @@ function screenSubtitle(app: AppModel) {
const local = app.local
const screen = local?.screen
if (!local || !screen || !screen.enabled) return `Control this ${isMac ? 'Mac' : 'computer'} from your iPhone`
+ if (app.screenNeedsApproval) return 'Needs approval in System Settings'
if (!screen.connected) return 'Starting Codync Screen…'
if (!screen.capture || !screen.input) return 'Needs permission'
if (screen.viewers > 0) return screen.viewers === 1 ? 'Your iPhone is viewing' : `${screen.viewers} viewers`
diff --git a/apps/desktop/src/shared/ipc.ts b/apps/desktop/src/shared/ipc.ts
index d1da02ef..409eb98b 100644
--- a/apps/desktop/src/shared/ipc.ts
+++ b/apps/desktop/src/shared/ipc.ts
@@ -1,3 +1,5 @@
+import type { SSHBridge } from './ssh'
+
// The bridge between the main process (host lifecycle, loopback HTTP, tray, windows)
// and the renderer (stores and UI). Exposed as `window.codync` by the preload script.
@@ -101,6 +103,8 @@ export type SpeechEvent =
export interface CodyncBridge {
platform: 'darwin' | 'linux' | 'win32'
appVersion: string
+ /** The computer's name ("Kevin's MacBook Pro"). */
+ computerName: string
host: {
snapshot(): Promise<HostSnapshot>
onChange(cb: (s: HostSnapshot) => void): () => void
@@ -129,6 +133,11 @@ export interface CodyncBridge {
stop(): void
onEvent(cb: (e: SpeechEvent) => void): () => void
}
+ ssh: SSHBridge
+ /** The Codync cloud's `/v1` API with the signed-in session (and the device key for `signed`). */
+ cloud: {
+ request(method: string, path: string, body: unknown, signed: boolean): Promise<{ ok: true; value: unknown } | { ok: false; error: { status: number; code: string; message: string | null } }>
+ }
updates: {
state(): Promise<UpdateState>
onChange(cb: (s: UpdateState) => void): () => void
@@ -155,5 +164,8 @@ export interface CodyncBridge {
* (`codync://…`), like ASWebAuthenticationSession. Rejects when cancelled or timed out.
*/
authenticate(url: string, scheme: string): Promise<string>
+ /** macOS: registers or removes the Codync Screen agent (SMAppService). */
+ setScreenAgent(on: boolean): Promise<{ needsApproval: boolean }>
+ syncScreenAgent(enabled: boolean): Promise<{ needsApproval: boolean }>
}
}
diff --git a/apps/desktop/src/shared/ssh.ts b/apps/desktop/src/shared/ssh.ts
new file mode 100644
index 00000000..285de2d3
--- /dev/null
+++ b/apps/desktop/src/shared/ssh.ts
@@ -0,0 +1,68 @@
+// SSH computers (spec §11.4): profiles, tunnel status and the computers they attach.
+// The main process runs OpenSSH; the renderer shows the state and attaches a store per tunnel.
+
+/** A computer reached over SSH. Nothing secret: keys stay in ssh-agent or on disk. */
+export interface SSHProfile {
+ id: string
+ /** An `~/.ssh/config` alias or a hostname. */
+ host: string
+ port: number | null
+ user: string | null
+ identityFile: string | null
+ remotePort: number
+ /** Remembered after the first connection; a different computer behind the same address is refused. */
+ computerId: string | null
+ name: string
+}
+
+export type SSHStatus =
+ | { kind: 'idle' }
+ | { kind: 'connecting'; step: string }
+ /** First time: the user compares these fingerprints before anything connects. */
+ | { kind: 'confirmHostKey'; name: string; fingerprints: string[] }
+ | { kind: 'connected'; localPort: number }
+ /** A retry is scheduled. */
+ | { kind: 'retrying'; message: string }
+ /** Needs the user (settings, the remote side, or a security problem); nothing retries. */
+ | { kind: 'failed'; message: string }
+ | { kind: 'notInstalled' }
+
+/** A connected tunnel: the computer behind it, as a loopback host. */
+export interface SSHAttachment {
+ profileId: string
+ computer: { id: string; name: string; signKey: string; boxKey: string | null }
+ baseURL: string
+ token: string
+}
+
+export interface SSHState {
+ profiles: SSHProfile[]
+ status: Record<string, SSHStatus>
+ attachments: SSHAttachment[]
+}
+
+export const SSH_INSTALL_COMMAND = 'brew install leepokai/codync/codync-host && codync-host install'
+
+export const newSSHProfile = (): SSHProfile => ({
+ id: crypto.randomUUID(),
+ host: '',
+ port: null,
+ user: null,
+ identityFile: null,
+ remotePort: 19222,
+ computerId: null,
+ name: '',
+})
+
+export interface SSHBridge {
+ state(): Promise<SSHState>
+ onChange(cb: (s: SSHState) => void): () => void
+ /** Adds or replaces the profile; resolves with a problem to show, or null once saved. */
+ save(profile: SSHProfile): Promise<string | null>
+ remove(id: string): void
+ connect(id: string): void
+ disconnect(id: string): void
+ trustHostKey(id: string): void
+ /** The system file dialog; the chosen path, or null. */
+ chooseKey(): Promise<string | null>
+}
diff --git a/apps/desktop/tools/account-config.mjs b/apps/desktop/tools/account-config.mjs
new file mode 100644
index 00000000..84992e1d
--- /dev/null
+++ b/apps/desktop/tools/account-config.mjs
@@ -0,0 +1,16 @@
+// Writes resources/account-config.json from apps/shared/Config/<env>.plist (dev | main):
+// the Clerk publishable key and the cloud URL the build talks to. Usage: node tools/account-config.mjs dev
+import { readFileSync, writeFileSync } from 'node:fs'
+import { dirname, join } from 'node:path'
+import { fileURLToPath } from 'node:url'
+
+const env = process.argv[2] ?? 'dev'
+if (!['dev', 'main'].includes(env)) throw new Error(`unknown environment ${env}`)
+const root = join(dirname(fileURLToPath(import.meta.url)), '..')
+const plist = readFileSync(join(root, '../shared/Config', `${env}.plist`), 'utf8')
+const value = (key) => new RegExp(`<key>${key}</key>\\s*<string>([^<]*)</string>`).exec(plist)?.[1] ?? null
+writeFileSync(
+ join(root, 'resources/account-config.json'),
+ `${JSON.stringify({ clerkPublishableKey: value('clerkPublishableKey'), cloudURL: value('cloudURL') }, null, 2)}\n`,
+)
+console.log(`account config: ${env}`)