Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
68ecee84d1 | ||
|
|
d1ebc4489a | ||
|
|
a71efbe7de | ||
|
|
a7b8c0d963 |
No files matched your search
+60
-67
@@ -5,33 +5,12 @@ on:
|
|||||||
pull_request:
|
pull_request:
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
checks:
|
build:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
strategy:
|
strategy:
|
||||||
fail-fast: false
|
fail-fast: false
|
||||||
matrix:
|
matrix:
|
||||||
include:
|
runtime: [node, bun]
|
||||||
- runtime: node
|
|
||||||
task: lint
|
|
||||||
command: pnpm lint
|
|
||||||
- runtime: node
|
|
||||||
task: test
|
|
||||||
command: pnpm test
|
|
||||||
- runtime: node
|
|
||||||
task: build
|
|
||||||
command: pnpm build
|
|
||||||
- runtime: node
|
|
||||||
task: protocol
|
|
||||||
command: pnpm protocol:check
|
|
||||||
- runtime: bun
|
|
||||||
task: lint
|
|
||||||
command: bunx biome check src
|
|
||||||
- runtime: bun
|
|
||||||
task: test
|
|
||||||
command: bunx vitest run
|
|
||||||
- runtime: bun
|
|
||||||
task: build
|
|
||||||
command: bunx tsc -p tsconfig.json
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
@@ -98,42 +77,37 @@ jobs:
|
|||||||
pnpm -v
|
pnpm -v
|
||||||
pnpm install --ignore-scripts=false --config.engine-strict=false --config.enable-pre-post-scripts=true || pnpm install --ignore-scripts=false --config.engine-strict=false --config.enable-pre-post-scripts=true
|
pnpm install --ignore-scripts=false --config.engine-strict=false --config.enable-pre-post-scripts=true || pnpm install --ignore-scripts=false --config.engine-strict=false --config.enable-pre-post-scripts=true
|
||||||
|
|
||||||
- name: Run ${{ matrix.task }} (${{ matrix.runtime }})
|
- name: Lint (node)
|
||||||
run: ${{ matrix.command }}
|
if: matrix.runtime == 'node'
|
||||||
|
run: pnpm lint
|
||||||
|
|
||||||
|
- name: Test (node)
|
||||||
|
if: matrix.runtime == 'node'
|
||||||
|
run: pnpm test
|
||||||
|
|
||||||
|
- name: Build (node)
|
||||||
|
if: matrix.runtime == 'node'
|
||||||
|
run: pnpm build
|
||||||
|
|
||||||
|
- name: Protocol check (node)
|
||||||
|
if: matrix.runtime == 'node'
|
||||||
|
run: pnpm protocol:check
|
||||||
|
|
||||||
|
- name: Lint (bun)
|
||||||
|
if: matrix.runtime == 'bun'
|
||||||
|
run: bunx biome check src
|
||||||
|
|
||||||
|
- name: Test (bun)
|
||||||
|
if: matrix.runtime == 'bun'
|
||||||
|
run: bunx vitest run
|
||||||
|
|
||||||
|
- name: Build (bun)
|
||||||
|
if: matrix.runtime == 'bun'
|
||||||
|
run: bunx tsc -p tsconfig.json
|
||||||
|
|
||||||
macos-app:
|
macos-app:
|
||||||
if: github.event_name == 'pull_request'
|
if: github.event_name == 'pull_request'
|
||||||
runs-on: macos-latest
|
runs-on: macos-latest
|
||||||
strategy:
|
|
||||||
fail-fast: false
|
|
||||||
matrix:
|
|
||||||
include:
|
|
||||||
- task: lint
|
|
||||||
command: |
|
|
||||||
swiftlint --config .swiftlint.yml
|
|
||||||
swiftformat --lint apps/macos/Sources --config .swiftformat
|
|
||||||
- task: build
|
|
||||||
command: |
|
|
||||||
set -euo pipefail
|
|
||||||
for attempt in 1 2 3; do
|
|
||||||
if swift build --package-path apps/macos --configuration release; then
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
echo "swift build failed (attempt $attempt/3). Retrying…"
|
|
||||||
sleep $((attempt * 20))
|
|
||||||
done
|
|
||||||
exit 1
|
|
||||||
- task: test
|
|
||||||
command: |
|
|
||||||
set -euo pipefail
|
|
||||||
for attempt in 1 2 3; do
|
|
||||||
if swift test --package-path apps/macos --parallel --enable-code-coverage --show-codecov-path; then
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
echo "swift test failed (attempt $attempt/3). Retrying…"
|
|
||||||
sleep $((attempt * 20))
|
|
||||||
done
|
|
||||||
exit 1
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
@@ -168,8 +142,35 @@ jobs:
|
|||||||
xcodebuild -version
|
xcodebuild -version
|
||||||
swift --version
|
swift --version
|
||||||
|
|
||||||
- name: Run ${{ matrix.task }}
|
- name: SwiftLint
|
||||||
run: ${{ matrix.command }}
|
run: swiftlint --config .swiftlint.yml
|
||||||
|
|
||||||
|
- name: SwiftFormat (lint mode)
|
||||||
|
run: swiftformat --lint apps/macos/Sources --config .swiftformat
|
||||||
|
|
||||||
|
- name: Swift build (release)
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
for attempt in 1 2 3; do
|
||||||
|
if swift build --package-path apps/macos --configuration release; then
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
echo "swift build failed (attempt $attempt/3). Retrying…"
|
||||||
|
sleep $((attempt * 20))
|
||||||
|
done
|
||||||
|
exit 1
|
||||||
|
|
||||||
|
- name: Swift tests (coverage)
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
for attempt in 1 2 3; do
|
||||||
|
if swift test --package-path apps/macos --parallel --enable-code-coverage --show-codecov-path; then
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
echo "swift test failed (attempt $attempt/3). Retrying…"
|
||||||
|
sleep $((attempt * 20))
|
||||||
|
done
|
||||||
|
exit 1
|
||||||
ios:
|
ios:
|
||||||
if: false # ignore iOS in CI for now
|
if: false # ignore iOS in CI for now
|
||||||
runs-on: macos-latest
|
runs-on: macos-latest
|
||||||
@@ -345,14 +346,6 @@ jobs:
|
|||||||
|
|
||||||
android:
|
android:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
strategy:
|
|
||||||
fail-fast: false
|
|
||||||
matrix:
|
|
||||||
include:
|
|
||||||
- task: test
|
|
||||||
command: ./gradlew --no-daemon :app:testDebugUnitTest
|
|
||||||
- task: build
|
|
||||||
command: ./gradlew --no-daemon :app:assembleDebug
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
@@ -392,6 +385,6 @@ jobs:
|
|||||||
"platforms;android-36" \
|
"platforms;android-36" \
|
||||||
"build-tools;36.0.0"
|
"build-tools;36.0.0"
|
||||||
|
|
||||||
- name: Run Android ${{ matrix.task }}
|
- name: Android unit tests + debug build
|
||||||
working-directory: apps/android
|
working-directory: apps/android
|
||||||
run: ${{ matrix.command }}
|
run: ./gradlew --no-daemon :app:testDebugUnitTest :app:assembleDebug
|
||||||
@@ -5,33 +5,18 @@
|
|||||||
### Breaking
|
### Breaking
|
||||||
- Identifiers: rename bundle IDs and internal domains to `com.clawdis.*` (macOS: `com.clawdis.mac`, iOS: `com.clawdis.ios`, Android: `com.clawdis.android`) and update the gateway LaunchAgent label to `com.clawdis.gateway`.
|
- Identifiers: rename bundle IDs and internal domains to `com.clawdis.*` (macOS: `com.clawdis.mac`, iOS: `com.clawdis.ios`, Android: `com.clawdis.android`) and update the gateway LaunchAgent label to `com.clawdis.gateway`.
|
||||||
- Agent tools: drop the `clawdis_` prefix (`browser`, `canvas`, `nodes`, `cron`, `gateway`).
|
- Agent tools: drop the `clawdis_` prefix (`browser`, `canvas`, `nodes`, `cron`, `gateway`).
|
||||||
- Bash tool: remove `stdinMode: "pty"`/node-pty support; use the tmux skill for real TTYs.
|
|
||||||
- Sessions: primary session key is fixed to `main` (or `global` for global scope); `session.mainKey` is ignored.
|
|
||||||
|
|
||||||
### Features
|
### Features
|
||||||
- Gateway: support `gateway.port` + `CLAWDIS_GATEWAY_PORT` across CLI, TUI, and macOS app.
|
- Gateway: support `gateway.port` + `CLAWDIS_GATEWAY_PORT` across CLI, TUI, and macOS app.
|
||||||
- Gateway: add config hot reload with hybrid restart strategy (`gateway.reload`) and per-section reload handling.
|
|
||||||
- UI: centralize tool display metadata and show action/detail summaries across Web Chat, SwiftUI, Android, and the TUI.
|
- UI: centralize tool display metadata and show action/detail summaries across Web Chat, SwiftUI, Android, and the TUI.
|
||||||
- Control UI: support configurable base paths (`gateway.controlUi.basePath`, default unchanged) for hosting under URL prefixes.
|
- Control UI: support configurable base paths (`gateway.controlUi.basePath`, default unchanged) for hosting under URL prefixes.
|
||||||
- Onboarding: shared wizard engine powering CLI + macOS via gateway wizard RPC.
|
- Onboarding: shared wizard engine powering CLI + macOS via gateway wizard RPC.
|
||||||
- Config: expose schema + UI hints for generic config forms (Web UI + future clients).
|
- Config: expose schema + UI hints for generic config forms (Web UI + future clients).
|
||||||
- Skills: add blogwatcher skill for RSS/Atom monitoring — thanks @Hyaxia.
|
- Skills: add blogwatcher skill for RSS/Atom monitoring — thanks @Hyaxia.
|
||||||
- Discord: emit system events for reaction add/remove with per-guild reaction notifications (off|own|all|allowlist) (#140) — thanks @thewilloftheshadow.
|
- Discord: emit system events for reaction add/remove with per-guild reaction notifications (off|own|all|allowlist) (#140) — thanks @thewilloftheshadow.
|
||||||
- Agent: add optional per-session Docker sandbox for tool execution (`agent.sandbox`) with allow/deny policy and auto-pruning.
|
|
||||||
- Agent: add sandboxed Chromium browser (CDP + optional noVNC observer) for sandboxed sessions.
|
|
||||||
- Nodes: add `location.get` with Always/Precise settings on macOS/iOS/Android plus CLI/tool support.
|
|
||||||
- Sessions: add agent‑to‑agent post step with `ANNOUNCE_SKIP` to suppress channel announcements.
|
|
||||||
|
|
||||||
### Fixes
|
### Fixes
|
||||||
- macOS: improve Swift 6 strict concurrency compatibility (#166) — thanks @Nachx639.
|
|
||||||
- CI: fix lint ordering after merge cleanup (#156) — thanks @steipete.
|
|
||||||
- CI: consolidate checks to avoid redundant installs (#144) — thanks @thewilloftheshadow.
|
|
||||||
- WhatsApp: support `gifPlayback` for MP4 GIF sends via CLI/gateway.
|
|
||||||
- Sessions: prevent `sessions_send` timeouts by running nested agent turns on a separate lane.
|
|
||||||
- Sessions: use per-send run IDs for gateway agent calls to avoid wait collisions.
|
|
||||||
- Auto-reply: drop final payloads when block streaming to avoid duplicate Discord sends.
|
- Auto-reply: drop final payloads when block streaming to avoid duplicate Discord sends.
|
||||||
- Auto-reply: fix typing TTL to 2 minutes and log TTL with s/m units.
|
|
||||||
- Bash tool: default auto-background delay to 10s.
|
|
||||||
- Telegram: chunk block-stream replies to avoid “message is too long” errors (#124) — thanks @mukhtharcm.
|
- Telegram: chunk block-stream replies to avoid “message is too long” errors (#124) — thanks @mukhtharcm.
|
||||||
- Block streaming: default to text_end and suppress duplicate block sends while in-flight.
|
- Block streaming: default to text_end and suppress duplicate block sends while in-flight.
|
||||||
- Block streaming: avoid duplicate block chunks when providers repeat full content on text_end.
|
- Block streaming: avoid duplicate block chunks when providers repeat full content on text_end.
|
||||||
@@ -39,24 +24,19 @@
|
|||||||
- Gmail hooks: resolve gcloud Python to a real executable when PATH uses mise shims — thanks @joargp.
|
- Gmail hooks: resolve gcloud Python to a real executable when PATH uses mise shims — thanks @joargp.
|
||||||
- Control UI: generate UUIDs when `crypto.randomUUID()` is unavailable over HTTP — thanks @ratulsarna.
|
- Control UI: generate UUIDs when `crypto.randomUUID()` is unavailable over HTTP — thanks @ratulsarna.
|
||||||
- Control UI: stream live tool output cards in Chat (agent events include sessionKey).
|
- Control UI: stream live tool output cards in Chat (agent events include sessionKey).
|
||||||
- Chat UI: render assistant `<think>`/`<final>` markup as italic thinking text in history + streaming instead of showing raw tags.
|
|
||||||
- Agent: add soft block-stream chunking (800–1200 chars default) with paragraph/newline preference.
|
- Agent: add soft block-stream chunking (800–1200 chars default) with paragraph/newline preference.
|
||||||
- Agent: route embedded run lifecycle logs through subsystem console formatting and reduce log noise.
|
|
||||||
- Agent tools: scope the Discord tool to Discord surface runs.
|
- Agent tools: scope the Discord tool to Discord surface runs.
|
||||||
- Agent tools: format verbose tool summaries without brackets, with unique emojis and `tool: detail` style.
|
- Agent tools: format verbose tool summaries without brackets, with unique emojis and `tool: detail` style.
|
||||||
- Agent tools: emit verbose tool summaries at tool start (no debounce).
|
- Agent tools: emit verbose tool summaries at tool start (no debounce).
|
||||||
- Gateway: split server helpers/tests into hooks/session-utils/ws-log/net modules for better isolation; add unit coverage for hooks/session utils/ws log.
|
- Gateway: split server helpers/tests into hooks/session-utils/ws-log/net modules for better isolation; add unit coverage for hooks/session utils/ws log.
|
||||||
- Gateway: extract WS method handling + HTTP/provider/constant helpers to shrink server wiring and improve testability.
|
- Gateway: extract WS method handling + HTTP/provider/constant helpers to shrink server wiring and improve testability.
|
||||||
- Gateway: prevent deleting the main session and abort active runs before deleting other sessions.
|
|
||||||
- Onboarding: fix Control UI basePath usage when showing/opening gateway URLs.
|
- Onboarding: fix Control UI basePath usage when showing/opening gateway URLs.
|
||||||
- Onboarding: clarify provider requirements (WhatsApp/Signal phone numbers, iMessage Apple ID guidance) in the provider picker.
|
|
||||||
- macOS Connections: move to sidebar + detail layout with structured sections and header actions.
|
- macOS Connections: move to sidebar + detail layout with structured sections and header actions.
|
||||||
- macOS onboarding: increase window height so the permissions page fits without scrolling.
|
- macOS onboarding: increase window height so the permissions page fits without scrolling.
|
||||||
- Thinking: default to low for reasoning-capable models when no /think or config default is set.
|
- Thinking: default to low for reasoning-capable models when no /think or config default is set.
|
||||||
- Logging: decouple file log levels from console verbosity; verbose-only details are captured when `logging.level` is debug/trace.
|
- Logging: decouple file log levels from console verbosity; verbose-only details are captured when `logging.level` is debug/trace.
|
||||||
- Build: fix regex literal in tool-meta path detection (watch build error).
|
- Build: fix regex literal in tool-meta path detection (watch build error).
|
||||||
- Build: require AVX2 Bun for x86_64 relay packaging (reject baseline builds).
|
- Build: require AVX2 Bun for x86_64 relay packaging (reject baseline builds).
|
||||||
- Build: drop stale ClawdisCLI product from macOS build-and-run script.
|
|
||||||
- Auto-reply: add run-level telemetry + typing TTL guardrails to diagnose stuck replies.
|
- Auto-reply: add run-level telemetry + typing TTL guardrails to diagnose stuck replies.
|
||||||
- WhatsApp: honor per-group mention gating overrides when group ids are stored as session keys.
|
- WhatsApp: honor per-group mention gating overrides when group ids are stored as session keys.
|
||||||
- Dependencies: bump pi-mono packages to 0.32.3.
|
- Dependencies: bump pi-mono packages to 0.32.3.
|
||||||
@@ -65,15 +45,10 @@
|
|||||||
- Skills: add Sheets/Docs examples to gog skill (#128) — thanks @mbelinky.
|
- Skills: add Sheets/Docs examples to gog skill (#128) — thanks @mbelinky.
|
||||||
- Skills: clarify bear-notes token + callback usage (#120) — thanks @tylerwince.
|
- Skills: clarify bear-notes token + callback usage (#120) — thanks @tylerwince.
|
||||||
- Skills: document Discord `sendMessage` media attachments and `to` format clarification.
|
- Skills: document Discord `sendMessage` media attachments and `to` format clarification.
|
||||||
- Skills: expand peekaboo skill examples + common parameters.
|
|
||||||
- Skills: add tmux skill + interactive coding guidance in coding-agent.
|
- Skills: add tmux skill + interactive coding guidance in coding-agent.
|
||||||
- Gateway: document port configuration + multi-instance isolation.
|
- Gateway: document port configuration + multi-instance isolation.
|
||||||
- Gateway: document config hot reload + reload matrix.
|
|
||||||
- Onboarding/Config: add protocol notes for wizard + schema RPC.
|
- Onboarding/Config: add protocol notes for wizard + schema RPC.
|
||||||
- Queue: clarify steer-backlog behavior with inline commands and update examples for streaming surfaces.
|
- Queue: clarify steer-backlog behavior with inline commands and update examples for streaming surfaces.
|
||||||
- Sandbox: document per-session agent sandbox setup, browser image, and Docker build.
|
|
||||||
- macOS: clarify menu bar uses sessionKey from agent events.
|
|
||||||
- Sessions: document agent-to-agent post step and `ANNOUNCE_SKIP`.
|
|
||||||
|
|
||||||
## 2.0.0-beta5 — 2026-01-03
|
## 2.0.0-beta5 — 2026-01-03
|
||||||
|
|
||||||
|
|||||||
@@ -1,16 +0,0 @@
|
|||||||
FROM debian:bookworm-slim
|
|
||||||
|
|
||||||
ENV DEBIAN_FRONTEND=noninteractive
|
|
||||||
|
|
||||||
RUN apt-get update \
|
|
||||||
&& apt-get install -y --no-install-recommends \
|
|
||||||
bash \
|
|
||||||
ca-certificates \
|
|
||||||
curl \
|
|
||||||
git \
|
|
||||||
jq \
|
|
||||||
python3 \
|
|
||||||
ripgrep \
|
|
||||||
&& rm -rf /var/lib/apt/lists/*
|
|
||||||
|
|
||||||
CMD ["sleep", "infinity"]
|
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
FROM debian:bookworm-slim
|
|
||||||
|
|
||||||
ENV DEBIAN_FRONTEND=noninteractive
|
|
||||||
|
|
||||||
RUN apt-get update \
|
|
||||||
&& apt-get install -y --no-install-recommends \
|
|
||||||
bash \
|
|
||||||
ca-certificates \
|
|
||||||
chromium \
|
|
||||||
curl \
|
|
||||||
fonts-liberation \
|
|
||||||
fonts-noto-color-emoji \
|
|
||||||
git \
|
|
||||||
jq \
|
|
||||||
novnc \
|
|
||||||
python3 \
|
|
||||||
websockify \
|
|
||||||
x11vnc \
|
|
||||||
xvfb \
|
|
||||||
&& rm -rf /var/lib/apt/lists/*
|
|
||||||
|
|
||||||
COPY scripts/sandbox-browser-entrypoint.sh /usr/local/bin/clawdis-sandbox-browser
|
|
||||||
RUN chmod +x /usr/local/bin/clawdis-sandbox-browser
|
|
||||||
|
|
||||||
EXPOSE 9222 5900 6080
|
|
||||||
|
|
||||||
CMD ["clawdis-sandbox-browser"]
|
|
||||||
+1
-1
Submodule Peekaboo updated: b69e4e8dc0...9db365b73c.
@@ -18,7 +18,7 @@
|
|||||||
**Clawdis** is a *personal AI assistant* you run on your own devices.
|
**Clawdis** is a *personal AI assistant* you run on your own devices.
|
||||||
It answers you on the surfaces you already use (WhatsApp, Telegram, Discord, iMessage, WebChat), can speak and listen on macOS/iOS, and can render a live Canvas you control. The Gateway is just the control plane — the product is the assistant.
|
It answers you on the surfaces you already use (WhatsApp, Telegram, Discord, iMessage, WebChat), can speak and listen on macOS/iOS, and can render a live Canvas you control. The Gateway is just the control plane — the product is the assistant.
|
||||||
|
|
||||||
If you want a personal, single-user assistant that feels local, fast, and always-on, this is it.
|
If you want a private, single-user assistant that feels local, fast, and always-on, this is it.
|
||||||
|
|
||||||
Website: https://clawd.me · Docs: [`docs/index.md`](docs/index.md) · FAQ: [`docs/faq.md`](docs/faq.md) · Wizard: [`docs/wizard.md`](docs/wizard.md) · Docker (optional): [`docs/docker.md`](docs/docker.md) · Discord: https://discord.gg/clawd
|
Website: https://clawd.me · Docs: [`docs/index.md`](docs/index.md) · FAQ: [`docs/faq.md`](docs/faq.md) · Wizard: [`docs/wizard.md`](docs/wizard.md) · Docker (optional): [`docs/docker.md`](docs/docker.md) · Discord: https://discord.gg/clawd
|
||||||
|
|
||||||
|
|||||||
@@ -9,9 +9,12 @@
|
|||||||
<uses-permission
|
<uses-permission
|
||||||
android:name="android.permission.NEARBY_WIFI_DEVICES"
|
android:name="android.permission.NEARBY_WIFI_DEVICES"
|
||||||
android:usesPermissionFlags="neverForLocation" />
|
android:usesPermissionFlags="neverForLocation" />
|
||||||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
|
<uses-permission
|
||||||
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
|
android:name="android.permission.ACCESS_FINE_LOCATION"
|
||||||
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
|
android:maxSdkVersion="32" />
|
||||||
|
<uses-permission
|
||||||
|
android:name="android.permission.ACCESS_COARSE_LOCATION"
|
||||||
|
android:maxSdkVersion="32" />
|
||||||
<uses-permission android:name="android.permission.CAMERA" />
|
<uses-permission android:name="android.permission.CAMERA" />
|
||||||
<uses-permission android:name="android.permission.RECORD_AUDIO" />
|
<uses-permission android:name="android.permission.RECORD_AUDIO" />
|
||||||
<uses-feature
|
<uses-feature
|
||||||
|
|||||||
@@ -1,15 +0,0 @@
|
|||||||
package com.clawdis.android
|
|
||||||
|
|
||||||
enum class LocationMode(val rawValue: String) {
|
|
||||||
Off("off"),
|
|
||||||
WhileUsing("whileUsing"),
|
|
||||||
Always("always"),
|
|
||||||
;
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
fun fromRawValue(raw: String?): LocationMode {
|
|
||||||
val normalized = raw?.trim()?.lowercase()
|
|
||||||
return entries.firstOrNull { it.rawValue.lowercase() == normalized } ?: Off
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -33,8 +33,6 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {
|
|||||||
val instanceId: StateFlow<String> = runtime.instanceId
|
val instanceId: StateFlow<String> = runtime.instanceId
|
||||||
val displayName: StateFlow<String> = runtime.displayName
|
val displayName: StateFlow<String> = runtime.displayName
|
||||||
val cameraEnabled: StateFlow<Boolean> = runtime.cameraEnabled
|
val cameraEnabled: StateFlow<Boolean> = runtime.cameraEnabled
|
||||||
val locationMode: StateFlow<LocationMode> = runtime.locationMode
|
|
||||||
val locationPreciseEnabled: StateFlow<Boolean> = runtime.locationPreciseEnabled
|
|
||||||
val preventSleep: StateFlow<Boolean> = runtime.preventSleep
|
val preventSleep: StateFlow<Boolean> = runtime.preventSleep
|
||||||
val wakeWords: StateFlow<List<String>> = runtime.wakeWords
|
val wakeWords: StateFlow<List<String>> = runtime.wakeWords
|
||||||
val voiceWakeMode: StateFlow<VoiceWakeMode> = runtime.voiceWakeMode
|
val voiceWakeMode: StateFlow<VoiceWakeMode> = runtime.voiceWakeMode
|
||||||
@@ -72,14 +70,6 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {
|
|||||||
runtime.setCameraEnabled(value)
|
runtime.setCameraEnabled(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setLocationMode(mode: LocationMode) {
|
|
||||||
runtime.setLocationMode(mode)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun setLocationPreciseEnabled(value: Boolean) {
|
|
||||||
runtime.setLocationPreciseEnabled(value)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun setPreventSleep(value: Boolean) {
|
fun setPreventSleep(value: Boolean) {
|
||||||
runtime.setPreventSleep(value)
|
runtime.setPreventSleep(value)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package com.clawdis.android
|
|||||||
import android.Manifest
|
import android.Manifest
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.pm.PackageManager
|
import android.content.pm.PackageManager
|
||||||
import android.location.LocationManager
|
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.SystemClock
|
import android.os.SystemClock
|
||||||
import androidx.core.content.ContextCompat
|
import androidx.core.content.ContextCompat
|
||||||
@@ -17,7 +16,6 @@ import com.clawdis.android.bridge.BridgeEndpoint
|
|||||||
import com.clawdis.android.bridge.BridgePairingClient
|
import com.clawdis.android.bridge.BridgePairingClient
|
||||||
import com.clawdis.android.bridge.BridgeSession
|
import com.clawdis.android.bridge.BridgeSession
|
||||||
import com.clawdis.android.node.CameraCaptureManager
|
import com.clawdis.android.node.CameraCaptureManager
|
||||||
import com.clawdis.android.node.LocationCaptureManager
|
|
||||||
import com.clawdis.android.BuildConfig
|
import com.clawdis.android.BuildConfig
|
||||||
import com.clawdis.android.node.CanvasController
|
import com.clawdis.android.node.CanvasController
|
||||||
import com.clawdis.android.node.ScreenRecordManager
|
import com.clawdis.android.node.ScreenRecordManager
|
||||||
@@ -26,7 +24,6 @@ import com.clawdis.android.protocol.ClawdisCameraCommand
|
|||||||
import com.clawdis.android.protocol.ClawdisCanvasA2UIAction
|
import com.clawdis.android.protocol.ClawdisCanvasA2UIAction
|
||||||
import com.clawdis.android.protocol.ClawdisCanvasA2UICommand
|
import com.clawdis.android.protocol.ClawdisCanvasA2UICommand
|
||||||
import com.clawdis.android.protocol.ClawdisCanvasCommand
|
import com.clawdis.android.protocol.ClawdisCanvasCommand
|
||||||
import com.clawdis.android.protocol.ClawdisLocationCommand
|
|
||||||
import com.clawdis.android.protocol.ClawdisScreenCommand
|
import com.clawdis.android.protocol.ClawdisScreenCommand
|
||||||
import com.clawdis.android.voice.TalkModeManager
|
import com.clawdis.android.voice.TalkModeManager
|
||||||
import com.clawdis.android.voice.VoiceWakeManager
|
import com.clawdis.android.voice.VoiceWakeManager
|
||||||
@@ -34,7 +31,6 @@ import kotlinx.coroutines.CoroutineScope
|
|||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.Job
|
import kotlinx.coroutines.Job
|
||||||
import kotlinx.coroutines.SupervisorJob
|
import kotlinx.coroutines.SupervisorJob
|
||||||
import kotlinx.coroutines.TimeoutCancellationException
|
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.flow.StateFlow
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
@@ -59,7 +55,6 @@ class NodeRuntime(context: Context) {
|
|||||||
val prefs = SecurePrefs(appContext)
|
val prefs = SecurePrefs(appContext)
|
||||||
val canvas = CanvasController()
|
val canvas = CanvasController()
|
||||||
val camera = CameraCaptureManager(appContext)
|
val camera = CameraCaptureManager(appContext)
|
||||||
val location = LocationCaptureManager(appContext)
|
|
||||||
val screenRecorder = ScreenRecordManager(appContext)
|
val screenRecorder = ScreenRecordManager(appContext)
|
||||||
private val json = Json { ignoreUnknownKeys = true }
|
private val json = Json { ignoreUnknownKeys = true }
|
||||||
|
|
||||||
@@ -191,8 +186,6 @@ class NodeRuntime(context: Context) {
|
|||||||
val instanceId: StateFlow<String> = prefs.instanceId
|
val instanceId: StateFlow<String> = prefs.instanceId
|
||||||
val displayName: StateFlow<String> = prefs.displayName
|
val displayName: StateFlow<String> = prefs.displayName
|
||||||
val cameraEnabled: StateFlow<Boolean> = prefs.cameraEnabled
|
val cameraEnabled: StateFlow<Boolean> = prefs.cameraEnabled
|
||||||
val locationMode: StateFlow<LocationMode> = prefs.locationMode
|
|
||||||
val locationPreciseEnabled: StateFlow<Boolean> = prefs.locationPreciseEnabled
|
|
||||||
val preventSleep: StateFlow<Boolean> = prefs.preventSleep
|
val preventSleep: StateFlow<Boolean> = prefs.preventSleep
|
||||||
val wakeWords: StateFlow<List<String>> = prefs.wakeWords
|
val wakeWords: StateFlow<List<String>> = prefs.wakeWords
|
||||||
val voiceWakeMode: StateFlow<VoiceWakeMode> = prefs.voiceWakeMode
|
val voiceWakeMode: StateFlow<VoiceWakeMode> = prefs.voiceWakeMode
|
||||||
@@ -319,14 +312,6 @@ class NodeRuntime(context: Context) {
|
|||||||
prefs.setCameraEnabled(value)
|
prefs.setCameraEnabled(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setLocationMode(mode: LocationMode) {
|
|
||||||
prefs.setLocationMode(mode)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun setLocationPreciseEnabled(value: Boolean) {
|
|
||||||
prefs.setLocationPreciseEnabled(value)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun setPreventSleep(value: Boolean) {
|
fun setPreventSleep(value: Boolean) {
|
||||||
prefs.setPreventSleep(value)
|
prefs.setPreventSleep(value)
|
||||||
}
|
}
|
||||||
@@ -388,9 +373,6 @@ class NodeRuntime(context: Context) {
|
|||||||
add(ClawdisCameraCommand.Snap.rawValue)
|
add(ClawdisCameraCommand.Snap.rawValue)
|
||||||
add(ClawdisCameraCommand.Clip.rawValue)
|
add(ClawdisCameraCommand.Clip.rawValue)
|
||||||
}
|
}
|
||||||
if (locationMode.value != LocationMode.Off) {
|
|
||||||
add(ClawdisLocationCommand.Get.rawValue)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
val resolved =
|
val resolved =
|
||||||
if (storedToken.isNullOrBlank()) {
|
if (storedToken.isNullOrBlank()) {
|
||||||
@@ -402,9 +384,6 @@ class NodeRuntime(context: Context) {
|
|||||||
if (voiceWakeMode.value != VoiceWakeMode.Off && hasRecordAudioPermission()) {
|
if (voiceWakeMode.value != VoiceWakeMode.Off && hasRecordAudioPermission()) {
|
||||||
add(ClawdisCapability.VoiceWake.rawValue)
|
add(ClawdisCapability.VoiceWake.rawValue)
|
||||||
}
|
}
|
||||||
if (locationMode.value != LocationMode.Off) {
|
|
||||||
add(ClawdisCapability.Location.rawValue)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
val versionName = BuildConfig.VERSION_NAME.trim().ifEmpty { "dev" }
|
val versionName = BuildConfig.VERSION_NAME.trim().ifEmpty { "dev" }
|
||||||
val advertisedVersion =
|
val advertisedVersion =
|
||||||
@@ -433,8 +412,7 @@ class NodeRuntime(context: Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!resolved.ok || resolved.token.isNullOrBlank()) {
|
if (!resolved.ok || resolved.token.isNullOrBlank()) {
|
||||||
val errorMessage = resolved.error?.trim().orEmpty().ifEmpty { "pairing required" }
|
_statusText.value = "Failed: pairing required"
|
||||||
_statusText.value = "Failed: $errorMessage"
|
|
||||||
return@launch
|
return@launch
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -466,9 +444,6 @@ class NodeRuntime(context: Context) {
|
|||||||
if (voiceWakeMode.value != VoiceWakeMode.Off && hasRecordAudioPermission()) {
|
if (voiceWakeMode.value != VoiceWakeMode.Off && hasRecordAudioPermission()) {
|
||||||
add(ClawdisCapability.VoiceWake.rawValue)
|
add(ClawdisCapability.VoiceWake.rawValue)
|
||||||
}
|
}
|
||||||
if (locationMode.value != LocationMode.Off) {
|
|
||||||
add(ClawdisCapability.Location.rawValue)
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
commands = invokeCommands,
|
commands = invokeCommands,
|
||||||
),
|
),
|
||||||
@@ -483,28 +458,6 @@ class NodeRuntime(context: Context) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun hasFineLocationPermission(): Boolean {
|
|
||||||
return (
|
|
||||||
ContextCompat.checkSelfPermission(appContext, Manifest.permission.ACCESS_FINE_LOCATION) ==
|
|
||||||
PackageManager.PERMISSION_GRANTED
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun hasCoarseLocationPermission(): Boolean {
|
|
||||||
return (
|
|
||||||
ContextCompat.checkSelfPermission(appContext, Manifest.permission.ACCESS_COARSE_LOCATION) ==
|
|
||||||
PackageManager.PERMISSION_GRANTED
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun hasBackgroundLocationPermission(): Boolean {
|
|
||||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) return true
|
|
||||||
return (
|
|
||||||
ContextCompat.checkSelfPermission(appContext, Manifest.permission.ACCESS_BACKGROUND_LOCATION) ==
|
|
||||||
PackageManager.PERMISSION_GRANTED
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun connectManual() {
|
fun connectManual() {
|
||||||
val host = manualHost.value.trim()
|
val host = manualHost.value.trim()
|
||||||
val port = manualPort.value
|
val port = manualPort.value
|
||||||
@@ -714,14 +667,6 @@ class NodeRuntime(context: Context) {
|
|||||||
message = "CAMERA_DISABLED: enable Camera in Settings",
|
message = "CAMERA_DISABLED: enable Camera in Settings",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
if (command.startsWith(ClawdisLocationCommand.NamespacePrefix) &&
|
|
||||||
locationMode.value == LocationMode.Off
|
|
||||||
) {
|
|
||||||
return BridgeSession.InvokeResult.error(
|
|
||||||
code = "LOCATION_DISABLED",
|
|
||||||
message = "LOCATION_DISABLED: enable Location in Settings",
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
return when (command) {
|
return when (command) {
|
||||||
ClawdisCanvasCommand.Present.rawValue -> {
|
ClawdisCanvasCommand.Present.rawValue -> {
|
||||||
@@ -842,59 +787,6 @@ class NodeRuntime(context: Context) {
|
|||||||
if (includeAudio) externalAudioCaptureActive.value = false
|
if (includeAudio) externalAudioCaptureActive.value = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ClawdisLocationCommand.Get.rawValue -> {
|
|
||||||
val mode = locationMode.value
|
|
||||||
if (!isForeground.value && mode != LocationMode.Always) {
|
|
||||||
return BridgeSession.InvokeResult.error(
|
|
||||||
code = "LOCATION_BACKGROUND_UNAVAILABLE",
|
|
||||||
message = "LOCATION_BACKGROUND_UNAVAILABLE: background location requires Always",
|
|
||||||
)
|
|
||||||
}
|
|
||||||
if (!hasFineLocationPermission() && !hasCoarseLocationPermission()) {
|
|
||||||
return BridgeSession.InvokeResult.error(
|
|
||||||
code = "LOCATION_PERMISSION_REQUIRED",
|
|
||||||
message = "LOCATION_PERMISSION_REQUIRED: grant Location permission",
|
|
||||||
)
|
|
||||||
}
|
|
||||||
if (!isForeground.value && mode == LocationMode.Always && !hasBackgroundLocationPermission()) {
|
|
||||||
return BridgeSession.InvokeResult.error(
|
|
||||||
code = "LOCATION_PERMISSION_REQUIRED",
|
|
||||||
message = "LOCATION_PERMISSION_REQUIRED: enable Always in system Settings",
|
|
||||||
)
|
|
||||||
}
|
|
||||||
val (maxAgeMs, timeoutMs, desiredAccuracy) = parseLocationParams(paramsJson)
|
|
||||||
val preciseEnabled = locationPreciseEnabled.value
|
|
||||||
val accuracy =
|
|
||||||
when (desiredAccuracy) {
|
|
||||||
"precise" -> if (preciseEnabled && hasFineLocationPermission()) "precise" else "balanced"
|
|
||||||
"coarse" -> "coarse"
|
|
||||||
else -> if (preciseEnabled && hasFineLocationPermission()) "precise" else "balanced"
|
|
||||||
}
|
|
||||||
val providers =
|
|
||||||
when (accuracy) {
|
|
||||||
"precise" -> listOf(LocationManager.GPS_PROVIDER, LocationManager.NETWORK_PROVIDER)
|
|
||||||
"coarse" -> listOf(LocationManager.NETWORK_PROVIDER, LocationManager.GPS_PROVIDER)
|
|
||||||
else -> listOf(LocationManager.NETWORK_PROVIDER, LocationManager.GPS_PROVIDER)
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
val payload =
|
|
||||||
location.getLocation(
|
|
||||||
desiredProviders = providers,
|
|
||||||
maxAgeMs = maxAgeMs,
|
|
||||||
timeoutMs = timeoutMs,
|
|
||||||
isPrecise = accuracy == "precise",
|
|
||||||
)
|
|
||||||
BridgeSession.InvokeResult.ok(payload.payloadJson)
|
|
||||||
} catch (err: TimeoutCancellationException) {
|
|
||||||
BridgeSession.InvokeResult.error(
|
|
||||||
code = "LOCATION_TIMEOUT",
|
|
||||||
message = "LOCATION_TIMEOUT: no fix in time",
|
|
||||||
)
|
|
||||||
} catch (err: Throwable) {
|
|
||||||
val message = err.message ?: "LOCATION_UNAVAILABLE: no fix"
|
|
||||||
BridgeSession.InvokeResult.error(code = "LOCATION_UNAVAILABLE", message = message)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ClawdisScreenCommand.Record.rawValue -> {
|
ClawdisScreenCommand.Record.rawValue -> {
|
||||||
// Status pill mirrors screen recording state so it stays visible without overlay stacking.
|
// Status pill mirrors screen recording state so it stays visible without overlay stacking.
|
||||||
_screenRecordActive.value = true
|
_screenRecordActive.value = true
|
||||||
@@ -948,25 +840,6 @@ class NodeRuntime(context: Context) {
|
|||||||
return code to "$code: $message"
|
return code to "$code: $message"
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun parseLocationParams(paramsJson: String?): Triple<Long?, Long, String?> {
|
|
||||||
if (paramsJson.isNullOrBlank()) {
|
|
||||||
return Triple(null, 10_000L, null)
|
|
||||||
}
|
|
||||||
val root =
|
|
||||||
try {
|
|
||||||
json.parseToJsonElement(paramsJson).asObjectOrNull()
|
|
||||||
} catch (_: Throwable) {
|
|
||||||
null
|
|
||||||
}
|
|
||||||
val maxAgeMs = (root?.get("maxAgeMs") as? JsonPrimitive)?.content?.toLongOrNull()
|
|
||||||
val timeoutMs =
|
|
||||||
(root?.get("timeoutMs") as? JsonPrimitive)?.content?.toLongOrNull()?.coerceIn(1_000L, 60_000L)
|
|
||||||
?: 10_000L
|
|
||||||
val desiredAccuracy =
|
|
||||||
(root?.get("desiredAccuracy") as? JsonPrimitive)?.content?.trim()?.lowercase()
|
|
||||||
return Triple(maxAgeMs, timeoutMs, desiredAccuracy)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun resolveA2uiHostUrl(): String? {
|
private fun resolveA2uiHostUrl(): String? {
|
||||||
val raw = session.currentCanvasHostUrl()?.trim().orEmpty()
|
val raw = session.currentCanvasHostUrl()?.trim().orEmpty()
|
||||||
if (raw.isBlank()) return null
|
if (raw.isBlank()) return null
|
||||||
|
|||||||
@@ -47,14 +47,6 @@ class SecurePrefs(context: Context) {
|
|||||||
private val _cameraEnabled = MutableStateFlow(prefs.getBoolean("camera.enabled", true))
|
private val _cameraEnabled = MutableStateFlow(prefs.getBoolean("camera.enabled", true))
|
||||||
val cameraEnabled: StateFlow<Boolean> = _cameraEnabled
|
val cameraEnabled: StateFlow<Boolean> = _cameraEnabled
|
||||||
|
|
||||||
private val _locationMode =
|
|
||||||
MutableStateFlow(LocationMode.fromRawValue(prefs.getString("location.enabledMode", "off")))
|
|
||||||
val locationMode: StateFlow<LocationMode> = _locationMode
|
|
||||||
|
|
||||||
private val _locationPreciseEnabled =
|
|
||||||
MutableStateFlow(prefs.getBoolean("location.preciseEnabled", true))
|
|
||||||
val locationPreciseEnabled: StateFlow<Boolean> = _locationPreciseEnabled
|
|
||||||
|
|
||||||
private val _preventSleep = MutableStateFlow(prefs.getBoolean("screen.preventSleep", true))
|
private val _preventSleep = MutableStateFlow(prefs.getBoolean("screen.preventSleep", true))
|
||||||
val preventSleep: StateFlow<Boolean> = _preventSleep
|
val preventSleep: StateFlow<Boolean> = _preventSleep
|
||||||
|
|
||||||
@@ -101,16 +93,6 @@ class SecurePrefs(context: Context) {
|
|||||||
_cameraEnabled.value = value
|
_cameraEnabled.value = value
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setLocationMode(mode: LocationMode) {
|
|
||||||
prefs.edit { putString("location.enabledMode", mode.rawValue) }
|
|
||||||
_locationMode.value = mode
|
|
||||||
}
|
|
||||||
|
|
||||||
fun setLocationPreciseEnabled(value: Boolean) {
|
|
||||||
prefs.edit { putBoolean("location.preciseEnabled", value) }
|
|
||||||
_locationPreciseEnabled.value = value
|
|
||||||
}
|
|
||||||
|
|
||||||
fun setPreventSleep(value: Boolean) {
|
fun setPreventSleep(value: Boolean) {
|
||||||
prefs.edit { putBoolean("screen.preventSleep", value) }
|
prefs.edit { putBoolean("screen.preventSleep", value) }
|
||||||
_preventSleep.value = value
|
_preventSleep.value = value
|
||||||
|
|||||||
@@ -37,21 +37,21 @@ class BridgePairingClient {
|
|||||||
withContext(Dispatchers.IO) {
|
withContext(Dispatchers.IO) {
|
||||||
val socket = Socket()
|
val socket = Socket()
|
||||||
socket.tcpNoDelay = true
|
socket.tcpNoDelay = true
|
||||||
|
socket.connect(InetSocketAddress(endpoint.host, endpoint.port), 8_000)
|
||||||
|
socket.soTimeout = 60_000
|
||||||
|
|
||||||
|
val reader = BufferedReader(InputStreamReader(socket.getInputStream(), Charsets.UTF_8))
|
||||||
|
val writer = BufferedWriter(OutputStreamWriter(socket.getOutputStream(), Charsets.UTF_8))
|
||||||
|
|
||||||
|
fun send(line: String) {
|
||||||
|
writer.write(line)
|
||||||
|
writer.write("\n")
|
||||||
|
writer.flush()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun sendJson(obj: JsonObject) = send(obj.toString())
|
||||||
|
|
||||||
try {
|
try {
|
||||||
socket.connect(InetSocketAddress(endpoint.host, endpoint.port), 8_000)
|
|
||||||
socket.soTimeout = 60_000
|
|
||||||
|
|
||||||
val reader = BufferedReader(InputStreamReader(socket.getInputStream(), Charsets.UTF_8))
|
|
||||||
val writer = BufferedWriter(OutputStreamWriter(socket.getOutputStream(), Charsets.UTF_8))
|
|
||||||
|
|
||||||
fun send(line: String) {
|
|
||||||
writer.write(line)
|
|
||||||
writer.write("\n")
|
|
||||||
writer.flush()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun sendJson(obj: JsonObject) = send(obj.toString())
|
|
||||||
|
|
||||||
sendJson(
|
sendJson(
|
||||||
buildJsonObject {
|
buildJsonObject {
|
||||||
put("type", JsonPrimitive("hello"))
|
put("type", JsonPrimitive("hello"))
|
||||||
@@ -111,9 +111,6 @@ class BridgePairingClient {
|
|||||||
}
|
}
|
||||||
else -> PairResult(ok = false, token = null, error = "unexpected bridge response")
|
else -> PairResult(ok = false, token = null, error = "unexpected bridge response")
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
|
||||||
val message = e.message?.trim().orEmpty().ifEmpty { "gateway unreachable" }
|
|
||||||
PairResult(ok = false, token = null, error = message)
|
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
try {
|
||||||
socket.close()
|
socket.close()
|
||||||
|
|||||||
@@ -1,98 +0,0 @@
|
|||||||
package com.clawdis.android.node
|
|
||||||
|
|
||||||
import android.annotation.SuppressLint
|
|
||||||
import android.content.Context
|
|
||||||
import android.location.Location
|
|
||||||
import android.location.LocationManager
|
|
||||||
import android.os.CancellationSignal
|
|
||||||
import java.time.Instant
|
|
||||||
import java.time.format.DateTimeFormatter
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.withContext
|
|
||||||
import kotlinx.coroutines.withTimeout
|
|
||||||
import kotlin.coroutines.resume
|
|
||||||
import kotlin.coroutines.resumeWithException
|
|
||||||
import kotlinx.coroutines.suspendCancellableCoroutine
|
|
||||||
|
|
||||||
class LocationCaptureManager(private val context: Context) {
|
|
||||||
data class Payload(val payloadJson: String)
|
|
||||||
|
|
||||||
suspend fun getLocation(
|
|
||||||
desiredProviders: List<String>,
|
|
||||||
maxAgeMs: Long?,
|
|
||||||
timeoutMs: Long,
|
|
||||||
isPrecise: Boolean,
|
|
||||||
): Payload =
|
|
||||||
withContext(Dispatchers.Main) {
|
|
||||||
val manager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
|
|
||||||
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER) &&
|
|
||||||
!manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
|
|
||||||
) {
|
|
||||||
throw IllegalStateException("LOCATION_UNAVAILABLE: no location providers enabled")
|
|
||||||
}
|
|
||||||
|
|
||||||
val cached = bestLastKnown(manager, desiredProviders, maxAgeMs)
|
|
||||||
val location =
|
|
||||||
cached ?: requestCurrent(manager, desiredProviders, timeoutMs)
|
|
||||||
|
|
||||||
val timestamp = DateTimeFormatter.ISO_INSTANT.format(Instant.ofEpochMilli(location.time))
|
|
||||||
val source = location.provider
|
|
||||||
val altitudeMeters = if (location.hasAltitude()) location.altitude else null
|
|
||||||
val speedMps = if (location.hasSpeed()) location.speed.toDouble() else null
|
|
||||||
val headingDeg = if (location.hasBearing()) location.bearing.toDouble() else null
|
|
||||||
Payload(
|
|
||||||
buildString {
|
|
||||||
append("{\"lat\":")
|
|
||||||
append(location.latitude)
|
|
||||||
append(",\"lon\":")
|
|
||||||
append(location.longitude)
|
|
||||||
append(",\"accuracyMeters\":")
|
|
||||||
append(location.accuracy.toDouble())
|
|
||||||
if (altitudeMeters != null) append(",\"altitudeMeters\":").append(altitudeMeters)
|
|
||||||
if (speedMps != null) append(",\"speedMps\":").append(speedMps)
|
|
||||||
if (headingDeg != null) append(",\"headingDeg\":").append(headingDeg)
|
|
||||||
append(",\"timestamp\":\"").append(timestamp).append('"')
|
|
||||||
append(",\"isPrecise\":").append(isPrecise)
|
|
||||||
append(",\"source\":\"").append(source).append('"')
|
|
||||||
append('}')
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun bestLastKnown(
|
|
||||||
manager: LocationManager,
|
|
||||||
providers: List<String>,
|
|
||||||
maxAgeMs: Long?,
|
|
||||||
): Location? {
|
|
||||||
val now = System.currentTimeMillis()
|
|
||||||
val candidates =
|
|
||||||
providers.mapNotNull { provider -> manager.getLastKnownLocation(provider) }
|
|
||||||
val freshest = candidates.maxByOrNull { it.time } ?: return null
|
|
||||||
if (maxAgeMs != null && now - freshest.time > maxAgeMs) return null
|
|
||||||
return freshest
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressLint("MissingPermission")
|
|
||||||
private suspend fun requestCurrent(
|
|
||||||
manager: LocationManager,
|
|
||||||
providers: List<String>,
|
|
||||||
timeoutMs: Long,
|
|
||||||
): Location {
|
|
||||||
val resolved =
|
|
||||||
providers.firstOrNull { manager.isProviderEnabled(it) }
|
|
||||||
?: throw IllegalStateException("LOCATION_UNAVAILABLE: no providers available")
|
|
||||||
return withTimeout(timeoutMs.coerceAtLeast(1)) {
|
|
||||||
suspendCancellableCoroutine { cont ->
|
|
||||||
val signal = CancellationSignal()
|
|
||||||
cont.invokeOnCancellation { signal.cancel() }
|
|
||||||
manager.getCurrentLocation(resolved, signal, context.mainExecutor) { location ->
|
|
||||||
if (location != null) {
|
|
||||||
cont.resume(location)
|
|
||||||
} else {
|
|
||||||
cont.resumeWithException(IllegalStateException("LOCATION_UNAVAILABLE: no fix"))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-10
@@ -5,7 +5,6 @@ enum class ClawdisCapability(val rawValue: String) {
|
|||||||
Camera("camera"),
|
Camera("camera"),
|
||||||
Screen("screen"),
|
Screen("screen"),
|
||||||
VoiceWake("voiceWake"),
|
VoiceWake("voiceWake"),
|
||||||
Location("location"),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
enum class ClawdisCanvasCommand(val rawValue: String) {
|
enum class ClawdisCanvasCommand(val rawValue: String) {
|
||||||
@@ -50,12 +49,3 @@ enum class ClawdisScreenCommand(val rawValue: String) {
|
|||||||
const val NamespacePrefix: String = "screen."
|
const val NamespacePrefix: String = "screen."
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
enum class ClawdisLocationCommand(val rawValue: String) {
|
|
||||||
Get("location.get"),
|
|
||||||
;
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
const val NamespacePrefix: String = "location."
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,12 +1,8 @@
|
|||||||
package com.clawdis.android.ui
|
package com.clawdis.android.ui
|
||||||
|
|
||||||
import android.Manifest
|
import android.Manifest
|
||||||
import android.content.Context
|
|
||||||
import android.content.Intent
|
|
||||||
import android.content.pm.PackageManager
|
import android.content.pm.PackageManager
|
||||||
import android.net.Uri
|
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.provider.Settings
|
|
||||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||||
import androidx.activity.result.contract.ActivityResultContracts
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
import androidx.compose.animation.AnimatedVisibility
|
import androidx.compose.animation.AnimatedVisibility
|
||||||
@@ -46,14 +42,12 @@ import androidx.compose.runtime.collectAsState
|
|||||||
import androidx.compose.runtime.getValue
|
import androidx.compose.runtime.getValue
|
||||||
import androidx.compose.runtime.mutableStateOf
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
import androidx.compose.runtime.setValue
|
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.platform.LocalContext
|
import androidx.compose.ui.platform.LocalContext
|
||||||
import androidx.compose.ui.text.style.TextAlign
|
import androidx.compose.ui.text.style.TextAlign
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.core.content.ContextCompat
|
import androidx.core.content.ContextCompat
|
||||||
import com.clawdis.android.BuildConfig
|
import com.clawdis.android.BuildConfig
|
||||||
import com.clawdis.android.LocationMode
|
|
||||||
import com.clawdis.android.MainViewModel
|
import com.clawdis.android.MainViewModel
|
||||||
import com.clawdis.android.NodeForegroundService
|
import com.clawdis.android.NodeForegroundService
|
||||||
import com.clawdis.android.VoiceWakeMode
|
import com.clawdis.android.VoiceWakeMode
|
||||||
@@ -64,8 +58,6 @@ fun SettingsSheet(viewModel: MainViewModel) {
|
|||||||
val instanceId by viewModel.instanceId.collectAsState()
|
val instanceId by viewModel.instanceId.collectAsState()
|
||||||
val displayName by viewModel.displayName.collectAsState()
|
val displayName by viewModel.displayName.collectAsState()
|
||||||
val cameraEnabled by viewModel.cameraEnabled.collectAsState()
|
val cameraEnabled by viewModel.cameraEnabled.collectAsState()
|
||||||
val locationMode by viewModel.locationMode.collectAsState()
|
|
||||||
val locationPreciseEnabled by viewModel.locationPreciseEnabled.collectAsState()
|
|
||||||
val preventSleep by viewModel.preventSleep.collectAsState()
|
val preventSleep by viewModel.preventSleep.collectAsState()
|
||||||
val wakeWords by viewModel.wakeWords.collectAsState()
|
val wakeWords by viewModel.wakeWords.collectAsState()
|
||||||
val voiceWakeMode by viewModel.voiceWakeMode.collectAsState()
|
val voiceWakeMode by viewModel.voiceWakeMode.collectAsState()
|
||||||
@@ -109,41 +101,6 @@ fun SettingsSheet(viewModel: MainViewModel) {
|
|||||||
viewModel.setCameraEnabled(cameraOk)
|
viewModel.setCameraEnabled(cameraOk)
|
||||||
}
|
}
|
||||||
|
|
||||||
var pendingLocationMode by remember { mutableStateOf<LocationMode?>(null) }
|
|
||||||
var pendingPreciseToggle by remember { mutableStateOf(false) }
|
|
||||||
|
|
||||||
val locationPermissionLauncher =
|
|
||||||
rememberLauncherForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { perms ->
|
|
||||||
val fineOk = perms[Manifest.permission.ACCESS_FINE_LOCATION] == true
|
|
||||||
val coarseOk = perms[Manifest.permission.ACCESS_COARSE_LOCATION] == true
|
|
||||||
val granted = fineOk || coarseOk
|
|
||||||
val requestedMode = pendingLocationMode
|
|
||||||
pendingLocationMode = null
|
|
||||||
|
|
||||||
if (pendingPreciseToggle) {
|
|
||||||
pendingPreciseToggle = false
|
|
||||||
viewModel.setLocationPreciseEnabled(fineOk)
|
|
||||||
return@rememberLauncherForActivityResult
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!granted) {
|
|
||||||
viewModel.setLocationMode(LocationMode.Off)
|
|
||||||
return@rememberLauncherForActivityResult
|
|
||||||
}
|
|
||||||
|
|
||||||
if (requestedMode != null) {
|
|
||||||
viewModel.setLocationMode(requestedMode)
|
|
||||||
if (requestedMode == LocationMode.Always && Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
|
||||||
val backgroundOk =
|
|
||||||
ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_BACKGROUND_LOCATION) ==
|
|
||||||
PackageManager.PERMISSION_GRANTED
|
|
||||||
if (!backgroundOk) {
|
|
||||||
openAppSettings(context)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
val audioPermissionLauncher =
|
val audioPermissionLauncher =
|
||||||
rememberLauncherForActivityResult(ActivityResultContracts.RequestPermission()) { _ ->
|
rememberLauncherForActivityResult(ActivityResultContracts.RequestPermission()) { _ ->
|
||||||
// Status text is handled by NodeRuntime.
|
// Status text is handled by NodeRuntime.
|
||||||
@@ -165,47 +122,6 @@ fun SettingsSheet(viewModel: MainViewModel) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun requestLocationPermissions(targetMode: LocationMode) {
|
|
||||||
val fineOk =
|
|
||||||
ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) ==
|
|
||||||
PackageManager.PERMISSION_GRANTED
|
|
||||||
val coarseOk =
|
|
||||||
ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) ==
|
|
||||||
PackageManager.PERMISSION_GRANTED
|
|
||||||
if (fineOk || coarseOk) {
|
|
||||||
viewModel.setLocationMode(targetMode)
|
|
||||||
if (targetMode == LocationMode.Always && Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
|
||||||
val backgroundOk =
|
|
||||||
ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_BACKGROUND_LOCATION) ==
|
|
||||||
PackageManager.PERMISSION_GRANTED
|
|
||||||
if (!backgroundOk) {
|
|
||||||
openAppSettings(context)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
pendingLocationMode = targetMode
|
|
||||||
locationPermissionLauncher.launch(
|
|
||||||
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun setPreciseLocationChecked(checked: Boolean) {
|
|
||||||
if (!checked) {
|
|
||||||
viewModel.setLocationPreciseEnabled(false)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
val fineOk =
|
|
||||||
ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) ==
|
|
||||||
PackageManager.PERMISSION_GRANTED
|
|
||||||
if (fineOk) {
|
|
||||||
viewModel.setLocationPreciseEnabled(true)
|
|
||||||
} else {
|
|
||||||
pendingPreciseToggle = true
|
|
||||||
locationPermissionLauncher.launch(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
val visibleBridges =
|
val visibleBridges =
|
||||||
if (isConnected && remoteAddress != null) {
|
if (isConnected && remoteAddress != null) {
|
||||||
bridges.filterNot { "${it.host}:${it.port}" == remoteAddress }
|
bridges.filterNot { "${it.host}:${it.port}" == remoteAddress }
|
||||||
@@ -233,7 +149,7 @@ fun SettingsSheet(viewModel: MainViewModel) {
|
|||||||
contentPadding = PaddingValues(16.dp),
|
contentPadding = PaddingValues(16.dp),
|
||||||
verticalArrangement = Arrangement.spacedBy(6.dp),
|
verticalArrangement = Arrangement.spacedBy(6.dp),
|
||||||
) {
|
) {
|
||||||
// Order parity: Node → Bridge → Voice → Camera → Location → Screen.
|
// Order parity: Node → Bridge → Voice → Camera → Screen.
|
||||||
item { Text("Node", style = MaterialTheme.typography.titleSmall) }
|
item { Text("Node", style = MaterialTheme.typography.titleSmall) }
|
||||||
item {
|
item {
|
||||||
OutlinedTextField(
|
OutlinedTextField(
|
||||||
@@ -507,64 +423,6 @@ fun SettingsSheet(viewModel: MainViewModel) {
|
|||||||
|
|
||||||
item { HorizontalDivider() }
|
item { HorizontalDivider() }
|
||||||
|
|
||||||
// Location
|
|
||||||
item { Text("Location", style = MaterialTheme.typography.titleSmall) }
|
|
||||||
item {
|
|
||||||
Column(verticalArrangement = Arrangement.spacedBy(6.dp), modifier = Modifier.fillMaxWidth()) {
|
|
||||||
ListItem(
|
|
||||||
headlineContent = { Text("Off") },
|
|
||||||
supportingContent = { Text("Disable location sharing.") },
|
|
||||||
trailingContent = {
|
|
||||||
RadioButton(
|
|
||||||
selected = locationMode == LocationMode.Off,
|
|
||||||
onClick = { viewModel.setLocationMode(LocationMode.Off) },
|
|
||||||
)
|
|
||||||
},
|
|
||||||
)
|
|
||||||
ListItem(
|
|
||||||
headlineContent = { Text("While Using") },
|
|
||||||
supportingContent = { Text("Only while Clawdis is open.") },
|
|
||||||
trailingContent = {
|
|
||||||
RadioButton(
|
|
||||||
selected = locationMode == LocationMode.WhileUsing,
|
|
||||||
onClick = { requestLocationPermissions(LocationMode.WhileUsing) },
|
|
||||||
)
|
|
||||||
},
|
|
||||||
)
|
|
||||||
ListItem(
|
|
||||||
headlineContent = { Text("Always") },
|
|
||||||
supportingContent = { Text("Allow background location (requires system permission).") },
|
|
||||||
trailingContent = {
|
|
||||||
RadioButton(
|
|
||||||
selected = locationMode == LocationMode.Always,
|
|
||||||
onClick = { requestLocationPermissions(LocationMode.Always) },
|
|
||||||
)
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
item {
|
|
||||||
ListItem(
|
|
||||||
headlineContent = { Text("Precise Location") },
|
|
||||||
supportingContent = { Text("Use precise GPS when available.") },
|
|
||||||
trailingContent = {
|
|
||||||
Switch(
|
|
||||||
checked = locationPreciseEnabled,
|
|
||||||
onCheckedChange = ::setPreciseLocationChecked,
|
|
||||||
enabled = locationMode != LocationMode.Off,
|
|
||||||
)
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
item {
|
|
||||||
Text(
|
|
||||||
"Always may require Android Settings to allow background location.",
|
|
||||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
item { HorizontalDivider() }
|
|
||||||
|
|
||||||
// Screen
|
// Screen
|
||||||
item { Text("Screen", style = MaterialTheme.typography.titleSmall) }
|
item { Text("Screen", style = MaterialTheme.typography.titleSmall) }
|
||||||
item {
|
item {
|
||||||
@@ -595,12 +453,3 @@ fun SettingsSheet(viewModel: MainViewModel) {
|
|||||||
item { Spacer(modifier = Modifier.height(20.dp)) }
|
item { Spacer(modifier = Modifier.height(20.dp)) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun openAppSettings(context: Context) {
|
|
||||||
val intent =
|
|
||||||
Intent(
|
|
||||||
Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
|
|
||||||
Uri.fromParts("package", context.packageName, null),
|
|
||||||
)
|
|
||||||
context.startActivity(intent)
|
|
||||||
}
|
|
||||||
@@ -229,10 +229,6 @@ final class BridgeConnectionController {
|
|||||||
let voiceWakeEnabled = UserDefaults.standard.bool(forKey: VoiceWakePreferences.enabledKey)
|
let voiceWakeEnabled = UserDefaults.standard.bool(forKey: VoiceWakePreferences.enabledKey)
|
||||||
if voiceWakeEnabled { caps.append(ClawdisCapability.voiceWake.rawValue) }
|
if voiceWakeEnabled { caps.append(ClawdisCapability.voiceWake.rawValue) }
|
||||||
|
|
||||||
let locationModeRaw = UserDefaults.standard.string(forKey: "location.enabledMode") ?? "off"
|
|
||||||
let locationMode = ClawdisLocationMode(rawValue: locationModeRaw) ?? .off
|
|
||||||
if locationMode != .off { caps.append(ClawdisCapability.location.rawValue) }
|
|
||||||
|
|
||||||
return caps
|
return caps
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -255,9 +251,6 @@ final class BridgeConnectionController {
|
|||||||
commands.append(ClawdisCameraCommand.snap.rawValue)
|
commands.append(ClawdisCameraCommand.snap.rawValue)
|
||||||
commands.append(ClawdisCameraCommand.clip.rawValue)
|
commands.append(ClawdisCameraCommand.clip.rawValue)
|
||||||
}
|
}
|
||||||
if caps.contains(ClawdisCapability.location.rawValue) {
|
|
||||||
commands.append(ClawdisLocationCommand.get.rawValue)
|
|
||||||
}
|
|
||||||
|
|
||||||
return commands
|
return commands
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,10 +35,6 @@
|
|||||||
<string>Clawdis can capture photos or short video clips when requested via the bridge.</string>
|
<string>Clawdis can capture photos or short video clips when requested via the bridge.</string>
|
||||||
<key>NSLocalNetworkUsageDescription</key>
|
<key>NSLocalNetworkUsageDescription</key>
|
||||||
<string>Clawdis discovers and connects to your Clawdis bridge on the local network.</string>
|
<string>Clawdis discovers and connects to your Clawdis bridge on the local network.</string>
|
||||||
<key>NSLocationWhenInUseUsageDescription</key>
|
|
||||||
<string>Clawdis uses your location when you allow location sharing.</string>
|
|
||||||
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
|
|
||||||
<string>Clawdis can share your location in the background when you enable Always.</string>
|
|
||||||
<key>NSMicrophoneUsageDescription</key>
|
<key>NSMicrophoneUsageDescription</key>
|
||||||
<string>Clawdis needs microphone access for voice wake.</string>
|
<string>Clawdis needs microphone access for voice wake.</string>
|
||||||
<key>NSSpeechRecognitionUsageDescription</key>
|
<key>NSSpeechRecognitionUsageDescription</key>
|
||||||
|
|||||||
@@ -1,142 +0,0 @@
|
|||||||
import ClawdisKit
|
|
||||||
import CoreLocation
|
|
||||||
import Foundation
|
|
||||||
|
|
||||||
@MainActor
|
|
||||||
final class LocationService: NSObject, CLLocationManagerDelegate {
|
|
||||||
enum Error: Swift.Error {
|
|
||||||
case timeout
|
|
||||||
case unavailable
|
|
||||||
}
|
|
||||||
|
|
||||||
private let manager = CLLocationManager()
|
|
||||||
private var authContinuation: CheckedContinuation<CLAuthorizationStatus, Never>?
|
|
||||||
private var locationContinuation: CheckedContinuation<CLLocation, Swift.Error>?
|
|
||||||
|
|
||||||
override init() {
|
|
||||||
super.init()
|
|
||||||
self.manager.delegate = self
|
|
||||||
self.manager.desiredAccuracy = kCLLocationAccuracyBest
|
|
||||||
}
|
|
||||||
|
|
||||||
func authorizationStatus() -> CLAuthorizationStatus {
|
|
||||||
self.manager.authorizationStatus
|
|
||||||
}
|
|
||||||
|
|
||||||
func accuracyAuthorization() -> CLAccuracyAuthorization {
|
|
||||||
if #available(iOS 14.0, *) {
|
|
||||||
return self.manager.accuracyAuthorization
|
|
||||||
}
|
|
||||||
return .fullAccuracy
|
|
||||||
}
|
|
||||||
|
|
||||||
func ensureAuthorization(mode: ClawdisLocationMode) async -> CLAuthorizationStatus {
|
|
||||||
guard CLLocationManager.locationServicesEnabled() else { return .denied }
|
|
||||||
|
|
||||||
let status = self.manager.authorizationStatus
|
|
||||||
if status == .notDetermined {
|
|
||||||
self.manager.requestWhenInUseAuthorization()
|
|
||||||
let updated = await self.awaitAuthorizationChange()
|
|
||||||
if mode != .always { return updated }
|
|
||||||
}
|
|
||||||
|
|
||||||
if mode == .always {
|
|
||||||
let current = self.manager.authorizationStatus
|
|
||||||
if current == .authorizedWhenInUse {
|
|
||||||
self.manager.requestAlwaysAuthorization()
|
|
||||||
return await self.awaitAuthorizationChange()
|
|
||||||
}
|
|
||||||
return current
|
|
||||||
}
|
|
||||||
|
|
||||||
return self.manager.authorizationStatus
|
|
||||||
}
|
|
||||||
|
|
||||||
func currentLocation(
|
|
||||||
params: ClawdisLocationGetParams,
|
|
||||||
desiredAccuracy: ClawdisLocationAccuracy,
|
|
||||||
maxAgeMs: Int?,
|
|
||||||
timeoutMs: Int?) async throws -> CLLocation
|
|
||||||
{
|
|
||||||
let now = Date()
|
|
||||||
if let maxAgeMs,
|
|
||||||
let cached = self.manager.location,
|
|
||||||
now.timeIntervalSince(cached.timestamp) * 1000 <= Double(maxAgeMs)
|
|
||||||
{
|
|
||||||
return cached
|
|
||||||
}
|
|
||||||
|
|
||||||
self.manager.desiredAccuracy = Self.accuracyValue(desiredAccuracy)
|
|
||||||
let timeout = max(0, timeoutMs ?? 10_000)
|
|
||||||
return try await self.withTimeout(timeoutMs: timeout) {
|
|
||||||
try await self.requestLocation()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private func requestLocation() async throws -> CLLocation {
|
|
||||||
try await withCheckedThrowingContinuation { cont in
|
|
||||||
self.locationContinuation = cont
|
|
||||||
self.manager.requestLocation()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private func awaitAuthorizationChange() async -> CLAuthorizationStatus {
|
|
||||||
await withCheckedContinuation { cont in
|
|
||||||
self.authContinuation = cont
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private func withTimeout<T>(
|
|
||||||
timeoutMs: Int,
|
|
||||||
operation: @escaping () async throws -> T) async throws -> T
|
|
||||||
{
|
|
||||||
if timeoutMs == 0 {
|
|
||||||
return try await operation()
|
|
||||||
}
|
|
||||||
|
|
||||||
return try await withThrowingTaskGroup(of: T.self) { group in
|
|
||||||
group.addTask { try await operation() }
|
|
||||||
group.addTask {
|
|
||||||
try await Task.sleep(nanoseconds: UInt64(timeoutMs) * 1_000_000)
|
|
||||||
throw Error.timeout
|
|
||||||
}
|
|
||||||
let result = try await group.next()!
|
|
||||||
group.cancelAll()
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static func accuracyValue(_ accuracy: ClawdisLocationAccuracy) -> CLLocationAccuracy {
|
|
||||||
switch accuracy {
|
|
||||||
case .coarse:
|
|
||||||
return kCLLocationAccuracyKilometer
|
|
||||||
case .balanced:
|
|
||||||
return kCLLocationAccuracyHundredMeters
|
|
||||||
case .precise:
|
|
||||||
return kCLLocationAccuracyBest
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
|
|
||||||
if let cont = self.authContinuation {
|
|
||||||
self.authContinuation = nil
|
|
||||||
cont.resume(returning: manager.authorizationStatus)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
|
|
||||||
guard let cont = self.locationContinuation else { return }
|
|
||||||
self.locationContinuation = nil
|
|
||||||
if let latest = locations.last {
|
|
||||||
cont.resume(returning: latest)
|
|
||||||
} else {
|
|
||||||
cont.resume(throwing: Error.unavailable)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func locationManager(_ manager: CLLocationManager, didFailWithError error: Swift.Error) {
|
|
||||||
guard let cont = self.locationContinuation else { return }
|
|
||||||
self.locationContinuation = nil
|
|
||||||
cont.resume(throwing: error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -31,7 +31,6 @@ final class NodeAppModel {
|
|||||||
@ObservationIgnored private var cameraHUDDismissTask: Task<Void, Never>?
|
@ObservationIgnored private var cameraHUDDismissTask: Task<Void, Never>?
|
||||||
let voiceWake = VoiceWakeManager()
|
let voiceWake = VoiceWakeManager()
|
||||||
let talkMode = TalkModeManager()
|
let talkMode = TalkModeManager()
|
||||||
private let locationService = LocationService()
|
|
||||||
private var lastAutoA2uiURL: String?
|
private var lastAutoA2uiURL: String?
|
||||||
|
|
||||||
var bridgeSession: BridgeSession { self.bridge }
|
var bridgeSession: BridgeSession { self.bridge }
|
||||||
@@ -189,19 +188,6 @@ final class NodeAppModel {
|
|||||||
self.talkMode.setEnabled(enabled)
|
self.talkMode.setEnabled(enabled)
|
||||||
}
|
}
|
||||||
|
|
||||||
func requestLocationPermissions(mode: ClawdisLocationMode) async -> Bool {
|
|
||||||
guard mode != .off else { return true }
|
|
||||||
let status = await self.locationService.ensureAuthorization(mode: mode)
|
|
||||||
switch status {
|
|
||||||
case .authorizedAlways:
|
|
||||||
return true
|
|
||||||
case .authorizedWhenInUse:
|
|
||||||
return mode != .always
|
|
||||||
default:
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func connectToBridge(
|
func connectToBridge(
|
||||||
endpoint: NWEndpoint,
|
endpoint: NWEndpoint,
|
||||||
hello: BridgeHello)
|
hello: BridgeHello)
|
||||||
@@ -480,64 +466,6 @@ final class NodeAppModel {
|
|||||||
|
|
||||||
do {
|
do {
|
||||||
switch command {
|
switch command {
|
||||||
case ClawdisLocationCommand.get.rawValue:
|
|
||||||
let mode = self.locationMode()
|
|
||||||
guard mode != .off else {
|
|
||||||
return BridgeInvokeResponse(
|
|
||||||
id: req.id,
|
|
||||||
ok: false,
|
|
||||||
error: ClawdisNodeError(
|
|
||||||
code: .unavailable,
|
|
||||||
message: "LOCATION_DISABLED: enable Location in Settings"))
|
|
||||||
}
|
|
||||||
if self.isBackgrounded, mode != .always {
|
|
||||||
return BridgeInvokeResponse(
|
|
||||||
id: req.id,
|
|
||||||
ok: false,
|
|
||||||
error: ClawdisNodeError(
|
|
||||||
code: .backgroundUnavailable,
|
|
||||||
message: "LOCATION_BACKGROUND_UNAVAILABLE: background location requires Always"))
|
|
||||||
}
|
|
||||||
let params = (try? Self.decodeParams(ClawdisLocationGetParams.self, from: req.paramsJSON)) ??
|
|
||||||
ClawdisLocationGetParams()
|
|
||||||
let desired = params.desiredAccuracy ??
|
|
||||||
(self.isLocationPreciseEnabled() ? .precise : .balanced)
|
|
||||||
let status = self.locationService.authorizationStatus()
|
|
||||||
if status != .authorizedAlways && status != .authorizedWhenInUse {
|
|
||||||
return BridgeInvokeResponse(
|
|
||||||
id: req.id,
|
|
||||||
ok: false,
|
|
||||||
error: ClawdisNodeError(
|
|
||||||
code: .unavailable,
|
|
||||||
message: "LOCATION_PERMISSION_REQUIRED: grant Location permission"))
|
|
||||||
}
|
|
||||||
if self.isBackgrounded && status != .authorizedAlways {
|
|
||||||
return BridgeInvokeResponse(
|
|
||||||
id: req.id,
|
|
||||||
ok: false,
|
|
||||||
error: ClawdisNodeError(
|
|
||||||
code: .unavailable,
|
|
||||||
message: "LOCATION_PERMISSION_REQUIRED: enable Always for background access"))
|
|
||||||
}
|
|
||||||
let location = try await self.locationService.currentLocation(
|
|
||||||
params: params,
|
|
||||||
desiredAccuracy: desired,
|
|
||||||
maxAgeMs: params.maxAgeMs,
|
|
||||||
timeoutMs: params.timeoutMs)
|
|
||||||
let isPrecise = self.locationService.accuracyAuthorization() == .fullAccuracy
|
|
||||||
let payload = ClawdisLocationPayload(
|
|
||||||
lat: location.coordinate.latitude,
|
|
||||||
lon: location.coordinate.longitude,
|
|
||||||
accuracyMeters: location.horizontalAccuracy,
|
|
||||||
altitudeMeters: location.verticalAccuracy >= 0 ? location.altitude : nil,
|
|
||||||
speedMps: location.speed >= 0 ? location.speed : nil,
|
|
||||||
headingDeg: location.course >= 0 ? location.course : nil,
|
|
||||||
timestamp: ISO8601DateFormatter().string(from: location.timestamp),
|
|
||||||
isPrecise: isPrecise,
|
|
||||||
source: nil)
|
|
||||||
let json = try Self.encodePayload(payload)
|
|
||||||
return BridgeInvokeResponse(id: req.id, ok: true, payloadJSON: json)
|
|
||||||
|
|
||||||
case ClawdisCanvasCommand.present.rawValue:
|
case ClawdisCanvasCommand.present.rawValue:
|
||||||
let params = (try? Self.decodeParams(ClawdisCanvasPresentParams.self, from: req.paramsJSON)) ??
|
let params = (try? Self.decodeParams(ClawdisCanvasPresentParams.self, from: req.paramsJSON)) ??
|
||||||
ClawdisCanvasPresentParams()
|
ClawdisCanvasPresentParams()
|
||||||
@@ -768,16 +696,6 @@ final class NodeAppModel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func locationMode() -> ClawdisLocationMode {
|
|
||||||
let raw = UserDefaults.standard.string(forKey: "location.enabledMode") ?? "off"
|
|
||||||
return ClawdisLocationMode(rawValue: raw) ?? .off
|
|
||||||
}
|
|
||||||
|
|
||||||
private func isLocationPreciseEnabled() -> Bool {
|
|
||||||
if UserDefaults.standard.object(forKey: "location.preciseEnabled") == nil { return true }
|
|
||||||
return UserDefaults.standard.bool(forKey: "location.preciseEnabled")
|
|
||||||
}
|
|
||||||
|
|
||||||
private static func decodeParams<T: Decodable>(_ type: T.Type, from json: String?) throws -> T {
|
private static func decodeParams<T: Decodable>(_ type: T.Type, from json: String?) throws -> T {
|
||||||
guard let json, let data = json.data(using: .utf8) else {
|
guard let json, let data = json.data(using: .utf8) else {
|
||||||
throw NSError(domain: "Bridge", code: 20, userInfo: [
|
throw NSError(domain: "Bridge", code: 20, userInfo: [
|
||||||
|
|||||||
@@ -23,8 +23,6 @@ struct SettingsTab: View {
|
|||||||
@AppStorage("talk.enabled") private var talkEnabled: Bool = false
|
@AppStorage("talk.enabled") private var talkEnabled: Bool = false
|
||||||
@AppStorage("talk.button.enabled") private var talkButtonEnabled: Bool = true
|
@AppStorage("talk.button.enabled") private var talkButtonEnabled: Bool = true
|
||||||
@AppStorage("camera.enabled") private var cameraEnabled: Bool = true
|
@AppStorage("camera.enabled") private var cameraEnabled: Bool = true
|
||||||
@AppStorage("location.enabledMode") private var locationEnabledModeRaw: String = ClawdisLocationMode.off.rawValue
|
|
||||||
@AppStorage("location.preciseEnabled") private var locationPreciseEnabled: Bool = true
|
|
||||||
@AppStorage("screen.preventSleep") private var preventSleep: Bool = true
|
@AppStorage("screen.preventSleep") private var preventSleep: Bool = true
|
||||||
@AppStorage("bridge.preferredStableID") private var preferredBridgeStableID: String = ""
|
@AppStorage("bridge.preferredStableID") private var preferredBridgeStableID: String = ""
|
||||||
@AppStorage("bridge.lastDiscoveredStableID") private var lastDiscoveredBridgeStableID: String = ""
|
@AppStorage("bridge.lastDiscoveredStableID") private var lastDiscoveredBridgeStableID: String = ""
|
||||||
@@ -36,7 +34,6 @@ struct SettingsTab: View {
|
|||||||
@State private var connectStatus = ConnectStatusStore()
|
@State private var connectStatus = ConnectStatusStore()
|
||||||
@State private var connectingBridgeID: String?
|
@State private var connectingBridgeID: String?
|
||||||
@State private var localIPAddress: String?
|
@State private var localIPAddress: String?
|
||||||
@State private var lastLocationModeRaw: String = ClawdisLocationMode.off.rawValue
|
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
NavigationStack {
|
NavigationStack {
|
||||||
@@ -184,22 +181,6 @@ struct SettingsTab: View {
|
|||||||
.foregroundStyle(.secondary)
|
.foregroundStyle(.secondary)
|
||||||
}
|
}
|
||||||
|
|
||||||
Section("Location") {
|
|
||||||
Picker("Location Access", selection: self.$locationEnabledModeRaw) {
|
|
||||||
Text("Off").tag(ClawdisLocationMode.off.rawValue)
|
|
||||||
Text("While Using").tag(ClawdisLocationMode.whileUsing.rawValue)
|
|
||||||
Text("Always").tag(ClawdisLocationMode.always.rawValue)
|
|
||||||
}
|
|
||||||
.pickerStyle(.segmented)
|
|
||||||
|
|
||||||
Toggle("Precise Location", isOn: self.$locationPreciseEnabled)
|
|
||||||
.disabled(self.locationMode == .off)
|
|
||||||
|
|
||||||
Text("Always requires system permission and may prompt to open Settings.")
|
|
||||||
.font(.footnote)
|
|
||||||
.foregroundStyle(.secondary)
|
|
||||||
}
|
|
||||||
|
|
||||||
Section("Screen") {
|
Section("Screen") {
|
||||||
Toggle("Prevent Sleep", isOn: self.$preventSleep)
|
Toggle("Prevent Sleep", isOn: self.$preventSleep)
|
||||||
Text("Keeps the screen awake while Clawdis is open.")
|
Text("Keeps the screen awake while Clawdis is open.")
|
||||||
@@ -220,7 +201,6 @@ struct SettingsTab: View {
|
|||||||
}
|
}
|
||||||
.onAppear {
|
.onAppear {
|
||||||
self.localIPAddress = Self.primaryIPv4Address()
|
self.localIPAddress = Self.primaryIPv4Address()
|
||||||
self.lastLocationModeRaw = self.locationEnabledModeRaw
|
|
||||||
}
|
}
|
||||||
.onChange(of: self.preferredBridgeStableID) { _, newValue in
|
.onChange(of: self.preferredBridgeStableID) { _, newValue in
|
||||||
let trimmed = newValue.trimmingCharacters(in: .whitespacesAndNewlines)
|
let trimmed = newValue.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||||
@@ -230,20 +210,6 @@ struct SettingsTab: View {
|
|||||||
.onChange(of: self.appModel.bridgeServerName) { _, _ in
|
.onChange(of: self.appModel.bridgeServerName) { _, _ in
|
||||||
self.connectStatus.text = nil
|
self.connectStatus.text = nil
|
||||||
}
|
}
|
||||||
.onChange(of: self.locationEnabledModeRaw) { _, newValue in
|
|
||||||
let previous = self.lastLocationModeRaw
|
|
||||||
self.lastLocationModeRaw = newValue
|
|
||||||
guard let mode = ClawdisLocationMode(rawValue: newValue) else { return }
|
|
||||||
Task {
|
|
||||||
let granted = await self.appModel.requestLocationPermissions(mode: mode)
|
|
||||||
if !granted {
|
|
||||||
await MainActor.run {
|
|
||||||
self.locationEnabledModeRaw = previous
|
|
||||||
self.lastLocationModeRaw = previous
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -312,10 +278,6 @@ struct SettingsTab: View {
|
|||||||
return "iOS \(v.majorVersion).\(v.minorVersion).\(v.patchVersion)"
|
return "iOS \(v.majorVersion).\(v.minorVersion).\(v.patchVersion)"
|
||||||
}
|
}
|
||||||
|
|
||||||
private var locationMode: ClawdisLocationMode {
|
|
||||||
ClawdisLocationMode(rawValue: self.locationEnabledModeRaw) ?? .off
|
|
||||||
}
|
|
||||||
|
|
||||||
private func appVersion() -> String {
|
private func appVersion() -> String {
|
||||||
Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "dev"
|
Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "dev"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,8 +8,6 @@ import SwiftUI
|
|||||||
@Observable
|
@Observable
|
||||||
final class AppState {
|
final class AppState {
|
||||||
private let isPreview: Bool
|
private let isPreview: Bool
|
||||||
private var isInitializing = true
|
|
||||||
private var configWatcher: ConfigFileWatcher?
|
|
||||||
private var suppressVoiceWakeGlobalSync = false
|
private var suppressVoiceWakeGlobalSync = false
|
||||||
private var voiceWakeGlobalSyncTask: Task<Void, Never>?
|
private var voiceWakeGlobalSyncTask: Task<Void, Never>?
|
||||||
|
|
||||||
@@ -155,7 +153,6 @@ final class AppState {
|
|||||||
var connectionMode: ConnectionMode {
|
var connectionMode: ConnectionMode {
|
||||||
didSet {
|
didSet {
|
||||||
self.ifNotPreview { UserDefaults.standard.set(self.connectionMode.rawValue, forKey: connectionModeKey) }
|
self.ifNotPreview { UserDefaults.standard.set(self.connectionMode.rawValue, forKey: connectionModeKey) }
|
||||||
self.syncGatewayConfigIfNeeded()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -184,10 +181,7 @@ final class AppState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var remoteTarget: String {
|
var remoteTarget: String {
|
||||||
didSet {
|
didSet { self.ifNotPreview { UserDefaults.standard.set(self.remoteTarget, forKey: remoteTargetKey) } }
|
||||||
self.ifNotPreview { UserDefaults.standard.set(self.remoteTarget, forKey: remoteTargetKey) }
|
|
||||||
self.syncGatewayConfigIfNeeded()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var remoteIdentity: String {
|
var remoteIdentity: String {
|
||||||
@@ -251,44 +245,13 @@ final class AppState {
|
|||||||
UserDefaults.standard.set(IconOverrideSelection.system.rawValue, forKey: iconOverrideKey)
|
UserDefaults.standard.set(IconOverrideSelection.system.rawValue, forKey: iconOverrideKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
let configRoot = ClawdisConfigFile.loadDict()
|
|
||||||
let configGateway = configRoot["gateway"] as? [String: Any]
|
|
||||||
let configModeRaw = (configGateway?["mode"] as? String)?.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
||||||
let configMode: ConnectionMode? = {
|
|
||||||
switch configModeRaw {
|
|
||||||
case "local":
|
|
||||||
return .local
|
|
||||||
case "remote":
|
|
||||||
return .remote
|
|
||||||
default:
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
let configRemoteUrl = (configGateway?["remote"] as? [String: Any])?["url"] as? String
|
|
||||||
let configHasRemoteUrl = !(configRemoteUrl?
|
|
||||||
.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
||||||
.isEmpty ?? true)
|
|
||||||
|
|
||||||
let storedMode = UserDefaults.standard.string(forKey: connectionModeKey)
|
let storedMode = UserDefaults.standard.string(forKey: connectionModeKey)
|
||||||
if let configMode {
|
if let storedMode {
|
||||||
self.connectionMode = configMode
|
|
||||||
} else if configHasRemoteUrl {
|
|
||||||
self.connectionMode = .remote
|
|
||||||
} else if let storedMode {
|
|
||||||
self.connectionMode = ConnectionMode(rawValue: storedMode) ?? .local
|
self.connectionMode = ConnectionMode(rawValue: storedMode) ?? .local
|
||||||
} else {
|
} else {
|
||||||
self.connectionMode = onboardingSeen ? .local : .unconfigured
|
self.connectionMode = onboardingSeen ? .local : .unconfigured
|
||||||
}
|
}
|
||||||
|
self.remoteTarget = UserDefaults.standard.string(forKey: remoteTargetKey) ?? ""
|
||||||
let storedRemoteTarget = UserDefaults.standard.string(forKey: remoteTargetKey) ?? ""
|
|
||||||
if self.connectionMode == .remote,
|
|
||||||
storedRemoteTarget.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty,
|
|
||||||
let host = AppState.remoteHost(from: configRemoteUrl)
|
|
||||||
{
|
|
||||||
self.remoteTarget = "\(NSUserName())@\(host)"
|
|
||||||
} else {
|
|
||||||
self.remoteTarget = storedRemoteTarget
|
|
||||||
}
|
|
||||||
self.remoteIdentity = UserDefaults.standard.string(forKey: remoteIdentityKey) ?? ""
|
self.remoteIdentity = UserDefaults.standard.string(forKey: remoteIdentityKey) ?? ""
|
||||||
self.remoteProjectRoot = UserDefaults.standard.string(forKey: remoteProjectRootKey) ?? ""
|
self.remoteProjectRoot = UserDefaults.standard.string(forKey: remoteProjectRootKey) ?? ""
|
||||||
self.remoteCliPath = UserDefaults.standard.string(forKey: remoteCliPathKey) ?? ""
|
self.remoteCliPath = UserDefaults.standard.string(forKey: remoteCliPathKey) ?? ""
|
||||||
@@ -315,138 +278,6 @@ final class AppState {
|
|||||||
Task { await VoiceWakeRuntime.shared.refresh(state: self) }
|
Task { await VoiceWakeRuntime.shared.refresh(state: self) }
|
||||||
Task { await TalkModeController.shared.setEnabled(self.talkEnabled) }
|
Task { await TalkModeController.shared.setEnabled(self.talkEnabled) }
|
||||||
}
|
}
|
||||||
|
|
||||||
self.isInitializing = false
|
|
||||||
if !self.isPreview {
|
|
||||||
self.startConfigWatcher()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
deinit {
|
|
||||||
self.configWatcher?.stop()
|
|
||||||
}
|
|
||||||
|
|
||||||
private static func remoteHost(from urlString: String?) -> String? {
|
|
||||||
guard let raw = urlString?.trimmingCharacters(in: .whitespacesAndNewlines),
|
|
||||||
!raw.isEmpty,
|
|
||||||
let url = URL(string: raw),
|
|
||||||
let host = url.host?.trimmingCharacters(in: .whitespacesAndNewlines),
|
|
||||||
!host.isEmpty
|
|
||||||
else {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return host
|
|
||||||
}
|
|
||||||
|
|
||||||
private func startConfigWatcher() {
|
|
||||||
let configUrl = ClawdisConfigFile.url()
|
|
||||||
self.configWatcher = ConfigFileWatcher(url: configUrl) { [weak self] in
|
|
||||||
Task { @MainActor in
|
|
||||||
self?.applyConfigFromDisk()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
self.configWatcher?.start()
|
|
||||||
}
|
|
||||||
|
|
||||||
private func applyConfigFromDisk() {
|
|
||||||
let root = ClawdisConfigFile.loadDict()
|
|
||||||
self.applyConfigOverrides(root)
|
|
||||||
}
|
|
||||||
|
|
||||||
private func applyConfigOverrides(_ root: [String: Any]) {
|
|
||||||
let gateway = root["gateway"] as? [String: Any]
|
|
||||||
let modeRaw = (gateway?["mode"] as? String)?.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
||||||
let remoteUrl = (gateway?["remote"] as? [String: Any])?["url"] as? String
|
|
||||||
let hasRemoteUrl = !(remoteUrl?
|
|
||||||
.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
||||||
.isEmpty ?? true)
|
|
||||||
|
|
||||||
let desiredMode: ConnectionMode? = {
|
|
||||||
switch modeRaw {
|
|
||||||
case "local":
|
|
||||||
return .local
|
|
||||||
case "remote":
|
|
||||||
return .remote
|
|
||||||
case "unconfigured":
|
|
||||||
return .unconfigured
|
|
||||||
default:
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
if let desiredMode {
|
|
||||||
if desiredMode != self.connectionMode {
|
|
||||||
self.connectionMode = desiredMode
|
|
||||||
}
|
|
||||||
} else if hasRemoteUrl, self.connectionMode != .remote {
|
|
||||||
self.connectionMode = .remote
|
|
||||||
}
|
|
||||||
|
|
||||||
let targetMode = desiredMode ?? self.connectionMode
|
|
||||||
if targetMode == .remote,
|
|
||||||
let host = AppState.remoteHost(from: remoteUrl)
|
|
||||||
{
|
|
||||||
self.updateRemoteTarget(host: host)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private func updateRemoteTarget(host: String) {
|
|
||||||
let parsed = CommandResolver.parseSSHTarget(self.remoteTarget)
|
|
||||||
let user = parsed?.user ?? NSUserName()
|
|
||||||
let port = parsed?.port ?? 22
|
|
||||||
let assembled = port == 22 ? "\(user)@\(host)" : "\(user)@\(host):\(port)"
|
|
||||||
if assembled != self.remoteTarget {
|
|
||||||
self.remoteTarget = assembled
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private func syncGatewayConfigIfNeeded() {
|
|
||||||
guard !self.isPreview, !self.isInitializing else { return }
|
|
||||||
|
|
||||||
var root = ClawdisConfigFile.loadDict()
|
|
||||||
var gateway = root["gateway"] as? [String: Any] ?? [:]
|
|
||||||
var changed = false
|
|
||||||
|
|
||||||
let desiredMode: String?
|
|
||||||
switch self.connectionMode {
|
|
||||||
case .local:
|
|
||||||
desiredMode = "local"
|
|
||||||
case .remote:
|
|
||||||
desiredMode = "remote"
|
|
||||||
case .unconfigured:
|
|
||||||
desiredMode = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
let currentMode = (gateway["mode"] as? String)?.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
||||||
if let desiredMode {
|
|
||||||
if currentMode != desiredMode {
|
|
||||||
gateway["mode"] = desiredMode
|
|
||||||
changed = true
|
|
||||||
}
|
|
||||||
} else if currentMode != nil {
|
|
||||||
gateway.removeValue(forKey: "mode")
|
|
||||||
changed = true
|
|
||||||
}
|
|
||||||
|
|
||||||
if self.connectionMode == .remote,
|
|
||||||
let host = CommandResolver.parseSSHTarget(self.remoteTarget)?.host
|
|
||||||
{
|
|
||||||
var remote = gateway["remote"] as? [String: Any] ?? [:]
|
|
||||||
let existingUrl = (remote["url"] as? String)?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
|
|
||||||
let parsedExisting = existingUrl.isEmpty ? nil : URL(string: existingUrl)
|
|
||||||
let scheme = parsedExisting?.scheme?.isEmpty == false ? parsedExisting?.scheme : "ws"
|
|
||||||
let port = parsedExisting?.port ?? 18789
|
|
||||||
let desiredUrl = "\(scheme ?? "ws")://\(host):\(port)"
|
|
||||||
if existingUrl != desiredUrl {
|
|
||||||
remote["url"] = desiredUrl
|
|
||||||
gateway["remote"] = remote
|
|
||||||
changed = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
guard changed else { return }
|
|
||||||
root["gateway"] = gateway
|
|
||||||
ClawdisConfigFile.saveDict(root)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func triggerVoiceEars(ttl: TimeInterval? = 5) {
|
func triggerVoiceEars(ttl: TimeInterval? = 5) {
|
||||||
|
|||||||
@@ -1,118 +0,0 @@
|
|||||||
import CoreServices
|
|
||||||
import Foundation
|
|
||||||
|
|
||||||
final class ConfigFileWatcher: @unchecked Sendable {
|
|
||||||
private let url: URL
|
|
||||||
private let queue: DispatchQueue
|
|
||||||
private var stream: FSEventStreamRef?
|
|
||||||
private var pending = false
|
|
||||||
private let onChange: () -> Void
|
|
||||||
private let watchedDir: URL
|
|
||||||
private let targetPath: String
|
|
||||||
private let targetName: String
|
|
||||||
|
|
||||||
init(url: URL, onChange: @escaping () -> Void) {
|
|
||||||
self.url = url
|
|
||||||
self.queue = DispatchQueue(label: "com.clawdis.configwatcher")
|
|
||||||
self.onChange = onChange
|
|
||||||
self.watchedDir = url.deletingLastPathComponent()
|
|
||||||
self.targetPath = url.path
|
|
||||||
self.targetName = url.lastPathComponent
|
|
||||||
}
|
|
||||||
|
|
||||||
deinit {
|
|
||||||
self.stop()
|
|
||||||
}
|
|
||||||
|
|
||||||
func start() {
|
|
||||||
guard self.stream == nil else { return }
|
|
||||||
|
|
||||||
let retainedSelf = Unmanaged.passRetained(self)
|
|
||||||
var context = FSEventStreamContext(
|
|
||||||
version: 0,
|
|
||||||
info: retainedSelf.toOpaque(),
|
|
||||||
retain: nil,
|
|
||||||
release: { pointer in
|
|
||||||
guard let pointer else { return }
|
|
||||||
Unmanaged<ConfigFileWatcher>.fromOpaque(pointer).release()
|
|
||||||
},
|
|
||||||
copyDescription: nil)
|
|
||||||
|
|
||||||
let paths = [self.watchedDir.path] as CFArray
|
|
||||||
let flags = FSEventStreamCreateFlags(
|
|
||||||
kFSEventStreamCreateFlagFileEvents |
|
|
||||||
kFSEventStreamCreateFlagUseCFTypes |
|
|
||||||
kFSEventStreamCreateFlagNoDefer)
|
|
||||||
|
|
||||||
guard let stream = FSEventStreamCreate(
|
|
||||||
kCFAllocatorDefault,
|
|
||||||
Self.callback,
|
|
||||||
&context,
|
|
||||||
paths,
|
|
||||||
FSEventStreamEventId(kFSEventStreamEventIdSinceNow),
|
|
||||||
0.05,
|
|
||||||
flags)
|
|
||||||
else {
|
|
||||||
retainedSelf.release()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
self.stream = stream
|
|
||||||
FSEventStreamSetDispatchQueue(stream, self.queue)
|
|
||||||
if FSEventStreamStart(stream) == false {
|
|
||||||
self.stream = nil
|
|
||||||
FSEventStreamSetDispatchQueue(stream, nil)
|
|
||||||
FSEventStreamInvalidate(stream)
|
|
||||||
FSEventStreamRelease(stream)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func stop() {
|
|
||||||
guard let stream = self.stream else { return }
|
|
||||||
self.stream = nil
|
|
||||||
FSEventStreamStop(stream)
|
|
||||||
FSEventStreamSetDispatchQueue(stream, nil)
|
|
||||||
FSEventStreamInvalidate(stream)
|
|
||||||
FSEventStreamRelease(stream)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
extension ConfigFileWatcher {
|
|
||||||
private static let callback: FSEventStreamCallback = { _, info, numEvents, eventPaths, eventFlags, _ in
|
|
||||||
guard let info else { return }
|
|
||||||
let watcher = Unmanaged<ConfigFileWatcher>.fromOpaque(info).takeUnretainedValue()
|
|
||||||
watcher.handleEvents(
|
|
||||||
numEvents: numEvents,
|
|
||||||
eventPaths: eventPaths,
|
|
||||||
eventFlags: eventFlags)
|
|
||||||
}
|
|
||||||
|
|
||||||
private func handleEvents(
|
|
||||||
numEvents: Int,
|
|
||||||
eventPaths: UnsafeMutableRawPointer?,
|
|
||||||
eventFlags: UnsafePointer<FSEventStreamEventFlags>?
|
|
||||||
) {
|
|
||||||
guard numEvents > 0 else { return }
|
|
||||||
guard eventFlags != nil else { return }
|
|
||||||
guard self.matchesTarget(eventPaths: eventPaths) else { return }
|
|
||||||
|
|
||||||
if self.pending { return }
|
|
||||||
self.pending = true
|
|
||||||
self.queue.asyncAfter(deadline: .now() + 0.12) { [weak self] in
|
|
||||||
guard let self else { return }
|
|
||||||
self.pending = false
|
|
||||||
self.onChange()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private func matchesTarget(eventPaths: UnsafeMutableRawPointer?) -> Bool {
|
|
||||||
guard let eventPaths else { return true }
|
|
||||||
let paths = unsafeBitCast(eventPaths, to: NSArray.self)
|
|
||||||
for case let path as String in paths {
|
|
||||||
if path == self.targetPath { return true }
|
|
||||||
if path.hasSuffix("/\(self.targetName)") { return true }
|
|
||||||
if path == self.watchedDir.path { return true }
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -25,8 +25,6 @@ let remoteProjectRootKey = "clawdis.remoteProjectRoot"
|
|||||||
let remoteCliPathKey = "clawdis.remoteCliPath"
|
let remoteCliPathKey = "clawdis.remoteCliPath"
|
||||||
let canvasEnabledKey = "clawdis.canvasEnabled"
|
let canvasEnabledKey = "clawdis.canvasEnabled"
|
||||||
let cameraEnabledKey = "clawdis.cameraEnabled"
|
let cameraEnabledKey = "clawdis.cameraEnabled"
|
||||||
let locationModeKey = "clawdis.locationMode"
|
|
||||||
let locationPreciseKey = "clawdis.locationPreciseEnabled"
|
|
||||||
let peekabooBridgeEnabledKey = "clawdis.peekabooBridgeEnabled"
|
let peekabooBridgeEnabledKey = "clawdis.peekabooBridgeEnabled"
|
||||||
let deepLinkKeyKey = "clawdis.deepLinkKey"
|
let deepLinkKeyKey = "clawdis.deepLinkKey"
|
||||||
let modelCatalogPathKey = "clawdis.modelCatalogPath"
|
let modelCatalogPathKey = "clawdis.modelCatalogPath"
|
||||||
|
|||||||
@@ -306,7 +306,6 @@ extension GatewayConnection {
|
|||||||
struct SnapshotConfig: Decodable, Sendable {
|
struct SnapshotConfig: Decodable, Sendable {
|
||||||
struct Session: Decodable, Sendable {
|
struct Session: Decodable, Sendable {
|
||||||
let mainKey: String?
|
let mainKey: String?
|
||||||
let scope: String?
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let session: Session?
|
let session: Session?
|
||||||
@@ -317,11 +316,9 @@ extension GatewayConnection {
|
|||||||
|
|
||||||
static func mainSessionKey(fromConfigGetData data: Data) throws -> String {
|
static func mainSessionKey(fromConfigGetData data: Data) throws -> String {
|
||||||
let snapshot = try JSONDecoder().decode(ConfigGetSnapshot.self, from: data)
|
let snapshot = try JSONDecoder().decode(ConfigGetSnapshot.self, from: data)
|
||||||
let scope = snapshot.config?.session?.scope?.trimmingCharacters(in: .whitespacesAndNewlines)
|
let raw = snapshot.config?.session?.mainKey
|
||||||
if scope == "global" {
|
let trimmed = (raw ?? "").trimmingCharacters(in: .whitespacesAndNewlines)
|
||||||
return "global"
|
return trimmed.isEmpty ? "main" : trimmed
|
||||||
}
|
|
||||||
return "main"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func mainSessionKey(timeoutMs: Double = 15000) async -> String {
|
func mainSessionKey(timeoutMs: Double = 15000) async -> String {
|
||||||
|
|||||||
@@ -1,15 +1,11 @@
|
|||||||
import AppKit
|
import AppKit
|
||||||
import ClawdisIPC
|
import ClawdisIPC
|
||||||
import ClawdisKit
|
|
||||||
import CoreLocation
|
|
||||||
import Observation
|
import Observation
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
struct GeneralSettings: View {
|
struct GeneralSettings: View {
|
||||||
@Bindable var state: AppState
|
@Bindable var state: AppState
|
||||||
@AppStorage(cameraEnabledKey) private var cameraEnabled: Bool = false
|
@AppStorage(cameraEnabledKey) private var cameraEnabled: Bool = false
|
||||||
@AppStorage(locationModeKey) private var locationModeRaw: String = ClawdisLocationMode.off.rawValue
|
|
||||||
@AppStorage(locationPreciseKey) private var locationPreciseEnabled: Bool = true
|
|
||||||
private let healthStore = HealthStore.shared
|
private let healthStore = HealthStore.shared
|
||||||
private let gatewayManager = GatewayProcessManager.shared
|
private let gatewayManager = GatewayProcessManager.shared
|
||||||
@State private var gatewayDiscovery = GatewayDiscoveryModel()
|
@State private var gatewayDiscovery = GatewayDiscoveryModel()
|
||||||
@@ -22,7 +18,6 @@ struct GeneralSettings: View {
|
|||||||
@State private var showRemoteAdvanced = false
|
@State private var showRemoteAdvanced = false
|
||||||
private let isPreview = ProcessInfo.processInfo.isPreview
|
private let isPreview = ProcessInfo.processInfo.isPreview
|
||||||
private var isNixMode: Bool { ProcessInfo.processInfo.isNixMode }
|
private var isNixMode: Bool { ProcessInfo.processInfo.isNixMode }
|
||||||
@State private var lastLocationModeRaw: String = ClawdisLocationMode.off.rawValue
|
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
ScrollView(.vertical) {
|
ScrollView(.vertical) {
|
||||||
@@ -69,26 +64,6 @@ struct GeneralSettings: View {
|
|||||||
subtitle: "Allow the agent to capture a photo or short video via the built-in camera.",
|
subtitle: "Allow the agent to capture a photo or short video via the built-in camera.",
|
||||||
binding: self.$cameraEnabled)
|
binding: self.$cameraEnabled)
|
||||||
|
|
||||||
VStack(alignment: .leading, spacing: 6) {
|
|
||||||
Text("Location Access")
|
|
||||||
.font(.body)
|
|
||||||
|
|
||||||
Picker("", selection: self.$locationModeRaw) {
|
|
||||||
Text("Off").tag(ClawdisLocationMode.off.rawValue)
|
|
||||||
Text("While Using").tag(ClawdisLocationMode.whileUsing.rawValue)
|
|
||||||
Text("Always").tag(ClawdisLocationMode.always.rawValue)
|
|
||||||
}
|
|
||||||
.pickerStyle(.segmented)
|
|
||||||
|
|
||||||
Toggle("Precise Location", isOn: self.$locationPreciseEnabled)
|
|
||||||
.disabled(self.locationMode == .off)
|
|
||||||
|
|
||||||
Text("Always may require System Settings to approve background location.")
|
|
||||||
.font(.footnote)
|
|
||||||
.foregroundStyle(.tertiary)
|
|
||||||
.fixedSize(horizontal: false, vertical: true)
|
|
||||||
}
|
|
||||||
|
|
||||||
SettingsToggleRow(
|
SettingsToggleRow(
|
||||||
title: "Enable Peekaboo Bridge",
|
title: "Enable Peekaboo Bridge",
|
||||||
subtitle: "Allow signed tools (e.g. `peekaboo`) to drive UI automation via PeekabooBridge.",
|
subtitle: "Allow signed tools (e.g. `peekaboo`) to drive UI automation via PeekabooBridge.",
|
||||||
@@ -115,27 +90,12 @@ struct GeneralSettings: View {
|
|||||||
guard !self.isPreview else { return }
|
guard !self.isPreview else { return }
|
||||||
self.refreshCLIStatus()
|
self.refreshCLIStatus()
|
||||||
self.refreshGatewayStatus()
|
self.refreshGatewayStatus()
|
||||||
self.lastLocationModeRaw = self.locationModeRaw
|
|
||||||
}
|
}
|
||||||
.onChange(of: self.state.canvasEnabled) { _, enabled in
|
.onChange(of: self.state.canvasEnabled) { _, enabled in
|
||||||
if !enabled {
|
if !enabled {
|
||||||
CanvasManager.shared.hideAll()
|
CanvasManager.shared.hideAll()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.onChange(of: self.locationModeRaw) { _, newValue in
|
|
||||||
let previous = self.lastLocationModeRaw
|
|
||||||
self.lastLocationModeRaw = newValue
|
|
||||||
guard let mode = ClawdisLocationMode(rawValue: newValue) else { return }
|
|
||||||
Task {
|
|
||||||
let granted = await self.requestLocationAuthorization(mode: mode)
|
|
||||||
if !granted {
|
|
||||||
await MainActor.run {
|
|
||||||
self.locationModeRaw = previous
|
|
||||||
self.lastLocationModeRaw = previous
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private var activeBinding: Binding<Bool> {
|
private var activeBinding: Binding<Bool> {
|
||||||
@@ -144,29 +104,6 @@ struct GeneralSettings: View {
|
|||||||
set: { self.state.isPaused = !$0 })
|
set: { self.state.isPaused = !$0 })
|
||||||
}
|
}
|
||||||
|
|
||||||
private var locationMode: ClawdisLocationMode {
|
|
||||||
ClawdisLocationMode(rawValue: self.locationModeRaw) ?? .off
|
|
||||||
}
|
|
||||||
|
|
||||||
private func requestLocationAuthorization(mode: ClawdisLocationMode) async -> Bool {
|
|
||||||
guard mode != .off else { return true }
|
|
||||||
let status = CLLocationManager.authorizationStatus()
|
|
||||||
if status == .authorizedAlways || status == .authorizedWhenInUse {
|
|
||||||
if mode == .always && status != .authorizedAlways {
|
|
||||||
let updated = await LocationPermissionRequester.shared.request(always: true)
|
|
||||||
return updated == .authorizedAlways || updated == .authorizedWhenInUse
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
let updated = await LocationPermissionRequester.shared.request(always: mode == .always)
|
|
||||||
switch updated {
|
|
||||||
case .authorizedAlways, .authorizedWhenInUse:
|
|
||||||
return true
|
|
||||||
default:
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private var connectionSection: some View {
|
private var connectionSection: some View {
|
||||||
VStack(alignment: .leading, spacing: 10) {
|
VStack(alignment: .leading, spacing: 10) {
|
||||||
Text("Clawdis runs")
|
Text("Clawdis runs")
|
||||||
|
|||||||
@@ -435,7 +435,7 @@ final class MenuSessionsInjector: NSObject, NSMenuDelegate {
|
|||||||
compact.representedObject = row.key
|
compact.representedObject = row.key
|
||||||
menu.addItem(compact)
|
menu.addItem(compact)
|
||||||
|
|
||||||
if row.key != "main" && row.key != "global" {
|
if row.key != "main" {
|
||||||
let del = NSMenuItem(title: "Delete Session", action: #selector(self.deleteSession(_:)), keyEquivalent: "")
|
let del = NSMenuItem(title: "Delete Session", action: #selector(self.deleteSession(_:)), keyEquivalent: "")
|
||||||
del.target = self
|
del.target = self
|
||||||
del.representedObject = row.key
|
del.representedObject = row.key
|
||||||
|
|||||||
@@ -1,116 +0,0 @@
|
|||||||
import ClawdisKit
|
|
||||||
import CoreLocation
|
|
||||||
import Foundation
|
|
||||||
|
|
||||||
@MainActor
|
|
||||||
final class MacNodeLocationService: NSObject, CLLocationManagerDelegate {
|
|
||||||
enum Error: Swift.Error {
|
|
||||||
case timeout
|
|
||||||
case unavailable
|
|
||||||
}
|
|
||||||
|
|
||||||
private let manager = CLLocationManager()
|
|
||||||
private var locationContinuation: CheckedContinuation<CLLocation, Swift.Error>?
|
|
||||||
|
|
||||||
override init() {
|
|
||||||
super.init()
|
|
||||||
self.manager.delegate = self
|
|
||||||
self.manager.desiredAccuracy = kCLLocationAccuracyBest
|
|
||||||
}
|
|
||||||
|
|
||||||
func authorizationStatus() -> CLAuthorizationStatus {
|
|
||||||
self.manager.authorizationStatus
|
|
||||||
}
|
|
||||||
|
|
||||||
func accuracyAuthorization() -> CLAccuracyAuthorization {
|
|
||||||
if #available(macOS 11.0, *) {
|
|
||||||
return self.manager.accuracyAuthorization
|
|
||||||
}
|
|
||||||
return .fullAccuracy
|
|
||||||
}
|
|
||||||
|
|
||||||
func currentLocation(
|
|
||||||
desiredAccuracy: ClawdisLocationAccuracy,
|
|
||||||
maxAgeMs: Int?,
|
|
||||||
timeoutMs: Int?) async throws -> CLLocation
|
|
||||||
{
|
|
||||||
guard CLLocationManager.locationServicesEnabled() else {
|
|
||||||
throw Error.unavailable
|
|
||||||
}
|
|
||||||
|
|
||||||
let now = Date()
|
|
||||||
if let maxAgeMs,
|
|
||||||
let cached = self.manager.location,
|
|
||||||
now.timeIntervalSince(cached.timestamp) * 1000 <= Double(maxAgeMs)
|
|
||||||
{
|
|
||||||
return cached
|
|
||||||
}
|
|
||||||
|
|
||||||
self.manager.desiredAccuracy = Self.accuracyValue(desiredAccuracy)
|
|
||||||
let timeout = max(0, timeoutMs ?? 10_000)
|
|
||||||
return try await self.withTimeout(timeoutMs: timeout) {
|
|
||||||
try await self.requestLocation()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private func requestLocation() async throws -> CLLocation {
|
|
||||||
try await withCheckedThrowingContinuation { cont in
|
|
||||||
self.locationContinuation = cont
|
|
||||||
self.manager.requestLocation()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private func withTimeout<T>(
|
|
||||||
timeoutMs: Int,
|
|
||||||
operation: @escaping () async throws -> T) async throws -> T
|
|
||||||
{
|
|
||||||
if timeoutMs == 0 {
|
|
||||||
return try await operation()
|
|
||||||
}
|
|
||||||
|
|
||||||
return try await withThrowingTaskGroup(of: T.self) { group in
|
|
||||||
group.addTask { try await operation() }
|
|
||||||
group.addTask {
|
|
||||||
try await Task.sleep(nanoseconds: UInt64(timeoutMs) * 1_000_000)
|
|
||||||
throw Error.timeout
|
|
||||||
}
|
|
||||||
let result = try await group.next()!
|
|
||||||
group.cancelAll()
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static func accuracyValue(_ accuracy: ClawdisLocationAccuracy) -> CLLocationAccuracy {
|
|
||||||
switch accuracy {
|
|
||||||
case .coarse:
|
|
||||||
return kCLLocationAccuracyKilometer
|
|
||||||
case .balanced:
|
|
||||||
return kCLLocationAccuracyHundredMeters
|
|
||||||
case .precise:
|
|
||||||
return kCLLocationAccuracyBest
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// MARK: - CLLocationManagerDelegate (nonisolated for Swift 6 compatibility)
|
|
||||||
|
|
||||||
nonisolated func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
|
|
||||||
Task { @MainActor in
|
|
||||||
guard let cont = self.locationContinuation else { return }
|
|
||||||
self.locationContinuation = nil
|
|
||||||
if let latest = locations.last {
|
|
||||||
cont.resume(returning: latest)
|
|
||||||
} else {
|
|
||||||
cont.resume(throwing: Error.unavailable)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
nonisolated func locationManager(_ manager: CLLocationManager, didFailWithError error: Swift.Error) {
|
|
||||||
let errorCopy = error // Capture error for Sendable compliance
|
|
||||||
Task { @MainActor in
|
|
||||||
guard let cont = self.locationContinuation else { return }
|
|
||||||
self.locationContinuation = nil
|
|
||||||
cont.resume(throwing: errorCopy)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -110,10 +110,6 @@ final class MacNodeModeCoordinator {
|
|||||||
if UserDefaults.standard.object(forKey: cameraEnabledKey) as? Bool ?? false {
|
if UserDefaults.standard.object(forKey: cameraEnabledKey) as? Bool ?? false {
|
||||||
caps.append(ClawdisCapability.camera.rawValue)
|
caps.append(ClawdisCapability.camera.rawValue)
|
||||||
}
|
}
|
||||||
let rawLocationMode = UserDefaults.standard.string(forKey: locationModeKey) ?? "off"
|
|
||||||
if ClawdisLocationMode(rawValue: rawLocationMode) != .off {
|
|
||||||
caps.append(ClawdisCapability.location.rawValue)
|
|
||||||
}
|
|
||||||
return caps
|
return caps
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -143,9 +139,6 @@ final class MacNodeModeCoordinator {
|
|||||||
commands.append(ClawdisCameraCommand.snap.rawValue)
|
commands.append(ClawdisCameraCommand.snap.rawValue)
|
||||||
commands.append(ClawdisCameraCommand.clip.rawValue)
|
commands.append(ClawdisCameraCommand.clip.rawValue)
|
||||||
}
|
}
|
||||||
if capsSet.contains(ClawdisCapability.location.rawValue) {
|
|
||||||
commands.append(ClawdisLocationCommand.get.rawValue)
|
|
||||||
}
|
|
||||||
|
|
||||||
return commands
|
return commands
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import Foundation
|
|||||||
actor MacNodeRuntime {
|
actor MacNodeRuntime {
|
||||||
private let cameraCapture = CameraCaptureService()
|
private let cameraCapture = CameraCaptureService()
|
||||||
@MainActor private let screenRecorder = ScreenRecordService()
|
@MainActor private let screenRecorder = ScreenRecordService()
|
||||||
@MainActor private let locationService = MacNodeLocationService()
|
|
||||||
|
|
||||||
// swiftlint:disable:next function_body_length cyclomatic_complexity
|
// swiftlint:disable:next function_body_length cyclomatic_complexity
|
||||||
func handleInvoke(_ req: BridgeInvokeRequest) async -> BridgeInvokeResponse {
|
func handleInvoke(_ req: BridgeInvokeRequest) async -> BridgeInvokeResponse {
|
||||||
@@ -169,63 +168,6 @@ actor MacNodeRuntime {
|
|||||||
let payload = try Self.encodePayload(["devices": devices])
|
let payload = try Self.encodePayload(["devices": devices])
|
||||||
return BridgeInvokeResponse(id: req.id, ok: true, payloadJSON: payload)
|
return BridgeInvokeResponse(id: req.id, ok: true, payloadJSON: payload)
|
||||||
|
|
||||||
case ClawdisLocationCommand.get.rawValue:
|
|
||||||
let mode = Self.locationMode()
|
|
||||||
guard mode != .off else {
|
|
||||||
return BridgeInvokeResponse(
|
|
||||||
id: req.id,
|
|
||||||
ok: false,
|
|
||||||
error: ClawdisNodeError(
|
|
||||||
code: .unavailable,
|
|
||||||
message: "LOCATION_DISABLED: enable Location in Settings"))
|
|
||||||
}
|
|
||||||
let params = (try? Self.decodeParams(ClawdisLocationGetParams.self, from: req.paramsJSON)) ??
|
|
||||||
ClawdisLocationGetParams()
|
|
||||||
let desired = params.desiredAccuracy ??
|
|
||||||
(Self.locationPreciseEnabled() ? .precise : .balanced)
|
|
||||||
let status = await self.locationService.authorizationStatus()
|
|
||||||
if status != .authorizedAlways && status != .authorizedWhenInUse {
|
|
||||||
return BridgeInvokeResponse(
|
|
||||||
id: req.id,
|
|
||||||
ok: false,
|
|
||||||
error: ClawdisNodeError(
|
|
||||||
code: .unavailable,
|
|
||||||
message: "LOCATION_PERMISSION_REQUIRED: grant Location permission"))
|
|
||||||
}
|
|
||||||
do {
|
|
||||||
let location = try await self.locationService.currentLocation(
|
|
||||||
desiredAccuracy: desired,
|
|
||||||
maxAgeMs: params.maxAgeMs,
|
|
||||||
timeoutMs: params.timeoutMs)
|
|
||||||
let isPrecise = await self.locationService.accuracyAuthorization() == .fullAccuracy
|
|
||||||
let payload = ClawdisLocationPayload(
|
|
||||||
lat: location.coordinate.latitude,
|
|
||||||
lon: location.coordinate.longitude,
|
|
||||||
accuracyMeters: location.horizontalAccuracy,
|
|
||||||
altitudeMeters: location.verticalAccuracy >= 0 ? location.altitude : nil,
|
|
||||||
speedMps: location.speed >= 0 ? location.speed : nil,
|
|
||||||
headingDeg: location.course >= 0 ? location.course : nil,
|
|
||||||
timestamp: ISO8601DateFormatter().string(from: location.timestamp),
|
|
||||||
isPrecise: isPrecise,
|
|
||||||
source: nil)
|
|
||||||
let json = try Self.encodePayload(payload)
|
|
||||||
return BridgeInvokeResponse(id: req.id, ok: true, payloadJSON: json)
|
|
||||||
} catch MacNodeLocationService.Error.timeout {
|
|
||||||
return BridgeInvokeResponse(
|
|
||||||
id: req.id,
|
|
||||||
ok: false,
|
|
||||||
error: ClawdisNodeError(
|
|
||||||
code: .unavailable,
|
|
||||||
message: "LOCATION_TIMEOUT: no fix in time"))
|
|
||||||
} catch {
|
|
||||||
return BridgeInvokeResponse(
|
|
||||||
id: req.id,
|
|
||||||
ok: false,
|
|
||||||
error: ClawdisNodeError(
|
|
||||||
code: .unavailable,
|
|
||||||
message: "LOCATION_UNAVAILABLE: \(error.localizedDescription)"))
|
|
||||||
}
|
|
||||||
|
|
||||||
case MacNodeScreenCommand.record.rawValue:
|
case MacNodeScreenCommand.record.rawValue:
|
||||||
let params = (try? Self.decodeParams(MacNodeScreenRecordParams.self, from: req.paramsJSON)) ??
|
let params = (try? Self.decodeParams(MacNodeScreenRecordParams.self, from: req.paramsJSON)) ??
|
||||||
MacNodeScreenRecordParams()
|
MacNodeScreenRecordParams()
|
||||||
@@ -471,16 +413,6 @@ actor MacNodeRuntime {
|
|||||||
UserDefaults.standard.object(forKey: cameraEnabledKey) as? Bool ?? false
|
UserDefaults.standard.object(forKey: cameraEnabledKey) as? Bool ?? false
|
||||||
}
|
}
|
||||||
|
|
||||||
private nonisolated static func locationMode() -> ClawdisLocationMode {
|
|
||||||
let raw = UserDefaults.standard.string(forKey: locationModeKey) ?? "off"
|
|
||||||
return ClawdisLocationMode(rawValue: raw) ?? .off
|
|
||||||
}
|
|
||||||
|
|
||||||
private nonisolated static func locationPreciseEnabled() -> Bool {
|
|
||||||
if UserDefaults.standard.object(forKey: locationPreciseKey) == nil { return true }
|
|
||||||
return UserDefaults.standard.bool(forKey: locationPreciseKey)
|
|
||||||
}
|
|
||||||
|
|
||||||
private static func errorResponse(
|
private static func errorResponse(
|
||||||
_ req: BridgeInvokeRequest,
|
_ req: BridgeInvokeRequest,
|
||||||
code: ClawdisNodeErrorCode,
|
code: ClawdisNodeErrorCode,
|
||||||
|
|||||||
@@ -6,25 +6,6 @@ import SwiftUI
|
|||||||
|
|
||||||
private let onboardingWizardLogger = Logger(subsystem: "com.clawdis", category: "onboarding.wizard")
|
private let onboardingWizardLogger = Logger(subsystem: "com.clawdis", category: "onboarding.wizard")
|
||||||
|
|
||||||
// MARK: - Swift 6 AnyCodable Bridging Helpers
|
|
||||||
// Bridge between ClawdisProtocol.AnyCodable and the local module to avoid
|
|
||||||
// Swift 6 strict concurrency type conflicts.
|
|
||||||
|
|
||||||
private typealias ProtocolAnyCodable = ClawdisProtocol.AnyCodable
|
|
||||||
|
|
||||||
private func bridgeToLocal(_ value: ProtocolAnyCodable) -> AnyCodable {
|
|
||||||
if let data = try? JSONEncoder().encode(value),
|
|
||||||
let decoded = try? JSONDecoder().decode(AnyCodable.self, from: data)
|
|
||||||
{
|
|
||||||
return decoded
|
|
||||||
}
|
|
||||||
return AnyCodable(value.value)
|
|
||||||
}
|
|
||||||
|
|
||||||
private func bridgeToLocal(_ value: ProtocolAnyCodable?) -> AnyCodable? {
|
|
||||||
value.map(bridgeToLocal)
|
|
||||||
}
|
|
||||||
|
|
||||||
@MainActor
|
@MainActor
|
||||||
@Observable
|
@Observable
|
||||||
final class OnboardingWizardModel {
|
final class OnboardingWizardModel {
|
||||||
@@ -304,11 +285,11 @@ struct OnboardingWizardStepView: View {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
let option = optionItems[selectedIndex].option
|
let option = optionItems[selectedIndex].option
|
||||||
onSubmit(bridgeToLocal(option.value) ?? AnyCodable(option.label))
|
onSubmit(option.value ?? AnyCodable(option.label))
|
||||||
case "multiselect":
|
case "multiselect":
|
||||||
let values = optionItems
|
let values = optionItems
|
||||||
.filter { selectedIndices.contains($0.index) }
|
.filter { selectedIndices.contains($0.index) }
|
||||||
.map { bridgeToLocal($0.option.value) ?? AnyCodable($0.option.label) }
|
.map { $0.option.value ?? AnyCodable($0.option.label) }
|
||||||
onSubmit(AnyCodable(values))
|
onSubmit(AnyCodable(values))
|
||||||
case "action":
|
case "action":
|
||||||
onSubmit(AnyCodable(true))
|
onSubmit(AnyCodable(true))
|
||||||
@@ -326,12 +307,12 @@ private struct WizardOptionItem: Identifiable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private struct WizardOption {
|
private struct WizardOption {
|
||||||
let value: ProtocolAnyCodable?
|
let value: AnyCodable?
|
||||||
let label: String
|
let label: String
|
||||||
let hint: String?
|
let hint: String?
|
||||||
}
|
}
|
||||||
|
|
||||||
private func decodeWizardStep(_ raw: [String: ProtocolAnyCodable]?) -> WizardStep? {
|
private func decodeWizardStep(_ raw: [String: AnyCodable]?) -> WizardStep? {
|
||||||
guard let raw else { return nil }
|
guard let raw else { return nil }
|
||||||
do {
|
do {
|
||||||
let data = try JSONEncoder().encode(raw)
|
let data = try JSONEncoder().encode(raw)
|
||||||
@@ -342,7 +323,7 @@ private func decodeWizardStep(_ raw: [String: ProtocolAnyCodable]?) -> WizardSte
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func parseWizardOptions(_ raw: [[String: ProtocolAnyCodable]]?) -> [WizardOption] {
|
private func parseWizardOptions(_ raw: [[String: AnyCodable]]?) -> [WizardOption] {
|
||||||
guard let raw else { return [] }
|
guard let raw else { return [] }
|
||||||
return raw.map { entry in
|
return raw.map { entry in
|
||||||
let value = entry["value"]
|
let value = entry["value"]
|
||||||
@@ -356,7 +337,7 @@ private func wizardStepType(_ step: WizardStep) -> String {
|
|||||||
(step.type.value as? String) ?? ""
|
(step.type.value as? String) ?? ""
|
||||||
}
|
}
|
||||||
|
|
||||||
private func anyCodableString(_ value: ProtocolAnyCodable?) -> String {
|
private func anyCodableString(_ value: AnyCodable?) -> String {
|
||||||
switch value?.value {
|
switch value?.value {
|
||||||
case let string as String:
|
case let string as String:
|
||||||
return string
|
return string
|
||||||
@@ -371,11 +352,11 @@ private func anyCodableString(_ value: ProtocolAnyCodable?) -> String {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func anyCodableStringValue(_ value: ProtocolAnyCodable?) -> String? {
|
private func anyCodableStringValue(_ value: AnyCodable?) -> String? {
|
||||||
value?.value as? String
|
value?.value as? String
|
||||||
}
|
}
|
||||||
|
|
||||||
private func anyCodableBool(_ value: ProtocolAnyCodable?) -> Bool {
|
private func anyCodableBool(_ value: AnyCodable?) -> Bool {
|
||||||
switch value?.value {
|
switch value?.value {
|
||||||
case let bool as Bool:
|
case let bool as Bool:
|
||||||
return bool
|
return bool
|
||||||
@@ -386,18 +367,18 @@ private func anyCodableBool(_ value: ProtocolAnyCodable?) -> Bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func anyCodableArray(_ value: ProtocolAnyCodable?) -> [ProtocolAnyCodable] {
|
private func anyCodableArray(_ value: AnyCodable?) -> [AnyCodable] {
|
||||||
switch value?.value {
|
switch value?.value {
|
||||||
case let arr as [ProtocolAnyCodable]:
|
case let arr as [AnyCodable]:
|
||||||
return arr
|
return arr
|
||||||
case let arr as [Any]:
|
case let arr as [Any]:
|
||||||
return arr.map { ProtocolAnyCodable($0) }
|
return arr.map { AnyCodable($0) }
|
||||||
default:
|
default:
|
||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func anyCodableEqual(_ lhs: ProtocolAnyCodable?, _ rhs: ProtocolAnyCodable?) -> Bool {
|
private func anyCodableEqual(_ lhs: AnyCodable?, _ rhs: AnyCodable?) -> Bool {
|
||||||
switch (lhs?.value, rhs?.value) {
|
switch (lhs?.value, rhs?.value) {
|
||||||
case let (l as String, r as String):
|
case let (l as String, r as String):
|
||||||
return l == r
|
return l == r
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import ApplicationServices
|
|||||||
import AVFoundation
|
import AVFoundation
|
||||||
import ClawdisIPC
|
import ClawdisIPC
|
||||||
import CoreGraphics
|
import CoreGraphics
|
||||||
import CoreLocation
|
|
||||||
import Foundation
|
import Foundation
|
||||||
import Observation
|
import Observation
|
||||||
import Speech
|
import Speech
|
||||||
@@ -34,8 +33,6 @@ enum PermissionManager {
|
|||||||
await self.ensureSpeechRecognition(interactive: interactive)
|
await self.ensureSpeechRecognition(interactive: interactive)
|
||||||
case .camera:
|
case .camera:
|
||||||
await self.ensureCamera(interactive: interactive)
|
await self.ensureCamera(interactive: interactive)
|
||||||
case .location:
|
|
||||||
await self.ensureLocation(interactive: interactive)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -137,25 +134,6 @@ enum PermissionManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static func ensureLocation(interactive: Bool) async -> Bool {
|
|
||||||
let status = CLLocationManager.authorizationStatus()
|
|
||||||
switch status {
|
|
||||||
case .authorizedAlways, .authorizedWhenInUse:
|
|
||||||
return true
|
|
||||||
case .notDetermined:
|
|
||||||
guard interactive else { return false }
|
|
||||||
let updated = await LocationPermissionRequester.shared.request(always: false)
|
|
||||||
return updated == .authorizedAlways || updated == .authorizedWhenInUse
|
|
||||||
case .denied, .restricted:
|
|
||||||
if interactive {
|
|
||||||
LocationPermissionHelper.openSettings()
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
@unknown default:
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static func voiceWakePermissionsGranted() -> Bool {
|
static func voiceWakePermissionsGranted() -> Bool {
|
||||||
let mic = AVCaptureDevice.authorizationStatus(for: .audio) == .authorized
|
let mic = AVCaptureDevice.authorizationStatus(for: .audio) == .authorized
|
||||||
let speech = SFSpeechRecognizer.authorizationStatus() == .authorized
|
let speech = SFSpeechRecognizer.authorizationStatus() == .authorized
|
||||||
@@ -198,9 +176,6 @@ enum PermissionManager {
|
|||||||
|
|
||||||
case .camera:
|
case .camera:
|
||||||
results[cap] = AVCaptureDevice.authorizationStatus(for: .video) == .authorized
|
results[cap] = AVCaptureDevice.authorizationStatus(for: .video) == .authorized
|
||||||
case .location:
|
|
||||||
let status = CLLocationManager.authorizationStatus()
|
|
||||||
results[cap] = status == .authorizedAlways || status == .authorizedWhenInUse
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return results
|
return results
|
||||||
@@ -252,54 +227,6 @@ enum CameraPermissionHelper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
enum LocationPermissionHelper {
|
|
||||||
static func openSettings() {
|
|
||||||
let candidates = [
|
|
||||||
"x-apple.systempreferences:com.apple.preference.security?Privacy_LocationServices",
|
|
||||||
"x-apple.systempreferences:com.apple.preference.security",
|
|
||||||
]
|
|
||||||
|
|
||||||
for candidate in candidates {
|
|
||||||
if let url = URL(string: candidate), NSWorkspace.shared.open(url) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@MainActor
|
|
||||||
final class LocationPermissionRequester: NSObject, CLLocationManagerDelegate {
|
|
||||||
static let shared = LocationPermissionRequester()
|
|
||||||
private let manager = CLLocationManager()
|
|
||||||
private var continuation: CheckedContinuation<CLAuthorizationStatus, Never>?
|
|
||||||
|
|
||||||
override init() {
|
|
||||||
super.init()
|
|
||||||
self.manager.delegate = self
|
|
||||||
}
|
|
||||||
|
|
||||||
func request(always: Bool) async -> CLAuthorizationStatus {
|
|
||||||
if always {
|
|
||||||
self.manager.requestAlwaysAuthorization()
|
|
||||||
} else {
|
|
||||||
self.manager.requestWhenInUseAuthorization()
|
|
||||||
}
|
|
||||||
return await withCheckedContinuation { cont in
|
|
||||||
self.continuation = cont
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// nonisolated for Swift 6 strict concurrency compatibility
|
|
||||||
nonisolated func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
|
|
||||||
let status = manager.authorizationStatus
|
|
||||||
Task { @MainActor in
|
|
||||||
guard let cont = self.continuation else { return }
|
|
||||||
self.continuation = nil
|
|
||||||
cont.resume(returning: status)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
enum AppleScriptPermission {
|
enum AppleScriptPermission {
|
||||||
private static let logger = Logger(subsystem: "com.clawdis", category: "AppleScriptPermission")
|
private static let logger = Logger(subsystem: "com.clawdis", category: "AppleScriptPermission")
|
||||||
|
|
||||||
|
|||||||
@@ -121,7 +121,6 @@ struct PermissionRow: View {
|
|||||||
case .microphone: "Microphone"
|
case .microphone: "Microphone"
|
||||||
case .speechRecognition: "Speech Recognition"
|
case .speechRecognition: "Speech Recognition"
|
||||||
case .camera: "Camera"
|
case .camera: "Camera"
|
||||||
case .location: "Location"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -135,7 +134,6 @@ struct PermissionRow: View {
|
|||||||
case .microphone: "Allow Voice Wake and audio capture"
|
case .microphone: "Allow Voice Wake and audio capture"
|
||||||
case .speechRecognition: "Transcribe Voice Wake trigger phrases on-device"
|
case .speechRecognition: "Transcribe Voice Wake trigger phrases on-device"
|
||||||
case .camera: "Capture photos and video from the camera"
|
case .camera: "Capture photos and video from the camera"
|
||||||
case .location: "Share location when requested by the agent"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -148,7 +146,6 @@ struct PermissionRow: View {
|
|||||||
case .microphone: "mic"
|
case .microphone: "mic"
|
||||||
case .speechRecognition: "waveform"
|
case .speechRecognition: "waveform"
|
||||||
case .camera: "camera"
|
case .camera: "camera"
|
||||||
case .location: "location"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -47,8 +47,6 @@
|
|||||||
<string>Clawdis captures the screen when the agent needs screenshots for context.</string>
|
<string>Clawdis captures the screen when the agent needs screenshots for context.</string>
|
||||||
<key>NSCameraUsageDescription</key>
|
<key>NSCameraUsageDescription</key>
|
||||||
<string>Clawdis can capture photos or short video clips when requested by the agent.</string>
|
<string>Clawdis can capture photos or short video clips when requested by the agent.</string>
|
||||||
<key>NSLocationUsageDescription</key>
|
|
||||||
<string>Clawdis can share your location when requested by the agent.</string>
|
|
||||||
<key>NSMicrophoneUsageDescription</key>
|
<key>NSMicrophoneUsageDescription</key>
|
||||||
<string>Clawdis needs the mic for Voice Wake tests and agent audio capture.</string>
|
<string>Clawdis needs the mic for Voice Wake tests and agent audio capture.</string>
|
||||||
<key>NSSpeechRecognitionUsageDescription</key>
|
<key>NSSpeechRecognitionUsageDescription</key>
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ public enum Capability: String, Codable, CaseIterable, Sendable {
|
|||||||
case microphone
|
case microphone
|
||||||
case speechRecognition
|
case speechRecognition
|
||||||
case camera
|
case camera
|
||||||
case location
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum CameraFacing: String, Codable, Sendable {
|
public enum CameraFacing: String, Codable, Sendable {
|
||||||
|
|||||||
@@ -340,7 +340,6 @@ public struct SendParams: Codable {
|
|||||||
public let to: String
|
public let to: String
|
||||||
public let message: String
|
public let message: String
|
||||||
public let mediaurl: String?
|
public let mediaurl: String?
|
||||||
public let gifplayback: Bool?
|
|
||||||
public let provider: String?
|
public let provider: String?
|
||||||
public let idempotencykey: String
|
public let idempotencykey: String
|
||||||
|
|
||||||
@@ -348,14 +347,12 @@ public struct SendParams: Codable {
|
|||||||
to: String,
|
to: String,
|
||||||
message: String,
|
message: String,
|
||||||
mediaurl: String?,
|
mediaurl: String?,
|
||||||
gifplayback: Bool?,
|
|
||||||
provider: String?,
|
provider: String?,
|
||||||
idempotencykey: String
|
idempotencykey: String
|
||||||
) {
|
) {
|
||||||
self.to = to
|
self.to = to
|
||||||
self.message = message
|
self.message = message
|
||||||
self.mediaurl = mediaurl
|
self.mediaurl = mediaurl
|
||||||
self.gifplayback = gifplayback
|
|
||||||
self.provider = provider
|
self.provider = provider
|
||||||
self.idempotencykey = idempotencykey
|
self.idempotencykey = idempotencykey
|
||||||
}
|
}
|
||||||
@@ -363,7 +360,6 @@ public struct SendParams: Codable {
|
|||||||
case to
|
case to
|
||||||
case message
|
case message
|
||||||
case mediaurl = "mediaUrl"
|
case mediaurl = "mediaUrl"
|
||||||
case gifplayback = "gifPlayback"
|
|
||||||
case provider
|
case provider
|
||||||
case idempotencykey = "idempotencyKey"
|
case idempotencykey = "idempotencyKey"
|
||||||
}
|
}
|
||||||
@@ -378,8 +374,6 @@ public struct AgentParams: Codable {
|
|||||||
public let deliver: Bool?
|
public let deliver: Bool?
|
||||||
public let channel: String?
|
public let channel: String?
|
||||||
public let timeout: Int?
|
public let timeout: Int?
|
||||||
public let lane: String?
|
|
||||||
public let extrasystemprompt: String?
|
|
||||||
public let idempotencykey: String
|
public let idempotencykey: String
|
||||||
|
|
||||||
public init(
|
public init(
|
||||||
@@ -391,8 +385,6 @@ public struct AgentParams: Codable {
|
|||||||
deliver: Bool?,
|
deliver: Bool?,
|
||||||
channel: String?,
|
channel: String?,
|
||||||
timeout: Int?,
|
timeout: Int?,
|
||||||
lane: String?,
|
|
||||||
extrasystemprompt: String?,
|
|
||||||
idempotencykey: String
|
idempotencykey: String
|
||||||
) {
|
) {
|
||||||
self.message = message
|
self.message = message
|
||||||
@@ -403,8 +395,6 @@ public struct AgentParams: Codable {
|
|||||||
self.deliver = deliver
|
self.deliver = deliver
|
||||||
self.channel = channel
|
self.channel = channel
|
||||||
self.timeout = timeout
|
self.timeout = timeout
|
||||||
self.lane = lane
|
|
||||||
self.extrasystemprompt = extrasystemprompt
|
|
||||||
self.idempotencykey = idempotencykey
|
self.idempotencykey = idempotencykey
|
||||||
}
|
}
|
||||||
private enum CodingKeys: String, CodingKey {
|
private enum CodingKeys: String, CodingKey {
|
||||||
@@ -416,33 +406,10 @@ public struct AgentParams: Codable {
|
|||||||
case deliver
|
case deliver
|
||||||
case channel
|
case channel
|
||||||
case timeout
|
case timeout
|
||||||
case lane
|
|
||||||
case extrasystemprompt = "extraSystemPrompt"
|
|
||||||
case idempotencykey = "idempotencyKey"
|
case idempotencykey = "idempotencyKey"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct AgentWaitParams: Codable {
|
|
||||||
public let runid: String
|
|
||||||
public let afterms: Int?
|
|
||||||
public let timeoutms: Int?
|
|
||||||
|
|
||||||
public init(
|
|
||||||
runid: String,
|
|
||||||
afterms: Int?,
|
|
||||||
timeoutms: Int?
|
|
||||||
) {
|
|
||||||
self.runid = runid
|
|
||||||
self.afterms = afterms
|
|
||||||
self.timeoutms = timeoutms
|
|
||||||
}
|
|
||||||
private enum CodingKeys: String, CodingKey {
|
|
||||||
case runid = "runId"
|
|
||||||
case afterms = "afterMs"
|
|
||||||
case timeoutms = "timeoutMs"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public struct WakeParams: Codable {
|
public struct WakeParams: Codable {
|
||||||
public let mode: AnyCodable
|
public let mode: AnyCodable
|
||||||
public let text: String
|
public let text: String
|
||||||
@@ -647,7 +614,6 @@ public struct SessionsPatchParams: Codable {
|
|||||||
public let thinkinglevel: AnyCodable?
|
public let thinkinglevel: AnyCodable?
|
||||||
public let verboselevel: AnyCodable?
|
public let verboselevel: AnyCodable?
|
||||||
public let model: AnyCodable?
|
public let model: AnyCodable?
|
||||||
public let sendpolicy: AnyCodable?
|
|
||||||
public let groupactivation: AnyCodable?
|
public let groupactivation: AnyCodable?
|
||||||
|
|
||||||
public init(
|
public init(
|
||||||
@@ -655,14 +621,12 @@ public struct SessionsPatchParams: Codable {
|
|||||||
thinkinglevel: AnyCodable?,
|
thinkinglevel: AnyCodable?,
|
||||||
verboselevel: AnyCodable?,
|
verboselevel: AnyCodable?,
|
||||||
model: AnyCodable?,
|
model: AnyCodable?,
|
||||||
sendpolicy: AnyCodable?,
|
|
||||||
groupactivation: AnyCodable?
|
groupactivation: AnyCodable?
|
||||||
) {
|
) {
|
||||||
self.key = key
|
self.key = key
|
||||||
self.thinkinglevel = thinkinglevel
|
self.thinkinglevel = thinkinglevel
|
||||||
self.verboselevel = verboselevel
|
self.verboselevel = verboselevel
|
||||||
self.model = model
|
self.model = model
|
||||||
self.sendpolicy = sendpolicy
|
|
||||||
self.groupactivation = groupactivation
|
self.groupactivation = groupactivation
|
||||||
}
|
}
|
||||||
private enum CodingKeys: String, CodingKey {
|
private enum CodingKeys: String, CodingKey {
|
||||||
@@ -670,7 +634,6 @@ public struct SessionsPatchParams: Codable {
|
|||||||
case thinkinglevel = "thinkingLevel"
|
case thinkinglevel = "thinkingLevel"
|
||||||
case verboselevel = "verboseLevel"
|
case verboselevel = "verboseLevel"
|
||||||
case model
|
case model
|
||||||
case sendpolicy = "sendPolicy"
|
|
||||||
case groupactivation = "groupActivation"
|
case groupactivation = "groupActivation"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -871,7 +834,7 @@ public struct WizardStep: Codable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct WizardNextResult: Codable, Sendable {
|
public struct WizardNextResult: Codable {
|
||||||
public let done: Bool
|
public let done: Bool
|
||||||
public let step: [String: AnyCodable]?
|
public let step: [String: AnyCodable]?
|
||||||
public let status: AnyCodable?
|
public let status: AnyCodable?
|
||||||
@@ -896,7 +859,7 @@ public struct WizardNextResult: Codable, Sendable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct WizardStartResult: Codable, Sendable {
|
public struct WizardStartResult: Codable {
|
||||||
public let sessionid: String
|
public let sessionid: String
|
||||||
public let done: Bool
|
public let done: Bool
|
||||||
public let step: [String: AnyCodable]?
|
public let step: [String: AnyCodable]?
|
||||||
@@ -925,7 +888,7 @@ public struct WizardStartResult: Codable, Sendable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct WizardStatusResult: Codable, Sendable {
|
public struct WizardStatusResult: Codable {
|
||||||
public let status: AnyCodable
|
public let status: AnyCodable
|
||||||
public let error: String?
|
public let error: String?
|
||||||
|
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ import Testing
|
|||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
let key = try GatewayConnection.mainSessionKey(fromConfigGetData: Data(json.utf8))
|
let key = try GatewayConnection.mainSessionKey(fromConfigGetData: Data(json.utf8))
|
||||||
#expect(key == "main")
|
#expect(key == "primary")
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test func configGetSnapshotMainKeyFallsBackWhenEmptyOrWhitespace() throws {
|
@Test func configGetSnapshotMainKeyFallsBackWhenEmptyOrWhitespace() throws {
|
||||||
@@ -54,14 +54,4 @@ import Testing
|
|||||||
let key = try GatewayConnection.mainSessionKey(fromConfigGetData: Data(json.utf8))
|
let key = try GatewayConnection.mainSessionKey(fromConfigGetData: Data(json.utf8))
|
||||||
#expect(key == "main")
|
#expect(key == "main")
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test func configGetSnapshotUsesGlobalScope() throws {
|
|
||||||
let json = """
|
|
||||||
{
|
|
||||||
"config": { "session": { "scope": "global" } }
|
|
||||||
}
|
|
||||||
"""
|
|
||||||
let key = try GatewayConnection.mainSessionKey(fromConfigGetData: Data(json.utf8))
|
|
||||||
#expect(key == "global")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,139 +0,0 @@
|
|||||||
import Foundation
|
|
||||||
|
|
||||||
struct AssistantTextSegment: Identifiable {
|
|
||||||
enum Kind {
|
|
||||||
case thinking
|
|
||||||
case response
|
|
||||||
}
|
|
||||||
|
|
||||||
let id = UUID()
|
|
||||||
let kind: Kind
|
|
||||||
let text: String
|
|
||||||
}
|
|
||||||
|
|
||||||
enum AssistantTextParser {
|
|
||||||
static func segments(from raw: String) -> [AssistantTextSegment] {
|
|
||||||
let trimmed = raw.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
||||||
guard !trimmed.isEmpty else { return [] }
|
|
||||||
guard raw.contains("<") else {
|
|
||||||
return [AssistantTextSegment(kind: .response, text: trimmed)]
|
|
||||||
}
|
|
||||||
|
|
||||||
var segments: [AssistantTextSegment] = []
|
|
||||||
var cursor = raw.startIndex
|
|
||||||
var currentKind: AssistantTextSegment.Kind = .response
|
|
||||||
var matchedTag = false
|
|
||||||
|
|
||||||
while let match = self.nextTag(in: raw, from: cursor) {
|
|
||||||
matchedTag = true
|
|
||||||
if match.range.lowerBound > cursor {
|
|
||||||
self.appendSegment(kind: currentKind, text: raw[cursor..<match.range.lowerBound], to: &segments)
|
|
||||||
}
|
|
||||||
|
|
||||||
guard let tagEnd = raw.range(of: ">", range: match.range.upperBound..<raw.endIndex) else {
|
|
||||||
cursor = raw.endIndex
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
let isSelfClosing = self.isSelfClosingTag(in: raw, tagEnd: tagEnd)
|
|
||||||
cursor = tagEnd.upperBound
|
|
||||||
if isSelfClosing { continue }
|
|
||||||
|
|
||||||
if match.closing {
|
|
||||||
currentKind = .response
|
|
||||||
} else {
|
|
||||||
currentKind = match.kind == .think ? .thinking : .response
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if cursor < raw.endIndex {
|
|
||||||
self.appendSegment(kind: currentKind, text: raw[cursor..<raw.endIndex], to: &segments)
|
|
||||||
}
|
|
||||||
|
|
||||||
guard matchedTag else {
|
|
||||||
return [AssistantTextSegment(kind: .response, text: trimmed)]
|
|
||||||
}
|
|
||||||
|
|
||||||
return segments
|
|
||||||
}
|
|
||||||
|
|
||||||
static func hasVisibleContent(in raw: String) -> Bool {
|
|
||||||
!self.segments(from: raw).isEmpty
|
|
||||||
}
|
|
||||||
|
|
||||||
private enum TagKind {
|
|
||||||
case think
|
|
||||||
case final
|
|
||||||
}
|
|
||||||
|
|
||||||
private struct TagMatch {
|
|
||||||
let kind: TagKind
|
|
||||||
let closing: Bool
|
|
||||||
let range: Range<String.Index>
|
|
||||||
}
|
|
||||||
|
|
||||||
private static func nextTag(in text: String, from start: String.Index) -> TagMatch? {
|
|
||||||
let candidates: [TagMatch] = [
|
|
||||||
self.findTagStart(tag: "think", closing: false, in: text, from: start).map {
|
|
||||||
TagMatch(kind: .think, closing: false, range: $0)
|
|
||||||
},
|
|
||||||
self.findTagStart(tag: "think", closing: true, in: text, from: start).map {
|
|
||||||
TagMatch(kind: .think, closing: true, range: $0)
|
|
||||||
},
|
|
||||||
self.findTagStart(tag: "final", closing: false, in: text, from: start).map {
|
|
||||||
TagMatch(kind: .final, closing: false, range: $0)
|
|
||||||
},
|
|
||||||
self.findTagStart(tag: "final", closing: true, in: text, from: start).map {
|
|
||||||
TagMatch(kind: .final, closing: true, range: $0)
|
|
||||||
},
|
|
||||||
].compactMap { $0 }
|
|
||||||
|
|
||||||
return candidates.min { $0.range.lowerBound < $1.range.lowerBound }
|
|
||||||
}
|
|
||||||
|
|
||||||
private static func findTagStart(
|
|
||||||
tag: String,
|
|
||||||
closing: Bool,
|
|
||||||
in text: String,
|
|
||||||
from start: String.Index) -> Range<String.Index>?
|
|
||||||
{
|
|
||||||
let token = closing ? "</\(tag)" : "<\(tag)"
|
|
||||||
var searchRange = start..<text.endIndex
|
|
||||||
while let range = text.range(
|
|
||||||
of: token,
|
|
||||||
options: [.caseInsensitive, .diacriticInsensitive],
|
|
||||||
range: searchRange)
|
|
||||||
{
|
|
||||||
let boundaryIndex = range.upperBound
|
|
||||||
guard boundaryIndex < text.endIndex else { return range }
|
|
||||||
let boundary = text[boundaryIndex]
|
|
||||||
let isBoundary = boundary == ">" || boundary.isWhitespace || (!closing && boundary == "/")
|
|
||||||
if isBoundary {
|
|
||||||
return range
|
|
||||||
}
|
|
||||||
searchRange = boundaryIndex..<text.endIndex
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
private static func isSelfClosingTag(in text: String, tagEnd: Range<String.Index>) -> Bool {
|
|
||||||
var cursor = tagEnd.lowerBound
|
|
||||||
while cursor > text.startIndex {
|
|
||||||
cursor = text.index(before: cursor)
|
|
||||||
let char = text[cursor]
|
|
||||||
if char.isWhitespace { continue }
|
|
||||||
return char == "/"
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
private static func appendSegment(
|
|
||||||
kind: AssistantTextSegment.Kind,
|
|
||||||
text: Substring,
|
|
||||||
to segments: inout [AssistantTextSegment])
|
|
||||||
{
|
|
||||||
let trimmed = text.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
||||||
guard !trimmed.isEmpty else { return }
|
|
||||||
segments.append(AssistantTextSegment(kind: kind, text: trimmed))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -158,6 +158,7 @@ private struct ChatMessageBody: View {
|
|||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
let text = self.primaryText
|
let text = self.primaryText
|
||||||
|
let split = ChatMarkdownSplitter.split(markdown: text)
|
||||||
let textColor = self.isUser ? ClawdisChatTheme.userText : ClawdisChatTheme.assistantText
|
let textColor = self.isUser ? ClawdisChatTheme.userText : ClawdisChatTheme.assistantText
|
||||||
|
|
||||||
VStack(alignment: .leading, spacing: 10) {
|
VStack(alignment: .leading, spacing: 10) {
|
||||||
@@ -168,40 +169,37 @@ private struct ChatMessageBody: View {
|
|||||||
text: text,
|
text: text,
|
||||||
isUser: self.isUser)
|
isUser: self.isUser)
|
||||||
}
|
}
|
||||||
} else if self.isUser {
|
} else {
|
||||||
let split = ChatMarkdownSplitter.split(markdown: text)
|
|
||||||
ForEach(split.blocks) { block in
|
ForEach(split.blocks) { block in
|
||||||
switch block.kind {
|
switch block.kind {
|
||||||
case .text:
|
case .text:
|
||||||
MarkdownTextView(text: block.text, textColor: textColor, font: .system(size: 14))
|
MarkdownTextView(text: block.text, textColor: textColor)
|
||||||
case let .code(language):
|
case let .code(language):
|
||||||
CodeBlockView(code: block.text, language: language, isUser: self.isUser)
|
CodeBlockView(code: block.text, language: language, isUser: self.isUser)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if !split.images.isEmpty {
|
if !split.images.isEmpty {
|
||||||
ForEach(
|
ForEach(
|
||||||
split.images,
|
split.images,
|
||||||
id: \ChatMarkdownSplitter.InlineImage.id)
|
id: \ChatMarkdownSplitter.InlineImage.id)
|
||||||
{ (item: ChatMarkdownSplitter.InlineImage) in
|
{ (item: ChatMarkdownSplitter.InlineImage) in
|
||||||
if let img = item.image {
|
if let img = item.image {
|
||||||
ClawdisPlatformImageFactory.image(img)
|
ClawdisPlatformImageFactory.image(img)
|
||||||
.resizable()
|
.resizable()
|
||||||
.scaledToFit()
|
.scaledToFit()
|
||||||
.frame(maxHeight: 260)
|
.frame(maxHeight: 260)
|
||||||
.clipShape(RoundedRectangle(cornerRadius: 12, style: .continuous))
|
.clipShape(RoundedRectangle(cornerRadius: 12, style: .continuous))
|
||||||
.overlay(
|
.overlay(
|
||||||
RoundedRectangle(cornerRadius: 12, style: .continuous)
|
RoundedRectangle(cornerRadius: 12, style: .continuous)
|
||||||
.strokeBorder(Color.white.opacity(0.12), lineWidth: 1))
|
.strokeBorder(Color.white.opacity(0.12), lineWidth: 1))
|
||||||
} else {
|
} else {
|
||||||
Text(item.label.isEmpty ? "Image" : item.label)
|
Text(item.label.isEmpty ? "Image" : item.label)
|
||||||
.font(.footnote)
|
.font(.footnote)
|
||||||
.foregroundStyle(.secondary)
|
.foregroundStyle(.secondary)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
ChatAssistantTextBody(text: text)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if !self.inlineAttachments.isEmpty {
|
if !self.inlineAttachments.isEmpty {
|
||||||
@@ -508,7 +506,7 @@ struct ChatStreamingAssistantBubble: View {
|
|||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
VStack(alignment: .leading, spacing: 10) {
|
VStack(alignment: .leading, spacing: 10) {
|
||||||
ChatAssistantTextBody(text: self.text)
|
ChatMarkdownBody(text: self.text, textColor: ClawdisChatTheme.assistantText)
|
||||||
}
|
}
|
||||||
.padding(12)
|
.padding(12)
|
||||||
.background(
|
.background(
|
||||||
@@ -616,7 +614,6 @@ private struct TypingDots: View {
|
|||||||
private struct MarkdownTextView: View {
|
private struct MarkdownTextView: View {
|
||||||
let text: String
|
let text: String
|
||||||
let textColor: Color
|
let textColor: Color
|
||||||
let font: Font
|
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
let normalized = self.text.replacingOccurrences(
|
let normalized = self.text.replacingOccurrences(
|
||||||
@@ -627,36 +624,20 @@ private struct MarkdownTextView: View {
|
|||||||
interpretedSyntax: .inlineOnlyPreservingWhitespace)
|
interpretedSyntax: .inlineOnlyPreservingWhitespace)
|
||||||
if let attributed = try? AttributedString(markdown: normalized, options: options) {
|
if let attributed = try? AttributedString(markdown: normalized, options: options) {
|
||||||
Text(attributed)
|
Text(attributed)
|
||||||
.font(self.font)
|
.font(.system(size: 14))
|
||||||
.foregroundStyle(self.textColor)
|
.foregroundStyle(self.textColor)
|
||||||
} else {
|
} else {
|
||||||
Text(normalized)
|
Text(normalized)
|
||||||
.font(self.font)
|
.font(.system(size: 14))
|
||||||
.foregroundStyle(self.textColor)
|
.foregroundStyle(self.textColor)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@MainActor
|
|
||||||
private struct ChatAssistantTextBody: View {
|
|
||||||
let text: String
|
|
||||||
|
|
||||||
var body: some View {
|
|
||||||
let segments = AssistantTextParser.segments(from: self.text)
|
|
||||||
VStack(alignment: .leading, spacing: 10) {
|
|
||||||
ForEach(segments) { segment in
|
|
||||||
let font = segment.kind == .thinking ? Font.system(size: 14).italic() : Font.system(size: 14)
|
|
||||||
ChatMarkdownBody(text: segment.text, textColor: ClawdisChatTheme.assistantText, font: font)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@MainActor
|
@MainActor
|
||||||
private struct ChatMarkdownBody: View {
|
private struct ChatMarkdownBody: View {
|
||||||
let text: String
|
let text: String
|
||||||
let textColor: Color
|
let textColor: Color
|
||||||
let font: Font
|
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
let split = ChatMarkdownSplitter.split(markdown: self.text)
|
let split = ChatMarkdownSplitter.split(markdown: self.text)
|
||||||
@@ -664,7 +645,7 @@ private struct ChatMarkdownBody: View {
|
|||||||
ForEach(split.blocks) { block in
|
ForEach(split.blocks) { block in
|
||||||
switch block.kind {
|
switch block.kind {
|
||||||
case .text:
|
case .text:
|
||||||
MarkdownTextView(text: block.text, textColor: self.textColor, font: self.font)
|
MarkdownTextView(text: block.text, textColor: self.textColor)
|
||||||
case let .code(language):
|
case let .code(language):
|
||||||
CodeBlockView(code: block.text, language: language, isUser: false)
|
CodeBlockView(code: block.text, language: language, isUser: false)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -169,7 +169,7 @@ public struct ClawdisChatView: View {
|
|||||||
.frame(maxWidth: .infinity, alignment: .leading)
|
.frame(maxWidth: .infinity, alignment: .leading)
|
||||||
}
|
}
|
||||||
|
|
||||||
if let text = self.viewModel.streamingAssistantText, AssistantTextParser.hasVisibleContent(in: text) {
|
if let text = self.viewModel.streamingAssistantText, !text.isEmpty {
|
||||||
ChatStreamingAssistantBubble(text: text)
|
ChatStreamingAssistantBubble(text: text)
|
||||||
.frame(maxWidth: .infinity, alignment: .leading)
|
.frame(maxWidth: .infinity, alignment: .leading)
|
||||||
}
|
}
|
||||||
@@ -246,7 +246,7 @@ public struct ClawdisChatView: View {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if let text = self.viewModel.streamingAssistantText,
|
if let text = self.viewModel.streamingAssistantText,
|
||||||
AssistantTextParser.hasVisibleContent(in: text)
|
!text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
|
||||||
{
|
{
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@@ -261,7 +261,7 @@ public struct ClawdisChatView: View {
|
|||||||
|
|
||||||
private var showsEmptyState: Bool {
|
private var showsEmptyState: Bool {
|
||||||
self.viewModel.messages.isEmpty &&
|
self.viewModel.messages.isEmpty &&
|
||||||
!(self.viewModel.streamingAssistantText.map { AssistantTextParser.hasVisibleContent(in: $0) } ?? false) &&
|
(self.viewModel.streamingAssistantText?.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty ?? true) &&
|
||||||
self.viewModel.pendingRunCount == 0 &&
|
self.viewModel.pendingRunCount == 0 &&
|
||||||
self.viewModel.pendingToolCalls.isEmpty
|
self.viewModel.pendingToolCalls.isEmpty
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,5 +5,4 @@ public enum ClawdisCapability: String, Codable, Sendable {
|
|||||||
case camera
|
case camera
|
||||||
case screen
|
case screen
|
||||||
case voiceWake
|
case voiceWake
|
||||||
case location
|
|
||||||
}
|
}
|
||||||
@@ -1,57 +0,0 @@
|
|||||||
import Foundation
|
|
||||||
|
|
||||||
public enum ClawdisLocationCommand: String, Codable, Sendable {
|
|
||||||
case get = "location.get"
|
|
||||||
}
|
|
||||||
|
|
||||||
public enum ClawdisLocationAccuracy: String, Codable, Sendable {
|
|
||||||
case coarse
|
|
||||||
case balanced
|
|
||||||
case precise
|
|
||||||
}
|
|
||||||
|
|
||||||
public struct ClawdisLocationGetParams: Codable, Sendable, Equatable {
|
|
||||||
public var timeoutMs: Int?
|
|
||||||
public var maxAgeMs: Int?
|
|
||||||
public var desiredAccuracy: ClawdisLocationAccuracy?
|
|
||||||
|
|
||||||
public init(timeoutMs: Int? = nil, maxAgeMs: Int? = nil, desiredAccuracy: ClawdisLocationAccuracy? = nil) {
|
|
||||||
self.timeoutMs = timeoutMs
|
|
||||||
self.maxAgeMs = maxAgeMs
|
|
||||||
self.desiredAccuracy = desiredAccuracy
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public struct ClawdisLocationPayload: Codable, Sendable, Equatable {
|
|
||||||
public var lat: Double
|
|
||||||
public var lon: Double
|
|
||||||
public var accuracyMeters: Double
|
|
||||||
public var altitudeMeters: Double?
|
|
||||||
public var speedMps: Double?
|
|
||||||
public var headingDeg: Double?
|
|
||||||
public var timestamp: String
|
|
||||||
public var isPrecise: Bool
|
|
||||||
public var source: String?
|
|
||||||
|
|
||||||
public init(
|
|
||||||
lat: Double,
|
|
||||||
lon: Double,
|
|
||||||
accuracyMeters: Double,
|
|
||||||
altitudeMeters: Double? = nil,
|
|
||||||
speedMps: Double? = nil,
|
|
||||||
headingDeg: Double? = nil,
|
|
||||||
timestamp: String,
|
|
||||||
isPrecise: Bool,
|
|
||||||
source: String? = nil)
|
|
||||||
{
|
|
||||||
self.lat = lat
|
|
||||||
self.lon = lon
|
|
||||||
self.accuracyMeters = accuracyMeters
|
|
||||||
self.altitudeMeters = altitudeMeters
|
|
||||||
self.speedMps = speedMps
|
|
||||||
self.headingDeg = headingDeg
|
|
||||||
self.timestamp = timestamp
|
|
||||||
self.isPrecise = isPrecise
|
|
||||||
self.source = source
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
import Foundation
|
|
||||||
|
|
||||||
public enum ClawdisLocationMode: String, Codable, Sendable, CaseIterable {
|
|
||||||
case off
|
|
||||||
case whileUsing
|
|
||||||
case always
|
|
||||||
}
|
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
@testable import ClawdisChatUI
|
|
||||||
import Testing
|
|
||||||
|
|
||||||
@Suite struct AssistantTextParserTests {
|
|
||||||
@Test func splitsThinkAndFinalSegments() {
|
|
||||||
let segments = AssistantTextParser.segments(
|
|
||||||
from: "<think>internal</think>\n\n<final>Hello there</final>")
|
|
||||||
|
|
||||||
#expect(segments.count == 2)
|
|
||||||
#expect(segments[0].kind == .thinking)
|
|
||||||
#expect(segments[0].text == "internal")
|
|
||||||
#expect(segments[1].kind == .response)
|
|
||||||
#expect(segments[1].text == "Hello there")
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test func keepsTextWithoutTags() {
|
|
||||||
let segments = AssistantTextParser.segments(from: "Just text.")
|
|
||||||
|
|
||||||
#expect(segments.count == 1)
|
|
||||||
#expect(segments[0].kind == .response)
|
|
||||||
#expect(segments[0].text == "Just text.")
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test func ignoresThinkingLikeTags() {
|
|
||||||
let raw = "<thinking>example</thinking>\nKeep this."
|
|
||||||
let segments = AssistantTextParser.segments(from: raw)
|
|
||||||
|
|
||||||
#expect(segments.count == 1)
|
|
||||||
#expect(segments[0].kind == .response)
|
|
||||||
#expect(segments[0].text == raw.trimmingCharacters(in: .whitespacesAndNewlines))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test func dropsEmptyTaggedContent() {
|
|
||||||
let segments = AssistantTextParser.segments(from: "<think></think>")
|
|
||||||
#expect(segments.isEmpty)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+1
-1
@@ -11,7 +11,7 @@ read_when:
|
|||||||
- Required: `--message <text>`
|
- Required: `--message <text>`
|
||||||
- Session selection:
|
- Session selection:
|
||||||
- If `--session-id` is given, reuse it.
|
- If `--session-id` is given, reuse it.
|
||||||
- Else if `--to <e164>` is given, derive the session key from `session.scope` (direct chats collapse to `main`, or `global` when scope is global).
|
- Else if `--to <e164>` is given, derive the session key from `session.scope` (direct chats collapse to `session.mainKey`).
|
||||||
- Runs the embedded Pi agent (configured via `agent`).
|
- Runs the embedded Pi agent (configured via `agent`).
|
||||||
- Thinking/verbose:
|
- Thinking/verbose:
|
||||||
- Flags `--thinking <off|minimal|low|medium|high>` and `--verbose <on|off>` persist into the session store.
|
- Flags `--thinking <off|minimal|low|medium|high>` and `--verbose <on|off>` persist into the session store.
|
||||||
|
|||||||
@@ -13,10 +13,10 @@ Clawdis runs shell commands through the `bash` tool and keeps long‑running tas
|
|||||||
|
|
||||||
Key parameters:
|
Key parameters:
|
||||||
- `command` (required)
|
- `command` (required)
|
||||||
- `yieldMs` (default 10000): auto‑background after this delay
|
- `yieldMs` (default 20000): auto‑background after this delay
|
||||||
- `background` (bool): background immediately
|
- `background` (bool): background immediately
|
||||||
- `timeout` (seconds, default 1800): kill the process after this timeout
|
- `timeout` (seconds, default 1800): kill the process after this timeout
|
||||||
- Need a real TTY? Use the tmux skill.
|
- `stdinMode` (`pipe` | `pty`): use a real TTY when `pty` is requested and node-pty loads (otherwise warns + falls back)
|
||||||
- `workdir`, `env`
|
- `workdir`, `env`
|
||||||
|
|
||||||
Behavior:
|
Behavior:
|
||||||
@@ -30,7 +30,7 @@ Environment overrides:
|
|||||||
- `PI_BASH_JOB_TTL_MS`: TTL for finished sessions (ms, bounded to 1m–3h)
|
- `PI_BASH_JOB_TTL_MS`: TTL for finished sessions (ms, bounded to 1m–3h)
|
||||||
|
|
||||||
Config (preferred):
|
Config (preferred):
|
||||||
- `agent.bash.backgroundMs` (default 10000)
|
- `agent.bash.backgroundMs` (default 20000)
|
||||||
- `agent.bash.timeoutSec` (default 1800)
|
- `agent.bash.timeoutSec` (default 1800)
|
||||||
- `agent.bash.cleanupMs` (default 1800000)
|
- `agent.bash.cleanupMs` (default 1800000)
|
||||||
|
|
||||||
|
|||||||
+15
-2
@@ -12,10 +12,18 @@ Run shell commands in the workspace. Supports foreground + background execution
|
|||||||
## Parameters
|
## Parameters
|
||||||
|
|
||||||
- `command` (required)
|
- `command` (required)
|
||||||
- `yieldMs` (default 10000): auto-background after delay
|
- `yieldMs` (default 20000): auto-background after delay
|
||||||
- `background` (bool): background immediately
|
- `background` (bool): background immediately
|
||||||
- `timeout` (seconds, default 1800): kill on expiry
|
- `timeout` (seconds, default 1800): kill on expiry
|
||||||
- Need a real TTY? Use the tmux skill.
|
- `stdinMode` (`pipe` | `pty`):
|
||||||
|
- `pipe` (default): classic stdin/stdout/stderr pipes
|
||||||
|
- `pty`: real TTY via node-pty (merged stdout/stderr)
|
||||||
|
|
||||||
|
## TTY mode (`stdinMode: "pty"`)
|
||||||
|
|
||||||
|
- Uses node-pty if available. If node-pty fails to load/start, the tool warns and falls back to `pipe`.
|
||||||
|
- Output streams are merged (no separate stderr).
|
||||||
|
- `process write` sends raw input; `eof: true` sends Ctrl-D (`\x04`).
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
@@ -29,3 +37,8 @@ Background + poll:
|
|||||||
{"tool":"bash","command":"npm run build","yieldMs":1000}
|
{"tool":"bash","command":"npm run build","yieldMs":1000}
|
||||||
{"tool":"process","action":"poll","sessionId":"<id>"}
|
{"tool":"process","action":"poll","sessionId":"<id>"}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
TTY command:
|
||||||
|
```json
|
||||||
|
{"tool":"bash","command":"htop","stdinMode":"pty","background":true}
|
||||||
|
```
|
||||||
+2
-1
@@ -138,7 +138,8 @@ Example:
|
|||||||
session: {
|
session: {
|
||||||
scope: "per-sender",
|
scope: "per-sender",
|
||||||
resetTriggers: ["/new", "/reset"],
|
resetTriggers: ["/new", "/reset"],
|
||||||
idleMinutes: 10080
|
idleMinutes: 10080,
|
||||||
|
mainKey: "main"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -31,9 +31,6 @@ Author: steipete · Status: draft spec · Date: 2025-12-20
|
|||||||
- The gateway port is configurable via `gateway.port` or `CLAWDIS_GATEWAY_PORT` (default 18789). The mac app reads that value for launchd, probes, and remote SSH tunnels.
|
- The gateway port is configurable via `gateway.port` or `CLAWDIS_GATEWAY_PORT` (default 18789). The mac app reads that value for launchd, probes, and remote SSH tunnels.
|
||||||
- The mac app connects to the bridge as a **node** and advertises capabilities/commands.
|
- The mac app connects to the bridge as a **node** and advertises capabilities/commands.
|
||||||
- Agent‑facing actions are exposed via `node.invoke` (no local control socket).
|
- Agent‑facing actions are exposed via `node.invoke` (no local control socket).
|
||||||
- The mac app watches `~/.clawdis/clawdis.json` and switches modes live when `gateway.mode` or `gateway.remote.url` changes.
|
|
||||||
- If `gateway.mode` is unset but `gateway.remote.url` is set, the mac app treats it as remote mode.
|
|
||||||
- Changing connection mode in the mac app writes `gateway.mode` (and `gateway.remote.url` in remote mode) back to the config file.
|
|
||||||
|
|
||||||
### Node commands (mac)
|
### Node commands (mac)
|
||||||
- Canvas: `canvas.present|navigate|eval|snapshot|a2ui.*`
|
- Canvas: `canvas.present|navigate|eval|snapshot|a2ui.*`
|
||||||
|
|||||||
+3
-139
@@ -33,11 +33,6 @@ better forms without hard-coding config knowledge.
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Build the default image once with:
|
|
||||||
```bash
|
|
||||||
scripts/sandbox-setup.sh
|
|
||||||
```
|
|
||||||
|
|
||||||
## Self-chat mode (recommended for group control)
|
## Self-chat mode (recommended for group control)
|
||||||
|
|
||||||
To prevent the bot from responding to WhatsApp @-mentions in groups (only respond to specific text triggers):
|
To prevent the bot from responding to WhatsApp @-mentions in groups (only respond to specific text triggers):
|
||||||
@@ -328,9 +323,6 @@ Default: `~/clawd`.
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
If `agent.sandbox` is enabled, non-main sessions can override this with their
|
|
||||||
own per-session workspaces under `agent.sandbox.workspaceRoot`.
|
|
||||||
|
|
||||||
### `messages`
|
### `messages`
|
||||||
|
|
||||||
Controls inbound/outbound prefixes and timestamps.
|
Controls inbound/outbound prefixes and timestamps.
|
||||||
@@ -396,7 +388,7 @@ Controls the embedded agent runtime (model/thinking/verbose/timeouts).
|
|||||||
},
|
},
|
||||||
maxConcurrent: 3,
|
maxConcurrent: 3,
|
||||||
bash: {
|
bash: {
|
||||||
backgroundMs: 10000,
|
backgroundMs: 20000,
|
||||||
timeoutSec: 1800,
|
timeoutSec: 1800,
|
||||||
cleanupMs: 1800000
|
cleanupMs: 1800000
|
||||||
},
|
},
|
||||||
@@ -435,7 +427,7 @@ Z.AI models are available as `zai/<model>` (e.g. `zai/glm-4.7`) and require
|
|||||||
- `prompt`: optional override for the heartbeat body (default: `HEARTBEAT`).
|
- `prompt`: optional override for the heartbeat body (default: `HEARTBEAT`).
|
||||||
|
|
||||||
`agent.bash` configures background bash defaults:
|
`agent.bash` configures background bash defaults:
|
||||||
- `backgroundMs`: time before auto-background (ms, default 10000)
|
- `backgroundMs`: time before auto-background (ms, default 20000)
|
||||||
- `timeoutSec`: auto-kill after this runtime (seconds, default 1800)
|
- `timeoutSec`: auto-kill after this runtime (seconds, default 1800)
|
||||||
- `cleanupMs`: how long to keep finished sessions in memory (ms, default 1800000)
|
- `cleanupMs`: how long to keep finished sessions in memory (ms, default 1800000)
|
||||||
|
|
||||||
@@ -443,77 +435,6 @@ Z.AI models are available as `zai/<model>` (e.g. `zai/glm-4.7`) and require
|
|||||||
execute in parallel across sessions. Each session is still serialized (one run
|
execute in parallel across sessions. Each session is still serialized (one run
|
||||||
per session key at a time). Default: 1.
|
per session key at a time). Default: 1.
|
||||||
|
|
||||||
### `agent.sandbox`
|
|
||||||
|
|
||||||
Optional per-session **Docker sandboxing** for the embedded agent. Intended for
|
|
||||||
non-main sessions so they cannot access your host system.
|
|
||||||
|
|
||||||
Defaults (if enabled):
|
|
||||||
- one container per session
|
|
||||||
- Debian bookworm-slim based image
|
|
||||||
- workspace per session under `~/.clawdis/sandboxes`
|
|
||||||
- auto-prune: idle > 24h OR age > 7d
|
|
||||||
- tools: allow only `bash`, `process`, `read`, `write`, `edit` (deny wins)
|
|
||||||
- optional sandboxed browser (Chromium + CDP, noVNC observer)
|
|
||||||
|
|
||||||
```json5
|
|
||||||
{
|
|
||||||
agent: {
|
|
||||||
sandbox: {
|
|
||||||
mode: "non-main", // off | non-main | all
|
|
||||||
perSession: true,
|
|
||||||
workspaceRoot: "~/.clawdis/sandboxes",
|
|
||||||
docker: {
|
|
||||||
image: "clawdis-sandbox:bookworm-slim",
|
|
||||||
containerPrefix: "clawdis-sbx-",
|
|
||||||
workdir: "/workspace",
|
|
||||||
readOnlyRoot: true,
|
|
||||||
tmpfs: ["/tmp", "/var/tmp", "/run"],
|
|
||||||
network: "bridge",
|
|
||||||
user: "1000:1000",
|
|
||||||
capDrop: ["ALL"],
|
|
||||||
env: { LANG: "C.UTF-8" },
|
|
||||||
setupCommand: "apt-get update && apt-get install -y git curl jq"
|
|
||||||
},
|
|
||||||
browser: {
|
|
||||||
enabled: false,
|
|
||||||
image: "clawdis-sandbox-browser:bookworm-slim",
|
|
||||||
containerPrefix: "clawdis-sbx-browser-",
|
|
||||||
cdpPort: 9222,
|
|
||||||
vncPort: 5900,
|
|
||||||
noVncPort: 6080,
|
|
||||||
headless: false,
|
|
||||||
enableNoVnc: true
|
|
||||||
},
|
|
||||||
tools: {
|
|
||||||
allow: ["bash", "process", "read", "write", "edit"],
|
|
||||||
deny: ["browser", "canvas", "nodes", "cron", "discord", "gateway"]
|
|
||||||
},
|
|
||||||
prune: {
|
|
||||||
idleHours: 24, // 0 disables idle pruning
|
|
||||||
maxAgeDays: 7 // 0 disables max-age pruning
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
Build the default sandbox image once with:
|
|
||||||
```bash
|
|
||||||
scripts/sandbox-setup.sh
|
|
||||||
```
|
|
||||||
|
|
||||||
Build the optional browser image with:
|
|
||||||
```bash
|
|
||||||
scripts/sandbox-browser-setup.sh
|
|
||||||
```
|
|
||||||
|
|
||||||
When `agent.sandbox.browser.enabled=true`, the browser tool uses a sandboxed
|
|
||||||
Chromium instance (CDP). If noVNC is enabled (default when headless=false),
|
|
||||||
the noVNC URL is injected into the system prompt so the agent can reference it.
|
|
||||||
This does not require `browser.enabled` in the main config; the sandbox control
|
|
||||||
URL is injected per session.
|
|
||||||
|
|
||||||
### `models` (custom providers + base URLs)
|
### `models` (custom providers + base URLs)
|
||||||
|
|
||||||
Clawdis uses the **pi-coding-agent** model catalog. You can add custom providers
|
Clawdis uses the **pi-coding-agent** model catalog. You can add custom providers
|
||||||
@@ -620,21 +541,11 @@ Controls session scoping, idle expiry, reset triggers, and where the session sto
|
|||||||
idleMinutes: 60,
|
idleMinutes: 60,
|
||||||
resetTriggers: ["/new", "/reset"],
|
resetTriggers: ["/new", "/reset"],
|
||||||
store: "~/.clawdis/sessions/sessions.json",
|
store: "~/.clawdis/sessions/sessions.json",
|
||||||
// mainKey is ignored; primary key is fixed to "main"
|
mainKey: "main"
|
||||||
sendPolicy: {
|
|
||||||
rules: [
|
|
||||||
{ action: "deny", match: { surface: "discord", chatType: "group" } }
|
|
||||||
],
|
|
||||||
default: "allow"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Fields:
|
|
||||||
- `sendPolicy.default`: `allow` or `deny` fallback when no rule matches.
|
|
||||||
- `sendPolicy.rules[]`: match by `surface` (provider), `chatType` (`direct|group|room`), or `keyPrefix` (e.g. `cron:`). First deny wins; otherwise allow.
|
|
||||||
|
|
||||||
### `skills` (skills config)
|
### `skills` (skills config)
|
||||||
|
|
||||||
Controls bundled allowlist, install preferences, extra skill folders, and per-skill
|
Controls bundled allowlist, install preferences, extra skill folders, and per-skill
|
||||||
@@ -772,11 +683,6 @@ Remote client defaults (CLI):
|
|||||||
- `gateway.remote.token` supplies the token for remote calls (leave unset for no auth).
|
- `gateway.remote.token` supplies the token for remote calls (leave unset for no auth).
|
||||||
- `gateway.remote.password` supplies the password for remote calls (leave unset for no auth).
|
- `gateway.remote.password` supplies the password for remote calls (leave unset for no auth).
|
||||||
|
|
||||||
macOS app behavior:
|
|
||||||
- Clawdis.app watches `~/.clawdis/clawdis.json` and switches modes live when `gateway.mode` or `gateway.remote.url` changes.
|
|
||||||
- If `gateway.mode` is unset but `gateway.remote.url` is set, the macOS app treats it as remote mode.
|
|
||||||
- When you change connection mode in the macOS app, it writes `gateway.mode` (and `gateway.remote.url` in remote mode) back to the config file.
|
|
||||||
|
|
||||||
```json5
|
```json5
|
||||||
{
|
{
|
||||||
gateway: {
|
gateway: {
|
||||||
@@ -790,48 +696,6 @@ macOS app behavior:
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### `gateway.reload` (Config hot reload)
|
|
||||||
|
|
||||||
The Gateway watches `~/.clawdis/clawdis.json` (or `CLAWDIS_CONFIG_PATH`) and applies changes automatically.
|
|
||||||
|
|
||||||
Modes:
|
|
||||||
- `hybrid` (default): hot-apply safe changes; restart the Gateway for critical changes.
|
|
||||||
- `hot`: only apply hot-safe changes; log when a restart is required.
|
|
||||||
- `restart`: restart the Gateway on any config change.
|
|
||||||
- `off`: disable hot reload.
|
|
||||||
|
|
||||||
```json5
|
|
||||||
{
|
|
||||||
gateway: {
|
|
||||||
reload: {
|
|
||||||
mode: "hybrid",
|
|
||||||
debounceMs: 300
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Hot reload matrix (files + impact)
|
|
||||||
|
|
||||||
Files watched:
|
|
||||||
- `~/.clawdis/clawdis.json` (or `CLAWDIS_CONFIG_PATH`)
|
|
||||||
|
|
||||||
Hot-applied (no full gateway restart):
|
|
||||||
- `hooks` (webhook auth/path/mappings) + `hooks.gmail` (Gmail watcher restarted)
|
|
||||||
- `browser` (browser control server restart)
|
|
||||||
- `cron` (cron service restart + concurrency update)
|
|
||||||
- `agent.heartbeat` (heartbeat runner restart)
|
|
||||||
- `web` (WhatsApp web provider restart)
|
|
||||||
- `telegram`, `discord`, `signal`, `imessage` (provider restarts)
|
|
||||||
- `agent`, `models`, `routing`, `messages`, `session`, `whatsapp`, `logging`, `skills`, `ui`, `talk`, `identity`, `wizard` (dynamic reads)
|
|
||||||
|
|
||||||
Requires full Gateway restart:
|
|
||||||
- `gateway` (port/bind/auth/control UI/tailscale)
|
|
||||||
- `bridge`
|
|
||||||
- `discovery`
|
|
||||||
- `canvasHost`
|
|
||||||
- Any unknown/unsupported config path (defaults to restart for safety)
|
|
||||||
|
|
||||||
### Multi-instance isolation
|
### Multi-instance isolation
|
||||||
|
|
||||||
To run multiple gateways on one host, isolate per-instance state + config and use unique ports:
|
To run multiple gateways on one host, isolate per-instance state + config and use unique ports:
|
||||||
|
|||||||
+8
-178
@@ -9,27 +9,16 @@ read_when:
|
|||||||
|
|
||||||
Docker is **optional**. Use it only if you want a containerized gateway or to validate the Docker flow.
|
Docker is **optional**. Use it only if you want a containerized gateway or to validate the Docker flow.
|
||||||
|
|
||||||
This guide covers:
|
## Quick start (recommended)
|
||||||
- Containerized Gateway (full Clawdis in Docker)
|
|
||||||
- Per-session Agent Sandbox (host gateway + Docker-isolated agent tools)
|
|
||||||
|
|
||||||
## Requirements
|
From the repo root:
|
||||||
|
|
||||||
- Docker Desktop (or Docker Engine) + Docker Compose v2
|
|
||||||
- Enough disk for images + logs
|
|
||||||
|
|
||||||
## Containerized Gateway (Docker Compose)
|
|
||||||
|
|
||||||
### Quick start (recommended)
|
|
||||||
|
|
||||||
From repo root:
|
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
./docker-setup.sh
|
./docker-setup.sh
|
||||||
```
|
```
|
||||||
|
|
||||||
This script:
|
This script:
|
||||||
- builds the gateway image
|
- builds the image
|
||||||
- runs the onboarding wizard
|
- runs the onboarding wizard
|
||||||
- runs WhatsApp login
|
- runs WhatsApp login
|
||||||
- starts the gateway via Docker Compose
|
- starts the gateway via Docker Compose
|
||||||
@@ -38,7 +27,7 @@ It writes config/workspace on the host:
|
|||||||
- `~/.clawdis/`
|
- `~/.clawdis/`
|
||||||
- `~/clawd`
|
- `~/clawd`
|
||||||
|
|
||||||
### Manual flow (compose)
|
## Manual flow (compose)
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
docker build -t clawdis:local -f Dockerfile .
|
docker build -t clawdis:local -f Dockerfile .
|
||||||
@@ -47,173 +36,14 @@ docker compose run --rm clawdis-cli login
|
|||||||
docker compose up -d clawdis-gateway
|
docker compose up -d clawdis-gateway
|
||||||
```
|
```
|
||||||
|
|
||||||
### Health check
|
## E2E smoke test (Docker)
|
||||||
|
|
||||||
```bash
|
|
||||||
docker compose exec clawdis-gateway node dist/index.js health --token "$CLAWDIS_GATEWAY_TOKEN"
|
|
||||||
```
|
|
||||||
|
|
||||||
### E2E smoke test (Docker)
|
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
scripts/e2e/onboard-docker.sh
|
scripts/e2e/onboard-docker.sh
|
||||||
```
|
```
|
||||||
|
|
||||||
### Notes
|
## Notes
|
||||||
|
|
||||||
- Gateway bind defaults to `lan` for container use.
|
- Gateway bind defaults to `lan` for container use.
|
||||||
- The gateway container is the source of truth for sessions (`~/.clawdis/sessions`).
|
- Health check:
|
||||||
|
`docker compose exec clawdis-gateway node dist/index.js health --token "$CLAWDIS_GATEWAY_TOKEN"`
|
||||||
## Per-session Agent Sandbox (host gateway + Docker tools)
|
|
||||||
|
|
||||||
### What it does
|
|
||||||
|
|
||||||
When `agent.sandbox` is enabled, **non-main sessions** run tools inside a Docker
|
|
||||||
container. The gateway stays on your host, but the tool execution is isolated:
|
|
||||||
- one container per session (hard wall)
|
|
||||||
- per-session workspace folder mounted at `/workspace`
|
|
||||||
- allow/deny tool policy (deny wins)
|
|
||||||
|
|
||||||
### Default behavior
|
|
||||||
|
|
||||||
- Image: `clawdis-sandbox:bookworm-slim`
|
|
||||||
- One container per session
|
|
||||||
- Workspace per session under `~/.clawdis/sandboxes`
|
|
||||||
- Auto-prune: idle > 24h OR age > 7d
|
|
||||||
- Default allow: `bash`, `process`, `read`, `write`, `edit`
|
|
||||||
- Default deny: `browser`, `canvas`, `nodes`, `cron`, `discord`, `gateway`
|
|
||||||
|
|
||||||
### Enable sandboxing
|
|
||||||
|
|
||||||
```json5
|
|
||||||
{
|
|
||||||
agent: {
|
|
||||||
sandbox: {
|
|
||||||
mode: "non-main", // off | non-main | all
|
|
||||||
perSession: true,
|
|
||||||
workspaceRoot: "~/.clawdis/sandboxes",
|
|
||||||
docker: {
|
|
||||||
image: "clawdis-sandbox:bookworm-slim",
|
|
||||||
workdir: "/workspace",
|
|
||||||
readOnlyRoot: true,
|
|
||||||
tmpfs: ["/tmp", "/var/tmp", "/run"],
|
|
||||||
network: "bridge",
|
|
||||||
user: "1000:1000",
|
|
||||||
capDrop: ["ALL"],
|
|
||||||
env: { LANG: "C.UTF-8" },
|
|
||||||
setupCommand: "apt-get update && apt-get install -y git curl jq"
|
|
||||||
},
|
|
||||||
tools: {
|
|
||||||
allow: ["bash", "process", "read", "write", "edit"],
|
|
||||||
deny: ["browser", "canvas", "nodes", "cron", "discord", "gateway"]
|
|
||||||
},
|
|
||||||
prune: {
|
|
||||||
idleHours: 24, // 0 disables idle pruning
|
|
||||||
maxAgeDays: 7 // 0 disables max-age pruning
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### Build the default sandbox image
|
|
||||||
|
|
||||||
```bash
|
|
||||||
scripts/sandbox-setup.sh
|
|
||||||
```
|
|
||||||
|
|
||||||
This builds `clawdis-sandbox:bookworm-slim` using `Dockerfile.sandbox`.
|
|
||||||
|
|
||||||
### Sandbox browser image
|
|
||||||
|
|
||||||
To run the browser tool inside the sandbox, build the browser image:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
scripts/sandbox-browser-setup.sh
|
|
||||||
```
|
|
||||||
|
|
||||||
This builds `clawdis-sandbox-browser:bookworm-slim` using
|
|
||||||
`Dockerfile.sandbox-browser`. The container runs Chromium with CDP enabled and
|
|
||||||
an optional noVNC observer (headful via Xvfb).
|
|
||||||
|
|
||||||
Notes:
|
|
||||||
- Headful (Xvfb) reduces bot blocking vs headless.
|
|
||||||
- Headless can still be used by setting `agent.sandbox.browser.headless=true`.
|
|
||||||
- No full desktop environment (GNOME) is needed; Xvfb provides the display.
|
|
||||||
|
|
||||||
Use config:
|
|
||||||
|
|
||||||
```json5
|
|
||||||
{
|
|
||||||
agent: {
|
|
||||||
sandbox: {
|
|
||||||
browser: { enabled: true }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
Custom browser image:
|
|
||||||
|
|
||||||
```json5
|
|
||||||
{
|
|
||||||
agent: {
|
|
||||||
sandbox: { browser: { image: "my-clawdis-browser" } }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
When enabled, the agent receives:
|
|
||||||
- a sandbox browser control URL (for the `browser` tool)
|
|
||||||
- a noVNC URL (if enabled and headless=false)
|
|
||||||
|
|
||||||
Remember: if you use an allowlist for tools, add `browser` (and remove it from
|
|
||||||
deny) or the tool remains blocked.
|
|
||||||
Prune rules (`agent.sandbox.prune`) apply to browser containers too.
|
|
||||||
|
|
||||||
### Custom sandbox image
|
|
||||||
|
|
||||||
Build your own image and point config to it:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
docker build -t my-clawdis-sbx -f Dockerfile.sandbox .
|
|
||||||
```
|
|
||||||
|
|
||||||
```json5
|
|
||||||
{
|
|
||||||
agent: {
|
|
||||||
sandbox: { docker: { image: "my-clawdis-sbx" } }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### Tool policy (allow/deny)
|
|
||||||
|
|
||||||
- `deny` wins over `allow`.
|
|
||||||
- If `allow` is empty: all tools (except deny) are available.
|
|
||||||
- If `allow` is non-empty: only tools in `allow` are available (minus deny).
|
|
||||||
|
|
||||||
### Pruning strategy
|
|
||||||
|
|
||||||
Two knobs:
|
|
||||||
- `prune.idleHours`: remove containers not used in X hours (0 = disable)
|
|
||||||
- `prune.maxAgeDays`: remove containers older than X days (0 = disable)
|
|
||||||
|
|
||||||
Example:
|
|
||||||
- Keep busy sessions but cap lifetime:
|
|
||||||
`idleHours: 24`, `maxAgeDays: 7`
|
|
||||||
- Never prune:
|
|
||||||
`idleHours: 0`, `maxAgeDays: 0`
|
|
||||||
|
|
||||||
### Security notes
|
|
||||||
|
|
||||||
- Hard wall only applies to **tools** (bash/read/write/edit).
|
|
||||||
- Host-only tools like browser/camera/canvas are blocked by default.
|
|
||||||
- Allowing `browser` in sandbox **breaks isolation** (browser runs on host).
|
|
||||||
|
|
||||||
## Troubleshooting
|
|
||||||
|
|
||||||
- Image missing: build with `scripts/sandbox-setup.sh` or set `agent.sandbox.docker.image`.
|
|
||||||
- Container not running: it will auto-create per session on demand.
|
|
||||||
- Permission errors in sandbox: set `docker.user` to a UID:GID that matches your
|
|
||||||
mounted workspace ownership (or chown the workspace folder).
|
|
||||||
@@ -1,86 +0,0 @@
|
|||||||
{
|
|
||||||
"$schema": "https://mintlify.com/docs.json",
|
|
||||||
"name": "Clawdis",
|
|
||||||
"theme": "mint",
|
|
||||||
"logo": {
|
|
||||||
"light": "/assets/pixel-lobster.svg",
|
|
||||||
"dark": "/assets/pixel-lobster.svg"
|
|
||||||
},
|
|
||||||
"favicon": "/assets/pixel-lobster.svg",
|
|
||||||
"colors": {
|
|
||||||
"primary": "#FF5A36"
|
|
||||||
},
|
|
||||||
"topbarLinks": [
|
|
||||||
{
|
|
||||||
"name": "GitHub",
|
|
||||||
"url": "https://github.com/steipete/clawdis"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"name": "Releases",
|
|
||||||
"url": "https://github.com/steipete/clawdis/releases"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"navigation": {
|
|
||||||
"groups": [
|
|
||||||
{
|
|
||||||
"group": "Getting Started",
|
|
||||||
"pages": [
|
|
||||||
"index",
|
|
||||||
"onboarding",
|
|
||||||
"wizard",
|
|
||||||
"clawd",
|
|
||||||
"faq"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"group": "Core Concepts",
|
|
||||||
"pages": [
|
|
||||||
"architecture",
|
|
||||||
"agent",
|
|
||||||
"session",
|
|
||||||
"rpc",
|
|
||||||
"queue",
|
|
||||||
"tools"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"group": "Gateway & Providers",
|
|
||||||
"pages": [
|
|
||||||
"gateway",
|
|
||||||
"gateway/pairing",
|
|
||||||
"whatsapp",
|
|
||||||
"telegram",
|
|
||||||
"discord",
|
|
||||||
"imessage",
|
|
||||||
"webhook",
|
|
||||||
"gmail-pubsub"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"group": "Platforms",
|
|
||||||
"pages": [
|
|
||||||
"mac/dev-setup",
|
|
||||||
"mac/menu-bar",
|
|
||||||
"mac/voicewake",
|
|
||||||
"ios/connect",
|
|
||||||
"android/connect",
|
|
||||||
"webchat",
|
|
||||||
"web"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"group": "Operations",
|
|
||||||
"pages": [
|
|
||||||
"doctor",
|
|
||||||
"troubleshooting",
|
|
||||||
"logging",
|
|
||||||
"security",
|
|
||||||
"health",
|
|
||||||
"background-process",
|
|
||||||
"gateway-lock",
|
|
||||||
"remote"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-26
@@ -530,32 +530,6 @@ Use `/model` to switch without restarting:
|
|||||||
|
|
||||||
**Tip:** `/model` is processed at the gateway level — it works even if you're rate-limited (429) on the current model!
|
**Tip:** `/model` is processed at the gateway level — it works even if you're rate-limited (429) on the current model!
|
||||||
|
|
||||||
### Alternative providers (OpenRouter, Z.AI)?
|
|
||||||
|
|
||||||
If you don't want to use Anthropic directly, you can use alternative providers:
|
|
||||||
|
|
||||||
**OpenRouter** (pay-per-token, many models):
|
|
||||||
```json5
|
|
||||||
{
|
|
||||||
agent: {
|
|
||||||
model: "openrouter/anthropic/claude-sonnet-4",
|
|
||||||
env: { OPENROUTER_API_KEY: "sk-or-..." }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
**Z.AI** (flat-rate plans, GLM models):
|
|
||||||
```json5
|
|
||||||
{
|
|
||||||
agent: {
|
|
||||||
model: "zai/glm-4.7",
|
|
||||||
env: { ZAI_API_KEY: "..." }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
**Important:** Always use the latest Claude models (4.5 series). Don't use older 3.x models — they're deprecated and less capable. Check [OpenRouter models](https://openrouter.ai/models?q=claude) for exact IDs.
|
|
||||||
|
|
||||||
### Model + thinking mode issues?
|
### Model + thinking mode issues?
|
||||||
|
|
||||||
Some models don't support extended thinking well:
|
Some models don't support extended thinking well:
|
||||||
|
|||||||
@@ -22,10 +22,6 @@ pnpm clawdis gateway --force
|
|||||||
# dev loop (auto-reload on TS changes):
|
# dev loop (auto-reload on TS changes):
|
||||||
pnpm gateway:watch
|
pnpm gateway:watch
|
||||||
```
|
```
|
||||||
- Config hot reload watches `~/.clawdis/clawdis.json` (or `CLAWDIS_CONFIG_PATH`).
|
|
||||||
- Default mode: `gateway.reload.mode="hybrid"` (hot-apply safe changes, restart on critical).
|
|
||||||
- Hot reload uses in-process restart via **SIGUSR1** when needed.
|
|
||||||
- Disable with `gateway.reload.mode="off"`.
|
|
||||||
- Binds WebSocket control plane to `127.0.0.1:<port>` (default 18789).
|
- Binds WebSocket control plane to `127.0.0.1:<port>` (default 18789).
|
||||||
- The same port also serves HTTP (control UI, hooks, A2UI). Single-port multiplex.
|
- The same port also serves HTTP (control UI, hooks, A2UI). Single-port multiplex.
|
||||||
- Starts a Canvas file server by default on `canvasHost.port` (default `18793`), serving `http://<gateway-host>:18793/__clawdis__/canvas/` from `~/clawd/canvas`. Disable with `canvasHost.enabled=false` or `CLAWDIS_SKIP_CANVAS_HOST=1`.
|
- Starts a Canvas file server by default on `canvasHost.port` (default `18793`), serving `http://<gateway-host>:18793/__clawdis__/canvas/` from `~/clawd/canvas`. Disable with `canvasHost.enabled=false` or `CLAWDIS_SKIP_CANVAS_HOST=1`.
|
||||||
|
|||||||
+1
-1
@@ -50,7 +50,7 @@ message. If the reply is only `HEARTBEAT_OK`, it is dropped.
|
|||||||
- `prompt`: optional override for the heartbeat body (default: `HEARTBEAT`).
|
- `prompt`: optional override for the heartbeat body (default: `HEARTBEAT`).
|
||||||
|
|
||||||
## Behavior
|
## Behavior
|
||||||
- Runs in the main session (`main`, or `global` when scope is global).
|
- Runs in the main session (`session.mainKey`, or `global` when scope is global).
|
||||||
- Uses the main lane queue; if requests are in flight, the wake is retried.
|
- Uses the main lane queue; if requests are in flight, the wake is retried.
|
||||||
- Empty output or `HEARTBEAT_OK` is treated as “ok” and does **not** keep the
|
- Empty output or `HEARTBEAT_OK` is treated as “ok” and does **not** keep the
|
||||||
session alive (`updatedAt` is restored).
|
session alive (`updatedAt` is restored).
|
||||||
|
|||||||
@@ -24,7 +24,6 @@ CLAWDIS is now **web-only** (Baileys). This document captures the current media
|
|||||||
- **Images:** resize & recompress to JPEG (max side 2048px) targeting `agent.mediaMaxMb` (default 5 MB), capped at 6 MB.
|
- **Images:** resize & recompress to JPEG (max side 2048px) targeting `agent.mediaMaxMb` (default 5 MB), capped at 6 MB.
|
||||||
- **Audio/Voice/Video:** pass-through up to 16 MB; audio is sent as a voice note (`ptt: true`).
|
- **Audio/Voice/Video:** pass-through up to 16 MB; audio is sent as a voice note (`ptt: true`).
|
||||||
- **Documents:** anything else, up to 100 MB, with filename preserved when available.
|
- **Documents:** anything else, up to 100 MB, with filename preserved when available.
|
||||||
- WhatsApp GIF-style playback: send an MP4 with `gifPlayback: true` (CLI: `--gif-playback`) so mobile clients loop inline.
|
|
||||||
- MIME detection prefers magic bytes, then headers, then file extension.
|
- MIME detection prefers magic bytes, then headers, then file extension.
|
||||||
- Caption comes from `--message` or `reply.text`; empty caption is allowed.
|
- Caption comes from `--message` or `reply.text`; empty caption is allowed.
|
||||||
- Logging: non-verbose shows `↩️`/`✅`; verbose includes size and source path/URL.
|
- Logging: non-verbose shows `↩️`/`✅`; verbose includes size and source path/URL.
|
||||||
|
|||||||
@@ -1,95 +0,0 @@
|
|||||||
---
|
|
||||||
summary: "Location command for nodes (location.get), permission modes, and background behavior"
|
|
||||||
read_when:
|
|
||||||
- Adding location node support or permissions UI
|
|
||||||
- Designing background location + push flows
|
|
||||||
---
|
|
||||||
|
|
||||||
# Location command (nodes)
|
|
||||||
|
|
||||||
## TL;DR
|
|
||||||
- `location.get` is a node command (via `node.invoke`).
|
|
||||||
- Off by default.
|
|
||||||
- Settings use a selector: Off / While Using / Always.
|
|
||||||
- Separate toggle: Precise Location.
|
|
||||||
|
|
||||||
## Why a selector (not just a switch)
|
|
||||||
OS permissions are multi-level. We can expose a selector in-app, but the OS still decides the actual grant.
|
|
||||||
- iOS/macOS: user can choose **While Using** or **Always** in system prompts/Settings. App can request upgrade, but OS may require Settings.
|
|
||||||
- Android: background location is a separate permission; on Android 10+ it often requires a Settings flow.
|
|
||||||
- Precise location is a separate grant (iOS 14+ “Precise”, Android “fine” vs “coarse”).
|
|
||||||
|
|
||||||
Selector in UI drives our requested mode; actual grant lives in OS settings.
|
|
||||||
|
|
||||||
## Settings model
|
|
||||||
Per node device:
|
|
||||||
- `location.enabledMode`: `off | whileUsing | always`
|
|
||||||
- `location.preciseEnabled`: bool
|
|
||||||
|
|
||||||
UI behavior:
|
|
||||||
- Selecting `whileUsing` requests foreground permission.
|
|
||||||
- Selecting `always` first ensures `whileUsing`, then requests background (or sends user to Settings if required).
|
|
||||||
- If OS denies requested level, revert to the highest granted level and show status.
|
|
||||||
|
|
||||||
## Permissions mapping (node.permissions)
|
|
||||||
Optional. macOS node reports `location` via the permissions map; iOS/Android may omit it.
|
|
||||||
|
|
||||||
## Command: `location.get`
|
|
||||||
Called via `node.invoke`.
|
|
||||||
|
|
||||||
Params (suggested):
|
|
||||||
```json
|
|
||||||
{
|
|
||||||
"timeoutMs": 10000,
|
|
||||||
"maxAgeMs": 15000,
|
|
||||||
"desiredAccuracy": "coarse|balanced|precise"
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
Response payload:
|
|
||||||
```json
|
|
||||||
{
|
|
||||||
"lat": 48.20849,
|
|
||||||
"lon": 16.37208,
|
|
||||||
"accuracyMeters": 12.5,
|
|
||||||
"altitudeMeters": 182.0,
|
|
||||||
"speedMps": 0.0,
|
|
||||||
"headingDeg": 270.0,
|
|
||||||
"timestamp": "2026-01-03T12:34:56.000Z",
|
|
||||||
"isPrecise": true,
|
|
||||||
"source": "gps|wifi|cell|unknown"
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
Errors (stable codes):
|
|
||||||
- `LOCATION_DISABLED`: selector is off.
|
|
||||||
- `LOCATION_PERMISSION_REQUIRED`: permission missing for requested mode.
|
|
||||||
- `LOCATION_BACKGROUND_UNAVAILABLE`: app is backgrounded but only While Using allowed.
|
|
||||||
- `LOCATION_TIMEOUT`: no fix in time.
|
|
||||||
- `LOCATION_UNAVAILABLE`: system failure / no providers.
|
|
||||||
|
|
||||||
## Background behavior (future)
|
|
||||||
Goal: model can request location even when node is backgrounded, but only when:
|
|
||||||
- User selected **Always**.
|
|
||||||
- OS grants background location.
|
|
||||||
- App is allowed to run in background for location (iOS background mode / Android foreground service or special allowance).
|
|
||||||
|
|
||||||
Push-triggered flow (future):
|
|
||||||
1) Gateway sends a push to the node (silent push or FCM data).
|
|
||||||
2) Node wakes briefly and calls `location.get` internally.
|
|
||||||
3) Node forwards payload to Gateway.
|
|
||||||
|
|
||||||
Notes:
|
|
||||||
- iOS: Always permission + background location mode required. Silent push may be throttled; expect intermittent failures.
|
|
||||||
- Android: background location may require a foreground service; otherwise, expect denial.
|
|
||||||
|
|
||||||
## Model/tooling integration
|
|
||||||
- Tool surface: `nodes` tool adds `location_get` action (node required).
|
|
||||||
- CLI: `clawdis nodes location get --node <id>`.
|
|
||||||
- Agent guidelines: only call when user enabled location and understands the scope.
|
|
||||||
|
|
||||||
## UX copy (suggested)
|
|
||||||
- Off: “Location sharing is disabled.”
|
|
||||||
- While Using: “Only when Clawdis is open.”
|
|
||||||
- Always: “Allow background location. Requires system permission.”
|
|
||||||
- Precise: “Use precise GPS location. Toggle off to share approximate location.”
|
|
||||||
@@ -11,7 +11,7 @@ read_when:
|
|||||||
- The “Nodes” block in the menu lists **devices** only (gateway bridge nodes via `node.list`), not client/presence entries.
|
- The “Nodes” block in the menu lists **devices** only (gateway bridge nodes via `node.list`), not client/presence entries.
|
||||||
|
|
||||||
## State model
|
## State model
|
||||||
- Sessions: events arrive with `runId` (per-run) plus `sessionKey` in the payload. The “main” session is the key `main`; if absent, we fall back to the most recently updated session.
|
- Sessions: events arrive with `runId` (session key). The “main” session is the key `main`; if absent, we fall back to the most recently updated session.
|
||||||
- Priority: main always wins. If main is active, its state is shown immediately. If main is idle, the most recently active non‑main session is shown. We do not flip‑flop mid‑activity; we only switch when the current session goes idle or main becomes active.
|
- Priority: main always wins. If main is active, its state is shown immediately. If main is idle, the most recently active non‑main session is shown. We do not flip‑flop mid‑activity; we only switch when the current session goes idle or main becomes active.
|
||||||
- Activity kinds:
|
- Activity kinds:
|
||||||
- `job`: high‑level command execution (`state: started|streaming|done|error`).
|
- `job`: high‑level command execution (`state: started|streaming|done|error`).
|
||||||
|
|||||||
+1
-1
@@ -5,7 +5,7 @@ read_when:
|
|||||||
---
|
---
|
||||||
# Web Chat (macOS app)
|
# Web Chat (macOS app)
|
||||||
|
|
||||||
The macOS menu bar app shows the WebChat UI as a native SwiftUI view and reuses the **primary Clawd session** (`main`, or `global` when scope is global).
|
The macOS menu bar app shows the WebChat UI as a native SwiftUI view and reuses the **primary Clawd session** (`main` by default, configurable via `session.mainKey`).
|
||||||
|
|
||||||
- **Local mode**: connects directly to the local Gateway WebSocket.
|
- **Local mode**: connects directly to the local Gateway WebSocket.
|
||||||
- **Remote mode**: forwards the Gateway WebSocket control port over SSH and uses that as the data plane.
|
- **Remote mode**: forwards the Gateway WebSocket control port over SSH and uses that as the data plane.
|
||||||
|
|||||||
@@ -94,22 +94,6 @@ Notes:
|
|||||||
- Screen recordings are clamped to `<= 60s`.
|
- Screen recordings are clamped to `<= 60s`.
|
||||||
- `--no-audio` disables microphone capture (supported on iOS/Android; macOS uses system capture audio).
|
- `--no-audio` disables microphone capture (supported on iOS/Android; macOS uses system capture audio).
|
||||||
|
|
||||||
## Location (nodes)
|
|
||||||
|
|
||||||
Nodes expose `location.get` when Location is enabled in settings.
|
|
||||||
|
|
||||||
CLI helper:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
clawdis nodes location get --node <idOrNameOrIp>
|
|
||||||
clawdis nodes location get --node <idOrNameOrIp> --accuracy precise --max-age 15000 --location-timeout 10000
|
|
||||||
```
|
|
||||||
|
|
||||||
Notes:
|
|
||||||
- Location is **off by default**.
|
|
||||||
- “Always” requires system permission; background fetch is best-effort.
|
|
||||||
- The response includes lat/lon, accuracy (meters), and timestamp.
|
|
||||||
|
|
||||||
## System commands (mac node)
|
## System commands (mac node)
|
||||||
|
|
||||||
The macOS node exposes `system.run` and `system.notify`.
|
The macOS node exposes `system.run` and `system.notify`.
|
||||||
|
|||||||
@@ -99,12 +99,6 @@ services:
|
|||||||
network_mode: bridge # Limited network
|
network_mode: bridge # Limited network
|
||||||
```
|
```
|
||||||
|
|
||||||
### Per-session sandbox (Clawdis-native)
|
|
||||||
|
|
||||||
Clawdis can also run **non-main sessions** inside per-session Docker containers
|
|
||||||
(`agent.sandbox`). This keeps the gateway on your host while isolating agent
|
|
||||||
tools in a hard wall container. See `docs/configuration.md` for the full config.
|
|
||||||
|
|
||||||
Expose only the services your AI needs:
|
Expose only the services your AI needs:
|
||||||
- ✅ GoWA API (for WhatsApp)
|
- ✅ GoWA API (for WhatsApp)
|
||||||
- ✅ Specific HTTP APIs
|
- ✅ Specific HTTP APIs
|
||||||
|
|||||||
@@ -1,116 +0,0 @@
|
|||||||
---
|
|
||||||
summary: "Agent session tools for listing sessions, fetching history, and sending cross-session messages"
|
|
||||||
read_when:
|
|
||||||
- Adding or modifying session tools
|
|
||||||
---
|
|
||||||
|
|
||||||
# Session Tools
|
|
||||||
|
|
||||||
Goal: small, hard-to-misuse tool surface so agents can list sessions, fetch history, and send to another session.
|
|
||||||
|
|
||||||
## Tool Names
|
|
||||||
- `sessions_list`
|
|
||||||
- `sessions_history`
|
|
||||||
- `sessions_send`
|
|
||||||
|
|
||||||
## Key Model
|
|
||||||
- Main direct chat bucket is always the literal key `"main"`.
|
|
||||||
- Group chats use `surface:group:<id>` or `surface:channel:<id>`.
|
|
||||||
- Cron jobs use `cron:<job.id>`.
|
|
||||||
- Hooks use `hook:<uuid>` unless explicitly set.
|
|
||||||
- Node bridge uses `node-<nodeId>` unless explicitly set.
|
|
||||||
|
|
||||||
`global` and `unknown` are internal-only and never listed. If `session.scope = "global"`, we alias it to `main` for all tools so callers never see `global`.
|
|
||||||
|
|
||||||
## sessions_list
|
|
||||||
List sessions as an array of rows.
|
|
||||||
|
|
||||||
Parameters:
|
|
||||||
- `kinds?: string[]` filter: any of `"main" | "group" | "cron" | "hook" | "node" | "other"`
|
|
||||||
- `limit?: number` max rows (default: server default, clamp e.g. 200)
|
|
||||||
- `activeMinutes?: number` only sessions updated within N minutes
|
|
||||||
- `messageLimit?: number` 0 = no messages (default 0); >0 = include last N messages
|
|
||||||
|
|
||||||
Behavior:
|
|
||||||
- `messageLimit > 0` fetches `chat.history` per session and includes the last N messages.
|
|
||||||
- Tool results are filtered out in list output; use `sessions_history` for tool messages.
|
|
||||||
|
|
||||||
Row shape (JSON):
|
|
||||||
- `key`: session key (string)
|
|
||||||
- `kind`: `main | group | cron | hook | node | other`
|
|
||||||
- `provider`: `whatsapp | telegram | discord | signal | imessage | webchat | internal | unknown`
|
|
||||||
- `displayName` (group display label if available)
|
|
||||||
- `updatedAt` (ms)
|
|
||||||
- `sessionId`
|
|
||||||
- `model`, `contextTokens`, `totalTokens`
|
|
||||||
- `thinkingLevel`, `verboseLevel`, `systemSent`, `abortedLastRun`
|
|
||||||
- `sendPolicy` (session override if set)
|
|
||||||
- `lastChannel`, `lastTo`
|
|
||||||
- `transcriptPath` (best-effort path derived from store dir + sessionId)
|
|
||||||
- `messages?` (only when `messageLimit > 0`)
|
|
||||||
|
|
||||||
## sessions_history
|
|
||||||
Fetch transcript for one session.
|
|
||||||
|
|
||||||
Parameters:
|
|
||||||
- `sessionKey` (required)
|
|
||||||
- `limit?: number` max messages (server clamps)
|
|
||||||
- `includeTools?: boolean` (default false)
|
|
||||||
|
|
||||||
Behavior:
|
|
||||||
- `includeTools=false` filters `role: "toolResult"` messages.
|
|
||||||
- Returns messages array in the raw transcript format.
|
|
||||||
|
|
||||||
## sessions_send
|
|
||||||
Send a message into another session.
|
|
||||||
|
|
||||||
Parameters:
|
|
||||||
- `sessionKey` (required)
|
|
||||||
- `message` (required)
|
|
||||||
- `timeoutSeconds?: number` (default >0; 0 = fire-and-forget)
|
|
||||||
|
|
||||||
Behavior:
|
|
||||||
- `timeoutSeconds = 0`: enqueue and return `{ runId, status: "accepted" }`.
|
|
||||||
- `timeoutSeconds > 0`: wait up to N seconds for completion, then return `{ runId, status: "ok", reply }`.
|
|
||||||
- If wait times out: `{ runId, status: "timeout", error }`. Run continues; call `sessions_history` later.
|
|
||||||
- If the run fails: `{ runId, status: "error", error }`.
|
|
||||||
- Waits via gateway `agent.wait` (server-side) so reconnects don't drop the wait.
|
|
||||||
- Agent-to-agent message context is injected for the primary run.
|
|
||||||
- After the primary run completes, Clawdis starts an **agent-to-agent post step**:
|
|
||||||
- The agent can reply with the announcement to post to the target session.
|
|
||||||
- To stay silent, reply exactly `ANNOUNCE_SKIP`.
|
|
||||||
- Any other reply is sent to the target channel.
|
|
||||||
- The post step includes the original request and round‑1 reply in context.
|
|
||||||
|
|
||||||
## Provider Field
|
|
||||||
- For groups, `provider` is the `surface` recorded on the session entry.
|
|
||||||
- For direct chats, `provider` maps from `lastChannel`.
|
|
||||||
- For cron/hook/node, `provider` is `internal`.
|
|
||||||
- If missing, `provider` is `unknown`.
|
|
||||||
|
|
||||||
## Security / Send Policy
|
|
||||||
Policy-based blocking by surface/chat type (not per session id).
|
|
||||||
|
|
||||||
```json
|
|
||||||
{
|
|
||||||
"session": {
|
|
||||||
"sendPolicy": {
|
|
||||||
"rules": [
|
|
||||||
{
|
|
||||||
"match": { "surface": "discord", "chatType": "group" },
|
|
||||||
"action": "deny"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"default": "allow"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
Runtime override (per session entry):
|
|
||||||
- `sendPolicy: "allow" | "deny"` (unset = inherit config)
|
|
||||||
- Settable via `sessions.patch` or owner-only `/send on|off|inherit`.
|
|
||||||
|
|
||||||
Enforcement points:
|
|
||||||
- `chat.send` / `agent` (gateway)
|
|
||||||
- auto-reply delivery logic
|
|
||||||
+2
-28
@@ -5,7 +5,7 @@ read_when:
|
|||||||
---
|
---
|
||||||
# Session Management
|
# Session Management
|
||||||
|
|
||||||
Clawdis treats **one session as primary**. The canonical key is fixed to `main` for direct chats (or `global` when scope is global); no configuration is required. `session.mainKey` is ignored. Older/local sessions can stay on disk, but only the primary key is used for desktop/web chat and direct agent calls.
|
Clawdis treats **one session as primary**. By default the canonical key is `main` for every direct chat; no configuration is required. You can rename it via `session.mainKey` if you really want, but there is still only a single primary session. Older/local sessions can stay on disk, but only the primary key is used for desktop/web chat and direct agent calls.
|
||||||
|
|
||||||
## Gateway is the source of truth
|
## Gateway is the source of truth
|
||||||
All session state is **owned by the gateway** (the “master” Clawdis). UI clients (macOS app, WebChat, etc.) must query the gateway for session lists and token counts instead of reading local files.
|
All session state is **owned by the gateway** (the “master” Clawdis). UI clients (macOS app, WebChat, etc.) must query the gateway for session lists and token counts instead of reading local files.
|
||||||
@@ -26,38 +26,12 @@ All session state is **owned by the gateway** (the “master” Clawdis). UI cli
|
|||||||
- Multiple phone numbers can map to that same key; they act as transports into the same conversation.
|
- Multiple phone numbers can map to that same key; they act as transports into the same conversation.
|
||||||
- Group chats isolate state with `surface:group:<id>` keys (rooms/channels use `surface:channel:<id>`); do not reuse the primary key for groups. (Discord display names show `discord:<guildSlug>#<channelSlug>`.)
|
- Group chats isolate state with `surface:group:<id>` keys (rooms/channels use `surface:channel:<id>`); do not reuse the primary key for groups. (Discord display names show `discord:<guildSlug>#<channelSlug>`.)
|
||||||
- Legacy `group:<surface>:<id>` and `group:<id>` keys are still recognized.
|
- Legacy `group:<surface>:<id>` and `group:<id>` keys are still recognized.
|
||||||
- Other sources:
|
|
||||||
- Cron jobs: `cron:<job.id>`
|
|
||||||
- Webhooks: `hook:<uuid>` (unless explicitly set by the hook)
|
|
||||||
- Node bridge runs: `node-<nodeId>`
|
|
||||||
|
|
||||||
## Lifecyle
|
## Lifecyle
|
||||||
- Idle expiry: `session.idleMinutes` (default 60). After the timeout a new `sessionId` is minted on the next message.
|
- Idle expiry: `session.idleMinutes` (default 60). After the timeout a new `sessionId` is minted on the next message.
|
||||||
- Reset triggers: exact `/new` or `/reset` (plus any extras in `resetTriggers`) start a fresh session id and pass the remainder of the message through. If `/new` or `/reset` is sent alone, Clawdis runs a short “hello” greeting turn to confirm the reset.
|
- Reset triggers: exact `/new` or `/reset` (plus any extras in `resetTriggers`) start a fresh session id and pass the remainder of the message through. If `/new` or `/reset` is sent alone, Clawdis runs a short “hello” greeting turn to confirm the reset.
|
||||||
- Manual reset: delete specific keys from the store or remove the JSONL transcript; the next message recreates them.
|
- Manual reset: delete specific keys from the store or remove the JSONL transcript; the next message recreates them.
|
||||||
|
|
||||||
## Send policy (optional)
|
|
||||||
Block delivery for specific session types without listing individual ids.
|
|
||||||
|
|
||||||
```json5
|
|
||||||
{
|
|
||||||
session: {
|
|
||||||
sendPolicy: {
|
|
||||||
rules: [
|
|
||||||
{ action: "deny", match: { surface: "discord", chatType: "group" } },
|
|
||||||
{ action: "deny", match: { keyPrefix: "cron:" } }
|
|
||||||
],
|
|
||||||
default: "allow"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
Runtime override (owner only):
|
|
||||||
- `/send on` → allow for this session
|
|
||||||
- `/send off` → deny for this session
|
|
||||||
- `/send inherit` → clear override and use config rules
|
|
||||||
|
|
||||||
## Configuration (optional rename example)
|
## Configuration (optional rename example)
|
||||||
```json5
|
```json5
|
||||||
// ~/.clawdis/clawdis.json
|
// ~/.clawdis/clawdis.json
|
||||||
@@ -67,7 +41,7 @@ Runtime override (owner only):
|
|||||||
idleMinutes: 120,
|
idleMinutes: 120,
|
||||||
resetTriggers: ["/new", "/reset"],
|
resetTriggers: ["/new", "/reset"],
|
||||||
store: "~/.clawdis/sessions/sessions.json",
|
store: "~/.clawdis/sessions/sessions.json",
|
||||||
// mainKey is ignored; primary key is fixed to "main"
|
mainKey: "main" // optional rename; still a single primary
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
Vendored
-7
@@ -73,13 +73,6 @@ You have access to your human's stuff. That doesn't mean you *share* their stuff
|
|||||||
|
|
||||||
Skills provide your tools. When you need one, check its `SKILL.md`. Keep local notes (camera names, SSH details, voice preferences) in `TOOLS.md`.
|
Skills provide your tools. When you need one, check its `SKILL.md`. Keep local notes (camera names, SSH details, voice preferences) in `TOOLS.md`.
|
||||||
|
|
||||||
**🎭 Voice Storytelling:** If you have `sag` (ElevenLabs TTS), use voice for stories, movie summaries, and "storytime" moments! Way more engaging than walls of text. Surprise people with funny voices.
|
|
||||||
|
|
||||||
**📝 Platform Formatting:**
|
|
||||||
- **Discord/WhatsApp:** No markdown tables! Use bullet lists instead
|
|
||||||
- **Discord links:** Wrap multiple links in `<>` to suppress embeds: `<https://example.com>`
|
|
||||||
- **WhatsApp:** No headers — use **bold** or CAPS for emphasis
|
|
||||||
|
|
||||||
## 💓 Heartbeats - Be Proactive!
|
## 💓 Heartbeats - Be Proactive!
|
||||||
|
|
||||||
When you receive a `HEARTBEAT` message, don't just reply `HEARTBEAT_OK` every time. Use heartbeats productively!
|
When you receive a `HEARTBEAT` message, don't just reply `HEARTBEAT_OK` every time. Use heartbeats productively!
|
||||||
|
|||||||
+2
-18
@@ -18,10 +18,10 @@ Run shell commands in the workspace.
|
|||||||
|
|
||||||
Core parameters:
|
Core parameters:
|
||||||
- `command` (required)
|
- `command` (required)
|
||||||
- `yieldMs` (auto-background after timeout, default 10000)
|
- `yieldMs` (auto-background after timeout, default 20000)
|
||||||
- `background` (immediate background)
|
- `background` (immediate background)
|
||||||
- `timeout` (seconds; kills the process if exceeded, default 1800)
|
- `timeout` (seconds; kills the process if exceeded, default 1800)
|
||||||
- Need a real TTY? Use the tmux skill.
|
- `stdinMode` (`pipe` | `pty`; `pty` uses node-pty for a real TTY with fallback warning)
|
||||||
|
|
||||||
Notes:
|
Notes:
|
||||||
- Returns `status: "running"` with a `sessionId` when backgrounded.
|
- Returns `status: "running"` with a `sessionId` when backgrounded.
|
||||||
@@ -78,13 +78,11 @@ Core actions:
|
|||||||
- `pending`, `approve`, `reject` (pairing)
|
- `pending`, `approve`, `reject` (pairing)
|
||||||
- `notify` (macOS `system.notify`)
|
- `notify` (macOS `system.notify`)
|
||||||
- `camera_snap`, `camera_clip`, `screen_record`
|
- `camera_snap`, `camera_clip`, `screen_record`
|
||||||
- `location_get`
|
|
||||||
|
|
||||||
Notes:
|
Notes:
|
||||||
- Camera/screen commands require the node app to be foregrounded.
|
- Camera/screen commands require the node app to be foregrounded.
|
||||||
- Images return image blocks + `MEDIA:<path>`.
|
- Images return image blocks + `MEDIA:<path>`.
|
||||||
- Videos return `FILE:<path>` (mp4).
|
- Videos return `FILE:<path>` (mp4).
|
||||||
- Location returns a JSON payload (lat/lon/accuracy/timestamp).
|
|
||||||
|
|
||||||
### `cron`
|
### `cron`
|
||||||
Manage Gateway cron jobs and wakeups.
|
Manage Gateway cron jobs and wakeups.
|
||||||
@@ -107,20 +105,6 @@ Core actions:
|
|||||||
Notes:
|
Notes:
|
||||||
- Use `delayMs` (defaults to 2000) to avoid interrupting an in-flight reply.
|
- Use `delayMs` (defaults to 2000) to avoid interrupting an in-flight reply.
|
||||||
|
|
||||||
### `sessions_list` / `sessions_history` / `sessions_send`
|
|
||||||
List sessions, inspect transcript history, or send to another session.
|
|
||||||
|
|
||||||
Core parameters:
|
|
||||||
- `sessions_list`: `kinds?`, `limit?`, `activeMinutes?`, `messageLimit?` (0 = none)
|
|
||||||
- `sessions_history`: `sessionKey`, `limit?`, `includeTools?`
|
|
||||||
- `sessions_send`: `sessionKey`, `message`, `timeoutSeconds?` (0 = fire-and-forget)
|
|
||||||
|
|
||||||
Notes:
|
|
||||||
- `main` is the canonical direct-chat key; global/unknown are hidden.
|
|
||||||
- `messageLimit > 0` fetches last N messages per session (tool messages filtered).
|
|
||||||
- `sessions_send` waits for final completion when `timeoutSeconds > 0`.
|
|
||||||
- `sessions_send` always runs a follow‑up **agent‑to‑agent post step**; reply `ANNOUNCE_SKIP` to suppress the announcement.
|
|
||||||
|
|
||||||
### `discord`
|
### `discord`
|
||||||
Send Discord reactions, stickers, or polls.
|
Send Discord reactions, stickers, or polls.
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -28,7 +28,7 @@ Use SSH tunneling or Tailscale to reach the Gateway WS.
|
|||||||
- `--url <url>`: Gateway WebSocket URL (defaults to config `gateway.remote.url` or `ws://127.0.0.1:18789`).
|
- `--url <url>`: Gateway WebSocket URL (defaults to config `gateway.remote.url` or `ws://127.0.0.1:18789`).
|
||||||
- `--token <token>`: Gateway token (if required).
|
- `--token <token>`: Gateway token (if required).
|
||||||
- `--password <password>`: Gateway password (if required).
|
- `--password <password>`: Gateway password (if required).
|
||||||
- `--session <key>`: Session key (default: `main`, or `global` when scope is global).
|
- `--session <key>`: Session key (default: `session.mainKey` or `main`).
|
||||||
- `--deliver`: Deliver assistant replies to the provider (default off).
|
- `--deliver`: Deliver assistant replies to the provider (default off).
|
||||||
- `--thinking <level>`: Override thinking level for sends.
|
- `--thinking <level>`: Override thinking level for sends.
|
||||||
- `--timeout-ms <ms>`: Agent timeout in ms (default 30000).
|
- `--timeout-ms <ms>`: Agent timeout in ms (default 30000).
|
||||||
|
|||||||
+1
-4
@@ -92,9 +92,6 @@ WhatsApp requires a real mobile number for verification. VoIP and virtual number
|
|||||||
- Audio sent as PTT; `audio/ogg` => `audio/ogg; codecs=opus`.
|
- Audio sent as PTT; `audio/ogg` => `audio/ogg; codecs=opus`.
|
||||||
- Caption only on first media item.
|
- Caption only on first media item.
|
||||||
- Media fetch supports HTTP(S) and local paths.
|
- Media fetch supports HTTP(S) and local paths.
|
||||||
- Animated GIFs: WhatsApp expects MP4 with `gifPlayback: true` for inline looping.
|
|
||||||
- CLI: `clawdis send --media <mp4> --gif-playback`
|
|
||||||
- Gateway: `send` params include `gifPlayback: true`
|
|
||||||
|
|
||||||
## Media limits + optimization
|
## Media limits + optimization
|
||||||
- Default cap: 5 MB (per media item).
|
- Default cap: 5 MB (per media item).
|
||||||
@@ -126,7 +123,7 @@ WhatsApp requires a real mobile number for verification. VoIP and virtual number
|
|||||||
- `agent.heartbeat.model` (optional override)
|
- `agent.heartbeat.model` (optional override)
|
||||||
- `agent.heartbeat.target`
|
- `agent.heartbeat.target`
|
||||||
- `agent.heartbeat.to`
|
- `agent.heartbeat.to`
|
||||||
- `session.*` (scope, idle, store; `mainKey` is ignored)
|
- `session.*` (scope, idle, store, mainKey)
|
||||||
- `web.enabled` (disable provider startup when false)
|
- `web.enabled` (disable provider startup when false)
|
||||||
- `web.heartbeatSeconds`
|
- `web.heartbeatSeconds`
|
||||||
- `web.reconnect.*`
|
- `web.reconnect.*`
|
||||||
|
|||||||
+1
-2
@@ -38,8 +38,6 @@
|
|||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "tsx src/index.ts",
|
"dev": "tsx src/index.ts",
|
||||||
"docs:list": "tsx scripts/docs-list.ts",
|
"docs:list": "tsx scripts/docs-list.ts",
|
||||||
"docs:dev": "cd docs && mint dev",
|
|
||||||
"docs:build": "cd docs && mint broken-links",
|
|
||||||
"build": "tsc -p tsconfig.json && tsx scripts/canvas-a2ui-copy.ts",
|
"build": "tsc -p tsconfig.json && tsx scripts/canvas-a2ui-copy.ts",
|
||||||
"release:check": "tsx scripts/release-check.ts",
|
"release:check": "tsx scripts/release-check.ts",
|
||||||
"ui:install": "pnpm -C ui install",
|
"ui:install": "pnpm -C ui install",
|
||||||
@@ -97,6 +95,7 @@
|
|||||||
"grammy": "^1.39.2",
|
"grammy": "^1.39.2",
|
||||||
"json5": "^2.2.3",
|
"json5": "^2.2.3",
|
||||||
"long": "5.3.2",
|
"long": "5.3.2",
|
||||||
|
"node-pty": "^1.1.0",
|
||||||
"playwright-core": "1.57.0",
|
"playwright-core": "1.57.0",
|
||||||
"qrcode-terminal": "^0.12.0",
|
"qrcode-terminal": "^0.12.0",
|
||||||
"sharp": "^0.34.5",
|
"sharp": "^0.34.5",
|
||||||
|
|||||||
Generated
+10
@@ -94,6 +94,9 @@ importers:
|
|||||||
long:
|
long:
|
||||||
specifier: 5.3.2
|
specifier: 5.3.2
|
||||||
version: 5.3.2
|
version: 5.3.2
|
||||||
|
node-pty:
|
||||||
|
specifier: ^1.1.0
|
||||||
|
version: 1.1.0
|
||||||
playwright-core:
|
playwright-core:
|
||||||
specifier: 1.57.0
|
specifier: 1.57.0
|
||||||
version: 1.57.0
|
version: 1.57.0
|
||||||
@@ -2185,6 +2188,9 @@ packages:
|
|||||||
resolution: {integrity: sha512-dRB78srN/l6gqWulah9SrxeYnxeddIG30+GOqK/9OlLVyLg3HPnr6SqOWTWOXKRwC2eGYCkZ59NNuSgvSrpgOA==}
|
resolution: {integrity: sha512-dRB78srN/l6gqWulah9SrxeYnxeddIG30+GOqK/9OlLVyLg3HPnr6SqOWTWOXKRwC2eGYCkZ59NNuSgvSrpgOA==}
|
||||||
engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0}
|
engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0}
|
||||||
|
|
||||||
|
node-pty@1.1.0:
|
||||||
|
resolution: {integrity: sha512-20JqtutY6JPXTUnL0ij1uad7Qe1baT46lyolh2sSENDd4sTzKZ4nmAFkeAARDKwmlLjPx6XKRlwRUxwjOy+lUg==}
|
||||||
|
|
||||||
node-wav@0.0.2:
|
node-wav@0.0.2:
|
||||||
resolution: {integrity: sha512-M6Rm/bbG6De/gKGxOpeOobx/dnGuP0dz40adqx38boqHhlWssBJZgLCPBNtb9NkrmnKYiV04xELq+R6PFOnoLA==}
|
resolution: {integrity: sha512-M6Rm/bbG6De/gKGxOpeOobx/dnGuP0dz40adqx38boqHhlWssBJZgLCPBNtb9NkrmnKYiV04xELq+R6PFOnoLA==}
|
||||||
engines: {node: '>=4.4.0'}
|
engines: {node: '>=4.4.0'}
|
||||||
@@ -4828,6 +4834,10 @@ snapshots:
|
|||||||
fetch-blob: 3.2.0
|
fetch-blob: 3.2.0
|
||||||
formdata-polyfill: 4.0.10
|
formdata-polyfill: 4.0.10
|
||||||
|
|
||||||
|
node-pty@1.1.0:
|
||||||
|
dependencies:
|
||||||
|
node-addon-api: 7.1.1
|
||||||
|
|
||||||
node-wav@0.0.2:
|
node-wav@0.0.2:
|
||||||
optional: true
|
optional: true
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ PRODUCT="Clawdis"
|
|||||||
BIN="$BUILD_PATH/debug/$PRODUCT"
|
BIN="$BUILD_PATH/debug/$PRODUCT"
|
||||||
|
|
||||||
printf "\n▶️ Building $PRODUCT (debug, build path: $BUILD_PATH)\n"
|
printf "\n▶️ Building $PRODUCT (debug, build path: $BUILD_PATH)\n"
|
||||||
swift build -c debug --product "$PRODUCT" --build-path "$BUILD_PATH"
|
swift build -c debug --product "$PRODUCT" --product "${PRODUCT}CLI" --build-path "$BUILD_PATH"
|
||||||
|
|
||||||
printf "\n⏹ Stopping existing $PRODUCT...\n"
|
printf "\n⏹ Stopping existing $PRODUCT...\n"
|
||||||
killall -q "$PRODUCT" 2>/dev/null || true
|
killall -q "$PRODUCT" 2>/dev/null || true
|
||||||
|
|||||||
@@ -1,45 +0,0 @@
|
|||||||
#!/usr/bin/env bash
|
|
||||||
set -euo pipefail
|
|
||||||
|
|
||||||
export DISPLAY=:1
|
|
||||||
|
|
||||||
CDP_PORT="${CLAWDIS_BROWSER_CDP_PORT:-9222}"
|
|
||||||
VNC_PORT="${CLAWDIS_BROWSER_VNC_PORT:-5900}"
|
|
||||||
NOVNC_PORT="${CLAWDIS_BROWSER_NOVNC_PORT:-6080}"
|
|
||||||
ENABLE_NOVNC="${CLAWDIS_BROWSER_ENABLE_NOVNC:-1}"
|
|
||||||
HEADLESS="${CLAWDIS_BROWSER_HEADLESS:-0}"
|
|
||||||
|
|
||||||
mkdir -p /workspace/.chrome
|
|
||||||
|
|
||||||
Xvfb :1 -screen 0 1280x800x24 -ac -nolisten tcp &
|
|
||||||
|
|
||||||
if [[ "${HEADLESS}" == "1" ]]; then
|
|
||||||
CHROME_ARGS=(
|
|
||||||
"--headless=new"
|
|
||||||
"--disable-gpu"
|
|
||||||
)
|
|
||||||
else
|
|
||||||
CHROME_ARGS=()
|
|
||||||
fi
|
|
||||||
|
|
||||||
CHROME_ARGS+=(
|
|
||||||
"--remote-debugging-address=0.0.0.0"
|
|
||||||
"--remote-debugging-port=${CDP_PORT}"
|
|
||||||
"--user-data-dir=/workspace/.chrome"
|
|
||||||
"--no-first-run"
|
|
||||||
"--no-default-browser-check"
|
|
||||||
"--disable-dev-shm-usage"
|
|
||||||
"--disable-background-networking"
|
|
||||||
"--disable-features=TranslateUI"
|
|
||||||
"--metrics-recording-only"
|
|
||||||
"--no-sandbox"
|
|
||||||
)
|
|
||||||
|
|
||||||
chromium "${CHROME_ARGS[@]}" about:blank &
|
|
||||||
|
|
||||||
if [[ "${ENABLE_NOVNC}" == "1" && "${HEADLESS}" != "1" ]]; then
|
|
||||||
x11vnc -display :1 -rfbport "${VNC_PORT}" -shared -forever -nopw -localhost &
|
|
||||||
websockify --web /usr/share/novnc/ "${NOVNC_PORT}" "localhost:${VNC_PORT}" &
|
|
||||||
fi
|
|
||||||
|
|
||||||
wait -n
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
#!/usr/bin/env bash
|
|
||||||
set -euo pipefail
|
|
||||||
|
|
||||||
IMAGE_NAME="clawdis-sandbox-browser:bookworm-slim"
|
|
||||||
|
|
||||||
docker build -t "${IMAGE_NAME}" -f Dockerfile.sandbox-browser .
|
|
||||||
echo "Built ${IMAGE_NAME}"
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
#!/usr/bin/env bash
|
|
||||||
set -euo pipefail
|
|
||||||
|
|
||||||
IMAGE_NAME="clawdis-sandbox:bookworm-slim"
|
|
||||||
|
|
||||||
docker build -t "${IMAGE_NAME}" -f Dockerfile.sandbox .
|
|
||||||
echo "Built ${IMAGE_NAME}"
|
|
||||||
@@ -1,67 +1,43 @@
|
|||||||
---
|
---
|
||||||
name: apple-reminders
|
name: apple-reminders
|
||||||
description: Manage Apple Reminders via the `remindctl` CLI on macOS (list, add, edit, complete, delete). Supports lists, date filters, and JSON/plain output.
|
description: Manage Apple Reminders via the `memo` CLI on macOS (create, complete, and delete reminders). Use when a user asks Clawdis to add a reminder, mark reminders as done, or manage their reminder list.
|
||||||
homepage: https://github.com/steipete/remindctl
|
homepage: https://github.com/antoniorodr/memo
|
||||||
metadata: {"clawdis":{"emoji":"⏰","os":["darwin"],"requires":{"bins":["remindctl"]},"install":[{"id":"brew","kind":"brew","formula":"steipete/tap/remindctl","bins":["remindctl"],"label":"Install remindctl via Homebrew"}]}}
|
metadata: {"clawdis":{"emoji":"⏰","os":["darwin"],"requires":{"bins":["memo"]},"install":[{"id":"brew","kind":"brew","formula":"antoniorodr/memo/memo","bins":["memo"],"label":"Install memo via Homebrew"}]}}
|
||||||
---
|
---
|
||||||
|
|
||||||
# Apple Reminders CLI (remindctl)
|
# Apple Reminders CLI
|
||||||
|
|
||||||
Use `remindctl` to manage Apple Reminders directly from the terminal. It supports list filtering, date-based views, and scripting output.
|
Use `memo rem` to manage Apple Reminders directly from the terminal. Create, complete, and delete reminders with simple commands.
|
||||||
|
|
||||||
Setup
|
Setup
|
||||||
- Install (Homebrew): `brew install steipete/tap/remindctl`
|
- Install (Homebrew): `brew tap antoniorodr/memo && brew install antoniorodr/memo/memo`
|
||||||
- From source: `pnpm install && pnpm build` (binary at `./bin/remindctl`)
|
- Manual (pip): `pip install .` (after cloning the repo)
|
||||||
- macOS-only; grant Reminders permission when prompted.
|
- macOS-only; if prompted, grant Automation access to Reminders.app.
|
||||||
|
|
||||||
Permissions
|
|
||||||
- Check status: `remindctl status`
|
|
||||||
- Request access: `remindctl authorize`
|
|
||||||
|
|
||||||
View Reminders
|
View Reminders
|
||||||
- Default (today): `remindctl`
|
- List all reminders: `memo rem`
|
||||||
- Today: `remindctl today`
|
|
||||||
- Tomorrow: `remindctl tomorrow`
|
|
||||||
- Week: `remindctl week`
|
|
||||||
- Overdue: `remindctl overdue`
|
|
||||||
- Upcoming: `remindctl upcoming`
|
|
||||||
- Completed: `remindctl completed`
|
|
||||||
- All: `remindctl all`
|
|
||||||
- Specific date: `remindctl 2026-01-04`
|
|
||||||
|
|
||||||
Manage Lists
|
|
||||||
- List all lists: `remindctl list`
|
|
||||||
- Show list: `remindctl list Work`
|
|
||||||
- Create list: `remindctl list Projects --create`
|
|
||||||
- Rename list: `remindctl list Work --rename Office`
|
|
||||||
- Delete list: `remindctl list Work --delete`
|
|
||||||
|
|
||||||
Create Reminders
|
Create Reminders
|
||||||
- Quick add: `remindctl add "Buy milk"`
|
- Add a new reminder: `memo rem -a`
|
||||||
- With list + due: `remindctl add --title "Call mom" --list Personal --due tomorrow`
|
- Opens interactive prompt to create a reminder.
|
||||||
|
- Quick add: `memo rem -a "Buy groceries"`
|
||||||
Edit Reminders
|
|
||||||
- Edit title/due: `remindctl edit 1 --title "New title" --due 2026-01-04`
|
|
||||||
|
|
||||||
Complete Reminders
|
Complete Reminders
|
||||||
- Complete by id: `remindctl complete 1 2 3`
|
- Mark reminder as done: `memo rem -c`
|
||||||
|
- Interactive selection of reminder to complete.
|
||||||
|
|
||||||
Delete Reminders
|
Delete Reminders
|
||||||
- Delete by id: `remindctl delete 4A83 --force`
|
- Delete a reminder: `memo rem -d`
|
||||||
|
- Interactive selection of reminder to delete.
|
||||||
|
|
||||||
Output Formats
|
Examples
|
||||||
- JSON (scripting): `remindctl today --json`
|
- Add a quick reminder: `memo rem -a "Call dentist tomorrow"`
|
||||||
- Plain TSV: `remindctl today --plain`
|
- List all reminders: `memo rem`
|
||||||
- Counts only: `remindctl today --quiet`
|
- Complete a reminder: `memo rem -c` (then select from list)
|
||||||
|
- Delete a reminder: `memo rem -d` (then select from list)
|
||||||
Date Formats
|
|
||||||
Accepted by `--due` and date filters:
|
|
||||||
- `today`, `tomorrow`, `yesterday`
|
|
||||||
- `YYYY-MM-DD`
|
|
||||||
- `YYYY-MM-DD HH:mm`
|
|
||||||
- ISO 8601 (`2026-01-04T12:34:56Z`)
|
|
||||||
|
|
||||||
Notes
|
Notes
|
||||||
- macOS-only.
|
- macOS-only.
|
||||||
- If access is denied, enable Terminal/remindctl in System Settings → Privacy & Security → Reminders.
|
- Requires Apple Reminders.app to be accessible.
|
||||||
- If running over SSH, grant access on the Mac that runs the command.
|
- For automation, grant permissions in System Settings > Privacy & Security > Automation.
|
||||||
|
- The `memo` CLI shares the same installation for both Notes and Reminders functionality.
|
||||||
@@ -41,15 +41,9 @@ process action:kill sessionId:XXX
|
|||||||
|
|
||||||
**Model:** `gpt-5.2-codex` is the default (set in ~/.codex/config.toml)
|
**Model:** `gpt-5.2-codex` is the default (set in ~/.codex/config.toml)
|
||||||
|
|
||||||
### Building/Creating (use --full-auto or --yolo)
|
### Building/Creating (use --full-auto)
|
||||||
```bash
|
```bash
|
||||||
# --full-auto: sandboxed but auto-approves in workspace
|
|
||||||
bash workdir:~/project background:true command:"codex exec --full-auto \"Build a snake game with dark theme\""
|
bash workdir:~/project background:true command:"codex exec --full-auto \"Build a snake game with dark theme\""
|
||||||
|
|
||||||
# --yolo: NO sandbox, NO approvals (fastest, most dangerous)
|
|
||||||
bash workdir:~/project background:true command:"codex --yolo \"Build a snake game with dark theme\""
|
|
||||||
|
|
||||||
# Note: --yolo is a shortcut for --dangerously-bypass-approvals-and-sandbox
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Reviewing PRs (vanilla, no flags)
|
### Reviewing PRs (vanilla, no flags)
|
||||||
@@ -150,51 +144,6 @@ Use the tmux skill for interactive coding sessions (always, except very simple o
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Parallel Issue Fixing with git worktrees + tmux
|
|
||||||
|
|
||||||
For fixing multiple issues in parallel, use git worktrees (isolated branches) + tmux sessions:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
# 1. Clone repo to temp location
|
|
||||||
cd /tmp && git clone git@github.com:user/repo.git repo-worktrees
|
|
||||||
cd repo-worktrees
|
|
||||||
|
|
||||||
# 2. Create worktrees for each issue (isolated branches!)
|
|
||||||
git worktree add -b fix/issue-78 /tmp/issue-78 main
|
|
||||||
git worktree add -b fix/issue-99 /tmp/issue-99 main
|
|
||||||
|
|
||||||
# 3. Set up tmux sessions
|
|
||||||
SOCKET="${TMPDIR:-/tmp}/codex-fixes.sock"
|
|
||||||
tmux -S "$SOCKET" new-session -d -s fix-78
|
|
||||||
tmux -S "$SOCKET" new-session -d -s fix-99
|
|
||||||
|
|
||||||
# 4. Launch Codex in each (after pnpm install!)
|
|
||||||
tmux -S "$SOCKET" send-keys -t fix-78 "cd /tmp/issue-78 && pnpm install && codex --yolo 'Fix issue #78: <description>. Commit and push.'" Enter
|
|
||||||
tmux -S "$SOCKET" send-keys -t fix-99 "cd /tmp/issue-99 && pnpm install && codex --yolo 'Fix issue #99: <description>. Commit and push.'" Enter
|
|
||||||
|
|
||||||
# 5. Monitor progress
|
|
||||||
tmux -S "$SOCKET" capture-pane -p -t fix-78 -S -30
|
|
||||||
tmux -S "$SOCKET" capture-pane -p -t fix-99 -S -30
|
|
||||||
|
|
||||||
# 6. Check if done (prompt returned)
|
|
||||||
tmux -S "$SOCKET" capture-pane -p -t fix-78 -S -3 | grep -q "❯" && echo "Done!"
|
|
||||||
|
|
||||||
# 7. Create PRs after fixes
|
|
||||||
cd /tmp/issue-78 && git push -u origin fix/issue-78
|
|
||||||
gh pr create --repo user/repo --head fix/issue-78 --title "fix: ..." --body "..."
|
|
||||||
|
|
||||||
# 8. Cleanup
|
|
||||||
tmux -S "$SOCKET" kill-server
|
|
||||||
git worktree remove /tmp/issue-78
|
|
||||||
git worktree remove /tmp/issue-99
|
|
||||||
```
|
|
||||||
|
|
||||||
**Why worktrees?** Each Codex works in isolated branch, no conflicts. Can run 5+ parallel fixes!
|
|
||||||
|
|
||||||
**Why tmux over bash background?** Codex is interactive — needs TTY for proper output. tmux provides persistent sessions with full history capture.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## ⚠️ Rules
|
## ⚠️ Rules
|
||||||
|
|
||||||
1. **Respect tool choice** — if user asks for Codex, use Codex. NEVER offer to build it yourself!
|
1. **Respect tool choice** — if user asks for Codex, use Codex. NEVER offer to build it yourself!
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ metadata: {"clawdis":{"emoji":"👀","os":["darwin"],"requires":{"bins":["peekab
|
|||||||
|
|
||||||
Peekaboo is a full macOS UI automation CLI: capture/inspect screens, target UI
|
Peekaboo is a full macOS UI automation CLI: capture/inspect screens, target UI
|
||||||
elements, drive input, and manage apps/windows/menus. Commands share a snapshot
|
elements, drive input, and manage apps/windows/menus. Commands share a snapshot
|
||||||
cache and support `--json`/`-j` for scripting. Run `peekaboo` or
|
cache and most support `--json-output` for scripting. Run `peekaboo` or
|
||||||
`peekaboo <cmd> --help` for flags; `peekaboo --version` prints build metadata.
|
`peekaboo <cmd> --help` for flags; `peekaboo --version` prints build metadata.
|
||||||
Tip: run via `polter peekaboo` to ensure fresh builds.
|
Tip: run via `polter peekaboo` to ensure fresh builds.
|
||||||
|
|
||||||
@@ -33,7 +33,7 @@ Interaction
|
|||||||
- `drag`: drag & drop across elements/coords/Dock
|
- `drag`: drag & drop across elements/coords/Dock
|
||||||
- `hotkey`: modifier combos like `cmd,shift,t`
|
- `hotkey`: modifier combos like `cmd,shift,t`
|
||||||
- `move`: cursor positioning with optional smoothing
|
- `move`: cursor positioning with optional smoothing
|
||||||
- `paste`: set clipboard -> paste -> restore
|
- `paste`: set clipboard → paste → restore
|
||||||
- `press`: special-key sequences with repeats
|
- `press`: special-key sequences with repeats
|
||||||
- `scroll`: directional scrolling (targeted + smooth)
|
- `scroll`: directional scrolling (targeted + smooth)
|
||||||
- `swipe`: gesture-style drags between targets
|
- `swipe`: gesture-style drags between targets
|
||||||
@@ -58,96 +58,6 @@ Global runtime flags
|
|||||||
- `--json`/`-j`, `--verbose`/`-v`, `--log-level <level>`
|
- `--json`/`-j`, `--verbose`/`-v`, `--log-level <level>`
|
||||||
- `--no-remote`, `--bridge-socket <path>`
|
- `--no-remote`, `--bridge-socket <path>`
|
||||||
|
|
||||||
## Quickstart (happy path)
|
|
||||||
```bash
|
|
||||||
peekaboo permissions
|
|
||||||
peekaboo list apps --json
|
|
||||||
peekaboo see --annotate --path /tmp/peekaboo-see.png
|
|
||||||
peekaboo click --on B1
|
|
||||||
peekaboo type "Hello" --return
|
|
||||||
```
|
|
||||||
|
|
||||||
## Common targeting parameters (most interaction commands)
|
|
||||||
- App/window: `--app`, `--pid`, `--window-title`, `--window-id`, `--window-index`
|
|
||||||
- Snapshot targeting: `--snapshot` (ID from `see`; defaults to latest)
|
|
||||||
- Element/coords: `--on`/`--id` (element ID), `--coords x,y`
|
|
||||||
- Focus control: `--no-auto-focus`, `--space-switch`, `--bring-to-current-space`,
|
|
||||||
`--focus-timeout-seconds`, `--focus-retry-count`
|
|
||||||
|
|
||||||
## Common capture parameters
|
|
||||||
- Output: `--path`, `--format png|jpg`, `--retina`
|
|
||||||
- Targeting: `--mode screen|window|frontmost`, `--screen-index`,
|
|
||||||
`--window-title`, `--window-id`
|
|
||||||
- Analysis: `--analyze "prompt"`, `--annotate`
|
|
||||||
- Capture engine: `--capture-engine auto|classic|cg|modern|sckit`
|
|
||||||
|
|
||||||
## Common motion/typing parameters
|
|
||||||
- Timing: `--duration` (drag/swipe), `--steps`, `--delay` (type/scroll/press)
|
|
||||||
- Human-ish movement: `--profile human|linear`, `--wpm` (typing)
|
|
||||||
- Scroll: `--direction up|down|left|right`, `--amount <ticks>`, `--smooth`
|
|
||||||
|
|
||||||
## Examples
|
|
||||||
### See -> click -> type (most reliable flow)
|
|
||||||
```bash
|
|
||||||
peekaboo see --app Safari --window-title "Login" --annotate --path /tmp/see.png
|
|
||||||
peekaboo click --on B3 --app Safari
|
|
||||||
peekaboo type "user@example.com" --app Safari
|
|
||||||
peekaboo press tab --count 1 --app Safari
|
|
||||||
peekaboo type "supersecret" --app Safari --return
|
|
||||||
```
|
|
||||||
|
|
||||||
### Target by window id
|
|
||||||
```bash
|
|
||||||
peekaboo list windows --app "Visual Studio Code" --json
|
|
||||||
peekaboo click --window-id 12345 --coords 120,160
|
|
||||||
peekaboo type "Hello from Peekaboo" --window-id 12345
|
|
||||||
```
|
|
||||||
|
|
||||||
### Capture screenshots + analyze
|
|
||||||
```bash
|
|
||||||
peekaboo image --mode screen --screen-index 0 --retina --path /tmp/screen.png
|
|
||||||
peekaboo image --app Safari --window-title "Dashboard" --analyze "Summarize KPIs"
|
|
||||||
peekaboo see --mode screen --screen-index 0 --analyze "Summarize the dashboard"
|
|
||||||
```
|
|
||||||
|
|
||||||
### Live capture (motion-aware)
|
|
||||||
```bash
|
|
||||||
peekaboo capture live --mode region --region 100,100,800,600 --duration 30 \
|
|
||||||
--active-fps 8 --idle-fps 2 --highlight-changes --path /tmp/capture
|
|
||||||
```
|
|
||||||
|
|
||||||
### App + window management
|
|
||||||
```bash
|
|
||||||
peekaboo app launch "Safari" --open https://example.com
|
|
||||||
peekaboo window focus --app Safari --window-title "Example"
|
|
||||||
peekaboo window set-bounds --app Safari --x 50 --y 50 --width 1200 --height 800
|
|
||||||
peekaboo app quit --app Safari
|
|
||||||
```
|
|
||||||
|
|
||||||
### Menus, menubar, dock
|
|
||||||
```bash
|
|
||||||
peekaboo menu click --app Safari --item "New Window"
|
|
||||||
peekaboo menu click --app TextEdit --path "Format > Font > Show Fonts"
|
|
||||||
peekaboo menu click-extra --title "WiFi"
|
|
||||||
peekaboo dock launch Safari
|
|
||||||
peekaboo menubar list --json
|
|
||||||
```
|
|
||||||
|
|
||||||
### Mouse + gesture input
|
|
||||||
```bash
|
|
||||||
peekaboo move 500,300 --smooth
|
|
||||||
peekaboo drag --from B1 --to T2
|
|
||||||
peekaboo swipe --from-coords 100,500 --to-coords 100,200 --duration 800
|
|
||||||
peekaboo scroll --direction down --amount 6 --smooth
|
|
||||||
```
|
|
||||||
|
|
||||||
### Keyboard input
|
|
||||||
```bash
|
|
||||||
peekaboo hotkey --keys "cmd,shift,t"
|
|
||||||
peekaboo press escape
|
|
||||||
peekaboo type "Line 1\nLine 2" --delay 10
|
|
||||||
```
|
|
||||||
|
|
||||||
Notes
|
Notes
|
||||||
- Requires Screen Recording + Accessibility permissions.
|
- Requires Screen Recording + Accessibility permissions.
|
||||||
- Use `peekaboo see --annotate` to identify targets before clicking.
|
- Use `peekaboo see --annotate` to identify targets before clicking.
|
||||||
@@ -65,41 +65,6 @@ To monitor:
|
|||||||
- tmux is supported on macOS/Linux. On Windows, use WSL and install tmux inside WSL.
|
- tmux is supported on macOS/Linux. On Windows, use WSL and install tmux inside WSL.
|
||||||
- This skill is gated to `darwin`/`linux` and requires `tmux` on PATH.
|
- This skill is gated to `darwin`/`linux` and requires `tmux` on PATH.
|
||||||
|
|
||||||
## Orchestrating Coding Agents (Codex, Claude Code)
|
|
||||||
|
|
||||||
tmux excels at running multiple coding agents in parallel:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
SOCKET="${TMPDIR:-/tmp}/codex-army.sock"
|
|
||||||
|
|
||||||
# Create multiple sessions
|
|
||||||
for i in 1 2 3 4 5; do
|
|
||||||
tmux -S "$SOCKET" new-session -d -s "agent-$i"
|
|
||||||
done
|
|
||||||
|
|
||||||
# Launch agents in different workdirs
|
|
||||||
tmux -S "$SOCKET" send-keys -t agent-1 "cd /tmp/project1 && codex --yolo 'Fix bug X'" Enter
|
|
||||||
tmux -S "$SOCKET" send-keys -t agent-2 "cd /tmp/project2 && codex --yolo 'Fix bug Y'" Enter
|
|
||||||
|
|
||||||
# Poll for completion (check if prompt returned)
|
|
||||||
for sess in agent-1 agent-2; do
|
|
||||||
if tmux -S "$SOCKET" capture-pane -p -t "$sess" -S -3 | grep -q "❯"; then
|
|
||||||
echo "$sess: DONE"
|
|
||||||
else
|
|
||||||
echo "$sess: Running..."
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
|
|
||||||
# Get full output from completed session
|
|
||||||
tmux -S "$SOCKET" capture-pane -p -t agent-1 -S -500
|
|
||||||
```
|
|
||||||
|
|
||||||
**Tips:**
|
|
||||||
- Use separate git worktrees for parallel fixes (no branch conflicts)
|
|
||||||
- `pnpm install` first before running codex in fresh clones
|
|
||||||
- Check for shell prompt (`❯` or `$`) to detect completion
|
|
||||||
- Codex needs `--yolo` or `--full-auto` for non-interactive fixes
|
|
||||||
|
|
||||||
## Cleanup
|
## Cleanup
|
||||||
|
|
||||||
- Kill a session: `tmux -S "$SOCKET" kill-session -t "$SESSION"`.
|
- Kill a session: `tmux -S "$SOCKET" kill-session -t "$SESSION"`.
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ describe("bash process registry", () => {
|
|||||||
id: "sess",
|
id: "sess",
|
||||||
command: "echo test",
|
command: "echo test",
|
||||||
child: { pid: 123 } as ChildProcessWithoutNullStreams,
|
child: { pid: 123 } as ChildProcessWithoutNullStreams,
|
||||||
|
stdinMode: "pipe",
|
||||||
startedAt: Date.now(),
|
startedAt: Date.now(),
|
||||||
cwd: "/tmp",
|
cwd: "/tmp",
|
||||||
maxOutputChars: 10,
|
maxOutputChars: 10,
|
||||||
@@ -48,6 +49,7 @@ describe("bash process registry", () => {
|
|||||||
id: "sess",
|
id: "sess",
|
||||||
command: "echo test",
|
command: "echo test",
|
||||||
child: { pid: 123 } as ChildProcessWithoutNullStreams,
|
child: { pid: 123 } as ChildProcessWithoutNullStreams,
|
||||||
|
stdinMode: "pipe",
|
||||||
startedAt: Date.now(),
|
startedAt: Date.now(),
|
||||||
cwd: "/tmp",
|
cwd: "/tmp",
|
||||||
maxOutputChars: 100,
|
maxOutputChars: 100,
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import type { ChildProcessWithoutNullStreams } from "node:child_process";
|
import type { ChildProcessWithoutNullStreams } from "node:child_process";
|
||||||
|
import type { IPty } from "node-pty";
|
||||||
|
|
||||||
const DEFAULT_JOB_TTL_MS = 30 * 60 * 1000; // 30 minutes
|
const DEFAULT_JOB_TTL_MS = 30 * 60 * 1000; // 30 minutes
|
||||||
const MIN_JOB_TTL_MS = 60 * 1000; // 1 minute
|
const MIN_JOB_TTL_MS = 60 * 1000; // 1 minute
|
||||||
@@ -15,11 +16,15 @@ let jobTtlMs = clampTtl(
|
|||||||
|
|
||||||
export type ProcessStatus = "running" | "completed" | "failed" | "killed";
|
export type ProcessStatus = "running" | "completed" | "failed" | "killed";
|
||||||
|
|
||||||
|
export type ProcessStdinMode = "pipe" | "pty";
|
||||||
|
|
||||||
export interface ProcessSession {
|
export interface ProcessSession {
|
||||||
id: string;
|
id: string;
|
||||||
command: string;
|
command: string;
|
||||||
child?: ChildProcessWithoutNullStreams;
|
child?: ChildProcessWithoutNullStreams;
|
||||||
|
pty?: IPty;
|
||||||
pid?: number;
|
pid?: number;
|
||||||
|
stdinMode: ProcessStdinMode;
|
||||||
startedAt: number;
|
startedAt: number;
|
||||||
cwd?: string;
|
cwd?: string;
|
||||||
maxOutputChars: number;
|
maxOutputChars: number;
|
||||||
|
|||||||
@@ -0,0 +1,68 @@
|
|||||||
|
import { describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
|
describe("bash tool pty mode", () => {
|
||||||
|
it("falls back to pipe with warning when node-pty fails to load", async () => {
|
||||||
|
vi.resetModules();
|
||||||
|
vi.doMock("node-pty", () => {
|
||||||
|
throw new Error("boom");
|
||||||
|
});
|
||||||
|
|
||||||
|
const { createBashTool } = await import("./bash-tools.js");
|
||||||
|
const tool = createBashTool({ backgroundMs: 10, timeoutSec: 1 });
|
||||||
|
const result = await tool.execute("call", {
|
||||||
|
command: "echo test",
|
||||||
|
stdinMode: "pty",
|
||||||
|
});
|
||||||
|
|
||||||
|
const text = result.content.find((c) => c.type === "text")?.text ?? "";
|
||||||
|
expect(text).toContain("Warning: node-pty failed to load");
|
||||||
|
expect(text).toContain("falling back to pipe mode.");
|
||||||
|
|
||||||
|
vi.doUnmock("node-pty");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("uses node-pty when available", async () => {
|
||||||
|
vi.resetModules();
|
||||||
|
const spawn = vi.fn(() => {
|
||||||
|
let onData: ((data: string) => void) | undefined;
|
||||||
|
let onExit:
|
||||||
|
| ((event: { exitCode: number | null; signal?: number | null }) => void)
|
||||||
|
| undefined;
|
||||||
|
const pty = {
|
||||||
|
pid: 4321,
|
||||||
|
onData: (cb: (data: string) => void) => {
|
||||||
|
onData = cb;
|
||||||
|
},
|
||||||
|
onExit: (
|
||||||
|
cb: (event: {
|
||||||
|
exitCode: number | null;
|
||||||
|
signal?: number | null;
|
||||||
|
}) => void,
|
||||||
|
) => {
|
||||||
|
onExit = cb;
|
||||||
|
},
|
||||||
|
write: vi.fn(),
|
||||||
|
kill: vi.fn(),
|
||||||
|
};
|
||||||
|
setTimeout(() => {
|
||||||
|
onData?.("hello\n");
|
||||||
|
onExit?.({ exitCode: 0, signal: null });
|
||||||
|
}, 10);
|
||||||
|
return pty;
|
||||||
|
});
|
||||||
|
vi.doMock("node-pty", () => ({ spawn }));
|
||||||
|
|
||||||
|
const { createBashTool } = await import("./bash-tools.js");
|
||||||
|
const tool = createBashTool({ backgroundMs: 10, timeoutSec: 1 });
|
||||||
|
const result = await tool.execute("call", {
|
||||||
|
command: "ignored",
|
||||||
|
stdinMode: "pty",
|
||||||
|
});
|
||||||
|
|
||||||
|
const text = result.content.find((c) => c.type === "text")?.text ?? "";
|
||||||
|
expect(text).toContain("hello");
|
||||||
|
expect(text).not.toContain("Warning:");
|
||||||
|
|
||||||
|
vi.doUnmock("node-pty");
|
||||||
|
});
|
||||||
|
});
|
||||||
+231
-183
@@ -1,11 +1,11 @@
|
|||||||
import { type ChildProcessWithoutNullStreams, spawn } from "node:child_process";
|
import { type ChildProcessWithoutNullStreams, spawn } from "node:child_process";
|
||||||
import { randomUUID } from "node:crypto";
|
import { randomUUID } from "node:crypto";
|
||||||
import { existsSync, statSync } from "node:fs";
|
import { existsSync, statSync } from "node:fs";
|
||||||
import fs from "node:fs/promises";
|
|
||||||
import { homedir } from "node:os";
|
import { homedir } from "node:os";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import type { AgentTool, AgentToolResult } from "@mariozechner/pi-agent-core";
|
import type { AgentTool, AgentToolResult } from "@mariozechner/pi-agent-core";
|
||||||
import { Type } from "@sinclair/typebox";
|
import { Type } from "@sinclair/typebox";
|
||||||
|
import type { IPty } from "node-pty";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
addSession,
|
addSession,
|
||||||
@@ -18,9 +18,9 @@ import {
|
|||||||
listRunningSessions,
|
listRunningSessions,
|
||||||
markBackgrounded,
|
markBackgrounded,
|
||||||
markExited,
|
markExited,
|
||||||
|
type ProcessStdinMode,
|
||||||
setJobTtlMs,
|
setJobTtlMs,
|
||||||
} from "./bash-process-registry.js";
|
} from "./bash-process-registry.js";
|
||||||
import { assertSandboxPath } from "./sandbox-paths.js";
|
|
||||||
import {
|
import {
|
||||||
getShellConfig,
|
getShellConfig,
|
||||||
killProcessTree,
|
killProcessTree,
|
||||||
@@ -34,8 +34,23 @@ const DEFAULT_MAX_OUTPUT = clampNumber(
|
|||||||
1_000,
|
1_000,
|
||||||
150_000,
|
150_000,
|
||||||
);
|
);
|
||||||
|
const DEFAULT_SHELL_PATH = "/bin/sh";
|
||||||
const DEFAULT_PATH =
|
const DEFAULT_PATH =
|
||||||
"/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin";
|
"/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin";
|
||||||
|
const DEFAULT_PTY_NAME = "xterm-256color";
|
||||||
|
|
||||||
|
type PtyModule = typeof import("node-pty");
|
||||||
|
type PtyLoadResult = { module: PtyModule | null; error?: unknown };
|
||||||
|
let ptyModulePromise: Promise<PtyLoadResult> | null = null;
|
||||||
|
|
||||||
|
async function loadPtyModule(): Promise<PtyLoadResult> {
|
||||||
|
if (!ptyModulePromise) {
|
||||||
|
ptyModulePromise = import("node-pty")
|
||||||
|
.then((mod) => ({ module: mod }))
|
||||||
|
.catch((error) => ({ module: null, error }));
|
||||||
|
}
|
||||||
|
return ptyModulePromise;
|
||||||
|
}
|
||||||
|
|
||||||
const stringEnum = (
|
const stringEnum = (
|
||||||
values: readonly string[],
|
values: readonly string[],
|
||||||
@@ -52,20 +67,12 @@ const stringEnum = (
|
|||||||
export type BashToolDefaults = {
|
export type BashToolDefaults = {
|
||||||
backgroundMs?: number;
|
backgroundMs?: number;
|
||||||
timeoutSec?: number;
|
timeoutSec?: number;
|
||||||
sandbox?: BashSandboxConfig;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export type ProcessToolDefaults = {
|
export type ProcessToolDefaults = {
|
||||||
cleanupMs?: number;
|
cleanupMs?: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type BashSandboxConfig = {
|
|
||||||
containerName: string;
|
|
||||||
workspaceDir: string;
|
|
||||||
containerWorkdir: string;
|
|
||||||
env?: Record<string, string>;
|
|
||||||
};
|
|
||||||
|
|
||||||
const bashSchema = Type.Object({
|
const bashSchema = Type.Object({
|
||||||
command: Type.String({ description: "Bash command to execute" }),
|
command: Type.String({ description: "Bash command to execute" }),
|
||||||
workdir: Type.Optional(
|
workdir: Type.Optional(
|
||||||
@@ -74,7 +81,7 @@ const bashSchema = Type.Object({
|
|||||||
env: Type.Optional(Type.Record(Type.String(), Type.String())),
|
env: Type.Optional(Type.Record(Type.String(), Type.String())),
|
||||||
yieldMs: Type.Optional(
|
yieldMs: Type.Optional(
|
||||||
Type.Number({
|
Type.Number({
|
||||||
description: "Milliseconds to wait before backgrounding (default 10000)",
|
description: "Milliseconds to wait before backgrounding (default 20000)",
|
||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
background: Type.Optional(
|
background: Type.Optional(
|
||||||
@@ -85,6 +92,11 @@ const bashSchema = Type.Object({
|
|||||||
description: "Timeout in seconds (optional, kills process on expiry)",
|
description: "Timeout in seconds (optional, kills process on expiry)",
|
||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
|
stdinMode: Type.Optional(
|
||||||
|
stringEnum(["pipe", "pty"] as const, {
|
||||||
|
description: "stdin mode (pipe or pty when node-pty is available)",
|
||||||
|
}),
|
||||||
|
),
|
||||||
});
|
});
|
||||||
|
|
||||||
export type BashToolDetails =
|
export type BashToolDetails =
|
||||||
@@ -108,7 +120,7 @@ export function createBashTool(
|
|||||||
): AgentTool<any, BashToolDetails> {
|
): AgentTool<any, BashToolDetails> {
|
||||||
const defaultBackgroundMs = clampNumber(
|
const defaultBackgroundMs = clampNumber(
|
||||||
defaults?.backgroundMs ?? readEnvInt("PI_BASH_YIELD_MS"),
|
defaults?.backgroundMs ?? readEnvInt("PI_BASH_YIELD_MS"),
|
||||||
10_000,
|
20_000,
|
||||||
10,
|
10,
|
||||||
120_000,
|
120_000,
|
||||||
);
|
);
|
||||||
@@ -121,7 +133,7 @@ export function createBashTool(
|
|||||||
name: "bash",
|
name: "bash",
|
||||||
label: "bash",
|
label: "bash",
|
||||||
description:
|
description:
|
||||||
"Execute bash with background continuation. Use yieldMs/background to continue later via process tool. For real TTY mode, use the tmux skill.",
|
"Execute bash with background continuation. Use yieldMs/background to continue later via process tool.",
|
||||||
parameters: bashSchema,
|
parameters: bashSchema,
|
||||||
execute: async (_toolCallId, args, signal, onUpdate) => {
|
execute: async (_toolCallId, args, signal, onUpdate) => {
|
||||||
const params = args as {
|
const params = args as {
|
||||||
@@ -131,6 +143,7 @@ export function createBashTool(
|
|||||||
yieldMs?: number;
|
yieldMs?: number;
|
||||||
background?: boolean;
|
background?: boolean;
|
||||||
timeout?: number;
|
timeout?: number;
|
||||||
|
stdinMode?: "pipe" | "pty";
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!params.command) {
|
if (!params.command) {
|
||||||
@@ -149,61 +162,91 @@ export function createBashTool(
|
|||||||
const startedAt = Date.now();
|
const startedAt = Date.now();
|
||||||
const sessionId = randomUUID();
|
const sessionId = randomUUID();
|
||||||
const warnings: string[] = [];
|
const warnings: string[] = [];
|
||||||
const sandbox = defaults?.sandbox;
|
const workdir = resolveWorkdir(
|
||||||
const rawWorkdir = params.workdir?.trim() || process.cwd();
|
params.workdir?.trim() || process.cwd(),
|
||||||
let workdir = rawWorkdir;
|
warnings,
|
||||||
let containerWorkdir = sandbox?.containerWorkdir;
|
);
|
||||||
if (sandbox) {
|
|
||||||
const resolved = await resolveSandboxWorkdir({
|
|
||||||
workdir: rawWorkdir,
|
|
||||||
sandbox,
|
|
||||||
warnings,
|
|
||||||
});
|
|
||||||
workdir = resolved.hostWorkdir;
|
|
||||||
containerWorkdir = resolved.containerWorkdir;
|
|
||||||
} else {
|
|
||||||
workdir = resolveWorkdir(rawWorkdir, warnings);
|
|
||||||
}
|
|
||||||
|
|
||||||
const { shell, args: shellArgs } = getShellConfig();
|
const { shell, args: shellArgs } = getShellConfig();
|
||||||
const baseEnv = coerceEnv(process.env);
|
const env = params.env ? { ...process.env, ...params.env } : process.env;
|
||||||
const mergedEnv = params.env ? { ...baseEnv, ...params.env } : baseEnv;
|
const requestedStdinMode = params.stdinMode === "pty" ? "pty" : "pipe";
|
||||||
const env = sandbox
|
let stdinMode: ProcessStdinMode = requestedStdinMode;
|
||||||
? buildSandboxEnv({
|
let warning: string | null = null;
|
||||||
paramsEnv: params.env,
|
let child: ChildProcessWithoutNullStreams | undefined;
|
||||||
sandboxEnv: sandbox.env,
|
let pty: IPty | undefined;
|
||||||
containerWorkdir: containerWorkdir ?? sandbox.containerWorkdir,
|
|
||||||
})
|
if (stdinMode === "pty") {
|
||||||
: mergedEnv;
|
const { module: ptyModule, error: ptyError } = await loadPtyModule();
|
||||||
const child = sandbox
|
if (!ptyModule) {
|
||||||
? spawn(
|
warning =
|
||||||
"docker",
|
`Warning: node-pty failed to load${formatPtyError(ptyError)}; ` +
|
||||||
buildDockerExecArgs({
|
"falling back to pipe mode.";
|
||||||
containerName: sandbox.containerName,
|
stdinMode = "pipe";
|
||||||
command: params.command,
|
} else {
|
||||||
workdir: containerWorkdir ?? sandbox.containerWorkdir,
|
const ptyEnv = ensurePath({
|
||||||
env,
|
...env,
|
||||||
tty: false,
|
TERM: env.TERM ?? DEFAULT_PTY_NAME,
|
||||||
}),
|
} as Record<string, string>);
|
||||||
{
|
const ptyShell = resolveShellPath(shell, ptyEnv);
|
||||||
|
try {
|
||||||
|
pty = ptyModule.spawn(ptyShell, [...shellArgs, params.command], {
|
||||||
cwd: workdir,
|
cwd: workdir,
|
||||||
env: process.env,
|
env: ptyEnv,
|
||||||
detached: true,
|
name: ptyEnv.TERM || DEFAULT_PTY_NAME,
|
||||||
stdio: ["pipe", "pipe", "pipe"],
|
cols: 120,
|
||||||
},
|
rows: 30,
|
||||||
)
|
});
|
||||||
: spawn(shell, [...shellArgs, params.command], {
|
} catch (error) {
|
||||||
cwd: workdir,
|
if (
|
||||||
env,
|
ptyShell !== DEFAULT_SHELL_PATH &&
|
||||||
detached: true,
|
existsSync(DEFAULT_SHELL_PATH)
|
||||||
stdio: ["pipe", "pipe", "pipe"],
|
) {
|
||||||
});
|
try {
|
||||||
|
pty = ptyModule.spawn(
|
||||||
|
DEFAULT_SHELL_PATH,
|
||||||
|
[...shellArgs, params.command],
|
||||||
|
{
|
||||||
|
cwd: workdir,
|
||||||
|
env: ptyEnv,
|
||||||
|
name: ptyEnv.TERM || DEFAULT_PTY_NAME,
|
||||||
|
cols: 120,
|
||||||
|
rows: 30,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
} catch (fallbackError) {
|
||||||
|
warning =
|
||||||
|
`Warning: node-pty failed to start${formatPtyError(fallbackError)}; ` +
|
||||||
|
"falling back to pipe mode.";
|
||||||
|
stdinMode = "pipe";
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
warning =
|
||||||
|
`Warning: node-pty failed to start${formatPtyError(error)}; ` +
|
||||||
|
"falling back to pipe mode.";
|
||||||
|
stdinMode = "pipe";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (stdinMode === "pipe") {
|
||||||
|
child = spawn(shell, [...shellArgs, params.command], {
|
||||||
|
cwd: workdir,
|
||||||
|
env,
|
||||||
|
detached: true,
|
||||||
|
stdio: ["pipe", "pipe", "pipe"],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (warning) warnings.push(warning);
|
||||||
|
|
||||||
const session = {
|
const session = {
|
||||||
id: sessionId,
|
id: sessionId,
|
||||||
command: params.command,
|
command: params.command,
|
||||||
child,
|
child,
|
||||||
pid: child?.pid,
|
pty,
|
||||||
|
pid: child?.pid ?? pty?.pid,
|
||||||
|
stdinMode,
|
||||||
startedAt,
|
startedAt,
|
||||||
cwd: workdir,
|
cwd: workdir,
|
||||||
maxOutputChars: maxOutput,
|
maxOutputChars: maxOutput,
|
||||||
@@ -266,21 +309,33 @@ export function createBashTool(
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
child.stdout.on("data", (data) => {
|
if (child) {
|
||||||
const str = sanitizeBinaryOutput(data.toString());
|
child.stdout.on("data", (data) => {
|
||||||
for (const chunk of chunkString(str)) {
|
const str = sanitizeBinaryOutput(data.toString());
|
||||||
appendOutput(session, "stdout", chunk);
|
for (const chunk of chunkString(str)) {
|
||||||
emitUpdate();
|
appendOutput(session, "stdout", chunk);
|
||||||
}
|
emitUpdate();
|
||||||
});
|
}
|
||||||
|
});
|
||||||
|
|
||||||
child.stderr.on("data", (data) => {
|
child.stderr.on("data", (data) => {
|
||||||
const str = sanitizeBinaryOutput(data.toString());
|
const str = sanitizeBinaryOutput(data.toString());
|
||||||
for (const chunk of chunkString(str)) {
|
for (const chunk of chunkString(str)) {
|
||||||
appendOutput(session, "stderr", chunk);
|
appendOutput(session, "stderr", chunk);
|
||||||
emitUpdate();
|
emitUpdate();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pty) {
|
||||||
|
pty.onData((data) => {
|
||||||
|
const str = sanitizeBinaryOutput(data);
|
||||||
|
for (const chunk of chunkString(str)) {
|
||||||
|
appendOutput(session, "stdout", chunk);
|
||||||
|
emitUpdate();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return new Promise<AgentToolResult<BashToolDetails>>(
|
return new Promise<AgentToolResult<BashToolDetails>>(
|
||||||
(resolve, reject) => {
|
(resolve, reject) => {
|
||||||
@@ -379,16 +434,24 @@ export function createBashTool(
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
child.once("exit", (code, exitSignal) => {
|
if (child) {
|
||||||
handleExit(code, exitSignal);
|
child.once("exit", (code, exitSignal) => {
|
||||||
});
|
handleExit(code, exitSignal);
|
||||||
|
});
|
||||||
|
|
||||||
child.once("error", (err) => {
|
child.once("error", (err) => {
|
||||||
if (yieldTimer) clearTimeout(yieldTimer);
|
if (yieldTimer) clearTimeout(yieldTimer);
|
||||||
if (timeoutTimer) clearTimeout(timeoutTimer);
|
if (timeoutTimer) clearTimeout(timeoutTimer);
|
||||||
markExited(session, null, null, "failed");
|
markExited(session, null, null, "failed");
|
||||||
settle(() => reject(err));
|
settle(() => reject(err));
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pty) {
|
||||||
|
pty.onExit(({ exitCode, signal }) => {
|
||||||
|
handleExit(exitCode ?? null, signal ?? null);
|
||||||
|
});
|
||||||
|
}
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
@@ -684,25 +747,43 @@ export function createProcessTool(
|
|||||||
details: { status: "failed" },
|
details: { status: "failed" },
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
if (!session.child?.stdin || session.child.stdin.destroyed) {
|
if (session.stdinMode === "pty") {
|
||||||
return {
|
if (!session.pty || session.exited) {
|
||||||
content: [
|
return {
|
||||||
{
|
content: [
|
||||||
type: "text",
|
{
|
||||||
text: `Session ${params.sessionId} stdin is not writable.`,
|
type: "text",
|
||||||
},
|
text: `Session ${params.sessionId} stdin is not writable.`,
|
||||||
],
|
},
|
||||||
details: { status: "failed" },
|
],
|
||||||
};
|
details: { status: "failed" },
|
||||||
}
|
};
|
||||||
await new Promise<void>((resolve, reject) => {
|
}
|
||||||
session.child?.stdin.write(params.data ?? "", (err) => {
|
session.pty.write(params.data ?? "");
|
||||||
if (err) reject(err);
|
if (params.eof) {
|
||||||
else resolve();
|
session.pty.write("\x04");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (!session.child?.stdin || session.child.stdin.destroyed) {
|
||||||
|
return {
|
||||||
|
content: [
|
||||||
|
{
|
||||||
|
type: "text",
|
||||||
|
text: `Session ${params.sessionId} stdin is not writable.`,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
details: { status: "failed" },
|
||||||
|
};
|
||||||
|
}
|
||||||
|
await new Promise<void>((resolve, reject) => {
|
||||||
|
session.child?.stdin.write(params.data ?? "", (err) => {
|
||||||
|
if (err) reject(err);
|
||||||
|
else resolve();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
if (params.eof) {
|
||||||
if (params.eof) {
|
session.child.stdin.end();
|
||||||
session.child.stdin.end();
|
}
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
content: [
|
content: [
|
||||||
@@ -825,94 +906,23 @@ export function createProcessTool(
|
|||||||
|
|
||||||
export const processTool = createProcessTool();
|
export const processTool = createProcessTool();
|
||||||
|
|
||||||
function buildSandboxEnv(params: {
|
|
||||||
paramsEnv?: Record<string, string>;
|
|
||||||
sandboxEnv?: Record<string, string>;
|
|
||||||
containerWorkdir: string;
|
|
||||||
}) {
|
|
||||||
const env: Record<string, string> = {
|
|
||||||
PATH: DEFAULT_PATH,
|
|
||||||
HOME: params.containerWorkdir,
|
|
||||||
};
|
|
||||||
for (const [key, value] of Object.entries(params.sandboxEnv ?? {})) {
|
|
||||||
env[key] = value;
|
|
||||||
}
|
|
||||||
for (const [key, value] of Object.entries(params.paramsEnv ?? {})) {
|
|
||||||
env[key] = value;
|
|
||||||
}
|
|
||||||
return env;
|
|
||||||
}
|
|
||||||
|
|
||||||
function coerceEnv(env?: NodeJS.ProcessEnv | Record<string, string>) {
|
|
||||||
const record: Record<string, string> = {};
|
|
||||||
if (!env) return record;
|
|
||||||
for (const [key, value] of Object.entries(env)) {
|
|
||||||
if (typeof value === "string") record[key] = value;
|
|
||||||
}
|
|
||||||
return record;
|
|
||||||
}
|
|
||||||
|
|
||||||
function buildDockerExecArgs(params: {
|
|
||||||
containerName: string;
|
|
||||||
command: string;
|
|
||||||
workdir?: string;
|
|
||||||
env: Record<string, string>;
|
|
||||||
tty: boolean;
|
|
||||||
}) {
|
|
||||||
const args = ["exec", "-i"];
|
|
||||||
if (params.tty) args.push("-t");
|
|
||||||
if (params.workdir) {
|
|
||||||
args.push("-w", params.workdir);
|
|
||||||
}
|
|
||||||
for (const [key, value] of Object.entries(params.env)) {
|
|
||||||
args.push("-e", `${key}=${value}`);
|
|
||||||
}
|
|
||||||
args.push(params.containerName, "sh", "-lc", params.command);
|
|
||||||
return args;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function resolveSandboxWorkdir(params: {
|
|
||||||
workdir: string;
|
|
||||||
sandbox: BashSandboxConfig;
|
|
||||||
warnings: string[];
|
|
||||||
}) {
|
|
||||||
const fallback = params.sandbox.workspaceDir;
|
|
||||||
try {
|
|
||||||
const resolved = await assertSandboxPath({
|
|
||||||
filePath: params.workdir,
|
|
||||||
cwd: process.cwd(),
|
|
||||||
root: params.sandbox.workspaceDir,
|
|
||||||
});
|
|
||||||
const stats = await fs.stat(resolved.resolved);
|
|
||||||
if (!stats.isDirectory()) {
|
|
||||||
throw new Error("workdir is not a directory");
|
|
||||||
}
|
|
||||||
const relative = resolved.relative
|
|
||||||
? resolved.relative.split(path.sep).join(path.posix.sep)
|
|
||||||
: "";
|
|
||||||
const containerWorkdir = relative
|
|
||||||
? path.posix.join(params.sandbox.containerWorkdir, relative)
|
|
||||||
: params.sandbox.containerWorkdir;
|
|
||||||
return { hostWorkdir: resolved.resolved, containerWorkdir };
|
|
||||||
} catch {
|
|
||||||
params.warnings.push(
|
|
||||||
`Warning: workdir "${params.workdir}" is unavailable; using "${fallback}".`,
|
|
||||||
);
|
|
||||||
return {
|
|
||||||
hostWorkdir: fallback,
|
|
||||||
containerWorkdir: params.sandbox.containerWorkdir,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function killSession(session: {
|
function killSession(session: {
|
||||||
pid?: number;
|
pid?: number;
|
||||||
|
stdinMode: ProcessStdinMode;
|
||||||
|
pty?: IPty;
|
||||||
child?: ChildProcessWithoutNullStreams;
|
child?: ChildProcessWithoutNullStreams;
|
||||||
}) {
|
}) {
|
||||||
const pid = session.pid ?? session.child?.pid;
|
const pid = session.pid ?? session.child?.pid ?? session.pty?.pid;
|
||||||
if (pid) {
|
if (pid) {
|
||||||
killProcessTree(pid);
|
killProcessTree(pid);
|
||||||
}
|
}
|
||||||
|
if (session.stdinMode === "pty") {
|
||||||
|
try {
|
||||||
|
session.pty?.kill();
|
||||||
|
} catch {
|
||||||
|
// ignore kill failures
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function resolveWorkdir(workdir: string, warnings: string[]) {
|
function resolveWorkdir(workdir: string, warnings: string[]) {
|
||||||
@@ -939,6 +949,44 @@ function safeCwd() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function ensurePath(env: Record<string, string>) {
|
||||||
|
if (!env.PATH?.trim()) {
|
||||||
|
env.PATH = DEFAULT_PATH;
|
||||||
|
}
|
||||||
|
return env;
|
||||||
|
}
|
||||||
|
|
||||||
|
function resolveShellPath(shell: string, env: Record<string, string>) {
|
||||||
|
if (process.platform === "win32") return shell;
|
||||||
|
if (shell.includes("/") && existsSync(shell)) {
|
||||||
|
return shell;
|
||||||
|
}
|
||||||
|
const searchPath = env.PATH ?? "";
|
||||||
|
for (const segment of searchPath.split(path.delimiter)) {
|
||||||
|
if (!segment) continue;
|
||||||
|
const candidate = path.join(segment, shell);
|
||||||
|
if (existsSync(candidate)) return candidate;
|
||||||
|
}
|
||||||
|
if (existsSync(DEFAULT_SHELL_PATH)) {
|
||||||
|
return DEFAULT_SHELL_PATH;
|
||||||
|
}
|
||||||
|
return shell;
|
||||||
|
}
|
||||||
|
|
||||||
|
function formatPtyError(error: unknown) {
|
||||||
|
if (!error) return "";
|
||||||
|
if (typeof error === "string") return ` (${error})`;
|
||||||
|
if (error instanceof Error) {
|
||||||
|
const firstLine = error.message.split(/\r?\n/)[0]?.trim();
|
||||||
|
return firstLine ? ` (${firstLine})` : "";
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return ` (${JSON.stringify(error)})`;
|
||||||
|
} catch {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function clampNumber(
|
function clampNumber(
|
||||||
value: number | undefined,
|
value: number | undefined,
|
||||||
defaultValue: number,
|
defaultValue: number,
|
||||||
|
|||||||
@@ -1,247 +0,0 @@
|
|||||||
import { describe, expect, it, vi } from "vitest";
|
|
||||||
|
|
||||||
const callGatewayMock = vi.fn();
|
|
||||||
vi.mock("../gateway/call.js", () => ({
|
|
||||||
callGateway: (opts: unknown) => callGatewayMock(opts),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("../config/config.js", () => ({
|
|
||||||
loadConfig: () => ({
|
|
||||||
session: { mainKey: "main", scope: "per-sender" },
|
|
||||||
}),
|
|
||||||
resolveGatewayPort: () => 18789,
|
|
||||||
}));
|
|
||||||
|
|
||||||
import { createClawdisTools } from "./clawdis-tools.js";
|
|
||||||
|
|
||||||
describe("sessions tools", () => {
|
|
||||||
it("sessions_list filters kinds and includes messages", async () => {
|
|
||||||
callGatewayMock.mockReset();
|
|
||||||
callGatewayMock.mockImplementation(async (opts: unknown) => {
|
|
||||||
const request = opts as { method?: string };
|
|
||||||
if (request.method === "sessions.list") {
|
|
||||||
return {
|
|
||||||
path: "/tmp/sessions.json",
|
|
||||||
sessions: [
|
|
||||||
{
|
|
||||||
key: "main",
|
|
||||||
kind: "direct",
|
|
||||||
sessionId: "s-main",
|
|
||||||
updatedAt: 10,
|
|
||||||
lastChannel: "whatsapp",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
key: "discord:group:dev",
|
|
||||||
kind: "group",
|
|
||||||
sessionId: "s-group",
|
|
||||||
updatedAt: 11,
|
|
||||||
surface: "discord",
|
|
||||||
displayName: "discord:g-dev",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
key: "cron:job-1",
|
|
||||||
kind: "direct",
|
|
||||||
sessionId: "s-cron",
|
|
||||||
updatedAt: 9,
|
|
||||||
},
|
|
||||||
{ key: "global", kind: "global" },
|
|
||||||
{ key: "unknown", kind: "unknown" },
|
|
||||||
],
|
|
||||||
};
|
|
||||||
}
|
|
||||||
if (request.method === "chat.history") {
|
|
||||||
return {
|
|
||||||
messages: [
|
|
||||||
{ role: "toolResult", content: [] },
|
|
||||||
{
|
|
||||||
role: "assistant",
|
|
||||||
content: [{ type: "text", text: "hi" }],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
};
|
|
||||||
}
|
|
||||||
return {};
|
|
||||||
});
|
|
||||||
|
|
||||||
const tool = createClawdisTools().find(
|
|
||||||
(candidate) => candidate.name === "sessions_list",
|
|
||||||
);
|
|
||||||
expect(tool).toBeDefined();
|
|
||||||
if (!tool) throw new Error("missing sessions_list tool");
|
|
||||||
|
|
||||||
const result = await tool.execute("call1", { messageLimit: 1 });
|
|
||||||
const details = result.details as {
|
|
||||||
sessions?: Array<Record<string, unknown>>;
|
|
||||||
};
|
|
||||||
expect(details.sessions).toHaveLength(3);
|
|
||||||
const main = details.sessions?.find((s) => s.key === "main");
|
|
||||||
expect(main?.provider).toBe("whatsapp");
|
|
||||||
expect(main?.messages?.length).toBe(1);
|
|
||||||
expect(main?.messages?.[0]?.role).toBe("assistant");
|
|
||||||
|
|
||||||
const cronOnly = await tool.execute("call2", { kinds: ["cron"] });
|
|
||||||
const cronDetails = cronOnly.details as {
|
|
||||||
sessions?: Array<Record<string, unknown>>;
|
|
||||||
};
|
|
||||||
expect(cronDetails.sessions).toHaveLength(1);
|
|
||||||
expect(cronDetails.sessions?.[0]?.kind).toBe("cron");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("sessions_history filters tool messages by default", async () => {
|
|
||||||
callGatewayMock.mockReset();
|
|
||||||
callGatewayMock.mockImplementation(async (opts: unknown) => {
|
|
||||||
const request = opts as { method?: string };
|
|
||||||
if (request.method === "chat.history") {
|
|
||||||
return {
|
|
||||||
messages: [
|
|
||||||
{ role: "toolResult", content: [] },
|
|
||||||
{ role: "assistant", content: [{ type: "text", text: "ok" }] },
|
|
||||||
],
|
|
||||||
};
|
|
||||||
}
|
|
||||||
return {};
|
|
||||||
});
|
|
||||||
|
|
||||||
const tool = createClawdisTools().find(
|
|
||||||
(candidate) => candidate.name === "sessions_history",
|
|
||||||
);
|
|
||||||
expect(tool).toBeDefined();
|
|
||||||
if (!tool) throw new Error("missing sessions_history tool");
|
|
||||||
|
|
||||||
const result = await tool.execute("call3", { sessionKey: "main" });
|
|
||||||
const details = result.details as { messages?: unknown[] };
|
|
||||||
expect(details.messages).toHaveLength(1);
|
|
||||||
expect(details.messages?.[0]?.role).toBe("assistant");
|
|
||||||
|
|
||||||
const withTools = await tool.execute("call4", {
|
|
||||||
sessionKey: "main",
|
|
||||||
includeTools: true,
|
|
||||||
});
|
|
||||||
const withToolsDetails = withTools.details as { messages?: unknown[] };
|
|
||||||
expect(withToolsDetails.messages).toHaveLength(2);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("sessions_send supports fire-and-forget and wait", async () => {
|
|
||||||
callGatewayMock.mockReset();
|
|
||||||
const calls: Array<{ method?: string; params?: unknown }> = [];
|
|
||||||
let agentCallCount = 0;
|
|
||||||
let _historyCallCount = 0;
|
|
||||||
let sendCallCount = 0;
|
|
||||||
let waitRunId: string | undefined;
|
|
||||||
let nextHistoryIsWaitReply = false;
|
|
||||||
callGatewayMock.mockImplementation(async (opts: unknown) => {
|
|
||||||
const request = opts as { method?: string; params?: unknown };
|
|
||||||
calls.push(request);
|
|
||||||
if (request.method === "agent") {
|
|
||||||
agentCallCount += 1;
|
|
||||||
const runId = `run-${agentCallCount}`;
|
|
||||||
const params = request.params as { message?: string } | undefined;
|
|
||||||
if (params?.message === "wait") {
|
|
||||||
waitRunId = runId;
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
runId,
|
|
||||||
status: "accepted",
|
|
||||||
acceptedAt: 1234 + agentCallCount,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
if (request.method === "agent.wait") {
|
|
||||||
const params = request.params as { runId?: string } | undefined;
|
|
||||||
if (params?.runId && params.runId === waitRunId) {
|
|
||||||
nextHistoryIsWaitReply = true;
|
|
||||||
}
|
|
||||||
return { runId: params?.runId ?? "run-1", status: "ok" };
|
|
||||||
}
|
|
||||||
if (request.method === "chat.history") {
|
|
||||||
_historyCallCount += 1;
|
|
||||||
const text = nextHistoryIsWaitReply ? "done" : "ANNOUNCE_SKIP";
|
|
||||||
nextHistoryIsWaitReply = false;
|
|
||||||
return {
|
|
||||||
messages: [
|
|
||||||
{
|
|
||||||
role: "assistant",
|
|
||||||
content: [
|
|
||||||
{
|
|
||||||
type: "text",
|
|
||||||
text,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
timestamp: 20,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
};
|
|
||||||
}
|
|
||||||
if (request.method === "send") {
|
|
||||||
sendCallCount += 1;
|
|
||||||
return { messageId: "m1" };
|
|
||||||
}
|
|
||||||
return {};
|
|
||||||
});
|
|
||||||
|
|
||||||
const tool = createClawdisTools().find(
|
|
||||||
(candidate) => candidate.name === "sessions_send",
|
|
||||||
);
|
|
||||||
expect(tool).toBeDefined();
|
|
||||||
if (!tool) throw new Error("missing sessions_send tool");
|
|
||||||
|
|
||||||
const fire = await tool.execute("call5", {
|
|
||||||
sessionKey: "main",
|
|
||||||
message: "ping",
|
|
||||||
timeoutSeconds: 0,
|
|
||||||
});
|
|
||||||
expect(fire.details).toMatchObject({ status: "accepted", runId: "run-1" });
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
||||||
|
|
||||||
const waitPromise = tool.execute("call6", {
|
|
||||||
sessionKey: "main",
|
|
||||||
message: "wait",
|
|
||||||
timeoutSeconds: 1,
|
|
||||||
});
|
|
||||||
const waited = await waitPromise;
|
|
||||||
expect(waited.details).toMatchObject({
|
|
||||||
status: "ok",
|
|
||||||
reply: "done",
|
|
||||||
});
|
|
||||||
expect(typeof (waited.details as { runId?: string }).runId).toBe("string");
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
||||||
|
|
||||||
const agentCalls = calls.filter((call) => call.method === "agent");
|
|
||||||
const waitCalls = calls.filter((call) => call.method === "agent.wait");
|
|
||||||
const historyOnlyCalls = calls.filter(
|
|
||||||
(call) => call.method === "chat.history",
|
|
||||||
);
|
|
||||||
expect(agentCalls).toHaveLength(4);
|
|
||||||
for (const call of agentCalls) {
|
|
||||||
expect(call.params).toMatchObject({ lane: "nested" });
|
|
||||||
}
|
|
||||||
expect(
|
|
||||||
agentCalls.some(
|
|
||||||
(call) =>
|
|
||||||
typeof (call.params as { extraSystemPrompt?: string })
|
|
||||||
?.extraSystemPrompt === "string" &&
|
|
||||||
(
|
|
||||||
call.params as { extraSystemPrompt?: string }
|
|
||||||
)?.extraSystemPrompt?.includes("Agent-to-agent message context"),
|
|
||||||
),
|
|
||||||
).toBe(true);
|
|
||||||
expect(
|
|
||||||
agentCalls.some(
|
|
||||||
(call) =>
|
|
||||||
typeof (call.params as { extraSystemPrompt?: string })
|
|
||||||
?.extraSystemPrompt === "string" &&
|
|
||||||
(
|
|
||||||
call.params as { extraSystemPrompt?: string }
|
|
||||||
)?.extraSystemPrompt?.includes("Agent-to-agent post step"),
|
|
||||||
),
|
|
||||||
).toBe(true);
|
|
||||||
expect(waitCalls).toHaveLength(3);
|
|
||||||
expect(historyOnlyCalls).toHaveLength(3);
|
|
||||||
expect(
|
|
||||||
waitCalls.some(
|
|
||||||
(call) =>
|
|
||||||
typeof (call.params as { afterMs?: number })?.afterMs === "number",
|
|
||||||
),
|
|
||||||
).toBe(true);
|
|
||||||
expect(sendCallCount).toBe(0);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
+7
-793
@@ -1,6 +1,5 @@
|
|||||||
import crypto from "node:crypto";
|
import crypto from "node:crypto";
|
||||||
import fs from "node:fs/promises";
|
import fs from "node:fs/promises";
|
||||||
import path from "node:path";
|
|
||||||
|
|
||||||
import type { AgentTool, AgentToolResult } from "@mariozechner/pi-agent-core";
|
import type { AgentTool, AgentToolResult } from "@mariozechner/pi-agent-core";
|
||||||
import { Type } from "@sinclair/typebox";
|
import { Type } from "@sinclair/typebox";
|
||||||
@@ -41,11 +40,7 @@ import {
|
|||||||
writeScreenRecordToFile,
|
writeScreenRecordToFile,
|
||||||
} from "../cli/nodes-screen.js";
|
} from "../cli/nodes-screen.js";
|
||||||
import { parseDurationMs } from "../cli/parse-duration.js";
|
import { parseDurationMs } from "../cli/parse-duration.js";
|
||||||
import {
|
import { type DiscordActionConfig, loadConfig } from "../config/config.js";
|
||||||
type ClawdisConfig,
|
|
||||||
type DiscordActionConfig,
|
|
||||||
loadConfig,
|
|
||||||
} from "../config/config.js";
|
|
||||||
import {
|
import {
|
||||||
addRoleDiscord,
|
addRoleDiscord,
|
||||||
banMemberDiscord,
|
banMemberDiscord,
|
||||||
@@ -213,130 +208,6 @@ function jsonResult(payload: unknown): AgentToolResult<unknown> {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
type SessionKind = "main" | "group" | "cron" | "hook" | "node" | "other";
|
|
||||||
type SessionListRow = {
|
|
||||||
key: string;
|
|
||||||
kind: SessionKind;
|
|
||||||
provider: string;
|
|
||||||
displayName?: string;
|
|
||||||
updatedAt?: number | null;
|
|
||||||
sessionId?: string;
|
|
||||||
model?: string;
|
|
||||||
contextTokens?: number | null;
|
|
||||||
totalTokens?: number | null;
|
|
||||||
thinkingLevel?: string;
|
|
||||||
verboseLevel?: string;
|
|
||||||
systemSent?: boolean;
|
|
||||||
abortedLastRun?: boolean;
|
|
||||||
sendPolicy?: string;
|
|
||||||
lastChannel?: string;
|
|
||||||
lastTo?: string;
|
|
||||||
transcriptPath?: string;
|
|
||||||
messages?: unknown[];
|
|
||||||
};
|
|
||||||
|
|
||||||
function normalizeKey(value?: string) {
|
|
||||||
const trimmed = value?.trim();
|
|
||||||
return trimmed ? trimmed : undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
function resolveMainSessionAlias(cfg: ClawdisConfig) {
|
|
||||||
const mainKey = normalizeKey(cfg.session?.mainKey) ?? "main";
|
|
||||||
const scope = cfg.session?.scope ?? "per-sender";
|
|
||||||
const alias = scope === "global" ? "global" : mainKey;
|
|
||||||
return { mainKey, alias, scope };
|
|
||||||
}
|
|
||||||
|
|
||||||
function resolveDisplaySessionKey(params: {
|
|
||||||
key: string;
|
|
||||||
alias: string;
|
|
||||||
mainKey: string;
|
|
||||||
}) {
|
|
||||||
if (params.key === params.alias) return "main";
|
|
||||||
if (params.key === params.mainKey) return "main";
|
|
||||||
return params.key;
|
|
||||||
}
|
|
||||||
|
|
||||||
function resolveInternalSessionKey(params: {
|
|
||||||
key: string;
|
|
||||||
alias: string;
|
|
||||||
mainKey: string;
|
|
||||||
}) {
|
|
||||||
if (params.key === "main") return params.alias;
|
|
||||||
return params.key;
|
|
||||||
}
|
|
||||||
|
|
||||||
function classifySessionKind(params: {
|
|
||||||
key: string;
|
|
||||||
gatewayKind?: string | null;
|
|
||||||
alias: string;
|
|
||||||
mainKey: string;
|
|
||||||
}): SessionKind {
|
|
||||||
const key = params.key;
|
|
||||||
if (key === params.alias || key === params.mainKey) return "main";
|
|
||||||
if (key.startsWith("cron:")) return "cron";
|
|
||||||
if (key.startsWith("hook:")) return "hook";
|
|
||||||
if (key.startsWith("node-") || key.startsWith("node:")) return "node";
|
|
||||||
if (params.gatewayKind === "group") return "group";
|
|
||||||
if (
|
|
||||||
key.startsWith("group:") ||
|
|
||||||
key.includes(":group:") ||
|
|
||||||
key.includes(":channel:")
|
|
||||||
) {
|
|
||||||
return "group";
|
|
||||||
}
|
|
||||||
return "other";
|
|
||||||
}
|
|
||||||
|
|
||||||
function deriveProvider(params: {
|
|
||||||
key: string;
|
|
||||||
kind: SessionKind;
|
|
||||||
surface?: string | null;
|
|
||||||
lastChannel?: string | null;
|
|
||||||
}): string {
|
|
||||||
if (
|
|
||||||
params.kind === "cron" ||
|
|
||||||
params.kind === "hook" ||
|
|
||||||
params.kind === "node"
|
|
||||||
)
|
|
||||||
return "internal";
|
|
||||||
const surface = normalizeKey(params.surface ?? undefined);
|
|
||||||
if (surface) return surface;
|
|
||||||
const lastChannel = normalizeKey(params.lastChannel ?? undefined);
|
|
||||||
if (lastChannel) return lastChannel;
|
|
||||||
const parts = params.key.split(":").filter(Boolean);
|
|
||||||
if (parts.length >= 3 && (parts[1] === "group" || parts[1] === "channel")) {
|
|
||||||
return parts[0];
|
|
||||||
}
|
|
||||||
return "unknown";
|
|
||||||
}
|
|
||||||
|
|
||||||
function stripToolMessages(messages: unknown[]): unknown[] {
|
|
||||||
return messages.filter((msg) => {
|
|
||||||
if (!msg || typeof msg !== "object") return true;
|
|
||||||
const role = (msg as { role?: unknown }).role;
|
|
||||||
return role !== "toolResult";
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function extractAssistantText(message: unknown): string | undefined {
|
|
||||||
if (!message || typeof message !== "object") return undefined;
|
|
||||||
if ((message as { role?: unknown }).role !== "assistant") return undefined;
|
|
||||||
const content = (message as { content?: unknown }).content;
|
|
||||||
if (!Array.isArray(content)) return undefined;
|
|
||||||
const chunks: string[] = [];
|
|
||||||
for (const block of content) {
|
|
||||||
if (!block || typeof block !== "object") continue;
|
|
||||||
if ((block as { type?: unknown }).type !== "text") continue;
|
|
||||||
const text = (block as { text?: unknown }).text;
|
|
||||||
if (typeof text === "string" && text.trim()) {
|
|
||||||
chunks.push(text);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const joined = chunks.join("").trim();
|
|
||||||
return joined ? joined : undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function imageResult(params: {
|
async function imageResult(params: {
|
||||||
label: string;
|
label: string;
|
||||||
path: string;
|
path: string;
|
||||||
@@ -385,7 +256,7 @@ async function imageResultFromFile(params: {
|
|||||||
function resolveBrowserBaseUrl(controlUrl?: string) {
|
function resolveBrowserBaseUrl(controlUrl?: string) {
|
||||||
const cfg = loadConfig();
|
const cfg = loadConfig();
|
||||||
const resolved = resolveBrowserConfig(cfg.browser);
|
const resolved = resolveBrowserConfig(cfg.browser);
|
||||||
if (!resolved.enabled && !controlUrl?.trim()) {
|
if (!resolved.enabled) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
"Browser control is disabled. Set browser.enabled=true in ~/.clawdis/clawdis.json.",
|
"Browser control is disabled. Set browser.enabled=true in ~/.clawdis/clawdis.json.",
|
||||||
);
|
);
|
||||||
@@ -704,9 +575,7 @@ const BrowserToolSchema = Type.Union([
|
|||||||
}),
|
}),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
function createBrowserTool(opts?: {
|
function createBrowserTool(): AnyAgentTool {
|
||||||
defaultControlUrl?: string;
|
|
||||||
}): AnyAgentTool {
|
|
||||||
return {
|
return {
|
||||||
label: "Browser",
|
label: "Browser",
|
||||||
name: "browser",
|
name: "browser",
|
||||||
@@ -717,9 +586,7 @@ function createBrowserTool(opts?: {
|
|||||||
const params = args as Record<string, unknown>;
|
const params = args as Record<string, unknown>;
|
||||||
const action = readStringParam(params, "action", { required: true });
|
const action = readStringParam(params, "action", { required: true });
|
||||||
const controlUrl = readStringParam(params, "controlUrl");
|
const controlUrl = readStringParam(params, "controlUrl");
|
||||||
const baseUrl = resolveBrowserBaseUrl(
|
const baseUrl = resolveBrowserBaseUrl(controlUrl);
|
||||||
controlUrl ?? opts?.defaultControlUrl,
|
|
||||||
);
|
|
||||||
|
|
||||||
switch (action) {
|
switch (action) {
|
||||||
case "status":
|
case "status":
|
||||||
@@ -1217,22 +1084,6 @@ const NodesToolSchema = Type.Union([
|
|||||||
includeAudio: Type.Optional(Type.Boolean()),
|
includeAudio: Type.Optional(Type.Boolean()),
|
||||||
outPath: Type.Optional(Type.String()),
|
outPath: Type.Optional(Type.String()),
|
||||||
}),
|
}),
|
||||||
Type.Object({
|
|
||||||
action: Type.Literal("location_get"),
|
|
||||||
gatewayUrl: Type.Optional(Type.String()),
|
|
||||||
gatewayToken: Type.Optional(Type.String()),
|
|
||||||
timeoutMs: Type.Optional(Type.Number()),
|
|
||||||
node: Type.String(),
|
|
||||||
maxAgeMs: Type.Optional(Type.Number()),
|
|
||||||
locationTimeoutMs: Type.Optional(Type.Number()),
|
|
||||||
desiredAccuracy: Type.Optional(
|
|
||||||
Type.Union([
|
|
||||||
Type.Literal("coarse"),
|
|
||||||
Type.Literal("balanced"),
|
|
||||||
Type.Literal("precise"),
|
|
||||||
]),
|
|
||||||
),
|
|
||||||
}),
|
|
||||||
]);
|
]);
|
||||||
|
|
||||||
function createNodesTool(): AnyAgentTool {
|
function createNodesTool(): AnyAgentTool {
|
||||||
@@ -1240,7 +1091,7 @@ function createNodesTool(): AnyAgentTool {
|
|||||||
label: "Nodes",
|
label: "Nodes",
|
||||||
name: "nodes",
|
name: "nodes",
|
||||||
description:
|
description:
|
||||||
"Discover and control paired nodes (status/describe/pairing/notify/camera/screen/location).",
|
"Discover and control paired nodes (status/describe/pairing/notify/camera/screen).",
|
||||||
parameters: NodesToolSchema,
|
parameters: NodesToolSchema,
|
||||||
execute: async (_toolCallId, args) => {
|
execute: async (_toolCallId, args) => {
|
||||||
const params = args as Record<string, unknown>;
|
const params = args as Record<string, unknown>;
|
||||||
@@ -1532,37 +1383,6 @@ function createNodesTool(): AnyAgentTool {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
case "location_get": {
|
|
||||||
const node = readStringParam(params, "node", { required: true });
|
|
||||||
const nodeId = await resolveNodeId(gatewayOpts, node);
|
|
||||||
const maxAgeMs =
|
|
||||||
typeof params.maxAgeMs === "number" &&
|
|
||||||
Number.isFinite(params.maxAgeMs)
|
|
||||||
? params.maxAgeMs
|
|
||||||
: undefined;
|
|
||||||
const desiredAccuracy =
|
|
||||||
params.desiredAccuracy === "coarse" ||
|
|
||||||
params.desiredAccuracy === "balanced" ||
|
|
||||||
params.desiredAccuracy === "precise"
|
|
||||||
? params.desiredAccuracy
|
|
||||||
: undefined;
|
|
||||||
const locationTimeoutMs =
|
|
||||||
typeof params.locationTimeoutMs === "number" &&
|
|
||||||
Number.isFinite(params.locationTimeoutMs)
|
|
||||||
? params.locationTimeoutMs
|
|
||||||
: undefined;
|
|
||||||
const raw = (await callGatewayTool("node.invoke", gatewayOpts, {
|
|
||||||
nodeId,
|
|
||||||
command: "location.get",
|
|
||||||
params: {
|
|
||||||
maxAgeMs,
|
|
||||||
desiredAccuracy,
|
|
||||||
timeoutMs: locationTimeoutMs,
|
|
||||||
},
|
|
||||||
idempotencyKey: crypto.randomUUID(),
|
|
||||||
})) as { payload?: unknown };
|
|
||||||
return jsonResult(raw?.payload ?? {});
|
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
throw new Error(`Unknown action: ${action}`);
|
throw new Error(`Unknown action: ${action}`);
|
||||||
}
|
}
|
||||||
@@ -2484,619 +2304,13 @@ function createGatewayTool(): AnyAgentTool {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const SessionsListToolSchema = Type.Object({
|
export function createClawdisTools(): AnyAgentTool[] {
|
||||||
kinds: Type.Optional(Type.Array(Type.String())),
|
|
||||||
limit: Type.Optional(Type.Integer({ minimum: 1 })),
|
|
||||||
activeMinutes: Type.Optional(Type.Integer({ minimum: 1 })),
|
|
||||||
messageLimit: Type.Optional(Type.Integer({ minimum: 0 })),
|
|
||||||
});
|
|
||||||
|
|
||||||
const SessionsHistoryToolSchema = Type.Object({
|
|
||||||
sessionKey: Type.String(),
|
|
||||||
limit: Type.Optional(Type.Integer({ minimum: 1 })),
|
|
||||||
includeTools: Type.Optional(Type.Boolean()),
|
|
||||||
});
|
|
||||||
|
|
||||||
const SessionsSendToolSchema = Type.Object({
|
|
||||||
sessionKey: Type.String(),
|
|
||||||
message: Type.String(),
|
|
||||||
timeoutSeconds: Type.Optional(Type.Integer({ minimum: 0 })),
|
|
||||||
});
|
|
||||||
|
|
||||||
function createSessionsListTool(): AnyAgentTool {
|
|
||||||
return {
|
|
||||||
label: "Sessions",
|
|
||||||
name: "sessions_list",
|
|
||||||
description: "List sessions with optional filters and last messages.",
|
|
||||||
parameters: SessionsListToolSchema,
|
|
||||||
execute: async (_toolCallId, args) => {
|
|
||||||
const params = args as Record<string, unknown>;
|
|
||||||
const cfg = loadConfig();
|
|
||||||
const { mainKey, alias } = resolveMainSessionAlias(cfg);
|
|
||||||
|
|
||||||
const kindsRaw = readStringArrayParam(params, "kinds")?.map((value) =>
|
|
||||||
value.trim().toLowerCase(),
|
|
||||||
);
|
|
||||||
const allowedKindsList = (kindsRaw ?? []).filter((value) =>
|
|
||||||
["main", "group", "cron", "hook", "node", "other"].includes(value),
|
|
||||||
);
|
|
||||||
const allowedKinds = allowedKindsList.length
|
|
||||||
? new Set(allowedKindsList)
|
|
||||||
: undefined;
|
|
||||||
|
|
||||||
const limit =
|
|
||||||
typeof params.limit === "number" && Number.isFinite(params.limit)
|
|
||||||
? Math.max(1, Math.floor(params.limit))
|
|
||||||
: undefined;
|
|
||||||
const activeMinutes =
|
|
||||||
typeof params.activeMinutes === "number" &&
|
|
||||||
Number.isFinite(params.activeMinutes)
|
|
||||||
? Math.max(1, Math.floor(params.activeMinutes))
|
|
||||||
: undefined;
|
|
||||||
const messageLimitRaw =
|
|
||||||
typeof params.messageLimit === "number" &&
|
|
||||||
Number.isFinite(params.messageLimit)
|
|
||||||
? Math.max(0, Math.floor(params.messageLimit))
|
|
||||||
: 0;
|
|
||||||
const messageLimit = Math.min(messageLimitRaw, 20);
|
|
||||||
|
|
||||||
const list = (await callGateway({
|
|
||||||
method: "sessions.list",
|
|
||||||
params: {
|
|
||||||
limit,
|
|
||||||
activeMinutes,
|
|
||||||
includeGlobal: true,
|
|
||||||
includeUnknown: true,
|
|
||||||
},
|
|
||||||
})) as {
|
|
||||||
path?: string;
|
|
||||||
sessions?: Array<Record<string, unknown>>;
|
|
||||||
};
|
|
||||||
|
|
||||||
const sessions = Array.isArray(list?.sessions) ? list.sessions : [];
|
|
||||||
const storePath = typeof list?.path === "string" ? list.path : undefined;
|
|
||||||
const rows: SessionListRow[] = [];
|
|
||||||
|
|
||||||
for (const entry of sessions) {
|
|
||||||
if (!entry || typeof entry !== "object") continue;
|
|
||||||
const key = typeof entry.key === "string" ? entry.key : "";
|
|
||||||
if (!key) continue;
|
|
||||||
if (key === "unknown") continue;
|
|
||||||
if (key === "global" && alias !== "global") continue;
|
|
||||||
|
|
||||||
const gatewayKind =
|
|
||||||
typeof entry.kind === "string" ? entry.kind : undefined;
|
|
||||||
const kind = classifySessionKind({ key, gatewayKind, alias, mainKey });
|
|
||||||
if (allowedKinds && !allowedKinds.has(kind)) continue;
|
|
||||||
|
|
||||||
const displayKey = resolveDisplaySessionKey({
|
|
||||||
key,
|
|
||||||
alias,
|
|
||||||
mainKey,
|
|
||||||
});
|
|
||||||
|
|
||||||
const surface =
|
|
||||||
typeof entry.surface === "string" ? entry.surface : undefined;
|
|
||||||
const lastChannel =
|
|
||||||
typeof entry.lastChannel === "string" ? entry.lastChannel : undefined;
|
|
||||||
const provider = deriveProvider({
|
|
||||||
key,
|
|
||||||
kind,
|
|
||||||
surface,
|
|
||||||
lastChannel,
|
|
||||||
});
|
|
||||||
|
|
||||||
const sessionId =
|
|
||||||
typeof entry.sessionId === "string" ? entry.sessionId : undefined;
|
|
||||||
const transcriptPath =
|
|
||||||
sessionId && storePath
|
|
||||||
? path.join(path.dirname(storePath), `${sessionId}.jsonl`)
|
|
||||||
: undefined;
|
|
||||||
|
|
||||||
const row: SessionListRow = {
|
|
||||||
key: displayKey,
|
|
||||||
kind,
|
|
||||||
provider,
|
|
||||||
displayName:
|
|
||||||
typeof entry.displayName === "string"
|
|
||||||
? entry.displayName
|
|
||||||
: undefined,
|
|
||||||
updatedAt:
|
|
||||||
typeof entry.updatedAt === "number" ? entry.updatedAt : undefined,
|
|
||||||
sessionId,
|
|
||||||
model: typeof entry.model === "string" ? entry.model : undefined,
|
|
||||||
contextTokens:
|
|
||||||
typeof entry.contextTokens === "number"
|
|
||||||
? entry.contextTokens
|
|
||||||
: undefined,
|
|
||||||
totalTokens:
|
|
||||||
typeof entry.totalTokens === "number"
|
|
||||||
? entry.totalTokens
|
|
||||||
: undefined,
|
|
||||||
thinkingLevel:
|
|
||||||
typeof entry.thinkingLevel === "string"
|
|
||||||
? entry.thinkingLevel
|
|
||||||
: undefined,
|
|
||||||
verboseLevel:
|
|
||||||
typeof entry.verboseLevel === "string"
|
|
||||||
? entry.verboseLevel
|
|
||||||
: undefined,
|
|
||||||
systemSent:
|
|
||||||
typeof entry.systemSent === "boolean"
|
|
||||||
? entry.systemSent
|
|
||||||
: undefined,
|
|
||||||
abortedLastRun:
|
|
||||||
typeof entry.abortedLastRun === "boolean"
|
|
||||||
? entry.abortedLastRun
|
|
||||||
: undefined,
|
|
||||||
sendPolicy:
|
|
||||||
typeof entry.sendPolicy === "string" ? entry.sendPolicy : undefined,
|
|
||||||
lastChannel,
|
|
||||||
lastTo: typeof entry.lastTo === "string" ? entry.lastTo : undefined,
|
|
||||||
transcriptPath,
|
|
||||||
};
|
|
||||||
|
|
||||||
if (messageLimit > 0) {
|
|
||||||
const resolvedKey = resolveInternalSessionKey({
|
|
||||||
key: displayKey,
|
|
||||||
alias,
|
|
||||||
mainKey,
|
|
||||||
});
|
|
||||||
const history = (await callGateway({
|
|
||||||
method: "chat.history",
|
|
||||||
params: { sessionKey: resolvedKey, limit: messageLimit },
|
|
||||||
})) as { messages?: unknown[] };
|
|
||||||
const rawMessages = Array.isArray(history?.messages)
|
|
||||||
? history.messages
|
|
||||||
: [];
|
|
||||||
const filtered = stripToolMessages(rawMessages);
|
|
||||||
row.messages =
|
|
||||||
filtered.length > messageLimit
|
|
||||||
? filtered.slice(-messageLimit)
|
|
||||||
: filtered;
|
|
||||||
}
|
|
||||||
|
|
||||||
rows.push(row);
|
|
||||||
}
|
|
||||||
|
|
||||||
return jsonResult({
|
|
||||||
count: rows.length,
|
|
||||||
sessions: rows,
|
|
||||||
});
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function createSessionsHistoryTool(): AnyAgentTool {
|
|
||||||
return {
|
|
||||||
label: "Session History",
|
|
||||||
name: "sessions_history",
|
|
||||||
description: "Fetch message history for a session.",
|
|
||||||
parameters: SessionsHistoryToolSchema,
|
|
||||||
execute: async (_toolCallId, args) => {
|
|
||||||
const params = args as Record<string, unknown>;
|
|
||||||
const sessionKey = readStringParam(params, "sessionKey", {
|
|
||||||
required: true,
|
|
||||||
});
|
|
||||||
const cfg = loadConfig();
|
|
||||||
const { mainKey, alias } = resolveMainSessionAlias(cfg);
|
|
||||||
const resolvedKey = resolveInternalSessionKey({
|
|
||||||
key: sessionKey,
|
|
||||||
alias,
|
|
||||||
mainKey,
|
|
||||||
});
|
|
||||||
const limit =
|
|
||||||
typeof params.limit === "number" && Number.isFinite(params.limit)
|
|
||||||
? Math.max(1, Math.floor(params.limit))
|
|
||||||
: undefined;
|
|
||||||
const includeTools = Boolean(params.includeTools);
|
|
||||||
const result = (await callGateway({
|
|
||||||
method: "chat.history",
|
|
||||||
params: { sessionKey: resolvedKey, limit },
|
|
||||||
})) as { messages?: unknown[] };
|
|
||||||
const rawMessages = Array.isArray(result?.messages)
|
|
||||||
? result.messages
|
|
||||||
: [];
|
|
||||||
const messages = includeTools
|
|
||||||
? rawMessages
|
|
||||||
: stripToolMessages(rawMessages);
|
|
||||||
return jsonResult({
|
|
||||||
sessionKey: resolveDisplaySessionKey({
|
|
||||||
key: sessionKey,
|
|
||||||
alias,
|
|
||||||
mainKey,
|
|
||||||
}),
|
|
||||||
messages,
|
|
||||||
});
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const ANNOUNCE_SKIP_TOKEN = "ANNOUNCE_SKIP";
|
|
||||||
|
|
||||||
type AnnounceTarget = {
|
|
||||||
channel: string;
|
|
||||||
to: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
function resolveAnnounceTargetFromKey(
|
|
||||||
sessionKey: string,
|
|
||||||
): AnnounceTarget | null {
|
|
||||||
const parts = sessionKey.split(":").filter(Boolean);
|
|
||||||
if (parts.length < 3) return null;
|
|
||||||
const [surface, kind, ...rest] = parts;
|
|
||||||
if (kind !== "group" && kind !== "channel") return null;
|
|
||||||
const id = rest.join(":").trim();
|
|
||||||
if (!id) return null;
|
|
||||||
if (!surface) return null;
|
|
||||||
const channel = surface.toLowerCase();
|
|
||||||
if (channel === "discord") {
|
|
||||||
return { channel, to: `channel:${id}` };
|
|
||||||
}
|
|
||||||
if (channel === "signal") {
|
|
||||||
return { channel, to: `group:${id}` };
|
|
||||||
}
|
|
||||||
return { channel, to: id };
|
|
||||||
}
|
|
||||||
|
|
||||||
function buildAgentToAgentMessageContext(params: {
|
|
||||||
requesterSessionKey?: string;
|
|
||||||
requesterSurface?: string;
|
|
||||||
targetSessionKey: string;
|
|
||||||
}) {
|
|
||||||
const lines = [
|
|
||||||
"Agent-to-agent message context:",
|
|
||||||
params.requesterSessionKey
|
|
||||||
? `Requester session: ${params.requesterSessionKey}.`
|
|
||||||
: undefined,
|
|
||||||
params.requesterSurface
|
|
||||||
? `Requester surface: ${params.requesterSurface}.`
|
|
||||||
: undefined,
|
|
||||||
`Target session: ${params.targetSessionKey}.`,
|
|
||||||
].filter(Boolean);
|
|
||||||
return lines.join("\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
function buildAgentToAgentPostContext(params: {
|
|
||||||
requesterSessionKey?: string;
|
|
||||||
requesterSurface?: string;
|
|
||||||
targetSessionKey: string;
|
|
||||||
targetChannel?: string;
|
|
||||||
originalMessage: string;
|
|
||||||
roundOneReply?: string;
|
|
||||||
}) {
|
|
||||||
const lines = [
|
|
||||||
"Agent-to-agent post step:",
|
|
||||||
params.requesterSessionKey
|
|
||||||
? `Requester session: ${params.requesterSessionKey}.`
|
|
||||||
: undefined,
|
|
||||||
params.requesterSurface
|
|
||||||
? `Requester surface: ${params.requesterSurface}.`
|
|
||||||
: undefined,
|
|
||||||
`Target session: ${params.targetSessionKey}.`,
|
|
||||||
params.targetChannel
|
|
||||||
? `Target surface: ${params.targetChannel}.`
|
|
||||||
: undefined,
|
|
||||||
`Original request: ${params.originalMessage}`,
|
|
||||||
params.roundOneReply
|
|
||||||
? `Round 1 reply: ${params.roundOneReply}`
|
|
||||||
: "Round 1 reply: (not available).",
|
|
||||||
`If you want to remain silent, reply exactly "${ANNOUNCE_SKIP_TOKEN}".`,
|
|
||||||
"Any other reply will be posted to the target channel.",
|
|
||||||
"After this reply, the agent-to-agent conversation is over.",
|
|
||||||
].filter(Boolean);
|
|
||||||
return lines.join("\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
function isAnnounceSkip(text?: string) {
|
|
||||||
return (text ?? "").trim() === ANNOUNCE_SKIP_TOKEN;
|
|
||||||
}
|
|
||||||
|
|
||||||
function createSessionsSendTool(opts?: {
|
|
||||||
agentSessionKey?: string;
|
|
||||||
agentSurface?: string;
|
|
||||||
}): AnyAgentTool {
|
|
||||||
return {
|
|
||||||
label: "Session Send",
|
|
||||||
name: "sessions_send",
|
|
||||||
description: "Send a message into another session.",
|
|
||||||
parameters: SessionsSendToolSchema,
|
|
||||||
execute: async (_toolCallId, args) => {
|
|
||||||
const params = args as Record<string, unknown>;
|
|
||||||
const sessionKey = readStringParam(params, "sessionKey", {
|
|
||||||
required: true,
|
|
||||||
});
|
|
||||||
const message = readStringParam(params, "message", { required: true });
|
|
||||||
const cfg = loadConfig();
|
|
||||||
const { mainKey, alias } = resolveMainSessionAlias(cfg);
|
|
||||||
const resolvedKey = resolveInternalSessionKey({
|
|
||||||
key: sessionKey,
|
|
||||||
alias,
|
|
||||||
mainKey,
|
|
||||||
});
|
|
||||||
const timeoutSeconds =
|
|
||||||
typeof params.timeoutSeconds === "number" &&
|
|
||||||
Number.isFinite(params.timeoutSeconds)
|
|
||||||
? Math.max(0, Math.floor(params.timeoutSeconds))
|
|
||||||
: 30;
|
|
||||||
const timeoutMs = timeoutSeconds * 1000;
|
|
||||||
const announceTimeoutMs = timeoutSeconds === 0 ? 30_000 : timeoutMs;
|
|
||||||
const idempotencyKey = crypto.randomUUID();
|
|
||||||
let runId: string = idempotencyKey;
|
|
||||||
const displayKey = resolveDisplaySessionKey({
|
|
||||||
key: sessionKey,
|
|
||||||
alias,
|
|
||||||
mainKey,
|
|
||||||
});
|
|
||||||
const agentMessageContext = buildAgentToAgentMessageContext({
|
|
||||||
requesterSessionKey: opts?.agentSessionKey,
|
|
||||||
requesterSurface: opts?.agentSurface,
|
|
||||||
targetSessionKey: displayKey,
|
|
||||||
});
|
|
||||||
const sendParams = {
|
|
||||||
message,
|
|
||||||
sessionKey: resolvedKey,
|
|
||||||
idempotencyKey,
|
|
||||||
deliver: false,
|
|
||||||
lane: "nested",
|
|
||||||
extraSystemPrompt: agentMessageContext,
|
|
||||||
};
|
|
||||||
|
|
||||||
const resolveAnnounceTarget =
|
|
||||||
async (): Promise<AnnounceTarget | null> => {
|
|
||||||
const parsed = resolveAnnounceTargetFromKey(resolvedKey);
|
|
||||||
if (parsed) return parsed;
|
|
||||||
try {
|
|
||||||
const list = (await callGateway({
|
|
||||||
method: "sessions.list",
|
|
||||||
params: {
|
|
||||||
includeGlobal: true,
|
|
||||||
includeUnknown: true,
|
|
||||||
limit: 200,
|
|
||||||
},
|
|
||||||
})) as { sessions?: Array<Record<string, unknown>> };
|
|
||||||
const sessions = Array.isArray(list?.sessions) ? list.sessions : [];
|
|
||||||
const match =
|
|
||||||
sessions.find((entry) => entry?.key === resolvedKey) ??
|
|
||||||
sessions.find((entry) => entry?.key === displayKey);
|
|
||||||
const channel =
|
|
||||||
typeof match?.lastChannel === "string"
|
|
||||||
? match.lastChannel
|
|
||||||
: undefined;
|
|
||||||
const to =
|
|
||||||
typeof match?.lastTo === "string" ? match.lastTo : undefined;
|
|
||||||
if (channel && to) return { channel, to };
|
|
||||||
} catch {
|
|
||||||
// ignore; fall through to null
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
};
|
|
||||||
|
|
||||||
const runAgentToAgentPost = async (roundOneReply?: string) => {
|
|
||||||
const announceTarget = await resolveAnnounceTarget();
|
|
||||||
try {
|
|
||||||
const postPrompt = buildAgentToAgentPostContext({
|
|
||||||
requesterSessionKey: opts?.agentSessionKey,
|
|
||||||
requesterSurface: opts?.agentSurface,
|
|
||||||
targetSessionKey: displayKey,
|
|
||||||
targetChannel: announceTarget?.channel ?? "unknown",
|
|
||||||
originalMessage: message,
|
|
||||||
roundOneReply,
|
|
||||||
});
|
|
||||||
const postIdem = crypto.randomUUID();
|
|
||||||
const postResponse = (await callGateway({
|
|
||||||
method: "agent",
|
|
||||||
params: {
|
|
||||||
message: "Agent-to-agent post step.",
|
|
||||||
sessionKey: resolvedKey,
|
|
||||||
idempotencyKey: postIdem,
|
|
||||||
deliver: false,
|
|
||||||
lane: "nested",
|
|
||||||
extraSystemPrompt: postPrompt,
|
|
||||||
},
|
|
||||||
timeoutMs: 10_000,
|
|
||||||
})) as { runId?: string; acceptedAt?: number };
|
|
||||||
const postRunId =
|
|
||||||
typeof postResponse?.runId === "string" && postResponse.runId
|
|
||||||
? postResponse.runId
|
|
||||||
: postIdem;
|
|
||||||
const postAcceptedAt =
|
|
||||||
typeof postResponse?.acceptedAt === "number"
|
|
||||||
? postResponse.acceptedAt
|
|
||||||
: undefined;
|
|
||||||
const postWaitMs = Math.min(announceTimeoutMs, 60_000);
|
|
||||||
const postWait = (await callGateway({
|
|
||||||
method: "agent.wait",
|
|
||||||
params: {
|
|
||||||
runId: postRunId,
|
|
||||||
afterMs: postAcceptedAt,
|
|
||||||
timeoutMs: postWaitMs,
|
|
||||||
},
|
|
||||||
timeoutMs: postWaitMs + 2000,
|
|
||||||
})) as { status?: string };
|
|
||||||
if (postWait?.status === "ok") {
|
|
||||||
const postHistory = (await callGateway({
|
|
||||||
method: "chat.history",
|
|
||||||
params: { sessionKey: resolvedKey, limit: 50 },
|
|
||||||
})) as { messages?: unknown[] };
|
|
||||||
const postFiltered = stripToolMessages(
|
|
||||||
Array.isArray(postHistory?.messages) ? postHistory.messages : [],
|
|
||||||
);
|
|
||||||
const postLast =
|
|
||||||
postFiltered.length > 0
|
|
||||||
? postFiltered[postFiltered.length - 1]
|
|
||||||
: undefined;
|
|
||||||
const postReply = postLast
|
|
||||||
? extractAssistantText(postLast)
|
|
||||||
: undefined;
|
|
||||||
if (
|
|
||||||
announceTarget &&
|
|
||||||
postReply &&
|
|
||||||
postReply.trim() &&
|
|
||||||
!isAnnounceSkip(postReply)
|
|
||||||
) {
|
|
||||||
await callGateway({
|
|
||||||
method: "send",
|
|
||||||
params: {
|
|
||||||
to: announceTarget.to,
|
|
||||||
message: postReply.trim(),
|
|
||||||
provider: announceTarget.channel,
|
|
||||||
idempotencyKey: crypto.randomUUID(),
|
|
||||||
},
|
|
||||||
timeoutMs: 10_000,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
// Best-effort announce; ignore failures to avoid breaking the caller response.
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
if (timeoutSeconds === 0) {
|
|
||||||
try {
|
|
||||||
const response = (await callGateway({
|
|
||||||
method: "agent",
|
|
||||||
params: sendParams,
|
|
||||||
timeoutMs: 10_000,
|
|
||||||
})) as { runId?: string };
|
|
||||||
if (typeof response?.runId === "string" && response.runId) {
|
|
||||||
runId = response.runId;
|
|
||||||
}
|
|
||||||
void runAgentToAgentPost();
|
|
||||||
return jsonResult({
|
|
||||||
runId,
|
|
||||||
status: "accepted",
|
|
||||||
sessionKey: displayKey,
|
|
||||||
});
|
|
||||||
} catch (err) {
|
|
||||||
const message =
|
|
||||||
err instanceof Error
|
|
||||||
? err.message
|
|
||||||
: typeof err === "string"
|
|
||||||
? err
|
|
||||||
: "error";
|
|
||||||
return jsonResult({
|
|
||||||
runId,
|
|
||||||
status: "error",
|
|
||||||
error: message,
|
|
||||||
sessionKey: displayKey,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let acceptedAt: number | undefined;
|
|
||||||
try {
|
|
||||||
const response = (await callGateway({
|
|
||||||
method: "agent",
|
|
||||||
params: sendParams,
|
|
||||||
timeoutMs: 10_000,
|
|
||||||
})) as { runId?: string; acceptedAt?: number };
|
|
||||||
if (typeof response?.runId === "string" && response.runId) {
|
|
||||||
runId = response.runId;
|
|
||||||
}
|
|
||||||
if (typeof response?.acceptedAt === "number") {
|
|
||||||
acceptedAt = response.acceptedAt;
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
const message =
|
|
||||||
err instanceof Error
|
|
||||||
? err.message
|
|
||||||
: typeof err === "string"
|
|
||||||
? err
|
|
||||||
: "error";
|
|
||||||
return jsonResult({
|
|
||||||
runId,
|
|
||||||
status: "error",
|
|
||||||
error: message,
|
|
||||||
sessionKey: displayKey,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
let waitStatus: string | undefined;
|
|
||||||
let waitError: string | undefined;
|
|
||||||
try {
|
|
||||||
const wait = (await callGateway({
|
|
||||||
method: "agent.wait",
|
|
||||||
params: {
|
|
||||||
runId,
|
|
||||||
afterMs: acceptedAt,
|
|
||||||
timeoutMs,
|
|
||||||
},
|
|
||||||
timeoutMs: timeoutMs + 2000,
|
|
||||||
})) as { status?: string; error?: string };
|
|
||||||
waitStatus = typeof wait?.status === "string" ? wait.status : undefined;
|
|
||||||
waitError = typeof wait?.error === "string" ? wait.error : undefined;
|
|
||||||
} catch (err) {
|
|
||||||
const message =
|
|
||||||
err instanceof Error
|
|
||||||
? err.message
|
|
||||||
: typeof err === "string"
|
|
||||||
? err
|
|
||||||
: "error";
|
|
||||||
return jsonResult({
|
|
||||||
runId,
|
|
||||||
status: message.includes("gateway timeout") ? "timeout" : "error",
|
|
||||||
error: message,
|
|
||||||
sessionKey: displayKey,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (waitStatus === "timeout") {
|
|
||||||
return jsonResult({
|
|
||||||
runId,
|
|
||||||
status: "timeout",
|
|
||||||
error: waitError,
|
|
||||||
sessionKey: displayKey,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
if (waitStatus === "error") {
|
|
||||||
return jsonResult({
|
|
||||||
runId,
|
|
||||||
status: "error",
|
|
||||||
error: waitError ?? "agent error",
|
|
||||||
sessionKey: displayKey,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const history = (await callGateway({
|
|
||||||
method: "chat.history",
|
|
||||||
params: { sessionKey: resolvedKey, limit: 50 },
|
|
||||||
})) as { messages?: unknown[] };
|
|
||||||
const filtered = stripToolMessages(
|
|
||||||
Array.isArray(history?.messages) ? history.messages : [],
|
|
||||||
);
|
|
||||||
const last =
|
|
||||||
filtered.length > 0 ? filtered[filtered.length - 1] : undefined;
|
|
||||||
const reply = last ? extractAssistantText(last) : undefined;
|
|
||||||
void runAgentToAgentPost(reply ?? undefined);
|
|
||||||
|
|
||||||
return jsonResult({
|
|
||||||
runId,
|
|
||||||
status: "ok",
|
|
||||||
reply,
|
|
||||||
sessionKey: displayKey,
|
|
||||||
});
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export function createClawdisTools(options?: {
|
|
||||||
browserControlUrl?: string;
|
|
||||||
agentSessionKey?: string;
|
|
||||||
agentSurface?: string;
|
|
||||||
}): AnyAgentTool[] {
|
|
||||||
return [
|
return [
|
||||||
createBrowserTool({ defaultControlUrl: options?.browserControlUrl }),
|
createBrowserTool(),
|
||||||
createCanvasTool(),
|
createCanvasTool(),
|
||||||
createNodesTool(),
|
createNodesTool(),
|
||||||
createCronTool(),
|
createCronTool(),
|
||||||
createDiscordTool(),
|
createDiscordTool(),
|
||||||
createGatewayTool(),
|
createGatewayTool(),
|
||||||
createSessionsListTool(),
|
|
||||||
createSessionsHistoryTool(),
|
|
||||||
createSessionsSendTool({
|
|
||||||
agentSessionKey: options?.agentSessionKey,
|
|
||||||
agentSurface: options?.agentSurface,
|
|
||||||
}),
|
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
@@ -1,47 +0,0 @@
|
|||||||
import { describe, expect, it } from "vitest";
|
|
||||||
|
|
||||||
import { buildEmbeddedSandboxInfo } from "./pi-embedded-runner.js";
|
|
||||||
import type { SandboxContext } from "./sandbox.js";
|
|
||||||
|
|
||||||
describe("buildEmbeddedSandboxInfo", () => {
|
|
||||||
it("returns undefined when sandbox is missing", () => {
|
|
||||||
expect(buildEmbeddedSandboxInfo()).toBeUndefined();
|
|
||||||
});
|
|
||||||
|
|
||||||
it("maps sandbox context into prompt info", () => {
|
|
||||||
const sandbox = {
|
|
||||||
enabled: true,
|
|
||||||
sessionKey: "session:test",
|
|
||||||
workspaceDir: "/tmp/clawdis-sandbox",
|
|
||||||
containerName: "clawdis-sbx-test",
|
|
||||||
containerWorkdir: "/workspace",
|
|
||||||
docker: {
|
|
||||||
image: "clawdis-sandbox:bookworm-slim",
|
|
||||||
containerPrefix: "clawdis-sbx-",
|
|
||||||
workdir: "/workspace",
|
|
||||||
readOnlyRoot: true,
|
|
||||||
tmpfs: ["/tmp"],
|
|
||||||
network: "none",
|
|
||||||
user: "1000:1000",
|
|
||||||
capDrop: ["ALL"],
|
|
||||||
env: { LANG: "C.UTF-8" },
|
|
||||||
},
|
|
||||||
tools: {
|
|
||||||
allow: ["bash"],
|
|
||||||
deny: ["browser"],
|
|
||||||
},
|
|
||||||
browser: {
|
|
||||||
controlUrl: "http://localhost:9222",
|
|
||||||
noVncUrl: "http://localhost:6080",
|
|
||||||
containerName: "clawdis-sbx-browser-test",
|
|
||||||
},
|
|
||||||
} satisfies SandboxContext;
|
|
||||||
|
|
||||||
expect(buildEmbeddedSandboxInfo(sandbox)).toEqual({
|
|
||||||
enabled: true,
|
|
||||||
workspaceDir: "/tmp/clawdis-sandbox",
|
|
||||||
browserControlUrl: "http://localhost:9222",
|
|
||||||
browserNoVncUrl: "http://localhost:6080",
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@@ -26,12 +26,12 @@ import type { ThinkLevel, VerboseLevel } from "../auto-reply/thinking.js";
|
|||||||
import { formatToolAggregate } from "../auto-reply/tool-meta.js";
|
import { formatToolAggregate } from "../auto-reply/tool-meta.js";
|
||||||
import type { ClawdisConfig } from "../config/config.js";
|
import type { ClawdisConfig } from "../config/config.js";
|
||||||
import { getMachineDisplayName } from "../infra/machine-name.js";
|
import { getMachineDisplayName } from "../infra/machine-name.js";
|
||||||
import { createSubsystemLogger } from "../logging.js";
|
|
||||||
import { splitMediaFromOutput } from "../media/parse.js";
|
import { splitMediaFromOutput } from "../media/parse.js";
|
||||||
import {
|
import {
|
||||||
type enqueueCommand,
|
type enqueueCommand,
|
||||||
enqueueCommandInLane,
|
enqueueCommandInLane,
|
||||||
} from "../process/command-queue.js";
|
} from "../process/command-queue.js";
|
||||||
|
import { defaultRuntime } from "../runtime.js";
|
||||||
import { CONFIG_DIR, resolveUserPath } from "../utils.js";
|
import { CONFIG_DIR, resolveUserPath } from "../utils.js";
|
||||||
import { resolveClawdisAgentDir } from "./agent-paths.js";
|
import { resolveClawdisAgentDir } from "./agent-paths.js";
|
||||||
import { DEFAULT_MODEL, DEFAULT_PROVIDER } from "./defaults.js";
|
import { DEFAULT_MODEL, DEFAULT_PROVIDER } from "./defaults.js";
|
||||||
@@ -95,26 +95,13 @@ type EmbeddedPiQueueHandle = {
|
|||||||
abort: () => void;
|
abort: () => void;
|
||||||
};
|
};
|
||||||
|
|
||||||
const log = createSubsystemLogger("agent/embedded");
|
|
||||||
|
|
||||||
const ACTIVE_EMBEDDED_RUNS = new Map<string, EmbeddedPiQueueHandle>();
|
const ACTIVE_EMBEDDED_RUNS = new Map<string, EmbeddedPiQueueHandle>();
|
||||||
type EmbeddedRunWaiter = {
|
|
||||||
resolve: (ended: boolean) => void;
|
|
||||||
timer: NodeJS.Timeout;
|
|
||||||
};
|
|
||||||
const EMBEDDED_RUN_WAITERS = new Map<string, Set<EmbeddedRunWaiter>>();
|
|
||||||
|
|
||||||
const OAUTH_FILENAME = "oauth.json";
|
const OAUTH_FILENAME = "oauth.json";
|
||||||
const DEFAULT_OAUTH_DIR = path.join(CONFIG_DIR, "credentials");
|
const DEFAULT_OAUTH_DIR = path.join(CONFIG_DIR, "credentials");
|
||||||
let oauthStorageConfigured = false;
|
let oauthStorageConfigured = false;
|
||||||
|
|
||||||
type OAuthStorage = Record<string, OAuthCredentials>;
|
type OAuthStorage = Record<string, OAuthCredentials>;
|
||||||
type EmbeddedSandboxInfo = {
|
|
||||||
enabled: boolean;
|
|
||||||
workspaceDir?: string;
|
|
||||||
browserControlUrl?: string;
|
|
||||||
browserNoVncUrl?: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
function resolveSessionLane(key: string) {
|
function resolveSessionLane(key: string) {
|
||||||
const cleaned = key.trim() || "main";
|
const cleaned = key.trim() || "main";
|
||||||
@@ -126,18 +113,6 @@ function resolveGlobalLane(lane?: string) {
|
|||||||
return cleaned ? cleaned : "main";
|
return cleaned ? cleaned : "main";
|
||||||
}
|
}
|
||||||
|
|
||||||
export function buildEmbeddedSandboxInfo(
|
|
||||||
sandbox?: Awaited<ReturnType<typeof resolveSandboxContext>>,
|
|
||||||
): EmbeddedSandboxInfo | undefined {
|
|
||||||
if (!sandbox?.enabled) return undefined;
|
|
||||||
return {
|
|
||||||
enabled: true,
|
|
||||||
workspaceDir: sandbox.workspaceDir,
|
|
||||||
browserControlUrl: sandbox.browser?.controlUrl,
|
|
||||||
browserNoVncUrl: sandbox.browser?.noVncUrl,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function resolveClawdisOAuthPath(): string {
|
function resolveClawdisOAuthPath(): string {
|
||||||
const overrideDir =
|
const overrideDir =
|
||||||
process.env.CLAWDIS_OAUTH_DIR?.trim() || DEFAULT_OAUTH_DIR;
|
process.env.CLAWDIS_OAUTH_DIR?.trim() || DEFAULT_OAUTH_DIR;
|
||||||
@@ -252,46 +227,6 @@ export function isEmbeddedPiRunStreaming(sessionId: string): boolean {
|
|||||||
return handle.isStreaming();
|
return handle.isStreaming();
|
||||||
}
|
}
|
||||||
|
|
||||||
export function waitForEmbeddedPiRunEnd(
|
|
||||||
sessionId: string,
|
|
||||||
timeoutMs = 15_000,
|
|
||||||
): Promise<boolean> {
|
|
||||||
if (!sessionId || !ACTIVE_EMBEDDED_RUNS.has(sessionId))
|
|
||||||
return Promise.resolve(true);
|
|
||||||
return new Promise((resolve) => {
|
|
||||||
const waiters = EMBEDDED_RUN_WAITERS.get(sessionId) ?? new Set();
|
|
||||||
const waiter: EmbeddedRunWaiter = {
|
|
||||||
resolve,
|
|
||||||
timer: setTimeout(
|
|
||||||
() => {
|
|
||||||
waiters.delete(waiter);
|
|
||||||
if (waiters.size === 0) EMBEDDED_RUN_WAITERS.delete(sessionId);
|
|
||||||
resolve(false);
|
|
||||||
},
|
|
||||||
Math.max(100, timeoutMs),
|
|
||||||
),
|
|
||||||
};
|
|
||||||
waiters.add(waiter);
|
|
||||||
EMBEDDED_RUN_WAITERS.set(sessionId, waiters);
|
|
||||||
if (!ACTIVE_EMBEDDED_RUNS.has(sessionId)) {
|
|
||||||
waiters.delete(waiter);
|
|
||||||
if (waiters.size === 0) EMBEDDED_RUN_WAITERS.delete(sessionId);
|
|
||||||
clearTimeout(waiter.timer);
|
|
||||||
resolve(true);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function notifyEmbeddedRunEnded(sessionId: string) {
|
|
||||||
const waiters = EMBEDDED_RUN_WAITERS.get(sessionId);
|
|
||||||
if (!waiters || waiters.size === 0) return;
|
|
||||||
EMBEDDED_RUN_WAITERS.delete(sessionId);
|
|
||||||
for (const waiter of waiters) {
|
|
||||||
clearTimeout(waiter.timer);
|
|
||||||
waiter.resolve(true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function resolveEmbeddedSessionLane(key: string) {
|
export function resolveEmbeddedSessionLane(key: string) {
|
||||||
return resolveSessionLane(key);
|
return resolveSessionLane(key);
|
||||||
}
|
}
|
||||||
@@ -428,7 +363,13 @@ export async function runEmbeddedPiAgent(params: {
|
|||||||
return enqueueCommandInLane(sessionLane, () =>
|
return enqueueCommandInLane(sessionLane, () =>
|
||||||
enqueueGlobal(async () => {
|
enqueueGlobal(async () => {
|
||||||
const started = Date.now();
|
const started = Date.now();
|
||||||
const resolvedWorkspace = resolveUserPath(params.workspaceDir);
|
const sandbox = await resolveSandboxContext({
|
||||||
|
config: params.config,
|
||||||
|
sessionKey: params.sessionKey,
|
||||||
|
workspaceDir: params.workspaceDir,
|
||||||
|
});
|
||||||
|
const workspaceDir = sandbox?.workspaceDir ?? params.workspaceDir;
|
||||||
|
const resolvedWorkspace = resolveUserPath(workspaceDir);
|
||||||
const prevCwd = process.cwd();
|
const prevCwd = process.cwd();
|
||||||
|
|
||||||
const provider =
|
const provider =
|
||||||
@@ -449,7 +390,7 @@ export async function runEmbeddedPiAgent(params: {
|
|||||||
|
|
||||||
const thinkingLevel = mapThinkingLevel(params.thinkLevel);
|
const thinkingLevel = mapThinkingLevel(params.thinkLevel);
|
||||||
|
|
||||||
log.debug(
|
defaultRuntime.log?.(
|
||||||
`embedded run start: runId=${params.runId} sessionId=${params.sessionId} provider=${provider} model=${modelId} surface=${params.surface ?? "unknown"}`,
|
`embedded run start: runId=${params.runId} sessionId=${params.sessionId} provider=${provider} model=${modelId} surface=${params.surface ?? "unknown"}`,
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -474,12 +415,6 @@ export async function runEmbeddedPiAgent(params: {
|
|||||||
config: params.config,
|
config: params.config,
|
||||||
entries: skillEntries,
|
entries: skillEntries,
|
||||||
});
|
});
|
||||||
const sandboxSessionKey = params.sessionKey?.trim() || params.sessionId;
|
|
||||||
const sandbox = await resolveSandboxContext({
|
|
||||||
config: params.config,
|
|
||||||
sessionKey: sandboxSessionKey,
|
|
||||||
workspaceDir: resolvedWorkspace,
|
|
||||||
});
|
|
||||||
restoreSkillEnv = params.skillsSnapshot
|
restoreSkillEnv = params.skillsSnapshot
|
||||||
? applySkillEnvOverridesFromSnapshot({
|
? applySkillEnvOverridesFromSnapshot({
|
||||||
snapshot: params.skillsSnapshot,
|
snapshot: params.skillsSnapshot,
|
||||||
@@ -496,9 +431,8 @@ export async function runEmbeddedPiAgent(params: {
|
|||||||
const promptSkills = resolvePromptSkills(skillsSnapshot, skillEntries);
|
const promptSkills = resolvePromptSkills(skillsSnapshot, skillEntries);
|
||||||
const tools = createClawdisCodingTools({
|
const tools = createClawdisCodingTools({
|
||||||
bash: params.config?.agent?.bash,
|
bash: params.config?.agent?.bash,
|
||||||
sandbox,
|
|
||||||
surface: params.surface,
|
surface: params.surface,
|
||||||
sessionKey: params.sessionKey ?? params.sessionId,
|
sandbox,
|
||||||
});
|
});
|
||||||
const machineName = await getMachineDisplayName();
|
const machineName = await getMachineDisplayName();
|
||||||
const runtimeInfo = {
|
const runtimeInfo = {
|
||||||
@@ -508,7 +442,6 @@ export async function runEmbeddedPiAgent(params: {
|
|||||||
node: process.version,
|
node: process.version,
|
||||||
model: `${provider}/${modelId}`,
|
model: `${provider}/${modelId}`,
|
||||||
};
|
};
|
||||||
const sandboxInfo = buildEmbeddedSandboxInfo(sandbox);
|
|
||||||
const reasoningTagHint = provider === "ollama";
|
const reasoningTagHint = provider === "ollama";
|
||||||
const systemPrompt = buildSystemPrompt({
|
const systemPrompt = buildSystemPrompt({
|
||||||
appendPrompt: buildAgentSystemPromptAppend({
|
appendPrompt: buildAgentSystemPromptAppend({
|
||||||
@@ -518,7 +451,6 @@ export async function runEmbeddedPiAgent(params: {
|
|||||||
ownerNumbers: params.ownerNumbers,
|
ownerNumbers: params.ownerNumbers,
|
||||||
reasoningTagHint,
|
reasoningTagHint,
|
||||||
runtimeInfo,
|
runtimeInfo,
|
||||||
sandboxInfo,
|
|
||||||
}),
|
}),
|
||||||
contextFiles,
|
contextFiles,
|
||||||
skills: promptSkills,
|
skills: promptSkills,
|
||||||
@@ -562,7 +494,7 @@ export async function runEmbeddedPiAgent(params: {
|
|||||||
};
|
};
|
||||||
const queueHandle: EmbeddedPiQueueHandle = {
|
const queueHandle: EmbeddedPiQueueHandle = {
|
||||||
queueMessage: async (text: string) => {
|
queueMessage: async (text: string) => {
|
||||||
await session.steer(text);
|
await session.queueMessage(text);
|
||||||
},
|
},
|
||||||
isStreaming: () => session.isStreaming,
|
isStreaming: () => session.isStreaming,
|
||||||
abort: abortRun,
|
abort: abortRun,
|
||||||
@@ -591,14 +523,14 @@ export async function runEmbeddedPiAgent(params: {
|
|||||||
let abortWarnTimer: NodeJS.Timeout | undefined;
|
let abortWarnTimer: NodeJS.Timeout | undefined;
|
||||||
const abortTimer = setTimeout(
|
const abortTimer = setTimeout(
|
||||||
() => {
|
() => {
|
||||||
log.warn(
|
defaultRuntime.log(
|
||||||
`embedded run timeout: runId=${params.runId} sessionId=${params.sessionId} timeoutMs=${params.timeoutMs}`,
|
`embedded run timeout: runId=${params.runId} sessionId=${params.sessionId} timeoutMs=${params.timeoutMs}`,
|
||||||
);
|
);
|
||||||
abortRun();
|
abortRun();
|
||||||
if (!abortWarnTimer) {
|
if (!abortWarnTimer) {
|
||||||
abortWarnTimer = setTimeout(() => {
|
abortWarnTimer = setTimeout(() => {
|
||||||
if (!session.isStreaming) return;
|
if (!session.isStreaming) return;
|
||||||
log.warn(
|
defaultRuntime.log(
|
||||||
`embedded run abort still streaming: runId=${params.runId} sessionId=${params.sessionId}`,
|
`embedded run abort still streaming: runId=${params.runId} sessionId=${params.sessionId}`,
|
||||||
);
|
);
|
||||||
}, 10_000);
|
}, 10_000);
|
||||||
@@ -624,7 +556,7 @@ export async function runEmbeddedPiAgent(params: {
|
|||||||
let promptError: unknown = null;
|
let promptError: unknown = null;
|
||||||
try {
|
try {
|
||||||
const promptStartedAt = Date.now();
|
const promptStartedAt = Date.now();
|
||||||
log.debug(
|
defaultRuntime.log?.(
|
||||||
`embedded run prompt start: runId=${params.runId} sessionId=${params.sessionId}`,
|
`embedded run prompt start: runId=${params.runId} sessionId=${params.sessionId}`,
|
||||||
);
|
);
|
||||||
try {
|
try {
|
||||||
@@ -632,7 +564,7 @@ export async function runEmbeddedPiAgent(params: {
|
|||||||
} catch (err) {
|
} catch (err) {
|
||||||
promptError = err;
|
promptError = err;
|
||||||
} finally {
|
} finally {
|
||||||
log.debug(
|
defaultRuntime.log?.(
|
||||||
`embedded run prompt end: runId=${params.runId} sessionId=${params.sessionId} durationMs=${Date.now() - promptStartedAt}`,
|
`embedded run prompt end: runId=${params.runId} sessionId=${params.sessionId} durationMs=${Date.now() - promptStartedAt}`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -648,7 +580,6 @@ export async function runEmbeddedPiAgent(params: {
|
|||||||
unsubscribe();
|
unsubscribe();
|
||||||
if (ACTIVE_EMBEDDED_RUNS.get(params.sessionId) === queueHandle) {
|
if (ACTIVE_EMBEDDED_RUNS.get(params.sessionId) === queueHandle) {
|
||||||
ACTIVE_EMBEDDED_RUNS.delete(params.sessionId);
|
ACTIVE_EMBEDDED_RUNS.delete(params.sessionId);
|
||||||
notifyEmbeddedRunEnded(params.sessionId);
|
|
||||||
}
|
}
|
||||||
session.dispose();
|
session.dispose();
|
||||||
params.abortSignal?.removeEventListener?.("abort", onAbort);
|
params.abortSignal?.removeEventListener?.("abort", onAbort);
|
||||||
@@ -722,7 +653,7 @@ export async function runEmbeddedPiAgent(params: {
|
|||||||
p.text || p.mediaUrl || (p.mediaUrls && p.mediaUrls.length > 0),
|
p.text || p.mediaUrl || (p.mediaUrls && p.mediaUrls.length > 0),
|
||||||
);
|
);
|
||||||
|
|
||||||
log.debug(
|
defaultRuntime.log?.(
|
||||||
`embedded run done: runId=${params.runId} sessionId=${params.sessionId} durationMs=${Date.now() - started} aborted=${aborted}`,
|
`embedded run done: runId=${params.runId} sessionId=${params.sessionId} durationMs=${Date.now() - started} aborted=${aborted}`,
|
||||||
);
|
);
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -4,8 +4,8 @@ import type { AgentSession } from "@mariozechner/pi-coding-agent";
|
|||||||
|
|
||||||
import { formatToolAggregate } from "../auto-reply/tool-meta.js";
|
import { formatToolAggregate } from "../auto-reply/tool-meta.js";
|
||||||
import { emitAgentEvent } from "../infra/agent-events.js";
|
import { emitAgentEvent } from "../infra/agent-events.js";
|
||||||
import { createSubsystemLogger } from "../logging.js";
|
|
||||||
import { splitMediaFromOutput } from "../media/parse.js";
|
import { splitMediaFromOutput } from "../media/parse.js";
|
||||||
|
import { defaultRuntime } from "../runtime.js";
|
||||||
import {
|
import {
|
||||||
extractAssistantText,
|
extractAssistantText,
|
||||||
inferToolMetaFromArgs,
|
inferToolMetaFromArgs,
|
||||||
@@ -15,7 +15,6 @@ const THINKING_TAG_RE = /<\s*\/?\s*think(?:ing)?\s*>/gi;
|
|||||||
const THINKING_OPEN_RE = /<\s*think(?:ing)?\s*>/i;
|
const THINKING_OPEN_RE = /<\s*think(?:ing)?\s*>/i;
|
||||||
const THINKING_CLOSE_RE = /<\s*\/\s*think(?:ing)?\s*>/i;
|
const THINKING_CLOSE_RE = /<\s*\/\s*think(?:ing)?\s*>/i;
|
||||||
const TOOL_RESULT_MAX_CHARS = 8000;
|
const TOOL_RESULT_MAX_CHARS = 8000;
|
||||||
const log = createSubsystemLogger("agent/embedded");
|
|
||||||
|
|
||||||
export type BlockReplyChunking = {
|
export type BlockReplyChunking = {
|
||||||
minChars: number;
|
minChars: number;
|
||||||
@@ -325,7 +324,7 @@ export function subscribeEmbeddedPiSession(params: {
|
|||||||
const args = (evt as AgentEvent & { args: unknown }).args;
|
const args = (evt as AgentEvent & { args: unknown }).args;
|
||||||
const meta = inferToolMetaFromArgs(toolName, args);
|
const meta = inferToolMetaFromArgs(toolName, args);
|
||||||
toolMetaById.set(toolCallId, meta);
|
toolMetaById.set(toolCallId, meta);
|
||||||
log.debug(
|
defaultRuntime.log?.(
|
||||||
`embedded run tool start: runId=${params.runId} tool=${toolName} toolCallId=${toolCallId}`,
|
`embedded run tool start: runId=${params.runId} tool=${toolName} toolCallId=${toolCallId}`,
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -591,19 +590,21 @@ export function subscribeEmbeddedPiSession(params: {
|
|||||||
const toolCallId = String(
|
const toolCallId = String(
|
||||||
(evt as AgentEvent & { toolCallId: string }).toolCallId,
|
(evt as AgentEvent & { toolCallId: string }).toolCallId,
|
||||||
);
|
);
|
||||||
log.debug(
|
defaultRuntime.log?.(
|
||||||
`embedded run tool end: runId=${params.runId} tool=${toolName} toolCallId=${toolCallId}`,
|
`embedded run tool end: runId=${params.runId} tool=${toolName} toolCallId=${toolCallId}`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (evt.type === "agent_start") {
|
if (evt.type === "agent_start") {
|
||||||
log.debug(`embedded run agent start: runId=${params.runId}`);
|
defaultRuntime.log?.(`embedded run agent start: runId=${params.runId}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (evt.type === "auto_compaction_start") {
|
if (evt.type === "auto_compaction_start") {
|
||||||
compactionInFlight = true;
|
compactionInFlight = true;
|
||||||
ensureCompactionPromise();
|
ensureCompactionPromise();
|
||||||
log.debug(`embedded run compaction start: runId=${params.runId}`);
|
defaultRuntime.log?.(
|
||||||
|
`embedded run compaction start: runId=${params.runId}`,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (evt.type === "auto_compaction_end") {
|
if (evt.type === "auto_compaction_end") {
|
||||||
@@ -612,14 +613,16 @@ export function subscribeEmbeddedPiSession(params: {
|
|||||||
if (willRetry) {
|
if (willRetry) {
|
||||||
noteCompactionRetry();
|
noteCompactionRetry();
|
||||||
resetForCompactionRetry();
|
resetForCompactionRetry();
|
||||||
log.debug(`embedded run compaction retry: runId=${params.runId}`);
|
defaultRuntime.log?.(
|
||||||
|
`embedded run compaction retry: runId=${params.runId}`,
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
maybeResolveCompactionWait();
|
maybeResolveCompactionWait();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (evt.type === "agent_end") {
|
if (evt.type === "agent_end") {
|
||||||
log.debug(`embedded run agent end: runId=${params.runId}`);
|
defaultRuntime.log?.(`embedded run agent end: runId=${params.runId}`);
|
||||||
if (pendingCompactionRetry > 0) {
|
if (pendingCompactionRetry > 0) {
|
||||||
resolveCompactionRetry();
|
resolveCompactionRetry();
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -10,5 +10,4 @@ export {
|
|||||||
queueEmbeddedPiMessage,
|
queueEmbeddedPiMessage,
|
||||||
resolveEmbeddedSessionLane,
|
resolveEmbeddedSessionLane,
|
||||||
runEmbeddedPiAgent,
|
runEmbeddedPiAgent,
|
||||||
waitForEmbeddedPiRunEnd,
|
|
||||||
} from "./pi-embedded-runner.js";
|
} from "./pi-embedded-runner.js";
|
||||||
@@ -158,33 +158,4 @@ describe("createClawdisCodingTools", () => {
|
|||||||
await fs.rm(tmpDir, { recursive: true, force: true });
|
await fs.rm(tmpDir, { recursive: true, force: true });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it("filters tools by sandbox policy", () => {
|
|
||||||
const sandbox = {
|
|
||||||
enabled: true,
|
|
||||||
sessionKey: "sandbox:test",
|
|
||||||
workspaceDir: path.join(os.tmpdir(), "clawdis-sandbox"),
|
|
||||||
containerName: "clawdis-sbx-test",
|
|
||||||
containerWorkdir: "/workspace",
|
|
||||||
docker: {
|
|
||||||
image: "clawdis-sandbox:bookworm-slim",
|
|
||||||
containerPrefix: "clawdis-sbx-",
|
|
||||||
workdir: "/workspace",
|
|
||||||
readOnlyRoot: true,
|
|
||||||
tmpfs: [],
|
|
||||||
network: "none",
|
|
||||||
user: "1000:1000",
|
|
||||||
capDrop: ["ALL"],
|
|
||||||
env: { LANG: "C.UTF-8" },
|
|
||||||
},
|
|
||||||
tools: {
|
|
||||||
allow: ["bash"],
|
|
||||||
deny: ["browser"],
|
|
||||||
},
|
|
||||||
};
|
|
||||||
const tools = createClawdisCodingTools({ sandbox });
|
|
||||||
expect(tools.some((tool) => tool.name === "bash")).toBe(true);
|
|
||||||
expect(tools.some((tool) => tool.name === "read")).toBe(false);
|
|
||||||
expect(tools.some((tool) => tool.name === "browser")).toBe(false);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
+5
-100
@@ -1,11 +1,5 @@
|
|||||||
import type { AgentTool, AgentToolResult } from "@mariozechner/pi-agent-core";
|
import type { AgentTool, AgentToolResult } from "@mariozechner/pi-agent-core";
|
||||||
import {
|
import { codingTools, readTool } from "@mariozechner/pi-coding-agent";
|
||||||
codingTools,
|
|
||||||
createEditTool,
|
|
||||||
createReadTool,
|
|
||||||
createWriteTool,
|
|
||||||
readTool,
|
|
||||||
} from "@mariozechner/pi-coding-agent";
|
|
||||||
import { Type } from "@sinclair/typebox";
|
import { Type } from "@sinclair/typebox";
|
||||||
|
|
||||||
import { detectMime } from "../media/mime.js";
|
import { detectMime } from "../media/mime.js";
|
||||||
@@ -17,8 +11,6 @@ import {
|
|||||||
type ProcessToolDefaults,
|
type ProcessToolDefaults,
|
||||||
} from "./bash-tools.js";
|
} from "./bash-tools.js";
|
||||||
import { createClawdisTools } from "./clawdis-tools.js";
|
import { createClawdisTools } from "./clawdis-tools.js";
|
||||||
import type { SandboxContext, SandboxToolPolicy } from "./sandbox.js";
|
|
||||||
import { assertSandboxPath } from "./sandbox-paths.js";
|
|
||||||
import { sanitizeToolResultImages } from "./tool-images.js";
|
import { sanitizeToolResultImages } from "./tool-images.js";
|
||||||
|
|
||||||
// NOTE(steipete): Upstream read now does file-magic MIME detection; we keep the wrapper
|
// NOTE(steipete): Upstream read now does file-magic MIME detection; we keep the wrapper
|
||||||
@@ -292,59 +284,6 @@ function normalizeToolParameters(tool: AnyAgentTool): AnyAgentTool {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function normalizeToolNames(list?: string[]) {
|
|
||||||
if (!list) return [];
|
|
||||||
return list.map((entry) => entry.trim().toLowerCase()).filter(Boolean);
|
|
||||||
}
|
|
||||||
|
|
||||||
function filterToolsByPolicy(
|
|
||||||
tools: AnyAgentTool[],
|
|
||||||
policy?: SandboxToolPolicy,
|
|
||||||
) {
|
|
||||||
if (!policy) return tools;
|
|
||||||
const deny = new Set(normalizeToolNames(policy.deny));
|
|
||||||
const allowRaw = normalizeToolNames(policy.allow);
|
|
||||||
const allow = allowRaw.length > 0 ? new Set(allowRaw) : null;
|
|
||||||
return tools.filter((tool) => {
|
|
||||||
const name = tool.name.toLowerCase();
|
|
||||||
if (deny.has(name)) return false;
|
|
||||||
if (allow) return allow.has(name);
|
|
||||||
return true;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function wrapSandboxPathGuard(tool: AnyAgentTool, root: string): AnyAgentTool {
|
|
||||||
return {
|
|
||||||
...tool,
|
|
||||||
execute: async (toolCallId, args, signal, onUpdate) => {
|
|
||||||
const record =
|
|
||||||
args && typeof args === "object"
|
|
||||||
? (args as Record<string, unknown>)
|
|
||||||
: undefined;
|
|
||||||
const filePath = record?.path;
|
|
||||||
if (typeof filePath === "string" && filePath.trim()) {
|
|
||||||
await assertSandboxPath({ filePath, cwd: root, root });
|
|
||||||
}
|
|
||||||
return tool.execute(toolCallId, args, signal, onUpdate);
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function createSandboxedReadTool(root: string) {
|
|
||||||
const base = createReadTool(root);
|
|
||||||
return wrapSandboxPathGuard(createClawdisReadTool(base), root);
|
|
||||||
}
|
|
||||||
|
|
||||||
function createSandboxedWriteTool(root: string) {
|
|
||||||
const base = createWriteTool(root);
|
|
||||||
return wrapSandboxPathGuard(base as unknown as AnyAgentTool, root);
|
|
||||||
}
|
|
||||||
|
|
||||||
function createSandboxedEditTool(root: string) {
|
|
||||||
const base = createEditTool(root);
|
|
||||||
return wrapSandboxPathGuard(base as unknown as AnyAgentTool, root);
|
|
||||||
}
|
|
||||||
|
|
||||||
function createWhatsAppLoginTool(): AnyAgentTool {
|
function createWhatsAppLoginTool(): AnyAgentTool {
|
||||||
return {
|
return {
|
||||||
label: "WhatsApp Login",
|
label: "WhatsApp Login",
|
||||||
@@ -444,61 +383,27 @@ function shouldIncludeDiscordTool(surface?: string): boolean {
|
|||||||
export function createClawdisCodingTools(options?: {
|
export function createClawdisCodingTools(options?: {
|
||||||
bash?: BashToolDefaults & ProcessToolDefaults;
|
bash?: BashToolDefaults & ProcessToolDefaults;
|
||||||
surface?: string;
|
surface?: string;
|
||||||
sandbox?: SandboxContext | null;
|
|
||||||
sessionKey?: string;
|
|
||||||
}): AnyAgentTool[] {
|
}): AnyAgentTool[] {
|
||||||
const bashToolName = "bash";
|
const bashToolName = "bash";
|
||||||
const sandbox = options?.sandbox?.enabled ? options.sandbox : undefined;
|
|
||||||
const sandboxRoot = sandbox?.workspaceDir;
|
|
||||||
const base = (codingTools as unknown as AnyAgentTool[]).flatMap((tool) => {
|
const base = (codingTools as unknown as AnyAgentTool[]).flatMap((tool) => {
|
||||||
if (tool.name === readTool.name) {
|
if (tool.name === readTool.name) return [createClawdisReadTool(tool)];
|
||||||
return sandboxRoot
|
|
||||||
? [createSandboxedReadTool(sandboxRoot)]
|
|
||||||
: [createClawdisReadTool(tool)];
|
|
||||||
}
|
|
||||||
if (tool.name === bashToolName) return [];
|
if (tool.name === bashToolName) return [];
|
||||||
if (sandboxRoot && (tool.name === "write" || tool.name === "edit")) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
return [tool as AnyAgentTool];
|
return [tool as AnyAgentTool];
|
||||||
});
|
});
|
||||||
const bashTool = createBashTool({
|
const bashTool = createBashTool(options?.bash);
|
||||||
...options?.bash,
|
|
||||||
sandbox: sandbox
|
|
||||||
? {
|
|
||||||
containerName: sandbox.containerName,
|
|
||||||
workspaceDir: sandbox.workspaceDir,
|
|
||||||
containerWorkdir: sandbox.containerWorkdir,
|
|
||||||
env: sandbox.docker.env,
|
|
||||||
}
|
|
||||||
: undefined,
|
|
||||||
});
|
|
||||||
const processTool = createProcessTool({
|
const processTool = createProcessTool({
|
||||||
cleanupMs: options?.bash?.cleanupMs,
|
cleanupMs: options?.bash?.cleanupMs,
|
||||||
});
|
});
|
||||||
const tools: AnyAgentTool[] = [
|
const tools: AnyAgentTool[] = [
|
||||||
...base,
|
...base,
|
||||||
...(sandboxRoot
|
|
||||||
? [
|
|
||||||
createSandboxedEditTool(sandboxRoot),
|
|
||||||
createSandboxedWriteTool(sandboxRoot),
|
|
||||||
]
|
|
||||||
: []),
|
|
||||||
bashTool as unknown as AnyAgentTool,
|
bashTool as unknown as AnyAgentTool,
|
||||||
processTool as unknown as AnyAgentTool,
|
processTool as unknown as AnyAgentTool,
|
||||||
createWhatsAppLoginTool(),
|
createWhatsAppLoginTool(),
|
||||||
...createClawdisTools({
|
...createClawdisTools(),
|
||||||
browserControlUrl: sandbox?.browser?.controlUrl,
|
|
||||||
agentSessionKey: options?.sessionKey,
|
|
||||||
agentSurface: options?.surface,
|
|
||||||
}),
|
|
||||||
];
|
];
|
||||||
const allowDiscord = shouldIncludeDiscordTool(options?.surface);
|
const allowDiscord = shouldIncludeDiscordTool(options?.surface);
|
||||||
const filtered = allowDiscord
|
const filtered = allowDiscord
|
||||||
? tools
|
? tools
|
||||||
: tools.filter((tool) => tool.name !== "discord");
|
: tools.filter((tool) => tool.name !== "discord");
|
||||||
const sandboxed = sandbox
|
return filtered.map(normalizeToolParameters);
|
||||||
? filterToolsByPolicy(filtered, sandbox.tools)
|
|
||||||
: filtered;
|
|
||||||
return sandboxed.map(normalizeToolParameters);
|
|
||||||
}
|
}
|
||||||
@@ -1,83 +0,0 @@
|
|||||||
import fs from "node:fs/promises";
|
|
||||||
import os from "node:os";
|
|
||||||
import path from "node:path";
|
|
||||||
|
|
||||||
const UNICODE_SPACES = /[\u00A0\u2000-\u200A\u202F\u205F\u3000]/g;
|
|
||||||
|
|
||||||
function normalizeUnicodeSpaces(str: string): string {
|
|
||||||
return str.replace(UNICODE_SPACES, " ");
|
|
||||||
}
|
|
||||||
|
|
||||||
function expandPath(filePath: string): string {
|
|
||||||
const normalized = normalizeUnicodeSpaces(filePath);
|
|
||||||
if (normalized === "~") {
|
|
||||||
return os.homedir();
|
|
||||||
}
|
|
||||||
if (normalized.startsWith("~/")) {
|
|
||||||
return os.homedir() + normalized.slice(1);
|
|
||||||
}
|
|
||||||
return normalized;
|
|
||||||
}
|
|
||||||
|
|
||||||
function resolveToCwd(filePath: string, cwd: string): string {
|
|
||||||
const expanded = expandPath(filePath);
|
|
||||||
if (path.isAbsolute(expanded)) return expanded;
|
|
||||||
return path.resolve(cwd, expanded);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function resolveSandboxPath(params: {
|
|
||||||
filePath: string;
|
|
||||||
cwd: string;
|
|
||||||
root: string;
|
|
||||||
}): { resolved: string; relative: string } {
|
|
||||||
const resolved = resolveToCwd(params.filePath, params.cwd);
|
|
||||||
const rootResolved = path.resolve(params.root);
|
|
||||||
const relative = path.relative(rootResolved, resolved);
|
|
||||||
if (!relative || relative === "") {
|
|
||||||
return { resolved, relative: "" };
|
|
||||||
}
|
|
||||||
if (relative.startsWith("..") || path.isAbsolute(relative)) {
|
|
||||||
throw new Error(
|
|
||||||
`Path escapes sandbox root (${shortPath(rootResolved)}): ${params.filePath}`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return { resolved, relative };
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function assertSandboxPath(params: {
|
|
||||||
filePath: string;
|
|
||||||
cwd: string;
|
|
||||||
root: string;
|
|
||||||
}) {
|
|
||||||
const resolved = resolveSandboxPath(params);
|
|
||||||
await assertNoSymlink(resolved.relative, path.resolve(params.root));
|
|
||||||
return resolved;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function assertNoSymlink(relative: string, root: string) {
|
|
||||||
if (!relative) return;
|
|
||||||
const parts = relative.split(path.sep).filter(Boolean);
|
|
||||||
let current = root;
|
|
||||||
for (const part of parts) {
|
|
||||||
current = path.join(current, part);
|
|
||||||
try {
|
|
||||||
const stat = await fs.lstat(current);
|
|
||||||
if (stat.isSymbolicLink()) {
|
|
||||||
throw new Error(`Symlink not allowed in sandbox path: ${current}`);
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
const anyErr = err as { code?: string };
|
|
||||||
if (anyErr.code === "ENOENT") {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function shortPath(value: string) {
|
|
||||||
if (value.startsWith(os.homedir())) {
|
|
||||||
return `~${value.slice(os.homedir().length)}`;
|
|
||||||
}
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
@@ -1,776 +0,0 @@
|
|||||||
import { spawn } from "node:child_process";
|
|
||||||
import crypto from "node:crypto";
|
|
||||||
import fs from "node:fs/promises";
|
|
||||||
import os from "node:os";
|
|
||||||
import path from "node:path";
|
|
||||||
|
|
||||||
import {
|
|
||||||
type BrowserBridge,
|
|
||||||
startBrowserBridgeServer,
|
|
||||||
stopBrowserBridgeServer,
|
|
||||||
} from "../browser/bridge-server.js";
|
|
||||||
import type { ResolvedBrowserConfig } from "../browser/config.js";
|
|
||||||
import { DEFAULT_CLAWD_BROWSER_COLOR } from "../browser/constants.js";
|
|
||||||
import type { ClawdisConfig } from "../config/config.js";
|
|
||||||
import { STATE_DIR_CLAWDIS } from "../config/config.js";
|
|
||||||
import { defaultRuntime } from "../runtime.js";
|
|
||||||
import { resolveUserPath } from "../utils.js";
|
|
||||||
import {
|
|
||||||
DEFAULT_AGENT_WORKSPACE_DIR,
|
|
||||||
DEFAULT_AGENTS_FILENAME,
|
|
||||||
DEFAULT_BOOTSTRAP_FILENAME,
|
|
||||||
DEFAULT_IDENTITY_FILENAME,
|
|
||||||
DEFAULT_SOUL_FILENAME,
|
|
||||||
DEFAULT_TOOLS_FILENAME,
|
|
||||||
DEFAULT_USER_FILENAME,
|
|
||||||
ensureAgentWorkspace,
|
|
||||||
} from "./workspace.js";
|
|
||||||
|
|
||||||
export type SandboxToolPolicy = {
|
|
||||||
allow?: string[];
|
|
||||||
deny?: string[];
|
|
||||||
};
|
|
||||||
|
|
||||||
export type SandboxBrowserConfig = {
|
|
||||||
enabled: boolean;
|
|
||||||
image: string;
|
|
||||||
containerPrefix: string;
|
|
||||||
cdpPort: number;
|
|
||||||
vncPort: number;
|
|
||||||
noVncPort: number;
|
|
||||||
headless: boolean;
|
|
||||||
enableNoVnc: boolean;
|
|
||||||
};
|
|
||||||
|
|
||||||
export type SandboxDockerConfig = {
|
|
||||||
image: string;
|
|
||||||
containerPrefix: string;
|
|
||||||
workdir: string;
|
|
||||||
readOnlyRoot: boolean;
|
|
||||||
tmpfs: string[];
|
|
||||||
network: string;
|
|
||||||
user?: string;
|
|
||||||
capDrop: string[];
|
|
||||||
env?: Record<string, string>;
|
|
||||||
setupCommand?: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
export type SandboxPruneConfig = {
|
|
||||||
idleHours: number;
|
|
||||||
maxAgeDays: number;
|
|
||||||
};
|
|
||||||
|
|
||||||
export type SandboxConfig = {
|
|
||||||
mode: "off" | "non-main" | "all";
|
|
||||||
perSession: boolean;
|
|
||||||
workspaceRoot: string;
|
|
||||||
docker: SandboxDockerConfig;
|
|
||||||
browser: SandboxBrowserConfig;
|
|
||||||
tools: SandboxToolPolicy;
|
|
||||||
prune: SandboxPruneConfig;
|
|
||||||
};
|
|
||||||
|
|
||||||
export type SandboxBrowserContext = {
|
|
||||||
controlUrl: string;
|
|
||||||
noVncUrl?: string;
|
|
||||||
containerName: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
export type SandboxContext = {
|
|
||||||
enabled: boolean;
|
|
||||||
sessionKey: string;
|
|
||||||
workspaceDir: string;
|
|
||||||
containerName: string;
|
|
||||||
containerWorkdir: string;
|
|
||||||
docker: SandboxDockerConfig;
|
|
||||||
tools: SandboxToolPolicy;
|
|
||||||
browser?: SandboxBrowserContext;
|
|
||||||
};
|
|
||||||
|
|
||||||
const DEFAULT_SANDBOX_WORKSPACE_ROOT = path.join(
|
|
||||||
os.homedir(),
|
|
||||||
".clawdis",
|
|
||||||
"sandboxes",
|
|
||||||
);
|
|
||||||
const DEFAULT_SANDBOX_IMAGE = "clawdis-sandbox:bookworm-slim";
|
|
||||||
const DEFAULT_SANDBOX_CONTAINER_PREFIX = "clawdis-sbx-";
|
|
||||||
const DEFAULT_SANDBOX_WORKDIR = "/workspace";
|
|
||||||
const DEFAULT_SANDBOX_IDLE_HOURS = 24;
|
|
||||||
const DEFAULT_SANDBOX_MAX_AGE_DAYS = 7;
|
|
||||||
const DEFAULT_TOOL_ALLOW = ["bash", "process", "read", "write", "edit"];
|
|
||||||
const DEFAULT_TOOL_DENY = [
|
|
||||||
"browser",
|
|
||||||
"canvas",
|
|
||||||
"nodes",
|
|
||||||
"cron",
|
|
||||||
"discord",
|
|
||||||
"gateway",
|
|
||||||
];
|
|
||||||
const DEFAULT_SANDBOX_BROWSER_IMAGE = "clawdis-sandbox-browser:bookworm-slim";
|
|
||||||
const DEFAULT_SANDBOX_BROWSER_PREFIX = "clawdis-sbx-browser-";
|
|
||||||
const DEFAULT_SANDBOX_BROWSER_CDP_PORT = 9222;
|
|
||||||
const DEFAULT_SANDBOX_BROWSER_VNC_PORT = 5900;
|
|
||||||
const DEFAULT_SANDBOX_BROWSER_NOVNC_PORT = 6080;
|
|
||||||
|
|
||||||
const SANDBOX_STATE_DIR = path.join(STATE_DIR_CLAWDIS, "sandbox");
|
|
||||||
const SANDBOX_REGISTRY_PATH = path.join(SANDBOX_STATE_DIR, "containers.json");
|
|
||||||
const SANDBOX_BROWSER_REGISTRY_PATH = path.join(
|
|
||||||
SANDBOX_STATE_DIR,
|
|
||||||
"browsers.json",
|
|
||||||
);
|
|
||||||
|
|
||||||
type SandboxRegistryEntry = {
|
|
||||||
containerName: string;
|
|
||||||
sessionKey: string;
|
|
||||||
createdAtMs: number;
|
|
||||||
lastUsedAtMs: number;
|
|
||||||
image: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
type SandboxRegistry = {
|
|
||||||
entries: SandboxRegistryEntry[];
|
|
||||||
};
|
|
||||||
|
|
||||||
type SandboxBrowserRegistryEntry = {
|
|
||||||
containerName: string;
|
|
||||||
sessionKey: string;
|
|
||||||
createdAtMs: number;
|
|
||||||
lastUsedAtMs: number;
|
|
||||||
image: string;
|
|
||||||
cdpPort: number;
|
|
||||||
noVncPort?: number;
|
|
||||||
};
|
|
||||||
|
|
||||||
type SandboxBrowserRegistry = {
|
|
||||||
entries: SandboxBrowserRegistryEntry[];
|
|
||||||
};
|
|
||||||
|
|
||||||
let lastPruneAtMs = 0;
|
|
||||||
const BROWSER_BRIDGES = new Map<
|
|
||||||
string,
|
|
||||||
{ bridge: BrowserBridge; containerName: string }
|
|
||||||
>();
|
|
||||||
|
|
||||||
function normalizeToolList(values?: string[]) {
|
|
||||||
if (!values) return [];
|
|
||||||
return values
|
|
||||||
.map((value) => value.trim())
|
|
||||||
.filter(Boolean)
|
|
||||||
.map((value) => value.toLowerCase());
|
|
||||||
}
|
|
||||||
|
|
||||||
function isToolAllowed(policy: SandboxToolPolicy, name: string) {
|
|
||||||
const deny = new Set(normalizeToolList(policy.deny));
|
|
||||||
if (deny.has(name.toLowerCase())) return false;
|
|
||||||
const allow = normalizeToolList(policy.allow);
|
|
||||||
if (allow.length === 0) return true;
|
|
||||||
return allow.includes(name.toLowerCase());
|
|
||||||
}
|
|
||||||
|
|
||||||
function defaultSandboxConfig(cfg?: ClawdisConfig): SandboxConfig {
|
|
||||||
const agent = cfg?.agent?.sandbox;
|
|
||||||
return {
|
|
||||||
mode: agent?.mode ?? "off",
|
|
||||||
perSession: agent?.perSession ?? true,
|
|
||||||
workspaceRoot: agent?.workspaceRoot ?? DEFAULT_SANDBOX_WORKSPACE_ROOT,
|
|
||||||
docker: {
|
|
||||||
image: agent?.docker?.image ?? DEFAULT_SANDBOX_IMAGE,
|
|
||||||
containerPrefix:
|
|
||||||
agent?.docker?.containerPrefix ?? DEFAULT_SANDBOX_CONTAINER_PREFIX,
|
|
||||||
workdir: agent?.docker?.workdir ?? DEFAULT_SANDBOX_WORKDIR,
|
|
||||||
readOnlyRoot: agent?.docker?.readOnlyRoot ?? true,
|
|
||||||
tmpfs: agent?.docker?.tmpfs ?? ["/tmp", "/var/tmp", "/run"],
|
|
||||||
network: agent?.docker?.network ?? "bridge",
|
|
||||||
user: agent?.docker?.user,
|
|
||||||
capDrop: agent?.docker?.capDrop ?? ["ALL"],
|
|
||||||
env: agent?.docker?.env ?? { LANG: "C.UTF-8" },
|
|
||||||
setupCommand: agent?.docker?.setupCommand,
|
|
||||||
},
|
|
||||||
browser: {
|
|
||||||
enabled: agent?.browser?.enabled ?? false,
|
|
||||||
image: agent?.browser?.image ?? DEFAULT_SANDBOX_BROWSER_IMAGE,
|
|
||||||
containerPrefix:
|
|
||||||
agent?.browser?.containerPrefix ?? DEFAULT_SANDBOX_BROWSER_PREFIX,
|
|
||||||
cdpPort: agent?.browser?.cdpPort ?? DEFAULT_SANDBOX_BROWSER_CDP_PORT,
|
|
||||||
vncPort: agent?.browser?.vncPort ?? DEFAULT_SANDBOX_BROWSER_VNC_PORT,
|
|
||||||
noVncPort:
|
|
||||||
agent?.browser?.noVncPort ?? DEFAULT_SANDBOX_BROWSER_NOVNC_PORT,
|
|
||||||
headless: agent?.browser?.headless ?? false,
|
|
||||||
enableNoVnc: agent?.browser?.enableNoVnc ?? true,
|
|
||||||
},
|
|
||||||
tools: {
|
|
||||||
allow: agent?.tools?.allow ?? DEFAULT_TOOL_ALLOW,
|
|
||||||
deny: agent?.tools?.deny ?? DEFAULT_TOOL_DENY,
|
|
||||||
},
|
|
||||||
prune: {
|
|
||||||
idleHours: agent?.prune?.idleHours ?? DEFAULT_SANDBOX_IDLE_HOURS,
|
|
||||||
maxAgeDays: agent?.prune?.maxAgeDays ?? DEFAULT_SANDBOX_MAX_AGE_DAYS,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function shouldSandboxSession(
|
|
||||||
cfg: SandboxConfig,
|
|
||||||
sessionKey: string,
|
|
||||||
mainKey: string,
|
|
||||||
) {
|
|
||||||
if (cfg.mode === "off") return false;
|
|
||||||
if (cfg.mode === "all") return true;
|
|
||||||
return sessionKey.trim() !== mainKey.trim();
|
|
||||||
}
|
|
||||||
|
|
||||||
function slugifySessionKey(value: string) {
|
|
||||||
const trimmed = value.trim() || "session";
|
|
||||||
const hash = crypto
|
|
||||||
.createHash("sha1")
|
|
||||||
.update(trimmed)
|
|
||||||
.digest("hex")
|
|
||||||
.slice(0, 8);
|
|
||||||
const safe = trimmed
|
|
||||||
.toLowerCase()
|
|
||||||
.replace(/[^a-z0-9._-]+/g, "-")
|
|
||||||
.replace(/^-+|-+$/g, "");
|
|
||||||
const base = safe.slice(0, 32) || "session";
|
|
||||||
return `${base}-${hash}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
function resolveSandboxWorkspaceDir(root: string, sessionKey: string) {
|
|
||||||
const resolvedRoot = resolveUserPath(root);
|
|
||||||
const slug = slugifySessionKey(sessionKey);
|
|
||||||
return path.join(resolvedRoot, slug);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function readRegistry(): Promise<SandboxRegistry> {
|
|
||||||
try {
|
|
||||||
const raw = await fs.readFile(SANDBOX_REGISTRY_PATH, "utf-8");
|
|
||||||
const parsed = JSON.parse(raw) as SandboxRegistry;
|
|
||||||
if (parsed && Array.isArray(parsed.entries)) return parsed;
|
|
||||||
} catch {
|
|
||||||
// ignore
|
|
||||||
}
|
|
||||||
return { entries: [] };
|
|
||||||
}
|
|
||||||
|
|
||||||
async function writeRegistry(registry: SandboxRegistry) {
|
|
||||||
await fs.mkdir(SANDBOX_STATE_DIR, { recursive: true });
|
|
||||||
await fs.writeFile(
|
|
||||||
SANDBOX_REGISTRY_PATH,
|
|
||||||
`${JSON.stringify(registry, null, 2)}\n`,
|
|
||||||
"utf-8",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function updateRegistry(entry: SandboxRegistryEntry) {
|
|
||||||
const registry = await readRegistry();
|
|
||||||
const existing = registry.entries.find(
|
|
||||||
(item) => item.containerName === entry.containerName,
|
|
||||||
);
|
|
||||||
const next = registry.entries.filter(
|
|
||||||
(item) => item.containerName !== entry.containerName,
|
|
||||||
);
|
|
||||||
next.push({
|
|
||||||
...entry,
|
|
||||||
createdAtMs: existing?.createdAtMs ?? entry.createdAtMs,
|
|
||||||
image: existing?.image ?? entry.image,
|
|
||||||
});
|
|
||||||
await writeRegistry({ entries: next });
|
|
||||||
}
|
|
||||||
|
|
||||||
async function removeRegistryEntry(containerName: string) {
|
|
||||||
const registry = await readRegistry();
|
|
||||||
const next = registry.entries.filter(
|
|
||||||
(item) => item.containerName !== containerName,
|
|
||||||
);
|
|
||||||
if (next.length === registry.entries.length) return;
|
|
||||||
await writeRegistry({ entries: next });
|
|
||||||
}
|
|
||||||
|
|
||||||
async function readBrowserRegistry(): Promise<SandboxBrowserRegistry> {
|
|
||||||
try {
|
|
||||||
const raw = await fs.readFile(SANDBOX_BROWSER_REGISTRY_PATH, "utf-8");
|
|
||||||
const parsed = JSON.parse(raw) as SandboxBrowserRegistry;
|
|
||||||
if (parsed && Array.isArray(parsed.entries)) return parsed;
|
|
||||||
} catch {
|
|
||||||
// ignore
|
|
||||||
}
|
|
||||||
return { entries: [] };
|
|
||||||
}
|
|
||||||
|
|
||||||
async function writeBrowserRegistry(registry: SandboxBrowserRegistry) {
|
|
||||||
await fs.mkdir(SANDBOX_STATE_DIR, { recursive: true });
|
|
||||||
await fs.writeFile(
|
|
||||||
SANDBOX_BROWSER_REGISTRY_PATH,
|
|
||||||
`${JSON.stringify(registry, null, 2)}\n`,
|
|
||||||
"utf-8",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function updateBrowserRegistry(entry: SandboxBrowserRegistryEntry) {
|
|
||||||
const registry = await readBrowserRegistry();
|
|
||||||
const existing = registry.entries.find(
|
|
||||||
(item) => item.containerName === entry.containerName,
|
|
||||||
);
|
|
||||||
const next = registry.entries.filter(
|
|
||||||
(item) => item.containerName !== entry.containerName,
|
|
||||||
);
|
|
||||||
next.push({
|
|
||||||
...entry,
|
|
||||||
createdAtMs: existing?.createdAtMs ?? entry.createdAtMs,
|
|
||||||
image: existing?.image ?? entry.image,
|
|
||||||
});
|
|
||||||
await writeBrowserRegistry({ entries: next });
|
|
||||||
}
|
|
||||||
|
|
||||||
async function removeBrowserRegistryEntry(containerName: string) {
|
|
||||||
const registry = await readBrowserRegistry();
|
|
||||||
const next = registry.entries.filter(
|
|
||||||
(item) => item.containerName !== containerName,
|
|
||||||
);
|
|
||||||
if (next.length === registry.entries.length) return;
|
|
||||||
await writeBrowserRegistry({ entries: next });
|
|
||||||
}
|
|
||||||
|
|
||||||
function execDocker(args: string[], opts?: { allowFailure?: boolean }) {
|
|
||||||
return new Promise<{ stdout: string; stderr: string; code: number }>(
|
|
||||||
(resolve, reject) => {
|
|
||||||
const child = spawn("docker", args, {
|
|
||||||
stdio: ["ignore", "pipe", "pipe"],
|
|
||||||
});
|
|
||||||
let stdout = "";
|
|
||||||
let stderr = "";
|
|
||||||
child.stdout?.on("data", (chunk) => {
|
|
||||||
stdout += chunk.toString();
|
|
||||||
});
|
|
||||||
child.stderr?.on("data", (chunk) => {
|
|
||||||
stderr += chunk.toString();
|
|
||||||
});
|
|
||||||
child.on("close", (code) => {
|
|
||||||
const exitCode = code ?? 0;
|
|
||||||
if (exitCode !== 0 && !opts?.allowFailure) {
|
|
||||||
reject(new Error(stderr.trim() || `docker ${args.join(" ")} failed`));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
resolve({ stdout, stderr, code: exitCode });
|
|
||||||
});
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function readDockerPort(containerName: string, port: number) {
|
|
||||||
const result = await execDocker(["port", containerName, `${port}/tcp`], {
|
|
||||||
allowFailure: true,
|
|
||||||
});
|
|
||||||
if (result.code !== 0) return null;
|
|
||||||
const line = result.stdout.trim().split(/\r?\n/)[0] ?? "";
|
|
||||||
const match = line.match(/:(\d+)\s*$/);
|
|
||||||
if (!match) return null;
|
|
||||||
const mapped = Number.parseInt(match[1] ?? "", 10);
|
|
||||||
return Number.isFinite(mapped) ? mapped : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function dockerImageExists(image: string) {
|
|
||||||
const result = await execDocker(["image", "inspect", image], {
|
|
||||||
allowFailure: true,
|
|
||||||
});
|
|
||||||
return result.code === 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function ensureDockerImage(image: string) {
|
|
||||||
const exists = await dockerImageExists(image);
|
|
||||||
if (exists) return;
|
|
||||||
if (image === DEFAULT_SANDBOX_IMAGE) {
|
|
||||||
await execDocker(["pull", "debian:bookworm-slim"]);
|
|
||||||
await execDocker(["tag", "debian:bookworm-slim", DEFAULT_SANDBOX_IMAGE]);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
throw new Error(`Sandbox image not found: ${image}. Build or pull it first.`);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function dockerContainerState(name: string) {
|
|
||||||
const result = await execDocker(
|
|
||||||
["inspect", "-f", "{{.State.Running}}", name],
|
|
||||||
{ allowFailure: true },
|
|
||||||
);
|
|
||||||
if (result.code !== 0) return { exists: false, running: false };
|
|
||||||
return { exists: true, running: result.stdout.trim() === "true" };
|
|
||||||
}
|
|
||||||
|
|
||||||
async function ensureSandboxWorkspace(workspaceDir: string, seedFrom?: string) {
|
|
||||||
await fs.mkdir(workspaceDir, { recursive: true });
|
|
||||||
if (seedFrom) {
|
|
||||||
const seed = resolveUserPath(seedFrom);
|
|
||||||
const files = [
|
|
||||||
DEFAULT_AGENTS_FILENAME,
|
|
||||||
DEFAULT_SOUL_FILENAME,
|
|
||||||
DEFAULT_TOOLS_FILENAME,
|
|
||||||
DEFAULT_IDENTITY_FILENAME,
|
|
||||||
DEFAULT_USER_FILENAME,
|
|
||||||
DEFAULT_BOOTSTRAP_FILENAME,
|
|
||||||
];
|
|
||||||
for (const name of files) {
|
|
||||||
const src = path.join(seed, name);
|
|
||||||
const dest = path.join(workspaceDir, name);
|
|
||||||
try {
|
|
||||||
await fs.access(dest);
|
|
||||||
} catch {
|
|
||||||
try {
|
|
||||||
const content = await fs.readFile(src, "utf-8");
|
|
||||||
await fs.writeFile(dest, content, { encoding: "utf-8", flag: "wx" });
|
|
||||||
} catch {
|
|
||||||
// ignore missing seed file
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
await ensureAgentWorkspace({ dir: workspaceDir, ensureBootstrapFiles: true });
|
|
||||||
}
|
|
||||||
|
|
||||||
async function createSandboxContainer(params: {
|
|
||||||
name: string;
|
|
||||||
cfg: SandboxDockerConfig;
|
|
||||||
workspaceDir: string;
|
|
||||||
sessionKey: string;
|
|
||||||
}) {
|
|
||||||
const { name, cfg, workspaceDir, sessionKey } = params;
|
|
||||||
await ensureDockerImage(cfg.image);
|
|
||||||
|
|
||||||
const args = ["create", "--name", name];
|
|
||||||
args.push("--label", "clawdis.sandbox=1");
|
|
||||||
args.push("--label", `clawdis.sessionKey=${sessionKey}`);
|
|
||||||
args.push("--label", `clawdis.createdAtMs=${Date.now()}`);
|
|
||||||
if (cfg.readOnlyRoot) args.push("--read-only");
|
|
||||||
for (const entry of cfg.tmpfs) {
|
|
||||||
args.push("--tmpfs", entry);
|
|
||||||
}
|
|
||||||
if (cfg.network) args.push("--network", cfg.network);
|
|
||||||
if (cfg.user) args.push("--user", cfg.user);
|
|
||||||
for (const cap of cfg.capDrop) {
|
|
||||||
args.push("--cap-drop", cap);
|
|
||||||
}
|
|
||||||
args.push("--security-opt", "no-new-privileges");
|
|
||||||
args.push("--workdir", cfg.workdir);
|
|
||||||
args.push("-v", `${workspaceDir}:${cfg.workdir}`);
|
|
||||||
args.push(cfg.image, "sleep", "infinity");
|
|
||||||
|
|
||||||
await execDocker(args);
|
|
||||||
await execDocker(["start", name]);
|
|
||||||
|
|
||||||
if (cfg.setupCommand?.trim()) {
|
|
||||||
await execDocker(["exec", "-i", name, "sh", "-lc", cfg.setupCommand]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function ensureSandboxContainer(params: {
|
|
||||||
sessionKey: string;
|
|
||||||
workspaceDir: string;
|
|
||||||
cfg: SandboxConfig;
|
|
||||||
}) {
|
|
||||||
const slug = params.cfg.perSession
|
|
||||||
? slugifySessionKey(params.sessionKey)
|
|
||||||
: "shared";
|
|
||||||
const name = `${params.cfg.docker.containerPrefix}${slug}`;
|
|
||||||
const containerName = name.slice(0, 63);
|
|
||||||
const state = await dockerContainerState(containerName);
|
|
||||||
if (!state.exists) {
|
|
||||||
await createSandboxContainer({
|
|
||||||
name: containerName,
|
|
||||||
cfg: params.cfg.docker,
|
|
||||||
workspaceDir: params.workspaceDir,
|
|
||||||
sessionKey: params.sessionKey,
|
|
||||||
});
|
|
||||||
} else if (!state.running) {
|
|
||||||
await execDocker(["start", containerName]);
|
|
||||||
}
|
|
||||||
const now = Date.now();
|
|
||||||
await updateRegistry({
|
|
||||||
containerName,
|
|
||||||
sessionKey: params.sessionKey,
|
|
||||||
createdAtMs: now,
|
|
||||||
lastUsedAtMs: now,
|
|
||||||
image: params.cfg.docker.image,
|
|
||||||
});
|
|
||||||
return containerName;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function ensureSandboxBrowserImage(image: string) {
|
|
||||||
const exists = await dockerImageExists(image);
|
|
||||||
if (exists) return;
|
|
||||||
throw new Error(
|
|
||||||
`Sandbox browser image not found: ${image}. Build it with scripts/sandbox-browser-setup.sh.`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function buildSandboxBrowserResolvedConfig(params: {
|
|
||||||
controlPort: number;
|
|
||||||
cdpPort: number;
|
|
||||||
headless: boolean;
|
|
||||||
}): ResolvedBrowserConfig {
|
|
||||||
const controlHost = "127.0.0.1";
|
|
||||||
const controlUrl = `http://${controlHost}:${params.controlPort}`;
|
|
||||||
const cdpHost = "127.0.0.1";
|
|
||||||
const cdpUrl = `http://${cdpHost}:${params.cdpPort}`;
|
|
||||||
return {
|
|
||||||
enabled: true,
|
|
||||||
controlUrl,
|
|
||||||
controlHost,
|
|
||||||
controlPort: params.controlPort,
|
|
||||||
cdpUrl,
|
|
||||||
cdpHost,
|
|
||||||
cdpPort: params.cdpPort,
|
|
||||||
cdpIsLoopback: true,
|
|
||||||
color: DEFAULT_CLAWD_BROWSER_COLOR,
|
|
||||||
executablePath: undefined,
|
|
||||||
headless: params.headless,
|
|
||||||
noSandbox: false,
|
|
||||||
attachOnly: true,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
async function ensureSandboxBrowser(params: {
|
|
||||||
sessionKey: string;
|
|
||||||
workspaceDir: string;
|
|
||||||
cfg: SandboxConfig;
|
|
||||||
}): Promise<SandboxBrowserContext | null> {
|
|
||||||
if (!params.cfg.browser.enabled) return null;
|
|
||||||
if (!isToolAllowed(params.cfg.tools, "browser")) return null;
|
|
||||||
|
|
||||||
const slug = params.cfg.perSession
|
|
||||||
? slugifySessionKey(params.sessionKey)
|
|
||||||
: "shared";
|
|
||||||
const name = `${params.cfg.browser.containerPrefix}${slug}`;
|
|
||||||
const containerName = name.slice(0, 63);
|
|
||||||
const state = await dockerContainerState(containerName);
|
|
||||||
if (!state.exists) {
|
|
||||||
await ensureSandboxBrowserImage(params.cfg.browser.image);
|
|
||||||
const args = ["create", "--name", containerName];
|
|
||||||
args.push("--label", "clawdis.sandbox=1");
|
|
||||||
args.push("--label", "clawdis.sandboxBrowser=1");
|
|
||||||
args.push("--label", `clawdis.sessionKey=${params.sessionKey}`);
|
|
||||||
args.push("--label", `clawdis.createdAtMs=${Date.now()}`);
|
|
||||||
if (params.cfg.docker.readOnlyRoot) args.push("--read-only");
|
|
||||||
for (const entry of params.cfg.docker.tmpfs) {
|
|
||||||
args.push("--tmpfs", entry);
|
|
||||||
}
|
|
||||||
if (params.cfg.docker.network)
|
|
||||||
args.push("--network", params.cfg.docker.network);
|
|
||||||
if (params.cfg.docker.user) args.push("--user", params.cfg.docker.user);
|
|
||||||
for (const cap of params.cfg.docker.capDrop) {
|
|
||||||
args.push("--cap-drop", cap);
|
|
||||||
}
|
|
||||||
args.push("--security-opt", "no-new-privileges");
|
|
||||||
args.push("-v", `${params.workspaceDir}:${params.cfg.docker.workdir}`);
|
|
||||||
args.push("-p", `127.0.0.1::${params.cfg.browser.cdpPort}`);
|
|
||||||
if (params.cfg.browser.enableNoVnc && !params.cfg.browser.headless) {
|
|
||||||
args.push("-p", `127.0.0.1::${params.cfg.browser.noVncPort}`);
|
|
||||||
}
|
|
||||||
args.push(
|
|
||||||
"-e",
|
|
||||||
`CLAWDIS_BROWSER_HEADLESS=${params.cfg.browser.headless ? "1" : "0"}`,
|
|
||||||
);
|
|
||||||
args.push(
|
|
||||||
"-e",
|
|
||||||
`CLAWDIS_BROWSER_ENABLE_NOVNC=${
|
|
||||||
params.cfg.browser.enableNoVnc ? "1" : "0"
|
|
||||||
}`,
|
|
||||||
);
|
|
||||||
args.push("-e", `CLAWDIS_BROWSER_CDP_PORT=${params.cfg.browser.cdpPort}`);
|
|
||||||
args.push("-e", `CLAWDIS_BROWSER_VNC_PORT=${params.cfg.browser.vncPort}`);
|
|
||||||
args.push(
|
|
||||||
"-e",
|
|
||||||
`CLAWDIS_BROWSER_NOVNC_PORT=${params.cfg.browser.noVncPort}`,
|
|
||||||
);
|
|
||||||
args.push(params.cfg.browser.image);
|
|
||||||
await execDocker(args);
|
|
||||||
await execDocker(["start", containerName]);
|
|
||||||
} else if (!state.running) {
|
|
||||||
await execDocker(["start", containerName]);
|
|
||||||
}
|
|
||||||
|
|
||||||
const mappedCdp = await readDockerPort(
|
|
||||||
containerName,
|
|
||||||
params.cfg.browser.cdpPort,
|
|
||||||
);
|
|
||||||
if (!mappedCdp) {
|
|
||||||
throw new Error(`Failed to resolve CDP port mapping for ${containerName}.`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const mappedNoVnc =
|
|
||||||
params.cfg.browser.enableNoVnc && !params.cfg.browser.headless
|
|
||||||
? await readDockerPort(containerName, params.cfg.browser.noVncPort)
|
|
||||||
: null;
|
|
||||||
|
|
||||||
const existing = BROWSER_BRIDGES.get(params.sessionKey);
|
|
||||||
const shouldReuse =
|
|
||||||
existing &&
|
|
||||||
existing.containerName === containerName &&
|
|
||||||
existing.bridge.state.resolved.cdpPort === mappedCdp;
|
|
||||||
if (existing && !shouldReuse) {
|
|
||||||
await stopBrowserBridgeServer(existing.bridge.server).catch(
|
|
||||||
() => undefined,
|
|
||||||
);
|
|
||||||
BROWSER_BRIDGES.delete(params.sessionKey);
|
|
||||||
}
|
|
||||||
let bridge: BrowserBridge;
|
|
||||||
if (shouldReuse && existing) {
|
|
||||||
bridge = existing.bridge;
|
|
||||||
} else {
|
|
||||||
bridge = await startBrowserBridgeServer({
|
|
||||||
resolved: buildSandboxBrowserResolvedConfig({
|
|
||||||
controlPort: 0,
|
|
||||||
cdpPort: mappedCdp,
|
|
||||||
headless: params.cfg.browser.headless,
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
if (!shouldReuse) {
|
|
||||||
BROWSER_BRIDGES.set(params.sessionKey, { bridge, containerName });
|
|
||||||
}
|
|
||||||
|
|
||||||
const now = Date.now();
|
|
||||||
await updateBrowserRegistry({
|
|
||||||
containerName,
|
|
||||||
sessionKey: params.sessionKey,
|
|
||||||
createdAtMs: now,
|
|
||||||
lastUsedAtMs: now,
|
|
||||||
image: params.cfg.browser.image,
|
|
||||||
cdpPort: mappedCdp,
|
|
||||||
noVncPort: mappedNoVnc ?? undefined,
|
|
||||||
});
|
|
||||||
|
|
||||||
const noVncUrl =
|
|
||||||
mappedNoVnc &&
|
|
||||||
params.cfg.browser.enableNoVnc &&
|
|
||||||
!params.cfg.browser.headless
|
|
||||||
? `http://127.0.0.1:${mappedNoVnc}/vnc.html?autoconnect=1&resize=remote`
|
|
||||||
: undefined;
|
|
||||||
|
|
||||||
return {
|
|
||||||
controlUrl: bridge.baseUrl,
|
|
||||||
noVncUrl,
|
|
||||||
containerName,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
async function pruneSandboxContainers(cfg: SandboxConfig) {
|
|
||||||
const now = Date.now();
|
|
||||||
const idleHours = cfg.prune.idleHours;
|
|
||||||
const maxAgeDays = cfg.prune.maxAgeDays;
|
|
||||||
if (idleHours === 0 && maxAgeDays === 0) return;
|
|
||||||
const registry = await readRegistry();
|
|
||||||
for (const entry of registry.entries) {
|
|
||||||
const idleMs = now - entry.lastUsedAtMs;
|
|
||||||
const ageMs = now - entry.createdAtMs;
|
|
||||||
if (
|
|
||||||
(idleHours > 0 && idleMs > idleHours * 60 * 60 * 1000) ||
|
|
||||||
(maxAgeDays > 0 && ageMs > maxAgeDays * 24 * 60 * 60 * 1000)
|
|
||||||
) {
|
|
||||||
try {
|
|
||||||
await execDocker(["rm", "-f", entry.containerName], {
|
|
||||||
allowFailure: true,
|
|
||||||
});
|
|
||||||
} catch {
|
|
||||||
// ignore prune failures
|
|
||||||
} finally {
|
|
||||||
await removeRegistryEntry(entry.containerName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function pruneSandboxBrowsers(cfg: SandboxConfig) {
|
|
||||||
const now = Date.now();
|
|
||||||
const idleHours = cfg.prune.idleHours;
|
|
||||||
const maxAgeDays = cfg.prune.maxAgeDays;
|
|
||||||
if (idleHours === 0 && maxAgeDays === 0) return;
|
|
||||||
const registry = await readBrowserRegistry();
|
|
||||||
for (const entry of registry.entries) {
|
|
||||||
const idleMs = now - entry.lastUsedAtMs;
|
|
||||||
const ageMs = now - entry.createdAtMs;
|
|
||||||
if (
|
|
||||||
(idleHours > 0 && idleMs > idleHours * 60 * 60 * 1000) ||
|
|
||||||
(maxAgeDays > 0 && ageMs > maxAgeDays * 24 * 60 * 60 * 1000)
|
|
||||||
) {
|
|
||||||
try {
|
|
||||||
await execDocker(["rm", "-f", entry.containerName], {
|
|
||||||
allowFailure: true,
|
|
||||||
});
|
|
||||||
} catch {
|
|
||||||
// ignore prune failures
|
|
||||||
} finally {
|
|
||||||
await removeBrowserRegistryEntry(entry.containerName);
|
|
||||||
const bridge = BROWSER_BRIDGES.get(entry.sessionKey);
|
|
||||||
if (bridge?.containerName === entry.containerName) {
|
|
||||||
await stopBrowserBridgeServer(bridge.bridge.server).catch(
|
|
||||||
() => undefined,
|
|
||||||
);
|
|
||||||
BROWSER_BRIDGES.delete(entry.sessionKey);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function maybePruneSandboxes(cfg: SandboxConfig) {
|
|
||||||
const now = Date.now();
|
|
||||||
if (now - lastPruneAtMs < 5 * 60 * 1000) return;
|
|
||||||
lastPruneAtMs = now;
|
|
||||||
try {
|
|
||||||
await pruneSandboxContainers(cfg);
|
|
||||||
await pruneSandboxBrowsers(cfg);
|
|
||||||
} catch (error) {
|
|
||||||
const message =
|
|
||||||
error instanceof Error
|
|
||||||
? error.message
|
|
||||||
: typeof error === "string"
|
|
||||||
? error
|
|
||||||
: JSON.stringify(error);
|
|
||||||
defaultRuntime.error?.(
|
|
||||||
`Sandbox prune failed: ${message ?? "unknown error"}`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function resolveSandboxContext(params: {
|
|
||||||
config?: ClawdisConfig;
|
|
||||||
sessionKey?: string;
|
|
||||||
workspaceDir?: string;
|
|
||||||
}): Promise<SandboxContext | null> {
|
|
||||||
const rawSessionKey = params.sessionKey?.trim();
|
|
||||||
if (!rawSessionKey) return null;
|
|
||||||
const cfg = defaultSandboxConfig(params.config);
|
|
||||||
const mainKey = params.config?.session?.mainKey?.trim() || "main";
|
|
||||||
if (!shouldSandboxSession(cfg, rawSessionKey, mainKey)) return null;
|
|
||||||
|
|
||||||
await maybePruneSandboxes(cfg);
|
|
||||||
|
|
||||||
const workspaceRoot = resolveUserPath(cfg.workspaceRoot);
|
|
||||||
const workspaceDir = cfg.perSession
|
|
||||||
? resolveSandboxWorkspaceDir(workspaceRoot, rawSessionKey)
|
|
||||||
: workspaceRoot;
|
|
||||||
const seedWorkspace =
|
|
||||||
params.workspaceDir?.trim() || DEFAULT_AGENT_WORKSPACE_DIR;
|
|
||||||
await ensureSandboxWorkspace(workspaceDir, seedWorkspace);
|
|
||||||
|
|
||||||
const containerName = await ensureSandboxContainer({
|
|
||||||
sessionKey: rawSessionKey,
|
|
||||||
workspaceDir,
|
|
||||||
cfg,
|
|
||||||
});
|
|
||||||
|
|
||||||
const browser = await ensureSandboxBrowser({
|
|
||||||
sessionKey: rawSessionKey,
|
|
||||||
workspaceDir,
|
|
||||||
cfg,
|
|
||||||
});
|
|
||||||
|
|
||||||
return {
|
|
||||||
enabled: true,
|
|
||||||
sessionKey: rawSessionKey,
|
|
||||||
workspaceDir,
|
|
||||||
containerName,
|
|
||||||
containerWorkdir: cfg.docker.workdir,
|
|
||||||
docker: cfg.docker,
|
|
||||||
tools: cfg.tools,
|
|
||||||
browser: browser ?? undefined,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@@ -13,12 +13,6 @@ export function buildAgentSystemPromptAppend(params: {
|
|||||||
node?: string;
|
node?: string;
|
||||||
model?: string;
|
model?: string;
|
||||||
};
|
};
|
||||||
sandboxInfo?: {
|
|
||||||
enabled: boolean;
|
|
||||||
workspaceDir?: string;
|
|
||||||
browserControlUrl?: string;
|
|
||||||
browserNoVncUrl?: string;
|
|
||||||
};
|
|
||||||
}) {
|
}) {
|
||||||
const thinkHint =
|
const thinkHint =
|
||||||
params.defaultThinkLevel && params.defaultThinkLevel !== "off"
|
params.defaultThinkLevel && params.defaultThinkLevel !== "off"
|
||||||
@@ -78,25 +72,6 @@ export function buildAgentSystemPromptAppend(params: {
|
|||||||
`Your working directory is: ${params.workspaceDir}`,
|
`Your working directory is: ${params.workspaceDir}`,
|
||||||
"Treat this directory as the single global workspace for file operations unless explicitly instructed otherwise.",
|
"Treat this directory as the single global workspace for file operations unless explicitly instructed otherwise.",
|
||||||
"",
|
"",
|
||||||
params.sandboxInfo?.enabled ? "## Sandbox" : "",
|
|
||||||
params.sandboxInfo?.enabled
|
|
||||||
? [
|
|
||||||
"Tool execution is isolated in a Docker sandbox.",
|
|
||||||
"Some tools may be unavailable due to sandbox policy.",
|
|
||||||
params.sandboxInfo.workspaceDir
|
|
||||||
? `Sandbox workspace: ${params.sandboxInfo.workspaceDir}`
|
|
||||||
: "",
|
|
||||||
params.sandboxInfo.browserControlUrl
|
|
||||||
? `Sandbox browser control URL: ${params.sandboxInfo.browserControlUrl}`
|
|
||||||
: "",
|
|
||||||
params.sandboxInfo.browserNoVncUrl
|
|
||||||
? `Sandbox browser observer (noVNC): ${params.sandboxInfo.browserNoVncUrl}`
|
|
||||||
: "",
|
|
||||||
]
|
|
||||||
.filter(Boolean)
|
|
||||||
.join("\n")
|
|
||||||
: "",
|
|
||||||
params.sandboxInfo?.enabled ? "" : "",
|
|
||||||
ownerLine ? "## User Identity" : "",
|
ownerLine ? "## User Identity" : "",
|
||||||
ownerLine ?? "",
|
ownerLine ?? "",
|
||||||
ownerLine ? "" : "",
|
ownerLine ? "" : "",
|
||||||
|
|||||||
@@ -150,21 +150,6 @@
|
|||||||
"restart": { "label": "restart", "detailKeys": ["reason", "delayMs"] }
|
"restart": { "label": "restart", "detailKeys": ["reason", "delayMs"] }
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"sessions_list": {
|
|
||||||
"emoji": "🗂️",
|
|
||||||
"title": "Sessions",
|
|
||||||
"detailKeys": ["kinds", "limit", "activeMinutes", "messageLimit"]
|
|
||||||
},
|
|
||||||
"sessions_history": {
|
|
||||||
"emoji": "🧾",
|
|
||||||
"title": "Session History",
|
|
||||||
"detailKeys": ["sessionKey", "limit"]
|
|
||||||
},
|
|
||||||
"sessions_send": {
|
|
||||||
"emoji": "📨",
|
|
||||||
"title": "Session Send",
|
|
||||||
"detailKeys": ["sessionKey", "timeoutSeconds"]
|
|
||||||
},
|
|
||||||
"whatsapp_login": {
|
"whatsapp_login": {
|
||||||
"emoji": "🟢",
|
"emoji": "🟢",
|
||||||
"title": "WhatsApp Login",
|
"title": "WhatsApp Login",
|
||||||
|
|||||||
@@ -107,42 +107,6 @@ describe("trigger handling", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("allows owner to set send policy", async () => {
|
|
||||||
await withTempHome(async (home) => {
|
|
||||||
const cfg = {
|
|
||||||
agent: {
|
|
||||||
model: "anthropic/claude-opus-4-5",
|
|
||||||
workspace: join(home, "clawd"),
|
|
||||||
},
|
|
||||||
whatsapp: {
|
|
||||||
allowFrom: ["+1000"],
|
|
||||||
},
|
|
||||||
session: { store: join(home, "sessions.json") },
|
|
||||||
};
|
|
||||||
|
|
||||||
const res = await getReplyFromConfig(
|
|
||||||
{
|
|
||||||
Body: "/send off",
|
|
||||||
From: "+1000",
|
|
||||||
To: "+2000",
|
|
||||||
Surface: "whatsapp",
|
|
||||||
SenderE164: "+1000",
|
|
||||||
},
|
|
||||||
{},
|
|
||||||
cfg,
|
|
||||||
);
|
|
||||||
const text = Array.isArray(res) ? res[0]?.text : res?.text;
|
|
||||||
expect(text).toContain("Send policy set to off");
|
|
||||||
|
|
||||||
const storeRaw = await fs.readFile(cfg.session.store, "utf-8");
|
|
||||||
const store = JSON.parse(storeRaw) as Record<
|
|
||||||
string,
|
|
||||||
{ sendPolicy?: string }
|
|
||||||
>;
|
|
||||||
expect(store.main?.sendPolicy).toBe("deny");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it("returns a context overflow fallback when the embedded agent throws", async () => {
|
it("returns a context overflow fallback when the embedded agent throws", async () => {
|
||||||
await withTempHome(async (home) => {
|
await withTempHome(async (home) => {
|
||||||
vi.mocked(runEmbeddedPiAgent).mockRejectedValue(
|
vi.mocked(runEmbeddedPiAgent).mockRejectedValue(
|
||||||
@@ -167,30 +131,6 @@ describe("trigger handling", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("includes the error cause when the embedded agent throws", async () => {
|
|
||||||
await withTempHome(async (home) => {
|
|
||||||
vi.mocked(runEmbeddedPiAgent).mockRejectedValue(
|
|
||||||
new Error("sandbox is not defined"),
|
|
||||||
);
|
|
||||||
|
|
||||||
const res = await getReplyFromConfig(
|
|
||||||
{
|
|
||||||
Body: "hello",
|
|
||||||
From: "+1002",
|
|
||||||
To: "+2000",
|
|
||||||
},
|
|
||||||
{},
|
|
||||||
makeCfg(home),
|
|
||||||
);
|
|
||||||
|
|
||||||
const text = Array.isArray(res) ? res[0]?.text : res?.text;
|
|
||||||
expect(text).toBe(
|
|
||||||
"⚠️ Agent failed before reply: sandbox is not defined. Check gateway logs for details.",
|
|
||||||
);
|
|
||||||
expect(runEmbeddedPiAgent).toHaveBeenCalledOnce();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it("uses heartbeat model override for heartbeat runs", async () => {
|
it("uses heartbeat model override for heartbeat runs", async () => {
|
||||||
await withTempHome(async (home) => {
|
await withTempHome(async (home) => {
|
||||||
vi.mocked(runEmbeddedPiAgent).mockResolvedValue({
|
vi.mocked(runEmbeddedPiAgent).mockResolvedValue({
|
||||||
|
|||||||
+3
-54
@@ -55,7 +55,6 @@ import {
|
|||||||
} from "../infra/system-events.js";
|
} from "../infra/system-events.js";
|
||||||
import { clearCommandLane, getQueueSize } from "../process/command-queue.js";
|
import { clearCommandLane, getQueueSize } from "../process/command-queue.js";
|
||||||
import { defaultRuntime } from "../runtime.js";
|
import { defaultRuntime } from "../runtime.js";
|
||||||
import { resolveSendPolicy } from "../sessions/send-policy.js";
|
|
||||||
import { normalizeE164 } from "../utils.js";
|
import { normalizeE164 } from "../utils.js";
|
||||||
import { resolveHeartbeatSeconds } from "../web/reconnect.js";
|
import { resolveHeartbeatSeconds } from "../web/reconnect.js";
|
||||||
import { getWebAuthAgeMs, webAuthExists } from "../web/session.js";
|
import { getWebAuthAgeMs, webAuthExists } from "../web/session.js";
|
||||||
@@ -66,7 +65,6 @@ import {
|
|||||||
} from "./group-activation.js";
|
} from "./group-activation.js";
|
||||||
import { stripHeartbeatToken } from "./heartbeat.js";
|
import { stripHeartbeatToken } from "./heartbeat.js";
|
||||||
import { extractModelDirective } from "./model.js";
|
import { extractModelDirective } from "./model.js";
|
||||||
import { parseSendPolicyCommand } from "./send-policy.js";
|
|
||||||
import { buildStatusMessage } from "./status.js";
|
import { buildStatusMessage } from "./status.js";
|
||||||
import type { MsgContext, TemplateContext } from "./templating.js";
|
import type { MsgContext, TemplateContext } from "./templating.js";
|
||||||
import {
|
import {
|
||||||
@@ -833,11 +831,7 @@ export async function getReplyFromConfig(
|
|||||||
const typingIntervalSeconds =
|
const typingIntervalSeconds =
|
||||||
typeof configuredTypingSeconds === "number" ? configuredTypingSeconds : 6;
|
typeof configuredTypingSeconds === "number" ? configuredTypingSeconds : 6;
|
||||||
const typingIntervalMs = typingIntervalSeconds * 1000;
|
const typingIntervalMs = typingIntervalSeconds * 1000;
|
||||||
const typingTtlMs = 2 * 60_000;
|
const typingTtlMs = Math.min(Math.max(15_000, typingIntervalMs * 5), 60_000);
|
||||||
const formatTypingTtl = (ms: number) => {
|
|
||||||
if (ms % 60_000 === 0) return `${ms / 60_000}m`;
|
|
||||||
return `${Math.round(ms / 1000)}s`;
|
|
||||||
};
|
|
||||||
const cleanupTyping = () => {
|
const cleanupTyping = () => {
|
||||||
if (typingTtlTimer) {
|
if (typingTtlTimer) {
|
||||||
clearTimeout(typingTtlTimer);
|
clearTimeout(typingTtlTimer);
|
||||||
@@ -858,7 +852,7 @@ export async function getReplyFromConfig(
|
|||||||
typingTtlTimer = setTimeout(() => {
|
typingTtlTimer = setTimeout(() => {
|
||||||
if (!typingTimer) return;
|
if (!typingTimer) return;
|
||||||
defaultRuntime.log(
|
defaultRuntime.log(
|
||||||
`typing TTL reached (${formatTypingTtl(typingTtlMs)}); stopping typing indicator`,
|
`typing TTL reached (${typingTtlMs}ms); stopping typing indicator`,
|
||||||
);
|
);
|
||||||
cleanupTyping();
|
cleanupTyping();
|
||||||
}, typingTtlMs);
|
}, typingTtlMs);
|
||||||
@@ -992,7 +986,6 @@ export async function getReplyFromConfig(
|
|||||||
verboseLevel: persistedVerbose ?? baseEntry?.verboseLevel,
|
verboseLevel: persistedVerbose ?? baseEntry?.verboseLevel,
|
||||||
modelOverride: persistedModelOverride ?? baseEntry?.modelOverride,
|
modelOverride: persistedModelOverride ?? baseEntry?.modelOverride,
|
||||||
providerOverride: persistedProviderOverride ?? baseEntry?.providerOverride,
|
providerOverride: persistedProviderOverride ?? baseEntry?.providerOverride,
|
||||||
sendPolicy: baseEntry?.sendPolicy,
|
|
||||||
queueMode: baseEntry?.queueMode,
|
queueMode: baseEntry?.queueMode,
|
||||||
queueDebounceMs: baseEntry?.queueDebounceMs,
|
queueDebounceMs: baseEntry?.queueDebounceMs,
|
||||||
queueCap: baseEntry?.queueCap,
|
queueCap: baseEntry?.queueCap,
|
||||||
@@ -1594,7 +1587,6 @@ export async function getReplyFromConfig(
|
|||||||
? stripMentions(rawBodyNormalized, ctx, cfg)
|
? stripMentions(rawBodyNormalized, ctx, cfg)
|
||||||
: rawBodyNormalized;
|
: rawBodyNormalized;
|
||||||
const activationCommand = parseActivationCommand(commandBodyNormalized);
|
const activationCommand = parseActivationCommand(commandBodyNormalized);
|
||||||
const sendPolicyCommand = parseSendPolicyCommand(commandBodyNormalized);
|
|
||||||
const senderE164 = normalizeE164(ctx.SenderE164 ?? "");
|
const senderE164 = normalizeE164(ctx.SenderE164 ?? "");
|
||||||
const ownerCandidates = isWhatsAppSurface
|
const ownerCandidates = isWhatsAppSurface
|
||||||
? (allowFrom ?? []).filter((entry) => entry && entry !== "*")
|
? (allowFrom ?? []).filter((entry) => entry && entry !== "*")
|
||||||
@@ -1641,36 +1633,6 @@ export async function getReplyFromConfig(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sendPolicyCommand.hasCommand) {
|
|
||||||
if (!isOwnerSender) {
|
|
||||||
logVerbose(`Ignoring /send from non-owner: ${senderE164 || "<unknown>"}`);
|
|
||||||
cleanupTyping();
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
if (!sendPolicyCommand.mode) {
|
|
||||||
cleanupTyping();
|
|
||||||
return { text: "⚙️ Usage: /send on|off|inherit" };
|
|
||||||
}
|
|
||||||
if (sessionEntry && sessionStore && sessionKey) {
|
|
||||||
if (sendPolicyCommand.mode === "inherit") {
|
|
||||||
delete sessionEntry.sendPolicy;
|
|
||||||
} else {
|
|
||||||
sessionEntry.sendPolicy = sendPolicyCommand.mode;
|
|
||||||
}
|
|
||||||
sessionEntry.updatedAt = Date.now();
|
|
||||||
sessionStore[sessionKey] = sessionEntry;
|
|
||||||
await saveSessionStore(storePath, sessionStore);
|
|
||||||
}
|
|
||||||
cleanupTyping();
|
|
||||||
const label =
|
|
||||||
sendPolicyCommand.mode === "inherit"
|
|
||||||
? "inherit"
|
|
||||||
: sendPolicyCommand.mode === "allow"
|
|
||||||
? "on"
|
|
||||||
: "off";
|
|
||||||
return { text: `⚙️ Send policy set to ${label}.` };
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
if (
|
||||||
commandBodyNormalized === "/restart" ||
|
commandBodyNormalized === "/restart" ||
|
||||||
commandBodyNormalized === "restart" ||
|
commandBodyNormalized === "restart" ||
|
||||||
@@ -1748,19 +1710,6 @@ export async function getReplyFromConfig(
|
|||||||
return { text: "⚙️ Agent was aborted." };
|
return { text: "⚙️ Agent was aborted." };
|
||||||
}
|
}
|
||||||
|
|
||||||
const sendPolicy = resolveSendPolicy({
|
|
||||||
cfg,
|
|
||||||
entry: sessionEntry,
|
|
||||||
sessionKey,
|
|
||||||
surface: sessionEntry?.surface ?? surface,
|
|
||||||
chatType: sessionEntry?.chatType,
|
|
||||||
});
|
|
||||||
if (sendPolicy === "deny") {
|
|
||||||
logVerbose(`Send blocked by policy for session ${sessionKey ?? "unknown"}`);
|
|
||||||
cleanupTyping();
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
const isFirstTurnInSession = isNewSession || !systemSent;
|
const isFirstTurnInSession = isNewSession || !systemSent;
|
||||||
const isGroupChat = sessionCtx.ChatType === "group";
|
const isGroupChat = sessionCtx.ChatType === "group";
|
||||||
const wasMentioned = ctx.WasMentioned === true;
|
const wasMentioned = ctx.WasMentioned === true;
|
||||||
@@ -2342,7 +2291,7 @@ export async function getReplyFromConfig(
|
|||||||
return finalizeWithFollowup({
|
return finalizeWithFollowup({
|
||||||
text: isContextOverflow
|
text: isContextOverflow
|
||||||
? "⚠️ Context overflow - conversation too long. Starting fresh might help!"
|
? "⚠️ Context overflow - conversation too long. Starting fresh might help!"
|
||||||
: `⚠️ Agent failed before reply: ${message}. Check gateway logs for details.`,
|
: "⚠️ Agent failed. Check gateway logs.",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,29 +0,0 @@
|
|||||||
export type SendPolicyOverride = "allow" | "deny";
|
|
||||||
|
|
||||||
export function normalizeSendPolicyOverride(
|
|
||||||
raw?: string | null,
|
|
||||||
): SendPolicyOverride | undefined {
|
|
||||||
const value = raw?.trim().toLowerCase();
|
|
||||||
if (!value) return undefined;
|
|
||||||
if (value === "allow" || value === "on") return "allow";
|
|
||||||
if (value === "deny" || value === "off") return "deny";
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function parseSendPolicyCommand(raw?: string): {
|
|
||||||
hasCommand: boolean;
|
|
||||||
mode?: SendPolicyOverride | "inherit";
|
|
||||||
} {
|
|
||||||
if (!raw) return { hasCommand: false };
|
|
||||||
const trimmed = raw.trim();
|
|
||||||
if (!trimmed) return { hasCommand: false };
|
|
||||||
const match = trimmed.match(/^\/?send\b(?:\s+([a-zA-Z]+))?/i);
|
|
||||||
if (!match) return { hasCommand: false };
|
|
||||||
const token = match[1]?.trim().toLowerCase();
|
|
||||||
if (!token) return { hasCommand: true };
|
|
||||||
if (token === "inherit" || token === "default" || token === "reset") {
|
|
||||||
return { hasCommand: true, mode: "inherit" };
|
|
||||||
}
|
|
||||||
const mode = normalizeSendPolicyOverride(token);
|
|
||||||
return { hasCommand: true, mode };
|
|
||||||
}
|
|
||||||
@@ -1,67 +0,0 @@
|
|||||||
import type { Server } from "node:http";
|
|
||||||
import type { AddressInfo } from "node:net";
|
|
||||||
import express from "express";
|
|
||||||
|
|
||||||
import type { ResolvedBrowserConfig } from "./config.js";
|
|
||||||
import { registerBrowserRoutes } from "./routes/index.js";
|
|
||||||
import {
|
|
||||||
type BrowserServerState,
|
|
||||||
createBrowserRouteContext,
|
|
||||||
} from "./server-context.js";
|
|
||||||
|
|
||||||
export type BrowserBridge = {
|
|
||||||
server: Server;
|
|
||||||
port: number;
|
|
||||||
baseUrl: string;
|
|
||||||
state: BrowserServerState;
|
|
||||||
};
|
|
||||||
|
|
||||||
export async function startBrowserBridgeServer(params: {
|
|
||||||
resolved: ResolvedBrowserConfig;
|
|
||||||
host?: string;
|
|
||||||
port?: number;
|
|
||||||
}): Promise<BrowserBridge> {
|
|
||||||
const host = params.host ?? "127.0.0.1";
|
|
||||||
const port = params.port ?? 0;
|
|
||||||
|
|
||||||
const app = express();
|
|
||||||
app.use(express.json({ limit: "1mb" }));
|
|
||||||
|
|
||||||
const state: BrowserServerState = {
|
|
||||||
server: null as unknown as Server,
|
|
||||||
port,
|
|
||||||
cdpPort: params.resolved.cdpPort,
|
|
||||||
running: null,
|
|
||||||
resolved: params.resolved,
|
|
||||||
};
|
|
||||||
|
|
||||||
const ctx = createBrowserRouteContext({
|
|
||||||
getState: () => state,
|
|
||||||
setRunning: (running) => {
|
|
||||||
state.running = running;
|
|
||||||
},
|
|
||||||
});
|
|
||||||
registerBrowserRoutes(app, ctx);
|
|
||||||
|
|
||||||
const server = await new Promise<Server>((resolve, reject) => {
|
|
||||||
const s = app.listen(port, host, () => resolve(s));
|
|
||||||
s.once("error", reject);
|
|
||||||
});
|
|
||||||
|
|
||||||
const address = server.address() as AddressInfo | null;
|
|
||||||
const resolvedPort = address?.port ?? port;
|
|
||||||
state.server = server;
|
|
||||||
state.port = resolvedPort;
|
|
||||||
state.resolved.controlHost = host;
|
|
||||||
state.resolved.controlPort = resolvedPort;
|
|
||||||
state.resolved.controlUrl = `http://${host}:${resolvedPort}`;
|
|
||||||
|
|
||||||
const baseUrl = state.resolved.controlUrl;
|
|
||||||
return { server, port: resolvedPort, baseUrl, state };
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function stopBrowserBridgeServer(server: Server): Promise<void> {
|
|
||||||
await new Promise<void>((resolve) => {
|
|
||||||
server.close(() => resolve());
|
|
||||||
});
|
|
||||||
}
|
|
||||||
@@ -211,40 +211,49 @@ export async function evaluateViaPlaywright(opts: {
|
|||||||
ensurePageState(page);
|
ensurePageState(page);
|
||||||
if (opts.ref) {
|
if (opts.ref) {
|
||||||
const locator = refLocator(page, opts.ref);
|
const locator = refLocator(page, opts.ref);
|
||||||
// Use Function constructor at runtime to avoid esbuild adding __name helper
|
return await locator.evaluate((el, fnBody) => {
|
||||||
// which doesn't exist in the browser context
|
const compileRunner = (body: string) => {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-implied-eval -- required for browser-context eval
|
const inner = `"use strict"; const candidate = ${body}; return typeof candidate === "function" ? candidate(element) : candidate;`;
|
||||||
const elementEvaluator = new Function(
|
// This intentionally evaluates user-supplied code in the browser context.
|
||||||
"el",
|
// oxlint-disable-next-line typescript-eslint/no-implied-eval
|
||||||
"fnBody",
|
return new Function("element", inner) as (element: Element) => unknown;
|
||||||
`
|
};
|
||||||
"use strict";
|
let compiled: unknown;
|
||||||
try {
|
try {
|
||||||
var candidate = eval("(" + fnBody + ")");
|
compiled = compileRunner(fnBody);
|
||||||
return typeof candidate === "function" ? candidate(el) : candidate;
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw new Error("Invalid evaluate function: " + (err && err.message ? err.message : String(err)));
|
const message =
|
||||||
|
err instanceof Error
|
||||||
|
? err.message
|
||||||
|
: typeof err === "string"
|
||||||
|
? err
|
||||||
|
: "invalid expression";
|
||||||
|
throw new Error(`Invalid evaluate function: ${message}`);
|
||||||
}
|
}
|
||||||
`,
|
return (compiled as (element: Element) => unknown)(el as Element);
|
||||||
) as (el: Element, fnBody: string) => unknown;
|
}, fnText);
|
||||||
return await locator.evaluate(elementEvaluator, fnText);
|
|
||||||
}
|
}
|
||||||
// Use Function constructor at runtime to avoid esbuild adding __name helper
|
return await page.evaluate((fnBody) => {
|
||||||
// which doesn't exist in the browser context
|
const compileRunner = (body: string) => {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-implied-eval -- required for browser-context eval
|
const inner = `"use strict"; const candidate = ${body}; return typeof candidate === "function" ? candidate() : candidate;`;
|
||||||
const browserEvaluator = new Function(
|
// This intentionally evaluates user-supplied code in the browser context.
|
||||||
"fnBody",
|
// oxlint-disable-next-line typescript-eslint/no-implied-eval
|
||||||
`
|
return new Function(inner) as () => unknown;
|
||||||
"use strict";
|
};
|
||||||
|
let compiled: unknown;
|
||||||
try {
|
try {
|
||||||
var candidate = eval("(" + fnBody + ")");
|
compiled = compileRunner(fnBody);
|
||||||
return typeof candidate === "function" ? candidate() : candidate;
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw new Error("Invalid evaluate function: " + (err && err.message ? err.message : String(err)));
|
const message =
|
||||||
|
err instanceof Error
|
||||||
|
? err.message
|
||||||
|
: typeof err === "string"
|
||||||
|
? err
|
||||||
|
: "invalid expression";
|
||||||
|
throw new Error(`Invalid evaluate function: ${message}`);
|
||||||
}
|
}
|
||||||
`,
|
return (compiled as () => unknown)();
|
||||||
) as (fnBody: string) => unknown;
|
}, fnText);
|
||||||
return await page.evaluate(browserEvaluator, fnText);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function armFileUploadViaPlaywright(opts: {
|
export async function armFileUploadViaPlaywright(opts: {
|
||||||
|
|||||||
@@ -129,30 +129,6 @@ describe("gateway-cli coverage", () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("passes gifPlayback for gateway send when flag set", async () => {
|
|
||||||
runtimeLogs.length = 0;
|
|
||||||
runtimeErrors.length = 0;
|
|
||||||
callGateway.mockClear();
|
|
||||||
randomIdempotencyKey.mockClear();
|
|
||||||
|
|
||||||
const { registerGatewayCli } = await import("./gateway-cli.js");
|
|
||||||
const program = new Command();
|
|
||||||
program.exitOverride();
|
|
||||||
registerGatewayCli(program);
|
|
||||||
|
|
||||||
await program.parseAsync(
|
|
||||||
["gateway", "send", "--to", "+1555", "--message", "hi", "--gif-playback"],
|
|
||||||
{ from: "user" },
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(callGateway).toHaveBeenCalledWith(
|
|
||||||
expect.objectContaining({
|
|
||||||
method: "send",
|
|
||||||
params: expect.objectContaining({ gifPlayback: true }),
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("validates gateway ports and handles force/start errors", async () => {
|
it("validates gateway ports and handles force/start errors", async () => {
|
||||||
runtimeLogs.length = 0;
|
runtimeLogs.length = 0;
|
||||||
runtimeErrors.length = 0;
|
runtimeErrors.length = 0;
|
||||||
|
|||||||
+1
-10
@@ -33,14 +33,7 @@ type GatewayRunSignalAction = "stop" | "restart";
|
|||||||
|
|
||||||
function parsePort(raw: unknown): number | null {
|
function parsePort(raw: unknown): number | null {
|
||||||
if (raw === undefined || raw === null) return null;
|
if (raw === undefined || raw === null) return null;
|
||||||
const value =
|
const parsed = Number.parseInt(String(raw), 10);
|
||||||
typeof raw === "string"
|
|
||||||
? raw
|
|
||||||
: typeof raw === "number" || typeof raw === "bigint"
|
|
||||||
? raw.toString()
|
|
||||||
: null;
|
|
||||||
if (value === null) return null;
|
|
||||||
const parsed = Number.parseInt(value, 10);
|
|
||||||
if (!Number.isFinite(parsed) || parsed <= 0) return null;
|
if (!Number.isFinite(parsed) || parsed <= 0) return null;
|
||||||
return parsed;
|
return parsed;
|
||||||
}
|
}
|
||||||
@@ -578,7 +571,6 @@ export function registerGatewayCli(program: Command) {
|
|||||||
.requiredOption("--to <jidOrPhone>", "Destination (E.164 or jid)")
|
.requiredOption("--to <jidOrPhone>", "Destination (E.164 or jid)")
|
||||||
.requiredOption("--message <text>", "Message text")
|
.requiredOption("--message <text>", "Message text")
|
||||||
.option("--media-url <url>", "Optional media URL")
|
.option("--media-url <url>", "Optional media URL")
|
||||||
.option("--gif-playback", "Treat video media as GIF playback", false)
|
|
||||||
.option("--idempotency-key <key>", "Idempotency key")
|
.option("--idempotency-key <key>", "Idempotency key")
|
||||||
.action(async (opts) => {
|
.action(async (opts) => {
|
||||||
try {
|
try {
|
||||||
@@ -587,7 +579,6 @@ export function registerGatewayCli(program: Command) {
|
|||||||
to: opts.to,
|
to: opts.to,
|
||||||
message: opts.message,
|
message: opts.message,
|
||||||
mediaUrl: opts.mediaUrl,
|
mediaUrl: opts.mediaUrl,
|
||||||
gifPlayback: opts.gifPlayback,
|
|
||||||
idempotencyKey,
|
idempotencyKey,
|
||||||
});
|
});
|
||||||
defaultRuntime.log(JSON.stringify(result, null, 2));
|
defaultRuntime.log(JSON.stringify(result, null, 2));
|
||||||
|
|||||||
@@ -158,47 +158,4 @@ describe("nodes-cli coverage", () => {
|
|||||||
delivery: "overlay",
|
delivery: "overlay",
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("invokes location.get with params", async () => {
|
|
||||||
runtimeLogs.length = 0;
|
|
||||||
runtimeErrors.length = 0;
|
|
||||||
callGateway.mockClear();
|
|
||||||
|
|
||||||
const { registerNodesCli } = await import("./nodes-cli.js");
|
|
||||||
const program = new Command();
|
|
||||||
program.exitOverride();
|
|
||||||
registerNodesCli(program);
|
|
||||||
|
|
||||||
await program.parseAsync(
|
|
||||||
[
|
|
||||||
"nodes",
|
|
||||||
"location",
|
|
||||||
"get",
|
|
||||||
"--node",
|
|
||||||
"mac-1",
|
|
||||||
"--accuracy",
|
|
||||||
"precise",
|
|
||||||
"--max-age",
|
|
||||||
"1000",
|
|
||||||
"--location-timeout",
|
|
||||||
"5000",
|
|
||||||
"--invoke-timeout",
|
|
||||||
"6000",
|
|
||||||
],
|
|
||||||
{ from: "user" },
|
|
||||||
);
|
|
||||||
|
|
||||||
const invoke = callGateway.mock.calls.find(
|
|
||||||
(call) => call[0]?.method === "node.invoke",
|
|
||||||
)?.[0];
|
|
||||||
|
|
||||||
expect(invoke).toBeTruthy();
|
|
||||||
expect(invoke?.params?.command).toBe("location.get");
|
|
||||||
expect(invoke?.params?.params).toEqual({
|
|
||||||
maxAgeMs: 1000,
|
|
||||||
desiredAccuracy: "precise",
|
|
||||||
timeoutMs: 5000,
|
|
||||||
});
|
|
||||||
expect(invoke?.params?.timeoutMs).toBe(6000);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
Loaded 100 of 142 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user